Projet

Général

Profil

Paste
Télécharger (4,67 ko) Statistiques
| Branche: | Révision:

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

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 using PHP code.
169
 *
170
 * Based on code from http://php.net/uniqid#65879, but corrected.
171
 */
172
function _uuid_generate_php() {
173
  // The field names refer to RFC 4122 section 4.1.2.
174
  return sprintf('%04x%04x-%04x-4%03x-%04x-%04x%04x%04x',
175
    // 32 bits for "time_low".
176
    mt_rand(0, 65535), mt_rand(0, 65535),
177
    // 16 bits for "time_mid".
178
    mt_rand(0, 65535),
179
    // 12 bits after the 0100 of (version) 4 for "time_hi_and_version".
180
    mt_rand(0, 4095),
181
    bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '10', 0, 2)),
182
    // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
183
    // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
184
    // 8 bits for "clk_seq_low" 48 bits for "node".
185
    mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)
186
  );
187
}
188

    
189

    
190
// This is wrapped in an if block to avoid conflicts with PECL's uuid_is_valid().
191
if (!function_exists('uuid_is_valid')) {
192
  /**
193
   * Check that a string appears to be in the format of a UUID.
194
   *
195
   * @param $uuid
196
   *  The string to test.
197
   *
198
   * @return
199
   *   TRUE if the string is well formed.
200
   */
201
  function uuid_is_valid($uuid) {
202
    return preg_match('/^' . UUID_PATTERN . '$/', $uuid);
203
  }
204
}