PluginProbe
Patchstack – WordPress & Plugins Security / 2.3.0
Patchstack – WordPress & Plugins Security v2.3.0
2.3.7 trunk 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.17 2.1.18 2.1.19 2.1.2 2.1.20 2.1.21 2.1.22 2.1.23 2.1.24 2.1.25 2.1.3 2.1.4 2.1.5 2.1.6 All 49 releases
patchstack / includes / cron.php

cron.php in Patchstack – WordPress & Plugins Security 2.3.0, at includes/cron.php

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