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