#!/usr/bin/perl
# cPanel 12 - LeechProtect.pm                   Copyright(c) 1999-2007 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cpanel license. Unauthorized copying is prohibited

package Cpanel::LeechProtect;

use strict;
use Carp ();
use Cpanel ();
use Cpanel::SafeDir ();
use Cpanel::Lang ();

our $VERSION = '1.1';

require 5.004;

sub LeechProtect_init {
	return(1);
}

sub LeechProtect_setup {
   if ($Cpanel::flags !~ /pro/i) {
      print "Sorry, this copy of cPanel Pro is missing the license file!\n";
      return();
   }

   my($dir,$item,$type) = @_;

   $dir =~ s/\.\.//g;
   $dir = Cpanel::SafeDir::safedir($dir);

   open(LPC,"<","${dir}/.leechprotect-conf");
   while(<LPC>) {
      chomp();
      if (/^${item}=(\S*)/) {
         close(LPC);
         if ($type eq "checkbox") {
            if ($1 eq "1" || $1 ne "") {
               print "checked";
            }
            return();
         } else {
            print "$1";
            return();
         }
      }
   }
   close(LPC);
}

sub LeechProtect_init {
   return(1);
}

sub LeechProtect_status {
   if ($Cpanel::flags !~ /pro/i) {
      print "Sorry, this copy of cPanel Pro is missing the license file!\n";
      return();
   }

   my ($dir) = @_;
   $dir =~ s/\.\.//g;
   $dir = Cpanel::SafeDir::safedir($dir);

   open(HC,"<","${dir}/.htaccess");
   while(<HC>) {
      if (/LeechProtect/) { print "enabled"; return(); }
   }
   close(HC);
   print "disabled";
}

sub LeechProtect_enable {
   if ($Cpanel::flags !~ /pro/i) {
      print "Sorry, this copy of cPanel Pro is missing the license file!\n";
      return();
   }

   if ($Cpanel::CPDATA{'DEMO'} eq "1") {
      print "Sorry this feature is disabled in demo mode";
      return();
   }

   my ($dir,$numhits,$badurl,$email,$killcompro) = @_;

   $numhits=int($numhits);
   if ($numhits < 1) { $numhits = 4; }

   my($hasrengine) = 0;
   my($skipline) = 0;
   my(@HC);

   $dir =~ s/\.\.//g;
   
   $dir = Cpanel::SafeDir::safedir($dir);

   open(LPC,">","${dir}/.leechprotect-conf");
   print LPC "email=${email}\n";
   print LPC "kill=${killcompro}\n";
   print LPC "url=${badurl}\n";
   print LPC "numhits=${numhits}\n";
   close(LPC);


   open(HC,"<","${dir}/.htaccess");
   while(<HC>) {
      push(@HC,$_);
      if (/^[\s\t]*RewriteEngine on/i) { $hasrengine = 1; }
   }
   close(HC);

   open(HC,">","${dir}/.htaccess") || print "<b>Error: while opening htaccess</b>\n";

   if (!${hasrengine}) {
      print HC "\nRewriteEngine on\n";
   }
   foreach (@HC) {
      if ($skipline)  { $skipline = 0; next; }
      if (/LeechProtect/) { $skipline = 1; next; }
      print HC;
   }

   print HC "\n" . 'RewriteCond ${LeechProtect:' . ${dir} .
   ':%{REMOTE_USER}:%{REMOTE_ADDR}:' . ${numhits} . "} leech\n";
   print HC "RewriteRule .* ${badurl}\n";
   close(HC);


}

sub LeechProtect_disable {
   if ($Cpanel::CPDATA{'DEMO'} eq "1") {
      print "Sorry this feature is disabled in demo mode";
      return();
   }

   my ($dir) = @_;
   $dir =~ s/\.\.//g;
   $dir = Cpanel::SafeDir::safedir($dir);



   my($hasrengine) = 0;
   my($hasr) = 0; #has other rewrite rules
   my($skipline) = 0;
   my(@HC);

   open(HC,"<","${dir}/.htaccess");
   while(<HC>) {
      if ($skipline)  { $skipline = 0; next; }
      if (/LeechProtect/) { $skipline = 1; next; }
      push(@HC,$_);
      if (/^[\s\t]*RewriteEngine on/i) { $hasrengine = 1; }
      elsif (/^[\s\t]*Rewrite/i) { $hasr = 1; }
   }
   close(HC);

   open(HC,">","${dir}/.htaccess") || print "<b>Error: while opening htaccess</b>\n";
   foreach (@HC) {
      if (/^[\s\t]*RewriteEngine on/i && (!$hasr)) { next; }
      print HC;
   }
   close(HC);
}

sub LeechProtect_showpasswdfile {
   if ($Cpanel::flags !~ /pro/i) {
      print "Sorry, this copy of cPanel Pro is missing the license file!\n";
      return();
   }

   my($dir) =  @_;
   my($lang) = $Cpanel::CPDATA{'LANG'};
   my($tdir);
   $dir =~ s/^$Cpanel::homedir\/public_html//g;
   $dir =~ s/^\/public_html\/$//g;
   $dir =~ s/\.\.//g;
   $dir =~ s/^\///g;
   $dir = "$Cpanel::homedir/public_html/$dir";
   if (! -e "$dir") {
      print "$Cpanel::Lang::LANG{$lang}{'cpanel-findfolder'} [$dir]\n";
      exit;
   }
   chdir("$dir");
   $dir =~ s/\/$//g;
   $tdir = $dir;
   $tdir =~ s/^$Cpanel::homedir//g;
   $tdir =~ s/^\/public_html//g;
   $tdir =~ s/^\///g;
   $tdir =~ s/\.\.//g;
   $tdir =~ s/^\///g;


   print "$Cpanel::homedir/.htpasswds/$tdir/passwd";
}

sub LeechProtect_manageusers {
    if ($Cpanel::flags !~ /pro/i) {
        print "Sorry, this copy of cPanel Pro is missing the license file!\n";
        return();
    }

    my($dir) =  @_;
    my $passwdfile = &LeechProtect_getrealpasswdfile("$dir");
    my($lang) = $Cpanel::CPDATA{'LANG'};
    my($tdir);
    $dir =~ s/^$Cpanel::homedir\/public_html//g;
    $dir =~ s/^\/public_html\/$//g;
    $dir =~ s/\.\.//g;
    $dir =~ s/^\///g;
    $dir = "$Cpanel::homedir/public_html/$dir";
    if (! -e "$dir") {
        print "$Cpanel::Lang::LANG{$lang}{'cpanel-findfolder'} [$dir]\n";
        exit;
    }
    chdir("$dir");
    $dir =~ s/\/$//g;
    $tdir = $dir;
    $tdir =~ s/^$Cpanel::homedir//g;
    $tdir =~ s/^\/public_html//g;
    $tdir =~ s/^\///g;
    $tdir =~ s/\.\.//g;
    $tdir =~ s/^\///g;

    my $defaultpasswdfile = "$Cpanel::homedir/.htpasswds/$tdir/passwd";
    if ($defaultpasswdfile eq $passwdfile) {
        print "<div align=\"center\">\n";
        print "<p>If you are using third party software to add/remove users, ";
        print "you should have it use the password file:<br/>\n";
        print "<i>$defaultpasswdfile</i></div></p>\n";
        print "<form action=\"../htaccess/dohtaccess.html\">\n";
        print "<input type=hidden name=dir value=\"$dir\">\n";
        print "<div align=\"center\"><input type=submit value=\"Manage Users\"></div>\n";
        print "</form>\n";
    }
    elsif ($passwdfile eq "") {
        print "<div align=\"center\">\n";
        print "<p><b>A password file was not specified in the directory's .htaccess file. ";
        print "<br/>This feature is not available.</b></p>\n";
        print "<p>Please enable password protection of the directory<br/>\n";
        print "in order to activate this feature</p>\n";
        print "</div>\n";
    }
    else {
        print "<div align=\"center\">\n";
        print "<p><b>You have specified a non-default password file in the directory's .htaccess file. ";
        print "This feature is not available.</b></p>\n";
        print "<p>Please move the password file to: <i>$defaultpasswdfile</i><br/>\n";
        print "Then edit the .htaccess file in order to activate this feature</p>\n";
        print "</div>\n";
    }

}

sub LeechProtect_getrealpasswdfile {
    my ($dir) = @_;
    my $passwdfile = '';
    open(HC,"<","${dir}/.htaccess");
    while(<HC>) {
        if (/AuthUserFile \"([^\"]+)\"/) {
            $passwdfile = $1;
        }
    }
    close(HC);
    return $passwdfile;
}



1;
