uuid.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * @file
  4. * Handling of universally unique identifiers.
  5. */
  6. /**
  7. * Pattern for detecting a valid UUID.
  8. */
  9. define('UUID_PATTERN', '[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}');
  10. /**
  11. * Generates an universally unique identifier.
  12. *
  13. * This function first checks for the PECL alternative for generating
  14. * universally unique identifiers. If that doesn't exist, then it falls back on
  15. * PHP for generating that.
  16. *
  17. * @return
  18. * An UUID, made up of 32 hex digits and 4 hyphens.
  19. */
  20. function uuid_generate() {
  21. $callback = drupal_static(__FUNCTION__);
  22. if (empty($callback)) {
  23. if (function_exists('uuid_create') && !function_exists('uuid_make')) {
  24. $callback = '_uuid_generate_pecl';
  25. }
  26. elseif (function_exists('com_create_guid')) {
  27. $callback = '_uuid_generate_com';
  28. }
  29. else {
  30. $callback = '_uuid_generate_php';
  31. }
  32. }
  33. return $callback();
  34. }
  35. /**
  36. * Generate all missing UUIDs.
  37. */
  38. function uuid_sync_all() {
  39. module_invoke_all('uuid_sync');
  40. }
  41. /**
  42. * Generates a UUID URI for an entity.
  43. *
  44. * @param object $entity
  45. * The entity to use for generating the UUID URI.
  46. * @param string $entity_type
  47. * The type of entity being used.
  48. *
  49. * @return string
  50. * The generated UUID URI or normal URI if entity doesn't support UUIDs.
  51. */
  52. function uuid_entity_uuid_uri($entity, $entity_type) {
  53. $entity_info = entity_get_info($entity_type);
  54. if (empty($entity_info['uuid'])) {
  55. $uri = $entity_info['uri callback']($entity);
  56. return $uri['path'];
  57. }
  58. if (isset($entity_info['uuid uri callback'])) {
  59. return $entity_info['uuid uri callback']($entity);
  60. }
  61. return "uuid/{$entity_type}/" . $entity->{$entity_info['entity keys']['uuid']};
  62. }
  63. /**
  64. * Converts an ID URI string to an entity data array.
  65. *
  66. * @see uuid_id_uri_array_to_data()
  67. *
  68. * @param string $uri
  69. * The URI to convert.
  70. *
  71. * @return array
  72. * The entity data.
  73. */
  74. function uuid_id_uri_to_data($uri) {
  75. $parts = explode('/', $uri);
  76. return uuid_id_uri_array_to_data($parts);
  77. }
  78. /**
  79. * Converts a URI array to entity data array.
  80. *
  81. * @param array $uri
  82. * The URI parts, often taken from arg().
  83. *
  84. * @return array
  85. * The entity data.
  86. */
  87. function uuid_id_uri_array_to_data($uri) {
  88. $data = array(
  89. 'request' => $uri,
  90. 'entity_type' => $uri[0],
  91. 'id' => $uri[1],
  92. );
  93. drupal_alter('uuid_id_uri_data', $data);
  94. return $data;
  95. }
  96. /**
  97. * Converts a UUID URI string to an entity data array.
  98. *
  99. * @see uuid_uri_array_to_data()
  100. *
  101. * @param string $uri
  102. * The URI to convert.
  103. *
  104. * @return array
  105. * The entity data.
  106. */
  107. function uuid_uri_to_data($uri, $strip_uuid = TRUE) {
  108. return uuid_uri_array_to_data(explode('/', $uri));
  109. }
  110. /**
  111. * Converts a URI array to entity data array.
  112. *
  113. * @param array $uri
  114. * The URI parts, often taken from arg().
  115. *
  116. * @return array
  117. * The entity data.
  118. */
  119. function uuid_uri_array_to_data($uri, $strip_uuid = TRUE) {
  120. if ($strip_uuid) {
  121. array_shift($uri);
  122. }
  123. $data = array(
  124. 'request' => $uri,
  125. 'entity_type' => $uri[0],
  126. 'uuid' => $uri[1],
  127. );
  128. drupal_alter('uuid_uri_data', $data);
  129. return $data;
  130. }
  131. /**
  132. * Generates a UUID using the Windows internal GUID generator.
  133. *
  134. * @see http://php.net/com_create_guid
  135. */
  136. function _uuid_generate_com() {
  137. // Remove {} wrapper and make lower case to keep result consistent.
  138. return drupal_strtolower(trim(com_create_guid(), '{}'));
  139. }
  140. /**
  141. * Generates an universally unique identifier using the PECL extension.
  142. */
  143. function _uuid_generate_pecl() {
  144. return uuid_create(UUID_TYPE_DEFAULT);
  145. }
  146. /**
  147. * Generates a UUID v4 using PHP code.
  148. *
  149. * Based on code from http://php.net/uniqid#65879, but corrected.
  150. */
  151. function _uuid_generate_php() {
  152. // The field names refer to RFC 4122 section 4.1.2.
  153. return sprintf('%04x%04x-%04x-4%03x-%04x-%04x%04x%04x',
  154. // 32 bits for "time_low".
  155. mt_rand(0, 65535), mt_rand(0, 65535),
  156. // 16 bits for "time_mid".
  157. mt_rand(0, 65535),
  158. // 12 bits after the 0100 of (version) 4 for "time_hi_and_version".
  159. mt_rand(0, 4095),
  160. bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '10', 0, 2)),
  161. // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
  162. // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
  163. // 8 bits for "clk_seq_low" 48 bits for "node".
  164. mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)
  165. );
  166. }
  167. // This is wrapped in an if block to avoid conflicts with PECL's uuid_is_valid().
  168. if (!function_exists('uuid_is_valid')) {
  169. /**
  170. * Check that a string appears to be in the format of a UUID.
  171. *
  172. * @param $uuid
  173. * The string to test.
  174. *
  175. * @return
  176. * TRUE if the string is well formed.
  177. */
  178. function uuid_is_valid($uuid) {
  179. return preg_match('/^' . UUID_PATTERN . '$/', $uuid);
  180. }
  181. }