文書の過去の版を表示しています。


NP_TypePadAntiSpam

NP_TypePadAntiSpam is a plugin that integrates TypePad AntiSpam with Nucleus CMS. TypePad AntiSpam is an Akismet-compatible collaborative spam blocking service from Six Apart.

NP_TypePadAntiSpam は、Nucleus CMS で TypePad AntiSpam を利用できるようにするプラグインです。TypePad AntiSpam は、シックス・アパート社が提供する Akismet 互換の協調スパムフィルターサービスです。

Prototype

This version is a prototype, a quick hack based on NP_Akismet and NP_AddSpamCheckEvent, just to see if the API works with Nucleus. However, it practically works and rejects comment spams in English pretty well. Install this in your plugin directory, and set your API Key in admin area.

np_typepadantispam.php.zip

<?php
   /* ========================================================================
    * NP_TypePadAntiSpam -- TypePad AntiSpam Plugin for Nucleus CMS
    * Copyright (C) 2008 Osamu Higuchi
    * ------------------------------------------------------------------------
	* This program 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.
	*
	* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
    * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit
    * http://www.gnu.org/licenses/gpl.html
    * ========================================================================
    * By Osamu Higuchi http://www.higuchi.com/
    * Based on NP_Akismet by Niels Leenheer and Matt Mullenweg
    * and NP_AddSpamCheckEvent by hsur
    * ------------------------------------------------------------------------
    * Version History
    * 0.1 May 30, 2008 : Prototype
    * ========================================================================
    */
 
class NP_TypePadAntiSpam extends NucleusPlugin {
	function getName() 			{ return 'TypePadAntiSpam'; }
	function getAuthor()  	  	{ return 'Osamu Higuchi'; }
	function getURL()  			{ return 'http://www.higuchi.com/'; }
	function getVersion() 	  	{ return '0.1'; }
	function getDescription() 	{ return 'Check for spam with TypePad AntiSpam';	}
 
	function supportsFeature($what) {
		switch($what) {
		    case 'SqlTablePrefix':
				return 1;
			default:
				return 0;
		}
	}
 
	function getEventList() {
		return array('SpamCheck', 'SpamMark', 'SpamPlugin', 'ValidateForm');
	}
 
	function install() {
		$this->createOption('ApiKey', 'TypePad API key', 'text', '');
	    $this->createOption('Privacy', 'Privacy protection', 'select', 'normal', 'Paranoid|high|Moderate|normal|None|low');
	}
 
// Borrowed from NP_AddSpamCheckEvent	
	function event_ValidateForm(& $data) {
		global $manager, $member;
		if ($member->isLoggedIn())
			return;
 
		$spamcheck = array();
		switch( $data['type'] ){
			case 'membermail':
				$spamcheck = array (
					'type' => 'membermail', 
					'data' => postVar('frommail')."\n".postVar('message'), 
					'live' => true, 
					'return' => true,
				);
				break;
			case 'comment':
				$spamcheck = array (
					'type' => 'comment', 
					'body' => postVar('body'), 
					'author' => $data['comment']['user'], 
					'url' => $data['comment']['userid'], 
					'id' => intval($data['comment']['itemid']), 
					'live' => true, 
					'return' => true,
					//SpamCheck API1 Compat
					'data' => postVar('body')."\n".$data['comment']['user']."\n".$data['comment']['userid'],
				);
				break;
			default:
				return;
		}
 
		$manager->notify('SpamCheck', array ('spamcheck' => & $spamcheck));
		if (isset($spamcheck['result']) && $spamcheck['result'] == true) {
			if ($manager->pluginInstalled('NP_Blacklist')) {
				$plugin = & $manager->getPlugin('NP_Blacklist');
				$plugin->_redirect($plugin->getOption('redirect'));
			} else {
				$this->_showForbiddenMessage($spamcheck['message']);
			}
		}
	}
 
	function _showForbiddenMessage($message = '') {
		header("HTTP/1.0 403 Forbidden");
		header("Status: 403 Forbidden");
		echo '<html><header><title>403 Forbidden</title></header><body><h1>403 Forbidden</h1><p>'.$message.'</p></body></html>';
		exit;
	}
 
// Borrowed from NP_Akismet
 
	function event_SpamPlugin(&$data) {
		$data['spamplugin'][] = array (
			'name' => $this->getName(),
			'version' => $this->getVersion()
		);
	}
 
	function event_SpamCheck(&$data) {
		global $CONF, $manager;
 
		if (isset($data['spamcheck']['result']) && $data['spamcheck']['result'] == true) 
			return;
 
		if ($data['spamcheck']['type'] == 'comment' || $data['spamcheck']['type'] == 'trackback') 
		{
			if ($data['spamcheck']['type'] == 'comment') 
				$comment = array (
					'comment_post_ID' => $data['spamcheck']['id'], 
					'comment_author' => $data['spamcheck']['author'], 
					'comment_author_email' => $data['spamcheck']['email'], 
					'comment_author_url' => $data['spamcheck']['url'], 
					'comment_content' => $data['spamcheck']['body'],
					'comment_type' => '',
					'blog' => $CONF['IndexURL']
				);
 
			if ($data['spamcheck']['type'] == 'trackback') 
				$comment = array (
					'comment_post_ID' => $data['spamcheck']['id'], 
					'comment_author' => $data['spamcheck']['blogname'], 
					'comment_author_url' => $data['spamcheck']['url'], 
					'comment_content' => $data['spamcheck']['title'] . ' ' . $data['spamcheck']['excerpt'],
					'comment_type' => '',
					'blog' => $CONF['IndexURL']
				);
 
			if (isset($data['spamcheck']['live']) && $data['spamcheck']['live'] == true)
			{
				$comment['user_ip'] = $_SERVER['REMOTE_ADDR'];
				$comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
				$comment['referrer'] = $_SERVER['HTTP_REFERER'];
 
				if ($this->getOption('Privacy') == 'low')
				{
					$ignore = array (
						'HTTP_COOKIE'
					);
 
					foreach ($_SERVER as $key => $value)
						if (!in_array($key, $ignore))
							$comment[$key] = $value;
				}
 
				if ($this->getOption('Privacy') == 'normal')
				{
					$approved = array (
						'HTTP_HOST', 'HTTP_USER_AGENT', 'HTTP_ACCEPT',
						'HTTP_ACCEPT_LANGUAGE', 'HTTP_ACCEPT_ENCODING',
						'HTTP_ACCEPT_CHARSET', 'HTTP_KEEP_ALIVE', 'HTTP_REFERER',
						'HTTP_CONNECTION', 'HTTP_FORWARDED', 'HTTP_FORWARDED_FOR',
						'HTTP_X_FORWARDED', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP',
						'REMOTE_ADDR', 'REMOTE_HOST', 'REMOTE_PORT', 'SERVER_PROTOCOL',
						'REQUEST_METHOD'
					);
 
					foreach ($_SERVER as $key => $value)
						if (in_array($key, $approved))
							$comment[$key] = $value;
				}
			}
 
			$query_string = '';
 
			foreach ($comment as $key => $part)
				$query_string .= $key . '=' . urlencode(stripslashes($part)) . '&';
 
			$response = $this->_PostHTTP(
				$query_string, 
				$this->getOption('ApiKey') . '.api.antispam.typepad.com', 
				'/1.1/comment-check', 
				80
			);
 
			if (is_array($response)) {
				$data['spamcheck']['result'] = ($response[1] == 'true');
 
				if ($response[1] == 'true') {
					$data['spamcheck']['plugin'] = $this->getName();
					$data['spamcheck']['message'] = 'Marked as spam by TypePad AntiSpam';
				}
			}
 
			$manager->notify('TypePadAntiSpamResult', array ('id' => $data['spamcheck']['id'], 'status' => (int) $data['spamcheck']['result']));
		}
	}
 
	function event_SpamMark(&$data) {
		global $CONF;
 
		if ($data['spammark']['type'] == 'comment' || $data['spammark']['type'] == 'trackback') 
		{
			if ($data['spammark']['type'] == 'comment') 
				$comment = array (
					'comment_post_ID' => $data['spammark']['id'], 
					'comment_author' => $data['spammark']['author'], 
					'comment_author_email' => $data['spammark']['email'], 
					'comment_author_url' => $data['spammark']['url'], 
					'comment_content' => $data['spammark']['body'],
					'comment_type' => '',
					'blog' => $CONF['IndexURL']
				);
 
			if ($data['spammark']['type'] == 'trackback') 
				$comment = array (
					'comment_post_ID' => $data['spammark']['id'], 
					'comment_author' => $data['spammark']['blogname'], 
					'comment_author_url' => $data['spammark']['url'], 
					'comment_content' => $data['spammark']['title'] . ' ' . $data['spammark']['excerpt'],
					'comment_type' => '',
					'blog' => $CONF['IndexURL']
				);
 
			$query_string = '';
 
			foreach ($comment as $key => $part)
				$query_string .= $key . '=' . urlencode(stripslashes($part)) . '&';
 
			if ($data['spammark']['result'] == true)
			{
				$response = $this->_PostHTTP(
					$query_string, 
					$this->getOption('ApiKey') . '.api.antispam.typepad.com', 
					'/1.1/submit-spam', 
					80
				);
			}
			else
			{
				$response = $this->_PostHTTP(
					$query_string, 
					$this->getOption('ApiKey') . '.api.antispam.typepad.com', 
					'/1.1/submit-ham', 
					80
				);
			}
		}		
	}
 
	function _PostHTTP($request, $host, $path, $port = 80) {
		global $nucleus;
 
		$http_request  = "POST $path HTTP/1.0\r\n";
		$http_request .= "Host: $host\r\n";
		$http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . _CHARSET . "\r\n";
		$http_request .= "Content-Length: " . strlen($request) . "\r\n";
		$http_request .= "User-Agent: Nucleus/" . substr($nucleus['version'], 1) . " | TypePadAntiSpam/" . $this->getVersion() . "\r\n";
		$http_request .= "\r\n";
		$http_request .= $request;
 
		$response = '';
		if( false !== ( $fs = @fsockopen($host, $port, $errno, $errstr, 3) ) ) {
			fwrite($fs, $http_request);
			while ( !feof($fs) )
				$response .= fgets($fs, 1160); // One TCP-IP packet
			fclose($fs);
 
			$response = explode("\r\n\r\n", $response, 2);
		}
 
		return $response;
	}
}
 
?>
nucleus/np_typepadantispam.1212314250.txt.gz · 最終更新: 2008/06/01 18:57 by osamu
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0