1 |
dashley |
35 |
<?php
|
2 |
|
|
//$Header: /hl/cvsroots/gpl01/gpl01/webprojs/fboprime/sw/phplib/sguid.inc,v 1.4 2006/04/10 22:48:02 dashley Exp $
|
3 |
|
|
//********************************************************************************
|
4 |
|
|
//Copyright (C) 2006 David T. Ashley
|
5 |
|
|
//********************************************************************************
|
6 |
|
|
//This program or source file is free software; you can redistribute it and/or
|
7 |
|
|
//modify it under the terms of the GNU General Public License as published by
|
8 |
|
|
//the Free Software Foundation; either version 2 of the License, or (at your
|
9 |
|
|
//option) any later version.
|
10 |
|
|
//
|
11 |
|
|
//This program or source file is distributed in the hope that it will
|
12 |
|
|
//be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
|
//GNU General Public License for more details.
|
15 |
|
|
//
|
16 |
|
|
//You may have received a copy of the GNU General Public License
|
17 |
|
|
//along with this program; if not, write to the Free Software
|
18 |
|
|
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 |
|
|
//********************************************************************************
|
20 |
|
|
//Dave Ashley, 04/06
|
21 |
|
|
//
|
22 |
|
|
//This source file provides the code to create server globally-unique identifiers.
|
23 |
|
|
//The rationale is that since no process can have the same PID at the same
|
24 |
|
|
//time as another process, using the PID concatenated with a guaranteed
|
25 |
|
|
//unique time should provide an identifier that can never recur in the lifetime
|
26 |
|
|
//of the server.
|
27 |
|
|
//
|
28 |
|
|
//The format of the SGUID is documented in the FBO-Prime manual.
|
29 |
|
|
//
|
30 |
|
|
require_once("strfunc.inc"); //Necesssary for zero padding.
|
31 |
|
|
require_once("timeraw.inc"); //Necessary to get raw time.
|
32 |
|
|
//
|
33 |
|
|
//
|
34 |
|
|
//--------------------------------------------------------------------------------
|
35 |
|
|
//Returns the full generated SGUID as described in the documentation.
|
36 |
|
|
//
|
37 |
|
|
//Unit-tested 20060408.
|
38 |
|
|
//
|
39 |
|
|
function SGUID_sguid()
|
40 |
|
|
{
|
41 |
|
|
return("SG" . SGUID_sguid_raw());
|
42 |
|
|
}
|
43 |
|
|
//
|
44 |
|
|
//
|
45 |
|
|
//--------------------------------------------------------------------------------
|
46 |
|
|
//Returns the raw form of an SGUID (without the "SG" prefix).
|
47 |
|
|
//
|
48 |
|
|
//Unit-tested 20060408.
|
49 |
|
|
//
|
50 |
|
|
function SGUID_sguid_raw()
|
51 |
|
|
{
|
52 |
|
|
//Get the reference timestamp.
|
53 |
|
|
$reftime = TIMERAW_time_precision_mixed_array_2();
|
54 |
|
|
|
55 |
|
|
//Get the PID. This may also chew up some time and hopefully let the
|
56 |
|
|
//server time change.
|
57 |
|
|
$pid = getmypid();
|
58 |
|
|
|
59 |
|
|
//Form the SGUID proper. Hopefully this will also chew up more time.
|
60 |
|
|
$reftime_whole = $reftime[0];
|
61 |
|
|
$reftime_frac = sprintf("%d", $reftime[1]);
|
62 |
|
|
$pid_string = sprintf("%d", $pid );
|
63 |
|
|
$rv = STRFUNC_pad_left_zero($reftime_whole, 11)
|
64 |
|
|
. STRFUNC_pad_left_zero($reftime_frac, 9)
|
65 |
|
|
. STRFUNC_pad_left_zero($pid_string, 10);
|
66 |
|
|
|
67 |
|
|
//Spin-lock until the time has changed. With any luck, the statements
|
68 |
|
|
//above have done that for us. It is hard to judge without some data
|
69 |
|
|
//whether microsleep would make things more efficient ... let's just leave
|
70 |
|
|
//it as a pure spin-lock.
|
71 |
|
|
do
|
72 |
|
|
{
|
73 |
|
|
$comptime = TIMERAW_time_precision_mixed_array_2();
|
74 |
|
|
}
|
75 |
|
|
while (($reftime[1] == $comptime[1]) && ($reftime[0] == $comptime[0]));
|
76 |
|
|
|
77 |
|
|
//Return the value to the caller.
|
78 |
|
|
return($rv);
|
79 |
|
|
}
|
80 |
|
|
//
|
81 |
|
|
//
|
82 |
|
|
//--------------------------------------------------------------------------------
|
83 |
|
|
//Returns the length of a SGUID (in characters).
|
84 |
|
|
//
|
85 |
|
|
//Unit-tested 20060408.
|
86 |
|
|
//
|
87 |
|
|
function SGUID_sguid_len()
|
88 |
|
|
{
|
89 |
|
|
return(32);
|
90 |
|
|
}
|
91 |
|
|
//
|
92 |
|
|
//
|
93 |
|
|
//--------------------------------------------------------------------------------
|
94 |
|
|
//Returns 1 if the SGUID is syntactically valid or 0 otherwise.
|
95 |
|
|
//
|
96 |
|
|
//Unit-tested 20060408.
|
97 |
|
|
//
|
98 |
|
|
function SGUID_is_syntactically_valid($sguid_to_check)
|
99 |
|
|
{
|
100 |
|
|
//Must be a string data type.
|
101 |
|
|
if (! is_string($sguid_to_check))
|
102 |
|
|
return(0);
|
103 |
|
|
|
104 |
|
|
//Must be of appropriate length.
|
105 |
|
|
if (strlen($sguid_to_check) != 32)
|
106 |
|
|
return(0);
|
107 |
|
|
|
108 |
|
|
//First two characters must be "SG".
|
109 |
|
|
if (substr($sguid_to_check, 0, 2) !== "SG")
|
110 |
|
|
return(0);
|
111 |
|
|
|
112 |
|
|
//The remainder of the string must be digits only.
|
113 |
|
|
if (! STRFUNC_is_pure_digits(SubStr($sguid_to_check, 2, 30)))
|
114 |
|
|
return(0);
|
115 |
|
|
|
116 |
|
|
//If we're here, nothing obvious wrong with it.
|
117 |
|
|
return(1);
|
118 |
|
|
}
|
119 |
|
|
//--------------------------------------------------------------------------------
|
120 |
|
|
?>
|