Get down to a 23-char id. Discard the more_entropy from uniqid since it just comes...
authorChuck Hagenbuch <chuck@horde.org>
Tue, 7 Sep 2010 21:20:53 +0000 (17:20 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Tue, 7 Sep 2010 21:20:53 +0000 (17:20 -0400)
framework/Support/lib/Horde/Support/Randomid.php

index 71c160e..815e6eb 100644 (file)
@@ -7,13 +7,13 @@
  */
 
 /**
- * Class for generating a 24-character random ID string. This string uses all
+ * Class for generating a 23-character random ID string. This string uses all
  * characters in the class [-_0-9a-zA-Z].
  *
  * <code>
  *  <?php
  *
- *  $rid = (string)new Horde_Support_Randomid();
+ *  $id = (string)new Horde_Support_Randomid();
  *
  *  ?>
  * </code>
@@ -31,14 +31,14 @@ class Horde_Support_Randomid
      *
      * @var string
      */
-    private $_rid;
+    private $_id;
 
     /**
      * New random ID.
      */
     public function __construct()
     {
-        $this->generate();
+        $this->_id = $this->generate();
     }
 
     /**
@@ -46,9 +46,10 @@ class Horde_Support_Randomid
      */
     public function generate()
     {
-        // Base64 can have /, +, and = characters. Restrict to URL-safe
-        // characters.
-        $this->_rid = str_replace(array('/', '+', '='), array('-', '_', ''), base64_encode(pack('I', mt_rand()) . pack('H*', str_replace('.', '', uniqid('', true)) . dechex(getmypid()))));
+        // Base64 can have /, +, and = characters. Restrict to URL-safe characters.
+        return str_replace(array('/', '+', '='), array('-', '_', ''), base64_encode(
+            pack('II', mt_rand(), crc32(php_uname('n'))) . pack('H*', uniqid() . dechex(getmypid()))
+        ));
     }
 
     /**
@@ -58,7 +59,6 @@ class Horde_Support_Randomid
      */
     public function __toString()
     {
-        return $this->_rid;
+        return $this->_id;
     }
-
 }