]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAV/UUIDUtil.php
Merge pull request #73 from CatoTH/master
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAV / UUIDUtil.php
1 <?php
2
3 /**
4  * UUID Utility
5  *
6  * This class has static methods to generate and validate UUID's.
7  * UUIDs are used a decent amount within various *DAV standards, so it made
8  * sense to include it.
9  *
10  * @package Sabre
11  * @subpackage DAV
12  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
13  * @author Evert Pot (http://www.rooftopsolutions.nl/)
14  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
15  */
16 class Sabre_DAV_UUIDUtil {
17
18     /**
19      * Returns a pseudo-random v4 UUID
20      *
21      * This function is based on a comment by Andrew Moore on php.net
22      *
23      * @see http://www.php.net/manual/en/function.uniqid.php#94959
24      * @return string
25      */
26     static function getUUID() {
27
28         return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
29             // 32 bits for "time_low"
30             mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
31
32             // 16 bits for "time_mid"
33             mt_rand( 0, 0xffff ),
34
35             // 16 bits for "time_hi_and_version",
36             // four most significant bits holds version number 4
37             mt_rand( 0, 0x0fff ) | 0x4000,
38
39             // 16 bits, 8 bits for "clk_seq_hi_res",
40             // 8 bits for "clk_seq_low",
41             // two most significant bits holds zero and one for variant DCE1.1
42             mt_rand( 0, 0x3fff ) | 0x8000,
43
44             // 48 bits for "node"
45             mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
46         );
47     }
48
49     /**
50      * Checks if a string is a valid UUID.
51      *
52      * @param string $uuid
53      * @return bool
54      */
55     static function validateUUID($uuid) {
56
57         return preg_match(
58             '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i',
59             $uuid
60         ) == true;
61
62     }
63
64 }