diff options
author | VirtualTam <virtualtam@flibidi.net> | 2015-11-24 02:52:22 +0100 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2015-11-26 23:19:37 +0100 |
commit | 4bf35ba56bb9f06de0cb9ab920b799a39f8eaffc (patch) | |
tree | e46d75e5afba96bc0d4edf4cc8af9b54869415b3 /index.php | |
parent | 61873e3ded8dfba397b39aebd2322d0939c82caa (diff) | |
download | Shaarli-4bf35ba56bb9f06de0cb9ab920b799a39f8eaffc.tar.gz Shaarli-4bf35ba56bb9f06de0cb9ab920b799a39f8eaffc.tar.zst Shaarli-4bf35ba56bb9f06de0cb9ab920b799a39f8eaffc.zip |
application: refactor version checks, move to ApplicationUtils
Relates to #372
Modifications:
- move checkUpdate() to ApplicationUtils
- reduce file I/O operations during version checks
- apply coding conventions
- add test coverage
Tools:
- create a sandbox directory for tests
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'index.php')
-rw-r--r-- | index.php | 39 |
1 files changed, 12 insertions, 27 deletions
@@ -305,32 +305,6 @@ function setup_login_state() { | |||
305 | } | 305 | } |
306 | $userIsLoggedIn = setup_login_state(); | 306 | $userIsLoggedIn = setup_login_state(); |
307 | 307 | ||
308 | // Checks if an update is available for Shaarli. | ||
309 | // (at most once a day, and only for registered user.) | ||
310 | // Output: '' = no new version. | ||
311 | // other= the available version. | ||
312 | function checkUpdate() | ||
313 | { | ||
314 | if (!isLoggedIn()) return ''; // Do not check versions for visitors. | ||
315 | if (empty($GLOBALS['config']['ENABLE_UPDATECHECK'])) return ''; // Do not check if the user doesn't want to. | ||
316 | |||
317 | // Get latest version number at most once a day. | ||
318 | if (!is_file($GLOBALS['config']['UPDATECHECK_FILENAME']) || (filemtime($GLOBALS['config']['UPDATECHECK_FILENAME'])<time()-($GLOBALS['config']['UPDATECHECK_INTERVAL']))) | ||
319 | { | ||
320 | $version = shaarli_version; | ||
321 | list($headers, $data) = get_http_url('https://raw.githubusercontent.com/shaarli/Shaarli/master/shaarli_version.php', 2); | ||
322 | if (strpos($headers[0], '200 OK') !== false) { | ||
323 | $version = str_replace(' */ ?>', '', str_replace('<?php /* ', '', $data)); | ||
324 | } | ||
325 | // If failed, never mind. We don't want to bother the user with that. | ||
326 | file_put_contents($GLOBALS['config']['UPDATECHECK_FILENAME'],$version); // touch file date | ||
327 | } | ||
328 | // Compare versions: | ||
329 | $newestversion=file_get_contents($GLOBALS['config']['UPDATECHECK_FILENAME']); | ||
330 | if (version_compare($newestversion,shaarli_version)==1) return $newestversion; | ||
331 | return ''; | ||
332 | } | ||
333 | |||
334 | 308 | ||
335 | // ----------------------------------------------------------------------------------------------- | 309 | // ----------------------------------------------------------------------------------------------- |
336 | // Log to text file | 310 | // Log to text file |
@@ -657,7 +631,18 @@ class pageBuilder | |||
657 | private function initialize() | 631 | private function initialize() |
658 | { | 632 | { |
659 | $this->tpl = new RainTPL; | 633 | $this->tpl = new RainTPL; |
660 | $this->tpl->assign('newversion', escape(checkUpdate())); | 634 | $this->tpl->assign( |
635 | 'newversion', | ||
636 | escape( | ||
637 | ApplicationUtils::checkUpdate( | ||
638 | shaarli_version, | ||
639 | $GLOBALS['config']['UPDATECHECK_FILENAME'], | ||
640 | $GLOBALS['config']['UPDATECHECK_INTERVAL'], | ||
641 | $GLOBALS['config']['ENABLE_UPDATECHECK'], | ||
642 | isLoggedIn() | ||
643 | ) | ||
644 | ) | ||
645 | ); | ||
661 | $this->tpl->assign('feedurl', escape(index_url($_SERVER))); | 646 | $this->tpl->assign('feedurl', escape(index_url($_SERVER))); |
662 | $searchcrits = ''; // Search criteria | 647 | $searchcrits = ''; // Search criteria |
663 | if (!empty($_GET['searchtags'])) { | 648 | if (!empty($_GET['searchtags'])) { |