123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- define('UUID_PATTERN', '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}');
- function uuid_generate() {
- $callback = drupal_static(__FUNCTION__);
- if (empty($callback)) {
- if (function_exists('uuid_create') && !function_exists('uuid_make')) {
- $callback = '_uuid_generate_pecl';
- }
- elseif (function_exists('com_create_guid')) {
- $callback = '_uuid_generate_com';
- }
- else {
- $callback = '_uuid_generate_php';
- }
- }
- return $callback();
- }
- function uuid_generate_v5($namespace, $name) {
- if (!uuid_is_valid($namespace)) {
- return FALSE;
- }
-
- $nhex = str_replace(array('-', '{', '}'), '', $namespace);
-
- $nstr = '';
-
- for ($i = 0; $i < strlen($nhex); $i += 2) {
- $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
- }
-
- $hash = sha1($nstr . $name);
- return sprintf('%08s-%04s-%04x-%04x-%12s',
-
- substr($hash, 0, 8),
-
- substr($hash, 8, 4),
-
-
- (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
-
-
-
- (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
-
- substr($hash, 20, 12)
- );
- }
- function uuid_sync_all() {
- module_invoke_all('uuid_sync');
- }
- function uuid_entity_uuid_uri($entity, $entity_type) {
- $entity_info = entity_get_info($entity_type);
- if (empty($entity_info['uuid'])) {
- $uri = $entity_info['uri callback']($entity);
- return $uri['path'];
- }
- if (isset($entity_info['uuid uri callback'])) {
- return $entity_info['uuid uri callback']($entity);
- }
- return "uuid/{$entity_type}/" . $entity->{$entity_info['entity keys']['uuid']};
- }
- function uuid_id_uri_to_data($uri) {
- $parts = explode('/', $uri);
- return uuid_id_uri_array_to_data($parts);
- }
- function uuid_id_uri_array_to_data($uri) {
- $data = array(
- 'request' => $uri,
- 'entity_type' => $uri[0],
- 'id' => $uri[1],
- );
- drupal_alter('uuid_id_uri_data', $data);
- return $data;
- }
- function uuid_uri_to_data($uri, $strip_uuid = TRUE) {
- return uuid_uri_array_to_data(explode('/', $uri));
- }
- function uuid_uri_array_to_data($uri, $strip_uuid = TRUE) {
- if ($strip_uuid) {
- array_shift($uri);
- }
- $data = array(
- 'request' => $uri,
- 'entity_type' => isset($uri[0]) ? $uri[0] : NULL,
- 'uuid' => isset($uri[1]) ? $uri[1] : NULL,
- );
- drupal_alter('uuid_uri_data', $data);
- return $data;
- }
- function _uuid_generate_com() {
-
- return drupal_strtolower(trim(com_create_guid(), '{}'));
- }
- function _uuid_generate_pecl() {
- return uuid_create(UUID_TYPE_DEFAULT);
- }
- function _uuid_generate_php() {
-
-
-
-
-
- return sprintf('%04x%04x-%04x-4%03x-%04x-%04x%04x%04x',
-
- mt_rand(0, 0xffff), mt_rand(0, 0xffff),
-
- mt_rand(0, 0xffff),
-
- mt_rand(0, 0x0fff),
-
-
-
-
- mt_rand(0, 0x3fff) | 0x8000,
-
- mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
- );
- }
- if (!function_exists('uuid_is_valid')) {
-
- function uuid_is_valid($uuid) {
- return preg_match('/^' . UUID_PATTERN . '$/', $uuid);
- }
- }
|