]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/ApplicationUtils.php
a3b2dcb18effa309c6d776961913cfe6f1385de0
[github/shaarli/Shaarli.git] / application / ApplicationUtils.php
1 <?php
2 /**
3 * Shaarli (application) utilities
4 */
5 class ApplicationUtils
6 {
7 /**
8 * @var string File containing the current version
9 */
10 public static $VERSION_FILE = 'shaarli_version.php';
11
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 = ' */ ?>';
16
17 /**
18 * Gets the latest version code from the Git repository
19 *
20 * The code is read from the raw content of the version file on the Git server.
21 *
22 * @param string $url URL to reach to get the latest version.
23 * @param int $timeout Timeout to check the URL (in seconds).
24 *
25 * @return mixed the version code from the repository if available, else 'false'
26 */
27 public static function getLatestGitVersionCode($url, $timeout = 2)
28 {
29 list($headers, $data) = get_http_response($url, $timeout);
30
31 if (strpos($headers[0], '200 OK') === false) {
32 error_log('Failed to retrieve ' . $url);
33 return false;
34 }
35
36 return $data;
37 }
38
39 /**
40 * Retrieve the version from a remote URL or a file.
41 *
42 * @param string $remote URL or file to fetch.
43 * @param int $timeout For URLs fetching.
44 *
45 * @return bool|string The version or false if it couldn't be retrieved.
46 */
47 public static function getVersion($remote, $timeout = 2)
48 {
49 if (startsWith($remote, 'http')) {
50 if (($data = static::getLatestGitVersionCode($remote, $timeout)) === false) {
51 return false;
52 }
53 } else {
54 if (! is_file($remote)) {
55 return false;
56 }
57 $data = file_get_contents($remote);
58 }
59
60 return str_replace(
61 array(self::$VERSION_START_TAG, self::$VERSION_END_TAG, PHP_EOL),
62 array('', '', ''),
63 $data
64 );
65 }
66
67 /**
68 * Checks if a new Shaarli version has been published on the Git repository
69 *
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.
77 *
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
84 *
85 * @throws Exception an invalid branch has been set for update checks
86 *
87 * @return mixed the new version code if available and greater, else 'false'
88 */
89 public static function checkUpdate(
90 $currentVersion,
91 $updateFile,
92 $checkInterval,
93 $enableCheck,
94 $isLoggedIn,
95 $branch = 'stable'
96 ) {
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') {
101 return false;
102 }
103
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);
107
108 if (version_compare($latestKnownVersion, $currentVersion) == 1) {
109 return $latestKnownVersion;
110 }
111 return false;
112 }
113
114 if (! in_array($branch, self::$GIT_BRANCHES)) {
115 throw new Exception(
116 'Invalid branch selected for updates: "' . $branch . '"'
117 );
118 }
119
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
124 );
125
126 if (! $latestVersion) {
127 // Only update the file's modification date
128 file_put_contents($updateFile, $currentVersion);
129 return false;
130 }
131
132 // Update the file's content and modification date
133 file_put_contents($updateFile, $latestVersion);
134
135 if (version_compare($latestVersion, $currentVersion) == 1) {
136 return $latestVersion;
137 }
138
139 return false;
140 }
141
142 /**
143 * Checks the PHP version to ensure Shaarli can run
144 *
145 * @param string $minVersion minimum PHP required version
146 * @param string $curVersion current PHP version (use PHP_VERSION)
147 *
148 * @throws Exception the PHP version is not supported
149 */
150 public static function checkPHPVersion($minVersion, $curVersion)
151 {
152 if (version_compare($curVersion, $minVersion) < 0) {
153 $msg = t(
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.'
158 );
159 throw new Exception(sprintf($msg, $minVersion));
160 }
161 }
162
163 /**
164 * Checks Shaarli has the proper access permissions to its resources
165 *
166 * @param ConfigManager $conf Configuration Manager instance.
167 *
168 * @return array A list of the detected configuration issues
169 */
170 public static function checkResourcePermissions($conf)
171 {
172 $errors = array();
173 $rainTplDir = rtrim($conf->get('resource.raintpl_tpl'), '/');
174
175 // Check script and template directories are readable
176 foreach (array(
177 'application',
178 'inc',
179 'plugins',
180 $rainTplDir,
181 $rainTplDir.'/'.$conf->get('resource.theme'),
182 ) as $path) {
183 if (! is_readable(realpath($path))) {
184 $errors[] = '"'.$path.'" '. t('directory is not readable');
185 }
186 }
187
188 // Check cache and data directories are readable and writable
189 foreach (array(
190 $conf->get('resource.thumbnails_cache'),
191 $conf->get('resource.data_dir'),
192 $conf->get('resource.page_cache'),
193 $conf->get('resource.raintpl_tmp'),
194 ) as $path) {
195 if (! is_readable(realpath($path))) {
196 $errors[] = '"'.$path.'" '. t('directory is not readable');
197 }
198 if (! is_writable(realpath($path))) {
199 $errors[] = '"'.$path.'" '. t('directory is not writable');
200 }
201 }
202
203 // Check configuration files are readable and writable
204 foreach (array(
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'),
210 ) as $path) {
211 if (! is_file(realpath($path))) {
212 # the file may not exist yet
213 continue;
214 }
215
216 if (! is_readable(realpath($path))) {
217 $errors[] = '"'.$path.'" '. t('file is not readable');
218 }
219 if (! is_writable(realpath($path))) {
220 $errors[] = '"'.$path.'" '. t('file is not writable');
221 }
222 }
223
224 return $errors;
225 }
226
227 /**
228 * Returns a salted hash representing the current Shaarli version.
229 *
230 * Useful for assets browser cache.
231 *
232 * @param string $currentVersion of Shaarli
233 * @param string $salt User personal salt, also used for the authentication
234 *
235 * @return string version hash
236 */
237 public static function getVersionHash($currentVersion, $salt)
238 {
239 return hash_hmac('sha256', $currentVersion, $salt);
240 }
241 }