| 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 |
add_action( 'update_option_siteurl', [ $this, 'update_option_url' ], 10, 2 ); |
| 27 |
|
| 28 |
// If the firewall or whitelist rules do not exist, attempt to pull fresh. |
| 29 |
$token = get_option( 'patchstack_api_token', false ); |
| 30 |
if ( ! empty( $token ) && ( get_option( 'patchstack_firewall_rules', '' ) == '' || get_option( 'patchstack_whitelist_keys_rules' ) == '' ) && get_option( 'patchstack_license_free', 0 ) != 1 ) { |
| 31 |
do_action( 'patchstack_post_dynamic_firewall_rules' ); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Display error message if file/folder permissions are not set properly. |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function file_error_notice() { |
| 41 |
// No need to display this error if the .htaccess functionality has been disabled. |
| 42 |
if ( get_site_option( 'patchstack_disable_htaccess', 0 ) ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
// No need to display if a free user without protection. |
| 47 |
if ( get_option( 'patchstack_license_free', 0 ) == 1 ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// No need to display on nginx. |
| 52 |
if ( isset( $_SERVER['SERVER_SOFTWARE'] ) && stripos( $_SERVER['SERVER_SOFTWARE'], 'nginx' ) !== false ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// Check root .htaccess file and data folder writability. |
| 57 |
$files = []; |
| 58 |
if ( file_exists( ABSPATH . '.htaccess' ) && ! wp_is_writable( ABSPATH . '.htaccess' ) ) { |
| 59 |
array_push( $files, ABSPATH . '.htaccess' ); |
| 60 |
} |
| 61 |
|
| 62 |
// Are there any errors to display? |
| 63 |
if ( count( $files ) > 0 ) { |
| 64 |
?> |
| 65 |
<div class="error notice"> |
| 66 |
<h2>Patchstack File Permission Error</h2> |
| 67 |
<p><?php esc_html_e( 'The following file/folder could not be written to:<br />' . implode( '<br />', $files ), 'patchstack' ); ?></p> |
| 68 |
<?php |
| 69 |
foreach ( $files as $file ) { |
| 70 |
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 ); |
| 71 |
} |
| 72 |
?> |
| 73 |
<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> |
| 74 |
<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> |
| 75 |
</div> |
| 76 |
<?php |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* When the user updates the site URL, update it on the API side as well. |
| 82 |
* This needs to be done so we can communicate with the site properly. |
| 83 |
* |
| 84 |
* @param mixed $old_value |
| 85 |
* @param mixed $new_value |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function update_option_url( $old_value, $new_value ) { |
| 89 |
if ( $old_value != $new_value ) { |
| 90 |
$this->plugin->api->update_url( [ 'plugin_url' => $new_value ] ); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|