Created
March 31, 2014 22:59
-
-
Save naeluh/9904276 to your computer and use it in GitHub Desktop.
captcha.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MollomMyPlatform extends Mollom | |
{ | |
public function loadConfiguration($name) | |
{ | |
// You may also hard-code your settings like this: | |
$config = array( | |
'publicKey' => 'key', | |
'privateKey' => 'key', | |
); | |
return $config[$name]; | |
} | |
public function saveConfiguration($name, $value) | |
{ | |
} | |
public function deleteConfiguration($name) | |
{ | |
} | |
public function getClientInformation() | |
{ | |
$data = array( | |
// Only supply different platform values, if your client maps to a | |
// public platform/application that may be used by others. | |
// Examples: Drupal 7.x, Wordpress 3.6, etc. | |
'platformName' => 'PHP', | |
'platformVersion' => PHP_VERSION, | |
// Always specify your implementation values here: | |
'clientName' => 'Mollom PHP client example', | |
'clientVersion' => '1.0', | |
); | |
return $data; | |
} | |
protected function request($method, $server, $path, $query = NULL, array $headers = array()) | |
{ | |
$ch = curl_init(); | |
// CURLOPT_HTTPHEADER expects all headers as values: | |
// @see http://php.net/manual/function.curl-setopt.php | |
foreach($headers as $name => & $value) { | |
$value = $name . ': ' . $value; | |
} | |
// Compose the Mollom endpoint URL. | |
$url = $server . '/' . $path; | |
if (isset($query) && $method == 'GET') { | |
$url.= '?' . $query; | |
} | |
curl_setopt($ch, CURLOPT_URL, $url); | |
// Send OAuth + other request headers. | |
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_VERBOSE, 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) , | |
); | |
} | |
curl_close($ch); | |
$response = (object)$response; | |
return $response; | |
} | |
} | |
// When a form is submitted: | |
$mollom = new MollomMyPlatform(); | |
$form = $_POST['email']; | |
$userval = $_POST['mollom_captcha_val']; | |
echo '<!--'; | |
print_r($mollom); | |
echo '-->'; | |
$result = $mollom->checkContent(array( | |
'checks' => array( | |
'spam' | |
) , | |
'postTitle' => $form['title'], | |
'postBody' => $form['body'], | |
'authorName' => $form['name'], | |
'authorUrl' => $form['homepage'], | |
'authorIp' => $_SERVER['REMOTE_ADDR'], | |
'authorId' => $userid, // If the author is logged in. | |
)); | |
echo '<!--'; | |
print_r($result); | |
echo '-->'; | |
echo '<!--'; | |
print_r($data); | |
echo '-->'; | |
print '<input type="text" id="captcha_id" name="captcha_id" value="' . $result['id'] . '">'; | |
// You might want to make the fallback case configurable: | |
if (!is_array($result) || !isset($result['id'])) { | |
print "The content moderation system is currently unavailable. Please try again later."; | |
die(); | |
} | |
// Check the final spam classification. | |
switch ($result['spamClassification']) { | |
case 'ham': | |
// Do nothing. (Accept content.) | |
break; | |
case 'spam': | |
// Discard (block) the form submission. | |
print "Your submission has triggered the spam filter and will not be accepted."; | |
die(); | |
break; | |
case 'unsure': | |
// Require to solve a CAPTCHA to get the post submitted. | |
$captcha = $mollom->createCaptcha(array( | |
'contentId' => $result['id'], | |
'type' => 'image', | |
)); | |
if (!is_array($captcha) || !isset($captcha['id'])) { | |
print "The content moderation system is currently unavailable. Please try again later."; | |
die(); | |
} | |
// Output the CAPTCHA. | |
print '<img src="' . $captcha['url'] . '" alt="Type the characters you see in this picture." />'; | |
print '<input type="text" name="captcha" size="10" value="" autocomplete="off" />'; | |
print '<input type="hidden" id="captcha_id" name="captcha_id" value="' . $result['id'] . '">'; | |
// Re-inject the submitted form values, re-render the form, | |
// and ask the user to solve the CAPTCHA. | |
break; | |
default: | |
// If we end up here, Mollom responded with a unknown spamClassification. | |
// Normally, this should not happen. | |
break; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment