| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\SpamProtection\Extension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Core\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Core\Config\Configurable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Core\Extension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * An extension to the {@link Form} class which provides the method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * {@link enableSpamProtection()} helper. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package spamprotection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | class FormSpamProtectionExtension extends Extension | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     use Configurable; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @config | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * The default spam protector class name to use. Class should implement the | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * {@link SpamProtector} interface. | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @var string $spam_protector | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     private static $default_spam_protector; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @config | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * The {@link enableSpamProtection} method will define which of the form | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * values correlates to this form mapped fields list. Totally custom forms | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * and subclassed SpamProtector instances are define their own mapping | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @var array $mappable_fields | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     private static $mappable_fields =  array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         'id', | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         'title', | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         'body', | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         'contextUrl', | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         'contextTitle', | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         'authorName', | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         'authorMail', | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         'authorUrl', | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         'authorIp', | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         'authorId' | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @config | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * The field name to use for the {@link SpamProtector} {@link FormField} | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @var string $spam_protector | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     private static $field_name = "Captcha"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * Instantiate a SpamProtector instance | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param array $options Configuration options | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return SpamProtector|null | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public static function get_protector($options = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         // generate the spam protector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (isset($options['protector'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $protector = $options['protector']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $protector = Config::inst()->get(self::class, 'default_spam_protector'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if ($protector && class_exists($protector)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return Injector::inst()->create($protector); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * Activates the spam protection module. | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @param array $options | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @return Object | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function enableSpamProtection($options = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         // captcha form field name (must be unique) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if (isset($options['name'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $name = $options['name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $name = Config::inst()->get(self::class, 'field_name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         // captcha field title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if (isset($options['title'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $title = $options['title']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $title = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         // set custom mapping on this form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $protector = self::get_protector($options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if (isset($options['mapping'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $protector->setFieldMapping($options['mapping']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         if ($protector) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             // add the form field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             if ($field = $protector->getFormField($name, $title)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $field->setForm($this->owner); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 // Add before field specified by insertBefore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $inserted = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 if (!empty($options['insertBefore'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     $inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 if (!$inserted) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                     // Add field to end if not added already | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     $this->owner->Fields()->push($field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return $this->owner; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  | 
            
                        
This check marks private properties in classes that are never used. Those properties can be removed.