Advertisement
Guest User

Untitled

a guest
Oct 26th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. <?php
  2. class UUID {
  3. public static function v3($namespace, $name) {
  4. if(!self::is_valid($namespace)) return false;
  5.  
  6. // Get hexadecimal components of namespace
  7. $nhex = str_replace(array('-','{','}'), '', $namespace);
  8.  
  9. // Binary Value
  10. $nstr = '';
  11.  
  12. // Convert Namespace UUID to bits
  13. for($i = 0; $i < strlen($nhex); $i+=2) {
  14. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  15. }
  16.  
  17. // Calculate hash value
  18. $hash = md5($nstr . $name);
  19.  
  20. return sprintf('%08s-%04s-%04x-%04x-%12s',
  21.  
  22. // 32 bits for "time_low"
  23. substr($hash, 0, 8),
  24.  
  25. // 16 bits for "time_mid"
  26. substr($hash, 8, 4),
  27.  
  28. // 16 bits for "time_hi_and_version",
  29. // four most significant bits holds version number 3
  30. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
  31.  
  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. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  36.  
  37. // 48 bits for "node"
  38. substr($hash, 20, 12)
  39. );
  40. }
  41.  
  42. public static function v4() {
  43. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  44.  
  45. // 32 bits for "time_low"
  46. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  47.  
  48. // 16 bits for "time_mid"
  49. mt_rand(0, 0xffff),
  50.  
  51. // 16 bits for "time_hi_and_version",
  52. // four most significant bits holds version number 4
  53. mt_rand(0, 0x0fff) | 0x4000,
  54.  
  55. // 16 bits, 8 bits for "clk_seq_hi_res",
  56. // 8 bits for "clk_seq_low",
  57. // two most significant bits holds zero and one for variant DCE1.1
  58. mt_rand(0, 0x3fff) | 0x8000,
  59.  
  60. // 48 bits for "node"
  61. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  62. );
  63. }
  64.  
  65. public static function v5($namespace, $name) {
  66. if(!self::is_valid($namespace)) return false;
  67.  
  68. // Get hexadecimal components of namespace
  69. $nhex = str_replace(array('-','{','}'), '', $namespace);
  70.  
  71. // Binary Value
  72. $nstr = '';
  73.  
  74. // Convert Namespace UUID to bits
  75. for($i = 0; $i < strlen($nhex); $i+=2) {
  76. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  77. }
  78.  
  79. // Calculate hash value
  80. $hash = sha1($nstr . $name);
  81.  
  82. return sprintf('%08s-%04s-%04x-%04x-%12s',
  83.  
  84. // 32 bits for "time_low"
  85. substr($hash, 0, 8),
  86.  
  87. // 16 bits for "time_mid"
  88. substr($hash, 8, 4),
  89.  
  90. // 16 bits for "time_hi_and_version",
  91. // four most significant bits holds version number 5
  92. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
  93.  
  94. // 16 bits, 8 bits for "clk_seq_hi_res",
  95. // 8 bits for "clk_seq_low",
  96. // two most significant bits holds zero and one for variant DCE1.1
  97. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  98.  
  99. // 48 bits for "node"
  100. substr($hash, 20, 12)
  101. );
  102. }
  103.  
  104. public static function is_valid($uuid) {
  105. return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
  106. '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  107. }
  108. }
  109.  
  110. // Pseudo-random UUID
  111.  
  112. $v4uuid = UUID::v4();
  113.  
  114. $request = xmlrpc_encode_request("createuser", array(
  115. "FirstName" => $_POST["first"],
  116. "LastName" => $_POST["last"],
  117. "Email" => $_POST["email"],
  118. "Password" => $_POST["password"],
  119. "PrincipalID" => $v4uuid,
  120. ));
  121.  
  122. $context = stream_context_create(array('http' => array(
  123. 'method' => "POST",
  124. 'header' => "Content-Type: text/xml\r\nconnection: close\r\n",
  125. 'content' => $request
  126. )));
  127.  
  128. $file = file_get_contents("http://localhost:8003", false, $context);
  129.  
  130. $response = xmlrpc_decode($file);
  131. if ($response && xmlrpc_is_fault($response)) {
  132. trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  133. } else {
  134. print_r($response);
  135. }
  136.  
  137. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement