Projet

Général

Profil

Paste
Télécharger (5,35 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / uuid / uuid.inc @ 27945136

1
<?php
2

    
3
/**
4
 * @file
5
 * Handling of universally unique identifiers.
6
 */
7

    
8
/**
9
 * Pattern for detecting a valid UUID.
10
 */
11
define('UUID_PATTERN', '[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}');
12

    
13
/**
14
 * Generates an universally unique identifier.
15
 *
16
 * This function first checks for the PECL alternative for generating
17
 * universally unique identifiers. If that doesn't exist, then it falls back on
18
 * PHP for generating that.
19
 *
20
 * @return
21
 *   An UUID, made up of 32 hex digits and 4 hyphens.
22
 */
23
function uuid_generate() {
24
  $callback = drupal_static(__FUNCTION__);
25

    
26
  if (empty($callback)) {
27
    if (function_exists('uuid_create') && !function_exists('uuid_make')) {
28
      $callback = '_uuid_generate_pecl';
29
    }
30
    elseif (function_exists('com_create_guid')) {
31
      $callback = '_uuid_generate_com';
32
    }
33
    else {
34
      $callback = '_uuid_generate_php';
35
    }
36
  }
37
  return $callback();
38
}
39

    
40
/**
41
 * Generate all missing UUIDs.
42
 */
43
function uuid_sync_all() {
44
  module_invoke_all('uuid_sync');
45
}
46

    
47
/**
48
 * Generates a UUID URI for an entity.
49
 *
50
 * @param object $entity
51
 *   The entity to use for generating the UUID URI.
52
 * @param string $entity_type
53
 *   The type of entity being used.
54
 *
55
 * @return string
56
 *  The generated UUID URI or normal URI if entity doesn't support UUIDs.
57
 */
58
function uuid_entity_uuid_uri($entity, $entity_type) {
59
  $entity_info = entity_get_info($entity_type);
60

    
61
  if (empty($entity_info['uuid'])) {
62
    $uri = $entity_info['uri callback']($entity);
63
    return $uri['path'];
64
  }
65

    
66
  if (isset($entity_info['uuid uri callback'])) {
67
    return $entity_info['uuid uri callback']($entity);
68
  }
69

    
70
  return "uuid/{$entity_type}/" . $entity->{$entity_info['entity keys']['uuid']};
71
}
72

    
73
/**
74
 * Converts an ID URI string to an entity data array.
75
 *
76
 * @see uuid_id_uri_array_to_data()
77
 *
78
 * @param string $uri
79
 *  The URI to convert.
80
 *
81
 * @return array
82
 *  The entity data.
83
 */
84
function uuid_id_uri_to_data($uri) {
85
  $parts = explode('/', $uri);
86
  return uuid_id_uri_array_to_data($parts);
87
}
88

    
89
/**
90
 * Converts a URI array to entity data array.
91
 *
92
 * @param array $uri
93
 *  The URI parts, often taken from arg().
94
 *
95
 * @return array
96
 *  The entity data.
97
 */
98
function uuid_id_uri_array_to_data($uri) {
99
  $data = array(
100
    'request' => $uri,
101
    'entity_type' => $uri[0],
102
    'id' => $uri[1],
103
  );
104

    
105
  drupal_alter('uuid_id_uri_data', $data);
106

    
107
  return $data;
108
}
109

    
110
/**
111
 * Converts a UUID URI string to an entity data array.
112
 *
113
 * @see uuid_uri_array_to_data()
114
 *
115
 * @param string $uri
116
 *  The URI to convert.
117
 *
118
 * @return array
119
 *  The entity data.
120
 */
121
function uuid_uri_to_data($uri, $strip_uuid = TRUE) {
122
  return uuid_uri_array_to_data(explode('/', $uri));
123
}
124

    
125
/**
126
 * Converts a URI array to entity data array.
127
 *
128
 * @param array $uri
129
 *  The URI parts, often taken from arg().
130
 *
131
 * @return array
132
 *  The entity data.
133
 */
134
function uuid_uri_array_to_data($uri, $strip_uuid = TRUE) {
135
  if ($strip_uuid) {
136
    array_shift($uri);
137
  }
138

    
139
  $data = array(
140
    'request' => $uri,
141
    'entity_type' => $uri[0],
142
    'uuid' => $uri[1],
143
  );
144

    
145
  drupal_alter('uuid_uri_data', $data);
146

    
147
  return $data;
148
}
149

    
150
/**
151
 * Generates a UUID using the Windows internal GUID generator.
152
 *
153
 * @see http://php.net/com_create_guid
154
 */
155
function _uuid_generate_com() {
156
  // Remove {} wrapper and make lower case to keep result consistent.
157
  return drupal_strtolower(trim(com_create_guid(), '{}'));
158
}
159

    
160
/**
161
 * Generates an universally unique identifier using the PECL extension.
162
 */
163
function _uuid_generate_pecl() {
164
  return uuid_create(UUID_TYPE_DEFAULT);
165
}
166

    
167
/**
168
 * Generates a UUID v4 (RFC 4122 section 4.4) using PHP code.
169
 *
170
 * @see http://www.rfc-editor.org/rfc/rfc4122.txt
171
 *
172
 * The UUID layout and fields are defined in section 4.1.2.
173
 *
174
 * Note that there are inconsistencies in the RFC with respect to
175
 * bit numbering. Network Order is correct, so the most significant bit
176
 * always appears first (in left-to-right sequence). See errata 3546:
177
 * http://www.rfc-editor.org/errata_search.php?rfc=4122&eid=3546
178
 *
179
 * Based on code from http://php.net/uniqid
180
 */
181
function _uuid_generate_php() {
182
  // We limit each generated number to 16 bits (maximum value 0xffff)
183
  // because mt_rand() returns a *signed* integer, and hence a 32-bit
184
  // value can only have a 31-bit magnitude. Constructing a 32-bit
185
  // number from two 16-bit random numbers guarantees that all 32 bits
186
  // are random.
187
  return sprintf('%04x%04x-%04x-4%03x-%04x-%04x%04x%04x',
188
    // 32 bits for "time_low".
189
    mt_rand(0, 0xffff), mt_rand(0, 0xffff),
190
    // 16 bits for "time_mid".
191
    mt_rand(0, 0xffff),
192
    // 12 bits after the initial 0100 (version 4) for "time_hi_and_version".
193
    mt_rand(0, 0x0fff),
194
    // 16 bits in total for "clk_seq_hi_res" and "clk_seq_low", with the
195
    // most significant 2 bits of clk_seq_hi_res set to '10'. We do a
196
    // bitwise OR of a random 14-bit value (maximum 0x3fff) with 0x8000
197
    // (a 16-bit integer with only the most significant bit set).
198
    mt_rand(0, 0x3fff) | 0x8000,
199
    // 48 bits for "node".
200
    mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
201
  );
202
}
203

    
204

    
205
// This is wrapped in an if block to avoid conflicts with PECL's uuid_is_valid().
206
if (!function_exists('uuid_is_valid')) {
207
  /**
208
   * Check that a string appears to be in the format of a UUID.
209
   *
210
   * @param $uuid
211
   *  The string to test.
212
   *
213
   * @return
214
   *   TRUE if the string is well formed.
215
   */
216
  function uuid_is_valid($uuid) {
217
    return preg_match('/^' . UUID_PATTERN . '$/', $uuid);
218
  }
219
}