| 1 | #!perl -w | ||
| 2 | # | ||
| 3 | # CGI::Bus - CGI Application Object Model | ||
| 4 | # | ||
| 5 | # admiral | ||
| 6 | # | ||
| 7 | |||
| 8 | |||
| 9 | package CGI::Bus; | ||
| 10 | require 5.000; | ||
| 11 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use CGI::Carp qw(fatalsToBrowser warningsToBrowser); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | |||
| 15 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD); | ||
| 16 | $VERSION = '0.62'; | ||
| 17 | |||
| 18 | use vars qw($SELF); | ||
| 19 | |||
| 20 | $SELF =undef; | ||
| 21 | |||
| 22 | if ($ENV{MOD_PERL}) { # $ENV{GATEWAY_INTERFACE} && $ENV{GATEWAY_INTERFACE} =~/^CGI-Perl\// | ||
| 23 | eval('use Apache qw(exit);'); | ||
| 24 | # *exit =\&Apache::exit; | ||
| 25 | } | ||
| 26 | |||
| 27 | 1; | ||
| 28 | |||
| 29 | |||
| 30 | ####################### | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | my $c=shift; | ||
| 34 | my $s ={}; | ||
| 35 | bless $s,$c; | ||
| 36 | $s =$s->initialize(@_); | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | sub fcgicount { | ||
| 41 | my $s =shift; | ||
| 42 | if (!ref($s)) {$s =CGI::Bus->new(@_)} # while (fcgicount) {} | ||
| 43 | elsif (scalar(@_)) {$s =CGI::Bus->new(@_)} # while (fcgicount) {} | ||
| 44 | else {} # while(1) {new ;,,, last if !fcgicount} | ||
| 45 | return(undef) if !$s->{-cgi}; | ||
| 46 | $s->{-fcgicount} =($s->{-fcgicount} ||0) +1; | ||
| 47 | return(undef) if $s->{-fcgicount} >($s->{-fcgimax}||0); | ||
| 48 | $s; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | sub initialize { | ||
| 53 | my $s =shift; | ||
| 54 | local $SELF =$s; | ||
| 55 | if (ref($_[0]) && eval{$_[0]->isa('CGI::Bus')}) { # reuse | ||
| 56 | my $r =shift; # reuse object | ||
| 57 | $r->reset($s->{-reset}); | ||
| 58 | $s =$r; # in doubt | ||
| 59 | $s->{-cache} ={}; # -> reset? | ||
| 60 | foreach my $k (qw(-cgi -qpath -qurl)) { | ||
| 61 | $s->{$k} =undef; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | shift if !defined($_[0]); | ||
| 66 | %$s =( | ||
| 67 | -classes =>{} # Classes to autocreate Objects | ||
| 68 | #,-import =>{} # add Classes or Methods & Packages | ||
| 69 | ,-reset =>{} # Slotes to destroy on reuse | ||
| 70 | ,-endh =>{} # End handlers, used in 'reset' | ||
| 71 | #,-reimport =>{} # add Classes {} or Slotes [] to reset | ||
| 72 | ,-debug =>0 # Debug Mode | ||
| 73 | ,-problem =>undef # Current problem set by problem() | ||
| 74 | ,-cache =>{ # Data cache | ||
| 75 | #,-lngbase =>undef # Language messages base | ||
| 76 | #,-pushmsg =>undef # Messages to accumulate and display | ||
| 77 | #,-qrun =>undef # Query to run | ||
| 78 | #,-user =>undef # Current user name | ||
| 79 | #,-usdomain =>undef # Server's User Domain | ||
| 80 | #,-unames =>undef # User names list | ||
| 81 | #,-ugroups =>undef # User groups list | ||
| 82 | #,-ugnames =>undef # User and groups names list | ||
| 83 | #,-httpheader=>undef # HTTP header output from print->httpheader() | ||
| 84 | #,-htmlstart =>undef # HTML start output from print->htmlstart() | ||
| 85 | #,-htpgstart =>undef # HTML page begin output from print->htpgstart() | ||
| 86 | } | ||
| 87 | ,-lngname =>undef # Name and charset of the language to use | ||
| 88 | ,-pushlog =>undef # Log file name | ||
| 89 | |||
| 90 | ,-cgi =>undef # CGI predefined object | ||
| 91 | #,-fcgimax =>undef # CGI::Fast requests max | ||
| 92 | #,-fcgicount =>undef # CGI::Fast requests counter | ||
| 93 | ,-dbi =>undef # DBI predefined object | ||
| 94 | |||
| 95 | #,-qpath =>undef # Query (script) Path | ||
| 96 | #,-qurl =>undef # Query (script) URL | ||
| 97 | #,-spath =>undef # Site Path | ||
| 98 | #,-surl =>undef # Site URL | ||
| 99 | #,-bpath =>undef # Binary Path | ||
| 100 | #,-burl =>undef # Binary URL | ||
| 101 | #,-dpath =>undef # Data Path | ||
| 102 | #,-tpath =>undef # Temporary Files Path | ||
| 103 | #,-ppath =>undef # Publish Path | ||
| 104 | #,-purl =>undef # Publish URL | ||
| 105 | #,-fpath =>undef # Files Store Path | ||
| 106 | #,-furf =>undef # Files Store file URL | ||
| 107 | #,-furl =>undef # Files Store URL | ||
| 108 | #,-hpath =>undef # Homes Store Path | ||
| 109 | #,-hurf =>undef # Homes Store file URL | ||
| 110 | #,-hurl =>undef # Homes Store URL | ||
| 111 | #,-urfcnd =>undef # URFs condition sub{} | ||
| 112 | #,-iurl =>undef # Apaceh Images URL '/images' | ||
| 113 | |||
| 114 | #,-user =>undef # User name get optional sub | ||
| 115 | #,-usdomain =>undef # Server's User Domain optional sub | ||
| 116 | #,-ugroups =>undef # User groups list optional sub | ||
| 117 | #,-usercnv =>undef # User/Group names convertor optional sub | ||
| 118 | #,-ugrpcnv =>undef # User/Group names convertor optional sub | ||
| 119 | #,-userauth =>undef # User authentication optional sub | ||
| 120 | #,-uadmins =>undef # Administrators list | ||
| 121 | #,-w32IISdpsn =>($ENV{SERVER_SOFTWARE}||'') =~/IIS/ ? 2 : 0 # MsIIS deimpersonation | ||
| 122 | |||
| 123 | #,-httpheader =>undef # HTTP header hash ref, for httpheader() | ||
| 124 | #,-htmlstart =>undef # HTML start hash ref, for htmlstart() | ||
| 125 | #,-htpnstart =>undef # Navigator pane HTML start | ||
| 126 | #,-htpgstart =>undef # HTML page HTML start | ||
| 127 | #,-htpfstart =>undef # HTML form HTML start | ||
| 128 | #,-htpgtop =>undef # HTML page begin, for htpgstart() | ||
| 129 | #,-htpgend =>undef # HTML page end, for htpgend() | ||
| 130 | ); | ||
| 131 | } | ||
| 132 | $s->set(@_); | ||
| 133 | if ($ENV{MOD_PERL}) { | ||
| 134 | Apache->push_handlers("PerlCleanupHandler" | ||
| 135 | ,sub{eval{$s->reset}; eval('Apache::DECLINED;')}); # or '$s->reset' at the bottom of scripts | ||
| 136 | } | ||
| 137 | if (!$s->{-cgi}) { | ||
| 138 | eval('use CGI::Fast') if $s->{-fcgimax}; | ||
| 139 | eval('use CGI qw(-no_xhtml);'); | ||
| 140 | # $CGI::POST_MAX =-1; # default in CGI.pm | ||
| 141 | # $MultipartBuffer::INITIAL_FILLUNIT =1024*4; # default in CGI.pm | ||
| 142 | local $ENV{CONTENT_TYPE} ='multipart/form-data' # !!! fix CGI.pm: $boundary = "--$boundary" unless CGI::user_agent('MSIE\s+3\.0[12];\s*Mac') | ||
| 143 | if ($ENV{CONTENT_TYPE}||'') =~m|^multipart/form-data| | ||
| 144 | && !$ENV{MOD_PERL}; # !!! beter to read boundary from input, but CGI.pm BUG: This won't work correctly under mod_perl | ||
| 145 | # $s->pushmsg($ENV{CONTENT_TYPE}); | ||
| 146 | no warnings; | ||
| 147 | $s->{-cgi} =(!$s->{-fcgimax} ? eval('CGI->new') : eval('CGI::Fast->new')) | ||
| 148 | ||CGI::Carp::croak("'CGI->new' failure: $@\n"); | ||
| 149 | $CGI::Q =$s->{-cgi}; | ||
| 150 | $CGI::XHTML =0; | ||
| 151 | if ((($ENV{SERVER_SOFTWARE}||'') =~/IIS/) | ||
| 152 | || ($ENV{MOD_PERL} && !$ENV{PERL_SEND_HEADER})) { | ||
| 153 | $CGI::NPH =1; | ||
| 154 | } | ||
| 155 | #CGI quote: | ||
| 156 | #die "Malformed multipart POST: " | ||
| 157 | #.'boundary: ' .$self->{BOUNDARY} ."***\n" | ||
| 158 | #.'buffer: ' .$self->{BUFFER} ."***\n" | ||
| 159 | #." start=$start; selflen=" .$self->{LENGTH} .'; ' | ||
| 160 | #.join(',', map {($_=>$ENV{$_}||'')} qw (REQUEST_METHOD REQUEST_URI CONTENT_TYPE CONTENT_LENGTH)) | ||
| 161 | #unless ($start >= 0) || ($self->{LENGTH} > 0); | ||
| 162 | } | ||
| 163 | $s | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | sub class { | ||
| 168 | substr($_[0], 0, index($_[0],'=')) | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | sub set { | ||
| 173 | return(keys(%{$_[0]})) if scalar(@_) ==1; | ||
| 174 | return($_[0]->{$_[1]}) if scalar(@_) ==2; | ||
| 175 | my ($s, %opt) =@_; | ||
| 176 | foreach my $k (keys(%opt)) { | ||
| 177 | $s->{$k} =$opt{$k}; | ||
| 178 | } | ||
| 179 | my $h; | ||
| 180 | if ($h =$opt{-import}) { # Import Classes or Methods and Packages | ||
| 181 | delete $s->{-import}; | ||
| 182 | foreach my $k (keys %$h) { | ||
| 183 | my $l = $h->{$k}; | ||
| 184 | if (ref($l) eq 'HASH') { # 'use...'=>{-method=>call,...},... | ||
| 185 | my $p =$k =~/^([^\;\s\(]+)/ ? $1 : $k; | ||
| 186 | foreach my $c (keys %$l) { | ||
| 187 | my $m =$l->{$c}; | ||
| 188 | $s->{$m} = | ||
| 189 | sub{$s->{$m} =eval("use $k; \\\&$p::$c"); | ||
| 190 | eval("use $k; &$p::$c(\@_)")} | ||
| 191 | } | ||
| 192 | } | ||
| 193 | elsif (ref($l) eq 'ARRAY') { # 'use...'=>[method,...],... | ||
| 194 | my $p =$k =~/^([^\;\s\(]+)/ ? $1 : $k; | ||
| 195 | foreach my $m (@$l) { | ||
| 196 | $s->{"-$m"} = | ||
| 197 | sub{$s->{"-$m"} =eval("use $k; \\\&$p::$m"); | ||
| 198 | eval("use $k; &$p::$m(\@_)")} | ||
| 199 | } | ||
| 200 | } | ||
| 201 | else { # -key=>class,.... | ||
| 202 | $s->{-classes}->{$k} =$h->{$k} | ||
| 203 | } | ||
| 204 | } | ||
| 205 | } | ||
| 206 | if ($h =$opt{-reimport}) { # Reset or Load Classes | ||
| 207 | delete $s->{-reimport}; | ||
| 208 | if (ref($h) eq 'HASH') { # {-key=>class,...} | ||
| 209 | foreach my $k (keys %$h) { | ||
| 210 | $s->{-classes}->{$k} =$h->{$k}; | ||
| 211 | $s->{-reset}->{$k} =1 | ||
| 212 | } | ||
| 213 | } | ||
| 214 | elsif (ref($h) eq 'ARRAY') { # [-key,...] | ||
| 215 | foreach my $k (@$h) {$s->{-reset}->{$k} =1} | ||
| 216 | } | ||
| 217 | else { # -key | ||
| 218 | $s->{-reset}->{$h} =1; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | if ($opt{-debug}) { | ||
| 222 | $SIG{__WARN__} =sub{return if $^S; | ||
| 223 | eval{$s->pushmsg('WARN: ' .($_[0] =~/(.+)[\n\r]+$/ ? $1 : $_[0]))}}; | ||
| 224 | } | ||
| 225 | $TempFile::TMPDIRECTORY =$opt{-tpath} # use CGI | ||
| 226 | if $opt{-tpath} | ||
| 227 | && ((-d $opt{-tpath}) ||$s->fut->mkdir($opt{-tpath})); | ||
| 228 | $s | ||
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | sub reset { | ||
| 233 | my $s =shift; | ||
| 234 | local $SELF =$s; | ||
| 235 | my $v =!scalar(@_) | ||
| 236 | ? $s->{-reset} | ||
| 237 | :ref($_[0]) eq 'ARRAY' | ||
| 238 | ? {map {$_=>1} @{$_[0]}} | ||
| 239 | :$_[0]; | ||
| 240 | foreach my $k (sort keys %{$s->{-endh}}) { | ||
| 241 | eval{&{$s->{-endh}->{$k}}($s)} | ||
| 242 | } | ||
| 243 | $s->{-endh} ={}; | ||
| 244 | foreach my $k (keys %$v) { | ||
| 245 | my $o =$s->{$k}; | ||
| 246 | my $t =ref($o); | ||
| 247 | next if !$t || $t eq 'HASH' || $t eq 'ARRAY'; | ||
| 248 | delete $s->{$k}; | ||
| 249 | eval {$o->DESTROY()}; | ||
| 250 | eval {delete $o->{'CGI::Bus'} if ref($o) && $o->isa('HASH')}; | ||
| 251 | } | ||
| 252 | $SELF =undef; | ||
| 253 | if (!scalar(@_) && $ENV{MOD_PERL}) { | ||
| 254 | delete $ENV{REMOTE_USER}; | ||
| 255 | } | ||
| 256 | $s | ||
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | sub DESTROY { | ||
| 261 | my $s =shift; | ||
| 262 | $s->reset($s); | ||
| 263 | $s | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | sub evalsub { | ||
| 268 | my ($s, $c) =(shift, shift); | ||
| 269 | local $SELF =$s; | ||
| 270 | ref($c) ? &$c(@_) : eval $c | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | sub AUTOLOAD {# Objects & Methods Loader | ||
| 275 | my $s =shift; confess("!object($s) in AUTOLOAD") if !ref($s); | ||
| 276 | my $m =substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2); | ||
| 277 | my $k ='-' .$m; | ||
| 278 | if (ref($s->{$k}) eq 'CODE') {$s->evalsub($s->{$k},@_)} | ||
| 279 | elsif (!scalar(@_) && ref($s->{$k})) {$s->{$k}} | ||
| 280 | elsif ($s->{-classes}->{$k}) { | ||
| 281 | local $SELF =$s; | ||
| 282 | my $c =$s->{-classes}->{$k}; | ||
| 283 | my $o =ref($c) ? &$c(@_) : eval("use $c; $c->new(\@_)"); | ||
| 284 | $s->die($@) if $@; | ||
| 285 | eval {$o->{'CGI::Bus'}=$s if $o->isa('HASH')}; | ||
| 286 | $s->{$k} =$o; # cycle ref! | ||
| 287 | } | ||
| 288 | #elsif (grep {$m eq $_} qw(select tr link delete accept sub vars)) | ||
| 289 | # {$m =ucfirst($m); $s->{-cgi}->$m(@_)} | ||
| 290 | #else {$s->{-cgi}->$m(@_)} | ||
| 291 | #else {eval {$s->{-cgi}->$m(@_)}; $s->_selfload(@_) if $@} | ||
| 292 | else { | ||
| 293 | my @r; | ||
| 294 | wantarray ? eval{@r =$s->{-cgi}->$m(@_)} : eval{$r[0] =$s->{-cgi}->$m(@_)}; | ||
| 295 | if ($@) { | ||
| 296 | if (grep {$m eq $_} qw(select tr link delete accept sub vars)) { | ||
| 297 | $m =ucfirst($m); | ||
| 298 | wantarray ? eval{@r =$s->{-cgi}->$m(@_)} : eval{$r[0] =$s->{-cgi}->$m(@_)}; | ||
| 299 | } | ||
| 300 | $r[0] =$s->_selfload(@_) if $@; | ||
| 301 | } | ||
| 302 | wantarray ? @r : $r[0] | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | |||
| 307 | sub launch { # Objects Factory | ||
| 308 | my ($s,$m) =(shift, shift); | ||
| 309 | return CGI::BusLauncher->new($s) if !defined($m); | ||
| 310 | my $k ='-' .$m; | ||
| 311 | local $SELF =$s; | ||
| 312 | local $s->{$k}; | ||
| 313 | my $o; | ||
| 314 | if ($s->{-classes}->{$k}) { | ||
| 315 | my $c =$s->{-classes}->{$k}; | ||
| 316 | $o =ref($c) ? &$c(@_) : eval("use $c; $c->new(\@_)"); | ||
| 317 | } | ||
| 318 | else { | ||
| 319 | $o =eval "use CGI::Bus::$m; CGI::Bus::$m->new (\@_)"; | ||
| 320 | } | ||
| 321 | $s->die($@) if $@; | ||
| 322 | $s->die("Object not created '$m'") if !defined($o); | ||
| 323 | eval {$o->{'CGI::Bus'}=$s if $o->isa('HASH')}; | ||
| 324 | $o | ||
| 325 | } | ||
| 326 | |||
| 327 | |||
| 328 | sub _selfload{# Self SubObject Loader | ||
| 329 | my $s =shift; | ||
| 330 | local $SELF =$s; | ||
| 331 | my $e =$@; chomp($e); | ||
| 332 | my $o; | ||
| 333 | $o =eval "use $AUTOLOAD; $AUTOLOAD->new(\@_)"; | ||
| 334 | if (defined($o)) { | ||
| 335 | $s->{'-' .substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2)} =$o; | ||
| 336 | eval {$o->{'CGI::Bus'}=$s if $o->isa('HASH')}; | ||
| 337 | $o | ||
| 338 | } | ||
| 339 | else { | ||
| 340 | $s->die("$e. $@") | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | |||
| 345 | sub microtest{# Microtest of the Object | ||
| 346 | my $s =shift; | ||
| 347 | $s->{-debug} ? $s->print->hr : $s->print->htpgstart; | ||
| 348 | #local $s->{-debug} =0; | ||
| 349 | if (($s->{-debug}||0) >4) { | ||
| 350 | $s->print->h2('Methods'); | ||
| 351 | foreach my $k (qw(class request qpath qurl qrun spath surl bpath burl dpath ppath purl furl user usdomain useron usersn usercn userfn userds unames ugroups ugnames)) { | ||
| 352 | $s->print->text("$k = " ._stringify($s->$k()))->br; | ||
| 353 | } | ||
| 354 | } | ||
| 355 | $s->print->h2('Slotes'); | ||
| 356 | foreach my $k (sort keys %$s) { | ||
| 357 | $s->print->text("$k = " ._stringify($s->{$k}))->br; | ||
| 358 | } | ||
| 359 | $s->print->h2('Environment Variables'); | ||
| 360 | foreach my $k (sort keys %ENV) { | ||
| 361 | $s->print->text($s->htmlescape("$k = '" .$ENV{$k} ."'"))->br; | ||
| 362 | } | ||
| 363 | $s->print->text($s->htmlescape( "login = '" .(eval{$^O eq 'MSWin32' ? Win32::LoginName() : getlogin()} ||'') ."'"))->br; | ||
| 364 | $s->print->text($s->htmlescape("\$0 = '$0'"))->br; | ||
| 365 | $s->print->text($s->htmlescape("\$^V = '$^V'"))->br; | ||
| 366 | $s->print->text($s->htmlescape("\$^X = '$^X'"))->br if $^X; | ||
| 367 | local $s->{-debug} =0; | ||
| 368 | $s->print->htpgend(); | ||
| 369 | } | ||
| 370 | |||
| 371 | |||
| 372 | sub microenv {# Microenv text of the Object | ||
| 373 | my $s =shift; | ||
| 374 | join(', ',('LOGIN=' .(eval{$^O eq 'MSWin32' ? Win32::LoginName() : getlogin()} ||'')) | ||
| 375 | ,map {$_ .'=' .($ENV{$_}||'')} qw(REMOTE_USER REMOTE_ADDR REMOTE_PORT HTTP_USER_AGENT REQUEST_METHOD REQUEST_URI CONTENT_TYPE CONTENT_LENGTH HTTP_COOKIE GATEWAY_INTERFACE)) | ||
| 376 | } | ||
| 377 | |||
| 378 | |||
| 379 | sub _stringify { | ||
| 380 | my $v =$_[0]; | ||
| 381 | my $p =''; | ||
| 382 | if (!defined($v)) {$p ='null'} | ||
| 383 | elsif (UNIVERSAL::isa($v,'ARRAY')) { | ||
| 384 | $p =$v .'['; | ||
| 385 | foreach my $e (@$v) {$p .=_stringify($e) .','} | ||
| 386 | chop($p) if scalar(@$v); | ||
| 387 | $p .=']'; | ||
| 388 | } | ||
| 389 | elsif (UNIVERSAL::isa($v,'HASH') && !UNIVERSAL::isa($v,'CGI::Bus')) { | ||
| 390 | $p =$v .'{'; | ||
| 391 | foreach my $e (sort keys %$v) {$p .=$e .'=>' ._stringify($v->{$e}) .','} | ||
| 392 | chop($p) if scalar(%$v); | ||
| 393 | $p .='}'; | ||
| 394 | } | ||
| 395 | else { | ||
| 396 | # if (ref($CGI::Bus::USED{$v})) { $p ="''" ._stringify($CGI::Bus::USED{$v})} | ||
| 397 | # else {$p ="'" .$v ."'"} | ||
| 398 | $p ="'" .$v ."'" | ||
| 399 | } | ||
| 400 | $p | ||
| 401 | } | ||
| 402 | |||
| 403 | |||
| 404 | ####################### | ||
| 405 | |||
| 406 | sub lngname { # language name | ||
| 407 | if (!$_[0]->{-lngname} || $_[1]) { | ||
| 408 | if (defined($_[1])) { | ||
| 409 | $_[0]->{-lngname} =$_[1] | ||
| 410 | } | ||
| 411 | else { | ||
| 412 | $_[0]->{-lngname} =$_[0]->{-cgi}->http('Accept_language')||''; | ||
| 413 | # .($_[0]->{-cgi}->http('Accept_charset') ||'') | ||
| 414 | $_[0]->{-lngname} =$_[0]->{-lngname} =~/^([^ ;,]+)/ ? $1 : $_[0]->{-lngname}; | ||
| 415 | } | ||
| 416 | } | ||
| 417 | $_[0]->{-lngname} | ||
| 418 | } | ||
| 419 | |||
| 420 | |||
| 421 | sub lngload { # language load | ||
| 422 | my ($s, $c, $l) =@_; | ||
| 423 | $c =$s->class if !$c; | ||
| 424 | $l =$s->lngname if !$l; | ||
| 425 | my $r; | ||
| 426 | foreach my $m ($c .'_' .$l, $c) { | ||
| 427 | $m =~s/::/_/g; | ||
| 428 | $m =~s/[ -]/_/g; | ||
| 429 | eval("use CGI::Bus::lngbase::${m}; \$r ={CGI::Bus::lngbase::${m}::lngbase}"); | ||
| 430 | last if $r; | ||
| 431 | } | ||
| 432 | return $r | ||
| 433 | } | ||
| 434 | |||
| 435 | |||
| 436 | sub lng { # language string | ||
| 437 | $_[0]->{-cache}->{-lngbase} =$_[0]->lngload($_[0]->class) if !$_[0]->{-cache}->{-lngbase}; | ||
| 438 | my $r =$_[0]->{-cache}->{-lngbase}; | ||
| 439 | $r = !defined($_[2]) ? $r->{$_[1]} | ||
| 440 | :!defined($r->{$_[2]}) ||!defined($r->{$_[2]}->[$_[1]]) ? $_[2] | ||
| 441 | :$r->{$_[2]}->[$_[1]]; | ||
| 442 | foreach my $e (@_[3..$#_]) { | ||
| 443 | $r =~s/\$_/$e/e; | ||
| 444 | } | ||
| 445 | $r | ||
| 446 | } | ||
| 447 | |||
| 448 | |||
| 449 | sub pushmsg { # messages to accumulate and display | ||
| 450 | my $s =shift; | ||
| 451 | $s->{-cache}->{-pushmsg} =[] if !$s->{-cache}->{-pushmsg}; | ||
| 452 | push @{$s->{-cache}->{-pushmsg}}, @_ if scalar(@_); | ||
| 453 | $s->{-cache}->{-pushmsg} | ||
| 454 | } | ||
| 455 | |||
| 456 | |||
| 457 | sub pushlog { # push messages to log file | ||
| 458 | my $s =shift; | ||
| 459 | return @_ if !$s->{-pushlog}; | ||
| 460 | my $b ="[" .$0 ."\t" .$s->user ."\t" .$s->strtime() ."]\t"; | ||
| 461 | $s->fut->fstore('-', '>' .$s->{-pushlog}, map {$b .(defined($_) ?$_ :'')} @_); | ||
| 462 | @_ | ||
| 463 | } | ||
| 464 | |||
| 465 | |||
| 466 | sub problem { # problem flag | ||
| 467 | $_[0]->pushmsg($_[0]->{-problem} =$_[1] || $@ || $!); | ||
| 468 | } | ||
| 469 | |||
| 470 | |||
| 471 | sub warn { # warning | ||
| 472 | problem(@_); | ||
| 473 | my $m =$_[1] || $@ || $!; | ||
| 474 | if ($m !~/\n/) { | ||
| 475 | CGI::Carp::cluck($m) # carp cluck | ||
| 476 | } | ||
| 477 | else { | ||
| 478 | eval {$_[0]->pushlog('Warning $m')}; | ||
| 479 | $m=$_[0]->htmlescape($m); | ||
| 480 | if (!$_[0] ||!$_[0]->{-cache} ||!$_[0]->{-cache}->{-httpheader}) { | ||
| 481 | print STDOUT "Content-type: text/html\n\n"; | ||
| 482 | } | ||
| 483 | print STDOUT '<hr /><h1>' .$_[0]->lng(0,'Warning') ."</h1>\n"; | ||
| 484 | print STDOUT "$m<hr />\n"; | ||
| 485 | } | ||
| 486 | } | ||
| 487 | |||
| 488 | |||
| 489 | sub die { # stop error | ||
| 490 | my $m =$_[1] || $@ || $!; | ||
| 491 | if (!CGI::Carp::ineval) { #!$^S | ||
| 492 | eval {$_[0]->pushlog('Error $m', @{$_[0]->pushmsg} ,'<---Error')}; | ||
| 493 | if ($m !~/\n/ || !$_[0]->{-cgi}) { | ||
| 494 | eval{$_[0]->reset}; # for mod_perl | ||
| 495 | CGI::Carp::confess($m) # croak confess | ||
| 496 | } | ||
| 497 | $m=$_[0]->htmlescape($m); | ||
| 498 | if (!$_[0] ||!$_[0]->{-cache} ||!$_[0]->{-cache}->{-httpheader}) { | ||
| 499 | print STDOUT "Content-type: text/html\n\n"; | ||
| 500 | } | ||
| 501 | print STDOUT '<hr /><h1>' .$_[0]->lng(0,'Error') ."</h1>\n"; | ||
| 502 | print STDOUT "$m<br />\n"; | ||
| 503 | print STDOUT '<font size="2">' | ||
| 504 | , join(';<br />', map {$_[0]->htmlescape($_)} @{$_[0]->pushmsg}) | ||
| 505 | , '</font>'; | ||
| 506 | print STDOUT "<hr />\n"; | ||
| 507 | eval{$_[0]->reset}; # for mod_perl | ||
| 508 | exit; | ||
| 509 | } | ||
| 510 | $m !~/\n/ ? CGI::Carp::confess($m) : CORE::die($m); # croak confess | ||
| 511 | } | ||
| 512 | |||
| 513 | |||
| 514 | ####################### | ||
| 515 | |||
| 516 | |||
| 517 | sub cgi { # CGI object | ||
| 518 | $_[0]->{-cgi} | ||
| 519 | } | ||
| 520 | |||
| 521 | |||
| 522 | sub request { # Web server request object | ||
| 523 | $ENV{MOD_PERL} ? Apache->request | ||
| 524 | : $_[0]->{-cgi} | ||
| 525 | } | ||
| 526 | |||
| 527 | |||
| 528 | sub dbi { # DBI object | ||
| 529 | if (scalar(@_) >1) { | ||
| 530 | my $s =shift; | ||
| 531 | $s->{-dbi} =eval('use DBI; DBI->connect(@_)') ||$s->die("Cannot connect to database\n") | ||
| 532 | } | ||
| 533 | elsif (!$_[0]->{-dbi} && $_[0]->{-classes}->{-dbi}) { | ||
| 534 | my $s =shift; | ||
| 535 | # $s->pushmsg('DBI connect'); | ||
| 536 | my $v =$s->{-classes}->{-dbi}; | ||
| 537 | $s->{-dbi} =ref($v) eq 'CODE' ? &$v($s) : $s->dbi(@$v); | ||
| 538 | } | ||
| 539 | else { | ||
| 540 | $_[0]->{-dbi} | ||
| 541 | } | ||
| 542 | } | ||
| 543 | |||
| 544 | |||
| 545 | sub dbquote { | ||
| 546 | $_[0]->{-dbi} ||$_[0]->{-classes}->{-dbi} | ||
| 547 | ? $_[0]->dbi->quote(@_[1..$#_]) | ||
| 548 | : ('"' .join('', map {my $v=$_; $v=~s/([\\"])/\\$1/g; $v} @_[1..$#_]) .'"') | ||
| 549 | } | ||
| 550 | |||
| 551 | |||
| 552 | sub dblikesc { | ||
| 553 | join('', map {my $v =$_; $v =~s/([\\%_])/\\$1/g; $v} @_[1..$#_]) | ||
| 554 | } | ||
| 555 | |||
| 556 | |||
| 557 | ####################### | ||
| 558 | |||
| 559 | |||
| 560 | |||
| 561 | sub url { # CGI script URL | ||
| 562 | if ($#_ >0) { | ||
| 563 | local $^W =0; | ||
| 564 | my $v =($_[0]->{-cgi}||$_[0]->cgi)->url(@_[1..$#_]); | ||
| 565 | if ($v) {} | ||
| 566 | elsif (!($ENV{PERLXS} ||(($ENV{GATEWAY_INTERFACE}||'') =~/PerlEx/))) {} | ||
| 567 | elsif (($#_ >2) ||(($#_ ==2) && !$_[2])) {} | ||
| 568 | elsif ($_[1] eq '-relative') { | ||
| 569 | $v =$ENV{SCRIPT_NAME}; | ||
| 570 | $v =$1 if $v =~/[\\\/]([^\\\/]+)$/; | ||
| 571 | } | ||
| 572 | elsif ($_[1] eq '-absolute') { | ||
| 573 | $v =$ENV{SCRIPT_NAME} | ||
| 574 | } | ||
| 575 | return($v) | ||
| 576 | } | ||
| 577 | return($_[0]->{-cache}->{-url}) | ||
| 578 | if $_[0]->{-cache}->{-url}; | ||
| 579 | local $^W =0; | ||
| 580 | $_[0]->{-cache}->{-url} =$_[0]->cgi->url(); | ||
| 581 | if ($ENV{PERLXS} ||(($ENV{GATEWAY_INTERFACE}||'') =~/PerlEx/)) { | ||
| 582 | $_[0]->{-cache}->{-url} .= | ||
| 583 | (($_[0]->{-cache}->{-url} =~/\/$/) ||($ENV{SCRIPT_NAME} =~/^\//) ? '' : '/') | ||
| 584 | .$ENV{SCRIPT_NAME} | ||
| 585 | if ($_[0]->{-cache}->{-url} !~/\w\/\w/) && $ENV{SCRIPT_NAME}; | ||
| 586 | } | ||
| 587 | $_[0]->{-cache}->{-url} | ||
| 588 | } | ||
| 589 | |||
| 590 | |||
| 591 | sub url_form { # form url for start_form | ||
| 592 | $_[0]->url | ||
| 593 | # $_[0]->url(-absolute=>1,-path=>1) | ||
| 594 | # $_[0]->cgi->self_url() | ||
| 595 | } | ||
| 596 | |||
| 597 | |||
| 598 | sub qpath { # Query (script) path | ||
| 599 | defined($_[0]->{-qpath}) ||($_[0]->{-qpath} =$ENV{SCRIPT_FILENAME} ||$ENV{PATH_TRANSLATED}); | ||
| 600 | (!defined($_[1]) ? $_[0]->{-qpath} : $_[0]->{-qpath} .'/' .$_[1]) | ||
| 601 | } | ||
| 602 | |||
| 603 | |||
| 604 | sub qurl { # Query (script) URL | ||
| 605 | defined($_[0]->{-qurl}) ||($_[0]->{-qurl} =$_[0]->url); | ||
| 606 | (!defined($_[1]) || $_[1] eq '' ? $_[0]->{-qurl} : ($_[0]->{-qurl} .'/')) | ||
| 607 | .(scalar(@_) >1 ? $_[0]->htmlurl(@_[1..$#_]) :'') | ||
| 608 | } | ||
| 609 | |||
| 610 | |||
| 611 | sub qparam { # Query param(s) set or get | ||
| 612 | my $s =shift; | ||
| 613 | if (!ref($_[0])) { # CGI param call | ||
| 614 | $s->{-cgi}->param(@_) | ||
| 615 | } | ||
| 616 | elsif (ref($_[0]) eq 'ARRAY') { | ||
| 617 | if (!defined($_[1])) { # qparam([names]) -> [values] | ||
| 618 | my $r =[]; | ||
| 619 | for (my $i =0; $i <=$#{$_[0]}; $i++) {push @$r, $s->{-cgi}->param($_[0]->[$i])} | ||
| 620 | $r | ||
| 621 | } | ||
| 622 | else { # qparam([names]=>[values]) -> [values] | ||
| 623 | for (my $i =0; $i <=$#{$_[0]}; $i++) {$s->{-cgi}->param($_[0]->[$i], $_[1]->[$i])} | ||
| 624 | $_[1] | ||
| 625 | } | ||
| 626 | } | ||
| 627 | elsif (ref($_[0]) eq 'HASH') { # qparam({name=>value,...}) -> {name=>value,...} | ||
| 628 | foreach my $k (keys(%{$_[0]})) {$s->{-cgi}->param($k,$_[0]->{$k})} | ||
| 629 | $_[0] | ||
| 630 | } | ||
| 631 | else { # CGI param call | ||
| 632 | $s->{-cgi}->param(@_) | ||
| 633 | } | ||
| 634 | } | ||
| 635 | |||
| 636 | |||
| 637 | sub param { # CGI param call | ||
| 638 | shift->{-cgi}->param(@_) | ||
| 639 | } | ||
| 640 | |||
| 641 | |||
| 642 | sub qparamh { # Query params get as hash ref | ||
| 643 | my $s =shift; | ||
| 644 | return $s->qparam(@_) if ref($_[0]) ne 'ARRAY' || defined($_[1]); | ||
| 645 | my $r ={}; | ||
| 646 | for (my $i =0; $i <=$#{$_[0]}; $i++) {$r->{$_[0]->[$i]} =$s->{-cgi}->param($_[0]->[$i])} | ||
| 647 | $r | ||
| 648 | } | ||
| 649 | |||
| 650 | |||
| 651 | sub qrun { # Query 'run' param - Script to run | ||
| 652 | $_[0]->{-cache}->{-qrun} =$_[1] | ||
| 653 | ## || $ENV{REQUEST_URI} ? substr($ENV{REQUEST_URI}, length($ENV{SCRIPT_NAME})+1) :'' | ||
| 654 | || $_[0]->{-cgi}->param('_run') | ||
| 655 | || $_[0]->{-cgi}->url_param('') | ||
| 656 | || $_[0]->{-cgi}->url_param('run') | ||
| 657 | if !$_[0]->{-cache}->{-qrun} || $_[1]; | ||
| 658 | $_[0]->{-cache}->{-qrun} | ||
| 659 | } | ||
| 660 | |||
| 661 | |||
| 662 | ####################### | ||
| 663 | |||
| 664 | |||
| 665 | sub spath { # Site Path | ||
| 666 | if (!defined($_[0]->{-spath})) { | ||
| 667 | $_[0]->{-spath} =substr($ENV{SCRIPT_FILENAME} ||$ENV{PATH_TRANSLATED} | ||
| 668 | , 0 | ||
| 669 | , -length($ENV{SCRIPT_NAME} ||$ENV{PATH_INFO})); | ||
| 670 | } | ||
| 671 | !defined($_[1]) ? $_[0]->{-spath} : $_[0]->{-spath} .'/' .$_[1] | ||
| 672 | } | ||
| 673 | |||
| 674 | |||
| 675 | sub surl { # Site URL | ||
| 676 | ($_[0]->{-surl} | ||
| 677 | || ($_[0]->{-surl} = | ||
| 678 | $_[0]->url() =~/^([^\/]+:\/\/[^\/]+)/ ? $1 : $_[0]->url())) | ||
| 679 | . ((!defined($_[1]) || $_[1] eq '' ? '' : '/') | ||
| 680 | . (scalar(@_) >1 ? $_[0]->htmlurl(@_[1..$#_]) :'')); | ||
| 681 | } | ||
| 682 | |||
| 683 | |||
| 684 | sub bpath { # Binary Path | ||
| 685 | if (!defined($_[0]->{-bpath})) { | ||
| 686 | $_[0]->{-bpath} =(($ENV{SCRIPT_FILENAME} ||$ENV{PATH_TRANSLATED} ||$0) =~/^(.+?)[\\\/][^\\\/]+$/ ? $1 : ''); | ||
| 687 | } | ||
| 688 | !defined($_[1]) ? $_[0]->{-bpath} : $_[0]->{-bpath} .'/' .$_[1] | ||
| 689 | } | ||
| 690 | |||
| 691 | |||
| 692 | sub burl { # Binary URL | ||
| 693 | if (!defined($_[0]->{-burl})) { | ||
| 694 | my $pv =(($ENV{SCRIPT_NAME} ||$ENV{PATH_INFO} ||$0) =~/^[\\\/]*(.+?)[\\\/]+[^\\\/]+$/ ? $1 : ''); | ||
| 695 | $_[0]->{-burl} =$_[0]->surl .($pv ? '/' .$pv :''); | ||
| 696 | } | ||
| 697 | (!defined($_[1]) || $_[1] eq '' ? $_[0]->{-burl} : ($_[0]->{-burl} .'/')) | ||
| 698 | .(scalar(@_) >1 ? $_[0]->htmlurl(@_[1..$#_]) : '') | ||
| 699 | } | ||
| 700 | |||
| 701 | |||
| 702 | sub dpath { # Data Path | ||
| 703 | if (!defined($_[0]->{-dpath})) { | ||
| 704 | $_[0]->{-dpath} =$_[0]->tpath; | ||
| 705 | } | ||
| 706 | !defined($_[1]) ? $_[0]->{-dpath} : $_[0]->{-dpath} .'/' .$_[1] | ||
| 707 | } | ||
| 708 | |||
| 709 | |||
| 710 | sub tpath { # Temporary files Path | ||
| 711 | if (!defined($_[0]->{-tpath})) { | ||
| 712 | $_[0]->{-tpath} =$TempFile::TMPDIRECTORY # use CGI | ||
| 713 | ||$ENV{TMP} ||$ENV{TEMP} | ||
| 714 | ||$_[0]->orarg('-d' | ||
| 715 | ,$^O eq 'MSWin32' | ||
| 716 | ?('c:/tmp','c:/temp') | ||
| 717 | :('/tmp','/temp')); | ||
| 718 | $_[0]->{-tpath} = ($_[0]->{-tpath} ||'') .'/cgi-bus' | ||
| 719 | } | ||
| 720 | !defined($_[1]) ? $_[0]->{-tpath} : $_[0]->{-tpath} .'/' .$_[1] | ||
| 721 | } | ||
| 722 | |||
| 723 | |||
| 724 | sub ppath { # Publish Path | ||
| 725 | if (!defined($_[0]->{-ppath})) { | ||
| 726 | $_[0]->{-ppath} =$ENV{DOCUMENT_ROOT} ||$ENV{PATH_TRANSLATED} ||'.'; | ||
| 727 | } | ||
| 728 | !defined($_[1]) ? $_[0]->{-ppath} : $_[0]->{-ppath} .'/' .$_[1] | ||
| 729 | } | ||
| 730 | |||
| 731 | |||
| 732 | sub purl { # Publish URL | ||
| 733 | if (!defined($_[0]->{-purl})) { | ||
| 734 | $_[0]->{-purl} =$_[0]->surl; | ||
| 735 | } | ||
| 736 | (!defined($_[1]) || $_[1] eq '' ? $_[0]->{-purl} : ($_[0]->{-purl} .'/')) | ||
| 737 | .(scalar(@_) >1 ? $_[0]->htmlurl(@_[1..$#_]) : '') | ||
| 738 | } | ||
| 739 | |||
| 740 | |||
| 741 | sub fpath { # File Store Path | ||
| 742 | $_[0]->{-fpath} =$_[0]->ppath if !defined($_[0]->{-fpath}); | ||
| 743 | !defined($_[1]) ? $_[0]->{-fpath} : $_[0]->{-fpath} .'/' .$_[1] | ||
| 744 | } | ||
| 745 | |||
| 746 | |||
| 747 | sub furl { # File Store URL | ||
| 748 | $_[0]->{-furl} =$_[0]->purl if !defined($_[0]->{-furl}); | ||
| 749 | (!defined($_[1]) || $_[1] eq '' ? $_[0]->{-furl} : ($_[0]->{-furl} .'/')) | ||
| 750 | .(scalar(@_) >1 ? $_[0]->htmlurl(@_[1..$#_]) : '') | ||
| 751 | } | ||
| 752 | |||
| 753 | |||
| 754 | sub furf { # File Store file URL | ||
| 755 | $_[0]->{-furf} ='file://' .$_[0]->fpath if !defined($_[0]->{-furf}); | ||
| 756 | (!defined($_[1]) || $_[1] eq '' ? $_[0]->{-furf} : ($_[0]->{-furf} .'/')) | ||
| 757 | .(scalar(@_) >1 ? $_[0]->htmlurl(@_[1..$#_]) : '') | ||
| 758 | } | ||
| 759 | |||
| 760 | |||
| 761 | sub hpath { # Homes Store Path | ||
| 762 | $_[0]->{-hpath} =$_[0]->ppath if !defined($_[0]->{-hpath}); | ||
| 763 | !defined($_[1]) ? $_[0]->{-hpath} : $_[0]->{-hpath} .'/' .$_[1] | ||
| 764 | } | ||
| 765 | |||
| 766 | |||
| 767 | sub hurl { # Homes Store URL | ||
| 768 | $_[0]->{-hurl} =$_[0]->purl if !defined($_[0]->{-hurl}); | ||
| 769 | (!defined($_[1]) || $_[1] eq '' ? $_[0]->{-hurl} : ($_[0]->{-hurl} .'/')) | ||
| 770 | .(scalar(@_) >1 ? $_[0]->htmlurl(@_[1..$#_]) : '') | ||
| 771 | } | ||
| 772 | |||
| 773 | |||
| 774 | sub hurf { # Homes Store file URL | ||
| 775 | $_[0]->{-hurf} ='file://' .$_[0]->hpath if !defined($_[0]->{-hurf}); | ||
| 776 | (!defined($_[1]) || $_[1] eq '' ? $_[0]->{-hurf} : ($_[0]->{-hurf} .'/')) | ||
| 777 | .(scalar(@_) >1 ? $_[0]->htmlurl(@_[1..$#_]) : '') | ||
| 778 | } | ||
| 779 | |||
| 780 | |||
| 781 | sub urfcnd { # Use URFs? | ||
| 782 | my $s =shift; | ||
| 783 | ($s->{-cgi}->user_agent||'') =~/MSIE|StarOffice/ | ||
| 784 | && ( ref($s->{-urfcnd}) eq 'CODE' ? &{$s->{-urfcnd}}(@_) | ||
| 785 | : exists $s->{-urfcnd} ? $s->{-urfcnd} | ||
| 786 | : 1 # $ENV{REMOTE_ADDR} | ||
| 787 | ) | ||
| 788 | } | ||
| 789 | |||
| 790 | |||
| 791 | ####################### | ||
| 792 | |||
| 793 | |||
| 794 | sub hmerge { # merge hash ref with data given | ||
| 795 | my ($s, $h) =(shift, shift); | ||
| 796 | my $r =$h ? {%$h} : {}; | ||
| 797 | my %h =@_; | ||
| 798 | foreach my $k (keys %h) {$r->{$k} =$h{$k} if !exists($r->{$k})} | ||
| 799 | $r | ||
| 800 | } | ||
| 801 | |||
| 802 | |||
| 803 | sub max { # maximal number | ||
| 804 | (($_[1]||0) >($_[2]||0) ? $_[1] : $_[2])||0 | ||
| 805 | } | ||
| 806 | |||
| 807 | |||
| 808 | sub min { # minimal number | ||
| 809 | (($_[1]||0) >($_[2]||0) ? $_[2] : $_[1])||0 | ||
| 810 | } | ||
| 811 | |||
| 812 | |||
| 813 | sub orarg { # argument of true result | ||
| 814 | shift(@_); | ||
| 815 | my $s =ref($_[0]) ? shift | ||
| 816 | :index($_[0], '-') ==0 ? eval('sub{' .shift(@_) .' $_}') | ||
| 817 | :eval('sub{' .shift(@_) .'($_)}'); | ||
| 818 | local $_; | ||
| 819 | foreach (@_) {return $_ if &$s($_)}; | ||
| 820 | undef | ||
| 821 | } | ||
| 822 | |||
| 823 | |||
| 824 | sub strtime { # Stringify Time | ||
| 825 | my $s =shift; | ||
| 826 | my $msk =@_ ==0 || $_[0] =~/^\d+$/i ? 'yyyy-mm-dd hh:mm:ss' : shift; | ||
| 827 | my @tme =@_ ==0 ? localtime(time) : @_ ==1 ? localtime($_[0]) : @_; | ||
| 828 | $msk =~s/yyyy/%Y/; | ||
| 829 | $msk =~s/yy/%y/; | ||
| 830 | $msk =~s/mm/%m/; | ||
| 831 | $msk =~s/mm/%M/i; | ||
| 832 | $msk =~s/dd/%d/; | ||
| 833 | $msk =~s/hh/%H/; | ||
| 834 | $msk =~s/hh/%h/i; | ||
| 835 | $msk =~s/ss/%S/; | ||
| 836 | eval('use POSIX'); | ||
| 837 | POSIX::strftime($msk, @tme) | ||
| 838 | } | ||
| 839 | |||
| 840 | |||
| 841 | sub timestr { # Time from String | ||
| 842 | my $s =shift; | ||
| 843 | my $msk =@_ <2 || !$_[1] ? 'yyyy-mm-dd hh:mm:ss' : shift; | ||
| 844 | my $ts =shift; | ||
| 845 | my %th; | ||
| 846 | while ($msk =~/(yyyy|yy|mm|dd|hh|MM|ss)/) { | ||
| 847 | my $m=$1; $msk =$'; | ||
| 848 | last if !($ts =~/(\d+)/); | ||
| 849 | my $d =$1; $ts =$'; | ||
| 850 | $d -=1900 if $m eq 'yyyy' ||$m eq '%Y'; | ||
| 851 | $m =chop($m); | ||
| 852 | $m ='M' if $m eq 'm' && $th{$m}; | ||
| 853 | $m =lc($m) if $m ne 'M'; | ||
| 854 | $th{$m}=$d; | ||
| 855 | } | ||
| 856 | eval('use POSIX'); | ||
| 857 | POSIX::mktime($th{'s'}||0,$th{'M'}||0,$th{'h'}||0,$th{'d'}||0,($th{'m'}||1)-1,$th{'y'}||0) | ||
| 858 | } | ||
| 859 | |||
| 860 | |||
| 861 | sub timeadd { # Adjust time to years, months, days,... | ||
| 862 | my $s =shift; | ||
| 863 | my @t =localtime(shift); | ||
| 864 | my $i =5; | ||
| 865 | foreach my $a (@_) {$t[$i] += ($a||0); $i--} | ||
| 866 | eval('use POSIX'); | ||
| 867 | POSIX::mktime(@t[0..5]) | ||
| 868 | } | ||
| 869 | |||
| 870 | |||
| 871 | sub cptran { # Translate strings between codepages | ||
| 872 | my ($s,$f,$t,@s) =@_; | ||
| 873 | foreach my $v ($f, $t) { | ||
| 874 | if ($v =~/oem|866/i) {$v =' ð ¡¢£¤¥ñ¦§¨©ª«¬®¯àáâãäåæçèéìëêíîï'} | ||
| 875 | elsif ($v =~/ansi|1251/i) {$v ='ÀÁÂÃÄŨÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÜÛÚÝÞßàáâãä叿çèéêëìíîïðñòóôõö÷øùüûúýþÿ'} | ||
| 876 | elsif ($v =~/koi/i) {$v ='áâ÷çäå³öúéêëìíîïðòóôõæèãþûýøùÿüàñÁÂ×ÇÄÅ£ÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛÝØÙßÜÀÑ'} | ||
| 877 | elsif ($v =~/8859-5/i) {$v ='°±²³´µ¡¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÌËÊÍÎÏÐÑÒÓÔÕñÖרÙÚÛÜÝÞßàáâãäåæçèéìëêíîï'} | ||
| 878 | } | ||
| 879 | map {eval("~tr/$f/$t/")} @s; | ||
| 880 | @s >1 ? @s : $s[0]; | ||
| 881 | } | ||
| 882 | |||
| 883 | |||
| 884 | sub dumpout { # Data dump out | ||
| 885 | my ($s, $d) =@_; | ||
| 886 | eval('use Data::Dumper'); | ||
| 887 | my $o =Data::Dumper->new([$d]); | ||
| 888 | $o->Indent(1); | ||
| 889 | $o->Dump(); | ||
| 890 | } | ||
| 891 | |||
| 892 | |||
| 893 | sub dumpin { # Data dump in | ||
| 894 | my ($s, $d) =@_; | ||
| 895 | my $e; for(my $i=0; !$e && $i<10; $i++) {$e =eval('use Safe; Safe->new()')}; | ||
| 896 | defined($e) && $e->reval($d) | ||
| 897 | } | ||
| 898 | |||
| 899 | |||
| 900 | sub ishtml { # Is html code? | ||
| 901 | ($_[1] ||'') =~m/^<(?:(?:B|BIG|BLOCKQUOTE|CENTER|CITE|CODE|DFN|DIV|EM|I|KBD|P|SAMP|SMALL|SPAN|STRIKE|STRONG|STYLE|SUB|SUP|TT|U|VAR)\s*>|(?:BR|HR)\s*\/{0,1}>|(?:A|BASE|BASEFONT|DIR|DIV|DL|!DOCTYPE|FONT|H\d|HEAD|HTML|IMG|IFRAME|MAP|MENU|OL|P|PRE|TABLE|UL)\b)/i | ||
| 902 | } | ||
| 903 | |||
| 904 | |||
| 905 | |||
| 906 | ####################### | ||
| 907 | |||
| 908 | |||
| 909 | |||
| 910 | sub user { # User name | ||
| 911 | if (!$_[0]->{-cache}->{-user} ||$_[1]) { | ||
| 912 | $_[0]->{-cache}->{-user} =$_[0]->{-cache}->{-useron} = | ||
| 913 | $_[1] ? $_[1] : | ||
| 914 | ref($_[0]->{-user}) eq 'CODE' ? &{$_[0]->{-user}}(@_) | ||
| 915 | : $_[0]->uauth->user(@_[1..$#_]); | ||
| 916 | if ($_[0]->{-usercnv}) { | ||
| 917 | local $_ =$_[0]->{-cache}->{-user}; | ||
| 918 | $_[0]->{-cache}->{-user} =&{$_[0]->{-usercnv}}(@_) | ||
| 919 | } | ||
| 920 | } | ||
| 921 | $_[0]->{-cache}->{-user} | ||
| 922 | } | ||
| 923 | |||
| 924 | |||
| 925 | sub useron { # User original name | ||
| 926 | $_[0]->user if !$_[0]->{-cache}->{-useron}; | ||
| 927 | $_[0]->{-cache}->{-useron} | ||
| 928 | } | ||
| 929 | |||
| 930 | |||
| 931 | sub uadmin { # Is admin? | ||
| 932 | my $s =shift; | ||
| 933 | my $u =$s->user; | ||
| 934 | if (scalar(@_)) { | ||
| 935 | return $u if $_[0] eq $u; | ||
| 936 | return $s->uadmin ? $s->uglist | ||
| 937 | : ($s->udata->paramj('uauth_managed') ||[]) | ||
| 938 | if ref($_[0]); | ||
| 939 | my $l =$s->udata->paramj('uauth_managed') ||[]; | ||
| 940 | foreach my $n (@$l) { | ||
| 941 | return $n if $n eq $_[0] | ||
| 942 | } | ||
| 943 | } | ||
| 944 | if (!defined($s->{-uadmins})) {} | ||
| 945 | elsif (ref($s->{-uadmins}) eq 'CODE') {return &{$s->{-uadmins}}($s)} | ||
| 946 | elsif (ref($s->{-uadmins}) eq 'ARRAY') { | ||
| 947 | foreach my $n (@{$s->ugnames}) { | ||
| 948 | next if !defined($n); | ||
| 949 | return $n if grep {$_ eq $n} @{$s->{-uadmins}} | ||
| 950 | } | ||
| 951 | } | ||
| 952 | else {return $u if $u eq $s->{-uadmins}} | ||
| 953 | return ''; | ||
| 954 | } | ||
| 955 | |||
| 956 | |||
| 957 | sub uguest { # Is guest? | ||
| 958 | ($_[1] ||$_[0]->user ||'') eq $_[0]->uauth->guest | ||
| 959 | } | ||
| 960 | |||
| 961 | |||
| 962 | sub usercn { # User name CN | ||
| 963 | my $v =scalar(@_) >1 ? $_[1] : $_[0]->user; | ||
| 964 | return($v) if !defined($v) || $v eq ''; | ||
| 965 | $v =~/CN=([^=,]+)/i ? $1 | ||
| 966 | : $v =~/^([^\@])\@/i ? $1 | ||
| 967 | : $v =~/\\([^\\]+)$/ ? $1 | ||
| 968 | : $v | ||
| 969 | } | ||
| 970 | |||
| 971 | |||
| 972 | sub usersn { # User Shorten Name, remove domain if default | ||
| 973 | my $v =scalar(@_) >1 ? $_[1] : $_[0]->user; | ||
| 974 | return($v) if !defined($v) || $v eq ''; | ||
| 975 | my $d =$_[0]->usdomain; | ||
| 976 | if ($v =~m/^(.*?)[\/@]\Q$d\E$/i) {$1} | ||
| 977 | elsif ($v =~m/^\Q$d\E[\\](.*)$/i) {$1} | ||
| 978 | else {$v} | ||
| 979 | } | ||
| 980 | |||
| 981 | |||
| 982 | sub userfn { # User name translated to filename | ||
| 983 | my $v =scalar(@_) >1 ? $_[1] : $_[0]->user; | ||
| 984 | return($v) if !defined($v) || $v eq ''; | ||
| 985 | $v =~ s/[\\\/|\+\:\*\?\[\]\(\) &,]/-/g; | ||
| 986 | $v | ||
| 987 | } | ||
| 988 | |||
| 989 | |||
| 990 | sub userds { # User name as dir structure | ||
| 991 | my $u =scalar(@_) >1 ? $_[1] : $_[0]->user; | ||
| 992 | return($u) if !defined($u) || $u eq ''; | ||
| 993 | my $p =$_[0]->userfn($_[0]->usercn($u)); | ||
| 994 | $p =substr($p,0,1) .'/' .substr($p,0,2) .'/' .$_[0]->userfn($u); | ||
| 995 | } | ||
| 996 | |||
| 997 | |||
| 998 | sub unames { # User Names | ||
| 999 | if (!defined($_[0]->{-cache}->{-unames})) { | ||
| 1000 | my $s =$_[0]; | ||
| 1001 | return('') if !defined($s->user); | ||
| 1002 | $s->{-cache}->{-unames} =[]; | ||
| 1003 | local $_; | ||
| 1004 | foreach my $v ($_ =$s->user, $s->useron | ||
| 1005 | # , lc($s->user), $s->usercn, lc($s->usercn) | ||
| 1006 | #, $s->user =~/^([^\\]+)\\(.+)$/ ? lc("$2\@$1") : () | ||
| 1007 | #, $s->useron =~/^([^\\]+)\\(.+)$/ ? lc("$2\@$1") : () | ||
| 1008 | , $s->user =~/^([^@]+)\@(.+)$/ ? lc("$2\\$1") : () | ||
| 1009 | , $s->useron =~/^([^@]+)\@(.+)$/ ? lc("$2\\$1") : () | ||
| 1010 | , ref($s->{-unmsadd}) eq 'ARRAY' | ||
| 1011 | ? map {&$_($s)} @{$s->{-unmsadd}} | ||
| 1012 | : ref($s->{-unmsadd}) | ||
| 1013 | ? &{$s->{-unmsadd}}($s) | ||
| 1014 | : () | ||
| 1015 | ) { | ||
| 1016 | push @{$s->{-cache}->{-unames}}, $v | ||
| 1017 | if !grep /^\Q$v\E$/, @{$s->{-cache}->{-unames}}; | ||
| 1018 | } | ||
| 1019 | } | ||
| 1020 | $_[0]->{-cache}->{-unames} | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | |||
| 1024 | sub usdomain {# User names Server Domain | ||
| 1025 | if (!$_[0]->{-cache}->{-usdomain} ||$_[1]) { | ||
| 1026 | $_[0]->{-cache}->{-usdomain} =$_[1] | ||
| 1027 | || (ref($_[0]->{-usdomain}) eq 'CODE' | ||
| 1028 | ? &{$_[0]->{-usdomain}}(@_) | ||
| 1029 | : $_[0]->uauth->usdomain(@_[1..$#_])); | ||
| 1030 | } | ||
| 1031 | $_[0]->{-cache}->{-usdomain} | ||
| 1032 | } | ||
| 1033 | |||
| 1034 | |||
| 1035 | sub userver { # User names Server | ||
| 1036 | if (!$_[0]->{-cache}->{-userver} ||$_[1]) { | ||
| 1037 | $_[0]->{-cache}->{-userver} =$_[1] | ||
| 1038 | ||(ref($_[0]->{-userver}) eq 'CODE' | ||
| 1039 | ? &{$_[0]->{-userver}}(@_) | ||
| 1040 | : $_[0]->uauth->userver(@_[1..$#_])); | ||
| 1041 | } | ||
| 1042 | $_[0]->{-cache}->{-userver} | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | |||
| 1046 | sub ugroups { # User groups [user name] | ||
| 1047 | if (!defined($_[0]->{-cache}->{-ugroups}) | ||
| 1048 | || ($_[1] && (lc($_[0]->useron ||'') ne lc($_[1])) | ||
| 1049 | && (lc($_[0]->user ||'') ne lc($_[1])))) { | ||
| 1050 | my $s =$_[0]; | ||
| 1051 | my $r =[]; | ||
| 1052 | return($r) if !defined($s->user) && !$_[1]; | ||
| 1053 | $r = ref($s->{-ugroups}) eq 'CODE' | ||
| 1054 | ? &{$s->{-ugroups}}(@_) | ||
| 1055 | : $_[0]->uauth->ugroups(@_[1..$#_]); | ||
| 1056 | if ($_[0]->{-ugrpcnv}) { | ||
| 1057 | my $ga =[]; | ||
| 1058 | local $_; | ||
| 1059 | foreach $_ (@$r) { | ||
| 1060 | $_ =&{$_[0]->{-ugrpcnv}}(@_); | ||
| 1061 | push(@$ga, $_) if defined($_) && $_ ne ''; | ||
| 1062 | } | ||
| 1063 | $r =$ga; | ||
| 1064 | } | ||
| 1065 | if ($_[0]->{-ugrpadd}) { | ||
| 1066 | local $_ =$r; | ||
| 1067 | my $ugadd=ref($s->{-ugrpadd}) eq 'CODE' ? &{$s->{-ugrpadd}}(@_) : $s->{-ugrpadd}; | ||
| 1068 | foreach my $e ( ref($ugadd) eq 'ARRAY' | ||
| 1069 | ? @{$ugadd} | ||
| 1070 | : ref($ugadd) eq 'HASH' | ||
| 1071 | ? keys(%$ugadd) | ||
| 1072 | : $ugadd){ | ||
| 1073 | push @$r, $e if !grep /^\Q$e\E$/i, @$r | ||
| 1074 | } | ||
| 1075 | } | ||
| 1076 | { use locale; | ||
| 1077 | $r =[sort {lc($a) cmp lc($b)} @$r]; | ||
| 1078 | } | ||
| 1079 | $s->{-cache}->{-ugroups} =$r | ||
| 1080 | if !$_[1] | ||
| 1081 | || (lc($_[0]->useron) eq lc($_[1])) | ||
| 1082 | || (lc($_[0]->user) eq lc($_[1])); | ||
| 1083 | return($r) | ||
| 1084 | } | ||
| 1085 | $_[0]->{-cache}->{-ugroups} | ||
| 1086 | } | ||
| 1087 | |||
| 1088 | |||
| 1089 | sub ugnames { # User & Group Names | ||
| 1090 | if (!defined($_[0]->{-cache}->{-ugnames})) { | ||
| 1091 | my $s =$_[0]; | ||
| 1092 | return('') if !defined($s->user); | ||
| 1093 | $s->{-cache}->{-ugnames} =[]; | ||
| 1094 | push @{$s->{-cache}->{-ugnames}}, @{$s->unames}; | ||
| 1095 | push @{$s->{-cache}->{-ugnames}}, @{$s->ugroups}; | ||
| 1096 | } | ||
| 1097 | $_[0]->{-cache}->{-ugnames} | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | |||
| 1101 | sub uglist { # User & Group List | ||
| 1102 | my $s =shift; | ||
| 1103 | my $o =defined($_[0]) && substr($_[0],0,1) eq '-' ? shift : '-ug'; | ||
| 1104 | my $r = | ||
| 1105 | ref($s->{-uglist}) eq 'CODE' ? &{$s->{-uglist}}($s,$o,@_) | ||
| 1106 | : $s->uauth->uglist($o,@_); | ||
| 1107 | if ($s->{-ugrpadd}) { | ||
| 1108 | local $_ =$r; | ||
| 1109 | my $ugadd=ref($s->{-ugrpadd}) eq 'CODE' ? &{$s->{-ugrpadd}}(@_) : $s->{-ugrpadd}; | ||
| 1110 | if ((ref($r) eq 'HASH') | ||
| 1111 | && (ref($ugadd) eq 'HASH')) { | ||
| 1112 | foreach my $e (keys(%$ugadd)) { | ||
| 1113 | $r->{$e} =$ugadd->{$e} if !$r->{$e} | ||
| 1114 | } | ||
| 1115 | } | ||
| 1116 | else { | ||
| 1117 | foreach my $e ( ref($ugadd) eq 'ARRAY' | ||
| 1118 | ? @{$ugadd} | ||
| 1119 | : ref($ugadd) eq 'HASH' | ||
| 1120 | ? keys(%$ugadd) | ||
| 1121 | : $ugadd){ | ||
| 1122 | if (ref($r) eq 'HASH') { | ||
| 1123 | $r->{$e} =$e if !$r->{$e} | ||
| 1124 | } | ||
| 1125 | else { | ||
| 1126 | push @$r, $e if !grep /^\Q$e\E$/i, @$r | ||
| 1127 | } | ||
| 1128 | } | ||
| 1129 | } | ||
| 1130 | } | ||
| 1131 | $r =do{use locale; [sort {lc($a) cmp lc($b)} @$r]} if ref($r) eq 'ARRAY'; | ||
| 1132 | |||
| 1133 | if ($s->{-ugrpcnv}) { | ||
| 1134 | local $_; | ||
| 1135 | if (ref($r) eq 'ARRAY') { | ||
| 1136 | my @g; | ||
| 1137 | foreach $_ (@$r) { | ||
| 1138 | $_ =&{$s->{-ugrpcnv}}($s,$o); | ||
| 1139 | push(@g, $_) if defined($_) && $_ ne ''; | ||
| 1140 | } | ||
| 1141 | $r =[sort {lc($a) cmp lc($b)} @g]; | ||
| 1142 | } | ||
| 1143 | else { | ||
| 1144 | my $w =$_[1]; # width of label | ||
| 1145 | foreach my $k (keys %$r) { | ||
| 1146 | $_ =$k; | ||
| 1147 | $_ =&{$s->{-ugrpcnv}}($s,$o); | ||
| 1148 | if (defined($_) && $_ ne '') { | ||
| 1149 | $r->{$_} =$r->{$k}; | ||
| 1150 | $r->{$_} =substr($r->{$_},0,$w) if $w; | ||
| 1151 | } | ||
| 1152 | elsif (!defined($_) || $_ eq '' || $_ ne $k) { | ||
| 1153 | delete $r->{$k} | ||
| 1154 | } | ||
| 1155 | } | ||
| 1156 | } | ||
| 1157 | } | ||
| 1158 | $r | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | |||
| 1162 | sub unamesun {# User Names Unique list | ||
| 1163 | my $s =shift; | ||
| 1164 | my $r =[]; | ||
| 1165 | foreach my $n (ref($_[0]) ? @{$_[0]} : @_) { | ||
| 1166 | next if grep {lc($n) eq lc($_) | ||
| 1167 | || lc($s->usercn($n)) eq lc($s->usercn($_))} @$r; | ||
| 1168 | push @$r, $n; | ||
| 1169 | } | ||
| 1170 | $r | ||
| 1171 | } | ||
| 1172 | |||
| 1173 | |||
| 1174 | sub userauth {# User Authenticate | ||
| 1175 | my $s =shift; | ||
| 1176 | $s->{-w32IISdpsn} =($ENV{SERVER_SOFTWARE}||'') !~/IIS/ | ||
| 1177 | ? 0 | ||
| 1178 | : ($s->{-login}||'') =~/\/$/i | ||
| 1179 | ? 2 | ||
| 1180 | : 0 | ||
| 1181 | if !defined($s->{-w32IISdpsn}); | ||
| 1182 | ref($s->{-userauth}) eq 'CODE' ? &{$s->{-userauth}}($s,@_) | ||
| 1183 | : ref($s->{-userauth}) eq 'ARRAY' ? $s->uauth->auth($s->{-userauth},@_) | ||
| 1184 | : $s->{-userauth} ? $s->uauth->auth([$s->{-userauth}],@_) | ||
| 1185 | : $s->uauth->auth(@_); | ||
| 1186 | $s->{-cache}->{-userauth} =$s->user | ||
| 1187 | } | ||
| 1188 | |||
| 1189 | |||
| 1190 | |||
| 1191 | sub userauthopt { # User Authenticate optional | ||
| 1192 | my $s =shift; | ||
| 1193 | if ($s->{-cache}->{-userauth}) { | ||
| 1194 | } | ||
| 1195 | elsif ($s->uguest() | ||
| 1196 | &&(defined($s->{-cgi}->param('_auth')) | ||
| 1197 | || defined($s->{-cgi}->param('_login')))) { | ||
| 1198 | $s->userauth(@_) | ||
| 1199 | } | ||
| 1200 | elsif ((($ENV{SERVER_SOFTWARE}||'') =~/IIS/) | ||
| 1201 | &&($s->url() =~/\/_*(login|auth|a|ntlm|search|guest)\//i)) { # !!! IIS impersonation avoid | ||
| 1202 | my $url =$s->url(); | ||
| 1203 | $s->userauth(@_) if $url !~/\/_*(search|guest)\//i | ||
| 1204 | ## && !$s->{-cache}->{-RevertToSelf} # -w32IISdpsn | ||
| 1205 | && (!$s->{-cache}->{-RevertToSelf} && (!defined($s->{-w32IISdpsn}) ? ($s->{-login}||'') =~/\/$/i : $s->{-w32IISdpsn} >1)) | ||
| 1206 | && !$s->uauth()->signget(); # $s->uguest | ||
| 1207 | if ((($s->qparam('_run')||'') ne 'SEARCH') | ||
| 1208 | && !$s->{-cache}->{-RevertToSelf} | ||
| 1209 | && (!defined($s->{-w32IISdpsn}) || ($s->{-w32IISdpsn} >1)) | ||
| 1210 | ) { # see 'search' in 'upws' | ||
| 1211 | $url =~s/\/_*(login|auth|a|ntlm|search|guest)\//\//i; | ||
| 1212 | $url .=($ENV{QUERY_STRING} ? ('?' .$ENV{QUERY_STRING}) :''); | ||
| 1213 | $s->print()->redirect(-uri=>$url, -nph=>1); | ||
| 1214 | eval{$s->reset()}; | ||
| 1215 | exit; | ||
| 1216 | } | ||
| 1217 | } | ||
| 1218 | $s->user | ||
| 1219 | } | ||
| 1220 | |||
| 1221 | |||
| 1222 | |||
| 1223 | sub w32IISdpsn {# deimpersonate Microsoft IIS impersonated process | ||
| 1224 | # 'Win32::API' used. | ||
| 1225 | # Set 'IIS / Home Directory / Application Protection' = 'Low (IIS Process)' | ||
| 1226 | # or see 'Administrative Tools / Component Services'. | ||
| 1227 | # Do not use quering to 'Index Server'. | ||
| 1228 | return(undef) if (defined($_[0]->{-w32IISdpsn}) && !$_[0]->{-w32IISdpsn}) | ||
| 1229 | || $_[0]->{-cache}->{-RevertToSelf} | ||
| 1230 | || ($^O ne 'MSWin32') | ||
| 1231 | || !(($ENV{SERVER_SOFTWARE}||'') =~/IIS/) | ||
| 1232 | || $ENV{'FCGI_SERVER_VERSION'}; | ||
| 1233 | $_[0]->user(); | ||
| 1234 | my $o =eval('use Win32::API; new Win32::API("advapi32.dll","RevertToSelf",[],"N")'); | ||
| 1235 | my $l =eval{Win32::LoginName()}||''; | ||
| 1236 | if ($o && $o->Call() && ($l ne (eval{Win32::LoginName()} ||''))) { | ||
| 1237 | $_[0]->{-cache}->{-RevertToSelf} =(Win32::LoginName()||'?'); | ||
| 1238 | $_[1] && $_[0]->{-debug} | ||
| 1239 | && $_[0]->pushmsg('w32IISdpsn(' .(defined($_[0]->{-w32IISdpsn}) ? $_[0]->{-w32IISdpsn} : 'undef') .')' .($_[0]->{-debug} >2 ? ' '. $_[0]->{-cache}->{-RevertToSelf} : '')) | ||
| 1240 | } | ||
| 1241 | else { | ||
| 1242 | return $_[0]->die($_[0]->lng(0, 'w32IISdpsn') .": Win32::API('RevertToSelf') -> " .join('; ', map {$_ ? $_ : ()} $@,$!,$^E)) | ||
| 1243 | } | ||
| 1244 | 1 | ||
| 1245 | } | ||
| 1246 | |||
| 1247 | |||
| 1248 | ####################### | ||
| 1249 | |||
| 1250 | |||
| 1251 | sub oscmd { # OS Command with logging | ||
| 1252 | my $s =shift; | ||
| 1253 | my $opt = substr($_[0],0,1) eq '-' ? shift : ''; # 'h'ide, 'i'gnore | ||
| 1254 | my $sub =ref($_[$#_]) eq 'CODE' ? pop : undef; | ||
| 1255 | my $r; | ||
| 1256 | my $o; | ||
| 1257 | $s->pushmsg(join(' ',@_)) if $opt !~/h/; | ||
| 1258 | local(*RDRFH, *WTRFH); | ||
| 1259 | if ($^X =~/(?:perlis|perlex)\d*\.dll$/i) { # !!! ISAPI IIS problem | ||
| 1260 | if ($sub) { | ||
| 1261 | open(WTRFH, '|' .join(' ',@_)) && defined(*WTRFH) || $s->die(join(' ',@_) .' -> ' .$!); | ||
| 1262 | # open(WTRFH, '|' ,@_) && defined(*WTRFH) || $s->die(join(' ',@_) .' -> ' .$!); | ||
| 1263 | my $ls =select(); select(WTRFH); $| =1; | ||
| 1264 | &$sub($s); | ||
| 1265 | select($ls); | ||
| 1266 | eval{close(WTRFH)}; | ||
| 1267 | } | ||
| 1268 | else { | ||
| 1269 | if ($opt !~/h/ && $_[0] =~/cacls/) { # !!! IIS/cacls behaviour debug | ||
| 1270 | $r =join(' ',@_,'2>&1'); | ||
| 1271 | @$o =`$r`; | ||
| 1272 | # push @$o, Win32::LoginName, `logname`; # 'SYSTEM'/'IUSR_' || 'IUSR_'/'IWAM' | ||
| 1273 | } | ||
| 1274 | else { | ||
| 1275 | system(@_) | ||
| 1276 | } | ||
| 1277 | } | ||
| 1278 | } | ||
| 1279 | else { | ||
| 1280 | eval('use IPC::Open2'); | ||
| 1281 | my $pid = IPC::Open2::open2(\*RDRFH, \*WTRFH, @_); | ||
| 1282 | if ($pid) { | ||
| 1283 | if ($sub) { | ||
| 1284 | my $select =select(); | ||
| 1285 | select(WTRFH); | ||
| 1286 | $| =1; | ||
| 1287 | &$sub($s); | ||
| 1288 | select($select); | ||
| 1289 | } | ||
| 1290 | @$o =<RDRFH>; | ||
| 1291 | waitpid($pid,0); | ||
| 1292 | } | ||
| 1293 | } | ||
| 1294 | $r =$?>>8; | ||
| 1295 | $s->pushmsg(@$o) if $o && $opt !~/h/; | ||
| 1296 | $s->die(join(' ',@_) .($opt !~/h/ ? '' : ' -> ' .join('',@{$o||[]})) ." -> $r\n") if $r && $opt !~/i/; | ||
| 1297 | !$r | ||
| 1298 | } | ||
| 1299 | |||
| 1300 | |||
| 1301 | |||
| 1302 | ####################### | ||
| 1303 | |||
| 1304 | |||
| 1305 | sub httpheader { | ||
| 1306 | my $s =shift; | ||
| 1307 | my %p =!defined($_[0]) ? () : @_==1 && ref($_[0]) ? %{$_[0]} : @_; | ||
| 1308 | if (ref($s->{-httpheader})) { | ||
| 1309 | foreach my $k (keys(%{$s->{-httpheader}})) { | ||
| 1310 | if (!exists($p{$k})) {$p{$k} =$s->{-httpheader}->{$k}} | ||
| 1311 | } | ||
| 1312 | } | ||
| 1313 | $s->{-cgi}->header(%p) | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | |||
| 1317 | sub htmlstart { | ||
| 1318 | my $s =shift; | ||
| 1319 | my %p =!defined($_[0]) ? () : @_==1 && ref($_[0]) ? %{$_[0]} : @_; | ||
| 1320 | if (ref($s->{-htmlstart})) { | ||
| 1321 | foreach my $k (keys(%{$s->{-htmlstart}})) { | ||
| 1322 | if (!exists($p{$k})) {$p{$k} =$s->{-htmlstart}->{$k}} | ||
| 1323 | } | ||
| 1324 | } | ||
| 1325 | $p{-style} ={code=> | ||
| 1326 | ".Form, .List, .Help, .MenuArea, .FooterArea {margin-top:0px; font-size: 8pt; font-family: Verdana, Helvetica, Arial, sans-serif; }\n" | ||
| 1327 | #."a:link.ListTable {font-weight: bold}\n" | ||
| 1328 | .".MenuButton {background-color: buttonface; color: black; text-decoration: none; font-size: 7pt;}\n" | ||
| 1329 | #."td.MenuButton {background-color: activeborder;}\n" | ||
| 1330 | #.".MenuArea {background-color: blue; color: white;}" | ||
| 1331 | #.".MenuButton {background-color: blue; color: white; text-decoration: none; font-size: 7pt;}\n" | ||
| 1332 | .".PaneLeft, .PaneForm, .PaneList {margin-top:0px; font-size: 8pt; font-family: Verdana, Helvetica, Arial, sans-serif; }\n" | ||
| 1333 | ."td.ListTable {border-style: inset; border-bottom-width: 1px; border-top-width: 0px; border-left-width: 0px; border-right-width: 0px; padding-top: 0;}\n" | ||
| 1334 | ."th.ListTable {border-style: inset; border-bottom-width: 1px; border-top-width: 0px; border-left-width: 0px; border-right-width: 0px;}\n" | ||
| 1335 | } if !exists($p{-style}); | ||
| 1336 | $s->{-debug} && $s->{-debug} >2 | ||
| 1337 | ? $s->{-cgi}->start_html(%p) | ||
| 1338 | .("\n<!-- " .$s->{-cgi}->escapeHTML($s->microenv) ." -->\n") | ||
| 1339 | : $s->{-cgi}->start_html(%p) | ||
| 1340 | } | ||
| 1341 | |||
| 1342 | |||
| 1343 | sub htmlend { | ||
| 1344 | $_[0]->microtest if $_[0]->{-debug} && $_[0]->{-debug} >3; | ||
| 1345 | $_[0]->{-cgi}->end_html | ||
| 1346 | } | ||
| 1347 | |||
| 1348 | |||
| 1349 | sub htpgstart { | ||
| 1350 | $_[0]->httpheader($_[1]) | ||
| 1351 | .$_[0]->htmlstart($_[2]) | ||
| 1352 | .($_[0]->{-htpgtop}||'') | ||
| 1353 | } | ||
| 1354 | |||
| 1355 | |||
| 1356 | sub htpgend { | ||
| 1357 | ($_[0]->{-htpgbot}||'') | ||
| 1358 | .$_[0]->htmlend | ||
| 1359 | } | ||
| 1360 | |||
| 1361 | |||
| 1362 | sub htpfstart { | ||
| 1363 | my $s =shift; | ||
| 1364 | $s->htpgstart($_[0],$_[1]) ."\n" | ||
| 1365 | .((($ENV{HTTP_USER_AGENT} ||'') =~m{^[^/]+/(\d)} ? $1 >=3 : 0) | ||
| 1366 | ? $s->{-cgi}->start_multipart_form({-action=>$s->url_form() | ||
| 1367 | , -acceptcharset=>$s->{-httpheader} ?$s->{-httpheader}->{-charset} :undef | ||
| 1368 | , $_[2] ? %{$_[2]} : () | ||
| 1369 | }) | ||
| 1370 | : $s->{-cgi}->start_form({-action=>$s->url_form() | ||
| 1371 | , -acceptcharset=>$s->{-httpheader} ?$s->{-httpheader}->{-charset} :undef} | ||
| 1372 | , $_[2] ? %{$_[2]} : () | ||
| 1373 | ) | ||
| 1374 | ) ."\n" | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | |||
| 1378 | sub htpfend { | ||
| 1379 | "\n</form>\n" .$_[0]->htpgend(@_) | ||
| 1380 | } | ||
| 1381 | |||
| 1382 | |||
| 1383 | sub htmlescape { | ||
| 1384 | !defined($_[1]) ? '' : shift->{-cgi}->escapeHTML(@_) | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | |||
| 1388 | sub htmlescapetext { | ||
| 1389 | my $s =shift; | ||
| 1390 | my $r =join("\n",@_); | ||
| 1391 | my $g =$s->cgi; | ||
| 1392 | my ($e, $m, $l) =(''); | ||
| 1393 | while ($r =~/\b(\w{3,5}:\/\/[^\s\t,()<>\[\]"']+[^\s\t.,;()<>\[\]"'])/) { | ||
| 1394 | $m =$1; $r =$'; | ||
| 1395 | $l =$g->escapeHTML($`); $l =~s/( {2,})/' ' x length($1)/ge; $l =~s/\n/<br \/>\n/g; $l =~s/\r//g; | ||
| 1396 | $e .=$l; | ||
| 1397 | $m =~s/^(host|urlh):\/\//\//; | ||
| 1398 | $m =~s/^(url|urlr):\/\//$s->url(-relative=>1)/e; | ||
| 1399 | $e .=$g->a({-href=>$m, -target=>'_blank'}, $g->escapeHTML($m)); | ||
| 1400 | } | ||
| 1401 | $r =$g->escapeHTML($r); $r =~s/( {2,})/' ' x length($1)/ge; $r =~s/\n/<br \/>\n/g; $r =~s/\r//g; | ||
| 1402 | $e .=$r; | ||
| 1403 | $e ="<code>$e</code>" if $e =~/ /; | ||
| 1404 | $e | ||
| 1405 | } | ||
| 1406 | |||
| 1407 | |||
| 1408 | sub urlescape { | ||
| 1409 | !defined($_[1]) ? '' : shift->{-cgi}->escape(@_) | ||
| 1410 | } | ||
| 1411 | |||
| 1412 | |||
| 1413 | sub htmlurl { # Create URL from call string and parameters | ||
| 1414 | return($_[0]->url .($ENV{QUERY_STRING} ? '?' .$ENV{QUERY_STRING} : '')) if scalar(@_) <2; | ||
| 1415 | my $rsp = $_[1]; # do not escape at all?!!! | ||
| 1416 | $rsp ='' if !defined($rsp); | ||
| 1417 | chop $rsp if $rsp ne '' && substr($rsp, length($rsp) -1, 0) eq '/'; | ||
| 1418 | $rsp =~s/([^a-zA-Z0-9_\.\-\/\?\=\&;:%])/uc sprintf("%%%02x",ord($1))/eg; # see cgi->escape | ||
| 1419 | $rsp .=($rsp =~/\?/ ? '&' : '?'); | ||
| 1420 | for (my $i =2; $i <$#_; $i +=2) { # see cgi->escape | ||
| 1421 | my @a =($_[$i], $_[$i+1]); | ||
| 1422 | map {!defined($_) ? ($_ ='') | ||
| 1423 | : ~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg} @a; | ||
| 1424 | $rsp .=$a[0] .'=' .$a[1] .'&'; | ||
| 1425 | } | ||
| 1426 | chop($rsp); | ||
| 1427 | $rsp; | ||
| 1428 | } | ||
| 1429 | |||
| 1430 | |||
| 1431 | sub htmlddlb { # HTML Drop-Down List Box - Input helper | ||
| 1432 | shift->wg->ddlb(@_); | ||
| 1433 | } | ||
| 1434 | |||
| 1435 | sub htmltextfield { # HTML Text filed with autosizing | ||
| 1436 | shift->wg->textfield(@_); | ||
| 1437 | } | ||
| 1438 | |||
| 1439 | |||
| 1440 | sub htmltextarea { # HTML Text area with autorowing and hrefs | ||
| 1441 | shift->wg->textarea(@_); | ||
| 1442 | } | ||
| 1443 | |||
| 1444 | |||
| 1445 | sub htmlfsdir { # HTML Filesystem dir field | ||
| 1446 | shift->wg->fsdir(@_); | ||
| 1447 | } | ||
| 1448 | |||
| 1449 | |||
| 1450 | ####################### | ||
| 1451 | |||
| 1452 | |||
| 1453 | sub print { # print and CGI::BusCgiPrint object | ||
| 1454 | my $s =shift; | ||
| 1455 | #return(undef) if scalar(@_) && !CORE::print @_; | ||
| 1456 | CORE::print @_; | ||
| 1457 | CGI::BusCgiPrint->new($s); | ||
| 1458 | } | ||
| 1459 | |||
| 1460 | |||
| 1461 | sub text { # Retransalte text for print->text() | ||
| 1462 | shift; join('',@_) | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | |||
| 1466 | |||
| 1467 | ####################### | ||
| 1468 | |||
| 1469 | # Autoload Launcher Object | ||
| 1470 | package CGI::BusLauncher; # Used with 'launch' | ||
| 1471 | use vars qw($AUTOLOAD); | ||
| 1472 | 1; | ||
| 1473 | |||
| 1474 | sub new { | ||
| 1475 | my $c=shift; | ||
| 1476 | my $s =[$_[0]]; | ||
| 1477 | bless $s,$c; | ||
| 1478 | } | ||
| 1479 | |||
| 1480 | sub DESTROY { | ||
| 1481 | eval {$_[0]->[0] =undef} | ||
| 1482 | } | ||
| 1483 | |||
| 1484 | sub AUTOLOAD { | ||
| 1485 | shift->[0]->launch(substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2), @_) | ||
| 1486 | } | ||
| 1487 | |||
| 1488 | |||
| 1489 | |||
| 1490 | ####################### | ||
| 1491 | |||
| 1492 | # Autoload CGI method, print it result, return self | ||
| 1493 | package CGI::BusCgiPrint; # Used with 'print' | ||
| 1494 | use vars qw($AUTOLOAD); | ||
| 1495 | 1; | ||
| 1496 | |||
| 1497 | sub new { | ||
| 1498 | my $c=shift; | ||
| 1499 | my $s =[$_[0]]; | ||
| 1500 | bless $s,$c; | ||
| 1501 | } | ||
| 1502 | |||
| 1503 | sub DESTROY { | ||
| 1504 | eval {$_[0]->[0] =undef} | ||
| 1505 | } | ||
| 1506 | |||
| 1507 | |||
| 1508 | sub httpheader { | ||
| 1509 | my $s =shift; | ||
| 1510 | $s->[0]->print($s->[0]->{-cache}->{-httpheader} ? '' | ||
| 1511 | :($s->[0]->{-cache}->{-httpheader} =$s->[0]->httpheader(@_))); | ||
| 1512 | } | ||
| 1513 | |||
| 1514 | |||
| 1515 | sub htmlstart { | ||
| 1516 | my $s =shift; | ||
| 1517 | $s->[0]->print($s->[0]->{-cache}->{-htmlstart} ? '' | ||
| 1518 | :($s->[0]->{-cache}->{-htmlstart} =$s->[0]->htmlstart(@_))); | ||
| 1519 | } | ||
| 1520 | |||
| 1521 | |||
| 1522 | sub htpgstart { | ||
| 1523 | $_[0]->httpheader($_[1]); | ||
| 1524 | $_[0]->htmlstart ($_[2]); | ||
| 1525 | $_[0]->[0]->print($_[0]->[0]->{-cache}->{-htpgstart} ? '' | ||
| 1526 | :($_[0]->[0]->{-cache}->{-htpgstart} =$_[0]->[0]->{-htpgtop}||'')) | ||
| 1527 | } | ||
| 1528 | |||
| 1529 | |||
| 1530 | sub htpfstart { | ||
| 1531 | $_[0]->htpgstart($_[1],$_[2]); | ||
| 1532 | $_[0]->[0]->print("\n" | ||
| 1533 | .((($ENV{HTTP_USER_AGENT} ||'') =~m{^[^/]+/(\d)} ? $1 >=3 : 0) | ||
| 1534 | ? $_[0]->[0]->{-cgi}->start_multipart_form({-action=>$_[0]->[0]->url_form() | ||
| 1535 | , -acceptcharset=>$_[0]->[0]->{-httpheader} ?$_[0]->[0]->{-httpheader}->{-charset} :undef | ||
| 1536 | , $_[3] ? %{$_[3]} : ()}) | ||
| 1537 | : $_[0]->[0]->{-cgi}->start_form({-action=>$_[0]->[0]->url_form() | ||
| 1538 | ,-acceptcharset=>$_[0]->[0]->{-httpheader} ?$_[0]->[0]->{-httpheader}->{-charset} :undef | ||
| 1539 | , $_[3] ? %{$_[3]} : ()}) | ||
| 1540 | ) ."\n") | ||
| 1541 | } | ||
| 1542 | |||
| 1543 | |||
| 1544 | sub br { | ||
| 1545 | $_[0]->[0]->print('<br />') | ||
| 1546 | } | ||
| 1547 | |||
| 1548 | |||
| 1549 | sub AUTOLOAD { | ||
| 1550 | my $s =shift; | ||
| 1551 | my $m =substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2); | ||
| 1552 | $s->[0]->print($s->[0]->$m(@_)); | ||
| 1553 | } | ||
| 1554 | |||
| 1555 | |||