File: SPCGI.pm

package info (click to toggle)
sporum 1.8b1-3.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,108 kB
  • ctags: 676
  • sloc: perl: 14,300; makefile: 52
file content (312 lines) | stat: -rw-r--r-- 7,708 bytes parent folder | download
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package SPCGI;

use SmallPigVars qw($config);

use strict;

sub new{
    my ($class, $spdb) = @_;
    my $self = {};
    $self->{'spdb'} = $spdb;

    return bless $self, $class;
}

sub spparam{
    my ($self) = @_;
    my($query_string, $meth, $content_length);


    $meth=$ENV{'REQUEST_METHOD'} if defined($ENV{'REQUEST_METHOD'});

    # --- if multipart posting, ask CGI to handle it
    if($meth eq "POST" && defined($ENV{'CONTENT_TYPE'}) 
       && $ENV{'CONTENT_TYPE'} =~ m|^multipart/form-data|){

	require CGI;

	my $cgi = new CGI;

	return undef unless $cgi->param();
	
	my $state = {};
	my $isarr = 0;

	foreach ($cgi->param()){
	    my $key = $_;
	    my @tmp = $cgi->param($key);
	    my $curr_key;
	    foreach (@tmp){
		$isarr = 1;
		unless($curr_key eq $key){ $state->{$key} = $_; }
		else{ $state->{$key} .= "#$_"; }
		$curr_key = $key;
	    }
	}

	my $file_hdl = $cgi->param('file_hdl');
	$state->{ftype} = $cgi->uploadInfo($file_hdl)->{'Content-Type'} if $file_hdl;
	
	return $state;
    }

    $content_length = defined($ENV{'CONTENT_LENGTH'}) ? 
	$ENV{'CONTENT_LENGTH'} : 0;
    
    if($meth =~ /^GET$/) {
	$query_string = $ENV{'QUERY_STRING'} if defined $ENV{'QUERY_STRING'};
    }
    if($meth eq "POST"){
	$self->_read_from_client(\*STDIN, \$query_string, $content_length, 0)
	    if $content_length > 0;
    }
#    $query_string = q|poster=Admin&posteremail=webmeister%40realgsd.net&subject=test&body=test&op=Submit&sid=registry&fids=&cid=&uid=1&html=&markup=&prev_op=newcomment|;
    return $self->_parse_params($query_string);
}

sub cookie{
    my($self, @p) = @_;
    my($name, $value, $path, $domain, $secure, $expires) = @p;

    if(!defined($value)) {
        $self->{'.cookies'} = $self->_fetch_cookie_from_client() 
	    unless $self->{'.cookies'};

        return () unless $self->{'.cookies'};
        return keys %{$self->{'.cookies'}} unless $name;
        return () unless $self->{'.cookies'}->{$name};

        return $self->{'.cookies'}->{$name}->{'value'} 
	    if defined($name) && $name ne '';
    }

    return undef unless $name;
}

sub _set_anon{
    my ($STATE) = @_;
    $STATE->{'uid'} ||= 0;
    $STATE->{'nickname'} = $STATE->{'userstatus'} = "anon";
    $STATE->{'sort'} ||= $config->{'sort'};
    $STATE->{'postsper'} ||= 10;
    $STATE->{'view'} ||= $config->{'view'};
    $STATE->{'display'} ||= $config->{'display'};

    $STATE->{'theme'} ||= $config->{'theme'};
}

sub set_state_for_client{
    my ($self) = @_;
    my $STATE = {};
    $STATE = $self->{'STATE'} = $self->spparam(); 

    # --- verify cookie
    my $username = $self->_verify_cookie();

    my ($spdb) = map{ $self->{$_} } qw(spdb);
    my $DBH = $spdb->{'dbh'};

    $STATE->{'username'} = $username || "anon";
    $STATE->{'ip'} = $ENV{'REMOTE_ADDR'};

    if($STATE->{'username'} eq 'anon'){
	_set_anon($STATE);
	$self->{'STATE'} = $STATE;
	return;
    }
    
    my $u_q = $DBH->quote($STATE->{'username'});
    
    my $select = "uid, nickname, fakeemail, lastlogon, active, peek, lastresp, cookieexpire";
    my $res = 
	$spdb->db_select_hash_ref($select, "Users", "username=$u_q AND active=1");
    
    if($res){
	if($STATE->{'op'} ne "userinfo" && $STATE->{'op'} ne "confirm"){
	    $STATE->{'uid'} = $res->{'uid'};
	}
	foreach (keys(%$res)){ 
	    if(!$STATE->{$_} && $_ ne "uid"){
		$STATE->{$_} = $res->{$_} || $config->{$_}  
	    }
	    if($_ eq "uid"){
		($STATE->{'userstatus'}, $STATE->{'tuserstatus'})
		    = $self->check_user_status();
	    }
	}
    }
    else{
	_set_anon($STATE);
    }

    my $select = "sort, display, view, postsper, sig, theme, lang";
    $res = $spdb->db_select_hash_ref($select, "BoardPrefs", "uid=$STATE->{'uid'}");
    
    if($res){
	foreach (keys(%$res)){ 
	    if(!$STATE->{$_} && $_ ne "uid"){
		$STATE->{$_} = $res->{$_} || $config->{$_}  
	    }
	}
    }
    else{
	_set_anon($STATE);
    }
}

#############################################################################
# --- private function
# ---

sub _fetch_cookie_from_client{
    my $class = shift;
    my $raw_cookie = $ENV{HTTP_COOKIE} || $ENV{COOKIE};

    return () unless $raw_cookie;
    return $class->_parse_raw_cookie($raw_cookie);
}

sub _parse_raw_cookie{
    my ($self, $raw_cookie) = @_;
    my $results = {};

    my(@pairs) = split("; ",$raw_cookie);
    foreach (@pairs) {
        my($key, $value) = split("=");
        my(@values) = map $self->_unescape($_), split('&', $value);
        $key = $self->_unescape($key);
	
        # A bug in Netscape can cause several cookies with same name to
        # appear.  The FIRST one in HTTP_COOKIE is the most recent version.
	$results->{$key}->{'value'} ||= \@values;
    }
    return $results;
}

sub _verify_cookie{
  my $self = shift;

  require MD5;
  my $cookie = $self->cookie('login_cookie');

  return undef unless $cookie;

  my %cookie1;
  for(my $i=0; $i<@$cookie; $i=$i+2){
      $cookie1{$cookie->[$i]} = $cookie->[$i+1];
  }

  # --- set the pre username
  $self->{'STATE'}->{'preusername'} = $cookie1{'username'};

  my $secret = $config->{'cookiesecret'};
  my $newhash = MD5->hexhash(
	        $secret.MD5->hexhash(join('',
					  $secret,
					  @cookie1{qw(time username)})));

  unless($newhash eq $cookie1{'hash'}){
      return undef;
  }
  
  return $cookie1{'username'};
}

sub _read_from_client{
    my($self, $fh, $buff, $len, $offset) = @_;
    local $^W=0;
    return undef unless defined($fh);
    return read($fh, $$buff, $len, $offset);
}

sub _parse_params{
    my ($self, $tosplit) = @_;
    my @pairs = ();
    @pairs = split(/[&;]/,$tosplit);
    my($param, $value);
    my %ret;
    foreach (@pairs) {
        ($param,$value) = split('=',$_,2);
        $param = $self->_unescape($param);
        $value = $self->_unescape($value);
	if(defined($ret{$param})){
	    $ret{$param} .= "#$value";
	}
	else{
	    $ret{$param} = $value;
	}
    }
    return \%ret;
}

sub _unescape{
  my ($self, $todecode) = @_;
  return undef unless defined($todecode);
  $todecode =~ tr/+/ /;       # pluses become spaces
  $todecode =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
  return $todecode;
}

# return "anon"  - anonymous
#        "ruser  - registered user
#        "mod"   - moderator
#        "admin" - administration
#        "gmem"  - group member
sub check_user_status{
  my ($self, $tusername) = @_;
  my ($spdb, $STATE) = map{ $self->{$_} } qw(spdb STATE);
  my $DBH = $spdb->{'dbh'};

  my ($sid, $uid, $username) = 
    map{ $STATE->{$_} } qw(sid uid username);
  
  $username = $tusername if $tusername;
  return "anon" if($username eq "anon");

  my $sidq = $DBH->quote($sid);
  my $usernameq = $DBH->quote($username);

  my $where = "username=$usernameq";
  
  # --- if user is admin?
  my ($isadmin) = $spdb->db_select_cols("isadmin", "Users", 
					$where);
  return "admin" if $isadmin;

  my ($userstatus, %tuserstatus);
  $where = "uid=$uid ";
  #$where .= "AND sid=$sidq" if($sid);
  # --- if user is mod?
  my ($sids) = $spdb->db_select_many_new(1, "sid", "Moderators", $where);
  
  if($sids){
      $userstatus = "mod|||";
      while (my ($sid) = $sids->fetchrow_array){
	  $userstatus .= "$sid|||";
	  push @{$tuserstatus{mod}}, $sid;
      }
      
      $spdb->db_handler_done($sids);
  }
  
   $userstatus =~ s/(.+)\|\|\|$/$1/;
  # --- if user is gmem?
  ($sids) = $spdb->db_select_many_new(1, "sid", "GroupMems", $where);
  
  if($sids){
      $userstatus .= "###gmem|||";
      while (my ($sid) = $sids->fetchrow_array){
	  $userstatus .= "$sid|||";
	  push @{$tuserstatus{gmem}}, $sid;
      }
      
      $spdb->db_handler_done($sids);
  }
   
  $userstatus =~ s/(.+)\|\|\|$/$1/;
  $userstatus ||= "ruser";
  return ($userstatus, \%tuserstatus);
}

return 1;