Dernière mise à jour : 12/02/2016 à 08h39
Table des matières
Nous allons voir comment en créer un.
Création de la base du module du Captcha
Premièrement, il faut mettre en place la Structure de base du module.
Création de la classe principale de votre Captcha
Créer une classe avec le nom de votre Captcha dans le dossier phpboost de votre module. Cette classe doit être une classe fille de la classe Captcha.
Celle-ci doit comprendre à minima les fonctions suivantes :
-
public function get_name()
: retourne le nom du Captcha. -
public function is_available()
: permet de réaliser des tests afin de déterminer si le Captcha est disponible, utile s'il utilise une librairie distante par exemple. Mettretrues'il n'y a pas besoin de faire de test de disponibilité. -
public function is_valid()
: permet de vérifier la validité du Captcha. -
public function display()
: permet d'afficher le Captcha.
Exemple :
Code PHP :
<?php /*################################################## * QuestionCaptcha.class.php * ------------------- * begin : May 9, 2014 * copyright : (C) 2014 Julien BRISWALTER * email : j1.seth@phpboost.com * * ################################################### * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ###################################################*/ class QuestionCaptcha extends Captcha { private static $questions; public static function __static() { self::$questions = QuestionCaptchaConfig::load()->get_questions(); } public function get_name() { return 'QuestionCaptcha'; } public function is_available() { return true; } public function is_valid() { if (!$this->is_available() || AppContext::get_current_user()->check_level(User::MEMBER_LEVEL)) { return true; } $answer = AppContext::get_request()->get_value($this->get_html_id(), ''); $question_id = AppContext::get_request()->get_int($this->get_html_id() . '_question_id', 0); if (!empty($question_id)) { $question = new QuestionCaptchaQuestion(); $question->set_properties(self::$questions[$question_id]); return in_array(trim(strtolower($answer)), $question->get_formated_answers()); } return false; } public function display() { $question_id = array_rand(self::$questions); //Question aléatoire $question = new QuestionCaptchaQuestion(); $question->set_properties(self::$questions[$question_id]); $tpl = new FileTemplate('QuestionCaptcha/QuestionCaptcha.tpl'); $tpl->put_all(array( 'QUESTION_ID' => $question_id, 'QUESTION' => $question->get_label(), 'HTML_ID' => $this->get_html_id() )); return $tpl->render(); } } ?>
ExtensionPointProvider
L'ExtensionPointProvider est à remplir comme tout autre module, il faut en plus y indiquer la fonction
captcha()qui va retourner la classe principale de votre module.
Exemple :
Code PHP :
<?php /*################################################## * QuestionCaptchaExtensionPointProvider.class.php * ------------------- * begin : May 9, 2014 * copyright : (C) 2014 Julien BRISWALTER * email : j1.seth@phpboost.com * * ################################################### * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ###################################################*/ class QuestionCaptchaExtensionPointProvider extends ExtensionPointProvider { public function __construct() { parent::__construct('QuestionCaptcha'); } public function captcha() { return new QuestionCaptcha(); } public function css_files() { $module_css_files = new ModuleCssFiles(); $module_css_files->adding_running_module_displayed_file('QuestionCaptcha.css'); return $module_css_files; } public function url_mappings() { return new UrlMappings(array(new DispatcherUrlMapping('/QuestionCaptcha/index.php'))); } } ?>
Tout le reste fonctionne comme tout autre module pour l'utilisation des fichiers css, la création de templates, etc...