Admin
5 years ago
Commands
6 years ago
Db
6 years ago
Ecommerce
6 years ago
Report
6 years ago
Site
5 years ago
TrackingCode
5 years ago
User
5 years ago
views
6 years ago
API.php
5 years ago
Access.php
6 years ago
AjaxTracker.php
5 years ago
Annotations.php
6 years ago
Bootstrap.php
6 years ago
Capabilities.php
6 years ago
Compatibility.php
6 years ago
Email.php
5 years ago
Installer.php
6 years ago
Logger.php
5 years ago
OptOut.php
5 years ago
Paths.php
6 years ago
PrivacyBadge.php
6 years ago
Referral.php
6 years ago
Roles.php
6 years ago
ScheduledTasks.php
6 years ago
Settings.php
5 years ago
Site.php
6 years ago
TrackingCode.php
5 years ago
Uninstaller.php
6 years ago
Updater.php
6 years ago
User.php
6 years ago
Updater.php
165 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * @package matomo |
| 8 | */ |
| 9 | |
| 10 | namespace WpMatomo; |
| 11 | |
| 12 | use Piwik\Filesystem; |
| 13 | use Piwik\Plugins\CoreUpdater\CoreUpdater; |
| 14 | use Piwik\Plugins\Installation\ServerFilesGenerator; |
| 15 | use Piwik\Version; |
| 16 | |
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | exit; // if accessed directly |
| 19 | } |
| 20 | |
| 21 | class Updater { |
| 22 | /** |
| 23 | * @var Settings |
| 24 | */ |
| 25 | private $settings; |
| 26 | |
| 27 | /** |
| 28 | * @var Logger |
| 29 | */ |
| 30 | private $logger; |
| 31 | |
| 32 | public function __construct( Settings $settings ) { |
| 33 | $this->settings = $settings; |
| 34 | $this->logger = new Logger(); |
| 35 | } |
| 36 | |
| 37 | private function load_plugin_functions() { |
| 38 | if ( ! function_exists( 'get_plugin_data' ) ) { |
| 39 | require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 40 | } |
| 41 | |
| 42 | return function_exists( 'get_plugin_data' ); |
| 43 | } |
| 44 | |
| 45 | public function update_if_needed() { |
| 46 | if ( ! $this->load_plugin_functions() ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | $executed_updates = array(); |
| 51 | |
| 52 | $plugin_files = $GLOBALS['MATOMO_PLUGIN_FILES']; |
| 53 | if ( ! in_array( MATOMO_ANALYTICS_FILE, $plugin_files, true ) ) { |
| 54 | $plugin_files[] = MATOMO_ANALYTICS_FILE; |
| 55 | // making sure this plugin is in the list so when itself gets updated |
| 56 | // it will execute the core updates |
| 57 | } |
| 58 | |
| 59 | foreach ( $GLOBALS['MATOMO_PLUGIN_FILES'] as $plugin_file ) { |
| 60 | $plugin_data = get_plugin_data( $plugin_file, $markup = false, $translate = false ); |
| 61 | |
| 62 | $key = Settings::OPTION_PREFIX . 'plugin-version-' . basename( str_ireplace( '.php', '', $plugin_file ) ); |
| 63 | $installed_ver = get_option( $key ); |
| 64 | if ( ! $installed_ver || $installed_ver !== $plugin_data['Version'] ) { |
| 65 | if ( ! Installer::is_intalled() ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | try { |
| 70 | $this->update(); |
| 71 | } catch ( \Exception $e ) { |
| 72 | $this->logger->log_exception( 'plugin_update', $e ); |
| 73 | throw $e; |
| 74 | } |
| 75 | $executed_updates[] = $key; |
| 76 | |
| 77 | // we're scheduling another update in case there are some dimensions to be updated or anything |
| 78 | // we do not do this in the "update" method as otherwise we might be calling this recursively... |
| 79 | // it is possible that because the plugins need to be reloaded etc that those updates are not executed right |
| 80 | // away but need an actual reload and cache clearance etc |
| 81 | wp_schedule_single_event( time() + 5, ScheduledTasks::EVENT_UPDATE ); |
| 82 | |
| 83 | update_option( $key, $plugin_data['Version'] ); |
| 84 | |
| 85 | // we make sure to delete cache even if no component was updated eg there may be translation updates etc |
| 86 | // and caches need to be invalidated |
| 87 | Filesystem::deleteAllCacheOnUpdate(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return $executed_updates; |
| 92 | } |
| 93 | |
| 94 | public function update() { |
| 95 | Bootstrap::do_bootstrap(); |
| 96 | |
| 97 | if ( $this->load_plugin_functions() ) { |
| 98 | $plugin_data = get_plugin_data( MATOMO_ANALYTICS_FILE, $markup = false, $translate = false ); |
| 99 | |
| 100 | $history = $this->settings->get_global_option( 'version_history' ); |
| 101 | if ( empty( $history ) || ! is_array( $history ) ) { |
| 102 | $history = array(); |
| 103 | } |
| 104 | |
| 105 | if ( ! empty( $plugin_data['Version'] ) |
| 106 | && ! in_array( $plugin_data['Version'], $history, true ) ) { |
| 107 | // this allows us to see which versions of matomo the user was using before this update so we better understand |
| 108 | // which version maybe regressed something |
| 109 | array_unshift( $history, $plugin_data['Version'] ); |
| 110 | $history = array_slice( $history, 0, 5 ); // lets keep only the last 5 versions |
| 111 | $this->settings->set_global_option( 'version_history', $history ); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $this->settings->set_global_option( 'core_version', Version::VERSION ); |
| 116 | $this->settings->save(); |
| 117 | |
| 118 | $paths = new Paths(); |
| 119 | $paths->clear_cache_dir(); |
| 120 | |
| 121 | \Piwik\Access::doAsSuperUser( |
| 122 | function () { |
| 123 | self::update_components(); |
| 124 | self::update_components(); |
| 125 | } |
| 126 | ); |
| 127 | |
| 128 | $upload_dir = $paths->get_upload_base_dir(); |
| 129 | if ( is_dir( $upload_dir ) && is_writable( $upload_dir ) ) { |
| 130 | @file_put_contents( $upload_dir . '/index.php', '//hello' ); |
| 131 | @file_put_contents( $upload_dir . '/index.html', '//hello' ); |
| 132 | @file_put_contents( $upload_dir . '/index.htm', '//hello' ); |
| 133 | @file_put_contents( |
| 134 | $upload_dir . '/.htaccess', |
| 135 | '<Files ~ "(\.mmdb)$"> |
| 136 | ' . ServerFilesGenerator::getDenyHtaccessContent() . ' |
| 137 | </Files> |
| 138 | <Files ~ "(\.js)$"> |
| 139 | ' . ServerFilesGenerator::getAllowHtaccessContent() . ' |
| 140 | </Files>' |
| 141 | ); |
| 142 | } |
| 143 | $config_dir = $paths->get_config_ini_path(); |
| 144 | if ( is_dir( $config_dir ) && is_writable( $config_dir ) ) { |
| 145 | @file_put_contents( $config_dir . '/index.php', '//hello' ); |
| 146 | @file_put_contents( $config_dir . '/index.html', '//hello' ); |
| 147 | @file_put_contents( $config_dir . '/index.htm', '//hello' ); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | private static function update_components() { |
| 152 | $updater = new \Piwik\Updater(); |
| 153 | $components_with_update_file = CoreUpdater::getComponentUpdates( $updater ); |
| 154 | |
| 155 | if ( empty( $components_with_update_file ) ) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | CoreUpdater::updateComponents( $updater, $components_with_update_file ); |
| 160 | |
| 161 | \Piwik\Updater::recordComponentSuccessfullyUpdated( 'core', Version::VERSION ); |
| 162 | Filesystem::deleteAllCacheOnUpdate(); |
| 163 | } |
| 164 | } |
| 165 |