From e719aa9b63a2d09bcc0a06eb31336339b9128525 Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Sat, 29 Jun 2024 21:24:05 +0800 Subject: [PATCH 01/19] add in redis sockets, ACLs, and constants, update fastcgi to be able to use constants, bring in torden purge_all option --- admin/class-fastcgi-purger.php | 59 +++- admin/class-nginx-helper-admin.php | 141 ++++++--- admin/class-phpredis-purger.php | 35 ++- admin/class-predis-purger.php | 27 +- .../partials/nginx-helper-general-options.php | 281 ++++++++++++++++-- 5 files changed, 472 insertions(+), 71 deletions(-) diff --git a/admin/class-fastcgi-purger.php b/admin/class-fastcgi-purger.php index cce53b57..a869ecad 100644 --- a/admin/class-fastcgi-purger.php +++ b/admin/class-fastcgi-purger.php @@ -69,6 +69,10 @@ public function purge_url( $url, $feed = true ) { case 'get_request': // Go to default case. + + case 'get_request_torden': + // Go to default case. + default: $_url_purge_base = $this->purge_base_url() . $parse['path']; $_url_purge = $_url_purge_base; @@ -137,6 +141,10 @@ public function custom_purge_urls() { case 'get_request': // Go to default case. + + case 'get_request_torden': + // Go to default case. + default: $_url_purge_base = $this->purge_base_url(); @@ -166,9 +174,58 @@ public function custom_purge_urls() { */ public function purge_all() { - $this->unlink_recursive( RT_WP_NGINX_HELPER_CACHE_PATH, false ); + global $nginx_helper_admin; + + $purge_method = $nginx_helper_admin->options['purge_method']; + + switch ( $purge_method ) { + + case 'get_request_torden': + $site = get_site_url(); + /** @noinspection PhpLanguageLevelInspection */ + // Torden GET is guarded to detect 5.5+ in admin page and inform of incompatibility + $find = [ 'http://', 'https://' ]; + $replace = ''; + $host = str_replace( $find, $replace, $site); + + if ( is_ssl() ) { + $purgeurl = $site . '/purgeall' ; + $curl = curl_init( $purgeurl ); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PURGE" ); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + /** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */ + // Torden GET is guarded to detect 5.5+ in admin page and inform of incompatibility + curl_setopt($curl, CURLOPT_RESOLVE, array($host . ":443:127.0.0.1" )); + + } else { + + $curl = curl_init( "http://127.0.0.1/purgeall" ); + curl_setopt($curl, CURLOPT_HTTPHEADER, array('Host:' . $host )); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PURGE" ); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); + + } + + $response = curl_exec($curl); + if ($response === false) + $this->log( curl_errno($curl) .': '. curl_error($curl) ); + curl_close($curl); + break; + + case 'get_request': + // Go to default case. + + default: + $this->unlink_recursive( RT_WP_NGINX_HELPER_CACHE_PATH, false ); + break; + } + + $log_purge_method = '* Using: ' . $purge_method; + $this->log( '* * * * *' ); $this->log( '* Purged Everything!' ); + $this->log( $log_purge_method ); $this->log( '* * * * *' ); /** diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index f91289a7..39da0b90 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -255,29 +255,42 @@ public function nginx_helper_setting_page() { public function nginx_helper_default_settings() { return array( - 'enable_purge' => 0, - 'cache_method' => 'enable_fastcgi', - 'purge_method' => 'get_request', - 'enable_map' => 0, - 'enable_log' => 0, - 'log_level' => 'INFO', - 'log_filesize' => '5', - 'enable_stamp' => 0, - 'purge_homepage_on_edit' => 1, - 'purge_homepage_on_del' => 1, - 'purge_archive_on_edit' => 1, - 'purge_archive_on_del' => 1, - 'purge_archive_on_new_comment' => 0, - 'purge_archive_on_deleted_comment' => 0, - 'purge_page_on_mod' => 1, - 'purge_page_on_new_comment' => 1, - 'purge_page_on_deleted_comment' => 1, - 'purge_feeds' => 1, - 'redis_hostname' => '127.0.0.1', - 'redis_port' => '6379', - 'redis_prefix' => 'nginx-cache:', - 'purge_url' => '', - 'redis_enabled_by_constant' => 0, + 'enable_purge' => 0, + 'cache_method' => 'enable_fastcgi', + 'purge_method' => 'get_request', + 'enable_map' => 0, + 'enable_log' => 0, + 'log_level' => 'INFO', + 'log_filesize' => '5', + 'enable_stamp' => 0, + 'purge_homepage_on_edit' => 1, + 'purge_homepage_on_del' => 1, + 'purge_archive_on_edit' => 1, + 'purge_archive_on_del' => 1, + 'purge_archive_on_new_comment' => 0, + 'purge_archive_on_deleted_comment' => 0, + 'purge_page_on_mod' => 1, + 'purge_page_on_new_comment' => 1, + 'purge_page_on_deleted_comment' => 1, + 'purge_feeds' => 1, + 'redis_hostname' => '127.0.0.1', + 'redis_port' => '6379', + 'redis_prefix' => 'nginx-cache:', + 'redis_unix_socket' => '', + 'redis_username' => '', + 'redis_password' => '', + 'redis_database' => 0, + 'purge_url' => '', + 'auth_enabled_by_constant' => 0, + 'cache_method_set_by_constant' => 0, + 'purge_method_set_by_constant' => 0, + 'redis_hostname_set_by_constant' => 0, + 'redis_port_set_by_constant' => 0, + 'redis_unix_socket_set_by_constant' => 0, + 'redis_prefix_set_by_constant' => 0, + 'redis_database_set_by_constant' => 0, + 'redis_username_set_by_constant' => 0, + 'redis_password_socket_set_by_constant' => 0, ); } @@ -303,22 +316,78 @@ public function nginx_helper_settings() { $this->nginx_helper_default_settings() ); - $is_redis_enabled = ( - defined( 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME' ) && - defined( 'RT_WP_NGINX_HELPER_REDIS_PORT' ) && - defined( 'RT_WP_NGINX_HELPER_REDIS_PREFIX' ) - ); + if ( defined( 'RT_WP_NGINX_HELPER_PURGE_METHOD' ) ) { + $data['purge_method'] = RT_WP_NGINX_HELPER_PURGE_METHOD; + $data['purge_method_set_by_constant'] = 1; + } + + if ( defined( 'RT_WP_NGINX_HELPER_CACHE_METHOD' ) ) { + $data['cache_method'] = RT_WP_NGINX_HELPER_CACHE_METHOD; + $data['cache_method_set_by_constant'] = 1; + + if ( 'enable_fastcgi' === RT_WP_NGINX_HELPER_CACHE_METHOD ) { + return $data; + } + } + + $redis_tcp_connection_enabled_by_constants = false; + $redis_unix_socket_enabled_by_constants = false; + $redis_prefix = false; + $redis_hostname = false; + $redis_port = false; + $redis_unix_socket = false; + + if ( defined( 'RT_WP_NGINX_HELPER_REDIS_PREFIX' ) ) { + $redis_prefix = RT_WP_NGINX_HELPER_REDIS_PREFIX; + $data['redis_prefix'] = $redis_prefix; + $data['redis_prefix_set_by_constant'] = 1; + } + + if ( defined( 'RT_WP_NGINX_HELPER_REDIS_PORT' ) ) { + $redis_port = RT_WP_NGINX_HELPER_REDIS_PORT; + $data['redis_port'] = $redis_port; + $data['redis_port_set_by_constant'] = 1; + } + + if ( defined( 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME' ) ) { + $redis_hostname = RT_WP_NGINX_HELPER_REDIS_HOSTNAME; + $data['redis_hostname'] = $redis_hostname; + $data['redis_hostname_set_by_constant'] = 1; + } + + if ( defined( 'RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET' ) ) { + $redis_unix_socket = RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET; + $data['redis_unix_socket'] = $redis_unix_socket; + $data['redis_unix_socket_set_by_constant'] = 1; + } + + if ( defined( 'RT_WP_NGINX_HELPER_REDIS_DATABASE' ) ) { + $data['redis_database'] = RT_WP_NGINX_HELPER_REDIS_DATABASE; + $data['redis_database_set_by_constant'] = 1; + } + + if ( defined( 'RT_WP_NGINX_HELPER_REDIS_USERNAME' ) ) { + $data['redis_username'] = RT_WP_NGINX_HELPER_REDIS_USERNAME; + $data['redis_username_set_by_constant'] = 1; + } + + if ( defined( 'RT_WP_NGINX_HELPER_REDIS_PASSWORD' ) ) { + $data['redis_password'] = RT_WP_NGINX_HELPER_REDIS_PASSWORD; + $data['redis_password_set_by_constant'] = 1; + } - if ( ! $is_redis_enabled ) { - return $data; + if ( $redis_prefix && $redis_hostname && $redis_port ) { + $redis_tcp_connection_enabled_by_constants = true; } - $data['redis_enabled_by_constant'] = $is_redis_enabled; - $data['enable_purge'] = $is_redis_enabled; - $data['cache_method'] = 'enable_redis'; - $data['redis_hostname'] = RT_WP_NGINX_HELPER_REDIS_HOSTNAME; - $data['redis_port'] = RT_WP_NGINX_HELPER_REDIS_PORT; - $data['redis_prefix'] = RT_WP_NGINX_HELPER_REDIS_PREFIX; + if ( $redis_prefix && $redis_unix_socket ) { + $redis_unix_socket_enabled_by_constants = true; + } + + if ( $redis_tcp_connection_enabled_by_constants || $redis_unix_socket_enabled_by_constants ) { + $data['enable_purge'] = 1; + $data['cache_method'] = 'enable_redis'; + } return $data; diff --git a/admin/class-phpredis-purger.php b/admin/class-phpredis-purger.php index 10a84831..b297d6a3 100644 --- a/admin/class-phpredis-purger.php +++ b/admin/class-phpredis-purger.php @@ -39,12 +39,41 @@ public function __construct() { try { $this->redis_object = new Redis(); + + /*Composer sets default to version that doesn't allow modern php*/ + $redis_connection_others_array = array(); + + $path = $nginx_helper_admin->options['redis_unix_socket']; + + if ( $path ) { + $host = $path; + $port = 0; + } else { + $host = $nginx_helper_admin->options['redis_hostname']; + $port = $nginx_helper_admin->options['redis_port']; + } + + $username = $nginx_helper_admin->options['redis_username']; + $password = $nginx_helper_admin->options['redis_password']; + + if ( $username && $password ) { + $redis_connection_others_array['auth'] = [$username, $password]; + } + $this->redis_object->connect( - $nginx_helper_admin->options['redis_hostname'], - $nginx_helper_admin->options['redis_port'], - 5 + $host, + $port, + 5, + '', + 100, + 1.5, + $redis_connection_others_array ); + $redis_database = $nginx_helper_admin->options['redis_database']; + + $this->redis_object->select($redis_database); + } catch ( Exception $e ) { $this->log( $e->getMessage(), 'ERROR' ); } diff --git a/admin/class-predis-purger.php b/admin/class-predis-purger.php index 1290a0de..1f4771f2 100644 --- a/admin/class-predis-purger.php +++ b/admin/class-predis-purger.php @@ -38,13 +38,28 @@ public function __construct() { Predis\Autoloader::register(); + /*Composer sets default to version that doesn't allow modern php*/ + $predis_connection_array = array(); + + $path = $nginx_helper_admin->options['redis_unix_socket']; + $username = $nginx_helper_admin->options['redis_username']; + $password = $nginx_helper_admin->options['redis_password']; + $predis_connection_array['database'] = $nginx_helper_admin->options['redis_database']; + + if ( $path ) { + $predis_connection_array['path'] = $path; + } else { + $predis_connection_array['host'] = $nginx_helper_admin->options['redis_hostname'];; + $predis_connection_array['port'] = $nginx_helper_admin->options['redis_port']; + } + + if ( $username && $password ) { + $predis_connection_array['username'] = $username; + $predis_connection_array['password'] = $password; + } + // redis server parameter. - $this->redis_object = new Predis\Client( - array( - 'host' => $nginx_helper_admin->options['redis_hostname'], - 'port' => $nginx_helper_admin->options['redis_port'], - ) - ); + $this->redis_object = new Predis\Client( $predis_connection_array ); try { $this->redis_object->connect(); diff --git a/admin/partials/nginx-helper-general-options.php b/admin/partials/nginx-helper-general-options.php index a91eb53f..33be2dad 100644 --- a/admin/partials/nginx-helper-general-options.php +++ b/admin/partials/nginx-helper-general-options.php @@ -22,6 +22,10 @@ 'redis_hostname', 'redis_port', 'redis_prefix', + 'redis_unix_socket', + 'redis_username', + 'redis_password', + 'redis_database', 'purge_homepage_on_edit', 'purge_homepage_on_del', 'purge_url', @@ -93,9 +97,61 @@ } -$nginx_helper_settings = $nginx_helper_admin->nginx_helper_settings(); -$log_path = $nginx_helper_admin->functional_asset_path(); -$log_url = $nginx_helper_admin->functional_asset_url(); +$php_version = phpversion(); +$nginx_helper_settings = $nginx_helper_admin->nginx_helper_settings(); +$log_path = $nginx_helper_admin->functional_asset_path(); +$log_url = $nginx_helper_admin->functional_asset_url(); +$cache_method = $nginx_helper_settings['cache_method']; + +$purge_method_constant_warning = false; +$get_purge_method_radio_disabled = false; +$torden_get_purge_method_radio_disabled = false; +$unlink_files_purge_method_radio_disabled = false; +$purge_method_php_version_unsupported = false; +$redis_hostname_set_by_constant = false; +$redis_unix_socket_set_by_constant = false; +$redis_port_set_by_constant = false; +$redis_prefix_set_by_constant = false; +$redis_database_set_by_constant = false; +$redis_username_set_by_constant = false; +$redis_password_set_by_constant = false; + +// For testing +//$php_version = 5.4; + +if ( 'enable_fastcgi' === $cache_method ) { + $purge_method = $nginx_helper_settings['purge_method']; + $cache_method_set_by_constant = $nginx_helper_settings['cache_method_set_by_constant']; + $purge_method_set_by_constant = $nginx_helper_settings['purge_method_set_by_constant']; + + if (version_compare($php_version, '5.5', '<') && $purge_method === 'get_request_torden' ) { + $purge_method_php_version_unsupported = true; + } + + if ( ! $purge_method_set_by_constant ) { + if ( $purge_method_php_version_unsupported ) { + $torden_get_purge_method_radio_disabled = true; + $purge_method = 'get_request'; + } + } else { + $get_purge_method_radio_disabled = true; + $torden_get_purge_method_radio_disabled = true; + $unlink_files_purge_method_radio_disabled = true; + if ( $purge_method_php_version_unsupported ) { + $purge_method_constant_warning = true; + } + } +} + +if ( 'enable_redis' === $cache_method ) { + $redis_hostname_set_by_constant = $nginx_helper_settings['redis_hostname_set_by_constant']; + $redis_port_set_by_constant = $nginx_helper_settings['redis_port_set_by_constant']; + $redis_unix_socket_set_by_constant = $nginx_helper_settings['redis_unix_socket_set_by_constant']; + $redis_prefix_set_by_constant = $nginx_helper_settings['redis_prefix_set_by_constant']; + $redis_database_set_by_constant = $nginx_helper_settings['redis_database_set_by_constant']; + $redis_username_set_by_constant = $nginx_helper_settings['redis_username_set_by_constant']; + $redis_password_set_by_constant = $nginx_helper_settings['redis_password_set_by_constant']; +} /** * Get setting url for single multiple with subdomain OR multiple with subdirectory site. @@ -136,16 +192,30 @@
+ '; + echo ''; + esc_html_e( + sprintf( + __("Set by wp-config.php constant: define( 'RT_WP_NGINX_HELPER_CACHE_METHOD', '%s' );", 'nginx-helper'), + $cache_method + ) + ); + echo ''; + echo '

'; + } + ?>
- /> + />
- /> + /> @@ -166,11 +236,30 @@
-
> +
>

+ '; + esc_html_e( + sprintf( + __("Set by wp-config.php constant: define( 'RT_WP_NGINX_HELPER_PURGE_METHOD', '%s' );", 'nginx-helper'), + $purge_method + ) + ); + if ( $purge_method_constant_warning ) { + echo '

'; + echo wp_kses( + __( 'WARNING!! The running version of PHP does not support this method!!!', 'nginx-helper' ), + array( 'strong' => array() ) + ); + } + echo '

'; + } + ?>
@@ -182,14 +271,14 @@
+ +