Plugin Directory

Changeset 3149177


Ignore:
Timestamp:
09/10/2024 11:24:44 AM (15 months ago)
Author:
ignatggeorgiev
Message:

Bump to 1.5.4

Location:
sg-security/trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • sg-security/trunk/core/Activity_Log/Activity_Log.php

    r2907708 r3149177  
    143143
    144144        if ( ! class_exists( $class ) ) {
    145             throw new \Exception( 'Unknown activity log type "' . $type . '".' );
     145            throw new \Exception( 'Unknown activity log type "' . esc_html( $type ) . '".' );
    146146        }
    147147
     
    151151
    152152    /**
    153      * Set the cron job for deleting old logs.
     153     * Set the CRON job for deleting old logs.
    154154     *
    155155     * @since  1.0.0
    156156     */
    157157    public function set_sgs_logs_cron() {
    158         // Bail if cron is disabled.
     158        // Bail if CRON is disabled.
    159159        if ( 1 === Helper_Service::is_cron_disabled() ) {
    160160            return;
     
    167167
    168168    /**
    169      * Delete logs on plugin page if cron is disabled.
     169     * Delete logs on plugin page if CRON is disabled.
    170170     *
    171171     * @since  1.0.0
    172172     */
    173173    public function delete_logs_on_admin_page() {
    174         // Delete if we are on plugin page and cron is disabled.
     174        // Delete if we are on plugin page and CRON is disabled.
    175175        if (
    176176            isset( $_GET['page'] ) &&
     
    227227     */
    228228    public static function get_activity_log_lifetime() {
    229         // Set custom log lifetime interval in days. The intval covers the cases for string, array and sql injections.
     229        // Set custom log lifetime interval in days. The intval covers the cases for string, array and SQL injections.
    230230        $log_lifetime = intval( apply_filters( 'sgs_set_activity_log_lifetime', get_option( 'sgs_activity_log_lifetime', 12 ) ) );
    231231
  • sg-security/trunk/core/Activity_Log/Activity_Log_Helper.php

    r2907443 r3149177  
    88 */
    99class 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    }
    1023
    1124    /**
     
    107120     * @param  array $args Array of event args.
    108121     *
    109      * @return bool       True if the entry alredy exists, false otherwise.
     122     * @return bool       True if the entry already exists, false otherwise.
    110123     */
    111124    public function check_for_duplicates( $args ) {
    112         global $wpdb;
    113125
    114126        // Bail if table doesn't exist.
    115         if ( ! Helper::table_exists( $wpdb->sgs_visitors ) ) {
     127        if ( ! Helper::table_exists( $this->wpdb->sgs_visitors ) ) {
    116128            return false;
    117129        }
    118130
    119         $has_duplicate = $wpdb->get_row( // phpcs:ignore
    120             $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 ) . '`
    122134                    WHERE `visitor_id` = %s
    123135                        AND `ts` = %s
     
    125137                        LIMIT 1
    126138                        ;',
    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
    132145
    133146        if ( $has_duplicate ) {
     
    146159     */
    147160    public function insert( $args ) {
    148         global $wpdb;
    149161
    150162        if ( $this->check_for_duplicates( $args ) ) {
     
    152164        }
    153165
    154         $wpdb->insert(
    155             $wpdb->sgs_log,
     166        $this->wpdb->insert(
     167            $this->wpdb->sgs_log,
    156168            array(
    157169                'visitor_id'   => $args['visitor_id'],
     
    181193     */
    182194    public function get_visitor_by_user_id( $user_id ) {
    183         global $wpdb;
    184195
    185196        // 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 ) . '`
    189199                    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.
    194206
    195207        // If there is such record, return the visitor ID.
     
    199211
    200212        // 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,
    203215            array(
    204216                'user_id' => $user_id,
     
    208220        );
    209221
    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` = %s
    215                     LIMIT 1;',
    216                 $user_id
    217             )
    218         );
    219 
    220222        // Return the ID.
    221         return $id->ID;
    222     }
    223 
    224     /**
    225      * Get the visitor unique ID by Ip address.
     223        return $this->wpdb->insert_id;
     224    }
     225
     226    /**
     227     * Get the visitor unique ID by IP address.
    226228     *
    227229     * @since  1.0.0
     
    232234     */
    233235    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 ) . '`
    238239                    WHERE `ip` = %s
    239240                    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
    244247
    245248        if ( ! is_null( $maybe_id ) ) {
     
    247250        }
    248251
    249         // Insert the visitors ip in 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,
    252255            array(
    253256                'ip' => $ip,
     
    256259        );
    257260
    258         return $wpdb->insert_id;
     261        return $this->wpdb->insert_id;
    259262    }
    260263
     
    265268     */
    266269    public function add_log_visitor_indexes() {
    267         global $wpdb;
    268270
    269271        // Bail if tables does not exist.
    270272        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 )
    273275        ) {
    274276            return;
     
    276278
    277279        // 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'" );
    280282
    281283        // Add log event index if not set.
    282284        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`)' );
    284286        }
    285287
    286288        // Add the IP index if not set.
    287289        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`)' );
    289291        }
    290292    }
     
    296298     */
    297299    public function adjust_visitors_indexes() {
    298         global $wpdb;
    299300
    300301        // Bail if table does not exist.
    301         if ( ! Helper::table_exists( $wpdb->sgs_visitors ) ) {
     302        if ( ! Helper::table_exists( $this->wpdb->sgs_visitors ) ) {
    302303            return;
    303304        }
    304305
    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'" );
    307308
    308309        // Drop the user id index.
    309310        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' ) . '`' );
    311312        }
    312313
    313314        // Add the Block/User complex index if not set.
    314315        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`)' );
    316317        }
    317318    }
  • sg-security/trunk/core/Activity_Log/Activity_Log_Weekly_Emails.php

    r3000015 r3149177  
    1919
    2020    /**
     21     * Database placeholder.
     22     */
     23    public $wpdb;
     24
     25    /**
    2126     * The constructor.
    2227     *
     
    2429     */
    2530    public function __construct() {
     31        // Assign the Database.
     32        global $wpdb;
     33        $this->wpdb = $wpdb;
    2634
    2735        // Initiate the Email Service Class.
     
    222230     */
    223231    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
    233244    }
    234245
     
    244255     */
    245256    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
    255270    }
    256271
     
    268283
    269284    /**
    270      * Get notification receipient emails.
     285     * Get notification recipient emails.
    271286     *
    272287     * @since  1.2.0
     
    277292        $data = array();
    278293
    279         // Get the currently set receipients.
     294        // Get the currently set recipients.
    280295        $receipients = get_option( 'sg_security_notification_emails', array() );
    281296
    282         // Return empty array if no receipients are set.
     297        // Return empty array if no recipients are set.
    283298        if ( empty( $receipients ) ) {
    284299            return $data;
  • sg-security/trunk/core/Cli/Cli_List.php

    r3129214 r3149177  
    113113     * @param  string $type The type of log we want.
    114114     *
    115      * @return string       The sql query.
     115     * @return string       The SQL query.
    116116     */
    117117    public function get_query( $type ) {
     
    160160        foreach ( $visitors as $visit ) {
    161161            $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' ),
    163163                'Visitor Type' => $visit['visitor_type'],
    164164                'IP Address'   => $visit['ip'],
     
    182182
    183183        // Get all user visitors from the database.
    184         $visitors = $this->wpdb->get_results( // phpcs:ignore
    185             'SELECT * FROM `' . $this->wpdb->sgs_visitors . '`
    186                 WHERE `user_id` != 0
     184        $query = $this->wpdb->prepare(
     185            'SELECT * FROM `' . esc_sql( $this->wpdb->sgs_visitors ) . '`
     186                WHERE `user_id` != %d
    187187            ;',
    188             OBJECT_K
     188            0
    189189        );
    190190
    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.
    192194        $data = array();
    193195        foreach ( $results as $entry ) {
    194196            $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' ),
    196198                'user'       => $entry['ip'],
    197199                'visitor_id' => $entry['id'],
     
    228230
    229231        // Get visitors data.
    230         $visitors = $this->wpdb->get_results( // phpcs:ignore
    231             'SELECT * FROM `' . $this->wpdb->sgs_visitors . '`
    232                 WHERE `user_id` != 0
     232        $query = $this->wpdb->prepare(
     233            'SELECT * FROM `' . esc_sql( $this->wpdb->sgs_visitors ) . '`
     234                WHERE `user_id` != %d
    233235            ;',
    234             OBJECT_K
     236            0
    235237        );
     238
     239        $visitors = $this->wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore
    236240
    237241        // Populate the data for the table.
     
    243247            // Add the data to the table array.
    244248            $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' ),
    246250                'IP Address' => $entry['ip'],
    247251                'Activity'   => $entry['description'],
  • sg-security/trunk/core/Rest/Rest_Helper_Activity.php

    r2904929 r3149177  
    331331            $data[] = array(
    332332                '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' ),
    334334                'ip'           => $entry['ip'],
    335335                'page_visited' => $entry['description'],
     
    398398            $data[] = array(
    399399                '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' ),
    401401                'ip'           => $entry['ip'],
    402402                'activity'     => $entry['description'],
     
    720720        foreach ( $results as $entry ) {
    721721            $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' ),
    723723                'user'       => $entry['ip'],
    724724                'visitor_id' => $entry['id'],
     
    742742
    743743            $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' ),
    745745                'user'       => $ip,
    746746                'visitor_id' => 0,
  • sg-security/trunk/core/Salt_Shaker/Salt_Shaker.php

    r2582031 r3149177  
    11<?php
    22namespace SG_Security\Salt_Shaker;
     3
     4use SiteGround_Helper\Helper_Service;
    35
    46/**
     
    5254
    5355    /**
     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    /**
    5465     * Check if the config exists.
    5566     *
     
    5970     */
    6071    public function config_exist() {
     72
    6173        if ( file_exists( $this->config_file ) &&
    62             is_writable( $this->config_file )
     74            $this->wp_filesystem->is_writable( $this->config_file )
    6375        ) {
    6476            return $this->config_file;
     77
    6578        }
    6679
     
    7386     * @since  1.0.0
    7487     *
    75      * @return bool|string False if we dont get a response, the fresh salts otherwise.
     88     * @return bool|string False if we don't get a response, the fresh salts otherwise.
    7689     */
    7790    public function get_fresh_salts() {
     
    106119        $new_salts = $this->get_fresh_salts();
    107120
    108         // Bail if we dont get a response from the api.
     121        // Bail if we don't get a response from the API.
    109122        if ( false === $new_salts ) {
    110123            return false;
     
    149162
    150163        // 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 );
    152165
    153166        // 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 );
    155168
    156169        return true;
     
    158171
    159172    /**
    160      * Loop the salts, find them in the config file and replace them with the newly genereated ones.
     173     * Loop the salts, find them in the config file and replace them with the newly generated ones.
    161174     *
    162175     * @since  1.0.0
  • sg-security/trunk/readme.txt

    r3142129 r3149177  
    55Tested up to: 6.6
    66Requires PHP: 7.0
    7 Stable tag: 1.5.3
     7Stable tag: 1.5.4
    88License: GPLv3
    99License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    144144
    145145== Changelog ==
     146
     147= Version 1.5.4 =
     148Release Date: Sep 10th, 2024
     149
     150* Activity log code improvements.
     151* Salt Shaker code improvements.
    146152
    147153= Version 1.5.3 =
  • sg-security/trunk/sg-security.php

    r3142129 r3149177  
    1111 * Plugin URI:        https://siteground.com
    1212 * 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.3
     13 * Version:           1.5.4
    1414 * Author:            SiteGround
    1515 * Author URI:        https://www.siteground.com
     
    3333// Define version constant.
    3434if ( ! defined( __NAMESPACE__ . '\VERSION' ) ) {
    35     define( __NAMESPACE__ . '\VERSION', '1.5.3' );
     35    define( __NAMESPACE__ . '\VERSION', '1.5.4' );
    3636}
    3737
Note: See TracChangeset for help on using the changeset viewer.