]>
Commit | Line | Data |
---|---|---|
510377d2 A |
1 | <?php |
2 | ||
3 | /** | |
4 | * Class Updater. | |
5 | * Used to update stuff when a new Shaarli's version is reached. | |
6 | * Update methods are ran only once, and the stored in a JSON file. | |
7 | */ | |
8 | class Updater | |
9 | { | |
10 | /** | |
11 | * @var array Updates which are already done. | |
12 | */ | |
13 | protected $doneUpdates; | |
14 | ||
510377d2 A |
15 | /** |
16 | * @var LinkDB instance. | |
17 | */ | |
18 | protected $linkDB; | |
19 | ||
278d9ee2 A |
20 | /** |
21 | * @var ConfigManager $conf Configuration Manager instance. | |
22 | */ | |
23 | protected $conf; | |
24 | ||
510377d2 A |
25 | /** |
26 | * @var bool True if the user is logged in, false otherwise. | |
27 | */ | |
28 | protected $isLoggedIn; | |
29 | ||
30 | /** | |
31 | * @var ReflectionMethod[] List of current class methods. | |
32 | */ | |
33 | protected $methods; | |
34 | ||
35 | /** | |
36 | * Object constructor. | |
37 | * | |
278d9ee2 A |
38 | * @param array $doneUpdates Updates which are already done. |
39 | * @param LinkDB $linkDB LinkDB instance. | |
7af9a418 | 40 | * @param ConfigManager $conf Configuration Manager instance. |
278d9ee2 | 41 | * @param boolean $isLoggedIn True if the user is logged in. |
510377d2 | 42 | */ |
278d9ee2 | 43 | public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn) |
510377d2 A |
44 | { |
45 | $this->doneUpdates = $doneUpdates; | |
510377d2 | 46 | $this->linkDB = $linkDB; |
278d9ee2 | 47 | $this->conf = $conf; |
510377d2 A |
48 | $this->isLoggedIn = $isLoggedIn; |
49 | ||
50 | // Retrieve all update methods. | |
51 | $class = new ReflectionClass($this); | |
52 | $this->methods = $class->getMethods(); | |
53 | } | |
54 | ||
55 | /** | |
56 | * Run all new updates. | |
57 | * Update methods have to start with 'updateMethod' and return true (on success). | |
58 | * | |
59 | * @return array An array containing ran updates. | |
60 | * | |
61 | * @throws UpdaterException If something went wrong. | |
62 | */ | |
63 | public function update() | |
64 | { | |
65 | $updatesRan = array(); | |
66 | ||
67 | // If the user isn't logged in, exit without updating. | |
68 | if ($this->isLoggedIn !== true) { | |
69 | return $updatesRan; | |
70 | } | |
71 | ||
72 | if ($this->methods == null) { | |
73 | throw new UpdaterException('Couldn\'t retrieve Updater class methods.'); | |
74 | } | |
75 | ||
76 | foreach ($this->methods as $method) { | |
77 | // Not an update method or already done, pass. | |
78 | if (! startsWith($method->getName(), 'updateMethod') | |
79 | || in_array($method->getName(), $this->doneUpdates) | |
80 | ) { | |
81 | continue; | |
82 | } | |
83 | ||
84 | try { | |
85 | $method->setAccessible(true); | |
86 | $res = $method->invoke($this); | |
87 | // Update method must return true to be considered processed. | |
88 | if ($res === true) { | |
89 | $updatesRan[] = $method->getName(); | |
90 | } | |
91 | } catch (Exception $e) { | |
92 | throw new UpdaterException($method, $e); | |
93 | } | |
94 | } | |
95 | ||
96 | $this->doneUpdates = array_merge($this->doneUpdates, $updatesRan); | |
97 | ||
98 | return $updatesRan; | |
99 | } | |
100 | ||
101 | /** | |
102 | * @return array Updates methods already processed. | |
103 | */ | |
104 | public function getDoneUpdates() | |
105 | { | |
106 | return $this->doneUpdates; | |
107 | } | |
108 | ||
109 | /** | |
110 | * Move deprecated options.php to config.php. | |
111 | * | |
112 | * Milestone 0.9 (old versioning) - shaarli/Shaarli#41: | |
113 | * options.php is not supported anymore. | |
114 | */ | |
115 | public function updateMethodMergeDeprecatedConfigFile() | |
116 | { | |
894a3c4b A |
117 | if (is_file($this->conf->get('resource.data_dir') . '/options.php')) { |
118 | include $this->conf->get('resource.data_dir') . '/options.php'; | |
510377d2 A |
119 | |
120 | // Load GLOBALS into config | |
684e662a A |
121 | $allowedKeys = array_merge(ConfigPhp::$ROOT_KEYS); |
122 | $allowedKeys[] = 'config'; | |
510377d2 | 123 | foreach ($GLOBALS as $key => $value) { |
684e662a | 124 | if (in_array($key, $allowedKeys)) { |
278d9ee2 | 125 | $this->conf->set($key, $value); |
684e662a | 126 | } |
510377d2 | 127 | } |
278d9ee2 | 128 | $this->conf->write($this->isLoggedIn); |
894a3c4b | 129 | unlink($this->conf->get('resource.data_dir').'/options.php'); |
510377d2 A |
130 | } |
131 | ||
132 | return true; | |
133 | } | |
21979ff1 A |
134 | |
135 | /** | |
136 | * Rename tags starting with a '-' to work with tag exclusion search. | |
137 | */ | |
138 | public function updateMethodRenameDashTags() | |
139 | { | |
528a6f8a | 140 | $linklist = $this->linkDB->filterSearch(); |
1dc37f9c | 141 | foreach ($linklist as $key => $link) { |
21979ff1 A |
142 | $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); |
143 | $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); | |
1dc37f9c | 144 | $this->linkDB[$key] = $link; |
21979ff1 | 145 | } |
f21abf32 | 146 | $this->linkDB->save($this->conf->get('resource.page_cache')); |
21979ff1 A |
147 | return true; |
148 | } | |
b74b96bf A |
149 | |
150 | /** | |
151 | * Move old configuration in PHP to the new config system in JSON format. | |
152 | * | |
da10377b A |
153 | * Will rename 'config.php' into 'config.save.php' and create 'config.json.php'. |
154 | * It will also convert legacy setting keys to the new ones. | |
b74b96bf A |
155 | */ |
156 | public function updateMethodConfigToJson() | |
157 | { | |
b74b96bf | 158 | // JSON config already exists, nothing to do. |
278d9ee2 | 159 | if ($this->conf->getConfigIO() instanceof ConfigJson) { |
b74b96bf A |
160 | return true; |
161 | } | |
162 | ||
163 | $configPhp = new ConfigPhp(); | |
164 | $configJson = new ConfigJson(); | |
278d9ee2 A |
165 | $oldConfig = $configPhp->read($this->conf->getConfigFile() . '.php'); |
166 | rename($this->conf->getConfigFileExt(), $this->conf->getConfigFile() . '.save.php'); | |
167 | $this->conf->setConfigIO($configJson); | |
168 | $this->conf->reload(); | |
b74b96bf | 169 | |
da10377b | 170 | $legacyMap = array_flip(ConfigPhp::$LEGACY_KEYS_MAPPING); |
b74b96bf | 171 | foreach (ConfigPhp::$ROOT_KEYS as $key) { |
278d9ee2 | 172 | $this->conf->set($legacyMap[$key], $oldConfig[$key]); |
b74b96bf A |
173 | } |
174 | ||
175 | // Set sub config keys (config and plugins) | |
176 | $subConfig = array('config', 'plugins'); | |
177 | foreach ($subConfig as $sub) { | |
178 | foreach ($oldConfig[$sub] as $key => $value) { | |
da10377b A |
179 | if (isset($legacyMap[$sub .'.'. $key])) { |
180 | $configKey = $legacyMap[$sub .'.'. $key]; | |
181 | } else { | |
182 | $configKey = $sub .'.'. $key; | |
183 | } | |
278d9ee2 | 184 | $this->conf->set($configKey, $value); |
b74b96bf A |
185 | } |
186 | } | |
187 | ||
188 | try{ | |
278d9ee2 | 189 | $this->conf->write($this->isLoggedIn); |
b74b96bf A |
190 | return true; |
191 | } catch (IOException $e) { | |
192 | error_log($e->getMessage()); | |
193 | return false; | |
194 | } | |
195 | } | |
7f179985 A |
196 | |
197 | /** | |
198 | * Escape settings which have been manually escaped in every request in previous versions: | |
199 | * - general.title | |
200 | * - general.header_link | |
b9f8b837 | 201 | * - redirector.url |
7f179985 A |
202 | * |
203 | * @return bool true if the update is successful, false otherwise. | |
204 | */ | |
b9f8b837 | 205 | public function updateMethodEscapeUnescapedConfig() |
7f179985 | 206 | { |
7f179985 | 207 | try { |
278d9ee2 A |
208 | $this->conf->set('general.title', escape($this->conf->get('general.title'))); |
209 | $this->conf->set('general.header_link', escape($this->conf->get('general.header_link'))); | |
894a3c4b | 210 | $this->conf->set('redirector.url', escape($this->conf->get('redirector.url'))); |
278d9ee2 | 211 | $this->conf->write($this->isLoggedIn); |
7f179985 A |
212 | } catch (Exception $e) { |
213 | error_log($e->getMessage()); | |
214 | return false; | |
215 | } | |
216 | return true; | |
217 | } | |
1dc37f9c A |
218 | |
219 | /** | |
220 | * Update the database to use the new ID system, which replaces linkdate primary keys. | |
01878a75 | 221 | * Also, creation and update dates are now DateTime objects (done by LinkDB). |
1dc37f9c A |
222 | * |
223 | * Since this update is very sensitve (changing the whole database), the datastore will be | |
224 | * automatically backed up into the file datastore.<datetime>.php. | |
225 | * | |
d592daea A |
226 | * LinkDB also adds the field 'shorturl' with the precedent format (linkdate smallhash), |
227 | * which will be saved by this method. | |
228 | * | |
1dc37f9c A |
229 | * @return bool true if the update is successful, false otherwise. |
230 | */ | |
231 | public function updateMethodDatastoreIds() | |
232 | { | |
233 | // up to date database | |
234 | if (isset($this->linkDB[0])) { | |
235 | return true; | |
236 | } | |
237 | ||
238 | $save = $this->conf->get('resource.data_dir') .'/datastore.'. date('YmdHis') .'.php'; | |
239 | copy($this->conf->get('resource.datastore'), $save); | |
240 | ||
241 | $links = array(); | |
242 | foreach ($this->linkDB as $offset => $value) { | |
243 | $links[] = $value; | |
244 | unset($this->linkDB[$offset]); | |
245 | } | |
246 | $links = array_reverse($links); | |
247 | $cpt = 0; | |
248 | foreach ($links as $l) { | |
1dc37f9c A |
249 | unset($l['linkdate']); |
250 | $l['id'] = $cpt; | |
251 | $this->linkDB[$cpt++] = $l; | |
252 | } | |
253 | ||
254 | $this->linkDB->save($this->conf->get('resource.page_cache')); | |
255 | $this->linkDB->reorder(); | |
256 | ||
257 | return true; | |
258 | } | |
9ff17ae2 A |
259 | |
260 | /** | |
261 | * * `markdown_escape` is a new setting, set to true as default. | |
262 | * | |
263 | * If the markdown plugin was already enabled, escaping is disabled to avoid | |
264 | * breaking existing entries. | |
265 | */ | |
266 | public function updateMethodEscapeMarkdown() | |
267 | { | |
268 | if ($this->conf->exists('security.markdown_escape')) { | |
269 | return true; | |
270 | } | |
271 | ||
272 | if (in_array('markdown', $this->conf->get('general.enabled_plugins'))) { | |
273 | $this->conf->set('security.markdown_escape', false); | |
274 | } else { | |
275 | $this->conf->set('security.markdown_escape', true); | |
276 | } | |
277 | $this->conf->write($this->isLoggedIn); | |
278 | ||
279 | return true; | |
280 | } | |
510377d2 A |
281 | } |
282 | ||
283 | /** | |
284 | * Class UpdaterException. | |
285 | */ | |
286 | class UpdaterException extends Exception | |
287 | { | |
288 | /** | |
289 | * @var string Method where the error occurred. | |
290 | */ | |
291 | protected $method; | |
292 | ||
293 | /** | |
294 | * @var Exception The parent exception. | |
295 | */ | |
296 | protected $previous; | |
297 | ||
298 | /** | |
299 | * Constructor. | |
300 | * | |
301 | * @param string $message Force the error message if set. | |
302 | * @param string $method Method where the error occurred. | |
303 | * @param Exception|bool $previous Parent exception. | |
304 | */ | |
305 | public function __construct($message = '', $method = '', $previous = false) | |
306 | { | |
307 | $this->method = $method; | |
308 | $this->previous = $previous; | |
309 | $this->message = $this->buildMessage($message); | |
310 | } | |
311 | ||
312 | /** | |
313 | * Build the exception error message. | |
314 | * | |
315 | * @param string $message Optional given error message. | |
316 | * | |
317 | * @return string The built error message. | |
318 | */ | |
319 | private function buildMessage($message) | |
320 | { | |
321 | $out = ''; | |
322 | if (! empty($message)) { | |
323 | $out .= $message . PHP_EOL; | |
324 | } | |
325 | ||
326 | if (! empty($this->method)) { | |
327 | $out .= 'An error occurred while running the update '. $this->method . PHP_EOL; | |
328 | } | |
329 | ||
330 | if (! empty($this->previous)) { | |
331 | $out .= ' '. $this->previous->getMessage(); | |
332 | } | |
333 | ||
334 | return $out; | |
335 | } | |
336 | } | |
337 | ||
510377d2 A |
338 | /** |
339 | * Read the updates file, and return already done updates. | |
340 | * | |
341 | * @param string $updatesFilepath Updates file path. | |
342 | * | |
343 | * @return array Already done update methods. | |
344 | */ | |
345 | function read_updates_file($updatesFilepath) | |
346 | { | |
347 | if (! empty($updatesFilepath) && is_file($updatesFilepath)) { | |
348 | $content = file_get_contents($updatesFilepath); | |
349 | if (! empty($content)) { | |
350 | return explode(';', $content); | |
351 | } | |
352 | } | |
353 | return array(); | |
354 | } | |
355 | ||
356 | /** | |
357 | * Write updates file. | |
358 | * | |
359 | * @param string $updatesFilepath Updates file path. | |
360 | * @param array $updates Updates array to write. | |
361 | * | |
362 | * @throws Exception Couldn't write version number. | |
363 | */ | |
364 | function write_updates_file($updatesFilepath, $updates) | |
365 | { | |
366 | if (empty($updatesFilepath)) { | |
367 | throw new Exception('Updates file path is not set, can\'t write updates.'); | |
368 | } | |
369 | ||
370 | $res = file_put_contents($updatesFilepath, implode(';', $updates)); | |
371 | if ($res === false) { | |
372 | throw new Exception('Unable to write updates in '. $updatesFilepath . '.'); | |
373 | } | |
374 | } |