1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
4 interface IMTrackCaptchImplementation {
5 /** return the captcha content */
7 /** check that the captcha is good
8 * Returns true/false */
15 static function register(IMTrackCaptchImplementation $impl)
20 static function emit($form)
22 if (self::$impl !== null) {
23 return self::$impl->emit($form);
28 static function check($form)
30 if (self::$impl !== null) {
31 return self::$impl->check($form);
37 class MTrackCaptcha_Recaptcha implements IMTrackCaptchImplementation {
38 public $errcode = null;
43 function __construct($pub, $priv, $userclass = 'anonymous|authenticated') {
46 $this->userclass = explode("|", $userclass);
47 MTrackCaptcha::register($this);
52 $class = MTrackAuth::getUserClass();
53 if (!in_array($class, $this->userclass)) {
57 $err = $this->errcode === null ? '' : "&error=$this->errcode";
59 <script type='text/javascript' src="https://api-secure.recaptcha.net/challenge?k=$pub$err"></script>
61 <iframe src="https://api-secure.recaptcha.net/noscript?k=$pub$err"
62 height="300" width="500" frameborder="0"></iframe>
64 <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
65 <input type="hidden" name="recaptcha_response_field"
66 value="manual_challenge"/>
73 $class = MTrackAuth::getUserClass();
74 if (!in_array($class, $this->userclass)) {
77 if (empty($_POST['recaptcha_challenge_field']) or
78 empty($_POST['recaptcha_response_field'])) {
79 return array('false', 'incorrect-captcha-sol');
82 $data = http_build_query(array(
83 'privatekey' => $this->priv,
84 'remoteip' => $_SERVER['REMOTE_ADDR'],
85 'challenge' => $_POST['recaptcha_challenge_field'],
86 'response' => $_POST['recaptcha_response_field'],
94 $ctx = stream_context_create($params);
96 /* first line: true/false
97 * second line: error code
100 foreach (file('http://api-verify.recaptcha.net/verify', 0, $ctx) as $line) {
101 $res[] = trim($line);
103 if ($res[0] == 'true') {
106 $this->errcode = $res[1];