1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CoreUpdater;
use Exception;
use Piwik\ArchiveProcessor\Rules;
use Piwik\CliMulti;
use Piwik\Common;
use Piwik\Config\GeneralConfig;
use Piwik\Container\StaticContainer;
use Piwik\Filechecks;
use Piwik\Filesystem;
use Piwik\Http;
use Piwik\Option;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\Plugin\ReleaseChannels;
use Piwik\Plugins\CorePluginsAdmin\PluginInstaller;
use Piwik\Plugins\Marketplace\API as MarketplaceApi;
use Piwik\Plugins\Marketplace\Marketplace;
use Piwik\SettingsServer;
use Piwik\Translation\Translator;
use Piwik\Unzip;
use Piwik\Version;
class Updater
{
public const OPTION_LATEST_VERSION = 'UpdateCheck_LatestVersion';
public const PATH_TO_EXTRACT_LATEST_VERSION = '/latest/';
public const DOWNLOAD_TIMEOUT = 720;
/**
* @var Translator
*/
private $translator;
/**
* @var ReleaseChannels
*/
private $releaseChannels;
/**
* @var string
*/
private $tmpPath;
public function __construct(Translator $translator, ReleaseChannels $releaseChannels, $tmpPath)
{
$this->translator = $translator;
$this->releaseChannels = $releaseChannels;
$this->tmpPath = $tmpPath;
}
/**
* Returns the latest available version number. Does not perform a check whether a later version is available.
*
* @return false|string
*/
public function getLatestVersion()
{
return Option::get(self::OPTION_LATEST_VERSION);
}
/**
* @return bool
*/
public function isNewVersionAvailable()
{
// Debian is the only way to update and check for updates
return false;
$latestVersion = self::getLatestVersion();
return $latestVersion && version_compare(Version::VERSION, $latestVersion) === -1;
}
/**
* Update Piwik codebase by downloading and installing the latest version.
*
* @param bool $https Whether to use HTTPS if supported of not. If false, will use HTTP.
* @return string[] Return an array of messages for the user.
* @throws ArchiveDownloadException
* @throws UpdaterException
* @throws Exception
*/
public function updatePiwik($https = true)
{
if (!$this->isNewVersionAvailable()) {
throw new Exception($this->translator->translate('CoreUpdater_ExceptionAlreadyLatestVersion', Version::VERSION));
}
SettingsServer::setMaxExecutionTime(0);
$newVersion = $this->getLatestVersion();
$url = $this->getArchiveUrl($newVersion, $https);
$messages = array();
try {
$archiveFile = $this->downloadArchive($newVersion, $url);
$messages[] = $this->translator->translate('CoreUpdater_DownloadingUpdateFromX', $url);
$extractedArchiveDirectory = $this->decompressArchive($archiveFile);
$messages[] = $this->translator->translate('CoreUpdater_UnpackingTheUpdate');
$this->verifyDecompressedArchive($extractedArchiveDirectory);
$messages[] = $this->translator->translate('CoreUpdater_VerifyingUnpackedFiles');
$this->installNewFiles($extractedArchiveDirectory);
$messages[] = $this->translator->translate('CoreUpdater_InstallingTheLatestVersion');
} catch (ArchiveDownloadException $e) {
throw $e;
} catch (Exception $e) {
throw new UpdaterException($e, $messages);
}
$validFor10Minutes = time() + (60 * 10);
$nonce = Common::generateUniqId();
Option::set('NonceOneClickUpdatePartTwo', json_encode(['nonce' => $nonce, 'ttl' => $validFor10Minutes]));
$cliMulti = new CliMulti();
$responses = $cliMulti->request(['?module=CoreUpdater&action=oneClickUpdatePartTwo&nonce=' . $nonce]);
if (!empty($responses)) {
$responseCliMulti = array_shift($responses);
$responseCliMulti = @json_decode($responseCliMulti, $assoc = true);
if (is_array($responseCliMulti)) {
// we expect a json encoded array response from oneClickUpdatePartTwo. Otherwise something went wrong.
$messages = array_merge($messages, $responseCliMulti);
} else {
// there was likely an error eg such as an invalid ssl certificate... let's try executing it directly
// in case this works. For explample $response is in this case not an array but a string because the "communcation"
// with the controller went wrong: "Got invalid response from API request: https://ABC/?module=CoreUpdater&action=oneClickUpdatePartTwo&nonce=ABC. Response was \'curl_exec: SSL certificate problem: unable to get local issuer certificate. Hostname requested was: ABC"
try {
$response = $this->oneClickUpdatePartTwo($newVersion);
if (!empty($response) && is_array($response)) {
$messages = array_merge($messages, $response);
}
} catch (Exception $e) {
// ignore any error should this fail too. this might be the case eg if
// the user upgrades from one major version to another major version
if (is_string($responseCliMulti)) {
$messages[] = $responseCliMulti; // show why the original request failed eg invalid ssl certificate
}
}
}
}
return $messages;
}
public function oneClickUpdatePartTwo($newVersion = null)
{
$messages = [];
if (!Marketplace::isMarketplaceEnabled()) {
$messages[] = 'Marketplace is disabled. Not updating any plugins.';
// prevent error Entry "Piwik\Plugins\Marketplace\Api\Client" cannot be resolved: Entry "Piwik\Plugins\Marketplace\Api\Service" cannot be resolved
} else {
if (!isset($newVersion)) {
$newVersion = Version::VERSION;
}
// we also need to make sure to create a new instance here as otherwise we would change the "global"
// environment, but we only want to change piwik version temporarily for this task here
$environment = StaticContainer::getContainer()->make('Piwik\Plugins\Marketplace\Environment');
$environment->setPiwikVersion($newVersion);
/** @var \Piwik\Plugins\Marketplace\Api\Client $marketplaceClient */
$marketplaceClient = StaticContainer::getContainer()->make('Piwik\Plugins\Marketplace\Api\Client', [
'environment' => $environment,
]);
try {
$messages[] = $this->translator->translate('CoreUpdater_CheckingForPluginUpdates');
$pluginManager = PluginManager::getInstance();
$pluginManager->loadAllPluginsAndGetTheirInfo();
$loadedPlugins = $pluginManager->getLoadedPlugins();
$marketplaceClient->clearAllCacheEntries();
$pluginsWithUpdate = $marketplaceClient->checkUpdates($loadedPlugins);
foreach ($pluginsWithUpdate as $pluginWithUpdate) {
$pluginName = $pluginWithUpdate['name'];
$messages[] = $this->translator->translate(
'CoreUpdater_UpdatingPluginXToVersionY',
[$pluginName, $pluginWithUpdate['version']]
);
$pluginInstaller = new PluginInstaller($marketplaceClient);
$pluginInstaller->installOrUpdatePluginFromMarketplace($pluginName);
}
} catch (MarketplaceApi\Exception $e) {
// there is a problem with the connection to the server, ignore for now
} catch (Exception $e) {
throw new UpdaterException($e, $messages);
}
}
try {
// incompatible plugins may have already been disabled in oneClickUpdatePartTwo
// if this might have failed we try it here again.
$disabledPluginNames = $this->disableIncompatiblePlugins($newVersion);
if (!empty($disabledPluginNames)) {
$messages[] = $this->translator->translate('CoreUpdater_DisablingIncompatiblePlugins', implode(', ', $disabledPluginNames));
}
} catch (\Throwable $e) {
throw new UpdaterException($e, $messages);
}
Filesystem::deleteAllCacheOnUpdate();
return $messages;
}
private function downloadArchive($version, $url)
{
$path = $this->tmpPath . self::PATH_TO_EXTRACT_LATEST_VERSION;
$archiveFile = $path . 'latest.zip';
Filechecks::dieIfDirectoriesNotWritable(array($path));
$url .= '?cb=' . $version;
try {
Http::fetchRemoteFile($url, $archiveFile, 0, self::DOWNLOAD_TIMEOUT);
} catch (Exception $e) {
// We throw a specific exception allowing to offer HTTP download if HTTPS failed
throw new ArchiveDownloadException($e);
}
return $archiveFile;
}
private function decompressArchive($archiveFile)
{
$extractionPath = $this->tmpPath . self::PATH_TO_EXTRACT_LATEST_VERSION;
foreach (['piwik', 'matomo'] as $flavor) {
$extractedArchiveDirectory = $extractionPath . $flavor;
// Remove previous decompressed archive
if (file_exists($extractedArchiveDirectory)) {
Filesystem::unlinkRecursive($extractedArchiveDirectory, true);
}
}
$archive = Unzip::factory('PclZip', $archiveFile);
$archiveFiles = $archive->extract($extractionPath);
if (0 == $archiveFiles) {
throw new Exception($this->translator->translate('CoreUpdater_ExceptionArchiveIncompatible', $archive->errorInfo()));
}
if (0 == count($archiveFiles)) {
throw new Exception($this->translator->translate('CoreUpdater_ExceptionArchiveEmpty'));
}
unlink($archiveFile);
foreach (['piwik', 'matomo'] as $flavor) {
$extractedArchiveDirectory = $extractionPath . $flavor;
if (file_exists($extractedArchiveDirectory)) {
return $extractedArchiveDirectory;
}
}
throw new \Exception('Could not find matomo or piwik directory in downloaded archive!');
}
private function verifyDecompressedArchive($extractedArchiveDirectory)
{
$someExpectedFiles = array(
'/config/global.ini.php',
'/index.php',
'/core/Piwik.php',
'/piwik.php',
'/matomo.php',
'/plugins/API/API.php',
);
foreach ($someExpectedFiles as $file) {
if (!is_file($extractedArchiveDirectory . $file)) {
throw new Exception($this->translator->translate('CoreUpdater_ExceptionArchiveIncomplete', $file));
}
}
}
private function disableIncompatiblePlugins($version)
{
$pluginManager = PluginManager::getInstance();
$plugins = $pluginManager->getLoadedPlugins();
foreach ($plugins as $plugin) {
$plugin->reloadPluginInformation();
}
$incompatiblePlugins = $this->getIncompatiblePlugins($version);
$disabledPluginNames = array();
foreach ($incompatiblePlugins as $plugin) {
$name = $plugin->getPluginName();
PluginManager::getInstance()->deactivatePlugin($name);
$disabledPluginNames[] = $name;
}
return $disabledPluginNames;
}
private function installNewFiles($extractedArchiveDirectory)
{
// Make sure the execute bit is set for this shell script
if (!Rules::isBrowserTriggerEnabled()) {
@chmod($extractedArchiveDirectory . '/misc/cron/archive.sh', 0755);
}
$model = new Model();
// Check if the target directories are writable
$this->checkFolderPermissions($extractedArchiveDirectory, PIWIK_INCLUDE_PATH);
/*
* Copy all files to PIWIK_INCLUDE_PATH.
* These files are accessed through the dispatcher.
*/
Filesystem::copyRecursive($extractedArchiveDirectory, PIWIK_INCLUDE_PATH);
$model->removeGoneFiles($extractedArchiveDirectory, PIWIK_INCLUDE_PATH);
/*
* These files are visible in the web root and are generally
* served directly by the web server. May be shared.
*/
if (PIWIK_INCLUDE_PATH !== PIWIK_DOCUMENT_ROOT) { // @phpstan-ignore notIdentical.alwaysFalse
// Copy PHP files that expect to be in the document root
$specialCases = array(
'/index.php',
'/piwik.php',
'/js/index.php',
);
foreach ($specialCases as $file) {
Filesystem::copy($extractedArchiveDirectory . $file, PIWIK_DOCUMENT_ROOT . $file);
}
// Copy the non-PHP files (e.g., images, css, javascript)
Filesystem::copyRecursive($extractedArchiveDirectory, PIWIK_DOCUMENT_ROOT, true);
$model->removeGoneFiles($extractedArchiveDirectory, PIWIK_DOCUMENT_ROOT);
}
Filesystem::unlinkRecursive($extractedArchiveDirectory, true);
Filesystem::clearPhpCaches();
}
/**
* @param string $version
* @param bool $https Whether to use HTTPS if supported of not. If false, will use HTTP.
* @return string
*/
public function getArchiveUrl($version, $https = true)
{
$channel = $this->releaseChannels->getActiveReleaseChannel();
$url = $channel->getDownloadUrlWithoutScheme($version);
if (Http::isUpdatingOverHttps() && $https && GeneralConfig::getConfigValue('force_matomo_http_request') == 0) {
$url = 'https' . $url;
} else {
$url = 'http' . $url;
}
return $url;
}
private function getIncompatiblePlugins($piwikVersion)
{
return PluginManager::getInstance()->getIncompatiblePlugins($piwikVersion);
}
/**
* check if the target file directory is writeable
* @param string $source
* @param string $target
* @throws Exception
*/
private function checkFolderPermissions($source, $target)
{
$wrongPermissionDir = [];
if (is_dir($source)) {
$d = dir($source);
while (false !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
$sourcePath = $source . '/' . $entry;
if (is_dir($sourcePath) && !is_writable($target . '/' . $entry)) {
//add the wrong permission to the array
$wrongPermissionDir[] = $target . '/' . $entry;
}
}
}
if (!empty($wrongPermissionDir)) {
throw new Exception($this->translator->translate(
'CoreUpdater_ExceptionDirWrongPermission',
implode(', ', $wrongPermissionDir)
));
}
}
}
|