Several kinky code smoothed, fixes for admin login
[mailer.git] / inc / functions.php
index 25610ef39c4263cbded07e7e7d4ddf3acc22c15c..57e2c1036496f8d6305bdaeabc192e9770edb98b 100644 (file)
@@ -458,7 +458,7 @@ function loadEmailTemplate ($template, $content = array(), $UID = 0) {
        } // END - if
 
        // Load user's data
        } // END - if
 
        // Load user's data
-       //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):UID={$UID},template={$template},content[]=".gettype($content)."<br />");
+       //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):UID={$UID},template={$template},content[]=".gettype($content).'<br />');
        if (($UID > 0) && (is_array($content))) {
                // If nickname extension is installed, fetch nickname as well
                if (isExtensionActive('nickname')) {
        if (($UID > 0) && (is_array($content))) {
                // If nickname extension is installed, fetch nickname as well
                if (isExtensionActive('nickname')) {
@@ -585,7 +585,7 @@ function sendEmail ($toEmail, $subject, $message, $isHtml = 'N', $mailHeader = '
                        // Load email address
                        $result_email = SQL_QUERY_ESC("SELECT `email` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
                                array(bigintval($toEmail)), __FUNCTION__, __LINE__);
                        // Load email address
                        $result_email = SQL_QUERY_ESC("SELECT `email` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
                                array(bigintval($toEmail)), __FUNCTION__, __LINE__);
-                       //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):numRows=".SQL_NUMROWS($result_email)."<br />");
+                       //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):numRows=".SQL_NUMROWS($result_email).'<br />');
 
                        // Does the user exist?
                        if (SQL_NUMROWS($result_email)) {
 
                        // Does the user exist?
                        if (SQL_NUMROWS($result_email)) {
@@ -1307,7 +1307,7 @@ function generateRandomCode ($length, $code, $userid, $DATA = '') {
        // Calculate number for generating the code
        $a = $code + getConfig('_ADD') - 1;
 
        // Calculate number for generating the code
        $a = $code + getConfig('_ADD') - 1;
 
-       if (isConfigEntrySet('master_hash')) {
+       if (isConfigEntrySet('master_salt')) {
                // Generate hash with master salt from modula of number with the prime number and other data
                $saltedHash = generateHash(($a % getConfig('_PRIME')) . getConfig('ENCRYPT_SEPERATOR') . $server . getConfig('ENCRYPT_SEPERATOR') . $keys . getConfig('ENCRYPT_SEPERATOR') . $data . getConfig('ENCRYPT_SEPERATOR') . getConfig('DATE_KEY') . getConfig('ENCRYPT_SEPERATOR') . $a, getConfig('master_salt'));
 
                // Generate hash with master salt from modula of number with the prime number and other data
                $saltedHash = generateHash(($a % getConfig('_PRIME')) . getConfig('ENCRYPT_SEPERATOR') . $server . getConfig('ENCRYPT_SEPERATOR') . $keys . getConfig('ENCRYPT_SEPERATOR') . $data . getConfig('ENCRYPT_SEPERATOR') . getConfig('DATE_KEY') . getConfig('ENCRYPT_SEPERATOR') . $a, getConfig('master_salt'));
 
@@ -1315,7 +1315,7 @@ function generateRandomCode ($length, $code, $userid, $DATA = '') {
                $rcode = hexdec(substr($saltedHash, strlen(getConfig('master_salt')), 9)) / abs(getConfig('rand_no') - $a + sqrt(getConfig('_ADD'))) / pi();
        } else {
                // Generate hash with "hash of site key" from modula of number with the prime number and other data
                $rcode = hexdec(substr($saltedHash, strlen(getConfig('master_salt')), 9)) / abs(getConfig('rand_no') - $a + sqrt(getConfig('_ADD'))) / pi();
        } else {
                // Generate hash with "hash of site key" from modula of number with the prime number and other data
-               $saltedHash = generateHash(($a % getConfig('_PRIME')) . getConfig('ENCRYPT_SEPERATOR') . $server . getConfig('ENCRYPT_SEPERATOR') . $keys . getConfig('ENCRYPT_SEPERATOR') . $data . getConfig('ENCRYPT_SEPERATOR') . getConfig('DATE_KEY') . getConfig('ENCRYPT_SEPERATOR') . $a, substr(sha1(getConfig('SITE_KEY')), 0, 8));
+               $saltedHash = generateHash(($a % getConfig('_PRIME')) . getConfig('ENCRYPT_SEPERATOR') . $server . getConfig('ENCRYPT_SEPERATOR') . $keys . getConfig('ENCRYPT_SEPERATOR') . $data . getConfig('ENCRYPT_SEPERATOR') . getConfig('DATE_KEY') . getConfig('ENCRYPT_SEPERATOR') . $a, substr(sha1(getConfig('SITE_KEY')), 0, getConfig('salt_length')));
 
                // Create number from hash
                $rcode = hexdec(substr($saltedHash, 8, 9)) / abs(getConfig('rand_no') - $a + sqrt(getConfig('_ADD'))) / pi();
 
                // Create number from hash
                $rcode = hexdec(substr($saltedHash, 8, 9)) / abs(getConfig('rand_no') - $a + sqrt(getConfig('_ADD'))) / pi();
@@ -1724,7 +1724,7 @@ function extractHostnameFromUrl (&$script) {
        if (ereg('/', $host)) $host = substr($host, 0, strpos($host, '/'));
 
        // Generate relative URL
        if (ereg('/', $host)) $host = substr($host, 0, strpos($host, '/'));
 
        // Generate relative URL
-       //* DEBUG: */ print("SCRIPT=" . $script."<br />");
+       //* DEBUG: */ print("SCRIPT=" . $script.'<br />');
        if (substr(strtolower($script), 0, 7) == 'http://') {
                // But only if http:// is in front!
                $script = substr($script, (strlen($url) + 7));
        if (substr(strtolower($script), 0, 7) == 'http://') {
                // But only if http:// is in front!
                $script = substr($script, (strlen($url) + 7));
@@ -1733,7 +1733,7 @@ function extractHostnameFromUrl (&$script) {
                $script = substr($script, (strlen($url) + 8));
        }
 
                $script = substr($script, (strlen($url) + 8));
        }
 
-       //* DEBUG: */ print("SCRIPT=" . $script."<br />");
+       //* DEBUG: */ print("SCRIPT=" . $script.'<br />');
        if (substr($script, 0, 1) == '/') $script = substr($script, 1);
 
        // Return host name
        if (substr($script, 0, 1) == '/') $script = substr($script, 1);
 
        // Return host name
@@ -1834,7 +1834,7 @@ function sendRawRequest ($host, $request) {
        } // END - if
 
        // Open connection
        } // END - if
 
        // Open connection
-       //* DEBUG: */ die("SCRIPT=" . $script."<br />");
+       //* DEBUG: */ die("SCRIPT=" . $script.'<br />');
        if ($useProxy === true) {
                // Connect to host through proxy connection
                $fp = @fsockopen(compileCode(getConfig('proxy_host')), bigintval(getConfig('proxy_port')), $errno, $errdesc, 30);
        if ($useProxy === true) {
                // Connect to host through proxy connection
                $fp = @fsockopen(compileCode(getConfig('proxy_host')), bigintval(getConfig('proxy_port')), $errno, $errdesc, 30);
@@ -1952,11 +1952,11 @@ function isEmailValid ($email) {
 function isUrlValid ($URL, $compile=true) {
        // Trim URL a little
        $URL = trim(urldecode($URL));
 function isUrlValid ($URL, $compile=true) {
        // Trim URL a little
        $URL = trim(urldecode($URL));
-       //* DEBUG: */ outputHtml($URL."<br />");
+       //* DEBUG: */ outputHtml($URL.'<br />');
 
        // Compile some chars out...
        if ($compile === true) $URL = compileUriCode($URL, false, false, false);
 
        // Compile some chars out...
        if ($compile === true) $URL = compileUriCode($URL, false, false, false);
-       //* DEBUG: */ outputHtml($URL."<br />");
+       //* DEBUG: */ outputHtml($URL.'<br />');
 
        // Check for the extension filter
        if (isExtensionActive('filter')) {
 
        // Check for the extension filter
        if (isExtensionActive('filter')) {
@@ -2073,8 +2073,15 @@ function generateHash ($plainText, $salt = '') {
                //* DEBUG: */ outputHtml($salt." (".strlen($salt).")<br />");
        } else {
                // Use given salt
                //* DEBUG: */ outputHtml($salt." (".strlen($salt).")<br />");
        } else {
                // Use given salt
+               //* DEBUG: */ print 'salt=' . $salt . '<br />';
                $salt = substr($salt, 0, getConfig('salt_length'));
                $salt = substr($salt, 0, getConfig('salt_length'));
-               //* DEBUG: */ outputHtml("GIVEN={$salt}<br />");
+               //* DEBUG: */ print 'salt=' . $salt . '(' . strlen($salt) . '/' . getConfig('salt_length') . ')<br />';
+
+               // Sanity check on salt
+               if (strlen($salt) != getConfig('salt_length')) {
+                       // Not the same!
+                       debug_report_bug(__FUNCTION__.': salt length mismatch! ('.strlen($salt).'/'.getConfig('salt_length').')');
+               } // END - if
        }
 
        // Return hash
        }
 
        // Return hash
@@ -2167,7 +2174,7 @@ function generatePassString ($passHash) {
        $ret = $passHash;
 
        // Is a secret key and master salt already initialized?
        $ret = $passHash;
 
        // Is a secret key and master salt already initialized?
-       if ((isExtensionInstalled('sql_patches')) && (isConfigEntrySet('_PRIME')) && (isConfigEntrySet('secret_key')) && (isConfigEntrySet('master_salt'))) {
+       if ((isExtensionInstalled('sql_patches')) && (isExtensionInstalledAndNewer('other', '0.2.5')) && (isConfigEntrySet('_PRIME')) && (isConfigEntrySet('secret_key')) && (isConfigEntrySet('master_salt'))) {
                // Only calculate when the secret key is generated
                $newHash = ''; $start = 9;
                for ($idx = 0; $idx < 10; $idx++) {
                // Only calculate when the secret key is generated
                $newHash = ''; $start = 9;
                for ($idx = 0; $idx < 10; $idx++) {
@@ -2179,16 +2186,17 @@ function generatePassString ($passHash) {
                        } elseif ($part2 > $part1) {
                                $mod = dechex(sqrt(($part2 - $part1) * getConfig('_PRIME') / pi()));
                        }
                        } elseif ($part2 > $part1) {
                                $mod = dechex(sqrt(($part2 - $part1) * getConfig('_PRIME') / pi()));
                        }
-                       $mod = substr(round($mod), 0, 4);
-                       $mod = str_repeat(0, 4-strlen($mod)) . $mod;
-                       //* DEBUG: */ outputHtml("*" . $start.'=' . $mod."*<br />");
+                       $mod = substr($mod, 0, 4);
+                       //* DEBUG: */ outputHtml('part1='.$part1.'/part2='.$part2.'/mod=' . $mod . '('.strlen($mod).')<br />');
+                       $mod = str_repeat(0, (4 - strlen($mod))) . $mod;
+                       //* DEBUG: */ outputHtml('*' . $start . '=' . $mod . '*<br />');
                        $start += 4;
                        $newHash .= $mod;
                } // END - for
 
                        $start += 4;
                        $newHash .= $mod;
                } // END - for
 
-               //* DEBUG: */ print($passHash."<br />" . $newHash." (".strlen($newHash).')');
+               //* DEBUG: */ print($passHash.'<br />' . $newHash." (".strlen($newHash).')<br />');
                $ret = generateHash($newHash, getConfig('master_salt'));
                $ret = generateHash($newHash, getConfig('master_salt'));
-               //* DEBUG: */ print($ret."<br />");
+               //* DEBUG: */ print('ret='.$ret.'<br />');
        } else {
                // Hash it simple
                //* DEBUG: */ outputHtml("--" . $passHash."--<br />");
        } else {
                // Hash it simple
                //* DEBUG: */ outputHtml("--" . $passHash."--<br />");
@@ -2421,12 +2429,12 @@ function generateErrorCodeFromUserStatus ($status) {
 // Function to search for the last modifified file
 function searchDirsRecursive ($dir, &$last_changed) {
        // Get dir as array
 // Function to search for the last modifified file
 function searchDirsRecursive ($dir, &$last_changed) {
        // Get dir as array
-       //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):dir=" . $dir."<br />");
+       //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):dir=" . $dir.'<br />');
        // Does it match what we are looking for? (We skip a lot files already!)
        // RegexPattern to exclude  ., .., .revision,  .svn, debug.log or .cache in the filenames
        $excludePattern = '@(\.revision|debug\.log|\.cache|config\.php)$@';
        $ds = getArrayFromDirectory($dir, '', true, false, array(), '.php', $excludePattern);
        // Does it match what we are looking for? (We skip a lot files already!)
        // RegexPattern to exclude  ., .., .revision,  .svn, debug.log or .cache in the filenames
        $excludePattern = '@(\.revision|debug\.log|\.cache|config\.php)$@';
        $ds = getArrayFromDirectory($dir, '', true, false, array(), '.php', $excludePattern);
-       //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):ds[]=".count($ds)."<br />");
+       //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):ds[]=".count($ds).'<br />');
 
        // Walk through all entries
        foreach ($ds as $d) {
 
        // Walk through all entries
        foreach ($ds as $d) {
@@ -2439,7 +2447,7 @@ function searchDirsRecursive ($dir, &$last_changed) {
                        // $FQFN is a directory so also crawl into this directory
                        $newDir = $d;
                        if (!empty($dir)) $newDir = $dir . '/'. $d;
                        // $FQFN is a directory so also crawl into this directory
                        $newDir = $d;
                        if (!empty($dir)) $newDir = $dir . '/'. $d;
-                       //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):DESCENT: " . $newDir."<br />");
+                       //* DEBUG: */ outputHtml(__FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):DESCENT: " . $newDir.'<br />');
                        searchDirsRecursive($newDir, $last_changed);
                } elseif (isFileReadable($FQFN)) {
                        // $FQFN is a filename and no directory
                        searchDirsRecursive($newDir, $last_changed);
                } elseif (isFileReadable($FQFN)) {
                        // $FQFN is a filename and no directory
@@ -2854,7 +2862,7 @@ function isUrlValidSimple ($url) {
                        // @TODO Are these convertions still required?
                        $pat = str_replace('.', "&#92;&#46;", $pat);
                        $pat = str_replace('@', "&#92;&#64;", $pat);
                        // @TODO Are these convertions still required?
                        $pat = str_replace('.', "&#92;&#46;", $pat);
                        $pat = str_replace('@', "&#92;&#64;", $pat);
-                       //* DEBUG: */ outputHtml($key."=&nbsp;" . $pat . "<br />");
+                       //* DEBUG: */ outputHtml($key."=&nbsp;" . $pat . '<br />');
                } // END - if
 
                // Check if expression matches
                } // END - if
 
                // Check if expression matches
@@ -3402,7 +3410,7 @@ function getArrayFromDirectory ($baseDir, $prefix, $fileIncludeDirs = false, $ad
                // Exclude '.', '..' and entries in $excludeArray automatically
                if (in_array($baseFile, $excludeArray, true))  {
                        // Exclude them
                // Exclude '.', '..' and entries in $excludeArray automatically
                if (in_array($baseFile, $excludeArray, true))  {
                        // Exclude them
-                       //* DEBUG: */ outputHtml('excluded=' . $baseFile . "<br />");
+                       //* DEBUG: */ outputHtml('excluded=' . $baseFile . '<br />');
                        continue;
                } // END - if
 
                        continue;
                } // END - if
 
@@ -3416,9 +3424,9 @@ function getArrayFromDirectory ($baseDir, $prefix, $fileIncludeDirs = false, $ad
                // Check if the base filename matches an exclusion pattern and if the pattern is not empty
                if ((!empty($excludePattern)) && (preg_match($excludePattern, $baseFile, $match))) {
                        // These Lines are only for debugging!!
                // Check if the base filename matches an exclusion pattern and if the pattern is not empty
                if ((!empty($excludePattern)) && (preg_match($excludePattern, $baseFile, $match))) {
                        // These Lines are only for debugging!!
-                       //* DEBUG: */ outputHtml('baseDir:' . $baseDir . "<br />");
-                       //* DEBUG: */ outputHtml('baseFile:' . $baseFile . "<br />");
-                       //* DEBUG: */ outputHtml('FQFN:' . $FQFN . "<br />");
+                       //* DEBUG: */ outputHtml('baseDir:' . $baseDir . '<br />');
+                       //* DEBUG: */ outputHtml('baseFile:' . $baseFile . '<br />');
+                       //* DEBUG: */ outputHtml('FQFN:' . $FQFN . '<br />');
 
                        // Exclude this one
                        continue;
 
                        // Exclude this one
                        continue;