-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overhaul of spam protection to use an extension on Form rather than the SpamProtectorManager. More fluent interface and easier to replace the built in spam protection api with an alternative.
- Loading branch information
Showing
14 changed files
with
297 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,6 @@ | |||
--- | |||
name: spamprotection | |||
--- | |||
Form: | |||
extensions: | |||
- FormSpamProtectionExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,97 +1,38 @@ | |||
<?php | <?php | ||
|
|
||
/** | /** | ||
* This class is responsible for setting an system-wide spam protector field | |||
* and add the protecter field to a form. | |||
* | |||
* @package spamprotection | * @package spamprotection | ||
* | |||
* @deprecated 1.0 | |||
*/ | */ | ||
|
|
||
class SpamProtectorManager { | class SpamProtectorManager { | ||
|
|
||
/** | |||
* Current Spam Protector used on the site | |||
* | |||
* @var SpamProtector | |||
*/ | |||
private static $spam_protector = null; | private static $spam_protector = null; | ||
|
|
||
/** | |||
* Set the name of the spam protecter class | |||
* | |||
* @param String the name of protecter field class | |||
*/ | |||
public static function set_spam_protector($protector) { | public static function set_spam_protector($protector) { | ||
Deprecation::notice('1.1', | |||
'SpamProtectorManager::set_spam_protector() is deprecated. '. | |||
'Use the new config system. FormSpamProtectorExtension.default_spam_protector' | |||
); | |||
|
|||
self::$spam_protector = $protector; | self::$spam_protector = $protector; | ||
} | } | ||
|
|
||
/** | |||
* Get the name of the spam protector class | |||
*/ | |||
public static function get_spam_protector() { | public static function get_spam_protector() { | ||
Deprecation::notice('1.1', | |||
'SpamProtectorManager::get_spam_protector() is deprecated'. | |||
'Use the new config system. FormSpamProtectorExtension.default_spam_protector'); | |||
|
|||
return self::$spam_protector; | return self::$spam_protector; | ||
} | } | ||
|
|
||
/** | public static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null) { | ||
* Add the spam protector field to a form | Deprecation::notice('1.1', | ||
* @param Form the form that the protecter field added into | 'SpamProtectorManager::update_form is deprecated'. | ||
* @param string the name of the field that the protecter field will be added in front of | 'Please use $form->enableSpamProtection() for adding spamprotection' | ||
* @param array an associative array | ); | ||
* with the name of the spam web service's field, for example post_title, post_body, author_name |
|
||
* and a string of field names | return $form->enableSpamProtection(); | ||
* @param String Title for the captcha field | |||
* @param String RightTitle for the captcha field | |||
* @return SpamProtector object on success or null if the spamprotector class is not found | |||
* also null if spamprotectorfield creation fails. | |||
*/ | |||
static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null) { | |||
$protectorClass = self::get_spam_protector(); | |||
|
|||
// Don't update if no protector is set | |||
if(!$protectorClass) return false; | |||
|
|||
if(!class_exists($protectorClass)) { | |||
return user_error("Spam Protector class '$protectorClass' does not exist. Please define a valid Spam Protector", E_USER_WARNING); | |||
} | |||
|
|||
try { | |||
$protector = new $protectorClass(); | |||
$field = $protector->getFormField("Captcha", $title, null); | |||
$field->setForm($form); | |||
if ($rightTitle) $field->setRightTitle($rightTitle); | |||
|
|||
if($field) { | |||
// update the mapping | |||
$field->setFieldMapping($fieldsToSpamServiceMapping); | |||
|
|||
// add the form field | |||
if($before && $form->Fields()->fieldByName($before)) { | |||
$form->Fields()->insertBefore($field, $before); | |||
} | |||
else { | |||
$form->Fields()->push($field); | |||
} | |||
} | |||
|
|||
} catch (Exception $e) { | |||
return user_error("SpamProtectorManager::update_form(): '$protectorClass' is not correctly set up. " . $e, E_USER_WARNING); | |||
} | |||
} | |||
|
|||
/** | |||
* Send Feedback to the Spam Protection. The level of feedback | |||
* will depend on the Protector class. | |||
* | |||
* @param DataObject The Object which you want to send feedback about. Must have a | |||
* SessionID field. | |||
* @param String Feedback on the $object usually 'spam' or 'ham' for non spam entries | |||
*/ | |||
static function send_feedback($object, $feedback) { | |||
$protectorClass = self::get_spam_protector(); | |||
|
|||
if(!$protectorClass) return false; | |||
|
|||
$protector = new $protectorClass(); | |||
return $protector->sendFeedback($object, $feedback); | |||
} | } | ||
} | } |
Oops, something went wrong.