3 namespace Shaarli\Helper
;
6 use malkusch\lock\exception\LockAcquireException
;
7 use malkusch\lock\mutex\FlockMutex
;
8 use Shaarli\Config\ConfigManager
;
11 * Shaarli (application) utilities
13 class ApplicationUtils
16 * @var string File containing the current version
18 public static $VERSION_FILE = 'shaarli_version.php';
20 public static $GITHUB_URL = 'https://github.com/shaarli/Shaarli';
21 public static $GIT_RAW_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli';
22 public static $GIT_BRANCHES = ['latest', 'stable'];
23 private static $VERSION_START_TAG = '<?php /* ';
24 private static $VERSION_END_TAG = ' */ ?>';
27 * Gets the latest version code from the Git repository
29 * The code is read from the raw content of the version file on the Git server.
31 * @param string $url URL to reach to get the latest version.
32 * @param int $timeout Timeout to check the URL (in seconds).
34 * @return mixed the version code from the repository if available, else 'false'
36 public static function getLatestGitVersionCode($url, $timeout = 2)
38 list($headers, $data) = get_http_response($url, $timeout);
40 if (strpos($headers[0], '200 OK') === false) {
41 error_log('Failed to retrieve ' . $url);
49 * Retrieve the version from a remote URL or a file.
51 * @param string $remote URL or file to fetch.
52 * @param int $timeout For URLs fetching.
54 * @return bool|string The version or false if it couldn't be retrieved.
56 public static function getVersion($remote, $timeout = 2)
58 if (startsWith($remote, 'http')) {
59 if (($data = static::getLatestGitVersionCode($remote, $timeout)) === false) {
63 if (!is_file($remote)) {
66 $data = file_get_contents($remote);
70 [self
::$VERSION_START_TAG, self
::$VERSION_END_TAG, PHP_EOL
],
77 * Checks if a new Shaarli version has been published on the Git repository
79 * Updates checks are run periodically, according to the following criteria:
80 * - the update checks are enabled (install, global config);
81 * - the user is logged in (or this is an open instance);
82 * - the last check is older than a given interval;
83 * - the check is non-blocking if the HTTPS connection to Git fails;
84 * - in case of failure, the update file's modification date is updated,
85 * to avoid intempestive connection attempts.
87 * @param string $currentVersion the current version code
88 * @param string $updateFile the file where to store the latest version code
89 * @param int $checkInterval the minimum interval between update checks (in seconds
90 * @param bool $enableCheck whether to check for new versions
91 * @param bool $isLoggedIn whether the user is logged in
92 * @param string $branch check update for the given branch
94 * @throws Exception an invalid branch has been set for update checks
96 * @return mixed the new version code if available and greater, else 'false'
98 public static function checkUpdate(
106 // Do not check versions for visitors
107 // Do not check if the user doesn't want to
108 // Do not check with dev version
109 if (!$isLoggedIn || empty($enableCheck) || $currentVersion === 'dev') {
113 if (is_file($updateFile) && (filemtime($updateFile) > time() - $checkInterval)) {
114 // Shaarli has checked for updates recently - skip HTTP query
115 $latestKnownVersion = file_get_contents($updateFile);
117 if (version_compare($latestKnownVersion, $currentVersion) == 1) {
118 return $latestKnownVersion;
123 if (!in_array($branch, self
::$GIT_BRANCHES)) {
125 'Invalid branch selected for updates: "' . $branch . '"'
129 // Late Static Binding allows overriding within tests
130 // See http://php.net/manual/en/language.oop5.late-static-bindings.php
131 $latestVersion = static::getVersion(
132 self
::$GIT_RAW_URL . '/' . $branch . '/' . self
::$VERSION_FILE
135 if (!$latestVersion) {
136 // Only update the file's modification date
137 file_put_contents($updateFile, $currentVersion);
141 // Update the file's content and modification date
142 file_put_contents($updateFile, $latestVersion);
144 if (version_compare($latestVersion, $currentVersion) == 1) {
145 return $latestVersion;
152 * Checks the PHP version to ensure Shaarli can run
154 * @param string $minVersion minimum PHP required version
155 * @param string $curVersion current PHP version (use PHP_VERSION)
157 * @return bool true on success
159 * @throws Exception the PHP version is not supported
161 public static function checkPHPVersion($minVersion, $curVersion)
163 if (version_compare($curVersion, $minVersion) < 0) {
165 'Your PHP version is obsolete!'
166 . ' Shaarli requires at least PHP %s, and thus cannot run.'
167 . ' Your PHP version has known security vulnerabilities and should be'
168 . ' updated as soon as possible.'
170 throw new Exception(sprintf($msg, $minVersion));
176 * Checks Shaarli has the proper access permissions to its resources
178 * @param ConfigManager $conf Configuration Manager instance.
179 * @param bool $minimalMode In minimal mode we only check permissions to be able to display a template.
180 * Currently we only need to be able to read the theme and write in raintpl cache.
182 * @return array A list of the detected configuration issues
184 public static function checkResourcePermissions(ConfigManager
$conf, bool $minimalMode = false): array
187 $rainTplDir = rtrim($conf->get('resource.raintpl_tpl'), '/');
189 // Check script and template directories are readable
196 $rainTplDir . '/' . $conf->get('resource.theme'),
199 if (!is_readable(realpath($path))) {
200 $errors[] = '"' . $path . '" ' . t('directory is not readable');
204 // Check cache and data directories are readable and writable
207 $conf->get('resource.raintpl_tmp'),
211 $conf->get('resource.thumbnails_cache'),
212 $conf->get('resource.data_dir'),
213 $conf->get('resource.page_cache'),
214 $conf->get('resource.raintpl_tmp'),
218 foreach ($folders as $path) {
219 if (!is_readable(realpath($path))) {
220 $errors[] = '"' . $path . '" ' . t('directory is not readable');
222 if (!is_writable(realpath($path))) {
223 $errors[] = '"' . $path . '" ' . t('directory is not writable');
231 // Check configuration files are readable and writable
234 $conf->getConfigFileExt(),
235 $conf->get('resource.datastore'),
236 $conf->get('resource.ban_file'),
237 $conf->get('resource.log'),
238 $conf->get('resource.update_check'),
241 if (!is_file(realpath($path))) {
242 # the file may not exist yet
246 if (!is_readable(realpath($path))) {
247 $errors[] = '"' . $path . '" ' . t('file is not readable');
249 if (!is_writable(realpath($path))) {
250 $errors[] = '"' . $path . '" ' . t('file is not writable');
257 public static function checkDatastoreMutex(): array
259 $mutex = new FlockMutex(fopen(SHAARLI_MUTEX_FILE
, 'r'), 2);
261 $mutex->synchronized(function () {
264 } catch (LockAcquireException
$e) {
265 $errors[] = t('Lock can not be acquired on the datastore. You might encounter concurrent access issues.');
268 return $errors ?? [];
272 * Returns a salted hash representing the current Shaarli version.
274 * Useful for assets browser cache.
276 * @param string $currentVersion of Shaarli
277 * @param string $salt User personal salt, also used for the authentication
279 * @return string version hash
281 public static function getVersionHash($currentVersion, $salt)
283 return hash_hmac('sha256', $currentVersion, $salt);
287 * Get a list of PHP extensions used by Shaarli.
289 * @return array[] List of extension with following keys:
290 * - name: extension name
291 * - required: whether the extension is required to use Shaarli
292 * - desc: short description of extension usage in Shaarli
293 * - loaded: whether the extension is properly loaded or not
295 public static function getPhpExtensionsRequirement(): array
298 ['name' => 'json', 'required' => true, 'desc' => t('Configuration parsing')],
299 ['name' => 'simplexml', 'required' => true, 'desc' => t('Slim Framework (routing, etc.)')],
300 ['name' => 'mbstring', 'required' => true, 'desc' => t('Multibyte (Unicode) string support')],
301 ['name' => 'gd', 'required' => false, 'desc' => t('Required to use thumbnails')],
302 ['name' => 'intl', 'required' => false, 'desc' => t('Localized text sorting (e.g. e->รจ->f)')],
303 ['name' => 'curl', 'required' => false, 'desc' => t('Better retrieval of bookmark metadata and thumbnail')],
304 ['name' => 'gettext', 'required' => false, 'desc' => t('Use the translation system in gettext mode')],
305 ['name' => 'ldap', 'required' => false, 'desc' => t('Login using LDAP server')],
308 foreach ($extensions as &$extension) {
309 $extension['loaded'] = extension_loaded($extension['name']);
316 * Return the EOL date of given PHP version. If the version is unknown,
317 * we return today + 2 years.
319 * @param string $fullVersion PHP version, e.g. 7.4.7
321 * @return string Date format: YYYY-MM-DD
323 public static function getPhpEol(string $fullVersion): string
325 preg_match('/(\d+\.\d+)\.\d+/', $fullVersion, $matches);
328 '7.1' => '2019-12-01',
329 '7.2' => '2020-11-30',
330 '7.3' => '2021-12-06',
331 '7.4' => '2022-11-28',
332 '8.0' => '2023-12-01',
333 ][$matches[1]] ?? (new \
DateTime('+2 year'))->format('Y-m-d');