| @@ -19,8 +19,16 @@ | ||
| 19 | 19 | */ |
| 20 | 20 | public $plugin; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | + * Hold Current visitors IP Address. | |
| 24 | + * | |
| 25 | + * @var string | |
| 26 | + */ | |
| 27 | + private $ip_address; | |
| 28 | + | |
| 29 | + | |
| 30 | + /** | |
| 23 | 31 | * Previous Stream record ID, used for chaining same-session records |
| 24 | 32 | * |
| 25 | 33 | * @var int |
| 26 | 34 | */ |
| @@ -33,8 +41,14 @@ | ||
| 33 | 41 | */ |
| 34 | 42 | public function __construct( $plugin ) { |
| 35 | 43 | $this->plugin = $plugin; |
| 36 | 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 | + | |
| 37 | 51 | // Ensure function used in various methods is pre-loaded. |
| 38 | 52 | if ( ! function_exists( 'is_plugin_active_for_network' ) ) { |
| 39 | 53 | require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 40 | 54 | } |
| @@ -72,13 +86,11 @@ | ||
| 72 | 86 | if ( ! $wp_cron_tracking && 'wp_cron' === $agent ) { |
| 73 | 87 | return false; |
| 74 | 88 | } |
| 75 | 89 | |
| 76 | - $ip_address = $this->plugin->get_client_ip_address(); | |
| 77 | - | |
| 78 | 90 | $user = new \WP_User( $user_id ); |
| 79 | 91 | |
| 80 | - if ( $this->is_record_excluded( $connector, $context, $action, $user, $ip_address ) ) { | |
| 92 | + if ( $this->is_record_excluded( $connector, $context, $action, $user ) ) { | |
| 81 | 93 | return false; |
| 82 | 94 | } |
| 83 | 95 | |
| 84 | 96 | $user_meta = array( |
| @@ -92,22 +104,17 @@ | ||
| 92 | 104 | if ( 'wp_cli' === $agent && function_exists( 'posix_getuid' ) ) { |
| 93 | 105 | $uid = posix_getuid(); |
| 94 | 106 | $user_info = posix_getpwuid( $uid ); |
| 95 | 107 | |
| 96 | - // Normalize the user info to an array if it's not already. | |
| 97 | - if ( ! is_array( $user_info ) ) { | |
| 98 | - $user_info = array( 'name' => 'unknown' ); | |
| 99 | - } | |
| 100 | - | |
| 101 | 108 | $user_meta['system_user_id'] = (int) $uid; |
| 102 | - $user_meta['system_user_name'] = (string) ( $user_info['name'] ?? 'unknown' ); | |
| 109 | + $user_meta['system_user_name'] = (string) $user_info['name']; | |
| 103 | 110 | } |
| 104 | 111 | |
| 105 | 112 | // Prevent any meta with null values from being logged. |
| 106 | 113 | $stream_meta = array_filter( |
| 107 | 114 | $args, |
| 108 | - function ( $value ) { | |
| 109 | - return ! is_null( $value ); | |
| 115 | + function ( $var ) { | |
| 116 | + return ! is_null( $var ); | |
| 110 | 117 | } |
| 111 | 118 | ); |
| 112 | 119 | |
| 113 | 120 | // Add user meta to Stream meta. |
| @@ -132,9 +139,9 @@ | ||
| 132 | 139 | 'summary' => (string) vsprintf( $message, $args ), |
| 133 | 140 | 'connector' => (string) $connector, |
| 134 | 141 | 'context' => (string) $context, |
| 135 | 142 | 'action' => (string) $action, |
| 136 | - 'ip' => (string) $ip_address, | |
| 143 | + 'ip' => (string) $this->ip_address, | |
| 137 | 144 | 'meta' => (array) $stream_meta, |
| 138 | 145 | ); |
| 139 | 146 | |
| 140 | 147 | if ( 0 === $recordarr['object_id'] ) { |
| @@ -166,8 +173,14 @@ | ||
| 166 | 173 | if ( is_null( $user ) ) { |
| 167 | 174 | $user = wp_get_current_user(); |
| 168 | 175 | } |
| 169 | 176 | |
| 177 | + if ( is_null( $ip ) ) { | |
| 178 | + $ip = $this->ip_address; | |
| 179 | + } else { | |
| 180 | + $ip = wp_stream_filter_var( $ip, FILTER_VALIDATE_IP ); | |
| 181 | + } | |
| 182 | + | |
| 170 | 183 | if ( ! empty( $user->roles ) ) { |
| 171 | 184 | $roles = array_values( $user->roles ); |
| 172 | 185 | $role = $roles[0]; |
| 173 | 186 | } else { |
| @@ -198,14 +211,9 @@ | ||
| 198 | 211 | 'author' => is_numeric( $exclude_rule['author_or_role'] ) ? absint( $exclude_rule['author_or_role'] ) : null, |
| 199 | 212 | 'role' => ( ! empty( $exclude_rule['author_or_role'] ) && ! is_numeric( $exclude_rule['author_or_role'] ) ) ? $exclude_rule['author_or_role'] : null, |
| 200 | 213 | ); |
| 201 | 214 | |
| 202 | - $exclude_rules = array_filter( | |
| 203 | - $exclude, | |
| 204 | - function ( $value ) { | |
| 205 | - return ! is_null( $value ); | |
| 206 | - } | |
| 207 | - ); | |
| 215 | + $exclude_rules = array_filter( $exclude, 'strlen' ); | |
| 208 | 216 | |
| 209 | 217 | if ( $this->record_matches_rules( $record, $exclude_rules ) ) { |
| 210 | 218 | $exclude_record = true; |
| 211 | 219 | break; |
| @@ -233,12 +241,10 @@ | ||
| 233 | 241 | * |
| 234 | 242 | * @return boolean |
| 235 | 243 | */ |
| 236 | 244 | public function record_matches_rules( $record, $exclude_rules ) { |
| 237 | - $matches_needed = count( $exclude_rules ); | |
| 238 | - $matches_found = 0; | |
| 239 | 245 | foreach ( $exclude_rules as $exclude_key => $exclude_value ) { |
| 240 | - if ( ! isset( $record[ $exclude_key ] ) || is_null( $exclude_value ) ) { | |
| 246 | + if ( ! isset( $record[ $exclude_key ] ) ) { | |
| 241 | 247 | continue; |
| 242 | 248 | } |
| 243 | 249 | |
| 244 | 250 | if ( 'ip_address' === $exclude_key ) { |
| @@ -244,16 +250,16 @@ | ||
| 244 | 250 | if ( 'ip_address' === $exclude_key ) { |
| 245 | 251 | $ip_addresses = explode( ',', $exclude_value ); |
| 246 | 252 | |
| 247 | 253 | if ( in_array( $record['ip_address'], $ip_addresses, true ) ) { |
| 248 | - ++$matches_found; | |
| 254 | + return true; | |
| 249 | 255 | } |
| 250 | 256 | } elseif ( $record[ $exclude_key ] === $exclude_value ) { |
| 251 | - ++$matches_found; | |
| 257 | + return true; | |
| 252 | 258 | } |
| 253 | 259 | } |
| 254 | 260 | |
| 255 | - return $matches_found === $matches_needed; | |
| 261 | + return false; | |
| 256 | 262 | } |
| 257 | 263 | |
| 258 | 264 | /** |
| 259 | 265 | * Get all exclude rules by row because we store them by rule instead. |
| @@ -302,9 +308,9 @@ | ||
| 302 | 308 | * @return string |
| 303 | 309 | */ |
| 304 | 310 | public function debug_backtrace( $recordarr ) { |
| 305 | 311 | if ( version_compare( PHP_VERSION, '5.3.6', '<' ) ) { |
| 306 | - return __( 'Debug backtrace requires at least PHP 5.3.6', 'stream' ); | |
| 312 | + return __( 'Debug backtrace requires at least PHP 5.3.6', 'wp_stream' ); | |
| 307 | 313 | } |
| 308 | 314 | |
| 309 | 315 | // Record details. |
| 310 | 316 | $summary = isset( $recordarr['summary'] ) ? $recordarr['summary'] : null; |