| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Post Site Handler. |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class MainWP_Post_Site_Handler |
| 12 |
* |
| 13 |
* @package MainWP\Dashboard |
| 14 |
*/ |
| 15 |
class MainWP_Post_Site_Handler extends MainWP_Post_Base_Handler { |
| 16 |
|
| 17 |
/** |
| 18 |
* Private static variable to hold the single instance of the class. |
| 19 |
* |
| 20 |
* @static |
| 21 |
* |
| 22 |
* @var mixed Default null |
| 23 |
*/ |
| 24 |
private static $instance = null; |
| 25 |
|
| 26 |
/** |
| 27 |
* Method instance() |
| 28 |
* |
| 29 |
* Create MainWP Post Site Handler instance. |
| 30 |
* |
| 31 |
* @static |
| 32 |
* @return self $instance MainWP_Post_Site_Handler. |
| 33 |
*/ |
| 34 |
public static function instance() { |
| 35 |
if ( null == self::$instance ) { |
| 36 |
self::$instance = new self(); |
| 37 |
} |
| 38 |
return self::$instance; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Init site actions |
| 43 |
*/ |
| 44 |
public function init() { |
| 45 |
// Page: ManageSites. |
| 46 |
$this->add_action( 'mainwp_checkwp', array( &$this, 'mainwp_checkwp' ) ); |
| 47 |
$this->add_action( 'mainwp_addwp', array( &$this, 'mainwp_addwp' ) ); |
| 48 |
$this->add_action( 'mainwp_get_site_icon', array( &$this, 'get_site_icon' ) ); |
| 49 |
|
| 50 |
if ( mainwp_current_user_have_right( 'dashboard', 'test_connection' ) ) { |
| 51 |
$this->add_action( 'mainwp_testwp', array( &$this, 'mainwp_testwp' ) ); |
| 52 |
} |
| 53 |
|
| 54 |
$this->add_action( 'mainwp_removesite', array( &$this, 'mainwp_removesite' ) ); |
| 55 |
$this->add_action( 'mainwp_reconnectwp', array( &$this, 'mainwp_reconnectwp' ) ); |
| 56 |
$this->add_action( 'mainwp_updatechildsite_value', array( &$this, 'mainwp_updatechildsite_value' ) ); |
| 57 |
|
| 58 |
// Page: ManageGroups. |
| 59 |
$this->add_action( 'mainwp_group_rename', array( &$this, 'mainwp_group_rename' ) ); |
| 60 |
$this->add_action( 'mainwp_group_delete', array( &$this, 'mainwp_group_delete' ) ); |
| 61 |
$this->add_action( 'mainwp_group_add', array( &$this, 'mainwp_group_add' ) ); |
| 62 |
$this->add_action( 'mainwp_group_getsites', array( &$this, 'mainwp_group_getsites' ) ); |
| 63 |
$this->add_action( 'mainwp_group_updategroup', array( &$this, 'mainwp_group_updategroup' ) ); |
| 64 |
|
| 65 |
// Widget: RightNow. |
| 66 |
$this->add_action( 'mainwp_syncsites', array( &$this, 'mainwp_syncsites' ) ); |
| 67 |
|
| 68 |
$this->add_action( 'mainwp_checksites', array( &$this, 'mainwp_checksites' ) ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Method mainwp_group_rename() |
| 73 |
* |
| 74 |
* Rename Group. |
| 75 |
*/ |
| 76 |
public function mainwp_group_rename() { |
| 77 |
$this->secure_request( 'mainwp_group_rename' ); |
| 78 |
|
| 79 |
MainWP_Manage_Groups::rename_group(); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Method mainwp_group_delete() |
| 84 |
* |
| 85 |
* Delete Group. |
| 86 |
*/ |
| 87 |
public function mainwp_group_delete() { |
| 88 |
$this->secure_request( 'mainwp_group_delete' ); |
| 89 |
|
| 90 |
MainWP_Manage_Groups::delete_group(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Method mainwp_group_add() |
| 95 |
* |
| 96 |
* Add Group. |
| 97 |
*/ |
| 98 |
public function mainwp_group_add() { |
| 99 |
$this->secure_request( 'mainwp_group_add' ); |
| 100 |
|
| 101 |
MainWP_Manage_Groups::add_group(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Method mainwp_group_getsites() |
| 106 |
* |
| 107 |
* Get Child Sites in group. |
| 108 |
*/ |
| 109 |
public function mainwp_group_getsites() { |
| 110 |
$this->secure_request( 'mainwp_group_getsites' ); |
| 111 |
|
| 112 |
die( MainWP_Manage_Groups::get_sites() ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Method mainwp_group_updategroup() |
| 117 |
* |
| 118 |
* Update Group. |
| 119 |
*/ |
| 120 |
public function mainwp_group_updategroup() { |
| 121 |
$this->secure_request( 'mainwp_group_updategroup' ); |
| 122 |
|
| 123 |
MainWP_Manage_Groups::update_group(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Method mainwp_checkwp() |
| 128 |
* |
| 129 |
* Check if WP can be added. |
| 130 |
*/ |
| 131 |
public function mainwp_checkwp() { |
| 132 |
if ( $this->check_security( 'mainwp_checkwp', 'security' ) ) { |
| 133 |
MainWP_Manage_Sites_Handler::check_site(); |
| 134 |
} else { |
| 135 |
die( wp_json_encode( array( 'response' => __( 'ERROR: Invalid request!', 'mainwp' ) ) ) ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Method mainwp_addwp() |
| 141 |
* |
| 142 |
* Add WP to the database. |
| 143 |
*/ |
| 144 |
public function mainwp_addwp() { |
| 145 |
if ( $this->check_security( 'mainwp_addwp', 'security' ) ) { |
| 146 |
MainWP_Manage_Sites_Handler::add_site(); |
| 147 |
} else { |
| 148 |
die( wp_json_encode( array( 'response' => __( 'ERROR: Invalid request!', 'mainwp' ) ) ) ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Method get_site_icon() |
| 154 |
* |
| 155 |
* Get Child Site Favicon. |
| 156 |
*/ |
| 157 |
public function get_site_icon() { |
| 158 |
if ( $this->check_security( 'mainwp_get_site_icon', 'security' ) ) { |
| 159 |
$siteId = null; |
| 160 |
if ( isset( $_POST['siteId'] ) ) { |
| 161 |
$siteId = intval( $_POST['siteId'] ); |
| 162 |
} |
| 163 |
$result = MainWP_Sync::sync_site_icon( $siteId ); |
| 164 |
wp_send_json( $result ); |
| 165 |
} else { |
| 166 |
die( wp_json_encode( array( 'error' => __( 'ERROR: Invalid request!', 'mainwp' ) ) ) ); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Method mainwp_testwp() |
| 172 |
* |
| 173 |
* Test if Child Site can be reached. |
| 174 |
*/ |
| 175 |
public function mainwp_testwp() { |
| 176 |
$this->secure_request( 'mainwp_testwp' ); |
| 177 |
|
| 178 |
$url = null; |
| 179 |
$name = null; |
| 180 |
$http_user = null; |
| 181 |
$http_pass = null; |
| 182 |
$verifyCertificate = 1; |
| 183 |
$sslVersion = 0; |
| 184 |
|
| 185 |
if ( isset( $_POST['url'] ) ) { |
| 186 |
$url = sanitize_text_field( wp_unslash( $_POST['url'] ) ); |
| 187 |
|
| 188 |
$temp_url = MainWP_Utility::remove_http_prefix( $url, true ); |
| 189 |
|
| 190 |
if ( strpos( $temp_url, ':' ) ) { |
| 191 |
die( wp_json_encode( array( 'error' => __( 'Invalid URL.', 'mainwp' ) ) ) ); |
| 192 |
} |
| 193 |
|
| 194 |
$verifyCertificate = isset( $_POST['test_verify_cert'] ) ? sanitize_text_field( wp_unslash( $_POST['test_verify_cert'] ) ) : false; |
| 195 |
$forceUseIPv4 = isset( $_POST['test_force_use_ipv4'] ) ? sanitize_text_field( wp_unslash( $_POST['test_force_use_ipv4'] ) ) : false; |
| 196 |
$sslVersion = isset( $_POST['test_ssl_version'] ) ? sanitize_text_field( wp_unslash( $_POST['test_ssl_version'] ) ) : false; |
| 197 |
$http_user = isset( $_POST['http_user'] ) ? sanitize_text_field( wp_unslash( $_POST['http_user'] ) ) : ''; |
| 198 |
$http_pass = isset( $_POST['http_pass'] ) ? wp_unslash( $_POST['http_pass'] ) : ''; |
| 199 |
|
| 200 |
} elseif ( isset( $_POST['siteid'] ) ) { |
| 201 |
$website = MainWP_DB::instance()->get_website_by_id( intval( $_POST['siteid'] ) ); |
| 202 |
if ( $website ) { |
| 203 |
$url = $website->url; |
| 204 |
$name = $website->name; |
| 205 |
$verifyCertificate = $website->verify_certificate; |
| 206 |
$forceUseIPv4 = $website->force_use_ipv4; |
| 207 |
$sslVersion = $website->ssl_version; |
| 208 |
$http_user = $website->http_user; |
| 209 |
$http_pass = $website->http_pass; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
$rslt = MainWP_Connect::try_visit( $url, $verifyCertificate, $http_user, $http_pass, $sslVersion, $forceUseIPv4 ); |
| 214 |
|
| 215 |
if ( isset( $rslt['error'] ) && ( '' !== $rslt['error'] ) && ( 'wp-admin/' !== substr( $url, - 9 ) ) ) { |
| 216 |
if ( substr( $url, - 1 ) != '/' ) { |
| 217 |
$url .= '/'; |
| 218 |
} |
| 219 |
$url .= 'wp-admin/'; |
| 220 |
$newrslt = MainWP_Connect::try_visit( $url, $verifyCertificate, $http_user, $http_pass, $sslVersion, $forceUseIPv4 ); |
| 221 |
if ( isset( $newrslt['error'] ) && ( '' !== $rslt['error'] ) ) { |
| 222 |
$rslt = $newrslt; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if ( null != $name ) { |
| 227 |
$rslt['sitename'] = esc_html( $name ); |
| 228 |
} |
| 229 |
|
| 230 |
wp_send_json( $rslt ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Method mainwp_removesite() |
| 235 |
* |
| 236 |
* Remove a website from MainWP. |
| 237 |
*/ |
| 238 |
public function mainwp_removesite() { |
| 239 |
if ( ! mainwp_current_user_have_right( 'dashboard', 'delete_sites' ) ) { |
| 240 |
die( wp_json_encode( array( 'error' => mainwp_do_not_have_permissions( __( 'delete sites', 'mainwp' ), false ) ) ) ); |
| 241 |
} |
| 242 |
|
| 243 |
$this->secure_request( 'mainwp_removesite' ); |
| 244 |
|
| 245 |
MainWP_Manage_Sites_Handler::remove_site(); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Method mainwp_reconnectwp() |
| 250 |
* |
| 251 |
* Reconnect to Child Site. |
| 252 |
*/ |
| 253 |
public function mainwp_reconnectwp() { |
| 254 |
$this->secure_request( 'mainwp_reconnectwp' ); |
| 255 |
|
| 256 |
MainWP_Manage_Sites_Handler::reconnect_site(); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Method mainwp_updatechildsite_value() |
| 261 |
* |
| 262 |
* Update Child Site value. |
| 263 |
*/ |
| 264 |
public function mainwp_updatechildsite_value() { |
| 265 |
$this->secure_request( 'mainwp_updatechildsite_value' ); |
| 266 |
|
| 267 |
MainWP_Manage_Sites_Handler::update_child_site_value(); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Method mainwp_syncsites() |
| 272 |
* |
| 273 |
* Sync Child Sites. |
| 274 |
*/ |
| 275 |
public function mainwp_syncsites() { |
| 276 |
$this->secure_request( 'mainwp_syncsites' ); |
| 277 |
MainWP_Updates_Overview::dismiss_sync_errors( false ); |
| 278 |
MainWP_Updates_Overview::sync_site(); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Method mainwp_checksites() |
| 283 |
* |
| 284 |
* Check Child Sites. |
| 285 |
*/ |
| 286 |
public function mainwp_checksites() { |
| 287 |
$this->secure_request( 'mainwp_checksites' ); |
| 288 |
MainWP_Monitoring_Handler::ajax_check_status_site(); |
| 289 |
} |
| 290 |
|
| 291 |
} |
| 292 |
|