]> git.mxchange.org Git - mailer.git/blobdiff - inc/functions.php
Again usual change ... (you know what)
[mailer.git] / inc / functions.php
index 33acd8c1c7d852c5551186024f6eb4ee79156ba3..faecd19e7d1811d8ed10f2eacb03232503f0e332 100644 (file)
@@ -70,15 +70,17 @@ function addFatalMessage ($F, $L, $message, $extra = '') {
 // Getter for total fatal message count
 function getTotalFatalErrors () {
        // Init count
-       $count = '0';
+       $count = 0;
 
        // Is there at least the first entry?
        if (!empty($GLOBALS['fatal_messages'][0])) {
                // Get total count
                $count = count($GLOBALS['fatal_messages']);
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'count=' . $count . ' - FROM ARRAY');
        } // END - if
 
        // Return value
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'count=' . $count . ' - EXIT!');
        return $count;
 }
 
@@ -92,6 +94,9 @@ function generatePassword ($length = '0', $exclude =  array()) {
        // Exclude some entries
        $localAbc = array_diff($GLOBALS['_abc'], $exclude);
 
+       // $localAbc must have at least 10 entries
+       assert(count($localAbc) >= 10);
+
        // Start creating password
        $password = '';
        while (strlen($password) < $length) {
@@ -335,11 +340,14 @@ function generateDereferrerUrl ($url) {
                // Encode URL
                $encodedUrl = encodeString(compileUriCode($url));
 
-               // Log plain URL
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'url=' . $url);
+               // Generate hash
+               $hash = generateHash($url . getSiteKey() . getDateKey());
+
+               // Log plain URL and hash
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'url=' . $url . ',hash=' . $hash . '(' . strlen($hash) . ')');
 
                // De-refer this URL
-               $url = '{%url=modules.php?module=loader&amp;url=' . $encodedUrl . '&amp;hash=' . encodeHashForCookie(generateHash($url . getSiteKey() . getDateKey())) . '%}';
+               $url = '{%url=modules.php?module=loader&amp;url=' . $encodedUrl . '&amp;hash=' . encodeHashForCookie($hash) . '&amp;salt=' . substr($hash, 0, getSaltLength()) . '%}';
        } // END - if
 
        // Return link
@@ -642,7 +650,7 @@ function createFancyTime ($stamp) {
        } // END - foreach
 
        // Is something there?
-       if (strlen($ret) > 0) {
+       if (!empty($ret)) {
                // Remove leading commata and space
                $ret = substr($ret, 2);
        } else {
@@ -793,7 +801,9 @@ function scrambleString ($str) {
        }
 
        // Compare both lengths and abort if different
-       if (strlen($str) != count($scrambleNums)) return $str;
+       if (strlen($str) != count($scrambleNums)) {
+               return $str;
+       } // END - if
 
        // Scramble string here
        //* DEBUG: */ debugOutput('***Original=' . $str.'***<br />');
@@ -898,7 +908,7 @@ function encodeHashForCookie ($passHash) {
 
                        $mod = substr($mod, 0, 2);
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'idx=' . $idx . ',part1=' . $part1 . '/part2=' . $part2 . '/mod=' . $mod . '(' . strlen($mod) . ')');
-                       $mod = str_repeat('0', (2 - strlen($mod))) . $mod;
+                       $mod = str_pad($mod, 2, '0', STR_PAD_LEFT);
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'mod(' . ($idx * 2) . ')=' . $mod . '*');
                        $start += 2;
                        $newHash .= $mod;
@@ -1201,7 +1211,7 @@ function isUrlValidSimple ($url) {
 
 // Wtites data to a config.php-style file
 // @TODO Rewrite this function to use readFromFile() and writeToFile()
-function changeDataInFile ($FQFN, $comment, $prefix, $suffix, $inserted, $seek=0) {
+function changeDataInFile ($FQFN, $comment, $prefix, $suffix, $inserted, $seek = 0) {
        // Initialize some variables
        $done = FALSE;
        $seek++;
@@ -1229,7 +1239,7 @@ function changeDataInFile ($FQFN, $comment, $prefix, $suffix, $inserted, $seek=0
                                // Start reading
                                while (!feof($fp)) {
                                        // Read from source file
-                                       $line = fgets ($fp, 1024);
+                                       $line = fgets($fp, 1024);
 
                                        if (isInString($search, $line)) {
                                                $next = '0';
@@ -1239,7 +1249,7 @@ function changeDataInFile ($FQFN, $comment, $prefix, $suffix, $inserted, $seek=0
                                        if ($next > -1) {
                                                if ($next === $seek) {
                                                        $next = -1;
-                                                       $line = $prefix . $inserted . $suffix . chr(10);
+                                                       $line = $prefix . $inserted . $suffix . PHP_EOL;
                                                } else {
                                                        $next++;
                                                }
@@ -1260,18 +1270,22 @@ function changeDataInFile ($FQFN, $comment, $prefix, $suffix, $inserted, $seek=0
                        fclose($fp);
 
                        if (($done === TRUE) && ($found === TRUE)) {
-                               // Copy back tmp file and delete tmp :-)
+                               // Copy back temporary->FQFN file and ...
                                copyFileVerified($tmp, $FQFN, 0644);
+
+                               // ... delete temporay file :-)
                                return removeFile($tmp);
                        } elseif ($found === FALSE) {
-                               outputHtml('<strong>CHANGE:</strong> 404!');
+                               // Entry not found
+                               logDebugMessage(__FUNCTION__, __LINE__, 'File ' . basename($FQFN) . ' cannot be changed: comment=' . $comment . ',prefix=' . $prefix . ',inserted=' . $inserted . ',seek=' . $seek . ' - 404!');
                        } else {
-                               outputHtml('<strong>TMP:</strong> UNDONE!');
+                               // Temporary file not fully written
+                               logDebugMessage(__FUNCTION__, __LINE__, 'File ' . basename($FQFN) . ' cannot be changed: comment=' . $comment . ',prefix=' . $prefix . ',inserted=' . $inserted . ',seek=' . $seek . ' - Temporary file unfinished!');
                        }
                }
        } else {
                // File not found, not readable or writeable
-               reportBug(__FUNCTION__, __LINE__, 'File not readable/writeable. file=' . basename($FQFN));
+               reportBug(__FUNCTION__, __LINE__, 'File not readable/writeable. file=' . basename($FQFN) . ',comment=' . $comment . ',prefix=' . $prefix . ',inserted=' . $inserted . ',seek=' . $seek);
        }
 
        // An error was detected!
@@ -1283,10 +1297,10 @@ function logDebugMessage ($funcFile, $line, $message, $force=true) {
        // Is debug mode enabled?
        if ((isDebugModeEnabled()) || ($force === TRUE)) {
                // Remove CRLF
-               $message = str_replace(array(chr(13), chr(10)), array('', ''), $message);
+               $message = str_replace(array(chr(13), PHP_EOL), array('', ''), $message);
 
                // Log this message away
-               appendLineToFile(getPath() . getCachePath() . 'debug.log', generateDateTime(time(), '4') . '|' . getModule(FALSE) . '|' . basename($funcFile) . '|' . $line . '|' . $message);
+               appendLineToFile(getPath() . getCachePath() . 'debug.log', generateDateTime(time(), '4') . '|' . getModule(FALSE) . ':' . getExtraModule() . '|' . basename($funcFile) . '|' . $line . '|' . $message);
        } // END - if
 }
 
@@ -1337,19 +1351,22 @@ function handleExtraValues ($filterFunction, $value, $extraValue) {
 }
 
 // Tries to determine if call-back functions and/or extra values shall be parsed
-function doHandleExtraValues ($filterFunctions, $extraValues, $key, $entries, $userIdColumn, $search) {
-       // Debug message
-       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',entries=' . $entries . ',userIdColumn=' . $userIdColumn[0] . ',search=' . $search . ',filterFunctions=' . print_r($filterFunctions, TRUE) . ',extraValues=' . print_r($extraValues, TRUE));
+function doHandleExtraValues ($filterFunctions, $extraValues, $key, $entries, $userIdColumn, $search, $id = NULL) {
+       // Debug mode enabled?
+       if (isDebugModeEnabled()) {
+               // Debug message
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',entries=' . $entries . ',userIdColumn=' . $userIdColumn[0] . ',search=' . $search . ',filterFunctions=' . print_r($filterFunctions, TRUE) . ',extraValues=' . print_r($extraValues, TRUE));
+       } // END - if
 
        // Send data through the filter function if found
-       if ($key == $userIdColumn[0]) {
+       if ($key === $userIdColumn[0]) {
                // Is the userid, we have to process it with convertZeroToNull()
                $entries = convertZeroToNull($entries);
        } elseif ((!empty($filterFunctions[$key])) && (isset($extraValues[$key]))) {
                // Debug mode enabled?
                if (isDebugModeEnabled()) {
                        // Then log it
-                       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',filterFunctions=' . $filterFunctions[$search] . ',extraValues=' . $extraValues[$key] . ',key=' . $key . ',id=' . $id . ',entries[' . gettype($entries) . ']=' . $entries . ' - BEFORE!');
+                       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',filterFunctions=' . $filterFunctions[$key] . ',extraValues=' . $extraValues[$key] . ',key=' . $key . ',id=' . $id . ',entries[' . gettype($entries) . ']=' . $entries . ' - BEFORE!');
                } // END - if
 
                // Filter function + extra value set
@@ -1358,8 +1375,26 @@ function doHandleExtraValues ($filterFunctions, $extraValues, $key, $entries, $u
                // Debug mode enabled?
                if (isDebugModeEnabled()) {
                        // Then log it
-                       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',filterFunctions=' . $filterFunctions[$search] . ',extraValues=' . $extraValues[$key] . ',key=' . $key . ',id=' . $id . ',entries[' . gettype($entries) . ']=' . $entries . ' - AFTER!');
+                       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',filterFunctions=' . $filterFunctions[$key] . ',extraValues=' . $extraValues[$key] . ',key=' . $key . ',id=' . $id . ',entries[' . gettype($entries) . ']=' . $entries . ' - AFTER!');
+               } // END - if
+       } elseif ((!empty($filterFunctions[$search])) && (!empty($extraValues[$search]))) {
+               // Debug mode enabled?
+               if (isDebugModeEnabled()) {
+                       // Then log it
+                       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',filterFunctions=' . $filterFunctions[$search] . ',key=' . $key . ',search=' . $search . ',entries[' . gettype($entries) . ']=' . $entries . ' - BEFORE!');
                } // END - if
+
+               // Handle extra values
+               $entries = handleExtraValues($filterFunctions[$search], $entries, $extraValues[$search]);
+
+               // Debug mode enabled?
+               if (isDebugModeEnabled()) {
+                       // Then log it
+                       /* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'search=' . $search . ',filterFunctions=' . $filterFunctions[$search] . ',key=' . $key . ',search=' . $search . ',entries[' . gettype($entries) . ']=' . $entries . ' - AFTER!');
+               } // END - if
+
+               // Make sure entries is not bool, then something went wrong
+               assert(!is_bool($entries));
        } elseif (!empty($filterFunctions[$search])) {
                // Debug mode enabled?
                if (isDebugModeEnabled()) {
@@ -1591,8 +1626,8 @@ function doShutdown () {
        // Call the filter chain 'shutdown'
        runFilterChain('shutdown', NULL);
 
-       // Check if not in installation phase and the link is up
-       if ((!isInstallationPhase()) && (SQL_IS_LINK_UP())) {
+       // Check if link is up
+       if (SQL_IS_LINK_UP()) {
                // Close link
                SQL_CLOSE(__FUNCTION__, __LINE__);
        } elseif (!isInstallationPhase()) {
@@ -1799,7 +1834,6 @@ function mapModuleToTable ($moduleName) {
                case 'login': // ... and 'login' the member's menu
                        $moduleName = 'member';
                        break;
-
                // Anything else will not be mapped, silently.
        } // END - switch
 
@@ -1919,7 +1953,7 @@ function encodeUrl ($url, $outputMode = '0') {
        } // END - if
 
        // Is there a valid session?
-       if (((!isset($GLOBALS['valid_session'])) || ($GLOBALS['valid_session'] === FALSE) || (!isset($_COOKIE[session_name()]))) && (isSpider() === FALSE)) {
+       if ((!isSessionValid()) && (!isSpider())) {
                // Determine right separator
                $separator = '&amp;';
                if (!isInString('?', $url)) {
@@ -1927,11 +1961,8 @@ function encodeUrl ($url, $outputMode = '0') {
                        $separator = '?';
                } // END - if
 
-               // Is the session id set?
-               if (session_id() != '') {
-                       // Then add it to URL
-                       $url .= $separator . session_name() . '=' . session_id();
-               } // END - if
+               // Then add it to URL
+               $url .= $separator . session_name() . '=' . session_id();
        } // END - if
 
        // Add {?URL?} ?
@@ -2054,13 +2085,10 @@ function convertZeroToNull ($number) {
 // Converts a NULL to zero
 function convertNullToZero ($number) {
        // Is it a valid username?
-       if ((!is_null($number)) && (!empty($number)) && ($number > 0)) {
-               // Always secure it
-               $number = bigintval($number);
-       } else {
+       if ((is_null($number)) || (empty($number)) || ($number < 1)) {
                // Is not valid or zero
                $number = '0';
-       }
+       } // END - if
 
        // Return it
        return $number;
@@ -2175,7 +2203,7 @@ function isHexadecimal ($hex) {
 }
 
 /**
- * Replace chr(13) with "[r]" and chr(10) with "[n]" and add a final new-line to make
+ * Replace chr(13) with "[r]" and PHP_EOL with "[n]" and add a final new-line to make
  * them visible to the developer. Use this function to debug e.g. buggy HTTP
  * response handler functions.
  *
@@ -2183,7 +2211,7 @@ function isHexadecimal ($hex) {
  * @return     $str    Overworked string
  */
 function replaceReturnNewLine ($str) {
-       return str_replace(array(chr(13), chr(10)), array('[r]', '[n]'), $str);
+       return str_replace(array(chr(13), PHP_EOL), array('[r]', '[n]'), $str);
 }
 
 // Converts a given string by splitting it up with given delimiter similar to
@@ -2295,7 +2323,7 @@ function convertStringToBoolean ($str) {
                } // END - if
 
                // Determine it
-               $GLOBALS[__FUNCTION__][$str] = (($strTrimmed == 'true') ? true : false);
+               $GLOBALS[__FUNCTION__][$str] = ($strTrimmed == 'true');
        } // END - if
 
        // Return cache
@@ -2368,7 +2396,7 @@ function memberAddEntries ($tableName, $columns = array(), $filterFunctions = ar
 }
 
 // Edit rows by given id numbers
-function memberEditEntriesConfirm ($tableName, $columns = array(), $filterFunctions = array(), $extraValues = array(), $timeColumns = array(), $editNow = array(FALSE), $idColumn = array('id'), $userIdColumn = array('userid'), $rawUserId = array('userid'), $cacheFiles = array()) {
+function memberEditEntriesConfirm ($tableName, $columns = array(), $filterFunctions = array(), $extraValues = array(), $timeColumns = array(), $editNow = array(FALSE), $idColumn = array('id'), $userIdColumn = array('userid'), $rawUserId = array('userid'), $cacheFiles = array(), $content = array()) {
        // $tableName must be an array
        if ((!is_array($tableName)) || (count($tableName) != 1)) {
                // No tableName specified
@@ -2402,12 +2430,12 @@ function memberEditEntriesConfirm ($tableName, $columns = array(), $filterFuncti
                }
        } else {
                // List for editing
-               memberListBuilder('edit', $tableName, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn);
+               memberListBuilder('edit', $tableName, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn, $rawUserId, $content);
        }
 }
 
 // Delete rows by given id numbers
-function memberDeleteEntriesConfirm ($tableName, $columns = array(), $filterFunctions = array(), $extraValues = array(), $deleteNow = array(FALSE), $idColumn = array('id'), $userIdColumn = array('userid'), $rawUserId = array('userid'), $cacheFiles = array()) {
+function memberDeleteEntriesConfirm ($tableName, $columns = array(), $filterFunctions = array(), $extraValues = array(), $deleteNow = array(FALSE), $idColumn = array('id'), $userIdColumn = array('userid'), $rawUserId = array('userid'), $cacheFiles = array(), $content = array()) {
        // Do this only for members
        assert(isMember());
 
@@ -2444,17 +2472,18 @@ function memberDeleteEntriesConfirm ($tableName, $columns = array(), $filterFunc
                }
        } else {
                // List for deletion confirmation
-               memberListBuilder('delete', $tableName, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn);
+               memberListBuilder('delete', $tableName, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn, $rawUSerId, $content);
        }
 }
 
 // Build a special template list
-function memberListBuilder ($listType, $tableName, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn, $rawUserId = array('userid')) {
+// @TODO cacheFiles is not yet supported
+function memberListBuilder ($listType, $tableName, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn, $rawUserId = array('userid'), $content = array()) {
        // Do this only for logged in member
        assert(isMember());
 
        // Call inner (general) function
-       doGenericListBuilder('member', $listType, $tableName, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn, $rawUserId);
+       doGenericListBuilder('member', $listType, $tableName, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn, $rawUserId, $content);
 }
 
 // Checks whether given address is IPv4