Subversion Repositories uuid_mac_utils

Rev

Rev 45 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * UUID interpreter for PHP
  5.  * Copyright 2017 - 2023 Daniel Marschall, ViaThinkSoft
  6.  * Version 2023-08-02
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  */
  20.  
  21. $uuid = isset($_GET['uuid']) ? trim($_GET['uuid']) : 'CREATE';
  22.  
  23. $version = $_REQUEST['version'] ?? null;
  24. if (!is_null($version)) {
  25.         if (preg_match('@^(8)_namebased_(.+)$@', $version, $m)) {
  26.                 $version = $m[1];
  27.                 $hash_uuid = $m[2];
  28.         } else {
  29.                 $hash_uuid = null;
  30.         }
  31. } else {
  32.         $hash_uuid = null;
  33. }
  34. if (!is_numeric($version) || (strlen($version)!=1)) $version = 1; // default: Version 1 / time based
  35.  
  36. if ($uuid == 'CREATE') {
  37.         $title = 'Generate a UUIDv'.$version;
  38. } else {
  39.         $title = 'Interprete a UUID';
  40. }
  41.  
  42. ?><html>
  43.  
  44. <head>
  45.         <meta charset="iso-8859-1">
  46.         <link rel="stylesheet" type="text/css" href="style.css">
  47.         <title><?php echo htmlentities($title); ?></title>
  48.         <meta name=viewport content="width=device-width, initial-scale=1">
  49. </head>
  50.  
  51. <body>
  52.  
  53. <h1><?php echo htmlentities($title); ?></h1>
  54.  
  55. <p><a href="index.php">Back</a></p>
  56.  
  57. <?php
  58.  
  59. if ($uuid != 'CREATE') {
  60.         echo '<form method="GET" action="interprete_uuid.php">';
  61.         echo '  UUID: <input style="font-family:Courier,Courier New,Serif;width:325px" type="text" name="uuid" value="'.htmlentities($uuid).'"> <input type="submit" value="Interprete">';
  62.         echo '</form>';
  63. } else if (($version!=3) && ($version!=5) && ($version!=8)) {
  64.         echo '<p><i>Reload the page to receive another UUID.</i></p>';
  65. }
  66.  
  67. echo '<pre>';
  68.  
  69. require_once __DIR__ . '/includes/uuid_utils.inc.php';
  70.  
  71. try {
  72.         if ($uuid == 'CREATE') {
  73.                 if ($version == '1') {
  74.                         $uuid = gen_uuid_timebased();
  75.                 } else if ($version == '2') {
  76.                         $uuid = gen_uuid_dce(trim($_REQUEST['dce_domain']??''), trim($_REQUEST['dce_id']??''));
  77.                 } else if ($version == '3') {
  78.                         $uuid = gen_uuid_md5_namebased(trim($_REQUEST['nb_ns']??''), trim($_REQUEST['nb_val']??''));
  79.                 } else if ($version == '4') {
  80.                         $uuid = gen_uuid_random();
  81.                 } else if ($version == '5') {
  82.                         $uuid = gen_uuid_sha1_namebased(trim($_REQUEST['nb_ns']??''), trim($_REQUEST['nb_val']??''));
  83.                 } else if ($version == '6') {
  84.                         $uuid = gen_uuid_reordered();
  85.                 } else if ($version == '7') {
  86.                         $uuid = gen_uuid_unix_epoch();
  87.                 } else if ($version == '8') {
  88.                         if ($hash_uuid != null) {
  89.                                 $uuid = gen_uuid_v8_namebased($hash_uuid, trim($_REQUEST['nb_ns']??''), trim($_REQUEST['nb_val']??''));
  90.                         } else {
  91.                                 $uuid = gen_uuid_custom(trim($_REQUEST['block1']??'0'), trim($_REQUEST['block2']??'0'), trim($_REQUEST['block3']??'0'), trim($_REQUEST['block4']??'0'), trim($_REQUEST['block5']??'0'));
  92.                         }
  93.                 } else {
  94.                         throw new Exception("Unexpected version number");
  95.                 }
  96.         }
  97.         if (is_uuid_oid($uuid)) {
  98.                 $uuid = oid_to_uuid($uuid);
  99.         }
  100.  
  101.         if (!uuid_valid($uuid)) {
  102.                 echo 'This is not a valid UUID.';
  103.         } else {
  104.                 uuid_info($uuid);
  105.         }
  106. } catch (Exception $e) {
  107.     echo "Error: " . htmlentities($e->getMessage());
  108. }
  109.  
  110. ?></pre>
  111.  
  112. <br>
  113.  
  114. </body>
  115.  
  116. </html>
  117.