123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- <?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;
- 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_APP . '/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' => 'required|string:trim|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)) {
- $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
- ) {
- $test_table = 'test_tb_for_update';
- if (
- null === $e
- && true === $this->c->DB->tableExists($test_table)
- ) {
- $e = ['The %s table already exists. Delete it.', $test_table];
- }
- $schema = [
- 'FIELDS' => [
- 'id' => ['SERIAL', false],
- ],
- 'PRIMARY KEY' => ['id'],
- ];
- if (
- null === $e
- && false === $this->c->DB->createTable($test_table, $schema)
- ) {
- $e = ['Unable to create %s table', $test_table];
- }
- if (
- null === $e
- && false === $this->c->DB->addField($test_table, 'test_field', 'VARCHAR(80)', false, '')
- ) {
- $e = ['Unable to add test_field field to %s table', $test_table];
- }
- $sql = "INSERT INTO ::{$test_table} (test_field) VALUES ('TEST_VALUE')";
- if (
- null === $e
- && false === $this->c->DB->exec($sql)
- ) {
- $e = ['Unable to insert line to %s table', $test_table];
- }
- if (
- null === $e
- && false === $this->c->DB->dropField($test_table, 'test_field')
- ) {
- $e = ['Unable to drop test_field field from %s table', $test_table];
- }
- if (
- null === $e
- && false === $this->c->DB->dropTable($test_table)
- ) {
- $e = ['Unable to drop %s table', $test_table];
- }
- }
- 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, $dbpass)
- {
- if (\substr($this->c->DB_DSN, 0, 6) === 'sqlite') {
- if (! \hash_equals($this->c->DB_DSN, "sqlite:{$dbpass}")) { // ????
- $v->addError(['Invalid file error', self::CONFIG_FILE]);
- }
- } else {
- if (! \hash_equals($this->c->DB_PASSWORD, $dbpass)) {
- $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',
- 'required' => true,
- ],
- 'o_maintenance_message' => [
- 'type' => 'textarea',
- 'value' => $v ? $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);
- return null;
- }
- }
|