Compare commits

...

4 Commits

Author SHA1 Message Date
Clemens Schwaighofer
a523a4e831 ACL\Login class update for phpstan check 2022-05-24 16:50:37 +09:00
Clemens Schwaighofer
db8e17ae7c Convert static Session class to normal session class
All static Session:: calls (except for checking valid session name) are
converted to object type. This Object is passed on to Login, Admin
Backend and any other class that needs basic session checking
2022-05-24 15:00:04 +09:00
Clemens Schwaighofer
5b581c2ed6 Fix checkCLI call on false return from php_sapi_name() 2022-05-24 13:12:17 +09:00
Clemens Schwaighofer
1e734581d7 Session class update, cli check add, tests updates, edit table update
Update edit_access_data table and set unique check for edit_access_id +
name so we do not have two identical keys for one edit access set

Update config host and add more test domains for various access tests

Update Session and move cli check to Get\System class. Some other minor
session info updates

New method \Get\System::checkCLI() returns true if the sapi name has
cli inside, else false
2022-05-24 11:36:03 +09:00
43 changed files with 269 additions and 274 deletions

View File

@@ -14,3 +14,7 @@ CREATE TABLE edit_access_data (
name VARCHAR,
value VARCHAR
) INHERITS (edit_generic) WITHOUT OIDS;
-- create a unique index for each attached data block for each edit access can
-- only have ONE value;
CREATE UNIQUE INDEX edit_access_data_edit_access_id_name_ukey ON edit_access_data (edit_access_id, name);

View File

@@ -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);
}
}

View File

@@ -13,7 +13,6 @@ use PHPUnit\Framework\TestCase;
*/
final class CoreLibsConvertJsonTest extends TestCase
{
/**
* test list for json convert tests
*

View File

@@ -20,6 +20,10 @@ final class CoreLibsCreateSessionTest extends TestCase
*/
public function sessionProvider(): array
{
// 0: session name as parameter
// 1: type p (param), g: global, c: constant
// 2: exepcted name (session)
// 3: regex check
return [
'session parameter' => [
'sessionNameParameter',
@@ -33,32 +37,20 @@ final class CoreLibsCreateSessionTest extends TestCase
'sessionNameGlobals',
'/^\w+$/'
],
'session constant' => [
'sessionNameConstant',
'c',
'sessionNameConstant',
'session name default' => [
'',
'd',
'',
'/^\w+$/'
],
];
}
/**
* Undocumented function
*
* @return void
*/
protected function setUp(): void
{
if (session_id()) {
session_destroy();
}
}
/**
* Undocumented function
*
* @dataProvider sessionProvider
* @testdox startSession $input name for $type will be $expected_n with $expected_i [$_dataName]
* @testdox startSession $input name for $type will be $expected_n with $expected_i [$_dataName]
*
* @param string $input
* @param string $type
@@ -66,21 +58,28 @@ final class CoreLibsCreateSessionTest extends TestCase
* @param string|bool $expected_i
* @return void
*/
public function testStartSession(string $input, string $type, $expected_n, $expected_i): void
{
// NEEDS MOCKING
/* $session_id = '';
public function testStartSession(
string $input,
string $type,
$expected_n,
$expected_i
): void {
/*
// 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(
@@ -89,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']);
@@ -101,6 +100,64 @@ final class CoreLibsCreateSessionTest extends TestCase
$this->markTestSkipped('[CoreLibsCreateSessionTest] No implementation '
. 'for Create\Session. Cannot run session_start in CLI');
}
/**
* provider for session name check
*
* @return array
*/
public function sessionNameProvider(): array
{
// 0: string for session
// 1: expected return
return [
'valid name' => [
'abc',
true
],
'valid name longer' => [
'something-abc-123',
true
],
'invalid name' => [
'abc#abc',
false
],
'only numbers' => [
'123',
false
],
'longer than 128 chars' => [
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
. 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
. 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz',
false
],
'too short' => [
'',
false
],
];
}
/**
* Undocumented function
*
* @covers ::checkValidSessionName
* @dataProvider sessionNameProvider
* @testdox checkValidSessionName $input seessionn name is $expected [$_dataName]
*
* @param string $input
* @param bool $expected
* @return void
*/
public function testCheckValidSessionName(string $input, bool $expected): void
{
$this->assertEquals(
$expected,
\CoreLibs\Create\Session::checkValidSessionName($input)
);
}
}
// __END__

View File

@@ -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 "<!DOCTYPE html>";

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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';

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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 "<!DOCTYPE html>";
@@ -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') . "<br>";
. ($session->checkValidSessionName($_session_name) ? 'Valid' : 'Invalid') . "<br>";
}
echo "Global session name: " . ($GLOBALS['SET_SESSION_NAME'] ?? '-') . "<br>";
print "[UNSET] Current session id: " . Session::getSessionId() . "<br>";
print "[UNSET] Current session name: " . Session::getSessionName() . "<br>";
print "[UNSET] Current session active: " . (Session::checkActiveSession() ? 'Yes' : 'No') . "<br>";
print "[UNSET] Current session status: " . getSessionStatusString(Session::getSessionStatus()) . "<br>";
print "[UNSET] Current session id: " . $session->getSessionId() . "<br>";
print "[UNSET] Current session name: " . $session->getSessionName() . "<br>";
print "[UNSET] Current session active: " . ($session->checkActiveSession() ? 'Yes' : 'No') . "<br>";
print "[UNSET] Current session status: " . getSessionStatusString($session->getSessionStatus()) . "<br>";
if (isset($_SESSION)) {
print "[UNSET] _SESSION is: set<br>";
} else {
@@ -92,22 +89,22 @@ if (isset($_SESSION)) {
}
#
print "[UNSET] To set session name valid: "
. (Session::checkValidSessionName($session_name) ? 'Valid' : 'Invalid') . "<br>";
if (false === ($session = Session::startSession($session_name))) {
print "[FAILED] Session start failed: " . Session::getErrorStr() . "<br>";
. ($session->checkValidSessionName($session_name) ? 'Valid' : 'Invalid') . "<br>";
if (false === ($session_id = $session->startSession($session_name))) {
print "[FAILED] Session start failed: " . $session->getErrorStr() . "<br>";
} else {
print "[SET] Current session id: " . $session . "<br>";
print "[SET] Current session id: " . $session_id . "<br>";
}
// set again
if (false === ($session = Session::startSession($session_name))) {
print "[2 FAILED] Session start failed: " . Session::getErrorStr() . "<br>";
if (false === ($session_id = $session->startSession($session_name))) {
print "[2 FAILED] Session start failed: " . $session->getErrorStr() . "<br>";
} else {
print "[2 SET] Current session id: " . $session . "<br>";
print "[2 SET] Current session id: " . $session_id . "<br>";
}
print "[SET] Current session id: " . Session::getSessionId() . "<br>";
print "[SET] Current session name: " . Session::getSessionName() . "<br>";
print "[SET] Current session active: " . (Session::checkActiveSession() ? 'Yes' : 'No') . "<br>";
print "[SET] Current session status: " . getSessionStatusString(Session::getSessionStatus()) . "<br>";
print "[SET] Current session id: " . $session->getSessionId() . "<br>";
print "[SET] Current session name: " . $session->getSessionName() . "<br>";
print "[SET] Current session active: " . ($session->checkActiveSession() ? 'Yes' : 'No') . "<br>";
print "[SET] Current session status: " . getSessionStatusString($session->getSessionStatus()) . "<br>";
if (isset($_SESSION)) {
print "[SET] _SESSION is: set<br>";
} else {
@@ -117,51 +114,51 @@ if (!isset($_SESSION['counter'])) {
$_SESSION['counter'] = 0;
}
$_SESSION['counter']++;
print "[READ] " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "<br>";
print "[READ] A " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "<br>";
$_SESSION[$var] = $value;
print "[READ] " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "<br>";
print "[READ] B " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "<br>";
print "[READ] Confirm " . $var . " is " . $value . ": "
. (($_SESSION[$var] ?? '') == $value ? 'Matching' : 'Not matching') . "<br>";
// differnt session name
$session_name = 'class-test-session-ALT';
if (false === ($session = Session::startSession($session_name))) {
print "[3 FAILED] Session start failed: " . Session::getErrorStr() . "<br>";
if (false === ($session_id = $session->startSession($session_name))) {
print "[3 FAILED] Session start failed: " . $session->getErrorStr() . "<br>";
} else {
print "[3 SET] Current session id: " . $session . "<br>";
print "[3 SET] Current session id: " . $session_id . "<br>";
}
print "[SET AGAIN] Current session id: " . Session::getSessionId() . "<br>";
print "[SET AGAIN] Current session id: " . $session->getSessionId() . "<br>";
print "[ALL SESSION]: " . \CoreLibs\Debug\Support::printAr($_SESSION) . "<br>";
// 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() . "<br>";
if (false === ($session_id = $session->startSession($session_name))) {
print "[4 FAILED] Session start failed: " . $session->getErrorStr() . "<br>";
} else {
print "[4 SET] Current session id: " . $session . "<br>";
print "[4 SET] Current session id: " . $session_id . "<br>";
}
print "[START AGAIN] Current session id: " . Session::getSessionId() . "<br>";
print "[START AGAIN] Current session id: " . $session->getSessionId() . "<br>";
$_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() . "<br>";
if (false === ($session_id = $session->startSession($session_name))) {
print "[5 FAILED] Session start failed: " . $session->getErrorStr() . "<br>";
} else {
print "[5 SET] Current session id: " . $session . "<br>";
print "[5 SET] Current session id: " . $session_id . "<br>";
}
print "[BAD NAME] Current session id: " . Session::getSessionId() . "<br>";
print "[BAD NAME] Current session name: " . Session::getSessionName() . "<br>";
print "[BAD NAME] Current session active: " . (Session::checkActiveSession() ? 'Yes' : 'No') . "<br>";
print "[BAD NAME] Current session status: " . getSessionStatusString(Session::getSessionStatus()) . "<br>";
print "[BAD NAME] Current session id: " . $session->getSessionId() . "<br>";
print "[BAD NAME] Current session name: " . $session->getSessionName() . "<br>";
print "[BAD NAME] Current session active: " . ($session->checkActiveSession() ? 'Yes' : 'No') . "<br>";
print "[BAD NAME] Current session status: " . getSessionStatusString($session->getSessionStatus()) . "<br>";
// error message
print $log->printErrorMsg();

View File

@@ -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 "<!DOCTYPE html>";
@@ -77,28 +74,28 @@ $value = 'bar';
echo "Global session name: " . ($GLOBALS['SET_SESSION_NAME'] ?? '-') . "<br>";
print "[UNSET] Current session id: " . Session::getSessionId() . "<br>";
print "[UNSET] Current session name: " . Session::getSessionName() . "<br>";
print "[UNSET] Current session active: " . (Session::checkActiveSession() ? 'Yes' : 'No') . "<br>";
print "[UNSET] Current session status: " . getSessionStatusString(Session::getSessionStatus()) . "<br>";
print "[UNSET] Current session id: " . $session->getSessionId() . "<br>";
print "[UNSET] Current session name: " . $session->getSessionName() . "<br>";
print "[UNSET] Current session active: " . ($session->checkActiveSession() ? 'Yes' : 'No') . "<br>";
print "[UNSET] Current session status: " . getSessionStatusString($session->getSessionStatus()) . "<br>";
print "[READ] " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "<br>";
// start
if (false === ($session = Session::startSession($session_name))) {
print "Session start failed: " . Session::getErrorStr() . "<br>";
if (false === ($session_id = $session->startSession($session_name))) {
print "Session start failed: " . $session->getErrorStr() . "<br>";
} else {
print "Current session id: " . $session . "<br>";
print "Current session id: " . $session_id . "<br>";
}
// set again
if (false === ($session = Session::startSession($session_name))) {
if (false === ($session_id = $session->startSession($session_name))) {
print "[2] Session start failed<br>";
} else {
print "[2] Current session id: " . $session . "<br>";
print "[2] Current session id: " . $session_id . "<br>";
}
print "[SET] Current session id: " . Session::getSessionId() . "<br>";
print "[SET] Current session name: " . Session::getSessionName() . "<br>";
print "[SET] Current session active: " . (Session::checkActiveSession() ? 'Yes' : 'No') . "<br>";
print "[SET] Current session status: " . getSessionStatusString(Session::getSessionStatus()) . "<br>";
print "[SET] Current session id: " . $session->getSessionId() . "<br>";
print "[SET] Current session name: " . $session->getSessionName() . "<br>";
print "[SET] Current session active: " . ($session->checkActiveSession() ? 'Yes' : 'No') . "<br>";
print "[SET] Current session status: " . getSessionStatusString($session->getSessionStatus()) . "<br>";
print "[READ] " . $var . ": " . ($_SESSION[$var] ?? '{UNSET}') . "<br>";
print "[READ] Confirm " . $var . " is " . $value . ": "
. (($_SESSION[$var] ?? '') == $value ? 'Matching' : 'Not matching') . "<br>";

View File

@@ -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();

View File

@@ -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 "<body>";
print '<div><a href="class_test.php">Class Test Master</a></div>';
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
print "System::getHostName():<br>";
print "GETHOSTNAME: " . DgS::printAr(System::getHostName()) . "<br>";
print "System::getPageName():<br>";
print "GETPAGENAME(0): " . System::getPageName() . "<br>";
print "GETPAGENAME(1): " . System::getPageName(System::NO_EXTENSION) . "<br>";
print "GETPAGENAME(2): " . System::getPageName(System::FULL_PATH) . "<br>";
print "System::getPageNameArray():<br>";
print "GETPAGENAMEARRAY: " . \CoreLibs\Debug\Support::printAr(System::getPageNameArray()) . "<br>";
// seting errro codes file upload
print "System::fileUploadErrorMessage():<br>";
print "FILEUPLOADERRORMESSAGE(): " . System::fileUploadErrorMessage(-1) . "<br>";
print "FILEUPLOADERRORMESSAGE(UPLOAD_ERR_CANT_WRITE): "
. System::fileUploadErrorMessage(UPLOAD_ERR_CANT_WRITE) . "<br>";
print "System::checkCLI():<br>";
print "Are we in an CLI: " . (System::checkCLI() ? 'Yes' : 'No') . "<br>";
// error message
print $log->printErrorMsg();

View File

@@ -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();

View File

@@ -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();

View File

@@ -53,7 +53,10 @@ $SITE_CONFIG = [
'login_enabled' => true
],
// 'other.host.com' => $__LOCAL_CONFIG
'soba-dev.tequila.jp' => $__LOCAL_CONFIG
'soba-dev.tequila.jp' => $__LOCAL_CONFIG,
'soba.tequila.jp' => $__LOCAL_CONFIG,
'soba.teq.jp' => $__LOCAL_CONFIG,
'soba-local.tokyo.tequila.jp' => $__LOCAL_CONFIG,
];
// __END__

View File

@@ -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);

View File

@@ -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

View File

@@ -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();

View File

@@ -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 '<b>Session not started or could not be started!</b><br>'
. 'Use \'\CoreLibs\Create\Session::startSession();\'.<br>'
. 'For less problems with other session, you can set a '
. 'session name with \'\CoreLibs\Create\Session::startSession(\'name\');\'.<br>';
echo '<b>No active session found</b>';
exit;
}
@@ -219,7 +221,7 @@ class Login
// if there is a schema set in the config, or fall back to DB_SCHEMA
// if this exists, if this also does not exists use public schema
/** @phpstan-ignore-next-line */
if (defined('LOGIN_DB_SCHEMA') && !empty(LOGIN_DB_SCHEMA)) {
if (!empty(LOGIN_DB_SCHEMA)) {
$SCHEMA = LOGIN_DB_SCHEMA;
} elseif (!empty($this->db->dbGetSchema(true))) {
$SCHEMA = $this->db->dbGetSchema(true);
@@ -289,19 +291,19 @@ 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'] ?? '';
} else {
$locale = defined('SITE_LOCALE') && !empty(SITE_LOCALE) ?
$locale = !empty(SITE_LOCALE) ?
SITE_LOCALE :
/** @phpstan-ignore-next-line DEFAULT_LOCALE could be empty */
(defined('DEFAULT_LOCALE') && !empty(DEFAULT_LOCALE) ?
(!empty(DEFAULT_LOCALE) ?
DEFAULT_LOCALE : 'en.UTF-8');
}
// set domain
if (defined('CONTENT_PATH') && !empty(CONTENT_PATH)) {
if (defined('CONTENT_PATH')) {
$domain = str_replace('/', '', CONTENT_PATH);
} else {
$domain = 'admin';
@@ -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, ";

View File

@@ -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<mixed> */
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<string,string> $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<string,string> $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) . "', "

View File

@@ -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<mixed> */
@@ -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 ?? '');
}
/**

View File

@@ -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;
}
/**
@@ -61,44 +77,38 @@ class Session
}
/**
* Undocumented function
* start session with given session name if set
* aborts on command line or if sessions are not enabled
* also aborts if session cannot be started
* On sucess returns the session id
*
* @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 (php_sapi_name() === 'cli') {
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);
@@ -107,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();
}
/**
@@ -119,7 +129,7 @@ class Session
*
* @return string|bool
*/
public static function getSessionId()
public function getSessionId()
{
return session_id();
}
@@ -129,7 +139,7 @@ class Session
*
* @return string|bool
*/
public static function getSessionName()
public function getSessionName()
{
return session_name();
}
@@ -140,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;
@@ -157,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();
}
@@ -172,7 +182,7 @@ class Session
*
* @return int
*/
public static function getSessionStatus(): int
public function getSessionStatus(): int
{
return session_status();
}

View File

@@ -94,6 +94,24 @@ class System
{
return pathinfo($_SERVER['PHP_SELF']);
}
/**
* Check if the php sapi interface has cli inside
*
* @return bool True for CLI type PHP, else false
*/
public static function checkCLI(): bool
{
return substr(
// if return is false, use empty string
(($sapi_name = php_sapi_name()) === false ?
'' :
$sapi_name
),
0,
3
) === 'cli' ? true : false;
}
}
// __END__