2015-11-11 22:49:58 +01:00
|
|
|
<?php
|
2020-09-22 20:25:47 +02:00
|
|
|
|
2020-10-16 13:34:59 +02:00
|
|
|
namespace Shaarli\Helper;
|
2018-12-03 23:58:59 +01:00
|
|
|
|
|
|
|
use Exception;
|
2020-11-24 13:28:17 +01:00
|
|
|
use malkusch\lock\exception\LockAcquireException;
|
|
|
|
use malkusch\lock\mutex\FlockMutex;
|
2018-12-03 23:58:59 +01:00
|
|
|
use Shaarli\Config\ConfigManager;
|
|
|
|
|
2015-11-11 22:49:58 +01:00
|
|
|
/**
|
|
|
|
* Shaarli (application) utilities
|
|
|
|
*/
|
|
|
|
class ApplicationUtils
|
|
|
|
{
|
2017-03-21 20:08:40 +01:00
|
|
|
/**
|
|
|
|
* @var string File containing the current version
|
|
|
|
*/
|
|
|
|
public static $VERSION_FILE = 'shaarli_version.php';
|
|
|
|
|
2020-10-21 13:12:15 +02:00
|
|
|
public static $GITHUB_URL = 'https://github.com/shaarli/Shaarli';
|
|
|
|
public static $GIT_RAW_URL = 'https://raw.githubusercontent.com/shaarli/Shaarli';
|
2020-09-22 20:25:47 +02:00
|
|
|
public static $GIT_BRANCHES = ['latest', 'stable'];
|
2015-11-24 02:52:22 +01:00
|
|
|
private static $VERSION_START_TAG = '<?php /* ';
|
|
|
|
private static $VERSION_END_TAG = ' */ ?>';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the latest version code from the Git repository
|
|
|
|
*
|
|
|
|
* The code is read from the raw content of the version file on the Git server.
|
|
|
|
*
|
2016-10-20 11:31:52 +02:00
|
|
|
* @param string $url URL to reach to get the latest version.
|
|
|
|
* @param int $timeout Timeout to check the URL (in seconds).
|
|
|
|
*
|
2015-11-24 02:52:22 +01:00
|
|
|
* @return mixed the version code from the repository if available, else 'false'
|
|
|
|
*/
|
2018-10-13 00:19:03 +02:00
|
|
|
public static function getLatestGitVersionCode($url, $timeout = 2)
|
2015-11-24 02:52:22 +01:00
|
|
|
{
|
2016-01-04 10:45:54 +01:00
|
|
|
list($headers, $data) = get_http_response($url, $timeout);
|
2015-11-24 02:52:22 +01:00
|
|
|
|
2021-01-19 15:03:18 +01:00
|
|
|
if (preg_match('#HTTP/[\d\.]+ 200(?: OK)?#', $headers[0]) !== 1) {
|
2015-11-24 02:52:22 +01:00
|
|
|
error_log('Failed to retrieve ' . $url);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-21 20:08:40 +01:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the version from a remote URL or a file.
|
|
|
|
*
|
|
|
|
* @param string $remote URL or file to fetch.
|
|
|
|
* @param int $timeout For URLs fetching.
|
|
|
|
*
|
|
|
|
* @return bool|string The version or false if it couldn't be retrieved.
|
|
|
|
*/
|
|
|
|
public static function getVersion($remote, $timeout = 2)
|
|
|
|
{
|
|
|
|
if (startsWith($remote, 'http')) {
|
|
|
|
if (($data = static::getLatestGitVersionCode($remote, $timeout)) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-03 23:58:59 +01:00
|
|
|
if (!is_file($remote)) {
|
2017-03-21 20:08:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$data = file_get_contents($remote);
|
|
|
|
}
|
|
|
|
|
2015-11-24 02:52:22 +01:00
|
|
|
return str_replace(
|
2020-09-22 20:25:47 +02:00
|
|
|
[self::$VERSION_START_TAG, self::$VERSION_END_TAG, PHP_EOL],
|
|
|
|
['', '', ''],
|
2015-11-24 02:52:22 +01:00
|
|
|
$data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a new Shaarli version has been published on the Git repository
|
|
|
|
*
|
|
|
|
* Updates checks are run periodically, according to the following criteria:
|
|
|
|
* - the update checks are enabled (install, global config);
|
|
|
|
* - the user is logged in (or this is an open instance);
|
|
|
|
* - the last check is older than a given interval;
|
|
|
|
* - the check is non-blocking if the HTTPS connection to Git fails;
|
|
|
|
* - in case of failure, the update file's modification date is updated,
|
|
|
|
* to avoid intempestive connection attempts.
|
|
|
|
*
|
|
|
|
* @param string $currentVersion the current version code
|
|
|
|
* @param string $updateFile the file where to store the latest version code
|
|
|
|
* @param int $checkInterval the minimum interval between update checks (in seconds
|
|
|
|
* @param bool $enableCheck whether to check for new versions
|
|
|
|
* @param bool $isLoggedIn whether the user is logged in
|
2016-10-20 11:31:52 +02:00
|
|
|
* @param string $branch check update for the given branch
|
2015-11-24 02:52:22 +01:00
|
|
|
*
|
2015-12-03 20:30:46 +01:00
|
|
|
* @throws Exception an invalid branch has been set for update checks
|
|
|
|
*
|
2015-11-24 02:52:22 +01:00
|
|
|
* @return mixed the new version code if available and greater, else 'false'
|
|
|
|
*/
|
2018-10-13 00:19:03 +02:00
|
|
|
public static function checkUpdate(
|
|
|
|
$currentVersion,
|
|
|
|
$updateFile,
|
|
|
|
$checkInterval,
|
|
|
|
$enableCheck,
|
|
|
|
$isLoggedIn,
|
|
|
|
$branch = 'stable'
|
|
|
|
) {
|
2017-03-12 15:02:06 +01:00
|
|
|
// Do not check versions for visitors
|
|
|
|
// Do not check if the user doesn't want to
|
|
|
|
// Do not check with dev version
|
2018-12-03 23:58:59 +01:00
|
|
|
if (!$isLoggedIn || empty($enableCheck) || $currentVersion === 'dev') {
|
2015-11-24 02:52:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_file($updateFile) && (filemtime($updateFile) > time() - $checkInterval)) {
|
|
|
|
// Shaarli has checked for updates recently - skip HTTP query
|
|
|
|
$latestKnownVersion = file_get_contents($updateFile);
|
|
|
|
|
|
|
|
if (version_compare($latestKnownVersion, $currentVersion) == 1) {
|
|
|
|
return $latestKnownVersion;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-03 23:58:59 +01:00
|
|
|
if (!in_array($branch, self::$GIT_BRANCHES)) {
|
2015-11-27 00:10:43 +01:00
|
|
|
throw new Exception(
|
|
|
|
'Invalid branch selected for updates: "' . $branch . '"'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-24 02:52:22 +01:00
|
|
|
// Late Static Binding allows overriding within tests
|
|
|
|
// See http://php.net/manual/en/language.oop5.late-static-bindings.php
|
2017-03-21 20:08:40 +01:00
|
|
|
$latestVersion = static::getVersion(
|
2020-10-21 13:12:15 +02:00
|
|
|
self::$GIT_RAW_URL . '/' . $branch . '/' . self::$VERSION_FILE
|
2015-11-24 02:52:22 +01:00
|
|
|
);
|
|
|
|
|
2018-12-03 23:58:59 +01:00
|
|
|
if (!$latestVersion) {
|
2015-11-24 02:52:22 +01:00
|
|
|
// Only update the file's modification date
|
|
|
|
file_put_contents($updateFile, $currentVersion);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the file's content and modification date
|
|
|
|
file_put_contents($updateFile, $latestVersion);
|
|
|
|
|
|
|
|
if (version_compare($latestVersion, $currentVersion) == 1) {
|
|
|
|
return $latestVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-11 22:49:58 +01:00
|
|
|
|
2015-11-24 01:36:12 +01:00
|
|
|
/**
|
|
|
|
* Checks the PHP version to ensure Shaarli can run
|
|
|
|
*
|
|
|
|
* @param string $minVersion minimum PHP required version
|
|
|
|
* @param string $curVersion current PHP version (use PHP_VERSION)
|
|
|
|
*
|
2019-08-10 12:31:32 +02:00
|
|
|
* @return bool true on success
|
|
|
|
*
|
2015-11-24 01:36:12 +01:00
|
|
|
* @throws Exception the PHP version is not supported
|
|
|
|
*/
|
|
|
|
public static function checkPHPVersion($minVersion, $curVersion)
|
|
|
|
{
|
|
|
|
if (version_compare($curVersion, $minVersion) < 0) {
|
2017-05-09 18:12:15 +02:00
|
|
|
$msg = t(
|
2015-11-24 01:36:12 +01:00
|
|
|
'Your PHP version is obsolete!'
|
2018-12-03 23:58:59 +01:00
|
|
|
. ' Shaarli requires at least PHP %s, and thus cannot run.'
|
|
|
|
. ' Your PHP version has known security vulnerabilities and should be'
|
|
|
|
. ' updated as soon as possible.'
|
2015-11-24 01:36:12 +01:00
|
|
|
);
|
2017-05-09 18:12:15 +02:00
|
|
|
throw new Exception(sprintf($msg, $minVersion));
|
2015-11-24 01:36:12 +01:00
|
|
|
}
|
2019-08-10 12:31:32 +02:00
|
|
|
return true;
|
2015-11-24 01:36:12 +01:00
|
|
|
}
|
|
|
|
|
2015-11-11 22:49:58 +01:00
|
|
|
/**
|
|
|
|
* Checks Shaarli has the proper access permissions to its resources
|
|
|
|
*
|
2020-10-21 13:12:15 +02:00
|
|
|
* @param ConfigManager $conf Configuration Manager instance.
|
|
|
|
* @param bool $minimalMode In minimal mode we only check permissions to be able to display a template.
|
|
|
|
* Currently we only need to be able to read the theme and write in raintpl cache.
|
2016-06-09 20:04:02 +02:00
|
|
|
*
|
2015-11-11 22:49:58 +01:00
|
|
|
* @return array A list of the detected configuration issues
|
|
|
|
*/
|
2020-10-21 13:12:15 +02:00
|
|
|
public static function checkResourcePermissions(ConfigManager $conf, bool $minimalMode = false): array
|
2015-11-11 22:49:58 +01:00
|
|
|
{
|
2020-10-21 13:12:15 +02:00
|
|
|
$errors = [];
|
2017-09-19 20:21:28 +02:00
|
|
|
$rainTplDir = rtrim($conf->get('resource.raintpl_tpl'), '/');
|
2015-11-11 22:49:58 +01:00
|
|
|
|
|
|
|
// Check script and template directories are readable
|
2020-09-22 20:25:47 +02:00
|
|
|
foreach (
|
|
|
|
[
|
|
|
|
'application',
|
|
|
|
'inc',
|
|
|
|
'plugins',
|
|
|
|
$rainTplDir,
|
|
|
|
$rainTplDir . '/' . $conf->get('resource.theme'),
|
|
|
|
] as $path
|
|
|
|
) {
|
2018-12-03 23:58:59 +01:00
|
|
|
if (!is_readable(realpath($path))) {
|
|
|
|
$errors[] = '"' . $path . '" ' . t('directory is not readable');
|
2015-11-11 22:49:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 11:31:52 +02:00
|
|
|
// Check cache and data directories are readable and writable
|
2020-10-21 13:12:15 +02:00
|
|
|
if ($minimalMode) {
|
|
|
|
$folders = [
|
|
|
|
$conf->get('resource.raintpl_tmp'),
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$folders = [
|
2020-09-22 20:25:47 +02:00
|
|
|
$conf->get('resource.thumbnails_cache'),
|
|
|
|
$conf->get('resource.data_dir'),
|
|
|
|
$conf->get('resource.page_cache'),
|
|
|
|
$conf->get('resource.raintpl_tmp'),
|
2020-10-21 13:12:15 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($folders as $path) {
|
2018-12-03 23:58:59 +01:00
|
|
|
if (!is_readable(realpath($path))) {
|
|
|
|
$errors[] = '"' . $path . '" ' . t('directory is not readable');
|
2015-11-11 22:49:58 +01:00
|
|
|
}
|
2018-12-03 23:58:59 +01:00
|
|
|
if (!is_writable(realpath($path))) {
|
|
|
|
$errors[] = '"' . $path . '" ' . t('directory is not writable');
|
2015-11-11 22:49:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:12:15 +02:00
|
|
|
if ($minimalMode) {
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
2016-10-20 11:31:52 +02:00
|
|
|
// Check configuration files are readable and writable
|
2020-09-22 20:25:47 +02:00
|
|
|
foreach (
|
|
|
|
[
|
|
|
|
$conf->getConfigFileExt(),
|
|
|
|
$conf->get('resource.datastore'),
|
|
|
|
$conf->get('resource.ban_file'),
|
|
|
|
$conf->get('resource.log'),
|
|
|
|
$conf->get('resource.update_check'),
|
|
|
|
] as $path
|
|
|
|
) {
|
2022-09-14 08:17:07 +02:00
|
|
|
if (!is_string($path) || !is_file(realpath($path))) {
|
2015-11-11 22:49:58 +01:00
|
|
|
# the file may not exist yet
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-03 23:58:59 +01:00
|
|
|
if (!is_readable(realpath($path))) {
|
|
|
|
$errors[] = '"' . $path . '" ' . t('file is not readable');
|
2015-11-11 22:49:58 +01:00
|
|
|
}
|
2018-12-03 23:58:59 +01:00
|
|
|
if (!is_writable(realpath($path))) {
|
|
|
|
$errors[] = '"' . $path . '" ' . t('file is not writable');
|
2015-11-11 22:49:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
2017-10-01 11:02:48 +02:00
|
|
|
|
2020-11-24 13:28:17 +01:00
|
|
|
public static function checkDatastoreMutex(): array
|
|
|
|
{
|
|
|
|
$mutex = new FlockMutex(fopen(SHAARLI_MUTEX_FILE, 'r'), 2);
|
|
|
|
try {
|
|
|
|
$mutex->synchronized(function () {
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
} catch (LockAcquireException $e) {
|
|
|
|
$errors[] = t('Lock can not be acquired on the datastore. You might encounter concurrent access issues.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors ?? [];
|
|
|
|
}
|
|
|
|
|
2017-10-01 11:02:48 +02:00
|
|
|
/**
|
|
|
|
* Returns a salted hash representing the current Shaarli version.
|
|
|
|
*
|
|
|
|
* Useful for assets browser cache.
|
|
|
|
*
|
|
|
|
* @param string $currentVersion of Shaarli
|
|
|
|
* @param string $salt User personal salt, also used for the authentication
|
|
|
|
*
|
|
|
|
* @return string version hash
|
|
|
|
*/
|
|
|
|
public static function getVersionHash($currentVersion, $salt)
|
|
|
|
{
|
|
|
|
return hash_hmac('sha256', $currentVersion, $salt);
|
|
|
|
}
|
2020-10-21 13:12:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of PHP extensions used by Shaarli.
|
|
|
|
*
|
|
|
|
* @return array[] List of extension with following keys:
|
|
|
|
* - name: extension name
|
|
|
|
* - required: whether the extension is required to use Shaarli
|
|
|
|
* - desc: short description of extension usage in Shaarli
|
|
|
|
* - loaded: whether the extension is properly loaded or not
|
|
|
|
*/
|
|
|
|
public static function getPhpExtensionsRequirement(): array
|
|
|
|
{
|
|
|
|
$extensions = [
|
|
|
|
['name' => 'json', 'required' => true, 'desc' => t('Configuration parsing')],
|
|
|
|
['name' => 'simplexml', 'required' => true, 'desc' => t('Slim Framework (routing, etc.)')],
|
|
|
|
['name' => 'mbstring', 'required' => true, 'desc' => t('Multibyte (Unicode) string support')],
|
|
|
|
['name' => 'gd', 'required' => false, 'desc' => t('Required to use thumbnails')],
|
|
|
|
['name' => 'intl', 'required' => false, 'desc' => t('Localized text sorting (e.g. e->è->f)')],
|
|
|
|
['name' => 'curl', 'required' => false, 'desc' => t('Better retrieval of bookmark metadata and thumbnail')],
|
|
|
|
['name' => 'gettext', 'required' => false, 'desc' => t('Use the translation system in gettext mode')],
|
|
|
|
['name' => 'ldap', 'required' => false, 'desc' => t('Login using LDAP server')],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($extensions as &$extension) {
|
|
|
|
$extension['loaded'] = extension_loaded($extension['name']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the EOL date of given PHP version. If the version is unknown,
|
|
|
|
* we return today + 2 years.
|
|
|
|
*
|
|
|
|
* @param string $fullVersion PHP version, e.g. 7.4.7
|
|
|
|
*
|
|
|
|
* @return string Date format: YYYY-MM-DD
|
|
|
|
*/
|
|
|
|
public static function getPhpEol(string $fullVersion): string
|
|
|
|
{
|
|
|
|
preg_match('/(\d+\.\d+)\.\d+/', $fullVersion, $matches);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'7.1' => '2019-12-01',
|
|
|
|
'7.2' => '2020-11-30',
|
|
|
|
'7.3' => '2021-12-06',
|
|
|
|
'7.4' => '2022-11-28',
|
2023-03-18 19:27:41 +01:00
|
|
|
'8.0' => '2023-11-26',
|
|
|
|
'8.1' => '2024-11-25',
|
|
|
|
'8.2' => '2025-12-08',
|
2020-10-21 13:12:15 +02:00
|
|
|
][$matches[1]] ?? (new \DateTime('+2 year'))->format('Y-m-d');
|
|
|
|
}
|
2015-11-11 22:49:58 +01:00
|
|
|
}
|