SATS/lib/api/source/fetch.php

81 lines
1.6 KiB
PHP
Executable File

<?php
require_once __DIR__.'/../../include/php/const';
function api_fetch(){
$data = [];
/* [1] Get the list of daemons (features)
=========================================================*/
$f = new SplFileObject(ETREE_CONF, 'r');
while( !$f->eof() ){
/* (1) Try to parse each line */
$etree = trim($f->fgets(), "\n");
/* (2) Manage error */
if( is_null($etree) || strlen($etree) == 0 )
continue;
/* (3) Add entry to log */
$data['feature'][$etree] = [];
}
$f = null;
/* [2] Fetch & generate useful data from features
=========================================================*/
/* (1) Get identity
---------------------------------------------------------*/
$data['identity'] = [
'ap' => syscall(SOURCE_DIR.'/lib/netid/ap'),
'ip' => syscall(SOURCE_DIR.'/lib/netid/ip')
];
/* (2) Fetch for each feature which generates data
---------------------------------------------------------*/
foreach($data['feature'] as $feat_name=>$feat_data){
/* (5) Check for feature access log */
if( !file_exists(DATA_DIR."/$feat_name") )
continue;
/* (6) Try to read log file */
$f = new SplFileObject(DATA_DIR."/$feat_name", "r");
while( !$f->eof() ){
// {1} Try to parse JSON //
$parsed = json_decode($f->fgets(), true);
// {2} Manage error //
if( is_null($parsed) )
continue;
// {3} If correct -> add entry to feature //
$data['feature'][$feat_name][] = $parsed;
}
/* (7) Free file handler */
$f = null;
}
return json_encode($data);
}
echo api_fetch();
?>