]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/ApplicationUtils.php
Merge pull request #390 from virtualtam/app-utils/check-update
[github/shaarli/Shaarli.git] / application / ApplicationUtils.php
CommitLineData
2e28269b
V
1<?php
2/**
3 * Shaarli (application) utilities
4 */
5class ApplicationUtils
6{
4bf35ba5
V
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 }
2e28269b 99
c9cf2715
V
100 /**
101 * Checks the PHP version to ensure Shaarli can run
102 *
103 * @param string $minVersion minimum PHP required version
104 * @param string $curVersion current PHP version (use PHP_VERSION)
105 *
106 * @throws Exception the PHP version is not supported
107 */
108 public static function checkPHPVersion($minVersion, $curVersion)
109 {
110 if (version_compare($curVersion, $minVersion) < 0) {
111 throw new Exception(
112 'Your PHP version is obsolete!'
113 .' Shaarli requires at least PHP '.$minVersion.', and thus cannot run.'
114 .' Your PHP version has known security vulnerabilities and should be'
115 .' updated as soon as possible.'
116 );
117 }
118 }
119
2e28269b
V
120 /**
121 * Checks Shaarli has the proper access permissions to its resources
122 *
123 * @param array $globalConfig The $GLOBALS['config'] array
124 *
125 * @return array A list of the detected configuration issues
126 */
127 public static function checkResourcePermissions($globalConfig)
128 {
129 $errors = array();
130
131 // Check script and template directories are readable
132 foreach (array(
133 'application',
134 'inc',
135 'plugins',
136 $globalConfig['RAINTPL_TPL']
137 ) as $path) {
138 if (! is_readable(realpath($path))) {
139 $errors[] = '"'.$path.'" directory is not readable';
140 }
141 }
142
143 // Check cache and data directories are readable and writeable
144 foreach (array(
145 $globalConfig['CACHEDIR'],
146 $globalConfig['DATADIR'],
147 $globalConfig['PAGECACHE'],
148 $globalConfig['RAINTPL_TMP']
149 ) as $path) {
150 if (! is_readable(realpath($path))) {
151 $errors[] = '"'.$path.'" directory is not readable';
152 }
153 if (! is_writable(realpath($path))) {
154 $errors[] = '"'.$path.'" directory is not writable';
155 }
156 }
157
158 // Check configuration files are readable and writeable
159 foreach (array(
160 $globalConfig['CONFIG_FILE'],
161 $globalConfig['DATASTORE'],
162 $globalConfig['IPBANS_FILENAME'],
163 $globalConfig['LOG_FILE'],
164 $globalConfig['UPDATECHECK_FILENAME']
165 ) as $path) {
166 if (! is_file(realpath($path))) {
167 # the file may not exist yet
168 continue;
169 }
170
171 if (! is_readable(realpath($path))) {
172 $errors[] = '"'.$path.'" file is not readable';
173 }
174 if (! is_writable(realpath($path))) {
175 $errors[] = '"'.$path.'" file is not writable';
176 }
177 }
178
179 return $errors;
180 }
181}