Augmentation vers version 3.3.0

This commit is contained in:
Gauvain Boiché
2020-03-31 15:31:03 +02:00
parent d926806907
commit a1864c0414
2618 changed files with 406015 additions and 31377 deletions

View File

@@ -0,0 +1,173 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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\console\command\cron;
use phpbb\exception\runtime_exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
class run extends \phpbb\console\command\command
{
/** @var \phpbb\cron\manager */
protected $cron_manager;
/** @var \phpbb\lock\db */
protected $lock_db;
/**
* Construct method
*
* @param \phpbb\user $user The user object (used to get language information)
* @param \phpbb\cron\manager $cron_manager The cron manager containing
* the cron tasks to be executed.
* @param \phpbb\lock\db $lock_db The lock for accessing database.
*/
public function __construct(\phpbb\user $user, \phpbb\cron\manager $cron_manager, \phpbb\lock\db $lock_db)
{
$this->cron_manager = $cron_manager;
$this->lock_db = $lock_db;
parent::__construct($user);
}
/**
* Sets the command name and description
*
* @return null
*/
protected function configure()
{
$this
->setName('cron:run')
->setDescription($this->user->lang('CLI_DESCRIPTION_CRON_RUN'))
->setHelp($this->user->lang('CLI_HELP_CRON_RUN'))
->addArgument('name', InputArgument::OPTIONAL, $this->user->lang('CLI_DESCRIPTION_CRON_RUN_ARGUMENT_1'))
;
}
/**
* Executes the command cron:run.
*
* Tries to acquire the cron lock, then if no argument has been given runs all ready cron tasks.
* If the cron lock can not be obtained, an error message is printed
* and the exit status is set to 1.
* If the verbose option is specified, each start of a task is printed.
* Otherwise there is no output.
* If an argument is given to the command, only the task whose name matches the
* argument will be started. If verbose option is specified,
* an info message containing the name of the task is printed.
* If no task matches the argument given, an error message is printed
* and the exit status is set to 2.
*
* @param InputInterface $input The input stream used to get the argument and verboe option.
* @param OutputInterface $output The output stream, used for printing verbose-mode and error information.
*
* @return int 0 if all is ok, 1 if a lock error occurred and 2 if no task matching the argument was found.
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($this->lock_db->acquire())
{
$task_name = $input->getArgument('name');
if ($task_name)
{
$exit_status = $this->run_one($input, $output, $task_name);
}
else
{
$exit_status = $this->run_all($input, $output);
}
$this->lock_db->release();
return $exit_status;
}
else
{
throw new runtime_exception('CRON_LOCK_ERROR', array(), null, 1);
}
}
/**
* Executes all ready cron tasks.
*
* If verbose mode is set, an info message will be printed if there is no task to
* be run, or else for each starting task.
*
* @see execute
* @param InputInterface $input The input stream used to get the argument and verbose option.
* @param OutputInterface $output The output stream, used for printing verbose-mode and error information.
* @return int 0
*/
protected function run_all(InputInterface $input, OutputInterface $output)
{
$run_tasks = $this->cron_manager->find_all_ready_tasks();
if ($run_tasks)
{
foreach ($run_tasks as $task)
{
if ($input->getOption('verbose'))
{
$output->writeln('<info>' . $this->user->lang('RUNNING_TASK', $task->get_name()) . '</info>');
}
$task->run();
}
}
else
{
if ($input->getOption('verbose'))
{
$output->writeln('<info>' . $this->user->lang('CRON_NO_TASK') . '</info>');
}
}
return 0;
}
/**
* Executes a given cron task, if it is ready.
*
* If there is a task whose name matches $task_name, it is run and 0 is returned.
* and if verbose mode is set, print an info message with the name of the task.
* If there is no task matching $task_name, the function prints an error message
* and returns with status 2.
*
* @see execute
*
* @param InputInterface $input The input stream used to get the argument and verbose option.
* @param OutputInterface $output The output stream, used for printing verbose-mode and error information.
* @param string $task_name The name of the task that should be run.
*
* @return int 0 if all is well, 2 if no task matches $task_name.
*/
protected function run_one(InputInterface $input, OutputInterface $output, $task_name)
{
$task = $this->cron_manager->find_task($task_name);
if ($task)
{
if ($input->getOption('verbose'))
{
$output->writeln('<info>' . $this->user->lang('RUNNING_TASK', $task_name) . '</info>');
}
$task->run();
return 0;
}
else
{
throw new runtime_exception('CRON_NO_SUCH_TASK', array( $task_name), null, 2);
}
}
}

View File

@@ -0,0 +1,78 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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\console\command\extension;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class enable extends command
{
protected function configure()
{
$this
->setName('extension:enable')
->setDescription($this->user->lang('CLI_DESCRIPTION_ENABLE_EXTENSION'))
->addArgument(
'extension-name',
InputArgument::REQUIRED,
$this->user->lang('CLI_EXTENSION_NAME')
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$name = $input->getArgument('extension-name');
if (!$this->manager->is_available($name))
{
$io->error($this->user->lang('CLI_EXTENSION_NOT_EXIST', $name));
return 1;
}
$extension = $this->manager->get_extension($name);
if (($enableable = $extension->is_enableable()) !== true)
{
$message = !empty($enableable) ? $enableable : $this->user->lang('CLI_EXTENSION_NOT_ENABLEABLE', $name);
$message = is_array($message) ? implode(PHP_EOL, $message) : $message;
$io->error($message);
return 1;
}
if ($this->manager->is_enabled($name))
{
$io->error($this->user->lang('CLI_EXTENSION_ENABLED', $name));
return 1;
}
$this->manager->enable($name);
$this->manager->load_extensions();
if ($this->manager->is_enabled($name))
{
$this->log->add('admin', ANONYMOUS, '', 'LOG_EXT_ENABLE', time(), array($name));
$io->success($this->user->lang('CLI_EXTENSION_ENABLE_SUCCESS', $name));
return 0;
}
else
{
$io->error($this->user->lang('CLI_EXTENSION_ENABLE_FAILURE', $name));
return 1;
}
}
}

View File

@@ -0,0 +1,332 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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\console\command\update;
use phpbb\config\config;
use phpbb\exception\exception_interface;
use phpbb\language\language;
use phpbb\user;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;
class check extends \phpbb\console\command\command
{
/** @var \phpbb\config\config */
protected $config;
/** @var \Symfony\Component\DependencyInjection\ContainerBuilder */
protected $phpbb_container;
/**
* @var language
*/
private $language;
/**
* Construct method
*/
public function __construct(user $user, config $config, ContainerInterface $phpbb_container, language $language)
{
$this->config = $config;
$this->phpbb_container = $phpbb_container;
$this->language = $language;
$this->language->add_lang(array('acp/common', 'acp/extensions'));
parent::__construct($user);
}
/**
* Configures the service.
*
* Sets the name and description of the command.
*
* @return null
*/
protected function configure()
{
$this
->setName('update:check')
->setDescription($this->language->lang('CLI_DESCRIPTION_UPDATE_CHECK'))
->addArgument('ext-name', InputArgument::OPTIONAL, $this->language->lang('CLI_DESCRIPTION_UPDATE_CHECK_ARGUMENT_1'))
->addOption('stability', null, InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_UPDATE_CHECK_OPTION_STABILITY'))
->addOption('cache', 'c', InputOption::VALUE_NONE, $this->language->lang('CLI_DESCRIPTION_UPDATE_CHECK_OPTION_CACHE'))
;
}
/**
* Executes the command.
*
* Checks if an update is available.
* If at least one is available, a message is printed and if verbose mode is set the list of possible updates is printed.
* If their is none, nothing is printed unless verbose mode is set.
*
* @param InputInterface $input Input stream, used to get the options.
* @param OutputInterface $output Output stream, used to print messages.
* @return int 0 if the board is up to date, 1 if it is not and 2 if an error occurred.
* @throws \RuntimeException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$recheck = true;
if ($input->getOption('cache'))
{
$recheck = false;
}
$stability = null;
if ($input->getOption('stability'))
{
$stability = $input->getOption('stability');
if (!($stability == 'stable') && !($stability == 'unstable'))
{
$io->error($this->language->lang('CLI_ERROR_INVALID_STABILITY', $stability));
return 3;
}
}
$ext_name = $input->getArgument('ext-name');
if ($ext_name != null)
{
if ($ext_name == 'all')
{
return $this->check_all_ext($io, $stability, $recheck);
}
else
{
return $this->check_ext($input, $io, $stability, $recheck, $ext_name);
}
}
else
{
return $this->check_core($input, $io, $stability, $recheck);
}
}
/**
* Check if a given extension is up to date
*
* @param InputInterface $input Input stream, used to get the options.
* @param SymfonyStyle $io IO handler, for formatted and unified IO
* @param string $stability Force a given stability
* @param bool $recheck Disallow the use of the cache
* @param string $ext_name The extension name
* @return int
*/
protected function check_ext(InputInterface $input, SymfonyStyle $io, $stability, $recheck, $ext_name)
{
try
{
$ext_manager = $this->phpbb_container->get('ext.manager');
$md_manager = $ext_manager->create_extension_metadata_manager($ext_name);
$updates_available = $ext_manager->version_check($md_manager, $recheck, false, $stability);
$metadata = $md_manager->get_metadata('all');
if ($input->getOption('verbose'))
{
$io->title($md_manager->get_metadata('display-name'));
$io->note($this->language->lang('CURRENT_VERSION') . $this->language->lang('COLON') . ' ' . $metadata['version']);
}
if (!empty($updates_available))
{
if ($input->getOption('verbose'))
{
$io->caution($this->language->lang('NOT_UP_TO_DATE', $metadata['name']));
$this->display_versions($io, $updates_available);
}
return 1;
}
else
{
if ($input->getOption('verbose'))
{
$io->success($this->language->lang('UPDATE_NOT_NEEDED'));
}
return 0;
}
}
catch (\RuntimeException $e)
{
$io->error($this->language->lang('EXTENSION_NOT_INSTALLED', $ext_name));
return 1;
}
}
/**
* Check if the core is up to date
*
* @param InputInterface $input Input stream, used to get the options.
* @param SymfonyStyle $io IO handler, for formatted and unified IO
* @param string $stability Force a given stability
* @param bool $recheck Disallow the use of the cache
* @return int
*/
protected function check_core(InputInterface $input, SymfonyStyle $io, $stability, $recheck)
{
$version_helper = $this->phpbb_container->get('version_helper');
$version_helper->force_stability($stability);
$updates_available = $version_helper->get_suggested_updates($recheck);
if ($input->getOption('verbose'))
{
$io->title('phpBB core');
$io->note( $this->language->lang('CURRENT_VERSION') . $this->language->lang('COLON') . ' ' . $this->config['version']);
}
if (!empty($updates_available))
{
$io->caution($this->language->lang('UPDATE_NEEDED'));
if ($input->getOption('verbose'))
{
$this->display_versions($io, $updates_available);
}
return 1;
}
else
{
if ($input->getOption('verbose'))
{
$io->success($this->language->lang('UPDATE_NOT_NEEDED'));
}
return 0;
}
}
/**
* Check if all the available extensions are up to date
*
* @param SymfonyStyle $io IO handler, for formatted and unified IO
* @param string $stability Stability specifier string
* @param bool $recheck Disallow the use of the cache
* @return int
*/
protected function check_all_ext(SymfonyStyle $io, $stability, $recheck)
{
/** @var \phpbb\extension\manager $ext_manager */
$ext_manager = $this->phpbb_container->get('ext.manager');
$rows = [];
foreach ($ext_manager->all_available() as $ext_name => $ext_path)
{
$row = [];
$row[] = sprintf("<info>%s</info>", $ext_name);
$md_manager = $ext_manager->create_extension_metadata_manager($ext_name);
try
{
$metadata = $md_manager->get_metadata('all');
if (isset($metadata['extra']['version-check']))
{
try {
$updates_available = $ext_manager->version_check($md_manager, $recheck, false, $stability);
if (!empty($updates_available))
{
$versions = array_map(function($entry)
{
return $entry['current'];
}, $updates_available);
$row[] = sprintf("<comment>%s</comment>", $metadata['version']);
$row[] = implode(', ', $versions);
}
else
{
$row[] = sprintf("<info>%s</info>", $metadata['version']);
$row[] = '';
}
} catch (\RuntimeException $e) {
$row[] = $metadata['version'];
$row[] = '';
}
}
else
{
$row[] = $metadata['version'];
$row[] = '';
}
}
catch (exception_interface $e)
{
$exception_message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters()));
$row[] = '<error>' . $exception_message . '</error>';
}
catch (\RuntimeException $e)
{
$row[] = '<error>' . $e->getMessage() . '</error>';
}
$rows[] = $row;
}
$io->table([
$this->language->lang('EXTENSION_NAME'),
$this->language->lang('CURRENT_VERSION'),
$this->language->lang('LATEST_VERSION'),
], $rows);
return 0;
}
/**
* Display the details of the available updates
*
* @param SymfonyStyle $io IO handler, for formatted and unified IO
* @param array $updates_available The list of the available updates
*/
protected function display_versions(SymfonyStyle $io, $updates_available)
{
$io->section($this->language->lang('UPDATES_AVAILABLE'));
$rows = [];
foreach ($updates_available as $version_data)
{
$row = ['', '', ''];
$row[0] = $version_data['current'];
if (isset($version_data['announcement']))
{
$row[1] = $version_data['announcement'];
}
if (isset($version_data['download']))
{
$row[2] = $version_data['download'];
}
$rows[] = $row;
}
$io->table([
$this->language->lang('VERSION'),
$this->language->lang('ANNOUNCEMENT_TOPIC'),
$this->language->lang('DOWNLOAD_LATEST'),
], $rows);
}
}

View File

@@ -0,0 +1,334 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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\console\command\user;
use phpbb\config\config;
use phpbb\console\command\command;
use phpbb\db\driver\driver_interface;
use phpbb\exception\runtime_exception;
use phpbb\language\language;
use phpbb\passwords\manager;
use phpbb\user;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
class add extends command
{
/** @var array Array of interactively acquired options */
protected $data;
/** @var driver_interface */
protected $db;
/** @var config */
protected $config;
/** @var language */
protected $language;
/** @var manager */
protected $password_manager;
/**
* phpBB root path
*
* @var string
*/
protected $phpbb_root_path;
/**
* PHP extension.
*
* @var string
*/
protected $php_ext;
/**
* Construct method
*
* @param user $user
* @param driver_interface $db
* @param config $config
* @param language $language
* @param manager $password_manager
* @param string $phpbb_root_path
* @param string $php_ext
*/
public function __construct(user $user, driver_interface $db, config $config, language $language, manager $password_manager, $phpbb_root_path, $php_ext)
{
$this->db = $db;
$this->config = $config;
$this->language = $language;
$this->password_manager = $password_manager;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->language->add_lang('ucp');
parent::__construct($user);
}
/**
* Sets the command name and description
*
* @return null
*/
protected function configure()
{
$this
->setName('user:add')
->setDescription($this->language->lang('CLI_DESCRIPTION_USER_ADD'))
->setHelp($this->language->lang('CLI_HELP_USER_ADD'))
->addOption(
'username',
'U',
InputOption::VALUE_REQUIRED,
$this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME')
)
->addOption(
'password',
'P',
InputOption::VALUE_REQUIRED,
$this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD')
)
->addOption(
'email',
'E',
InputOption::VALUE_REQUIRED,
$this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL')
)
->addOption(
'send-email',
null,
InputOption::VALUE_NONE,
$this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_NOTIFY')
)
;
}
/**
* Executes the command user:add
*
* Adds a new user to the database. If options are not provided, it will ask for the username, password and email.
* User is added to the registered user group. Language and timezone default to $config settings.
*
* @param InputInterface $input The input stream used to get the options
* @param OutputInterface $output The output stream, used to print messages
*
* @return int 0 if all is well, 1 if any errors occurred
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
try
{
$this->validate_user_data();
$group_id = $this->get_group_id();
}
catch (runtime_exception $e)
{
$io->error($e->getMessage());
return 1;
}
$user_row = array(
'username' => $this->data['username'],
'user_password' => $this->password_manager->hash($this->data['new_password']),
'user_email' => $this->data['email'],
'group_id' => $group_id,
'user_timezone' => $this->config['board_timezone'],
'user_lang' => $this->config['default_lang'],
'user_type' => USER_NORMAL,
'user_regdate' => time(),
);
$user_id = (int) user_add($user_row);
if (!$user_id)
{
$io->error($this->language->lang('AUTH_NO_PROFILE_CREATED'));
return 1;
}
if ($input->getOption('send-email') && $this->config['email_enable'])
{
$this->send_activation_email($user_id);
}
$io->success($this->language->lang('CLI_USER_ADD_SUCCESS', $this->data['username']));
return 0;
}
/**
* Interacts with the user.
*
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$this->data = array(
'username' => $input->getOption('username'),
'new_password' => $input->getOption('password'),
'email' => $input->getOption('email'),
);
if (!$this->data['username'])
{
$question = new Question($this->ask_user('USERNAME'));
$this->data['username'] = $helper->ask($input, $output, $question);
}
if (!$this->data['new_password'])
{
$question = new Question($this->ask_user('PASSWORD'));
$question->setValidator(function ($value) use ($helper, $input, $output) {
$question = new Question($this->ask_user('CONFIRM_PASSWORD'));
$question->setHidden(true);
if ($helper->ask($input, $output, $question) != $value)
{
throw new runtime_exception($this->language->lang('NEW_PASSWORD_ERROR'));
}
return $value;
});
$question->setHidden(true);
$question->setMaxAttempts(5);
$this->data['new_password'] = $helper->ask($input, $output, $question);
}
if (!$this->data['email'])
{
$question = new Question($this->ask_user('EMAIL_ADDRESS'));
$this->data['email'] = $helper->ask($input, $output, $question);
}
}
/**
* Validate the submitted user data
*
* @throws runtime_exception if any data fails validation
* @return null
*/
protected function validate_user_data()
{
if (!function_exists('validate_data'))
{
require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
}
$error = validate_data($this->data, array(
'username' => array(
array('string', false, $this->config['min_name_chars'], $this->config['max_name_chars']),
array('username', '')),
'new_password' => array(
array('string', false, $this->config['min_pass_chars'], 0),
array('password')),
'email' => array(
array('string', false, 6, 60),
array('user_email')),
));
if ($error)
{
throw new runtime_exception(implode("\n", array_map(array($this->language, 'lang'), $error)));
}
}
/**
* Get the group id
*
* Go and find in the database the group_id corresponding to 'REGISTERED'
*
* @throws runtime_exception if the group id does not exist in database.
* @return null
*/
protected function get_group_id()
{
$sql = 'SELECT group_id
FROM ' . GROUPS_TABLE . "
WHERE group_name = '" . $this->db->sql_escape('REGISTERED') . "'
AND group_type = " . GROUP_SPECIAL;
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if (!$row || !$row['group_id'])
{
throw new runtime_exception($this->language->lang('NO_GROUP'));
}
return $row['group_id'];
}
/**
* Send account activation email
*
* @param int $user_id The new user's id
* @return null
*/
protected function send_activation_email($user_id)
{
switch ($this->config['require_activation'])
{
case USER_ACTIVATION_SELF:
$email_template = 'user_welcome_inactive';
$user_actkey = gen_rand_string(mt_rand(6, 10));
break;
case USER_ACTIVATION_ADMIN:
$email_template = 'admin_welcome_inactive';
$user_actkey = gen_rand_string(mt_rand(6, 10));
break;
default:
$email_template = 'user_welcome';
$user_actkey = '';
break;
}
if (!class_exists('messenger'))
{
require($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext);
}
$messenger = new \messenger(false);
$messenger->template($email_template, $this->user->lang_name);
$messenger->to($this->data['email'], $this->data['username']);
$messenger->anti_abuse_headers($this->config, $this->user);
$messenger->assign_vars(array(
'WELCOME_MSG' => htmlspecialchars_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename'])),
'USERNAME' => htmlspecialchars_decode($this->data['username']),
'PASSWORD' => htmlspecialchars_decode($this->data['new_password']),
'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey")
);
$messenger->send(NOTIFY_EMAIL);
}
/**
* Helper to translate questions to the user
*
* @param string $key The language key
* @return string The language key translated with a colon and space appended
*/
protected function ask_user($key)
{
return $this->language->lang($key) . $this->language->lang('COLON') . ' ';
}
}