1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* UUID generator |
4
|
|
|
* |
5
|
|
|
* Using method nodkz at mail dot ru post on <http://us.php.net/uniqid>, |
6
|
|
|
* but format changed. |
7
|
|
|
* |
8
|
|
|
* <code> |
9
|
|
|
* UUID format: |
10
|
|
|
* [time_low]-[time_mid]-[custom_1]-[custom_2](part1/2) |
11
|
|
|
* -[custom_2](part2/2)[random_1][random_2] |
12
|
|
|
* time_low: 8 chars, seconds in microtime, hex format. |
13
|
|
|
* time_mid: 4 chars, micro-second in microtime, plus 10000, hex format. |
14
|
|
|
* custom_1: 4 chars, user defined, '0000' if empty, hex format suggested. |
15
|
|
|
* custom_2: 8 chars, user defined, hex of user ip if empty, |
16
|
|
|
* and random hex string if user ip cannot get, hex format too. |
17
|
|
|
* random_1: 4 chars, random string, hex format. |
18
|
|
|
* random_2: 4 chars, random string, hex format. |
19
|
|
|
* </code> |
20
|
|
|
* @package fwolflib |
21
|
|
|
* @subpackage func |
22
|
|
|
* @copyright Copyright 2008-2010, Fwolf |
23
|
|
|
* @author Fwolf <[email protected]> |
24
|
|
|
* @since 2008-05-08 |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
require_once(dirname(__FILE__) . '/../fwolflib.php'); |
29
|
|
|
require_once(FWOLFLIB . 'func/ecl.php'); |
30
|
|
|
require_once(FWOLFLIB . 'func/client.php'); |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get a uuid |
35
|
|
|
* |
36
|
|
|
* User can combine cus and cus2 to sort uuid. |
37
|
|
|
* |
38
|
|
|
* @deprecated Use Fwlib\Util\Uuid::gen() |
39
|
|
|
* @param string $s_cus Custom part in uuid, 4 chars long, |
40
|
|
|
* positioned in 3rd section, |
41
|
|
|
* default fill by '0'. |
42
|
|
|
* @param string $s_cus2 Custom part2 in uuid, 8 chars long, |
43
|
|
|
* Positioned in 4 section and start of 5 section, |
44
|
|
|
* If empty given, user client user ip(hex) to fill, |
45
|
|
|
* and random string if can't get ip. |
46
|
|
|
* If given and length <> 8, will fill to 8 with random chars after it. |
47
|
|
|
* @return string |
48
|
|
|
* @link http://us.php.net/uniqid |
49
|
|
|
*/ |
50
|
|
|
function Uuid($s_cus = '0000', $s_cus2 = '') { |
51
|
|
|
$ar = explode(" ", microtime()); |
52
|
|
|
|
53
|
|
|
// Prepare custom string 2 |
54
|
|
|
if (empty($s_cus2)) |
55
|
|
|
$s_cus2 = ClientIpToHex(); |
|
|
|
|
56
|
|
View Code Duplication |
if (8 != strlen($s_cus2)) |
|
|
|
|
57
|
|
|
$s_cus2 = substr($s_cus2 . |
58
|
|
|
sprintf('%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff)), 0, 8); |
59
|
|
|
//if (empty($s_cus2) || (8 != strlen($s_cus2))) |
60
|
|
|
// $s_cus2 = sprintf('%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
61
|
|
|
|
62
|
|
|
return sprintf('%08s-%04s-%04s-%04s-%04s%04x%04x', |
63
|
|
|
// Unixtime, 8 chars from right-side end |
64
|
|
|
// 2030-12-31 = 1924876800(dec) = 72bb4a00(hex) |
65
|
|
|
substr(str_repeat('0', 8) . base_convert($ar[1], 10, 16), -8), |
66
|
|
|
// Microtime, 4chars from left-side start |
67
|
|
|
// to exceed 65534(length 4) limit, * 100000 and div by 2(max 50000) |
68
|
|
|
substr(base_convert(round($ar[0] * 100000 / 2), 10, 16), 0, 4), |
69
|
|
|
// Custom part 1, default 4 chars |
70
|
|
|
$s_cus, |
71
|
|
|
// Custom part2-part1, length: 4 |
72
|
|
|
substr($s_cus2, 0, 4), |
73
|
|
|
// Custom part2-part2, length: 4, used in 5th section |
74
|
|
|
substr($s_cus2, -4), |
75
|
|
|
// Random string, length: 4 |
76
|
|
|
mt_rand(0, 0xffff), |
77
|
|
|
// Random string, length: 4 |
78
|
|
|
mt_rand(0, 0xffff) |
79
|
|
|
); |
80
|
|
|
/* |
81
|
|
|
return sprintf( '%04x-%08s-%08s-%04s-%04x%04x', |
82
|
|
|
$serverID, |
83
|
|
|
clientIPToHex(), |
84
|
|
|
substr("00000000".dechex($t[1]),-8), // get 8HEX of unixtime |
85
|
|
|
substr("0000".dechex(round($t[0]*65536)),-4), // get 4HEX of microtime |
86
|
|
|
mt_rand(0,0xffff), mt_rand(0,0xffff)); |
87
|
|
|
*/ |
88
|
|
|
} // end of func Uuid |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Parse uuid, see what it means |
93
|
|
|
* |
94
|
|
|
* @deprecated Use Fwlib\Util\Uuid::parse() |
95
|
|
|
* @param string $uuid |
96
|
|
|
* @return array |
97
|
|
|
* @link http://us.php.net/uniqid |
98
|
|
|
*/ |
99
|
|
|
function UuidParse($uuid) { |
100
|
|
|
$ar = array(); |
101
|
|
|
$u = explode('-', $uuid); |
102
|
|
|
if (is_array($u) && (5 == count($u))) { |
103
|
|
|
$ar = array( |
104
|
|
|
'time_low' => hexdec($u[0]), |
105
|
|
|
'time_mid' => hexdec($u[1]), |
106
|
|
|
'custom_1' => $u[2], |
107
|
|
|
'custom_2' => $u[3] . substr($u[4], 0, 4), |
108
|
|
|
'ip' => ClientIpFromHex($u[3] . substr($u[4], 0, 4)), |
|
|
|
|
109
|
|
|
'random_1' => substr($u[4], 4, 4), |
110
|
|
|
'random_2' => substr($u[4], 8) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
return $ar; |
114
|
|
|
/* |
115
|
|
|
$rez=Array(); |
116
|
|
|
$u=explode("-",$uuid);cd50 |
117
|
|
|
if(is_array($u)&&count($u)==5) { |
118
|
|
|
$rez=Array( |
119
|
|
|
'serverID'=>$u[0], |
120
|
|
|
'ip'=>clientIPFromHex($u[1]), |
121
|
|
|
'unixtime'=>hexdec($u[2]), |
122
|
|
|
'micro'=>(hexdec($u[3])/65536) |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
return $rez; |
126
|
|
|
*/ |
127
|
|
|
} // end of func UuidParse |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Test how many uuid can this program generate per second |
132
|
|
|
* |
133
|
|
|
* @param long $num Number of uuid generated in test, the more the result more currect. |
134
|
|
|
* @param string $file If assigned, result will be write to this file, 1 uuid per line. |
135
|
|
|
*/ |
136
|
|
|
function UuidSpeedTest($num = 100, $file = '') { |
137
|
|
|
if (!is_numeric($num)) |
138
|
|
|
return ''; |
139
|
|
|
else |
140
|
|
|
$num = round($num); |
141
|
|
|
// Start time |
142
|
|
|
$t_start = microtime(true); |
143
|
|
|
$i = 0; |
144
|
|
|
$s = ''; |
145
|
|
|
while ($num > $i) { |
146
|
|
|
$s .= Uuid() . "\n"; |
|
|
|
|
147
|
|
|
$i ++; |
148
|
|
|
} |
149
|
|
|
// End time |
150
|
|
|
$t_end = microtime(true); |
151
|
|
|
// Compute |
152
|
|
|
$t_used = round($t_end - $t_start, 4); |
153
|
|
|
$speed = round($num / $t_used); |
154
|
|
|
// Outputis_numeric |
155
|
|
|
ecl("$num UUID generated, cost $t_used second(s), average $speed/s."); |
|
|
|
|
156
|
|
|
// Write to file ? |
157
|
|
|
if (!empty($file)) |
158
|
|
|
file_put_contents($file, $s); |
159
|
|
|
} // end of function UuidSpeedTest |
160
|
|
|
|
161
|
|
|
?> |
|
|
|
|
162
|
|
|
|
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.