Révision 4eeb3b46
Ajouté par Assos Assos il y a presque 8 ans
drupal7/sites/all/modules/uuid/uuid.inc | ||
---|---|---|
8 | 8 |
/** |
9 | 9 |
* Pattern for detecting a valid UUID. |
10 | 10 |
*/ |
11 |
define('UUID_PATTERN', '[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}');
|
|
11 |
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}');
|
|
12 | 12 |
|
13 | 13 |
/** |
14 | 14 |
* Generates an universally unique identifier. |
... | ... | |
17 | 17 |
* universally unique identifiers. If that doesn't exist, then it falls back on |
18 | 18 |
* PHP for generating that. |
19 | 19 |
* |
20 |
* @return |
|
20 |
* @return string
|
|
21 | 21 |
* An UUID, made up of 32 hex digits and 4 hyphens. |
22 | 22 |
*/ |
23 | 23 |
function uuid_generate() { |
... | ... | |
37 | 37 |
return $callback(); |
38 | 38 |
} |
39 | 39 |
|
40 |
/** |
|
41 |
* Generates a version 5 compliant UUID. |
|
42 |
* |
|
43 |
* @param string $namespace |
|
44 |
* Namespace UUID as a hex encoded string. |
|
45 |
* @param string $name |
|
46 |
* The name for the generating the UUID. |
|
47 |
* |
|
48 |
* @return string |
|
49 |
* UUID as a hex encoded string. |
|
50 |
* |
|
51 |
* @link http://www.php.net/manual/en/function.uniqid.php#94959 Code lifted from |
|
52 |
* PHP manual comment by Andrew Moore. @endlink |
|
53 |
*/ |
|
54 |
function uuid_generate_v5($namespace, $name) { |
|
55 |
if (!uuid_is_valid($namespace)) { |
|
56 |
return FALSE; |
|
57 |
} |
|
58 |
|
|
59 |
// Get hexadecimal components of namespace. |
|
60 |
$nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
61 |
|
|
62 |
// Binary Value. |
|
63 |
$nstr = ''; |
|
64 |
|
|
65 |
// Convert Namespace UUID to bits. |
|
66 |
for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
67 |
$nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
68 |
} |
|
69 |
|
|
70 |
// Calculate hash value. |
|
71 |
$hash = sha1($nstr . $name); |
|
72 |
|
|
73 |
return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
74 |
|
|
75 |
// 32 bits for "time_low". |
|
76 |
substr($hash, 0, 8), |
|
77 |
|
|
78 |
// 16 bits for "time_mid". |
|
79 |
substr($hash, 8, 4), |
|
80 |
|
|
81 |
// 16 bits for "time_hi_and_version", |
|
82 |
// four most significant bits holds version number 5. |
|
83 |
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
84 |
|
|
85 |
// 16 bits, 8 bits for "clk_seq_hi_res", |
|
86 |
// 8 bits for "clk_seq_low", |
|
87 |
// two most significant bits holds zero and one for variant DCE1.1. |
|
88 |
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
89 |
|
|
90 |
// 48 bits for "node". |
|
91 |
substr($hash, 20, 12) |
|
92 |
); |
|
93 |
} |
|
94 |
|
|
40 | 95 |
/** |
41 | 96 |
* Generate all missing UUIDs. |
42 | 97 |
*/ |
... | ... | |
53 | 108 |
* The type of entity being used. |
54 | 109 |
* |
55 | 110 |
* @return string |
56 |
* The generated UUID URI or normal URI if entity doesn't support UUIDs. |
|
111 |
* The generated UUID URI or normal URI if entity doesn't support UUIDs.
|
|
57 | 112 |
*/ |
58 | 113 |
function uuid_entity_uuid_uri($entity, $entity_type) { |
59 | 114 |
$entity_info = entity_get_info($entity_type); |
... | ... | |
73 | 128 |
/** |
74 | 129 |
* Converts an ID URI string to an entity data array. |
75 | 130 |
* |
76 |
* @see uuid_id_uri_array_to_data() |
|
77 |
* |
|
78 | 131 |
* @param string $uri |
79 |
* The URI to convert. |
|
132 |
* The URI to convert.
|
|
80 | 133 |
* |
81 | 134 |
* @return array |
82 |
* The entity data. |
|
135 |
* The entity data. |
|
136 |
* |
|
137 |
* @see uuid_id_uri_array_to_data() |
|
83 | 138 |
*/ |
84 | 139 |
function uuid_id_uri_to_data($uri) { |
85 | 140 |
$parts = explode('/', $uri); |
... | ... | |
90 | 145 |
* Converts a URI array to entity data array. |
91 | 146 |
* |
92 | 147 |
* @param array $uri |
93 |
* The URI parts, often taken from arg(). |
|
148 |
* The URI parts, often taken from arg().
|
|
94 | 149 |
* |
95 | 150 |
* @return array |
96 |
* The entity data. |
|
151 |
* The entity data.
|
|
97 | 152 |
*/ |
98 | 153 |
function uuid_id_uri_array_to_data($uri) { |
99 | 154 |
$data = array( |
... | ... | |
110 | 165 |
/** |
111 | 166 |
* Converts a UUID URI string to an entity data array. |
112 | 167 |
* |
113 |
* @see uuid_uri_array_to_data() |
|
114 |
* |
|
115 | 168 |
* @param string $uri |
116 |
* The URI to convert. |
|
169 |
* The URI to convert.
|
|
117 | 170 |
* |
118 | 171 |
* @return array |
119 |
* The entity data. |
|
172 |
* The entity data. |
|
173 |
* |
|
174 |
* @see uuid_uri_array_to_data() |
|
120 | 175 |
*/ |
121 | 176 |
function uuid_uri_to_data($uri, $strip_uuid = TRUE) { |
122 | 177 |
return uuid_uri_array_to_data(explode('/', $uri)); |
... | ... | |
126 | 181 |
* Converts a URI array to entity data array. |
127 | 182 |
* |
128 | 183 |
* @param array $uri |
129 |
* The URI parts, often taken from arg(). |
|
184 |
* The URI parts, often taken from arg().
|
|
130 | 185 |
* |
131 | 186 |
* @return array |
132 |
* The entity data. |
|
187 |
* The entity data.
|
|
133 | 188 |
*/ |
134 | 189 |
function uuid_uri_array_to_data($uri, $strip_uuid = TRUE) { |
135 | 190 |
if ($strip_uuid) { |
... | ... | |
138 | 193 |
|
139 | 194 |
$data = array( |
140 | 195 |
'request' => $uri, |
141 |
'entity_type' => $uri[0],
|
|
142 |
'uuid' => $uri[1],
|
|
196 |
'entity_type' => isset($uri[0]) ? $uri[0] : NULL,
|
|
197 |
'uuid' => isset($uri[1]) ? $uri[1] : NULL,
|
|
143 | 198 |
); |
144 | 199 |
|
145 | 200 |
drupal_alter('uuid_uri_data', $data); |
... | ... | |
201 | 256 |
); |
202 | 257 |
} |
203 | 258 |
|
204 |
|
|
205 |
// This is wrapped in an if block to avoid conflicts with PECL's uuid_is_valid(). |
|
259 |
// The if block avoids conflicts with PECL's uuid_is_valid(). |
|
206 | 260 |
if (!function_exists('uuid_is_valid')) { |
261 |
|
|
207 | 262 |
/** |
208 | 263 |
* Check that a string appears to be in the format of a UUID. |
209 | 264 |
* |
210 |
* @param $uuid |
|
211 |
* The string to test. |
|
265 |
* @param string $uuid
|
|
266 |
* The string to test.
|
|
212 | 267 |
* |
213 |
* @return |
|
214 |
* TRUE if the string is well formed. |
|
268 |
* @return bool
|
|
269 |
* TRUE if the string is well formed.
|
|
215 | 270 |
*/ |
216 | 271 |
function uuid_is_valid($uuid) { |
217 | 272 |
return preg_match('/^' . UUID_PATTERN . '$/', $uuid); |
218 | 273 |
} |
274 |
|
|
219 | 275 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules