This commit is contained in:
xdrm-brackets 2017-02-23 16:11:52 +01:00
parent 9f7da3f040
commit 7e70ee4ec8
1 changed files with 43 additions and 16 deletions

View File

@ -5,11 +5,10 @@
require_once __DIR__.'/../../include/php/const'; require_once __DIR__.'/../../include/php/const';
$f_auth; $f_auth;
$f_gstate;
$f_accesslog; $f_accesslog;
$f_actions; $f_actions;
$state = null;
$actions = []; $actions = [];
$last_user = null; $last_user = null;
@ -20,6 +19,32 @@
/* RETURN CURRENT GLOBAL STATE
*
* @return gstate<string> Current global state as string
*
*/
function get_gstate(){
/* (1) Export global file descriptor */
global $g_gstate;
/* (2) Read first line */
$f_gstate->seek(0);
$state = $f_gstate->fgets();
$state = preg_replace('@^\s+@', '', $state);
$state = preg_replace('@\s+$@', '', $state);
/* (3) Check data */
if( strlen($state) < 1 )
return false;
/* (4) Return global state */
return $state;
}
/* RETURNS THE PERMISSIONS FOR A SPECIFIC CODE AND AN ACTION /* RETURNS THE PERMISSIONS FOR A SPECIFIC CODE AND AN ACTION
* *
@ -80,11 +105,14 @@
global $timeout; global $timeout;
/* (2) Caches */ /* (2) Caches */
global $actions, $state; global $actions;
/* (3) Log history file descriptor */ /* (3) Log history file descriptor */
global $f_accesslog; global $f_accesslog;
/* (5) Update global state */
$state = get_gstate();
/* [2] Manage timeout /* [2] Manage timeout
=========================================================*/ =========================================================*/
@ -129,7 +157,7 @@
$action = $actionlist[$id_action]; $action = $actionlist[$id_action];
/* (4) Check if the state allows the action to be performed */ /* (4) Check if the state allows the action to be performed */
for( $c = 0 ; $c < count($state) || $c < strlen($action['prev']) ; $c++ ){ for( $c = 0 ; $c < strlen($state) || $c < strlen($action['prev']) ; $c++ ){
// {1} 'X' = any state -> so ignore // // {1} 'X' = any state -> so ignore //
if( $action['prev'][$c] == 'X' ) if( $action['prev'][$c] == 'X' )
continue; continue;
@ -160,7 +188,7 @@
/* [5] Process the action on the STATE /* [5] Process the action on the STATE
=========================================================*/ =========================================================*/
/* (1) Update the state with the found action */ /* (1) Update the state with the found action */
for( $c = 0 ; $c < count($state) || $c < strlen($action['next']) ; $c++ ){ for( $c = 0 ; $c < strlen($state) || $c < strlen($action['next']) ; $c++ ){
// {1} If 'x' -> let the current state // // {1} If 'x' -> let the current state //
if( $action['next'][$c] == 'X' ) if( $action['next'][$c] == 'X' )
@ -174,7 +202,7 @@
/* (2) Update the state file */ /* (2) Update the state file */
$written = @file_put_contents(STATE_CONF, implode('', $state)); $written = @file_put_contents(STATE_CONF, $state);
/* (3) Manage error */ /* (3) Manage error */
if( $written === false ) if( $written === false )
@ -213,10 +241,10 @@
/* [0] Initialize global variables /* [0] Initialize global variables
=========================================================*/ =========================================================*/
/* (1) File descriptiors */ /* (1) File descriptiors */
global $f_auth, $f_accesslog; global $f_auth, $f_accesslog, $f_gstate;
/* (2) Caches */ /* (2) Caches */
global $actions, $state; global $actions;
@ -225,7 +253,7 @@
/* (1) Read accesses */ /* (1) Read accesses */
$f_auth = new SplFileObject(AUTH_CONF, 'r'); $f_auth = new SplFileObject(AUTH_CONF, 'r');
$f_actions = new SplFileObject(ACTIONS_CONF, 'r'); $f_actions = new SplFileObject(ACTIONS_CONF, 'r');
$f_gstate = @file_get_contents(STATE_CONF); $f_gstate = new SplFileObject(STATE_CONF, 'r');
/* (2) Append accesses (logs) */ /* (2) Append accesses (logs) */
$f_accesslog = new SplFileObject(DEFAULT_DATA, 'a'); $f_accesslog = new SplFileObject(DEFAULT_DATA, 'a');
@ -261,9 +289,11 @@
/* [3] Cache global state /* [3] Check global state
=========================================================*/ =========================================================*/
/* (1) Check file */ /* (1) Check file */
$gstate = $f_gstate->current();
if( $f_gstate === false ) if( $f_gstate === false )
return 127; return 127;
@ -271,11 +301,8 @@
$f_gstate = preg_replace('@^\s+@', '', $f_gstate); $f_gstate = preg_replace('@^\s+@', '', $f_gstate);
$f_gstate = preg_replace('@\s+$@', '', $f_gstate); $f_gstate = preg_replace('@\s+$@', '', $f_gstate);
/* (3) For each character create an entry */ /* (3) Manage error */
$state = str_split($f_gstate); if( strlen($state) < 1 )
/* (4) Manage error */
if( is_null($state) )
return 127; return 127;
return 0; return 0;
@ -294,7 +321,7 @@
global $timeout; global $timeout;
/* (2) Caches */ /* (2) Caches */
global $actions, $state; global $actions;