|
| 1 | +<?php |
| 2 | +/*! |
| 3 | + * Traq |
| 4 | + * Copyright (C) 2009-2025 Jack Polgar |
| 5 | + * Copyright (C) 2012-2025 Traq.io |
| 6 | + * https://github.com/nirix |
| 7 | + * http://traq.io |
| 8 | + * |
| 9 | + * This file is part of Traq. |
| 10 | + * |
| 11 | + * Traq is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU General Public License as published by |
| 13 | + * the Free Software Foundation; version 3 only. |
| 14 | + * |
| 15 | + * Traq is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with Traq. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Traq\Plugins; |
| 25 | + |
| 26 | +use Avalon\Database; |
| 27 | +use \FishHook; |
| 28 | +use ReCaptcha\Controllers\ReCaptchaController; |
| 29 | +use Request; |
| 30 | +use Router; |
| 31 | +use Traq\Libraries\Plugin; |
| 32 | +use View; |
| 33 | + |
| 34 | +/** |
| 35 | + * Security Questions Plugin. |
| 36 | + * |
| 37 | + * @package Traq |
| 38 | + * @subpackage Plugins |
| 39 | + * @author Jack P. |
| 40 | + * @copyright (c) Jack P. |
| 41 | + */ |
| 42 | +class ReCaptcha extends Plugin |
| 43 | +{ |
| 44 | + protected static $info = [ |
| 45 | + 'name' => 'reCaptcha', |
| 46 | + 'version' => '1.0', |
| 47 | + 'author' => 'Jack P.' |
| 48 | + ]; |
| 49 | + |
| 50 | + public static function init() |
| 51 | + { |
| 52 | + // Add routes |
| 53 | + Router::register('security_questions.index', '/admin/settings/recaptcha', [ReCaptchaController::class, 'index']); |
| 54 | + |
| 55 | + // Register the view path |
| 56 | + View::$searchPaths[] = dirname(__FILE__) . '/views'; |
| 57 | + |
| 58 | + // Hook into the settings navbar |
| 59 | + FishHook::add('template:admin/settings/_nav', array(get_called_class(), 'adminNav')); |
| 60 | + |
| 61 | + if (settings('recaptcha_site_key') && settings('recaptcha_secret_key')) { |
| 62 | + // Hook into register form |
| 63 | + FishHook::add('template:users/register', array(get_called_class(), 'recaptchaField')); |
| 64 | + |
| 65 | + // Hook into the register action |
| 66 | + FishHook::add('controller:users.register', array(get_called_class(), 'verifyRecaptcha')); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Adds the link to the settings navbar. |
| 72 | + */ |
| 73 | + public static function adminNav() |
| 74 | + { |
| 75 | + echo sprintf('<li%s><a href="%s">%s</a></li>', iif(active_nav('/admin/settings/recaptcha'), ' class="active"', ''), Request::base('/admin/settings/recaptcha'), l('recaptcha')); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Adds the question field to the register form. |
| 80 | + */ |
| 81 | + public static function recaptchaField() |
| 82 | + { |
| 83 | + echo View::render('users/recaptcha_field', ['siteKey' => settings('recaptcha_site_key')]); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Checks the submitted answer. |
| 88 | + * |
| 89 | + * @param object $model |
| 90 | + */ |
| 91 | + public static function verifyRecaptcha(&$model) |
| 92 | + { |
| 93 | + $recaptcha = new \ReCaptcha\ReCaptcha(settings('recaptcha_secret_key')); |
| 94 | + $resp = $recaptcha->setExpectedHostname($_SERVER['SERVER_NAME']) |
| 95 | + ->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); |
| 96 | + |
| 97 | + if (!$resp->isSuccess()) { |
| 98 | + $model->_add_error('recaptcha', l('errors.recaptcha.failed')); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Creates the setting row. |
| 104 | + */ |
| 105 | + public static function __install() |
| 106 | + { |
| 107 | + Database::connection()->insert(array('setting' => 'recaptcha_site_key', 'value' => ''))->into('settings')->exec(); |
| 108 | + Database::connection()->insert(array('setting' => 'recaptcha_secret_key', 'value' => ''))->into('settings')->exec(); |
| 109 | + |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Deletes the setting row. |
| 115 | + */ |
| 116 | + public static function __uninstall() |
| 117 | + { |
| 118 | + Database::connection()->delete()->from('settings')->where('setting', 'recaptcha_site_key')->exec(); |
| 119 | + Database::connection()->delete()->from('settings')->where('setting', 'recaptcha_secret_key')->exec(); |
| 120 | + |
| 121 | + return true; |
| 122 | + } |
| 123 | +} |
0 commit comments