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-2012 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: $pid = function_exists('zend_thread_id')
40: ? zend_thread_id()
41: : getmypid();
42:
43: /* Base64 can have /, +, and = characters. Restrict to URL-safe
44: * characters. */
45: return str_replace(
46: array('/', '+', '='),
47: array('-', '_', ''),
48: base64_encode(
49: pack('II', mt_rand(), crc32(php_uname('n')))
50: . pack('H*', uniqid() . sprintf('%04s', dechex($pid)))));
51: }
52:
53: /**
54: * Cooerce to string.
55: *
56: * @return string The random ID.
57: */
58: public function __toString()
59: {
60: return $this->_id;
61: }
62: }
63: