indvdum@0: recaptcha_server = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? $this->recaptcha_server_secure : $this->recaptcha_server; indvdum@0: } indvdum@0: indvdum@0: function init($type) indvdum@0: { indvdum@0: global $config, $db, $user; indvdum@0: indvdum@0: $user->add_lang('captcha_recaptcha'); indvdum@0: parent::init($type); indvdum@0: $this->challenge = request_var('recaptcha_challenge_field', ''); indvdum@0: $this->response = request_var('recaptcha_response_field', ''); indvdum@0: } indvdum@0: indvdum@0: function &get_instance() indvdum@0: { indvdum@0: $instance =& new phpbb_recaptcha(); indvdum@0: return $instance; indvdum@0: } indvdum@0: indvdum@0: function is_available() indvdum@0: { indvdum@0: global $config, $user; indvdum@0: $user->add_lang('captcha_recaptcha'); indvdum@0: return (isset($config['recaptcha_pubkey']) && !empty($config['recaptcha_pubkey'])); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * API function indvdum@0: */ indvdum@0: function has_config() indvdum@0: { indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: function get_name() indvdum@0: { indvdum@0: return 'CAPTCHA_RECAPTCHA'; indvdum@0: } indvdum@0: indvdum@0: function get_class_name() indvdum@0: { indvdum@0: return 'phpbb_recaptcha'; indvdum@0: } indvdum@0: indvdum@0: function acp_page($id, &$module) indvdum@0: { indvdum@0: global $config, $db, $template, $user; indvdum@0: indvdum@0: $captcha_vars = array( indvdum@0: 'recaptcha_pubkey' => 'RECAPTCHA_PUBKEY', indvdum@0: 'recaptcha_privkey' => 'RECAPTCHA_PRIVKEY', indvdum@0: ); indvdum@0: indvdum@0: $module->tpl_name = 'captcha_recaptcha_acp'; indvdum@0: $module->page_title = 'ACP_VC_SETTINGS'; indvdum@0: $form_key = 'acp_captcha'; indvdum@0: add_form_key($form_key); indvdum@0: indvdum@0: $submit = request_var('submit', ''); indvdum@0: indvdum@0: if ($submit && check_form_key($form_key)) indvdum@0: { indvdum@0: $captcha_vars = array_keys($captcha_vars); indvdum@0: foreach ($captcha_vars as $captcha_var) indvdum@0: { indvdum@0: $value = request_var($captcha_var, ''); indvdum@0: if ($value) indvdum@0: { indvdum@0: set_config($captcha_var, $value); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: add_log('admin', 'LOG_CONFIG_VISUAL'); indvdum@0: trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($module->u_action)); indvdum@0: } indvdum@0: else if ($submit) indvdum@0: { indvdum@0: trigger_error($user->lang['FORM_INVALID'] . adm_back_link($module->u_action)); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: foreach ($captcha_vars as $captcha_var => $template_var) indvdum@0: { indvdum@0: $var = (isset($_REQUEST[$captcha_var])) ? request_var($captcha_var, '') : ((isset($config[$captcha_var])) ? $config[$captcha_var] : ''); indvdum@0: $template->assign_var($template_var, $var); indvdum@0: } indvdum@0: indvdum@0: $template->assign_vars(array( indvdum@0: 'CAPTCHA_PREVIEW' => $this->get_demo_template($id), indvdum@0: 'CAPTCHA_NAME' => $this->get_class_name(), indvdum@0: 'U_ACTION' => $module->u_action, indvdum@0: )); indvdum@0: indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // not needed indvdum@0: function execute_demo() indvdum@0: { indvdum@0: } indvdum@0: indvdum@0: // not needed indvdum@0: function execute() indvdum@0: { indvdum@0: } indvdum@0: indvdum@0: function get_template() indvdum@0: { indvdum@0: global $config, $user, $template; indvdum@0: indvdum@0: if ($this->is_solved()) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $explain = $user->lang(($this->type != CONFIRM_POST) ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN', '', ''); indvdum@0: indvdum@0: $template->assign_vars(array( indvdum@0: 'RECAPTCHA_SERVER' => $this->recaptcha_server, indvdum@0: 'RECAPTCHA_PUBKEY' => isset($config['recaptcha_pubkey']) ? $config['recaptcha_pubkey'] : '', indvdum@0: 'RECAPTCHA_ERRORGET' => '', indvdum@0: 'S_RECAPTCHA_AVAILABLE' => $this->is_available(), indvdum@0: 'S_CONFIRM_CODE' => true, indvdum@0: 'S_TYPE' => $this->type, indvdum@0: 'L_CONFIRM_EXPLAIN' => $explain, indvdum@0: )); indvdum@0: indvdum@0: return 'captcha_recaptcha.html'; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: function get_demo_template($id) indvdum@0: { indvdum@0: return $this->get_template(); indvdum@0: } indvdum@0: indvdum@0: function get_hidden_fields() indvdum@0: { indvdum@0: $hidden_fields = array(); indvdum@0: indvdum@0: // this is required for posting.php - otherwise we would forget about the captcha being already solved indvdum@0: if ($this->solved) indvdum@0: { indvdum@0: $hidden_fields['confirm_code'] = $this->code; indvdum@0: } indvdum@0: $hidden_fields['confirm_id'] = $this->confirm_id; indvdum@0: return $hidden_fields; indvdum@0: } indvdum@0: indvdum@0: function uninstall() indvdum@0: { indvdum@0: $this->garbage_collect(0); indvdum@0: } indvdum@0: indvdum@0: function install() indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: function validate() indvdum@0: { indvdum@0: if (!parent::validate()) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: return $this->recaptcha_check_answer(); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Code from here on is based on recaptchalib.php indvdum@0: /* indvdum@0: * This is a PHP library that handles calling reCAPTCHA. indvdum@0: * - Documentation and latest version indvdum@0: * http://recaptcha.net/plugins/php/ indvdum@0: * - Get a reCAPTCHA API Key indvdum@0: * http://recaptcha.net/api/getkey indvdum@0: * - Discussion group indvdum@0: * http://groups.google.com/group/recaptcha indvdum@0: * indvdum@0: * Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net indvdum@0: * AUTHORS: indvdum@0: * Mike Crawford indvdum@0: * Ben Maurer indvdum@0: * indvdum@0: * Permission is hereby granted, free of charge, to any person obtaining a copy indvdum@0: * of this software and associated documentation files (the "Software"), to deal indvdum@0: * in the Software without restriction, including without limitation the rights indvdum@0: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell indvdum@0: * copies of the Software, and to permit persons to whom the Software is indvdum@0: * furnished to do so, subject to the following conditions: indvdum@0: * indvdum@0: * The above copyright notice and this permission notice shall be included in indvdum@0: * all copies or substantial portions of the Software. indvdum@0: * indvdum@0: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR indvdum@0: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, indvdum@0: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE indvdum@0: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER indvdum@0: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, indvdum@0: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN indvdum@0: * THE SOFTWARE. indvdum@0: */ indvdum@0: indvdum@0: /** indvdum@0: * Submits an HTTP POST to a reCAPTCHA server indvdum@0: * @param string $host indvdum@0: * @param string $path indvdum@0: * @param array $data indvdum@0: * @param int port indvdum@0: * @return array response indvdum@0: */ indvdum@0: function _recaptcha_http_post($host, $path, $data, $port = 80) indvdum@0: { indvdum@0: $req = $this->_recaptcha_qsencode ($data); indvdum@0: indvdum@0: $http_request = "POST $path HTTP/1.0\r\n"; indvdum@0: $http_request .= "Host: $host\r\n"; indvdum@0: $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n"; indvdum@0: $http_request .= "Content-Length: " . strlen($req) . "\r\n"; indvdum@0: $http_request .= "User-Agent: reCAPTCHA/PHP/phpBB\r\n"; indvdum@0: $http_request .= "\r\n"; indvdum@0: $http_request .= $req; indvdum@0: indvdum@0: $response = ''; indvdum@0: if (false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10))) indvdum@0: { indvdum@0: trigger_error('Could not open socket', E_USER_ERROR); indvdum@0: } indvdum@0: indvdum@0: fwrite($fs, $http_request); indvdum@0: indvdum@0: while (!feof($fs)) indvdum@0: { indvdum@0: // One TCP-IP packet indvdum@0: $response .= fgets($fs, 1160); indvdum@0: } indvdum@0: fclose($fs); indvdum@0: $response = explode("\r\n\r\n", $response, 2); indvdum@0: indvdum@0: return $response; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Calls an HTTP POST function to verify if the user's guess was correct indvdum@0: * @param array $extra_params an array of extra variables to post to the server indvdum@0: * @return ReCaptchaResponse indvdum@0: */ indvdum@0: function recaptcha_check_answer($extra_params = array()) indvdum@0: { indvdum@0: global $config, $user; indvdum@0: indvdum@0: //discard spam submissions indvdum@0: if ($this->challenge == null || strlen($this->challenge) == 0 || $this->response == null || strlen($this->response) == 0) indvdum@0: { indvdum@0: return $user->lang['RECAPTCHA_INCORRECT']; indvdum@0: } indvdum@0: indvdum@0: $response = $this->_recaptcha_http_post($this->recaptcha_verify_server, $this->recaptcha_verify_path, indvdum@0: array( indvdum@0: 'privatekey' => $config['recaptcha_privkey'], indvdum@0: 'remoteip' => $user->ip, indvdum@0: 'challenge' => $this->challenge, indvdum@0: 'response' => $this->response indvdum@0: ) + $extra_params indvdum@0: ); indvdum@0: indvdum@0: $answers = explode("\n", $response[1]); indvdum@0: indvdum@0: if (trim($answers[0]) === 'true') indvdum@0: { indvdum@0: $this->solved = true; indvdum@0: return false; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: return $user->lang['RECAPTCHA_INCORRECT']; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Encodes the given data into a query string format indvdum@0: * @param $data - array of string elements to be encoded indvdum@0: * @return string - encoded request indvdum@0: */ indvdum@0: function _recaptcha_qsencode($data) indvdum@0: { indvdum@0: $req = ''; indvdum@0: foreach ($data as $key => $value) indvdum@0: { indvdum@0: $req .= $key . '=' . urlencode(stripslashes($value)) . '&'; indvdum@0: } indvdum@0: indvdum@0: // Cut the last '&' indvdum@0: $req = substr($req, 0, strlen($req) - 1); indvdum@0: return $req; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: ?>