2017-01-30 10:41:25 +00:00
|
|
|
#!/usr/bin/php
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__.'/../../include/php/const';
|
|
|
|
|
2017-02-21 17:50:37 +00:00
|
|
|
$f_auth;
|
2017-01-30 10:41:25 +00:00
|
|
|
$f_accesslog;
|
2017-02-21 17:50:37 +00:00
|
|
|
$f_states;
|
|
|
|
$f_actions;
|
|
|
|
|
|
|
|
$state = [];
|
|
|
|
|
|
|
|
$actions = [];
|
|
|
|
$states = [];
|
|
|
|
|
|
|
|
$last_user = null;
|
|
|
|
$timeout = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETURNS THE PERMISSIONS FOR A SPECIFIC CODE AND AN ACTION
|
|
|
|
*
|
|
|
|
* @code<string> Some RFID code
|
|
|
|
*
|
|
|
|
* @return user<array> User info if found ; else NULL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function auth($code){
|
|
|
|
|
|
|
|
global $f_auth;
|
|
|
|
|
|
|
|
|
|
|
|
/* (1) Goto first line */
|
|
|
|
$f_auth->seek(0);
|
|
|
|
|
|
|
|
/* (2) Parse each line */
|
|
|
|
while( $f_auth->eof() ){
|
|
|
|
|
|
|
|
/* (3) Try to parse line */
|
|
|
|
$parsed = json_decode($f_auth->fgets(), true);
|
|
|
|
|
|
|
|
/* (3) Ignore if parse error */
|
|
|
|
if( is_null($parsed) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* (4) If wrong code -> go to next */
|
|
|
|
if( $parsed[0] != $code )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* (5) return user info */
|
|
|
|
return [
|
|
|
|
'id' => $parsed[1],
|
|
|
|
'can' => $parsed[2]
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (6) Return FALSE if not found */
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-30 10:41:25 +00:00
|
|
|
|
2017-02-18 10:09:49 +00:00
|
|
|
|
2017-01-30 10:41:25 +00:00
|
|
|
|
|
|
|
function mfrc522_setup(){
|
|
|
|
|
2017-02-21 17:50:37 +00:00
|
|
|
/* [0] Initialize global variables
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) File descriptiors */
|
|
|
|
global $f_auth, $f_accesslog;
|
|
|
|
|
|
|
|
/* (2) Caches */
|
|
|
|
global $actions, $states, $state;
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-30 10:41:25 +00:00
|
|
|
/* [1] Open file descriptors on useful files
|
|
|
|
=========================================================*/
|
2017-02-21 17:50:37 +00:00
|
|
|
/* (1) Read accesses */
|
|
|
|
$f_auth = new SplFileObject(AUTH_LIST, 'r');
|
|
|
|
$f_states = new SplFileObject(STATES_CONF, 'r');
|
|
|
|
$f_actions = new SplFileObject(ACTIONS_CONF, 'r');
|
|
|
|
$f_gstate = @file_get_contents(STATE_CONF);
|
|
|
|
|
|
|
|
/* (2) Append accesses (logs) */
|
2017-01-30 10:41:25 +00:00
|
|
|
$f_accesslog = new SplFileObject(ACCESS_LOG, 'a');
|
|
|
|
|
2017-02-21 17:50:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] Parse ACTIONS and cache them
|
2017-01-30 10:41:25 +00:00
|
|
|
=========================================================*/
|
2017-02-21 17:50:37 +00:00
|
|
|
/* (1) Parse each line */
|
|
|
|
while( $f_actions->eof() ){
|
|
|
|
|
|
|
|
/* (2) Try to parse line */
|
|
|
|
$parsed = json_decode($f_actions->fgets(), true);
|
|
|
|
|
|
|
|
/* (2) Ignore if parse error */
|
|
|
|
if( is_null($parsed) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* (3) Add key (timeout) to cache */
|
|
|
|
if( !isset($actions[$parsed[0]] )
|
|
|
|
$actions[$parsed[0]] = [];
|
2017-01-30 10:41:25 +00:00
|
|
|
|
2017-02-21 17:50:37 +00:00
|
|
|
/* (4) Add entry to cache */
|
|
|
|
$actions[$parsed[0]][] = [
|
|
|
|
'id' => $parsed[1],
|
|
|
|
'prev' => $parsed[2],
|
|
|
|
'next' => $parsed[3],
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Free file descriptor */
|
|
|
|
$f_actions = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Parse STATES and cache them
|
2017-02-18 10:09:49 +00:00
|
|
|
=========================================================*/
|
2017-02-21 17:50:37 +00:00
|
|
|
/* (1) Parse each line */
|
|
|
|
while( $f_states->eof() ){
|
|
|
|
|
|
|
|
/* (2) Try to parse line */
|
|
|
|
$parsed = json_decode($f_states->fgets(), true);
|
|
|
|
|
|
|
|
/* (2) Ignore if parse error */
|
|
|
|
if( is_null($parsed) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* (3) Add entry to cache */
|
|
|
|
$states[] = $parsed;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Free file descriptor */
|
|
|
|
$f_states = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] Cache global state
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Check file */
|
|
|
|
if( $f_gstate === false )
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* (2) Remove external spaces */
|
|
|
|
$f_gstate = preg_replace('@^\s+@', '', $f_gstate);
|
|
|
|
$f_gstate = preg_replace('@\s+$@', '', $f_gstate);
|
|
|
|
|
|
|
|
/* (3) For each character create an entry */
|
|
|
|
for( $c = 0 ; $c < strlen($f_gstate) ; $c++ )
|
|
|
|
$state[] = $c;
|
|
|
|
|
2017-02-18 10:09:49 +00:00
|
|
|
|
2017-01-30 10:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function mfrc522_loop(){
|
|
|
|
|
2017-02-21 17:50:37 +00:00
|
|
|
/* [1] Load global variables
|
|
|
|
=========================================================*/
|
|
|
|
global $last_user;
|
|
|
|
global $timeout;
|
|
|
|
|
2017-01-30 10:41:25 +00:00
|
|
|
/* [1] Wait for rfid card
|
|
|
|
=========================================================*/
|
2017-02-18 10:09:49 +00:00
|
|
|
while( ($code=syscall(SOURCE_DIR.'/lib/mfrc522/read')) === false );
|
|
|
|
|
2017-02-17 15:47:19 +00:00
|
|
|
$code = strtoupper($code);
|
|
|
|
|
|
|
|
slog("card '$code' read", 'mfrc522:read');
|
2017-01-30 10:41:25 +00:00
|
|
|
|
|
|
|
$start_ts = microtime(true);
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Check for user in auth list
|
|
|
|
=========================================================*/
|
2017-02-21 17:50:37 +00:00
|
|
|
/* (0) CHECK FOR USER INFO IN AUTH LIST
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
$user = auth($code);
|
|
|
|
|
|
|
|
|
|
|
|
/* (1) UNKNOWN USER -> reset count
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( is_null($user) ){
|
|
|
|
|
|
|
|
$last_user = null;
|
|
|
|
$timeout = 1;
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) KNOWN USER -> Manage timeout
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
}else{
|
|
|
|
|
|
|
|
/* (1) If same as last -> increment @timeout */
|
|
|
|
if( $last_user == $user['id'] )
|
|
|
|
$timeout++;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2017-01-30 10:41:25 +00:00
|
|
|
|
|
|
|
|
2017-02-20 20:22:42 +00:00
|
|
|
|
2017-01-30 10:41:25 +00:00
|
|
|
|
|
|
|
/* [n] Wait for 0.5 s before reading again
|
|
|
|
=========================================================*/
|
|
|
|
while( microtime(true)-$start_ts < 0.5 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
slog('daemon started (loop)', 'mfrc522:loop');
|
|
|
|
|
|
|
|
while( true )
|
2017-02-18 10:09:49 +00:00
|
|
|
mfrc522_loop();
|
2017-01-30 10:41:25 +00:00
|
|
|
?>
|
|
|
|
|