From: <al...@us...> - 2008-10-01 23:04:26
|
Revision: 720 http://sciret.svn.sourceforge.net/sciret/?rev=720&view=rev Author: alpeb Date: 2008-10-01 23:04:13 +0000 (Wed, 01 Oct 2008) Log Message: ----------- implemented support for TypePad AntiSpam check for comments Modified Paths: -------------- trunk/actions/AddCommentAndRating.php trunk/actions/SavePreferences.php trunk/models/Configuration.php trunk/templates/EditPreferences.tpl trunk/views/EditPreferences.php Added Paths: ----------- trunk/models/AntiSpam.php Modified: trunk/actions/AddCommentAndRating.php =================================================================== --- trunk/actions/AddCommentAndRating.php 2008-10-01 23:03:32 UTC (rev 719) +++ trunk/actions/AddCommentAndRating.php 2008-10-01 23:04:13 UTC (rev 720) @@ -22,12 +22,36 @@ $message = ''; if ($_POST['comment_box'] != '') { - require 'models/Comment.php'; + $antiSpam = new AntiSpam($this->configuration); + + try { + $data = array( + 'user_ip' => $_SERVER['REMOTE_ADDR'], + 'user_agent' => $_SERVER['HTTP_USER_AGENT'], + 'referer' => isset($_SERVER['HTTP_REFERER'])? $_SERVER['HTTP_REFERER'] : '', + 'permalink' => Library::getBaseURL() . "/view=ViewArticle&id=$artId", + 'comment_type' => 'comment', + 'comment_author' => $_POST['commentUserName'], + 'comment_content' => $_POST['comment_box'], + ); + if ($antiSpam->isSpam($data)) { + $message = $this->user->lang('Comment detected as spam. ** DISCARDED **'); + Library::redirect(Library::getLink(array( + 'view' => 'ViewComments', + 'artId' => $artId, + 'commentsMessage' => urlencode($message) + ))); + } + } catch (Exception $e) { + // TypePad AntiSpam API Key is wrong, so nothing to do in this case :( + } + $comment = new Comment; $comment->setUserName($_POST['commentUserName']); $comment->setEntered(date('Y-m-d h:i:s')); $comment->setContents($_POST['comment_box']); $comment->setArticleId($artId); + $comment->setPublished($this->configuration->getConfigValue('publishCommentsAuto')); $comment->save(); $this->addHistoryEntry($artId, $this->user->lang('Added comment'), false, $_POST['commentUserName']); $message = $this->user->lang('Comment saved successfully'); Modified: trunk/actions/SavePreferences.php =================================================================== --- trunk/actions/SavePreferences.php 2008-10-01 23:03:32 UTC (rev 719) +++ trunk/actions/SavePreferences.php 2008-10-01 23:04:13 UTC (rev 720) @@ -33,7 +33,7 @@ $this->user->setPreference('navigationType', $_POST['navigationType']); } - $clamavError = ''; + $errors = array(); if (($this->user->role & User::ROLE_ADMIN) == User::ROLE_ADMIN) { $this->configuration->setConfigValue('publishKB', $_POST['publishKB'] == '1'? '1' : '0'); $this->configuration->setConfigValue('anonymousRegistration', $_POST['anonymousRegistration'] == '1'? '1' : '0'); @@ -54,18 +54,24 @@ $this->configuration->setConfigValue('smtpUser', $_POST['smtpUser']); $this->configuration->setConfigValue('smtpPassword', $_POST['smtpPassword']); $this->configuration->setConfigValue('smtpPort', $_POST['smtpPort']); + $this->configuration->setConfigValue('useClamAV', $_POST['useClamAV']); + $this->configuration->setConfigValue('typePadAntiSpamKey', $_POST['typePadAntiSpamKey']); if ($_POST['useClamAV'] == '1') { $clamav = new ClamAV(); if (!$clamav->canExecuteClamscan()) { - $clamavError = $this->user->lang('Couldn\'t attempt to use ClamAV: clamscan was not found (looking for %s)', - $clamav->getClamscanLocation()); - } else { - $this->configuration->setConfigValue('useClamAV', '1'); + $errors[] = $this->user->lang('Couldn\'t attempt to use ClamAV: clamscan was not found (looking for %s)', + $clamav->getClamscanLocation()); + $this->configuration->setConfigValue('useClamAV', '0'); } } - if ($_POST['useClamAV'] == '0' || $clamavError) { - $this->configuration->setConfigValue('useClamAV', '0'); + + if ($_POST['typePadAntiSpamKey'] != '') { + $antiSpam = new AntiSpam($this->configuration); + if (!$antiSpam->verifyKey()) { + $errors[] = $this->user->lang('Your TypePad AntiSpam key is wrong. Please verify.'); + $this->configuration->setConfigValue('typePadAntiSpamKey', ''); + } } $this->configuration->save(); @@ -74,8 +80,9 @@ $this->user->save(); $_SESSION['message'] = $this->user->lang('Preferences saved successfully'); - if ($clamavError) { - $_SESSION['message'] .= '<br />' . $clamavError; + if ($errors) { + $errors = implode('<br />', $errors); + $_SESSION['message'] .= '<br />' . $errors; Library::redirect(Library::getLink(array('view' => 'EditPreferences'))); } else { Library::redirect(Library::getLink(array('view' => 'MainView', 'set' => $this->user->getPreference('startBrowsing')))); Added: trunk/models/AntiSpam.php =================================================================== --- trunk/models/AntiSpam.php (rev 0) +++ trunk/models/AntiSpam.php 2008-10-01 23:04:13 UTC (rev 720) @@ -0,0 +1,47 @@ +<?php + +/* +* @copyright Copyright (C) 2005-2008 Keyboard Monkeys Ltd. http://www.kb-m.com +* @license http://www.fsf.org/copyleft/lgpl.html GNU Lesser General Public License +* @author Alejandro Pedraza +* @since Sciret 1.2 +* @package Sciret +* @packager Keyboard Monkeys +*/ + +class AntiSpam extends Zend_Service_Akismet +{ + const API_URL = 'api.antispam.typepad.com'; + + private $_config; + + public function __construct($config) + { + $this->_config = $config; + + parent::__construct($config->getConfigValue('typePadAntiSpamKey'), + Library::getBaseURL()); + } + + protected function _post($host, $path, array $params) + { + $caller = $this->_getCallerMethod(); + if (strtolower($caller) == 'verifykey') { + $host = self::API_URL; + } else { + $host = $this->getApiKey() . '.' . self::API_URL; + } + + return parent::_post($host, $path, $params); + } + + /** + * @return string + */ + private function _getCallerMethod() + { + $backTrace = debug_backtrace(); + + return $backTrace[2]['function']; + } +} Modified: trunk/models/Configuration.php =================================================================== --- trunk/models/Configuration.php 2008-10-01 23:03:32 UTC (rev 719) +++ trunk/models/Configuration.php 2008-10-01 23:04:13 UTC (rev 720) @@ -22,6 +22,7 @@ 'passwordExpirationDays'=> 60, 'version' => 0, 'useClamAV' => 0, + 'useTypePadAntiSpam' => '', ); function Configuration() { Modified: trunk/templates/EditPreferences.tpl =================================================================== --- trunk/templates/EditPreferences.tpl 2008-10-01 23:03:32 UTC (rev 719) +++ trunk/templates/EditPreferences.tpl 2008-10-01 23:04:13 UTC (rev 720) @@ -138,6 +138,15 @@ </td> </tr> <tr class="row_off"> + <td style="text-align:right; font-weight:bold"> + [l]Enter your TypePad AntiSpam API key to be able to discard spam comments[/l]:<br /> + [l](You can get a free key <a href="http://antispam.typepad.com/info/get-api-key.html">here</a>)[/l] + </td> + <td> + <input type="text" name="typePadAntiSpamKey" value="{typePadAntiSpamKey}"/> + </td> + </tr> + <tr class="row_on"> <td style="text-align:right; font-weight:bold">[l]Only admin and author can modify an article[/l]:</td> <td> <select name="restrictEditDelete"> @@ -146,25 +155,25 @@ </select> </td> </tr> - <tr class="row_on"> + <tr class="row_off"> <td style="text-align:right; font-weight:bold">[l]Days before password expires[/l]:</td> <td> <input type="text" name="passwordExpirationDays" value="{passwordExpirationDays}" size="2" /> </td> </tr> - <tr class="row_off"> + <tr class="row_on"> <td style="text-align:right; font-weight:bold">[l]Send mail from (name)[/l]:</td> <td> <input type="text" name="mailFromName" value="{mailFromName}"/> </td> </tr> - <tr class="row_on"> + <tr class="row_off"> <td style="text-align:right; font-weight:bold">[l]Send mail from (email)[/l]:</td> <td> <input type="text" name="mailFromMail" value="{mailFromMail}"/> </td> </tr> - <tr class="row_off"> + <tr class="row_on"> <td style="text-align:right; font-weight:bold">[l]Send article reports to (email)[/l]:</td> <td> <input type="text" name="mailArticleReports" value="{mailArticleReports}"/> @@ -176,7 +185,7 @@ <input type="text" name="mailCategoryRequest" value="{mailCategoryRequest}"/> </td> </tr> - <tr class="row_of"> + <tr class="row_on"> <td style="text-align:right; font-weight:bold">[l]Send mail using[/l]:</td> <td> <select name="mailTransport" onchange="javascript:toggleSMTPSettings(value);"> @@ -185,7 +194,7 @@ </select> </td> </tr> - <tr class="row_on" id="smtp_options" {showSMTPOptions}> + <tr class="row_off" id="smtp_options" {showSMTPOptions}> <td colspan="2"> <p style="text-align:center; font-weight:bold">[l]SMTP settings[/l]:</p> <ul> @@ -196,7 +205,7 @@ </ul> </td> </tr> - <tr class="row_off"> + <tr class="row_on"> <td style="text-align:right; font-weight:bold"> [l]Check uploaded files for viruses[/l]:<br /> [l](You need to have <a href="http://www.clamav.net">ClamAV</a> in your system)[/l] Modified: trunk/views/EditPreferences.php =================================================================== --- trunk/views/EditPreferences.php 2008-10-01 23:03:32 UTC (rev 719) +++ trunk/views/EditPreferences.php 2008-10-01 23:04:13 UTC (rev 720) @@ -68,6 +68,7 @@ 'smtpPort' => $this->configuration->getConfigValue('smtpPort'), 'useClamAV_yes_selected' => $this->configuration->getConfigValue('useClamAV') == '1'? 'selected="true"' : '', 'useClamAV_no_selected' => $this->configuration->getConfigValue('useClamAV') == '0'? 'selected="true"' : '', + 'typePadAntiSpamKey' => $this->configuration->getConfigValue('typePadAntiSpamKey'), )); // *** LANGUAGES *** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |