123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751 |
- <?php
- /**
- * This file is part of the ForkBB <https://github.com/forkbb>.
- *
- * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
- * @license The MIT License (MIT)
- */
- declare(strict_types=1);
- namespace ForkBB\Models\Pages\Admin;
- use ForkBB\Core\Config as CoreConfig;
- use ForkBB\Core\Container;
- use ForkBB\Core\Validator;
- use ForkBB\Models\Page;
- use ForkBB\Models\Pages\Admin;
- use PDO;
- use PDOException;
- use RuntimeException;
- use ForkBB\Core\Exceptions\ForkException;
- use function \ForkBB\__;
- class Update extends Admin
- {
- const PHP_MIN = '7.3.0';
- const REV_MIN_FOR_UPDATE = 42;
- const LATEST_REV_WITH_DB_CHANGES = 43;
- const LOCK_NAME = 'lock_update';
- const LOCk_TTL = 1800;
- const JSON_OPTIONS = \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | \JSON_THROW_ON_ERROR;
- const CONFIG_FILE = 'main.php';
- protected $configFile;
- /**
- * Флаг проверки пароля
- * @var bool
- */
- protected $okPass;
- public function __construct(Container $container)
- {
- parent::__construct($container);
- $container->Lang->load('validator');
- $container->Lang->load('admin_update');
- $this->aIndex = 'update';
- $this->httpStatus = 503;
- $this->onlinePos = null;
- $this->nameTpl = 'admin/form';
- $this->titleForm = 'Update ForkBB';
- $this->classForm = ['updateforkbb'];
- $this->configFile = $container->DIR_CONFIG . '/' . self::CONFIG_FILE;
- $this->header('Retry-After', '3600');
- }
- /**
- * Подготовка страницы к отображению
- */
- public function prepare(): void
- {
- $this->aNavigation = $this->aNavigation();
- $this->crumbs = $this->crumbs(...$this->aCrumbs);
- }
- /**
- * Возвращает массив ссылок с описанием для построения навигации админки
- */
- protected function aNavigation(): array
- {
- return [
- 'update' => [
- $this->c->Router->link('AdminUpdate'),
- __('Update ForkBB'),
- ],
- ];
- }
- /**
- * Возвращает страницу обслуживания с доп.сообщением
- */
- protected function returnMaintenance(bool $isStage = true): Page
- {
- $maintenance = $this->c->Maintenance;
- $maintenance->fIswev = ['w', 'Update script is running'];
- if ($isStage) {
- $maintenance->fIswev = ['e', 'Script runs error'];
- }
- return $maintenance;
- }
- /**
- * Проверяет наличие блокировки скрипта обновления
- */
- protected function hasLock(string $uid = null): bool
- {
- $lock = $this->c->Cache->get(self::LOCK_NAME);
- if (null === $uid) {
- return ! empty($lock);
- } else {
- return empty($lock) || ! \hash_equals($uid, (string) $lock);
- }
- }
- protected function setLock(string $uid = null): ?string
- {
- if (true === $this->hasLock($uid)) {
- return null;
- }
- if (null === $uid) {
- $uid = $this->c->Secury->randomHash(33);
- }
- $this->c->Cache->set(self::LOCK_NAME, $uid, self::LOCk_TTL);
- if (true === $this->hasLock($uid)) {
- return null;
- }
- return $uid;
- }
- /**
- * Подготавливает данные для страницы обновления форума
- */
- public function view(array $args, string $method): Page
- {
- if (true === $this->hasLock()) {
- return $this->returnMaintenance(false);
- }
- if (
- 'POST' === $method
- && empty($this->fIswev)
- ) {
- $v = $this->c->Validator->reset()
- ->addValidators([
- 'check_pass' => [$this, 'vCheckPass'],
- ])->addRules([
- 'token' => 'token:AdminUpdate',
- 'dbpass' => 'exist|string|check_pass',
- 'o_maintenance_message' => 'required|string:trim|max:65000 bytes|html',
- ])->addAliases([
- 'dbpass' => 'Database password',
- 'o_maintenance_message' => 'Maintenance message',
- ])->addMessages([
- ]);
- if (
- $v->validation($_POST)
- && $this->okPass
- ) {
- $e = null;
- // версия PHP
- if (
- null === $e
- && \version_compare(\PHP_VERSION, self::PHP_MIN, '<')
- ) {
- $e = __(['You are running error', 'PHP', \PHP_VERSION, $this->c->FORK_REVISION, self::PHP_MIN]);
- }
- // база не от ForkBB или старая ревизия
- if (
- null === $e
- && $this->c->config->i_fork_revision < self::REV_MIN_FOR_UPDATE
- ) {
- $e = 'Version mismatch error';
- }
- // загрузка и проверка конфига
- if (null === $e) {
- try {
- $coreConfig = new CoreConfig($this->configFile);
- } catch (ForkException $excp) {
- $e = $excp->getMessage();
- }
- }
- // проверка доступности базы данных на изменения
- if (
- null === $e
- && $this->c->config->i_fork_revision < self::LATEST_REV_WITH_DB_CHANGES
- ) {
- $testTable = '::test_tb_for_update';
- if (
- null === $e
- && true === $this->c->DB->tableExists($testTable)
- ) {
- $e = ['The %s table already exists. Delete it.', $testTable];
- }
- $schema = [
- 'FIELDS' => [
- 'id' => ['SERIAL', false],
- ],
- 'PRIMARY KEY' => ['id'],
- ];
- if (
- null === $e
- && false === $this->c->DB->createTable($testTable, $schema)
- ) {
- $e = ['Unable to create %s table', $testTable];
- }
- if (
- null === $e
- && false === $this->c->DB->addField($testTable, 'test_field', 'VARCHAR(80)', false, '')
- ) {
- $e = ['Unable to add test_field field to %s table', $testTable];
- }
- $sql = "INSERT INTO {$testTable} (test_field) VALUES ('TEST_VALUE')";
- if (
- null === $e
- && false === $this->c->DB->exec($sql)
- ) {
- $e = ['Unable to insert line to %s table', $testTable];
- }
- if (
- null === $e
- && false === $this->c->DB->dropField($testTable, 'test_field')
- ) {
- $e = ['Unable to drop test_field field from %s table', $testTable];
- }
- if (
- null === $e
- && false === $this->c->DB->dropTable($testTable)
- ) {
- $e = ['Unable to drop %s table', $testTable];
- }
- }
- if (null !== $e) {
- return $this->c->Message->message($e, true, 503);
- }
- $uid = $this->setLock();
- if (null === $uid) {
- $this->fIswev = ['e', 'Unable to write update lock'];
- } else {
- $this->c->config->o_maintenance_message = $v->o_maintenance_message;
- $this->c->config->save();
- return $this->c->Redirect->page('AdminUpdateStage', ['uid' => $uid, 'stage' => 1]);
- }
- } else {
- $this->fIswev = $v->getErrors();
- }
- } else {
- $v = null;
- }
- $this->form = $this->form($v);
- return $this;
- }
- /**
- * Проверяет пароль базы
- */
- public function vCheckPass(Validator $v, string $dbpass): string
- {
- $this->okPass = true;
- if (\substr($this->c->DB_DSN, 0, 6) === 'sqlite') {
- if (! \hash_equals($this->c->DB_DSN, "sqlite:{$dbpass}")) {
- $this->okPass = false;
- $v->addError(['Invalid file error', self::CONFIG_FILE]);
- }
- } else {
- if (! \hash_equals($this->c->DB_PASSWORD, $dbpass)) {
- $this->okPass = false;
- $v->addError(['Invalid password error', self::CONFIG_FILE]);
- }
- }
- return $dbpass;
- }
- /**
- * Формирует массив для формы
- */
- protected function form(?Validator $v): array
- {
- return [
- 'action' => $this->c->Router->link('AdminUpdate'),
- 'hidden' => [
- 'token' => $this->c->Csrf->create('AdminUpdate'),
- ],
- 'sets' => [
- 'update-info' => [
- 'info' => [
- [
- 'value' => __('Update message'),
- ],
- ],
- ],
- 'update' => [
- 'legend' => 'Update ForkBB',
- 'fields' => [
- 'dbpass' => [
- 'type' => 'password',
- 'value' => '',
- 'caption' => 'Database password',
- 'help' => 'Database password note',
- ],
- 'o_maintenance_message' => [
- 'type' => 'textarea',
- 'value' => $v->o_maintenance_message ?? $this->c->config->o_maintenance_message,
- 'caption' => 'Maintenance message',
- 'help' => 'Maintenance message info',
- 'required' => true,
- ],
- ],
- ],
- 'member-info' => [
- 'info' => [
- [
- 'value' => __('Members message'),
- ],
- ],
- ],
- ],
- 'btns' => [
- 'start' => [
- 'type' => 'submit',
- 'value' => __('Start update'),
- ],
- ],
- ];
- }
- /**
- * Обновляет форум
- */
- public function stage(array $args, string $method): Page
- {
- try {
- $uid = $this->setLock($args['uid']);
- if (null === $uid) {
- return $this->returnMaintenance();
- }
- $stage = \max($args['stage'], $this->c->config->i_fork_revision);
- do {
- if (\method_exists($this, 'stageNumber' . $stage)) {
- $start = $this->{'stageNumber' . $stage}($args);
- if (null === $start) {
- ++$stage;
- }
- return $this->c->Redirect->page(
- 'AdminUpdateStage',
- ['uid' => $uid, 'stage' => $stage, 'start' => $start]
- )->message(['Stage %1$s (%2$s)', $stage, (int) $start]);
- }
- ++$stage;
- } while ($stage < $this->c->FORK_REVISION);
- $this->c->config->i_fork_revision = $this->c->FORK_REVISION;
- $this->c->config->save();
- if (true !== $this->c->Cache->clear()) {
- throw new RuntimeException('Unable to clear cache');
- }
- return $this->c->Redirect->page('Index')->message('Successfully updated');
- } catch (ForkException $excp) {
- return $this->c->Message->message($excp->getMessage(), true, 503);
- }
- }
- # /**
- # * Выполняет определенный шаг обновления
- # *
- # * Возвращает null, если шаг выпонен
- # * Возвращает положительный int, если требуется продолжить выполнение шага
- # */
- # protected function stageNumber1(array $args): ?int
- # {
- # $coreConfig = new CoreConfig($this->configFile);
- #
- # $coreConfig->add(
- # 'multiple=>AdminUsersRecalculate',
- # '\\ForkBB\\Models\\Pages\\Admin\\Users\\Recalculate::class',
- # 'AdminUsersNew'
- # );
- #
- # $coreConfig->save();
- #
- # return null;
- # }
- /**
- * rev.42 to rev.43
- */
- protected function stageNumber42(array $args): ?int
- {
- $query = 'DELETE FROM ::users WHERE id=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::forums SET last_poster_id=0 WHERE last_poster_id=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::online SET user_id=0 WHERE user_id=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::pm_posts SET poster_id=0 WHERE poster_id=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::pm_topics SET poster_id=0 WHERE poster_id=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::pm_topics SET target_id=0 WHERE target_id=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::posts SET poster_id=0 WHERE poster_id=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::posts SET editor_id=0 WHERE editor_id=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::reports SET reported_by=0 WHERE reported_by=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::reports SET zapped_by=0 WHERE zapped_by=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::topics SET poster_id=0 WHERE poster_id=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::topics SET last_poster_id=0 WHERE last_poster_id=1';
- $this->c->DB->exec($query);
- $query = 'UPDATE ::warnings SET poster_id=0 WHERE poster_id=1';
- $this->c->DB->exec($query);
- $coreConfig = new CoreConfig($this->configFile);
- $coreConfig->add(
- 'shared=>Groups/save',
- '\\ForkBB\\Models\\Group\\Save::class',
- 'Group/save'
- );
- $coreConfig->add(
- 'shared=>Groups/perm',
- '\\ForkBB\\Models\\Group\\Perm::class',
- 'Group/save'
- );
- $coreConfig->add(
- 'shared=>Groups/delete',
- '\\ForkBB\\Models\\Group\\Delete::class',
- 'Group/save'
- );
- $result = $coreConfig->delete('shared=>Group/delete');
- $result = $coreConfig->delete('shared=>Group/perm');
- $result = $coreConfig->delete('shared=>Group/save');
- $coreConfig->save();
- $this->c->config->a_guest_set = [
- 'show_smilies' => 1,
- 'show_sig' => 1,
- 'show_avatars' => 1,
- 'show_img' => 1,
- 'show_img_sig' => 1,
- ];
- $this->c->config->save();
- return null;
- }
- /**
- * rev.43 to rev.44
- */
- protected function stageNumber43(array $args): ?int
- {
- $config = $this->c->config;
- $config->i_timeout_visit = $config->o_timeout_visit ?? 3600;
- $config->i_timeout_online = $config->o_timeout_online ?? 900;
- $config->i_redirect_delay = $config->o_redirect_delay ?? 1;
- $config->b_show_user_info = '1' == $config->o_show_user_info ? 1 : 0;
- $config->b_show_post_count = '1' == $config->o_show_post_count ? 1 : 0;
- $config->b_smilies_sig = '1' == $config->o_smilies_sig ? 1 : 0;
- $config->b_smilies = '1' == $config->o_smilies ? 1 : 0;
- $config->b_make_links = '1' == $config->o_make_links ? 1 : 0;
- $config->b_quickpost = '1' == $config->o_quickpost ? 1 : 0;
- $config->b_users_online = '1' == $config->o_users_online ? 1 : 0;
- $config->b_censoring = '1' == $config->o_censoring ? 1 : 0;
- $config->b_show_dot = '1' == $config->o_show_dot ? 1 : 0;
- $config->b_topic_views = '1' == $config->o_topic_views ? 1 : 0;
- $config->b_regs_report = '1' == $config->o_regs_report ? 1 : 0;
- $config->b_avatars = '1' == $config->o_avatars ? 1 : 0;
- $config->b_forum_subscriptions = '1' == $config->o_forum_subscriptions ? 1 : 0;
- $config->b_topic_subscriptions = '1' == $config->o_topic_subscriptions ? 1 : 0;
- $config->b_smtp_ssl = '1' == $config->o_smtp_ssl ? 1 : 0;
- $config->b_regs_allow = '1' == $config->o_regs_allow ? 1 : 0;
- $config->b_announcement = '1' == $config->o_announcement ? 1 : 0;
- $config->b_rules = '1' == $config->o_rules ? 1 : 0;
- $config->b_maintenance = '1' == $config->o_maintenance ? 1 : 0;
- $config->b_default_dst = '1' == $config->o_default_dst ? 1 : 0;
- $config->b_message_bbcode = '1' == $config->p_message_bbcode ? 1 : 0;
- $config->b_message_all_caps = '1' == $config->p_message_all_caps ? 1 : 0;
- $config->b_subject_all_caps = '1' == $config->p_subject_all_caps ? 1 : 0;
- $config->b_sig_all_caps = '1' == $config->p_sig_all_caps ? 1 : 0;
- $config->b_sig_bbcode = '1' == $config->p_sig_bbcode ? 1 : 0;
- $config->b_force_guest_email = '1' == $config->p_force_guest_email ? 1 : 0;
- unset($config->p_force_guest_email);
- unset($config->p_sig_bbcode);
- unset($config->p_sig_all_caps);
- unset($config->p_subject_all_caps);
- unset($config->p_message_all_caps);
- unset($config->p_message_bbcode);
- unset($config->o_default_dst);
- unset($config->o_maintenance);
- unset($config->o_rules);
- unset($config->o_announcement);
- unset($config->o_regs_allow);
- unset($config->o_smtp_ssl);
- unset($config->o_topic_subscriptions);
- unset($config->o_forum_subscriptions);
- unset($config->o_avatars);
- unset($config->o_regs_report);
- unset($config->o_topic_views);
- unset($config->o_show_dot);
- unset($config->o_timeout_visit);
- unset($config->o_timeout_online);
- unset($config->o_redirect_delay);
- unset($config->o_show_user_info);
- unset($config->o_show_post_count);
- unset($config->o_smilies_sig);
- unset($config->o_smilies);
- unset($config->o_make_links);
- unset($config->o_quickpost);
- unset($config->o_users_online);
- unset($config->o_censoring);
- unset($config->o_quickjump);
- unset($config->o_search_all_forums);
- $config->save();
- return null;
- }
- /**
- * rev.44 to rev.45
- */
- protected function stageNumber44(array $args): ?int
- {
- if (! $this->c->DB->query('SELECT id FROM ::bbcode WHERE bb_tag=?s', ['from'])->fetchColumn()) {
- $bbcodes = include $this->c->DIR_CONFIG . '/defaultBBCode.php';
- foreach ($bbcodes as $bbcode) {
- if ('from' !== $bbcode['tag']) {
- continue;
- }
- $vars = [
- ':tag' => $bbcode['tag'],
- ':structure' => \json_encode($bbcode, self::JSON_OPTIONS),
- ];
- $query = 'INSERT INTO ::bbcode (bb_tag, bb_edit, bb_delete, bb_structure)
- VALUES(?s:tag, 1, 0, ?s:structure)';
- $this->c->DB->exec($query, $vars);
- }
- }
- return null;
- }
- /**
- * rev.45 to rev.46
- */
- protected function stageNumber45(array $args): ?int
- {
- $coreConfig = new CoreConfig($this->configFile);
- $coreConfig->add(
- 'shared=>Cache=>reset_mark',
- '\'%DB_DSN% %DB_PREFIX%\'',
- 'cache_dir'
- );
- $coreConfig->save();
- // чтобы кэш не был сброшен до завершения обновления
- $hash = \sha1($this->c->DB_DSN . ' ' . $this->c->DB_PREFIX);
- $this->c->Cache->set('reset_mark_hash', $hash);
- return null;
- }
- /**
- * rev.46 to rev.47
- */
- protected function stageNumber46(array $args): ?int
- {
- $coreConfig = new CoreConfig($this->configFile);
- $coreConfig->add(
- 'shared=>Mail=>ssl',
- '\'%config.b_smtp_ssl%\''
- );
- $coreConfig->save();
- return null;
- }
- /**
- * rev.47 to rev.48
- */
- protected function stageNumber47(array $args): ?int
- {
- $config = $this->c->config;
- $config->s_РЕГИСТР = 'Ok';
- $config->save();
- $coreConfig = new CoreConfig($this->configFile);
- $coreConfig->add(
- 'shared=>Config/insensitive',
- '\\ForkBB\\Models\\Config\\Insensitive::class',
- 'Config/save'
- );
- $coreConfig->save();
- return null;
- }
- /**
- * rev.48 to rev.49
- */
- protected function stageNumber48(array $args): ?int
- {
- $coreConfig = new CoreConfig($this->configFile);
- $coreConfig->add(
- 'DATE_FORMATS',
- ['\'Y-m-d\'', '\'d M Y\'', '\'Y-m-d\'', '\'Y-d-m\'', '\'d-m-Y\'', '\'m-d-Y\'', '\'M j Y\'', '\'jS M Y\''],
- 'HTTP_HEADERS'
- );
- $coreConfig->add(
- 'TIME_FORMATS',
- ['\'H:i:s\'', '\'H:i\'', '\'H:i:s\'', '\'H:i\'', '\'g:i:s a\'', '\'g:i a\''],
- 'DATE_FORMATS'
- );
- $coreConfig->add(
- 'shared=>%DIR_VIEWS%',
- '\'%DIR_APP%/templates\'',
- 'DB'
- );
- $coreConfig->add(
- 'shared=>%DIR_LOG%',
- '\'%DIR_APP%/log\'',
- 'DB'
- );
- $coreConfig->add(
- 'shared=>%DIR_LANG%',
- '\'%DIR_APP%/lang\'',
- 'DB'
- );
- $coreConfig->add(
- 'shared=>%DIR_CONFIG%',
- '\'%DIR_APP%/config\'',
- 'DB'
- );
- $coreConfig->add(
- 'shared=>%DIR_CACHE%',
- '\'%DIR_APP%/cache\'',
- 'DB'
- );
- $coreConfig->add(
- 'shared=>%DIR_APP%',
- '\'%DIR_ROOT%/app\'',
- 'DB'
- );
- $coreConfig->add(
- 'shared=>%DIR_PUBLIC%',
- '\'%DIR_ROOT%/public\'',
- 'DB'
- );
- $coreConfig->add(
- 'shared=>%DIR_ROOT%',
- '\\realpath(__DIR__ . \'/../..\')',
- 'DB'
- );
- $coreConfig->add(
- 'shared=>HTMLCleaner=>config',
- '\'%DIR_CONFIG%/jevix.default.php\''
- );
- $coreConfig->save();
- return null;
- }
- }
|