config.php
196 lines
| 1 | <?php |
| 2 | |
| 3 | if (!defined( 'ABSPATH')) { |
| 4 | exit; // if accessed directly |
| 5 | } |
| 6 | |
| 7 | use Piwik\Cache; |
| 8 | use Piwik\Container\StaticContainer; |
| 9 | use Piwik\Option; |
| 10 | use Piwik\Plugin\API; |
| 11 | use WpMatomo\Capabilities; |
| 12 | use WpMatomo\Paths; |
| 13 | use WpMatomo\Settings; |
| 14 | |
| 15 | return array( |
| 16 | 'path.tmp' => function () { |
| 17 | $paths = new \WpMatomo\Paths(); |
| 18 | return $paths->get_tmp_dir(); |
| 19 | }, |
| 20 | 'path.misc.user' => function () { |
| 21 | $paths = new \WpMatomo\Paths(); |
| 22 | return $paths->get_relative_dir_to_matomo($paths->get_upload_base_dir()) . '/'; |
| 23 | }, |
| 24 | 'log.handlers' => array(), // required in the console |
| 25 | 'EnableDbVersionCheck' => false, |
| 26 | 'path.geoip2' => function () { |
| 27 | $paths = new \WpMatomo\Paths(); |
| 28 | return $paths->get_gloal_upload_dir_if_possible('DBIP-City.mmdb') . '/'; |
| 29 | }, |
| 30 | // we want to avoid the regular monolog logger as it could interfere with other plugins maybe. for now lets use a |
| 31 | // custom logger |
| 32 | 'Piwik\Log\LoggerInterface' => \Piwik\DI::get('\Piwik\Plugins\WordPress\Logger'), |
| 33 | // following two entries used by CoreAdminHome.runCronArchiving |
| 34 | 'log.short.format' => '%level% %tag%[%datetime%] %message%', |
| 35 | 'Piwik\Plugins\Monolog\Formatter\LineMessageFormatter' => \Piwik\DI::create('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter') |
| 36 | ->constructor(\Piwik\DI::get('log.short.format')), |
| 37 | 'TagManagerContainerStorageDir' => function () { |
| 38 | if (defined('MATOMO_TAG_MANAGER_STORAGE_DIR')) { |
| 39 | return MATOMO_TAG_MANAGER_STORAGE_DIR; |
| 40 | } |
| 41 | |
| 42 | // the location where we store the generated javascript or json container files |
| 43 | $paths = new \WpMatomo\Paths(); |
| 44 | return rtrim('/'. $paths->get_relative_dir_to_matomo($paths->get_upload_base_dir().'/', @realpath(MATOMO_ANALYTICS_FILE)), '/'); |
| 45 | }, |
| 46 | 'TagManagerContainerWebDir' => function () { |
| 47 | if (defined('MATOMO_TAG_MANAGER_WEB_DIR')) { |
| 48 | return MATOMO_TAG_MANAGER_WEB_DIR; |
| 49 | } |
| 50 | |
| 51 | // the location where we store the generated javascript or json container files |
| 52 | $paths = new \WpMatomo\Paths(); |
| 53 | return rtrim('/'. matomo_rel_path($paths->get_upload_base_dir() . '/', WP_PLUGIN_DIR . '/matomo/app'), '/'); |
| 54 | }, |
| 55 | 'Piwik\Plugins\Login\PasswordVerifier' => \Piwik\DI::autowire('Piwik\Plugins\WordPress\WpPasswordVerifier'), |
| 56 | 'Piwik\Session\SessionAuth' => \Piwik\DI::autowire('Piwik\Plugins\WordPress\SessionAuth'), |
| 57 | 'Piwik\Auth' => \Piwik\DI::autowire('Piwik\Plugins\WordPress\Auth'), |
| 58 | \Piwik\Config::class => \Piwik\DI::decorate(function ($previous) { |
| 59 | |
| 60 | \Piwik\Plugins\TagManager\TagManager::$enableAutoContainerCreation = false; |
| 61 | |
| 62 | if (defined('FORCE_SSL_ADMIN') && FORCE_SSL_ADMIN) { |
| 63 | $general = $previous->General; |
| 64 | $general['force_ssl'] = 1; |
| 65 | $general['assume_secure_protocol'] = 1; |
| 66 | $previous->General = $general; |
| 67 | } |
| 68 | |
| 69 | $database = $previous->database; |
| 70 | $previous->database = \WpMatomo\Installer::get_db_infos($database); |
| 71 | |
| 72 | $paths = new Paths(); |
| 73 | if ( file_exists( $paths->get_config_ini_path() ) ) { |
| 74 | $general = $previous->General; |
| 75 | |
| 76 | if (defined('MATOMO_TRIGGER_BROWSER_ARCHIVING')) { |
| 77 | $general['enable_browser_archiving_triggering'] = (int) MATOMO_TRIGGER_BROWSER_ARCHIVING; |
| 78 | } |
| 79 | |
| 80 | $matomo_salt_key = Settings::OPTION_PREFIX . 'matomo_salt'; |
| 81 | $matomo_salt = get_option($matomo_salt_key); // needs to be per site! |
| 82 | if (!$matomo_salt) { |
| 83 | $matomo_salt = \Piwik\Common::getRandomString(32); |
| 84 | update_option($matomo_salt_key, $matomo_salt, true); |
| 85 | } |
| 86 | |
| 87 | $general['salt'] = $matomo_salt; |
| 88 | |
| 89 | if (empty($general['trusted_hosts'])) { |
| 90 | $general['trusted_hosts'] = array(); |
| 91 | } |
| 92 | $site_url = site_url(); |
| 93 | if (!in_array($site_url, $general['trusted_hosts'])) { |
| 94 | $general['trusted_hosts'][] = $site_url; |
| 95 | } |
| 96 | $previous->General = $general; |
| 97 | |
| 98 | if (empty($GLOBALS['MATOMO_SWITCH_BLOG_SET_UP'])) { |
| 99 | // only execute it once since we might init this several times... |
| 100 | $GLOBALS['MATOMO_SWITCH_BLOG_SET_UP'] = true; |
| 101 | |
| 102 | add_action('switch_blog', function ($new_blog, $prev_blog) { |
| 103 | if ($new_blog == $prev_blog) { |
| 104 | return; |
| 105 | } |
| 106 | // ensure correct path to config is set, ensure to update tables_prefix etc. |
| 107 | \WpMatomo\Bootstrap::destroy_bootstrapped_environment(); |
| 108 | }, 10, 2); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $previous; |
| 113 | }), |
| 114 | 'Piwik\Mail\Transport' => \Piwik\DI::autowire('WpMatomo\Email'), |
| 115 | 'Piwik\Plugins\CustomJsTracker\TrackerUpdater' => \Piwik\DI::decorate(function ($previous) { |
| 116 | /** @var \Piwik\Plugins\CustomJsTracker\TrackerUpdater $previous */ |
| 117 | |
| 118 | $paths = new Paths(); |
| 119 | $dir = $paths->get_matomo_js_upload_path(); |
| 120 | |
| 121 | $previous->setToFile($dir); |
| 122 | |
| 123 | return $previous; |
| 124 | }), |
| 125 | 'diagnostics.optional' => \Piwik\DI::decorate(function ($checks) { |
| 126 | foreach ($checks as $index => $check) { |
| 127 | if ($check && is_object($check)) { |
| 128 | $class_name = get_class($check); |
| 129 | if ($class_name === 'Piwik\Plugins\Diagnostics\Diagnostic\ForceSSLCheck' |
| 130 | || $class_name === 'Piwik\Plugins\Diagnostics\Diagnostic\LoadDataInfileCheck' |
| 131 | || $class_name === 'Piwik\Plugins\CustomJsTracker\Diagnostic\TrackerJsCheck' |
| 132 | || $class_name === 'Piwik\Plugins\Diagnostics\Diagnostic\RequiredPrivateDirectories' // it doesn't resolve config path correctly as it is outside matomo dir etc |
| 133 | || $class_name === 'Piwik\Plugins\Diagnostics\Diagnostic\RecommendedPrivateDirectories' // tmp check doesn't work so far see matomo-org/matomo#18684 |
| 134 | || $class_name === 'Piwik\Plugins\Diagnostics\Diagnostic\CronArchivingCheck' |
| 135 | || $class_name === 'Piwik\Plugins\Diagnostics\Diagnostic\FileIntegrityCheck') { |
| 136 | $checks[$index] = null; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | return array_values(array_filter($checks)); |
| 141 | }), |
| 142 | 'diagnostics.disabled' => \Piwik\DI::add([ |
| 143 | \Piwik\DI::get(\Piwik\Plugins\Diagnostics\Diagnostic\PageSpeedCheck::class), |
| 144 | \Piwik\DI::get(\Piwik\Plugins\Diagnostics\Diagnostic\RecommendedPrivateDirectories::class), |
| 145 | ]), |
| 146 | 'observers.global' => \Piwik\DI::add(array( |
| 147 | array('FrontController.modifyErrorPage', \Piwik\DI::value(function (&$result, $ex) { |
| 148 | if (!empty($ex) && is_object($ex) && $ex instanceof \Piwik\Exception\NoWebsiteFoundException) { |
| 149 | // try to repair itself in case for some reason the site was not yet synced... on next reload it would |
| 150 | // then work |
| 151 | $sync = new \WpMatomo\Site\Sync(new Settings()); |
| 152 | $sync->sync_current_site(); |
| 153 | } |
| 154 | if (!empty($ex) |
| 155 | && is_object($ex) |
| 156 | && $ex instanceof \Piwik\Exception\NoPrivilegesException |
| 157 | && is_user_logged_in()) { |
| 158 | if (current_user_can(Capabilities::KEY_VIEW)) { |
| 159 | // some error... it looks like user should by synced but isn't yet |
| 160 | // could happen eg when in network activated mode the super admin changes permission and another |
| 161 | // user from a blog wants to access the UI while not all users are synced just yet |
| 162 | // try to repair itself in case for some reason the user was not yet synced... on next reload it would |
| 163 | // then work |
| 164 | $sync = new \WpMatomo\User\Sync(); |
| 165 | $sync->sync_current_users(); |
| 166 | } |
| 167 | } |
| 168 | })), |
| 169 | array('Db.getDatabaseConfig', \Piwik\DI::value(function (&$config) { |
| 170 | // we don't want to save these and instead detect them on demand. |
| 171 | // for security reasons etc we don't want to duplicate these values |
| 172 | include_once plugin_dir_path(MATOMO_ANALYTICS_FILE ) . 'classes/WpMatomo/Db/WordPress.php'; |
| 173 | })), |
| 174 | array('Tracker.getDatabaseConfig', \Piwik\DI::value(function (&$configDb) { |
| 175 | // we don't want to save these and instead detect them on demand. |
| 176 | // for security reasons etc we don't want to duplicate these values |
| 177 | include_once plugin_dir_path(MATOMO_ANALYTICS_FILE ) . 'classes/WpMatomo/Db/WordPress.php'; |
| 178 | })), |
| 179 | array('Config.beforeSave', \Piwik\DI::value(function (&$values) { |
| 180 | // we don't want to save these and instead detect them on demand. |
| 181 | // for security reasons etc we don't want to duplicate these values |
| 182 | unset($values['database']['host']); |
| 183 | unset($values['database']['username']); |
| 184 | unset($values['database']['password']); |
| 185 | unset($values['database']['dbname']); |
| 186 | unset($values['database']['tables_prefix']); |
| 187 | unset($values['database']['charset']); |
| 188 | unset($values['Plugins']); |
| 189 | unset($values['General']['enable_users_admin']); |
| 190 | unset($values['General']['enable_sites_admin']); |
| 191 | unset($values['General']['salt']); |
| 192 | })), |
| 193 | )), |
| 194 | |
| 195 | ); |
| 196 |