From 77f3926f7352b06b77ff14bfb9cd1a79a5a09cd1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 4 Nov 2012 00:11:24 +0000 Subject: [PATCH] Speed up of isAdmin() --- inc/mysql-manager.php | 68 +++++++++++++++++++++------------------ inc/session-functions.php | 3 ++ 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/inc/mysql-manager.php b/inc/mysql-manager.php index 8d59df8a9f..a7ecf50e8d 100644 --- a/inc/mysql-manager.php +++ b/inc/mysql-manager.php @@ -562,8 +562,15 @@ function fetchUserData ($value, $column = 'userid') { * login. */ function isAdmin () { + // Is there cache? + if (isset($GLOBALS[__FUNCTION__])) { + // Return it + return $GLOBALS[__FUNCTION__]; + } // END - if + // No admin in installation phase! if ((isInstallationPhase()) || (!isAdminRegistered())) { + $GLOBALS[__FUNCTION__] = FALSE; return FALSE; } // END - if @@ -588,44 +595,43 @@ function isAdmin () { //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Current adminId is zero. isSessionVariableSet(admin_id)=' . intval(isSessionVariableSet('admin_id')) . ',isSessionVariableSet(admin_md5)=' . intval(isSessionVariableSet('admin_md5'))); // Abort here now + $GLOBALS[__FUNCTION__] = FALSE; return FALSE; } // END - if - // Is there cache? - if (!isset($GLOBALS[__FUNCTION__][$adminId])) { - // Init it with failed - $GLOBALS[__FUNCTION__][$adminId] = FALSE; - - // Search in array for entry - if (isset($GLOBALS['admin_hash'])) { - // Use cached string - $valPass = $GLOBALS['admin_hash']; - } elseif ((!empty($passwordFromCookie)) && (isAdminHashSet($adminId) === TRUE) && (!empty($adminId))) { - // Login data is valid or not? - $valPass = encodeHashForCookie(getAdminHash($adminId)); - - // Cache it away - $GLOBALS['admin_hash'] = $valPass; - - // Count cache hits - incrementStatsEntry('cache_hits'); - } elseif ((!empty($adminId)) && ((!isExtensionActive('cache')) || (isAdminHashSet($adminId) === FALSE))) { - // Get admin hash and hash it - $valPass = encodeHashForCookie(getAdminHash($adminId)); - - // Cache it away - $GLOBALS['admin_hash'] = $valPass; - } + // Init it with failed + $GLOBALS[__FUNCTION__] = FALSE; - if (!empty($valPass)) { - // Check if password is valid - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '(' . $valPass . '==' . $passwordFromCookie . ')='.intval($valPass == $passwordFromCookie)); - $GLOBALS[__FUNCTION__][$adminId] = ($GLOBALS['admin_hash'] == $passwordFromCookie); - } // END - if + // Search in array for entry + if (isset($GLOBALS['admin_hash'])) { + // Use cached string + $valPass = $GLOBALS['admin_hash']; + } elseif ((!empty($passwordFromCookie)) && (isAdminHashSet($adminId) === TRUE) && (!empty($adminId))) { + // Login data is valid or not? + $valPass = encodeHashForCookie(getAdminHash($adminId)); + + // Cache it away + $GLOBALS['admin_hash'] = $valPass; + + // Count cache hits + incrementStatsEntry('cache_hits'); + } elseif ((!empty($adminId)) && ((!isExtensionActive('cache')) || (isAdminHashSet($adminId) === FALSE))) { + // Get admin hash and hash it + $valPass = encodeHashForCookie(getAdminHash($adminId)); + + // Cache it away + $GLOBALS['admin_hash'] = $valPass; + } + + // $valPass shall not be empty. If so, the admin has not found. + if (!empty($valPass)) { + // Check if password is valid + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '(' . $valPass . '==' . $passwordFromCookie . ')='.intval($valPass == $passwordFromCookie)); + $GLOBALS[__FUNCTION__] = ($GLOBALS['admin_hash'] == $passwordFromCookie); } // END - if // Return result of comparision - return $GLOBALS[__FUNCTION__][$adminId]; + return $GLOBALS[__FUNCTION__]; } // Generates a list of "max receiveable emails per day" diff --git a/inc/session-functions.php b/inc/session-functions.php index 384e3f465e..7601911fc8 100644 --- a/inc/session-functions.php +++ b/inc/session-functions.php @@ -135,6 +135,9 @@ function destroyAdminSession ($destroy = TRUE) { setAdminMd5(''); setAdminLast(''); + // Set cache to FALSE + $GLOBALS['isAdmin'] = FALSE; + // Destroy session if requested and return status if ($destroy === TRUE) { return session_destroy(); -- 2.39.2