From afdbb2406d8108d2cbfcf0ba12ef42015667bc96 Mon Sep 17 00:00:00 2001 From: Daniel Preussker Date: Wed, 30 Sep 2015 15:20:06 +0000 Subject: [PATCH] Added `$nocache` parameter Fixed typo in caching Excluded caching for MySQL-Authentication & /poll-log/ --- html/includes/authentication/mysql.inc.php | 14 +++++----- html/includes/table/poll-log.inc.php | 2 +- html/index.php | 4 ++- includes/dbFacile.mysql.php | 24 ++++++++--------- includes/dbFacile.mysqli.php | 30 +++++++++++----------- includes/definitions.inc.php | 7 +++-- 6 files changed, 41 insertions(+), 40 deletions(-) diff --git a/html/includes/authentication/mysql.inc.php b/html/includes/authentication/mysql.inc.php index 84e119cea..c16e716f8 100644 --- a/html/includes/authentication/mysql.inc.php +++ b/html/includes/authentication/mysql.inc.php @@ -3,7 +3,7 @@ function authenticate($username, $password) { $encrypted_old = md5($password); - $row = dbFetchRow('SELECT username,password FROM `users` WHERE `username`= ?', array($username)); + $row = dbFetchRow('SELECT username,password FROM `users` WHERE `username`= ?', array($username), true); if ($row['username'] && $row['username'] == $username) { // Migrate from old, unhashed password if ($row['password'] == $encrypted_old) { @@ -36,7 +36,7 @@ function authenticate($username, $password) { function reauthenticate($sess_id, $token) { list($uname,$hash) = explode('|', $token); - $session = dbFetchRow("SELECT * FROM `session` WHERE `session_username` = '$uname' AND session_value='$sess_id'"); + $session = dbFetchRow("SELECT * FROM `session` WHERE `session_username` = '$uname' AND session_value='$sess_id'", array(), true); $hasher = new PasswordHash(8, false); if ($hasher->CheckPassword($uname.$session['session_token'], $hash)) { $_SESSION['username'] = $uname; @@ -59,7 +59,7 @@ function passwordscanchange($username='') { return 1; } else { - return dbFetchCell('SELECT can_modify_passwd FROM users WHERE username = ?', array($username)); + return dbFetchCell('SELECT can_modify_passwd FROM users WHERE username = ?', array($username), true); } }//end passwordscanchange() @@ -114,20 +114,20 @@ function adduser($username, $password, $level, $email='', $realname='', $can_mod function user_exists($username) { - $return = @dbFetchCell('SELECT COUNT(*) FROM users WHERE username = ?', array($username)); + $return = @dbFetchCell('SELECT COUNT(*) FROM users WHERE username = ?', array($username), true); return $return; }//end user_exists() function get_userlevel($username) { - return dbFetchCell('SELECT `level` FROM `users` WHERE `username` = ?', array($username)); + return dbFetchCell('SELECT `level` FROM `users` WHERE `username` = ?', array($username), true); }//end get_userlevel() function get_userid($username) { - return dbFetchCell('SELECT `user_id` FROM `users` WHERE `username` = ?', array($username)); + return dbFetchCell('SELECT `user_id` FROM `users` WHERE `username` = ?', array($username), true); }//end get_userid() @@ -158,7 +158,7 @@ function can_update_users() { function get_user($user_id) { - return dbFetchRow('SELECT * FROM `users` WHERE `user_id` = ?', array($user_id)); + return dbFetchRow('SELECT * FROM `users` WHERE `user_id` = ?', array($user_id), true); }//end get_user() diff --git a/html/includes/table/poll-log.inc.php b/html/includes/table/poll-log.inc.php index 0b0ce89c4..722c47d31 100644 --- a/html/includes/table/poll-log.inc.php +++ b/html/includes/table/poll-log.inc.php @@ -42,7 +42,7 @@ if ($rowCount != -1) { $sql = "SELECT D.device_id,D.hostname AS `hostname`, D.last_polled AS `last_polled`, `group_name`, D.last_polled_timetaken AS `last_polled_timetaken` $sql"; -foreach (dbFetchRows($sql) as $device) { +foreach (dbFetchRows($sql,array(),true) as $device) { if (empty($device['group_name'])) { $device['group_name'] = 'General'; } diff --git a/html/index.php b/html/index.php index 37cff02ea..60a09e494 100644 --- a/html/index.php +++ b/html/index.php @@ -32,7 +32,7 @@ function catchFatal() { } } -if (strpos($_SERVER['PATH_INFO'], "debug")) { +if (strpos($_SERVER['PATH_INFO'], "debug") || true) { $debug = "1"; ini_set('display_errors', 0); ini_set('display_startup_errors', 1); @@ -66,6 +66,8 @@ require 'includes/functions.inc.php'; require 'includes/vars.inc.php'; require 'includes/plugins.inc.php'; +$config['memcached']['ttl'] = $config['time']['now']+300; + Plugins::start(); $runtime_start = utime(); diff --git a/includes/dbFacile.mysql.php b/includes/dbFacile.mysql.php index e51a695b7..bdcce1ac1 100644 --- a/includes/dbFacile.mysql.php +++ b/includes/dbFacile.mysql.php @@ -238,10 +238,10 @@ function dbDelete($table, $where=null, $parameters=array()) { * */ -function dbFetchRows($sql, $parameters=array()) { +function dbFetchRows($sql, $parameters=array(), $nocache=false) { global $db_stats, $config; - if ($config['memcached']['enable']) { + if ($config['memcached']['enable'] && $nocache === false) { $result = $config['memcached']['resource']->get(hash('sha512',$sql.'|'.serialize($parameters))); if (!empty($result)) { return $result; @@ -258,7 +258,7 @@ function dbFetchRows($sql, $parameters=array()) { } mysql_free_result($result); - if ($config['memcached']['enable']) { + if ($config['memcached']['enable'] && $nocache === false) { $config['memcached']['resource']->set(hash('sha512',$sql.'|'.serialize($parameters)),$rows,$config['memcached']['ttl']); } return $rows; @@ -283,8 +283,8 @@ function dbFetchRows($sql, $parameters=array()) { * */ -function dbFetch($sql, $parameters=array()) { - return dbFetchRows($sql, $parameters); +function dbFetch($sql, $parameters=array(), $nocache=false) { + return dbFetchRows($sql, $parameters, $nocache); /* // for now, don't do the iterator thing $result = dbQuery($sql, $parameters); @@ -305,10 +305,10 @@ function dbFetch($sql, $parameters=array()) { * */ -function dbFetchRow($sql=null, $parameters=array()) { +function dbFetchRow($sql=null, $parameters=array(), $nocache=false) { global $db_stats, $config; - if ($config['memcached']['enable']) { + if ($config['memcached']['enable'] && $nocache === false) { $result = $config['memcached']['resource']->get(hash('sha512',$sql.'|'.serialize($parameters))); if (!empty($result)) { return $result; @@ -325,7 +325,7 @@ function dbFetchRow($sql=null, $parameters=array()) { $db_stats['fetchrow_sec'] += number_format(($time_end - $time_start), 8); $db_stats['fetchrow']++; - if ($config['memcached']['enable']) { + if ($config['memcached']['enable'] && $nocache === false) { $config['memcached']['resource']->set(hash('sha512',$sql.'|'.serialize($parameters)),$row,$config['memcached']['ttl']); } return $row; @@ -344,10 +344,10 @@ function dbFetchRow($sql=null, $parameters=array()) { * */ -function dbFetchCell($sql, $parameters=array()) { +function dbFetchCell($sql, $parameters=array(), $nocache=false) { global $db_stats; $time_start = microtime(true); - $row = dbFetchRow($sql, $parameters); + $row = dbFetchRow($sql, $parameters, $nocache); if ($row) { return array_shift($row); // shift first field off first row @@ -369,11 +369,11 @@ function dbFetchCell($sql, $parameters=array()) { * */ -function dbFetchColumn($sql, $parameters=array()) { +function dbFetchColumn($sql, $parameters=array(), $nocache=false) { global $db_stats; $time_start = microtime(true); $cells = array(); - foreach (dbFetch($sql, $parameters) as $row) { + foreach (dbFetch($sql, $parameters, $nocache) as $row) { $cells[] = array_shift($row); } diff --git a/includes/dbFacile.mysqli.php b/includes/dbFacile.mysqli.php index e956e78ba..9e49491a6 100644 --- a/includes/dbFacile.mysqli.php +++ b/includes/dbFacile.mysqli.php @@ -239,10 +239,10 @@ function dbDelete($table, $where=null, $parameters=array()) { * */ -function dbFetchRows($sql, $parameters=array()) { +function dbFetchRows($sql, $parameters=array(), $nocache=false) { global $db_stats, $config; - if ($config['memcached']['enable']) { + if ($config['memcached']['enable'] && $nocache === false) { $result = $config['memcached']['resource']->get(hash('sha512',$sql.'|'.serialize($parameters))); if (!empty($result)) { return $result; @@ -259,7 +259,7 @@ function dbFetchRows($sql, $parameters=array()) { } mysqli_free_result($result); - if ($config['memcached']['enable']) { + if ($config['memcached']['enable'] && $nocache === false) { $config['memcached']['resource']->set(hash('sha512',$sql.'|'.serialize($parameters)),$rows,$config['memcached']['ttl']); } return $rows; @@ -284,8 +284,8 @@ function dbFetchRows($sql, $parameters=array()) { * */ -function dbFetch($sql, $parameters=array()) { - return dbFetchRows($sql, $parameters); +function dbFetch($sql, $parameters=array(), $nocache=false) { + return dbFetchRows($sql, $parameters, $nocache); /* // for now, don't do the iterator thing $result = dbQuery($sql, $parameters); @@ -306,10 +306,10 @@ function dbFetch($sql, $parameters=array()) { * */ -function dbFetchRow($sql=null, $parameters=array()) { +function dbFetchRow($sql=null, $parameters=array(), $nocache=false) { global $db_stats, $config; - if ($config['memcached']['enable']) { + if ($config['memcached']['enable'] && $nocache === false) { $result = $config['memcached']['resource']->get(hash('sha512',$sql.'|'.serialize($parameters))); if (!empty($result)) { return $result; @@ -326,8 +326,8 @@ function dbFetchRow($sql=null, $parameters=array()) { $db_stats['fetchrow_sec'] += number_format(($time_end - $time_start), 8); $db_stats['fetchrow']++; - if ($config['memcached']['enable']) { - $config['memcached']['resource']->set(hash('sha512',$sql.'|'.serialize($parameters)),$rows,$config['memcached']['ttl']); + if ($config['memcached']['enable'] && $nocache === false) { + $config['memcached']['resource']->set(hash('sha512',$sql.'|'.serialize($parameters)),$row,$config['memcached']['ttl']); } return $row; } @@ -345,11 +345,11 @@ function dbFetchRow($sql=null, $parameters=array()) { * */ -function dbFetchCell($sql, $parameters=array()) { +function dbFetchCell($sql, $parameters=array(), $nocache=false) { global $db_stats, $config; $time_start = microtime(true); - $row = dbFetchRow($sql, $parameters); + $row = dbFetchRow($sql, $parameters, $nocache); if ($row) { return array_shift($row); // shift first field off first row @@ -371,11 +371,11 @@ function dbFetchCell($sql, $parameters=array()) { * */ -function dbFetchColumn($sql, $parameters=array()) { +function dbFetchColumn($sql, $parameters=array(), $nocache=false) { global $db_stats; $time_start = microtime(true); $cells = array(); - foreach (dbFetch($sql, $parameters) as $row) { + foreach (dbFetch($sql, $parameters, $nocache) as $row) { $cells[] = array_shift($row); } @@ -396,9 +396,9 @@ function dbFetchColumn($sql, $parameters=array()) { */ -function dbFetchKeyValue($sql, $parameters=array()) { +function dbFetchKeyValue($sql, $parameters=array(), $nocache=false) { $data = array(); - foreach (dbFetch($sql, $parameters) as $row) { + foreach (dbFetch($sql, $parameters, $nocache) as $row) { $key = array_shift($row); if (sizeof($row) == 1) { // if there were only 2 fields in the result diff --git a/includes/definitions.inc.php b/includes/definitions.inc.php index 23f6dd97b..327e5e574 100644 --- a/includes/definitions.inc.php +++ b/includes/definitions.inc.php @@ -30,12 +30,9 @@ else { $database_db = mysql_select_db($config['db_name'], $database_link); } -$config['time']['now'] = time(); -$config['time']['now'] -= ($config['time']['now'] % 300); - if ($config['memcached']['enable'] === true) { if (class_exists('Memcached')) { - $config['memcached']['ttl'] += $config['time']['now']; + $config['memcached']['ttl'] = 60; $config['memcached']['resource'] = new Memcached(); $config['memcached']['resource']->addServer($config['memcached']['host'], $config['memcached']['port']); } @@ -1720,6 +1717,8 @@ if (isset($_SERVER['HTTPS'])) { } // Set some times needed by loads of scripts (it's dynamic, so we do it here!) +$config['time']['now'] = time(); +$config['time']['now'] -= ($config['time']['now'] % 300); $config['time']['fourhour'] = ($config['time']['now'] - 14400); // time() - (4 * 60 * 60); $config['time']['sixhour'] = ($config['time']['now'] - 21600);