]>
Commit | Line | Data |
---|---|---|
2e28269b | 1 | <?php |
53054b2b | 2 | |
c2cd15da | 3 | namespace Shaarli\Helper; |
9778a155 V |
4 | |
5 | use Exception; | |
6 | use Shaarli\Config\ConfigManager; | |
7 | ||
2e28269b V |
8 | /** |
9 | * Shaarli (application) utilities | |
10 | */ | |
11 | class ApplicationUtils | |
12 | { | |
b786c883 A |
13 | /** |
14 | * @var string File containing the current version | |
15 | */ | |
16 | public static $VERSION_FILE = 'shaarli_version.php'; | |
17 | ||
0cf76ccb A |
18 | public static $GITHUB_URL = 'https://github.com/shaarli/Shaarli'; |
19 | public static $GIT_RAW_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli'; | |
53054b2b | 20 | public static $GIT_BRANCHES = ['latest', 'stable']; |
4bf35ba5 V |
21 | private static $VERSION_START_TAG = '<?php /* '; |
22 | private static $VERSION_END_TAG = ' */ ?>'; | |
23 | ||
24 | /** | |
25 | * Gets the latest version code from the Git repository | |
26 | * | |
27 | * The code is read from the raw content of the version file on the Git server. | |
28 | * | |
7af9a418 A |
29 | * @param string $url URL to reach to get the latest version. |
30 | * @param int $timeout Timeout to check the URL (in seconds). | |
31 | * | |
4bf35ba5 V |
32 | * @return mixed the version code from the repository if available, else 'false' |
33 | */ | |
f211e417 | 34 | public static function getLatestGitVersionCode($url, $timeout = 2) |
4bf35ba5 | 35 | { |
1557cefb | 36 | list($headers, $data) = get_http_response($url, $timeout); |
4bf35ba5 V |
37 | |
38 | if (strpos($headers[0], '200 OK') === false) { | |
39 | error_log('Failed to retrieve ' . $url); | |
40 | return false; | |
41 | } | |
42 | ||
b786c883 A |
43 | return $data; |
44 | } | |
45 | ||
46 | /** | |
47 | * Retrieve the version from a remote URL or a file. | |
48 | * | |
49 | * @param string $remote URL or file to fetch. | |
50 | * @param int $timeout For URLs fetching. | |
51 | * | |
52 | * @return bool|string The version or false if it couldn't be retrieved. | |
53 | */ | |
54 | public static function getVersion($remote, $timeout = 2) | |
55 | { | |
56 | if (startsWith($remote, 'http')) { | |
57 | if (($data = static::getLatestGitVersionCode($remote, $timeout)) === false) { | |
58 | return false; | |
59 | } | |
60 | } else { | |
9778a155 | 61 | if (!is_file($remote)) { |
b786c883 A |
62 | return false; |
63 | } | |
64 | $data = file_get_contents($remote); | |
65 | } | |
66 | ||
4bf35ba5 | 67 | return str_replace( |
53054b2b A |
68 | [self::$VERSION_START_TAG, self::$VERSION_END_TAG, PHP_EOL], |
69 | ['', '', ''], | |
4bf35ba5 V |
70 | $data |
71 | ); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Checks if a new Shaarli version has been published on the Git repository | |
76 | * | |
77 | * Updates checks are run periodically, according to the following criteria: | |
78 | * - the update checks are enabled (install, global config); | |
79 | * - the user is logged in (or this is an open instance); | |
80 | * - the last check is older than a given interval; | |
81 | * - the check is non-blocking if the HTTPS connection to Git fails; | |
82 | * - in case of failure, the update file's modification date is updated, | |
83 | * to avoid intempestive connection attempts. | |
84 | * | |
85 | * @param string $currentVersion the current version code | |
86 | * @param string $updateFile the file where to store the latest version code | |
87 | * @param int $checkInterval the minimum interval between update checks (in seconds | |
88 | * @param bool $enableCheck whether to check for new versions | |
89 | * @param bool $isLoggedIn whether the user is logged in | |
7af9a418 | 90 | * @param string $branch check update for the given branch |
4bf35ba5 | 91 | * |
4a7af975 V |
92 | * @throws Exception an invalid branch has been set for update checks |
93 | * | |
4bf35ba5 V |
94 | * @return mixed the new version code if available and greater, else 'false' |
95 | */ | |
f211e417 V |
96 | public static function checkUpdate( |
97 | $currentVersion, | |
98 | $updateFile, | |
99 | $checkInterval, | |
100 | $enableCheck, | |
101 | $isLoggedIn, | |
102 | $branch = 'stable' | |
103 | ) { | |
b897c81f A |
104 | // Do not check versions for visitors |
105 | // Do not check if the user doesn't want to | |
106 | // Do not check with dev version | |
9778a155 | 107 | if (!$isLoggedIn || empty($enableCheck) || $currentVersion === 'dev') { |
4bf35ba5 V |
108 | return false; |
109 | } | |
110 | ||
111 | if (is_file($updateFile) && (filemtime($updateFile) > time() - $checkInterval)) { | |
112 | // Shaarli has checked for updates recently - skip HTTP query | |
113 | $latestKnownVersion = file_get_contents($updateFile); | |
114 | ||
115 | if (version_compare($latestKnownVersion, $currentVersion) == 1) { | |
116 | return $latestKnownVersion; | |
117 | } | |
118 | return false; | |
119 | } | |
120 | ||
9778a155 | 121 | if (!in_array($branch, self::$GIT_BRANCHES)) { |
4407b45f V |
122 | throw new Exception( |
123 | 'Invalid branch selected for updates: "' . $branch . '"' | |
124 | ); | |
125 | } | |
126 | ||
4bf35ba5 V |
127 | // Late Static Binding allows overriding within tests |
128 | // See http://php.net/manual/en/language.oop5.late-static-bindings.php | |
b786c883 | 129 | $latestVersion = static::getVersion( |
0cf76ccb | 130 | self::$GIT_RAW_URL . '/' . $branch . '/' . self::$VERSION_FILE |
4bf35ba5 V |
131 | ); |
132 | ||
9778a155 | 133 | if (!$latestVersion) { |
4bf35ba5 V |
134 | // Only update the file's modification date |
135 | file_put_contents($updateFile, $currentVersion); | |
136 | return false; | |
137 | } | |
138 | ||
139 | // Update the file's content and modification date | |
140 | file_put_contents($updateFile, $latestVersion); | |
141 | ||
142 | if (version_compare($latestVersion, $currentVersion) == 1) { | |
143 | return $latestVersion; | |
144 | } | |
145 | ||
146 | return false; | |
147 | } | |
2e28269b | 148 | |
c9cf2715 V |
149 | /** |
150 | * Checks the PHP version to ensure Shaarli can run | |
151 | * | |
152 | * @param string $minVersion minimum PHP required version | |
153 | * @param string $curVersion current PHP version (use PHP_VERSION) | |
154 | * | |
def39d0d A |
155 | * @return bool true on success |
156 | * | |
c9cf2715 V |
157 | * @throws Exception the PHP version is not supported |
158 | */ | |
159 | public static function checkPHPVersion($minVersion, $curVersion) | |
160 | { | |
161 | if (version_compare($curVersion, $minVersion) < 0) { | |
12266213 | 162 | $msg = t( |
c9cf2715 | 163 | 'Your PHP version is obsolete!' |
9778a155 V |
164 | . ' Shaarli requires at least PHP %s, and thus cannot run.' |
165 | . ' Your PHP version has known security vulnerabilities and should be' | |
166 | . ' updated as soon as possible.' | |
c9cf2715 | 167 | ); |
12266213 | 168 | throw new Exception(sprintf($msg, $minVersion)); |
c9cf2715 | 169 | } |
def39d0d | 170 | return true; |
c9cf2715 V |
171 | } |
172 | ||
2e28269b V |
173 | /** |
174 | * Checks Shaarli has the proper access permissions to its resources | |
175 | * | |
0cf76ccb A |
176 | * @param ConfigManager $conf Configuration Manager instance. |
177 | * @param bool $minimalMode In minimal mode we only check permissions to be able to display a template. | |
178 | * Currently we only need to be able to read the theme and write in raintpl cache. | |
278d9ee2 | 179 | * |
2e28269b V |
180 | * @return array A list of the detected configuration issues |
181 | */ | |
0cf76ccb | 182 | public static function checkResourcePermissions(ConfigManager $conf, bool $minimalMode = false): array |
2e28269b | 183 | { |
0cf76ccb | 184 | $errors = []; |
e4325b15 | 185 | $rainTplDir = rtrim($conf->get('resource.raintpl_tpl'), '/'); |
2e28269b V |
186 | |
187 | // Check script and template directories are readable | |
53054b2b A |
188 | foreach ( |
189 | [ | |
190 | 'application', | |
191 | 'inc', | |
192 | 'plugins', | |
193 | $rainTplDir, | |
194 | $rainTplDir . '/' . $conf->get('resource.theme'), | |
195 | ] as $path | |
196 | ) { | |
9778a155 V |
197 | if (!is_readable(realpath($path))) { |
198 | $errors[] = '"' . $path . '" ' . t('directory is not readable'); | |
2e28269b V |
199 | } |
200 | } | |
201 | ||
7af9a418 | 202 | // Check cache and data directories are readable and writable |
0cf76ccb A |
203 | if ($minimalMode) { |
204 | $folders = [ | |
205 | $conf->get('resource.raintpl_tmp'), | |
206 | ]; | |
207 | } else { | |
208 | $folders = [ | |
53054b2b A |
209 | $conf->get('resource.thumbnails_cache'), |
210 | $conf->get('resource.data_dir'), | |
211 | $conf->get('resource.page_cache'), | |
212 | $conf->get('resource.raintpl_tmp'), | |
0cf76ccb A |
213 | ]; |
214 | } | |
215 | ||
216 | foreach ($folders as $path) { | |
9778a155 V |
217 | if (!is_readable(realpath($path))) { |
218 | $errors[] = '"' . $path . '" ' . t('directory is not readable'); | |
2e28269b | 219 | } |
9778a155 V |
220 | if (!is_writable(realpath($path))) { |
221 | $errors[] = '"' . $path . '" ' . t('directory is not writable'); | |
2e28269b V |
222 | } |
223 | } | |
224 | ||
0cf76ccb A |
225 | if ($minimalMode) { |
226 | return $errors; | |
227 | } | |
228 | ||
7af9a418 | 229 | // Check configuration files are readable and writable |
53054b2b A |
230 | foreach ( |
231 | [ | |
232 | $conf->getConfigFileExt(), | |
233 | $conf->get('resource.datastore'), | |
234 | $conf->get('resource.ban_file'), | |
235 | $conf->get('resource.log'), | |
236 | $conf->get('resource.update_check'), | |
237 | ] as $path | |
238 | ) { | |
9778a155 | 239 | if (!is_file(realpath($path))) { |
2e28269b V |
240 | # the file may not exist yet |
241 | continue; | |
242 | } | |
243 | ||
9778a155 V |
244 | if (!is_readable(realpath($path))) { |
245 | $errors[] = '"' . $path . '" ' . t('file is not readable'); | |
2e28269b | 246 | } |
9778a155 V |
247 | if (!is_writable(realpath($path))) { |
248 | $errors[] = '"' . $path . '" ' . t('file is not writable'); | |
2e28269b V |
249 | } |
250 | } | |
251 | ||
252 | return $errors; | |
253 | } | |
bfe4f536 A |
254 | |
255 | /** | |
256 | * Returns a salted hash representing the current Shaarli version. | |
257 | * | |
258 | * Useful for assets browser cache. | |
259 | * | |
260 | * @param string $currentVersion of Shaarli | |
261 | * @param string $salt User personal salt, also used for the authentication | |
262 | * | |
263 | * @return string version hash | |
264 | */ | |
265 | public static function getVersionHash($currentVersion, $salt) | |
266 | { | |
267 | return hash_hmac('sha256', $currentVersion, $salt); | |
268 | } | |
0cf76ccb A |
269 | |
270 | /** | |
271 | * Get a list of PHP extensions used by Shaarli. | |
272 | * | |
273 | * @return array[] List of extension with following keys: | |
274 | * - name: extension name | |
275 | * - required: whether the extension is required to use Shaarli | |
276 | * - desc: short description of extension usage in Shaarli | |
277 | * - loaded: whether the extension is properly loaded or not | |
278 | */ | |
279 | public static function getPhpExtensionsRequirement(): array | |
280 | { | |
281 | $extensions = [ | |
282 | ['name' => 'json', 'required' => true, 'desc' => t('Configuration parsing')], | |
283 | ['name' => 'simplexml', 'required' => true, 'desc' => t('Slim Framework (routing, etc.)')], | |
284 | ['name' => 'mbstring', 'required' => true, 'desc' => t('Multibyte (Unicode) string support')], | |
285 | ['name' => 'gd', 'required' => false, 'desc' => t('Required to use thumbnails')], | |
286 | ['name' => 'intl', 'required' => false, 'desc' => t('Localized text sorting (e.g. e->รจ->f)')], | |
287 | ['name' => 'curl', 'required' => false, 'desc' => t('Better retrieval of bookmark metadata and thumbnail')], | |
288 | ['name' => 'gettext', 'required' => false, 'desc' => t('Use the translation system in gettext mode')], | |
289 | ['name' => 'ldap', 'required' => false, 'desc' => t('Login using LDAP server')], | |
290 | ]; | |
291 | ||
292 | foreach ($extensions as &$extension) { | |
293 | $extension['loaded'] = extension_loaded($extension['name']); | |
294 | } | |
295 | ||
296 | return $extensions; | |
297 | } | |
298 | ||
299 | /** | |
300 | * Return the EOL date of given PHP version. If the version is unknown, | |
301 | * we return today + 2 years. | |
302 | * | |
303 | * @param string $fullVersion PHP version, e.g. 7.4.7 | |
304 | * | |
305 | * @return string Date format: YYYY-MM-DD | |
306 | */ | |
307 | public static function getPhpEol(string $fullVersion): string | |
308 | { | |
309 | preg_match('/(\d+\.\d+)\.\d+/', $fullVersion, $matches); | |
310 | ||
311 | return [ | |
312 | '7.1' => '2019-12-01', | |
313 | '7.2' => '2020-11-30', | |
314 | '7.3' => '2021-12-06', | |
315 | '7.4' => '2022-11-28', | |
316 | '8.0' => '2023-12-01', | |
317 | ][$matches[1]] ?? (new \DateTime('+2 year'))->format('Y-m-d'); | |
318 | } | |
2e28269b | 319 | } |