PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Sabre/DAV/UUIDUtil.php

https://github.com/KOLANICH/SabreDAV
PHP | 64 lines | 19 code | 12 blank | 33 comment | 1 complexity | 2945bdc488ce7c1bc1ac22007490fb71 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\DAV;
  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. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  11. * @author Evert Pot (http://evertpot.com/)
  12. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  13. */
  14. class UUIDUtil {
  15. /**
  16. * Returns a pseudo-random v4 UUID
  17. *
  18. * This function is based on a comment by Andrew Moore on php.net
  19. *
  20. * @see http://www.php.net/manual/en/function.uniqid.php#94959
  21. * @return string
  22. */
  23. static function getUUID() {
  24. return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  25. // 32 bits for "time_low"
  26. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
  27. // 16 bits for "time_mid"
  28. mt_rand( 0, 0xffff ),
  29. // 16 bits for "time_hi_and_version",
  30. // four most significant bits holds version number 4
  31. mt_rand( 0, 0x0fff ) | 0x4000,
  32. // 16 bits, 8 bits for "clk_seq_hi_res",
  33. // 8 bits for "clk_seq_low",
  34. // two most significant bits holds zero and one for variant DCE1.1
  35. mt_rand( 0, 0x3fff ) | 0x8000,
  36. // 48 bits for "node"
  37. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
  38. );
  39. }
  40. /**
  41. * Checks if a string is a valid UUID.
  42. *
  43. * @param string $uuid
  44. * @return bool
  45. */
  46. static function validateUUID($uuid) {
  47. return preg_match(
  48. '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i',
  49. $uuid
  50. ) == true;
  51. }
  52. }