]> git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Id.pm
added version checks, fixed a small bug in Template.pm
[selfforum.git] / selfforum-cgi / shared / Id.pm
1 package Id;
2
3 ################################################################################
4 # #
5 # File: shared/Id.pm #
6 # #
7 # Authors: André Malo <nd@o3media.de>, 2001-05-03 #
8 # #
9 # Description: compose an unique ID (in CGI context) #
10 # #
11 ################################################################################
12
13 use strict;
14 use vars qw(
15 @table
16 @EXPORT
17 $VERSION
18 );
19
20 ################################################################################
21 #
22 # Version check
23 #
24 $VERSION = do { my @r =(q$Revision$ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
25
26 ################################################################################
27 #
28 # Export
29 #
30 use base qw(Exporter);
31 @EXPORT = qw(
32 unique_id
33 may_id
34 );
35
36 ### sub unique_id () ###########################################################
37 #
38 # compose an unique ID
39 #
40 # Params: ~none~
41 #
42 # Return: Scalar: unique ID
43 #
44 sub unique_id () {
45 my $id;
46
47 my $ip=$ENV{REMOTE_ADDR};
48 my $time=time;
49 my $port=$ENV{REMOTE_PORT};
50 my $rand=int(rand(time));
51
52 # works only with IPv4! (will be changed later...)
53 #
54 $ip = hex(join ('',map {sprintf ('%02X',$_)} split (/\./,$ip)));
55
56 join '' => map {to_base64 ($_)} ($time, $port, $ip, $rand, $$);
57 }
58
59 ### sub to_base64 ($) ##########################################################
60 #
61 # only converts (max.) 32-bit numbers into a system with base 64
62 #
63 # Params: $x - number to convert
64 #
65 # Return: converted number ;-)
66 #
67 sub to_base64 ($) {
68 my $x = shift;
69 my $y = $table[$x % 64];
70
71 $y = $table[$x % 64].$y while ($x = int ($x/64));
72
73 $y;
74 }
75
76 BEGIN {
77 # 64 'digits' (for our base 64 system)
78 #
79 @table = ('a'..'z','-','0'..'9','A'..'Z','_');
80
81 # define sub may_id
82 #
83 *may_id = eval join quotemeta join ('' => @table) => (
84 q[sub {local $_=shift; defined and length and not y/],
85 q[//cd;}]
86 );
87 }
88
89 # keep 'require' happy
90 1;
91
92 #
93 #
94 ### end of Id ##################################################################

patrick-canterino.de