| @@ -17,11 +17,10 @@ | ||
| 17 | 17 | * @return void |
| 18 | 18 | */ |
| 19 | 19 | public function __construct( $core ) { |
| 20 | 20 | parent::__construct( $core ); |
| 21 | - add_action( 'init', [ $this, 'schedule_events' ] ); | |
| 22 | - add_filter( 'cron_schedules', [ $this, 'cron_schedules' ] ); | |
| 23 | - add_action( 'patchstack_check_env', [ $this, 'check_env' ] ); | |
| 21 | + add_action( 'init', array( $this, 'schedule_events' ) ); | |
| 22 | + add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) ); | |
| 24 | 23 | } |
| 25 | 24 | |
| 26 | 25 | /** |
| 27 | 26 | * Define our own custom cron schedules. |
| @@ -30,32 +29,32 @@ | ||
| 30 | 29 | * @return array |
| 31 | 30 | */ |
| 32 | 31 | public function cron_schedules( $schedules ) { |
| 33 | 32 | // Define the scheduled tasks. |
| 34 | - $schedules['patchstack_15minute'] = [ | |
| 33 | + $schedules['patchstack_15minute'] = array( | |
| 35 | 34 | 'interval' => ( 60 * 15 ), |
| 36 | - 'display' => esc_attr__( 'Every 15 Minutes' ), | |
| 37 | - ]; | |
| 35 | + 'display' => __( 'Every 15 Minutes' ), | |
| 36 | + ); | |
| 38 | 37 | |
| 39 | - $schedules['patchstack_hourly'] = [ | |
| 38 | + $schedules['patchstack_hourly'] = array( | |
| 40 | 39 | 'interval' => ( 60 * 60 ), |
| 41 | - 'display' => esc_attr__( 'Every Hour' ), | |
| 42 | - ]; | |
| 40 | + 'display' => __( 'Every Hour' ), | |
| 41 | + ); | |
| 43 | 42 | |
| 44 | - $schedules['patchstack_trihourly'] = [ | |
| 43 | + $schedules['patchstack_trihourly'] = array( | |
| 45 | 44 | 'interval' => ( 60 * 60 * 3 ), |
| 46 | - 'display' => esc_attr__( 'Every 3 Hours' ), | |
| 47 | - ]; | |
| 45 | + 'display' => __( 'Every 3 Hours' ), | |
| 46 | + ); | |
| 48 | 47 | |
| 49 | - $schedules['patchstack_twicedaily'] = [ | |
| 48 | + $schedules['patchstack_twicedaily'] = array( | |
| 50 | 49 | 'interval' => ( 60 * 60 * 12 ), |
| 51 | - 'display' => esc_attr__( '2 Times Daily' ), | |
| 52 | - ]; | |
| 50 | + 'display' => __( '2 Times Daily' ), | |
| 51 | + ); | |
| 53 | 52 | |
| 54 | - $schedules['patchstack_daily'] = [ | |
| 53 | + $schedules['patchstack_daily'] = array( | |
| 55 | 54 | 'interval' => ( 60 * 60 * 24 ), |
| 56 | - 'display' => esc_attr__( '1 Time Daily' ), | |
| 57 | - ]; | |
| 55 | + 'display' => __( '1 Time Daily' ), | |
| 56 | + ); | |
| 58 | 57 | |
| 59 | 58 | return $schedules; |
| 60 | 59 | } |
| 61 | 60 | |
| @@ -65,117 +64,54 @@ | ||
| 65 | 64 | * @return void |
| 66 | 65 | */ |
| 67 | 66 | public function schedule_events() { |
| 68 | 67 | // Random time throughout the day to make sure not all sites synchronize at the same time. |
| 69 | - $crons = get_option( 'patchstack_cron_offset', false ); | |
| 70 | - if ( ! $crons || ! isset ( $crons['patchstack_daily'] ) ) { | |
| 71 | - $crons = [ | |
| 68 | + if ( ! get_option( 'patchstack_cron_offset' ) ) { | |
| 69 | + $crons = array( | |
| 72 | 70 | 'patchstack_daily' => strtotime( 'today' ) + mt_rand( 0, 86399 ), |
| 73 | 71 | 'patchstack_hourly' => strtotime( 'today' ) + mt_rand( 0, 3600 ), |
| 74 | 72 | 'patchstack_trihourly' => strtotime( 'today' ) + mt_rand( 0, 9599 ), |
| 75 | 73 | 'patchstack_twicedaily' => strtotime( 'today' ) + mt_rand( 0, 43199 ), |
| 76 | 74 | 'patchstack_15minute' => strtotime( 'today' ) + mt_rand( 0, 899 ), |
| 77 | - ]; | |
| 75 | + ); | |
| 78 | 76 | update_option( 'patchstack_cron_offset', $crons ); |
| 79 | 77 | |
| 80 | 78 | // Clear existing scheduled events so we can use the new timestamps. |
| 81 | - foreach ( [ 'patchstack_send_software_data', 'patchstack_send_hacker_logs', 'patchstack_post_firewall_rules', 'patchstack_post_firewall_htaccess_rules', 'patchstack_post_dynamic_firewall_rules', 'patchstack_update_license_status', 'patchstack_send_event_logs', 'patchstack_update_plugins', 'patchstack_send_ping' ] as $event ) { | |
| 79 | + foreach ( array( 'patchstack_send_software_data', 'patchstack_send_hacker_logs', 'patchstack_post_firewall_rules', 'patchstack_post_firewall_htaccess_rules', 'patchstack_post_dynamic_firewall_rules', 'patchstack_update_license_status', 'patchstack_send_event_logs', 'patchstack_update_plugins', 'patchstack_send_ping' ) as $event ) { | |
| 82 | 80 | wp_clear_scheduled_hook( $event ); |
| 83 | 81 | } |
| 82 | + } else { | |
| 83 | + $crons = get_option( 'patchstack_cron_offset' ); | |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | // List of the different events. |
| 87 | - $free = [ | |
| 88 | - 'patchstack_send_software_data' => 'patchstack_twicedaily', | |
| 87 | + $free = array( | |
| 88 | + 'patchstack_send_software_data' => 'patchstack_hourly', | |
| 89 | 89 | 'patchstack_update_license_status' => 'patchstack_twicedaily', |
| 90 | 90 | 'patchstack_send_ping' => 'patchstack_trihourly', |
| 91 | - 'patchstack_update_plugins' => 'patchstack_15minute' | |
| 92 | - ]; | |
| 91 | + 'patchstack_update_plugins' => 'patchstack_15minute', | |
| 92 | + ); | |
| 93 | 93 | |
| 94 | - $premium = [ | |
| 94 | + $premium = array( | |
| 95 | 95 | 'patchstack_send_hacker_logs' => 'patchstack_15minute', |
| 96 | - 'patchstack_post_firewall_rules' => 'patchstack_daily', | |
| 96 | + 'patchstack_post_firewall_rules' => 'patchstack_twicedaily', | |
| 97 | + 'patchstack_post_firewall_htaccess_rules' => 'patchstack_trihourly', | |
| 97 | 98 | 'patchstack_post_dynamic_firewall_rules' => 'patchstack_hourly', |
| 98 | 99 | 'patchstack_send_event_logs' => 'patchstack_15minute', |
| 99 | - 'patchstack_check_env' => 'patchstack_trihourly' | |
| 100 | - ]; | |
| 100 | + ); | |
| 101 | 101 | |
| 102 | 102 | // Schedule the events if they are not scheduled yet. |
| 103 | - if ( $this->get_option( 'patchstack_clientid', false ) ) { | |
| 104 | - // These events should always be scheduled for activated sites. | |
| 105 | - foreach ( $free as $event => $interval ) { | |
| 103 | + foreach ( $free as $event => $interval ) { | |
| 104 | + if ( ! wp_next_scheduled( $event ) ) { | |
| 105 | + wp_schedule_event( $crons[ $interval ], $interval, $event ); | |
| 106 | + } | |
| 107 | + } | |
| 108 | + | |
| 109 | + if ( $this->get_option( 'patchstack_license_free', 0 ) == 0 ) { | |
| 110 | + foreach ( $premium as $event => $interval ) { | |
| 106 | 111 | if ( ! wp_next_scheduled( $event ) ) { |
| 107 | 112 | wp_schedule_event( $crons[ $interval ], $interval, $event ); |
| 108 | 113 | } |
| 109 | 114 | } |
| 110 | - | |
| 111 | - // Only schedule these events for protected sites. | |
| 112 | - if ( $this->get_option( 'patchstack_license_free', 0 ) != 1 && $this->license_is_active() ) { | |
| 113 | - foreach ( $premium as $event => $interval ) { | |
| 114 | - if ( ! wp_next_scheduled( $event ) ) { | |
| 115 | - wp_schedule_event( $crons[ $interval ], $interval, $event ); | |
| 116 | - } | |
| 117 | - } | |
| 118 | - } | |
| 119 | 115 | } |
| 120 | - } | |
| 121 | - | |
| 122 | - /** | |
| 123 | - * Determine if the environment has been changed, we must do this to determine if the | |
| 124 | - * real IP address header has changed to a different HTTP field. | |
| 125 | - * | |
| 126 | - * @return void | |
| 127 | - */ | |
| 128 | - public function check_env() { | |
| 129 | - $hash = $this->get_option( 'patchstack_environment_hash', '' ); | |
| 130 | - $computed = $this->get_option( 'patchstack_ip_header_computed', 0 ); | |
| 131 | - $env_hash = $this->get_env_hash(); | |
| 132 | - | |
| 133 | - // If hash has not been computed yet, but IP header has been computed, we ignore for the first time. | |
| 134 | - if ( $hash == '' && $computed ) { | |
| 135 | - update_option( 'patchstack_environment_hash', $env_hash ); | |
| 136 | - return; | |
| 137 | - } | |
| 138 | - | |
| 139 | - // If computed hash and previous hash are the same, we ignore. | |
| 140 | - if ( $hash == $env_hash ) { | |
| 141 | - return; | |
| 142 | - } | |
| 143 | - | |
| 144 | - // At this point we can assume the previous environment hash and the current environment hash are different. | |
| 145 | - // In this scenario, we want to determine which IP address header contains the appropriate IP address. | |
| 146 | - update_option( 'patchstack_ip_header_force_compute', 1 ); | |
| 147 | - update_option( 'patchstack_environment_hash', $env_hash ); | |
| 148 | - do_action( 'patchstack_send_header_request' ); | |
| 149 | - } | |
| 150 | - | |
| 151 | - /** | |
| 152 | - * Compute a hash of several server specific values. | |
| 153 | - * | |
| 154 | - * @return string | |
| 155 | - */ | |
| 156 | - private function get_env_hash() { | |
| 157 | - // Parameters of which we want the full string. | |
| 158 | - $params_full = [ 'SERVER_ADDR', 'SERVER_NAME', 'SERVER_SOFTWARE' ]; | |
| 159 | - | |
| 160 | - // Parameters of which we want to get boolean value. | |
| 161 | - $params_boolean = [ 'REMOTE_ADDR', 'HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR' ]; | |
| 162 | - | |
| 163 | - // String to compute hash value of. | |
| 164 | - $string = ''; | |
| 165 | - | |
| 166 | - // Append full parameter to string. | |
| 167 | - foreach ( $params_full as $param ) { | |
| 168 | - $string .= isset( $_SERVER[$param] ) ? $_SERVER[$param] : ''; | |
| 169 | - } | |
| 170 | - | |
| 171 | - // Append existence to string, only if not running under cli. | |
| 172 | - $sapi = function_exists( 'php_sapi_name' ) ? php_sapi_name() : false; | |
| 173 | - if ( $sapi && substr( $sapi, 0, 3 ) != 'cli' ) { | |
| 174 | - foreach ( $params_boolean as $param ) { | |
| 175 | - $string .= isset( $_SERVER[$param] ) ? '1' : '0'; | |
| 176 | - } | |
| 177 | - } | |
| 178 | - | |
| 179 | - return sha1( $string ); | |
| 180 | 116 | } |
| 181 | 117 | } |