Version: current

     1 <?php
     2
     3 /**
     4  * This class is responsible for setting an system-wide spam protector field
     5  * and add the protecter field to a form.
     6  *
     7  * @package spamprotection
     8  */
     9
    10 class SpamProtectorManager {
    11
    12     /**
    13      * Current Spam Protector used on the site
    14      *
    15      * @var SpamProtector
    16      */
    17     private static $spam_protector = null;
    18
    19     /**
    20      * Set the name of the spam protecter class
    21      *
    22      * @param String the name of protecter field class
    23      */
    24     public static function set_spam_protector($protector) {
    25         self::$spam_protector = $protector;
    26     }
    27
    28     /**
    29      * Get the name of the spam protector class
    30      */
    31     public static function get_spam_protector() {
    32         return self::$spam_protector;
    33     }
    34
    35     /**
    36      * Add the spam protector field to a form
    37      * @param   Form    the form that the protecter field added into
    38      * @param   string  the name of the field that the protecter field will be added in front of
    39      * @param   array   an associative array
    40      *                  with the name of the spam web service's field, for example post_title, post_body, author_name
    41      *                  and a string of field names
    42      * @param   String  Title for the captcha field
    43      * @param   String  RightTitle for the captcha field
    44      * @return  SpamProtector   object on success or null if the spamprotector class is not found
    45      *                          also null if spamprotectorfield creation fails.
    46      */
    47     static function update_form($form, $before = null, $fieldsToSpamServiceMapping = array(), $title = null, $rightTitle = null) {
    48         $protectorClass = self::get_spam_protector();
    49
    50         // Don't update if no protector is set
    51         if(!$protectorClass) return false;
    52
    53         if(!class_exists($protectorClass)) {
    54             return user_error("Spam Protector class '$protectorClass' does not exist. Please define a valid Spam Protector", E_USER_WARNING);
    55         }
    56
    57         try {
    58             if ($title === null)
    59                 $title = _t('EditableSpamProtectionField.FORM_TITLE', 'Enter a code');
    60
    61             $protector = new $protectorClass();
    62             $field = $protector->getFormField("Captcha", $title, null, $form, $rightTitle);
    63
    64             if($field) {
    65                 // update the mapping
    66                 $field->setFieldMapping($fieldsToSpamServiceMapping);
    67
    68                 // add the form field
    69                 if($before && $form->Fields()->fieldByName($before)) {
    70                     $form->Fields()->insertBefore($field, $before);
    71                 }
    72                 else {
    73                     $form->Fields()->push($field);
    74                 }
    75             }
    76
    77         } catch (Exception $e) {
    78             return user_error("SpamProtectorManager::update_form(): '$protectorClass' is not correctly set up. " . $e, E_USER_WARNING);
    79         }
    80     }
    81
    82     /**
    83      * Send Feedback to the Spam Protection. The level of feedback
    84      * will depend on the Protector class.
    85      *
    86      * @param DataObject The Object which you want to send feedback about. Must have a
    87      *                      SessionID field.
    88      * @param String Feedback on the $object usually 'spam' or 'ham' for non spam entries
    89      */
    90     static function send_feedback($object, $feedback) {
    91         $protectorClass = self::get_spam_protector();
    92
    93         if(!$protectorClass) return false;
    94
    95         $protector = new $protectorClass();
    96         return $protector->sendFeedback($object, $feedback);
    97     }
    98 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -