Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F4793498
D1162.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
10 KB
Referenced Files
None
Subscribers
None
D1162.diff
View Options
diff --git a/includes/core.php b/includes/core.php
--- a/includes/core.php
+++ b/includes/core.php
@@ -41,7 +41,7 @@
* @return string the username
*/
function get_username ($user_id) {
- global $db;
+ $db = sql_db::load();
$user_id = $db->sql_escape($user_id);
$sql = 'SELECT username FROM '. TABLE_USERS . " WHERE user_id = '$userid'";
@@ -54,7 +54,7 @@
* @return string the user ID
*/
function get_userid ($username) {
- global $db;
+ $db = sql_db::load();
$username = $db->sql_escape($username);
$sql = 'SELECT user_id FROM '. TABLE_USERS . " WHERE username LIKE '$username'";
diff --git a/includes/document.php b/includes/document.php
--- a/includes/document.php
+++ b/includes/document.php
@@ -254,7 +254,7 @@
* Prints the document body
*/
public function render_body () {
- global $db, $Config, $Session, $CurrentUser;
+ global $Config, $Session, $CurrentUser;
$document = $this;
//404 header
@@ -300,14 +300,14 @@
* Prints the document
*
* Use this method if you don't wish to have access to any other global
- * variables than $db, $Config, $Session and $CurrentUser.
+ * variables than $Config, $Session and $CurrentUser.
*
* A more flexible method is the body of this method in _includes/body.php
* and to add in your skin <?php include('_includes/body.php'); ?>
*/
function render () {
//Global variables for the header and the footer
- global $db, $Config, $Session, $CurrentUser;
+ global $Config, $Session, $CurrentUser;
$document = $this;
//HTML output
diff --git a/includes/error.php b/includes/error.php
--- a/includes/error.php
+++ b/includes/error.php
@@ -78,7 +78,7 @@
break;
case SQL_ERROR:
- global $db;
+ $db = sql_db::load();
$title = $title ? $title : "SQL error";
//Gets SQL error information
diff --git a/includes/mysqli.php b/includes/mysqli.php
--- a/includes/mysqli.php
+++ b/includes/mysqli.php
@@ -27,6 +27,13 @@
private $db;
/**
+ * Singleton instance
+ *
+ * @var sql_db
+ */
+ private static $instance = null;
+
+ /**
* Initializes a new instance of the database abstraction class, for MySQLi engine
*/
function __construct($host = 'localhost', $username = '', $password = '', $database = '') {
@@ -37,6 +44,27 @@
if ($database != '') {
$this->db->select_db($database);
}
+
+ $db->set_charset('utf8');
+ }
+
+ static function load() {
+ if (self::$instance === null) {
+ self::makeSingletonInstance();
+ }
+
+ return self::$instance;
+ }
+
+ private static function makeSingletonInstance() {
+ global $Config;
+
+ self::$instance = new sql_db(
+ $Config['sql']['host'], $Config['sql']['username'],
+ $Config['sql']['password'], $Config['sql']['database']
+ );
+
+ unset($Config['sql']);
}
/**
@@ -137,13 +165,4 @@
$this->db->set_charset($encoding);
}
}
-
- //Creates an instance of this database class with configuration values
- $db = new sql_db($Config['sql']['host'], $Config['sql']['username'], $Config['sql']['password'], $Config['sql']['database']);
-
- //To improve security, we unset sql parameters
- unset($Config['sql']);
-
- //Sets SQL connexion in UTF-8.
- $db->set_charset('utf8');
}
diff --git a/includes/objects/user.php b/includes/objects/user.php
--- a/includes/objects/user.php
+++ b/includes/objects/user.php
@@ -24,7 +24,7 @@
public $active = 0;
public $email;
public $regdate;
-
+
/*
* Initializes a new instance
* @param int $id the primary key
@@ -35,7 +35,7 @@
$this->load_from_database();
}
}
-
+
/*
* Loads the object User (ie fill the properties) from the $_POST array
*/
@@ -47,24 +47,24 @@
if (array_key_exists('email', $_POST)) $this->email = $_POST['email'];
if (array_key_exists('regdate', $_POST)) $this->regdate = $_POST['regdate'];
}
-
+
/*
* Loads the object User (ie fill the properties) from the database
*/
function load_from_database () {
- global $db;
+ $db = sql_db::load();
$sql = "SELECT * FROM " . TABLE_USERS . " WHERE user_id = '" . $this->id . "'";
if ( !($result = $db->sql_query($sql)) ) message_die(SQL_ERROR, "Unable to query users", '', __LINE__, __FILE__, $sql);
if (!$row = $db->sql_fetchrow($result)) {
$this->lastError = "User unkwown: " . $this->id;
return false;
}
-
+
$this->load_from_row($row);
-
+
return true;
}
-
+
/*
* Loads the object User (ie fill the properties) from the database row
*/
@@ -76,13 +76,13 @@
$this->email = $row['user_email'];
$this->regdate = $row['user_regdate'];
}
-
+
/*
* Saves to database
*/
function save_to_database () {
- global $db;
-
+ $db = sql_db::load();
+
$id = $this->id ? "'" . $db->sql_escape($this->id) . "'" : 'NULL';
$name = $db->sql_escape($this->name);
$password = $db->sql_escape($this->password);
@@ -95,18 +95,18 @@
if (!$db->sql_query($sql)) {
message_die(SQL_ERROR, "Unable to save user", '', __LINE__, __FILE__, $sql);
}
-
+
if (!$this->id) {
//Gets new record id value
$this->id = $db->sql_nextid();
}
}
-
+
/*
* Updates the specified field in the database record
*/
function save_field ($field) {
- global $db;
+ $db = sql_db::load();
if (!$this->id) {
message_die(GENERAL_ERROR, "You're trying to update a record not yet saved in the database");
}
@@ -117,13 +117,13 @@
message_die(SQL_ERROR, "Unable to save $field field", '', __LINE__, __FILE__, $sql);
}
}
-
+
/*
* Generates a unique user id
*/
function generate_id () {
- global $db;
-
+ $db = sql_db::load();
+
do {
$this->id = mt_rand(2001, 9999);
$sql = "SELECT COUNT(*) FROM " . TABLE_USERS . " WHERE user_id = $this->id";
@@ -131,9 +131,9 @@
message_die(SQL_ERROR, "Can't check if a user id is free", '', __LINE__, __FILE__, $sql);
}
$row = $db->sql_fetchrow($result);
- } while ($row[0]);
+ } while ($row[0]);
}
-
+
/*
* Fills password field with encrypted version
* of the specified clear password
@@ -148,7 +148,8 @@
* @return boolean true if the login is avaiable ; otherwise, false.
*/
public static function is_available_login ($login) {
- global $db;
+ $db = sql_db::load();
+
$sql = "SELECT COUNT(*) FROM " . TABLE_USERS . " WHERE username = '$login'";
if (!$result = $db->sql_query($sql)) {
message_die(SQL_ERROR, "Can't check if the specified login is available", '', __LINE__, __FILE__, $sql);
@@ -156,10 +157,10 @@
$row = $db->sql_fetchrow($result);
return ($row[0] == 0);
}
-
+
/*
* Initializes a new User instance ready to have its property filled
- * @return User the new user instance
+ * @return User the new user instance
*/
public static function create () {
$user = new User();
@@ -167,25 +168,26 @@
$user->active = true;
return $user;
}
-
+
/*
* Gets user from specified e-mail
* @return User the user matching the specified e-mail ; null, if the mail were not found.
*/
public static function get_user_from_email ($mail) {
- global $db;
+ $db = sql_db::load();
+
$sql = "SELECT username FROM " . TABLE_USERS . " WHERE user_email = '$mail'";
if (!$result = $db->sql_query($sql)) {
message_die(SQL_ERROR, "Can't get user", '', __LINE__, __FILE__, $sql);
}
-
+
if ($row = $db->sql_fetchrow($result)) {
//E-mail found.
$user = new User();
$user->load_from_row($row);
return $user;
}
-
+
//E-mail not found.
return null;
}
diff --git a/includes/session.php b/includes/session.php
--- a/includes/session.php
+++ b/includes/session.php
@@ -86,7 +86,8 @@
* ii. sets offline relevant sessions
*/
public static function clean_old_sessions () {
- global $db, $Config;
+ global $Config;
+ $db = sql_db::load();
//Gets session and online status lifetime (in seconds)
//If not specified in config, sets default 5 and 120 minutes values
@@ -109,7 +110,8 @@
* Updates or creates a session in the database
*/
public function update () {
- global $db, $Config;
+ global $Config;
+ $db = sql_db::load();
//Cleans up session
//To boost SQL performances, try a random trigger
@@ -136,7 +138,8 @@
if ($count == -1) {
//Queries sessions table
- global $db, $Config;
+ global $Config;
+ $db = sql_db::load();
$resource = array_key_exists('ResourceID', $Config) ? '\'' . $db->sql_escape($Config['ResourceID']) . '\'' : 'default';
$sql = "SELECT count(*) FROM " . TABLE_SESSIONS . " WHERE session_resource = $resource AND session_online = 1";
@@ -153,7 +156,7 @@
* @return string the session specified field's value
*/
public function get_info ($info) {
- global $db;
+ $db = sql_db::load();
$id = $db->sql_escape($this->id);
$sql = "SELECT `$info` FROM " . TABLE_SESSIONS . " WHERE session_id = '$id'";
@@ -166,7 +169,7 @@
* @param string $value the value to set
*/
public function set_info ($info, $value) {
- global $db;
+ $db = sql_db::load();
$value = ($value === null) ? 'NULL' : "'" . $db->sql_escape($value) . "'";
$id = $db->sql_escape($this->id);
@@ -180,7 +183,7 @@
* @return User the logged user information
*/
public function get_logged_user () {
- global $db;
+ $db = sql_db::load();;
//Gets session information
$id = $db->sql_escape($this->id);
@@ -216,7 +219,7 @@
* @param string $user_id the user ID
*/
public function user_login ($user_id) {
- global $db;
+ $db = sql_db::load();
//Sets specified user ID in sessions table
$user_id = $db->sql_escape($user_id);
@@ -230,7 +233,7 @@
* Updates the session in an user logout context
*/
public function user_logout () {
- global $db;
+ $db = sql_db::load();
//Sets anonymous user in sessions table
$user_id = $db->sql_escape(ANONYMOUS_USER);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Feb 27, 21:53 (17 h, 12 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2447910
Default Alt Text
D1162.diff (10 KB)
Attached To
Mode
D1162: Initialize MySQL instance when needed
Attached
Detach File
Event Timeline
Log In to Comment