Adds a config option to invert the time for Solaris machines in the US.
[squirrelmail.git] / functions / strings.php
index 288f1e13d02f703baef8db44dfdfdf5481e4c125..33d01527c738d28b1f6a14e2a4abd10293c6b558 100644 (file)
    }
 
    /* SquirrelMail version number -- DO NOT CHANGE */
-   $version = "0.5";
+   $version = "0.6pre1 (cvs)";
 
 
    function find_mailbox_name ($mailbox) {
       #
       #   http://www.myhost.com/squirrelmail/src/login.php
    
-      global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST;
+      global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
 
       // Get the path
       $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
         $proto = "https://";
       }
    
+      $port = "";
+      if (isset($SERVER_PORT)) {
+        if ($SERVER_PORT != 80) {
+            $port = sprintf(':%d', $SERVER_PORT);
+        }
+      }
+          
       // Get the hostname from the Host header or server config.
       // Fallback is to omit the server name and use a relative URI,
       // although this is not RFC 2616 compliant.
       if(isset($HTTP_HOST) && !empty($HTTP_HOST)) {
-        $location = $proto . $HTTP_HOST . $path;
+        $location = $proto . $HTTP_HOST . $port . $path;
       } else if(isset($SERVER_NAME) && !empty($SERVER_NAME)) {
-        $location = $proto . $SERVER_NAME . $path;
+        $location = $proto . $SERVER_NAME . $port . $path;
       } else {
         $location = $path;
       }
       return $decrypted;
    }
 
-   function OneTimePadCreate ($length=100) {
-      global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
 
-      // Entropy gathering
-      if (function_exists("crc32")) {
-        $seed1 = (double) microtime() * 1000000;
-        $seed2 = md5($REMOTE_PORT . $REMOTE_ADDR . $UNIQUE_ID);
-        if (function_exists("getrusage")) {
-           $dat = getrusage();
-           $seed3 = md5($dat["ru_nswap"].$dat["ru_majflt"].$dat["ru_utime.tv_sec"].$dat["ru_utime.tv_usec"].getmypid());
-        } else {
-           $seed3 = getmypid();
-        }
-
-        $seed = crc32($seed1)*1000000 + crc32($seed2)*10000 + crc32($seed3);
-      } else {
-        $seed = (double) microtime() * 1000000;
-      }
+   // Randomize the mt_rand() function.  Toss this in strings or
+   // integers and it will seed the generator appropriately.
+   // With strings, it is better to get them long. Use md5() to
+   // lengthen smaller strings.
+   function sq_mt_seed($Val)
+   {
+       // if mt_getrandmax() does not return a 2^n - 1 number,
+       // this might not work well.  This uses $Max as a bitmask.
+       $Max = mt_getrandmax();
+       
+       if (! is_int($Val))
+       {
+           if (function_exists("crc32"))
+           {
+               $Val = crc32($Val);
+           }
+           else
+           {
+               $Str = $Val;
+               $Pos = 0;
+               $Val = 0;
+               $Mask = $Max / 2;
+               $HighBit = $Max ^ $Mask;
+               while ($Pos < strlen($Str))
+               {
+                   if ($Val & $HighBit)
+                   {
+                       $Val = (($Val & $Mask) << 1) + 1;
+                   }
+                   else
+                   {
+                       $Val = ($Val & $Mask) << 1;
+                   }
+                   $Val ^= $Str[$Pos];
+                   $Pos ++;
+               }
+           }
+       }
 
-      srand ($seed);
+       if ($Val < 0)
+         $Val *= -1;
+       if ($Val = 0)
+         return;
+
+       mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
+   }
+   
+   
+   // This function initializes the random number generator fairly well.
+   // It also only initializes it once, so you don't accidentally get
+   // the same 'random' numbers twice in one session.
+   function sq_mt_randomize()
+   {
+      global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
+      static $randomized;
+      
+      if ($randomized)
+         return;
+      
+      // Global   
+      sq_mt_seed((int)((double) microtime() * 1000000));
+      sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
+      
+      // getrusage
+      if (function_exists("getrusage")) {
+         $dat = getrusage();
+        sq_mt_seed(md5($dat["ru_nswap"] . $dat["ru_majflt"] . 
+           $dat["ru_utime.tv_sec"] . $dat["ru_utime.tv_usec"]));
+      }
+      
+      // Apache-specific
+      sq_mt_seed(md5($UNIQUE_ID));
+      
+      $randomized = 1;
+   }
+   
+   function OneTimePadCreate ($length=100) {
+      sq_mt_randomize();
       
       for ($i = 0; $i < $length; $i++) {
-        $pad .= chr(rand(0,255));
+        $pad .= chr(mt_rand(0,255));
       }
 
       return $pad;