line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: Shorten.pm,v 1.4 2003/09/10 07:28:54 cvspub Exp $ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package CGI::Shorten; |
4
|
1
|
|
|
1
|
|
18803
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
39
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
8463
|
use CGI; |
|
1
|
|
|
|
|
44479
|
|
|
1
|
|
|
|
|
9
|
|
8
|
|
|
|
|
|
|
our $cgi = new CGI; |
9
|
|
|
|
|
|
|
our @dbs = qw(lndb); |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
550
|
use DB_File; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use Fcntl ':flock'; |
13
|
|
|
|
|
|
|
use Math::String; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
16
|
|
|
|
|
|
|
sub new { |
17
|
|
|
|
|
|
|
my $pkg = shift; |
18
|
|
|
|
|
|
|
my %arg = @_; |
19
|
|
|
|
|
|
|
my $db_prefix = $arg{db_prefix} or die "Please specify the prefix of the databases\n"; |
20
|
|
|
|
|
|
|
my $self = { |
21
|
|
|
|
|
|
|
_db_prefix => $db_prefix, |
22
|
|
|
|
|
|
|
_config_file => $db_prefix."_conf", |
23
|
|
|
|
|
|
|
_script_url => $arg{script_url} || 'http://127.0.0.1/shorten.pl', |
24
|
|
|
|
|
|
|
}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
if(!-e$self->{_config_file}){ |
27
|
|
|
|
|
|
|
open CONF, '>', $self->{_config_file}; |
28
|
|
|
|
|
|
|
close CONF; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
if(open CONF, $self->{_config_file}){ |
32
|
|
|
|
|
|
|
flock(CONF,LOCK_EX); |
33
|
|
|
|
|
|
|
my $line; |
34
|
|
|
|
|
|
|
if(chomp($line=)){ |
35
|
|
|
|
|
|
|
$self->{_id} = new Math::String $line; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
flock(CONF,LOCK_UN); |
38
|
|
|
|
|
|
|
close CONF; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
$self->{_id} ||= new Math::String 'a'; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
foreach my $db (@dbs){ |
43
|
|
|
|
|
|
|
tie |
44
|
|
|
|
|
|
|
%{$self->{'_'.$db}} => 'DB_File', |
45
|
|
|
|
|
|
|
$db_prefix."_$db", O_CREAT | O_RDWR, 0644, |
46
|
|
|
|
|
|
|
$DB_BTREE; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
bless $self => $pkg; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
53
|
|
|
|
|
|
|
use IO::Handle; |
54
|
|
|
|
|
|
|
sub DESTROY { |
55
|
|
|
|
|
|
|
my $self = shift; |
56
|
|
|
|
|
|
|
foreach my $db (@dbs){ |
57
|
|
|
|
|
|
|
untie %{$self->{'_'.$db}}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $retval; |
61
|
|
|
|
|
|
|
do{ |
62
|
|
|
|
|
|
|
if($retval = sysopen CONF, $self->{_config_file}, O_RDWR){ |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
flock(CONF,LOCK_EX); |
65
|
|
|
|
|
|
|
local $/; |
66
|
|
|
|
|
|
|
my $line; |
67
|
|
|
|
|
|
|
if(chomp($line=)){ |
68
|
|
|
|
|
|
|
my $id = new Math::String $line; |
69
|
|
|
|
|
|
|
$self->{_id} = $id if $id > $self->{_id}; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
seek(CONF, 0, 0); |
72
|
|
|
|
|
|
|
print CONF $self->{_id}->bstr(), "\n"; |
73
|
|
|
|
|
|
|
flock(CONF,LOCK_UN); |
74
|
|
|
|
|
|
|
close CONF; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
}while(!$retval); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
undef $self->{_id}; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
82
|
|
|
|
|
|
|
sub shorten($$) { |
83
|
|
|
|
|
|
|
my ($self, $url) = @_; |
84
|
|
|
|
|
|
|
my $shurl = $self->{_script_url}.'?'.$self->{_id}->bstr(); |
85
|
|
|
|
|
|
|
$self->{_lndb}->{$self->{_id}} = $url; |
86
|
|
|
|
|
|
|
$self->{_id}++; |
87
|
|
|
|
|
|
|
$shurl; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
91
|
|
|
|
|
|
|
sub lengthen($$) { |
92
|
|
|
|
|
|
|
my ($self, $url) = @_; |
93
|
|
|
|
|
|
|
if($url =~ s/^\Q$self->{_script_url}?\E//o ){ |
94
|
|
|
|
|
|
|
return $self->{_lndb}->{$'}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
99
|
|
|
|
|
|
|
sub redirect($$) { |
100
|
|
|
|
|
|
|
die "Where is your redirection url\n" unless $_[1]; |
101
|
|
|
|
|
|
|
my $lnurl = $_[0]->lengthen($_[1]); |
102
|
|
|
|
|
|
|
return $lnurl ? $cgi->redirect($lnurl) : $cgi->header(-status=> '404'), |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
108
|
|
|
|
|
|
|
__END__ |