From 160ff7b912243dc14d871b820213ddcd20dd06f4 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 8 Aug 2014 18:02:03 +0200 Subject: [PATCH 01/21] [ticket/11854] Move captcha stuff to phpbb/ and use DI for plugins PHPBB3-11854 --- phpBB/config/captcha.yml | 43 ++++++++ phpBB/config/services.yml | 1 + phpBB/includes/acp/acp_captcha.php | 17 ++- phpBB/includes/captcha/captcha_factory.php | 100 ------------------ .../plugins/phpbb_captcha_gd_wave_plugin.php | 69 ------------ .../plugins/phpbb_captcha_nogd_plugin.php | 70 ------------ phpBB/includes/functions.php | 7 +- phpBB/includes/ucp/ucp_confirm.php | 5 +- phpBB/includes/ucp/ucp_login_link.php | 4 +- phpBB/includes/ucp/ucp_register.php | 3 +- phpBB/phpbb/auth/provider/db.php | 8 +- phpBB/phpbb/captcha/factory.php | 83 +++++++++++++++ .../captcha_gd.php => phpbb/captcha/gd.php} | 10 +- .../captcha/gd_wave.php} | 4 +- .../captcha/non_gd.php} | 10 +- .../captcha/plugins/captcha_abstract.php | 32 +++--- .../captcha/plugins/gd.php} | 62 ++++------- phpBB/phpbb/captcha/plugins/gd_wave.php | 50 +++++++++ phpBB/phpbb/captcha/plugins/nogd.php | 50 +++++++++ .../captcha/plugins/qa.php} | 40 +++---- .../captcha/plugins/recaptcha.php} | 41 +++---- phpBB/phpbb/session.php | 8 +- phpBB/posting.php | 3 +- phpBB/report.php | 3 +- 24 files changed, 318 insertions(+), 405 deletions(-) create mode 100644 phpBB/config/captcha.yml delete mode 100644 phpBB/includes/captcha/captcha_factory.php delete mode 100644 phpBB/includes/captcha/plugins/phpbb_captcha_gd_wave_plugin.php delete mode 100644 phpBB/includes/captcha/plugins/phpbb_captcha_nogd_plugin.php create mode 100644 phpBB/phpbb/captcha/factory.php rename phpBB/{includes/captcha/captcha_gd.php => phpbb/captcha/gd.php} (99%) rename phpBB/{includes/captcha/captcha_gd_wave.php => phpbb/captcha/gd_wave.php} (99%) rename phpBB/{includes/captcha/captcha_non_gd.php => phpbb/captcha/non_gd.php} (99%) rename phpBB/{includes => phpbb}/captcha/plugins/captcha_abstract.php (94%) rename phpBB/{includes/captcha/plugins/phpbb_captcha_gd_plugin.php => phpbb/captcha/plugins/gd.php} (78%) create mode 100644 phpBB/phpbb/captcha/plugins/gd_wave.php create mode 100644 phpBB/phpbb/captcha/plugins/nogd.php rename phpBB/{includes/captcha/plugins/phpbb_captcha_qa_plugin.php => phpbb/captcha/plugins/qa.php} (97%) rename phpBB/{includes/captcha/plugins/phpbb_recaptcha_plugin.php => phpbb/captcha/plugins/recaptcha.php} (93%) diff --git a/phpBB/config/captcha.yml b/phpBB/config/captcha.yml new file mode 100644 index 0000000000..527dba2257 --- /dev/null +++ b/phpBB/config/captcha.yml @@ -0,0 +1,43 @@ +services: + captchas.factory: + class: phpbb\captcha\factory + arguments: + - @service_container + - @captchas.plugins.service_collection + + captchas.plugins.service_collection: + class: phpbb\di\service_collection + arguments: + - @service_container + tags: + - { name: service_collection, tag: captchas.plugins } + + core.captcha.plugins.gd: + class: phpbb\captcha\plugins\gd + scope: prototype # scope MUST be prototype for this to work! # scope MUST be prototype for this to work! + tags: + - { name: captchas.plugins } + + core.captcha.plugins.gd_wave: + class: phpbb\captcha\plugins\gd_wave + scope: prototype # scope MUST be prototype for this to work! # scope MUST be prototype for this to work! + tags: + - { name: captchas.plugins } + + core.captcha.plugins.nogd: + class: phpbb\captcha\plugins\nogd + scope: prototype # scope MUST be prototype for this to work! # scope MUST be prototype for this to work! + tags: + - { name: captchas.plugins } + + core.captcha.plugins.qa: + class: phpbb\captcha\plugins\qa + scope: prototype # scope MUST be prototype for this to work! # scope MUST be prototype for this to work! + tags: + - { name: captchas.plugins } + + core.captcha.plugins.recaptcha: + class: phpbb\captcha\plugins\recaptcha + scope: prototype # scope MUST be prototype for this to work! # scope MUST be prototype for this to work! + tags: + - { name: captchas.plugins } diff --git a/phpBB/config/services.yml b/phpBB/config/services.yml index 6b06deb256..a588046245 100644 --- a/phpBB/config/services.yml +++ b/phpBB/config/services.yml @@ -10,6 +10,7 @@ imports: - { resource: mimetype_guessers.yml } - { resource: passwords.yml } - { resource: profilefields.yml } + - { resource: captcha.yml } - { resource: parameters.yml } services: diff --git a/phpBB/includes/acp/acp_captcha.php b/phpBB/includes/acp/acp_captcha.php index a625005bf8..099acf707b 100644 --- a/phpBB/includes/acp/acp_captcha.php +++ b/phpBB/includes/acp/acp_captcha.php @@ -26,12 +26,11 @@ class acp_captcha function main($id, $mode) { global $db, $user, $auth, $template; - global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx; + global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx, $phpbb_container; $user->add_lang('acp/board'); - include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx); - $factory = new phpbb_captcha_factory(); + $factory = $phpbb_container->get('captchas.factory'); $captchas = $factory->get_captcha_types(); $selected = request_var('select_captcha', $config['captcha_plugin']); @@ -47,7 +46,7 @@ class acp_captcha // Delegate if ($configure) { - $config_captcha = phpbb_captcha_factory::get_instance($selected); + $config_captcha = $factory->get_instance($selected); $config_captcha->acp_page($id, $this); } else @@ -79,11 +78,11 @@ class acp_captcha // sanity check if (isset($captchas['available'][$selected])) { - $old_captcha = phpbb_captcha_factory::get_instance($config['captcha_plugin']); + $old_captcha = $factory->get_instance($config['captcha_plugin']); $old_captcha->uninstall(); set_config('captcha_plugin', $selected); - $new_captcha = phpbb_captcha_factory::get_instance($config['captcha_plugin']); + $new_captcha = $factory->get_instance($config['captcha_plugin']); $new_captcha->install(); add_log('admin', 'LOG_CONFIG_VISUAL'); @@ -114,7 +113,7 @@ class acp_captcha $captcha_select .= ''; } - $demo_captcha = phpbb_captcha_factory::get_instance($selected); + $demo_captcha = $factory->get_instance($selected); foreach ($config_vars as $config_var => $options) { @@ -137,9 +136,9 @@ class acp_captcha */ function deliver_demo($selected) { - global $db, $user, $config; + global $db, $user, $config, $phpbb_container; - $captcha = phpbb_captcha_factory::get_instance($selected); + $captcha = $phpbb_container->get('captchas.factory')->get_instance($selected); $captcha->init(CONFIRM_REG); $captcha->execute_demo(); diff --git a/phpBB/includes/captcha/captcha_factory.php b/phpBB/includes/captcha/captcha_factory.php deleted file mode 100644 index ebeb695282..0000000000 --- a/phpBB/includes/captcha/captcha_factory.php +++ /dev/null @@ -1,100 +0,0 @@ - -* @license GNU General Public License, version 2 (GPL-2.0) -* -* For full copyright and license information, please see -* the docs/CREDITS.txt file. -* -*/ - -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} - -/** -* A small class for 3.0.x (no autoloader in 3.0.x) -*/ -class phpbb_captcha_factory -{ - /** - * return an instance of class $name in file $name_plugin.php - */ - static public function get_instance($name) - { - global $phpbb_root_path, $phpEx; - - $name = basename($name); - if (!class_exists($name)) - { - include($phpbb_root_path . "includes/captcha/plugins/{$name}_plugin." . $phpEx); - } - $instance = call_user_func(array($name, 'get_instance')); - return $instance; - } - - /** - * Call the garbage collector - */ - function garbage_collect($name) - { - global $phpbb_root_path, $phpEx; - - $name = basename($name); - if (!class_exists($name)) - { - include($phpbb_root_path . "includes/captcha/plugins/{$name}_plugin." . $phpEx); - } - $captcha = self::get_instance($name); - $captcha->garbage_collect(0); - } - - /** - * return a list of all discovered CAPTCHA plugins - */ - function get_captcha_types() - { - global $phpbb_root_path, $phpEx, $phpbb_extension_manager; - - $captchas = array( - 'available' => array(), - 'unavailable' => array(), - ); - - $finder = $phpbb_extension_manager->get_finder(); - $captcha_plugin_classes = $finder - ->extension_directory('/captcha') - ->suffix('_plugin') - ->core_path('includes/captcha/plugins/') - ->get_classes(); - - foreach ($captcha_plugin_classes as $class) - { - // check if this class needs to be loaded in legacy mode - $old_class = preg_replace('/^phpbb_captcha_plugins_/', '', $class); - if (file_exists($phpbb_root_path . "includes/captcha/plugins/$old_class.$phpEx") && !class_exists($old_class)) - { - include($phpbb_root_path . "includes/captcha/plugins/$old_class.$phpEx"); - $class = preg_replace('/_plugin$/', '', $old_class); - } - - if (call_user_func(array($class, 'is_available'))) - { - $captchas['available'][$class] = call_user_func(array($class, 'get_name')); - } - else - { - $captchas['unavailable'][$class] = call_user_func(array($class, 'get_name')); - } - } - - return $captchas; - } -} diff --git a/phpBB/includes/captcha/plugins/phpbb_captcha_gd_wave_plugin.php b/phpBB/includes/captcha/plugins/phpbb_captcha_gd_wave_plugin.php deleted file mode 100644 index b6ccabaa2e..0000000000 --- a/phpBB/includes/captcha/plugins/phpbb_captcha_gd_wave_plugin.php +++ /dev/null @@ -1,69 +0,0 @@ - -* @license GNU General Public License, version 2 (GPL-2.0) -* -* For full copyright and license information, please see -* the docs/CREDITS.txt file. -* -*/ - -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} - -/** -* Placeholder for autoload -*/ -if (!class_exists('phpbb_default_captcha', false)) -{ - include($phpbb_root_path . 'includes/captcha/plugins/captcha_abstract.' . $phpEx); -} - -class phpbb_captcha_gd_wave extends phpbb_default_captcha -{ - - function phpbb_captcha_gd_wave() - { - global $phpbb_root_path, $phpEx; - - if (!class_exists('captcha')) - { - include_once($phpbb_root_path . 'includes/captcha/captcha_gd_wave.' . $phpEx); - } - } - - static public function get_instance() - { - return new phpbb_captcha_gd_wave(); - } - - static public function is_available() - { - return @extension_loaded('gd'); - } - - static public function get_name() - { - return 'CAPTCHA_GD_3D'; - } - - function get_class_name() - { - return 'phpbb_captcha_gd_wave'; - } - - function acp_page($id, &$module) - { - global $config, $db, $template, $user; - - trigger_error($user->lang['CAPTCHA_NO_OPTIONS'] . adm_back_link($module->u_action)); - } -} diff --git a/phpBB/includes/captcha/plugins/phpbb_captcha_nogd_plugin.php b/phpBB/includes/captcha/plugins/phpbb_captcha_nogd_plugin.php deleted file mode 100644 index 64f788a659..0000000000 --- a/phpBB/includes/captcha/plugins/phpbb_captcha_nogd_plugin.php +++ /dev/null @@ -1,70 +0,0 @@ - -* @license GNU General Public License, version 2 (GPL-2.0) -* -* For full copyright and license information, please see -* the docs/CREDITS.txt file. -* -*/ - -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} - -/** -* Placeholder for autoload -*/ -if (!class_exists('phpbb_default_captcha', false)) -{ - include($phpbb_root_path . 'includes/captcha/plugins/captcha_abstract.' . $phpEx); -} - -class phpbb_captcha_nogd extends phpbb_default_captcha -{ - - function phpbb_captcha_nogd() - { - global $phpbb_root_path, $phpEx; - - if (!class_exists('captcha')) - { - include_once($phpbb_root_path . 'includes/captcha/captcha_non_gd.' . $phpEx); - } - } - - static public function get_instance() - { - $instance = new phpbb_captcha_nogd(); - return $instance; - } - - static public function is_available() - { - return true; - } - - static public function get_name() - { - return 'CAPTCHA_NO_GD'; - } - - function get_class_name() - { - return 'phpbb_captcha_nogd'; - } - - function acp_page($id, &$module) - { - global $user; - - trigger_error($user->lang['CAPTCHA_NO_OPTIONS'] . adm_back_link($module->u_action)); - } -} diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 6a2edb0d58..d0a32030e3 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2730,11 +2730,6 @@ function login_box($redirect = '', $l_explain = '', $l_success = '', $admin = fa global $db, $user, $template, $auth, $phpEx, $phpbb_root_path, $config; global $request, $phpbb_container; - if (!class_exists('phpbb_captcha_factory', false)) - { - include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx); - } - $err = ''; // Make sure user->setup() has been called @@ -2844,7 +2839,7 @@ function login_box($redirect = '', $l_explain = '', $l_success = '', $admin = fa { case LOGIN_ERROR_ATTEMPTS: - $captcha = phpbb_captcha_factory::get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_LOGIN); // $captcha->reset(); diff --git a/phpBB/includes/ucp/ucp_confirm.php b/phpBB/includes/ucp/ucp_confirm.php index bcba32cdf6..8ade142e2b 100644 --- a/phpBB/includes/ucp/ucp_confirm.php +++ b/phpBB/includes/ucp/ucp_confirm.php @@ -36,10 +36,9 @@ class ucp_confirm function main($id, $mode) { - global $db, $user, $phpbb_root_path, $config, $phpEx; + global $db, $user, $phpbb_root_path, $config, $phpEx, $phpbb_container; - include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx); - $captcha = phpbb_captcha_factory::get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); $captcha->init(request_var('type', 0)); $captcha->execute(); diff --git a/phpBB/includes/ucp/ucp_login_link.php b/phpBB/includes/ucp/ucp_login_link.php index a8762313fd..7c768992f7 100644 --- a/phpBB/includes/ucp/ucp_login_link.php +++ b/phpBB/includes/ucp/ucp_login_link.php @@ -181,7 +181,7 @@ class ucp_login_link */ protected function process_login_result($result) { - global $config, $request, $template, $user; + global $config, $request, $template, $user, $phpbb_container; $login_error = null; @@ -197,7 +197,7 @@ class ucp_login_link { case LOGIN_ERROR_ATTEMPTS: - $captcha = phpbb_captcha_factory::get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_LOGIN); $template->assign_vars(array( diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php index 97934fc32d..dbe2233553 100644 --- a/phpBB/includes/ucp/ucp_register.php +++ b/phpBB/includes/ucp/ucp_register.php @@ -182,8 +182,7 @@ class ucp_register // The CAPTCHA kicks in here. We can't help that the information gets lost on language change. if ($config['enable_confirm']) { - include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx); - $captcha = phpbb_captcha_factory::get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_REG); } diff --git a/phpBB/phpbb/auth/provider/db.php b/phpBB/phpbb/auth/provider/db.php index 142a47247f..cb7a2f8b1a 100644 --- a/phpBB/phpbb/auth/provider/db.php +++ b/phpBB/phpbb/auth/provider/db.php @@ -146,13 +146,7 @@ class db extends \phpbb\auth\provider\base // Every auth module is able to define what to do by itself... if ($show_captcha) { - // Visual Confirmation handling - if (!class_exists('phpbb_captcha_factory', false)) - { - include ($this->phpbb_root_path . 'includes/captcha/captcha_factory.' . $this->php_ext); - } - - $captcha = \phpbb_captcha_factory::get_instance($this->config['captcha_plugin']); + $captcha = $this->captchas_manager->get_instance($this->config['captcha_plugin']); $captcha->init(CONFIRM_LOGIN); $vc_response = $captcha->validate($row); if ($vc_response) diff --git a/phpBB/phpbb/captcha/factory.php b/phpBB/phpbb/captcha/factory.php new file mode 100644 index 0000000000..6164b12e85 --- /dev/null +++ b/phpBB/phpbb/captcha/factory.php @@ -0,0 +1,83 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\captcha; + +/** +* A small class for 3.0.x (no autoloader in 3.0.x) +*/ +class factory +{ + /** + * @var \Symfony\Component\DependencyInjection\ContainerInterface + */ + private $container; + + /** + * @var \phpbb\di\service_collection + */ + private $plugins; + + /** + * Constructor + * + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * @param \phpbb\di\service_collection $plugins + */ + public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container,\phpbb\di\service_collection $plugins) + { + $this->container = $container; + $this->plugins = $plugins; + } + + //static public function get_instance($name) + + public function get_instance($name) + { + return $this->container->get($name); + } + + /** + * Call the garbage collector + */ + function garbage_collect($name) + { + $captcha = $this->get_instance($name); + $captcha->garbage_collect(0); + } + + /** + * return a list of all registered CAPTCHA plugins + */ + function get_captcha_types() + { + $captchas = array( + 'available' => array(), + 'unavailable' => array(), + ); + + foreach ($this->plugins as $plugin => $plugin_instance) + { + if ($plugin_instance->is_available()) + { + $captchas['available'][$plugin] = $plugin_instance->get_name(); + } + else + { + $captchas['unavailable'][$plugin] = $plugin_instance->get_name(); + } + } + + return $captchas; + } +} diff --git a/phpBB/includes/captcha/captcha_gd.php b/phpBB/phpbb/captcha/gd.php similarity index 99% rename from phpBB/includes/captcha/captcha_gd.php rename to phpBB/phpbb/captcha/gd.php index 7e37cc33f9..682022c8ed 100644 --- a/phpBB/includes/captcha/captcha_gd.php +++ b/phpBB/phpbb/captcha/gd.php @@ -11,15 +11,9 @@ * */ -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} +namespace phpbb\captcha; -class captcha +class gd { var $width = 360; var $height = 96; diff --git a/phpBB/includes/captcha/captcha_gd_wave.php b/phpBB/phpbb/captcha/gd_wave.php similarity index 99% rename from phpBB/includes/captcha/captcha_gd_wave.php rename to phpBB/phpbb/captcha/gd_wave.php index c2a4d3a31e..d48fc753a5 100644 --- a/phpBB/includes/captcha/captcha_gd_wave.php +++ b/phpBB/phpbb/captcha/gd_wave.php @@ -11,10 +11,12 @@ * */ +namespace phpbb\captcha; + /** * Wave3D CAPTCHA */ -class captcha +class gd_wave { var $width = 360; var $height = 96; diff --git a/phpBB/includes/captcha/captcha_non_gd.php b/phpBB/phpbb/captcha/non_gd.php similarity index 99% rename from phpBB/includes/captcha/captcha_non_gd.php rename to phpBB/phpbb/captcha/non_gd.php index 91970ea7a4..0a7aeb0e6c 100644 --- a/phpBB/includes/captcha/captcha_non_gd.php +++ b/phpBB/phpbb/captcha/non_gd.php @@ -11,19 +11,13 @@ * */ -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} +namespace phpbb\captcha; /** * Main non-gd captcha class * @ignore */ -class captcha +class non_gd { var $filtered_pngs; var $width = 320; diff --git a/phpBB/includes/captcha/plugins/captcha_abstract.php b/phpBB/phpbb/captcha/plugins/captcha_abstract.php similarity index 94% rename from phpBB/includes/captcha/plugins/captcha_abstract.php rename to phpBB/phpbb/captcha/plugins/captcha_abstract.php index 8e1e61bdb7..982798e464 100644 --- a/phpBB/includes/captcha/plugins/captcha_abstract.php +++ b/phpBB/phpbb/captcha/plugins/captcha_abstract.php @@ -11,18 +11,12 @@ * */ -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} +namespace phpbb\captcha\plugins; /** * This class holds the code shared by the two default 3.0.x CAPTCHAs. */ -class phpbb_captcha_plugins_captcha_abstract +abstract class captcha_abstract { var $confirm_id; var $confirm_code; @@ -65,7 +59,8 @@ class phpbb_captcha_plugins_captcha_abstract // compute $seed % 0x7fffffff $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff); - $captcha = new captcha(); + $generator = $this->get_generator_class(); + $captcha = new $generator(); define('IMAGE_OUTPUT', 1); $captcha->execute($this->code, $this->seed); } @@ -80,7 +75,8 @@ class phpbb_captcha_plugins_captcha_abstract return false; } } - $captcha = new captcha(); + $generator = $this->get_generator_class(); + $captcha = new $generator(); define('IMAGE_OUTPUT', 1); $captcha->execute($this->code, $this->seed); } @@ -130,7 +126,7 @@ class phpbb_captcha_plugins_captcha_abstract // acp_captcha has a delivery function; let's use it $template->assign_vars(array( - 'CONFIRM_IMAGE' => append_sid($phpbb_admin_path . 'index.' . $phpEx, 'captcha_demo=1&mode=visual&i=' . $id . '&select_captcha=' . $this->get_class_name()) . $variables, + 'CONFIRM_IMAGE' => append_sid($phpbb_admin_path . 'index.' . $phpEx, 'captcha_demo=1&mode=visual&i=' . $id . '&select_captcha=' . $this->get_service_name()) . $variables, 'CONFIRM_ID' => $this->confirm_id, )); @@ -364,11 +360,13 @@ class phpbb_captcha_plugins_captcha_abstract return false; } -} + /** + * @return string the name of the service corresponding to the plugin + */ + abstract function get_service_name(); -/** -* Old class name for legacy use. The new class name is auto loadable. -*/ -class phpbb_default_captcha extends phpbb_captcha_plugins_captcha_abstract -{ + /** + * @return string the name of the class used to generate the captcha + */ + abstract function get_generator_class(); } diff --git a/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php b/phpBB/phpbb/captcha/plugins/gd.php similarity index 78% rename from phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php rename to phpBB/phpbb/captcha/plugins/gd.php index 8dbd458ede..c1d8c83095 100644 --- a/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php +++ b/phpBB/phpbb/captcha/plugins/gd.php @@ -11,25 +11,10 @@ * */ -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} +namespace phpbb\captcha\plugins; -/** -* Placeholder for autoload -*/ -if (!class_exists('phpbb_default_captcha', false)) +class gd extends captcha_abstract { - include($phpbb_root_path . 'includes/captcha/plugins/captcha_abstract.' . $phpEx); -} - -class phpbb_captcha_gd extends phpbb_default_captcha -{ - var $captcha_vars = array( 'captcha_gd_x_grid' => 'CAPTCHA_GD_X_GRID', 'captcha_gd_y_grid' => 'CAPTCHA_GD_Y_GRID', @@ -40,27 +25,27 @@ class phpbb_captcha_gd extends phpbb_default_captcha 'captcha_gd_fonts' => 'CAPTCHA_GD_FONTS', ); - function phpbb_captcha_gd() - { - global $phpbb_root_path, $phpEx; - - if (!class_exists('captcha')) - { - include($phpbb_root_path . 'includes/captcha/captcha_gd.' . $phpEx); - } - } - - static public function get_instance() - { - $instance = new phpbb_captcha_gd(); - return $instance; - } - - static public function is_available() + public function is_available() { return @extension_loaded('gd'); } + /** + * @return string the name of the service corresponding to the plugin + */ + function get_service_name() + { + return 'core.captcha.plugins.gd'; + } + + /** + * @return string the name of the class used to generate the captcha + */ + function get_generator_class() + { + return '\\phpbb\\captcha\\gd'; + } + /** * API function */ @@ -69,16 +54,11 @@ class phpbb_captcha_gd extends phpbb_default_captcha return true; } - static public function get_name() + public function get_name() { return 'CAPTCHA_GD'; } - function get_class_name() - { - return 'phpbb_captcha_gd'; - } - function acp_page($id, &$module) { global $db, $user, $auth, $template; @@ -129,7 +109,7 @@ class phpbb_captcha_gd extends phpbb_default_captcha $template->assign_vars(array( 'CAPTCHA_PREVIEW' => $this->get_demo_template($id), - 'CAPTCHA_NAME' => $this->get_class_name(), + 'CAPTCHA_NAME' => $this->get_service_name(), 'U_ACTION' => $module->u_action, )); } diff --git a/phpBB/phpbb/captcha/plugins/gd_wave.php b/phpBB/phpbb/captcha/plugins/gd_wave.php new file mode 100644 index 0000000000..2d38ef08dd --- /dev/null +++ b/phpBB/phpbb/captcha/plugins/gd_wave.php @@ -0,0 +1,50 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\captcha\plugins; + +class gd_wave extends captcha_abstract +{ + public function is_available() + { + return @extension_loaded('gd'); + } + + public function get_name() + { + return 'CAPTCHA_GD_3D'; + } + + /** + * @return string the name of the service corresponding to the plugin + */ + function get_service_name() + { + return 'core.captcha.plugins.gd_wave'; + } + + /** + * @return string the name of the class used to generate the captcha + */ + function get_generator_class() + { + return '\\phpbb\\captcha\\gd_wave'; + } + + function acp_page($id, &$module) + { + global $config, $db, $template, $user; + + trigger_error($user->lang['CAPTCHA_NO_OPTIONS'] . adm_back_link($module->u_action)); + } +} diff --git a/phpBB/phpbb/captcha/plugins/nogd.php b/phpBB/phpbb/captcha/plugins/nogd.php new file mode 100644 index 0000000000..dbf0664a62 --- /dev/null +++ b/phpBB/phpbb/captcha/plugins/nogd.php @@ -0,0 +1,50 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\captcha\plugins; + +class nogd extends captcha_abstract +{ + public function is_available() + { + return true; + } + + public function get_name() + { + return 'CAPTCHA_NO_GD'; + } + + /** + * @return string the name of the service corresponding to the plugin + */ + function get_service_name() + { + return 'core.captcha.plugins.nogd'; + } + + /** + * @return string the name of the class used to generate the captcha + */ + function get_generator_class() + { + return '\\phpbb\\captcha\\non_gd'; + } + + function acp_page($id, &$module) + { + global $user; + + trigger_error($user->lang['CAPTCHA_NO_OPTIONS'] . adm_back_link($module->u_action)); + } +} diff --git a/phpBB/includes/captcha/plugins/phpbb_captcha_qa_plugin.php b/phpBB/phpbb/captcha/plugins/qa.php similarity index 97% rename from phpBB/includes/captcha/plugins/phpbb_captcha_qa_plugin.php rename to phpBB/phpbb/captcha/plugins/qa.php index 5a44755365..6dab573c3e 100644 --- a/phpBB/includes/captcha/plugins/phpbb_captcha_qa_plugin.php +++ b/phpBB/phpbb/captcha/plugins/qa.php @@ -11,13 +11,7 @@ * */ -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} +namespace phpbb\captcha\plugins; global $table_prefix; @@ -29,7 +23,7 @@ define('CAPTCHA_QA_CONFIRM_TABLE', $table_prefix . 'qa_confirm'); * And now to something completely different. Let's make a captcha without extending the abstract class. * QA CAPTCHA sample implementation */ -class phpbb_captcha_qa +class qa { var $confirm_id; var $answer; @@ -97,20 +91,10 @@ class phpbb_captcha_qa } } - /** - * API function - */ - static public function get_instance() - { - $instance = new phpbb_captcha_qa(); - - return $instance; - } - /** * See if the captcha has created its tables. */ - static public function is_installed() + public function is_installed() { global $db; @@ -122,14 +106,14 @@ class phpbb_captcha_qa /** * API function - for the captcha to be available, it must have installed itself and there has to be at least one question in the board's default lang */ - static public function is_available() + public function is_available() { global $config, $db, $phpbb_root_path, $phpEx, $user; // load language file for pretty display in the ACP dropdown $user->add_lang('captcha_qa'); - if (!self::is_installed()) + if (!$this->is_installed()) { return false; } @@ -163,9 +147,9 @@ class phpbb_captcha_qa /** * API function */ - function get_class_name() + function get_service_name() { - return 'phpbb_captcha_qa'; + return 'core.captcha.plugins.qa'; } /** @@ -621,12 +605,12 @@ class phpbb_captcha_qa $action = request_var('action', ''); // we have two pages, so users might want to navigate from one to the other - $list_url = $module->u_action . "&configure=1&select_captcha=" . $this->get_class_name(); + $list_url = $module->u_action . "&configure=1&select_captcha=" . $this->get_service_name(); $template->assign_vars(array( 'U_ACTION' => $module->u_action, 'QUESTION_ID' => $question_id , - 'CLASS' => $this->get_class_name(), + 'CLASS' => $this->get_service_name(), )); // show the list? @@ -636,7 +620,7 @@ class phpbb_captcha_qa } else if ($question_id && $action == 'delete') { - if ($this->get_class_name() !== $config['captcha_plugin'] || !$this->acp_is_last($question_id)) + if ($this->get_service_name() !== $config['captcha_plugin'] || !$this->acp_is_last($question_id)) { if (confirm_box(true)) { @@ -650,7 +634,7 @@ class phpbb_captcha_qa 'question_id' => $question_id, 'action' => $action, 'configure' => 1, - 'select_captcha' => $this->get_class_name(), + 'select_captcha' => $this->get_service_name(), )) ); } @@ -759,7 +743,7 @@ class phpbb_captcha_qa while ($row = $db->sql_fetchrow($result)) { - $url = $module->u_action . "&question_id={$row['question_id']}&configure=1&select_captcha=" . $this->get_class_name() . '&'; + $url = $module->u_action . "&question_id={$row['question_id']}&configure=1&select_captcha=" . $this->get_service_name() . '&'; $template->assign_block_vars('questions', array( 'QUESTION_TEXT' => $row['question_text'], diff --git a/phpBB/includes/captcha/plugins/phpbb_recaptcha_plugin.php b/phpBB/phpbb/captcha/plugins/recaptcha.php similarity index 93% rename from phpBB/includes/captcha/plugins/phpbb_recaptcha_plugin.php rename to phpBB/phpbb/captcha/plugins/recaptcha.php index 12cc49ef9b..9fffea756e 100644 --- a/phpBB/includes/captcha/plugins/phpbb_recaptcha_plugin.php +++ b/phpBB/phpbb/captcha/plugins/recaptcha.php @@ -11,21 +11,9 @@ * */ -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} +namespace phpbb\captcha\plugins; -if (!class_exists('phpbb_default_captcha', false)) -{ - // we need the classic captcha code for tracking solutions and attempts - include($phpbb_root_path . 'includes/captcha/plugins/captcha_abstract.' . $phpEx); -} - -class phpbb_recaptcha extends phpbb_default_captcha +class recaptcha extends \phpbb\captcha\plugins\captcha_abstract { var $recaptcha_server = 'http://www.google.com/recaptcha/api'; var $recaptcha_server_secure = 'https://www.google.com/recaptcha/api'; // class constants :( @@ -55,13 +43,7 @@ class phpbb_recaptcha extends phpbb_default_captcha $this->response = request_var('recaptcha_response_field', ''); } - static public function get_instance() - { - $instance = new phpbb_recaptcha(); - return $instance; - } - - static public function is_available() + public function is_available() { global $config, $user; $user->add_lang('captcha_recaptcha'); @@ -81,9 +63,20 @@ class phpbb_recaptcha extends phpbb_default_captcha return 'CAPTCHA_RECAPTCHA'; } - function get_class_name() + /** + * @return string the name of the service corresponding to the plugin + */ + function get_service_name() { - return 'phpbb_recaptcha'; + return 'core.captcha.plugins.recaptcha'; + } + + /** + * @return string the name of the class used to generate the captcha + */ + function get_generator_class() + { + throw new \Exception('Go out devil!'); } function acp_page($id, &$module) @@ -131,7 +124,7 @@ class phpbb_recaptcha extends phpbb_default_captcha $template->assign_vars(array( 'CAPTCHA_PREVIEW' => $this->get_demo_template($id), - 'CAPTCHA_NAME' => $this->get_class_name(), + 'CAPTCHA_NAME' => $this->get_service_name(), 'U_ACTION' => $module->u_action, )); diff --git a/phpBB/phpbb/session.php b/phpBB/phpbb/session.php index 5a0d7c0031..a9dde5bbaa 100644 --- a/phpBB/phpbb/session.php +++ b/phpBB/phpbb/session.php @@ -962,7 +962,7 @@ class session */ function session_gc() { - global $db, $config, $phpbb_root_path, $phpEx; + global $db, $config, $phpbb_root_path, $phpEx, $phpbb_container; $batch_size = 10; @@ -1022,11 +1022,7 @@ class session } // only called from CRON; should be a safe workaround until the infrastructure gets going - if (!class_exists('phpbb_captcha_factory', false)) - { - include($phpbb_root_path . "includes/captcha/captcha_factory." . $phpEx); - } - $captcha_factory = new \phpbb_captcha_factory(); + $captcha_factory = $phpbb_container->get('captchas.factory'); $captcha_factory->garbage_collect($config['captcha_plugin']); $sql = 'DELETE FROM ' . LOGIN_ATTEMPT_TABLE . ' diff --git a/phpBB/posting.php b/phpBB/posting.php index 6638caa94b..6d6297741b 100644 --- a/phpBB/posting.php +++ b/phpBB/posting.php @@ -241,8 +241,7 @@ $user->setup(array('posting', 'mcp', 'viewtopic'), $post_data['forum_style']); if ($config['enable_post_confirm'] && !$user->data['is_registered']) { - include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx); - $captcha = phpbb_captcha_factory::get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_POST); } diff --git a/phpBB/report.php b/phpBB/report.php index 3684c2162f..39df109904 100644 --- a/phpBB/report.php +++ b/phpBB/report.php @@ -151,8 +151,7 @@ else if ($config['enable_post_confirm'] && !$user->data['is_registered']) { - include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx); - $captcha = phpbb_captcha_factory::get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_REPORT); } From dd88022a7010d6840f305423cb631c7c3e658f7f Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 8 Aug 2014 18:22:11 +0200 Subject: [PATCH 02/21] [ticket/11854] Add a migration to update the captcha_plugin setting PHPBB3-11854 --- phpBB/install/schemas/schema_data.sql | 2 +- .../migration/data/v310/captcha_plugins.php | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 phpBB/phpbb/db/migration/data/v310/captcha_plugins.php diff --git a/phpBB/install/schemas/schema_data.sql b/phpBB/install/schemas/schema_data.sql index 9cc151f728..5589e10243 100644 --- a/phpBB/install/schemas/schema_data.sql +++ b/phpBB/install/schemas/schema_data.sql @@ -70,7 +70,7 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('browser_check', '1 INSERT INTO phpbb_config (config_name, config_value) VALUES ('bump_interval', '10'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('bump_type', 'd'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('cache_gc', '7200'); -INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_plugin', 'phpbb_captcha_nogd'); +INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_plugin', 'core.captcha.plugins.nogd'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_gd', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_gd_foreground_noise', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_gd_x_grid', '25'); diff --git a/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php b/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php new file mode 100644 index 0000000000..8fe52355a9 --- /dev/null +++ b/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php @@ -0,0 +1,43 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\db\migration\data\v310; + +class captcha_plugins extends \phpbb\db\migration\migration +{ + static public function depends_on() + { + return array( + '\phpbb\db\migration\data\v310\dev', + ); + } + + public function update_data() + { + if (strpos($this->config['captcha_plugin'], 'phpbb_captcha_') === 0) + { + $captcha_plugin = substr(strlen('phpbb_captcha_'), $this->config['captcha_plugin']); + } + + return array( + array('if', array( + (is_file($this->phpbb_root_path . 'phpbb/captcha/plugins/' . $captcha_plugin . $this->php_ext)), + array('config.update', array('captcha_plugin', 'core.captcha.plugins.' . $captcha_plugin)), + )), + array('if', array( + (!is_file($this->phpbb_root_path . 'phpbb/captcha/plugins/' . $captcha_plugin . $this->php_ext)), + array('config.update', array('captcha_plugin', 'core.captcha.plugins.nogd')), + )), + ); + } +} From a1d5ad8a37144aaa1b5d32b3e61afc895cc60bfb Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 8 Aug 2014 19:04:26 +0200 Subject: [PATCH 03/21] [ticket/11854] Add missing dot in the migration file PHPBB3-11854 --- phpBB/phpbb/db/migration/data/v310/captcha_plugins.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php b/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php index 8fe52355a9..5e0a09e75a 100644 --- a/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php +++ b/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php @@ -31,11 +31,11 @@ class captcha_plugins extends \phpbb\db\migration\migration return array( array('if', array( - (is_file($this->phpbb_root_path . 'phpbb/captcha/plugins/' . $captcha_plugin . $this->php_ext)), + (is_file($this->phpbb_root_path . 'phpbb/captcha/plugins/' . $captcha_plugin . '.' . $this->php_ext)), array('config.update', array('captcha_plugin', 'core.captcha.plugins.' . $captcha_plugin)), )), array('if', array( - (!is_file($this->phpbb_root_path . 'phpbb/captcha/plugins/' . $captcha_plugin . $this->php_ext)), + (!is_file($this->phpbb_root_path . 'phpbb/captcha/plugins/' . $captcha_plugin . '.' . $this->php_ext)), array('config.update', array('captcha_plugin', 'core.captcha.plugins.nogd')), )), ); From 6e4991093ae8f4ce22cc5bdf8b5bd8dcf42c14f3 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 9 Aug 2014 10:41:56 +0200 Subject: [PATCH 04/21] [ticket/11854] Fix migration PHPBB3-11854 --- phpBB/config/captcha.yml | 10 +++++----- phpBB/phpbb/db/migration/data/v310/captcha_plugins.php | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/phpBB/config/captcha.yml b/phpBB/config/captcha.yml index 527dba2257..fa40c89045 100644 --- a/phpBB/config/captcha.yml +++ b/phpBB/config/captcha.yml @@ -14,30 +14,30 @@ services: core.captcha.plugins.gd: class: phpbb\captcha\plugins\gd - scope: prototype # scope MUST be prototype for this to work! # scope MUST be prototype for this to work! + scope: prototype # scope MUST be prototype for this to work! tags: - { name: captchas.plugins } core.captcha.plugins.gd_wave: class: phpbb\captcha\plugins\gd_wave - scope: prototype # scope MUST be prototype for this to work! # scope MUST be prototype for this to work! + scope: prototype # scope MUST be prototype for this to work! tags: - { name: captchas.plugins } core.captcha.plugins.nogd: class: phpbb\captcha\plugins\nogd - scope: prototype # scope MUST be prototype for this to work! # scope MUST be prototype for this to work! + scope: prototype # scope MUST be prototype for this to work! tags: - { name: captchas.plugins } core.captcha.plugins.qa: class: phpbb\captcha\plugins\qa - scope: prototype # scope MUST be prototype for this to work! # scope MUST be prototype for this to work! + scope: prototype # scope MUST be prototype for this to work! tags: - { name: captchas.plugins } core.captcha.plugins.recaptcha: class: phpbb\captcha\plugins\recaptcha - scope: prototype # scope MUST be prototype for this to work! # scope MUST be prototype for this to work! + scope: prototype # scope MUST be prototype for this to work! tags: - { name: captchas.plugins } diff --git a/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php b/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php index 5e0a09e75a..e6f15ca407 100644 --- a/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php +++ b/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php @@ -24,9 +24,10 @@ class captcha_plugins extends \phpbb\db\migration\migration public function update_data() { + $captcha_plugin = $this->config['captcha_plugin']; if (strpos($this->config['captcha_plugin'], 'phpbb_captcha_') === 0) { - $captcha_plugin = substr(strlen('phpbb_captcha_'), $this->config['captcha_plugin']); + $captcha_plugin = substr($this->config['captcha_plugin'], strlen('phpbb_captcha_')); } return array( From 28cf0ef8323d121a25b1c9ddd55fdd76412752ad Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 9 Aug 2014 11:46:10 +0200 Subject: [PATCH 05/21] [ticket/11854] Update unit tests PHPBB3-11854 --- tests/session/garbage_collection_test.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/session/garbage_collection_test.php b/tests/session/garbage_collection_test.php index 0fbc71dcd7..027a7ca852 100644 --- a/tests/session/garbage_collection_test.php +++ b/tests/session/garbage_collection_test.php @@ -12,6 +12,7 @@ */ require_once dirname(__FILE__) . '/../test_framework/phpbb_session_test_case.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; class phpbb_session_garbage_collection_test extends phpbb_session_test_case { @@ -26,6 +27,19 @@ class phpbb_session_garbage_collection_test extends phpbb_session_test_case { parent::setUp(); $this->session = $this->session_factory->get_session($this->db); + + global $phpbb_container; + + $plugins = new \phpbb\di\service_collection($phpbb_container); + $plugins->add('core.captcha.plugins.nogd'); + $phpbb_container->set( + 'captchas.factory', + new \phpbb\captcha\factory($phpbb_container, $plugins) + ); + $phpbb_container->set( + 'core.captcha.plugins.nogd', + new \phpbb\captcha\plugins\nogd() + ); } public function test_cleanup_all() @@ -47,7 +61,7 @@ class phpbb_session_garbage_collection_test extends phpbb_session_test_case global $config; $config['session_length'] = 0; // There is an error unless the captcha plugin is set - $config['captcha_plugin'] = 'phpbb_captcha_nogd'; + $config['captcha_plugin'] = 'core.captcha.plugins.nogd'; $this->session->session_gc(); $this->check_sessions_equals( array(), From 99b7d18ab2ee1b5b3ead555347b71fcb2de6e308 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 9 Aug 2014 15:30:05 +0200 Subject: [PATCH 06/21] [ticket/11854] Migrate all old php4 constructor PHPBB3-11854 --- phpBB/phpbb/captcha/gd.php | 4 ++-- phpBB/phpbb/captcha/non_gd.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/phpbb/captcha/gd.php b/phpBB/phpbb/captcha/gd.php index 682022c8ed..99c279b55d 100644 --- a/phpBB/phpbb/captcha/gd.php +++ b/phpBB/phpbb/captcha/gd.php @@ -1862,7 +1862,7 @@ class char_cube3d /** */ - function char_cube3d(&$bitmaps, $letter) + function __construct(&$bitmaps, $letter) { $this->bitmap = $bitmaps['data'][$letter]; $this->bitmap_width = $bitmaps['width']; @@ -2119,7 +2119,7 @@ class colour_manager /** * Create the colour manager, link it to the image resource */ - function colour_manager($img, $background = false, $mode = 'ahsv') + function __construct($img, $background = false, $mode = 'ahsv') { $this->img = $img; $this->mode = $mode; diff --git a/phpBB/phpbb/captcha/non_gd.php b/phpBB/phpbb/captcha/non_gd.php index 0a7aeb0e6c..3818672f17 100644 --- a/phpBB/phpbb/captcha/non_gd.php +++ b/phpBB/phpbb/captcha/non_gd.php @@ -26,7 +26,7 @@ class non_gd /** * Define filtered pngs on init */ - function captcha() + function __construct() { // If we can we will generate a single filtered png, we avoid nastiness via emulation of some Zlib stuff $this->define_filtered_pngs(); From 577f9ac7e4989258b7925bc1b9c15986f7dfd748 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 9 Aug 2014 15:41:34 +0200 Subject: [PATCH 07/21] [ticket/11854] Split phpbb\captcha\gd.php PHPBB3-11854 --- phpBB/phpbb/captcha/char_cube3d.php | 277 +++++++++++++ phpBB/phpbb/captcha/colour_manager.php | 527 +++++++++++++++++++++++++ 2 files changed, 804 insertions(+) create mode 100644 phpBB/phpbb/captcha/char_cube3d.php create mode 100644 phpBB/phpbb/captcha/colour_manager.php diff --git a/phpBB/phpbb/captcha/char_cube3d.php b/phpBB/phpbb/captcha/char_cube3d.php new file mode 100644 index 0000000000..a712b16dce --- /dev/null +++ b/phpBB/phpbb/captcha/char_cube3d.php @@ -0,0 +1,277 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\captcha; + +class char_cube3d +{ + var $bitmap; + var $bitmap_width; + var $bitmap_height; + + var $basis_matrix = array(array(1, 0, 0), array(0, 1, 0), array(0, 0, 1)); + var $abs_x = array(1, 0); + var $abs_y = array(0, 1); + var $x = 0; + var $y = 1; + var $z = 2; + var $letter = ''; + + /** + */ + function __construct(&$bitmaps, $letter) + { + $this->bitmap = $bitmaps['data'][$letter]; + $this->bitmap_width = $bitmaps['width']; + $this->bitmap_height = $bitmaps['height']; + + $this->basis_matrix[0][0] = mt_rand(-600, 600); + $this->basis_matrix[0][1] = mt_rand(-600, 600); + $this->basis_matrix[0][2] = (mt_rand(0, 1) * 2000) - 1000; + $this->basis_matrix[1][0] = mt_rand(-1000, 1000); + $this->basis_matrix[1][1] = mt_rand(-1000, 1000); + $this->basis_matrix[1][2] = mt_rand(-1000, 1000); + + $this->normalize($this->basis_matrix[0]); + $this->normalize($this->basis_matrix[1]); + $this->basis_matrix[2] = $this->cross_product($this->basis_matrix[0], $this->basis_matrix[1]); + $this->normalize($this->basis_matrix[2]); + + // $this->basis_matrix[1] might not be (probably isn't) orthogonal to $basis_matrix[0] + $this->basis_matrix[1] = $this->cross_product($this->basis_matrix[0], $this->basis_matrix[2]); + $this->normalize($this->basis_matrix[1]); + + // Make sure our cube is facing into the canvas (assuming +z == in) + for ($i = 0; $i < 3; ++$i) + { + if ($this->basis_matrix[$i][2] < 0) + { + $this->basis_matrix[$i][0] *= -1; + $this->basis_matrix[$i][1] *= -1; + $this->basis_matrix[$i][2] *= -1; + } + } + + // Force our "z" basis vector to be the one with greatest absolute z value + $this->x = 0; + $this->y = 1; + $this->z = 2; + + // Swap "y" with "z" + if ($this->basis_matrix[1][2] > $this->basis_matrix[2][2]) + { + $this->z = 1; + $this->y = 2; + } + + // Swap "x" with "z" + if ($this->basis_matrix[0][2] > $this->basis_matrix[$this->z][2]) + { + $this->x = $this->z; + $this->z = 0; + } + + // Still need to determine which of $x,$y are which. + // wrong orientation if y's y-component is less than it's x-component + // likewise if x's x-component is less than it's y-component + // if they disagree, go with the one with the greater weight difference. + // rotate if positive + $weight = (abs($this->basis_matrix[$this->x][1]) - abs($this->basis_matrix[$this->x][0])) + (abs($this->basis_matrix[$this->y][0]) - abs($this->basis_matrix[$this->y][1])); + + // Swap "x" with "y" + if ($weight > 0) + { + list($this->x, $this->y) = array($this->y, $this->x); + } + + $this->abs_x = array($this->basis_matrix[$this->x][0], $this->basis_matrix[$this->x][1]); + $this->abs_y = array($this->basis_matrix[$this->y][0], $this->basis_matrix[$this->y][1]); + + if ($this->abs_x[0] < 0) + { + $this->abs_x[0] *= -1; + $this->abs_x[1] *= -1; + } + + if ($this->abs_y[1] > 0) + { + $this->abs_y[0] *= -1; + $this->abs_y[1] *= -1; + } + + $this->letter = $letter; + } + + /** + * Draw a character + */ + function drawchar($scale, $xoff, $yoff, $img, $background, $colours) + { + $width = $this->bitmap_width; + $height = $this->bitmap_height; + $bitmap = $this->bitmap; + + $colour1 = $colours[array_rand($colours)]; + $colour2 = $colours[array_rand($colours)]; + + $swapx = ($this->basis_matrix[$this->x][0] > 0); + $swapy = ($this->basis_matrix[$this->y][1] < 0); + + for ($y = 0; $y < $height; ++$y) + { + for ($x = 0; $x < $width; ++$x) + { + $xp = ($swapx) ? ($width - $x - 1) : $x; + $yp = ($swapy) ? ($height - $y - 1) : $y; + + if ($bitmap[$height - $yp - 1][$xp]) + { + $dx = $this->scale($this->abs_x, ($xp - ($swapx ? ($width / 2) : ($width / 2) - 1)) * $scale); + $dy = $this->scale($this->abs_y, ($yp - ($swapy ? ($height / 2) : ($height / 2) - 1)) * $scale); + $xo = $xoff + $dx[0] + $dy[0]; + $yo = $yoff + $dx[1] + $dy[1]; + + $origin = array(0, 0, 0); + $xvec = $this->scale($this->basis_matrix[$this->x], $scale); + $yvec = $this->scale($this->basis_matrix[$this->y], $scale); + $face_corner = $this->sum2($xvec, $yvec); + + $zvec = $this->scale($this->basis_matrix[$this->z], $scale); + $x_corner = $this->sum2($xvec, $zvec); + $y_corner = $this->sum2($yvec, $zvec); + + imagefilledpolygon($img, $this->gen_poly($xo, $yo, $origin, $xvec, $x_corner,$zvec), 4, $colour1); + imagefilledpolygon($img, $this->gen_poly($xo, $yo, $origin, $yvec, $y_corner,$zvec), 4, $colour2); + + $face = $this->gen_poly($xo, $yo, $origin, $xvec, $face_corner, $yvec); + + imagefilledpolygon($img, $face, 4, $background); + imagepolygon($img, $face, 4, $colour1); + } + } + } + } + + /* + * return a roughly acceptable range of sizes for rendering with this texttype + */ + function range() + { + return array(3, 4); + } + + /** + * Vector length + */ + function vectorlen($vector) + { + return sqrt(pow($vector[0], 2) + pow($vector[1], 2) + pow($vector[2], 2)); + } + + /** + * Normalize + */ + function normalize(&$vector, $length = 1) + { + $length = (( $length < 1) ? 1 : $length); + $length /= $this->vectorlen($vector); + $vector[0] *= $length; + $vector[1] *= $length; + $vector[2] *= $length; + } + + /** + */ + function cross_product($vector1, $vector2) + { + $retval = array(0, 0, 0); + $retval[0] = (($vector1[1] * $vector2[2]) - ($vector1[2] * $vector2[1])); + $retval[1] = -(($vector1[0] * $vector2[2]) - ($vector1[2] * $vector2[0])); + $retval[2] = (($vector1[0] * $vector2[1]) - ($vector1[1] * $vector2[0])); + + return $retval; + } + + /** + */ + function sum($vector1, $vector2) + { + return array($vector1[0] + $vector2[0], $vector1[1] + $vector2[1], $vector1[2] + $vector2[2]); + } + + /** + */ + function sum2($vector1, $vector2) + { + return array($vector1[0] + $vector2[0], $vector1[1] + $vector2[1]); + } + + /** + */ + function scale($vector, $length) + { + if (sizeof($vector) == 2) + { + return array($vector[0] * $length, $vector[1] * $length); + } + + return array($vector[0] * $length, $vector[1] * $length, $vector[2] * $length); + } + + /** + */ + function gen_poly($xoff, $yoff, &$vec1, &$vec2, &$vec3, &$vec4) + { + $poly = array(); + $poly[0] = $xoff + $vec1[0]; + $poly[1] = $yoff + $vec1[1]; + $poly[2] = $xoff + $vec2[0]; + $poly[3] = $yoff + $vec2[1]; + $poly[4] = $xoff + $vec3[0]; + $poly[5] = $yoff + $vec3[1]; + $poly[6] = $xoff + $vec4[0]; + $poly[7] = $yoff + $vec4[1]; + + return $poly; + } + + /** + * dimensions + */ + function dimensions($size) + { + $xn = $this->scale($this->basis_matrix[$this->x], -($this->bitmap_width / 2) * $size); + $xp = $this->scale($this->basis_matrix[$this->x], ($this->bitmap_width / 2) * $size); + $yn = $this->scale($this->basis_matrix[$this->y], -($this->bitmap_height / 2) * $size); + $yp = $this->scale($this->basis_matrix[$this->y], ($this->bitmap_height / 2) * $size); + + $p = array(); + $p[0] = $this->sum2($xn, $yn); + $p[1] = $this->sum2($xp, $yn); + $p[2] = $this->sum2($xp, $yp); + $p[3] = $this->sum2($xn, $yp); + + $min_x = $max_x = $p[0][0]; + $min_y = $max_y = $p[0][1]; + + for ($i = 1; $i < 4; ++$i) + { + $min_x = ($min_x > $p[$i][0]) ? $p[$i][0] : $min_x; + $min_y = ($min_y > $p[$i][1]) ? $p[$i][1] : $min_y; + $max_x = ($max_x < $p[$i][0]) ? $p[$i][0] : $max_x; + $max_y = ($max_y < $p[$i][1]) ? $p[$i][1] : $max_y; + } + + return array($min_x, $min_y, $max_x, $max_y); + } +} diff --git a/phpBB/phpbb/captcha/colour_manager.php b/phpBB/phpbb/captcha/colour_manager.php new file mode 100644 index 0000000000..6ca3c3fd2c --- /dev/null +++ b/phpBB/phpbb/captcha/colour_manager.php @@ -0,0 +1,527 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\captcha; + +class colour_manager +{ + var $img; + var $mode; + var $colours; + var $named_colours; + + /** + * Create the colour manager, link it to the image resource + */ + function __construct($img, $background = false, $mode = 'ahsv') + { + $this->img = $img; + $this->mode = $mode; + $this->colours = array(); + $this->named_colours = array(); + + if ($background !== false) + { + $bg = $this->allocate_named('background', $background); + imagefill($this->img, 0, 0, $bg); + } + } + + /** + * Lookup a named colour resource + */ + function get_resource($named_colour) + { + if (isset($this->named_colours[$named_colour])) + { + return $this->named_colours[$named_colour]; + } + + if (isset($this->named_rgb[$named_colour])) + { + return $this->allocate_named($named_colour, $this->named_rgb[$named_colour], 'rgb'); + } + + return false; + } + + /** + * Assign a name to a colour resource + */ + function name_colour($name, $resource) + { + $this->named_colours[$name] = $resource; + } + + /** + * names and allocates a colour resource + */ + function allocate_named($name, $colour, $mode = false) + { + $resource = $this->allocate($colour, $mode); + + if ($resource !== false) + { + $this->name_colour($name, $resource); + } + return $resource; + } + + /** + * allocates a specified colour into the image + */ + function allocate($colour, $mode = false) + { + if ($mode === false) + { + $mode = $this->mode; + } + + if (!is_array($colour)) + { + if (isset($this->named_rgb[$colour])) + { + return $this->allocate_named($colour, $this->named_rgb[$colour], 'rgb'); + } + + if (!is_int($colour)) + { + return false; + } + + $mode = 'rgb'; + $colour = array(255 & ($colour >> 16), 255 & ($colour >> 8), 255 & $colour); + } + + if (isset($colour['mode'])) + { + $mode = $colour['mode']; + unset($colour['mode']); + } + + if (isset($colour['random'])) + { + unset($colour['random']); + // everything else is params + return $this->random_colour($colour, $mode); + } + + $rgb = $this->model_convert($colour, $mode, 'rgb'); + $store = ($this->mode == 'rgb') ? $rgb : $this->model_convert($colour, $mode, $this->mode); + $resource = imagecolorallocate($this->img, $rgb[0], $rgb[1], $rgb[2]); + $this->colours[$resource] = $store; + + return $resource; + } + + /** + * randomly generates a colour, with optional params + */ + function random_colour($params = array(), $mode = false) + { + if ($mode === false) + { + $mode = $this->mode; + } + + switch ($mode) + { + case 'rgb': + // @TODO random rgb generation. do we intend to do this, or is it just too tedious? + break; + + case 'ahsv': + case 'hsv': + default: + + $default_params = array( + 'hue_bias' => false, // degree / 'r'/'g'/'b'/'c'/'m'/'y' /'o' + 'hue_range' => false, // if hue bias, then difference range +/- from bias + 'min_saturation' => 30, // 0 - 100 + 'max_saturation' => 80, // 0 - 100 + 'min_value' => 30, // 0 - 100 + 'max_value' => 80, // 0 - 100 + ); + + $alt = ($mode == 'ahsv') ? true : false; + $params = array_merge($default_params, $params); + + $min_hue = 0; + $max_hue = 359; + $min_saturation = max(0, $params['min_saturation']); + $max_saturation = min(100, $params['max_saturation']); + $min_value = max(0, $params['min_value']); + $max_value = min(100, $params['max_value']); + + if ($params['hue_bias'] !== false) + { + if (is_numeric($params['hue_bias'])) + { + $h = intval($params['hue_bias']) % 360; + } + else + { + switch ($params['hue_bias']) + { + case 'o': + $h = $alt ? 60 : 30; + break; + + case 'y': + $h = $alt ? 120 : 60; + break; + + case 'g': + $h = $alt ? 180 : 120; + break; + + case 'c': + $h = $alt ? 210 : 180; + break; + + case 'b': + $h = 240; + break; + + case 'm': + $h = 300; + break; + + case 'r': + default: + $h = 0; + break; + } + } + + $min_hue = $h + 360; + $max_hue = $h + 360; + + if ($params['hue_range']) + { + $min_hue -= min(180, $params['hue_range']); + $max_hue += min(180, $params['hue_range']); + } + } + + $h = mt_rand($min_hue, $max_hue); + $s = mt_rand($min_saturation, $max_saturation); + $v = mt_rand($min_value, $max_value); + + return $this->allocate(array($h, $s, $v), $mode); + + break; + } + } + + /** + */ + function colour_scheme($resource, $include_original = true) + { + $mode = 'hsv'; + + if (($pre = $this->get_resource($resource)) !== false) + { + $resource = $pre; + } + + $colour = $this->model_convert($this->colours[$resource], $this->mode, $mode); + $results = ($include_original) ? array($resource) : array(); + $colour2 = $colour3 = $colour4 = $colour; + $colour2[0] += 150; + $colour3[0] += 180; + $colour4[0] += 210; + + $results[] = $this->allocate($colour2, $mode); + $results[] = $this->allocate($colour3, $mode); + $results[] = $this->allocate($colour4, $mode); + + return $results; + } + + /** + */ + function mono_range($resource, $count = 5, $include_original = true) + { + if (is_array($resource)) + { + $results = array(); + for ($i = 0, $size = sizeof($resource); $i < $size; ++$i) + { + $results = array_merge($results, $this->mono_range($resource[$i], $count, $include_original)); + } + return $results; + } + + $mode = (in_array($this->mode, array('hsv', 'ahsv'), true) ? $this->mode : 'ahsv'); + if (($pre = $this->get_resource($resource)) !== false) + { + $resource = $pre; + } + + $colour = $this->model_convert($this->colours[$resource], $this->mode, $mode); + + $results = array(); + if ($include_original) + { + $results[] = $resource; + $count--; + } + + // This is a hard problem. I chicken out and try to maintain readability at the cost of less randomness. + + while ($count > 0) + { + $colour[1] = ($colour[1] + mt_rand(40,60)) % 99; + $colour[2] = ($colour[2] + mt_rand(40,60)); + $results[] = $this->allocate($colour, $mode); + $count--; + } + return $results; + } + + /** + * Convert from one colour model to another + */ + function model_convert($colour, $from_model, $to_model) + { + if ($from_model == $to_model) + { + return $colour; + } + + switch ($to_model) + { + case 'hsv': + + switch ($from_model) + { + case 'ahsv': + return $this->ah2h($colour); + break; + + case 'rgb': + return $this->rgb2hsv($colour); + break; + } + break; + + case 'ahsv': + + switch ($from_model) + { + case 'hsv': + return $this->h2ah($colour); + break; + + case 'rgb': + return $this->h2ah($this->rgb2hsv($colour)); + break; + } + break; + + case 'rgb': + switch ($from_model) + { + case 'hsv': + return $this->hsv2rgb($colour); + break; + + case 'ahsv': + return $this->hsv2rgb($this->ah2h($colour)); + break; + } + break; + } + return false; + } + + /** + * Slightly altered from wikipedia's algorithm + */ + function hsv2rgb($hsv) + { + $this->normalize_hue($hsv[0]); + + $h = $hsv[0]; + $s = min(1, max(0, $hsv[1] / 100)); + $v = min(1, max(0, $hsv[2] / 100)); + + // calculate hue sector + $hi = floor($hsv[0] / 60); + + // calculate opposite colour + $p = $v * (1 - $s); + + // calculate distance between hex vertices + $f = ($h / 60) - $hi; + + // coming in or going out? + if (!($hi & 1)) + { + $f = 1 - $f; + } + + // calculate adjacent colour + $q = $v * (1 - ($f * $s)); + + switch ($hi) + { + case 0: + $rgb = array($v, $q, $p); + break; + + case 1: + $rgb = array($q, $v, $p); + break; + + case 2: + $rgb = array($p, $v, $q); + break; + + case 3: + $rgb = array($p, $q, $v); + break; + + case 4: + $rgb = array($q, $p, $v); + break; + + case 5: + $rgb = array($v, $p, $q); + break; + + default: + return array(0, 0, 0); + break; + } + + return array(255 * $rgb[0], 255 * $rgb[1], 255 * $rgb[2]); + } + + /** + * (more than) Slightly altered from wikipedia's algorithm + */ + function rgb2hsv($rgb) + { + $r = min(255, max(0, $rgb[0])); + $g = min(255, max(0, $rgb[1])); + $b = min(255, max(0, $rgb[2])); + $max = max($r, $g, $b); + $min = min($r, $g, $b); + + $v = $max / 255; + $s = (!$max) ? 0 : 1 - ($min / $max); + + // if max - min is 0, we want hue to be 0 anyway. + $h = $max - $min; + + if ($h) + { + switch ($max) + { + case $g: + $h = 120 + (60 * ($b - $r) / $h); + break; + + case $b: + $h = 240 + (60 * ($r - $g) / $h); + break; + + case $r: + $h = 360 + (60 * ($g - $b) / $h); + break; + } + } + $this->normalize_hue($h); + + return array($h, $s * 100, $v * 100); + } + + /** + */ + function normalize_hue(&$hue) + { + $hue %= 360; + + if ($hue < 0) + { + $hue += 360; + } + } + + /** + * Alternate hue to hue + */ + function ah2h($ahue) + { + if (is_array($ahue)) + { + $ahue[0] = $this->ah2h($ahue[0]); + return $ahue; + } + $this->normalize_hue($ahue); + + // blue through red is already ok + if ($ahue >= 240) + { + return $ahue; + } + + // ahue green is at 180 + if ($ahue >= 180) + { + // return (240 - (2 * (240 - $ahue))); + return (2 * $ahue) - 240; // equivalent + } + + // ahue yellow is at 120 (RYB rather than RGB) + if ($ahue >= 120) + { + return $ahue - 60; + } + + return $ahue / 2; + } + + /** + * hue to Alternate hue + */ + function h2ah($hue) + { + if (is_array($hue)) + { + $hue[0] = $this->h2ah($hue[0]); + return $hue; + } + $this->normalize_hue($hue); + + // blue through red is already ok + if ($hue >= 240) + { + return $hue; + } + else if ($hue <= 60) + { + return $hue * 2; + } + else if ($hue <= 120) + { + return $hue + 60; + } + else + { + return ($hue + 240) / 2; + } + } +} From d73d606f11897f13e3779c76261be6f96139f0e0 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 9 Aug 2014 15:55:40 +0200 Subject: [PATCH 08/21] [ticket/11854] Fix various comments PHPBB3-11854 --- phpBB/phpbb/captcha/factory.php | 8 ++++++-- phpBB/phpbb/captcha/plugins/recaptcha.php | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/phpBB/phpbb/captcha/factory.php b/phpBB/phpbb/captcha/factory.php index 6164b12e85..838f8a4ccd 100644 --- a/phpBB/phpbb/captcha/factory.php +++ b/phpBB/phpbb/captcha/factory.php @@ -40,8 +40,12 @@ class factory $this->plugins = $plugins; } - //static public function get_instance($name) - + /** + * Return a new instance of a given plugin + * + * @param $name + * @return object + */ public function get_instance($name) { return $this->container->get($name); diff --git a/phpBB/phpbb/captcha/plugins/recaptcha.php b/phpBB/phpbb/captcha/plugins/recaptcha.php index 9fffea756e..ca131ba8a0 100644 --- a/phpBB/phpbb/captcha/plugins/recaptcha.php +++ b/phpBB/phpbb/captcha/plugins/recaptcha.php @@ -72,7 +72,7 @@ class recaptcha extends \phpbb\captcha\plugins\captcha_abstract } /** - * @return string the name of the class used to generate the captcha + * This function is implemented because required by the upper class, but is never used for reCaptcha. */ function get_generator_class() { From b91abf1a0bd751da640219596a94019f70086c0b Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 9 Aug 2014 15:57:01 +0200 Subject: [PATCH 09/21] [ticket/11854] Rename captchas.factory service to captcha.factory PHPBB3-11854 --- phpBB/config/captcha.yml | 2 +- phpBB/includes/acp/acp_captcha.php | 4 ++-- phpBB/includes/functions.php | 2 +- phpBB/includes/ucp/ucp_confirm.php | 2 +- phpBB/includes/ucp/ucp_login_link.php | 2 +- phpBB/includes/ucp/ucp_register.php | 2 +- phpBB/phpbb/session.php | 2 +- phpBB/posting.php | 2 +- phpBB/report.php | 2 +- tests/session/garbage_collection_test.php | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/phpBB/config/captcha.yml b/phpBB/config/captcha.yml index fa40c89045..a9618f3fad 100644 --- a/phpBB/config/captcha.yml +++ b/phpBB/config/captcha.yml @@ -1,5 +1,5 @@ services: - captchas.factory: + captcha.factory: class: phpbb\captcha\factory arguments: - @service_container diff --git a/phpBB/includes/acp/acp_captcha.php b/phpBB/includes/acp/acp_captcha.php index 099acf707b..fa8d8fb6a9 100644 --- a/phpBB/includes/acp/acp_captcha.php +++ b/phpBB/includes/acp/acp_captcha.php @@ -30,7 +30,7 @@ class acp_captcha $user->add_lang('acp/board'); - $factory = $phpbb_container->get('captchas.factory'); + $factory = $phpbb_container->get('captcha.factory'); $captchas = $factory->get_captcha_types(); $selected = request_var('select_captcha', $config['captcha_plugin']); @@ -138,7 +138,7 @@ class acp_captcha { global $db, $user, $config, $phpbb_container; - $captcha = $phpbb_container->get('captchas.factory')->get_instance($selected); + $captcha = $phpbb_container->get('captcha.factory')->get_instance($selected); $captcha->init(CONFIRM_REG); $captcha->execute_demo(); diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index d0a32030e3..6cdee825d2 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2839,7 +2839,7 @@ function login_box($redirect = '', $l_explain = '', $l_success = '', $admin = fa { case LOGIN_ERROR_ATTEMPTS: - $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captcha.factory')->get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_LOGIN); // $captcha->reset(); diff --git a/phpBB/includes/ucp/ucp_confirm.php b/phpBB/includes/ucp/ucp_confirm.php index 8ade142e2b..7392f8dea8 100644 --- a/phpBB/includes/ucp/ucp_confirm.php +++ b/phpBB/includes/ucp/ucp_confirm.php @@ -38,7 +38,7 @@ class ucp_confirm { global $db, $user, $phpbb_root_path, $config, $phpEx, $phpbb_container; - $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captcha.factory')->get_instance($config['captcha_plugin']); $captcha->init(request_var('type', 0)); $captcha->execute(); diff --git a/phpBB/includes/ucp/ucp_login_link.php b/phpBB/includes/ucp/ucp_login_link.php index 7c768992f7..5ca5df00f7 100644 --- a/phpBB/includes/ucp/ucp_login_link.php +++ b/phpBB/includes/ucp/ucp_login_link.php @@ -197,7 +197,7 @@ class ucp_login_link { case LOGIN_ERROR_ATTEMPTS: - $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captcha.factory')->get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_LOGIN); $template->assign_vars(array( diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php index dbe2233553..a303197563 100644 --- a/phpBB/includes/ucp/ucp_register.php +++ b/phpBB/includes/ucp/ucp_register.php @@ -182,7 +182,7 @@ class ucp_register // The CAPTCHA kicks in here. We can't help that the information gets lost on language change. if ($config['enable_confirm']) { - $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captcha.factory')->get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_REG); } diff --git a/phpBB/phpbb/session.php b/phpBB/phpbb/session.php index a9dde5bbaa..7d564742af 100644 --- a/phpBB/phpbb/session.php +++ b/phpBB/phpbb/session.php @@ -1022,7 +1022,7 @@ class session } // only called from CRON; should be a safe workaround until the infrastructure gets going - $captcha_factory = $phpbb_container->get('captchas.factory'); + $captcha_factory = $phpbb_container->get('captcha.factory'); $captcha_factory->garbage_collect($config['captcha_plugin']); $sql = 'DELETE FROM ' . LOGIN_ATTEMPT_TABLE . ' diff --git a/phpBB/posting.php b/phpBB/posting.php index 6d6297741b..4c3d012ca5 100644 --- a/phpBB/posting.php +++ b/phpBB/posting.php @@ -241,7 +241,7 @@ $user->setup(array('posting', 'mcp', 'viewtopic'), $post_data['forum_style']); if ($config['enable_post_confirm'] && !$user->data['is_registered']) { - $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captcha.factory')->get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_POST); } diff --git a/phpBB/report.php b/phpBB/report.php index 39df109904..fd6fab6645 100644 --- a/phpBB/report.php +++ b/phpBB/report.php @@ -151,7 +151,7 @@ else if ($config['enable_post_confirm'] && !$user->data['is_registered']) { - $captcha = $phpbb_container->get('captchas.factory')->get_instance($config['captcha_plugin']); + $captcha = $phpbb_container->get('captcha.factory')->get_instance($config['captcha_plugin']); $captcha->init(CONFIRM_REPORT); } diff --git a/tests/session/garbage_collection_test.php b/tests/session/garbage_collection_test.php index 027a7ca852..3fad81c68b 100644 --- a/tests/session/garbage_collection_test.php +++ b/tests/session/garbage_collection_test.php @@ -33,7 +33,7 @@ class phpbb_session_garbage_collection_test extends phpbb_session_test_case $plugins = new \phpbb\di\service_collection($phpbb_container); $plugins->add('core.captcha.plugins.nogd'); $phpbb_container->set( - 'captchas.factory', + 'captcha.factory', new \phpbb\captcha\factory($phpbb_container, $plugins) ); $phpbb_container->set( From bd09356c30fd3c6019a2ae403dbda600db598865 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 9 Aug 2014 15:58:39 +0200 Subject: [PATCH 10/21] [ticket/11854] Rename captchas.plugins.service_collection service PHPBB3-11854 --- phpBB/config/captcha.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/config/captcha.yml b/phpBB/config/captcha.yml index a9618f3fad..0774319f93 100644 --- a/phpBB/config/captcha.yml +++ b/phpBB/config/captcha.yml @@ -3,9 +3,9 @@ services: class: phpbb\captcha\factory arguments: - @service_container - - @captchas.plugins.service_collection + - @captcha.plugins.service_collection - captchas.plugins.service_collection: + captcha.plugins.service_collection: class: phpbb\di\service_collection arguments: - @service_container From 850b7dbf07fb1888b41d39698499a80b2b39cc79 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 9 Aug 2014 18:17:22 +0200 Subject: [PATCH 11/21] [ticket/11854] Update the default captcha choosen during the installation PHPBB3-11854 --- phpBB/install/install_install.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/install/install_install.php b/phpBB/install/install_install.php index b82df84a00..20b211dde9 100644 --- a/phpBB/install/install_install.php +++ b/phpBB/install/install_install.php @@ -1382,7 +1382,7 @@ class install_install extends module if (@extension_loaded('gd')) { $sql_ary[] = 'UPDATE ' . $data['table_prefix'] . "config - SET config_value = 'phpbb_captcha_gd' + SET config_value = 'core.captcha.plugins.gd' WHERE config_name = 'captcha_plugin'"; $sql_ary[] = 'UPDATE ' . $data['table_prefix'] . "config From 02f3b74e8a7259560f48b6fdf06c3bbd8aa09dbb Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 10 Aug 2014 00:08:52 +0200 Subject: [PATCH 12/21] [ticket/11854] Fix coding style PHPBB3-11854 --- phpBB/phpbb/captcha/factory.php | 2 +- phpBB/phpbb/captcha/gd.php | 776 ----------------------------- phpBB/phpbb/captcha/plugins/qa.php | 64 ++- 3 files changed, 30 insertions(+), 812 deletions(-) diff --git a/phpBB/phpbb/captcha/factory.php b/phpBB/phpbb/captcha/factory.php index 838f8a4ccd..19c2f46e89 100644 --- a/phpBB/phpbb/captcha/factory.php +++ b/phpBB/phpbb/captcha/factory.php @@ -34,7 +34,7 @@ class factory * @param \Symfony\Component\DependencyInjection\ContainerInterface $container * @param \phpbb\di\service_collection $plugins */ - public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container,\phpbb\di\service_collection $plugins) + public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, \phpbb\di\service_collection $plugins) { $this->container = $container; $this->plugins = $plugins; diff --git a/phpBB/phpbb/captcha/gd.php b/phpBB/phpbb/captcha/gd.php index 99c279b55d..652df28f8a 100644 --- a/phpBB/phpbb/captcha/gd.php +++ b/phpBB/phpbb/captcha/gd.php @@ -1845,779 +1845,3 @@ class gd ); } } - -class char_cube3d -{ - var $bitmap; - var $bitmap_width; - var $bitmap_height; - - var $basis_matrix = array(array(1, 0, 0), array(0, 1, 0), array(0, 0, 1)); - var $abs_x = array(1, 0); - var $abs_y = array(0, 1); - var $x = 0; - var $y = 1; - var $z = 2; - var $letter = ''; - - /** - */ - function __construct(&$bitmaps, $letter) - { - $this->bitmap = $bitmaps['data'][$letter]; - $this->bitmap_width = $bitmaps['width']; - $this->bitmap_height = $bitmaps['height']; - - $this->basis_matrix[0][0] = mt_rand(-600, 600); - $this->basis_matrix[0][1] = mt_rand(-600, 600); - $this->basis_matrix[0][2] = (mt_rand(0, 1) * 2000) - 1000; - $this->basis_matrix[1][0] = mt_rand(-1000, 1000); - $this->basis_matrix[1][1] = mt_rand(-1000, 1000); - $this->basis_matrix[1][2] = mt_rand(-1000, 1000); - - $this->normalize($this->basis_matrix[0]); - $this->normalize($this->basis_matrix[1]); - $this->basis_matrix[2] = $this->cross_product($this->basis_matrix[0], $this->basis_matrix[1]); - $this->normalize($this->basis_matrix[2]); - - // $this->basis_matrix[1] might not be (probably isn't) orthogonal to $basis_matrix[0] - $this->basis_matrix[1] = $this->cross_product($this->basis_matrix[0], $this->basis_matrix[2]); - $this->normalize($this->basis_matrix[1]); - - // Make sure our cube is facing into the canvas (assuming +z == in) - for ($i = 0; $i < 3; ++$i) - { - if ($this->basis_matrix[$i][2] < 0) - { - $this->basis_matrix[$i][0] *= -1; - $this->basis_matrix[$i][1] *= -1; - $this->basis_matrix[$i][2] *= -1; - } - } - - // Force our "z" basis vector to be the one with greatest absolute z value - $this->x = 0; - $this->y = 1; - $this->z = 2; - - // Swap "y" with "z" - if ($this->basis_matrix[1][2] > $this->basis_matrix[2][2]) - { - $this->z = 1; - $this->y = 2; - } - - // Swap "x" with "z" - if ($this->basis_matrix[0][2] > $this->basis_matrix[$this->z][2]) - { - $this->x = $this->z; - $this->z = 0; - } - - // Still need to determine which of $x,$y are which. - // wrong orientation if y's y-component is less than it's x-component - // likewise if x's x-component is less than it's y-component - // if they disagree, go with the one with the greater weight difference. - // rotate if positive - $weight = (abs($this->basis_matrix[$this->x][1]) - abs($this->basis_matrix[$this->x][0])) + (abs($this->basis_matrix[$this->y][0]) - abs($this->basis_matrix[$this->y][1])); - - // Swap "x" with "y" - if ($weight > 0) - { - list($this->x, $this->y) = array($this->y, $this->x); - } - - $this->abs_x = array($this->basis_matrix[$this->x][0], $this->basis_matrix[$this->x][1]); - $this->abs_y = array($this->basis_matrix[$this->y][0], $this->basis_matrix[$this->y][1]); - - if ($this->abs_x[0] < 0) - { - $this->abs_x[0] *= -1; - $this->abs_x[1] *= -1; - } - - if ($this->abs_y[1] > 0) - { - $this->abs_y[0] *= -1; - $this->abs_y[1] *= -1; - } - - $this->letter = $letter; - } - - /** - * Draw a character - */ - function drawchar($scale, $xoff, $yoff, $img, $background, $colours) - { - $width = $this->bitmap_width; - $height = $this->bitmap_height; - $bitmap = $this->bitmap; - - $colour1 = $colours[array_rand($colours)]; - $colour2 = $colours[array_rand($colours)]; - - $swapx = ($this->basis_matrix[$this->x][0] > 0); - $swapy = ($this->basis_matrix[$this->y][1] < 0); - - for ($y = 0; $y < $height; ++$y) - { - for ($x = 0; $x < $width; ++$x) - { - $xp = ($swapx) ? ($width - $x - 1) : $x; - $yp = ($swapy) ? ($height - $y - 1) : $y; - - if ($bitmap[$height - $yp - 1][$xp]) - { - $dx = $this->scale($this->abs_x, ($xp - ($swapx ? ($width / 2) : ($width / 2) - 1)) * $scale); - $dy = $this->scale($this->abs_y, ($yp - ($swapy ? ($height / 2) : ($height / 2) - 1)) * $scale); - $xo = $xoff + $dx[0] + $dy[0]; - $yo = $yoff + $dx[1] + $dy[1]; - - $origin = array(0, 0, 0); - $xvec = $this->scale($this->basis_matrix[$this->x], $scale); - $yvec = $this->scale($this->basis_matrix[$this->y], $scale); - $face_corner = $this->sum2($xvec, $yvec); - - $zvec = $this->scale($this->basis_matrix[$this->z], $scale); - $x_corner = $this->sum2($xvec, $zvec); - $y_corner = $this->sum2($yvec, $zvec); - - imagefilledpolygon($img, $this->gen_poly($xo, $yo, $origin, $xvec, $x_corner,$zvec), 4, $colour1); - imagefilledpolygon($img, $this->gen_poly($xo, $yo, $origin, $yvec, $y_corner,$zvec), 4, $colour2); - - $face = $this->gen_poly($xo, $yo, $origin, $xvec, $face_corner, $yvec); - - imagefilledpolygon($img, $face, 4, $background); - imagepolygon($img, $face, 4, $colour1); - } - } - } - } - - /* - * return a roughly acceptable range of sizes for rendering with this texttype - */ - function range() - { - return array(3, 4); - } - - /** - * Vector length - */ - function vectorlen($vector) - { - return sqrt(pow($vector[0], 2) + pow($vector[1], 2) + pow($vector[2], 2)); - } - - /** - * Normalize - */ - function normalize(&$vector, $length = 1) - { - $length = (( $length < 1) ? 1 : $length); - $length /= $this->vectorlen($vector); - $vector[0] *= $length; - $vector[1] *= $length; - $vector[2] *= $length; - } - - /** - */ - function cross_product($vector1, $vector2) - { - $retval = array(0, 0, 0); - $retval[0] = (($vector1[1] * $vector2[2]) - ($vector1[2] * $vector2[1])); - $retval[1] = -(($vector1[0] * $vector2[2]) - ($vector1[2] * $vector2[0])); - $retval[2] = (($vector1[0] * $vector2[1]) - ($vector1[1] * $vector2[0])); - - return $retval; - } - - /** - */ - function sum($vector1, $vector2) - { - return array($vector1[0] + $vector2[0], $vector1[1] + $vector2[1], $vector1[2] + $vector2[2]); - } - - /** - */ - function sum2($vector1, $vector2) - { - return array($vector1[0] + $vector2[0], $vector1[1] + $vector2[1]); - } - - /** - */ - function scale($vector, $length) - { - if (sizeof($vector) == 2) - { - return array($vector[0] * $length, $vector[1] * $length); - } - - return array($vector[0] * $length, $vector[1] * $length, $vector[2] * $length); - } - - /** - */ - function gen_poly($xoff, $yoff, &$vec1, &$vec2, &$vec3, &$vec4) - { - $poly = array(); - $poly[0] = $xoff + $vec1[0]; - $poly[1] = $yoff + $vec1[1]; - $poly[2] = $xoff + $vec2[0]; - $poly[3] = $yoff + $vec2[1]; - $poly[4] = $xoff + $vec3[0]; - $poly[5] = $yoff + $vec3[1]; - $poly[6] = $xoff + $vec4[0]; - $poly[7] = $yoff + $vec4[1]; - - return $poly; - } - - /** - * dimensions - */ - function dimensions($size) - { - $xn = $this->scale($this->basis_matrix[$this->x], -($this->bitmap_width / 2) * $size); - $xp = $this->scale($this->basis_matrix[$this->x], ($this->bitmap_width / 2) * $size); - $yn = $this->scale($this->basis_matrix[$this->y], -($this->bitmap_height / 2) * $size); - $yp = $this->scale($this->basis_matrix[$this->y], ($this->bitmap_height / 2) * $size); - - $p = array(); - $p[0] = $this->sum2($xn, $yn); - $p[1] = $this->sum2($xp, $yn); - $p[2] = $this->sum2($xp, $yp); - $p[3] = $this->sum2($xn, $yp); - - $min_x = $max_x = $p[0][0]; - $min_y = $max_y = $p[0][1]; - - for ($i = 1; $i < 4; ++$i) - { - $min_x = ($min_x > $p[$i][0]) ? $p[$i][0] : $min_x; - $min_y = ($min_y > $p[$i][1]) ? $p[$i][1] : $min_y; - $max_x = ($max_x < $p[$i][0]) ? $p[$i][0] : $max_x; - $max_y = ($max_y < $p[$i][1]) ? $p[$i][1] : $max_y; - } - - return array($min_x, $min_y, $max_x, $max_y); - } -} - -class colour_manager -{ - var $img; - var $mode; - var $colours; - var $named_colours; - - /** - * Create the colour manager, link it to the image resource - */ - function __construct($img, $background = false, $mode = 'ahsv') - { - $this->img = $img; - $this->mode = $mode; - $this->colours = array(); - $this->named_colours = array(); - - if ($background !== false) - { - $bg = $this->allocate_named('background', $background); - imagefill($this->img, 0, 0, $bg); - } - } - - /** - * Lookup a named colour resource - */ - function get_resource($named_colour) - { - if (isset($this->named_colours[$named_colour])) - { - return $this->named_colours[$named_colour]; - } - - if (isset($this->named_rgb[$named_colour])) - { - return $this->allocate_named($named_colour, $this->named_rgb[$named_colour], 'rgb'); - } - - return false; - } - - /** - * Assign a name to a colour resource - */ - function name_colour($name, $resource) - { - $this->named_colours[$name] = $resource; - } - - /** - * names and allocates a colour resource - */ - function allocate_named($name, $colour, $mode = false) - { - $resource = $this->allocate($colour, $mode); - - if ($resource !== false) - { - $this->name_colour($name, $resource); - } - return $resource; - } - - /** - * allocates a specified colour into the image - */ - function allocate($colour, $mode = false) - { - if ($mode === false) - { - $mode = $this->mode; - } - - if (!is_array($colour)) - { - if (isset($this->named_rgb[$colour])) - { - return $this->allocate_named($colour, $this->named_rgb[$colour], 'rgb'); - } - - if (!is_int($colour)) - { - return false; - } - - $mode = 'rgb'; - $colour = array(255 & ($colour >> 16), 255 & ($colour >> 8), 255 & $colour); - } - - if (isset($colour['mode'])) - { - $mode = $colour['mode']; - unset($colour['mode']); - } - - if (isset($colour['random'])) - { - unset($colour['random']); - // everything else is params - return $this->random_colour($colour, $mode); - } - - $rgb = $this->model_convert($colour, $mode, 'rgb'); - $store = ($this->mode == 'rgb') ? $rgb : $this->model_convert($colour, $mode, $this->mode); - $resource = imagecolorallocate($this->img, $rgb[0], $rgb[1], $rgb[2]); - $this->colours[$resource] = $store; - - return $resource; - } - - /** - * randomly generates a colour, with optional params - */ - function random_colour($params = array(), $mode = false) - { - if ($mode === false) - { - $mode = $this->mode; - } - - switch ($mode) - { - case 'rgb': - // @TODO random rgb generation. do we intend to do this, or is it just too tedious? - break; - - case 'ahsv': - case 'hsv': - default: - - $default_params = array( - 'hue_bias' => false, // degree / 'r'/'g'/'b'/'c'/'m'/'y' /'o' - 'hue_range' => false, // if hue bias, then difference range +/- from bias - 'min_saturation' => 30, // 0 - 100 - 'max_saturation' => 80, // 0 - 100 - 'min_value' => 30, // 0 - 100 - 'max_value' => 80, // 0 - 100 - ); - - $alt = ($mode == 'ahsv') ? true : false; - $params = array_merge($default_params, $params); - - $min_hue = 0; - $max_hue = 359; - $min_saturation = max(0, $params['min_saturation']); - $max_saturation = min(100, $params['max_saturation']); - $min_value = max(0, $params['min_value']); - $max_value = min(100, $params['max_value']); - - if ($params['hue_bias'] !== false) - { - if (is_numeric($params['hue_bias'])) - { - $h = intval($params['hue_bias']) % 360; - } - else - { - switch ($params['hue_bias']) - { - case 'o': - $h = $alt ? 60 : 30; - break; - - case 'y': - $h = $alt ? 120 : 60; - break; - - case 'g': - $h = $alt ? 180 : 120; - break; - - case 'c': - $h = $alt ? 210 : 180; - break; - - case 'b': - $h = 240; - break; - - case 'm': - $h = 300; - break; - - case 'r': - default: - $h = 0; - break; - } - } - - $min_hue = $h + 360; - $max_hue = $h + 360; - - if ($params['hue_range']) - { - $min_hue -= min(180, $params['hue_range']); - $max_hue += min(180, $params['hue_range']); - } - } - - $h = mt_rand($min_hue, $max_hue); - $s = mt_rand($min_saturation, $max_saturation); - $v = mt_rand($min_value, $max_value); - - return $this->allocate(array($h, $s, $v), $mode); - - break; - } - } - - /** - */ - function colour_scheme($resource, $include_original = true) - { - $mode = 'hsv'; - - if (($pre = $this->get_resource($resource)) !== false) - { - $resource = $pre; - } - - $colour = $this->model_convert($this->colours[$resource], $this->mode, $mode); - $results = ($include_original) ? array($resource) : array(); - $colour2 = $colour3 = $colour4 = $colour; - $colour2[0] += 150; - $colour3[0] += 180; - $colour4[0] += 210; - - $results[] = $this->allocate($colour2, $mode); - $results[] = $this->allocate($colour3, $mode); - $results[] = $this->allocate($colour4, $mode); - - return $results; - } - - /** - */ - function mono_range($resource, $count = 5, $include_original = true) - { - if (is_array($resource)) - { - $results = array(); - for ($i = 0, $size = sizeof($resource); $i < $size; ++$i) - { - $results = array_merge($results, $this->mono_range($resource[$i], $count, $include_original)); - } - return $results; - } - - $mode = (in_array($this->mode, array('hsv', 'ahsv'), true) ? $this->mode : 'ahsv'); - if (($pre = $this->get_resource($resource)) !== false) - { - $resource = $pre; - } - - $colour = $this->model_convert($this->colours[$resource], $this->mode, $mode); - - $results = array(); - if ($include_original) - { - $results[] = $resource; - $count--; - } - - // This is a hard problem. I chicken out and try to maintain readability at the cost of less randomness. - - while ($count > 0) - { - $colour[1] = ($colour[1] + mt_rand(40,60)) % 99; - $colour[2] = ($colour[2] + mt_rand(40,60)); - $results[] = $this->allocate($colour, $mode); - $count--; - } - return $results; - } - - /** - * Convert from one colour model to another - */ - function model_convert($colour, $from_model, $to_model) - { - if ($from_model == $to_model) - { - return $colour; - } - - switch ($to_model) - { - case 'hsv': - - switch ($from_model) - { - case 'ahsv': - return $this->ah2h($colour); - break; - - case 'rgb': - return $this->rgb2hsv($colour); - break; - } - break; - - case 'ahsv': - - switch ($from_model) - { - case 'hsv': - return $this->h2ah($colour); - break; - - case 'rgb': - return $this->h2ah($this->rgb2hsv($colour)); - break; - } - break; - - case 'rgb': - switch ($from_model) - { - case 'hsv': - return $this->hsv2rgb($colour); - break; - - case 'ahsv': - return $this->hsv2rgb($this->ah2h($colour)); - break; - } - break; - } - return false; - } - - /** - * Slightly altered from wikipedia's algorithm - */ - function hsv2rgb($hsv) - { - $this->normalize_hue($hsv[0]); - - $h = $hsv[0]; - $s = min(1, max(0, $hsv[1] / 100)); - $v = min(1, max(0, $hsv[2] / 100)); - - // calculate hue sector - $hi = floor($hsv[0] / 60); - - // calculate opposite colour - $p = $v * (1 - $s); - - // calculate distance between hex vertices - $f = ($h / 60) - $hi; - - // coming in or going out? - if (!($hi & 1)) - { - $f = 1 - $f; - } - - // calculate adjacent colour - $q = $v * (1 - ($f * $s)); - - switch ($hi) - { - case 0: - $rgb = array($v, $q, $p); - break; - - case 1: - $rgb = array($q, $v, $p); - break; - - case 2: - $rgb = array($p, $v, $q); - break; - - case 3: - $rgb = array($p, $q, $v); - break; - - case 4: - $rgb = array($q, $p, $v); - break; - - case 5: - $rgb = array($v, $p, $q); - break; - - default: - return array(0, 0, 0); - break; - } - - return array(255 * $rgb[0], 255 * $rgb[1], 255 * $rgb[2]); - } - - /** - * (more than) Slightly altered from wikipedia's algorithm - */ - function rgb2hsv($rgb) - { - $r = min(255, max(0, $rgb[0])); - $g = min(255, max(0, $rgb[1])); - $b = min(255, max(0, $rgb[2])); - $max = max($r, $g, $b); - $min = min($r, $g, $b); - - $v = $max / 255; - $s = (!$max) ? 0 : 1 - ($min / $max); - - // if max - min is 0, we want hue to be 0 anyway. - $h = $max - $min; - - if ($h) - { - switch ($max) - { - case $g: - $h = 120 + (60 * ($b - $r) / $h); - break; - - case $b: - $h = 240 + (60 * ($r - $g) / $h); - break; - - case $r: - $h = 360 + (60 * ($g - $b) / $h); - break; - } - } - $this->normalize_hue($h); - - return array($h, $s * 100, $v * 100); - } - - /** - */ - function normalize_hue(&$hue) - { - $hue %= 360; - - if ($hue < 0) - { - $hue += 360; - } - } - - /** - * Alternate hue to hue - */ - function ah2h($ahue) - { - if (is_array($ahue)) - { - $ahue[0] = $this->ah2h($ahue[0]); - return $ahue; - } - $this->normalize_hue($ahue); - - // blue through red is already ok - if ($ahue >= 240) - { - return $ahue; - } - - // ahue green is at 180 - if ($ahue >= 180) - { - // return (240 - (2 * (240 - $ahue))); - return (2 * $ahue) - 240; // equivalent - } - - // ahue yellow is at 120 (RYB rather than RGB) - if ($ahue >= 120) - { - return $ahue - 60; - } - - return $ahue / 2; - } - - /** - * hue to Alternate hue - */ - function h2ah($hue) - { - if (is_array($hue)) - { - $hue[0] = $this->h2ah($hue[0]); - return $hue; - } - $this->normalize_hue($hue); - - // blue through red is already ok - if ($hue >= 240) - { - return $hue; - } - else if ($hue <= 60) - { - return $hue * 2; - } - else if ($hue <= 120) - { - return $hue + 60; - } - else - { - return ($hue + 240) / 2; - } - } -} diff --git a/phpBB/phpbb/captcha/plugins/qa.php b/phpBB/phpbb/captcha/plugins/qa.php index 6dab573c3e..3d8112e0f0 100644 --- a/phpBB/phpbb/captcha/plugins/qa.php +++ b/phpBB/phpbb/captcha/plugins/qa.php @@ -13,12 +13,6 @@ namespace phpbb\captcha\plugins; -global $table_prefix; - -define('CAPTCHA_QUESTIONS_TABLE', $table_prefix . 'captcha_questions'); -define('CAPTCHA_ANSWERS_TABLE', $table_prefix . 'captcha_answers'); -define('CAPTCHA_QA_CONFIRM_TABLE', $table_prefix . 'qa_confirm'); - /** * And now to something completely different. Let's make a captcha without extending the abstract class. * QA CAPTCHA sample implementation @@ -41,7 +35,7 @@ class qa */ function init($type) { - global $config, $db, $user; + global $config, $db, $user, $table_prefix; // load our language file $user->add_lang('captcha_qa'); @@ -56,7 +50,7 @@ class qa // we need all defined questions - shouldn't be too many, so we can just grab them // try the user's lang first $sql = 'SELECT question_id - FROM ' . CAPTCHA_QUESTIONS_TABLE . " + FROM ' . $table_prefix . 'captcha_questions' . " WHERE lang_iso = '" . $db->sql_escape($user->lang_name) . "'"; $result = $db->sql_query($sql, 3600); @@ -72,7 +66,7 @@ class qa $this->question_lang = $config['default_lang']; $sql = 'SELECT question_id - FROM ' . CAPTCHA_QUESTIONS_TABLE . " + FROM ' . $table_prefix . 'captcha_questions' . " WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'"; $result = $db->sql_query($sql, 7200); @@ -96,11 +90,11 @@ class qa */ public function is_installed() { - global $db; + global $db, $table_prefix; $db_tool = new \phpbb\db\tools($db); - return $db_tool->sql_table_exists(CAPTCHA_QUESTIONS_TABLE); + return $db_tool->sql_table_exists($table_prefix . 'captcha_questions'); } /** @@ -108,7 +102,7 @@ class qa */ public function is_available() { - global $config, $db, $phpbb_root_path, $phpEx, $user; + global $config, $db, $phpbb_root_path, $phpEx, $user, $table_prefix; // load language file for pretty display in the ACP dropdown $user->add_lang('captcha_qa'); @@ -119,7 +113,7 @@ class qa } $sql = 'SELECT COUNT(question_id) AS question_count - FROM ' . CAPTCHA_QUESTIONS_TABLE . " + FROM ' . $table_prefix . 'captcha_questions' . " WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'"; $result = $db->sql_query($sql); $row = $db->sql_fetchrow($result); @@ -195,12 +189,12 @@ class qa */ function get_demo_template() { - global $config, $db, $template; + global $config, $db, $template, $table_prefix; if ($this->is_available()) { $sql = 'SELECT question_text - FROM ' . CAPTCHA_QUESTIONS_TABLE . " + FROM ' . $table_prefix . 'captcha_questions' . " WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'"; $result = $db->sql_query_limit($sql, 1); if ($row = $db->sql_fetchrow($result)) @@ -279,16 +273,16 @@ class qa */ function install() { - global $db; + global $db, $table_prefix; $db_tool = new \phpbb\db\tools($db); - $tables = array(CAPTCHA_QUESTIONS_TABLE, CAPTCHA_ANSWERS_TABLE, CAPTCHA_QA_CONFIRM_TABLE); + $tables = array($table_prefix . 'captcha_questions', CAPTCHA_ANSWERS_TABLE, CAPTCHA_QA_CONFIRM_TABLE); $schemas = array( - CAPTCHA_QUESTIONS_TABLE => array ( + $table_prefix . 'captcha_questions' => array ( 'COLUMNS' => array( - 'question_id' => array('UINT', Null, 'auto_increment'), + 'question_id' => array('UINT', null, 'auto_increment'), 'strict' => array('BOOL', 0), 'lang_id' => array('UINT', 0), 'lang_iso' => array('VCHAR:30', ''), @@ -480,7 +474,7 @@ class qa */ function load_answer() { - global $db, $user; + global $db, $user, $table_prefix; if (!strlen($this->confirm_id) || !sizeof($this->question_ids)) { @@ -488,7 +482,7 @@ class qa } $sql = 'SELECT con.question_id, attempts, question_text, strict - FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' con, ' . CAPTCHA_QUESTIONS_TABLE . " qes + FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' con, ' . $table_prefix . 'captcha_questions' . " qes WHERE con.question_id = qes.question_id AND confirm_id = '" . $db->sql_escape($this->confirm_id) . "' AND session_id = '" . $db->sql_escape($user->session_id) . "' @@ -731,10 +725,10 @@ class qa */ function acp_question_list(&$module) { - global $db, $template; + global $db, $template, $table_prefix; $sql = 'SELECT * - FROM ' . CAPTCHA_QUESTIONS_TABLE; + FROM ' . $table_prefix . 'captcha_questions'; $result = $db->sql_query($sql); $template->assign_vars(array( @@ -761,12 +755,12 @@ class qa */ function acp_get_question_data($question_id) { - global $db; + global $db, $table_prefix; if ($question_id) { $sql = 'SELECT * - FROM ' . CAPTCHA_QUESTIONS_TABLE . ' + FROM ' . $table_prefix . 'captcha_questions' . ' WHERE question_id = ' . $question_id; $result = $db->sql_query($sql); $question = $db->sql_fetchrow($result); @@ -816,7 +810,7 @@ class qa */ function acp_update_question($data, $question_id) { - global $db, $cache; + global $db, $cache, $table_prefix; // easier to delete all answers than to figure out which to update $sql = 'DELETE FROM ' . CAPTCHA_ANSWERS_TABLE . " WHERE question_id = $question_id"; @@ -827,14 +821,14 @@ class qa $question_ary['lang_id'] = $langs[$question_ary['lang_iso']]['id']; unset($question_ary['answers']); - $sql = 'UPDATE ' . CAPTCHA_QUESTIONS_TABLE . ' + $sql = 'UPDATE ' . $table_prefix . 'captcha_questions' . ' SET ' . $db->sql_build_array('UPDATE', $question_ary) . " WHERE question_id = $question_id"; $db->sql_query($sql); $this->acp_insert_answers($data, $question_id); - $cache->destroy('sql', CAPTCHA_QUESTIONS_TABLE); + $cache->destroy('sql', $table_prefix . 'captcha_questions'); } /** @@ -843,7 +837,7 @@ class qa */ function acp_add_question($data) { - global $db, $cache; + global $db, $cache, $table_prefix; $langs = $this->get_languages(); $question_ary = $data; @@ -851,14 +845,14 @@ class qa $question_ary['lang_id'] = $langs[$data['lang_iso']]['id']; unset($question_ary['answers']); - $sql = 'INSERT INTO ' . CAPTCHA_QUESTIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $question_ary); + $sql = 'INSERT INTO ' . $table_prefix . 'captcha_questions' . ' ' . $db->sql_build_array('INSERT', $question_ary); $db->sql_query($sql); $question_id = $db->sql_nextid(); $this->acp_insert_answers($data, $question_id); - $cache->destroy('sql', CAPTCHA_QUESTIONS_TABLE); + $cache->destroy('sql', $table_prefix . 'captcha_questions'); } /** @@ -888,9 +882,9 @@ class qa */ function acp_delete_question($question_id) { - global $db, $cache; + global $db, $cache, $table_prefix; - $tables = array(CAPTCHA_QUESTIONS_TABLE, CAPTCHA_ANSWERS_TABLE); + $tables = array($table_prefix . 'captcha_questions', CAPTCHA_ANSWERS_TABLE); foreach ($tables as $table) { @@ -960,12 +954,12 @@ class qa */ function acp_is_last($question_id) { - global $config, $db; + global $config, $db, $table_prefix; if ($question_id) { $sql = 'SELECT question_id - FROM ' . CAPTCHA_QUESTIONS_TABLE . " + FROM ' . $table_prefix . 'captcha_questions' . " WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "' AND question_id <> " . (int) $question_id; $result = $db->sql_query_limit($sql, 1); From e889b8f9318ab8e6324f6d5ca4fedc08ce418c8f Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 10 Aug 2014 13:15:07 +0200 Subject: [PATCH 13/21] [ticket/11854] Remove constants and use DI for QA tables PHPBB3-11854 --- phpBB/config/captcha.yml | 21 +++++--- phpBB/phpbb/captcha/plugins/qa.php | 78 ++++++++++++++++++------------ 2 files changed, 63 insertions(+), 36 deletions(-) diff --git a/phpBB/config/captcha.yml b/phpBB/config/captcha.yml index 0774319f93..15d6a4ce3d 100644 --- a/phpBB/config/captcha.yml +++ b/phpBB/config/captcha.yml @@ -1,3 +1,8 @@ +parameters: + tables.captcha_qa_questions: %core.table_prefix%captcha_questions + tables.captcha_qa_answers: %core.table_prefix%captcha_answers + tables.captcha_qa_confirm: %core.table_prefix%qa_confirm + services: captcha.factory: class: phpbb\captcha\factory @@ -10,34 +15,38 @@ services: arguments: - @service_container tags: - - { name: service_collection, tag: captchas.plugins } + - { name: service_collection, tag: captcha.plugins } core.captcha.plugins.gd: class: phpbb\captcha\plugins\gd scope: prototype # scope MUST be prototype for this to work! tags: - - { name: captchas.plugins } + - { name: captcha.plugins } core.captcha.plugins.gd_wave: class: phpbb\captcha\plugins\gd_wave scope: prototype # scope MUST be prototype for this to work! tags: - - { name: captchas.plugins } + - { name: captcha.plugins } core.captcha.plugins.nogd: class: phpbb\captcha\plugins\nogd scope: prototype # scope MUST be prototype for this to work! tags: - - { name: captchas.plugins } + - { name: captcha.plugins } core.captcha.plugins.qa: class: phpbb\captcha\plugins\qa scope: prototype # scope MUST be prototype for this to work! + arguments: + - %tables.captcha_qa_questions% + - %tables.captcha_qa_answers% + - %tables.captcha_qa_confirm% tags: - - { name: captchas.plugins } + - { name: captcha.plugins } core.captcha.plugins.recaptcha: class: phpbb\captcha\plugins\recaptcha scope: prototype # scope MUST be prototype for this to work! tags: - - { name: captchas.plugins } + - { name: captcha.plugins } diff --git a/phpBB/phpbb/captcha/plugins/qa.php b/phpBB/phpbb/captcha/plugins/qa.php index 3d8112e0f0..c0bcf86531 100644 --- a/phpBB/phpbb/captcha/plugins/qa.php +++ b/phpBB/phpbb/captcha/plugins/qa.php @@ -30,6 +30,24 @@ class qa // dirty trick: 0 is false, but can still encode that the captcha is not yet validated var $solved = 0; + protected $table_captcha_questions; + protected $table_captcha_answers; + protected $table_qa_confirm; + + /** + * Constructor + * + * @param string $table_captcha_questions + * @param string $table_captcha_answers + * @param string $table_qa_confirm + */ + function __construct($table_captcha_questions, $table_captcha_answers, $table_qa_confirm) + { + $this->table_captcha_questions = $table_captcha_questions; + $this->table_captcha_answers = $table_captcha_answers; + $this->table_qa_confirm = $table_qa_confirm; + } + /** * @param int $type as per the CAPTCHA API docs, the type */ @@ -50,7 +68,7 @@ class qa // we need all defined questions - shouldn't be too many, so we can just grab them // try the user's lang first $sql = 'SELECT question_id - FROM ' . $table_prefix . 'captcha_questions' . " + FROM ' . $this->table_captcha_questions . " WHERE lang_iso = '" . $db->sql_escape($user->lang_name) . "'"; $result = $db->sql_query($sql, 3600); @@ -66,7 +84,7 @@ class qa $this->question_lang = $config['default_lang']; $sql = 'SELECT question_id - FROM ' . $table_prefix . 'captcha_questions' . " + FROM ' . $this->table_captcha_questions . " WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'"; $result = $db->sql_query($sql, 7200); @@ -94,7 +112,7 @@ class qa $db_tool = new \phpbb\db\tools($db); - return $db_tool->sql_table_exists($table_prefix . 'captcha_questions'); + return $db_tool->sql_table_exists($this->table_captcha_questions); } /** @@ -113,7 +131,7 @@ class qa } $sql = 'SELECT COUNT(question_id) AS question_count - FROM ' . $table_prefix . 'captcha_questions' . " + FROM ' . $this->table_captcha_questions . " WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'"; $result = $db->sql_query($sql); $row = $db->sql_fetchrow($result); @@ -194,7 +212,7 @@ class qa if ($this->is_available()) { $sql = 'SELECT question_text - FROM ' . $table_prefix . 'captcha_questions' . " + FROM ' . $this->table_captcha_questions . " WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'"; $result = $db->sql_query_limit($sql, 1); if ($row = $db->sql_fetchrow($result)) @@ -233,7 +251,7 @@ class qa global $db, $config; $sql = 'SELECT c.confirm_id - FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' c + FROM ' . $this->table_qa_confirm . ' c LEFT JOIN ' . SESSIONS_TABLE . ' s ON (c.session_id = s.session_id) WHERE s.session_id IS NULL' . @@ -252,7 +270,7 @@ class qa if (sizeof($sql_in)) { - $sql = 'DELETE FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' + $sql = 'DELETE FROM ' . $this->table_qa_confirm . ' WHERE ' . $db->sql_in_set('confirm_id', $sql_in); $db->sql_query($sql); } @@ -277,10 +295,10 @@ class qa $db_tool = new \phpbb\db\tools($db); - $tables = array($table_prefix . 'captcha_questions', CAPTCHA_ANSWERS_TABLE, CAPTCHA_QA_CONFIRM_TABLE); + $tables = array($this->table_captcha_questions, $this->table_captcha_answers, $this->table_qa_confirm); $schemas = array( - $table_prefix . 'captcha_questions' => array ( + $this->table_captcha_questions => array ( 'COLUMNS' => array( 'question_id' => array('UINT', null, 'auto_increment'), 'strict' => array('BOOL', 0), @@ -293,7 +311,7 @@ class qa 'lang' => array('INDEX', 'lang_iso'), ), ), - CAPTCHA_ANSWERS_TABLE => array ( + $this->table_captcha_answers => array ( 'COLUMNS' => array( 'question_id' => array('UINT', 0), 'answer_text' => array('STEXT_UNI', ''), @@ -302,7 +320,7 @@ class qa 'qid' => array('INDEX', 'question_id'), ), ), - CAPTCHA_QA_CONFIRM_TABLE => array ( + $this->table_qa_confirm => array ( 'COLUMNS' => array( 'session_id' => array('CHAR:32', ''), 'confirm_id' => array('CHAR:32', ''), @@ -386,7 +404,7 @@ class qa $this->confirm_id = md5(unique_id($user->ip)); $this->question = (int) array_rand($this->question_ids); - $sql = 'INSERT INTO ' . CAPTCHA_QA_CONFIRM_TABLE . ' ' . $db->sql_build_array('INSERT', array( + $sql = 'INSERT INTO ' . $this->table_qa_confirm . ' ' . $db->sql_build_array('INSERT', array( 'confirm_id' => (string) $this->confirm_id, 'session_id' => (string) $user->session_id, 'lang_iso' => (string) $this->question_lang, @@ -413,7 +431,7 @@ class qa $this->question = (int) array_rand($this->question_ids); $this->solved = 0; - $sql = 'UPDATE ' . CAPTCHA_QA_CONFIRM_TABLE . ' + $sql = 'UPDATE ' . $this->table_qa_confirm . ' SET question_id = ' . (int) $this->question . " WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "' AND session_id = '" . $db->sql_escape($user->session_id) . "'"; @@ -433,7 +451,7 @@ class qa $this->question = (int) array_rand($this->question_ids); $this->solved = 0; - $sql = 'UPDATE ' . CAPTCHA_QA_CONFIRM_TABLE . ' + $sql = 'UPDATE ' . $this->table_qa_confirm . ' SET question_id = ' . (int) $this->question . ", attempts = attempts + 1 WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "' @@ -452,7 +470,7 @@ class qa global $db, $user; $sql = 'SELECT confirm_id - FROM ' . CAPTCHA_QA_CONFIRM_TABLE . " + FROM ' . $this->table_qa_confirm . " WHERE session_id = '" . $db->sql_escape($user->session_id) . "' AND lang_iso = '" . $db->sql_escape($this->question_lang) . "' @@ -482,7 +500,7 @@ class qa } $sql = 'SELECT con.question_id, attempts, question_text, strict - FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' con, ' . $table_prefix . 'captcha_questions' . " qes + FROM ' . $this->table_qa_confirm . ' con, ' . $this->table_captcha_questions . " qes WHERE con.question_id = qes.question_id AND confirm_id = '" . $db->sql_escape($this->confirm_id) . "' AND session_id = '" . $db->sql_escape($user->session_id) . "' @@ -516,7 +534,7 @@ class qa $answer = ($this->question_strict) ? utf8_normalize_nfc(request_var('qa_answer', '', true)) : utf8_clean_string(utf8_normalize_nfc(request_var('qa_answer', '', true))); $sql = 'SELECT answer_text - FROM ' . CAPTCHA_ANSWERS_TABLE . ' + FROM ' . $this->table_captcha_answers . ' WHERE question_id = ' . (int) $this->question; $result = $db->sql_query($sql); @@ -551,7 +569,7 @@ class qa { global $db, $user; - $sql = 'DELETE FROM ' . CAPTCHA_QA_CONFIRM_TABLE . " + $sql = 'DELETE FROM ' . $this->table_qa_confirm . " WHERE session_id = '" . $db->sql_escape($user->session_id) . "' AND confirm_type = " . (int) $this->type; $db->sql_query($sql); @@ -728,7 +746,7 @@ class qa global $db, $template, $table_prefix; $sql = 'SELECT * - FROM ' . $table_prefix . 'captcha_questions'; + FROM ' . $this->table_captcha_questions; $result = $db->sql_query($sql); $template->assign_vars(array( @@ -760,7 +778,7 @@ class qa if ($question_id) { $sql = 'SELECT * - FROM ' . $table_prefix . 'captcha_questions' . ' + FROM ' . $this->table_captcha_questions . ' WHERE question_id = ' . $question_id; $result = $db->sql_query($sql); $question = $db->sql_fetchrow($result); @@ -774,7 +792,7 @@ class qa $question['answers'] = array(); $sql = 'SELECT * - FROM ' . CAPTCHA_ANSWERS_TABLE . ' + FROM ' . $this->table_captcha_answers . ' WHERE question_id = ' . $question_id; $result = $db->sql_query($sql); @@ -813,7 +831,7 @@ class qa global $db, $cache, $table_prefix; // easier to delete all answers than to figure out which to update - $sql = 'DELETE FROM ' . CAPTCHA_ANSWERS_TABLE . " WHERE question_id = $question_id"; + $sql = 'DELETE FROM ' . $this->table_captcha_answers . " WHERE question_id = $question_id"; $db->sql_query($sql); $langs = $this->get_languages(); @@ -821,14 +839,14 @@ class qa $question_ary['lang_id'] = $langs[$question_ary['lang_iso']]['id']; unset($question_ary['answers']); - $sql = 'UPDATE ' . $table_prefix . 'captcha_questions' . ' + $sql = 'UPDATE ' . $this->table_captcha_questions . ' SET ' . $db->sql_build_array('UPDATE', $question_ary) . " WHERE question_id = $question_id"; $db->sql_query($sql); $this->acp_insert_answers($data, $question_id); - $cache->destroy('sql', $table_prefix . 'captcha_questions'); + $cache->destroy('sql', $this->table_captcha_questions); } /** @@ -845,14 +863,14 @@ class qa $question_ary['lang_id'] = $langs[$data['lang_iso']]['id']; unset($question_ary['answers']); - $sql = 'INSERT INTO ' . $table_prefix . 'captcha_questions' . ' ' . $db->sql_build_array('INSERT', $question_ary); + $sql = 'INSERT INTO ' . $this->table_captcha_questions . ' ' . $db->sql_build_array('INSERT', $question_ary); $db->sql_query($sql); $question_id = $db->sql_nextid(); $this->acp_insert_answers($data, $question_id); - $cache->destroy('sql', $table_prefix . 'captcha_questions'); + $cache->destroy('sql', $this->table_captcha_questions); } /** @@ -870,11 +888,11 @@ class qa 'answer_text' => $answer, ); - $sql = 'INSERT INTO ' . CAPTCHA_ANSWERS_TABLE . ' ' . $db->sql_build_array('INSERT', $answer_ary); + $sql = 'INSERT INTO ' . $this->table_captcha_answers . ' ' . $db->sql_build_array('INSERT', $answer_ary); $db->sql_query($sql); } - $cache->destroy('sql', CAPTCHA_ANSWERS_TABLE); + $cache->destroy('sql', $this->table_captcha_answers); } /** @@ -884,7 +902,7 @@ class qa { global $db, $cache, $table_prefix; - $tables = array($table_prefix . 'captcha_questions', CAPTCHA_ANSWERS_TABLE); + $tables = array($this->table_captcha_questions, $this->table_captcha_answers); foreach ($tables as $table) { @@ -959,7 +977,7 @@ class qa if ($question_id) { $sql = 'SELECT question_id - FROM ' . $table_prefix . 'captcha_questions' . " + FROM ' . $this->table_captcha_questions . " WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "' AND question_id <> " . (int) $question_id; $result = $db->sql_query_limit($sql, 1); From 036a61a904ce1a3023bb137fbfaf546b473c528d Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 10 Aug 2014 14:53:15 +0200 Subject: [PATCH 14/21] [ticket/11854] Remove unused globals PHPBB3-11854 --- phpBB/phpbb/captcha/plugins/qa.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/phpBB/phpbb/captcha/plugins/qa.php b/phpBB/phpbb/captcha/plugins/qa.php index c0bcf86531..96cd99826d 100644 --- a/phpBB/phpbb/captcha/plugins/qa.php +++ b/phpBB/phpbb/captcha/plugins/qa.php @@ -53,7 +53,7 @@ class qa */ function init($type) { - global $config, $db, $user, $table_prefix; + global $config, $db, $user; // load our language file $user->add_lang('captcha_qa'); @@ -108,7 +108,7 @@ class qa */ public function is_installed() { - global $db, $table_prefix; + global $db; $db_tool = new \phpbb\db\tools($db); @@ -120,7 +120,7 @@ class qa */ public function is_available() { - global $config, $db, $phpbb_root_path, $phpEx, $user, $table_prefix; + global $config, $db, $phpbb_root_path, $phpEx, $user; // load language file for pretty display in the ACP dropdown $user->add_lang('captcha_qa'); @@ -207,7 +207,7 @@ class qa */ function get_demo_template() { - global $config, $db, $template, $table_prefix; + global $config, $db, $template; if ($this->is_available()) { @@ -291,7 +291,7 @@ class qa */ function install() { - global $db, $table_prefix; + global $db; $db_tool = new \phpbb\db\tools($db); @@ -492,7 +492,7 @@ class qa */ function load_answer() { - global $db, $user, $table_prefix; + global $db, $user; if (!strlen($this->confirm_id) || !sizeof($this->question_ids)) { @@ -743,7 +743,7 @@ class qa */ function acp_question_list(&$module) { - global $db, $template, $table_prefix; + global $db, $template; $sql = 'SELECT * FROM ' . $this->table_captcha_questions; @@ -773,7 +773,7 @@ class qa */ function acp_get_question_data($question_id) { - global $db, $table_prefix; + global $db; if ($question_id) { @@ -828,7 +828,7 @@ class qa */ function acp_update_question($data, $question_id) { - global $db, $cache, $table_prefix; + global $db, $cache; // easier to delete all answers than to figure out which to update $sql = 'DELETE FROM ' . $this->table_captcha_answers . " WHERE question_id = $question_id"; @@ -855,7 +855,7 @@ class qa */ function acp_add_question($data) { - global $db, $cache, $table_prefix; + global $db, $cache; $langs = $this->get_languages(); $question_ary = $data; @@ -900,7 +900,7 @@ class qa */ function acp_delete_question($question_id) { - global $db, $cache, $table_prefix; + global $db, $cache; $tables = array($this->table_captcha_questions, $this->table_captcha_answers); @@ -972,7 +972,7 @@ class qa */ function acp_is_last($question_id) { - global $config, $db, $table_prefix; + global $config, $db; if ($question_id) { From 2cbcf4bcbf5ece84fcfef8848233421f386972f3 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 10 Aug 2014 14:57:50 +0200 Subject: [PATCH 15/21] [ticket/11854] Fix the factory in \phpbb\auth\provider\db PHPBB3-11854 --- phpBB/config/auth_providers.yml | 1 + phpBB/phpbb/auth/provider/db.php | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/phpBB/config/auth_providers.yml b/phpBB/config/auth_providers.yml index d2f22ec477..db7c603bcd 100644 --- a/phpBB/config/auth_providers.yml +++ b/phpBB/config/auth_providers.yml @@ -14,6 +14,7 @@ services: - @passwords.manager - @request - @user + - @service_container - %core.root_path% - %core.php_ext% tags: diff --git a/phpBB/phpbb/auth/provider/db.php b/phpBB/phpbb/auth/provider/db.php index cb7a2f8b1a..722eeffa9a 100644 --- a/phpBB/phpbb/auth/provider/db.php +++ b/phpBB/phpbb/auth/provider/db.php @@ -26,6 +26,13 @@ class db extends \phpbb\auth\provider\base */ protected $passwords_manager; + /** + * DI container + * + * @var \Symfony\Component\DependencyInjection\ContainerInterface + */ + protected $phpbb_container; + /** * Database Authentication Constructor * @@ -34,10 +41,11 @@ class db extends \phpbb\auth\provider\base * @param \phpbb\passwords\manager $passwords_manager * @param \phpbb\request\request $request * @param \phpbb\user $user + * @param \Symfony\Component\DependencyInjection\ContainerInterface $phpbb_container DI container * @param string $phpbb_root_path * @param string $php_ext */ - public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\passwords\manager $passwords_manager, \phpbb\request\request $request, \phpbb\user $user, $phpbb_root_path, $php_ext) + public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\passwords\manager $passwords_manager, \phpbb\request\request $request, \phpbb\user $user, \Symfony\Component\DependencyInjection\ContainerInterface $phpbb_container, $phpbb_root_path, $php_ext) { $this->db = $db; $this->config = $config; @@ -46,6 +54,7 @@ class db extends \phpbb\auth\provider\base $this->user = $user; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; + $this->phpbb_container = $phpbb_container; } /** @@ -146,7 +155,8 @@ class db extends \phpbb\auth\provider\base // Every auth module is able to define what to do by itself... if ($show_captcha) { - $captcha = $this->captchas_manager->get_instance($this->config['captcha_plugin']); + $captcha_factory = $this->phpbb_container->get('captcha.factory'); + $captcha = $captcha_factory->get_instance($this->config['captcha_plugin']); $captcha->init(CONFIRM_LOGIN); $vc_response = $captcha->validate($row); if ($vc_response) From 351a6dcad245ce302def5952e631df6bad9c694d Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 10 Aug 2014 14:58:48 +0200 Subject: [PATCH 16/21] [ticket/11854] Base the migration on rc2 PHPBB3-11854 --- phpBB/phpbb/db/migration/data/v310/captcha_plugins.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php b/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php index e6f15ca407..13071e9891 100644 --- a/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php +++ b/phpBB/phpbb/db/migration/data/v310/captcha_plugins.php @@ -18,7 +18,7 @@ class captcha_plugins extends \phpbb\db\migration\migration static public function depends_on() { return array( - '\phpbb\db\migration\data\v310\dev', + '\phpbb\db\migration\data\v310\rc2', ); } From fc4b1090cc9685efd0ac98bac9bff2582f0b6858 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 10 Aug 2014 15:02:55 +0200 Subject: [PATCH 17/21] [ticket/11854] Fix comments PHPBB3-11854 --- phpBB/phpbb/captcha/factory.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/phpBB/phpbb/captcha/factory.php b/phpBB/phpbb/captcha/factory.php index 19c2f46e89..dd44aca8bb 100644 --- a/phpBB/phpbb/captcha/factory.php +++ b/phpBB/phpbb/captcha/factory.php @@ -13,9 +13,6 @@ namespace phpbb\captcha; -/** -* A small class for 3.0.x (no autoloader in 3.0.x) -*/ class factory { /** @@ -53,6 +50,8 @@ class factory /** * Call the garbage collector + * + * @param string $name The name to the captcha service. */ function garbage_collect($name) { @@ -61,7 +60,9 @@ class factory } /** - * return a list of all registered CAPTCHA plugins + * Return a list of all registered CAPTCHA plugins + * + * @returns array */ function get_captcha_types() { From 76633544ee6084e61641ec423cd0480bb4b771c5 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 10 Aug 2014 15:03:42 +0200 Subject: [PATCH 18/21] [ticket/11854] Change the error message in reCaptcha PHPBB3-11854 --- phpBB/phpbb/captcha/plugins/recaptcha.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/captcha/plugins/recaptcha.php b/phpBB/phpbb/captcha/plugins/recaptcha.php index ca131ba8a0..efa93ab72a 100644 --- a/phpBB/phpbb/captcha/plugins/recaptcha.php +++ b/phpBB/phpbb/captcha/plugins/recaptcha.php @@ -76,7 +76,7 @@ class recaptcha extends \phpbb\captcha\plugins\captcha_abstract */ function get_generator_class() { - throw new \Exception('Go out devil!'); + throw new \Exception('No generator class given.'); } function acp_page($id, &$module) From fa2ac1f27126292a24aa33136b2d3a7f1832ea47 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 10 Aug 2014 15:10:14 +0200 Subject: [PATCH 19/21] [ticket/11854] Use a set_name method instead of overriding get_service_name PHPBB3-11854 --- phpBB/config/captcha.yml | 10 ++++++++++ .../captcha/plugins/captcha_abstract.php | 20 ++++++++++++++++++- phpBB/phpbb/captcha/plugins/gd.php | 8 -------- phpBB/phpbb/captcha/plugins/gd_wave.php | 8 -------- phpBB/phpbb/captcha/plugins/qa.php | 19 ++++++++++++++++-- phpBB/phpbb/captcha/plugins/recaptcha.php | 8 -------- 6 files changed, 46 insertions(+), 27 deletions(-) diff --git a/phpBB/config/captcha.yml b/phpBB/config/captcha.yml index 15d6a4ce3d..bca37767af 100644 --- a/phpBB/config/captcha.yml +++ b/phpBB/config/captcha.yml @@ -20,18 +20,24 @@ services: core.captcha.plugins.gd: class: phpbb\captcha\plugins\gd scope: prototype # scope MUST be prototype for this to work! + calls: + - [set_name, [core.captcha.plugins.gd]] tags: - { name: captcha.plugins } core.captcha.plugins.gd_wave: class: phpbb\captcha\plugins\gd_wave scope: prototype # scope MUST be prototype for this to work! + calls: + - [set_name, [core.captcha.plugins.gd_wave]] tags: - { name: captcha.plugins } core.captcha.plugins.nogd: class: phpbb\captcha\plugins\nogd scope: prototype # scope MUST be prototype for this to work! + calls: + - [set_name, [core.captcha.plugins.nogd]] tags: - { name: captcha.plugins } @@ -42,11 +48,15 @@ services: - %tables.captcha_qa_questions% - %tables.captcha_qa_answers% - %tables.captcha_qa_confirm% + calls: + - [set_name, [core.captcha.plugins.qa]] tags: - { name: captcha.plugins } core.captcha.plugins.recaptcha: class: phpbb\captcha\plugins\recaptcha scope: prototype # scope MUST be prototype for this to work! + calls: + - [set_name, [core.captcha.plugins.recaptcha]] tags: - { name: captcha.plugins } diff --git a/phpBB/phpbb/captcha/plugins/captcha_abstract.php b/phpBB/phpbb/captcha/plugins/captcha_abstract.php index 982798e464..24ed7f939d 100644 --- a/phpBB/phpbb/captcha/plugins/captcha_abstract.php +++ b/phpBB/phpbb/captcha/plugins/captcha_abstract.php @@ -27,6 +27,11 @@ abstract class captcha_abstract var $solved = 0; var $captcha_vars = false; + /** + * @var string name of the service. + */ + protected $service_name; + function init($type) { global $config, $db, $user; @@ -363,7 +368,20 @@ abstract class captcha_abstract /** * @return string the name of the service corresponding to the plugin */ - abstract function get_service_name(); + function get_service_name() + { + return $this->service_name; + } + + /** + * Set the name of the plugin + * + * @param string $name + */ + public function set_name($name) + { + $this->service_name = $name; + } /** * @return string the name of the class used to generate the captcha diff --git a/phpBB/phpbb/captcha/plugins/gd.php b/phpBB/phpbb/captcha/plugins/gd.php index c1d8c83095..f6200b5b2f 100644 --- a/phpBB/phpbb/captcha/plugins/gd.php +++ b/phpBB/phpbb/captcha/plugins/gd.php @@ -30,14 +30,6 @@ class gd extends captcha_abstract return @extension_loaded('gd'); } - /** - * @return string the name of the service corresponding to the plugin - */ - function get_service_name() - { - return 'core.captcha.plugins.gd'; - } - /** * @return string the name of the class used to generate the captcha */ diff --git a/phpBB/phpbb/captcha/plugins/gd_wave.php b/phpBB/phpbb/captcha/plugins/gd_wave.php index 2d38ef08dd..e1d44df778 100644 --- a/phpBB/phpbb/captcha/plugins/gd_wave.php +++ b/phpBB/phpbb/captcha/plugins/gd_wave.php @@ -25,14 +25,6 @@ class gd_wave extends captcha_abstract return 'CAPTCHA_GD_3D'; } - /** - * @return string the name of the service corresponding to the plugin - */ - function get_service_name() - { - return 'core.captcha.plugins.gd_wave'; - } - /** * @return string the name of the class used to generate the captcha */ diff --git a/phpBB/phpbb/captcha/plugins/qa.php b/phpBB/phpbb/captcha/plugins/qa.php index 96cd99826d..a7ba994cc3 100644 --- a/phpBB/phpbb/captcha/plugins/qa.php +++ b/phpBB/phpbb/captcha/plugins/qa.php @@ -34,6 +34,11 @@ class qa protected $table_captcha_answers; protected $table_qa_confirm; + /** + * @var string name of the service. + */ + protected $service_name; + /** * Constructor * @@ -157,11 +162,21 @@ class qa } /** - * API function + * @return string the name of the service corresponding to the plugin */ function get_service_name() { - return 'core.captcha.plugins.qa'; + return $this->service_name; + } + + /** + * Set the name of the plugin + * + * @param string $name + */ + public function set_name($name) + { + $this->service_name = $name; } /** diff --git a/phpBB/phpbb/captcha/plugins/recaptcha.php b/phpBB/phpbb/captcha/plugins/recaptcha.php index efa93ab72a..928694f6ca 100644 --- a/phpBB/phpbb/captcha/plugins/recaptcha.php +++ b/phpBB/phpbb/captcha/plugins/recaptcha.php @@ -63,14 +63,6 @@ class recaptcha extends \phpbb\captcha\plugins\captcha_abstract return 'CAPTCHA_RECAPTCHA'; } - /** - * @return string the name of the service corresponding to the plugin - */ - function get_service_name() - { - return 'core.captcha.plugins.recaptcha'; - } - /** * This function is implemented because required by the upper class, but is never used for reCaptcha. */ From 4683864b4bb710a32a6a9078bfa2c7d929c3f1a1 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 10 Aug 2014 15:14:00 +0200 Subject: [PATCH 20/21] [ticket/11854] Update tests PHPBB3-11854 --- tests/auth/provider_db_test.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/auth/provider_db_test.php b/tests/auth/provider_db_test.php index 744f955531..e33eae6b54 100644 --- a/tests/auth/provider_db_test.php +++ b/tests/auth/provider_db_test.php @@ -52,7 +52,9 @@ class phpbb_auth_provider_db_test extends phpbb_database_test_case // Set up passwords manager $passwords_manager = new \phpbb\passwords\manager($config, $passwords_drivers, $passwords_helper, array_keys($passwords_drivers)); - $provider = new \phpbb\auth\provider\db($db, $config, $passwords_manager, $request, $user, $phpbb_root_path, $phpEx); + $phpbb_container = new phpbb_mock_container_builder(); + + $provider = new \phpbb\auth\provider\db($db, $config, $passwords_manager, $request, $user, $phpbb_container, $phpbb_root_path, $phpEx); if (version_compare(PHP_VERSION, '5.3.7', '<')) { $password_hash = '$2a$10$e01Syh9PbJjUkio66eFuUu4FhCE2nRgG7QPc1JACalsPXcIuG2bbi'; From 756aa68b34e8584c7e010b2295ff63e485be1907 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Mon, 11 Aug 2014 08:04:14 +0200 Subject: [PATCH 21/21] [ticket/11854] Don't override get service name in nogd PHPBB3-11854 --- phpBB/phpbb/captcha/plugins/nogd.php | 8 -------- phpBB/phpbb/captcha/plugins/recaptcha.php | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/phpBB/phpbb/captcha/plugins/nogd.php b/phpBB/phpbb/captcha/plugins/nogd.php index dbf0664a62..6845e5935c 100644 --- a/phpBB/phpbb/captcha/plugins/nogd.php +++ b/phpBB/phpbb/captcha/plugins/nogd.php @@ -25,14 +25,6 @@ class nogd extends captcha_abstract return 'CAPTCHA_NO_GD'; } - /** - * @return string the name of the service corresponding to the plugin - */ - function get_service_name() - { - return 'core.captcha.plugins.nogd'; - } - /** * @return string the name of the class used to generate the captcha */ diff --git a/phpBB/phpbb/captcha/plugins/recaptcha.php b/phpBB/phpbb/captcha/plugins/recaptcha.php index 928694f6ca..ea446d7bc3 100644 --- a/phpBB/phpbb/captcha/plugins/recaptcha.php +++ b/phpBB/phpbb/captcha/plugins/recaptcha.php @@ -13,7 +13,7 @@ namespace phpbb\captcha\plugins; -class recaptcha extends \phpbb\captcha\plugins\captcha_abstract +class recaptcha extends captcha_abstract { var $recaptcha_server = 'http://www.google.com/recaptcha/api'; var $recaptcha_server_secure = 'https://www.google.com/recaptcha/api'; // class constants :(