File Coverage

blib/lib/Slash/OurNet/Standalone.pm
Criterion Covered Total %
statement 6 85 7.0
branch 0 40 0.0
condition 0 25 0.0
subroutine 2 15 13.3
pod 0 13 0.0
total 8 178 4.4


line stmt bran cond sub pod time code
1             package Slash::OurNet::Standalone;
2              
3             our $VERSION = '0.02';
4              
5             require CGI;
6 1     1   9450 use base 'Exporter';
  1         3  
  1         293  
7              
8             our @EXPORT = qw(
9             getCurrentVirtualUser getCurrentForm getCurrentStatic slashDisplay
10             getCurrentDB getUser getCurrentUser createEnvironment header
11             titlebar footer SetCookie timeCalc
12             );
13              
14             our %Sessions;
15              
16             sub header {
17 0     0 0   print "@_";
18             }
19              
20             sub footer {
21 0     0 0   print "
@_";
22             }
23              
24             sub titlebar {
25 0     0 0   shift;
26 0           print "

@_

";
27             }
28              
29             sub getCurrentVirtualUser {
30 0     0 0   return 'guest';
31             }
32              
33             sub getCurrentForm {
34 0     0 0   my $flavor = 'OurNetBBS';
35 0           my $show_cookie;
36             my ($cookie);
37              
38 0           require SDBM_File;
39              
40 1     1   7 use Fcntl;
  1         2  
  1         2759  
41 0 0         tie(%Sessions, 'SDBM_File', Slash::OurNet::PATH . '/ournet.db', O_RDWR|O_CREAT, 0666) or die $!;
42              
43 0           require Encode;
44 0           require CGI::Cookie;
45              
46 0           my $CGI = CGI->new;
47 0           my $vars = {map Encode::decode_utf8($_), %{$CGI->Vars}};
  0            
48 0           $CGI->delete_all;
49              
50 0 0 0       if (exists $vars->{op} and $vars->{op} eq 'userlogin') {
    0 0        
      0        
51 0           my ($uid, $pwd) = @{$vars}{qw/unickname upasswd/};
  0            
52 0           my $bbs = (values(%Slash::OurNet::ALLBBS))[0];
53 0 0         $bbs = $bbs->{bbs} if $bbs;
54 0   0       $bbs ||= OurNet::BBS->new(@Slash::OurNet::Connection);
55 0 0         if (exists $bbs->{users}{$uid}) {
56 0           my $user = $bbs->{users}{$uid};
57 0           my $crypted = $user->{passwd};
58 0 0         if (crypt($pwd, $crypted) eq $crypted) {
59 0           my $val = SetCookie();
60 0           $Sessions{$val} = $vars->{unickname};
61 0           $show_cookie = 1;
62             }
63             }
64             }
65             elsif ($CGI->path_translated() or $CGI::MOD_PERL or $^O ne 'MSWin32') {
66 0           my %cookies = CGI::Cookie->fetch;
67 0 0         $cookie = $cookies{$flavor} if exists $cookies{$flavor};
68             }
69             else {
70 0           $cookie = CGI::Cookie->new(-value => 'localhost');
71             }
72              
73             # print << '.';
74             #Content-Type: text/html; charset=UTF-8
75             #.
76              
77 0 0 0       if ($cookie and $Sessions{$cookie->value}) {
78 0 0 0       if (exists $vars->{op} and $vars->{op} eq 'userclose') {
79 0           delete $Sessions{$cookie->value};
80             }
81             else {
82 0           my $sescook = CGI::Cookie->new(
83             -name => $flavor,
84             -value => $cookie->value,
85             -expires => '+1h',
86             -domain => $cookie->domain
87             );
88              
89 0 0         print "Set-Cookie: $sescook\n" if $show_cookie;
90 0           $vars->{uid} = $Sessions{$cookie->value};
91             }
92             }
93              
94 0           untie(%Sessions);
95             # print "\n\n\n";
96              
97 0           return $vars;
98             }
99              
100 0     0 0   sub getCurrentStatic {
101             }
102              
103             sub getCurrentDB {
104 0     0 0   my $a;
105 0           return bless \$a, __PACKAGE__;
106             }
107              
108             sub getUser {
109 0     0 0   my ($self, $uid, $key) = @_;
110              
111 0 0         return $uid if $key eq 'nickname';
112 0 0         if ($key eq 'fakeemail') {
113 0           my $bbs = $Slash::OurNet::ALLBBS{$uid}{bbs};
114 0           my $user = $bbs->{users}{$uid};
115 0           return $user->{username};
116             }
117             }
118              
119             sub getCurrentUser {
120 0     0 0   my ($self, $key) = @_;
121 0 0         return unless $key;
122              
123 0 0         if ($key eq 'is_anon') {
    0          
124 0           return ($vars->{username} eq $Slash::OurNet::DefaultUser);
125             }
126             elsif ($key eq 'off_set') {
127 0           require Time::Local;
128 0           return ((timegm(localtime) - timegm(gmtime)) / 3600);
129             }
130             }
131              
132             my $template;
133              
134             sub slashDisplay {
135 0     0 0   my ($file, $vars) = @_;
136 0 0         my $path = exists $ENV{SCRIPT_FILENAME} ? $ENV{SCRIPT_FILENAME} : $0;
137 0 0         $path =~ s|[\\/][^\\/]+$|/templates| or $path = './templates';
138 0 0         $path .= "/$Slash::OurNet::Theme" if $Slash::OurNet::Theme;
139 0           $path = "$path/$file;ournet;default";
140              
141 0           $vars->{user} = $Slash::OurNet::Colors;
142 0           $vars->{user}{nickname} = $vars->{username};
143 0           $vars->{user}{fakeemail} = $vars->{usernick};
144 0           $vars->{user}{is_anon} = ($vars->{username} eq $Slash::OurNet::DefaultUser);
145              
146 0           local $/;
147 0 0         open my $fh, $path or die "cannot open template: $path ($!)";
148 0           binmode($fh, ':utf8');
149 0           my $text = <$fh>;
150 0           $text =~ s/.*\n__template__\n//s;
151 0           $text =~ s/__seclev__\n.*//s;
152              
153 0           require Template;
154 0   0       $template ||= Template->new;
155 0 0         my $selfh = select() eq 'main::STDOUT' ? undef : select();
156 0   0       return $template->process(\$text, $vars, $selfh) || die($template->error);
157             }
158              
159 0     0 0   sub createEnvironment {
160             }
161              
162             sub SetCookie {
163 0   0 0 0   my $flavor = shift || 'OurNetBBS';
164 0           my $sescook = CGI::Cookie->new(
165             -name => $flavor,
166             -value => crypt(time, substr(CGI::remote_host(), -2)),
167             -expires => '+1h'
168             );
169              
170 0           print "Set-Cookie: $sescook\n";
171 0           return $sescook->value;
172             }
173              
174             sub timeCalc {
175 0     0 0   return scalar localtime;
176             }
177              
178             1;