<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Antispam, a plugin for DotClear.  
# Copyright (c) 2007 Alain Vagner and contributors. All rights
# reserved.
#
# Antispam is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# Antispam is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Antispam; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ***** END LICENSE BLOCK *****

class dcFilterIpLookup extends dcSpamFilter
{
	public $name = 'IP Lookup';
	public $has_gui = true;
	
	private $default_bls = 'sbl-xbl.spamhaus.org , bsb.spamlookup.net';
	
	public function __construct(&$core)
	{
		parent::__construct($core);
		
		if (defined('DC_DNSBL_SUPER') && DC_DNSBL_SUPER && !$core->auth->isSuperAdmin()) {
			$this->has_gui = false;
		}
	}
	
	protected function setInfo()
	{
		$this->description = __('Checks sender IP address against DNSBL servers');
	}
	
	public function getStatusMessage($status,$comment_id)
	{
		return sprintf(__('Filtered by %1$s with server %2$s.'),$this->guiLink(),$status);
	}
	
	public function isSpam($type,$author,$email,$site,$ip,$content,$post_id,&$status)
	{
		if (!$ip || long2ip(ip2long($ip)) != $ip) {
			return;
		}
		
		$match = array();
		
		$bls = $this->getServers();
		$bls = preg_split('/\s*,\s*/',$bls);
		
		foreach ($bls as $bl)
		{
			if ($this->dnsblLookup($ip,$bl)) {
				$match[] = $bl;
			}
		}
		
		if (!empty($match)) {
			$status = substr(implode(', ',$match),0,128);
			return true;
		}
	}
	
	public function gui($url)
	{
		$bls = $this->getServers();
		
		if (isset($_POST['bls']))
		{
			try {
				$this->core->blog->settings->setNameSpace('antispam');
				$this->core->blog->settings->put('antispam_dnsbls',$_POST['bls'],'string','Antispam DNSBL servers',true,false);
				http::redirect($url.'&upd=1');
			} catch (Exception $e) {
				$core->error->add($e->getMessage());
			}
		}
		
		/* DISPLAY
		---------------------------------------------- */
		$res = '';
		
		$res .=
		'<form action="'.html::escapeURL($url).'" method="post">'.
		'<fieldset><legend>' . __('IP Lookup servers') . '</legend>'.
		'<p>'.__('Add here a coma separated list of servers.').'</p>'.
		'<p>'.form::textarea('bls',40,3,html::escapeHTML($bls),'maximal').'</p>'.
		'<p><input type="submit" value="'.__('Save').'" />'.
		$this->core->formNonce().'</p>'.
		'</fieldset>'.
		'</form>';
		
		return $res;
	}
	
	private function getServers()
	{
		$bls = $this->core->blog->settings->antispam_dnsbls;
		if ($bls === null) {
			$this->core->blog->settings->setNameSpace('antispam');
			$this->core->blog->settings->put('antispam_dnsbls',$this->default_bls,'string','Antispam DNSBL servers',true,false);
			return $this->default_bls;
		}
		
		return $bls;
	}
	
	private function dnsblLookup($ip,$bl)
	{
		$revIp = implode('.',array_reverse(explode('.',$ip)));
		
		$host = $revIp.'.'.$bl.'.';
		if (gethostbyname($host) != $host) {
			return true;
		}
		
		return false;
	}
}
?>