1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Horde_Support_Uuid
16: {
17: 18: 19: 20:
21: private $_uuid;
22:
23: 24: 25:
26: public function __construct()
27: {
28: $this->generate();
29: }
30:
31: 32: 33: 34: 35: 36:
37: public function generate()
38: {
39: if (extension_loaded('uuid')) {
40: $this->_uuid = uuid_create();
41: } else {
42: list($time_mid, $time_low) = explode(' ', microtime());
43: $time_low = (int)$time_low;
44: $time_mid = (int)substr($time_mid, 2) & 0xffff;
45: $time_high = mt_rand(0, 0x0fff) | 0x4000;
46:
47: $clock = mt_rand(0, 0x3fff) | 0x8000;
48:
49: $node_low = function_exists('zend_thread_id')
50: ? zend_thread_id()
51: : getmypid();
52: $node_high = isset($_SERVER['SERVER_ADDR'])
53: ? ip2long($_SERVER['SERVER_ADDR'])
54: : crc32(php_uname());
55: $node = bin2hex(pack('nN', $node_low, $node_high));
56:
57: $this->_uuid = sprintf('%08x-%04x-%04x-%04x-%s',
58: $time_low, $time_mid, $time_high, $clock, $node);
59: }
60: }
61:
62: 63: 64: 65: 66:
67: public function __toString()
68: {
69: return $this->_uuid;
70: }
71:
72: }
73: