]> git.mxchange.org Git - mailer.git/blobdiff - inc/mysql-manager.php
Checked also for ext-cache, purged 'payments' cache file in FILTER_CACHE_DESTROY_ALL()
[mailer.git] / inc / mysql-manager.php
index c72706cbd7a135ae93c65367927a18aca8c93ab6..cfd3e318cd896fcbf174e2eaf150380de76538d5 100644 (file)
@@ -87,7 +87,7 @@ function addYouAreHereLink ($accessLevel, $FQFN, $return = false) {
        $file = basename($FQFN);
 
        // Init variables
-       $LINK_ADD = '';
+       $linkAdd = '';
        $OUT = '';
        $ADD = '';
        $prefix = '';
@@ -125,13 +125,11 @@ function addYouAreHereLink ($accessLevel, $FQFN, $return = false) {
                $type     = 'what';
                $search   = $file;
                $modCheck = getModule();
-               $ADD      = '';
        } else {
                // Other
                $type     = 'menu';
                $search   = $file;
                $modCheck = getModule();
-               $ADD      = '';
        }
 
        // Begin the navigation line
@@ -140,18 +138,19 @@ function addYouAreHereLink ($accessLevel, $FQFN, $return = false) {
                $GLOBALS['nav_depth'] = '0';
 
                // Run the pre-filter chain
-               $ret = runFilterChain('pre_youhere_line', array('access_level' => $accessLevel, 'type' => $type, 'search' => $search, 'prefix' => $prefix, 'link_add' => $LINK_ADD, 'content' => '', 'add' => $ADD));
+               $ret = runFilterChain('pre_youhere_line', array('access_level' => $accessLevel, 'type' => $type, 'search' => $search, 'prefix' => $prefix, 'link_add' => $linkAdd, 'content' => '', 'add' => $ADD));
 
                // Add pre-content
                $prefix = $ret['content'];
 
                // Add default content
-               $prefix .= '<div class="you_are_here">{--YOU_ARE_HERE--}&nbsp;<strong><a class="you_are_here" href="{%url=modules.php?module=' . getModule() . $LINK_ADD . '%}">Home</a></strong>';
+               $prefix .= '<div class="you_are_here">{--YOU_ARE_HERE--}&nbsp;<strong><a class="you_are_here" href="{%url=modules.php?module=' . getModule() . $linkAdd . '%}">Home</a></strong>';
        } elseif ($return === false) {
                // Count depth
                $GLOBALS['nav_depth']++;
        }
 
+       // Add arrow
        $prefix .= '&nbsp;-&gt;&nbsp;';
 
        // We need to remove .php and the end
@@ -160,19 +159,21 @@ function addYouAreHereLink ($accessLevel, $FQFN, $return = false) {
                $search = substr($search, 0, -4);
        } // END - if
 
+       // Is ext-sql_patches installed?
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isExtensionInstalledAndNewer()=' . intval(isExtensionInstalledAndNewer('sql_patches', '0.2.3')) . ',youre_here=' . getConfig('youre_here') . ',isAdmin()=' . intval(isAdmin()) . ',modCheck=' . $modCheck);
        if (((isExtensionInstalledAndNewer('sql_patches', '0.2.3')) && (getConfig('youre_here') == 'Y')) || ((isAdmin()) && ($modCheck == 'admin'))) {
                // Output HTML code
-               $OUT = $prefix . '<strong><a class="you_are_here" href="{%url=modules.php?module=' . $modCheck . '&amp;' . $type . '=' . $search . $LINK_ADD . '%}">' . getTitleFromMenu($accessLevel, $search, $type, $ADD) . '</a></strong>';
+               $OUT = $prefix . '<strong><a class="you_are_here" href="{%url=modules.php?module=' . $modCheck . '&amp;' . $type . '=' . $search . $linkAdd . '%}">' . getTitleFromMenu($accessLevel, $search, $type, $ADD) . '</a></strong>';
 
                // Can we close the you-are-here navigation?
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'type=' . $type . 'getWhat()=' . getWhat());
-               if (($type == 'what') || (($type == 'action') && ((!isWhatSet()) || (getWhat() == 'welcome')))) {
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'type=' . $type . ',getWhat()=' . getWhat() . ',accessLevel=' . $accessLevel . ',isWhatSet()=' . intval(isWhatSet()));
+               if (($type == 'what') || (($type == 'action') && ((!isWhatSet()) || (($accessLevel == 'admin') && (getWhat() == 'welcome'))))) {
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'type=' . $type);
                        // Add closing div and br-tag
                        $GLOBALS['nav_depth'] = '0';
 
                        // Run the post-filter chain
-                       $ret = runFilterChain('post_youhere_line', array('access_level' => $accessLevel, 'type' => $type, 'search' => $search, 'prefix' => $prefix, 'link_add' => $LINK_ADD, 'content' => $OUT, 'add' => $ADD));
+                       $ret = runFilterChain('post_youhere_line', array('access_level' => $accessLevel, 'type' => $type, 'search' => $search, 'prefix' => $prefix, 'link_add' => $linkAdd, 'content' => $OUT, 'add' => $ADD));
 
                        // Get content from filter back
                        $OUT = $ret['content'];
@@ -211,7 +212,11 @@ function addMenu ($mode, $action, $what) {
 
        // Load SQL data and add the menu to the output stream...
        $result_main = SQL_QUERY_ESC("SELECT
-       `title`,`what`,`action`,`visible`,`locked`
+       `title`,
+       `what`,
+       `action`,
+       `visible`,
+       `locked`
 FROM
        `{?_MYSQL_PREFIX?}_%s_menu`
 WHERE
@@ -245,10 +250,13 @@ WHERE
        `action`='%s' AND
        `what` != '' AND
        `what` IS NOT NULL
-       ".$ADD."
+       " . $ADD . "
 ORDER BY
        `sort` ASC",
-                               array($mode, $content['action']), __FUNCTION__, __LINE__);
+                               array(
+                                       $mode,
+                                       $content['action']
+                               ), __FUNCTION__, __LINE__);
 
                        // Do we have some entries?
                        if (!SQL_HASZERONUMS($result_sub)) {
@@ -415,7 +423,7 @@ function isMember () {
        // Init user data array
        initUserData();
 
-       // Fix "deleted" cookies first
+       // Fix "deleted" cookies
        fixDeletedCookies(array('userid', 'u_hash'));
 
        // Are cookies set and can the member data be loaded?
@@ -464,19 +472,22 @@ function fetchUserData ($value, $column = 'userid') {
                // Secure userid
                $value = bigintval($value);
 
-               // Set it here
-               setCurrentUserId($value);
-
                // Don't look for invalid userids...
                if (!isValidUserId($value)) {
                        // Invalid, so abort here
                        reportBug(__FUNCTION__, __LINE__, 'User id ' . $value . ' is invalid.');
+               } // END - if
+
+               // Unset cached values if found and different
+               if ((isCurrentUserIdSet()) && (getCurrentUserId() != $value)) {
+                       // Unset it
+                       unsetCurrentUserId();
                } elseif (isUserDataValid()) {
                        // Use cache, so it is fine
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $value . ' is valid, using cache #1');
                        return true;
-               }
-       } elseif (isUserDataValid())  {
+               } // END - if
+       } elseif (isUserDataValid()) {
                // Using cache is fine
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $value . ' is valid, using cache #2');
                return true;
@@ -486,11 +497,14 @@ function fetchUserData ($value, $column = 'userid') {
        $found = false;
 
        // Extra SQL statements
-       $ADD = runFilterChain('convert_user_data_columns', '');
+       $ADD = runFilterChain('convert_user_data_columns', ' ');
 
        // Query for the user
        $result = SQL_QUERY_ESC("SELECT *" . $ADD . " FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `%s`='%s' LIMIT 1",
-               array($column, $value), __FUNCTION__, __LINE__);
+               array(
+                       $column,
+                       $value
+               ), __FUNCTION__, __LINE__);
 
        // Do we have a record?
        if (SQL_NUMROWS($result) == 1) {
@@ -854,7 +868,7 @@ function getCategory ($cid) {
                $data['cat'] = '{--_CATEGORY_NONE--}';
        } elseif ($cid > 0) {
                // Lookup the category in database
-               $result = SQL_QUERY_ESC("SELECT `cat` FROM `{?_MYSQL_PREFIX?}_cats` WHERE `id`=%s LIMIT 1",
+               $result = SQL_QUERY_ESC('SELECT `cat` FROM `{?_MYSQL_PREFIX?}_cats` WHERE `id`=%s LIMIT 1',
                        array(bigintval($cid)), __FUNCTION__, __LINE__);
                if (SQL_NUMROWS($result) == 1) {
                        // Category found... :-)
@@ -870,56 +884,47 @@ function getCategory ($cid) {
 }
 
 // Get a string of "mail title" and price back
-function getPaymentTitlePrice ($pid, $full = false) {
-       // Default is not found
-       $ret = '{--_PAYMENT_404--}';
-
-       // Load payment data
-       $result = SQL_QUERY_ESC("SELECT `mail_title`,`price` FROM `{?_MYSQL_PREFIX?}_payments` WHERE `id`=%s LIMIT 1",
-               array(
-                       bigintval($pid)
-               ), __FUNCTION__, __LINE__);
-
-       // Do we have an entry?
-       if (SQL_NUMROWS($result) == 1) {
-               // Payment type found... :-)
-               $data = SQL_FETCHARRAY($result);
-
-               // Only title or also including price?
-               if ($full === false) {
-                       $ret = $data['mail_title'];
-               } else {
-                       $ret = $data['mail_title'] . ' / {%pipe,translateComma=' . $data['price'] . '%} {?POINTS?}';
-               }
-       } // END - if
-
-       // Free result
-       SQL_FREERESULT($result);
+function getPaymentTitlePrice ($paymentsId, $full = false) {
+       // Only title or also including price?
+       if ($full === false) {
+               $ret = getPaymentData($paymentsId, 'main_title');
+       } else {
+               $ret = getPaymentData($paymentsId, 'main_title') . ' / {%pipe,getPaymentData,translateComma=' . $paymentsId . '%} {?POINTS?}';
+       }
 
        // Return result
        return $ret;
 }
 
-// Get (basicly) the price of given payment id
-function getPaymentPoints ($pid, $lookFor = 'price') {
+// "Getter" for payment data (cached)
+function getPaymentData ($paymentsId, $lookFor = 'price') {
        // Default value...
-       $data[$lookFor] = -1;
+       $data[$lookFor] = NULL;
 
-       // Search for it in database
-       $result = SQL_QUERY_ESC("SELECT `%s` FROM `{?_MYSQL_PREFIX?}_payments` WHERE `id`=%s LIMIT 1",
-               array(
-                       $lookFor,
-                       bigintval($pid)
-               ), __FUNCTION__, __LINE__);
+       // Do we have cache?
+       if (isset($GLOBALS['cache_array']['payments'][$paymentsId]['id'])) {
+               // Use it if found to save SQL queries
+               $data[$lookFor] = $GLOBALS['cache_array']['payments'][$lookFor][$paymentsId];
 
-       // Is the entry there?
-       if (SQL_NUMROWS($result) == 1) {
-               // Payment type found... :-)
-               $data = SQL_FETCHARRAY($result);
-       } // END - if
+               // Update cache hits
+               incrementStatsEntry('cache_hits');
+       } elseif (!isExtensionActive('cache')) {
+               // Search for it in database
+               $result = SQL_QUERY_ESC('SELECT `%s` FROM `{?_MYSQL_PREFIX?}_payments` WHERE `id`=%s LIMIT 1',
+                       array(
+                               $lookFor,
+                               bigintval($paymentsId)
+                       ), __FUNCTION__, __LINE__);
 
-       // Free result
-       SQL_FREERESULT($result);
+               // Is the entry there?
+               if (SQL_NUMROWS($result) == 1) {
+                       // Payment type found... :-)
+                       $data = SQL_FETCHARRAY($result);
+               } // END - if
+
+               // Free result
+               SQL_FREERESULT($result);
+       }
 
        // Return value
        return $data[$lookFor];
@@ -970,7 +975,7 @@ function removeReceiver (&$receivers, $key, $userid, $poolId, $statsId = 0, $isB
                                // Update 'mails_sent' if sql_patches is updated
                                if (isExtensionInstalledAndNewer('sql_patches', '0.7.4')) {
                                        // Update the pool
-                                       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `mails_sent`=`mails_sent`+1 WHERE `id`=%s LIMIT 1",
+                                       SQL_QUERY_ESC('UPDATE `{?_MYSQL_PREFIX?}_pool` SET `mails_sent`=`mails_sent`+1 WHERE `id`=%s LIMIT 1',
                                                array(bigintval($poolId)), __FUNCTION__, __LINE__);
                                } // END - if
                                $ret = 'done';
@@ -998,14 +1003,14 @@ function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatemen
                // Count or sum whole table?
                if ($countRows === true) {
                        // Count whole table
-                       $result = SQL_QUERY_ESC("SELECT COUNT(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`" . $add . ' LIMIT 1',
+                       $result = SQL_QUERY_ESC('SELECT COUNT(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`' . $add . ' LIMIT 1',
                                array(
                                        $lookFor,
                                        $tableName
                                ), __FUNCTION__, __LINE__);
                } else {
                        // Sum whole table
-                       $result = SQL_QUERY_ESC("SELECT SUM(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`" . $add . ' LIMIT 1',
+                       $result = SQL_QUERY_ESC('SELECT SUM(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`' . $add . ' LIMIT 1',
                                array(
                                        $lookFor,
                                        $tableName
@@ -1313,18 +1318,18 @@ function getAdminMenuMode ($adminId) {
 }
 
 // Generates an option list from various parameters
-function generateOptions ($table, $id, $name, $default = '', $special = '', $whereStatement = '', $disabled = array(), $callback = '') {
+function generateOptions ($table, $key, $value, $default = '', $extra = '', $whereStatement = '', $disabled = array(), $callback = '') {
        $ret = '';
        if ($table == '/ARRAY/') {
                // Selection from array
-               if ((is_array($id)) && (is_array($name)) && ((count($id)) == (count($name)) || (!empty($callback)))) {
+               if ((is_array($key)) && (is_array($value)) && ((count($key)) == (count($value)) || (!empty($callback)))) {
                        // Both are arrays
-                       foreach ($id as $idx => $value) {
-                               $ret .= '<option value="' . $value . '"';
-                               if ($default == $value) {
+                       foreach ($key as $idx => $optionValue) {
+                               $ret .= '<option value="' . $optionValue . '"';
+                               if ($default == $optionValue) {
                                        // Selected by default
                                        $ret .= ' selected="selected"';
-                               } elseif (isset($disabled[$value])) {
+                               } elseif (isset($disabled[$optionValue])) {
                                        // Disabled!
                                        $ret .= ' disabled="disabled"';
                                }
@@ -1332,65 +1337,69 @@ function generateOptions ($table, $id, $name, $default = '', $special = '', $whe
                                // Is the call-back function set?
                                if (!empty($callback)) {
                                        // Call it
-                                       $name[$idx] = call_user_func_array($callback, array($id[$idx]));
+                                       $value[$idx] = call_user_func_array($callback, array($key[$idx]));
                                } // END - if
 
                                // Finish option tag
-                               $ret .= '>' . $name[$idx] . '</option>';
+                               $ret .= '>' . $value[$idx] . '</option>';
                        } // END - foreach
                } else {
                        // Problem in request
-                       reportBug(__FUNCTION__, __LINE__, 'Not all are arrays: id[' . count($id) . ']=' . gettype($id) . ',name[' . count($name) . ']=' . gettype($name) . ',callback=' . $callback);
+                       reportBug(__FUNCTION__, __LINE__, 'Not all are arrays: key[' . count($key) . ']=' . gettype($key) . ',value[' . count($value) . ']=' . gettype($value) . ',callback=' . $callback);
                }
        } else {
-               // Data from database
-               $SPEC = ',`' . $id . '`';
-               if (!empty($special)) {
-                       $SPEC = ',`' . $special . '` AS `special`';
+           ///////////////////////
+               // Data from database /
+               ///////////////////////
+
+               // Init extra column (if requested)
+               $extraColumn = '';
+               if (!empty($extra)) {
+                       $extraColumn = ',`' . $extra . '` AS `extra`';
                } // END - if
 
-               // Query the database
-               $result = SQL_QUERY_ESC("SELECT `%s` AS `id`,`%s` AS `name`".$SPEC." FROM `{?_MYSQL_PREFIX?}_%s` ".$whereStatement." ORDER BY `%s` ASC",
+               // Run SQL query
+               $result = SQL_QUERY_ESC("SELECT `%s` AS `key`,`%s` AS `value`" . $extraColumn . " FROM `{?_MYSQL_PREFIX?}_%s` " . $whereStatement . " ORDER BY `%s` ASC",
                        array(
-                               $id,
-                               $name,
+                               $key,
+                               $value,
                                $table,
-                               $name
+                               $value
                        ), __FUNCTION__, __LINE__);
 
                // Do we have rows?
                if (!SQL_HASZERONUMS($result)) {
                        // Found data so add them as OPTION lines
                        while ($content = SQL_FETCHARRAY($result)) {
-                               // Is special set?
-                               if (!isset($content['special'])) {
+                               // Is extra set?
+                               if (!isset($content['extra'])) {
                                        // Set it to empty
-                                       $content['special'] = '';
+                                       $content['extra'] = '';
                                } // END - if
 
-                               $ret .= '<option value="' . $content['id'] . '"';
+                               $ret .= '<option value="' . $content['key'] . '"';
 
-                               if ($default == $content['id']) {
+                               if ($default == $content['key']) {
                                        // Selected by default
                                        $ret .= ' selected="selected"';
-                               } elseif (isset($disabled[$content['id']])) {
+                               } elseif (isset($disabled[$content['key']])) {
                                        // Disabled!
                                        $ret .= ' disabled="disabled"';
                                }
 
                                // Add it, if set
-                               if (!empty($content['special'])) {
-                                       $content['special'] = ' (' . $content['special'] . ')';
+                               if (!empty($content['extra'])) {
+                                       $content['extra'] = ' (' . $content['extra'] . ')';
                                } // END - if
 
                                // Is the call-back function set?
                                if (!empty($callback)) {
                                        // Call it
-                                       $content['name'] = call_user_func_array($callback, array($content['name']));
+                                       $content['value'] = call_user_func_array($callback, array($content['value']));
                                } // END - if
 
                                // Finish option list
-                               $ret .= '>' . $content['name'] . $content['special'] . '</option>';
+                               $ret .= '>' . $content['value'] . $content['extra'] . '</option>';
                        } // END - while
                } else {
                        // No data found
@@ -1803,14 +1812,21 @@ function registerExtensionPointsData ($subject, $columnName, $lockedMode, $payme
        $add = '';
 
        // Is the extension equal or newer 0.8.9?
-       if (isExtensionInstalledAndNewer('sql_patches', '0.8.9')) {
+       if (((isInstallationPhase()) && ((getExtensionMode() == 'register') || (getExtensionMode() == 'update'))) || (isExtensionInstalledAndNewer('sql_patches', '0.8.9'))) {
                // Then add provider
                $add = " AND `account_provider`='EXTENSION'";
        } // END - if
 
        // Is the 'subject' there?
-       if (countSumTotalData($subject, 'points_data', 'id', 'subject', true, $add) == 0) {
-               // Not found so add an SQL query
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ifSqlTableExists(points_data)=' . ifSqlTableExists('points_data') . ',getExtensionMode()=' . getExtensionMode() . ',add=' . $add);
+       if (((!ifSqlTableExists('points_data')) && ((getExtensionMode() == 'register') || (getExtensionMode() == 'update'))) || (countSumTotalData($subject, 'points_data', 'id', 'subject', true, $add) == 0)) {
+               // Not found so:
+               if ((isset($GLOBALS['previous_extension'][getCurrentExtensionName()])) && (!ifSqlTableExists('points_data'))) {
+                       $dummy = $GLOBALS['previous_extension'][getCurrentExtensionName()];
+                       reportBug(__FUNCTION__, __LINE__, 'previous_extension[' . gettype($dummy) . ']=' . $dummy . ',getCurrentExtensionName()=' . getCurrentExtensionName() . ' - Under development, please report this!');
+               } // END - if
+
+               // ... add an SQL query
                addExtensionSql(sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`,`column_name`,`locked_mode`,`payment_method`) VALUES ('%s','%s','%s','%s')",
                        $subject,
                        $columnName,