]>
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($currentVersion,
96 // Do not check versions for visitors
97 // Do not check if the user doesn't want to
98 // Do not check with dev version
99 if (! $isLoggedIn || empty($enableCheck) || $currentVersion === 'dev') {
103 if (is_file($updateFile) && (filemtime($updateFile) > time() - $checkInterval)) {
104 // Shaarli has checked for updates recently - skip HTTP query
105 $latestKnownVersion = file_get_contents($updateFile);
107 if (version_compare($latestKnownVersion, $currentVersion) == 1) {
108 return $latestKnownVersion;
113 if (! in_array($branch, self
::$GIT_BRANCHES)) {
115 'Invalid branch selected for updates: "' . $branch . '"'
119 // Late Static Binding allows overriding within tests
120 // See http://php.net/manual/en/language.oop5.late-static-bindings.php
121 $latestVersion = static::getVersion(
122 self
::$GIT_URL . '/' . $branch . '/' . self
::$VERSION_FILE
125 if (! $latestVersion) {
126 // Only update the file's modification date
127 file_put_contents($updateFile, $currentVersion);
131 // Update the file's content and modification date
132 file_put_contents($updateFile, $latestVersion);
134 if (version_compare($latestVersion, $currentVersion) == 1) {
135 return $latestVersion;
142 * Checks the PHP version to ensure Shaarli can run
144 * @param string $minVersion minimum PHP required version
145 * @param string $curVersion current PHP version (use PHP_VERSION)
147 * @throws Exception the PHP version is not supported
149 public static function checkPHPVersion($minVersion, $curVersion)
151 if (version_compare($curVersion, $minVersion) < 0) {
153 'Your PHP version is obsolete!'
154 .' Shaarli requires at least PHP '.$minVersion.', and thus cannot run.'
155 .' Your PHP version has known security vulnerabilities and should be'
156 .' updated as soon as possible.'
162 * Checks Shaarli has the proper access permissions to its resources
164 * @param ConfigManager $conf Configuration Manager instance.
166 * @return array A list of the detected configuration issues
168 public static function checkResourcePermissions($conf)
172 // Check script and template directories are readable
177 $conf->get('resource.raintpl_tpl'),
178 $conf->get('resource.raintpl_tpl').'/'.$conf->get('resource.theme'),
180 if (! is_readable(realpath($path))) {
181 $errors[] = '"'.$path.'" directory is not readable';
185 // Check cache and data directories are readable and writable
187 $conf->get('resource.thumbnails_cache'),
188 $conf->get('resource.data_dir'),
189 $conf->get('resource.page_cache'),
190 $conf->get('resource.raintpl_tmp'),
192 if (! is_readable(realpath($path))) {
193 $errors[] = '"'.$path.'" directory is not readable';
195 if (! is_writable(realpath($path))) {
196 $errors[] = '"'.$path.'" directory is not writable';
200 // Check configuration files are readable and writable
202 $conf->getConfigFileExt(),
203 $conf->get('resource.datastore'),
204 $conf->get('resource.ban_file'),
205 $conf->get('resource.log'),
206 $conf->get('resource.update_check'),
208 if (! is_file(realpath($path))) {
209 # the file may not exist yet
213 if (! is_readable(realpath($path))) {
214 $errors[] = '"'.$path.'" file is not readable';
216 if (! is_writable(realpath($path))) {
217 $errors[] = '"'.$path.'" file is not writable';