[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/horde/framework/Horde/Support/ -> Randomid.php (source)

   1  <?php
   2  /**
   3   * Class for generating a 23-character random ID string. This string uses all
   4   * characters in the class [-_0-9a-zA-Z].
   5   *
   6   * <code>
   7   * $id = (string)new Horde_Support_Randomid();
   8   * </code>
   9   *
  10   * Copyright 2010-2014 Horde LLC (http://www.horde.org/)
  11   *
  12   * @author   Michael Slusarz <slusarz@horde.org>
  13   * @category Horde
  14   * @license  http://www.horde.org/licenses/bsd BSD
  15   * @package  Support
  16   */
  17  class Horde_Support_Randomid
  18  {
  19      /**
  20       * Generated ID.
  21       *
  22       * @var string
  23       */
  24      private $_id;
  25  
  26      /**
  27       * New random ID.
  28       */
  29      public function __construct()
  30      {
  31          $this->_id = $this->generate();
  32      }
  33  
  34      /**
  35       * Generate a random ID.
  36       */
  37      public function generate()
  38      {
  39          $r = mt_rand();
  40  
  41          $elts = array(
  42              $r,
  43              uniqid(),
  44              getmypid()
  45          );
  46          if (function_exists('zend_thread_id')) {
  47              $elts[] = zend_thread_id();
  48          }
  49          if (function_exists('sys_getloadavg') &&
  50              $loadavg = sys_getloadavg()) {
  51              $elts = array_merge($elts, $loadavg);
  52          }
  53  
  54          shuffle($elts);
  55  
  56          /* Base64 can have /, +, and = characters. Restrict to URL-safe
  57           * characters. */
  58          return substr(str_replace(
  59              array('/', '+', '='),
  60              array('-', '_', ''),
  61              base64_encode(pack('H*', hash('md5', implode('', $elts))))
  62          ) . $r, 0, 23);
  63      }
  64  
  65      /**
  66       * Cooerce to string.
  67       *
  68       * @return string  The random ID.
  69       */
  70      public function __toString()
  71      {
  72          return $this->_id;
  73      }
  74  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1