Source of file MollomSpamProtector.php
Size: 5,011 Bytes - Last Modified: 2021-12-23T10:33:14+00:00
/var/www/docs.ssmods.com/process/src/code/MollomSpamProtector.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 | <?php require_once(BASE_PATH . '/vendor/mollom/client/mollom.class.inc'); /** * A customized version of {@link Mollom} to run on SilverStripe. See the * MollomPHP page (https://github.com/mollom/MollomPHP) for more information * about the abstracted methods. * * @package mollom */ class MollomSpamProtector extends Mollom implements SpamProtector { /** * @var array */ private $fieldMapping = array(); /** * @var array */ public $configurationMap = array( 'publicKey' => 'public_key', 'privateKey' => 'private_key', ); /** * Load configuration for a given variable such as privateKey. Since * SilverStripe uses YAML conventions, look for those variables * * @param string $name * * @return mixed */ public function loadConfiguration($name) { return Config::inst()->get('Mollom', $this->configurationMap[$name]); } /** * Save configuration value for a given variable * * @param string $name * @param mixed $value * * @return void */ public function saveConfiguration($name, $value) { return Config::inst()->update('Mollom', $this->configurationMap[$name], $value); } /** * Delete a configuration value. * * SilverStripe does not provide 'delete' as such, but let's just save null * as the value for the session. * * @param string $name */ public function deleteConfiguration($name) { return $this->saveConfiguration($name, null); } /** * Helper for Mollom to know this current client instance. * * @return array */ public function getClientInformation() { $info = new SapphireInfo(); $useragent = 'SilverStripe/' . $info->Version(); $data = array( 'platformName' => $useragent, 'platformVersion' => $info->Version(), 'clientName' => 'MollomPHP', 'clientVersion' => 'Unknown', ); return $data; } /** * Send the request to Mollom. Must return the result in the format * prescribed by the Mollom base class. * * @param string $method * @param string $server * @param string $path, * @param string $data * @param array $headers */ protected function request($method, $server, $path, $query = NULL, array $headers = array()) { // if the user has turned on debug mode in the Config API, change the // server to the dev version if(Config::inst()->get('Mollom', 'dev')) { $server = 'http://' . 'dev.mollom.com' . '/' . Mollom::API_VERSION; } else { // Mollom authentication headers should not be sent to the dev endpoint // CURLOPT_HTTPHEADER expects all headers as values: // @see http://php.net/manual/function.curl-setopt.php foreach ($headers as $name => &$value) { $value = $name . ': ' . $value; } } $ch = curl_init(); // Compose the Mollom endpoint URL. $url = $server . '/' . $path; if (isset($query) && $method == 'GET') { $url .= '?' . $query; } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // Prevent API calls from taking too long. // Under normal operations, API calls may time out for Mollom users without // a paid subscription. curl_setopt($ch, CURLOPT_TIMEOUT, $this->requestTimeout); if ($method == 'POST') { curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $query); } else { curl_setopt($ch, CURLOPT_HTTPGET, TRUE); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, TRUE); // Execute the HTTP request. if ($raw_response = curl_exec($ch)) { // Split the response headers from the response body. list($raw_response_headers, $response_body) = explode("\r\n\r\n", $raw_response, 2); // Parse HTTP response headers. // @see http_parse_headers() $raw_response_headers = str_replace("\r", '', $raw_response_headers); $raw_response_headers = explode("\n", $raw_response_headers); $message = array_shift($raw_response_headers); $response_headers = array(); foreach ($raw_response_headers as $line) { list($name, $value) = explode(': ', $line, 2); // Mollom::handleRequest() expects response header names in lowercase. $response_headers[strtolower($name)] = $value; } $info = curl_getinfo($ch); $response = array( 'code' => $info['http_code'], 'message' => $message, 'headers' => $response_headers, 'body' => $response_body, ); } else { $response = array( 'code' => curl_errno($ch), 'message' => curl_error($ch), 'body' => null ); } curl_close($ch); return (object) $response; } /** * Return the Field that we will use in this protector. * * @param string $name * @param string $title * @param mixed $value * @return MollomField */ public function getFormField($name = "MollomField", $title = "Captcha", $value = null) { $field = new MollomField($name, $title, $value); $field->setFieldMapping($this->fieldMapping); return $field; } public function setFieldMapping($fieldMapping) { $this->fieldMapping = $fieldMapping; } } |