Subversion Repositories uuid_mac_utils

Rev

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

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
26 daniel-mar 3
/*
4
 * UUID interpreter for PHP
82 daniel-mar 5
 * Copyright 2017 - 2024 Daniel Marschall, ViaThinkSoft
6
 * Version 2024-03-09
26 daniel-mar 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
 
2 daniel-mar 21
$uuid = isset($_GET['uuid']) ? trim($_GET['uuid']) : 'CREATE';
22
 
32 daniel-mar 23
$version = $_REQUEST['version'] ?? null;
82 daniel-mar 24
$hash_sqlserver_version = null;
25
$hash_algo = null;
57 daniel-mar 26
if (!is_null($version)) {
82 daniel-mar 27
        if (preg_match('@^(8)_sqlserver_v(.+)$@', $version, $m)) {
57 daniel-mar 28
                $version = $m[1];
82 daniel-mar 29
                $hash_sqlserver_version = $m[2];
30
        } else if (preg_match('@^(8)_namebased_(.+)$@', $version, $m)) {
31
                $version = $m[1];
32
                $hash_algo = $m[2];
57 daniel-mar 33
        }
34
}
32 daniel-mar 35
if (!is_numeric($version) || (strlen($version)!=1)) $version = 1; // default: Version 1 / time based
36
 
2 daniel-mar 37
if ($uuid == 'CREATE') {
32 daniel-mar 38
        $title = 'Generate a UUIDv'.$version;
2 daniel-mar 39
} else {
31 daniel-mar 40
        $title = 'Interprete a UUID';
2 daniel-mar 41
}
42
 
43
?><html>
44
 
45
<head>
46
        <meta charset="iso-8859-1">
47
        <link rel="stylesheet" type="text/css" href="style.css">
35 daniel-mar 48
        <title><?php echo htmlentities($title); ?></title>
49
        <meta name=viewport content="width=device-width, initial-scale=1">
2 daniel-mar 50
</head>
51
 
52
<body>
53
 
35 daniel-mar 54
<h1><?php echo htmlentities($title); ?></h1>
2 daniel-mar 55
 
56
<p><a href="index.php">Back</a></p>
57
 
32 daniel-mar 58
<?php
2 daniel-mar 59
 
32 daniel-mar 60
if ($uuid != 'CREATE') {
61
        echo '<form method="GET" action="interprete_uuid.php">';
36 daniel-mar 62
        echo '  UUID: <input style="font-family:Courier,Courier New,Serif;width:325px" type="text" name="uuid" value="'.htmlentities($uuid).'"> <input type="submit" value="Interprete">';
32 daniel-mar 63
        echo '</form>';
34 daniel-mar 64
} else if (($version!=3) && ($version!=5) && ($version!=8)) {
32 daniel-mar 65
        echo '<p><i>Reload the page to receive another UUID.</i></p>';
66
}
67
 
68
echo '<pre>';
69
 
5 daniel-mar 70
require_once __DIR__ . '/includes/uuid_utils.inc.php';
2 daniel-mar 71
 
31 daniel-mar 72
try {
73
        if ($uuid == 'CREATE') {
32 daniel-mar 74
                if ($version == '1') {
31 daniel-mar 75
                        $uuid = gen_uuid_timebased();
32 daniel-mar 76
                } else if ($version == '2') {
34 daniel-mar 77
                        $uuid = gen_uuid_dce(trim($_REQUEST['dce_domain']??''), trim($_REQUEST['dce_id']??''));
32 daniel-mar 78
                } else if ($version == '3') {
34 daniel-mar 79
                        $uuid = gen_uuid_md5_namebased(trim($_REQUEST['nb_ns']??''), trim($_REQUEST['nb_val']??''));
32 daniel-mar 80
                } else if ($version == '4') {
31 daniel-mar 81
                        $uuid = gen_uuid_random();
32 daniel-mar 82
                } else if ($version == '5') {
34 daniel-mar 83
                        $uuid = gen_uuid_sha1_namebased(trim($_REQUEST['nb_ns']??''), trim($_REQUEST['nb_val']??''));
32 daniel-mar 84
                } else if ($version == '6') {
31 daniel-mar 85
                        $uuid = gen_uuid_reordered();
32 daniel-mar 86
                } else if ($version == '7') {
31 daniel-mar 87
                        $uuid = gen_uuid_unix_epoch();
34 daniel-mar 88
                } else if ($version == '8') {
82 daniel-mar 89
                        if ($hash_sqlserver_version != null) {
90
                                $uuid = gen_uuid_v8_sqlserver_sortable(intval($hash_sqlserver_version));
91
                        } else if ($hash_algo != null) {
92
                                $uuid = gen_uuid_v8_namebased($hash_algo, trim($_REQUEST['nb_ns']??''), trim($_REQUEST['nb_val']??''));
57 daniel-mar 93
                        } else {
94
                                $uuid = gen_uuid_custom(trim($_REQUEST['block1']??'0'), trim($_REQUEST['block2']??'0'), trim($_REQUEST['block3']??'0'), trim($_REQUEST['block4']??'0'), trim($_REQUEST['block5']??'0'));
95
                        }
2 daniel-mar 96
                } else {
31 daniel-mar 97
                        throw new Exception("Unexpected version number");
2 daniel-mar 98
                }
99
        }
31 daniel-mar 100
        if (is_uuid_oid($uuid)) {
101
                $uuid = oid_to_uuid($uuid);
2 daniel-mar 102
        }
30 daniel-mar 103
 
31 daniel-mar 104
        if (!uuid_valid($uuid)) {
105
                echo 'This is not a valid UUID.';
106
        } else {
107
                uuid_info($uuid);
30 daniel-mar 108
        }
31 daniel-mar 109
} catch (Exception $e) {
110
    echo "Error: " . htmlentities($e->getMessage());
2 daniel-mar 111
}
112
 
113
?></pre>
114
 
25 daniel-mar 115
<br>
116
 
2 daniel-mar 117
</body>
118
 
119
</html>