Overview

Packages

  • Support

Classes

  • Horde_Support_Array
  • Horde_Support_Backtrace
  • Horde_Support_CombineStream
  • Horde_Support_ConsistentHash
  • Horde_Support_Guid
  • Horde_Support_Inflector
  • Horde_Support_Memory
  • Horde_Support_Numerizer
  • Horde_Support_Numerizer_Locale_Base
  • Horde_Support_Numerizer_Locale_De
  • Horde_Support_Numerizer_Locale_Pt
  • Horde_Support_Randomid
  • Horde_Support_Stack
  • Horde_Support_StringStream
  • Horde_Support_Stub
  • Horde_Support_Timer
  • Horde_Support_Uuid
  • Overview
  • Package
  • Class
  • Tree
 1: <?php
 2: /**
 3:  * Class for generating RFC 4122 UUIDs. Usage:
 4:  *
 5:  * <code>
 6:  * $uuid = (string)new Horde_Support_Uuid;
 7:  * </code>
 8:  *
 9:  * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
10:  *
11:  * @category   Horde
12:  * @package    Support
13:  * @license    http://www.horde.org/licenses/bsd
14:  */
15: class Horde_Support_Uuid
16: {
17:     /**
18:      * Generated UUID
19:      * @var string
20:      */
21:     private $_uuid;
22: 
23:     /**
24:      * New UUID.
25:      */
26:     public function __construct()
27:     {
28:         $this->generate();
29:     }
30: 
31:     /**
32:      * Generate a 36-character RFC 4122 UUID, without the urn:uuid: prefix.
33:      *
34:      * @see http://www.ietf.org/rfc/rfc4122.txt
35:      * @see http://labs.omniti.com/alexandria/trunk/OmniTI/Util/UUID.php
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:      * Cooerce to string.
64:      *
65:      * @return string  UUID.
66:      */
67:     public function __toString()
68:     {
69:         return $this->_uuid;
70:     }
71: 
72: }
73: 
API documentation generated by ApiGen