public function __construct() { if (!self::$scripts_added) { self::$scripts_added = true; Context::loadFile(array('./addons/recaptcha/recaptcha.js', 'body')); Context::addHtmlFooter('<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=reCaptchaCallback" async defer></script>'); $html = '<div id="recaptcha-config" data-sitekey="%s" data-theme="%s" data-size="%s"></div>'; $html = sprintf($html, escape(self::$config->site_key), self::$config->theme ?: 'light', self::$config->size ?: 'normal'); Context::addHtmlFooter($html); } }
public static function check($challenge, $response) { self::$error_msg = false; if (empty($challenge) || empty($response)) { self::$error_msg = 'Invalid challenge or response'; return false; } $params = array('privatekey' => ConfigValue::get('recaptcha.PrivateKey'), 'remoteip' => $_SERVER['REMOTE_ADDR'], 'challenge' => $challenge, 'response' => $response); $result = curl_request('http://api-verify.recaptcha.net/verify', $params, array('method' => 'post')); if (!$result) { self::$error_msg = 'Could not contact reCAPTCHA server'; return false; } $result = explode("\n", $result); if ($result[0] != 'true') { self::$error_msg = $result[1]; return false; } return true; }
/** * Save a new comment * @return bool */ function save() { global $DB, $ID, $USER, $CURRENT, $CONFIG; $_POST->setType('commentbody', 'any'); $_POST->setType('author', 'string'); if ($USER->ID === NOBODY) { if ($CONFIG->comments->CAPTCHA_for_guests && !reCAPTCHA::verify()) { Flash::create(__('CAPTCHA verification failed'), 'warning'); return false; } $ctype = $CONFIG->comments->guest_comments; } else { $ctype = $CONFIG->comments->user_comments; } if ($ctype == 'none') { return false; } $DB->comments->insert(array('id' => $ID, 'comment' => $_POST['commentbody'], 'author' => $_POST['author'] && $USER->ID === NOBODY ? $_POST['author'] : $USER->ID, 'ip' => $_SERVER['REMOTE_ADDR'], 'authd_by' => $CURRENT->mayI(EDIT) ? $USER->ID : ($ctype == 'review' ? 0 : $USER->ID), 'created' => time())); $_POST->clear('commentbody', 'author'); return true; }
$control = clone $this->control; $control->add($this->getRecaptchaHTML($error)); $control->add($this->getError($error)); return $control; } protected function getRecaptchaHTML(&$error) { return recaptcha_get_html(self::$publicKey, $error); } protected function getError(&$error) { $status = clone $this->status; $status->class('error'); $status->setText($error); return $status; } public function getInput() { $control = parent::getControl(); $control->value = $this->value === '' ? $this->emptyValue : $this->tmpValue; return $control; } public static function validate(IFormControl $control) { $response = recaptcha_check_answer(self::$privateKey, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']); return $response->is_valid; } } reCAPTCHA::$libPath = dirname(__FILE__) . '/reCaptchaLib.php'; /** add Nette\FormContainer method */ NFormContainer::extensionMethod('NFormContainer::addRecaptcha', array('reCAPTCHA', 'addRecaptcha'));
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ /** * The reCAPTCHA server URL's */ define("RECAPTCHA_API_SERVER", "http://api.recaptcha.net"); define("RECAPTCHA_API_SECURE_SERVER", "https://api-secure.recaptcha.net"); define("RECAPTCHA_VERIFY_SERVER", "api-verify.recaptcha.net"); global $CONFIG; $CONFIG->UI->setType('reCAPTCHA_public_key', 'text'); $CONFIG->UI->setDescription('reCAPTCHA_public_key', __('Visit') . ' <a href="' . reCAPTCHA::recaptcha_get_signup_url($_SERVER['HTTP_HOST'], 'solidba.se') . '">reCAPTCHA ' . __('website') . '</a>'); $CONFIG->UI->setType('reCAPTCHA_private_key', 'text'); $CONFIG->UI->setDescription('reCAPTCHA_private_key', __('Visit') . ' <a href="' . reCAPTCHA::recaptcha_get_signup_url($_SERVER['HTTP_HOST'], 'solidba.se') . '">reCAPTCHA ' . __('website') . '</a>'); /** * * */ class reCAPTCHA { function verify() { global $CONFIG; $_POST->setType('recaptcha_challenge_field', 'string'); $_POST->setType('recaptcha_response_field', 'string'); $resp = self::recaptcha_check_answer($CONFIG->UI->reCAPTCHA_private_key, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); return $resp->is_valid; } function view($echo = false, $error = null)
if (!defined('RX_BASEDIR') || !$addon_info->site_key || !$addon_info->secret_key || $called_position !== 'before_module_init') { return; } $current_action = Context::get('act'); $current_member = Context::get('logged_info'); if ($current_member->is_admin === 'Y') { $enable_captcha = false; } elseif ($addon_info->target_users !== 'everyone' && $current_member->member_srl) { $enable_captcha = false; } elseif ($addon_info->target_frequency !== 'every_time' && isset($_SESSION['recaptcha_authenticated']) && $_SESSION['recaptcha_authenticated']) { $enable_captcha = false; } elseif ($addon_info->use_signup === 'Y' && preg_match('/^(?:disp|proc)Member(?:SignUp|Insert)/i', $current_action)) { $enable_captcha = true; } elseif ($addon_info->use_recovery === 'Y' && preg_match('/^(?:disp|proc)Member(?:FindAccount|ResendAuthMail)/i', $current_action)) { $enable_captcha = true; } elseif ($addon_info->use_document === 'Y' && preg_match('/^(?:disp|proc)Board(Write|InsertDocument)/i', $current_action)) { $enable_captcha = true; } elseif ($addon_info->use_comment === 'Y' && (preg_match('/^(?:disp|proc)Board(Content|InsertComment)/i', $current_action) || !$current_action && Context::get('document_srl'))) { $enable_captcha = true; } else { $enable_captcha = false; } if ($enable_captcha) { include_once __DIR__ . '/recaptcha.class.php'; reCAPTCHA::init($addon_info); if (strncasecmp('proc', $current_action, 4) === 0) { getController('module')->addTriggerFunction('moduleObject.proc', 'before', 'reCAPTCHA::check'); } else { Context::set('captcha', new reCAPTCHA()); } }