]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/ApplicationUtils.php
3 * Shaarli (application) utilities
7 private static $GIT_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli';
8 private static $GIT_BRANCHES = array('master', 'stable');
9 private static $VERSION_FILE = 'shaarli_version.php';
10 private static $VERSION_START_TAG = '<?php /* ';
11 private static $VERSION_END_TAG = ' */ ?>';
14 * Gets the latest version code from the Git repository
16 * The code is read from the raw content of the version file on the Git server.
18 * @param string $url URL to reach to get the latest version.
19 * @param int $timeout Timeout to check the URL (in seconds).
21 * @return mixed the version code from the repository if available, else 'false'
23 public static function getLatestGitVersionCode($url, $timeout=2)
25 list($headers, $data) = get_http_response($url, $timeout);
27 if (strpos($headers[0], '200 OK') === false) {
28 error_log('Failed to retrieve ' . $url);
33 array(self
::$VERSION_START_TAG, self
::$VERSION_END_TAG, PHP_EOL
),
40 * Checks if a new Shaarli version has been published on the Git repository
42 * Updates checks are run periodically, according to the following criteria:
43 * - the update checks are enabled (install, global config);
44 * - the user is logged in (or this is an open instance);
45 * - the last check is older than a given interval;
46 * - the check is non-blocking if the HTTPS connection to Git fails;
47 * - in case of failure, the update file's modification date is updated,
48 * to avoid intempestive connection attempts.
50 * @param string $currentVersion the current version code
51 * @param string $updateFile the file where to store the latest version code
52 * @param int $checkInterval the minimum interval between update checks (in seconds
53 * @param bool $enableCheck whether to check for new versions
54 * @param bool $isLoggedIn whether the user is logged in
55 * @param string $branch check update for the given branch
57 * @throws Exception an invalid branch has been set for update checks
59 * @return mixed the new version code if available and greater, else 'false'
61 public static function checkUpdate($currentVersion,
69 // Do not check versions for visitors
73 if (empty($enableCheck)) {
74 // Do not check if the user doesn't want to
78 if (is_file($updateFile) && (filemtime($updateFile) > time() - $checkInterval)) {
79 // Shaarli has checked for updates recently - skip HTTP query
80 $latestKnownVersion = file_get_contents($updateFile);
82 if (version_compare($latestKnownVersion, $currentVersion) == 1) {
83 return $latestKnownVersion;
88 if (! in_array($branch, self
::$GIT_BRANCHES)) {
90 'Invalid branch selected for updates: "' . $branch . '"'
94 // Late Static Binding allows overriding within tests
95 // See http://php.net/manual/en/language.oop5.late-static-bindings.php
96 $latestVersion = static::getLatestGitVersionCode(
97 self
::$GIT_URL . '/' . $branch . '/' . self
::$VERSION_FILE
100 if (! $latestVersion) {
101 // Only update the file's modification date
102 file_put_contents($updateFile, $currentVersion);
106 // Update the file's content and modification date
107 file_put_contents($updateFile, $latestVersion);
109 if (version_compare($latestVersion, $currentVersion) == 1) {
110 return $latestVersion;
117 * Checks the PHP version to ensure Shaarli can run
119 * @param string $minVersion minimum PHP required version
120 * @param string $curVersion current PHP version (use PHP_VERSION)
122 * @throws Exception the PHP version is not supported
124 public static function checkPHPVersion($minVersion, $curVersion)
126 if (version_compare($curVersion, $minVersion) < 0) {
128 'Your PHP version is obsolete!'
129 .' Shaarli requires at least PHP '.$minVersion.', and thus cannot run.'
130 .' Your PHP version has known security vulnerabilities and should be'
131 .' updated as soon as possible.'
137 * Checks Shaarli has the proper access permissions to its resources
139 * @param ConfigManager $conf Configuration Manager instance.
141 * @return array A list of the detected configuration issues
143 public static function checkResourcePermissions($conf)
147 // Check script and template directories are readable
152 $conf->get('resource.raintpl_tpl'),
153 $conf->get('resource.raintpl_tpl').'/'.$conf->get('resource.theme'),
155 if (! is_readable(realpath($path))) {
156 $errors[] = '"'.$path.'" directory is not readable';
160 // Check cache and data directories are readable and writable
162 $conf->get('resource.thumbnails_cache'),
163 $conf->get('resource.data_dir'),
164 $conf->get('resource.page_cache'),
165 $conf->get('resource.raintpl_tmp'),
167 if (! is_readable(realpath($path))) {
168 $errors[] = '"'.$path.'" directory is not readable';
170 if (! is_writable(realpath($path))) {
171 $errors[] = '"'.$path.'" directory is not writable';
175 // Check configuration files are readable and writable
177 $conf->getConfigFileExt(),
178 $conf->get('resource.datastore'),
179 $conf->get('resource.ban_file'),
180 $conf->get('resource.log'),
181 $conf->get('resource.update_check'),
183 if (! is_file(realpath($path))) {
184 # the file may not exist yet
188 if (! is_readable(realpath($path))) {
189 $errors[] = '"'.$path.'" file is not readable';
191 if (! is_writable(realpath($path))) {
192 $errors[] = '"'.$path.'" file is not writable';