Reverted of changes in 1704, see ticket #160
[mailer.git] / inc / mysql-manager.php
index bd453fac30a4aa7c3b1f44fd84a53c971608df86..7d64dcb2413784b242f03bc793875104daf95a8b 100644 (file)
@@ -18,6 +18,7 @@
  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -261,7 +262,7 @@ function checkModulePermissions ($module = '') {
 
                        // Destroy cache here
                        // @TODO Rewrite this to a filter
-                       if ((getOutputMode() == '0') || (getOutputMode() == -1)) rebuildCacheFile('modules', 'modules');
+                       if ((getOutputMode() == '0') || (getOutputMode() == -1)) rebuildCache('modules', 'modules');
 
                        // And reload data
                        unset($GLOBALS['module_status'][$module]);
@@ -272,7 +273,7 @@ function checkModulePermissions ($module = '') {
                }
        } elseif (($ret == 'cache_miss') && (getOutputMode() == '0')) {
                // Rebuild the cache files
-               rebuildCacheFile('modules', 'modules');
+               rebuildCache('modules', 'modules');
        } elseif ($found === false) {
                // Problem with module detected
                logDebugMessage(__FUNCTION__, __LINE__, sprintf("Problem in module %s detected. ret=%s, locked=%s, hidden=%s, mem=%s, admin=%s, output_mode=%s",
@@ -559,16 +560,20 @@ function isMember () {
        // is the cache entry there?
        if (isset($GLOBALS['is_member'])) {
                // Then return it
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHED! (' . intval($GLOBALS['is_member']) . ')');
                return $GLOBALS['is_member'];
-       } elseif (getMemberId() == '0') {
+       } elseif ((!isSessionVariableSet('userid')) || (!isSessionVariableSet('u_hash'))) {
                // No member
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'No member set in cookie/session.');
                return false;
        } else {
-               // Transfer userid=>current
-               setCurrentUserid(getMemberId());
+               // Get it secured from session
+               setMemberId(getSession('userid'));
+               setCurrentUserId(getMemberId());
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . getSession('userid') . ' used from cookie/session.');
        }
 
-       // Init global user data array
+       // Init user data array
        initUserData();
 
        // Fix "deleted" cookies first
@@ -579,7 +584,7 @@ function isMember () {
                // Cookies are set with values, but are they valid?
                if (fetchUserData(getMemberId()) === true) {
                        // Validate password by created the difference of it and the secret key
-                       $valPass = generatePassString(getUserData('password'));
+                       $valPass = encodeHashForCookie(getUserData('password'));
 
                        // Transfer last module and online time
                        $GLOBALS['last_online']['module'] = getUserData('last_module');
@@ -591,15 +596,17 @@ function isMember () {
                                $ret = true;
                        } else {
                                // Maybe got locked etc.
-                               logDebugMessage(__FUNCTION__, __LINE__, 'status=' . getUserData('status'));
+                               //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'status=' . getUserData('status') . ',' . $valPass . '(' . strlen($valPass) . ')/' . getSession('u_hash') . '(' . strlen(getSession('u_hash')) . ')/' . getUserData('password') . '(' . strlen(getUserData('password')) . ')');
                                destroyMemberSession();
                        }
                } else {
                        // Cookie data is invalid!
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cookie data invalid or user not found.');
                        destroyMemberSession();
                }
        } else {
                // Cookie data is invalid!
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cookie data not complete.');
                destroyMemberSession();
        }
 
@@ -607,6 +614,7 @@ function isMember () {
        $GLOBALS['is_member'] = $ret;
 
        // Return status
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . intval($ret));
        return $ret;
 }
 
@@ -693,42 +701,42 @@ function fetchUserData ($userid, $column = 'userid') {
 }
 
 // This patched function will reduce many SELECT queries for the specified or current admin login
-function isAdmin ($admin = '') {
+function isAdmin ($adminLogin = '') {
        // Init variables
        $ret = false;
        $passCookie = '';
        $valPass = '';
-       //* DEBUG: */ print(__FUNCTION__.':'.$admin.'<br />');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $adminLogin.'<br />');
 
        // If admin login is not given take current from cookies...
-       if ((empty($admin)) && (isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5'))) {
+       if ((empty($adminLogin)) && (isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5'))) {
                // Get admin login and password from session/cookies
-               $admin = getSession('admin_login');
+               $adminLogin = getSession('admin_login');
                $passCookie = getSession('admin_md5');
        } // END - if
-       //* DEBUG: */ print(__FUNCTION__.':'.$admin.'/'.$passCookie.'<br />');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $adminLogin.'/'.$passCookie.'<br />');
 
        // Do we have cache?
-       if (!isset($GLOBALS['is_admin'][$admin])) {
+       if (!isset($GLOBALS['is_admin'][$adminLogin])) {
                // Init it with failed
-               $GLOBALS['is_admin'][$admin] = false;
+               $GLOBALS['is_admin'][$adminLogin] = false;
 
                // Search in array for entry
                if (isset($GLOBALS['admin_hash'])) {
                        // Use cached string
                        $valPass = $GLOBALS['admin_hash'];
-               } elseif ((!empty($passCookie)) && (isAdminHashSet($admin) === true) && (!empty($admin))) {
+               } elseif ((!empty($passCookie)) && (isAdminHashSet($adminLogin) === true) && (!empty($adminLogin))) {
                        // Login data is valid or not?
-                       $valPass = generatePassString(getAdminHash($admin));
+                       $valPass = encodeHashForCookie(getAdminHash($adminLogin));
 
                        // Cache it away
                        $GLOBALS['admin_hash'] = $valPass;
 
                        // Count cache hits
                        incrementStatsEntry('cache_hits');
-               } elseif ((!empty($admin)) && ((!isExtensionActive('cache')) || (isAdminHashSet($admin) === false))) {
+               } elseif ((!empty($adminLogin)) && ((!isExtensionActive('cache')) || (isAdminHashSet($adminLogin) === false))) {
                        // Get admin hash and hash it
-                       $valPass = generatePassString(getAdminHash($admin));
+                       $valPass = encodeHashForCookie(getAdminHash($adminLogin));
 
                        // Cache it away
                        $GLOBALS['admin_hash'] = $valPass;
@@ -736,13 +744,13 @@ function isAdmin ($admin = '') {
 
                if (!empty($valPass)) {
                        // Check if password is valid
-                       //* DEBUG: */ print(__FUNCTION__ . ':(' . $valPass . '==' . $passCookie . ')='.intval($valPass == $passCookie).'<br />');
-                       $GLOBALS['is_admin'][$admin] = (($valPass == $passCookie) || ((strlen($valPass) == 32) && ($valPass == md5($passCookie))) || (($valPass == '*FAILED*') && (!isExtensionActive('cache'))));
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '(' . $valPass . '==' . $passCookie . ')='.intval($valPass == $passCookie));
+                       $GLOBALS['is_admin'][$adminLogin] = (($valPass == $passCookie) || ((strlen($valPass) == 32) && ($valPass == md5($passCookie))) || (($valPass == '*FAILED*') && (!isExtensionActive('cache'))));
                } // END - if
        } // END - if
 
        // Return result of comparision
-       return $GLOBALS['is_admin'][$admin];
+       return $GLOBALS['is_admin'][$adminLogin];
 }
 
 // Generates a list of "max receiveable emails per day"
@@ -1246,7 +1254,7 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
 
                                // Send email
                                sendEmail($userid, getMessage('THANX_REFERAL_ONE_SUBJECT'), $message);
-                       } elseif (($sendNotify) && (getUserData('refid') == '0') && ($locked === false) && ($add_mode == 'direct')) {
+                       } elseif (($sendNotify === true) && (getUserData('refid') == '0') && ($locked === false) && ($add_mode == 'direct')) {
                                // Prepare content
                                $content = array(
                                        'text'   => getMessage('REASON_DIRECT_PAYMENT'),
@@ -1312,7 +1320,7 @@ function updateReferalCounter ($userid) {
        } elseif ((($ref == $userid) || ($ref == '0')) && (isExtensionInstalledAndNewer('cache', '0.1.2'))) {
                // Remove cache here
                //* DEBUG: */ print(__FUNCTION__ . '(<font color="#0000aa">' . __LINE__."</font>):ref={$ref} - CACHE!<br />");
-               rebuildCacheFile('refsystem', 'refsystem');
+               rebuildCache('refsystem', 'refsystem');
        }
 
        // "Walk" back here
@@ -1341,21 +1349,21 @@ function sendAdminEmails ($subj, $message) {
 }
 
 // Get id number from administrator's login name
-function getAdminId ($login) {
+function getAdminId ($adminLogin) {
        // By default no admin is found
        $data['id'] = '-1';
 
        // Check cache
-       if (isset($GLOBALS['cache_array']['admin']['admin_id'][$login])) {
+       if (isset($GLOBALS['cache_array']['admin']['admin_id'][$adminLogin])) {
                // Use it if found to save SQL queries
-               $data['id'] = $GLOBALS['cache_array']['admin']['admin_id'][$login];
+               $data['id'] = $GLOBALS['cache_array']['admin']['admin_id'][$adminLogin];
 
                // Update cache hits
                incrementStatsEntry('cache_hits');
        } elseif (!isExtensionActive('cache')) {
                // Load from database
                $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `login`='%s' LIMIT 1",
-                       array($login), __FUNCTION__, __LINE__);
+                       array($adminLogin), __FUNCTION__, __LINE__);
 
                // Do we have an entry?
                if (SQL_NUMROWS($result) == 1) {
@@ -1382,28 +1390,34 @@ function getCurrentAdminId () {
                $adminId = getAdminId($adminLogin);
 
                // Remember in cache securely
-               $GLOBALS['current_admin_id'] = bigintval($adminId);
+               setCurrentAdminId(bigintval($adminId));
        } // END - if
 
        // Return it
        return $GLOBALS['current_admin_id'];
 }
 
+// Setter for current admin id
+function setCurrentAdminId ($currentAdminId) {
+       // Set it secured
+       $GLOBALS['current_admin_id'] = bigintval($currentAdminId);
+}
+
 // Get password hash from administrator's login name
-function getAdminHash ($admin) {
+function getAdminHash ($adminLogin) {
        // By default an invalid hash is returned
        $data['password'] = '-1';
 
-       if (isAdminHashSet($admin)) {
+       if (isAdminHashSet($adminLogin)) {
                // Check cache
-               $data['password'] = $GLOBALS['cache_array']['admin']['password'][$admin];
+               $data['password'] = $GLOBALS['cache_array']['admin']['password'][$adminLogin];
 
                // Update cache hits
                incrementStatsEntry('cache_hits');
        } elseif (!isExtensionActive('cache')) {
                // Load from database
                $result = SQL_QUERY_ESC("SELECT `password` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `login`='%s' LIMIT 1",
-                       array($admin), __FUNCTION__, __LINE__);
+                       array($adminLogin), __FUNCTION__, __LINE__);
 
                // Do we have an entry?
                if (SQL_NUMROWS($result) == 1) {
@@ -1411,7 +1425,7 @@ function getAdminHash ($admin) {
                        $data = SQL_FETCHARRAY($result);
 
                        // Set cache
-                       setAdminHash($admin, $data['password']);
+                       setAdminHash($adminLogin, $data['password']);
                } // END - if
 
                // Free result
@@ -1613,7 +1627,7 @@ function FILTER_ACTIVATE_EXCHANGE () {
                updateConfiguration('activate_xchange' ,0);
 
                // Rebuild cache
-               rebuildCacheFile('modules', 'modules');
+               rebuildCache('modules', 'modules');
        } // END - if
 }