| 1 |
<?php |
| 2 |
/** |
| 3 |
* This class extends the MainWP Post Base Handler class |
| 4 |
* to add support for MainWP Extensions. |
| 5 |
* |
| 6 |
* @package MainWP/Dashboard |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace MainWP\Dashboard; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class MainWP_Post_Extension_Handler |
| 13 |
* |
| 14 |
* @package MainWP\Dashboard |
| 15 |
*/ |
| 16 |
class MainWP_Post_Extension_Handler extends MainWP_Post_Base_Handler { |
| 17 |
|
| 18 |
/** |
| 19 |
* Public static varibale to hold the instance. |
| 20 |
* |
| 21 |
* @var null Default value. |
| 22 |
*/ |
| 23 |
private static $instance = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Method instance() |
| 27 |
* |
| 28 |
* Create public static instance. |
| 29 |
* |
| 30 |
* @return self $instance. |
| 31 |
*/ |
| 32 |
public static function instance() { |
| 33 |
if ( null == self::$instance ) { |
| 34 |
self::$instance = new self(); |
| 35 |
} |
| 36 |
return self::$instance; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Init extensions actions |
| 41 |
*/ |
| 42 |
public function init() { |
| 43 |
|
| 44 |
$this->add_action( 'mainwp_extension_add_menu', array( &$this, 'add_extension_menu' ) ); |
| 45 |
$this->add_action( 'mainwp_extension_remove_menu', array( &$this, 'remove_extension_menu_from_mainwp_menu' ) ); |
| 46 |
|
| 47 |
$this->add_action( 'mainwp_extension_api_activate', array( &$this, 'activate_api_extension' ) ); |
| 48 |
$this->add_action( 'mainwp_extension_deactivate', array( &$this, 'deactivate_extension' ) ); |
| 49 |
$this->add_action( 'mainwp_extension_testextensionapilogin', array( &$this, 'test_extensions_api_login' ) ); |
| 50 |
|
| 51 |
if ( mainwp_current_user_have_right( 'dashboard', 'bulk_install_and_activate_extensions' ) ) { |
| 52 |
$this->add_action( 'mainwp_extension_grabapikey', array( &$this, 'grab_extension_api_key' ) ); |
| 53 |
$this->add_action( 'mainwp_extension_saveextensionapilogin', array( &$this, 'save_extensions_api_login' ) ); |
| 54 |
$this->add_action( 'mainwp_extension_getpurchased', array( MainWP_Extensions::get_class_name(), 'get_purchased_exts' ) ); |
| 55 |
$this->add_action( 'mainwp_extension_downloadandinstall', array( &$this, 'download_and_install' ) ); |
| 56 |
$this->add_action( 'mainwp_extension_bulk_activate', array( &$this, 'bulk_activate' ) ); |
| 57 |
$this->add_action( 'mainwp_extension_apisslverifycertificate', array( &$this, 'save_api_ssl_verify' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
// Page: ManageSites. |
| 61 |
$this->add_action( 'mainwp_ext_applypluginsettings', array( &$this, 'mainwp_ext_applypluginsettings' ) ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Apply plugin settings. |
| 66 |
* |
| 67 |
* @return mixed success|error. |
| 68 |
*/ |
| 69 |
public function mainwp_ext_applypluginsettings() { |
| 70 |
if ( $this->check_security( 'mainwp_ext_applypluginsettings', 'security' ) ) { |
| 71 |
MainWP_Manage_Sites_Handler::apply_plugin_settings(); |
| 72 |
} else { |
| 73 |
die( wp_json_encode( array( 'error' => __( 'ERROR: Invalid request!', 'mainwp' ) ) ) ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Ajax add extension menu. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function add_extension_menu() { |
| 83 |
$this->check_security( 'mainwp_extension_add_menu' ); |
| 84 |
$slug = isset( $_POST['slug'] ) ? wp_unslash( $_POST['slug'] ) : ''; |
| 85 |
MainWP_Extensions_Handler::add_extension_menu( $slug ); |
| 86 |
die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Activate MainWP Extension. |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function activate_api_extension() { |
| 95 |
$this->check_security( 'mainwp_extension_api_activate' ); |
| 96 |
MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook(); |
| 97 |
$api = isset( $_POST['slug'] ) ? dirname( $_POST['slug'] ) : ''; |
| 98 |
$api_key = isset( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; |
| 99 |
$api_email = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : ''; |
| 100 |
$result = MainWP_Api_Manager::instance()->license_key_activation( $api, $api_key, $api_email ); |
| 101 |
wp_send_json( $result ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Deactivate MainWP Extension. |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function deactivate_extension() { |
| 110 |
$this->check_security( 'mainwp_extension_deactivate' ); |
| 111 |
MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook(); |
| 112 |
$api = isset( $_POST['slug'] ) ? dirname( wp_unslash( $_POST['slug'] ) ) : ''; |
| 113 |
$result = MainWP_Api_Manager::instance()->license_key_deactivation( $api ); |
| 114 |
wp_send_json( $result ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Grab MainWP Extension API Key. |
| 119 |
* |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
public function grab_extension_api_key() { |
| 123 |
$this->check_security( 'mainwp_extension_grabapikey' ); |
| 124 |
$username = isset( $_POST['username'] ) ? sanitize_text_field( wp_unslash( $_POST['username'] ) ) : ''; |
| 125 |
$password = isset( $_POST['password'] ) ? trim( wp_unslash( $_POST['password'] ) ) : ''; |
| 126 |
$api = isset( $_POST['slug'] ) ? dirname( wp_unslash( $_POST['slug'] ) ) : ''; |
| 127 |
$result = MainWP_Api_Manager::instance()->grab_license_key( $api, $username, $password ); |
| 128 |
wp_send_json( $result ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Save MainWP Extensions API Login details for future logins. |
| 133 |
* |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
public function save_extensions_api_login() { |
| 137 |
$this->check_security( 'mainwp_extension_saveextensionapilogin' ); |
| 138 |
$api_login_history = isset( $_SESSION['api_login_history'] ) ? $_SESSION['api_login_history'] : array(); |
| 139 |
|
| 140 |
$new_api_login_history = array(); |
| 141 |
$requests = 0; |
| 142 |
|
| 143 |
foreach ( $api_login_history as $api_login ) { |
| 144 |
if ( $api_login['time'] > ( time() - 1 * 60 ) ) { |
| 145 |
$new_api_login_history[] = $api_login; |
| 146 |
$requests++; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if ( 4 < $requests ) { |
| 151 |
$_SESSION['api_login_history'] = $new_api_login_history; |
| 152 |
die( wp_json_encode( array( 'error' => __( 'Too many requests', 'mainwp' ) ) ) ); |
| 153 |
} else { |
| 154 |
$new_api_login_history[] = array( 'time' => time() ); |
| 155 |
$_SESSION['api_login_history'] = $new_api_login_history; |
| 156 |
} |
| 157 |
|
| 158 |
$username = isset( $_POST['username'] ) ? sanitize_text_field( wp_unslash( $_POST['username'] ) ) : ''; |
| 159 |
$password = isset( $_POST['password'] ) ? trim( $_POST['password'] ) : ''; |
| 160 |
if ( ( '' == $username ) && ( '' == $password ) ) { |
| 161 |
MainWP_Utility::update_option( 'mainwp_extensions_api_username', $username ); |
| 162 |
MainWP_Utility::update_option( 'mainwp_extensions_api_password', $password ); |
| 163 |
die( wp_json_encode( array( 'saved' => 1 ) ) ); |
| 164 |
} |
| 165 |
$result = array(); |
| 166 |
try { |
| 167 |
$test = MainWP_Api_Manager::instance()->test_login_api( $username, $password ); |
| 168 |
} catch ( \Exception $e ) { |
| 169 |
$return['error'] = $e->getMessage(); |
| 170 |
die( wp_json_encode( $return ) ); |
| 171 |
} |
| 172 |
|
| 173 |
if ( is_array( $test ) && isset( $test['retry_action'] ) ) { |
| 174 |
wp_send_json( $test ); |
| 175 |
} |
| 176 |
|
| 177 |
$result = json_decode( $test, true ); |
| 178 |
$save_login = ( isset( $_POST['saveLogin'] ) && ( 1 == $_POST['saveLogin'] ) ) ? true : false; |
| 179 |
$return = array(); |
| 180 |
if ( is_array( $result ) ) { |
| 181 |
if ( isset( $result['success'] ) && $result['success'] ) { |
| 182 |
if ( $save_login ) { |
| 183 |
$enscrypt_u = MainWP_Api_Manager_Password_Management::encrypt_string( $username ); |
| 184 |
$enscrypt_p = MainWP_Api_Manager_Password_Management::encrypt_string( $password ); |
| 185 |
MainWP_Utility::update_option( 'mainwp_extensions_api_username', $enscrypt_u ); |
| 186 |
MainWP_Utility::update_option( 'mainwp_extensions_api_password', $enscrypt_p ); |
| 187 |
MainWP_Utility::update_option( 'mainwp_extensions_api_save_login', true ); |
| 188 |
} |
| 189 |
$return['result'] = 'SUCCESS'; |
| 190 |
} elseif ( isset( $result['error'] ) ) { |
| 191 |
$return['error'] = $result['error']; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if ( ! $save_login ) { |
| 196 |
MainWP_Utility::update_option( 'mainwp_extensions_api_username', '' ); |
| 197 |
MainWP_Utility::update_option( 'mainwp_extensions_api_password', '' ); |
| 198 |
MainWP_Utility::update_option( 'mainwp_extensions_api_save_login', '' ); |
| 199 |
} |
| 200 |
|
| 201 |
die( wp_json_encode( $return ) ); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Save whenther or not to verify MainWP API SSL certificate. |
| 206 |
* |
| 207 |
* @return void |
| 208 |
*/ |
| 209 |
public function save_api_ssl_verify() { |
| 210 |
$this->check_security( 'mainwp_extension_apisslverifycertificate' ); |
| 211 |
MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', isset( $_POST['api_sslverify'] ) ? intval( $_POST['api_sslverify'] ) : 0 ); |
| 212 |
die( wp_json_encode( array( 'saved' => 1 ) ) ); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Test Extension page MainWP.com login details. |
| 217 |
* |
| 218 |
* @return void |
| 219 |
*/ |
| 220 |
public function test_extensions_api_login() { |
| 221 |
$this->check_security( 'mainwp_extension_testextensionapilogin' ); |
| 222 |
$enscrypt_u = get_option( 'mainwp_extensions_api_username' ); |
| 223 |
$enscrypt_p = get_option( 'mainwp_extensions_api_password' ); |
| 224 |
$username = ! empty( $enscrypt_u ) ? MainWP_Api_Manager_Password_Management::decrypt_string( $enscrypt_u ) : ''; |
| 225 |
$password = ! empty( $enscrypt_p ) ? MainWP_Api_Manager_Password_Management::decrypt_string( $enscrypt_p ) : ''; |
| 226 |
|
| 227 |
if ( ( '' === $username ) || ( '' === $password ) ) { |
| 228 |
die( wp_json_encode( array( 'error' => __( 'Login Invalid.', 'mainwp' ) ) ) ); |
| 229 |
} |
| 230 |
|
| 231 |
$result = array(); |
| 232 |
try { |
| 233 |
$test = MainWP_Api_Manager::instance()->test_login_api( $username, $password ); |
| 234 |
} catch ( \Exception $e ) { |
| 235 |
$return['error'] = $e->getMessage(); |
| 236 |
die( wp_json_encode( $return ) ); |
| 237 |
} |
| 238 |
|
| 239 |
if ( is_array( $test ) && isset( $test['retry_action'] ) ) { |
| 240 |
wp_send_json( $test ); |
| 241 |
} |
| 242 |
|
| 243 |
$result = json_decode( $test, true ); |
| 244 |
$return = array(); |
| 245 |
if ( is_array( $result ) ) { |
| 246 |
if ( isset( $result['success'] ) && $result['success'] ) { |
| 247 |
$return['result'] = 'SUCCESS'; |
| 248 |
} elseif ( isset( $result['error'] ) ) { |
| 249 |
$return['error'] = $result['error']; |
| 250 |
} |
| 251 |
} else { |
| 252 |
$apisslverify = get_option( 'mainwp_api_sslVerifyCertificate' ); |
| 253 |
if ( 1 == $apisslverify ) { |
| 254 |
MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', 0 ); |
| 255 |
$return['retry_action'] = 1; |
| 256 |
} |
| 257 |
} |
| 258 |
wp_send_json( $return ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Download & Install MainWP Extension. |
| 263 |
* |
| 264 |
* @return void |
| 265 |
*/ |
| 266 |
public function download_and_install() { |
| 267 |
$this->check_security( 'mainwp_extension_downloadandinstall' ); |
| 268 |
// phpcs:ignore -- custom setting to install plugin. |
| 269 |
ini_set( 'zlib.output_compression', 'Off' ); |
| 270 |
$download_link = isset( $_POST['download_link'] ) ? wp_unslash( $_POST['download_link'] ) : ''; |
| 271 |
|
| 272 |
$return = MainWP_Extensions_Handler::install_plugin( $download_link ); |
| 273 |
|
| 274 |
die( '<mainwp>' . wp_json_encode( $return ) . '</mainwp>' ); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* MainWP Extension Bulck Activation. |
| 279 |
* |
| 280 |
* @return void |
| 281 |
*/ |
| 282 |
public function bulk_activate() { |
| 283 |
$this->check_security( 'mainwp_extension_bulk_activate' ); |
| 284 |
$plugins = isset( $_POST['plugins'] ) ? wp_unslash( $_POST['plugins'] ) : false; |
| 285 |
if ( is_array( $plugins ) && 0 < count( $plugins ) ) { |
| 286 |
if ( current_user_can( 'activate_plugins' ) ) { |
| 287 |
activate_plugins( $plugins ); |
| 288 |
die( 'SUCCESS' ); |
| 289 |
} |
| 290 |
} |
| 291 |
die( 'FAILED' ); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Remove Extensions menu from MainWP Menu. |
| 296 |
* |
| 297 |
* @return void |
| 298 |
*/ |
| 299 |
public function remove_extension_menu_from_mainwp_menu() { |
| 300 |
$this->check_security( 'mainwp_extension_remove_menu' ); |
| 301 |
$snMenuExtensions = get_option( 'mainwp_extmenu' ); |
| 302 |
if ( ! is_array( $snMenuExtensions ) ) { |
| 303 |
$snMenuExtensions = array(); |
| 304 |
} |
| 305 |
|
| 306 |
$key = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : ''; |
| 307 |
|
| 308 |
if ( ! empty( $key ) && isset( $snMenuExtensions[ $key ] ) ) { |
| 309 |
unset( $snMenuExtensions[ $key ] ); |
| 310 |
MainWP_Utility::update_option( 'mainwp_extmenu', $snMenuExtensions ); |
| 311 |
do_action( 'mainwp_removed_extension_menu', $key ); |
| 312 |
die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) ); |
| 313 |
} |
| 314 |
|
| 315 |
die( - 1 ); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
} |
| 320 |
|