Projet

Général

Profil

Paste
Télécharger (2,01 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / includes / uuid.inc @ 1e39edcb

1
<?php
2

    
3
/**
4
 * @file
5
 * Enables ctools generated modules to use UUIDs without the UUID module enabled.
6
 * Per the ctools.module, this file only gets included if UUID doesn't exist.
7
 */
8

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

    
16
/**
17
 * Generates a UUID using the Windows internal GUID generator.
18
 *
19
 * @see http://php.net/com_create_guid
20
 */
21
function _ctools_uuid_generate_com() {
22
  // Remove {} wrapper and make lower case to keep result consistent.
23
  return drupal_strtolower(trim(com_create_guid(), '{}'));
24
}
25

    
26
/**
27
 * Generates an universally unique identifier using the PECL extension.
28
 */
29
function _ctools_uuid_generate_pecl() {
30
  $uuid_type = UUID_TYPE_DEFAULT;
31
  return uuid_create($uuid_type);
32
}
33

    
34
/**
35
 * Generates a UUID v4 using PHP code.
36
 *
37
 * Based on code from @see http://php.net/uniqid#65879 , but corrected.
38
 */
39
function _ctools_uuid_generate_php() {
40
  // The field names refer to RFC 4122 section 4.1.2.
41
  return sprintf('%04x%04x-%04x-4%03x-%04x-%04x%04x%04x',
42
    // 32 bits for "time_low".
43
    mt_rand(0, 65535), mt_rand(0, 65535),
44
    // 16 bits for "time_mid".
45
    mt_rand(0, 65535),
46
    // 12 bits after the 0100 of (version) 4 for "time_hi_and_version".
47
    mt_rand(0, 4095),
48
    bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '10', 0, 2)),
49
    // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
50
    // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
51
    // 8 bits for "clk_seq_low" 48 bits for "node".
52
    mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)
53
  );
54
}
55

    
56
// This is wrapped in an if block to avoid conflicts with PECL's uuid_is_valid().
57
/**
58
 * Check that a string appears to be in the format of a UUID.
59
 *
60
 * @param $uuid
61
 *  The string to test.
62
 *
63
 * @return
64
 *   TRUE if the string is well formed.
65
 */
66
if (!function_exists('uuid_is_valid')) {
67
  function uuid_is_valid($uuid) {
68
    return preg_match('/^' . UUID_PATTERN . '$/', $uuid);
69
  }
70
}