PluginProbe
Stream – Activity Log & Audit Trail / 3.4.1
Stream – Activity Log & Audit Trail v3.4.1
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.4.1, at classes/class-log.php

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