| 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 for any general admin functionality. |
| 10 |
* For example to display general errors on all pages or event listeners. |
| 11 |
*/ |
| 12 |
class P_Admin_General extends P_Core { |
| 13 |
|
| 14 |
/** |
| 15 |
* Add any general actions for the backend. |
| 16 |
* |
| 17 |
* @param Patchstack $core |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public function __construct( $core ) { |
| 21 |
parent::__construct( $core ); |
| 22 |
|
| 23 |
// Add admin and network notices. |
| 24 |
add_action( 'admin_notices', [ $this, 'file_error_notice' ] ); |
| 25 |
add_action( 'network_admin_notices', [ $this, 'file_error_notice' ] ); |
| 26 |
|
| 27 |
add_action( 'wp_loaded', [ $this, 'update_rules' ] ); |
| 28 |
add_action( 'update_option_siteurl', [ $this, 'update_option_url' ], 10, 2 ); |
| 29 |
add_action( 'admin_init', [ $this, 'alter_ips' ] ); |
| 30 |
add_action( 'admin_init', [ $this, 'enable_settings' ] ); |
| 31 |
|
| 32 |
// If the firewall or whitelist rules do not exist, attempt to pull fresh. |
| 33 |
$token = get_option( 'patchstack_api_token', false ); |
| 34 |
if ( ! empty( $token ) && ( get_option( 'patchstack_firewall_rules', '' ) == '' || get_option( 'patchstack_whitelist_keys_rules' ) == '' ) && get_option( 'patchstack_license_free', 0 ) != 1 ) { |
| 35 |
do_action( 'patchstack_post_dynamic_firewall_rules' ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Display error message if file/folder permissions are not set properly. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function file_error_notice() { |
| 45 |
// No need to display this error if the .htaccess functionality has been disabled. |
| 46 |
if ( get_site_option( 'patchstack_disable_htaccess', 0 ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
// Check root .htaccess file and data folder writability. |
| 51 |
$files = []; |
| 52 |
if ( file_exists( ABSPATH . '.htaccess' ) && ! wp_is_writable( ABSPATH . '.htaccess' ) ) { |
| 53 |
array_push( $files, ABSPATH . '.htaccess' ); |
| 54 |
} |
| 55 |
|
| 56 |
// Are there any errors to display? |
| 57 |
if ( count( $files ) > 0 ) { |
| 58 |
?> |
| 59 |
<div class="error notice"> |
| 60 |
<h2>Patchstack File Permission Error</h2> |
| 61 |
<p><?php esc_html_e( 'The following file/folder could not be written to:<br />' . implode( '<br />', $files ), 'patchstack' ); ?></p> |
| 62 |
<?php |
| 63 |
foreach ( $files as $file ) { |
| 64 |
echo wp_kses( '<p><b>Debug info: </b>' . $file . ' chmod permissions: <b>' . substr( decoct( fileperms( $file ) ), -3 ) . '</b>, owned by <b>' . posix_getpwuid( fileowner( $file ) )['name'] . '</b></p>', $this->allowed_html ); |
| 65 |
} |
| 66 |
?> |
| 67 |
<p><?php esc_html_e( '<strong>How to fix?</strong><br />CHMOD the file/folder to <strong>755</strong> through a <a href="http://www.dummies.com/web-design-development/wordpress/navigation-customization/how-to-change-file-permissions-using-filezilla-on-your-ftp-site/" target="_blank">FTP client</a>, <a href="http://support.hostgator.com/articles/cpanel/how-to-change-permissions-chmod-of-a-file" target="_blank">CPanel</a>, <a href="https://www.inmotionhosting.com/support/website/managing-files/change-file-permissions" target="_blank">WHM</a> or ask your hosting provider. Make sure file or folder ownership is set to <b>' . posix_getpwuid( fileowner( ABSPATH . 'index.php' ) )['name'] . '</b> user .', 'patchstack_file_error_notice' ); ?></p> |
| 68 |
<p><?php esc_html_e( '<strong>CHMOD properly set but still not working?</strong><br />Make sure the group/owner (chown) settings of the /wp-content/plugins/patchstack/ folder is properly setup, you may have to ask your host to fix this.', 'patchstack_file_error_notice' ); ?></p> |
| 69 |
</div> |
| 70 |
<?php |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* When the user changes Patchstack plugin settings, update the firewall rules. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function update_rules() { |
| 80 |
if ( isset( $_GET['settings-updated'], $_GET['page'] ) && strpos( $_GET['page'], 'patchstack' ) !== false && current_user_can( 'administrator' ) ) { |
| 81 |
$this->plugin->rules->post_firewall_rules(); |
| 82 |
$this->plugin->rules->dynamic_firewall_rules(); |
| 83 |
|
| 84 |
// Update firewall status after settings saved |
| 85 |
$token = $this->plugin->api->get_access_token(); |
| 86 |
|
| 87 |
// Update the firewall status. |
| 88 |
if ( ! empty( $token ) ) { |
| 89 |
$this->plugin->api->update_firewall_status( [ 'status' => $this->get_option( 'patchstack_basic_firewall' ) == 1 ] ); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* When the user updates the site URL, update it on the API side as well. |
| 96 |
* This needs to be done so we can communicate with the site properly. |
| 97 |
* |
| 98 |
* @param mixed $old_value |
| 99 |
* @param mixed $new_value |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
public function update_option_url( $old_value, $new_value ) { |
| 103 |
if ( $old_value != $new_value ) { |
| 104 |
$this->plugin->api->update_url( [ 'plugin_url' => $new_value ] ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Executed when the user modifies the blocked or whitelisted IP addresses on the |
| 110 |
* login protection settings page. |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
public function alter_ips() { |
| 115 |
if ( ! isset( $_GET['action'], $_GET['PatchstackNonce'] ) || ! wp_verify_nonce( $_GET['PatchstackNonce'], 'patchstack-nonce-alter-ips' ) || ! current_user_can( 'administrator' ) || ! in_array( $_GET['action'], [ 'patchstack_unblock', 'patchstack_unblock_whitelist', 'patchstack_whitelist' ] ) ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
global $wpdb; |
| 120 |
|
| 121 |
// Unblock the IP; delete the logs of the IP. |
| 122 |
if ( $_GET['action'] == 'patchstack_unblock' && isset( $_GET['id'] ) && ctype_digit( $_GET['id'] ) ) { |
| 123 |
// First get the IP address to unblock. |
| 124 |
$result = $wpdb->get_results( |
| 125 |
$wpdb->prepare( 'SELECT ip FROM ' . $wpdb->prefix . 'patchstack_event_log WHERE id = %d', [ (int) $_GET['id'] ] ) |
| 126 |
); |
| 127 |
|
| 128 |
// Unblock the IP address. |
| 129 |
if ( isset( $result[0], $result[0]->ip ) ) { |
| 130 |
$wpdb->query( |
| 131 |
$wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'patchstack_event_log WHERE ip = %s', [ $result[0]->ip ] ) |
| 132 |
); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
// Unblock and whitelist the IP. |
| 137 |
if ( $_GET['action'] == 'patchstack_unblock_whitelist' && isset( $_GET['id'] ) && ctype_digit( $_GET['id'] ) ) { |
| 138 |
// First get the IP address to whitelist. |
| 139 |
$result = $wpdb->get_results( |
| 140 |
$wpdb->prepare( 'SELECT ip FROM ' . $wpdb->prefix . 'patchstack_event_log WHERE id = %d', [ (int) $_GET['id'] ] ) |
| 141 |
); |
| 142 |
|
| 143 |
// Whitelist and unblock the IP address. |
| 144 |
if ( isset( $result[0], $result[0]->ip ) && filter_var( $result[0]->ip, FILTER_VALIDATE_IP ) ) { |
| 145 |
update_option( 'patchstack_login_whitelist', $this->get_option( 'patchstack_login_whitelist', '' ) . "\n" . $result[0]->ip ); |
| 146 |
$wpdb->query( |
| 147 |
$wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'patchstack_event_log WHERE ip = %s', [ $result[0]->ip ] ) |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// Whitelist an IP address. |
| 153 |
if ( $_GET['action'] == 'patchstack_whitelist' && isset( $_GET['ip'] ) && filter_var( $_GET['ip'], FILTER_VALIDATE_IP ) ) { |
| 154 |
update_option( 'patchstack_login_whitelist', $this->get_option( 'patchstack_login_whitelist', '' ) . "\n" . $_GET['ip'] ); |
| 155 |
} |
| 156 |
|
| 157 |
// Redirect the user back to the login tab. |
| 158 |
wp_safe_redirect( admin_url( 'admin.php?page=' . $this->plugin->name . '&tab=login' ) ); |
| 159 |
exit; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Turn on the Patchstack settings feature on WordPress. |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public function enable_settings() { |
| 168 |
if ( ! isset( $_GET['action'], $_GET['patchstack_settings_nonce'] ) || ! wp_verify_nonce( $_GET['patchstack_settings_nonce'], 'patchstack_settings_nonce' ) || ! current_user_can( 'administrator' ) || $_GET['action'] != 'enable_settings' ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
// Turn it on. |
| 173 |
update_option( 'patchstack_show_settings', 1 ); |
| 174 |
|
| 175 |
// Redirect the user back to the license page. |
| 176 |
wp_safe_redirect( admin_url( 'admin.php?page=' . $this->plugin->name ) ); |
| 177 |
exit; |
| 178 |
} |
| 179 |
} |
| 180 |
|