2009-02-03 22:29:55 +01:00
|
|
|
<?php
|
2009-09-17 06:06:53 +02:00
|
|
|
|
2009-02-03 22:29:55 +01:00
|
|
|
/**
|
2009-09-17 06:06:53 +02:00
|
|
|
* This class is responsible for setting an system-wide spam protector field
|
2009-08-11 04:51:03 +02:00
|
|
|
* and add the protecter field to a form.
|
|
|
|
*
|
|
|
|
* @package spamprotection
|
2009-02-03 22:29:55 +01:00
|
|
|
*/
|
2009-08-11 04:51:03 +02:00
|
|
|
|
2009-06-05 00:57:33 +02:00
|
|
|
class SpamProtectorManager {
|
2009-02-03 22:29:55 +01:00
|
|
|
|
2009-09-17 06:06:53 +02:00
|
|
|
/**
|
|
|
|
* Current Spam Protector used on the site
|
|
|
|
*
|
|
|
|
* @var SpamProtector
|
|
|
|
*/
|
|
|
|
private static $spam_protector = null;
|
2009-02-03 22:29:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the name of the spam protecter class
|
2009-08-11 04:51:03 +02:00
|
|
|
*
|
|
|
|
* @param String the name of protecter field class
|
2009-02-03 22:29:55 +01:00
|
|
|
*/
|
2009-09-17 06:06:53 +02:00
|
|
|
public static function set_spam_protector($protector) {
|
2009-06-05 00:57:33 +02:00
|
|
|
self::$spam_protector = $protector;
|
2009-02-03 22:29:55 +01:00
|
|
|
}
|
|
|
|
|
2009-03-25 03:14:26 +01:00
|
|
|
/**
|
|
|
|
* Get the name of the spam protector class
|
|
|
|
*/
|
2009-09-17 06:06:53 +02:00
|
|
|
public static function get_spam_protector() {
|
2009-06-05 00:57:33 +02:00
|
|
|
return self::$spam_protector;
|
2009-03-25 03:14:26 +01:00
|
|
|
}
|
|
|
|
|
2009-02-03 22:29:55 +01:00
|
|
|
/**
|
2009-06-05 00:57:33 +02:00
|
|
|
* Add the spam protector field to a form
|
2009-02-03 22:29:55 +01:00
|
|
|
* @param Form the form that the protecter field added into
|
|
|
|
* @param string the name of the field that the protecter field will be added in front of
|
|
|
|
* @param array an associative array
|
|
|
|
* with the name of the spam web service's field, for example post_title, post_body, author_name
|
2009-09-17 06:06:53 +02:00
|
|
|
* and a string of field names
|
|
|
|
* @param String Title for the captcha field
|
|
|
|
* @param String RightTitle for the captcha field
|
2009-06-05 01:44:08 +02:00
|
|
|
* @return SpamProtector object on success or null if the spamprotector class is not found
|
|
|
|
* also null if spamprotectorfield creation fails.
|
2009-02-03 22:29:55 +01:00
|
|
|
*/
|
2009-09-17 06:06:53 +02:00
|
|
|
static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null) {
|
|
|
|
$protectorClass = self::get_spam_protector();
|
2009-08-11 04:51:03 +02:00
|
|
|
|
2009-11-26 21:16:37 +01:00
|
|
|
// Don't update if no protector is set
|
2010-02-02 04:04:52 +01:00
|
|
|
if(!$protectorClass) return false;
|
2009-11-26 21:16:37 +01:00
|
|
|
|
2009-08-11 04:51:03 +02:00
|
|
|
if(!class_exists($protectorClass)) {
|
2009-09-17 06:06:53 +02:00
|
|
|
return user_error("Spam Protector class '$protectorClass' does not exist. Please define a valid Spam Protector", E_USER_WARNING);
|
2009-08-11 04:51:03 +02:00
|
|
|
}
|
2009-02-17 10:03:59 +01:00
|
|
|
|
2009-02-24 01:35:59 +01:00
|
|
|
try {
|
2009-09-17 06:06:53 +02:00
|
|
|
$protector = new $protectorClass();
|
2012-07-06 04:43:38 +02:00
|
|
|
$field = $protector->getFormField("Captcha", $title, null);
|
|
|
|
$field->setForm($form);
|
|
|
|
if ($rightTitle) $field->setRightTitle($rightTitle);
|
2009-09-17 06:06:53 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-26 01:26:23 +02:00
|
|
|
} catch (Exception $e) {
|
2009-09-17 06:06:53 +02:00
|
|
|
return user_error("SpamProtectorManager::update_form(): '$protectorClass' is not correctly set up. " . $e, E_USER_WARNING);
|
2009-02-24 01:35:59 +01:00
|
|
|
}
|
2009-02-03 22:29:55 +01:00
|
|
|
}
|
2009-02-16 01:28:53 +01:00
|
|
|
|
|
|
|
/**
|
2009-03-11 05:42:21 +01:00
|
|
|
* Send Feedback to the Spam Protection. The level of feedback
|
|
|
|
* will depend on the Protector class.
|
2009-02-16 01:28:53 +01:00
|
|
|
*
|
2009-03-11 05:42:21 +01:00
|
|
|
* @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
|
2009-02-16 01:28:53 +01:00
|
|
|
*/
|
2009-03-11 05:42:21 +01:00
|
|
|
static function send_feedback($object, $feedback) {
|
2010-02-02 04:04:52 +01:00
|
|
|
$protectorClass = self::get_spam_protector();
|
|
|
|
|
|
|
|
if(!$protectorClass) return false;
|
|
|
|
|
|
|
|
$protector = new $protectorClass();
|
2009-06-05 00:57:33 +02:00
|
|
|
return $protector->sendFeedback($object, $feedback);
|
2009-02-16 01:28:53 +01:00
|
|
|
}
|
2009-09-17 06:06:53 +02:00
|
|
|
}
|