[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/captcha/plugins/ -> recaptcha.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\captcha\plugins;
  15  
  16  class recaptcha extends captcha_abstract
  17  {
  18      var $recaptcha_server = 'http://www.google.com/recaptcha/api';
  19      var $recaptcha_server_secure = 'https://www.google.com/recaptcha/api'; // class constants :(
  20  
  21      // We are opening a socket to port 80 of this host and send
  22      // the POST request asking for verification to the path specified here.
  23      var $recaptcha_verify_server = 'www.google.com';
  24      var $recaptcha_verify_path = '/recaptcha/api/verify';
  25  
  26      var $challenge;
  27      var $response;
  28  
  29      /**
  30      * Constructor
  31      */
  32  	public function __construct()
  33      {
  34          global $request;
  35          $this->recaptcha_server = $request->is_secure() ? $this->recaptcha_server_secure : $this->recaptcha_server;
  36      }
  37  
  38  	function init($type)
  39      {
  40          global $config, $db, $user;
  41  
  42          $user->add_lang('captcha_recaptcha');
  43          parent::init($type);
  44          $this->challenge = request_var('recaptcha_challenge_field', '');
  45          $this->response = request_var('recaptcha_response_field', '');
  46      }
  47  
  48  	public function is_available()
  49      {
  50          global $config, $user;
  51          $user->add_lang('captcha_recaptcha');
  52          return (isset($config['recaptcha_pubkey']) && !empty($config['recaptcha_pubkey']));
  53      }
  54  
  55      /**
  56      *  API function
  57      */
  58  	function has_config()
  59      {
  60          return true;
  61      }
  62  
  63  	static public function get_name()
  64      {
  65          return 'CAPTCHA_RECAPTCHA';
  66      }
  67  
  68      /**
  69      * This function is implemented because required by the upper class, but is never used for reCaptcha.
  70      */
  71  	function get_generator_class()
  72      {
  73          throw new \Exception('No generator class given.');
  74      }
  75  
  76  	function acp_page($id, &$module)
  77      {
  78          global $config, $db, $template, $user;
  79  
  80          $captcha_vars = array(
  81              'recaptcha_pubkey'                => 'RECAPTCHA_PUBKEY',
  82              'recaptcha_privkey'                => 'RECAPTCHA_PRIVKEY',
  83          );
  84  
  85          $module->tpl_name = 'captcha_recaptcha_acp';
  86          $module->page_title = 'ACP_VC_SETTINGS';
  87          $form_key = 'acp_captcha';
  88          add_form_key($form_key);
  89  
  90          $submit = request_var('submit', '');
  91  
  92          if ($submit && check_form_key($form_key))
  93          {
  94              $captcha_vars = array_keys($captcha_vars);
  95              foreach ($captcha_vars as $captcha_var)
  96              {
  97                  $value = request_var($captcha_var, '');
  98                  if ($value)
  99                  {
 100                      set_config($captcha_var, $value);
 101                  }
 102              }
 103  
 104              add_log('admin', 'LOG_CONFIG_VISUAL');
 105              trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($module->u_action));
 106          }
 107          else if ($submit)
 108          {
 109              trigger_error($user->lang['FORM_INVALID'] . adm_back_link($module->u_action));
 110          }
 111          else
 112          {
 113              foreach ($captcha_vars as $captcha_var => $template_var)
 114              {
 115                  $var = (isset($_REQUEST[$captcha_var])) ? request_var($captcha_var, '') : ((isset($config[$captcha_var])) ? $config[$captcha_var] : '');
 116                  $template->assign_var($template_var, $var);
 117              }
 118  
 119              $template->assign_vars(array(
 120                  'CAPTCHA_PREVIEW'    => $this->get_demo_template($id),
 121                  'CAPTCHA_NAME'        => $this->get_service_name(),
 122                  'U_ACTION'            => $module->u_action,
 123              ));
 124  
 125          }
 126      }
 127  
 128      // not needed
 129  	function execute_demo()
 130      {
 131      }
 132  
 133      // not needed
 134  	function execute()
 135      {
 136      }
 137  
 138  	function get_template()
 139      {
 140          global $config, $user, $template, $phpbb_root_path, $phpEx;
 141  
 142          if ($this->is_solved())
 143          {
 144              return false;
 145          }
 146          else
 147          {
 148              $contact_link = phpbb_get_board_contact_link($config, $phpbb_root_path, $phpEx);
 149              $explain = $user->lang(($this->type != CONFIRM_POST) ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN', '<a href="' . $contact_link . '">', '</a>');
 150  
 151              $template->assign_vars(array(
 152                  'RECAPTCHA_SERVER'            => $this->recaptcha_server,
 153                  'RECAPTCHA_PUBKEY'            => isset($config['recaptcha_pubkey']) ? $config['recaptcha_pubkey'] : '',
 154                  'RECAPTCHA_ERRORGET'        => '',
 155                  'S_RECAPTCHA_AVAILABLE'        => self::is_available(),
 156                  'S_CONFIRM_CODE'            => true,
 157                  'S_TYPE'                    => $this->type,
 158                  'L_CONFIRM_EXPLAIN'            => $explain,
 159              ));
 160  
 161              return 'captcha_recaptcha.html';
 162          }
 163      }
 164  
 165  	function get_demo_template($id)
 166      {
 167          return $this->get_template();
 168      }
 169  
 170  	function get_hidden_fields()
 171      {
 172          $hidden_fields = array();
 173  
 174          // this is required for posting.php - otherwise we would forget about the captcha being already solved
 175          if ($this->solved)
 176          {
 177              $hidden_fields['confirm_code'] = $this->code;
 178          }
 179          $hidden_fields['confirm_id'] = $this->confirm_id;
 180          return $hidden_fields;
 181      }
 182  
 183  	function uninstall()
 184      {
 185          $this->garbage_collect(0);
 186      }
 187  
 188  	function install()
 189      {
 190          return;
 191      }
 192  
 193  	function validate()
 194      {
 195          if (!parent::validate())
 196          {
 197              return false;
 198          }
 199          else
 200          {
 201              return $this->recaptcha_check_answer();
 202          }
 203      }
 204  
 205  // Code from here on is based on recaptchalib.php
 206  /*
 207   * This is a PHP library that handles calling reCAPTCHA.
 208   *    - Documentation and latest version
 209   *          http://recaptcha.net/plugins/php/
 210   *    - Get a reCAPTCHA API Key
 211   *          http://recaptcha.net/api/getkey
 212   *    - Discussion group
 213   *          http://groups.google.com/group/recaptcha
 214   *
 215   * Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
 216   * AUTHORS:
 217   *   Mike Crawford
 218   *   Ben Maurer
 219   *
 220   * Permission is hereby granted, free of charge, to any person obtaining a copy
 221   * of this software and associated documentation files (the "Software"), to deal
 222   * in the Software without restriction, including without limitation the rights
 223   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 224   * copies of the Software, and to permit persons to whom the Software is
 225   * furnished to do so, subject to the following conditions:
 226   *
 227   * The above copyright notice and this permission notice shall be included in
 228   * all copies or substantial portions of the Software.
 229   *
 230   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 231   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 232   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 233   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 234   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 235   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 236   * THE SOFTWARE.
 237   */
 238  
 239      /**
 240      * Submits an HTTP POST to a reCAPTCHA server
 241      * @param string $host
 242      * @param string $path
 243      * @param array $data
 244      * @param int port
 245      * @return array response
 246      */
 247  	function _recaptcha_http_post($host, $path, $data, $port = 80)
 248      {
 249          $req = $this->_recaptcha_qsencode ($data);
 250  
 251          $http_request  = "POST $path HTTP/1.0\r\n";
 252          $http_request .= "Host: $host\r\n";
 253          $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
 254          $http_request .= "Content-Length: " . strlen($req) . "\r\n";
 255          $http_request .= "User-Agent: reCAPTCHA/PHP/phpBB\r\n";
 256          $http_request .= "\r\n";
 257          $http_request .= $req;
 258  
 259          $response = '';
 260          if (false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10)))
 261          {
 262              trigger_error('RECAPTCHA_SOCKET_ERROR', E_USER_ERROR);
 263          }
 264  
 265          fwrite($fs, $http_request);
 266  
 267          while (!feof($fs))
 268          {
 269              // One TCP-IP packet
 270              $response .= fgets($fs, 1160);
 271          }
 272          fclose($fs);
 273          $response = explode("\r\n\r\n", $response, 2);
 274  
 275          return $response;
 276      }
 277  
 278      /**
 279      * Calls an HTTP POST function to verify if the user's guess was correct
 280      * @param array $extra_params an array of extra variables to post to the server
 281      * @return ReCaptchaResponse
 282      */
 283  	function recaptcha_check_answer($extra_params = array())
 284      {
 285          global $config, $user;
 286  
 287          //discard spam submissions
 288          if ($this->challenge == null || strlen($this->challenge) == 0 || $this->response == null || strlen($this->response) == 0)
 289          {
 290              return $user->lang['RECAPTCHA_INCORRECT'];
 291          }
 292  
 293          $response = $this->_recaptcha_http_post($this->recaptcha_verify_server, $this->recaptcha_verify_path,
 294              array(
 295                  'privatekey'    => $config['recaptcha_privkey'],
 296                  'remoteip'        => $user->ip,
 297                  'challenge'        => $this->challenge,
 298                  'response'        => $this->response
 299              ) + $extra_params
 300          );
 301  
 302          $answers = explode("\n", $response[1]);
 303  
 304          if (trim($answers[0]) === 'true')
 305          {
 306              $this->solved = true;
 307              return false;
 308          }
 309          else
 310          {
 311              return $user->lang['RECAPTCHA_INCORRECT'];
 312          }
 313      }
 314  
 315      /**
 316      * Encodes the given data into a query string format
 317      * @param $data - array of string elements to be encoded
 318      * @return string - encoded request
 319      */
 320  	function _recaptcha_qsencode($data)
 321      {
 322          $req = '';
 323          foreach ($data as $key => $value)
 324          {
 325              $req .= $key . '=' . urlencode(stripslashes($value)) . '&';
 326          }
 327  
 328          // Cut the last '&'
 329          $req = substr($req, 0, strlen($req) - 1);
 330          return $req;
 331      }
 332  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1