diff --git a/lib/api/source/fetch.php b/lib/api/source/fetch.php index 2f6eab3..2a217a6 100755 --- a/lib/api/source/fetch.php +++ b/lib/api/source/fetch.php @@ -6,17 +6,81 @@ /* [1] Fetch & generate useful data from features =========================================================*/ - /* (1) Default data : access */ + /* (1) Initialize dataset + ---------------------------------------------------------*/ $data = [ - 'access' => file_get_contents(DATA_DIR.'/access.dat'), + 'default' => [], 'feature' => [] ]; - /* (2) Fetch for each feature which generates data */ + + /* (2) Fetch DEFAULT data log + ---------------------------------------------------------*/ + $f = new SplFileObject(DEFAULT_DATA, 'r'); + + while( !$f->eof() ){ + + /* (1) Try to parse each line */ + $parsed = json_decode($f->fgets(), true); + + /* (2) Manage error */ + if( is_null($parsed) ) + continue; + + /* (3) Add entry to log */ + $data['default'][] = $parsed; + + } + + + + /* (3) Fetch for each feature which generates data + ---------------------------------------------------------*/ + foreach(scandir(SOURCE_DIR.'/feature') as $feat_name){ + + /* (1) Ignore . and .. entries */ + if( in_array($feat_name, ['.', '..']) ) + continue; + + /* (2) Get absolute path */ + $abs_path = SOURCE_DIR."/feature/$feat_name"; + + /* (3) Check if it is a directory */ + if( !file_exists($abs_path) || !is_dir($abs_path) ) + continue; + + /* (4) Store the feature entry (empty) */ + $data['feature'][$feat_name] = []; + + /* (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(['a'=>1]); + return json_encode($data); } echo api_fetch();