PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / Paths.php
matomo / classes / WpMatomo Last commit date
Admin 1 week ago Commands 2 years ago Db 3 weeks ago Ecommerce 1 week ago Report 1 week ago Site 1 week ago TrackingCode 1 week ago Updater 4 years ago User 1 week ago Workarounds 2 years ago WpStatistics 1 week ago views 1 week ago AIBotTracking.php 1 week ago API.php 3 weeks ago Access.php 1 week ago AjaxTracker.php 5 months ago Annotations.php 1 week ago Bootstrap.php 11 months ago Capabilities.php 1 week ago Compatibility.php 1 week ago Email.php 1 week ago ErrorNotice.php 1 week ago Feature.php 3 months ago Installer.php 1 week ago Logger.php 1 year ago MinimumRequirementsNotice.php 1 month ago OptOut.php 2 months ago Paths.php 1 week ago PluginActionLinks.php 3 months ago PluginAdminOverrides.php 1 week ago PluginInit.php 1 week ago PrivacyBadge.php 4 years ago RedirectOnActivation.php 3 months ago Referral.php 3 months ago Roles.php 3 months ago ScheduledTasks.php 1 week ago Settings.php 1 week ago Site.php 3 years ago TrackingCode.php 1 week ago Uninstaller.php 6 months ago Updater.php 1 week ago User.php 3 weeks ago
Paths.php
260 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 stdClass;
13 use WP_Filesystem_Direct;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // if accessed directly
17 }
18
19 /**
20 * phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_is_writable
21 */
22 class Paths {
23
24 private static $host_init_filesystem = false;
25 private static $host_init_filesystem_succeeded = false;
26
27 public function get_file_system() {
28 if ( ! function_exists( 'WP_Filesystem' ) ) {
29 require_once ABSPATH . '/wp-admin/includes/file.php';
30 }
31
32 if ( ! self::$host_init_filesystem ) {
33 self::$host_init_filesystem = true;
34 self::$host_init_filesystem_succeeded = WP_Filesystem();
35 }
36
37 if ( ! class_exists( '\WP_Filesystem_Direct' ) ) {
38 require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php';
39 require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php';
40 }
41
42 return new WP_Filesystem_Direct( new stdClass() );
43 }
44
45 public function get_host_init_filesystem_succeeded() {
46 return self::$host_init_filesystem_succeeded;
47 }
48
49 public function get_upload_base_url() {
50 $upload_dir = wp_upload_dir();
51 $path_upload_url = $upload_dir['baseurl'];
52
53 return rtrim( $path_upload_url, '/' ) . '/' . MATOMO_UPLOAD_DIR;
54 }
55
56 public function get_upload_base_dir() {
57 $upload_dir = wp_upload_dir();
58 $path_upload_dir = $upload_dir['basedir'];
59 $path_upload_dir = rtrim( $path_upload_dir, '/' ) . '/' . MATOMO_UPLOAD_DIR;
60
61 return $path_upload_dir;
62 }
63
64 public function get_matomo_js_upload_path() {
65 return $this->get_upload_base_dir() . '/' . MATOMO_JS_NAME;
66 }
67
68 public function get_config_ini_path() {
69 return $this->get_upload_base_dir() . '/' . MATOMO_CONFIG_PATH;
70 }
71
72 public function get_tracker_api_rest_api_endpoint() {
73 return path_join( get_rest_url(), API::VERSION . '/' . API::ROUTE_HIT . '/' );
74 }
75
76 public function get_tracker_api_url_in_matomo_dir() {
77 return plugins_url( 'app/matomo.php', MATOMO_ANALYTICS_FILE );
78 }
79
80 public function get_js_tracker_rest_api_endpoint() {
81 return $this->get_tracker_api_rest_api_endpoint();
82 }
83
84 public function get_js_tracker_url_in_matomo_dir() {
85 $paths = new Paths();
86
87 if ( file_exists( $paths->get_matomo_js_upload_path() ) ) {
88 return $this->get_upload_base_url() . '/' . MATOMO_JS_NAME;
89 }
90
91 return plugins_url( 'app/matomo.js', MATOMO_ANALYTICS_FILE );
92 }
93
94 public function get_tmp_dir() {
95 $is_multi_site = function_exists( 'is_multisite' ) && is_multisite();
96
97 $cache_dir_alternative = $this->get_upload_base_dir() . '/tmp';
98 $base_cache_dir = WP_CONTENT_DIR . '/cache';
99 $default_cache_dir = $base_cache_dir . '/' . MATOMO_UPLOAD_DIR;
100
101 if ( ! $is_multi_site &&
102 ( ( is_writable( WP_CONTENT_DIR ) && ! is_dir( $base_cache_dir ) )
103 || is_writable( $base_cache_dir ) ) ) {
104 // we prefer wp-content/cache
105 $cache_dir = $default_cache_dir;
106
107 if ( ! is_dir( $cache_dir ) ) {
108 wp_mkdir_p( $cache_dir );
109 }
110
111 if ( ! is_writable( $cache_dir ) ) {
112 // wasn't made writable for some reason so we prefer to use the upload dir just to be safe
113 $cache_dir = $cache_dir_alternative;
114 }
115 } else {
116 // fallback wp-content/uploads/matomo/tmp if $defaultCacheDir is not writable or if multisite is used
117 // with multisite we need to make sure to cache files per site
118 $cache_dir = $cache_dir_alternative;
119
120 if ( ! is_dir( $cache_dir ) ) {
121 wp_mkdir_p( $cache_dir );
122 }
123 }
124
125 return $cache_dir;
126 }
127
128 /**
129 * parameter matomo_file is required for the unit test cases (when checking with a path including the string matomo)
130 *
131 * @param string $target_dir
132 * @param string $matomo_file
133 *
134 * @return string
135 */
136 public function get_relative_dir_to_matomo( $target_dir, $matomo_file = MATOMO_ANALYTICS_FILE ) {
137 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
138 return matomo_rel_path( $target_dir, dirname( $matomo_file ) . '/app' );
139 }
140
141 public function get_gloal_upload_dir_if_possible( $file_to_look_for = '' ) {
142 if ( defined( 'MATOMO_GLOBAL_UPLOAD_DIR' ) ) {
143 return MATOMO_GLOBAL_UPLOAD_DIR;
144 }
145
146 $path_upload_dir = $this->get_upload_base_dir();
147
148 if ( ! is_multisite() || is_network_admin() ) {
149 return $path_upload_dir;
150 }
151
152 $global_path_upload_dir = $this->get_global_path_upload_dir_if_matches_site_specific_dir( $path_upload_dir );
153 if ( ! empty( $global_path_upload_dir ) ) {
154 $path_upload_dir = $global_path_upload_dir;
155 } else {
156 // re-implement _wp_upload_dir to find hopefully the upload_dir for the network site
157 $upload_path = trim( get_option( 'upload_path' ) );
158 if ( empty( $upload_path ) || 'wp-content/uploads' === $upload_path ) {
159 $path_upload_dir = WP_CONTENT_DIR . '/uploads';
160 } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
161 // $dir is absolute, $upload_path is (maybe) relative to ABSPATH
162 $path_upload_dir = path_join( ABSPATH, $upload_path );
163 } else {
164 $path_upload_dir = $upload_path;
165 }
166 }
167
168 if ( ! empty( $file_to_look_for ) ) {
169 $file_to_look_for = MATOMO_UPLOAD_DIR . '/' . ltrim( $file_to_look_for, '/' );
170 }
171
172 $path_upload_dir = rtrim( $path_upload_dir, '/' ) . '/';
173 if ( ! empty( $file_to_look_for )
174 && ! file_exists( $path_upload_dir . $file_to_look_for ) ) {
175 // seems we haven't auto detected the right one yet... (or it is not yet installed)
176 // we go up the site upload dir step by step to try and find the network upload dir
177 $parent_dir = $path_upload_dir;
178 do {
179 $parent_dir = dirname( $parent_dir );
180 $parent_dir = rtrim( $parent_dir, '/' ) . '/';
181 // NOTE: file_exists here can trigger an open_basedir warning on some setups
182 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
183 if ( @file_exists( $parent_dir . $file_to_look_for ) ) {
184 return $parent_dir;
185 }
186 } while ( strpos( $parent_dir, ABSPATH ) === 0 ); // we don't go outside WP dir
187 }
188
189 $path_upload_dir = rtrim( $path_upload_dir, '/' ) . '/' . MATOMO_UPLOAD_DIR;
190
191 return $path_upload_dir;
192 }
193
194 public function clear_assets_dir() {
195 $tmp_dir = $this->get_tmp_dir() . '/assets';
196 if ( $tmp_dir && is_dir( $tmp_dir ) ) {
197 $file_system_direct = $this->get_file_system();
198 $file_system_direct->rmdir( $tmp_dir, true );
199 }
200 }
201
202 public function clear_cache_dir() {
203 $tmp_dir = $this->get_tmp_dir();
204 if ( $tmp_dir
205 && is_dir( $tmp_dir )
206 && is_dir( $tmp_dir . '/cache' ) ) {
207 // we make sure it's a matomo cache dir to not delete something falsely
208 $file_system_direct = $this->get_file_system();
209 $file_system_direct->rmdir( $tmp_dir, true );
210 }
211 }
212
213 public function uninstall() {
214 $this->clear_cache_dir();
215
216 $dir = $this->get_upload_base_dir();
217
218 $file_system_direct = $this->get_file_system();
219 $file_system_direct->rmdir( $dir, true );
220
221 $global_dir = $this->get_upload_base_dir();
222 if ( $global_dir && $global_dir !== $dir ) {
223 $file_system_direct->rmdir( $dir );
224 }
225 }
226
227 /**
228 * Must be called after Paths::get_file_system().
229 *
230 * @return bool
231 */
232 public function is_upload_dir_writable() {
233 $upload_dir = $this->get_upload_base_dir();
234
235 // the WP direct filesystem abstraction may not be available based on user
236 // configuration. in this case, we can't write to the upload dir using WP_Filesystem
237 $is_filesystem_direct_available = defined( 'FS_CHMOD_FILE' );
238 return is_dir( $upload_dir )
239 && is_writable( $upload_dir )
240 && $this->get_host_init_filesystem_succeeded()
241 && $is_filesystem_direct_available;
242 }
243
244 /**
245 * public for tests
246 *
247 * @param string $path_upload_dir
248 * @return string|null
249 */
250 public function get_global_path_upload_dir_if_matches_site_specific_dir( $path_upload_dir ) {
251 $multisite_path_regex = '%sites/(\d)+/' . preg_quote( MATOMO_UPLOAD_DIR, '%' ) . '$%';
252
253 if ( ! preg_match( $multisite_path_regex, $path_upload_dir ) ) {
254 return null;
255 }
256
257 return preg_replace( $multisite_path_regex, '', $path_upload_dir );
258 }
259 }
260