diff options
Diffstat (limited to 'phpBB/phpbb/console/command')
31 files changed, 2302 insertions, 123 deletions
| diff --git a/phpBB/phpbb/console/command/cache/purge.php b/phpBB/phpbb/console/command/cache/purge.php index d0c2ef6f72..b7a51b2bb4 100644 --- a/phpBB/phpbb/console/command/cache/purge.php +++ b/phpBB/phpbb/console/command/cache/purge.php @@ -14,6 +14,7 @@ namespace phpbb\console\command\cache;  use Symfony\Component\Console\Input\InputInterface;  use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle;  class purge extends \phpbb\console\command\command  { @@ -39,7 +40,7 @@ class purge extends \phpbb\console\command\command  	* @param \phpbb\cache\driver\driver_interface	$cache	Cache instance  	* @param \phpbb\db\driver\driver_interface		$db		Database connection  	* @param \phpbb\auth\auth						$auth	Auth instance -	* @param \phpbb\log\log							$log	Logger instance +	* @param \phpbb\log\log_interface				$log	Logger instance  	* @param \phpbb\config\config					$config	Config instance  	*/  	public function __construct(\phpbb\user $user, \phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\auth\auth $auth, \phpbb\log\log_interface $log, \phpbb\config\config $config) @@ -71,7 +72,7 @@ class purge extends \phpbb\console\command\command  	* @param InputInterface  $input  An InputInterface instance  	* @param OutputInterface $output An OutputInterface instance  	* -	* @return null +	* @return void  	*/  	protected function execute(InputInterface $input, OutputInterface $output)  	{ @@ -84,6 +85,7 @@ class purge extends \phpbb\console\command\command  		$this->log->add('admin', ANONYMOUS, '', 'LOG_PURGE_CACHE', time(), array()); -		$output->writeln($this->user->lang('PURGE_CACHE_SUCCESS')); +		$io = new SymfonyStyle($input, $output); +		$io->success($this->user->lang('PURGE_CACHE_SUCCESS'));  	}  } diff --git a/phpBB/phpbb/console/command/command.php b/phpBB/phpbb/console/command/command.php index 638c989da2..0124c00d22 100644 --- a/phpBB/phpbb/console/command/command.php +++ b/phpBB/phpbb/console/command/command.php @@ -13,6 +13,10 @@  namespace phpbb\console\command; +use Symfony\Component\Console\Helper\ProgressBar; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; +  abstract class command extends \Symfony\Component\Console\Command\Command  {  	/** @var \phpbb\user */ @@ -28,4 +32,45 @@ abstract class command extends \Symfony\Component\Console\Command\Command  		$this->user = $user;  		parent::__construct();  	} + +	/** +	 * Create a styled progress bar +	 * +	 * @param int             $max     Max value for the progress bar +	 * @param SymfonyStyle    $io      Symfony style output decorator +	 * @param OutputInterface $output  The output stream, used to print messages +	 * @param bool            $message Should we display message output under the progress bar? +	 * @return ProgressBar +	 */ +	public function create_progress_bar($max, SymfonyStyle $io, OutputInterface $output, $message = false) +	{ +		$progress = $io->createProgressBar($max); +		if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERBOSE) +		{ +			$progress->setFormat('<info>[%percent:3s%%]</info> %message%'); +			$progress->setOverwrite(false); +		} +		else if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) +		{ +			$progress->setFormat('<info>[%current:s%/%max:s%]</info><comment>[%elapsed%/%estimated%][%memory%]</comment> %message%'); +			$progress->setOverwrite(false); +		} +		else +		{ +			$io->newLine(2); +			$progress->setFormat( +				"    %current:s%/%max:s% %bar%  %percent:3s%%\n" . +				"        " . ($message ? '%message%' : '                ') . " %elapsed:6s%/%estimated:-6s% %memory:6s%\n"); +			$progress->setBarWidth(60); +		} + +		if (!defined('PHP_WINDOWS_VERSION_BUILD')) +		{ +			$progress->setEmptyBarCharacter('░'); // light shade character \u2591 +			$progress->setProgressCharacter(''); +			$progress->setBarCharacter('▓'); // dark shade character \u2593 +		} + +		return $progress; +	}  } diff --git a/phpBB/phpbb/console/command/config/command.php b/phpBB/phpbb/console/command/config/command.php index f0ad5d4d19..19f67d3b6c 100644 --- a/phpBB/phpbb/console/command/config/command.php +++ b/phpBB/phpbb/console/command/config/command.php @@ -17,7 +17,7 @@ abstract class command extends \phpbb\console\command\command  	/** @var \phpbb\config\config */  	protected $config; -	function __construct(\phpbb\user $user, \phpbb\config\config $config) +	public function __construct(\phpbb\user $user, \phpbb\config\config $config)  	{  		$this->config = $config; diff --git a/phpBB/phpbb/console/command/config/delete.php b/phpBB/phpbb/console/command/config/delete.php index efd276d7e3..2da0801337 100644 --- a/phpBB/phpbb/console/command/config/delete.php +++ b/phpBB/phpbb/console/command/config/delete.php @@ -15,6 +15,7 @@ namespace phpbb\console\command\config;  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 delete extends command  { @@ -42,22 +43,24 @@ class delete extends command  	* @param InputInterface  $input  An InputInterface instance  	* @param OutputInterface $output An OutputInterface instance  	* -	* @return null +	* @return void  	* @see \phpbb\config\config::delete()  	*/  	protected function execute(InputInterface $input, OutputInterface $output)  	{ +		$io = new SymfonyStyle($input, $output); +  		$key = $input->getArgument('key');  		if (isset($this->config[$key]))  		{  			$this->config->delete($key); -			$output->writeln('<info>' . $this->user->lang('CLI_CONFIG_DELETE_SUCCESS', $key) . '</info>'); +			$io->success($this->user->lang('CLI_CONFIG_DELETE_SUCCESS', $key));  		}  		else  		{ -			$output->writeln('<error>' . $this->user->lang('CLI_CONFIG_NOT_EXISTS', $key) . '</error>'); +			$io->error($this->user->lang('CLI_CONFIG_NOT_EXISTS', $key));  		}  	}  } diff --git a/phpBB/phpbb/console/command/config/get.php b/phpBB/phpbb/console/command/config/get.php index 9c03b49a3d..f065787110 100644 --- a/phpBB/phpbb/console/command/config/get.php +++ b/phpBB/phpbb/console/command/config/get.php @@ -16,6 +16,7 @@ use Symfony\Component\Console\Input\InputArgument;  use Symfony\Component\Console\Input\InputInterface;  use Symfony\Component\Console\Input\InputOption;  use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle;  class get extends command  { @@ -49,11 +50,13 @@ class get extends command  	* @param InputInterface  $input  An InputInterface instance  	* @param OutputInterface $output An OutputInterface instance  	* -	* @return null +	* @return void  	* @see \phpbb\config\config::offsetGet()  	*/  	protected function execute(InputInterface $input, OutputInterface $output)  	{ +		$io = new SymfonyStyle($input, $output); +  		$key = $input->getArgument('key');  		if (isset($this->config[$key]) && $input->getOption('no-newline')) @@ -66,7 +69,7 @@ class get extends command  		}  		else  		{ -			$output->writeln('<error>' . $this->user->lang('CLI_CONFIG_NOT_EXISTS', $key) . '</error>'); +			$io->error($this->user->lang('CLI_CONFIG_NOT_EXISTS', $key));  		}  	}  } diff --git a/phpBB/phpbb/console/command/config/increment.php b/phpBB/phpbb/console/command/config/increment.php index b4d7438b66..647380a0bf 100644 --- a/phpBB/phpbb/console/command/config/increment.php +++ b/phpBB/phpbb/console/command/config/increment.php @@ -16,6 +16,7 @@ use Symfony\Component\Console\Input\InputArgument;  use Symfony\Component\Console\Input\InputInterface;  use Symfony\Component\Console\Input\InputOption;  use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle;  class increment extends command  { @@ -54,17 +55,19 @@ class increment extends command  	* @param InputInterface  $input  An InputInterface instance  	* @param OutputInterface $output An OutputInterface instance  	* -	* @return null +	* @return void  	* @see \phpbb\config\config::increment()  	*/  	protected function execute(InputInterface $input, OutputInterface $output)  	{ +		$io = new SymfonyStyle($input, $output); +  		$key = $input->getArgument('key');  		$increment = $input->getArgument('increment');  		$use_cache = !$input->getOption('dynamic');  		$this->config->increment($key, $increment, $use_cache); -		$output->writeln('<info>' . $this->user->lang('CLI_CONFIG_INCREMENT_SUCCESS', $key) . '</info>'); +		$io->success($this->user->lang('CLI_CONFIG_INCREMENT_SUCCESS', $key));  	}  } diff --git a/phpBB/phpbb/console/command/config/set.php b/phpBB/phpbb/console/command/config/set.php index 695de31013..e9f7f8f91e 100644 --- a/phpBB/phpbb/console/command/config/set.php +++ b/phpBB/phpbb/console/command/config/set.php @@ -16,6 +16,7 @@ use Symfony\Component\Console\Input\InputArgument;  use Symfony\Component\Console\Input\InputInterface;  use Symfony\Component\Console\Input\InputOption;  use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle;  class set extends command  { @@ -54,17 +55,19 @@ class set extends command  	* @param InputInterface  $input  An InputInterface instance  	* @param OutputInterface $output An OutputInterface instance  	* -	* @return null +	* @return void  	* @see \phpbb\config\config::set()  	*/  	protected function execute(InputInterface $input, OutputInterface $output)  	{ +		$io = new SymfonyStyle($input, $output); +  		$key = $input->getArgument('key');  		$value = $input->getArgument('value');  		$use_cache = !$input->getOption('dynamic');  		$this->config->set($key, $value, $use_cache); -		$output->writeln('<info>' . $this->user->lang('CLI_CONFIG_SET_SUCCESS', $key) . '</info>'); +		$io->success($this->user->lang('CLI_CONFIG_SET_SUCCESS', $key));  	}  } diff --git a/phpBB/phpbb/console/command/config/set_atomic.php b/phpBB/phpbb/console/command/config/set_atomic.php index e8c69a0885..475d8a9271 100644 --- a/phpBB/phpbb/console/command/config/set_atomic.php +++ b/phpBB/phpbb/console/command/config/set_atomic.php @@ -16,6 +16,7 @@ use Symfony\Component\Console\Input\InputArgument;  use Symfony\Component\Console\Input\InputInterface;  use Symfony\Component\Console\Input\InputOption;  use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle;  class set_atomic extends command  { @@ -65,6 +66,8 @@ class set_atomic extends command  	*/  	protected function execute(InputInterface $input, OutputInterface $output)  	{ +		$io = new SymfonyStyle($input, $output); +  		$key = $input->getArgument('key');  		$old_value = $input->getArgument('old');  		$new_value = $input->getArgument('new'); @@ -72,12 +75,12 @@ class set_atomic extends command  		if ($this->config->set_atomic($key, $old_value, $new_value, $use_cache))  		{ -			$output->writeln('<info>' . $this->user->lang('CLI_CONFIG_SET_SUCCESS', $key) . '</info>'); +			$io->success($this->user->lang('CLI_CONFIG_SET_SUCCESS', $key));  			return 0;  		}  		else  		{ -			$output->writeln('<error>' . $this->user->lang('CLI_CONFIG_SET_FAILURE', $key) . '</error>'); +			$io->error($this->user->lang('CLI_CONFIG_SET_FAILURE', $key));  			return 1;  		}  	} diff --git a/phpBB/phpbb/console/command/cron/cron_list.php b/phpBB/phpbb/console/command/cron/cron_list.php index c515fd9e80..ea61e45235 100644 --- a/phpBB/phpbb/console/command/cron/cron_list.php +++ b/phpBB/phpbb/console/command/cron/cron_list.php @@ -14,6 +14,7 @@ namespace phpbb\console\command\cron;  use Symfony\Component\Console\Input\InputInterface;  use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle;  class cron_list extends \phpbb\console\command\command  { @@ -51,61 +52,43 @@ class cron_list extends \phpbb\console\command\command  	* @param InputInterface  $input  An InputInterface instance  	* @param OutputInterface $output An OutputInterface instance  	* -	* @return null +	* @return void  	*/  	protected function execute(InputInterface $input, OutputInterface $output)  	{ +		$io = new SymfonyStyle($input, $output); +  		$tasks = $this->cron_manager->get_tasks();  		if (empty($tasks))  		{ -			$output->writeln($this->user->lang('CRON_NO_TASKS')); +			$io->error($this->user->lang('CRON_NO_TASKS'));  			return;  		} -		$ready_tasks = array(); -		$not_ready_tasks = array(); +		$ready_tasks = $not_ready_tasks = array();  		foreach ($tasks as $task)  		{  			if ($task->is_ready())  			{ -				$ready_tasks[] = $task; +				$ready_tasks[] = $task->get_name();  			}  			else  			{ -				$not_ready_tasks[] = $task; +				$not_ready_tasks[] = $task->get_name();  			}  		}  		if (!empty($ready_tasks))  		{ -			$output->writeln('<info>' . $this->user->lang('TASKS_READY') . '</info>'); -			$this->print_tasks_names($ready_tasks, $output); -		} - -		if (!empty($ready_tasks) && !empty($not_ready_tasks)) -		{ -			$output->writeln(''); +			$io->title($this->user->lang('TASKS_READY')); +			$io->listing($ready_tasks);  		}  		if (!empty($not_ready_tasks))  		{ -			$output->writeln('<info>' . $this->user->lang('TASKS_NOT_READY') . '</info>'); -			$this->print_tasks_names($not_ready_tasks, $output); -		} -	} - -	/** -	* Print a list of cron jobs -	* -	* @param array				$tasks A list of task to display -	* @param OutputInterface	$output An OutputInterface instance -	*/ -	protected function print_tasks_names(array $tasks, OutputInterface $output) -	{ -		foreach ($tasks as $task) -		{ -			$output->writeln($task->get_name()); +			$io->title($this->user->lang('TASKS_NOT_READY')); +			$io->listing($not_ready_tasks);  		}  	}  } diff --git a/phpBB/phpbb/console/command/cron/run.php b/phpBB/phpbb/console/command/cron/run.php index a9648fcd41..dea6493007 100644 --- a/phpBB/phpbb/console/command/cron/run.php +++ b/phpBB/phpbb/console/command/cron/run.php @@ -13,6 +13,7 @@  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; @@ -93,8 +94,7 @@ class run extends \phpbb\console\command\command  		}  		else  		{ -			$output->writeln('<error>' . $this->user->lang('CRON_LOCK_ERROR') . '</error>'); -			return 1; +			throw new runtime_exception('CRON_LOCK_ERROR', array(), null, 1);  		}  	} @@ -165,8 +165,7 @@ class run extends \phpbb\console\command\command  		}  		else  		{ -			$output->writeln('<error>' . $this->user->lang('CRON_NO_SUCH_TASK', $task_name) . '</error>'); -			return 2; +			throw new runtime_exception('CRON_NO_SUCH_TASK', array( $task_name), null, 2);  		}  	}  } diff --git a/phpBB/phpbb/console/command/db/console_migrator_output_handler.php b/phpBB/phpbb/console/command/db/console_migrator_output_handler.php index b9741a3838..568b2646d4 100644 --- a/phpBB/phpbb/console/command/db/console_migrator_output_handler.php +++ b/phpBB/phpbb/console/command/db/console_migrator_output_handler.php @@ -13,8 +13,8 @@  namespace phpbb\console\command\db; +use phpbb\db\output_handler\migrator_output_handler_interface;  use phpbb\user; -use phpbb\db\migrator_output_handler_interface;  use Symfony\Component\Console\Output\OutputInterface;  class console_migrator_output_handler implements migrator_output_handler_interface diff --git a/phpBB/phpbb/console/command/db/list_command.php b/phpBB/phpbb/console/command/db/list_command.php new file mode 100644 index 0000000000..77f26dd786 --- /dev/null +++ b/phpBB/phpbb/console/command/db/list_command.php @@ -0,0 +1,81 @@ +<?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\db; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class list_command extends \phpbb\console\command\db\migration_command +{ +	protected function configure() +	{ +		$this +			->setName('db:list') +			->setDescription($this->user->lang('CLI_DESCRIPTION_DB_LIST')) +			->addOption( +				'available', +				'u', +				InputOption::VALUE_NONE, +				$this->user->lang('CLI_MIGRATIONS_ONLY_AVAILABLE') +			) +		; +	} + +	protected function execute(InputInterface $input, OutputInterface $output) +	{ +		$io = new SymfonyStyle($input, $output); + +		$show_installed = !$input->getOption('available'); +		$installed = $available = array(); + +		foreach ($this->load_migrations() as $name) +		{ +			if ($this->migrator->migration_state($name) !== false) +			{ +				$installed[] = $name; +			} +			else +			{ +				$available[] = $name; +			} +		} + +		if ($show_installed) +		{ +			$io->section($this->user->lang('CLI_MIGRATIONS_INSTALLED')); + +			if (!empty($installed)) +			{ +				$io->listing($installed); +			} +			else +			{ +				$io->text($this->user->lang('CLI_MIGRATIONS_EMPTY')); +				$io->newLine(); +			} +		} + +		$io->section($this->user->lang('CLI_MIGRATIONS_AVAILABLE')); +		if (!empty($available)) +		{ +			$io->listing($available); +		} +		else +		{ +			$io->text($this->user->lang('CLI_MIGRATIONS_EMPTY')); +			$io->newLine(); +		} +	} +} diff --git a/phpBB/phpbb/console/command/db/migrate.php b/phpBB/phpbb/console/command/db/migrate.php index 87c2a057d1..4270e2d703 100644 --- a/phpBB/phpbb/console/command/db/migrate.php +++ b/phpBB/phpbb/console/command/db/migrate.php @@ -12,52 +12,48 @@  */  namespace phpbb\console\command\db; +use phpbb\db\output_handler\log_wrapper_migrator_output_handler;  use Symfony\Component\Console\Input\InputInterface;  use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; -class migrate extends \phpbb\console\command\command +class migrate extends \phpbb\console\command\db\migration_command  { -	/** @var \phpbb\db\migrator */ -	protected $migrator; - -	/** @var \phpbb\extension\manager */ -	protected $extension_manager; - -	/** @var \phpbb\config\config */ -	protected $config; - -	/** @var \phpbb\cache\service */ -	protected $cache; -  	/** @var \phpbb\log\log */  	protected $log;  	/** @var string phpBB root path */  	protected $phpbb_root_path; -	function __construct(\phpbb\user $user, \phpbb\db\migrator $migrator, \phpbb\extension\manager $extension_manager, \phpbb\config\config $config, \phpbb\cache\service $cache, \phpbb\log\log $log, $phpbb_root_path) +	/** @var  \phpbb\filesystem\filesystem_interface */ +	protected $filesystem; + +	/** @var \phpbb\language\language */ +	protected $language; + +	public function __construct(\phpbb\user $user, \phpbb\language\language $language, \phpbb\db\migrator $migrator, \phpbb\extension\manager $extension_manager, \phpbb\config\config $config, \phpbb\cache\service $cache, \phpbb\log\log $log, \phpbb\filesystem\filesystem_interface $filesystem, $phpbb_root_path)  	{ -		$this->migrator = $migrator; -		$this->extension_manager = $extension_manager; -		$this->config = $config; -		$this->cache = $cache; +		$this->language = $language;  		$this->log = $log; +		$this->filesystem = $filesystem;  		$this->phpbb_root_path = $phpbb_root_path; -		parent::__construct($user); -		$this->user->add_lang(array('common', 'install', 'migrator')); +		parent::__construct($user, $migrator, $extension_manager, $config, $cache); +		$this->language->add_lang(array('common', 'install', 'migrator'));  	}  	protected function configure()  	{  		$this  			->setName('db:migrate') -			->setDescription($this->user->lang('CLI_DESCRIPTION_DB_MIGRATE')) +			->setDescription($this->language->lang('CLI_DESCRIPTION_DB_MIGRATE'))  		;  	}  	protected function execute(InputInterface $input, OutputInterface $output)  	{ -		$this->migrator->set_output_handler(new \phpbb\db\log_wrapper_migrator_output_handler($this->user, new console_migrator_output_handler($this->user, $output), $this->phpbb_root_path . 'store/migrations_' . time() . '.log')); +		$io = new SymfonyStyle($input, $output); + +		$this->migrator->set_output_handler(new log_wrapper_migrator_output_handler($this->language, new console_migrator_output_handler($this->user, $output), $this->phpbb_root_path . 'store/migrations_' . time() . '.log', $this->filesystem));  		$this->migrator->create_migrations_table(); @@ -73,7 +69,7 @@ class migrate extends \phpbb\console\command\command  			}  			catch (\phpbb\db\migration\exception $e)  			{ -				$output->writeln('<error>' . $e->getLocalisedMessage($this->user) . '</error>'); +				$io->error($e->getLocalisedMessage($this->user));  				$this->finalise_update();  				return 1;  			} @@ -85,23 +81,6 @@ class migrate extends \phpbb\console\command\command  		}  		$this->finalise_update(); -		$output->writeln($this->user->lang['DATABASE_UPDATE_COMPLETE']); -	} - -	protected function load_migrations() -	{ -		$migrations = $this->extension_manager -			->get_finder() -			->core_path('phpbb/db/migration/data/') -			->extension_directory('/migrations') -			->get_classes(); - -		$this->migrator->set_migrations($migrations); -	} - -	protected function finalise_update() -	{ -		$this->cache->purge(); -		$this->config->increment('assets_version', 1); +		$io->success($this->language->lang('INLINE_UPDATE_SUCCESSFUL'));  	}  } diff --git a/phpBB/phpbb/console/command/db/migration_command.php b/phpBB/phpbb/console/command/db/migration_command.php new file mode 100644 index 0000000000..851f404fab --- /dev/null +++ b/phpBB/phpbb/console/command/db/migration_command.php @@ -0,0 +1,56 @@ +<?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\db; + +abstract class migration_command extends \phpbb\console\command\command +{ +	/** @var \phpbb\db\migrator */ +	protected $migrator; + +	/** @var \phpbb\extension\manager */ +	protected $extension_manager; + +	/** @var \phpbb\config\config */ +	protected $config; + +	/** @var \phpbb\cache\service */ +	protected $cache; + +	public function __construct(\phpbb\user $user, \phpbb\db\migrator $migrator, \phpbb\extension\manager $extension_manager, \phpbb\config\config $config, \phpbb\cache\service $cache) +	{ +		$this->migrator = $migrator; +		$this->extension_manager = $extension_manager; +		$this->config = $config; +		$this->cache = $cache; +		parent::__construct($user); +	} + +	protected function load_migrations() +	{ +		$migrations = $this->extension_manager +			->get_finder() +			->core_path('phpbb/db/migration/data/') +			->extension_directory('/migrations') +			->get_classes(); + +		$this->migrator->set_migrations($migrations); + +		return $this->migrator->get_migrations(); +	} + +	protected function finalise_update() +	{ +		$this->cache->purge(); +		$this->config->increment('assets_version', 1); +	} +} diff --git a/phpBB/phpbb/console/command/db/revert.php b/phpBB/phpbb/console/command/db/revert.php new file mode 100644 index 0000000000..3c79d8c554 --- /dev/null +++ b/phpBB/phpbb/console/command/db/revert.php @@ -0,0 +1,74 @@ +<?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\db; + +use phpbb\db\output_handler\log_wrapper_migrator_output_handler; +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 revert extends \phpbb\console\command\db\migrate +{ +	protected function configure() +	{ +		$this +			->setName('db:revert') +			->setDescription($this->language->lang('CLI_DESCRIPTION_DB_REVERT')) +			->addArgument( +				'name', +				InputArgument::REQUIRED, +				$this->language->lang('CLI_MIGRATION_NAME') +			) +		; +	} + +	protected function execute(InputInterface $input, OutputInterface $output) +	{ +		$io = new SymfonyStyle($input, $output); + +		$name = str_replace('/', '\\', $input->getArgument('name')); + +		$this->migrator->set_output_handler(new log_wrapper_migrator_output_handler($this->language, new console_migrator_output_handler($this->user, $output), $this->phpbb_root_path . 'store/migrations_' . time() . '.log', $this->filesystem)); + +		$this->cache->purge(); + +		if (!in_array($name, $this->load_migrations())) +		{ +			$io->error($this->language->lang('MIGRATION_NOT_VALID', $name)); +			return 1; +		} +		else if ($this->migrator->migration_state($name) === false) +		{ +			$io->error($this->language->lang('MIGRATION_NOT_INSTALLED', $name)); +			return 1; +		} + +		try +		{ +			while ($this->migrator->migration_state($name) !== false) +			{ +				$this->migrator->revert($name); +			} +		} +		catch (\phpbb\db\migration\exception $e) +		{ +			$io->error($e->getLocalisedMessage($this->user)); +			$this->finalise_update(); +			return 1; +		} + +		$this->finalise_update(); +		$io->success($this->language->lang('INLINE_UPDATE_SUCCESSFUL')); +	} +} diff --git a/phpBB/phpbb/console/command/dev/migration_tips.php b/phpBB/phpbb/console/command/dev/migration_tips.php index f9047bdac8..2ca0ddde2f 100644 --- a/phpBB/phpbb/console/command/dev/migration_tips.php +++ b/phpBB/phpbb/console/command/dev/migration_tips.php @@ -20,7 +20,7 @@ class migration_tips extends \phpbb\console\command\command  	/** @var \phpbb\extension\manager */  	protected $extension_manager; -	function __construct(\phpbb\user $user, \phpbb\extension\manager $extension_manager) +	public function __construct(\phpbb\user $user, \phpbb\extension\manager $extension_manager)  	{  		$this->extension_manager = $extension_manager;  		parent::__construct($user); diff --git a/phpBB/phpbb/console/command/extension/disable.php b/phpBB/phpbb/console/command/extension/disable.php index 1eee16cbd9..b2e10fb960 100644 --- a/phpBB/phpbb/console/command/extension/disable.php +++ b/phpBB/phpbb/console/command/extension/disable.php @@ -15,6 +15,7 @@ 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 disable extends command  { @@ -33,19 +34,28 @@ class disable extends command  	protected function execute(InputInterface $input, OutputInterface $output)  	{ +		$io = new SymfonyStyle($input, $output); +  		$name = $input->getArgument('extension-name'); + +		if (!$this->manager->is_enabled($name)) +		{ +			$io->error($this->user->lang('CLI_EXTENSION_DISABLED', $name)); +			return 2; +		} +  		$this->manager->disable($name);  		$this->manager->load_extensions();  		if ($this->manager->is_enabled($name))  		{ -			$output->writeln('<error>' . $this->user->lang('CLI_EXTENSION_DISABLE_FAILURE', $name) . '</error>'); +			$io->error($this->user->lang('CLI_EXTENSION_DISABLE_FAILURE', $name));  			return 1;  		}  		else  		{  			$this->log->add('admin', ANONYMOUS, '', 'LOG_EXT_DISABLE', time(), array($name)); -			$output->writeln('<info>' . $this->user->lang('CLI_EXTENSION_DISABLE_SUCCESS', $name) . '</info>'); +			$io->success($this->user->lang('CLI_EXTENSION_DISABLE_SUCCESS', $name));  			return 0;  		}  	} diff --git a/phpBB/phpbb/console/command/extension/enable.php b/phpBB/phpbb/console/command/extension/enable.php index 59ff11e9b7..a8312d5c15 100644 --- a/phpBB/phpbb/console/command/extension/enable.php +++ b/phpBB/phpbb/console/command/extension/enable.php @@ -15,6 +15,7 @@ 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  { @@ -33,19 +34,28 @@ class enable extends command  	protected function execute(InputInterface $input, OutputInterface $output)  	{ +		$io = new SymfonyStyle($input, $output); +  		$name = $input->getArgument('extension-name'); + +		if ($this->manager->is_enabled($name)) +		{ +			$io->error($this->user->lang('CLI_EXTENSION_ENABLED', $name)); +			return 2; +		} +  		$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)); -			$output->writeln('<info>' . $this->user->lang('CLI_EXTENSION_ENABLE_SUCCESS', $name) . '</info>'); +			$io->success($this->user->lang('CLI_EXTENSION_ENABLE_SUCCESS', $name));  			return 0;  		}  		else  		{ -			$output->writeln('<error>' . $this->user->lang('CLI_EXTENSION_ENABLE_FAILURE', $name) . '</error>'); +			$io->error($this->user->lang('CLI_EXTENSION_ENABLE_FAILURE', $name));  			return 1;  		}  	} diff --git a/phpBB/phpbb/console/command/extension/purge.php b/phpBB/phpbb/console/command/extension/purge.php index 517e9a74c9..25bde503f7 100644 --- a/phpBB/phpbb/console/command/extension/purge.php +++ b/phpBB/phpbb/console/command/extension/purge.php @@ -15,6 +15,7 @@ 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 purge extends command  { @@ -33,19 +34,21 @@ class purge extends command  	protected function execute(InputInterface $input, OutputInterface $output)  	{ +		$io = new SymfonyStyle($input, $output); +  		$name = $input->getArgument('extension-name');  		$this->manager->purge($name);  		$this->manager->load_extensions();  		if ($this->manager->is_enabled($name))  		{ -			$output->writeln('<error>' . $this->user->lang('CLI_EXTENSION_PURGE_FAILURE', $name) . '</error>'); +			$io->error($this->user->lang('CLI_EXTENSION_PURGE_FAILURE', $name));  			return 1;  		}  		else  		{  			$this->log->add('admin', ANONYMOUS, '', 'LOG_EXT_PURGE', time(), array($name)); -			$output->writeln('<info>' . $this->user->lang('CLI_EXTENSION_PURGE_SUCCESS', $name) . '</info>'); +			$io->success($this->user->lang('CLI_EXTENSION_PURGE_SUCCESS', $name));  			return 0;  		}  	} diff --git a/phpBB/phpbb/console/command/extension/show.php b/phpBB/phpbb/console/command/extension/show.php index f9322034d7..7bad0c0a5a 100644 --- a/phpBB/phpbb/console/command/extension/show.php +++ b/phpBB/phpbb/console/command/extension/show.php @@ -14,6 +14,7 @@ namespace phpbb\console\command\extension;  use Symfony\Component\Console\Input\InputInterface;  use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle;  class show extends command  { @@ -27,36 +28,27 @@ class show extends command  	protected function execute(InputInterface $input, OutputInterface $output)  	{ +		$io = new SymfonyStyle($input, $output); +  		$this->manager->load_extensions();  		$all = array_keys($this->manager->all_available());  		if (empty($all))  		{ -			$output->writeln('<comment>' . $this->user->lang('CLI_EXTENSION_NOT_FOUND') . '</comment>'); +			$io->note($this->user->lang('CLI_EXTENSION_NOT_FOUND'));  			return 3;  		}  		$enabled = array_keys($this->manager->all_enabled()); -		$this->print_extension_list($output, $this->user->lang('CLI_EXTENSIONS_ENABLED') . $this->user->lang('COLON'), $enabled); - -		$output->writeln(''); +		$io->section($this->user->lang('CLI_EXTENSIONS_ENABLED')); +		$io->listing($enabled);  		$disabled = array_keys($this->manager->all_disabled()); -		$this->print_extension_list($output, $this->user->lang('CLI_EXTENSIONS_DISABLED') . $this->user->lang('COLON'), $disabled); - -		$output->writeln(''); +		$io->section($this->user->lang('CLI_EXTENSIONS_DISABLED')); +		$io->listing($disabled);  		$purged = array_diff($all, $enabled, $disabled); -		$this->print_extension_list($output, $this->user->lang('CLI_EXTENSIONS_AVAILABLE') . $this->user->lang('COLON'), $purged); -	} - -	protected function print_extension_list(OutputInterface $output, $type, array $extensions) -	{ -		$output->writeln("<info>$type</info>"); - -		foreach ($extensions as $extension) -		{ -			$output->writeln(" - $extension"); -		} +		$io->section($this->user->lang('CLI_EXTENSIONS_AVAILABLE')); +		$io->listing($purged);  	}  } diff --git a/phpBB/phpbb/console/command/fixup/recalculate_email_hash.php b/phpBB/phpbb/console/command/fixup/recalculate_email_hash.php index ec4e1b0ee7..6f7096296d 100644 --- a/phpBB/phpbb/console/command/fixup/recalculate_email_hash.php +++ b/phpBB/phpbb/console/command/fixup/recalculate_email_hash.php @@ -14,13 +14,14 @@ namespace phpbb\console\command\fixup;  use Symfony\Component\Console\Input\InputInterface;  use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle;  class recalculate_email_hash extends \phpbb\console\command\command  {  	/** @var \phpbb\db\driver\driver_interface */  	protected $db; -	function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db) +	public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db)  	{  		$this->db = $db; @@ -37,6 +38,8 @@ class recalculate_email_hash extends \phpbb\console\command\command  	protected function execute(InputInterface $input, OutputInterface $output)  	{ +		$io = new SymfonyStyle($input, $output); +  		$sql = 'SELECT user_id, user_email, user_email_hash  			FROM ' . USERS_TABLE . '  			WHERE user_type <> ' . USER_IGNORE . " @@ -59,17 +62,15 @@ class recalculate_email_hash extends \phpbb\console\command\command  				if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG)  				{ -					$output->writeln(sprintf( -						'user_id %d, email %s => %s', -						$row['user_id'], -						$row['user_email'], -						$user_email_hash -					)); +					$io->table( +						array('user_id', 'user_email', 'user_email_hash'), +						array(array($row['user_id'], $row['user_email'], $user_email_hash)) +					);  				}  			}  		}  		$this->db->sql_freeresult($result); -		$output->writeln('<info>' . $this->user->lang('CLI_FIXUP_RECALCULATE_EMAIL_HASH_SUCCESS') . '</info>'); +		$io->success($this->user->lang('CLI_FIXUP_RECALCULATE_EMAIL_HASH_SUCCESS'));  	}  } diff --git a/phpBB/phpbb/console/command/reparser/list_all.php b/phpBB/phpbb/console/command/reparser/list_all.php new file mode 100644 index 0000000000..a79578abf0 --- /dev/null +++ b/phpBB/phpbb/console/command/reparser/list_all.php @@ -0,0 +1,72 @@ +<?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\reparser; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class list_all extends \phpbb\console\command\command +{ +	/** +	* @var string[] Names of the reparser services +	*/ +	protected $reparser_names; + +	/** +	* Constructor +	* +	* @param \phpbb\user $user +	* @param \phpbb\di\service_collection $reparsers +	*/ +	public function __construct(\phpbb\user $user, \phpbb\di\service_collection $reparsers) +	{ +		parent::__construct($user); +		$this->reparser_names = array(); +		foreach ($reparsers as $reparser) +		{ +			// Store the names without the "text_reparser." prefix +			$this->reparser_names[] = $reparser->get_name(); +		} +	} + +	/** +	* Sets the command name and description +	* +	* @return null +	*/ +	protected function configure() +	{ +		$this +			->setName('reparser:list') +			->setDescription($this->user->lang('CLI_DESCRIPTION_REPARSER_LIST')) +		; +	} + +	/** +	* Executes the command reparser:list +	* +	* @param InputInterface $input +	* @param OutputInterface $output +	* @return integer +	*/ +	protected function execute(InputInterface $input, OutputInterface $output) +	{ +		$io = new SymfonyStyle($input, $output); +		$io->section($this->user->lang('CLI_DESCRIPTION_REPARSER_AVAILABLE')); +		$io->listing($this->reparser_names); + +		return 0; +	} +} diff --git a/phpBB/phpbb/console/command/reparser/reparse.php b/phpBB/phpbb/console/command/reparser/reparse.php new file mode 100644 index 0000000000..f285977ea2 --- /dev/null +++ b/phpBB/phpbb/console/command/reparser/reparse.php @@ -0,0 +1,242 @@ +<?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\reparser; + +use phpbb\exception\runtime_exception; +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; + +class reparse extends \phpbb\console\command\command +{ +	/** +	* @var InputInterface +	*/ +	protected $input; + +	/** +	* @var SymfonyStyle +	*/ +	protected $io; + +	/** +	* @var OutputInterface +	*/ +	protected $output; + +	/** +	 * @var \phpbb\lock\db +	 */ +	protected $reparse_lock; + +	/** +	 * @var \phpbb\textreparser\manager +	 */ +	protected $reparser_manager; + +	/** +	* @var \phpbb\di\service_collection +	*/ +	protected $reparsers; + +	/** +	* @var array The reparser's last $current ID as values +	*/ +	protected $resume_data; + +	/** +	* Constructor +	* +	* @param \phpbb\user $user +	* @param \phpbb\lock\db $reparse_lock +	* @param \phpbb\textreparser\manager $reparser_manager +	* @param \phpbb\di\service_collection $reparsers +	*/ +	public function __construct(\phpbb\user $user, \phpbb\lock\db $reparse_lock, \phpbb\textreparser\manager $reparser_manager, \phpbb\di\service_collection $reparsers) +	{ +		require_once __DIR__ . '/../../../../includes/functions_content.php'; + +		$this->reparse_lock = $reparse_lock; +		$this->reparser_manager = $reparser_manager; +		$this->reparsers = $reparsers; +		parent::__construct($user); +	} + +	/** +	* Sets the command name and description +	* +	* @return null +	*/ +	protected function configure() +	{ +		$this +			->setName('reparser:reparse') +			->setDescription($this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE')) +			->addArgument('reparser-name', InputArgument::OPTIONAL, $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_ARG_1')) +			->addOption( +				'dry-run', +				null, +				InputOption::VALUE_NONE, +				$this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_DRY_RUN') +			) +			->addOption( +				'resume', +				null, +				InputOption::VALUE_NONE, +				$this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RESUME') +			) +			->addOption( +				'range-min', +				null, +				InputOption::VALUE_REQUIRED, +				$this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MIN'), +				1 +			) +			->addOption( +				'range-max', +				null, +				InputOption::VALUE_REQUIRED, +				$this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MAX') +			) +			->addOption( +				'range-size', +				null, +				InputOption::VALUE_REQUIRED, +				$this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_SIZE'), +				100 +			); +		; +	} + +	/** +	* Executes the command reparser:reparse +	* +	* @param InputInterface $input +	* @param OutputInterface $output +	* @return integer +	*/ +	protected function execute(InputInterface $input, OutputInterface $output) +	{ +		$this->input = $input; +		$this->output = $output; +		$this->io = new SymfonyStyle($input, $output); + +		if (!$this->reparse_lock->acquire()) +		{ +			throw new runtime_exception('REPARSE_LOCK_ERROR', array(), null, 1); +		} + +		$name = $input->getArgument('reparser-name'); +		if ($name) +		{ +			$name = $this->reparser_manager->find_reparser($name); +			$this->reparse($name); +		} +		else +		{ +			foreach ($this->reparsers as $name => $service) +			{ +				$this->reparse($name); +			} +		} + +		$this->io->success($this->user->lang('CLI_REPARSER_REPARSE_SUCCESS')); + +		$this->reparse_lock->release(); + +		return 0; +	} + +	/** +	* Get an option value, adjusted for given reparser +	* +	* Will use the last saved value if --resume is set and the option was not specified +	* on the command line +	* +	* @param  string  $option_name   Option name +	* @return integer +	*/ +	protected function get_option($option_name) +	{ +		// Return the option from the resume_data if applicable +		if ($this->input->getOption('resume') && isset($this->resume_data[$option_name]) && !$this->input->hasParameterOption('--' . $option_name)) +		{ +			return $this->resume_data[$option_name]; +		} + +		return $this->input->getOption($option_name); +	} + +	/** +	* Reparse all text handled by given reparser within given range +	* +	* @param string $name Reparser service name +	*/ +	protected function reparse($name) +	{ +		$reparser = $this->reparsers[$name]; +		$this->resume_data = $this->reparser_manager->get_resume_data($name); +		if ($this->input->getOption('dry-run')) +		{ +			$reparser->disable_save(); +		} +		else +		{ +			$reparser->enable_save(); +		} + +		// Start at range-max if specified or at the highest ID otherwise +		$max  = $this->get_option('range-max'); +		$min  = $this->get_option('range-min'); +		$size = $this->get_option('range-size'); + +		// range-max has no default value, it must be computed for each reparser +		if ($max === null) +		{ +			$max = $reparser->get_max_id(); +		} + +		if ($max < $min) +		{ +			return; +		} + +		$this->io->section($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', $reparser->get_name(), $min, $max)); + +		$progress = $this->create_progress_bar($max, $this->io, $this->output, true); +		$progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING_START', $reparser->get_name())); +		$progress->start(); + +		// Start from $max and decrement $current by $size until we reach $min +		$current = $max; +		while ($current >= $min) +		{ +			$start = max($min, $current + 1 - $size); +			$end   = max($min, $current); + +			$progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', $reparser->get_name(), $start, $end)); +			$reparser->reparse_range($start, $end); + +			$current = $start - 1; +			$progress->setProgress($max + 1 - $start); + +			$this->reparser_manager->update_resume_data($name, $min, $current, $size, !$this->input->getOption('dry-run')); +		} +		$progress->finish(); + +		$this->io->newLine(2); +	} +} diff --git a/phpBB/phpbb/console/command/thumbnail/delete.php b/phpBB/phpbb/console/command/thumbnail/delete.php new file mode 100644 index 0000000000..cfa9891fbc --- /dev/null +++ b/phpBB/phpbb/console/command/thumbnail/delete.php @@ -0,0 +1,153 @@ +<?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\thumbnail; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class delete extends \phpbb\console\command\command +{ +	/** +	* @var \phpbb\db\driver\driver_interface +	*/ +	protected $db; + +	/** +	* phpBB root path +	* @var string +	*/ +	protected $phpbb_root_path; + +	/** +	* Constructor +	* +	* @param \phpbb\user $user The user object (used to get language information) +	* @param \phpbb\db\driver\driver_interface $db Database connection +	* @param string $phpbb_root_path Root path +	*/ +	public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, $phpbb_root_path) +	{ +		$this->db = $db; +		$this->phpbb_root_path = $phpbb_root_path; + +		parent::__construct($user); +	} + +	/** +	* Sets the command name and description +	* +	* @return null +	*/ +	protected function configure() +	{ +		$this +			->setName('thumbnail:delete') +			->setDescription($this->user->lang('CLI_DESCRIPTION_THUMBNAIL_DELETE')) +		; +	} + +	/** +	* Executes the command thumbnail:delete. +	* +	* Deletes all existing thumbnails and updates the database accordingly. +	* +	* @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 if all is ok, 1 if a thumbnail couldn't be deleted. +	*/ +	protected function execute(InputInterface $input, OutputInterface $output) +	{ +		$io = new SymfonyStyle($input, $output); + +		$io->section($this->user->lang('CLI_THUMBNAIL_DELETING')); + +		$sql = 'SELECT COUNT(*) AS nb_missing_thumbnails +			FROM ' . ATTACHMENTS_TABLE . ' +			WHERE thumbnail = 1'; +		$result = $this->db->sql_query($sql); +		$nb_missing_thumbnails = (int) $this->db->sql_fetchfield('nb_missing_thumbnails'); +		$this->db->sql_freeresult($result); + +		if ($nb_missing_thumbnails === 0) +		{ +			$io->warning($this->user->lang('CLI_THUMBNAIL_NOTHING_TO_DELETE')); +			return 0; +		} + +		$sql = 'SELECT attach_id, physical_filename, extension, real_filename, mimetype +			FROM ' . ATTACHMENTS_TABLE . ' +			WHERE thumbnail = 1'; +		$result = $this->db->sql_query($sql); + +		$progress = $this->create_progress_bar($nb_missing_thumbnails, $io, $output); + +		$progress->setMessage($this->user->lang('CLI_THUMBNAIL_DELETING')); + +		$progress->start(); + +		$thumbnail_deleted = array(); +		$return = 0; +		while ($row = $this->db->sql_fetchrow($result)) +		{ +			$thumbnail_path = $this->phpbb_root_path . 'files/thumb_' . $row['physical_filename']; + +			if (@unlink($thumbnail_path)) +			{ +				$thumbnail_deleted[] = $row['attach_id']; + +				if (sizeof($thumbnail_deleted) === 250) +				{ +					$this->commit_changes($thumbnail_deleted); +					$thumbnail_deleted = array(); +				} + +				$progress->setMessage($this->user->lang('CLI_THUMBNAIL_DELETED', $row['real_filename'], $row['physical_filename'])); +			} +			else +			{ +				$return = 1; +				$progress->setMessage('<error>' . $this->user->lang('CLI_THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . '</error>'); +			} + +			$progress->advance(); +		} +		$this->db->sql_freeresult($result); + +		if (!empty($thumbnail_deleted)) +		{ +			$this->commit_changes($thumbnail_deleted); +		} + +		$progress->finish(); + +		$io->newLine(2); +		$io->success($this->user->lang('CLI_THUMBNAIL_DELETING_DONE')); + +		return $return; +	} + +	/** +	* Commits the changes to the database +	* +	* @param array $thumbnail_deleted +	*/ +	protected function commit_changes(array $thumbnail_deleted) +	{ +		$sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' +				SET thumbnail = 0 +				WHERE ' . $this->db->sql_in_set('attach_id', $thumbnail_deleted); +		$this->db->sql_query($sql); +	} +} diff --git a/phpBB/phpbb/console/command/thumbnail/generate.php b/phpBB/phpbb/console/command/thumbnail/generate.php new file mode 100644 index 0000000000..64f7555336 --- /dev/null +++ b/phpBB/phpbb/console/command/thumbnail/generate.php @@ -0,0 +1,179 @@ +<?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\thumbnail; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class generate extends \phpbb\console\command\command +{ +	/** +	* @var \phpbb\db\driver\driver_interface +	*/ +	protected $db; + +	/** +	* @var \phpbb\cache\service +	*/ +	protected $cache; + +	/** +	* phpBB root path +	* @var string +	*/ +	protected $phpbb_root_path; + +	/** +	* PHP extension. +	* +	* @var string +	*/ +	protected $php_ext; + +	/** +	* Constructor +	* +	* @param \phpbb\user $user The user object (used to get language information) +	* @param \phpbb\db\driver\driver_interface $db Database connection +	* @param \phpbb\cache\service $cache The cache service +	* @param string $phpbb_root_path Root path +	* @param string $php_ext PHP extension +	*/ +	public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\cache\service $cache, $phpbb_root_path, $php_ext) +	{ +		$this->db = $db; +		$this->cache = $cache; +		$this->phpbb_root_path = $phpbb_root_path; +		$this->php_ext = $php_ext; + +		parent::__construct($user); +	} + +	/** +	* Sets the command name and description +	* +	* @return null +	*/ +	protected function configure() +	{ +		$this +			->setName('thumbnail:generate') +			->setDescription($this->user->lang('CLI_DESCRIPTION_THUMBNAIL_GENERATE')) +		; +	} + +	/** +	* Executes the command thumbnail:generate. +	* +	* Generate a thumbnail for all attachments which need one and don't have it yet. +	* +	* @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. +	*/ +	protected function execute(InputInterface $input, OutputInterface $output) +	{ +		$io = new SymfonyStyle($input, $output); + +		$io->section($this->user->lang('CLI_THUMBNAIL_GENERATING')); + +		$sql = 'SELECT COUNT(*) AS nb_missing_thumbnails +			FROM ' . ATTACHMENTS_TABLE . ' +			WHERE thumbnail = 0'; +		$result = $this->db->sql_query($sql); +		$nb_missing_thumbnails = (int) $this->db->sql_fetchfield('nb_missing_thumbnails'); +		$this->db->sql_freeresult($result); + +		if ($nb_missing_thumbnails === 0) +		{ +			$io->warning($this->user->lang('CLI_THUMBNAIL_NOTHING_TO_GENERATE')); +			return 0; +		} + +		$extensions = $this->cache->obtain_attach_extensions(true); + +		$sql = 'SELECT attach_id, physical_filename, extension, real_filename, mimetype +			FROM ' . ATTACHMENTS_TABLE . ' +			WHERE thumbnail = 0'; +		$result = $this->db->sql_query($sql); + +		if (!function_exists('create_thumbnail')) +		{ +			require($this->phpbb_root_path . 'includes/functions_posting.' . $this->php_ext); +		} + +		$progress = $this->create_progress_bar($nb_missing_thumbnails, $io, $output); + +		$progress->setMessage($this->user->lang('CLI_THUMBNAIL_GENERATING')); + +		$progress->start(); + +		$thumbnail_created = array(); +		while ($row = $this->db->sql_fetchrow($result)) +		{ +			if (isset($extensions[$row['extension']]['display_cat']) && $extensions[$row['extension']]['display_cat'] == ATTACHMENT_CATEGORY_IMAGE) +			{ +				$source = $this->phpbb_root_path . 'files/' . $row['physical_filename']; +				$destination = $this->phpbb_root_path . 'files/thumb_' . $row['physical_filename']; + +				if (create_thumbnail($source, $destination, $row['mimetype'])) +				{ +					$thumbnail_created[] = (int) $row['attach_id']; + +					if (count($thumbnail_created) === 250) +					{ +						$this->commit_changes($thumbnail_created); +						$thumbnail_created = array(); +					} + +					$progress->setMessage($this->user->lang('CLI_THUMBNAIL_GENERATED', $row['real_filename'], $row['physical_filename'])); +				} +				else +				{ +					$progress->setMessage('<info>' . $this->user->lang('CLI_THUMBNAIL_SKIPPED', $row['real_filename'], $row['physical_filename']) . '</info>'); +				} +			} + +			$progress->advance(); +		} +		$this->db->sql_freeresult($result); + +		if (!empty($thumbnail_created)) +		{ +			$this->commit_changes($thumbnail_created); +		} + +		$progress->finish(); + +		$io->newLine(2); +		$io->success($this->user->lang('CLI_THUMBNAIL_GENERATING_DONE')); + +		return 0; +	} + +	/** +	* Commits the changes to the database +	* +	* @param array $thumbnail_created +	*/ +	protected function commit_changes(array $thumbnail_created) +	{ +		$sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' +				SET thumbnail = 1 +				WHERE ' . $this->db->sql_in_set('attach_id', $thumbnail_created); +		$this->db->sql_query($sql); +	} +} diff --git a/phpBB/phpbb/console/command/thumbnail/recreate.php b/phpBB/phpbb/console/command/thumbnail/recreate.php new file mode 100644 index 0000000000..382da290bf --- /dev/null +++ b/phpBB/phpbb/console/command/thumbnail/recreate.php @@ -0,0 +1,72 @@ +<?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\thumbnail; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\OutputInterface; + +class recreate extends \phpbb\console\command\command +{ +	/** +	* Sets the command name and description +	* +	* @return null +	*/ +	protected function configure() +	{ +		$this +			->setName('thumbnail:recreate') +			->setDescription($this->user->lang('CLI_DESCRIPTION_THUMBNAIL_RECREATE')) +		; +	} + +	/** +	* Executes the command thumbnail:recreate. +	* +	* This command is a "macro" to execute thumbnail:delete and then thumbnail:generate. +	* +	* @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 thumbnail couldn't be deleted. +	*/ +	protected function execute(InputInterface $input, OutputInterface $output) +	{ +		$parameters = array( +			'command' => 'thumbnail:delete' +		); + +		if ($input->getOption('verbose')) +		{ +			$parameters['-' . str_repeat('v', $output->getVerbosity() - 1)] = true; +		} + +		$this->getApplication()->setAutoExit(false); + +		$input_delete = new ArrayInput($parameters); +		$return = $this->getApplication()->run($input_delete, $output); + +		if ($return === 0) +		{ +			$parameters['command'] = 'thumbnail:generate'; + +			$input_create = new ArrayInput($parameters); +			$return = $this->getApplication()->run($input_create, $output); +		} + +		$this->getApplication()->setAutoExit(true); + +		return $return; +	} +} diff --git a/phpBB/phpbb/console/command/update/check.php b/phpBB/phpbb/console/command/update/check.php new file mode 100644 index 0000000000..ed8ad79eea --- /dev/null +++ b/phpBB/phpbb/console/command/update/check.php @@ -0,0 +1,331 @@ +<?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 occured. +	* @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 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); +	} +} diff --git a/phpBB/phpbb/console/command/user/activate.php b/phpBB/phpbb/console/command/user/activate.php new file mode 100644 index 0000000000..9c85718b4c --- /dev/null +++ b/phpBB/phpbb/console/command/user/activate.php @@ -0,0 +1,218 @@ +<?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\language\language; +use phpbb\log\log_interface; +use phpbb\notification\manager; +use phpbb\user; +use phpbb\user_loader; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class activate extends command +{ +	/** @var driver_interface */ +	protected $db; + +	/** @var config */ +	protected $config; + +	/** @var language */ +	protected $language; + +	/** @var log_interface */ +	protected $log; + +	/** @var manager */ +	protected $notifications; + +	/** @var user_loader */ +	protected $user_loader; + +	/** +	 * 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 log_interface    $log +	 * @param manager          $notifications +	 * @param user_loader      $user_loader +	 * @param string           $phpbb_root_path +	 * @param string           $php_ext +	 */ +	public function __construct(user $user, driver_interface $db, config $config, language $language, log_interface $log, manager $notifications, user_loader $user_loader, $phpbb_root_path, $php_ext) +	{ +		$this->db = $db; +		$this->config = $config; +		$this->language = $language; +		$this->log = $log; +		$this->notifications = $notifications; +		$this->user_loader = $user_loader; +		$this->phpbb_root_path = $phpbb_root_path; +		$this->php_ext = $php_ext; + +		$this->language->add_lang('acp/users'); +		parent::__construct($user); +	} + +	/** +	 * Sets the command name and description +	 * +	 * @return null +	 */ +	protected function configure() +	{ +		$this +			->setName('user:activate') +			->setDescription($this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE')) +			->setHelp($this->language->lang('CLI_HELP_USER_ACTIVATE')) +			->addArgument( +				'username', +				InputArgument::REQUIRED, +				$this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE_USERNAME') +			) +			->addOption( +				'deactivate', +				'd', +				InputOption::VALUE_NONE, +				$this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE_DEACTIVATE') +			) +			->addOption( +				'send-email', +				null, +				InputOption::VALUE_NONE, +				$this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_NOTIFY') +			) +		; +	} + +	/** +	 * Executes the command user:activate +	 * +	 * Activate (or deactivate) a user account +	 * +	 * @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); + +		$name = $input->getArgument('username'); +		$mode = ($input->getOption('deactivate')) ? 'deactivate' : 'activate'; + +		$user_id  = $this->user_loader->load_user_by_username($name); +		$user_row = $this->user_loader->get_user($user_id); + +		if ($user_row['user_id'] == ANONYMOUS) +		{ +			$io->error($this->language->lang('NO_USER')); +			return 1; +		} + +		// Check if the user is already active (or inactive) +		if ($mode == 'activate' && $user_row['user_type'] != USER_INACTIVE) +		{ +			$io->error($this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE_ACTIVE')); +			return 1; +		} +		else if ($mode == 'deactivate' && $user_row['user_type'] == USER_INACTIVE) +		{ +			$io->error($this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE_INACTIVE')); +			return 1; +		} + +		// Activate the user account +		if (!function_exists('user_active_flip')) +		{ +			require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); +		} + +		user_active_flip($mode, $user_row['user_id']); + +		// Notify the user upon activation +		if ($mode == 'activate' && $this->config['require_activation'] == USER_ACTIVATION_ADMIN) +		{ +			$this->send_notification($user_row, $input); +		} + +		// Log and display the result +		$msg = ($mode == 'activate') ? 'USER_ADMIN_ACTIVATED' : 'USER_ADMIN_DEACTIVED'; +		$log = ($mode == 'activate') ? 'LOG_USER_ACTIVE' : 'LOG_USER_INACTIVE'; + +		$this->log->add('admin', ANONYMOUS, '', $log, false, array($user_row['username'])); +		$this->log->add('user', ANONYMOUS, '', $log . '_USER', false, array( +			'reportee_id' => $user_row['user_id'] +		)); + +		$io->success($this->language->lang($msg)); + +		return 0; +	} + +	/** +	 * Send account activation notification to user +	 * +	 * @param array           $user_row The user data array +	 * @param InputInterface  $input    The input stream used to get the options +	 * @return null +	 */ +	protected function send_notification($user_row, InputInterface $input) +	{ +		$this->notifications->delete_notifications('notification.type.admin_activate_user', $user_row['user_id']); + +		if ($input->getOption('send-email')) +		{ +			if (!class_exists('messenger')) +			{ +				require($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext); +			} + +			$messenger = new \messenger(false); +			$messenger->template('admin_welcome_activated', $user_row['user_lang']); +			$messenger->set_addresses($user_row); +			$messenger->anti_abuse_headers($this->config, $this->user); +			$messenger->assign_vars(array( +					'USERNAME'	=> htmlspecialchars_decode($user_row['username'])) +			); + +			$messenger->send(NOTIFY_EMAIL); +		} +	} +} diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php new file mode 100644 index 0000000000..c60a059251 --- /dev/null +++ b/phpBB/phpbb/console/command/user/add.php @@ -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'], $this->config['max_pass_chars']), +				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') . ' '; +	} +} diff --git a/phpBB/phpbb/console/command/user/delete.php b/phpBB/phpbb/console/command/user/delete.php new file mode 100644 index 0000000000..8593541c1a --- /dev/null +++ b/phpBB/phpbb/console/command/user/delete.php @@ -0,0 +1,170 @@ +<?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\console\command\command; +use phpbb\db\driver\driver_interface; +use phpbb\language\language; +use phpbb\log\log_interface; +use phpbb\user; +use phpbb\user_loader; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Console\Style\SymfonyStyle; + +class delete extends command +{ +	/** @var driver_interface */ +	protected $db; + +	/** @var language */ +	protected $language; + +	/** @var log_interface */ +	protected $log; + +	/** @var user_loader */ +	protected $user_loader; + +	/** +	 * 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 language         $language +	 * @param log_interface    $log +	 * @param user_loader      $user_loader +	 * @param string           $phpbb_root_path +	 * @param string           $php_ext +	 */ +	public function __construct(user $user, driver_interface $db, language $language, log_interface $log, user_loader $user_loader, $phpbb_root_path, $php_ext) +	{ +		$this->db = $db; +		$this->language = $language; +		$this->log = $log; +		$this->user_loader = $user_loader; +		$this->phpbb_root_path = $phpbb_root_path; +		$this->php_ext = $php_ext; + +		$this->language->add_lang('acp/users'); +		parent::__construct($user); +	} + +	/** +	 * Sets the command name and description +	 * +	 * @return null +	 */ +	protected function configure() +	{ +		$this +			->setName('user:delete') +			->setDescription($this->language->lang('CLI_DESCRIPTION_USER_DELETE')) +			->addArgument( +				'username', +				InputArgument::REQUIRED, +				$this->language->lang('CLI_DESCRIPTION_USER_DELETE_USERNAME') +			) +			->addOption( +				'delete-posts', +				null, +				InputOption::VALUE_NONE, +				$this->language->lang('CLI_DESCRIPTION_USER_DELETE_OPTION_POSTS') +			) +		; +	} + +	/** +	 * Executes the command user:delete +	 * +	 * Deletes a user from the database. An option to delete the user's posts +	 * is available, by default posts will be retained. +	 * +	 * @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) +	{ +		$name = $input->getArgument('username'); +		$mode = ($input->getOption('delete-posts')) ? 'remove' : 'retain'; + +		if ($name) +		{ +			$io = new SymfonyStyle($input, $output); + +			$user_id  = $this->user_loader->load_user_by_username($name); +			$user_row = $this->user_loader->get_user($user_id); + +			if ($user_row['user_id'] == ANONYMOUS) +			{ +				$io->error($this->language->lang('NO_USER')); +				return 1; +			} + +			if (!function_exists('user_delete')) +			{ +				require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); +			} + +			user_delete($mode, $user_row['user_id'], $user_row['username']); + +			$this->log->add('admin', ANONYMOUS, '', 'LOG_USER_DELETED', false, array($user_row['username'])); + +			$io->success($this->language->lang('USER_DELETED')); +		} + +		return 0; +	} + +	/** +	 * Interacts with the user. +	 * Confirm they really want to delete the account...last chance! +	 * +	 * @param InputInterface  $input  An InputInterface instance +	 * @param OutputInterface $output An OutputInterface instance +	 */ +	protected function interact(InputInterface $input, OutputInterface $output) +	{ +		$helper = $this->getHelper('question'); + +		$question = new ConfirmationQuestion( +			$this->language->lang('CLI_USER_DELETE_CONFIRM', $input->getArgument('username')), +			false +		); + +		if (!$helper->ask($input, $output, $question)) +		{ +			$input->setArgument('username', false); +		} +	} +} diff --git a/phpBB/phpbb/console/command/user/reclean.php b/phpBB/phpbb/console/command/user/reclean.php new file mode 100644 index 0000000000..1a89f13382 --- /dev/null +++ b/phpBB/phpbb/console/command/user/reclean.php @@ -0,0 +1,158 @@ +<?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\console\command\command; +use phpbb\db\driver\driver_interface; +use phpbb\language\language; +use phpbb\user; +use Symfony\Component\Console\Helper\ProgressBar; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class reclean extends command +{ +	/** @var driver_interface */ +	protected $db; + +	/** @var language */ +	protected $language; + +	/** @var int A count of the number of re-cleaned user names */ +	protected $processed; + +	/** @var ProgressBar */ +	protected $progress; + +	/** +	 * Construct method +	 * +	 * @param user             $user +	 * @param driver_interface $db +	 * @param language         $language +	 */ +	public function __construct(user $user, driver_interface $db, language $language) +	{ +		$this->db = $db; +		$this->language = $language; + +		parent::__construct($user); +	} + +	/** +	 * Sets the command name and description +	 * +	 * @return null +	 */ +	protected function configure() +	{ +		$this +			->setName('user:reclean') +			->setDescription($this->language->lang('CLI_DESCRIPTION_USER_RECLEAN')) +			->setHelp($this->language->lang('CLI_HELP_USER_RECLEAN')) +		; +	} + +	/** +	 * Executes the command user:reclean +	 * +	 * Cleans user names that are unclean. +	 * +	 * @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); + +		$io->section($this->language->lang('CLI_USER_RECLEAN_START')); + +		$this->processed = 0; + +		$this->progress = $this->create_progress_bar($this->get_count(), $io, $output); +		$this->progress->setMessage($this->language->lang('CLI_USER_RECLEAN_START')); +		$this->progress->start(); + +		$stage = 0; +		while ($stage !== true) +		{ +			$stage = $this->reclean_usernames($stage); +		} + +		$this->progress->finish(); + +		$io->newLine(2); +		$io->success($this->language->lang('CLI_USER_RECLEAN_DONE', $this->processed)); + +		return 0; +	} + +	/** +	 * Re-clean user names +	 * Only user names that are unclean will be re-cleaned +	 * +	 * @param int $start An offset index +	 * @return bool|int Return the next offset index or true if all records have been processed. +	 */ +	protected function reclean_usernames($start = 0) +	{ +		$limit = 500; +		$i = 0; + +		$this->db->sql_transaction('begin'); + +		$sql = 'SELECT user_id, username, username_clean FROM ' . USERS_TABLE; +		$result = $this->db->sql_query_limit($sql, $limit, $start); +		while ($row = $this->db->sql_fetchrow($result)) +		{ +			$i++; +			$username_clean = $this->db->sql_escape(utf8_clean_string($row['username'])); + +			if ($username_clean != $row['username_clean']) +			{ +				$sql = 'UPDATE ' . USERS_TABLE . " +					SET username_clean = '$username_clean' +					WHERE user_id = {$row['user_id']}"; +				$this->db->sql_query($sql); + +				$this->processed++; +			} + +			$this->progress->advance(); +		} +		$this->db->sql_freeresult($result); + +		$this->db->sql_transaction('commit'); + +		return ($i < $limit) ? true : $start + $i; +	} + +	/** +	 * Get the count of users in the database +	 * +	 * @return int +	 */ +	protected function get_count() +	{ +		$sql = 'SELECT COUNT(user_id) AS count FROM ' . USERS_TABLE; +		$result = $this->db->sql_query($sql); +		$count = (int) $this->db->sql_fetchfield('count'); +		$this->db->sql_freeresult($result); + +		return $count; +	} +} | 
