5 use Shaarli\Config\ConfigManager
;
8 * Shaarli (application) utilities
10 class ApplicationUtils
13 * @var string File containing the current version
15 public static $VERSION_FILE = 'shaarli_version.php';
17 private static $GIT_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli';
18 private static $GIT_BRANCHES = array('latest', 'stable');
19 private static $VERSION_START_TAG = '<?php /* ';
20 private static $VERSION_END_TAG = ' */ ?>';
23 * Gets the latest version code from the Git repository
25 * The code is read from the raw content of the version file on the Git server.
27 * @param string $url URL to reach to get the latest version.
28 * @param int $timeout Timeout to check the URL (in seconds).
30 * @return mixed the version code from the repository if available, else 'false'
32 public static function getLatestGitVersionCode($url, $timeout = 2)
34 list($headers, $data) = get_http_response($url, $timeout);
36 if (strpos($headers[0], '200 OK') === false) {
37 error_log('Failed to retrieve ' . $url);
45 * Retrieve the version from a remote URL or a file.
47 * @param string $remote URL or file to fetch.
48 * @param int $timeout For URLs fetching.
50 * @return bool|string The version or false if it couldn't be retrieved.
52 public static function getVersion($remote, $timeout = 2)
54 if (startsWith($remote, 'http')) {
55 if (($data = static::getLatestGitVersionCode($remote, $timeout)) === false) {
59 if (!is_file($remote)) {
62 $data = file_get_contents($remote);
66 array(self
::$VERSION_START_TAG, self
::$VERSION_END_TAG, PHP_EOL
),
73 * Checks if a new Shaarli version has been published on the Git repository
75 * Updates checks are run periodically, according to the following criteria:
76 * - the update checks are enabled (install, global config);
77 * - the user is logged in (or this is an open instance);
78 * - the last check is older than a given interval;
79 * - the check is non-blocking if the HTTPS connection to Git fails;
80 * - in case of failure, the update file's modification date is updated,
81 * to avoid intempestive connection attempts.
83 * @param string $currentVersion the current version code
84 * @param string $updateFile the file where to store the latest version code
85 * @param int $checkInterval the minimum interval between update checks (in seconds
86 * @param bool $enableCheck whether to check for new versions
87 * @param bool $isLoggedIn whether the user is logged in
88 * @param string $branch check update for the given branch
90 * @throws Exception an invalid branch has been set for update checks
92 * @return mixed the new version code if available and greater, else 'false'
94 public static function checkUpdate(
102 // Do not check versions for visitors
103 // Do not check if the user doesn't want to
104 // Do not check with dev version
105 if (!$isLoggedIn || empty($enableCheck) || $currentVersion === 'dev') {
109 if (is_file($updateFile) && (filemtime($updateFile) > time() - $checkInterval)) {
110 // Shaarli has checked for updates recently - skip HTTP query
111 $latestKnownVersion = file_get_contents($updateFile);
113 if (version_compare($latestKnownVersion, $currentVersion) == 1) {
114 return $latestKnownVersion;
119 if (!in_array($branch, self
::$GIT_BRANCHES)) {
121 'Invalid branch selected for updates: "' . $branch . '"'
125 // Late Static Binding allows overriding within tests
126 // See http://php.net/manual/en/language.oop5.late-static-bindings.php
127 $latestVersion = static::getVersion(
128 self
::$GIT_URL . '/' . $branch . '/' . self
::$VERSION_FILE
131 if (!$latestVersion) {
132 // Only update the file's modification date
133 file_put_contents($updateFile, $currentVersion);
137 // Update the file's content and modification date
138 file_put_contents($updateFile, $latestVersion);
140 if (version_compare($latestVersion, $currentVersion) == 1) {
141 return $latestVersion;
148 * Checks the PHP version to ensure Shaarli can run
150 * @param string $minVersion minimum PHP required version
151 * @param string $curVersion current PHP version (use PHP_VERSION)
153 * @return bool true on success
155 * @throws Exception the PHP version is not supported
157 public static function checkPHPVersion($minVersion, $curVersion)
159 if (version_compare($curVersion, $minVersion) < 0) {
161 'Your PHP version is obsolete!'
162 . ' Shaarli requires at least PHP %s, and thus cannot run.'
163 . ' Your PHP version has known security vulnerabilities and should be'
164 . ' updated as soon as possible.'
166 throw new Exception(sprintf($msg, $minVersion));
172 * Checks Shaarli has the proper access permissions to its resources
174 * @param ConfigManager $conf Configuration Manager instance.
176 * @return array A list of the detected configuration issues
178 public static function checkResourcePermissions($conf)
181 $rainTplDir = rtrim($conf->get('resource.raintpl_tpl'), '/');
183 // Check script and template directories are readable
189 $rainTplDir . '/' . $conf->get('resource.theme'),
191 if (!is_readable(realpath($path))) {
192 $errors[] = '"' . $path . '" ' . t('directory is not readable');
196 // Check cache and data directories are readable and writable
198 $conf->get('resource.thumbnails_cache'),
199 $conf->get('resource.data_dir'),
200 $conf->get('resource.page_cache'),
201 $conf->get('resource.raintpl_tmp'),
203 if (!is_readable(realpath($path))) {
204 $errors[] = '"' . $path . '" ' . t('directory is not readable');
206 if (!is_writable(realpath($path))) {
207 $errors[] = '"' . $path . '" ' . t('directory is not writable');
211 // Check configuration files are readable and writable
213 $conf->getConfigFileExt(),
214 $conf->get('resource.datastore'),
215 $conf->get('resource.ban_file'),
216 $conf->get('resource.log'),
217 $conf->get('resource.update_check'),
219 if (!is_file(realpath($path))) {
220 # the file may not exist yet
224 if (!is_readable(realpath($path))) {
225 $errors[] = '"' . $path . '" ' . t('file is not readable');
227 if (!is_writable(realpath($path))) {
228 $errors[] = '"' . $path . '" ' . t('file is not writable');
236 * Returns a salted hash representing the current Shaarli version.
238 * Useful for assets browser cache.
240 * @param string $currentVersion of Shaarli
241 * @param string $salt User personal salt, also used for the authentication
243 * @return string version hash
245 public static function getVersionHash($currentVersion, $salt)
247 return hash_hmac('sha256', $currentVersion, $salt);