siteguard-admin-filter.php
11 years ago
siteguard-base.php
11 years ago
siteguard-captcha.php
11 years ago
siteguard-config.php
11 years ago
siteguard-disable-pingback.php
11 years ago
siteguard-htaccess.php
11 years ago
siteguard-login-alert.php
11 years ago
siteguard-login-history.php
11 years ago
siteguard-login-lock.php
11 years ago
siteguard-rename-login.php
11 years ago
siteguard-updates-notify.php
11 years ago
siteguard-waf-exclude-rule.php
11 years ago
siteguard-updates-notify.php
272 lines
| 1 | <?php |
| 2 | /* |
| 3 | This function based on WP Updates Notifier 1.4.1 by Scott Cariss. |
| 4 | */ |
| 5 | class SiteGuard_UpdatesNotify extends SiteGuard_Base { |
| 6 | const CRON_NAME = 'siteguard_update_check'; |
| 7 | |
| 8 | function __construct( ) { |
| 9 | add_action( self::CRON_NAME, array( $this, 'do_update_check' ) ); // action to link cron task to actual task |
| 10 | } |
| 11 | |
| 12 | public function init( ) { |
| 13 | global $config; |
| 14 | $config->set( 'notify_wpcore', '1' ); |
| 15 | $config->set( 'notify_plugins', '2' ); |
| 16 | $config->set( 'notify_themes', '2' ); |
| 17 | $config->set( 'notified', array( 'core' => '', 'plugin' => array(), 'theme' => array() ) ); |
| 18 | $config->set( 'last_check_time', false ); |
| 19 | // We need save the configuration before calling self::check_requirements. |
| 20 | $config->update( ); |
| 21 | if ( true === self::check_requirements( ) ) { |
| 22 | $config->set( 'updates_notify_enable', '1' ); |
| 23 | $config->update( ); |
| 24 | self::feature_on( ); |
| 25 | } else { |
| 26 | $config->set( 'updates_notify_enable', '0' ); |
| 27 | $config->update( ); |
| 28 | } |
| 29 | } |
| 30 | public function check_requirements( ) { |
| 31 | $error = check_multisite( ); |
| 32 | if ( is_wp_error( $error ) ) { |
| 33 | return $error; |
| 34 | } |
| 35 | $error = self::check_disable_wp_cron( ); |
| 36 | if ( is_wp_error( $error ) ) { |
| 37 | return $error; |
| 38 | } |
| 39 | $error = self::check_wp_cron_access( ); |
| 40 | if ( is_wp_error( $error ) ) { |
| 41 | return $error; |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | static function check_disable_wp_cron( ) { |
| 46 | if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { |
| 47 | $message = esc_html__( "DISABLE_WP_CRON is defined true. This function can't be used.", 'siteguard' ); |
| 48 | $error = new WP_Error( 'siteguard_updates_notify', $message ); |
| 49 | return $error; |
| 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | static function check_wp_cron_access( ) { |
| 54 | $result = wp_remote_post( site_url( '/wp-cron.php' ) ); |
| 55 | if ( ! is_wp_error( $result ) && 200 === $result['response']['code'] ) { |
| 56 | return true; |
| 57 | } |
| 58 | $message = esc_html__( 'Please solve the problem that can not be accessed wp-cron.php. Might be access control.', 'siteguard' ); |
| 59 | $error = new WP_Error( 'siteguard_updates_notify', $message ); |
| 60 | return $error; |
| 61 | } |
| 62 | public function feature_on( ) { |
| 63 | // Already scheduled |
| 64 | if ( false !== wp_get_schedule( self::CRON_NAME ) ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | // Schedule cron for this plugin. |
| 69 | wp_schedule_event( time(), 'daily', self::CRON_NAME ); |
| 70 | } |
| 71 | |
| 72 | public function feature_off() { |
| 73 | wp_clear_scheduled_hook( self::CRON_NAME ); // clear cron |
| 74 | } |
| 75 | |
| 76 | public function do_update_check() { |
| 77 | global $config; |
| 78 | $message = ''; // start with a blank message |
| 79 | if ( '0' != $config->get( 'notify_wpcore' ) ) { // are we to check for WordPress core? |
| 80 | $core_updated = self::core_update_check( $message ); // check the WP core for updates |
| 81 | } |
| 82 | if ( '0' != $config->get( 'notify_plugins' ) ) { // are we to check for plugin updates? |
| 83 | $plugins_updated = self::plugins_update_check( $message, $config->get( 'notify_plugins' ) ); // check for plugin updates |
| 84 | } else { |
| 85 | $plugins_updated = false; // no plugin updates |
| 86 | } |
| 87 | if ( '0' != $config->get( 'notify_themes' ) ) { // are we to check for theme updates? |
| 88 | $themes_updated = self::themes_update_check( $message, $config->get( 'notify_themes' ) ); // check for theme updates |
| 89 | } else { |
| 90 | $themes_updated = false; // no theme updates |
| 91 | } |
| 92 | if ( $core_updated || $plugins_updated || $themes_updated ) { // Did anything come back as need updating? |
| 93 | $message = esc_html__( 'There are updates available for your WordPress site:', 'siteguard' ) . "\n" . $message . "\n"; |
| 94 | $message .= sprintf( esc_html__( 'Please visit %s to update.', 'siteguard' ), admin_url( 'update-core.php' ) ) . "\n\n--\nSiteGuard WP Plugin"; |
| 95 | self::send_notify( $message ); // send our notification email. |
| 96 | } |
| 97 | |
| 98 | self::log_last_check_time(); |
| 99 | } |
| 100 | |
| 101 | private static function core_update_check( &$message ) { |
| 102 | global $config, $wp_version; |
| 103 | do_action( 'wp_version_check' ); // force WP to check its core for updates |
| 104 | $update_core = get_site_transient( 'update_core' ); // get information of updates |
| 105 | $notified = $config->get( 'notified' ); |
| 106 | if ( 'upgrade' == $update_core->updates[0]->response ) { // is WP core update available? |
| 107 | if ( $update_core->updates[0]->current != $notified['core'] ) { // have we already notified about this version? |
| 108 | require_once( ABSPATH . WPINC . '/version.php' ); // Including this because some plugins can mess with the real version stored in the DB. |
| 109 | $new_core_ver = $update_core->updates[0]->current; // The new WP core version |
| 110 | $old_core_ver = $wp_version; // the old WP core version |
| 111 | $message .= "\n" . sprintf( esc_html__( 'WP-Core: WordPress is out of date. Please update from version %s to %s', 'siteguard' ), $old_core_ver, $new_core_ver ) . "\n"; |
| 112 | $notified['core'] = $new_core_ver; // set core version we are notifying about |
| 113 | $config->set( 'notified', $notified ); |
| 114 | $config->update( ); |
| 115 | return true; // we have updates so return true |
| 116 | } else { |
| 117 | return false; // There are updates but we have already notified in the past. |
| 118 | } |
| 119 | } |
| 120 | $notified['core'] = ''; // no updates lets set this nothing |
| 121 | $config->set( 'notified', $notified ); |
| 122 | $config->update( ); |
| 123 | return false; // no updates return false |
| 124 | } |
| 125 | |
| 126 | private static function plugins_update_check( &$message, $allOrActive ) { |
| 127 | global $config, $wp_version; |
| 128 | $cur_wp_version = preg_replace( '/-.*$/', '', $wp_version ); |
| 129 | $notified = $config->get( 'notified' ); |
| 130 | do_action( 'wp_update_plugins' ); // force WP to check plugins for updates |
| 131 | $update_plugins = get_site_transient( 'update_plugins' ); // get information of updates |
| 132 | if ( ! empty( $update_plugins->response ) ) { // any plugin updates available? |
| 133 | $plugins_need_update = $update_plugins->response; // plugins that need updating |
| 134 | if ( 2 == $allOrActive ) { // are we to check just active plugins? |
| 135 | $active_plugins = array_flip( get_option( 'active_plugins' ) ); // find which plugins are active |
| 136 | $plugins_need_update = array_intersect_key( $plugins_need_update, $active_plugins ); // only keep plugins that are active |
| 137 | } |
| 138 | $plugins_need_update = self::check_plugins_against_notified( $plugins_need_update ); // additional filtering of plugins need update |
| 139 | if ( is_array( $plugins_need_update ) && count( $plugins_need_update ) >= 1 ) { // any plugins need updating after all the filtering gone on above? |
| 140 | require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); // Required for plugin API |
| 141 | require_once( ABSPATH . WPINC . '/version.php' ); // Required for WP core version |
| 142 | foreach ( $plugins_need_update as $key => $data ) { // loop through the plugins that need updating |
| 143 | $plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $key ); // get local plugin info |
| 144 | $info = plugins_api( 'plugin_information', array( 'slug' => $data->slug ) ); // get repository plugin info |
| 145 | $message .= "\n" . sprintf( esc_html__( 'Plugin: %s is out of date. Please update from version %s to %s', 'siteguard' ), $plugin_info['Name'], $plugin_info['Version'], $data->new_version ) . "\n"; |
| 146 | $message .= "\t" . sprintf( esc_html__( 'Details: %s', 'siteguard' ), $data->url ) . "\n"; |
| 147 | $message .= "\t" . sprintf( esc_html__( 'Changelog: %s%s', 'siteguard' ), $data->url, 'changelog/' ) . "\n"; |
| 148 | if ( isset( $info->tested ) && version_compare( $info->tested, $wp_version, '>=' ) ) { |
| 149 | $compat = sprintf( esc_html__( 'Compatibility with WordPress %1$s: 100%% (according to its author)' ), $cur_wp_version ); |
| 150 | } elseif ( isset( $info->compatibility[ $wp_version ][ $data->new_version ] ) ) { |
| 151 | $compat = $info->compatibility[ $wp_version ][ $data->new_version ]; |
| 152 | $compat = sprintf( esc_html__( 'Compatibility with WordPress %1$s: %2$d%% (%3$d "works" votes out of %4$d total)' ), $wp_version, $compat[0], $compat[2], $compat[1] ); |
| 153 | } else { |
| 154 | $compat = sprintf( esc_html__( 'Compatibility with WordPress %1$s: Unknown' ), $wp_version ); |
| 155 | } |
| 156 | $message .= "\t" . sprintf( esc_html__( 'Compatibility: %s', 'siteguard' ), $compat ) . "\n"; |
| 157 | $notified['plugin'][ $key ] = $data->new_version; // set plugin version we are notifying about |
| 158 | } |
| 159 | $config->set( 'notified', $notified ); |
| 160 | $config->update( ); |
| 161 | return true; // we have plugin updates return true |
| 162 | } |
| 163 | } else { |
| 164 | if ( 0 != count( $notified['plugin'] ) ) { // is there any plugin notifications? |
| 165 | $notified['plugin'] = array(); // set plugin notifications to empty as all plugins up-to-date |
| 166 | $config->set( 'notified', $notified ); |
| 167 | $config->update( ); |
| 168 | } |
| 169 | } |
| 170 | return false; // No plugin updates so return false |
| 171 | } |
| 172 | |
| 173 | private function themes_update_check( &$message, $allOrActive ) { |
| 174 | global $config; |
| 175 | $notified = $config->get( 'notified' ); |
| 176 | do_action( 'wp_update_themes' ); // force WP to check for theme updates |
| 177 | $update_themes = get_site_transient( 'update_themes' ); // get information of updates |
| 178 | if ( ! empty( $update_themes->response ) ) { // any theme updates available? |
| 179 | $themes_need_update = $update_themes->response; // themes that need updating |
| 180 | if ( 2 == $allOrActive ) { // are we to check just active themes? |
| 181 | $active_theme = array( get_option( 'template' ) => array() ); // find current theme that is active |
| 182 | $themes_need_update = array_intersect_key( $themes_need_update, $active_theme ); // only keep theme that is active |
| 183 | } |
| 184 | $themes_need_update = self::check_themes_against_notified( $themes_need_update ); // additional filtering of themes need update |
| 185 | if ( is_array( $themes_need_update ) && count( $themes_need_update ) >= 1 ) { // any themes need updating after all the filtering gone on above? |
| 186 | foreach ( $themes_need_update as $key => $data ) { // loop through the themes that need updating |
| 187 | $theme_info = wp_get_theme( $key ); // get theme info |
| 188 | $message .= "\n" . sprintf( esc_html__( 'Theme: %s is out of date. Please update from version %s to %s', 'siteguard' ), $theme_info['Name'], $theme_info['Version'], $data['new_version'] ) . "\n"; |
| 189 | $notified['theme'][ $key ] = $data['new_version']; // set theme version we are notifying about |
| 190 | } |
| 191 | $config->set( 'notified', $notified ); |
| 192 | $config->update( ); |
| 193 | return true; // we have theme updates return true |
| 194 | } |
| 195 | } else { |
| 196 | if ( 0 != count( $notified['theme'] ) ) { // is there any theme notifications? |
| 197 | $notified['theme'] = array(); // set theme notifications to empty as all themes up-to-date |
| 198 | $config->set( 'notified', $notified ); |
| 199 | $config->update( ); |
| 200 | } |
| 201 | } |
| 202 | return false; // No theme updates so return false |
| 203 | } |
| 204 | |
| 205 | public function check_plugins_against_notified( $plugins_need_update ) { |
| 206 | global $config; |
| 207 | $notified = $config->get( 'notified' ); |
| 208 | if ( is_array( $plugins_need_update ) ) { |
| 209 | foreach ( $plugins_need_update as $key => $data ) { // loop through plugins that need update |
| 210 | if ( isset( $notified['plugin'][ $key ] ) ) { // has this plugin been notified before? |
| 211 | if ( $data->new_version == $notified['plugin'][ $key ] ) { // does this plugin version match that of the one that's been notified? |
| 212 | unset( $plugins_need_update[ $key ] ); // don't notify this plugin as has already been notified |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | return $plugins_need_update; |
| 218 | } |
| 219 | |
| 220 | public function check_themes_against_notified( $themes_need_update ) { |
| 221 | global $config; |
| 222 | $notified = $config->get( 'notified' ); |
| 223 | if ( is_array( $themes_need_update ) ) { |
| 224 | foreach ( $themes_need_update as $key => $data ) { // loop through themes that need update |
| 225 | if ( isset( $notified['theme'][ $key ] ) ) { // has this theme been notified before? |
| 226 | if ( $data['new_version'] == $notified['theme'][ $key ] ) { // does this theme version match that of the one that's been notified? |
| 227 | unset( $themes_need_update[ $key ] ); // don't notify this theme as has already been notified |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | return $themes_need_update; |
| 233 | } |
| 234 | |
| 235 | public function send_notify( $message ) { |
| 236 | global $config; |
| 237 | $subject = sprintf( esc_html__( 'WordPress: Updates Available @ %s', 'siteguard' ), home_url() ); |
| 238 | |
| 239 | $user_query = new WP_User_Query( array( 'role' => 'Administrator' ) ); |
| 240 | if ( is_array( $user_query->results ) ) { |
| 241 | foreach ( $user_query->results as $user ) { |
| 242 | @wp_mail( $user->get( 'user_email' ), $subject, $message ); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | private function log_last_check_time() { |
| 248 | global $config; |
| 249 | $config->set( 'last_check_time', current_time( 'timestamp' ) ); |
| 250 | $config->update( ); |
| 251 | } |
| 252 | |
| 253 | private static function get_schedules() { |
| 254 | $schedules = wp_get_schedules(); |
| 255 | uasort( $schedules, array( __CLASS__, 'sort_by_interval' ) ); |
| 256 | return $schedules; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | private static function get_intervals() { |
| 261 | $intervals = array_keys( self::get_schedules() ); |
| 262 | return $intervals; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | private static function sort_by_interval( $a, $b ) { |
| 267 | return $a['interval'] - $b['interval']; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | ?> |
| 272 |