1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
#!/usr/bin/perl
#########################################
# _session.txt
# coordinates all raw input/output
# plus formatting and headers for some reason
use DBI;
use Image::Size;
use constants;
our $base_path = $BUCKY_CONFIG->{BASE_PATH};
our $temp_path = $base_path."incoming";
our $data_path = $base_path."data";
our $live_path = "/bucky/data";
our $USER = -1;
our $input = getargs( { base => $temp_path, file => "temp_[(field)]" } );
our $DEBUG = $ENV{REQUEST_URI} =~ /debug/;
if ($DEBUG) { print "Content-type: text/html\n\n<tt>ENTERING DEBUG MODE . . .</tt><p>"; }
$input->{object_from_uri} = get_object_from_uri();
$input->{script_from_uri} = get_script_from_uri();
our $cookies = getCookies();
our $dsn = "DBI:mysql:$BUCKY_DB:localhost;mysql_read_default_file=$BUCKY_DB_CNF";
our ($dbh, $sth);
# extract script from URI
sub get_script_from_uri
{
my $request_uri = $ENV{REQUEST_URI};
# /profile
# /profile/
# /profile/jules
# /profile/jules/
# /profile?username=jules
# /profile/jules?username=rakubian
my ($script, $object) = $request_uri =~ /^$BUCKY\/(\w+)\/?(\w+)?\??/;
return $script if (length($script) > 0);
return undef;
}
# extract object from URI, if using / URI delimiters
sub get_object_from_uri
{
my $request_uri = $ENV{REQUEST_URI};
my ($method, $object) = $request_uri =~ /^$BUCKY\/(\w+)\/([a-zA-Z0-9]+)/;
# only return object if it exists and has a length > 0
print "method: $method<br>\n" if ($DEBUG);
print "view: $object<br>\n" if ($DEBUG);
return $object if (length($object) > 0);
return undef;
# return $object || undef;
# return "";
}
# check cookies, return userhash, lastlog
sub checkin
{
my $lastlog;
my $user = check_cookie();
if (ref($user) && $user != -1)
{ print "<br>HELLO $user->{username}<br>" if $DEBUG; }
elsif ($ENV{REQUEST_URI} =~ /$BUCKY\/invite/)
{ return ( -1 ); }
else
{ logout(0); }
$lastlog = lastlog($user);
touch_user($user->{id}) if ($user != -1);
return ($user, $lastlog);
}
sub check_cookie
{
if (exists($cookies->{name}))
{
if ($cookies->{name} && $cookies->{name} ne 'unknown')
{ $user = auth($cookies->{name}, $cookies->{pass}); }
elsif ($ENV{REQUEST_URI} =~ /$BUCKY\/invite/)
{ return ( -1 ); }
}
}
# check passwords, set cookie
sub auth
{
my ($inname, $inpass) = @_;
$inname = lc($inname);
# $inname = "marc";
my ($dbuh) = get_user($inname);
if ($dbuh != -1 && $dbuh->{ulevel} > 0 && $dbuh->{password} eq $inpass)
{
my $forever = check_key($dbuh->{boxes}, "nologout");
setCookie( { name => "name", value => $inname, path => "$BUCKY/", domain => $BUCKY_COOKIE_DOMAIN, nologout => $forever } );
setCookie( { name => "pass", value => $inpass, path => "$BUCKY/", domain => $BUCKY_COOKIE_DOMAIN, nologout => $forever } );
# setCookie("name", $inname, "$BUCKY/", $BUCKY_COOKIE_DOMAIN );
# setCookie("pass", $inpass, "$BUCKY/", $BUCKY_COOKIE_DOMAIN );
return ($dbuh);
}
else
# { return -1; }
{ logout(0); }
}
sub logout
{
my $error = shift;
my $lender = undef;
# my $s = $ENV{'HTTPS'} eq "on" ? "s" : "";
my $s = "";
setCookie( { name => "name", value => undef, path => "$BUCKY/", domain => $BUCKY_COOKIE_DOMAIN } );
setCookie( { name => "pass", value => undef, path => "$BUCKY/", domain => $BUCKY_COOKIE_DOMAIN } );
# setCookie("name", "", "$BUCKY/", $BUCKY_COOKIE_DOMAIN );
# setCookie("pass", "", "$BUCKY/", $BUCKY_COOKIE_DOMAIN );
print "Location: http$s://$BUCKY_HOST$BUCKY/login";
if ($ENV{REQUEST_URI} !~ /login/ && $ENV{REQUEST_URI} !~ /logout/ && $ENV{REQUEST_URI} ne "$BUCKY/index" && $ENV{REQUEST_URI} ne "/bucky/")
{
$lender .= "redir=$ENV{REQUEST_URI}";
}
if (defined($lender) || $error > 0)
{
print "?";
print "error=$error&" if ($error > 0);
print $lender;
}
print "\n\n";
$dbh->disconnect() if (defined($dbh));
exit (0);
}
sub nice_redirect
{
my $uri = '';
if ($input->{redir} && $input->{redir} ne "/bucky/")
{
redirect($input->{redir});
}
else
{
redirect("$BUCKY/index");
}
}
sub redirect
{
my $uri = shift;
# my $s = ($ENV{'HTTPS'} eq "on" || exists($input->{secure})) ? "s" : "";
my $s = "";
print "Location: http$s://$BUCKY_HOST$uri\n\n";
$dbh->disconnect() if (defined($dbh));
exit (0);
}
sub error
{
my $reason = shift;
# my $f = get_random_flagged_image();
if (defined($USER) && $USER != -1)
{
header("<center>error: $reason</center>");
print qq(<center>);
print qq(<div align="center" style="max-width: 390px;">);
print qq(<hr noshade color="$BUCKY_COLOR_HR">);
my $filename = lc($f->{filename});
print qq!<img src="/bucky/error.jpg" class="thumb"></a><br>\n!;
print "<p>sorry, there was an <b>error:</b><p><big>$reason</big><p>";
print qq!<a href="javascript:history.go(-1)"><big><b>go back</b></big></a> | <a href="$BUCKY/index">home</a>\n!;
print qq(</div>);
print qq(</center>);
}
else
{
header("error: $reason");
print "there was an <b>error:</b><p>$reason<p>";
}
footer();
$dbh->disconnect() if (defined($dbh));
exit(1);
}
sub corral
{
my ($things, $key) = @_;
my @values;
foreach my $k (sort keys %$things)
{
next unless ($k =~ /^$key/);
push @values, $things->{$k};
}
return sort @values;
}
# keys -- for storing arrays of ids
sub split_keys
{
my ($string) = @_;
my @out;
foreach my $v (split(" ", $string))
{
if (defined($v))
{ push @out, $v; }
}
return @out;
}
sub check_key
{
my ($string, $key) = @_;
if ($string =~ /\s$key\s/)
{ return 1; }
else
{ return 0; }
}
sub add_key
{
my ($string, $key) = @_;
$string .= " $key " unless (check_key($string, $key));
$string =~ s/( )+/ /g;
return $string;
}
sub toggle_key
{
my ($string, $key) = @_;
if (check_key($string, $key))
{ $string =~ s/ $key / /; }
else
{ $string .= " $key "; }
$string =~ s/( )+/ /g;
return $string;
}
sub delete_key
{
my ($string, $key) = @_;
$string =~ s/ $key / /;
$string =~ s/( )+/ /g;
return $string;
}
1;
|