/vendor/sabre/vobject/lib/UUIDUtil.php

https://github.com/xiebruce/PicUploader · PHP · 66 lines · 23 code · 8 blank · 35 comment · 0 complexity · 557ca2e896b3668aa039ee437b597885 MD5 · raw file

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