| @@ -1,20 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Handles top-level record keeping functionality. | |
| 4 | - * | |
| 5 | - * @package WP_Stream | |
| 6 | - */ | |
| 7 | 2 | |
| 8 | 3 | namespace WP_Stream; |
| 9 | 4 | |
| 10 | -/** | |
| 11 | - * Class - Log | |
| 12 | - */ | |
| 13 | 5 | class Log { |
| 14 | 6 | |
| 15 | 7 | /** |
| 16 | - * Holds Instance of plugin object | |
| 8 | + * Hold Plugin class | |
| 17 | 9 | * |
| 18 | 10 | * @var Plugin |
| 19 | 11 | */ |
| 20 | 12 | public $plugin; |
| @@ -19,8 +11,16 @@ | ||
| 19 | 11 | */ |
| 20 | 12 | public $plugin; |
| 21 | 13 | |
| 22 | 14 | /** |
| 15 | + * Hold Current visitors IP Address. | |
| 16 | + * | |
| 17 | + * @var string | |
| 18 | + */ | |
| 19 | + private $ip_address; | |
| 20 | + | |
| 21 | + | |
| 22 | + /** | |
| 23 | 23 | * Previous Stream record ID, used for chaining same-session records |
| 24 | 24 | * |
| 25 | 25 | * @var int |
| 26 | 26 | */ |
| @@ -28,13 +28,19 @@ | ||
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * Class constructor. |
| 31 | 31 | * |
| 32 | - * @param Plugin $plugin Instance of plugin object. | |
| 32 | + * @param Plugin $plugin The main Plugin class. | |
| 33 | 33 | */ |
| 34 | 34 | public function __construct( $plugin ) { |
| 35 | 35 | $this->plugin = $plugin; |
| 36 | 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 | + | |
| 37 | 43 | // Ensure function used in various methods is pre-loaded. |
| 38 | 44 | if ( ! function_exists( 'is_plugin_active_for_network' ) ) { |
| 39 | 45 | require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 40 | 46 | } |
| @@ -72,13 +78,11 @@ | ||
| 72 | 78 | if ( ! $wp_cron_tracking && 'wp_cron' === $agent ) { |
| 73 | 79 | return false; |
| 74 | 80 | } |
| 75 | 81 | |
| 76 | - $ip_address = $this->plugin->get_client_ip_address(); | |
| 77 | - | |
| 78 | 82 | $user = new \WP_User( $user_id ); |
| 79 | 83 | |
| 80 | - if ( $this->is_record_excluded( $connector, $context, $action, $user, $ip_address ) ) { | |
| 84 | + if ( $this->is_record_excluded( $connector, $context, $action, $user ) ) { | |
| 81 | 85 | return false; |
| 82 | 86 | } |
| 83 | 87 | |
| 84 | 88 | $user_meta = array( |
| @@ -92,22 +96,17 @@ | ||
| 92 | 96 | if ( 'wp_cli' === $agent && function_exists( 'posix_getuid' ) ) { |
| 93 | 97 | $uid = posix_getuid(); |
| 94 | 98 | $user_info = posix_getpwuid( $uid ); |
| 95 | 99 | |
| 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 | 100 | $user_meta['system_user_id'] = (int) $uid; |
| 102 | - $user_meta['system_user_name'] = (string) ( $user_info['name'] ?? 'unknown' ); | |
| 101 | + $user_meta['system_user_name'] = (string) $user_info['name']; | |
| 103 | 102 | } |
| 104 | 103 | |
| 105 | 104 | // Prevent any meta with null values from being logged. |
| 106 | 105 | $stream_meta = array_filter( |
| 107 | 106 | $args, |
| 108 | - function ( $value ) { | |
| 109 | - return ! is_null( $value ); | |
| 107 | + function ( $var ) { | |
| 108 | + return ! is_null( $var ); | |
| 110 | 109 | } |
| 111 | 110 | ); |
| 112 | 111 | |
| 113 | 112 | // Add user meta to Stream meta. |
| @@ -112,8 +111,11 @@ | ||
| 112 | 111 | |
| 113 | 112 | // Add user meta to Stream meta. |
| 114 | 113 | $stream_meta['user_meta'] = $user_meta; |
| 115 | 114 | |
| 115 | + // Get the current time in milliseconds. | |
| 116 | + $iso_8601_extended_date = wp_stream_get_iso_8601_extended_date(); | |
| 117 | + | |
| 116 | 118 | if ( ! empty( $user->roles ) ) { |
| 117 | 119 | $roles = array_values( $user->roles ); |
| 118 | 120 | $role = $roles[0]; |
| 119 | 121 | } elseif ( is_multisite() && is_super_admin() && $wp_roles->is_role( 'administrator' ) ) { |
| @@ -127,14 +129,14 @@ | ||
| 127 | 129 | 'site_id' => (int) is_multisite() ? get_current_site()->id : 1, |
| 128 | 130 | 'blog_id' => (int) apply_filters( 'wp_stream_blog_id_logged', get_current_blog_id() ), |
| 129 | 131 | 'user_id' => (int) $user_id, |
| 130 | 132 | 'user_role' => (string) $role, |
| 131 | - 'created' => (string) current_time( 'mysql', true ), | |
| 133 | + 'created' => (string) $iso_8601_extended_date, | |
| 132 | 134 | 'summary' => (string) vsprintf( $message, $args ), |
| 133 | 135 | 'connector' => (string) $connector, |
| 134 | 136 | 'context' => (string) $context, |
| 135 | 137 | 'action' => (string) $action, |
| 136 | - 'ip' => (string) $ip_address, | |
| 138 | + 'ip' => (string) $this->ip_address, | |
| 137 | 139 | 'meta' => (array) $stream_meta, |
| 138 | 140 | ); |
| 139 | 141 | |
| 140 | 142 | if ( 0 === $recordarr['object_id'] ) { |
| @@ -143,9 +145,9 @@ | ||
| 143 | 145 | |
| 144 | 146 | $result = $this->plugin->db->insert( $recordarr ); |
| 145 | 147 | |
| 146 | 148 | // This is helpful in development environments: |
| 147 | - // error_log( $this->debug_backtrace( $recordarr ) );. | |
| 149 | + // error_log( $this->debug_backtrace( $recordarr ) ); | |
| 148 | 150 | |
| 149 | 151 | return $result; |
| 150 | 152 | } |
| 151 | 153 | |
| @@ -166,8 +168,14 @@ | ||
| 166 | 168 | if ( is_null( $user ) ) { |
| 167 | 169 | $user = wp_get_current_user(); |
| 168 | 170 | } |
| 169 | 171 | |
| 172 | + if ( is_null( $ip ) ) { | |
| 173 | + $ip = $this->ip_address; | |
| 174 | + } else { | |
| 175 | + $ip = wp_stream_filter_var( $ip, FILTER_VALIDATE_IP ); | |
| 176 | + } | |
| 177 | + | |
| 170 | 178 | if ( ! empty( $user->roles ) ) { |
| 171 | 179 | $roles = array_values( $user->roles ); |
| 172 | 180 | $role = $roles[0]; |
| 173 | 181 | } else { |
| @@ -198,14 +206,9 @@ | ||
| 198 | 206 | 'author' => is_numeric( $exclude_rule['author_or_role'] ) ? absint( $exclude_rule['author_or_role'] ) : null, |
| 199 | 207 | 'role' => ( ! empty( $exclude_rule['author_or_role'] ) && ! is_numeric( $exclude_rule['author_or_role'] ) ) ? $exclude_rule['author_or_role'] : null, |
| 200 | 208 | ); |
| 201 | 209 | |
| 202 | - $exclude_rules = array_filter( | |
| 203 | - $exclude, | |
| 204 | - function ( $value ) { | |
| 205 | - return ! is_null( $value ); | |
| 206 | - } | |
| 207 | - ); | |
| 210 | + $exclude_rules = array_filter( $exclude, 'strlen' ); | |
| 208 | 211 | |
| 209 | 212 | if ( $this->record_matches_rules( $record, $exclude_rules ) ) { |
| 210 | 213 | $exclude_record = true; |
| 211 | 214 | break; |
| @@ -233,12 +236,10 @@ | ||
| 233 | 236 | * |
| 234 | 237 | * @return boolean |
| 235 | 238 | */ |
| 236 | 239 | public function record_matches_rules( $record, $exclude_rules ) { |
| 237 | - $matches_needed = count( $exclude_rules ); | |
| 238 | - $matches_found = 0; | |
| 239 | 240 | foreach ( $exclude_rules as $exclude_key => $exclude_value ) { |
| 240 | - if ( ! isset( $record[ $exclude_key ] ) || is_null( $exclude_value ) ) { | |
| 241 | + if ( ! isset( $record[ $exclude_key ] ) ) { | |
| 241 | 242 | continue; |
| 242 | 243 | } |
| 243 | 244 | |
| 244 | 245 | if ( 'ip_address' === $exclude_key ) { |
| @@ -244,16 +245,16 @@ | ||
| 244 | 245 | if ( 'ip_address' === $exclude_key ) { |
| 245 | 246 | $ip_addresses = explode( ',', $exclude_value ); |
| 246 | 247 | |
| 247 | 248 | if ( in_array( $record['ip_address'], $ip_addresses, true ) ) { |
| 248 | - ++$matches_found; | |
| 249 | + return true; | |
| 249 | 250 | } |
| 250 | 251 | } elseif ( $record[ $exclude_key ] === $exclude_value ) { |
| 251 | - ++$matches_found; | |
| 252 | + return true; | |
| 252 | 253 | } |
| 253 | 254 | } |
| 254 | 255 | |
| 255 | - return $matches_found === $matches_needed; | |
| 256 | + return false; | |
| 256 | 257 | } |
| 257 | 258 | |
| 258 | 259 | /** |
| 259 | 260 | * Get all exclude rules by row because we store them by rule instead. |
| @@ -302,9 +303,9 @@ | ||
| 302 | 303 | * @return string |
| 303 | 304 | */ |
| 304 | 305 | public function debug_backtrace( $recordarr ) { |
| 305 | 306 | if ( version_compare( PHP_VERSION, '5.3.6', '<' ) ) { |
| 306 | - return __( 'Debug backtrace requires at least PHP 5.3.6', 'stream' ); | |
| 307 | + return __( 'Debug backtrace requires at least PHP 5.3.6', 'wp_stream' ); | |
| 307 | 308 | } |
| 308 | 309 | |
| 309 | 310 | // Record details. |
| 310 | 311 | $summary = isset( $recordarr['summary'] ) ? $recordarr['summary'] : null; |