Changeset 3149177
- Timestamp:
- 09/10/2024 11:24:44 AM (15 months ago)
- Location:
- sg-security/trunk
- Files:
-
- 8 edited
-
core/Activity_Log/Activity_Log.php (modified) (4 diffs)
-
core/Activity_Log/Activity_Log_Helper.php (modified) (14 diffs)
-
core/Activity_Log/Activity_Log_Weekly_Emails.php (modified) (6 diffs)
-
core/Cli/Cli_List.php (modified) (5 diffs)
-
core/Rest/Rest_Helper_Activity.php (modified) (4 diffs)
-
core/Salt_Shaker/Salt_Shaker.php (modified) (7 diffs)
-
readme.txt (modified) (2 diffs)
-
sg-security.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sg-security/trunk/core/Activity_Log/Activity_Log.php
r2907708 r3149177 143 143 144 144 if ( ! class_exists( $class ) ) { 145 throw new \Exception( 'Unknown activity log type "' . $type. '".' );145 throw new \Exception( 'Unknown activity log type "' . esc_html( $type ) . '".' ); 146 146 } 147 147 … … 151 151 152 152 /** 153 * Set the cronjob for deleting old logs.153 * Set the CRON job for deleting old logs. 154 154 * 155 155 * @since 1.0.0 156 156 */ 157 157 public function set_sgs_logs_cron() { 158 // Bail if cronis disabled.158 // Bail if CRON is disabled. 159 159 if ( 1 === Helper_Service::is_cron_disabled() ) { 160 160 return; … … 167 167 168 168 /** 169 * Delete logs on plugin page if cronis disabled.169 * Delete logs on plugin page if CRON is disabled. 170 170 * 171 171 * @since 1.0.0 172 172 */ 173 173 public function delete_logs_on_admin_page() { 174 // Delete if we are on plugin page and cronis disabled.174 // Delete if we are on plugin page and CRON is disabled. 175 175 if ( 176 176 isset( $_GET['page'] ) && … … 227 227 */ 228 228 public static function get_activity_log_lifetime() { 229 // Set custom log lifetime interval in days. The intval covers the cases for string, array and sqlinjections.229 // Set custom log lifetime interval in days. The intval covers the cases for string, array and SQL injections. 230 230 $log_lifetime = intval( apply_filters( 'sgs_set_activity_log_lifetime', get_option( 'sgs_activity_log_lifetime', 12 ) ) ); 231 231 -
sg-security/trunk/core/Activity_Log/Activity_Log_Helper.php
r2907443 r3149177 8 8 */ 9 9 class Activity_Log_Helper { 10 11 /** 12 * The Database placeholder. 13 */ 14 public $wpdb; 15 16 /** 17 * The Constructor. 18 */ 19 public function __construct() { 20 global $wpdb; 21 $this->wpdb = $wpdb; 22 } 10 23 11 24 /** … … 107 120 * @param array $args Array of event args. 108 121 * 109 * @return bool True if the entry alre dy exists, false otherwise.122 * @return bool True if the entry already exists, false otherwise. 110 123 */ 111 124 public function check_for_duplicates( $args ) { 112 global $wpdb;113 125 114 126 // Bail if table doesn't exist. 115 if ( ! Helper::table_exists( $ wpdb->sgs_visitors ) ) {127 if ( ! Helper::table_exists( $this->wpdb->sgs_visitors ) ) { 116 128 return false; 117 129 } 118 130 119 $has_duplicate = $wpdb->get_row( // phpcs:ignore120 $wpdb->prepare(121 'SELECT `id` FROM `' . $wpdb->sgs_log. '`131 // Prepare the check for duplicates query. 132 $query = $this->wpdb->prepare( 133 'SELECT `ID` FROM `' . esc_sql( $this->wpdb->sgs_log ) . '` 122 134 WHERE `visitor_id` = %s 123 135 AND `ts` = %s … … 125 137 LIMIT 1 126 138 ;', 127 $args['visitor_id'], 128 $args['ts'], 129 $args['activity'] 130 ) 131 ); 139 $args['visitor_id'], 140 $args['ts'], 141 $args['activity'] 142 ); 143 144 $has_duplicate = $this->wpdb->get_row( $query ); //phpcs:ignore 132 145 133 146 if ( $has_duplicate ) { … … 146 159 */ 147 160 public function insert( $args ) { 148 global $wpdb;149 161 150 162 if ( $this->check_for_duplicates( $args ) ) { … … 152 164 } 153 165 154 $ wpdb->insert(155 $ wpdb->sgs_log,166 $this->wpdb->insert( 167 $this->wpdb->sgs_log, 156 168 array( 157 169 'visitor_id' => $args['visitor_id'], … … 181 193 */ 182 194 public function get_visitor_by_user_id( $user_id ) { 183 global $wpdb;184 195 185 196 // Check if there is already a record as a visitor for this user. 186 $maybe_id = $wpdb->get_row( // phpcs:ignore. 187 $wpdb->prepare( 188 'SELECT `ID` FROM `' . $wpdb->sgs_visitors . '` 197 $query = $this->wpdb->prepare( 198 'SELECT `ID` FROM `' . esc_sql( $this->wpdb->sgs_visitors ) . '` 189 199 WHERE `user_id` = %s 190 LIMIT 1;', 191 $user_id 192 ) 193 ); 200 LIMIT 1 201 ;', 202 $user_id 203 ); 204 205 $maybe_id = $this->wpdb->get_row( $query ); // phpcs:ignore. 194 206 195 207 // If there is such record, return the visitor ID. … … 199 211 200 212 // Create a new record for the user as a visitor. 201 $ wpdb->insert(202 $ wpdb->sgs_visitors,213 $this->wpdb->insert( 214 $this->wpdb->sgs_visitors, 203 215 array( 204 216 'user_id' => $user_id, … … 208 220 ); 209 221 210 // Get the user visitor ID.211 $id = $wpdb->get_row( // phpcs:ignore.212 $wpdb->prepare(213 'SELECT `ID` FROM `' . $wpdb->sgs_visitors . '`214 WHERE `user_id` = %s215 LIMIT 1;',216 $user_id217 )218 );219 220 222 // Return the ID. 221 return $ id->ID;222 } 223 224 /** 225 * Get the visitor unique ID by I paddress.223 return $this->wpdb->insert_id; 224 } 225 226 /** 227 * Get the visitor unique ID by IP address. 226 228 * 227 229 * @since 1.0.0 … … 232 234 */ 233 235 public function get_visitor_by_ip( $ip ) { 234 global $wpdb; 235 $maybe_id = $wpdb->get_row( // phpcs:ignore 236 $wpdb->prepare( 237 'SELECT `ID` FROM `' . $wpdb->sgs_visitors . '` 236 237 $query = $this->wpdb->prepare( 238 'SELECT `ID` FROM `' . esc_sql( $this->wpdb->sgs_visitors ) . '` 238 239 WHERE `ip` = %s 239 240 AND `user_id` = 0 240 LIMIT 1;', 241 $ip 242 ) 243 ); 241 LIMIT 1 242 ;', 243 $ip 244 ); 245 246 $maybe_id = $this->wpdb->get_row( $query ); // phpcs:ignore 244 247 245 248 if ( ! is_null( $maybe_id ) ) { … … 247 250 } 248 251 249 // Insert the visitors ipin the db.250 $ wpdb->insert(251 $ wpdb->sgs_visitors,252 // Insert the visitors IP in the db. 253 $this->wpdb->insert( 254 $this->wpdb->sgs_visitors, 252 255 array( 253 256 'ip' => $ip, … … 256 259 ); 257 260 258 return $ wpdb->insert_id;261 return $this->wpdb->insert_id; 259 262 } 260 263 … … 265 268 */ 266 269 public function add_log_visitor_indexes() { 267 global $wpdb;268 270 269 271 // Bail if tables does not exist. 270 272 if ( 271 ! Helper::table_exists( $ wpdb->sgs_visitors ) ||272 ! Helper::table_exists( $ wpdb->sgs_log )273 ! Helper::table_exists( $this->wpdb->sgs_visitors ) || 274 ! Helper::table_exists( $this->wpdb->sgs_log ) 273 275 ) { 274 276 return; … … 276 278 277 279 // Check if the indexes are already set. 278 $log_event_index = $ wpdb->get_var( "SHOW INDEX FROM `{$wpdb->prefix}sgs_log_events` WHERE `Key_name` = 'log_event_index'" );279 $ip_index_exists = $ wpdb->get_var( "SHOW INDEX FROM `{$wpdb->prefix}sgs_log_visitors` WHERE `Key_name` = 'ip_index'" );280 $log_event_index = $this->wpdb->get_var( 'SHOW INDEX FROM `' . esc_sql( $this->wpdb->prefix . 'sgs_log_events' ) . "` WHERE `Key_name` = 'log_event_index'" ); 281 $ip_index_exists = $this->wpdb->get_var( 'SHOW INDEX FROM `' . esc_sql( $this->wpdb->prefix . 'sgs_log_visitors' ) . "` WHERE `Key_name` = 'ip_index'" ); 280 282 281 283 // Add log event index if not set. 282 284 if ( is_null( $log_event_index ) ) { 283 $ wpdb->query( "ALTER TABLE `{$wpdb->prefix}sgs_log_events` ADD INDEX `log_event_index` (`visitor_id`, `ts`, `activity`, `id`)");285 $this->wpdb->query( 'ALTER TABLE `' . esc_sql( $this->wpdb->prefix . 'sgs_log_events' ) . '` ADD INDEX `log_event_index` (`visitor_id`, `ts`, `activity`, `id`)' ); 284 286 } 285 287 286 288 // Add the IP index if not set. 287 289 if ( is_null( $ip_index_exists ) ) { 288 $ wpdb->query( "ALTER TABLE `{$wpdb->prefix}sgs_log_visitors` ADD INDEX `ip_index` (`ip`)");290 $this->wpdb->query( 'ALTER TABLE `' . esc_sql( $this->wpdb->prefix . 'sgs_log_visitors' ) . '` ADD INDEX `ip_index` (`ip`)' ); 289 291 } 290 292 } … … 296 298 */ 297 299 public function adjust_visitors_indexes() { 298 global $wpdb;299 300 300 301 // Bail if table does not exist. 301 if ( ! Helper::table_exists( $ wpdb->sgs_visitors ) ) {302 if ( ! Helper::table_exists( $this->wpdb->sgs_visitors ) ) { 302 303 return; 303 304 } 304 305 305 $user_id_index_exists = $ wpdb->get_var( "SHOW INDEX FROM `{$wpdb->prefix}sgs_log_visitors` WHERE `Key_name` = 'user_id_index'" );306 $block_user_index_exists = $ wpdb->get_var( "SHOW INDEX FROM `{$wpdb->prefix}sgs_log_visitors` WHERE `Key_name` = 'block_user_index'" );306 $user_id_index_exists = $this->wpdb->get_var( 'SHOW INDEX FROM `' . esc_sql( $this->wpdb->prefix . 'sgs_log_visitors' ) . "` WHERE `Key_name` = 'user_id_index'" ); 307 $block_user_index_exists = $this->wpdb->get_var( 'SHOW INDEX FROM `' . esc_sql( $this->wpdb->prefix . 'sgs_log_visitors' ) . "` WHERE `Key_name` = 'block_user_index'" ); 307 308 308 309 // Drop the user id index. 309 310 if ( ! is_null( $user_id_index_exists ) ) { 310 $ wpdb->query( "DROP INDEX `user_id_index` ON `{$wpdb->prefix}sgs_log_visitors`");311 $this->wpdb->query( 'DROP INDEX `user_id_index` ON `' . esc_sql( $this->wpdb->prefix . 'sgs_log_visitors' ) . '`' ); 311 312 } 312 313 313 314 // Add the Block/User complex index if not set. 314 315 if ( is_null( $block_user_index_exists ) ) { 315 $ wpdb->query( "ALTER TABLE `{$wpdb->prefix}sgs_log_visitors` ADD INDEX `block_user_index` (`block`, `user_id`)");316 $this->wpdb->query( 'ALTER TABLE `' . esc_sql( $this->wpdb->prefix . 'sgs_log_visitors' ) . '` ADD INDEX `block_user_index` (`block`, `user_id`)' ); 316 317 } 317 318 } -
sg-security/trunk/core/Activity_Log/Activity_Log_Weekly_Emails.php
r3000015 r3149177 19 19 20 20 /** 21 * Database placeholder. 22 */ 23 public $wpdb; 24 25 /** 21 26 * The constructor. 22 27 * … … 24 29 */ 25 30 public function __construct() { 31 // Assign the Database. 32 global $wpdb; 33 $this->wpdb = $wpdb; 26 34 27 35 // Initiate the Email Service Class. … … 222 230 */ 223 231 private function get_total_human_stats( $start_date, $end_date ) { 224 global $wpdb; 225 226 return $wpdb->get_var( 227 'SELECT COUNT(*) FROM `' . $wpdb->prefix . 'sgs_log_events' . '` 228 WHERE `action` = "visit" 229 AND `visitor_type` = "Human" 230 AND `type` = "unknown" 231 AND `ts` BETWEEN ' . $start_date . ' AND ' . $end_date . ' ;' 232 ); 232 233 $query = $this->wpdb->prepare( 234 'SELECT COUNT(*) FROM `' . esc_sql( $this->wpdb->prefix . 'sgs_log_events' ) . "` 235 WHERE `action` = 'visit' 236 AND `visitor_type` = 'Human' 237 AND `type` = 'unknown' 238 AND `ts` BETWEEN %s AND %s", 239 $start_date, 240 $end_date 241 ); 242 243 return $this->wpdb->get_var( $query ); //phpcs:ignore 233 244 } 234 245 … … 244 255 */ 245 256 private function get_total_bots_stats( $start_date, $end_date ) { 246 global $wpdb; 247 248 return $wpdb->get_var( 249 'SELECT COUNT(*) FROM `' . $wpdb->prefix . 'sgs_log_events' . '` 250 WHERE `action` = "visit" 251 AND `visitor_type` <>"Human" AND `visitor_type` <>"unknown" 252 AND `type` = "unknown" 253 AND `ts` BETWEEN ' . $start_date . ' AND ' . $end_date . ' ;' 254 ); 257 258 $query = $this->wpdb->prepare( 259 'SELECT COUNT(*) FROM `' . esc_sql( $this->wpdb->prefix . 'sgs_log_events' ) . "` 260 WHERE `action` = 'visit' 261 AND `visitor_type` <> 'Human' 262 AND `visitor_type` <> 'unknown' 263 AND `type` = 'unknown' 264 AND `ts` BETWEEN %s AND %s", 265 $start_date, 266 $end_date 267 ); 268 269 return $this->wpdb->get_var( $query ); //phpcs:ignore 255 270 } 256 271 … … 268 283 269 284 /** 270 * Get notification rec eipient emails.285 * Get notification recipient emails. 271 286 * 272 287 * @since 1.2.0 … … 277 292 $data = array(); 278 293 279 // Get the currently set rec eipients.294 // Get the currently set recipients. 280 295 $receipients = get_option( 'sg_security_notification_emails', array() ); 281 296 282 // Return empty array if no rec eipients are set.297 // Return empty array if no recipients are set. 283 298 if ( empty( $receipients ) ) { 284 299 return $data; -
sg-security/trunk/core/Cli/Cli_List.php
r3129214 r3149177 113 113 * @param string $type The type of log we want. 114 114 * 115 * @return string The sqlquery.115 * @return string The SQL query. 116 116 */ 117 117 public function get_query( $type ) { … … 160 160 foreach ( $visitors as $visit ) { 161 161 $table_data[] = array( 162 'Timestamp' => get_date_from_gmt( date( 'Y-m-d h:i:s', $visit['ts'] ), 'Y-m-d H:i' ),162 'Timestamp' => get_date_from_gmt( gmdate( 'Y-m-d h:i:s', $visit['ts'] ), 'Y-m-d H:i' ), 163 163 'Visitor Type' => $visit['visitor_type'], 164 164 'IP Address' => $visit['ip'], … … 182 182 183 183 // Get all user visitors from the database. 184 $ visitors = $this->wpdb->get_results( // phpcs:ignore185 'SELECT * FROM `' . $this->wpdb->sgs_visitors. '`186 WHERE `user_id` != 0184 $query = $this->wpdb->prepare( 185 'SELECT * FROM `' . esc_sql( $this->wpdb->sgs_visitors ) . '` 186 WHERE `user_id` != %d 187 187 ;', 188 OBJECT_K188 0 189 189 ); 190 190 191 // Loop results and get necesary data. 191 $visitors = $this->wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore 192 193 // Loop results and get necessary data. 192 194 $data = array(); 193 195 foreach ( $results as $entry ) { 194 196 $log = array( 195 'ts' => get_date_from_gmt( date( 'Y-m-d H:i', $entry['blocked_on'] ), 'Y-m-d H:i' ),197 'ts' => get_date_from_gmt( gmdate( 'Y-m-d H:i', $entry['blocked_on'] ), 'Y-m-d H:i' ), 196 198 'user' => $entry['ip'], 197 199 'visitor_id' => $entry['id'], … … 228 230 229 231 // Get visitors data. 230 $ visitors = $this->wpdb->get_results( // phpcs:ignore231 'SELECT * FROM `' . $this->wpdb->sgs_visitors. '`232 WHERE `user_id` != 0232 $query = $this->wpdb->prepare( 233 'SELECT * FROM `' . esc_sql( $this->wpdb->sgs_visitors ) . '` 234 WHERE `user_id` != %d 233 235 ;', 234 OBJECT_K236 0 235 237 ); 238 239 $visitors = $this->wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore 236 240 237 241 // Populate the data for the table. … … 243 247 // Add the data to the table array. 244 248 $table_data[] = array( 245 'Timestamp' => get_date_from_gmt( date( 'Y-m-d H:i', $entry['ts'] ), 'Y-m-d H:i' ),249 'Timestamp' => get_date_from_gmt( gmdate( 'Y-m-d H:i', $entry['ts'] ), 'Y-m-d H:i' ), 246 250 'IP Address' => $entry['ip'], 247 251 'Activity' => $entry['description'], -
sg-security/trunk/core/Rest/Rest_Helper_Activity.php
r2904929 r3149177 331 331 $data[] = array( 332 332 'id' => $entry['id'], 333 'ts' => get_date_from_gmt( date( 'Y-m-d H:i', $entry['ts'] ), 'Y-m-d H:i' ),333 'ts' => get_date_from_gmt( gmdate( 'Y-m-d H:i', $entry['ts'] ), 'Y-m-d H:i' ), 334 334 'ip' => $entry['ip'], 335 335 'page_visited' => $entry['description'], … … 398 398 $data[] = array( 399 399 'id' => $entry['id'], 400 'ts' => get_date_from_gmt( date( 'Y-m-d H:i', $entry['ts'] ), 'Y-m-d H:i' ),400 'ts' => get_date_from_gmt( gmdate( 'Y-m-d H:i', $entry['ts'] ), 'Y-m-d H:i' ), 401 401 'ip' => $entry['ip'], 402 402 'activity' => $entry['description'], … … 720 720 foreach ( $results as $entry ) { 721 721 $log = array( 722 'ts' => get_date_from_gmt( date( 'Y-m-d H:i', $entry['blocked_on'] ), 'Y-m-d H:i' ),722 'ts' => get_date_from_gmt( gmdate( 'Y-m-d H:i', $entry['blocked_on'] ), 'Y-m-d H:i' ), 723 723 'user' => $entry['ip'], 724 724 'visitor_id' => $entry['id'], … … 742 742 743 743 $log = array( 744 'ts' => get_date_from_gmt( date( 'Y-m-d H:i', $attempt['timestamp'] ), 'Y-m-d H:i' ),744 'ts' => get_date_from_gmt( gmdate( 'Y-m-d H:i', $attempt['timestamp'] ), 'Y-m-d H:i' ), 745 745 'user' => $ip, 746 746 'visitor_id' => 0, -
sg-security/trunk/core/Salt_Shaker/Salt_Shaker.php
r2582031 r3149177 1 1 <?php 2 2 namespace SG_Security\Salt_Shaker; 3 4 use SiteGround_Helper\Helper_Service; 3 5 4 6 /** … … 52 54 53 55 /** 56 * The WordPress filesystem. 57 */ 58 public $wp_filesystem; 59 60 public function __construct() { 61 $this->wp_filesystem = Helper_Service::setup_wp_filesystem(); 62 } 63 64 /** 54 65 * Check if the config exists. 55 66 * … … 59 70 */ 60 71 public function config_exist() { 72 61 73 if ( file_exists( $this->config_file ) && 62 is_writable( $this->config_file )74 $this->wp_filesystem->is_writable( $this->config_file ) 63 75 ) { 64 76 return $this->config_file; 77 65 78 } 66 79 … … 73 86 * @since 1.0.0 74 87 * 75 * @return bool|string False if we don t get a response, the fresh salts otherwise.88 * @return bool|string False if we don't get a response, the fresh salts otherwise. 76 89 */ 77 90 public function get_fresh_salts() { … … 106 119 $new_salts = $this->get_fresh_salts(); 107 120 108 // Bail if we don t get a response from the api.121 // Bail if we don't get a response from the API. 109 122 if ( false === $new_salts ) { 110 123 return false; … … 149 162 150 163 // Rename the file. 151 rename( $this->tmp_config_file, $this->config_file );164 $this->wp_filesystem->move( $this->tmp_config_file, $this->config_file, true ); 152 165 153 166 // Keep the original permissions of wp-config.php. 154 chmod( $this->config_file, $config_permissions );167 $this->wp_filesystem->chmod( $this->config_file, $config_permissions ); 155 168 156 169 return true; … … 158 171 159 172 /** 160 * Loop the salts, find them in the config file and replace them with the newly gener eated ones.173 * Loop the salts, find them in the config file and replace them with the newly generated ones. 161 174 * 162 175 * @since 1.0.0 -
sg-security/trunk/readme.txt
r3142129 r3149177 5 5 Tested up to: 6.6 6 6 Requires PHP: 7.0 7 Stable tag: 1.5. 37 Stable tag: 1.5.4 8 8 License: GPLv3 9 9 License URI: http://www.gnu.org/licenses/gpl-3.0.html … … 144 144 145 145 == Changelog == 146 147 = Version 1.5.4 = 148 Release Date: Sep 10th, 2024 149 150 * Activity log code improvements. 151 * Salt Shaker code improvements. 146 152 147 153 = Version 1.5.3 = -
sg-security/trunk/sg-security.php
r3142129 r3149177 11 11 * Plugin URI: https://siteground.com 12 12 * Description: Security Optimizer by SiteGround is the all-in-one security solution for your WordPress website. With the carefully selected and easy to configure functions the plugin provides everything you need to secure your website and prevent a number of threats such as brute-force attacks, compromised login, data leaks and more. 13 * Version: 1.5. 313 * Version: 1.5.4 14 14 * Author: SiteGround 15 15 * Author URI: https://www.siteground.com … … 33 33 // Define version constant. 34 34 if ( ! defined( __NAMESPACE__ . '\VERSION' ) ) { 35 define( __NAMESPACE__ . '\VERSION', '1.5. 3' );35 define( __NAMESPACE__ . '\VERSION', '1.5.4' ); 36 36 } 37 37
Note: See TracChangeset
for help on using the changeset viewer.