Skip to content

Commit 9b5028b

Browse files
committed
SolveMedia support
SolveMedia support
1 parent 933efed commit 9b5028b

File tree

3 files changed

+249
-1
lines changed

3 files changed

+249
-1
lines changed

config.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
"use_captcha" => true, // require the user to enter a captcha
7171

72-
"captcha" => "recaptcha", // valid options: recaptcha, recaptcha2
72+
"captcha" => "recaptcha", // valid options: recaptcha, recaptcha2, solvemedia
7373

7474
"captcha_https" => false, // use https (only for recaptcha2) valid options: true, false
7575

@@ -79,6 +79,12 @@
7979
"public_key" => "publickey"
8080
),
8181

82+
// enter your private and public solvemedia key here:
83+
"solvemedia_config" => array(
84+
"public_key" => "publickey",
85+
"private_key" => "privatekey",
86+
"hash_key" => "hashkey"
87+
),
8288

8389
// proxy filter:
8490
"filter_proxies" => true, // whether to filter proxies or not. It's up to you to fill the proxy ban table. (see also the tor node cron job in ./lib/proxy_filter/cron/tor.php)

lib/simple_faucet.php

+13
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public function __construct($config)
7070
require_once('./lib/recaptchalib.php');
7171
elseif ($this->config["captcha"] == "recaptcha2")
7272
require_once('./lib/recaptchalib2.php');
73+
elseif ($this->config["captcha"] == "solvemedia")
74+
require_once('./lib/solvemedia.php');
7375

7476
if (isset($config["rpc_user"],$config["rpc_password"],$config["rpc_host"],$config["rpc_port"],$config["mysql_user"],$config["mysql_password"],$config["mysql_host"],$config["mysql_database"]))
7577
{
@@ -300,6 +302,8 @@ public function render()
300302
{
301303
if ($config["captcha"] == "recaptcha" || $config["captcha"] == "recaptcha2")
302304
return recaptcha_get_html(@$config["captcha_config"]["public_key"]);
305+
if ($config["captcha"] == "solvemedia")
306+
return solvemedia_get_html(@$config["solvemedia_config"]["public_key"]);
303307
}
304308
return '';
305309
case "ads":
@@ -353,6 +357,15 @@ protected function valid_captcha()
353357
$resp = @recaptcha_check_answer($this->config["captcha_config"]["private_key"],$_SERVER["REMOTE_ADDR"],@$_POST["recaptcha_challenge_field"],@$_POST["recaptcha_response_field"]);
354358
return $resp->is_valid; // $resp->error;
355359
}
360+
elseif ($this->config["captcha"] == "solvemedia")
361+
{
362+
$resp = @solvemedia_check_answer($this->config["solvemedia_config"]["private_key"],
363+
$_SERVER["REMOTE_ADDR"],
364+
@$_POST["adcopy_challenge"],
365+
@$_POST["adcopy_response"],
366+
$this->config["solvemedia_config"]["hash_key"]);
367+
return $resp->is_valid; // $resp->error;
368+
}
356369
else
357370
return recaptcha_check_answer($this->config["captcha_config"]["private_key"],@$_POST["g-recaptcha-response"],$this->config["captcha_https"]);
358371
return false;

lib/solvemedia.php

+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
<?php
2+
/*
3+
* Copyright (c) 2009 by Jeff Weisberg
4+
* Author: Jeff Weisberg
5+
* Created: 2009-Jun-22 16:44 (EDT)
6+
* Function: SolveMedia API php code
7+
*
8+
* $Id$
9+
*
10+
* This is a PHP library that handles calling SolveMedia.
11+
* - Documentation and latest version
12+
* http://www.solvemedia.com/
13+
* - Get a SolveMedia API Keys
14+
* http://api.solvemedia.com/public/signup
15+
*/
16+
17+
/* This code is based on code from,
18+
* and copied, modified and distributed with permission in accordance with its terms:
19+
*
20+
* Copyright (c) 2007 reCAPTCHA
21+
* AUTHORS:
22+
* Mike Crawford
23+
* Ben Maurer
24+
*
25+
* Permission is hereby granted, free of charge, to any person obtaining a copy
26+
* of this software and associated documentation files (the "Software"), to deal
27+
* in the Software without restriction, including without limitation the rights
28+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29+
* copies of the Software, and to permit persons to whom the Software is
30+
* furnished to do so, subject to the following conditions:
31+
*
32+
* The above copyright notice and this permission notice shall be included in
33+
* all copies or substantial portions of the Software.
34+
*
35+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41+
* THE SOFTWARE.
42+
*/
43+
44+
/**
45+
* The solvemedia server URL's
46+
*/
47+
define("ADCOPY_API_SERVER", "http://api.solvemedia.com");
48+
define("ADCOPY_API_SECURE_SERVER", "https://api-secure.solvemedia.com");
49+
define("ADCOPY_VERIFY_SERVER", "verify.solvemedia.com");
50+
define("ADCOPY_SIGNUP", "http://api.solvemedia.com/public/signup");
51+
52+
/**
53+
* Encodes the given data into a query string format
54+
* @param $data - array of string elements to be encoded
55+
* @return string - encoded request
56+
*/
57+
function _adcopy_qsencode ($data) {
58+
$req = "";
59+
foreach ( $data as $key => $value )
60+
$req .= $key . '=' . urlencode( stripslashes($value) ) . '&';
61+
62+
// Cut the last '&'
63+
$req=substr($req,0,strlen($req)-1);
64+
return $req;
65+
}
66+
67+
68+
69+
/**
70+
* Submits an HTTP POST to a solvemedia server
71+
* @param string $host
72+
* @param string $path
73+
* @param array $data
74+
* @param int port
75+
* @return array response
76+
*/
77+
function _adcopy_http_post($host, $path, $data, $port = 80) {
78+
79+
$req = _adcopy_qsencode ($data);
80+
81+
$http_request = "POST $path HTTP/1.0\r\n";
82+
$http_request .= "Host: $host\r\n";
83+
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
84+
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
85+
$http_request .= "User-Agent: solvemedia/PHP\r\n";
86+
$http_request .= "\r\n";
87+
$http_request .= $req;
88+
89+
$response = '';
90+
if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
91+
die ('Could not open socket');
92+
}
93+
94+
fwrite($fs, $http_request);
95+
96+
while ( !feof($fs) )
97+
$response .= fgets($fs, 1024); // One TCP-IP packet [sic]
98+
fclose($fs);
99+
$response = explode("\r\n\r\n", $response, 2);
100+
101+
return $response;
102+
}
103+
104+
105+
106+
/**
107+
* Gets the challenge HTML (javascript and non-javascript version).
108+
* This is called from the browser, and the resulting solvemedia HTML widget
109+
* is embedded within the HTML form it was called from.
110+
* @param string $pubkey A public key for solvemedia
111+
* @param string $error The error given by solvemedia (optional, default is null)
112+
* @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
113+
114+
* @return string - The HTML to be embedded in the user's form.
115+
*/
116+
function solvemedia_get_html ($pubkey, $error = null, $use_ssl = false)
117+
{
118+
if ($pubkey == null || $pubkey == '') {
119+
die ("To use solvemedia you must get an API key from <a href='" . ADCOPY_SIGNUP . "'>" . ADCOPY_SIGNUP . "</a>");
120+
}
121+
122+
if ($use_ssl) {
123+
$server = ADCOPY_API_SECURE_SERVER;
124+
} else {
125+
$server = ADCOPY_API_SERVER;
126+
}
127+
128+
$errorpart = "";
129+
if ($error) {
130+
$errorpart = ";error=1";
131+
}
132+
return '<script type="text/javascript" src="'. $server . '/papi/challenge.script?k=' . $pubkey . $errorpart . '"></script>
133+
134+
<noscript>
135+
<iframe src="'. $server . '/papi/challenge.noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
136+
<textarea name="adcopy_challenge" rows="3" cols="40"></textarea>
137+
<input type="hidden" name="adcopy_response" value="manual_challenge"/>
138+
</noscript>';
139+
}
140+
141+
142+
143+
144+
/**
145+
* A SolveMediaResponse is returned from solvemedia_check_answer()
146+
*/
147+
class SolveMediaResponse {
148+
var $is_valid;
149+
var $error;
150+
}
151+
152+
153+
/**
154+
* Calls an HTTP POST function to verify if the user's guess was correct
155+
* @param string $privkey
156+
* @param string $remoteip
157+
* @param string $challenge
158+
* @param string $response
159+
* @param string $hashkey
160+
* @return SolveMediaResponse
161+
*/
162+
function solvemedia_check_answer ($privkey, $remoteip, $challenge, $response, $hashkey = '' )
163+
{
164+
if ($privkey == null || $privkey == '') {
165+
die ("To use solvemedia you must get an API key from <a href='" . ADCOPY_SIGNUP . "'>" . ADCOPY_SIGNUP . "</a>");
166+
}
167+
168+
if ($remoteip == null || $remoteip == '') {
169+
die ("For security reasons, you must pass the remote ip to solvemedia");
170+
}
171+
172+
//discard spam submissions
173+
if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) {
174+
$adcopy_response = new SolveMediaResponse();
175+
$adcopy_response->is_valid = false;
176+
$adcopy_response->error = 'incorrect-solution';
177+
return $adcopy_response;
178+
}
179+
180+
$response = _adcopy_http_post (ADCOPY_VERIFY_SERVER, "/papi/verify",
181+
array (
182+
'privatekey' => $privkey,
183+
'remoteip' => $remoteip,
184+
'challenge' => $challenge,
185+
'response' => $response
186+
)
187+
);
188+
189+
$answers = explode ("\n", $response [1]);
190+
$adcopy_response = new SolveMediaResponse();
191+
192+
if( strlen($hashkey) ){
193+
# validate message authenticator
194+
$hash = sha1( $answers[0] . $challenge . $hashkey );
195+
196+
if( $hash != $answers[2] ){
197+
$adcopy_response->is_valid = false;
198+
$adcopy_response->error = 'hash-fail';
199+
return $adcopy_response;
200+
}
201+
}
202+
203+
if (trim ($answers [0]) == 'true') {
204+
$adcopy_response->is_valid = true;
205+
}
206+
else {
207+
$adcopy_response->is_valid = false;
208+
$adcopy_response->error = $answers [1];
209+
}
210+
return $adcopy_response;
211+
212+
}
213+
214+
/**
215+
* gets a URL where the user can sign up for solvemedia. If your application
216+
* has a configuration page where you enter a key, you should provide a link
217+
* using this function.
218+
* @param string $domain The domain where the page is hosted
219+
* @param string $appname The name of your application
220+
*/
221+
function solvemedia_get_signup_url ($domain = null, $appname = null) {
222+
return ADCOPY_SIGNUP . "?" . _adcopy_qsencode (array ('domain' => $domain, 'app' => $appname));
223+
}
224+
225+
226+
/* Mailhide related code */
227+
/* [ deleted ] */
228+
229+
?>

0 commit comments

Comments
 (0)