]> git.mxchange.org Git - mailer.git/blobdiff - inc/mysql-manager.php
A lot fixes:
[mailer.git] / inc / mysql-manager.php
index bacb22cb67ec91d55483936337025f6fd7a36f54..ccda58a7a7ae4fafcdaae85e3a8e4147267c41e2 100644 (file)
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Alle datenbank-relevanten Funktionen             *
  * -------------------------------------------------------------------- *
- * $Revision::                                                        $ *
- * $Date::                                                            $ *
- * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author::                                                          $ *
- * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
  * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
@@ -273,7 +268,7 @@ ORDER BY
 
                                        // Full file name for checking menu
                                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sub_what=' . $content['sub_what']);
-                                       $inc = sprintf("inc/modules/%s/what-%s.php", $mode, $content['sub_what']);
+                                       $inc = sprintf('inc/modules/%s/what-%s.php', $mode, $content['sub_what']);
                                        if (isIncludeReadable($inc)) {
                                                // Mark currently selected menu - open
                                                if ((!empty($what)) && (($what == $content['sub_what']))) {
@@ -331,7 +326,7 @@ ORDER BY
                                enableBlockMode();
 
                                // Load menu block
-                               $INC = sprintf("inc/modules/%s/action-%s.php", $mode, $content['action']);
+                               $INC = sprintf('inc/modules/%s/action-%s.php', $mode, $content['action']);
                                if (isFileReadable($INC)) {
                                        // Load include file
                                        if ((!isExtensionActive($content['action'])) || ($content['action'] == 'online')) $GLOBALS['rows'] .= loadTemplate('menu_what_begin', TRUE, $mode);
@@ -492,12 +487,12 @@ function fetchUserData ($value, $column = 'userid') {
                if ((isCurrentUserIdSet()) && (getCurrentUserId() != $value)) {
                        // Unset it
                        unsetCurrentUserId();
-               } elseif (isUserDataValid()) {
+               } elseif (isValidUserData()) {
                        // Use cache, so it is fine
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $value . ' is valid, using cache #1');
                        return TRUE;
                } // END - if
-       } elseif (isUserDataValid()) {
+       } elseif (isValidUserData()) {
                // Using cache is fine
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $value . ' is valid, using cache #2');
                return TRUE;
@@ -555,7 +550,7 @@ function fetchUserData ($value, $column = 'userid') {
                } // END - if
 
                // Found, but valid?
-               $found = isUserDataValid();
+               $found = isValidUserData();
        } // END - if
 
        // Free memory
@@ -572,16 +567,20 @@ function fetchUserData ($value, $column = 'userid') {
  * login.
  */
 function isAdmin () {
+       //* DIE: */ reportBug(__FUNCTION__, __LINE__, 'CALLED!');
+       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CALLED!');
+
        // Is there cache?
        if (isset($GLOBALS[__FUNCTION__])) {
                // Return it
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isAdmin()=' . intval($GLOBALS[__FUNCTION__]));
+               /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isAdmin()=' . intval($GLOBALS[__FUNCTION__]) . ' - CACHED!');
                return $GLOBALS[__FUNCTION__];
        } // END - if
 
-       // No admin in installation phase!
-       if ((isInstallationPhase()) || (!isAdminRegistered())) {
+       if ((isInstaller()) || (!isAdminRegistered())) {
+               // No admin in installation phase!
                $GLOBALS[__FUNCTION__] = FALSE;
+               //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isAdmin()=' . intval($GLOBALS[__FUNCTION__]) . ' - INSTALLER!');
                return FALSE;
        } // END - if
 
@@ -589,20 +588,23 @@ function isAdmin () {
        $ret = FALSE;
        $adminId = '0';
        $passwordFromCookie = '';
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $adminId);
 
        // If admin login is not given take current from cookies...
+       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isSessionVariableSet(admin_id)=' . intval(isSessionVariableSet('admin_id')) . ',isSessionVariableSet(admin_md5)=' . intval(isSessionVariableSet('admin_md5')));
        if ((isSessionVariableSet('admin_id')) && (isSessionVariableSet('admin_md5'))) {
+               // Debug message
+               /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Getting admin data from session ...');
+
                // Get admin login and password from session/cookies
                $adminId            = getCurrentAdminId();
                $passwordFromCookie = getAdminMd5();
        } // END - if
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'adminId=' . $adminId . ',passwordFromCookie=' . $passwordFromCookie);
+       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'adminId=' . $adminId . ',passwordFromCookie=' . $passwordFromCookie);
 
        // Abort if admin id is zero
        if (($adminId == '0') || (empty($passwordFromCookie))) {
                // A very noisy debug message ...
-               //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Current adminId is zero. isSessionVariableSet(admin_id)=' . intval(isSessionVariableSet('admin_id')) . ',isSessionVariableSet(admin_md5)=' . intval(isSessionVariableSet('admin_md5')));
+               /* 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;
@@ -615,26 +617,26 @@ function isAdmin () {
        // Search in array for entry
        if (isset($GLOBALS['admin_hash'])) {
                // Use cached string
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using admin_hash=' . $GLOBALS['admin_hash'] . ' from cache');
+               /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using admin_hash=' . $GLOBALS['admin_hash'] . ' from cache');
        } elseif ((!empty($adminId)) && (!empty($passwordFromCookie)) && (isAdminHashSet($adminId) === TRUE)) {
                // Get admin hash and hash it
                $GLOBALS['admin_hash'] = encodeHashForCookie(getAdminHash($adminId));
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'valPass=' . $GLOBALS['admin_hash']);
+               /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'valPass=' . $GLOBALS['admin_hash']);
 
                // Count cache hits
                incrementStatsEntry('cache_hits');
        } elseif ((!empty($adminId)) && ((!isExtensionActive('cache')) || (isAdminHashSet($adminId) === FALSE))) {
                // Get admin hash and hash it
                $GLOBALS['admin_hash'] = encodeHashForCookie(getAdminHash($adminId));
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'valPass=' . $GLOBALS['admin_hash']);
+               /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'valPass=' . $GLOBALS['admin_hash']);
        }
 
        // Check if password is valid
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '(' . $GLOBALS['admin_hash'] . '==' . $passwordFromCookie . ')='.intval($GLOBALS['admin_hash'] == $passwordFromCookie));
+       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '(' . $GLOBALS['admin_hash'] . '==' . $passwordFromCookie . ')='.intval($GLOBALS['admin_hash'] == $passwordFromCookie));
        $GLOBALS[__FUNCTION__] = ((!empty($GLOBALS['admin_hash'])) && ($GLOBALS['admin_hash'] == $passwordFromCookie));
 
        // Return result of comparision
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isAdmin()=' . intval($GLOBALS[__FUNCTION__]));
+       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isAdmin()=' . intval($GLOBALS[__FUNCTION__]));
        return $GLOBALS[__FUNCTION__];
 }
 
@@ -658,7 +660,7 @@ function addMaxReceiveList ($mode, $default = '') {
                        break;
 
                default: // Invalid!
-                       logDebugMessage(__FUNCTION__, __LINE__, sprintf("Invalid mode %s detected.", $mode));
+                       logDebugMessage(__FUNCTION__, __LINE__, sprintf('Invalid mode %s detected.', $mode));
                        break;
        }
 
@@ -1387,8 +1389,15 @@ function getAdminMenuMode ($adminId) {
 }
 
 // Generates an option list from various parameters
-function generateOptions ($table, $key, $value, $default = '', $extra = '', $whereStatement = '', $disabled = array(), $callback = '') {
+function generateOptions ($table, $key, $value, $default = '', $extra = '', $whereStatement = '', $disabled = array(), $callback = '', $allowNone = FALSE) {
        $ret = '';
+
+       // Allow none?
+       if ($allowNone === TRUE) {
+               // Add option for none
+               $ret .= '<option value="0">{--SELECT_NONE--}</option>';
+       } // END - if
+
        if ($table == '/ARRAY/') {
                // Selection from array
                if ((is_array($key)) && (is_array($value)) && ((count($key)) == (count($value)) || (!empty($callback)))) {
@@ -1782,7 +1791,7 @@ function generateReceiverList ($categoryId, $receiver, $mode = '') {
        if (isValidId($categoryId)) {
                // Select category
                $extraColumns  = "LEFT JOIN `{?_MYSQL_PREFIX?}_user_cats` AS `c` ON `d`.`userid`=`c`.`userid`";
-               $addWhere = sprintf(" AND `c`.`cat_id`=%s", $categoryId);
+               $addWhere = sprintf(' AND `c`.`cat_id`=%s', $categoryId);
        } // END - if
 
        // Exclude users in holiday?
@@ -1791,11 +1800,6 @@ function generateReceiverList ($categoryId, $receiver, $mode = '') {
                $addWhere .= " AND `d`.`holiday_active`='N'";
        } // END - if
 
-       // Include only HTML recipients?
-       if ((isExtensionActive('html_mail')) && ($mode == 'html')) {
-               $addWhere .= " AND `d`.`html`='Y'";
-       } // END - if
-
        // Run query
        $result = sqlQueryEscaped("SELECT
        `d`.`userid`
@@ -1837,7 +1841,12 @@ LIMIT %s",
 function reduceRecipientReceivedMails ($column, $id, $count) {
        // Search for mail in database
        $result = sqlQueryEscaped("SELECT * FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `%s`=%s ORDER BY `userid` ASC LIMIT %s",
-               array($column, bigintval($id), $count), __FUNCTION__, __LINE__);
+               array(
+                       $column,
+                       bigintval($id),
+                       $count
+               ), __FUNCTION__, __LINE__
+       );
 
        // Are there entries?
        if (!ifSqlHasZeroNums($result)) {
@@ -1849,10 +1858,10 @@ function reduceRecipientReceivedMails ($column, $id, $count) {
 
                        // We must now look if he has already confirmed this mail, so might sound double, but it may resolve problems
                        // @TODO Rewrite this to a filter
-                       if ((isset($data['stats_id'])) && ($data['stats_id'] > 0)) {
+                       if ((isset($data['stats_id'])) && (isValidId($data['stats_id']))) {
                                // User email
                                $num = countSumTotalData($data['userid'], 'user_stats_data', 'id', 'userid', TRUE, sprintf(" AND `stats_type`='mailid' AND `stats_data`=%s", bigintval($data['stats_id'])));
-                       } elseif ((isset($data['bonus_id'])) && ($data['bonus_id'] > 0)) {
+                       } elseif ((isset($data['bonus_id'])) && (isValidId($data['bonus_id']))) {
                                // Bonus mail
                                $num = countSumTotalData($data['userid'], 'user_stats_data', 'id', 'userid', TRUE, sprintf(" AND `stats_type`='bonusid' AND `stats_data`=%s", bigintval($data['bonus_id'])));
                        }
@@ -1863,7 +1872,7 @@ function reduceRecipientReceivedMails ($column, $id, $count) {
                        } // END - if
                } // END - while
 
-               if (count($userids) > 0) {
+               if (isFilledArray($userids)) {
                        // Now update all user accounts
                        sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`-1 WHERE `userid` IN (%s) LIMIT %s",
                                array(
@@ -1936,7 +1945,7 @@ function doGenericJoinedListEntries ($tableTemplate, $rowTemplate, $noEntryMessa
        if ((!is_array($tableName)) || (count($tableName) != 1)) {
                // No tableName specified
                reportBug(__FUNCTION__, __LINE__, 'tableName is not given. Please fix your XML,tableName[]=' . gettype($tableName) . '!=array,tableTemplate=' . $tableTemplate . ',rowTemplate=' . $rowTemplate);
-       } elseif (count($columns) == 0) {
+       } elseif (!isFilledArray($columns)) {
                // No columns specified
                reportBug(__FUNCTION__, __LINE__, 'columns is not given. Please fix your XML,tableTemplate=' . $tableTemplate . ',rowTemplate=' . $rowTemplate . ',tableName[0]=' . $tableName[0]);
        }
@@ -1969,7 +1978,7 @@ function doGenericListEntries ($tableTemplate, $rowTemplate, $noEntryMessageId,
        if ((!is_array($tableName)) || (count($tableName) != 1)) {
                // No tableName specified
                reportBug(__FUNCTION__, __LINE__, 'tableName is not given. Please fix your XML,tableName[]=' . gettype($tableName) . '!=array,tableTemplate=' . $tableTemplate . ',rowTemplate=' . $rowTemplate);
-       } elseif (count($columns) == 0) {
+       } elseif (!isFilledArray($columns)) {
                // No columns specified
                reportBug(__FUNCTION__, __LINE__, 'columns is not given. Please fix your XML,tableTemplate=' . $tableTemplate . ',rowTemplate=' . $rowTemplate . ',tableName[0]=' . $tableName[0]);
        }
@@ -2016,7 +2025,7 @@ function doListEntries ($sql, $tableTemplate, $noEntryMessageId, $rowTemplate, $
 
                                // Call the callback-function
                                //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callbackFunction=' . $callbackName . ',args=<pre>'.print_r($args, TRUE).'</pre>');
-                               // @TODO If we can rewrite the EL sub-system to support more than one parameter, this call_user_func_array() can be avoided
+                               // @TODO If the EL sub-system can support more than one parameter, this call_user_func_array() can be avoided
                                $row[$columnName] = call_user_func_array($callbackName, $args);
                        } // END - foreach
 
@@ -2025,7 +2034,7 @@ function doListEntries ($sql, $tableTemplate, $noEntryMessageId, $rowTemplate, $
                } // END - while
 
                // Is at least one entry set in content?
-               if ((is_array($content)) && (count($content) > 0)) {
+               if (isFilledArray($content)) {
                        // Then add generic 'rows' element
                        $content['rows'] = $OUT;
                } else {
@@ -2051,7 +2060,7 @@ function doGenericAddEntries ($tableName, $columns = array(), $filterFunctions =
        if ((!is_array($tableName)) || (count($tableName) != 1)) {
                // No tableName specified
                reportBug(__FUNCTION__, __LINE__, 'tableName is not given. Please fix your XML,tableName[]=' . gettype($tableName) . '!=array: userIdColumn=' . $userIdColumn);
-       } elseif (count($columns) == 0) {
+       } elseif (!isFilledArray($columns)) {
                // No columns specified
                reportBug(__FUNCTION__, __LINE__, 'columns is not given. Please fix your XML.');
        }
@@ -2064,7 +2073,7 @@ function doGenericAddEntries ($tableName, $columns = array(), $filterFunctions =
        $GLOBALS['__XML_PARSE_RESULT'] = TRUE;
 
        // Is there "time columns"?
-       if (count($timeColumns) > 0) {
+       if (isFilledArray($timeColumns)) {
                // Then "walk" through all entries
                foreach ($timeColumns as $column) {
                        // Convert all (possible) selections
@@ -2088,9 +2097,18 @@ function doGenericAddEntries ($tableName, $columns = array(), $filterFunctions =
                // 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'])));
 
+               // Get value back (no array supported)
+               $value = postRequestElement($columnName);
+
+               // Is this an array and element 0 is set?
+               if ((is_array($value)) && (isset($value[0]))) {
+                       // Then only take this
+                       $value = $value[0];
+               } // END - if
+
                // Copy entry securely to the final arrays
                $sqlColumns[$key] = sqlEscapeString($columnName);
-               $sqlValues[$key]  = sqlEscapeString(postRequestElement($columnName));
+               $sqlValues[$key]  = sqlEscapeString($value);
 
                // Search for it
                $search = key(search_array($columns, 'column', $columnName));
@@ -2154,7 +2172,7 @@ 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) {
+       if (isFilledArray($timeColumns)) {
                // Then "walk" through all entries
                foreach ($timeColumns as $column) {
                        // Convert all (possible) selections
@@ -2172,7 +2190,7 @@ function doGenericEditEntriesConfirm ($tableName, $columns = array(), $filterFun
                $content = array();
 
                // Prepare SQL for this row
-               $sql = sprintf("UPDATE `{?_MYSQL_PREFIX?}_%s` SET",
+               $sql = sprintf('UPDATE `{?_MYSQL_PREFIX?}_%s` SET',
                        sqlEscapeString($tableName[0])
                );
 
@@ -2274,7 +2292,7 @@ function doGenericEditEntriesConfirm ($tableName, $columns = array(), $filterFun
        } // END - foreach
 
        // Delete cache?
-       if ((count($cacheFiles) > 0) && (!empty($cacheFiles[0]))) {
+       if ((isFilledArray($cacheFiles)) && (!empty($cacheFiles[0]))) {
                // Delete cache file(s)
                foreach ($cacheFiles as $cache) {
                        // Skip any empty entries
@@ -2436,7 +2454,7 @@ function doGenericListBuilder ($prefix, $listType, $tableName, $columns, $filter
                        } // END - foreach
 
                        // Then list it
-                       $OUT .= loadTemplate(sprintf("%s_%s_%s_row",
+                       $OUT .= loadTemplate(sprintf('%s_%s_%s_row',
                                $prefix,
                                $listType,
                                $tableName[0]
@@ -2449,7 +2467,7 @@ function doGenericListBuilder ($prefix, $listType, $tableName, $columns, $filter
        } // END - foreach
 
        // Is there an entry in $content?
-       if ((is_array($content)) && (count($content) > 0)) {
+       if (isFilledArray($content)) {
                // Use generic 'rows'
                $content['rows'] = $OUT;
        } else {
@@ -2459,7 +2477,7 @@ function doGenericListBuilder ($prefix, $listType, $tableName, $columns, $filter
 
        // Load master template
        loadTemplate(
-               sprintf("%s_%s_%s",
+               sprintf('%s_%s_%s',
                        $prefix,
                        $listType,
                        $tableName[0]
@@ -2480,7 +2498,7 @@ function addKeyValueSql ($key, $value) {
                );
        } elseif ((is_double($value)) || (is_float($value)) || (is_int($value))) {
                // Is a number, so addd it directly
-               $sql .= sprintf(" `%s`=%s,",
+               $sql .= sprintf(' `%s`=%s,',
                        sqlEscapeString($key),
                        $value
                );
@@ -2496,5 +2514,43 @@ function addKeyValueSql ($key, $value) {
        return $sql;
 }
 
+// "Getter" for an array from given table and columns
+function getArrayFromTable ($tableName, $columns, $orderBy, $ordered = 'ASC', $whereSql = '') {
+       // The table must exist
+       assert(ifSqlTableExists($tableName));
+
+       // Search for it
+       $result = sqlQueryEscaped('SELECT
+       `' . implode('`, `', $columns) . '`
+FROM
+       `{?_MYSQL_PREFIX?}_%s`
+' . $whereSql . '
+ORDER BY
+       `%s` %S',
+               array(
+                       $tableName,
+                       $orderBy,
+                       $ordered
+               ), __FUNCTION__, __LINE__
+       );
+
+       // Init array
+       $rows = array();
+
+       // Are there entries?
+       if (!ifSqlHasZeroNums($result)) {
+               // Load all entries
+               while ($row = sqlFetchArray($result)) {
+                       array_push($rows, $row);
+               } // END - while
+       } // END - if
+
+       // Free result
+       sqlFreeResult($result);
+
+       // Return all found rows
+       return $rows;
+}
+
 // [EOF]
 ?>