aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2015-11-24 02:52:22 +0100
committerVirtualTam <virtualtam@flibidi.net>2015-11-26 23:19:37 +0100
commit4bf35ba56bb9f06de0cb9ab920b799a39f8eaffc (patch)
treee46d75e5afba96bc0d4edf4cc8af9b54869415b3 /application
parent61873e3ded8dfba397b39aebd2322d0939c82caa (diff)
downloadShaarli-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 'application')
-rw-r--r--application/ApplicationUtils.php92
1 files changed, 92 insertions, 0 deletions
diff --git a/application/ApplicationUtils.php b/application/ApplicationUtils.php
index b0e94e24..c7414b77 100644
--- a/application/ApplicationUtils.php
+++ b/application/ApplicationUtils.php
@@ -4,6 +4,98 @@
4 */ 4 */
5class ApplicationUtils 5class ApplicationUtils
6{ 6{
7 private static $GIT_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli';
8 private static $GIT_BRANCH = 'master';
9 private static $VERSION_FILE = 'shaarli_version.php';
10 private static $VERSION_START_TAG = '<?php /* ';
11 private static $VERSION_END_TAG = ' */ ?>';
12
13 /**
14 * Gets the latest version code from the Git repository
15 *
16 * The code is read from the raw content of the version file on the Git server.
17 *
18 * @return mixed the version code from the repository if available, else 'false'
19 */
20 public static function getLatestGitVersionCode($url, $timeout=2)
21 {
22 list($headers, $data) = get_http_url($url, $timeout);
23
24 if (strpos($headers[0], '200 OK') === false) {
25 error_log('Failed to retrieve ' . $url);
26 return false;
27 }
28
29 return str_replace(
30 array(self::$VERSION_START_TAG, self::$VERSION_END_TAG, PHP_EOL),
31 array('', '', ''),
32 $data
33 );
34 }
35
36 /**
37 * Checks if a new Shaarli version has been published on the Git repository
38 *
39 * Updates checks are run periodically, according to the following criteria:
40 * - the update checks are enabled (install, global config);
41 * - the user is logged in (or this is an open instance);
42 * - the last check is older than a given interval;
43 * - the check is non-blocking if the HTTPS connection to Git fails;
44 * - in case of failure, the update file's modification date is updated,
45 * to avoid intempestive connection attempts.
46 *
47 * @param string $currentVersion the current version code
48 * @param string $updateFile the file where to store the latest version code
49 * @param int $checkInterval the minimum interval between update checks (in seconds
50 * @param bool $enableCheck whether to check for new versions
51 * @param bool $isLoggedIn whether the user is logged in
52 *
53 * @return mixed the new version code if available and greater, else 'false'
54 */
55 public static function checkUpdate(
56 $currentVersion, $updateFile, $checkInterval, $enableCheck, $isLoggedIn)
57 {
58 if (! $isLoggedIn) {
59 // Do not check versions for visitors
60 return false;
61 }
62
63 if (empty($enableCheck)) {
64 // Do not check if the user doesn't want to
65 return false;
66 }
67
68 if (is_file($updateFile) && (filemtime($updateFile) > time() - $checkInterval)) {
69 // Shaarli has checked for updates recently - skip HTTP query
70 $latestKnownVersion = file_get_contents($updateFile);
71
72 if (version_compare($latestKnownVersion, $currentVersion) == 1) {
73 return $latestKnownVersion;
74 }
75 return false;
76 }
77
78 // Late Static Binding allows overriding within tests
79 // See http://php.net/manual/en/language.oop5.late-static-bindings.php
80 $latestVersion = static::getLatestGitVersionCode(
81 self::$GIT_URL . '/' . self::$GIT_BRANCH . '/' . self::$VERSION_FILE
82 );
83
84 if (! $latestVersion) {
85 // Only update the file's modification date
86 file_put_contents($updateFile, $currentVersion);
87 return false;
88 }
89
90 // Update the file's content and modification date
91 file_put_contents($updateFile, $latestVersion);
92
93 if (version_compare($latestVersion, $currentVersion) == 1) {
94 return $latestVersion;
95 }
96
97 return false;
98 }
7 99
8 /** 100 /**
9 * Checks the PHP version to ensure Shaarli can run 101 * Checks the PHP version to ensure Shaarli can run