matomo
Last commit date
app
5 years ago
assets
5 years ago
classes
5 years ago
config
5 years ago
languages
6 years ago
plugins
5 years ago
.htaccess
6 years ago
LEGALNOTICE
6 years ago
LICENSE
6 years ago
matomo.php
5 years ago
readme.txt
5 years ago
shared.php
6 years ago
uninstall.php
6 years ago
matomo.php
210 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: Matomo Analytics - Ethical Stats. Powerful Insights. |
| 4 | * Description: The #1 Google Analytics alternative that gives you full control over your data and protects the privacy for your users. Free, secure and open. |
| 5 | * Author: Matomo |
| 6 | * Author URI: https://matomo.org |
| 7 | * Version: 4.1.0 |
| 8 | * Domain Path: /languages |
| 9 | * WC requires at least: 2.4.0 |
| 10 | * WC tested up to: 4.8.0 |
| 11 | * |
| 12 | * Matomo - free/libre analytics platform |
| 13 | * |
| 14 | * @link https://matomo.org |
| 15 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 16 | * @package matomo |
| 17 | */ |
| 18 | |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; // if accessed directly |
| 21 | } |
| 22 | |
| 23 | load_plugin_textdomain( 'matomo', false, basename( dirname( __FILE__ ) ) . '/languages' ); |
| 24 | |
| 25 | if ( ! defined( 'MATOMO_ANALYTICS_FILE' ) ) { |
| 26 | define( 'MATOMO_ANALYTICS_FILE', __FILE__ ); |
| 27 | } |
| 28 | |
| 29 | if ( ! defined('MATOMO_MARKETPLACE_PLUGIN_NAME' )) { |
| 30 | define( 'MATOMO_MARKETPLACE_PLUGIN_NAME', 'matomo-marketplace-for-wordpress/matomo-marketplace-for-wordpress.php' ); |
| 31 | } |
| 32 | |
| 33 | $GLOBALS['MATOMO_PLUGINS_ENABLED'] = array(); |
| 34 | |
| 35 | /** MATOMO_PLUGIN_FILES => used to check for updates etc */ |
| 36 | $GLOBALS['MATOMO_PLUGIN_FILES'] = array( MATOMO_ANALYTICS_FILE ); |
| 37 | |
| 38 | function matomo_has_compatible_content_dir() { |
| 39 | if ( !empty( $_SERVER['MATOMO_WP_ROOT_PATH'] ) |
| 40 | && file_exists( rtrim($_SERVER['MATOMO_WP_ROOT_PATH'], '/') . '/wp-load.php' ) ) { |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | if ( ! defined( 'WP_CONTENT_DIR' ) ) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | $contentDir = rtrim(rtrim( WP_CONTENT_DIR, '/' ), DIRECTORY_SEPARATOR ); |
| 49 | $contentDir = wp_normalize_path($contentDir); |
| 50 | $absPath = wp_normalize_path(ABSPATH); |
| 51 | |
| 52 | $absPaths = array( |
| 53 | $absPath . 'wp-content', |
| 54 | $absPath . '/wp-content', |
| 55 | $absPath . DIRECTORY_SEPARATOR . 'wp-content' |
| 56 | ); |
| 57 | |
| 58 | if (in_array($contentDir, $absPaths, true)) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | $wpload_base = '../../../wp-load.php'; |
| 63 | $wpload_full = dirname( __FILE__ ) . '/' . $wpload_base; |
| 64 | if ( file_exists($wpload_full ) && is_readable( $wpload_full ) ) { |
| 65 | return true; |
| 66 | } elseif (realpath( $wpload_full ) && file_exists(realpath( $wpload_full )) && is_readable(realpath( $wpload_full ))) { |
| 67 | return true; |
| 68 | } elseif (!empty($_SERVER['SCRIPT_FILENAME']) && file_exists($_SERVER['SCRIPT_FILENAME'])) { |
| 69 | // seems symlinked... eg the wp-content dir or wp-content/plugins dir is symlinked from some very much other place... |
| 70 | $wpload_full = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . $wpload_base; |
| 71 | if ( file_exists($wpload_full ) ) { |
| 72 | return true; |
| 73 | } elseif (realpath( $wpload_full ) && file_exists(realpath( $wpload_full ))) { |
| 74 | return true; |
| 75 | } elseif (file_exists(dirname( $_SERVER['SCRIPT_FILENAME'] )) . '/wp-load.php') { |
| 76 | return true; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // look in plugins directory if there is a config file for us |
| 81 | $wpload_config = dirname(__FILE__) . '/../matomo.wpload_dir.php'; |
| 82 | if (file_exists( $wpload_config) && is_readable($wpload_config)) { |
| 83 | $content = @file_get_contents($wpload_config); // we do not include that file for security reasons |
| 84 | if (!empty($content)) { |
| 85 | $content = str_replace(array('<?php', 'exit;'), '', $content); |
| 86 | $content = preg_replace('/\s/', '', $content); |
| 87 | $content = trim(ltrim(trim($content), '#')); // the path may be commented out # /abs/path |
| 88 | if (strpos($content, DIRECTORY_SEPARATOR) === 0) { |
| 89 | $wpload_file = rtrim($content, DIRECTORY_SEPARATOR) . '/wp-load.php'; |
| 90 | return file_exists($wpload_file) && is_readable($wpload_file); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | function matomo_header_icon( $full = false ) { |
| 99 | $file = 'logo'; |
| 100 | if ($full) { |
| 101 | $file = 'logo-full'; |
| 102 | } |
| 103 | echo '<img height="32" src="' . plugins_url( 'assets/img/'.$file.'.png', MATOMO_ANALYTICS_FILE ) . '" class="matomo-header-icon">'; |
| 104 | } |
| 105 | |
| 106 | function matomo_is_app_request() { |
| 107 | return ! empty( $_SERVER['SCRIPT_NAME'] ) |
| 108 | && ( substr( $_SERVER['SCRIPT_NAME'], - 1 * strlen( 'matomo/app/index.php' ) ) === 'matomo/app/index.php' ); |
| 109 | } |
| 110 | |
| 111 | function matomo_has_tag_manager() { |
| 112 | if ( defined( 'MATOMO_ENABLE_TAG_MANAGER' ) ) { |
| 113 | return !empty(MATOMO_ENABLE_TAG_MANAGER); |
| 114 | } |
| 115 | |
| 116 | $is_multisite = function_exists( 'is_multisite' ) && is_multisite(); |
| 117 | if ( $is_multisite ) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | function matomo_anonymize_value( $value ) { |
| 125 | if ( is_string( $value ) && ! empty( $value ) ) { |
| 126 | $values_to_anonymize = array( |
| 127 | ABSPATH => '$ABSPATH/', |
| 128 | str_replace( '/', '\/', ABSPATH ) => '$ABSPATH\/', |
| 129 | str_replace( '/', '\\', ABSPATH ) => '$ABSPATH\/', |
| 130 | WP_CONTENT_DIR => '$WP_CONTENT_DIR/', |
| 131 | str_replace( '/', '\\', WP_CONTENT_DIR ) => '$WP_CONTENT_DIR\\', |
| 132 | home_url() => '$home_url', |
| 133 | site_url() => '$site_url', |
| 134 | DB_PASSWORD => '$DB_PASSWORD', |
| 135 | DB_USER => '$DB_USER', |
| 136 | DB_HOST => '$DB_HOST', |
| 137 | DB_NAME => '$DB_NAME', |
| 138 | ); |
| 139 | $keys = array('AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'AUTH_SALT', 'NONCE_KEY', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT'); |
| 140 | foreach ($keys as $key) { |
| 141 | if (defined($key)) { |
| 142 | $const_value = constant($key); |
| 143 | if (!empty($const_value) && is_string($const_value) && strlen($key) > 3) { |
| 144 | $values_to_anonymize[$const_value] = '$' . $key; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | foreach ( $values_to_anonymize as $search => $replace ) { |
| 149 | if ($search) { |
| 150 | $value = str_replace( $search, $replace, $value ); |
| 151 | } |
| 152 | } |
| 153 | // replace anything like token_auth etc or md5 or sha1 ... |
| 154 | $value = preg_replace('/[[:xdigit:]]{31,80}/', 'TOKEN_REPLACED', $value); |
| 155 | } |
| 156 | |
| 157 | return $value; |
| 158 | } |
| 159 | |
| 160 | $GLOBALS['MATOMO_MARKETPLACE_PLUGINS'] = array(); |
| 161 | |
| 162 | function matomo_add_plugin( $plugins_directory, $wp_plugin_file, $is_marketplace_plugin = false ) { |
| 163 | if ( ! in_array( $wp_plugin_file, $GLOBALS['MATOMO_PLUGIN_FILES'], true ) ) { |
| 164 | $GLOBALS['MATOMO_PLUGIN_FILES'][] = $wp_plugin_file; |
| 165 | } |
| 166 | |
| 167 | if ( empty( $GLOBALS['MATOMO_PLUGIN_DIRS'] ) ) { |
| 168 | $GLOBALS['MATOMO_PLUGIN_DIRS'] = array(); |
| 169 | } |
| 170 | |
| 171 | if ( $is_marketplace_plugin && dirname( $wp_plugin_file ) === $plugins_directory ) { |
| 172 | $GLOBALS['MATOMO_MARKETPLACE_PLUGINS'][] = $wp_plugin_file; |
| 173 | } |
| 174 | |
| 175 | $GLOBALS['MATOMO_PLUGINS_ENABLED'][] = basename( $plugins_directory ); |
| 176 | $root_dir = dirname( $plugins_directory ); |
| 177 | foreach ( $GLOBALS['MATOMO_PLUGIN_DIRS'] as $path ) { |
| 178 | if ( $path['pluginsPathAbsolute'] === $root_dir ) { |
| 179 | return; // already added |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | $matomo_dir = __DIR__ . '/app'; |
| 184 | $matomo_dir_parts = explode( DIRECTORY_SEPARATOR, $matomo_dir ); |
| 185 | $root_dir_parts = explode( DIRECTORY_SEPARATOR, $root_dir ); |
| 186 | $webroot_dir = ''; |
| 187 | foreach ( $matomo_dir_parts as $index => $part ) { |
| 188 | if ( isset( $root_dir_parts[ $index ] ) && $root_dir_parts[ $index ] === $part ) { |
| 189 | continue; |
| 190 | } |
| 191 | $webroot_dir .= '../'; |
| 192 | } |
| 193 | $GLOBALS['MATOMO_PLUGIN_DIRS'][] = array( |
| 194 | 'pluginsPathAbsolute' => $root_dir, |
| 195 | 'webrootDirRelativeToMatomo' => $webroot_dir, |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | if (matomo_is_app_request() || !empty($GLOBALS['MATOMO_LOADED_DIRECTLY'])) { |
| 200 | // prevent layout being broken when thegem theme is used. their lazy items class causes the reporting UI to not appear |
| 201 | // because it creates a JS error because of escaping " too often. only breaks when " Activate image loading optimization (for desktops)" |
| 202 | // is enabled in the general theme settings |
| 203 | add_filter('thegem_lazy_items_need_process_content', '__return_false', 99999999, $args = 0); |
| 204 | } |
| 205 | |
| 206 | require_once __DIR__ . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'WpMatomo.php'; |
| 207 | require 'shared.php'; |
| 208 | matomo_add_plugin( __DIR__ . '/plugins/WordPress', MATOMO_ANALYTICS_FILE ); |
| 209 | new WpMatomo(); |
| 210 |