<?php 
/*#################################################################*\
|# Licence Number 0DT7-0122-0NW1-0210
|# -------------------------------------------------------------   #|
|# Copyright (c)2010 PHP Link Directory.                           #|
|# http://www.phplinkdirectory.com                                 #|
\*#################################################################*/
	
/**
# ################################################################################
# Project:   PHP Link Directory
#
# **********************************************************************
# Copyright (C) 2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
#
# This software is for use only to those who have purchased a license.
# A license must be purchased for EACH installation of the software.
#
# By using the software you agree to the terms:
#
#    - You may not redistribute, sell or otherwise share this software
#      in whole or in part without the consent of the the ownership
#      of PHP Link Directory. Please contact david@david-duval.com
#      if you need more information.
#
#    - You agree to retain a link back to http://www.phplinkdirectory.com/
#      on all pages of your directory in you purchased the $25 version
#      of the software.
#
# License holders are entitled to upgrades to the 3.4 branch of the software
# as they are made available at http://www.phplinkdirectory.com/
#
# In some cases, license holders may be required to agree to changes
# in the software license before receiving updates to the software.
# **********************************************************************
#
# For questions, help, comments, discussion, etc., please join the
# PHP Link Directory Forum http://www.phplinkdirectory.com/forum/
#
# @link           http://www.phplinkdirectory.com/
# @copyright      2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
# @projectManager David DuVal <david@david-duval.com>
# @package        PHPLinkDirectory
# @version        4.0.0
# ################################################################################
*/

require_once 'init.php';

if (empty ($_POST['submit']))
{
   SmartyValidate :: connect($tpl);
   SmartyValidate :: register_form('conf_bancontrol', true);
   SmartyValidate :: register_criteria('isIP'              , 'validate_banIp'        , 'conf_bancontrol');
   SmartyValidate :: register_criteria('isValueUnique'     , 'validate_unique'       , 'conf_bancontrol');
   SmartyValidate :: register_criteria('isNotRestrictedIP' , 'validate_restricted_ip', 'conf_bancontrol');
   SmartyValidate :: register_criteria('isDomain'          , 'validate_toBanDomain'  , 'conf_bancontrol');
   SmartyValidate :: register_criteria('isValidBanEmail'   , 'validate_toBanEmail'   , 'conf_bancontrol');

   SmartyValidate :: register_validator('v_BAN_IP'           , 'BAN_IP'            , 'isIP'             , true , false, 'trim', 'conf_bancontrol');

   SmartyValidate :: register_validator('v_BAN_IP_RESTRICTED', 'BAN_IP'            , 'isNotRestrictedIP', false, false,  null , 'conf_bancontrol');

   SmartyValidate :: register_validator('v_BAN_IP_UNIQUE'    , 'BAN_IP:banlist'    , 'isValueUnique'    , false, false,  null , 'conf_bancontrol');

   SmartyValidate :: register_validator('v_UNBAN_IP'         , 'UNBAN_IP'          , 'notEmpty'         , true , false, 'trim', 'conf_bancontrol');

   SmartyValidate :: register_validator('v_BAN_DOMAIN'       , 'BAN_DOMAIN'        , 'isDomain'         , true , false, 'trim', 'conf_bancontrol');
   SmartyValidate :: register_validator('v_BAN_DOMAIN_UNIQUE', 'BAN_DOMAIN:banlist', 'isValueUnique'    , false, false,  null , 'conf_bancontrol');

   SmartyValidate :: register_validator('v_UNBAN_DOMAIN'     , 'UNBAN_DOMAIN'      , 'notEmpty'         , true , false, 'trim', 'conf_bancontrol');

   SmartyValidate :: register_validator('v_BAN_EMAIL'       , 'BAN_EMAIL'          , 'isValidBanEmail'    , true , false, 'trim', 'conf_bancontrol');
   SmartyValidate :: register_validator('v_BAN_EMAIL_UNIQUE', 'BAN_EMAIL:banlist'  , 'isValueUnique'    , false, false,  null , 'conf_bancontrol');

   SmartyValidate :: register_validator('v_UNBAN_EMAIL'     , 'UNBAN_EMAIL'        , 'notEmpty'         , true , false, 'trim', 'conf_bancontrol');

   SmartyValidate :: register_validator('v_BAN_WORD_UNIQUE' , 'BAN_WORD:banlist'   , 'isValueUnique'    , false, false,  null , 'conf_bancontrol');

   SmartyValidate :: register_validator('v_UNBAN_WORD'      , 'UNBAN_WORD'         , 'notEmpty'         , true , false, 'trim', 'conf_bancontrol');
}
else
{
   SmartyValidate :: connect($tpl);
   if (SmartyValidate :: is_valid($_POST, 'conf_bancontrol'))
   {
      $confirmation = 0;

      if (!empty ($_POST['BAN_IP']))
      {
          if ($db->AutoExecute($tables['banlist']['name'], array ('BAN_IP' => $_POST['BAN_IP']), 'INSERT', false))
               $confirmation++;
      }

      if ($_POST['UNBAN_IP'] > 0)
      {
         if ($db->Execute("DELETE FROM `{$tables['banlist']['name']}` WHERE `ID` = ".$db->qstr($_POST['UNBAN_IP'])." LIMIT 1"))
            $confirmation++;
      }

      if (!empty ($_POST['BAN_DOMAIN']))
      {
         if ($db->AutoExecute($tables['banlist']['name'], array ('BAN_DOMAIN' => $_POST['BAN_DOMAIN']), 'INSERT', false))
               $confirmation++;
      }

      if ($_POST['UNBAN_DOMAIN'] > 0)
      {
         if ($db->Execute("DELETE FROM `{$tables['banlist']['name']}` WHERE `ID` = ".$db->qstr($_POST['UNBAN_DOMAIN'])." LIMIT 1"))
            $confirmation++;
      }

      if (!empty ($_POST['BAN_EMAIL']))
      {
          if ($db->AutoExecute($tables['banlist']['name'], array ('BAN_EMAIL' => $_POST['BAN_EMAIL']), 'INSERT', false))
               $confirmation++;
      }

      if ($_POST['UNBAN_EMAIL'] > 0)
      {
         if ($db->Execute("DELETE FROM `{$tables['banlist']['name']}` WHERE `ID` = ".$db->qstr($_POST['UNBAN_EMAIL'])." LIMIT 1"))
            $confirmation++;
      }

      if (!empty ($_POST['BAN_WORD']))
      {
         //Clean whitespace
         $_POST['BAN_WORD'] = strip_white_space($_POST['BAN_WORD']);
         $_POST['BAN_WORD'] = strtolower ($_POST['BAN_WORD']);

         //Make an array of words to ban
         $banWordsArray = explode (' ', $_POST['BAN_WORD']);

         //Keep each word only once
         $banWordsArray = array_unique ($banWordsArray);

         if (is_array ($banWordsArray) && !empty ($banWordsArray))
         {
            foreach ($banWordsArray as $key => $word)
            {
               //Count entries of same word
               $count = $db->GetOne("SELECT COUNT(*) FROM `{$tables['banlist']['name']}` WHERE `BAN_WORD` = ".$db->qstr($word));

               //Skip word if available
               if ($count < 1)
               {
                  if ($db->AutoExecute($tables['banlist']['name'], array ('BAN_WORD' => $word), 'INSERT', false))
                     $confirmation++;
               }

               //Free memory
               unset ($banWordsArray[$key], $word, $count);
            }
         }

      }

      if ($_POST['UNBAN_WORD'] > 0)
      {
         if ($db->Execute("DELETE FROM `{$tables['banlist']['name']}` WHERE `ID` = ".$db->qstr($_POST['UNBAN_WORD'])." LIMIT 1"))
            $confirmation++;
      }

      if ($confirmation > 0)
      {
         $tpl->assign('posted', true);
         unset ($_POST);
      }
   }
}

//Get all banned IP addresses out of DB and store it into an array
function list_IP()
{
   global $db, $tables;

   $result = $db->GetAssoc("SELECT `ID`, `BAN_IP` FROM `{$tables['banlist']['name']}` WHERE `BAN_IP` IS NOT NULL");

   if (!is_array ($result))
      $result = array ();

   $result[0] = _L('Select an IP to unban');

   ksort($result);

   return $result;
}

//Get all banned domains out of DB and store it into an array
function list_DOMAIN()
{
   global $db, $tables;

   $result = $db->GetAssoc("SELECT `ID`, `BAN_DOMAIN` FROM `{$tables['banlist']['name']}` WHERE `BAN_DOMAIN` IS NOT NULL");

   if (!is_array ($result))
      $result = array ();

   $result[0] = _L('Select a domain to unban');

   ksort($result);

   return $result;
}

//Get all banned email addresses out of DB and store it into an array
function list_EMAIL()
{
   global $db, $tables;

   $result = $db->GetAssoc("SELECT `ID`, `BAN_EMAIL` FROM `{$tables['banlist']['name']}` WHERE `BAN_EMAIL` IS NOT NULL");

   if (!is_array ($result))
      $result = array ();

   $result[0] = _L('Select email address to unban');

   ksort($result);

   return $result;
}

function list_WORD()
{
   global $db, $tables;

   $result = $db->GetAssoc("SELECT `ID`, `BAN_WORD` FROM `{$tables['banlist']['name']}` WHERE `BAN_WORD` IS NOT NULL");

   if (!is_array ($result))
      $result = array ();

   $result[0] = _L('Select a word to unban');

   ksort($result);

   return $result;
}

$wildcardIpTxt = _L('You may use a * as a wildcard for increased flexibility. For example, if you enter 123.123.12*, many IPs will be banned including: 123.123.12.9, 123.123.128.200, 123.123.121.189. To use a wildcard you have to enter at least the first two blocks and the dot, for example: 123.123.*');
$wildcardIpTxt = str_replace ('*', '<span class="important">*</span>', $wildcardIpTxt);
$tpl->assign('wildcardIpTxt', $wildcardIpTxt);

$wildcardDomainTxt = _L('You may use a * as a wildcard for increased flexibility. For example if you enter *.domain.com, all subdomains are banned. Another option is to ban *domain.com, this will ban domain.com, all it\'s subdomains like subdomain.domain.com but also unwanted domains like mydomain.com. We strongly suggest to ban in this case the top level domain (domain.com) and all it\'s subdomains (*.domain.com) separately.');
$wildcardDomainTxt = str_replace ('*', '<span class="important">*</span>', $wildcardDomainTxt);
$tpl->assign('wildcardDomainTxt', $wildcardDomainTxt);

$wildcardEmailTxt = str_replace (array ('#FULL_EMAIL_EXAMPLE#', '#PARTIAL_EMAIL_EXAMPLE#'), array ('<code>email@domain.com</code>', '<code>@domain.com</code>'), _L('You may ban any email addresses from registering and submitting. Ban the complete email address (#FULL_EMAIL_EXAMPLE#), or ban a partial email address (#PARTIAL_EMAIL_EXAMPLE#).'));
$tpl->assign('wildcardEmailTxt', $wildcardEmailTxt);

$tpl->assign($_POST);

$tpl->assign('IP_List', list_IP());
$tpl->assign('Domain_List', list_DOMAIN());
$tpl->assign('Email_List', list_EMAIL());
$tpl->assign('WORD_List', list_WORD());

$content = $tpl->fetch(ADMIN_TEMPLATE.'/conf_bancontrol.tpl');
$tpl->assign('content', $content);

//Clean whitespace
$tpl->load_filter('output', 'trimwhitespace');

//Make output
echo $tpl->fetch(ADMIN_TEMPLATE.'/main.tpl');
?>