]> git.mxchange.org Git - mailer.git/blobdiff - inc/mysql-manager.php
Fix for SQL
[mailer.git] / inc / mysql-manager.php
index 21ddd007d23b0696aaaa51865f48eae3fbea452c..8d59df8a9fb59edf9b3d77c04c8ecb59e49079b2 100644 (file)
@@ -792,7 +792,7 @@ function getActionFromModuleWhat ($module, $what) {
        $data['action'] = '';
 
        if (!isExtensionInstalledAndNewer('sql_patches', '0.0.5')) {
-               // sql_patches is missing so choose depending on mode
+               // ext-sql_patches is missing so choose depending on mode
                $what = determineWhat($module);
        } elseif ((empty($what)) && ($module != 'admin')) {
                // Use configured 'home'
@@ -841,7 +841,7 @@ function getActionFromModuleWhat ($module, $what) {
                // Free memory
                SQL_FREERESULT($result);
        } elseif ((!isExtensionInstalled('sql_patches')) && ($module != 'admin') && ($module != 'unknown')) {
-               // No sql_patches installed, but maybe we need to register an admin?
+               // No ext-sql_patches installed, but maybe we need to register an admin?
                if (isAdminRegistered()) {
                        // Redirect to admin area
                        redirectToUrl('admin.php');
@@ -967,7 +967,7 @@ function removeReceiver (&$receivers, $key, $userid, $poolId, $statsId = 0, $isB
                                                $type
                                        ), __FUNCTION__, __LINE__);
 
-                               // Update 'mails_sent' if sql_patches is updated
+                               // Update 'mails_sent' if ext-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',
@@ -990,14 +990,13 @@ function removeReceiver (&$receivers, $key, $userid, $poolId, $statsId = 0, $isB
 
 // Calculate sum (default) or count records of given criteria
 function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatement = 'userid', $countRows = FALSE, $add = '', $mode = '=') {
-       // Init count/sum
-       $data['res'] = '0';
-
+       // Debug message
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',tableName=' . $tableName . ',lookFor=' . $lookFor . ',whereStatement=' . $whereStatement . ',add=' . $add);
        if ((empty($search)) && ($search != '0')) {
                // Count or sum whole table?
                if ($countRows === TRUE) {
                        // Count whole table
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'COUNT!');
                        $result = SQL_QUERY_ESC('SELECT COUNT(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`' . $add . ' LIMIT 1',
                                array(
                                        $lookFor,
@@ -1005,6 +1004,7 @@ function countSumTotalData ($search, $tableName, $lookFor = 'id', $whereStatemen
                                ), __FUNCTION__, __LINE__);
                } else {
                        // Sum whole table
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SUM!');
                        $result = SQL_QUERY_ESC('SELECT SUM(`%s`) AS `res` FROM `{?_MYSQL_PREFIX?}_%s`' . $add . ' LIMIT 1',
                                array(
                                        $lookFor,
@@ -1243,7 +1243,7 @@ function getAdminDefaultAcl ($adminId) {
        // By default an invalid ACL value is returned
        $data['default_acl'] = 'NO-ACL';
 
-       // Is sql_patches there and was it found in cache?
+       // Is ext-sql_patches there and was it found in cache?
        if (!isExtensionActive('sql_patches')) {
                // Not found, which is bad, so we need to allow all
                $data['default_acl'] =  'allow';
@@ -1280,7 +1280,7 @@ function getAdminMenuMode ($adminId) {
        // By default an invalid mode
        $data['la_mode'] = 'INVALID';
 
-       // Is sql_patches there and was it found in cache?
+       // Is ext-sql_patches there and was it found in cache?
        if (!isExtensionActive('sql_patches')) {
                // Not found, which is bad, so we need to allow all
                $data['la_mode'] =  'global';
@@ -1418,9 +1418,9 @@ function deleteUserAccount ($userid, $reason) {
        $result = SQL_QUERY_ESC("SELECT
        (SUM(p.`points`) - d.`used_points`) AS `points`
 FROM
-       `{?_MYSQL_PREFIX?}_user_points` AS p
+       `{?_MYSQL_PREFIX?}_user_points` AS `p`
 LEFT JOIN
-       `{?_MYSQL_PREFIX?}_user_data` AS d
+       `{?_MYSQL_PREFIX?}_user_data` AS `d`
 ON
        p.`userid`=d.`userid`
 WHERE
@@ -1619,7 +1619,7 @@ function addBonusMailToQueue ($subject, $text, $receiverList, $points, $seconds,
        `html_msg`
 ) VALUES (
        '%s',
-       %s',
+       '%s',
        '%s',
        %s,
        %s,
@@ -1782,7 +1782,10 @@ function reduceRecipientReceivedMails ($column, $id, $count) {
                if (count($userids) > 0) {
                        // Now update all user accounts
                        SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`-1 WHERE `userid` IN (%s) LIMIT %s",
-                               array(implode(',', $userids), count($userids)), __FUNCTION__, __LINE__);
+                               array(
+                                       implode(',', $userids),
+                                       count($userids)
+                               ), __FUNCTION__, __LINE__);
                } else {
                        // Nothing deleted
                        displayMessage('{%message,ADMIN_MAIL_NOTHING_DELETED=' . $id . '%}');
@@ -1796,7 +1799,7 @@ function reduceRecipientReceivedMails ($column, $id, $count) {
 // Creates a new task
 function createNewTask ($subject, $notes, $taskType, $userid = NULL, $adminId = NULL, $strip = TRUE) {
        // Insert the task data into the database
-       SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_task_system` (`assigned_admin`, `userid`, `status`, `task_type`, `subject`, `text`, `task_created`) VALUES (%s,%s,'NEW','%s','%s','%s', UNIX_TIMESTAMP())",
+       SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_task_system` (`assigned_admin`, `userid`, `status`, `task_type`, `subject`, `text`, `task_created`) VALUES (%s, %s, 'NEW', '%s', '%s', '%s', UNIX_TIMESTAMP())",
                array(
                        convertZeroToNull($adminId),
                        convertZeroToNull($userid),
@@ -1810,23 +1813,20 @@ function createNewTask ($subject, $notes, $taskType, $userid = NULL, $adminId =
 }
 
 // Updates last module / online time
-function updateLastActivity($userid) {
+function updateLastActivity ($userid) {
        // Is 'what' set?
        if (isWhatSet()) {
                // Run the update query
                SQL_QUERY_ESC("UPDATE
        `{?_MYSQL_PREFIX?}_user_data`
 SET
-       `%s`='%s',
+       `{%%pipe,getUserLastWhatName%%}`='{%%pipe,getWhat%%}',
        `last_online`=UNIX_TIMESTAMP(),
-       `REMOTE_ADDR`='%s'
+       `REMOTE_ADDR`='{%%pipe,detectRemoteAddr%%}'
 WHERE
        `userid`=%s
 LIMIT 1",
                array(
-                       getUserLastWhatName(),
-                       getWhat(),
-                       detectRemoteAddr(),
                        bigintval($userid)
                ), __FUNCTION__, __LINE__);
        } else {
@@ -1834,15 +1834,13 @@ LIMIT 1",
                SQL_QUERY_ESC("UPDATE
        `{?_MYSQL_PREFIX?}_user_data`
 SET
-       `%s`=NULL,
+       `{%%pipe,getUserLastWhatName%%}`=NULL,
        `last_online`=UNIX_TIMESTAMP(),
-       `REMOTE_ADDR`='%s'
+       `REMOTE_ADDR`='{%%pipe,detectRemoteAddr%%}'
 WHERE
        `userid`=%s
 LIMIT 1",
                array(
-                       getUserLastWhatName(),
-                       detectRemoteAddr(),
                        bigintval($userid)
                ), __FUNCTION__, __LINE__);
        }
@@ -1920,7 +1918,7 @@ function doGenericListEntries ($tableTemplate, $rowTemplate, $noEntryMessageId,
                foreach ($orderByColumns as $orderByColumn => $array) {
                        // Get keys (table/alias) and values (sorting itself)
                        $table   = trim(implode('', array_keys($array)));
-                       $sorting = trim(implode('', array_keys($array)));
+                       $sorting = trim(implode('', array_values($array)));
 
                        // table/alias can be omitted
                        if (!empty($table)) {
@@ -1984,7 +1982,7 @@ function doListEntries ($sql, $tableTemplate, $noEntryMessageId, $rowTemplate, $
 
 // Adds a given entry to the database
 function doGenericAddEntries ($tableName, $columns = array(), $filterFunctions = array(), $extraValues = array(), $timeColumns = array(), $columnIndex = NULL) {
-       //* DEBUG: */ die('columns=<pre>'.print_r($columns,TRUE).'</pre>,filterFunctions=<pre>'.print_r($filterFunctions,TRUE).'</pre>,extraValues=<pre>'.print_r($extraValues,TRUE).'</pre>,POST=<pre>'.print_r($_POST,TRUE).'</pre>');
+       //* DEBUG: */ die(__FUNCTION__.':columns=<pre>'.print_r($columns,TRUE).'</pre>,filterFunctions=<pre>'.print_r($filterFunctions,TRUE).'</pre>,extraValues=<pre>'.print_r($extraValues,TRUE).'</pre>,timeColumns=<pre>'.print_r($timeColumns,TRUE).'</pre>,columnIndex=<pre>'.print_r($columnIndex,TRUE).'</pre>,POST=<pre>'.print_r($_POST,TRUE).'</pre>');
        // Verify that tableName and columns are not empty
        if ((!is_array($tableName)) || (count($tableName) != 1)) {
                // No tableName specified
@@ -2024,24 +2022,14 @@ function doGenericAddEntries ($tableName, $columns = array(), $filterFunctions =
                } // END - if
 
                // Debug message
-               //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',columnName[' . gettype($columnName) . ']=' . $columnName . ',filterFunctions=' . $filterFunctions[$key] . ',extraValues=' . intval(isset($extraValues[$key])) . ',extraValuesName=' . intval(isset($extraValues[$columnName . '_list'])) . '<br />');
+               //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',columnName[' . gettype($columnName) . ']=' . $columnName . ',filterFunctions=' . $filterFunctions[$key] . ',extraValues=' . intval(isset($extraValues[$key])) . ',extraValuesName=' . intval(isset($extraValues[$columnName . '_list'])));
 
                // Copy entry securely to the final arrays
                $sqlColumns[$key] = SQL_ESCAPE($columnName);
                $sqlValues[$key]  = SQL_ESCAPE(postRequestElement($columnName));
 
-               // Send data through the filter function if found
-               if ((isset($filterFunctions[$key])) && (isset($extraValues[$key . '_list']))) {
-                       // Filter function set!
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlValues[' . $key . '][' . gettype($sqlValues[$key]) . ']=' . $sqlValues[$key] . ' - BEFORE!');
-                       $sqlValues[$key] = call_user_func_array($filterFunctions[$key], merge_array(array($columnName), $extraValues[$key . '_list']));
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlValues[' . $key . '][' . gettype($sqlValues[$key]) . ']=' . $sqlValues[$key] . ' - AFTER!');
-               } elseif ((isset($filterFunctions[$key])) && (!empty($filterFunctions[$key]))) {
-                       // Run through an extra filter
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlValues[' . $key . '][' . gettype($sqlValues[$key]) . ']=' . $sqlValues[$key] . ' - BEFORE!');
-                       $sqlValues[$key] = handleExtraValues($filterFunctions[$key], $sqlValues[$key], NULL);
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlValues[' . $key . '][' . gettype($sqlValues[$key]) . ']=' . $sqlValues[$key] . ' - AFTER!');
-               }
+               // Try to handle call-back functions and/or extra values on the list
+               $sqlValues[$key] = doHandleExtraValues($filterFunctions, $extraValues, $key . '_list', $sqlValues[$key], $userIdColumn, key(search_array($columns, 'column', $key)));
 
                // Is the value not a number?
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlValues[' . $key . '][' . gettype($sqlValues[$key]) . ']=' . $sqlValues[$key]);
@@ -2066,7 +2054,7 @@ function doGenericAddEntries ($tableName, $columns = array(), $filterFunctions =
                // Run the SQL query
                SQL_QUERY($sql, __FUNCTION__, __LINE__);
 
-               // Add id
+               // Add id number
                setPostRequestElement('id', SQL_INSERTID());
 
                // Prepare filter data array
@@ -2090,6 +2078,15 @@ function doGenericAddEntries ($tableName, $columns = array(), $filterFunctions =
 
 // Edit rows by given id numbers
 function doGenericEditEntriesConfirm ($tableName, $columns = array(), $filterFunctions = array(), $extraValues = array(), $timeColumns = array(), $editNow = array(FALSE), $idColumn = array('id'), $userIdColumn = array('userid'), $rawUserId = array('userid'), $cacheFiles = array(), $subject = '') {
+       // Is there "time columns"?
+       if (count($timeColumns) > 0) {
+               // Then "walk" through all entries
+               foreach ($timeColumns as $column) {
+                       // Convert all (possible) selections
+                       convertSelectionsToEpocheTimeInPostData($column . '_ye');
+               } // END - foreach
+       } // END - if
+
        // Change them all
        $affected = '0';
        foreach (postRequestElement($idColumn[0]) as $id => $sel) {
@@ -2107,56 +2104,50 @@ function doGenericEditEntriesConfirm ($tableName, $columns = array(), $filterFun
                // "Walk" through all entries
                foreach (postRequestArray() as $key => $entries) {
                        // Skip raw userid which is always invalid
-                       if ($key == $rawUserId[0]) {
+                       if (($key == $rawUserId[0]) || ($key == 'do_edit')) {
                                // Continue with next field
                                //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',idColumn[0]=' . $idColumn[0] . ',rawUserId=' . $rawUserId[0]);
                                continue;
                        } // END - if
 
                        // Debug message
-                       //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',idColumn[0]=' . $idColumn[0] . ',entries=<pre>'.print_r($entries,TRUE).'</pre>');
+                       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',id=' . $id . ',idColumn[0]=' . $idColumn[0] . ',entries=<pre>'.print_r($entries,TRUE).'</pre>');
 
                        // Is entries an array?
                        if (($key != $idColumn[0]) && (is_array($entries)) && (isset($entries[$id]))) {
-                               //die($key.'/'.$id.'=<pre>'.print_r($columns,TRUE).'</pre><pre>'.print_r($filterFunctions,TRUE).'</pre>');
                                // Search for the right array index
                                $search = key(search_array($columns, 'column', $key));
 
                                // Add this entry to content
                                $content[$key] = $entries[$id];
 
-                               // Send data through the filter function if found
-                               if ($key == $userIdColumn[0]) {
-                                       // Is the userid, we have to process it with convertZeroToNull()
-                                       $entries[$id] = convertZeroToNull($entries[$id]);
-                               } elseif ((isset($filterFunctions[$key])) && (isset($extraValues[$key]))) {
-                                       // Filter function + extra value set
-                                       $entries[$id] = handleExtraValues($filterFunctions[$key], $entries[$id], $extraValues[$key]);
-                               } elseif (isset($filterFunctions[$search])) {
-                                       // Filter function set
-                                       //* BUG: */ die($id.'/'.$key.'='.$entries[$id].'/'.$filterFunctions[$search]);
-                                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',filterFunctions=' . $filterFunctions[$search] . ',key=' . $key . ',id=' . $id . ',entries[' . gettype($entries[$id]) . ']=' . $entries[$id] . ' - BEFORE!');
-                                       $entries[$id] = handleExtraValues($filterFunctions[$search], $entries[$id], NULL);
-                                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',filterFunctions=' . $filterFunctions[$search] . ',key=' . $key . ',id=' . $id . ',entries[' . gettype($entries[$id]) . ']=' . $entries[$id] . ' - AFTER!');
-                                       assert(!is_bool($entries[$id]));
-                               }
+                               // Debug message
+                               //* BUG: */ die($key.'/'.$id.'/'.$search.'=<pre>'.print_r($columns,TRUE).'</pre><pre>'.print_r($filterFunctions,TRUE).'</pre>');
 
-                               // Is the value NULL?
-                               if ($entries[$id] == 'NULL') {
-                                       // Add it directly
-                                       $sql .= sprintf(' `%s`=NULL,',
-                                               SQL_ESCAPE($key)
-                                       );
-                               } else {
-                                       // Else add the value covered
-                                       $sql .= sprintf(" `%s`='%s',",
-                                               SQL_ESCAPE($key),
-                                               SQL_ESCAPE($entries[$id])
-                                       );
-                               }
+                               // Handle possible call-back functions and/or extra values
+                               $entries[$id] = doHandleExtraValues($filterFunctions, $extraValues, $key, $entries[$id], $userIdColumn, $search);
+
+                               // Add key/value pair to SQL string
+                               $sql .= addKeyValueSql($key, $entries[$id]);
                        } elseif (($key != $idColumn[0]) && (!is_array($entries))) {
-                               // Add normal entries as well!
-                               $content[$key] =  $entries;
+                               // Search for it
+                               $search = key(search_array($columns, 'column', $key));
+                               //* BUG: */ die($key.'/<pre>'.print_r($search, TRUE).'</pre>=<pre>'.print_r($columns, TRUE).'</pre>');
+
+                               // Debug message
+                               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',entries[' . gettype($entries) . ']=' . $entries . ',search=' . $search . ' - BEFORE!');
+
+                               // Add normal entries as well
+                               $content[$key] = $entries;
+
+                               // Handle possible call-back functions and/or extra values
+                               $entries = doHandleExtraValues($filterFunctions, $extraValues, $key, $entries, $userIdColumn, $search);
+
+                               // Debug message
+                               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',entries[' . gettype($entries) . ']=' . $entries . ',search=' . $search . ' - AFTER!');
+
+                               // Add key/value pair to SQL string
+                               $sql .= addKeyValueSql($key, $entries);
                        }
                } // END - foreach
 
@@ -2169,6 +2160,7 @@ function doGenericEditEntriesConfirm ($tableName, $columns = array(), $filterFun
                $sql .= " LIMIT 1";
 
                // Run this query
+               //* BUG: */ die($sql.'<pre>'.print_r(postRequestArray(), TRUE).'</pre>');
                SQL_QUERY($sql, __FUNCTION__, __LINE__);
 
                // Add affected rows
@@ -2247,7 +2239,7 @@ function doGenericDeleteEntriesConfirm ($tableName, $columns = array(), $filterF
        } // END - if
 
        // Delete them all
-       //* BUG: */ die($sql.'<br /><pre>'.print_r($tableName,TRUE).'</pre><pre>'.print_r($columns,TRUE).'</pre><pre>'.print_r($filterFunctions,TRUE).'</pre><pre>'.print_r($extraValues,TRUE).'</pre><pre>'.print_r($deleteNow,TRUE).'</pre><pre>'.print_r($idColumn,TRUE).'</pre>');
+       //* BUG: */ die($sql.'<pre>'.print_r($tableName,TRUE).'</pre><pre>'.print_r($columns,TRUE).'</pre><pre>'.print_r($filterFunctions,TRUE).'</pre><pre>'.print_r($extraValues,TRUE).'</pre><pre>'.print_r($deleteNow,TRUE).'</pre><pre>'.print_r($idColumn,TRUE).'</pre>');
        $idList = '';
        foreach (postRequestElement($idColumn[0]) as $id => $sel) {
                // Is id zero?
@@ -2355,24 +2347,8 @@ function doGenericListBuilder ($prefix, $listType, $tableName, $columns, $filter
                                        $GLOBALS[$prefix . '_list_builder_id_value'] = bigintval($value);
                                } // END - if
 
-                               // Is there a call-back function and extra-value pair?
-                               if ((isset($filterFunctions[$idx])) && (isset($extraValues[$idx]))) {
-                                       // Handle the call in external function
-                                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',fucntion=' . $filterFunctions[$idx] . ',value=' . $value);
-                                       $content[$key] = handleExtraValues(
-                                               $filterFunctions[$idx],
-                                               $value,
-                                               $extraValues[$idx]
-                                       );
-                               } elseif ((isset($columns[$idx]['name'])) && (isset($filterFunctions[$columns[$idx]['name']])) && (isset($extraValues[$columns[$idx]['name']]))) {
-                                       // Handle the call in external function
-                                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',fucntion=' . $filterFunctions[$columns[$idx]['name']] . ',value=' . $value);
-                                       $content[$key] = handleExtraValues(
-                                               $filterFunctions[$columns[$idx]['name']],
-                                               $value,
-                                               $extraValues[$columns[$idx]['name']]
-                                       );
-                               }
+                               // Try to handle call-back functions and/or extra values
+                               $content[$key] = doHandleExtraValues($filterFunctions, $extraValues, $idx, $content[$key], $userIdColumn, $idx);
                        } // END - foreach
 
                        // Then list it
@@ -2431,5 +2407,34 @@ function isUrlBlacklisted ($url) {
        return $listed;
 }
 
+// Adds key/value pair to a working SQL string together
+function addKeyValueSql ($key, $value) {
+       // Init SQL
+       $sql = '';
+
+       // Is it NULL?
+       if (($value == 'NULL') || (is_null($value))) {
+               // Add key with NULL
+               $sql .= sprintf(' `%s`=NULL,',
+                       SQL_ESCAPE($key)
+               );
+       } elseif ((is_double($value)) || (is_float($value)) || (is_int($value))) {
+               // Is a number, so addd it directly
+               $sql .= sprintf(" `%s`=%s,",
+                       SQL_ESCAPE($key),
+                       $value
+               );
+       } else {
+               // Else add the value escape'd
+               $sql .= sprintf(" `%s`='%s',",
+                       SQL_ESCAPE($key),
+                       SQL_ESCAPE($value)
+               );
+       }
+
+       // Return SQL string
+       return $sql;
+}
+
 // [EOF]
 ?>