PluginProbe
Stream – Activity Log & Audit Trail / 3.5.0
Stream – Activity Log & Audit Trail v3.5.0
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / classes / class-log.php

class-log.php in Stream – Activity Log & Audit Trail 3.5.0, at classes/class-log.php

379 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles top-level record keeping functionality.
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class - Log
12 */
13 class Log {
14
15 /**
16 * Holds Instance of plugin object
17 *
18 * @var Plugin
19 */
20 public $plugin;
21
22 /**
23 * Hold Current visitors IP Address.
24 *
25 * @var string
26 */
27 private $ip_address;
28
29
30 /**
31 * Previous Stream record ID, used for chaining same-session records
32 *
33 * @var int
34 */
35 private $prev_record;
36
37 /**
38 * Class constructor.
39 *
40 * @param Plugin $plugin Instance of plugin object.
41 */
42 public function __construct( $plugin ) {
43 $this->plugin = $plugin;
44
45 // Support proxy mode by checking the `X-Forwarded-For` header first.
46 $ip_address = wp_stream_filter_input( INPUT_SERVER, 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP );
47 $ip_address = $ip_address ? $ip_address : wp_stream_filter_input( INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP );
48
49 $this->ip_address = $ip_address;
50
51 // Ensure function used in various methods is pre-loaded.
52 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
53 require_once ABSPATH . '/wp-admin/includes/plugin.php';
54 }
55 }
56
57 /**
58 * Log handler
59 *
60 * @param Connector $connector Connector responsible for logging the event.
61 * @param string $message sprintf-ready error message string.
62 * @param array $args sprintf (and extra) arguments to use.
63 * @param int $object_id Target object id.
64 * @param string $context Context of the event.
65 * @param string $action Action of the event.
66 * @param int $user_id User responsible for the event.
67 *
68 * @return mixed True if updated, otherwise false|WP_Error
69 */
70 public function log( $connector, $message, $args, $object_id, $context, $action, $user_id = null ) {
71 global $wp_roles;
72
73 if ( is_null( $user_id ) ) {
74 $user_id = get_current_user_id();
75 }
76
77 if ( is_null( $object_id ) ) {
78 $object_id = 0;
79 }
80
81 $wp_cron_tracking = isset( $this->plugin->settings->options['advanced_wp_cron_tracking'] ) ? $this->plugin->settings->options['advanced_wp_cron_tracking'] : false;
82 $author = new Author( $user_id );
83 $agent = $author->get_current_agent();
84
85 // WP Cron tracking requires opt-in and WP Cron to be enabled.
86 if ( ! $wp_cron_tracking && 'wp_cron' === $agent ) {
87 return false;
88 }
89
90 $user = new \WP_User( $user_id );
91
92 if ( $this->is_record_excluded( $connector, $context, $action, $user ) ) {
93 return false;
94 }
95
96 $user_meta = array(
97 'user_email' => (string) ! empty( $user->user_email ) ? $user->user_email : '',
98 'display_name' => (string) $author->get_display_name(),
99 'user_login' => (string) ! empty( $user->user_login ) ? $user->user_login : '',
100 'user_role_label' => (string) $author->get_role(),
101 'agent' => (string) $agent,
102 );
103
104 if ( 'wp_cli' === $agent && function_exists( 'posix_getuid' ) ) {
105 $uid = posix_getuid();
106 $user_info = posix_getpwuid( $uid );
107
108 $user_meta['system_user_id'] = (int) $uid;
109 $user_meta['system_user_name'] = (string) $user_info['name'];
110 }
111
112 // Prevent any meta with null values from being logged.
113 $stream_meta = array_filter(
114 $args,
115 function ( $var ) {
116 return ! is_null( $var );
117 }
118 );
119
120 // Add user meta to Stream meta.
121 $stream_meta['user_meta'] = $user_meta;
122
123 // Get the current time in milliseconds.
124 $iso_8601_extended_date = wp_stream_get_iso_8601_extended_date();
125
126 if ( ! empty( $user->roles ) ) {
127 $roles = array_values( $user->roles );
128 $role = $roles[0];
129 } elseif ( is_multisite() && is_super_admin() && $wp_roles->is_role( 'administrator' ) ) {
130 $role = 'administrator';
131 } else {
132 $role = '';
133 }
134
135 $recordarr = array(
136 'object_id' => (int) $object_id,
137 'site_id' => (int) is_multisite() ? get_current_site()->id : 1,
138 'blog_id' => (int) apply_filters( 'wp_stream_blog_id_logged', get_current_blog_id() ),
139 'user_id' => (int) $user_id,
140 'user_role' => (string) $role,
141 'created' => (string) $iso_8601_extended_date,
142 'summary' => (string) vsprintf( $message, $args ),
143 'connector' => (string) $connector,
144 'context' => (string) $context,
145 'action' => (string) $action,
146 'ip' => (string) $this->ip_address,
147 'meta' => (array) $stream_meta,
148 );
149
150 if ( 0 === $recordarr['object_id'] ) {
151 unset( $recordarr['object_id'] );
152 }
153
154 $result = $this->plugin->db->insert( $recordarr );
155
156 // This is helpful in development environments:
157 // error_log( $this->debug_backtrace( $recordarr ) );.
158
159 return $result;
160 }
161
162 /**
163 * This function is use to check whether or not a record should be excluded from the log.
164 *
165 * @param string $connector Name of the connector being logged.
166 * @param string $context Name of the context being logged.
167 * @param string $action Name of the action being logged.
168 * @param \WP_User $user The user being logged.
169 * @param string $ip IP address being logged.
170 *
171 * @return bool
172 */
173 public function is_record_excluded( $connector, $context, $action, $user = null, $ip = null ) {
174 $exclude_record = false;
175
176 if ( is_null( $user ) ) {
177 $user = wp_get_current_user();
178 }
179
180 if ( is_null( $ip ) ) {
181 $ip = $this->ip_address;
182 } else {
183 $ip = wp_stream_filter_var( $ip, FILTER_VALIDATE_IP );
184 }
185
186 if ( ! empty( $user->roles ) ) {
187 $roles = array_values( $user->roles );
188 $role = $roles[0];
189 } else {
190 $role = '';
191 }
192 $record = array(
193 'connector' => $connector,
194 'context' => $context,
195 'action' => $action,
196 'author' => $user->ID,
197 'role' => $role,
198 'ip_address' => $ip,
199 );
200
201 $exclude_settings = isset( $this->plugin->settings->options['exclude_rules'] ) ? $this->plugin->settings->options['exclude_rules'] : array();
202
203 if ( is_multisite() && $this->plugin->is_network_activated() && ! is_network_admin() ) {
204 $multisite_options = (array) get_site_option( 'wp_stream_network', array() );
205 $exclude_settings = isset( $multisite_options['exclude_rules'] ) ? $multisite_options['exclude_rules'] : array();
206 }
207
208 foreach ( $this->exclude_rules_by_rows( $exclude_settings ) as $exclude_rule ) {
209 $exclude = array(
210 'connector' => ! empty( $exclude_rule['connector'] ) ? $exclude_rule['connector'] : null,
211 'context' => ! empty( $exclude_rule['context'] ) ? $exclude_rule['context'] : null,
212 'action' => ! empty( $exclude_rule['action'] ) ? $exclude_rule['action'] : null,
213 'ip_address' => ! empty( $exclude_rule['ip_address'] ) ? $exclude_rule['ip_address'] : null,
214 'author' => is_numeric( $exclude_rule['author_or_role'] ) ? absint( $exclude_rule['author_or_role'] ) : null,
215 'role' => ( ! empty( $exclude_rule['author_or_role'] ) && ! is_numeric( $exclude_rule['author_or_role'] ) ) ? $exclude_rule['author_or_role'] : null,
216 );
217
218 $exclude_rules = array_filter( $exclude, 'strlen' );
219
220 if ( $this->record_matches_rules( $record, $exclude_rules ) ) {
221 $exclude_record = true;
222 break;
223 }
224 }
225
226 /**
227 * Filters whether or not a record should be excluded from the log.
228 *
229 * If true, the record is not logged.
230 *
231 * @param array $exclude_record Whether the record should excluded.
232 * @param array $recordarr The record to log.
233 *
234 * @return bool
235 */
236 return apply_filters( 'wp_stream_is_record_excluded', $exclude_record, $record );
237 }
238
239 /**
240 * Check if a record to stored matches certain rules.
241 *
242 * @param array $record List of record parameters.
243 * @param array $exclude_rules List of record exclude rules.
244 *
245 * @return boolean
246 */
247 public function record_matches_rules( $record, $exclude_rules ) {
248 foreach ( $exclude_rules as $exclude_key => $exclude_value ) {
249 if ( ! isset( $record[ $exclude_key ] ) ) {
250 continue;
251 }
252
253 if ( 'ip_address' === $exclude_key ) {
254 $ip_addresses = explode( ',', $exclude_value );
255
256 if ( in_array( $record['ip_address'], $ip_addresses, true ) ) {
257 return true;
258 }
259 } elseif ( $record[ $exclude_key ] === $exclude_value ) {
260 return true;
261 }
262 }
263
264 return false;
265 }
266
267 /**
268 * Get all exclude rules by row because we store them by rule instead.
269 *
270 * @param array $rules List of rules indexed by rule ID.
271 *
272 * @return array
273 */
274 public function exclude_rules_by_rows( $rules ) {
275 $excludes = array();
276
277 // TODO: Move these to where the settings are generated to ensure they're in sync.
278 $rule_keys = array(
279 'exclude_row',
280 'author_or_role',
281 'connector',
282 'context',
283 'action',
284 'ip_address',
285 );
286
287 if ( empty( $rules['exclude_row'] ) ) {
288 return array();
289 }
290
291 foreach ( array_keys( $rules['exclude_row'] ) as $row_id ) {
292 $excludes[ $row_id ] = array();
293
294 foreach ( $rule_keys as $rule_key ) {
295 if ( isset( $rules[ $rule_key ][ $row_id ] ) ) {
296 $excludes[ $row_id ][ $rule_key ] = $rules[ $rule_key ][ $row_id ];
297 } else {
298 $excludes[ $row_id ][ $rule_key ] = null;
299 }
300 }
301 }
302
303 return $excludes;
304 }
305
306 /**
307 * Helper function to send a full backtrace of calls to the PHP error log for debugging
308 *
309 * @param array $recordarr Record argument array.
310 *
311 * @return string
312 */
313 public function debug_backtrace( $recordarr ) {
314 if ( version_compare( PHP_VERSION, '5.3.6', '<' ) ) {
315 return __( 'Debug backtrace requires at least PHP 5.3.6', 'wp_stream' );
316 }
317
318 // Record details.
319 $summary = isset( $recordarr['summary'] ) ? $recordarr['summary'] : null;
320 $author = isset( $recordarr['author'] ) ? $recordarr['author'] : null;
321 $connector = isset( $recordarr['connector'] ) ? $recordarr['connector'] : null;
322 $context = isset( $recordarr['context'] ) ? $recordarr['context'] : null;
323 $action = isset( $recordarr['action'] ) ? $recordarr['action'] : null;
324
325 // Stream meta.
326 $stream_meta = isset( $recordarr['meta'] ) ? $recordarr['meta'] : null;
327
328 unset( $stream_meta['user_meta'] );
329
330 if ( $stream_meta ) {
331 array_walk(
332 $stream_meta,
333 function ( &$value, $key ) {
334 $value = sprintf( '%s: %s', $key, ( '' === $value ) ? 'null' : $value );
335 }
336 );
337 $stream_meta = implode( ', ', $stream_meta );
338 }
339
340 // User meta.
341 $user_meta = isset( $recordarr['meta']['user_meta'] ) ? $recordarr['meta']['user_meta'] : null;
342
343 if ( $user_meta ) {
344 array_walk(
345 $user_meta,
346 function ( &$value, $key ) {
347 $value = sprintf( '%s: %s', $key, ( '' === $value ) ? 'null' : $value );
348 }
349 );
350
351 $user_meta = implode( ', ', $user_meta );
352 }
353
354 // Debug backtrace.
355 ob_start();
356
357 // @codingStandardsIgnoreStart
358 debug_print_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // Option to ignore args requires PHP 5.3.6
359 // @codingStandardsIgnoreEnd
360
361 $backtrace = ob_get_clean();
362 $backtrace = array_values( array_filter( explode( "\n", $backtrace ) ) );
363
364 $output = sprintf(
365 "WP Stream Debug Backtrace\n\n Summary | %s\n Author | %s\n Connector | %s\n Context | %s\n Action | %s\nStream Meta | %s\nAuthor Meta | %s\n\n%s\n",
366 $summary,
367 $author,
368 $connector,
369 $context,
370 $action,
371 $stream_meta,
372 $user_meta,
373 implode( "\n", $backtrace )
374 );
375
376 return $output;
377 }
378 }
379