############################################################################### # Subs.pm # # $Date: 06.01.16 $ # ############################################################################### # YaBB: Yet another Bulletin Board # # Open-Source Community Software for Webmasters # # Version: YaBB 2.6.13 # # Packaged: June 1, 2016 # # Distributed by: http://www.yabbforum.com # # =========================================================================== # # Copyright (c) 2000-2016 YaBB (www.yabbforum.com) - All Rights Reserved. # # Software by: The YaBB Development Team # # with assistance from the YaBB community. # ############################################################################### # use strict; # use warnings; no warnings qw(uninitialized once); use CGI::Carp qw(fatalsToBrowser); use URI::Escape; use English qw(-no_match_vars); our $VERSION = '2.6.13'; $subspmver = 'YaBB 2.6.13 $Revision$'; use subs 'exit'; $yymain = q{}; $yyjavascript = q{}; $langopt = q{}; # set line wrap limit in Display. $linewrap = 80; $newswrap = 0; # get the current date/time $date = int( time() + $timecorrection ); # check if browser accepts encoded output $gzaccept = $ENV{'HTTP_ACCEPT_ENCODING'} =~ /\bgzip\b/sm || $gzforce; # parse the query string readform(); $uid = substr $date, length($date) - 3, 3; $session_id = $cookiesession_name; $randaction = substr $date, 0, length($date) - 2; $user_ip = $ENV{'REMOTE_ADDR'}; if ( $user_ip eq '127.0.0.1' ) { if ( $ENV{'HTTP_CLIENT_IP'} && $ENV{'HTTP_CLIENT_IP'} ne '127.0.0.1' ) { $user_ip = $ENV{'HTTP_CLIENT_IP'}; } elsif ( $ENV{'X_CLIENT_IP'} && $ENV{'X_CLIENT_IP'} ne '127.0.0.1' ) { $user_ip = $ENV{'X_CLIENT_IP'}; } elsif ($ENV{'HTTP_X_FORWARDED_FOR'} && $ENV{'HTTP_X_FORWARDED_FOR'} ne '127.0.0.1' ) { $user_ip = $ENV{'HTTP_X_FORWARDED_FOR'}; } } if ( -e "$yyexec.cgi" ) { $yyext = 'cgi'; } else { $yyext = 'pl'; } if ( -e 'AdminIndex.cgi' ) { $yyaext = 'cgi'; } else { $yyaext = 'pl'; } sub automaintenance { my ( $maction, $mreason ) = @_; if ( lc($maction) eq 'on' ) { fopen( MAINT, ">$vardir/maintenance.lock" ); print {MAINT} qq~$maintxt{'maint'}\n~ or croak qq~$maintxt{'maint'}~; fclose(MAINT); if ( $mreason eq 'low_disk' ) { LoadLanguage('Error'); alertbox( $error_txt{'low_diskspace'} ); } if ( !$maintenance ) { $maintenance = 2; } } elsif ( lc($maction) eq 'off' ) { unlink "$vardir/maintenance.lock" or fatal_error( 'cannot_open_dir', "$vardir/maintenance.lock" ); if ( $maintenance == 2 ) { $maintenance = 0; } } return; } sub getnewid { my $newid = $date; while ( -e "$datadir/$newid.txt" ) { ++$newid; } return $newid; } sub undupe { my (@indup) = @_; my ( @out, $duped, ); foreach my $check (@indup) { $duped = 0; foreach (@out) { if ( $_ eq $check ) { $duped = 1; last; } } if ( !$duped ) { push @out, $check; } } return @out; } sub exit { my ($inexit) = @_; my $OUTPUT_AUTOFLUSH = 1; my $OUTPUT_RECORD_SEPARATOR = q{}; print q{}; if ($child_pid) { wait; } CORE::exit( $inexit || 0 ); return; } sub print_output_header { if ($header_already_printed) { return; } $yyxml_lang = $abbr_lang; $header_already_printed = 1; $headerstatus ||= '200 OK'; $contenttype ||= 'text/html'; my $ret = $yyIIS ? "HTTP/1.0 $headerstatus\n" : "Status: $headerstatus\n"; foreach ( $yySetCookies1, $yySetCookies2, $yySetCookies3, @otherCookies ) { if ($_) { $ret .= "Set-Cookie: $_\n"; } } if ( !$no_error_page ) { if ($yySetLocation) { $ret .= "Location: $yySetLocation"; } else { if ( !$cachebehaviour ) { $ret .= "Cache-Control: no-cache, must-revalidate\nPragma: no-cache\n"; } if ($ETag) { $ret .= "ETag: \"$ETag\"\n"; } if ($LastModified) { $ret .= "Last-Modified: $LastModified\n"; } if ( $gzcomp && $gzaccept ) { $ret .= "Content-Encoding: gzip\n"; } $ret .= "Content-Type: $contenttype"; if ($yycharset) {$yymycharset = $yycharset;} if ($yymycharset) { $ret .= "; charset=$yymycharset"; } } } print $ret . "\r\n\r\n" or croak "$croak{'print'} ret"; return; } sub print_HTML_output_and_finish { if ( $gzcomp && $gzaccept ) { my $filehandle_exists = fileno GZIP; if ( $gzcomp == 1 || $filehandle_exists ) { $OUTPUT_AUTOFLUSH = 1; if ( !$filehandle_exists ) { open GZIP, '| gzip -f' or croak "$croak{'open'} GZIP"; } print {GZIP} $output or croak "$croak{'print'} GZIP"; close GZIP or croak "$croak{'close'}"; } else { require Compress::Zlib; binmode STDOUT; print Compress::Zlib::memGzip($output) or croak "$croak{'print'} ZLib"; } } else { print $output; # or croak "$croak{'print'} output"; } exit; } sub write_cookie { my %params = @_; if ( $params{'-expires'} =~ /\+(\d+)m/xsm ) { my ( $sec, $min, $hour, $mday, $mon, $year, $wday ) = gmtime( $date + $1 * 60 ); $year += 1900; my @mos = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my @dys = qw( Sun Mon Tue Wed Thu Fri Sat ); $mon = $mos[$mon]; $wday = $dys[$wday]; $params{'-expires'} = sprintf '%s, %02i-%s-%04i %02i:%02i:%02i GMT', $wday, $mday, $mon, $year, $hour, $min, $sec; } if ( $params{'-path'} ) { $params{'-path'} = " path=$params{'-path'};"; } if ( $params{'-expires'} ) { $params{'-expires'} = " expires=$params{'-expires'};"; } return "$params{'-name'}=$params{'-value'};$params{'-path'}$params{'-expires'}"; } sub redirectexit { $headerstatus = '302 Moved Temporarily'; print_output_header(); exit; } sub redirectmove { require Sources::MessageIndex; MessageIndex(); return; } sub redirectinternal { if ($currentboard) { if ( $INFO{'num'} ) { require Sources::Display; Display(); } else { require Sources::MessageIndex; MessageIndex(); } } else { require Sources::BoardIndex; BoardIndex(); } return; } sub ImgLoc { @img = @_; if ( exists $img_locs{ $img[0] } ) { $img_locs{ $img[0] } = $img_locs{ $img[0] }; } elsif ( -e "$htmldir/Templates/Forum/$useimages/$img[0]" ) { $img_locs{ $img[0] } = qq~$imagesdir/$img[0]~; } else { $img_locs{ $img[0] } = qq~$defaultimagesdir/$img[0]~; } return $img_locs{ $img[0] }; } sub template { print_output_header(); if ( $yytitle ne $maintxt{'error_description'} ) { if ( ( !$iamguest || ( $iamguest && $guestaccess == 1 ) ) && !$maintenance ) { $yyforumjump = jumpto(); } else { $yyforumjump = ' '; } } $yyposition = $yytitle; $yytitle = "$mbname - $yytitle"; $yyimages = $imagesdir; $yydefaultimages = $defaultimagesdir; $yysyntax_js = q{}; $yygreyboxstyle = q{}; $yygrayscript = q{}; if ( $INFO{'num'} || $action eq 'post' || $action eq 'modify' || $action eq 'preview' || $action eq 'search2' || $action eq 'imshow' || $action eq 'imsend' || $action eq 'myviewprofile' || $action eq 'eventcal' || $action eq 'help' || $action eq 'recenttopics' || $action eq 'recent' || $action eq 'usersrecentposts' || $action eq 'myusersrecentposts' ) { $yysyntax_js = qq~ ~; $yyjsstyle = qq~\n~; $yyhigh = q~~; if ($img_greybox) { $yygreyboxstyle = qq~\n~; $yygrayscript = qq~ ~; } } $yystyle = qq~\n~; $yystyle =~ s/$usestyle\///gxsm; $yystyle .= $yyjsstyle; $yystyle .= $yygreyboxstyle; $yystyle .= $yyinlinestyle; if ( $action eq 'register' || $action eq 'guestpm' || $action eq 'modalert' || $action eq 'post' || $action eq 'imsend' || ( $action eq 'eventcal' && $INFO{'addnew'} == 1 ) ) { $yystyle .= ''; } # Carsten's 'backtotop'; if ( !$yynavback ) { $yynavback .= q~ ~; } $yynavback .= qq~$tabsep $img_txt{'102'}   $tabsep~; if ( !$usehead ) { $usehead = q~default~; } $yytemplate = "$templatesdir/$usehead/$usehead.html"; fopen( TEMPLATE, $yytemplate ) or croak("$maintxt{'23'}: $yytemplate"); @whole_file =