]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/ApplicationUtils.php
3 * Shaarli (application) utilities
8 * @var string File containing the current version
10 public static $VERSION_FILE = 'shaarli_version.php';
12 private static $GIT_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli';
13 private static $GIT_BRANCHES = array('latest', 'stable');
14 private static $VERSION_START_TAG = '<?php /* ';
15 private static $VERSION_END_TAG = ' */ ?>';
18 * Gets the latest version code from the Git repository
20 * The code is read from the raw content of the version file on the Git server.
22 * @param string $url URL to reach to get the latest version.
23 * @param int $timeout Timeout to check the URL (in seconds).
25 * @return mixed the version code from the repository if available, else 'false'
27 public static function getLatestGitVersionCode($url, $timeout = 2)
29 list($headers, $data) = get_http_response($url, $timeout);
31 if (strpos($headers[0], '200 OK') === false) {
32 error_log('Failed to retrieve ' . $url);
40 * Retrieve the version from a remote URL or a file.
42 * @param string $remote URL or file to fetch.
43 * @param int $timeout For URLs fetching.
45 * @return bool|string The version or false if it couldn't be retrieved.
47 public static function getVersion($remote, $timeout = 2)
49 if (startsWith($remote, 'http')) {
50 if (($data = static::getLatestGitVersionCode($remote, $timeout)) === false) {
54 if (! is_file($remote)) {
57 $data = file_get_contents($remote);
61 array(self
::$VERSION_START_TAG, self
::$VERSION_END_TAG, PHP_EOL
),
68 * Checks if a new Shaarli version has been published on the Git repository
70 * Updates checks are run periodically, according to the following criteria:
71 * - the update checks are enabled (install, global config);
72 * - the user is logged in (or this is an open instance);
73 * - the last check is older than a given interval;
74 * - the check is non-blocking if the HTTPS connection to Git fails;
75 * - in case of failure, the update file's modification date is updated,
76 * to avoid intempestive connection attempts.
78 * @param string $currentVersion the current version code
79 * @param string $updateFile the file where to store the latest version code
80 * @param int $checkInterval the minimum interval between update checks (in seconds
81 * @param bool $enableCheck whether to check for new versions
82 * @param bool $isLoggedIn whether the user is logged in
83 * @param string $branch check update for the given branch
85 * @throws Exception an invalid branch has been set for update checks
87 * @return mixed the new version code if available and greater, else 'false'
89 public static function checkUpdate(
97 // Do not check versions for visitors
98 // Do not check if the user doesn't want to
99 // Do not check with dev version
100 if (! $isLoggedIn || empty($enableCheck) || $currentVersion === 'dev') {
104 if (is_file($updateFile) && (filemtime($updateFile) > time() - $checkInterval)) {
105 // Shaarli has checked for updates recently - skip HTTP query
106 $latestKnownVersion = file_get_contents($updateFile);
108 if (version_compare($latestKnownVersion, $currentVersion) == 1) {
109 return $latestKnownVersion;
114 if (! in_array($branch, self
::$GIT_BRANCHES)) {
116 'Invalid branch selected for updates: "' . $branch . '"'
120 // Late Static Binding allows overriding within tests
121 // See http://php.net/manual/en/language.oop5.late-static-bindings.php
122 $latestVersion = static::getVersion(
123 self
::$GIT_URL . '/' . $branch . '/' . self
::$VERSION_FILE
126 if (! $latestVersion) {
127 // Only update the file's modification date
128 file_put_contents($updateFile, $currentVersion);
132 // Update the file's content and modification date
133 file_put_contents($updateFile, $latestVersion);
135 if (version_compare($latestVersion, $currentVersion) == 1) {
136 return $latestVersion;
143 * Checks the PHP version to ensure Shaarli can run
145 * @param string $minVersion minimum PHP required version
146 * @param string $curVersion current PHP version (use PHP_VERSION)
148 * @throws Exception the PHP version is not supported
150 public static function checkPHPVersion($minVersion, $curVersion)
152 if (version_compare($curVersion, $minVersion) < 0) {
154 'Your PHP version is obsolete!'
155 . ' Shaarli requires at least PHP %s, and thus cannot run.'
156 . ' Your PHP version has known security vulnerabilities and should be'
157 . ' updated as soon as possible.'
159 throw new Exception(sprintf($msg, $minVersion));
164 * Checks Shaarli has the proper access permissions to its resources
166 * @param ConfigManager $conf Configuration Manager instance.
168 * @return array A list of the detected configuration issues
170 public static function checkResourcePermissions($conf)
173 $rainTplDir = rtrim($conf->get('resource.raintpl_tpl'), '/');
175 // Check script and template directories are readable
181 $rainTplDir.'/'.$conf->get('resource.theme'),
183 if (! is_readable(realpath($path))) {
184 $errors[] = '"'.$path.'" '. t('directory is not readable');
188 // Check cache and data directories are readable and writable
190 $conf->get('resource.thumbnails_cache'),
191 $conf->get('resource.data_dir'),
192 $conf->get('resource.page_cache'),
193 $conf->get('resource.raintpl_tmp'),
195 if (! is_readable(realpath($path))) {
196 $errors[] = '"'.$path.'" '. t('directory is not readable');
198 if (! is_writable(realpath($path))) {
199 $errors[] = '"'.$path.'" '. t('directory is not writable');
203 // Check configuration files are readable and writable
205 $conf->getConfigFileExt(),
206 $conf->get('resource.datastore'),
207 $conf->get('resource.ban_file'),
208 $conf->get('resource.log'),
209 $conf->get('resource.update_check'),
211 if (! is_file(realpath($path))) {
212 # the file may not exist yet
216 if (! is_readable(realpath($path))) {
217 $errors[] = '"'.$path.'" '. t('file is not readable');
219 if (! is_writable(realpath($path))) {
220 $errors[] = '"'.$path.'" '. t('file is not writable');
228 * Returns a salted hash representing the current Shaarli version.
230 * Useful for assets browser cache.
232 * @param string $currentVersion of Shaarli
233 * @param string $salt User personal salt, also used for the authentication
235 * @return string version hash
237 public static function getVersionHash($currentVersion, $salt)
239 return hash_hmac('sha256', $currentVersion, $salt);