Added $nocache parameter

Fixed typo in caching
Excluded caching for MySQL-Authentication & /poll-log/
This commit is contained in:
Daniel Preussker
2015-09-30 15:20:06 +00:00
parent a321ba1bf6
commit afdbb2406d
6 changed files with 41 additions and 40 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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