diff --git a/app/modules/web/Controllers/ControllerBase.php b/app/modules/web/Controllers/ControllerBase.php index 49a87284..b25075fa 100644 --- a/app/modules/web/Controllers/ControllerBase.php +++ b/app/modules/web/Controllers/ControllerBase.php @@ -36,7 +36,6 @@ use SP\Core\Context\ContextInterface; use SP\Core\Context\SessionContext; use SP\Core\Events\EventDispatcher; use SP\Core\Exceptions\FileNotFoundException; -use SP\Core\Language; use SP\Core\UI\Theme; use SP\DataModel\ProfileData; use SP\Modules\Web\Controllers\Helpers\LayoutHelper; @@ -91,10 +90,6 @@ abstract class ControllerBase * @var EventDispatcher */ protected $eventDispatcher; - /** - * @var bool - */ - protected $loggedIn = false; /** * @var ConfigData */ @@ -163,8 +158,6 @@ abstract class ControllerBase $this->setViewVars(); } - $this->view->assign('language', substr(Language::$globalLang, 0, 2)); - if (method_exists($this, 'initialize')) { $this->initialize(); } diff --git a/app/modules/web/Controllers/ErrorController.php b/app/modules/web/Controllers/ErrorController.php index a9bda0d9..7fd26f6d 100644 --- a/app/modules/web/Controllers/ErrorController.php +++ b/app/modules/web/Controllers/ErrorController.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -24,11 +24,12 @@ namespace SP\Modules\Web\Controllers; +use DI\Container; use Klein\Klein; -use SP\Bootstrap; +use SP\Core\Exceptions\FileNotFoundException; +use SP\Core\Exceptions\SPException; +use SP\Modules\Web\Controllers\Helpers\LayoutHelper; use SP\Mvc\View\Template; -use SP\Services\Install\Installer; -use SP\Util\Util; /** * Class ErrorController @@ -45,36 +46,83 @@ class ErrorController * @var Klein */ protected $router; + /** + * @var LayoutHelper + */ + protected $layoutHelper; /** * ErrorController constructor. - * @param Template $view - * @param Klein $router + * + * @param Container $container + * @param string $actionName + * @throws \DI\DependencyException + * @throws \DI\NotFoundException */ - public function __construct(Template $view, Klein $router) + public function __construct(Container $container, $actionName) { - $this->view = $view; - $this->router = $router; + $this->view = $container->get(Template::class); + $this->view->setBase('error'); + + $this->router = $container->get(Klein::class); + $this->layoutHelper = $container->get(LayoutHelper::class); + $this->layoutHelper->getPublicLayout('error'); } /** - * @todo + * indexAction */ public function indexAction() { - $this->view->assign('startTime', microtime()); + $this->layoutHelper->getPublicLayout('error'); + $this->view(); + } - $this->view->assign('appInfo', Util::getAppInfo()); - $this->view->assign('appVersion', Installer::VERSION_TEXT); - $this->view->assign('logoIcon', Bootstrap::$WEBURI . '/public/images/logo_icon.png'); - $this->view->assign('logoNoText', Bootstrap::$WEBURI . '/public/images/logo_icon.svg'); - $this->view->assign('logo', Bootstrap::$WEBURI . '/public/images/logo_full_bg.png'); - $this->view->assign('logonobg', Bootstrap::$WEBURI . '/public/images/logo_full_nobg.png'); - $this->view->assign('lang', 'en'); - $this->view->assign('error', 'Error!'); + /** + * Mostrar los datos de la plantilla + */ + protected function view() + { + try { + echo $this->view->render(); + } catch (FileNotFoundException $e) { + processException($e); - $this->router->response()->header('Content-Type', 'text/html; charset=UTF-8'); - $this->router->response()->header('Cache-Control', 'public, no-cache, max-age=0, must-revalidate'); - $this->router->response()->header('Pragma', 'public; max-age=0'); + echo __($e->getMessage()); + } + + die(); + } + + /** + * databaseErrorAction + */ + public function maintenanceErrorAction() + { + $this->layoutHelper->getPublicLayout('error-maintenance'); + + $this->view->append('errors', [ + 'type' => SPException::WARNING, + 'description' => __('Aplicación en mantenimiento'), + 'hint' => __('En breve estará operativa') + ]); + + $this->view(); + } + + /** + * databaseErrorAction + */ + public function databaseErrorAction() + { + $this->layoutHelper->getPublicLayout('error-database'); + + $this->view->append('errors', [ + 'type' => SPException::CRITICAL, + 'description' => __('Error en la verificación de la base de datos'), + 'hint' => __('Consulte con el administrador') + ]); + + $this->view(); } } \ No newline at end of file diff --git a/app/modules/web/Controllers/Helpers/LayoutHelper.php b/app/modules/web/Controllers/Helpers/LayoutHelper.php index 584fd682..4c972b7c 100644 --- a/app/modules/web/Controllers/Helpers/LayoutHelper.php +++ b/app/modules/web/Controllers/Helpers/LayoutHelper.php @@ -112,7 +112,7 @@ class LayoutHelper extends HelperBase $this->loggedIn = $this->context->isLoggedIn(); $this->view->assign('loggedIn', $this->loggedIn); - $this->view->assign('lang', $this->loggedIn ? Language::$userLang : Language::$globalLang); + $this->view->assign('lang', $this->loggedIn ? Language::$userLang : substr(Language::$globalLang, 0, 2)); $this->view->assign('loadApp', $this->context->getAuthCompleted()); @@ -340,23 +340,6 @@ class LayoutHelper extends HelperBase return $this; } - /** - * Sets a full layout page - * - * @param string $page Page/view name - * @return LayoutHelper - */ - public function getErrorLayout($page = '') - { - $this->view->addTemplate('error', '_layouts'); - $this->view->assign('useFixedHeader'); - - $this->setPage($page); - $this->initBody(); - - return $this; - } - /** * Sets a custom layout page * diff --git a/app/modules/web/Controllers/InstallController.php b/app/modules/web/Controllers/InstallController.php index c6e826e5..865d796d 100644 --- a/app/modules/web/Controllers/InstallController.php +++ b/app/modules/web/Controllers/InstallController.php @@ -119,7 +119,7 @@ class InstallController extends ControllerBase try { $this->dic->get(Installer::class)->run($installData); - $this->returnJsonResponse(JsonResponse::JSON_SUCCESS_STICKY, __u('Instalación finalizada')); + $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Instalación finalizada')); } catch (\Exception $e) { processException($e); diff --git a/app/modules/web/Init.php b/app/modules/web/Init.php index 341223b3..0bd05800 100644 --- a/app/modules/web/Init.php +++ b/app/modules/web/Init.php @@ -33,7 +33,6 @@ use SP\Core\Context\SessionContext; use SP\Core\Crypt\CryptSessionHandler; use SP\Core\Crypt\SecureKeyCookie; use SP\Core\Crypt\Session as CryptSession; -use SP\Core\Exceptions\InitializationException; use SP\Core\Language; use SP\Core\ModuleBase; use SP\Core\UI\Theme; @@ -57,7 +56,7 @@ class Init extends ModuleBase * List of controllers that don't need to perform fully initialization * like: install/database checks, session/event handlers initialization */ - const PARTIAL_INIT = ['resource', 'install', 'bootstrap', 'status', 'upgrade']; + const PARTIAL_INIT = ['resource', 'install', 'bootstrap', 'status', 'upgrade', 'error']; /** * @var SessionContext @@ -92,7 +91,6 @@ class Init extends ModuleBase * Initialize Web App * * @param string $controller - * @throws InitializationException * @throws \DI\DependencyException * @throws \DI\NotFoundException * @throws \SP\Core\Exceptions\SPException @@ -144,19 +142,17 @@ class Init extends ModuleBase } // Checks if maintenance mode is turned on - $this->checkMaintenanceMode($this->context); + if ($this->checkMaintenanceMode($this->context)) { + $this->router->response() + ->redirect('index.php?r=error/maintenanceError') + ->send(); + } - try { - // Checks if the database is set up - DBUtil::checkDatabaseExist($this->container->get(Database::class)->getDbHandler(), $this->configData->getDbName()); - } catch (\Exception $e) { - if ($e->getCode() === 1049) { - $this->router->response() - ->redirect('index.php?r=install/index') - ->send(); - } - - throw new InitializationException($e->getMessage()); + // Checks if the database is set up + if (!DBUtil::checkDatabaseExist($this->container->get(Database::class)->getDbHandler(), $this->configData->getDbName())) { + $this->router->response() + ->redirect('index.php?r=error/databaseError') + ->send(); } // Checks if upgrade is needed diff --git a/app/modules/web/themes/material-blue/views/_layouts/error.inc b/app/modules/web/themes/material-blue/views/_layouts/error.inc deleted file mode 100644 index 8610c2fc..00000000 --- a/app/modules/web/themes/material-blue/views/_layouts/error.inc +++ /dev/null @@ -1,47 +0,0 @@ - - - - - <?php echo $appInfo['appname'], ' :: ', $appInfo['appdesc']; ?> - - - - - - - - -
- - -
-
-
-
- -
-
-
-
- - - - -
-
-
-
- includePartial('error'); ?> -
-
-
-
- - includePartial('footer'); ?> -
- - - diff --git a/app/modules/web/themes/material-blue/views/_layouts/main.inc b/app/modules/web/themes/material-blue/views/_layouts/main.inc index a50d14ca..4aba3b5a 100644 --- a/app/modules/web/themes/material-blue/views/_layouts/main.inc +++ b/app/modules/web/themes/material-blue/views/_layouts/main.inc @@ -1,6 +1,6 @@ - + <?php echo $appInfo['appname'], ' :: ', $appInfo['appdesc']; ?> diff --git a/app/modules/web/themes/material-blue/views/_partials/body-content.inc b/app/modules/web/themes/material-blue/views/_partials/body-content.inc deleted file mode 100644 index ee5c0596..00000000 --- a/app/modules/web/themes/material-blue/views/_partials/body-content.inc +++ /dev/null @@ -1,3 +0,0 @@ -
-
-
\ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/_partials/body-end.inc b/app/modules/web/themes/material-blue/views/_partials/body-end.inc deleted file mode 100644 index db0b3d5d..00000000 --- a/app/modules/web/themes/material-blue/views/_partials/body-end.inc +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - diff --git a/app/modules/web/themes/material-blue/views/_partials/body-footer.inc b/app/modules/web/themes/material-blue/views/_partials/body-footer.inc deleted file mode 100644 index 0cde9a0d..00000000 --- a/app/modules/web/themes/material-blue/views/_partials/body-footer.inc +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/_partials/body-header-menu.inc b/app/modules/web/themes/material-blue/views/_partials/body-header-menu.inc deleted file mode 100644 index 42ad8877..00000000 --- a/app/modules/web/themes/material-blue/views/_partials/body-header-menu.inc +++ /dev/null @@ -1,164 +0,0 @@ -
-
- - - - - - - -
- - - -
-
- - -
- - -
- \ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/_partials/body-header.inc b/app/modules/web/themes/material-blue/views/_partials/body-header.inc deleted file mode 100644 index 36416469..00000000 --- a/app/modules/web/themes/material-blue/views/_partials/body-header.inc +++ /dev/null @@ -1,6 +0,0 @@ -
-
- - -
-
\ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/_partials/body-start.inc b/app/modules/web/themes/material-blue/views/_partials/body-start.inc deleted file mode 100644 index adfffee0..00000000 --- a/app/modules/web/themes/material-blue/views/_partials/body-start.inc +++ /dev/null @@ -1,13 +0,0 @@ - -
- -
-
-
-
-
- -
- \ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/_partials/error.inc b/app/modules/web/themes/material-blue/views/_partials/error.inc index f9732cf9..8af93bb3 100644 --- a/app/modules/web/themes/material-blue/views/_partials/error.inc +++ b/app/modules/web/themes/material-blue/views/_partials/error.inc @@ -1,40 +1,19 @@ . - */ - -/** - * @var $icons \SP\Core\UI\ThemeIcons - * @var \SP\Mvc\View\Template $this + * @var \SP\Core\UI\ThemeIcons $icons + * @var \SP\Mvc\View\Template $this */ ?>
includePartial('error-list'); ?> -
- -
+ +
+ +
+
diff --git a/app/modules/web/themes/material-blue/views/_partials/header.inc b/app/modules/web/themes/material-blue/views/_partials/header.inc deleted file mode 100644 index c4eeeebb..00000000 --- a/app/modules/web/themes/material-blue/views/_partials/header.inc +++ /dev/null @@ -1,11 +0,0 @@ - - - - <?php echo $appInfo['appname'], ' :: ', $appInfo['appdesc']; ?> - - - - - - - diff --git a/app/modules/web/themes/material-blue/views/_partials/sessionbar.inc b/app/modules/web/themes/material-blue/views/_partials/sessionbar.inc deleted file mode 100644 index e69de29b..00000000 diff --git a/app/modules/web/themes/material-blue/views/error/error-database.inc b/app/modules/web/themes/material-blue/views/error/error-database.inc new file mode 100644 index 00000000..f243053d --- /dev/null +++ b/app/modules/web/themes/material-blue/views/error/error-database.inc @@ -0,0 +1,22 @@ + +
+ includePartial('error-list'); ?> + +
+ + + + getIconPlay()->getIcon(); ?> + +
+
\ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/error/error-maintenance.inc b/app/modules/web/themes/material-blue/views/error/error-maintenance.inc new file mode 100644 index 00000000..25df5e53 --- /dev/null +++ b/app/modules/web/themes/material-blue/views/error/error-maintenance.inc @@ -0,0 +1,17 @@ + +
+ includePartial('error-list'); ?> + +
+ +
+
\ No newline at end of file diff --git a/lib/SP/Core/ModuleBase.php b/lib/SP/Core/ModuleBase.php index 9b60a5ae..beecd374 100644 --- a/lib/SP/Core/ModuleBase.php +++ b/lib/SP/Core/ModuleBase.php @@ -30,8 +30,6 @@ use SP\Bootstrap; use SP\Config\Config; use SP\Core\Context\ContextInterface; use SP\Core\Events\EventDispatcher; -use SP\Core\Exceptions\InitializationException; -use SP\Http\Request; use SP\Providers\Log\LogHandler; use SP\Providers\Mail\MailHandler; use SP\Providers\Notification\NotificationHandler; @@ -89,28 +87,22 @@ abstract class ModuleBase * Devuelve un error 503 y un reintento de 120s al cliente. * * @param ContextInterface $context - * @param bool $check sólo comprobar si está activado el modo - * @throws InitializationException + * @return bool */ - public function checkMaintenanceMode(ContextInterface $context, $check = false) + public function checkMaintenanceMode(ContextInterface $context) { if ($this->configData->isMaintenance()) { Bootstrap::$LOCK = Util::getAppLock(); - if ($check === true - || Checks::isAjax($this->router) - || Request::analyzeInt('nodbupgrade') === 1 - || (Bootstrap::$LOCK !== false && Bootstrap::$LOCK->userId > 0 && $context->isLoggedIn() && Bootstrap::$LOCK->userId === $context->getUserData()->getId()) - ) { - return; - } - - throw new InitializationException( - __u('Aplicación en mantenimiento'), - InitializationException::INFO, - __u('En breve estará operativa') - ); + return (Checks::isAjax($this->router) + || (Bootstrap::$LOCK !== false + && Bootstrap::$LOCK->userId > 0 + && $context->isLoggedIn() + && Bootstrap::$LOCK->userId === $context->getUserData()->getId()) + ) === false; } + + return false; } /** diff --git a/lib/SP/Services/Install/Installer.php b/lib/SP/Services/Install/Installer.php index e3b6d1a8..03d88935 100644 --- a/lib/SP/Services/Install/Installer.php +++ b/lib/SP/Services/Install/Installer.php @@ -188,8 +188,6 @@ class Installer extends Service $this->configService->create(new \SP\DataModel\ConfigData('version', $version)); $this->configData->setInstalled(true); - $this->configData->setDatabaseVersion($version); - $this->configData->setConfigVersion($version); $this->config->saveConfig($this->configData, false); } @@ -229,7 +227,11 @@ class Installer extends Service { // Generate a random salt that is used to salt the local user passwords $this->configData->setPasswordSalt(Util::generateRandomBytes(30)); + + // Sets version and remove upgrade key $this->configData->setConfigVersion(Util::getVersionStringNormalized()); + $this->configData->setDatabaseVersion(Util::getVersionStringNormalized()); + $this->configData->setUpgradeKey(null); // Set DB connection info $this->configData->setDbHost($this->installData->getDbHost()); diff --git a/lib/SP/Services/Install/MySQL.php b/lib/SP/Services/Install/MySQL.php index 742a7f13..9005e450 100644 --- a/lib/SP/Services/Install/MySQL.php +++ b/lib/SP/Services/Install/MySQL.php @@ -195,11 +195,11 @@ class MySQL implements DatabaseSetupInterface $query = /** @lang SQL */ 'GRANT ALL PRIVILEGES ON `' . $this->installData->getDbName() . '`.* - TO `' . $this->installData->getDbUser() . '`@`' . $this->installData->getDbAuthHost() . '`'; + TO `' . $this->configData->getDbUser() . '`@`' . $this->installData->getDbAuthHost() . '`'; $queryDns = /** @lang SQL */ 'GRANT ALL PRIVILEGES ON `' . $this->installData->getDbName() . '`.* - TO `' . $this->installData->getDbUser() . '`@`' . $this->installData->getDbAuthHostDns() . '`'; + TO `' . $this->configData->getDbUser() . '`@`' . $this->installData->getDbAuthHostDns() . '`'; try { $dbc->exec($query); @@ -257,8 +257,8 @@ class MySQL implements DatabaseSetupInterface } } else { $dbc->exec('DROP DATABASE IF EXISTS `' . $this->installData->getDbName() . '`'); - $dbc->exec('DROP USER `' . $this->installData->getDbUser() . '`@`' . $this->installData->getDbAuthHost() . '`'); - $dbc->exec('DROP USER `' . $this->installData->getDbUser() . '`@`' . $this->installData->getDbAuthHostDns() . '`'); + $dbc->exec('DROP USER `' . $this->configData->getDbUser() . '`@`' . $this->installData->getDbAuthHost() . '`'); + $dbc->exec('DROP USER `' . $this->configData->getDbUser() . '`@`' . $this->installData->getDbAuthHostDns() . '`'); // $this->DB->exec('DROP USER `' . $this->InstallData->getDbUser() . '`@`%`'); } diff --git a/lib/SP/Services/User/UserPassService.php b/lib/SP/Services/User/UserPassService.php index 0288318b..31a57e9d 100644 --- a/lib/SP/Services/User/UserPassService.php +++ b/lib/SP/Services/User/UserPassService.php @@ -30,7 +30,6 @@ use SP\Core\Crypt\Crypt; use SP\Core\Crypt\Hash; use SP\Core\Crypt\Session as CryptSession; use SP\Core\Exceptions\SPException; -use SP\Core\Upgrade\Crypt as CryptUpgrade; use SP\Core\Upgrade\User as UpgradeUser; use SP\DataModel\UserLoginData; use SP\Repositories\User\UserRepository; @@ -117,6 +116,7 @@ class UserPassService extends Service } if ($userLoginResponse->getIsMigrate() === 1) { + // FIXME return UpgradeUser::upgradeMasterKey($userLoginData, $this) ? new UserPassResponse(self::MPASS_OK) : new UserPassResponse(self::MPASS_WRONG); } @@ -186,9 +186,7 @@ class UserPassService extends Service $this->configService->save('masterPwd', $configHashMPass); } - if (Hash::checkHashKey($userMPass, $configHashMPass) - || CryptUpgrade::migrateHash($userMPass) - ) { + if (Hash::checkHashKey($userMPass, $configHashMPass)) { $response = $this->createMasterPass($userMPass, $userLoginData->getLoginUser(), $userLoginData->getLoginPass()); $this->userRepository->updateMasterPassById($userData->getId(), $response->getCryptMasterPass(), $response->getCryptSecuredKey()); diff --git a/lib/SP/Storage/DBUtil.php b/lib/SP/Storage/DBUtil.php index 5262cae0..bce48cab 100644 --- a/lib/SP/Storage/DBUtil.php +++ b/lib/SP/Storage/DBUtil.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link https://syspass.org + * @author nuxsmin + * @link https://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -24,7 +24,6 @@ namespace SP\Storage; -use RuntimeException; use SP\Core\Exceptions\SPException; /** @@ -126,17 +125,23 @@ class DBUtil public static function checkDatabaseExist(DBStorageInterface $DBStorage, $dbName) { try { + $tables = array_map(function ($value) { + return '\'' . $value . '\''; + }, self::$tables); + $query = /** @lang SQL */ 'SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = \'' . $dbName . '\' - AND `table_name` IN (\'Client\', \'Category\', \'Account\', \'User\', \'Config\', \'EventLog\')'; + AND `table_name` IN (' . implode(',', $tables) . ')'; - return (int)$DBStorage->getConnection()->query($query)->fetchColumn() === 6; + $numTables = $DBStorage->getConnection()->query($query)->fetchColumn(); + + return (int)$numTables === count(self::$tables); } catch (\Exception $e) { processException($e); - - throw new RuntimeException(__u('Error en la verificación de la base de datos')); } + + return false; } } \ No newline at end of file