Exemple #1
0
 public static function convert($uuid, $toFmt)
 {
     $uuidConv = $uuid;
     $fromFmt = self::isBinary($uuid) ? self::UUID_FMT_BIN : self::UUID_FMT_STR;
     uuid_import(&self::$_uuid, $fromFmt, $uuid);
     uuid_export(self::instance(), $toFmt, &$uuidConv);
     return $uuidConv;
 }
Exemple #2
0
 /**
  * generate
  *
  * v4
  */
 public static function generate()
 {
     if (!function_exists('uuid_create')) {
         return false;
     }
     uuid_create(&$context);
     uuid_make($context, UUID_MAKE_V4);
     uuid_export($context, UUID_FMT_STR, &$uuid);
     return trim($uuid);
 }
Exemple #3
0
 /**
  * Generates version 5 UUID: SHA-1 hash of URL
  */
 public static function v5($i_url)
 {
     if (!function_exists('uuid_create')) {
         return false;
     }
     if (!strlen($i_url)) {
         $i_url = self::v1();
     }
     uuid_create(&$context);
     uuid_create(&$namespace);
     uuid_make($context, UUID_MAKE_V5, $namespace, $i_url);
     uuid_export($context, UUID_FMT_STR, &$uuid);
     return trim($uuid);
 }
Exemple #4
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);
 }
Exemple #5
0
/**
 * Generate a uuid.
 *
 * Unique is hard. Very hard. Attempt to use the PECL UUID functions if available, and if not then revert to
 * constructing the uuid using mt_rand.
 *
 * It is important that this token is not solely based on time as this could lead
 * to duplicates in a clustered environment (especially on VMs due to poor time precision).
 *
 * @return string The uuid.
 */
function generate_uuid()
{
    $uuid = '';
    if (function_exists("uuid_create")) {
        $context = null;
        uuid_create($context);
        uuid_make($context, UUID_MAKE_V4);
        uuid_export($context, UUID_FMT_STR, $uuid);
    } else {
        // Fallback uuid generation based on:
        // "http://www.php.net/manual/en/function.uniqid.php#94959".
        $uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
    }
    return trim($uuid);
}
	/**
	 * @static
	 * @return string
	 */
	static function uuid4() {
		$uuid = '';
		uuid_create( &$uuid );
		uuid_make( $uuid, UUID_MAKE_V4 );
		$uuidExport = '';
		uuid_export( $uuid, UUID_FMT_STR, &$uuidExport );
		return trim( $uuidExport );
	}
Exemple #7
0
/**
 * Generate a uuid.
 *
 * Unique is hard. Very hard. Attempt to use the PECL UUID functions if available, and if not then revert to
 * constructing the uuid using mt_rand.
 *
 * It is important that this token is not solely based on time as this could lead
 * to duplicates in a clustered environment (especially on VMs due to poor time precision).
 *
 * @return string The uuid.
 */
function generate_uuid() {
    $uuid = '';

    if (function_exists("uuid_create")) {
        $context = null;
        uuid_create($context);

        uuid_make($context, UUID_MAKE_V4);
        uuid_export($context, UUID_FMT_STR, $uuid);
    } else {
        // Fallback uuid generation based on:
        // "http://www.php.net/manual/en/function.uniqid.php#94959".
        $uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

            // 32 bits for "time_low".
            mt_rand(0, 0xffff), mt_rand(0, 0xffff),

            // 16 bits for "time_mid".
            mt_rand(0, 0xffff),

            // 16 bits for "time_hi_and_version",
            // four most significant bits holds version number 4.
            mt_rand(0, 0x0fff) | 0x4000,

            // 16 bits, 8 bits for "clk_seq_hi_res",
            // 8 bits for "clk_seq_low",
            // two most significant bits holds zero and one for variant DCE1.1.
            mt_rand(0, 0x3fff) | 0x8000,

            // 48 bits for "node".
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
    }
    return trim($uuid);
}
Exemple #8
0
<?php

/** uuid v4
 *  php5-uuid
 */
return function ($i_url = '0') {
    if (!function_exists('uuid_create')) {
        return false;
    }
    $context = $uuid = '';
    uuid_create(&$context);
    uuid_make($context, UUID_MAKE_V4);
    uuid_export($context, UUID_FMT_STR, &$uuid);
    return trim($uuid);
};
Exemple #9
0
 /**
  * Return a type 5 (SHA-1 hash) uuid
  *
  * @param String $namespace The namespace to use
  * @param String $name The name to use
  * @return String The newly generated UUID
  */
 public static function v5($namespace, $name)
 {
     if (!self::isValidUuid($namespace)) {
         return false;
     }
     Uuid::initialize();
     if (self::$method == self::USE_PECL_MAKE) {
         uuid_make(Uuid::$uobject, UUID_MAKE_V4);
         @uuid_export(Uuid::$uobject, UUID_FMT_STR, &$uuidstring);
         return trim($uuidstring);
     }
     // Get hexadecimal components of namespace
     $nhex = str_replace(array('-', '{', '}'), '', $namespace);
     // Binary Value
     $nstr = '';
     // Convert Namespace UUID to bits
     for ($i = 0; $i < strlen($nhex); $i += 2) {
         $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
     }
     // Calculate hash value
     $hash = sha1($nstr . $name);
     return sprintf('%08s-%04s-%04x-%04x-%12s', substr($hash, 0, 8), substr($hash, 8, 4), hexdec(substr($hash, 12, 4)) & 0xfff | 0x5000, hexdec(substr($hash, 16, 4)) & 0x3fff | 0x8000, substr($hash, 20, 12));
 }
Exemple #10
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");
    }
}
 /**
  * Wrapper function to fenerates a UUID via the pecl uuid module.
  * More {@link http://pwet.fr/man/linux/fonctions_bibliotheques/ossp/uuid doucmentation} on ossp uuid.
  *
  * <br/>
  * Version 1 UUIDs are guaranteed to be unique through combinations of hardware addresses, time stamps and random seeds. There is a reference in the UUID to the hardware (MAC) address of the first network interface card (NIC) on the host which generated the UUID this reference is intended to ensure the UUID will be unique in space as the MAC address of every network card is assigned by a single global authority (IEEE) and is guaranteed to be unique. The next component in a UUID is a timestamp which, as clock always (should) move forward, will be unique in time. Just in case some part of the above goes wrong (the hardware address cannot be determined or the clock moved steps backward), there is a random clock sequence component placed into the UUID as a catch-all for uniqueness.
  * <br/>
  * Version 3 and version 5 UUIDs are guaranteed to be inherently globally unique if the combination of namespace and name used to generate them is unique.  It is  not supported at the moment
  * <br/>
  * Version 4 UUIDs are not guaranteed to be globally unique, because they are generated out of locally gathered pseudo-random numbers only. Nevertheless there is still a high likelihood of uniqueness over space and time and that they are computationally difficult to guess. 
  *                                                                                                *
  * Loosely based off of work of  {@link http://www.php.net/manual/en/function.uniqid.php#88434 Marius Karthaus }
  *
  * @param string $version.  Defaults to 1
  * @retruns mixed string or false on failure
  */
 public static function generateUUID($version = 4)
 {
     if (!self::ensure()) {
         return false;
     }
     $uuidstring = '';
     switch ($version) {
         case 3:
             I2CE::raiseError("Version 3 not supported");
             return false;
             $version = UUID_MAKE_V3;
             break;
         case 5:
             I2CE::raiseError("Version 5 not supported");
             return false;
             $version = UUID_MAKE_V5;
             break;
         case 4:
             $version = UUID_MAKE_V4;
             break;
         default:
         case 1:
             $version = UUID_MAKE_V1;
             break;
     }
     if (0 != uuid_make(self::$uuidobject, $version) > 0) {
         //i think it returns a non-zero on failure
         return false;
     }
     uuid_export(self::$uuidobject, UUID_FMT_STR, &$uuidstring);
     return trim($uuidstring);
 }
Exemple #12
0
 public function export($fmt)
 {
     $data = "";
     uuid_export($this->uuid, $fmt, &$data);
     return $data;
 }
Exemple #13
-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);
     }
 }