1<?php 2 3namespace Sabre\VObject; 4 5/** 6 * UUID Utility. 7 * 8 * This class has static methods to generate and validate UUID's. 9 * UUIDs are used a decent amount within various *DAV standards, so it made 10 * sense to include it. 11 * 12 * @copyright Copyright (C) fruux GmbH (https://fruux.com/) 13 * @author Evert Pot (http://evertpot.com/) 14 * @license http://sabre.io/license/ Modified BSD License 15 */ 16class 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 * 25 * @return string 26 */ 27 static function getUUID() { 28 29 return sprintf( 30 31 '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 32 33 // 32 bits for "time_low" 34 mt_rand(0, 0xffff), mt_rand(0, 0xffff), 35 36 // 16 bits for "time_mid" 37 mt_rand(0, 0xffff), 38 39 // 16 bits for "time_hi_and_version", 40 // four most significant bits holds version number 4 41 mt_rand(0, 0x0fff) | 0x4000, 42 43 // 16 bits, 8 bits for "clk_seq_hi_res", 44 // 8 bits for "clk_seq_low", 45 // two most significant bits holds zero and one for variant DCE1.1 46 mt_rand(0, 0x3fff) | 0x8000, 47 48 // 48 bits for "node" 49 mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) 50 ); 51 } 52 53 /** 54 * Checks if a string is a valid UUID. 55 * 56 * @param string $uuid 57 * 58 * @return bool 59 */ 60 static function validateUUID($uuid) { 61 62 return preg_match( 63 '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i', 64 $uuid 65 ) !== 0; 66 67 } 68 69} 70