mtrack /inc/captcha.php

Language PHP Lines 115
MD5 Hash 068fadef054192afdcc18a9c7267b6b5 Estimated Cost $2,231 (why?)
Repository https://bitbucket.org/yoander/mtrack View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php # vim:ts=2:sw=2:et:
/* For licensing and copyright terms, see the file named LICENSE */

interface IMTrackCaptchImplementation {
  /** return the captcha content */
  function emit($form);
  /** check that the captcha is good
   * Returns true/false */
  function check($form);
}

class MTrackCaptcha {
  static $impl = null;

  static function register(IMTrackCaptchImplementation $impl)
  {
    self::$impl = $impl;
  }

  static function emit($form)
  {
    if (self::$impl !== null) {
      return self::$impl->emit($form);
    }
    return '';
  }

  static function check($form)
  {
    if (self::$impl !== null) {
      return self::$impl->check($form);
    }
    return true;
  }
}

class MTrackCaptcha_Recaptcha implements IMTrackCaptchImplementation {
  public $errcode = null;
  public $pub;
  public $priv;
  public $userclass;

  function __construct($pub, $priv, $userclass = 'anonymous|authenticated') {
    $this->pub = $pub;
    $this->priv = $priv;
    $this->userclass = explode("|", $userclass);
    MTrackCaptcha::register($this);
  }

  function emit($form)
  {
    $class = MTrackAuth::getUserClass();
    if (!in_array($class, $this->userclass)) {
      return '';
    }
    $pub = $this->pub;
    $err = $this->errcode === null ? '' : "&error=$this->errcode";
    $http = $_SERVER['HTTPS'] ? 'https' : 'http';
    return <<<HTML
<script type='text/javascript'
  src="$http://www.google.com/recaptcha/api/challenge?k=$pub$err">
</script>
<noscript>
  <iframe src="$http://www.google.com/recaptcha/api/noscript?k=$pub$err"
    height="300" width="500" frameborder="0"></iframe>
  <br/>
  <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
  <input type="hidden" name="recaptcha_response_field"
    value="manual_challenge"/>
</noscript>
HTML;
  }

  function check($form)
  {
    $class = MTrackAuth::getUserClass();
    if (!in_array($class, $this->userclass)) {
      return true;
    }
    if (empty($_POST['recaptcha_challenge_field']) or
        empty($_POST['recaptcha_response_field'])) {
      return array('false', 'incorrect-captcha-sol');
    }

    $data = http_build_query(array(
          'privatekey' => $this->priv,
          'remoteip' => $_SERVER['REMOTE_ADDR'],
          'challenge' => $_POST['recaptcha_challenge_field'],
          'response' => $_POST['recaptcha_response_field'],
          ));
    $params = array(
        'http' => array(
          'method' => 'POST',
          'content' => $data,
          ),
        );
    $ctx = stream_context_create($params);

    /* first line: true/false
     * second line: error code
     */
    $res = array();
    foreach (file('http://www.google.com/recaptcha/api/verify', 0, $ctx) as $line) {
      $res[] = trim($line);
    }
    if ($res[0] == 'true') {
      return true;
    }
    $this->errcode = $res[1];
    return array(false, $this->errcode);
  }

}
Back to Top