###############################################################################
# GuardianAdmin.pm                                                            #
# $Date: 06.01.17 $                                                           #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2.7.00                                                 #
# Packaged:       June 1, 2017                                                #
# Distributed by: http://www.yabbforumsoftware.com                                    #
# =========================================================================== #
# Copyright (c) 2000-2017 YaBB (www.yabbforumsoftware.com) - All Rights Reserved.     #
# Software by:  The YaBB Development Team                                     #
#               with assistance from the YaBB community.                      #
###############################################################################
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
our $VERSION = '2.7.00';

our $guardianadminpmver  = 'YaBB 2.7.00 $Revision: 2044 $';
our @guardianadminpmmods = ();
our $guardianadminpmmods = 0;
if (@guardianadminpmmods) {
    $guardianadminpmmods = 1;
}

our ($action);
$action ||= q{};
if ( $action eq 'detailedversion' ) { return 1; }

##  languages ##
our ( %admin_img, %admin_txt, %croak, %guardian_txt );
## paths ##
our ( $adminurl, $yyhtml_root );
## settings ##
our (
    $banned_harvesters,     $banned_referers,
    $banned_requests,       $banned_strings,
    $clike_htaccess,        $clike_notify,
    $clike_on,              $disallow_proxy_htaccess,
    $disallow_proxy_notify, $disallow_proxy_on,
    $harvester_htaccess,    $harvester_notify,
    $harvester_on,          $referer_htaccess,
    $referer_notify,        $referer_on,
    $request_htaccess,      $request_notify,
    $request_on,            $script_htaccess,
    $script_notify,         $script_on,
    $string_htaccess,       $string_notify,
    $string_on,             $union_htaccess,
    $union_notify,          $union_on,
    $use_guardian,          $use_htaccess,
    $whitelist,             $yymycharset,
);
## system ##
our ( $action_area, $date, $language, $yymain, $yysetlocation, $yytitle, %FORM,
    %INFO, );

## our Mod Hook ##

load_language('Admin');
load_language('Guardian');
my $adminimages = "$yyhtml_root/Templates/Admin/default";
my $guardimg    = 'guardian.png';

sub setup_guardian {
    is_admin_or_gmod();

    ## make splits turn into linefeeds for the forms
    chomp $banned_harvesters;
    chomp $banned_referers;
    chomp $banned_requests;
    chomp $banned_strings;
    chomp $whitelist;
    $banned_harvesters =~ s/[|]/\n/gxsm;
    $banned_referers =~ s/[|]/\n/gxsm;
    $banned_requests =~ s/[|]/\n/gxsm;
    $banned_strings =~ s/[|]/\n/gxsm;
    $whitelist =~ s/[|]/\n/gxsm;
    my @access_denied = gr_update_htaccess('load');

    my $acc_denied = q{};
    foreach (@access_denied) {
        chomp;
        $acc_denied .= "$_\n";
    }

    $yymain .= qq~
<form action="$adminurl?action=setup_guardian2" method="post" onsubmit="savealert()" enctype="application/x-www-form-urlencoded" accept-charset="$yymycharset">
<div class="bordercolor rightboxdiv">
    <table class="border-space pad-cell" style="margin-bottom: .5em;">
        <tr>
            <td class="titlebg">
                <img src="$adminimages/$guardimg" alt="" /> <b>$guardian_txt{'title'}</b>
            </td>
        </tr><tr>
            <td class="windowbg2">
                <div class="pad-more">$guardian_txt{'description'}</div>
            </td>
        </tr>
    </table>
</div>
<div class="bordercolor rightboxdiv">
    <table class="border-space pad-cell" style="margin-bottom: .5em;">
        <tr>
            <td class="titlebg">
                <img src="$adminimages/$guardimg" alt="" /> <b>$guardian_txt{'general'}</b>
            </td>
        </tr><tr>
            <td class="windowbg2">
                <div class="setting-cell3">
                    <label for="use_guardian">$guardian_txt{'use_guardian'}</label>
                    <input type="checkbox" name="use_guardian" id="use_guardian" value="1"${ischecked($use_guardian)} />
                </div>
                <div class="setting-cell3">
                    <label for="use_htaccess">$guardian_txt{'use_htaccess'}
                    <input type="checkbox" name="use_htaccess" id="use_htaccess" value="1"${ischecked($use_htaccess)} onmouseup="remove_htaccess();" /><br />$guardian_txt{'use_htaccess_support'}</label>
                </div>
                <div class="setting-cell3">
                    <label for="access_denied">$guardian_txt{'htaccess_list'}</label>
                </div>
                <div class="setting-cell4">
                    <textarea cols="40" rows="8" name="access_denied" id="access_denied" style="width:98%">$acc_denied</textarea>
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="bordercolor rightboxdiv">
    <table class="border-space pad-cell" style="margin-bottom: .5em;">
        <tr>
            <td class="titlebg">
                <img src="$adminimages/$guardimg" alt="" /> <b>$guardian_txt{'proxy'}</b>
            </td>
        </tr><tr>
            <td class="windowbg2">
                <div class="setting-cell3">
                    <label for="disallow_proxy_on">$guardian_txt{'proxy_on'}</label>
                    <input type="checkbox" name="disallow_proxy_on" id="disallow_proxy_on" value="1"${ischecked($disallow_proxy_on)} />
                </div>
                <div class="setting-cell3">
                    <label for="whitelist">$guardian_txt{'white_list'}</label>
                </div>
                <div class="setting-cell4">
                    <textarea cols="40" rows="8" name="whitelist" id="whitelist" style="width:98%">$whitelist</textarea>
                </div>
                <div class="setting-cell3">
                    <label for="disallow_proxy_notify">$guardian_txt{'notify'}</label>
                    <input type="checkbox" name="disallow_proxy_notify" id="disallow_proxy_notify" value="1"${ischecked($disallow_proxy_notify)} />
                </div>
                <div class="setting-cell3">
                    <label for="disallow_proxy_htaccess">$guardian_txt{'htaccess_add'}
                    <input type="checkbox" name="disallow_proxy_htaccess" id="disallow_proxy_htaccess" value="1"${ischecked($disallow_proxy_htaccess)} /><br />$guardian_txt{'htaccess_support'}</label>
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="bordercolor rightboxdiv">
    <table class="border-space pad-cell" style="margin-bottom: .5em;">
        <tr>
            <td class="titlebg">
                <img src="$adminimages/$guardimg" alt="" /> <b>$guardian_txt{'referer'}</b>
            </td>
        </tr><tr>
            <td class="windowbg2">
                <div class="setting-cell3">
                    <label for="referer_on">$guardian_txt{'referer_on'}</label>
                    <input type="checkbox" name="referer_on" id="referer_on" value="1"${ischecked($referer_on)}} />
                </div>
                <div class="setting-cell3">
                    <label for="banned_referers">$guardian_txt{'referer_list'}</label>
                </div>
                <div class="setting-cell4">
                    <textarea cols="40" rows="8" name="banned_referers" id="banned_referers" style="width:98%">$banned_referers</textarea>
                </div>
                <div class="setting-cell3">
                    <label for="referer_notify">$guardian_txt{'notify'}</label>
                    <input type="checkbox" name="referer_notify" id="referer_notify" value="1"${ischecked($referer_notify)} />
                </div>
                <div class="setting-cell3">
                    <label for="referer_htaccess">$guardian_txt{'htaccess_add'}
                    <input type="checkbox" name="referer_htaccess" id="referer_htaccess" value="1"${ischecked($referer_htaccess)} /><br />$guardian_txt{'htaccess_support'}</label>
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="bordercolor rightboxdiv">
    <table class="border-space pad-cell" style="margin-bottom: .5em;">
        <tr>
            <td class="titlebg">
                <img src="$adminimages/$guardimg" alt="" /> <b>$guardian_txt{'harvester'}</b>
            </td>
        </tr><tr>
            <td class="windowbg2">
                <div class="setting-cell3">
                    <label for="harvester_on">$guardian_txt{'harvester_on'}</label>
                    <input type="checkbox" name="harvester_on" id="harvester_on" value="1"${ischecked($harvester_on)} />
                </div>
                <div class="setting-cell3">
                    <label for="banned_harvesters">$guardian_txt{'harvester_list'}</label>
                </div>
                <div class="setting-cell4">
                    <textarea cols="40" rows="8" name="banned_harvesters" id="banned_harvesters" style="width:98%">$banned_harvesters</textarea>
                </div>
                <div class="setting-cell3">
                    <label for="harvester_notify">$guardian_txt{'notify'}</label>
                    <input type="checkbox" name="harvester_notify" id="harvester_notify" value="1"${ischecked($harvester_notify)} />
                </div>
                <div class="setting-cell3">
                    <label for="harvester_htaccess">$guardian_txt{'htaccess_add'}
                    <input type="checkbox" name="harvester_htaccess" id="harvester_htaccess" value="1"${ischecked($harvester_htaccess)} /><br />$guardian_txt{'htaccess_support'}</label>
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="bordercolor rightboxdiv">
    <table class="border-space pad-cell" style="margin-bottom: .5em;">
        <tr>
            <td class="titlebg">
                <img src="$adminimages/$guardimg" alt="" /> <b>$guardian_txt{'request'}</b>
            </td>
        </tr><tr>
            <td class="windowbg2">
                <div class="setting-cell3">
                    <label for="request_on">$guardian_txt{'request_on'}</label>
                    <input type="checkbox" name="request_on" id="request_on" value="1"${ischecked($request_on)} />
                </div>
                <div class="setting-cell3">
                    <label for="banned_requests">$guardian_txt{'request_list'}</label>
                </div>
                <div class="setting-cell4">
                    <textarea cols="40" rows="8" name="banned_requests" id="banned_requests" style="width:98%">$banned_requests</textarea>
                </div>
                <div class="setting-cell3">
                    <label for="request_notify">$guardian_txt{'notify'}</label>
                    <input type="checkbox" name="request_notify" id="request_notify" value="1"${ischecked($request_notify)} />
                </div>
                <div class="setting-cell3">
                    <label for="request_htaccess">$guardian_txt{'htaccess_add'}
                    <input type="checkbox" name="request_htaccess" id="request_htaccess" value="1"${ischecked($request_htaccess)} /><br />$guardian_txt{'htaccess_support'}</label>
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="bordercolor rightboxdiv">
    <table class="border-space pad-cell" style="margin-bottom: .5em;">
        <tr>
            <td class="titlebg">
                <img src="$adminimages/$guardimg" alt="" /> <b>$guardian_txt{'string'}</b>
            </td>
        </tr><tr>
            <td class="windowbg2">
                <div class="setting-cell3">
                    <label for="string_on">$guardian_txt{'string_on'}</label>
                    <input type="checkbox" name="string_on" id="string_on" value="1"${ischecked($string_on)} />
                </div>
                <div class="setting-cell3">
                    <label for="banned_strings">$guardian_txt{'string_list'}</label>
                </div>
                <div class="setting-cell4">
                    <textarea cols="40" rows="8" name="banned_strings" id="banned_strings" style="width:98%">$banned_strings</textarea>
                </div>
                <div class="setting-cell3">
                    <label for="string_notify">$guardian_txt{'notify'}</label>
                    <input type="checkbox" name="string_notify" id="string_notify" value="1"${ischecked($string_notify)} />
                </div>
                <div class="setting-cell3">
                    <label for="string_htaccess">$guardian_txt{'htaccess_add'}
                    <input type="checkbox" name="string_htaccess" id="string_htaccess" value="1"${ischecked($string_htaccess)} /><br />$guardian_txt{'htaccess_support'}</label>
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="bordercolor rightboxdiv">
    <table class="border-space pad-cell" style="margin-bottom: .5em;">
        <tr>
            <td class="titlebg">
                <img src="$adminimages/$guardimg" alt="" /> <b>$guardian_txt{'script'}</b>
            </td>
        </tr><tr>
            <td class="windowbg2">
                <div class="setting-cell3">
                    <label for="script_on">$guardian_txt{'script_on'}</label>
                    <input type="checkbox" name="script_on" id="script_on" value="1"${ischecked($script_on)} />
                </div>
                <div class="setting-cell3">
                    <label for="script_notify">$guardian_txt{'notify'}</label>
                    <input type="checkbox" name="script_notify" id="script_notify" value="1"${ischecked($script_notify)} />
                </div>
                <div class="setting-cell3">
                    <label for="script_htaccess">$guardian_txt{'htaccess_add'}
                    <input type="checkbox" name="script_htaccess" id="script_htaccess" value="1"${ischecked($script_htaccess)} /><br />$guardian_txt{'htaccess_support'}</label>
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="bordercolor rightboxdiv">
    <table class="border-space pad-cell" style="margin-bottom: .5em;">
        <tr>
            <td class="titlebg">
                <img src="$adminimages/$guardimg" alt="" /> <b>$guardian_txt{'union'}</b>
            </td>
        </tr><tr>
            <td class="windowbg2">
                <div class="setting-cell3">
                    <label for="union_on">$guardian_txt{'union_on'}</label>
                    <input type="checkbox" name="union_on" id="union_on" value="1"${ischecked($union_on)} />
                </div>
                <div class="setting-cell3">
                    <label for="union_notify">$guardian_txt{'notify'}</label>
                    <input type="checkbox" name="union_notify" id="union_notify" value="1"${ischecked($union_notify)} />
                </div>
                <div class="setting-cell3">
                    <label for="union_htaccess">$guardian_txt{'htaccess_add'}
                    <input type="checkbox" name="union_htaccess" id="union_htaccess" value="1"${ischecked($union_htaccess)} /><br />$guardian_txt{'htaccess_support'}</label>
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="bordercolor rightboxdiv">
    <table class="border-space pad-cell" style="margin-bottom: .5em;">
        <tr>
            <td class="titlebg">
                <img src="$adminimages/$guardimg" alt="" /> <b>$guardian_txt{'clike'}</b>
            </td>
        </tr><tr>
            <td class="windowbg2">
                <div class="setting-cell3">
                    <label for="clike_on">$guardian_txt{'clike_on'}</label>
                    <input type="checkbox" name="clike_on" id="clike_on" value="1"${ischecked($clike_on)} />
                </div>
                <div class="setting-cell3">
                    <label for="clike_notify">$guardian_txt{'notify'}</label>
                    <input type="checkbox" name="clike_notify" id="clike_notify" value="1"${ischecked($clike_notify)} />
                </div>
                <div class="setting-cell3">
                    <label for="clike_htaccess">$guardian_txt{'htaccess_add'}
                    <input type="checkbox" name="clike_htaccess" id="clike_htaccess" value="1"${ischecked($clike_htaccess)} /><br />$guardian_txt{'htaccess_support'}</label>
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="bordercolor rightboxdiv">
<table class="border-space pad-cell">
    <tr>
        <th class="titlebg">$admin_img{'prefimg'} $admin_txt{'10'}</th>
    </tr><tr>
        <td class="catbg center">
            <input type="submit" value="$guardian_txt{'save'}" class="button" />
        </td>
    </tr>
</table>
</div>
</form>
~;
    $yytitle     = $guardian_txt{'setup'};
    $action_area = 'setup_guardian';
    admintemplate();
    return;
}

sub setup_guardian2 {
    is_admin_or_gmod();
    my @onoff = qw{
      use_guardian use_htaccess disallow_proxy_on disallow_proxy_htaccess referer_on referer_htaccess harvester_on harvester_htaccess request_on request_htaccess string_on string_htaccess union_on union_htaccess clike_on clike_htaccess script_on script_htaccess disallow_proxy_notify referer_notify harvester_notify request_notify string_notify union_notify clike_notify script_notify};

    # Set as 0 or 1 if box was checked or not
    foreach my $chk (@onoff) {
        my $fi = lc $chk;
        {
            no strict qw(refs);
            if   ( !$FORM{$fi} ) { ${$chk} = 0; }
            else                 { ${$chk} = $FORM{$fi} ? 1 : 0; }
        }
    }

    $banned_harvesters = $FORM{'banned_harvesters'};
    $banned_referers   = $FORM{'banned_referers'};
    $banned_requests   = $FORM{'banned_requests'};
    $banned_strings    = $FORM{'banned_strings'};
    my $access_denied = $FORM{'access_denied'};
    $whitelist = $FORM{'whitelist'};
    chomp $banned_harvesters;
    chomp $banned_referers;
    chomp $banned_requests;
    chomp $banned_strings;
    chomp $whitelist;
    $banned_harvesters =~ s/\r//gxsm;
    $banned_harvesters =~ s/\n/|/gxsm;
    $banned_referers =~ s/\r//gxsm;
    $banned_referers =~ s/\n/|/gxsm;
    $banned_requests =~ s/\r//gxsm;
    $banned_requests =~ s/\n/|/gxsm;
    $banned_strings =~ s/\r//gxsm;
    $banned_strings =~ s/\n/|/gxsm;
    $whitelist =~ s/\r//gxsm;
    $whitelist =~ s/\n/|/gxsm;

    # We shouldn't let them block POST and GET since it'll mess things up.
    $banned_requests =~ s/post//igxsm;
    $banned_requests =~ s/get//igxsm;
    $banned_requests =~ s/[|]+/|/igxsm;    # Clean up extra pipes
    my @whitelist = split /[|]/xsm, $whitelist;
    require Admin::NewSettings;
    save_settings_to('Settings.pm');
    my @access_denied = split /[\r\n]/xsm, $access_denied;
    gr_update_htaccess( 'save', @access_denied );

    $yysetlocation = qq~$adminurl?action=setup_guardian~;
    redirectexit();
    return;
}

sub gr_update_htaccess {
    my ( $act, @values ) = @_;
    my ( @denies, @htout, @htlines );
    if ( !$act ) { return 0; }
    if ( -e '.htaccess' ) {
        open my $HTA, '<', '.htaccess' or croak "$croak{'open'} HTA";
        @htlines = <$HTA>;
        close $HTA or croak "$croak{'close'} HTA";
    }

# header to determine only who has access to the main script, not the admin script
    my $htheader = q~<Files YaBB*>~;
    my $htfooter = q~</Files>~;
    foreach my $chk (@htlines) {
        chomp $chk;
        if ( $chk =~ s/\QDeny from \E//gxsm ) {
            push @denies, $chk;
        }
        elsif ($chk ne $htheader
            && $chk !~ m/\x23/xsm
            && $chk ne q{}
            && $chk ne $htfooter )
        {
            push @htout, "$chk\n";
        }
    }
    if ( $act eq 'save' && @values ) {
        my $erdate = ctbtime();
        my $prhta  = '# Last modified by The Guardian: ' . $erdate . " #\n\n";
        $prhta .= join q{}, @htout;
        $prhta .= "\n$htheader\n";
        my %have = ();
        my @sort = grep { !$have{$_}++ } @values;
        require Admin::AdminSubs;
        foreach my $ln (@sort) {

            if ($ln) {
                chomp $ln;
                my $bad = chk_ip($ln);
                if ($bad) {
                    if (   $ln !~ /[\w\-.*]/xsm
                        || $ln =~ /^\-/xsm
                        || $ln =~ /(?:\-.)/xsm )
                    {
                        fatal_error( 'illegal', $ln );
                    }
                    else { $prhta .= "Deny from $ln\n"; }
                }
                else { $prhta .= "Deny from $ln\n"; }
            }
        }
        $prhta .= "$htfooter\n";

        open my $HTA, '>', '.htaccess' or croak "$croak{'open'} HTA";
        print {$HTA} $prhta or croak "$croak{'print'} HTA";
        close $HTA or croak "$croak{'close'} HTA";
        return;
    }
    else {
        return @denies;
    }
}

sub guardian_block {
    is_admin_or_gmod();

    if ( $use_guardian && $use_htaccess ) {
        my $block_ip = $INFO{'ip'};
        update_htaccess( 'add', $block_ip );
        $yysetlocation = qq~$adminurl?action=$INFO{'return'}~;
        redirectexit();
    }
    return;
}

1;
