Example #1
0
 /**
  * @brief Generate a UUID
  *
  * @param mixed $version The version to generate (default UUID_V4)
  * @param mixed $url The URL to use for V3 and V5 UUIDs.
  * @return string The UUID
  */
 public function generate($version = self::UUID_V4, $url = null)
 {
     // Implementation for ossp-uuid
     $hu = null;
     $ustr = null;
     switch ($version) {
         case self::UUID_V1:
             \uuid_create($hu);
             \uuid_make($hu, \UUID_MAKE_V1 | \UUID_MAKE_MC);
             break;
         case self::UUID_V3:
             \uuid_create($hu);
             if (!$url) {
                 return null;
             }
             $ns = null;
             \uuid_create($ns);
             \uuid_make($hu, \UUID_MAKE_V3, $ns, $url);
             \uuid_destroy($ns);
             break;
         case self::UUID_V4:
             \uuid_create($hu);
             \uuid_make($hu, \UUID_MAKE_V4);
             break;
         case self::UUID_V5:
             \uuid_create($hu);
             \uuid_create($ns);
             \uuid_make($hu, \UUID_MAKE_V5, $ns, $url);
             \uuid_destroy($ns);
             break;
         default:
             return null;
     }
     \uuid_export($hu, UUID_FMT_STR, $ustr);
     $uuid = $ustr;
     return trim($uuid);
 }
Example #2
0
<?php

if (function_exists("uuid_export")) {
    echo "Version: " . uuid_version() . "\n";
    if (uuid_create($uuid)) {
        die("Can't create\n");
    }
    for ($i = 0; $i < 10; $i++) {
        if (!uuid_make($uuid, UUID_MAKE_V4) && !uuid_export($uuid, UUID_FMT_STR, $str)) {
            echo "{$i}:   {$str}\n";
        }
    }
    uuid_destroy($uuid);
} else {
    if (function_exists("uuid_create")) {
        for ($i = 0; $i < 10; $i++) {
            echo "{$i}:   " . uuid_create() . "\n";
        }
    } else {
        die("No UUID extension\n");
    }
}
Example #3
0
 public function __destruct()
 {
     uuid_destroy($this->uuid);
 }
Example #4
-1
 /**
  * Generate a 36-character RFC 4122 UUID, without the urn:uuid: prefix.
  *
  * @see http://www.ietf.org/rfc/rfc4122.txt
  * @see http://labs.omniti.com/alexandria/trunk/OmniTI/Util/UUID.php
  */
 public function generate()
 {
     $this->_uuid = null;
     if (extension_loaded('uuid')) {
         if (function_exists('uuid_export')) {
             // UUID extension from http://www.ossp.org/pkg/lib/uuid/
             if (uuid_create($ctx) == UUID_RC_OK && uuid_make($ctx, UUID_MAKE_V4) == UUID_RC_OK && uuid_export($ctx, UUID_FMT_STR, $str) == UUID_RC_OK) {
                 $this->_uuid = $str;
                 uuid_destroy($ctx);
             }
         } else {
             // UUID extension from http://pecl.php.net/package/uuid
             $this->_uuid = uuid_create();
         }
     }
     if (!$this->_uuid) {
         list($time_mid, $time_low) = explode(' ', microtime());
         $time_low = (int) $time_low;
         $time_mid = (int) substr($time_mid, 2) & 0xffff;
         $time_high = mt_rand(0, 0xfff) | 0x4000;
         $clock = mt_rand(0, 0x3fff) | 0x8000;
         $node_low = function_exists('zend_thread_id') ? zend_thread_id() : getmypid();
         $node_high = isset($_SERVER['SERVER_ADDR']) ? ip2long($_SERVER['SERVER_ADDR']) : crc32(php_uname());
         $node = bin2hex(pack('nN', $node_low, $node_high));
         $this->_uuid = sprintf('%08x-%04x-%04x-%04x-%s', $time_low, $time_mid, $time_high, $clock, $node);
     }
 }