1*a1a3b679SAndreas Boehler<?php 2*a1a3b679SAndreas Boehler 3*a1a3b679SAndreas Boehlernamespace Sabre\DAV; 4*a1a3b679SAndreas Boehler 5*a1a3b679SAndreas Boehler/** 6*a1a3b679SAndreas Boehler * UUID Utility 7*a1a3b679SAndreas Boehler * 8*a1a3b679SAndreas Boehler * This class has static methods to generate and validate UUID's. 9*a1a3b679SAndreas Boehler * UUIDs are used a decent amount within various *DAV standards, so it made 10*a1a3b679SAndreas Boehler * sense to include it. 11*a1a3b679SAndreas Boehler * 12*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/). 13*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/) 14*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License 15*a1a3b679SAndreas Boehler */ 16*a1a3b679SAndreas Boehlerclass UUIDUtil { 17*a1a3b679SAndreas Boehler 18*a1a3b679SAndreas Boehler /** 19*a1a3b679SAndreas Boehler * Returns a pseudo-random v4 UUID 20*a1a3b679SAndreas Boehler * 21*a1a3b679SAndreas Boehler * This function is based on a comment by Andrew Moore on php.net 22*a1a3b679SAndreas Boehler * 23*a1a3b679SAndreas Boehler * @see http://www.php.net/manual/en/function.uniqid.php#94959 24*a1a3b679SAndreas Boehler * @return string 25*a1a3b679SAndreas Boehler */ 26*a1a3b679SAndreas Boehler static function getUUID() { 27*a1a3b679SAndreas Boehler 28*a1a3b679SAndreas Boehler return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 29*a1a3b679SAndreas Boehler // 32 bits for "time_low" 30*a1a3b679SAndreas Boehler mt_rand(0, 0xffff), mt_rand(0, 0xffff), 31*a1a3b679SAndreas Boehler 32*a1a3b679SAndreas Boehler // 16 bits for "time_mid" 33*a1a3b679SAndreas Boehler mt_rand(0, 0xffff), 34*a1a3b679SAndreas Boehler 35*a1a3b679SAndreas Boehler // 16 bits for "time_hi_and_version", 36*a1a3b679SAndreas Boehler // four most significant bits holds version number 4 37*a1a3b679SAndreas Boehler mt_rand(0, 0x0fff) | 0x4000, 38*a1a3b679SAndreas Boehler 39*a1a3b679SAndreas Boehler // 16 bits, 8 bits for "clk_seq_hi_res", 40*a1a3b679SAndreas Boehler // 8 bits for "clk_seq_low", 41*a1a3b679SAndreas Boehler // two most significant bits holds zero and one for variant DCE1.1 42*a1a3b679SAndreas Boehler mt_rand(0, 0x3fff) | 0x8000, 43*a1a3b679SAndreas Boehler 44*a1a3b679SAndreas Boehler // 48 bits for "node" 45*a1a3b679SAndreas Boehler mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) 46*a1a3b679SAndreas Boehler ); 47*a1a3b679SAndreas Boehler } 48*a1a3b679SAndreas Boehler 49*a1a3b679SAndreas Boehler /** 50*a1a3b679SAndreas Boehler * Checks if a string is a valid UUID. 51*a1a3b679SAndreas Boehler * 52*a1a3b679SAndreas Boehler * @param string $uuid 53*a1a3b679SAndreas Boehler * @return bool 54*a1a3b679SAndreas Boehler */ 55*a1a3b679SAndreas Boehler static function validateUUID($uuid) { 56*a1a3b679SAndreas Boehler 57*a1a3b679SAndreas Boehler return preg_match( 58*a1a3b679SAndreas Boehler '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i', 59*a1a3b679SAndreas Boehler $uuid 60*a1a3b679SAndreas Boehler ) !== 0; 61*a1a3b679SAndreas Boehler 62*a1a3b679SAndreas Boehler } 63*a1a3b679SAndreas Boehler 64*a1a3b679SAndreas Boehler} 65