]>
Commit | Line | Data |
---|---|---|
2e28269b V |
1 | <?php |
2 | /** | |
3 | * Shaarli (application) utilities | |
4 | */ | |
5 | class ApplicationUtils | |
6 | { | |
b786c883 A |
7 | /** |
8 | * @var string File containing the current version | |
9 | */ | |
10 | public static $VERSION_FILE = 'shaarli_version.php'; | |
11 | ||
4bf35ba5 | 12 | private static $GIT_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli'; |
b897c81f | 13 | private static $GIT_BRANCHES = array('latest', 'stable'); |
4bf35ba5 V |
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 | * | |
7af9a418 A |
22 | * @param string $url URL to reach to get the latest version. |
23 | * @param int $timeout Timeout to check the URL (in seconds). | |
24 | * | |
4bf35ba5 V |
25 | * @return mixed the version code from the repository if available, else 'false' |
26 | */ | |
27 | public static function getLatestGitVersionCode($url, $timeout=2) | |
28 | { | |
1557cefb | 29 | list($headers, $data) = get_http_response($url, $timeout); |
4bf35ba5 V |
30 | |
31 | if (strpos($headers[0], '200 OK') === false) { | |
32 | error_log('Failed to retrieve ' . $url); | |
33 | return false; | |
34 | } | |
35 | ||
b786c883 A |
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 | ||
4bf35ba5 V |
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 | |
7af9a418 | 83 | * @param string $branch check update for the given branch |
4bf35ba5 | 84 | * |
4a7af975 V |
85 | * @throws Exception an invalid branch has been set for update checks |
86 | * | |
4bf35ba5 V |
87 | * @return mixed the new version code if available and greater, else 'false' |
88 | */ | |
4407b45f V |
89 | public static function checkUpdate($currentVersion, |
90 | $updateFile, | |
91 | $checkInterval, | |
92 | $enableCheck, | |
93 | $isLoggedIn, | |
94 | $branch='stable') | |
4bf35ba5 | 95 | { |
b897c81f A |
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') { | |
4bf35ba5 V |
100 | return false; |
101 | } | |
102 | ||
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); | |
106 | ||
107 | if (version_compare($latestKnownVersion, $currentVersion) == 1) { | |
108 | return $latestKnownVersion; | |
109 | } | |
110 | return false; | |
111 | } | |
112 | ||
4407b45f V |
113 | if (! in_array($branch, self::$GIT_BRANCHES)) { |
114 | throw new Exception( | |
115 | 'Invalid branch selected for updates: "' . $branch . '"' | |
116 | ); | |
117 | } | |
118 | ||
4bf35ba5 V |
119 | // Late Static Binding allows overriding within tests |
120 | // See http://php.net/manual/en/language.oop5.late-static-bindings.php | |
b786c883 | 121 | $latestVersion = static::getVersion( |
4407b45f | 122 | self::$GIT_URL . '/' . $branch . '/' . self::$VERSION_FILE |
4bf35ba5 V |
123 | ); |
124 | ||
125 | if (! $latestVersion) { | |
126 | // Only update the file's modification date | |
127 | file_put_contents($updateFile, $currentVersion); | |
128 | return false; | |
129 | } | |
130 | ||
131 | // Update the file's content and modification date | |
132 | file_put_contents($updateFile, $latestVersion); | |
133 | ||
134 | if (version_compare($latestVersion, $currentVersion) == 1) { | |
135 | return $latestVersion; | |
136 | } | |
137 | ||
138 | return false; | |
139 | } | |
2e28269b | 140 | |
c9cf2715 V |
141 | /** |
142 | * Checks the PHP version to ensure Shaarli can run | |
143 | * | |
144 | * @param string $minVersion minimum PHP required version | |
145 | * @param string $curVersion current PHP version (use PHP_VERSION) | |
146 | * | |
147 | * @throws Exception the PHP version is not supported | |
148 | */ | |
149 | public static function checkPHPVersion($minVersion, $curVersion) | |
150 | { | |
151 | if (version_compare($curVersion, $minVersion) < 0) { | |
152 | throw new Exception( | |
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.' | |
157 | ); | |
158 | } | |
159 | } | |
160 | ||
2e28269b V |
161 | /** |
162 | * Checks Shaarli has the proper access permissions to its resources | |
163 | * | |
278d9ee2 A |
164 | * @param ConfigManager $conf Configuration Manager instance. |
165 | * | |
2e28269b V |
166 | * @return array A list of the detected configuration issues |
167 | */ | |
278d9ee2 | 168 | public static function checkResourcePermissions($conf) |
2e28269b V |
169 | { |
170 | $errors = array(); | |
171 | ||
172 | // Check script and template directories are readable | |
173 | foreach (array( | |
174 | 'application', | |
175 | 'inc', | |
176 | 'plugins', | |
894a3c4b | 177 | $conf->get('resource.raintpl_tpl'), |
adc4aee8 | 178 | $conf->get('resource.raintpl_tpl').'/'.$conf->get('resource.theme'), |
2e28269b V |
179 | ) as $path) { |
180 | if (! is_readable(realpath($path))) { | |
181 | $errors[] = '"'.$path.'" directory is not readable'; | |
182 | } | |
183 | } | |
184 | ||
7af9a418 | 185 | // Check cache and data directories are readable and writable |
2e28269b | 186 | foreach (array( |
894a3c4b A |
187 | $conf->get('resource.thumbnails_cache'), |
188 | $conf->get('resource.data_dir'), | |
189 | $conf->get('resource.page_cache'), | |
190 | $conf->get('resource.raintpl_tmp'), | |
2e28269b V |
191 | ) as $path) { |
192 | if (! is_readable(realpath($path))) { | |
193 | $errors[] = '"'.$path.'" directory is not readable'; | |
194 | } | |
195 | if (! is_writable(realpath($path))) { | |
196 | $errors[] = '"'.$path.'" directory is not writable'; | |
197 | } | |
198 | } | |
199 | ||
7af9a418 | 200 | // Check configuration files are readable and writable |
2e28269b | 201 | foreach (array( |
278d9ee2 | 202 | $conf->getConfigFileExt(), |
894a3c4b A |
203 | $conf->get('resource.datastore'), |
204 | $conf->get('resource.ban_file'), | |
205 | $conf->get('resource.log'), | |
206 | $conf->get('resource.update_check'), | |
2e28269b V |
207 | ) as $path) { |
208 | if (! is_file(realpath($path))) { | |
209 | # the file may not exist yet | |
210 | continue; | |
211 | } | |
212 | ||
213 | if (! is_readable(realpath($path))) { | |
214 | $errors[] = '"'.$path.'" file is not readable'; | |
215 | } | |
216 | if (! is_writable(realpath($path))) { | |
217 | $errors[] = '"'.$path.'" file is not writable'; | |
218 | } | |
219 | } | |
220 | ||
221 | return $errors; | |
222 | } | |
223 | } |