X-Git-Url: https://git.mxchange.org/?p=mailer.git;a=blobdiff_plain;f=inc%2Fmysql-manager.php;h=2aedbdb8cf4a184929ffdf4ff3e81035af6b5c4e;hp=6297f28228010ac93de374857fa4eac1cd1ad9c3;hb=74ea26a36ff202cfdca545025a17c9faf4d68efb;hpb=b8a6f8012aa3509d8e0f8fd078e044f20e80707a diff --git a/inc/mysql-manager.php b/inc/mysql-manager.php index 6297f28228..2aedbdb8cf 100644 --- a/inc/mysql-manager.php +++ b/inc/mysql-manager.php @@ -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 * @@ -42,29 +43,29 @@ if (!defined('__SECURITY')) { } // END - if // "Getter" for module title -function getModuleTitle ($mod) { +function getModuleTitle ($module) { // Init variables - $title = ''; + $data['title'] = ''; $result = false; // Is the script installed? if (isInstalled()) { // Check if cache is valid - if ((isExtensionInstalledAndNewer('cache', '0.1.2')) && (isset($GLOBALS['cache_array']['modules']['module'])) && (in_array($mod, $GLOBALS['cache_array']['modules']['module']))) { + if ((isExtensionInstalledAndNewer('cache', '0.1.2')) && (isset($GLOBALS['cache_array']['modules']['module'])) && (in_array($module, $GLOBALS['cache_array']['modules']['module']))) { // Load from cache - $title = $GLOBALS['cache_array']['modules']['title'][$mod]; + $data['title'] = $GLOBALS['cache_array']['modules']['title'][$module]; // Update cache hits incrementStatsEntry('cache_hits'); } elseif (!isExtensionActive('cache')) { // Load from database $result = SQL_QUERY_ESC("SELECT `title` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1", - array($mod), __FUNCTION__, __LINE__); + array($module), __FUNCTION__, __LINE__); // Is the entry there? if (SQL_NUMROWS($result)) { // Get the title from database - list($title) = SQL_FETCHROW($result); + $data = SQL_FETCHARRAY($result); } // END - if // Free the result @@ -73,20 +74,20 @@ function getModuleTitle ($mod) { } // END - if // Trim name - $title = trim($title); + $data['title'] = trim($data['title']); // Still no luck or empty title? - if (empty($title)) { + if (empty($data['title'])) { // No name found - $title = sprintf("%s (%s)", getMessage('LANG_UNKNOWN_MODULE'), $mod); + $data['title'] = sprintf("%s (%s)", getMessage('LANG_UNKNOWN_MODULE'), $module); if (SQL_NUMROWS($result) == '0') { // Add module to database - $dummy = checkModulePermissions($mod); + $dummy = checkModulePermissions($module); } // END - if } // END - if // Return name - return $title; + return $data['title']; } // "Getter" for module description @@ -98,7 +99,7 @@ function getTitleFromMenu ($mode, $what, $column = 'what', $ADD='') { } // END - if // Default is not found - $ret = '??? (' . $what . ')'; + $data['title'] = '??? (' . $what . ')'; // Look for title $result = SQL_QUERY_ESC("SELECT `title` FROM `{?_MYSQL_PREFIX?}_%s_menu` WHERE `%s`='%s'" . $ADD . " LIMIT 1", @@ -111,40 +112,40 @@ function getTitleFromMenu ($mode, $what, $column = 'what', $ADD='') { // Is there an entry? if (SQL_NUMROWS($result) == 1) { // Fetch the title - list($ret) = SQL_FETCHROW($result); + $data = SQL_FETCHARRAY($result); } // END - if // Free result SQL_FREERESULT($result); // Return it - return $ret; + return $data['title']; } // Check validity of a given module name (no file extension) -function checkModulePermissions ($mod = '') { +function checkModulePermissions ($module = '') { // Is it empty (default), then take the current one - if (empty($mod)) $mod = getModule(); + if (empty($module)) $module = getModule(); // Do we have cache? - if (isset($GLOBALS['module_status'][$mod])) { + if (isset($GLOBALS['module_status'][$module])) { // Then use it - return $GLOBALS['module_status'][$mod]; + return $GLOBALS['module_status'][$module]; } // END - if // Filter module name (names with low chars and underlines are fine!) - $mod = preg_replace('/[^a-z_]/', '', $mod); + $module = preg_replace('/[^a-z_]/', '', $module); // Check for prefix is a extension... - $modSplit = explode('_', $mod); - $extension = ''; $mod_chk = $mod; - //* DEBUG: */ print(__LINE__."*".count($modSplit)."*/".$mod."*
"); + $modSplit = explode('_', $module); + $extension = ''; $module_chk = $module; + //* DEBUG: */ print(__LINE__."*".count($modSplit)."*/".$module."*
"); if (count($modSplit) == 2) { // Okay, there is a seperator (_) in the name so is the first part a module? //* DEBUG: */ print(__LINE__."*".$modSplit[0]."*
"); if (isExtensionActive($modSplit[0])) { // The prefix is an extension's name, so let's set it - $extension = $modSplit[0]; $mod = $modSplit[1]; + $extension = $modSplit[0]; $module = $modSplit[1]; } // END - if } // END - if @@ -157,24 +158,28 @@ function checkModulePermissions ($mod = '') { return 'done'; } // END - if - // Init variables - $locked = 'Y'; - $hidden = 'N'; - $admin = 'N'; - $mem = 'N'; + // Init data array + $data = array( + 'locked' => 'Y', + 'hidden' => 'N', + 'admin_only' => 'N', + 'mem_only' => 'N' + ); + + // By default nothing is found $found = false; // Check if cache is latest version if (isExtensionInstalledAndNewer('cache', '0.1.2')) { // Is the cache there? //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using cache.'); - if (isset($GLOBALS['cache_array']['modules']['locked'][$mod_chk])) { + if (isset($GLOBALS['cache_array']['modules']['locked'][$module_chk])) { // Check cache //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cache found.'); - $locked = $GLOBALS['cache_array']['modules']['locked'][$mod_chk]; - $hidden = $GLOBALS['cache_array']['modules']['hidden'][$mod_chk]; - $admin = $GLOBALS['cache_array']['modules']['admin_only'][$mod_chk]; - $mem = $GLOBALS['cache_array']['modules']['mem_only'][$mod_chk]; + $data['locked'] = $GLOBALS['cache_array']['modules']['locked'][$module_chk]; + $data['hidden'] = $GLOBALS['cache_array']['modules']['hidden'][$module_chk]; + $data['admin_only'] = $GLOBALS['cache_array']['modules']['admin_only'][$module_chk]; + $data['mem_only'] = $GLOBALS['cache_array']['modules']['mem_only'][$module_chk]; // Update cache hits incrementStatsEntry('cache_hits'); @@ -187,15 +192,15 @@ function checkModulePermissions ($mod = '') { // Check for module in database //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using database.'); $result = SQL_QUERY_ESC("SELECT `locked`, `hidden`, `admin_only`, `mem_only` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1", - array($mod_chk), __FUNCTION__, __LINE__); + array($module_chk), __FUNCTION__, __LINE__); if (SQL_NUMROWS($result) == 1) { // Read data //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Entry found.'); - list($locked, $hidden, $admin, $mem) = SQL_FETCHROW($result); + $data = SQL_FETCHARRAY($result); $found = true; } elseif (isDebugModeEnabled()) { // Debug message only in debug-mode... - logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $mod_chk . ' not found!'); + logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module_chk . ' not found!'); } // Free result @@ -207,26 +212,26 @@ function checkModulePermissions ($mod = '') { if ($found === true) { // Check returned values against current access permissions // - // Admin access ----- Guest access ----- --- Guest or member? --- - if ((isAdmin()) || (($locked != 'Y') && ($admin != 'Y') && (($mem != 'Y') || (isMember())))) { + // Admin access ----- Guest access ----- --- Guest or member? --- + if ((isAdmin()) || (($data['locked'] != 'Y') && ($data['admin_only'] != 'Y') && (($data['mem_only'] != 'Y') || (isMember())))) { // If you are admin you are welcome for everything! $ret = 'done'; - } elseif ($locked == 'Y') { + } elseif ($data['locked'] == 'Y') { // Module is locked $ret = 'locked'; - } elseif (($mem == 'Y') && (!isMember())) { + } elseif (($data['mem_only'] == 'Y') && (!isMember())) { // You have to login first! $ret = 'mem_only'; - } elseif (($admin == 'Y') && (!isAdmin())) { + } elseif (($data['admin_only'] == 'Y') && (!isAdmin())) { // Only the Admin is allowed to enter this module! $ret = 'admin_only'; } else { // @TODO Nothing helped??? logDebugMessage(__FUNCTION__, __LINE__, sprintf("ret=%s,locked=%s,admin=%s,mem=%s", $ret, - $locked, - $admin, - $mem + $data['locked'], + $data['admin_only'], + $data['mem_only'] )); } } // END - if @@ -234,19 +239,19 @@ function checkModulePermissions ($mod = '') { // Still no luck or not found? if (($found === false) && (!isExtensionActive('cache')) && ($ret != 'done')) { // ----- Legacy module ----- ---- Module in base folder ---- --- Module with extension's name --- - if ((isIncludeReadable(sprintf("inc/modules/%s.php", $mod))) || (isIncludeReadable(sprintf("%s.php", $mod))) || (isIncludeReadable(sprintf("%s/%s.php", $extension, $mod)))) { + if ((isIncludeReadable(sprintf("inc/modules/%s.php", $module))) || (isIncludeReadable(sprintf("%s.php", $module))) || (isIncludeReadable(sprintf("%s/%s.php", $extension, $module)))) { // Data is missing so we add it if (isExtensionInstalledAndNewer('sql_patches', '0.3.6')) { // Since 0.3.6 we have a has_menu column, this took me a half hour // to find a loop here... *sigh* SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg` (`module`, `locked`, `hidden`, `mem_only`, `admin_only`, `has_menu`) VALUES -('%s','Y','N','N','N','N')", array($mod_chk), __FUNCTION__, __LINE__); +('%s','Y','N','N','N','N')", array($module_chk), __FUNCTION__, __LINE__); } else { // Wrong/missing sql_patches! SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg` (`module`, `locked`, `hidden`, `mem_only`, `admin_only`) VALUES -('%s','Y','N','N','N')", array($mod_chk), __FUNCTION__, __LINE__); +('%s','Y','N','N','N')", array($module_chk), __FUNCTION__, __LINE__); } // Everthing is fine? @@ -257,34 +262,34 @@ function checkModulePermissions ($mod = '') { // 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'][$mod]); - $ret = checkModulePermissions($mod_chk); + unset($GLOBALS['module_status'][$module]); + $ret = checkModulePermissions($module_chk); } else { // Module not found we don't add it to the database $ret = '404'; } } 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", - $mod, + $module, $ret, - $locked, - $hidden, - $mem, - $admin, + $data['locked'], + $data['hidden'], + $data['mem_only'], + $data['admin_only'], getOutputMode() )); } // Return the value //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . $ret); - $GLOBALS['module_status'][$mod] = $ret; + $GLOBALS['module_status'][$module] = $ret; return $ret; } @@ -322,7 +327,7 @@ function addMenuDescription ($accessLevel, $FQFN, $return = false) { if (isAdmin()) $ADD = ''; $dummy = substr($search, 0, -4); - $ADD .= " AND `action`='".getModeAction($accessLevel, $dummy)."'"; + $ADD .= " AND `action`='".getActionFromModuleWhat($accessLevel, $dummy)."'"; } elseif (($accessLevel == 'sponsor') || ($accessLevel == 'engine')) { // Sponsor / engine menu $type = 'what'; @@ -340,7 +345,7 @@ function addMenuDescription ($accessLevel, $FQFN, $return = false) { // Begin the navigation line if ((!isset($GLOBALS['nav_depth'])) && ($return === false)) { $GLOBALS['nav_depth'] = '0'; - $prefix = "
{--YOU_ARE_HERE--} Home"; + $prefix = '
{--YOU_ARE_HERE--} Home'; } else { if ($return === false) $GLOBALS['nav_depth']++; $prefix = ''; @@ -356,14 +361,14 @@ function addMenuDescription ($accessLevel, $FQFN, $return = false) { if (((isExtensionInstalledAndNewer('sql_patches', '0.2.3')) && (getConfig('youre_here') == 'Y')) || ((isAdmin()) && ($modCheck == 'admin'))) { // Output HTML code - $OUT = $prefix . "' . getTitleFromMenu($accessLevel, $search, $type, $ADD) . ''; // Can we close the you-are-here navigation? //* DEBUG: */ print(__LINE__."*".$type.'/'.getWhat()."*
"); if (($type == 'what') || (($type == 'action') && ((!isWhatSet()) || (getWhat() == 'overview')))) { //* DEBUG: */ print(__LINE__.'+'.$type."+
"); // Add closing div and br-tag - $OUT .= "

\n"; + $OUT .= '

'; $GLOBALS['nav_depth'] = '0'; // Run the filter chain @@ -443,29 +448,29 @@ function addMenu ($mode, $action, $what) { if (isIncludeReadable($inc)) { // Mark currently selected menu - open if ((!empty($what)) && (($what == $content['sub_what']))) { - $OUT = ""; + $OUT = ''; } // END - if // Navigation link - $OUT .= ""; + $OUT .= ''; } else { // Not found! - open - $OUT .= ""; + $OUT .= ''; } // Menu title $OUT .= getConfig('menu_blur_spacer') . $content['sub_title']; if (isIncludeReadable($inc)) { - $OUT .= ""; + $OUT .= ''; // Mark currently selected menu - close if ((!empty($what)) && (($what == $content['sub_what']))) { - $OUT .= ""; + $OUT .= ''; } // END - if } else { // Not found! - close - $OUT .= ""; + $OUT .= ''; } // Cunt it up @@ -555,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 @@ -575,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'); @@ -587,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(); } @@ -603,11 +614,12 @@ function isMember () { $GLOBALS['is_member'] = $ret; // Return status + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . intval($ret)); return $ret; } // Fetch user data for given user id -function fetchUserData ($userid, $column='userid') { +function fetchUserData ($userid, $column = 'userid') { // If we should look for userid secure&set it here if (substr($column, -2, 2) == 'id') { // Secure userid @@ -633,8 +645,12 @@ function fetchUserData ($userid, $column='userid') { // By default none was found $found = false; + // Extra statements + $ADD = ''; + if (isExtensionInstalledAndNewer('user', '0.3.5')) $ADD = ', UNIX_TIMESTAMP(`lock_timestamp`) AS `lock_timestamp`'; + // Query for the user - $result = SQL_QUERY_ESC("SELECT * FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `%s`='%s' LIMIT 1", + $result = SQL_QUERY_ESC("SELECT *".$ADD." FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `%s`='%s' LIMIT 1", array($column, $userid), __FUNCTION__, __LINE__); // Do we have a record? @@ -685,68 +701,56 @@ 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.'
'); + $ret = false; + $passCookie = ''; + $valPass = ''; + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $adminLogin.'
'); // 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.'
'); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $adminLogin.'/'.$passCookie.'
'); // 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)) { - // Search for admin - $result = SQL_QUERY_ESC("SELECT HIGH_PRIORITY `password` FROM `{?_MYSQL_PREFIX?}_admins` WHERE `login`='%s' LIMIT 1", - array($admin), __FUNCTION__, __LINE__); - - // Is he admin? - $passDB = ''; - if (SQL_NUMROWS($result) == 1) { - // Admin login was found so let's load password from DB - list($passDB) = SQL_FETCHROW($result); - - // Temporary cache it - setAdminHash($admin, $passDB); - - // Generate password hash - $valPass = generatePassString($passDB); - } // END - if + } elseif ((!empty($adminLogin)) && ((!isExtensionActive('cache')) || (isAdminHashSet($adminLogin) === false))) { + // Get admin hash and hash it + $valPass = encodeHashForCookie(getAdminHash($adminLogin)); - // Free memory - SQL_FREERESULT($result); + // Cache it away + $GLOBALS['admin_hash'] = $valPass; } if (!empty($valPass)) { // Check if password is valid - //* DEBUG: */ print(__FUNCTION__ . ':(' . $valPass . '==' . $passCookie . ')='.intval($valPass == $passCookie).'
'); - $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" @@ -757,13 +761,13 @@ function addMaxReceiveList ($mode, $default = '', $return = false) { switch ($mode) { case 'guest': // Guests (in the registration form) are not allowed to select 0 mails per day. - $result = SQL_QUERY("SELECT value, comment FROM `{?_MYSQL_PREFIX?}_max_receive` WHERE value > 0 ORDER BY value", + $result = SQL_QUERY("SELECT `value`, `comment` FROM `{?_MYSQL_PREFIX?}_max_receive` WHERE `value` > 0 ORDER BY `value` ASC", __FUNCTION__, __LINE__); break; case 'member': // Members are allowed to set to zero mails per day (we will change this soon!) - $result = SQL_QUERY("SELECT value, comment FROM `{?_MYSQL_PREFIX?}_max_receive` ORDER BY value", + $result = SQL_QUERY("SELECT `value`, `comment` FROM `{?_MYSQL_PREFIX?}_max_receive` ORDER BY `value` ASC", __FUNCTION__, __LINE__); break; @@ -776,11 +780,11 @@ function addMaxReceiveList ($mode, $default = '', $return = false) { if (SQL_NUMROWS($result) > 0) { $OUT = ''; while ($content = SQL_FETCHARRAY($result)) { - $OUT .= "