<?php

/*

  Secure Scan PRO

 (c) 2013

 programmer: zignus.drionix

*/



class ssp_spam_ip_block {

public static function init() {

	  add_action('admin_menu', array(__CLASS__, 'admin_menu'));

	  add_action('admin_enqueue_scripts', array(__CLASS__, 'backend_enqueue'));

	  

	  add_action('wp_ajax_sipblock', array(__CLASS__, 'block_ip_range'));

	  add_action('wp_ajax_sipunblock', array(__CLASS__, 'unblock_ip_range'));

  }

  

  // create the admin menu item

public static function admin_menu() {

    add_submenu_page('secure-scan-pro', 'Spammer IP Block', 'Spammer IP Block', 'manage_options', 'ssp_spam_ip_block', array(__CLASS__, 'options_page'));

  } // admin_menu

  

public static function options_page() {

    if (!current_user_can('manage_options'))  {

      wp_die('You do not have sufficient permissions to access this page.');

    }



    echo '<div class="wrap">' . get_screen_icon('sn-lock');

    echo '<h2>Spammer IP Block <span id="ssp-help"><a href="http://www.securescanpro.com/help-and-support/">Help & Support</a></span></h2>';

	self::range_listings();

    echo '</div>'; // wrap

    echo '<div id="wf-dialog"></div>'; 

  }

  

  // enqueue CSS and JS

public static function backend_enqueue() {

      $plugin_url = plugin_dir_url(__FILE__);

	  

	  wp_enqueue_style('wp-jquery-ui-dialog');

      wp_enqueue_style('farbtastic');

	  

	  wp_enqueue_script('jquery-ui-tabs'); 

      wp_enqueue_script('ssp-jquery-plugins', $plugin_url . 'js/ssp-jquery-plugins.js', array(), SSP_VER, true);

	  wp_enqueue_script('sn-js', $plugin_url . 'js/ssp-common.js', array(), SSP_VER, true);



      wp_enqueue_style('wf-ln-css', plugin_dir_url(__FILE__) . 'css/ssp-ln-style.css', array(), '1.0');

      wp_enqueue_style('wf-ln-css-datatables', plugin_dir_url(__FILE__) . 'css/ssp-ln-datatables.css', array(), WF_LOGIN_NINJA_CORE_VER);

	  

  } // backend_enqueue

  

  // block_ip_range

  function block_ip_range() { 

  		

		$htaccess_path = get_home_path() . '.htaccess';

		$ipr_name=$_POST['range'];

		$ipblock_cnf = parse_ini_file('.security/ssp_ipb_verchk.txt',true);

		$rule = file_get_contents(dirname(__FILE__).'/.security/ipb_'.$ipr_name.'_'.$ipblock_cnf['version_info']['datetime']);

		

        $fh = fopen($htaccess_path, 'a');

        if (!$fh) {

          die('0');

        }

		

		$backed_up = self::backup_htaccess($htaccess_path);

        $tmp = fwrite($fh, $rule);

        if ($tmp != strlen($rule)) {

          die('0');

        }

        fclose($fh);

        die('10');

  } // block_ip_range

  

  // unblock_ip_range 

  function unblock_ip_range() { 

  		$htaccess_path = get_home_path() . '.htaccess';

		$ipbr_code = $_POST['range'];

	   

		if (is_file($htaccess_path)) {

			$ipblock_cnf = parse_ini_file('.security/ssp_ipb_verchk.txt',true); 

			$htaccess_content = file_get_contents($htaccess_path); 

			

			$pattern = '/(\#\#[\s]*SSP[\s]*\:\:[\s]*'.$ipbr_code.' '.$ipblock_cnf['ip_block_keys'][$ipbr_code].'\-\$START\#\#)+[^τ]*(\#\#[\s]*SSP[\s]*\:\:[\s]*'.$ipbr_code.' '.$ipblock_cnf['ip_block_keys'][$ipbr_code].'\-\$END\#\#)+/i';

			preg_match($pattern, $htaccess_content, $matches); 

			$new_content = preg_replace($pattern, '', $htaccess_content);

			

			$backed_up = self::backup_htaccess($htaccess_path); 

			$fh = fopen($htaccess_path, 'w+');

			if (!$fh) {

			  die('0'); 

			}

			

			$tmp = fwrite($fh, $new_content);

			if ($tmp != strlen($new_content)) {

			  die('0'); 

			}

			fclose($fh); 

			die('10'); 

		} 

		

		exit('Unblock '.$_POST['range']); 

  } // unblock_ip_range 

  

  // backup_htaccess

  private function backup_htaccess($htaccess_path = NULL) {

	  if (empty($htaccess_path)) $htaccess_path = get_home_path() . '.htaccess';

	  if (is_file($htaccess_path)) { 

	  		$destFile = dirname(__FILE__).'/.security/htaccess_back-up/'.time().'.htaccess';

			if (!copy($htaccess_path, $destFile)) return false; 

			return true; 

	  } 

	  return false; 

  } // backup_htaccess

	

  // is_inhtaccess

  private static function is_inhtaccess() {

	   $htaccess_path=get_home_path() . '.htaccess';

           if (!is_file($htaccess_path)) {

                $my_file = $htaccess_path;

                $handle = fopen($my_file, 'a') or die('Cannot open file:  '.$my_file); //implicitly creates file

                fclose($handle);

           }

           $retval = array();

	   if (is_file($htaccess_path)) {

		   $ipblock_cnf = parse_ini_file('.security/ssp_ipb_verchk.txt',true);

		   $htaccess_content = file_get_contents($htaccess_path);

		   

		   $ip_block_lists=explode(',',$ipblock_cnf['version_info']['ip_block_codes']);

		   foreach ($ip_block_lists as $br) {

			   /* RegEx to see if the configuration is already in .htaccess */

			   $pattern = '/##[\s]*SSP[\s]*::[\s]*'.$br.' '.$ipblock_cnf['ip_block_keys'][$br].'\-\$START##/i';

			   

			   if (preg_match($pattern, $htaccess_content, $matches)) {

				   $retval[$br] = true; // sets button action to 'unblock'

			   } else {

				   $retval[$br] = false; // sets button action to 'block'

			   } 

		   }

		  

	   }	   

	   return $retval;

	   //$fh = fopen($htaccess_path, 'a');

  } // is_inhtaccess



  // range_listings

public static function range_listings() {

	  $IPR = self::is_inhtaccess();

	  $buttons = array();

	  foreach ($IPR as $ipb_k => $v) {

		  $buttons[strtolower($ipb_k)]['action_class'] = ($v)?'unblock_ip_range':'block_ip_range';

		  $buttons[strtolower($ipb_k)]['action_label'] = ($v)?'Unblock!':'Block it!';

	  } 

?>

<table class="wp-list-table widefat" cellspacing="0" id="sn-tests-help">

  <thead>

    <tr>

      <th width="20%">IP Range</th>

      <th>Detailed explanation &amp; Action</th>

    </tr>

  </thead>

  <tbody>

    <tr>

      <td id="ver_check" height="60px"><strong>Russia, Turkey & Eastern Europe.</strong><br /> <a href="#" data-test="RTEE" class="button action <?php echo $buttons['rtee']['action_class']; ?>"><?php echo $buttons['rtee']['action_label']; ?></a></td>

      <td><p>Block known Spammers, Botnets, Content harvesters and Attackers from Russia, Turkey & Eastern Europe.</p></td>

    </tr>

    <tr>

      <td id="ver_check" height="60px"><strong>China & Korea.</strong><br /> <a href="#" data-test="ChKr" class="button action <?php echo $buttons['chkr']['action_class']; ?>"><?php echo $buttons['chkr']['action_label']; ?></a></td>

      <td><p>Block known Spammers, Botnets, Content harvesters and Attackers from China & Korea.</p></td>

    </tr>

    <tr>

      <td id="ver_check" height="60px"><strong>Latin America.</strong><br /> <a href="#" data-test="LatAm" class="button action <?php echo $buttons['latam']['action_class']; ?>"><?php echo $buttons['latam']['action_label']; ?></a></td>

      <td><p>Block known Spammers, Botnets, Content harvesters and Attackers from Latin America.</p></td>

    </tr>

    <tr>

      <td id="ver_check" height="60px"><strong>Nigeria & Africa.</strong><br /> <a href="#" data-test="NiAf" class="button action <?php echo $buttons['niaf']['action_class']; ?>"><?php echo $buttons['niaf']['action_label']; ?></a></td>

      <td><p>Block known Spammers, Botnets, Content harvesters and Attackers from Nigeria & Africa.</p></td>

    </tr>

    <tr>

      <td id="ver_check" height="60px"><strong>Known spam proxy servers.</strong><br /> <a href="#" data-test="spamproxy" class="button action <?php echo $buttons['spamproxy']['action_class']; ?>"><?php echo $buttons['spamproxy']['action_label']; ?></a></td>

      <td><p>Block known Spammers, Botnets, Content harvesters and Attackers from known spam proxy servers.</p></td>

    </tr>

      </tbody>

  <tfoot>

    <tr>

      <th>IP Range</th>

      <th>Detailed explanation &amp; Action</th>

    </tr>

  </tfoot>

</table>

<?php

  } // range_listings

  

}

?>