3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use Shaarli\Container\ShaarliContainer
;
8 use Shaarli\Front\Exception\AlreadyInstalledException
;
9 use Shaarli\Front\Exception\ResourcePermissionException
;
10 use Shaarli\Helper\ApplicationUtils
;
11 use Shaarli\Languages
;
12 use Shaarli\Security\SessionManager
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
17 * Slim controller used to render install page, and create initial configuration file.
19 class InstallController
extends ShaarliVisitorController
21 public const SESSION_TEST_KEY
= 'session_tested';
22 public const SESSION_TEST_VALUE
= 'Working';
24 public function __construct(ShaarliContainer
$container)
26 parent
::__construct($container);
28 if (is_file($this->container
->conf
->getConfigFileExt())) {
29 throw new AlreadyInstalledException();
34 * Display the install template page.
35 * Also test file permissions and sessions beforehand.
37 public function index(Request
$request, Response
$response): Response
39 // Before installation, we'll make sure that permissions are set properly, and sessions are working.
40 $this->checkPermissions();
43 static::SESSION_TEST_VALUE
44 !== $this->container
->sessionManager
->getSessionParameter(static::SESSION_TEST_KEY
)
46 $this->container
->sessionManager
->setSessionParameter(static::SESSION_TEST_KEY
, static::SESSION_TEST_VALUE
);
48 return $this->redirect($response, '/install/session-test');
51 [$continents, $cities] = generateTimeZoneData(timezone_identifiers_list(), date_default_timezone_get());
53 $this->assignView('continents', $continents);
54 $this->assignView('cities', $cities);
55 $this->assignView('languages', Languages
::getAvailableLanguages());
57 $phpEol = new \
DateTimeImmutable(ApplicationUtils
::getPhpEol(PHP_VERSION
));
59 $this->assignView('php_version', PHP_VERSION
);
60 $this->assignView('php_eol', format_date($phpEol, false));
61 $this->assignView('php_has_reached_eol', $phpEol < new \
DateTimeImmutable());
62 $this->assignView('php_extensions', ApplicationUtils
::getPhpExtensionsRequirement());
63 $this->assignView('permissions', ApplicationUtils
::checkResourcePermissions($this->container
->conf
));
65 $this->assignView('pagetitle', t('Install Shaarli'));
67 return $response->write($this->render('install'));
71 * Route checking that the session parameter has been properly saved between two distinct requests.
72 * If the session parameter is preserved, redirect to install template page, otherwise displays error.
74 public function sessionTest(Request
$request, Response
$response): Response
76 // This part makes sure sessions works correctly.
77 // (Because on some hosts, session.save_path may not be set correctly,
78 // or we may not have write access to it.)
80 static::SESSION_TEST_VALUE
81 !== $this->container
->sessionManager
->getSessionParameter(static::SESSION_TEST_KEY
)
83 // Step 2: Check if data in session is correct.
85 '<pre>Sessions do not seem to work correctly on your server.<br>' .
86 'Make sure the variable "session.save_path" is set correctly in your PHP config, ' .
87 'and that you have write access to it.<br>' .
88 'It currently points to %s.<br>' .
89 'On some browsers, accessing your server via a hostname like \'localhost\' ' .
90 'or any custom hostname without a dot causes cookie storage to fail. ' .
91 'We recommend accessing your server via it\'s IP address or Fully Qualified Domain Name.<br>'
93 $msg = sprintf($msg, $this->container
->sessionManager
->getSavePath());
95 $this->assignView('message', $msg);
97 return $response->write($this->render('error'));
100 return $this->redirect($response, '/install');
104 * Save installation form and initialize config file and datastore if necessary.
106 public function save(Request
$request, Response
$response): Response
110 !empty($request->getParam('continent'))
111 && !empty($request->getParam('city'))
112 && isTimeZoneValid($request->getParam('continent'), $request->getParam('city'))
114 $timezone = $request->getParam('continent') . '/' . $request->getParam('city');
116 $this->container
->conf
->set('general.timezone', $timezone);
118 $login = $request->getParam('setlogin');
119 $this->container
->conf
->set('credentials.login', $login);
120 $salt = sha1(uniqid('', true) . '_' . mt_rand());
121 $this->container
->conf
->set('credentials.salt', $salt);
122 $this->container
->conf
->set('credentials.hash', sha1($request->getParam('setpassword') . $login . $salt));
124 if (!empty($request->getParam('title'))) {
125 $this->container
->conf
->set('general.title', escape($request->getParam('title')));
127 $this->container
->conf
->set(
129 'Shared bookmarks on ' . escape(index_url($this->container
->environment
))
133 $this->container
->conf
->set('translation.language', escape($request->getParam('language')));
134 $this->container
->conf
->set('updates.check_updates', !empty($request->getParam('updateCheck')));
135 $this->container
->conf
->set('api.enabled', !empty($request->getParam('enableApi')));
136 $this->container
->conf
->set(
139 $this->container
->conf
->get('credentials.login'),
140 $this->container
->conf
->get('credentials.salt')
143 $this->container
->conf
->set('general.header_link', $this->container
->basePath
. '/');
146 // Everything is ok, let's create config file.
147 $this->container
->conf
->write($this->container
->loginManager
->isLoggedIn());
148 } catch (\Exception
$e) {
149 $this->assignView('message', t('Error while writing config file after configuration update.'));
150 $this->assignView('stacktrace', $e->getMessage() . PHP_EOL
. $e->getTraceAsString());
152 return $response->write($this->render('error'));
155 $this->container
->sessionManager
->setSessionParameter(
156 SessionManager
::KEY_SUCCESS_MESSAGES
,
157 [t('Shaarli is now configured. Please login and start shaaring your bookmarks!')]
160 return $this->redirect($response, '/login');
163 protected function checkPermissions(): bool
165 // Ensure Shaarli has proper access to its resources
166 $errors = ApplicationUtils
::checkResourcePermissions($this->container
->conf
, true);
167 if (empty($errors)) {
171 $message = t('Insufficient permissions:') . PHP_EOL
;
172 foreach ($errors as $error) {
173 $message .= PHP_EOL
. $error;
176 throw new ResourcePermissionException($message);