Page MenuHomeDevCentral

D3795.id9833.diff
No OneTemporary

D3795.id9833.diff

diff --git a/composer.json b/composer.json
--- a/composer.json
+++ b/composer.json
@@ -20,6 +20,7 @@
"keruald/database": "0.5.2",
"keruald/omnitools": "0.15.1",
"smarty/smarty": "^5.6.0",
+ "vlucas/phpdotenv": "^v5.6.2",
"ext-mysqli": "*"
},
"require-dev": {
diff --git a/workspaces/composer.json b/workspaces/composer.json
--- a/workspaces/composer.json
+++ b/workspaces/composer.json
@@ -11,6 +11,7 @@
"phpunit/phpunit": "^12.4",
"nasqueron/codestyle": "^0.1.2",
"squizlabs/php_codesniffer": "^4.0",
+ "vlucas/phpdotenv": "^v5.6.2",
"ext-mysqli": "*"
},
"license": "BSD-2-Clause",
diff --git a/workspaces/src/Engines/Framework/Application.php b/workspaces/src/Engines/Framework/Application.php
new file mode 100644
--- /dev/null
+++ b/workspaces/src/Engines/Framework/Application.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Waystone\Workspaces\Engines\Framework;
+
+use Keruald\Database\Database;
+use Waystone\Workspaces\Engines\Errors\ErrorHandling;
+
+class Application {
+
+ public static function init () : void {
+ Environment::init();
+ ErrorHandling::init();
+ }
+
+ public static function getContext(array $config) : Context {
+ $context = new Context();
+
+ $context->config = $config;
+ $context->db = Database::load($config["sql"]);
+ $context->session = Session::load($context->db);
+ $context->url = get_current_url_fragments();
+ $context->initializeTemplateEngine($context->config['Theme']);
+
+ return $context;
+ }
+
+}
diff --git a/workspaces/src/Engines/Framework/Environment.php b/workspaces/src/Engines/Framework/Environment.php
new file mode 100644
--- /dev/null
+++ b/workspaces/src/Engines/Framework/Environment.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace Waystone\Workspaces\Engines\Framework;
+
+use Dotenv\Dotenv;
+
+/**
+ * Interact with the environment
+ */
+class Environment {
+
+ /**
+ * Path to
+ */
+ const string ROOT_DIR = __DIR__ . "/../../..";
+
+ const array ENV_DIR_CANDIDATES = [
+ # Framework installed from workspaces/
+ self::ROOT_DIR,
+
+ # Monorepo installation
+ self::ROOT_DIR . "/..",
+ ];
+
+ /**
+ * Reads and loads .env environment file into environment
+ */
+ public static function init() : void {
+ $dotenv = Dotenv::createImmutable(self::ENV_DIR_CANDIDATES);
+ $dotenv->safeLoad();
+ }
+}
diff --git a/workspaces/src/includes/session.php b/workspaces/src/Engines/Framework/Session.php
rename from workspaces/src/includes/session.php
rename to workspaces/src/Engines/Framework/Session.php
--- a/workspaces/src/includes/session.php
+++ b/workspaces/src/Engines/Framework/Session.php
@@ -19,10 +19,17 @@
*
*/
+namespace Waystone\Workspaces\Engines\Framework;
+
+use Keruald\Database\DatabaseEngine;
+use User;
+use Waystone\Workspaces\Engines\Errors\ErrorHandling;
+
/**
* Session class
*/
class Session {
+
/**
* @var string session ID
*/
@@ -33,6 +40,8 @@
*/
public $ip;
+ public DatabaseEngine $db;
+
/*
* @var Session current session instance
*/
@@ -43,11 +52,9 @@
*
* @return Session current session instance
*/
- public static function load () {
+ public static function load (DatabaseEngine $db) {
if (!isset(self::$instance)) {
- //Creates new session instance
- $c = __CLASS__;
- self::$instance = new $c;
+ self::$instance = new self($db);
}
return self::$instance;
@@ -56,7 +63,9 @@
/**
* Initializes a new instance of Session object
*/
- private function __construct () {
+ private function __construct (DatabaseEngine $db) {
+ $this->db = $db;
+
//Starts PHP session, and gets id
session_start();
$_SESSION['ID'] = session_id();
@@ -71,6 +80,7 @@
/**
* Gets remote client IP address
+ *
* @return string IP
*/
public static function get_ip () {
@@ -90,23 +100,38 @@
* i. deletes expired session
* ii. sets offline relevant sessions
*/
- public static function clean_old_sessions () {
- global $db, $Config;
+ public function clean_old_sessions () {
+ global $Config;
+ $db = $this->db;
//Gets session and online status lifetime (in seconds)
//If not specified in config, sets default 5 and 120 minutes values
- $onlineDuration = array_key_exists('OnlineDuration', $Config) ? $Config['OnlineDuration'] : 300;
- $sessionDuration = array_key_exists('SessionDuration', $Config) ? $Config['SessionDuration'] : 7200;
+ $onlineDuration = array_key_exists('OnlineDuration', $Config)
+ ? $Config['OnlineDuration'] : 300;
+ $sessionDuration = array_key_exists('SessionDuration', $Config)
+ ? $Config['SessionDuration'] : 7200;
- $resource = array_key_exists('ResourceID', $Config) ? '\'' . $db->escape($Config['ResourceID']) . '\'' : 'default';
+ $resource = array_key_exists('ResourceID', $Config) ? '\''
+ . $db->escape($Config['ResourceID'])
+ . '\''
+ : 'default';
//Deletes expired sessions
- $sql = "DELETE FROM " . TABLE_SESSIONS . " WHERE session_resource = $resource AND TIMESTAMPDIFF(SECOND, session_updated, NOW()) > $sessionDuration";
- if (!$db->query($sql)) message_die(SQL_ERROR, "Can't delete expired sessions", '', __LINE__, __FILE__, $sql);
+ $sql = "DELETE FROM " . TABLE_SESSIONS
+ . " WHERE session_resource = $resource AND TIMESTAMPDIFF(SECOND, session_updated, NOW()) > $sessionDuration";
+ if (!$db->query($sql)) {
+ ErrorHandling::messageAndDie(SQL_ERROR,
+ "Can't delete expired sessions", '', __LINE__, __FILE__, $sql);
+ }
//Online -> offline
- $sql = "UPDATE " . TABLE_SESSIONS . " SET session_resource = $resource AND session_online = 0 WHERE TIMESTAMPDIFF(SECOND, session_updated, NOW()) > $onlineDuration";
- if (!$db->query($sql)) message_die(SQL_ERROR, 'Can\'t update sessions online statuses', '', __LINE__, __FILE__, $sql);
+ $sql = "UPDATE " . TABLE_SESSIONS
+ . " SET session_resource = $resource AND session_online = 0 WHERE TIMESTAMPDIFF(SECOND, session_updated, NOW()) > $onlineDuration";
+ if (!$db->query($sql)) {
+ ErrorHandling::messageAndDie(SQL_ERROR,
+ 'Can\'t update sessions online statuses', '', __LINE__,
+ __FILE__, $sql);
+ }
}
@@ -114,21 +139,29 @@
* Updates or creates a session in the database
*/
public function update () {
- global $db, $Config;
+ global $Config;
+ $db = $this->db;
//Cleans up session
//To boost SQL performances, try a random trigger
// e.g. if (rand(1, 100) < 3) self::clean_old_sessions();
//or comment this line and execute a cron script you launch each minute.
- self::clean_old_sessions();
+ $this->clean_old_sessions();
//Saves session in database.
//If the session already exists, it updates the field online and updated.
$id = $db->escape($this->id);
- $resource = array_key_exists('ResourceID', $Config) ? '\'' . $db->escape($Config['ResourceID']) . '\'' : 'default';
+ $resource = array_key_exists('ResourceID', $Config) ? '\''
+ . $db->escape($Config['ResourceID'])
+ . '\''
+ : 'default';
$user_id = $db->escape(ANONYMOUS_USER);
- $sql = "INSERT INTO " . TABLE_SESSIONS . " (session_id, session_ip, session_resource, user_id) VALUES ('$id', '$this->ip', $resource, '$user_id') ON DUPLICATE KEY UPDATE session_online = 1";
- if (!$db->query($sql)) message_die(SQL_ERROR, 'Can\'t save current session', '', __LINE__, __FILE__, $sql);
+ $sql = "INSERT INTO " . TABLE_SESSIONS
+ . " (session_id, session_ip, session_resource, user_id) VALUES ('$id', '$this->ip', $resource, '$user_id') ON DUPLICATE KEY UPDATE session_online = 1";
+ if (!$db->query($sql)) {
+ ErrorHandling::messageAndDie(SQL_ERROR,
+ 'Can\'t save current session', '', __LINE__, __FILE__, $sql);
+ }
}
/**
@@ -142,11 +175,17 @@
if ($count == -1) {
//Queries sessions table
- global $db, $Config;
-
- $resource = array_key_exists('ResourceID', $Config) ? '\'' . $db->escape($Config['ResourceID']) . '\'' : 'default';
- $sql = "SELECT count(*) FROM " . TABLE_SESSIONS . " WHERE session_resource = $resource AND session_online = 1";
- $count = (int)$db->queryScalar($sql, "Can't count online users");
+ global $Config;
+ $db = $this->db;
+
+ $resource = array_key_exists('ResourceID', $Config) ? '\''
+ . $db->escape($Config['ResourceID'])
+ . '\''
+ : 'default';
+ $sql = "SELECT count(*) FROM " . TABLE_SESSIONS
+ . " WHERE session_resource = $resource AND session_online = 1";
+ $count =
+ (int)$db->queryScalar($sql, "Can't count online users");
}
//Returns number of users online
@@ -157,30 +196,37 @@
* Gets the value of a custom session table field
*
* @param string $info the field to get
+ *
* @return string the session specified field's value
*/
public function get_info ($info) {
- global $db;
+ $db = $this->db;
$id = $db->escape($this->id);
- $sql = "SELECT `$info` FROM " . TABLE_SESSIONS . " WHERE session_id = '$id'";
+ $sql = "SELECT `$info` FROM " . TABLE_SESSIONS
+ . " WHERE session_id = '$id'";
+
return $db->queryScalar($sql, "Can't get session $info info");
}
/**
* Sets the value of a custom session table field to the specified value
*
- * @param string $info the field to update
+ * @param string $info the field to update
* @param string $value the value to set
*/
public function set_info ($info, $value) {
- global $db;
+ $db = $this->db;
- $value = ($value === null) ? 'NULL' : "'" . $db->escape($value) . "'";
+ $value =
+ ($value === null) ? 'NULL' : "'" . $db->escape($value) . "'";
$id = $db->escape($this->id);
- $sql = "UPDATE " . TABLE_SESSIONS . " SET `$info` = $value WHERE session_id = '$id'";
- if (!$db->query($sql))
- message_die(SQL_ERROR, "Can't set session $info info", '', __LINE__, __FILE__, $sql);
+ $sql = "UPDATE " . TABLE_SESSIONS
+ . " SET `$info` = $value WHERE session_id = '$id'";
+ if (!$db->query($sql)) {
+ ErrorHandling::messageAndDie(SQL_ERROR,
+ "Can't set session $info info", '', __LINE__, __FILE__, $sql);
+ }
}
/**
@@ -189,13 +235,16 @@
* @return User the logged user information
*/
public function get_logged_user () {
- global $db;
+ $db = $this->db;
//Gets session information
$id = $db->escape($this->id);
$sql = "SELECT * FROM " . TABLE_SESSIONS . " WHERE session_id = '$id'";
- if (!$result = $db->query($sql))
- message_die(SQL_ERROR, "Can't query session information", '', __LINE__, __FILE__, $sql);
+ if (!$result = $db->query($sql)) {
+ ErrorHandling::messageAndDie(SQL_ERROR,
+ "Can't query session information", '', __LINE__, __FILE__,
+ $sql);
+ }
$row = $db->fetchRow($result);
//Gets user instance
@@ -217,7 +266,9 @@
public function clean () {
//Destroys $_SESSION array values, help ID
foreach ($_SESSION as $key => $value) {
- if ($key != 'ID') unset($_SESSION[$key]);
+ if ($key != 'ID') {
+ unset($_SESSION[$key]);
+ }
}
}
@@ -227,35 +278,37 @@
* @param string $user_id the user ID
*/
public function user_login ($user_id) {
- global $db;
+ $db = $this->db;
//Sets specified user ID in sessions table
$user_id = $db->escape($user_id);
- $id = $db->escape($this->id);
- $sql = "UPDATE " . TABLE_SESSIONS . " SET user_id = '$user_id' WHERE session_id = '$id'";
- if (!$db->query($sql))
- message_die(SQL_ERROR, "Can't set logged in status", '', __LINE__, __FILE__, $sql);
+ $id = $db->escape($this->id);
+ $sql = "UPDATE " . TABLE_SESSIONS
+ . " SET user_id = '$user_id' WHERE session_id = '$id'";
+ if (!$db->query($sql)) {
+ ErrorHandling::messageAndDie(SQL_ERROR,
+ "Can't set logged in status", '', __LINE__, __FILE__, $sql);
+ }
}
/**
* Updates the session in a user logout context
*/
public function user_logout () {
- global $db;
+ $db = $this->db;
//Sets anonymous user in sessions table
$user_id = $db->escape(ANONYMOUS_USER);
- $id = $db->escape($this->id);
- $sql = "UPDATE " . TABLE_SESSIONS . " SET user_id = '$user_id' WHERE session_id = '$id'";
- if (!$db->query($sql))
- message_die(SQL_ERROR, "Can't set logged out status", '', __LINE__, __FILE__, $sql);
+ $id = $db->escape($this->id);
+ $sql = "UPDATE " . TABLE_SESSIONS
+ . " SET user_id = '$user_id' WHERE session_id = '$id'";
+ if (!$db->query($sql)) {
+ ErrorHandling::messageAndDie(SQL_ERROR,
+ "Can't set logged out status", '', __LINE__, __FILE__, $sql);
+ }
//Cleans session
$this->clean();
}
}
-//The user_id matching anonymous user (overridable in config file)
-if (!defined('ANONYMOUS_USER')) {
- define('ANONYMOUS_USER', -1);
-}
diff --git a/workspaces/src/includes/core.php b/workspaces/src/includes/core.php
--- a/workspaces/src/includes/core.php
+++ b/workspaces/src/includes/core.php
@@ -22,15 +22,6 @@
/// ///
////////////////////////////////////////////////////////////////////////////////
-require_once("autoload_vendor.php");
-
-//Errors management
-include_once("error.php");
-error_reporting(E_ALL);
-
-$minorRecoverableErrors = E_NOTICE | E_USER_NOTICE | E_DEPRECATED | E_USER_DEPRECATED;
-set_error_handler('throwExceptionErrorHandler', E_ALL ^ $minorRecoverableErrors);
-
//Loads global functions
include_once("GlobalFunctions.php"); //Global functions
@@ -48,5 +39,9 @@
}
//Loads libraries
-include_once("session.php"); //Sessions handler
include_once("autoload.php"); //Autoloader for needed classes
+
+//The user_id matching anonymous user (overridable in config file)
+if (!defined('ANONYMOUS_USER')) {
+ define('ANONYMOUS_USER', -1);
+}
diff --git a/workspaces/src/includes/login.php b/workspaces/src/includes/login.php
--- a/workspaces/src/includes/login.php
+++ b/workspaces/src/includes/login.php
@@ -18,6 +18,9 @@
use Waystone\Workspaces\Engines\Errors\ErrorHandling;
+global $context;
+$db = $context->db;
+
$action = array_key_exists('action', $_GET) ? $_GET['action'] : '';
if (array_key_exists('LogIn', $_POST)) {
@@ -35,7 +38,7 @@
$LoginError = "Incorrect password.";
} else {
//Login successful
- Session::load()->user_login($row['user_id']);
+ $context->session->user_login($row['user_id']);
$LoginSuccessful = true;
}
} else {
@@ -43,7 +46,7 @@
}
} elseif (array_key_exists('LogOut', $_POST) || $action == "user.logout") {
//User have submitted logout form or clicked a logout link
- Session::load()->user_logout();
+ $context->session->user_logout();
} elseif (array_key_exists('authenticationMethodId', $_GET)) {
//Call authentication method for more processing
$auth = AuthenticationMethod::getFromId($_GET['authenticationMethodId'], $context);
diff --git a/workspaces/src/index.php b/workspaces/src/index.php
--- a/workspaces/src/index.php
+++ b/workspaces/src/index.php
@@ -1,4 +1,5 @@
<?php
+global $Config;
/**
* _, __, _, _ __, _ _, _, _
@@ -16,25 +17,23 @@
*
*/
-use Keruald\Database\Database;
use Waystone\Workspaces\Engines\Errors\ErrorHandling;
+use Waystone\Workspaces\Engines\Framework\Application;
////////////////////////////////////////////////////////////////////////////////
///
/// Initialization
///
-//Keruald and Obsidian Workspaces libraries
+require_once("includes/autoload_vendor.php");
+Application::init();
include('includes/core.php');
//Prepares the site context
-$context = new Context();
-$context->config = $Config;
-$context->db = $db = Database::load($Config["sql"]);
-$context->session = Session::load();
-$context->url = get_current_url_fragments();
-$context->initializeTemplateEngine($context->config['Theme']);
+
+$context = Application::getContext($Config);
+$db = $context->db;
//Loads language files
Language::initialize();

File Metadata

Mime Type
text/plain
Expires
Fri, Oct 24, 04:44 (20 h, 10 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3099658
Default Alt Text
D3795.id9833.diff (17 KB)

Event Timeline