Research Labs

2026-09-17 21:32 UTCopen field data · live

api.php

Snapshot 2026.09.17-a2894de (current) · 40 lines · 2,029 bytes · tree · plain text

SHA-256 951a77bb8329fbfa2009e20f2cd3893ec96abc336d78dd472babd294ee856a86
Functions: out L4-L40
1<?php
2require_once __DIR__.'/inc/helpers.php';
3header('Content-Type: application/json; charset=utf-8');
4function out($ok,$data,$mode='n/a'){
5 echo json_encode(['ok'=>$ok,'data_mode'=>$mode,
6 'licence'=>'Open data licence, attribution required',
7 'citation'=>'Districthive Research Labs · The Skerðingsstaðir Experiment',
8 'data'=>$data]); exit;
9}
10$key=$_GET['key']??'';
11$kh=hash('sha256',$key);
12$row=$key?db()->prepare('SELECT id FROM api_keys WHERE key_hash=? AND active=1'):null;
13if($row){$row->execute([$kh]);$row=$row->fetch();}
14if(!$row){ http_response_code(401); out(false,['error'=>'valid key required; request one via the About page']); }
15db()->prepare('UPDATE api_keys SET last_used_at=NOW() WHERE key_hash=?')->execute([$kh]);
16log_activity('api',$_GET['action']??'?', substr(($_GET['stream']??''),0,40));
17
18$action=$_GET['action']??'streams';
19if($action==='streams'){
20 $q=db()->query("SELECT s.code,s.label,s.unit,s.mode,s.visibility,i.product,i.cadence_min FROM streams s JOIN instruments i ON i.id=s.instrument_id WHERE s.visibility IN ('public','keyed')");
21 out(true,$q->fetchAll());
22}
23if($action==='readings'){
24 $s=stream_by_code(preg_replace('/[^a-z0-9_]/','',$_GET['stream']??''));
25 if(!$s||$s['visibility']==='hidden') out(false,['error'=>'unknown or restricted stream']);
26 $from=($_GET['from']??date('Y-m-d',strtotime('-7 days'))).' 00:00:00';
27 $to=($_GET['to']??date('Y-m-d')).' 23:59:59';
28 out(true,array_map(fn($r)=>[$r['ts'],(float)$r['value']],series($s,$from,$to,2000)),$s['mode']);
29}
30if($action==='events'){
31 $cls=preg_replace('/[^a-z]/','',$_GET['class']??'');
32 $sql="SELECT ts,class,detail FROM events WHERE is_public=1".($cls?" AND class=".db()->quote($cls):"")." ORDER BY ts DESC LIMIT 500";
33 out(true,db()->query($sql)->fetchAll());
34}
35if($action==='checksums'){
36 $p=$_GET['path']??''; $q=db()->prepare('SELECT path,sha256,bytes,pulled_at FROM files_raw WHERE path=?');
37 $q->execute([$p]); out(true,$q->fetch()?:['error'=>'not found']);
38}
39out(false,['error'=>'unknown action']);
40

Lines can be cited as api.php L120-L140, platform code 2026.09.17-a2894de; add #L120-L140 to this page's address to highlight them. Source shown for scrutiny of the research platform. All rights reserved. Not offered as installable software.