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
Paths.php
219 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 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // if accessed directly |
| 14 | } |
| 15 | |
| 16 | class Paths { |
| 17 | |
| 18 | private function get_file_system() { |
| 19 | if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 20 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 21 | WP_Filesystem(); |
| 22 | } |
| 23 | |
| 24 | if ( ! class_exists( '\WP_Filesystem_Direct' ) ) { |
| 25 | require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php'; |
| 26 | require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php'; |
| 27 | } |
| 28 | |
| 29 | return new \WP_Filesystem_Direct( new \stdClass() ); |
| 30 | } |
| 31 | |
| 32 | public function get_upload_base_url() { |
| 33 | $upload_dir = wp_upload_dir(); |
| 34 | $path_upload_url = $upload_dir['baseurl']; |
| 35 | |
| 36 | return rtrim( $path_upload_url, '/' ) . '/' . MATOMO_UPLOAD_DIR; |
| 37 | } |
| 38 | |
| 39 | public function get_upload_base_dir() { |
| 40 | $upload_dir = wp_upload_dir(); |
| 41 | $path_upload_dir = $upload_dir['basedir']; |
| 42 | $path_upload_dir = rtrim( $path_upload_dir, '/' ) . '/' . MATOMO_UPLOAD_DIR; |
| 43 | |
| 44 | return $path_upload_dir; |
| 45 | } |
| 46 | |
| 47 | public function get_matomo_js_upload_path() { |
| 48 | return $this->get_upload_base_dir() . '/' . MATOMO_JS_NAME; |
| 49 | } |
| 50 | |
| 51 | public function get_config_ini_path() { |
| 52 | return $this->get_upload_base_dir() . '/' . MATOMO_CONFIG_PATH; |
| 53 | } |
| 54 | |
| 55 | public function get_tracker_api_rest_api_endpoint() { |
| 56 | return path_join( get_rest_url(), API::VERSION . '/' . API::ROUTE_HIT . '/' ); |
| 57 | } |
| 58 | |
| 59 | public function get_tracker_api_url_in_matomo_dir() { |
| 60 | return plugins_url( 'app/matomo.php', MATOMO_ANALYTICS_FILE ); |
| 61 | } |
| 62 | |
| 63 | public function get_js_tracker_rest_api_endpoint() { |
| 64 | return $this->get_tracker_api_rest_api_endpoint(); |
| 65 | } |
| 66 | |
| 67 | public function get_js_tracker_url_in_matomo_dir() { |
| 68 | $paths = new Paths(); |
| 69 | |
| 70 | if ( file_exists( $paths->get_matomo_js_upload_path() ) ) { |
| 71 | return $this->get_upload_base_url() . '/' . MATOMO_JS_NAME; |
| 72 | } |
| 73 | |
| 74 | return plugins_url( 'app/matomo.js', MATOMO_ANALYTICS_FILE ); |
| 75 | } |
| 76 | |
| 77 | public function get_tmp_dir() { |
| 78 | $is_multi_site = function_exists( 'is_multisite' ) && is_multisite(); |
| 79 | |
| 80 | $cache_dir_alternative = $this->get_upload_base_dir() . '/tmp'; |
| 81 | $base_cache_dir = WP_CONTENT_DIR . '/cache'; |
| 82 | $default_cache_dir = $base_cache_dir . '/' . MATOMO_UPLOAD_DIR; |
| 83 | |
| 84 | if ( ! $is_multi_site && |
| 85 | ( ( is_writable( WP_CONTENT_DIR ) && ! is_dir( $base_cache_dir ) ) |
| 86 | || is_writable( $base_cache_dir ) ) ) { |
| 87 | // we prefer wp-content/cache |
| 88 | $cache_dir = $default_cache_dir; |
| 89 | |
| 90 | if ( ! is_dir( $cache_dir ) ) { |
| 91 | wp_mkdir_p( $cache_dir ); |
| 92 | } |
| 93 | |
| 94 | if ( ! is_writable( $cache_dir ) ) { |
| 95 | // wasn't made writable for some reason so we prefer to use the upload dir just to be safe |
| 96 | $cache_dir = $cache_dir_alternative; |
| 97 | } |
| 98 | } else { |
| 99 | // fallback wp-content/uploads/matomo/tmp if $defaultCacheDir is not writable or if multisite is used |
| 100 | // with multisite we need to make sure to cache files per site |
| 101 | $cache_dir = $cache_dir_alternative; |
| 102 | |
| 103 | if ( ! is_dir( $cache_dir ) ) { |
| 104 | wp_mkdir_p( $cache_dir ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return $cache_dir; |
| 109 | } |
| 110 | |
| 111 | public function get_relative_dir_to_matomo( $target_dir ) { |
| 112 | $matomo_dir = plugin_dir_path( MATOMO_ANALYTICS_FILE ) . 'app'; |
| 113 | $matomo_dir_parts = explode( DIRECTORY_SEPARATOR, $matomo_dir ); |
| 114 | $target_dir_parts = explode( DIRECTORY_SEPARATOR, $target_dir ); |
| 115 | $relative_directory = ''; |
| 116 | $add_at_the_end = array(); |
| 117 | $was_previous_same = false; |
| 118 | |
| 119 | foreach ( $target_dir_parts as $index => $part ) { |
| 120 | if ( isset( $matomo_dir_parts[ $index ] ) |
| 121 | && 'matomo' !== $part // not when matomo is same part cause it's the plugin name but eg also the upload folder name and it would generate wrong path |
| 122 | && $matomo_dir_parts[ $index ] === $part |
| 123 | && ! $was_previous_same ) { |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | $was_previous_same = true; |
| 128 | |
| 129 | if ( isset( $matomo_dir_parts[ $index ] ) ) { |
| 130 | $relative_directory .= '../'; |
| 131 | } |
| 132 | $add_at_the_end[] = $part; |
| 133 | } |
| 134 | |
| 135 | return $relative_directory . implode( '/', $add_at_the_end ); |
| 136 | } |
| 137 | |
| 138 | public function get_gloal_upload_dir_if_possible( $file_to_look_for = '' ) { |
| 139 | if ( defined( 'MATOMO_GLOBAL_UPLOAD_DIR' ) ) { |
| 140 | return MATOMO_GLOBAL_UPLOAD_DIR; |
| 141 | } |
| 142 | |
| 143 | $path_upload_dir = $this->get_upload_base_dir(); |
| 144 | |
| 145 | if ( ! is_multisite() || is_network_admin() ) { |
| 146 | return $path_upload_dir; |
| 147 | } |
| 148 | |
| 149 | if ( preg_match( '/sites\/(\d)+$/', $path_upload_dir ) ) { |
| 150 | $path_upload_dir = preg_replace( '/sites\/(\d)+$/', '', $path_upload_dir ); |
| 151 | } else { |
| 152 | // re-implement _wp_upload_dir to find hopefully the upload_dir for the network site |
| 153 | $upload_path = trim( get_option( 'upload_path' ) ); |
| 154 | if ( empty( $upload_path ) || 'wp-content/uploads' === $upload_path ) { |
| 155 | $path_upload_dir = WP_CONTENT_DIR . '/uploads'; |
| 156 | } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) { |
| 157 | // $dir is absolute, $upload_path is (maybe) relative to ABSPATH |
| 158 | $path_upload_dir = path_join( ABSPATH, $upload_path ); |
| 159 | } else { |
| 160 | $path_upload_dir = $upload_path; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if ( ! empty( $file_to_look_for ) ) { |
| 165 | $file_to_look_for = MATOMO_UPLOAD_DIR . '/' . ltrim( $file_to_look_for, '/' ); |
| 166 | } |
| 167 | |
| 168 | if ( ! empty( $file_to_look_for ) |
| 169 | && ! file_exists( $path_upload_dir . $file_to_look_for ) ) { |
| 170 | // seems we haven't auto detected the right one yet... (or it is not yet installed) |
| 171 | // we go up the site upload dir step by step to try and find the network upload dir |
| 172 | $parent_dir = $path_upload_dir; |
| 173 | do { |
| 174 | $parent_dir = dirname( $parent_dir ); |
| 175 | if ( file_exists( $parent_dir . $file_to_look_for ) ) { |
| 176 | return $parent_dir; |
| 177 | } |
| 178 | } while ( strpos( $parent_dir, ABSPATH ) === 0 ); // we don't go outside WP dir |
| 179 | } |
| 180 | |
| 181 | $path_upload_dir = rtrim( $path_upload_dir, '/' ) . '/' . MATOMO_UPLOAD_DIR; |
| 182 | |
| 183 | return $path_upload_dir; |
| 184 | } |
| 185 | |
| 186 | public function clear_assets_dir() { |
| 187 | $tmp_dir = $this->get_tmp_dir() . '/assets'; |
| 188 | if ( $tmp_dir && is_dir( $tmp_dir ) ) { |
| 189 | $file_system_direct = $this->get_file_system(); |
| 190 | $file_system_direct->rmdir( $tmp_dir, true ); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | public function clear_cache_dir() { |
| 195 | $tmp_dir = $this->get_tmp_dir(); |
| 196 | if ( $tmp_dir |
| 197 | && is_dir( $tmp_dir ) |
| 198 | && is_dir( $tmp_dir . '/cache' ) ) { |
| 199 | // we make sure it's a matomo cache dir to not delete something falsely |
| 200 | $file_system_direct = $this->get_file_system(); |
| 201 | $file_system_direct->rmdir( $tmp_dir, true ); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | public function uninstall() { |
| 206 | $this->clear_cache_dir(); |
| 207 | |
| 208 | $dir = $this->get_upload_base_dir(); |
| 209 | |
| 210 | $file_system_direct = $this->get_file_system(); |
| 211 | $file_system_direct->rmdir( $dir, true ); |
| 212 | |
| 213 | $global_dir = $this->get_upload_base_dir(); |
| 214 | if ( $global_dir && $global_dir !== $dir ) { |
| 215 | $file_system_direct->rmdir( $dir ); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 |