Skip to content

Commit 7b411af

Browse files
author
helpfulrobot
committed
Converted to PSR-2
1 parent d7f1e50 commit 7b411af

2 files changed

Lines changed: 517 additions & 491 deletions

File tree

code/MollomSpamProtector.php

Lines changed: 183 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -10,183 +10,187 @@
1010
* @package mollom
1111
*/
1212

13-
class MollomSpamProtector extends Mollom implements SpamProtector {
14-
15-
/**
16-
* @var array
17-
*/
18-
private $fieldMapping = array();
19-
20-
/**
21-
* @var array
22-
*/
23-
public $configurationMap = array(
24-
'publicKey' => 'public_key',
25-
'privateKey' => 'private_key',
26-
);
27-
28-
/**
29-
* Load configuration for a given variable such as privateKey. Since
30-
* SilverStripe uses YAML conventions, look for those variables
31-
*
32-
* @param string $name
33-
*
34-
* @return mixed
35-
*/
36-
public function loadConfiguration($name) {
37-
return Config::inst()->get('Mollom', $this->configurationMap[$name]);
38-
}
39-
40-
/**
41-
* Save configuration value for a given variable
42-
*
43-
* @param string $name
44-
* @param mixed $value
45-
*
46-
* @return void
47-
*/
48-
public function saveConfiguration($name, $value) {
49-
return Config::inst()->update('Mollom', $this->configurationMap[$name], $value);
50-
}
51-
52-
/**
53-
* Delete a configuration value.
54-
*
55-
* SilverStripe does not provide 'delete' as such, but let's just save null
56-
* as the value for the session.
57-
*
58-
* @param string $name
59-
*/
60-
public function deleteConfiguration($name) {
61-
return $this->saveConfiguration($name, null);
62-
}
63-
64-
/**
65-
* Helper for Mollom to know this current client instance.
66-
*
67-
* @return array
68-
*/
69-
public function getClientInformation() {
70-
$info = new SapphireInfo();
71-
$useragent = 'SilverStripe/' . $info->Version();
72-
73-
$data = array(
74-
'platformName' => $useragent,
75-
'platformVersion' => $info->Version(),
76-
'clientName' => 'MollomPHP',
77-
'clientVersion' => 'Unknown',
78-
);
79-
80-
return $data;
81-
}
82-
83-
/**
84-
* Send the request to Mollom. Must return the result in the format
85-
* prescribed by the Mollom base class.
86-
*
87-
* @param string $method
88-
* @param string $server
89-
* @param string $path,
90-
* @param string $data
91-
* @param array $headers
92-
*/
93-
protected function request($method, $server, $path, $query = NULL, array $headers = array()) {
94-
// if the user has turned on debug mode in the Config API, change the
95-
// server to the dev version
96-
if(Config::inst()->get('Mollom', 'dev')) {
97-
$server = 'http://' . 'dev.mollom.com' . '/' . Mollom::API_VERSION;
98-
}
99-
else { // Mollom authentication headers should not be sent to the dev endpoint
100-
// CURLOPT_HTTPHEADER expects all headers as values:
101-
// @see http://php.net/manual/function.curl-setopt.php
102-
foreach ($headers as $name => &$value) {
103-
$value = $name . ': ' . $value;
104-
}
105-
}
106-
107-
$ch = curl_init();
108-
109-
// Compose the Mollom endpoint URL.
110-
$url = $server . '/' . $path;
111-
112-
if (isset($query) && $method == 'GET') {
113-
$url .= '?' . $query;
114-
}
115-
116-
curl_setopt($ch, CURLOPT_URL, $url);
117-
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
118-
119-
// Prevent API calls from taking too long.
120-
// Under normal operations, API calls may time out for Mollom users without
121-
// a paid subscription.
122-
curl_setopt($ch, CURLOPT_TIMEOUT, $this->requestTimeout);
123-
124-
if ($method == 'POST') {
125-
curl_setopt($ch, CURLOPT_POST, TRUE);
126-
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
127-
}
128-
else {
129-
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
130-
}
131-
132-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
133-
curl_setopt($ch, CURLOPT_HEADER, TRUE);
134-
135-
// Execute the HTTP request.
136-
if ($raw_response = curl_exec($ch)) {
137-
// Split the response headers from the response body.
138-
list($raw_response_headers, $response_body) = explode("\r\n\r\n", $raw_response, 2);
139-
140-
// Parse HTTP response headers.
141-
// @see http_parse_headers()
142-
$raw_response_headers = str_replace("\r", '', $raw_response_headers);
143-
$raw_response_headers = explode("\n", $raw_response_headers);
144-
$message = array_shift($raw_response_headers);
145-
$response_headers = array();
146-
147-
foreach ($raw_response_headers as $line) {
148-
list($name, $value) = explode(': ', $line, 2);
149-
// Mollom::handleRequest() expects response header names in lowercase.
150-
$response_headers[strtolower($name)] = $value;
151-
}
152-
153-
$info = curl_getinfo($ch);
154-
$response = array(
155-
'code' => $info['http_code'],
156-
'message' => $message,
157-
'headers' => $response_headers,
158-
'body' => $response_body,
159-
);
160-
}
161-
else {
162-
$response = array(
163-
'code' => curl_errno($ch),
164-
'message' => curl_error($ch),
165-
'body' => null
166-
);
167-
}
168-
169-
curl_close($ch);
170-
171-
return (object) $response;
172-
}
173-
174-
/**
175-
* Return the Field that we will use in this protector.
176-
*
177-
* @param string $name
178-
* @param string $title
179-
* @param mixed $value
180-
* @return MollomField
181-
*/
182-
public function getFormField($name = "MollomField", $title = "Captcha", $value = null) {
183-
$field = new MollomField($name, $title, $value);
184-
$field->setFieldMapping($this->fieldMapping);
185-
return $field;
186-
}
187-
188-
public function setFieldMapping($fieldMapping) {
189-
$this->fieldMapping = $fieldMapping;
190-
}
191-
13+
class MollomSpamProtector extends Mollom implements SpamProtector
14+
{
15+
16+
/**
17+
* @var array
18+
*/
19+
private $fieldMapping = array();
20+
21+
/**
22+
* @var array
23+
*/
24+
public $configurationMap = array(
25+
'publicKey' => 'public_key',
26+
'privateKey' => 'private_key',
27+
);
28+
29+
/**
30+
* Load configuration for a given variable such as privateKey. Since
31+
* SilverStripe uses YAML conventions, look for those variables
32+
*
33+
* @param string $name
34+
*
35+
* @return mixed
36+
*/
37+
public function loadConfiguration($name)
38+
{
39+
return Config::inst()->get('Mollom', $this->configurationMap[$name]);
40+
}
41+
42+
/**
43+
* Save configuration value for a given variable
44+
*
45+
* @param string $name
46+
* @param mixed $value
47+
*
48+
* @return void
49+
*/
50+
public function saveConfiguration($name, $value)
51+
{
52+
return Config::inst()->update('Mollom', $this->configurationMap[$name], $value);
53+
}
54+
55+
/**
56+
* Delete a configuration value.
57+
*
58+
* SilverStripe does not provide 'delete' as such, but let's just save null
59+
* as the value for the session.
60+
*
61+
* @param string $name
62+
*/
63+
public function deleteConfiguration($name)
64+
{
65+
return $this->saveConfiguration($name, null);
66+
}
67+
68+
/**
69+
* Helper for Mollom to know this current client instance.
70+
*
71+
* @return array
72+
*/
73+
public function getClientInformation()
74+
{
75+
$info = new SapphireInfo();
76+
$useragent = 'SilverStripe/' . $info->Version();
77+
78+
$data = array(
79+
'platformName' => $useragent,
80+
'platformVersion' => $info->Version(),
81+
'clientName' => 'MollomPHP',
82+
'clientVersion' => 'Unknown',
83+
);
84+
85+
return $data;
86+
}
87+
88+
/**
89+
* Send the request to Mollom. Must return the result in the format
90+
* prescribed by the Mollom base class.
91+
*
92+
* @param string $method
93+
* @param string $server
94+
* @param string $path,
95+
* @param string $data
96+
* @param array $headers
97+
*/
98+
protected function request($method, $server, $path, $query = null, array $headers = array())
99+
{
100+
// if the user has turned on debug mode in the Config API, change the
101+
// server to the dev version
102+
if (Config::inst()->get('Mollom', 'dev')) {
103+
$server = 'http://' . 'dev.mollom.com' . '/' . Mollom::API_VERSION;
104+
} else { // Mollom authentication headers should not be sent to the dev endpoint
105+
// CURLOPT_HTTPHEADER expects all headers as values:
106+
// @see http://php.net/manual/function.curl-setopt.php
107+
foreach ($headers as $name => &$value) {
108+
$value = $name . ': ' . $value;
109+
}
110+
}
111+
112+
$ch = curl_init();
113+
114+
// Compose the Mollom endpoint URL.
115+
$url = $server . '/' . $path;
116+
117+
if (isset($query) && $method == 'GET') {
118+
$url .= '?' . $query;
119+
}
120+
121+
curl_setopt($ch, CURLOPT_URL, $url);
122+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
123+
124+
// Prevent API calls from taking too long.
125+
// Under normal operations, API calls may time out for Mollom users without
126+
// a paid subscription.
127+
curl_setopt($ch, CURLOPT_TIMEOUT, $this->requestTimeout);
128+
129+
if ($method == 'POST') {
130+
curl_setopt($ch, CURLOPT_POST, true);
131+
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
132+
} else {
133+
curl_setopt($ch, CURLOPT_HTTPGET, true);
134+
}
135+
136+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
137+
curl_setopt($ch, CURLOPT_HEADER, true);
138+
139+
// Execute the HTTP request.
140+
if ($raw_response = curl_exec($ch)) {
141+
// Split the response headers from the response body.
142+
list($raw_response_headers, $response_body) = explode("\r\n\r\n", $raw_response, 2);
143+
144+
// Parse HTTP response headers.
145+
// @see http_parse_headers()
146+
$raw_response_headers = str_replace("\r", '', $raw_response_headers);
147+
$raw_response_headers = explode("\n", $raw_response_headers);
148+
$message = array_shift($raw_response_headers);
149+
$response_headers = array();
150+
151+
foreach ($raw_response_headers as $line) {
152+
list($name, $value) = explode(': ', $line, 2);
153+
// Mollom::handleRequest() expects response header names in lowercase.
154+
$response_headers[strtolower($name)] = $value;
155+
}
156+
157+
$info = curl_getinfo($ch);
158+
$response = array(
159+
'code' => $info['http_code'],
160+
'message' => $message,
161+
'headers' => $response_headers,
162+
'body' => $response_body,
163+
);
164+
} else {
165+
$response = array(
166+
'code' => curl_errno($ch),
167+
'message' => curl_error($ch),
168+
'body' => null
169+
);
170+
}
171+
172+
curl_close($ch);
173+
174+
return (object) $response;
175+
}
176+
177+
/**
178+
* Return the Field that we will use in this protector.
179+
*
180+
* @param string $name
181+
* @param string $title
182+
* @param mixed $value
183+
* @return MollomField
184+
*/
185+
public function getFormField($name = "MollomField", $title = "Captcha", $value = null)
186+
{
187+
$field = new MollomField($name, $title, $value);
188+
$field->setFieldMapping($this->fieldMapping);
189+
return $field;
190+
}
191+
192+
public function setFieldMapping($fieldMapping)
193+
{
194+
$this->fieldMapping = $fieldMapping;
195+
}
192196
}

0 commit comments

Comments
 (0)