diff --git a/4dev/tests/CoreLibsACLLoginTest.php b/4dev/tests/CoreLibsACLLoginTest.php index 42c692be..919f2aff 100644 --- a/4dev/tests/CoreLibsACLLoginTest.php +++ b/4dev/tests/CoreLibsACLLoginTest.php @@ -15,6 +15,7 @@ final class CoreLibsACLLoginTest extends TestCase { private static $db; private static $log; + private static $session; /** * start DB conneciton, setup DB, etc @@ -28,6 +29,8 @@ final class CoreLibsACLLoginTest extends TestCase 'The PgSQL extension is not available.' ); } + // init session + self::$session = new \CoreLibs\Create\Session('ACLLoginTest'); // logger is always needed // define basic connection set valid and one invalid self::$log = new \CoreLibs\Debug\Logging([ @@ -96,7 +99,7 @@ final class CoreLibsACLLoginTest extends TestCase 'ACL\Login Tests have not yet been implemented' ); - $login = new \CoreLibs\ACL\Login(self::$db, self::$log); + $login = new \CoreLibs\ACL\Login(self::$db, self::$log, self::$session); } } diff --git a/4dev/tests/CoreLibsCreateSessionTest.php b/4dev/tests/CoreLibsCreateSessionTest.php index 066e470e..be419e82 100644 --- a/4dev/tests/CoreLibsCreateSessionTest.php +++ b/4dev/tests/CoreLibsCreateSessionTest.php @@ -37,10 +37,10 @@ final class CoreLibsCreateSessionTest extends TestCase 'sessionNameGlobals', '/^\w+$/' ], - 'session constant' => [ - 'sessionNameConstant', - 'c', - 'sessionNameConstant', + 'session name default' => [ + '', + 'd', + '', '/^\w+$/' ], ]; @@ -64,19 +64,22 @@ final class CoreLibsCreateSessionTest extends TestCase $expected_n, $expected_i ): void { - // NEEDS MOCKING - /* $session_id = ''; + /* + // MOCK class for dummy call + $session = new \CoreLibs\Create\Session(); + $session_id = ''; + unset($GLOBALS['SET_SESSION_NAME']); switch ($type) { case 'p': - $session_id = \CoreLibs\Create\Session::startSession($input); + $session_id = $session->startSession($input); break; case 'g': $GLOBALS['SET_SESSION_NAME'] = $input; - $session_id = \CoreLibs\Create\Session::startSession(); + $session_id = $session->startSession(); break; - case 'c': - define('SET_SESSION_NAME', $input); - $session_id = \CoreLibs\Create\Session::startSession(); + case 'd': + $expected_n = ini_get('session.name'); + $session_id = \$session->startSession(); break; } $this->assertMatchesRegularExpression( @@ -85,11 +88,11 @@ final class CoreLibsCreateSessionTest extends TestCase ); $this->assertMatchesRegularExpression( $expected_i, - (string)\CoreLibs\Create\Session::getSessionId() + (string)$session->getSessionId() ); $this->assertEquals( $expected_n, - \CoreLibs\Create\Session::getSessionName() + $session->getSessionName() ); if ($type == 'g') { unset($GLOBALS['SET_SESSION_NAME']); diff --git a/www/admin/class_test.admin.backend.php b/www/admin/class_test.admin.backend.php index 0af630fd..f3820459 100644 --- a/www/admin/class_test.admin.backend.php +++ b/www/admin/class_test.admin.backend.php @@ -23,14 +23,12 @@ define('USE_DATABASE', true); require 'config.php'; // override ECHO ALL FALSE $ECHO_ALL = true; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-admin'; +$SET_SESSION_NAME = EDIT_SESSION_NAME; ob_end_flush(); +$session = new CoreLibs\Create\Session($SET_SESSION_NAME); $log = new CoreLibs\Debug\Logging([ 'log_folder' => BASE . LOG, 'file_id' => $LOG_FILE_ID, @@ -49,7 +47,7 @@ $l10n = new \CoreLibs\Language\L10n( $locale['domain'], $locale['path'], ); -$backend = new CoreLibs\Admin\Backend($db, $log, $l10n, $locale); +$backend = new CoreLibs\Admin\Backend($db, $log, $session, $l10n, $locale); $PAGE_NAME = 'TEST CLASS: ADMIN BACKEND'; print ""; diff --git a/www/admin/class_test.array.php b/www/admin/class_test.array.php index 111c839d..3f058470 100644 --- a/www/admin/class_test.array.php +++ b/www/admin/class_test.array.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-array'; ob_end_flush(); diff --git a/www/admin/class_test.autoloader.php b/www/admin/class_test.autoloader.php index 8012a352..24d85cf7 100644 --- a/www/admin/class_test.autoloader.php +++ b/www/admin/class_test.autoloader.php @@ -20,10 +20,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-autoloader'; ob_end_flush(); diff --git a/www/admin/class_test.byte.php b/www/admin/class_test.byte.php index 01b0c6a6..573a91d8 100644 --- a/www/admin/class_test.byte.php +++ b/www/admin/class_test.byte.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-byte'; ob_end_flush(); diff --git a/www/admin/class_test.colors.php b/www/admin/class_test.colors.php index 9d8b6bec..a66a0783 100644 --- a/www/admin/class_test.colors.php +++ b/www/admin/class_test.colors.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-colors'; ob_end_flush(); diff --git a/www/admin/class_test.datetime.php b/www/admin/class_test.datetime.php index e88af2b3..cf07f2c7 100644 --- a/www/admin/class_test.datetime.php +++ b/www/admin/class_test.datetime.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-datetime'; ob_end_flush(); diff --git a/www/admin/class_test.email.php b/www/admin/class_test.email.php index aae7a4d2..9652ffe7 100644 --- a/www/admin/class_test.email.php +++ b/www/admin/class_test.email.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-email'; ob_end_flush(); diff --git a/www/admin/class_test.encoding.php b/www/admin/class_test.encoding.php index e7cd6884..5cb34e16 100644 --- a/www/admin/class_test.encoding.php +++ b/www/admin/class_test.encoding.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-encoding'; ob_end_flush(); diff --git a/www/admin/class_test.file.php b/www/admin/class_test.file.php index 4ff3af84..4cb0b0a8 100644 --- a/www/admin/class_test.file.php +++ b/www/admin/class_test.file.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-datetime'; ob_end_flush(); diff --git a/www/admin/class_test.hash.php b/www/admin/class_test.hash.php index 2bd4dfe0..5e26c130 100644 --- a/www/admin/class_test.hash.php +++ b/www/admin/class_test.hash.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-hash'; ob_end_flush(); diff --git a/www/admin/class_test.html.php b/www/admin/class_test.html.php index e15318b5..e009ce23 100644 --- a/www/admin/class_test.html.php +++ b/www/admin/class_test.html.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-html'; ob_end_flush(); diff --git a/www/admin/class_test.image.php b/www/admin/class_test.image.php index 903504da..170105c6 100644 --- a/www/admin/class_test.image.php +++ b/www/admin/class_test.image.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-image'; ob_end_flush(); diff --git a/www/admin/class_test.json.php b/www/admin/class_test.json.php index 279fc8f9..10cb559a 100644 --- a/www/admin/class_test.json.php +++ b/www/admin/class_test.json.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-json'; ob_end_flush(); diff --git a/www/admin/class_test.lang.php b/www/admin/class_test.lang.php index a84ee41f..3381d76b 100644 --- a/www/admin/class_test.lang.php +++ b/www/admin/class_test.lang.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-lang'; ob_end_flush(); diff --git a/www/admin/class_test.login.php b/www/admin/class_test.login.php index ea71f836..9dae22ac 100644 --- a/www/admin/class_test.login.php +++ b/www/admin/class_test.login.php @@ -21,13 +21,11 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-login'; +$SET_SESSION_NAME = EDIT_SESSION_NAME; // init login & backend class +$session = new CoreLibs\Create\Session($SET_SESSION_NAME); $log = new CoreLibs\Debug\Logging([ 'log_folder' => BASE . LOG, 'file_id' => $LOG_FILE_ID, @@ -39,7 +37,7 @@ $log = new CoreLibs\Debug\Logging([ 'print_all' => $PRINT_ALL ?? false, ]); $db = new CoreLibs\DB\IO(DB_CONFIG, $log); -$login = new CoreLibs\ACL\Login($db, $log); +$login = new CoreLibs\ACL\Login($db, $log, $session); ob_end_flush(); $PAGE_NAME = 'TEST CLASS: LOGIN'; diff --git a/www/admin/class_test.math.php b/www/admin/class_test.math.php index 2ac6cd61..b84c504c 100644 --- a/www/admin/class_test.math.php +++ b/www/admin/class_test.math.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-math'; ob_end_flush(); diff --git a/www/admin/class_test.mime.php b/www/admin/class_test.mime.php index 3c237e24..4bd2598d 100644 --- a/www/admin/class_test.mime.php +++ b/www/admin/class_test.mime.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-mime'; ob_end_flush(); diff --git a/www/admin/class_test.output.form.php b/www/admin/class_test.output.form.php index 64d657f7..27f314d6 100644 --- a/www/admin/class_test.output.form.php +++ b/www/admin/class_test.output.form.php @@ -23,10 +23,6 @@ define('USE_DATABASE', true); require 'config.php'; // override ECHO ALL FALSE $ECHO_ALL = true; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-form'; ob_end_flush(); diff --git a/www/admin/class_test.password.php b/www/admin/class_test.password.php index af8a81f1..891a9838 100644 --- a/www/admin/class_test.password.php +++ b/www/admin/class_test.password.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-pass'; ob_end_flush(); diff --git a/www/admin/class_test.php b/www/admin/class_test.php index c3c7687e..37be6278 100644 --- a/www/admin/class_test.php +++ b/www/admin/class_test.php @@ -21,14 +21,12 @@ ob_start(); define('USE_DATABASE', true); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest'; +$SET_SESSION_NAME = EDIT_SESSION_NAME; // init login & backend class +$session = new CoreLibs\Create\Session($SET_SESSION_NAME); $log = new CoreLibs\Debug\Logging([ 'log_folder' => BASE . LOG, 'file_id' => $LOG_FILE_ID, @@ -40,14 +38,14 @@ $log = new CoreLibs\Debug\Logging([ 'print_all' => $PRINT_ALL ?? false, ]); $db = new CoreLibs\DB\IO(DB_CONFIG, $log); -$login = new CoreLibs\ACL\Login($db, $log); +$login = new CoreLibs\ACL\Login($db, $log, $session); $locale = \CoreLibs\Language\GetLocale::setLocale(); $l10n = new \CoreLibs\Language\L10n( $locale['locale'], $locale['domain'], $locale['path'], ); -$backend = new CoreLibs\Admin\Backend($db, $log, $l10n, $locale); +$backend = new CoreLibs\Admin\Backend($db, $log, $session, $l10n, $locale); $backend->db->dbInfo(true); ob_end_flush(); diff --git a/www/admin/class_test.phpv.php b/www/admin/class_test.phpv.php index dba43dd1..79027cdd 100644 --- a/www/admin/class_test.phpv.php +++ b/www/admin/class_test.phpv.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-phpv'; ob_end_flush(); diff --git a/www/admin/class_test.randomkey.php b/www/admin/class_test.randomkey.php index 3e82e0f8..ceec4542 100644 --- a/www/admin/class_test.randomkey.php +++ b/www/admin/class_test.randomkey.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-randomkey'; ob_end_flush(); diff --git a/www/admin/class_test.readenvfile.php b/www/admin/class_test.readenvfile.php index 7cdc7e37..ee5b277e 100644 --- a/www/admin/class_test.readenvfile.php +++ b/www/admin/class_test.readenvfile.php @@ -14,10 +14,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-readEnvFile'; ob_end_flush(); diff --git a/www/admin/class_test.runningtime.php b/www/admin/class_test.runningtime.php index 6a086a0a..7d33d272 100644 --- a/www/admin/class_test.runningtime.php +++ b/www/admin/class_test.runningtime.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-runningtime'; ob_end_flush(); diff --git a/www/admin/class_test.session.php b/www/admin/class_test.session.php index 6c1256aa..b65dc10c 100644 --- a/www/admin/class_test.session.php +++ b/www/admin/class_test.session.php @@ -43,10 +43,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -// if (!defined('SET_SESSION_NAME')) { -// define('SET_SESSION_NAME', EDIT_SESSION_NAME); -// } // define log file id $LOG_FILE_ID = 'classTest-session'; ob_end_flush(); @@ -62,6 +58,7 @@ $log = new CoreLibs\Debug\Logging([ 'print_all' => $PRINT_ALL ?? false, ]); use CoreLibs\Create\Session; +$session = new Session(); $PAGE_NAME = 'TEST CLASS: SESSION'; print ""; @@ -76,15 +73,15 @@ $value = 'bar'; foreach (['123', '123-123', '123abc'] as $_session_name) { print "[UNSET] Session Name valid for " . $_session_name . ": " - . (Session::checkValidSessionName($_session_name) ? 'Valid' : 'Invalid') . "
"; + . ($session->checkValidSessionName($_session_name) ? 'Valid' : 'Invalid') . "
"; } echo "Global session name: " . ($GLOBALS['SET_SESSION_NAME'] ?? '-') . "
"; -print "[UNSET] Current session id: " . Session::getSessionId() . "
"; -print "[UNSET] Current session name: " . Session::getSessionName() . "
"; -print "[UNSET] Current session active: " . (Session::checkActiveSession() ? 'Yes' : 'No') . "
"; -print "[UNSET] Current session status: " . getSessionStatusString(Session::getSessionStatus()) . "
"; +print "[UNSET] Current session id: " . $session->getSessionId() . "
"; +print "[UNSET] Current session name: " . $session->getSessionName() . "
"; +print "[UNSET] Current session active: " . ($session->checkActiveSession() ? 'Yes' : 'No') . "
"; +print "[UNSET] Current session status: " . getSessionStatusString($session->getSessionStatus()) . "
"; if (isset($_SESSION)) { print "[UNSET] _SESSION is: set
"; } else { @@ -92,22 +89,22 @@ if (isset($_SESSION)) { } # print "[UNSET] To set session name valid: " - . (Session::checkValidSessionName($session_name) ? 'Valid' : 'Invalid') . "
"; -if (false === ($session = Session::startSession($session_name))) { - print "[FAILED] Session start failed: " . Session::getErrorStr() . "
"; + . ($session->checkValidSessionName($session_name) ? 'Valid' : 'Invalid') . "
"; +if (false === ($session_id = $session->startSession($session_name))) { + print "[FAILED] Session start failed: " . $session->getErrorStr() . "
"; } else { - print "[SET] Current session id: " . $session . "
"; + print "[SET] Current session id: " . $session_id . "
"; } // set again -if (false === ($session = Session::startSession($session_name))) { - print "[2 FAILED] Session start failed: " . Session::getErrorStr() . "
"; +if (false === ($session_id = $session->startSession($session_name))) { + print "[2 FAILED] Session start failed: " . $session->getErrorStr() . "
"; } else { - print "[2 SET] Current session id: " . $session . "
"; + print "[2 SET] Current session id: " . $session_id . "
"; } -print "[SET] Current session id: " . Session::getSessionId() . "
"; -print "[SET] Current session name: " . Session::getSessionName() . "
"; -print "[SET] Current session active: " . (Session::checkActiveSession() ? 'Yes' : 'No') . "
"; -print "[SET] Current session status: " . getSessionStatusString(Session::getSessionStatus()) . "
"; +print "[SET] Current session id: " . $session->getSessionId() . "
"; +print "[SET] Current session name: " . $session->getSessionName() . "
"; +print "[SET] Current session active: " . ($session->checkActiveSession() ? 'Yes' : 'No') . "
"; +print "[SET] Current session status: " . getSessionStatusString($session->getSessionStatus()) . "
"; if (isset($_SESSION)) { print "[SET] _SESSION is: set
"; } else { @@ -117,51 +114,51 @@ if (!isset($_SESSION['counter'])) { $_SESSION['counter'] = 0; } $_SESSION['counter']++; -print "[READ] " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "
"; +print "[READ] A " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "
"; $_SESSION[$var] = $value; -print "[READ] " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "
"; +print "[READ] B " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "
"; print "[READ] Confirm " . $var . " is " . $value . ": " . (($_SESSION[$var] ?? '') == $value ? 'Matching' : 'Not matching') . "
"; // differnt session name $session_name = 'class-test-session-ALT'; -if (false === ($session = Session::startSession($session_name))) { - print "[3 FAILED] Session start failed: " . Session::getErrorStr() . "
"; +if (false === ($session_id = $session->startSession($session_name))) { + print "[3 FAILED] Session start failed: " . $session->getErrorStr() . "
"; } else { - print "[3 SET] Current session id: " . $session . "
"; + print "[3 SET] Current session id: " . $session_id . "
"; } -print "[SET AGAIN] Current session id: " . Session::getSessionId() . "
"; +print "[SET AGAIN] Current session id: " . $session->getSessionId() . "
"; print "[ALL SESSION]: " . \CoreLibs\Debug\Support::printAr($_SESSION) . "
"; // close session -Session::writeClose(); +$session->writeClose(); // will never be written $_SESSION['will_never_be_written'] = 'empty'; // open again $session_name = 'class-test-session'; -if (false === ($session = Session::startSession($session_name))) { - print "[4 FAILED] Session start failed: " . Session::getErrorStr() . "
"; +if (false === ($session_id = $session->startSession($session_name))) { + print "[4 FAILED] Session start failed: " . $session->getErrorStr() . "
"; } else { - print "[4 SET] Current session id: " . $session . "
"; + print "[4 SET] Current session id: " . $session_id . "
"; } -print "[START AGAIN] Current session id: " . Session::getSessionId() . "
"; +print "[START AGAIN] Current session id: " . $session->getSessionId() . "
"; $_SESSION['will_be_written_again'] = 'Full'; // close session -Session::writeClose(); +$session->writeClose(); // invalid $session_name = '123'; -if (false === ($session = Session::startSession($session_name))) { - print "[5 FAILED] Session start failed: " . Session::getErrorStr() . "
"; +if (false === ($session_id = $session->startSession($session_name))) { + print "[5 FAILED] Session start failed: " . $session->getErrorStr() . "
"; } else { - print "[5 SET] Current session id: " . $session . "
"; + print "[5 SET] Current session id: " . $session_id . "
"; } -print "[BAD NAME] Current session id: " . Session::getSessionId() . "
"; -print "[BAD NAME] Current session name: " . Session::getSessionName() . "
"; -print "[BAD NAME] Current session active: " . (Session::checkActiveSession() ? 'Yes' : 'No') . "
"; -print "[BAD NAME] Current session status: " . getSessionStatusString(Session::getSessionStatus()) . "
"; +print "[BAD NAME] Current session id: " . $session->getSessionId() . "
"; +print "[BAD NAME] Current session name: " . $session->getSessionName() . "
"; +print "[BAD NAME] Current session active: " . ($session->checkActiveSession() ? 'Yes' : 'No') . "
"; +print "[BAD NAME] Current session status: " . getSessionStatusString($session->getSessionStatus()) . "
"; // error message print $log->printErrorMsg(); diff --git a/www/admin/class_test.session.read.php b/www/admin/class_test.session.read.php index 6a63b066..7af08e61 100644 --- a/www/admin/class_test.session.read.php +++ b/www/admin/class_test.session.read.php @@ -43,10 +43,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-session.read'; ob_end_flush(); @@ -62,6 +58,7 @@ $log = new CoreLibs\Debug\Logging([ 'print_all' => $PRINT_ALL ?? false, ]); use CoreLibs\Create\Session; +$session = new Session(); $PAGE_NAME = 'TEST CLASS: SESSION (READ)'; print ""; @@ -77,28 +74,28 @@ $value = 'bar'; echo "Global session name: " . ($GLOBALS['SET_SESSION_NAME'] ?? '-') . "
"; -print "[UNSET] Current session id: " . Session::getSessionId() . "
"; -print "[UNSET] Current session name: " . Session::getSessionName() . "
"; -print "[UNSET] Current session active: " . (Session::checkActiveSession() ? 'Yes' : 'No') . "
"; -print "[UNSET] Current session status: " . getSessionStatusString(Session::getSessionStatus()) . "
"; +print "[UNSET] Current session id: " . $session->getSessionId() . "
"; +print "[UNSET] Current session name: " . $session->getSessionName() . "
"; +print "[UNSET] Current session active: " . ($session->checkActiveSession() ? 'Yes' : 'No') . "
"; +print "[UNSET] Current session status: " . getSessionStatusString($session->getSessionStatus()) . "
"; print "[READ] " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "
"; // start -if (false === ($session = Session::startSession($session_name))) { - print "Session start failed: " . Session::getErrorStr() . "
"; +if (false === ($session_id = $session->startSession($session_name))) { + print "Session start failed: " . $session->getErrorStr() . "
"; } else { - print "Current session id: " . $session . "
"; + print "Current session id: " . $session_id . "
"; } // set again -if (false === ($session = Session::startSession($session_name))) { +if (false === ($session_id = $session->startSession($session_name))) { print "[2] Session start failed
"; } else { - print "[2] Current session id: " . $session . "
"; + print "[2] Current session id: " . $session_id . "
"; } -print "[SET] Current session id: " . Session::getSessionId() . "
"; -print "[SET] Current session name: " . Session::getSessionName() . "
"; -print "[SET] Current session active: " . (Session::checkActiveSession() ? 'Yes' : 'No') . "
"; -print "[SET] Current session status: " . getSessionStatusString(Session::getSessionStatus()) . "
"; +print "[SET] Current session id: " . $session->getSessionId() . "
"; +print "[SET] Current session name: " . $session->getSessionName() . "
"; +print "[SET] Current session active: " . ($session->checkActiveSession() ? 'Yes' : 'No') . "
"; +print "[SET] Current session status: " . getSessionStatusString($session->getSessionStatus()) . "
"; print "[READ] " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "
"; print "[READ] Confirm " . $var . " is " . $value . ": " . (($_SESSION[$var] ?? '') == $value ? 'Matching' : 'Not matching') . "
"; diff --git a/www/admin/class_test.smarty.php b/www/admin/class_test.smarty.php index f1c0c1ed..6a115d5b 100644 --- a/www/admin/class_test.smarty.php +++ b/www/admin/class_test.smarty.php @@ -23,10 +23,6 @@ define('USE_DATABASE', true); require 'config.php'; // override ECHO ALL FALSE $ECHO_ALL = true; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-smarty'; ob_end_flush(); diff --git a/www/admin/class_test.system.php b/www/admin/class_test.system.php index 8bc13190..c36687dc 100644 --- a/www/admin/class_test.system.php +++ b/www/admin/class_test.system.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-system'; ob_end_flush(); @@ -50,16 +46,23 @@ print ""; print '
Class Test Master
'; print '

' . $PAGE_NAME . '

'; +print "System::getHostName():
"; print "GETHOSTNAME: " . DgS::printAr(System::getHostName()) . "
"; +print "System::getPageName():
"; print "GETPAGENAME(0): " . System::getPageName() . "
"; print "GETPAGENAME(1): " . System::getPageName(System::NO_EXTENSION) . "
"; print "GETPAGENAME(2): " . System::getPageName(System::FULL_PATH) . "
"; +print "System::getPageNameArray():
"; print "GETPAGENAMEARRAY: " . \CoreLibs\Debug\Support::printAr(System::getPageNameArray()) . "
"; // seting errro codes file upload +print "System::fileUploadErrorMessage():
"; print "FILEUPLOADERRORMESSAGE(): " . System::fileUploadErrorMessage(-1) . "
"; print "FILEUPLOADERRORMESSAGE(UPLOAD_ERR_CANT_WRITE): " . System::fileUploadErrorMessage(UPLOAD_ERR_CANT_WRITE) . "
"; +print "System::checkCLI():
"; +print "Are we in an CLI: " . (System::checkCLI() ? 'Yes' : 'No') . "
"; + // error message print $log->printErrorMsg(); diff --git a/www/admin/class_test.token.php b/www/admin/class_test.token.php index 892ac7fb..577450be 100644 --- a/www/admin/class_test.token.php +++ b/www/admin/class_test.token.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-token'; ob_end_flush(); diff --git a/www/admin/class_test.uids.php b/www/admin/class_test.uids.php index 7803f4c3..ce9db01b 100644 --- a/www/admin/class_test.uids.php +++ b/www/admin/class_test.uids.php @@ -21,10 +21,6 @@ ob_start(); define('USE_DATABASE', false); // sample config require 'config.php'; -// set session name -if (!defined('SET_SESSION_NAME')) { - define('SET_SESSION_NAME', EDIT_SESSION_NAME); -} // define log file id $LOG_FILE_ID = 'classTest-uids'; ob_end_flush(); diff --git a/www/configs/config.master.php b/www/configs/config.master.php index 898817d8..1897bb18 100644 --- a/www/configs/config.master.php +++ b/www/configs/config.master.php @@ -150,8 +150,6 @@ define('SERVER_PATH_HASH', hash('crc32b', BASE)); define('EDIT_SESSION_NAME', BASE_NAME . 'Admin' . SERVER_NAME_HASH . SERVER_PATH_HASH); // frontend define('SESSION_NAME', BASE_NAME . SERVER_NAME_HASH . SERVER_PATH_HASH); -// SET_SESSION_NAME should be set in the header if a special session name is needed -define('SET_SESSION_NAME', SESSION_NAME); /************* CACHE/COMPILE IDS *************/ define('CACHE_ID', 'CACHE_' . BASE_NAME . '_' . SERVER_NAME_HASH); diff --git a/www/includes/admin_header.php b/www/includes/admin_header.php index 0ae033c6..98c4ba82 100644 --- a/www/includes/admin_header.php +++ b/www/includes/admin_header.php @@ -42,11 +42,11 @@ if (isset($_POST['action']) && $_POST['action'] != 'download_csv' && !$AJAX_PAGE if ($AJAX_PAGE && !$ZIP_STREAM) { header("Content-Type: application/json; charset=UTF-8"); } -// start session -CoreLibs\Create\Session::startSession(); //------------------------------ basic variable settings start //------------------------------ class init start +// start session +$session = new \CoreLibs\Create\Session($SET_SESSION_NAME); // create logger $log = new CoreLibs\Debug\Logging([ 'log_folder' => BASE . LOG, @@ -71,7 +71,7 @@ if ( // db config with logger $db = new CoreLibs\DB\IO(DB_CONFIG, $log); // login & page access check -$login = new CoreLibs\ACL\Login($db, $log); +$login = new CoreLibs\ACL\Login($db, $log, $session); // lang, path, domain // pre auto detect language after login $locale = \CoreLibs\Language\GetLocale::setLocale(); @@ -84,7 +84,7 @@ $l10n = new \CoreLibs\Language\L10n( // create smarty object $smarty = new CoreLibs\Template\SmartyExtend($l10n, $locale); // create new Backend class with db and loger attached -$cms = new CoreLibs\Admin\Backend($db, $log, $l10n, $locale); +$cms = new CoreLibs\Admin\Backend($db, $log, $session, $l10n, $locale); // the menu show flag (what menu to show) $cms->menu_show_flag = 'main'; // db info diff --git a/www/includes/edit_base.php b/www/includes/edit_base.php index 2e5bbf7b..46c7d394 100644 --- a/www/includes/edit_base.php +++ b/www/includes/edit_base.php @@ -33,8 +33,6 @@ extract($_POST, EXTR_SKIP); ob_start(); require 'config.php'; -// set session name here -// $SET_SESSION_NAME = EDIT_SESSION_NAME; // overrride debug flags if (!DEBUG) { $DEBUG_ALL = false; @@ -45,8 +43,8 @@ if (!DEBUG) { // should be utf8 header("Content-type: text/html; charset=" . DEFAULT_ENCODING); -// set session -\CoreLibs\Create\Session::startSession(EDIT_SESSION_NAME); +// start session +$session = new \CoreLibs\Create\Session(EDIT_SESSION_NAME); // init logger $log = new CoreLibs\Debug\Logging([ 'log_folder' => BASE . LOG, @@ -60,7 +58,7 @@ $log = new CoreLibs\Debug\Logging([ // db connection $db = new CoreLibs\DB\IO(DB_CONFIG, $log); // login page -$login = new CoreLibs\ACL\Login($db, $log); +$login = new CoreLibs\ACL\Login($db, $log, $session); // lang, path, domain // pre auto detect language after login $locale = \CoreLibs\Language\GetLocale::setLocale(); diff --git a/www/lib/CoreLibs/ACL/Login.php b/www/lib/CoreLibs/ACL/Login.php index 57de99ea..9f824a14 100644 --- a/www/lib/CoreLibs/ACL/Login.php +++ b/www/lib/CoreLibs/ACL/Login.php @@ -69,7 +69,6 @@ declare(strict_types=1); namespace CoreLibs\ACL; use CoreLibs\Check\Password; -use CoreLibs\Create\Session; class Login { @@ -162,24 +161,30 @@ class Login public $db; /** @var \CoreLibs\Language\L10n language */ public $l; + /** @var \CoreLibs\Create\Session session class */ + public $session; /** * constructor, does ALL, opens db, works through connection checks, * finishes itself * - * @param \CoreLibs\DB\IO $db Database connection class - * @param \CoreLibs\Debug\Logging $log Logging class + * @param \CoreLibs\DB\IO $db Database connection class + * @param \CoreLibs\Debug\Logging $log Logging class + * @param \CoreLibs\Create\Session $session Session interface class */ public function __construct( \CoreLibs\DB\IO $db, - \CoreLibs\Debug\Logging $log + \CoreLibs\Debug\Logging $log, + \CoreLibs\Create\Session $session ) { + // attach db class + $this->db = $db; // log login data for this class only $log->setLogPer('class', true); // attach logger $this->log = $log; - // attach db class - $this->db = $db; + // attach session class + $this->session = $session; // set internal page name $this->page_name = \CoreLibs\Get\System::getPageName(); // set db special errors @@ -192,12 +197,9 @@ class Login // initial the session if there is no session running already // check if session exists and could be created // TODO: move session creation and check to outside? - if (Session::startSession() === false) { + if ($this->session->checkActiveSession() === false) { $this->login_error = 1; - echo 'Session not started or could not be started!
' - . 'Use \'\CoreLibs\Create\Session::startSession();\'.
' - . 'For less problems with other session, you can set a ' - . 'session name with \'\CoreLibs\Create\Session::startSession(\'name\');\'.
'; + echo 'No active session found'; exit; } @@ -289,7 +291,7 @@ class Login // ** LANGUAGE SET AFTER LOGIN ** // set the locale if ( - Session::getSessionId() !== false && + $this->session->checkActiveSession() === true && !empty($_SESSION['DEFAULT_LANG']) ) { $locale = $_SESSION['DEFAULT_LOCALE'] ?? ''; @@ -1369,7 +1371,7 @@ EOM; $q .= "NULL, "; } } - $q .= "'" . Session::getSessionId() . "', "; + $q .= "'" . $this->session->getSessionId() . "', "; $q .= "'" . $this->db->dbEscapeString($this->action) . "', "; $q .= "'" . $this->db->dbEscapeString($this->username) . "', "; $q .= "NULL, "; diff --git a/www/lib/CoreLibs/Admin/Backend.php b/www/lib/CoreLibs/Admin/Backend.php index a691c41e..314f5e12 100644 --- a/www/lib/CoreLibs/Admin/Backend.php +++ b/www/lib/CoreLibs/Admin/Backend.php @@ -101,6 +101,8 @@ class Backend public $db; /** @var \CoreLibs\Language\L10n language */ public $l; + /** @var \CoreLibs\Create\Session session class */ + public $session; // smarty publics [end processing in smarty class] /** @var array */ public $DATA; @@ -114,23 +116,27 @@ class Backend // CONSTRUCTOR / DECONSTRUCTOR |====================================> /** * main class constructor - * @param \CoreLibs\DB\IO $db Database connection class - * @param \CoreLibs\Debug\Logging $log Logging class - * @param \CoreLibs\Language\L10n $l10n l10n language class - * @param array $locale locale data read from setLocale + * @param \CoreLibs\DB\IO $db Database connection class + * @param \CoreLibs\Debug\Logging $log Logging class + * @param \CoreLibs\Create\Session $session Session interface class + * @param \CoreLibs\Language\L10n $l10n l10n language class + * @param array $locale locale data read from setLocale */ public function __construct( \CoreLibs\DB\IO $db, \CoreLibs\Debug\Logging $log, + \CoreLibs\Create\Session $session, \CoreLibs\Language\L10n $l10n, array $locale ) { + // attach db class + $this->db = $db; // set to log not per class $log->setLogPer('class', false); // attach logger $this->log = $log; - // attach db class - $this->db = $db; + // attach session class + $this->session = $session; // get the language sub class & init it $this->l = $l10n; // parse and read, legacy stuff @@ -232,9 +238,9 @@ class Backend . "'" . $this->db->dbEscapeString($_SERVER['HTTP_ACCEPT'] ?? '') . "', " . "'" . $this->db->dbEscapeString($_SERVER['HTTP_ACCEPT_CHARSET'] ?? '') . "', " . "'" . $this->db->dbEscapeString($_SERVER['HTTP_ACCEPT_ENCODING'] ?? '') . "', " - . (\CoreLibs\Create\Session::getSessionId() === false ? + . ($this->session->getSessionId() === false ? "NULL" : - "'" . \CoreLibs\Create\Session::getSessionId() . "'") + "'" . $this->session->getSessionId() . "'") . ", " . "'" . $this->db->dbEscapeString($this->action) . "', " . "'" . $this->db->dbEscapeString($this->action_id) . "', " diff --git a/www/lib/CoreLibs/Basic.php b/www/lib/CoreLibs/Basic.php index b2c22087..c9ce4dd9 100644 --- a/www/lib/CoreLibs/Basic.php +++ b/www/lib/CoreLibs/Basic.php @@ -66,6 +66,8 @@ class Basic // logging interface, Debug\Logging class /** @var \CoreLibs\Debug\Logging */ public $log; + /** @var\CoreLibs\Create\Session */ + public $session; // email valid checks /** @var array */ @@ -148,7 +150,7 @@ class Basic $this->email_regex_check = \CoreLibs\Check\Email::getEmailRegexCheck(); // initial the session if there is no session running already - \CoreLibs\Create\Session::startSession($session_name); + $this->session = new \CoreLibs\Create\Session($session_name ?? ''); } /** diff --git a/www/lib/CoreLibs/Create/Session.php b/www/lib/CoreLibs/Create/Session.php index 2c6cdd10..7b6e9dc6 100644 --- a/www/lib/CoreLibs/Create/Session.php +++ b/www/lib/CoreLibs/Create/Session.php @@ -6,7 +6,7 @@ * start a php sesseion * name can be given via startSession parameter * if not set tries to read $SET_SESSION_NAME from global - * if this is not set tries to read SET_SESSION_NAME constant + * else will use default set in php.ini */ declare(strict_types=1); @@ -16,13 +16,29 @@ namespace CoreLibs\Create; class Session { /** @var string list for errors*/ - private static $error_str = ''; + private $error_str = ''; /** - * init a session + * init a session, if array is empty or array does not have session_name set + * then no auto init is run + * + * @param string $session_name if set and not empty, will start session */ - public function __construct() + public function __construct(string $session_name = '') { + if (!empty($session_name)) { + $this->startSession($session_name); + } + } + + /** + * check if we are in CLI, we set this, so we can mock this too + * + * @return bool + */ + private function checkCLI(): bool + { + return \CoreLibs\Get\System::checkCLI(); } /** @@ -30,9 +46,9 @@ class Session * * @return string Last error string */ - public static function getErrorStr(): string + public function getErrorStr(): string { - return self::$error_str; + return $this->error_str; } /** @@ -69,39 +85,30 @@ class Session * @param string|null $session_name * @return string|bool */ - public static function startSession(?string $session_name = null) + public function startSession(?string $session_name = null) { // we can't start sessions on command line - if (\CoreLibs\Get\System::checkCLI()) { - self::$error_str = '[SESSION] No sessions in php cli'; + if ($this->checkCLI()) { + $this->error_str = '[SESSION] No sessions in php cli'; return false; } // if session are OFF - if (self::getSessionStatus() === PHP_SESSION_DISABLED) { - self::$error_str = '[SESSION] Sessions are disabled'; + if ($this->getSessionStatus() === PHP_SESSION_DISABLED) { + $this->error_str = '[SESSION] Sessions are disabled'; return false; } // session_status // initial the session if there is no session running already - if (!self::checkActiveSession()) { + if (!$this->checkActiveSession()) { // if session name is emtpy, check if there is a global set // this is a deprecated fallback $session_name = $session_name ?? $GLOBALS['SET_SESSION_NAME'] ?? ''; - // check if we have an external session name given, else skip this step - // this is a deprecated fallback - if ( - empty($session_name) && - defined('SET_SESSION_NAME') && - !empty(SET_SESSION_NAME) - ) { - // set the session name for possible later check - $session_name = SET_SESSION_NAME; - } + // DEPRECTED: constant SET_SESSION_NAME is no longer used // if set, set special session name if (!empty($session_name)) { // invalid session name, abort - if (!self::checkValidSessionName($session_name)) { - self::$error_str = '[SESSION] Invalid session name: ' . $session_name; + if (!$this->checkValidSessionName($session_name)) { + $this->error_str = '[SESSION] Invalid session name: ' . $session_name; return false; } session_name($session_name); @@ -110,11 +117,11 @@ class Session session_start(); } // if we still have no active session - if (!self::checkActiveSession()) { - self::$error_str = '[SESSION] Failed to activate session'; + if (!$this->checkActiveSession()) { + $this->error_str = '[SESSION] Failed to activate session'; return false; } - return self::getSessionId(); + return $this->getSessionId(); } /** @@ -122,7 +129,7 @@ class Session * * @return string|bool */ - public static function getSessionId() + public function getSessionId() { return session_id(); } @@ -132,7 +139,7 @@ class Session * * @return string|bool */ - public static function getSessionName() + public function getSessionName() { return session_name(); } @@ -143,9 +150,9 @@ class Session * * @return bool True if there is an active session, else false */ - public static function checkActiveSession(): bool + public function checkActiveSession(): bool { - if (self::getSessionStatus() === PHP_SESSION_ACTIVE) { + if ($this->getSessionStatus() === PHP_SESSION_ACTIVE) { return true; } else { return false; @@ -160,7 +167,7 @@ class Session * * @return bool True und sucess, false on failure */ - public static function writeClose(): bool + public function writeClose(): bool { return session_write_close(); } @@ -175,7 +182,7 @@ class Session * * @return int */ - public static function getSessionStatus(): int + public function getSessionStatus(): int { return session_status(); }