2015-09-22 20:51:35 +02:00
< ? php
2020-05-05 20:13:38 +07:00
namespace MainWP\Child ;
2015-09-22 20:51:35 +02:00
2015-10-15 22:52:37 +10:00
class MainWP_Child_Skeleton_Key {
2020-03-26 19:45:07 +00:00
public static $instance = null ;
2015-09-22 20:51:35 +02:00
public static $information = array ();
2020-03-26 19:45:07 +00:00
public $plugin_translate = 'mainwp-child' ;
2015-09-22 20:51:35 +02:00
2020-05-06 20:22:11 +07:00
public static function instance () {
2020-03-26 14:05:04 +00:00
if ( null === self :: $instance ) {
2020-05-07 01:03:56 +07:00
self :: $instance = new self ();
2015-09-22 20:51:35 +02:00
}
2020-03-26 14:05:04 +00:00
return self :: $instance ;
2015-09-22 20:51:35 +02:00
}
public function action () {
2020-05-07 16:27:57 +00:00
2015-09-22 20:51:35 +02:00
function mainwp_skeleton_key_handle_fatal_error () {
$error = error_get_last ();
2020-04-17 18:55:52 +02:00
if ( isset ( $error [ 'type' ] ) && in_array ( $error [ 'type' ], array ( 1 , 4 , 16 , 64 , 256 ) ) && isset ( $error [ 'message' ] ) ) {
2020-05-07 19:34:36 +07:00
mainwp_child_helper () -> write ( array ( 'error' => 'MainWP_Child fatal error : ' . $error [ 'message' ] . ' Line: ' . $error [ 'line' ] . ' File: ' . $error [ 'file' ] ) );
2015-09-22 20:51:35 +02:00
}
}
2015-10-15 22:52:37 +10:00
register_shutdown_function ( 'mainwp_skeleton_key_handle_fatal_error' );
2015-09-22 20:51:35 +02:00
switch ( $_POST [ 'action' ] ) {
case 'skeleton_key_visit_site_as_browser' :
$information = $this -> visit_site_as_browser ();
break ;
2017-08-24 20:41:12 +02:00
case 'save_settings' :
$information = $this -> save_settings ();
break ;
2015-09-22 20:51:35 +02:00
default :
$information = array ( 'error' => 'Unknown action' );
}
2015-10-15 22:52:37 +10:00
2020-05-07 19:34:36 +07:00
mainwp_child_helper () -> write ( $information );
2015-09-22 20:51:35 +02:00
exit ();
}
2020-05-20 01:07:47 +07:00
protected function visit_site_as_browser () { // phpcs:ignore -- ignore complex method notice.
2015-09-22 20:51:35 +02:00
if ( ! isset ( $_POST [ 'url' ] ) || ! is_string ( $_POST [ 'url' ] ) || strlen ( $_POST [ 'url' ] ) < 2 ) {
return array ( 'error' => 'Missing url' );
}
if ( ! isset ( $_POST [ 'args' ] ) || ! is_array ( $_POST [ 'args' ] ) ) {
return array ( 'error' => 'Missing args' );
}
$_POST = stripslashes_deep ( $_POST );
$args = $_POST [ 'args' ];
$current_user = wp_get_current_user ();
$url = '/' . $_POST [ 'url' ];
2016-12-29 22:19:20 +01:00
$expiration = time () + 600 ;
2015-09-22 20:51:35 +02:00
$manager = WP_Session_Tokens :: get_instance ( $current_user -> ID );
$token = $manager -> create ( $expiration );
2017-08-24 20:41:12 +02:00
$secure = is_ssl ();
if ( $secure ) {
$auth_cookie_name = SECURE_AUTH_COOKIE ;
2020-03-26 19:45:07 +00:00
$scheme = 'secure_auth' ;
2017-08-24 20:41:12 +02:00
} else {
$auth_cookie_name = AUTH_COOKIE ;
2020-03-26 19:45:07 +00:00
$scheme = 'auth' ;
2017-08-24 20:41:12 +02:00
}
2020-03-26 19:45:07 +00:00
$auth_cookie = wp_generate_auth_cookie ( $current_user -> ID , $expiration , $scheme , $token );
$logged_in_cookie = wp_generate_auth_cookie ( $current_user -> ID , $expiration , 'logged_in' , $token );
$_COOKIE [ $auth_cookie_name ] = $auth_cookie ;
$_COOKIE [ LOGGED_IN_COOKIE ] = $logged_in_cookie ;
$post_args = array ();
$post_args [ 'body' ] = array ();
$post_args [ 'redirection' ] = 5 ;
2020-04-17 18:55:52 +02:00
$post_args [ 'decompress' ] = false ;
2020-03-26 19:45:07 +00:00
$post_args [ 'cookies' ] = array (
2020-04-17 18:55:52 +02:00
new WP_Http_Cookie (
array (
'name' => $auth_cookie_name ,
'value' => $auth_cookie ,
)
),
new WP_Http_Cookie (
array (
'name' => LOGGED_IN_COOKIE ,
'value' => $logged_in_cookie ,
)
),
2015-09-22 20:51:35 +02:00
);
if ( isset ( $args [ 'get' ] ) ) {
$get_args = $args [ 'get' ];
parse_str ( $args [ 'get' ], $get_args );
}
if ( ! isset ( $get_args ) || ! is_array ( $get_args ) ) {
$get_args = array ();
}
$get_args [ 'skeleton_keyuse_nonce_key' ] = intval ( time () );
$get_args [ 'skeleton_keyuse_nonce_hmac' ] = hash_hmac ( 'sha256' , $get_args [ 'skeleton_keyuse_nonce_key' ], NONCE_KEY );
$good_nonce = null ;
if ( isset ( $args [ 'nonce' ] ) && ! empty ( $args [ 'nonce' ] ) ) {
parse_str ( $args [ 'nonce' ], $temp_nonce );
$good_nonce = $this -> wp_create_nonce_recursive ( $temp_nonce );
$get_args = array_merge ( $get_args , $good_nonce );
}
if ( isset ( $args [ 'post' ] ) ) {
parse_str ( $args [ 'post' ], $temp_post );
if ( ! isset ( $temp_post ) || ! is_array ( $temp_post ) ) {
$temp_post = array ();
}
if ( ! empty ( $good_nonce ) ) {
$temp_post = array_merge ( $temp_post , $good_nonce );
}
$post_args [ 'body' ] = $temp_post ;
}
2017-08-24 20:41:12 +02:00
$post_args [ 'timeout' ] = 25 ;
2015-09-22 20:51:35 +02:00
$full_url = add_query_arg ( $get_args , get_site_url () . $url );
2020-05-11 20:30:56 +07:00
add_filter ( 'http_request_args' , array ( MainWP_Helper :: get_class_name (), 'reject_unsafe_urls' ), 99 , 2 );
2018-12-19 17:01:08 +07:00
2015-09-22 20:51:35 +02:00
$response = wp_remote_post ( $full_url , $post_args );
if ( is_wp_error ( $response ) ) {
return array ( 'error' => 'wp_remote_post error: ' . $response -> get_error_message () );
}
$received_content = wp_remote_retrieve_body ( $response );
if ( preg_match ( '/<mainwp>(.*)<\/mainwp>/' , $received_content , $received_result ) > 0 ) {
2020-05-08 16:44:48 +07:00
$received_content_mainwp = json_decode ( base64_decode ( $received_result [ 1 ] ), true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
2015-09-22 20:51:35 +02:00
if ( isset ( $received_content_mainwp [ 'error' ] ) ) {
return array ( 'error' => $received_content_mainwp [ 'error' ] );
}
}
$search_ok_counter = 0 ;
$search_fail_counter = 0 ;
if ( isset ( $args [ 'search' ][ 'ok' ] ) ) {
foreach ( $args [ 'search' ][ 'ok' ] as $search ) {
if ( preg_match ( '/' . preg_quote ( $search , '/' ) . '/i' , $received_content ) ) {
++ $search_ok_counter ;
}
}
}
if ( isset ( $args [ 'search' ][ 'fail' ] ) ) {
foreach ( $args [ 'search' ][ 'fail' ] as $search ) {
if ( preg_match ( '/' . preg_quote ( $search , '/' ) . '/i' , $received_content ) ) {
++ $search_fail_counter ;
}
}
}
unset ( $get_args [ 'skeleton_keyuse_nonce_key' ] );
unset ( $get_args [ 'skeleton_keyuse_nonce_hmac' ] );
return array (
'success' => 1 ,
'content' => $received_content ,
'url' => $full_url ,
'get' => $get_args ,
'post' => $post_args [ 'body' ],
'search_ok_counter' => $search_ok_counter ,
2015-10-15 22:52:37 +10:00
'search_fail_counter' => $search_fail_counter ,
2015-09-22 20:51:35 +02:00
);
}
private function wp_create_nonce_recursive ( $array ) {
foreach ( $array as $key => $value ) {
if ( is_array ( $array [ $key ] ) ) {
$array [ $key ] = $this -> wp_create_nonce_recursive ( $array [ $key ] );
} else {
$array [ $key ] = wp_create_nonce ( $array [ $key ] );
}
}
return $array ;
}
2017-08-24 20:41:12 +02:00
public function save_settings () {
2020-04-17 18:55:52 +02:00
$settings = isset ( $_POST [ 'settings' ] ) ? $_POST [ 'settings' ] : array ();
2017-08-24 20:41:12 +02:00
2020-04-17 18:55:52 +02:00
if ( ! is_array ( $settings ) || empty ( $settings ) ) {
return array ( 'error' => __ ( 'Invalid data. Please check and try again.' , 'mainwp-child' ) );
2020-03-27 15:13:11 +00:00
}
2017-08-24 20:41:12 +02:00
$whitelist_options = array (
'general' => array ( 'blogname' , 'blogdescription' , 'gmt_offset' , 'date_format' , 'time_format' , 'start_of_week' , 'timezone_string' , 'WPLANG' ),
);
2020-03-26 17:03:00 +00:00
if ( ! is_multisite () ) {
if ( ! defined ( 'WP_SITEURL' ) ) {
2017-08-24 20:41:12 +02:00
$whitelist_options [ 'general' ][] = 'siteurl' ;
2020-03-27 15:13:11 +00:00
}
2020-03-26 17:03:00 +00:00
if ( ! defined ( 'WP_HOME' ) ) {
2017-08-24 20:41:12 +02:00
$whitelist_options [ 'general' ][] = 'home' ;
2020-03-27 15:13:11 +00:00
}
2017-08-24 20:41:12 +02:00
$whitelist_options [ 'general' ][] = 'admin_email' ;
$whitelist_options [ 'general' ][] = 'users_can_register' ;
$whitelist_options [ 'general' ][] = 'default_role' ;
}
2020-03-26 15:29:54 +00:00
$whitelist_general = $whitelist_options [ 'general' ];
2017-08-24 20:41:12 +02:00
if ( ! empty ( $settings [ 'WPLANG' ] ) ) {
2020-03-26 14:11:33 +00:00
require_once ABSPATH . 'wp-admin/includes/translation-install.php' ;
2017-08-24 20:41:12 +02:00
if ( wp_can_install_language_pack () ) {
$language = wp_download_language_pack ( $settings [ 'WPLANG' ] );
if ( $language ) {
$settings [ 'WPLANG' ] = $language ;
}
}
}
$updated = false ;
2020-03-27 14:11:21 +00:00
foreach ( $settings as $option => $value ) {
2020-04-17 18:55:52 +02:00
if ( in_array ( $option , $whitelist_general ) ) {
2020-03-26 14:11:33 +00:00
if ( ! is_array ( $value ) ) {
2017-08-24 20:41:12 +02:00
$value = trim ( $value );
2020-03-27 15:13:11 +00:00
}
2017-08-24 20:41:12 +02:00
$value = wp_unslash ( $value );
2020-04-17 18:55:52 +02:00
update_option ( $option , $value );
2017-08-24 20:41:12 +02:00
$updated = true ;
}
}
2020-03-27 14:11:21 +00:00
if ( ! $updated ) {
2017-08-24 20:41:12 +02:00
return false ;
2020-03-27 15:13:11 +00:00
}
2017-08-24 20:41:12 +02:00
2020-03-26 15:29:54 +00:00
return array ( 'result' => 'ok' );
2017-08-24 20:41:12 +02:00
}
2015-10-15 22:52:37 +10:00
}