|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spam protector for Akismet |
|
5
|
|
|
* |
|
6
|
|
|
* @author Damian Mooyman |
|
7
|
|
|
* @package akismet |
|
8
|
|
|
*/ |
|
9
|
|
|
class AkismetSpamProtector implements SpamProtector |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Set this to your API key |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
* @config |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $api_key = null; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Permission required to bypass check |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
* @config |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $bypass_permission = 'ADMIN'; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Set to try to bypass check for all logged in users |
|
29
|
|
|
* |
|
30
|
|
|
* @var boolean |
|
31
|
|
|
* @config |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $bypass_members = false; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* IMPORTANT: If you are operating in a country (such as Germany) that has content transmission disclosure |
|
37
|
|
|
* requirements, set this to true in order to require a user prompt prior to submission of user data |
|
38
|
|
|
* to the Akismet servers |
|
39
|
|
|
* |
|
40
|
|
|
* @var boolean |
|
41
|
|
|
* @config |
|
42
|
|
|
*/ |
|
43
|
|
|
private static $require_confirmation = false; |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Set to true to disable spam errors, instead saving this field to the dataobject with the spam |
|
47
|
|
|
* detection as a flag. This will disable validation errors when spam is encountered. |
|
48
|
|
|
* The flag will be saved to the same field specified by the 'name' option in enableSpamProtection() |
|
49
|
|
|
* |
|
50
|
|
|
* @var boolean |
|
51
|
|
|
* @config |
|
52
|
|
|
*/ |
|
53
|
|
|
private static $save_spam = false; |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
private $fieldMapping = array(); |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Overridden API key |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected static $_api_key = null; |
|
66
|
|
|
|
|
67
|
|
|
public function __construct($options = array()) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set the API key |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $key |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function set_api_key($key) |
|
77
|
|
|
{ |
|
78
|
|
|
self::$_api_key = $key; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the API key |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
protected static function get_api_key() |
|
87
|
|
|
{ |
|
88
|
|
|
if (self::$_api_key) { |
|
89
|
|
|
return self::$_api_key; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Check config |
|
93
|
|
|
$key = Config::inst()->get('AkismetSpamProtector', 'api_key'); |
|
94
|
|
|
if (!empty($key)) { |
|
95
|
|
|
return $key; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Check environment |
|
99
|
|
|
if (defined('SS_AKISMET_API_KEY')) { |
|
100
|
|
|
return SS_AKISMET_API_KEY; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Retrieves Akismet API object singleton, or null if not configured |
|
106
|
|
|
* |
|
107
|
|
|
* @return AkismetService |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function api() |
|
110
|
|
|
{ |
|
111
|
|
|
// Get API key and URL |
|
112
|
|
|
$key = self::get_api_key(); |
|
113
|
|
|
if (empty($key)) { |
|
114
|
|
|
user_error("AkismetSpamProtector is incorrectly configured. Please specify an API key.", E_USER_WARNING); |
|
115
|
|
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
$url = Director::protocolAndHost(); |
|
118
|
|
|
|
|
119
|
|
|
// Generate API object |
|
120
|
|
|
return Injector::inst()->get('AkismetService', true, array($key, $url)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getFormField($name = null, $title = null, $value = null, $form = null, $rightTitle = null) |
|
124
|
|
|
{ |
|
125
|
|
|
return AkismetField::create($name, $title, $value, $form, $rightTitle) |
|
126
|
|
|
->setFieldMapping($this->fieldMapping); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function setFieldMapping($fieldMapping) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->fieldMapping = $fieldMapping; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.