| 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 |
* @uses \MainWP\Dashboard\MainWP_Post_Base_Handler |
| 16 |
*/ |
| 17 |
class MainWP_Post_Site_Handler extends MainWP_Post_Base_Handler { |
| 18 |
|
| 19 |
/** |
| 20 |
* Private static variable to hold the single instance of the class. |
| 21 |
* |
| 22 |
* @static |
| 23 |
* |
| 24 |
* @var mixed Default null |
| 25 |
*/ |
| 26 |
private static $instance = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* Method instance() |
| 30 |
* |
| 31 |
* Create MainWP Post Site Handler instance. |
| 32 |
* |
| 33 |
* @static |
| 34 |
* @return self $instance MainWP_Post_Site_Handler. |
| 35 |
*/ |
| 36 |
public static function instance() { |
| 37 |
if ( null == self::$instance ) { |
| 38 |
self::$instance = new self(); |
| 39 |
} |
| 40 |
return self::$instance; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Init site actions |
| 45 |
*/ |
| 46 |
public function init() { |
| 47 |
// Page: ManageSites. |
| 48 |
$this->add_action( 'mainwp_checkwp', array( &$this, 'mainwp_checkwp' ) ); |
| 49 |
$this->add_action( 'mainwp_addwp', array( &$this, 'mainwp_addwp' ) ); |
| 50 |
$this->add_action( 'mainwp_get_site_icon', array( &$this, 'get_site_icon' ) ); |
| 51 |
$this->add_action( 'mainwp_check_abandoned', array( &$this, 'check_abandoned' ) ); |
| 52 |
|
| 53 |
if ( mainwp_current_user_have_right( 'dashboard', 'test_connection' ) ) { |
| 54 |
$this->add_action( 'mainwp_testwp', array( &$this, 'mainwp_testwp' ) ); |
| 55 |
} |
| 56 |
|
| 57 |
$this->add_action( 'mainwp_removesite', array( &$this, 'mainwp_removesite' ) ); |
| 58 |
$this->add_action( 'mainwp_reconnectwp', array( &$this, 'mainwp_reconnectwp' ) ); |
| 59 |
$this->add_action( 'mainwp_updatechildsite_value', array( &$this, 'mainwp_updatechildsite_value' ) ); |
| 60 |
|
| 61 |
// Page: ManageGroups. |
| 62 |
$this->add_action( 'mainwp_group_rename', array( &$this, 'mainwp_group_rename' ) ); |
| 63 |
$this->add_action( 'mainwp_group_delete', array( &$this, 'mainwp_group_delete' ) ); |
| 64 |
$this->add_action( 'mainwp_group_add', array( &$this, 'mainwp_group_add' ) ); |
| 65 |
$this->add_action( 'mainwp_group_getsites', array( &$this, 'mainwp_group_getsites' ) ); |
| 66 |
$this->add_action( 'mainwp_group_updategroup', array( &$this, 'mainwp_group_updategroup' ) ); |
| 67 |
|
| 68 |
// Widget: RightNow. |
| 69 |
$this->add_action( 'mainwp_syncsites', array( &$this, 'mainwp_syncsites' ) ); |
| 70 |
|
| 71 |
$this->add_action( 'mainwp_checksites', array( &$this, 'mainwp_checksites' ) ); |
| 72 |
$this->add_action( 'mainwp_manage_sites_suspend_site', array( &$this, 'manage_suspend_site' ) ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Method mainwp_group_rename() |
| 77 |
* |
| 78 |
* Rename Group. |
| 79 |
* |
| 80 |
* @uses \MainWP\Dashboard\MainWP_Manage_Groups::rename_group() |
| 81 |
*/ |
| 82 |
public function mainwp_group_rename() { |
| 83 |
$this->secure_request( 'mainwp_group_rename' ); |
| 84 |
|
| 85 |
MainWP_Manage_Groups::rename_group(); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Method mainwp_group_delete() |
| 90 |
* |
| 91 |
* Delete Group. |
| 92 |
* |
| 93 |
* @uses \MainWP\Dashboard\MainWP_Manage_Groups::delete_group() |
| 94 |
*/ |
| 95 |
public function mainwp_group_delete() { |
| 96 |
$this->secure_request( 'mainwp_group_delete' ); |
| 97 |
|
| 98 |
MainWP_Manage_Groups::delete_group(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Method mainwp_group_add() |
| 103 |
* |
| 104 |
* Add Group. |
| 105 |
* |
| 106 |
* @uses \MainWP\Dashboard\MainWP_Manage_Groups::add_group() |
| 107 |
*/ |
| 108 |
public function mainwp_group_add() { |
| 109 |
$this->secure_request( 'mainwp_group_add' ); |
| 110 |
|
| 111 |
MainWP_Manage_Groups::add_group(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Method mainwp_group_getsites() |
| 116 |
* |
| 117 |
* Get Child Sites in group. |
| 118 |
* |
| 119 |
* @uses \MainWP\Dashboard\MainWP_Manage_Groups::get_sites() |
| 120 |
*/ |
| 121 |
public function mainwp_group_getsites() { |
| 122 |
$this->secure_request( 'mainwp_group_getsites' ); |
| 123 |
|
| 124 |
die( MainWP_Manage_Groups::get_sites() ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Method mainwp_group_updategroup() |
| 129 |
* |
| 130 |
* Update Group. |
| 131 |
* |
| 132 |
* @uses \MainWP\Dashboard\MainWP_Manage_Groups::update_group() |
| 133 |
*/ |
| 134 |
public function mainwp_group_updategroup() { |
| 135 |
$this->secure_request( 'mainwp_group_updategroup' ); |
| 136 |
|
| 137 |
MainWP_Manage_Groups::update_group(); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Method mainwp_checkwp() |
| 142 |
* |
| 143 |
* Check if WP can be added. |
| 144 |
* |
| 145 |
* @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::check_site() |
| 146 |
*/ |
| 147 |
public function mainwp_checkwp() { |
| 148 |
$this->check_security( 'mainwp_checkwp', 'security' ); |
| 149 |
MainWP_Manage_Sites_Handler::check_site(); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Method mainwp_addwp() |
| 154 |
* |
| 155 |
* Add WP to the database. |
| 156 |
* |
| 157 |
* @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::add_site() |
| 158 |
*/ |
| 159 |
public function mainwp_addwp() { |
| 160 |
$this->check_security( 'mainwp_addwp', 'security' ); |
| 161 |
MainWP_Manage_Sites_Handler::add_site(); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Method get_site_icon() |
| 166 |
* |
| 167 |
* Get Child Site Favicon. |
| 168 |
* |
| 169 |
* @uses \MainWP\Dashboard\MainWP_Sync::sync_site_icon() |
| 170 |
*/ |
| 171 |
public function get_site_icon() { |
| 172 |
$this->check_security( 'mainwp_get_site_icon', 'security' ); |
| 173 |
$siteId = null; |
| 174 |
if ( isset( $_POST['siteId'] ) ) { |
| 175 |
$siteId = intval( $_POST['siteId'] ); |
| 176 |
} |
| 177 |
$result = MainWP_Sync::sync_site_icon( $siteId ); |
| 178 |
wp_send_json( $result ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Method check_abandoned() |
| 183 |
* |
| 184 |
* Check abandoned plugins or themes. |
| 185 |
*/ |
| 186 |
public function check_abandoned() { |
| 187 |
$this->check_security( 'mainwp_check_abandoned', 'security' ); |
| 188 |
$siteId = null; |
| 189 |
if ( isset( $_POST['siteId'] ) ) { |
| 190 |
$siteId = intval( $_POST['siteId'] ); |
| 191 |
} |
| 192 |
$which = sanitize_text_field( wp_unslash( $_POST['which'] ) ); |
| 193 |
|
| 194 |
$result = MainWP_Utility::check_abandoned( $siteId, $which ); |
| 195 |
wp_send_json( $result ); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Method mainwp_testwp() |
| 200 |
* |
| 201 |
* Test if Child Site can be reached. |
| 202 |
* |
| 203 |
* @uses \MainWP\Dashboard\MainWP_Connect::try_visit() |
| 204 |
* @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id() |
| 205 |
* @uses \MainWP\Dashboard\MainWP_Utility::remove_http_prefix() |
| 206 |
*/ |
| 207 |
public function mainwp_testwp() { // phpcs:ignore -- comlex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated. |
| 208 |
$this->secure_request( 'mainwp_testwp' ); |
| 209 |
|
| 210 |
$url = null; |
| 211 |
$name = null; |
| 212 |
$http_user = null; |
| 213 |
$http_pass = null; |
| 214 |
$verifyCertificate = 1; |
| 215 |
$sslVersion = 0; |
| 216 |
|
| 217 |
if ( isset( $_POST['url'] ) ) { |
| 218 |
$url = sanitize_text_field( wp_unslash( $_POST['url'] ) ); |
| 219 |
$url = urldecode( $url ); |
| 220 |
|
| 221 |
$invalid = false; |
| 222 |
$info = wp_parse_url( $url ); |
| 223 |
|
| 224 |
if ( is_array( $info ) && ! empty( $info['port'] ) && ( 21 === intval( $info['port'] ) || 22 === intval( $info['port'] ) ) ) { // port 21, 22. |
| 225 |
$invalid = true; |
| 226 |
} |
| 227 |
|
| 228 |
$temp_url = MainWP_Utility::remove_http_prefix( $url, true ); |
| 229 |
|
| 230 |
if ( $invalid || ( false !== strpos( $url, '?=' ) ) || strpos( $temp_url, ':' ) ) { |
| 231 |
die( wp_json_encode( array( 'error' => esc_html__( 'Invalid URL.', 'mainwp' ) ) ) ); |
| 232 |
} |
| 233 |
|
| 234 |
$verifyCertificate = isset( $_POST['test_verify_cert'] ) ? intval( $_POST['test_verify_cert'] ) : 1; |
| 235 |
$forceUseIPv4 = apply_filters( 'mainwp_manage_sites_force_use_ipv4', false, $url ); |
| 236 |
$sslVersion = isset( $_POST['ssl_version'] ) ? intval( $_POST['ssl_version'] ) : 0; |
| 237 |
$http_user = isset( $_POST['http_user'] ) ? sanitize_text_field( wp_unslash( $_POST['http_user'] ) ) : ''; |
| 238 |
$http_pass = isset( $_POST['http_pass'] ) ? wp_unslash( $_POST['http_pass'] ) : ''; |
| 239 |
|
| 240 |
} elseif ( isset( $_POST['siteid'] ) ) { |
| 241 |
$website = MainWP_DB::instance()->get_website_by_id( intval( $_POST['siteid'] ) ); |
| 242 |
if ( $website ) { |
| 243 |
$url = $website->url; |
| 244 |
$name = $website->name; |
| 245 |
$verifyCertificate = $website->verify_certificate; |
| 246 |
$forceUseIPv4 = $website->force_use_ipv4; |
| 247 |
$sslVersion = $website->ssl_version; |
| 248 |
$http_user = $website->http_user; |
| 249 |
$http_pass = $website->http_pass; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
$ssl_verifyhost = false; |
| 254 |
|
| 255 |
if ( 1 == $verifyCertificate ) { |
| 256 |
$ssl_verifyhost = true; |
| 257 |
} elseif ( 2 == $verifyCertificate ) { |
| 258 |
if ( ( ( false === get_option( 'mainwp_sslVerifyCertificate' ) ) || ( 1 == get_option( 'mainwp_sslVerifyCertificate' ) ) ) ) { |
| 259 |
$ssl_verifyhost = true; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
$rslt = MainWP_Connect::try_visit( $url, $ssl_verifyhost, $http_user, $http_pass, $sslVersion, $forceUseIPv4 ); |
| 264 |
|
| 265 |
if ( isset( $rslt['error'] ) && ( '' !== $rslt['error'] ) && ( 'wp-admin/' !== substr( $url, - 9 ) ) ) { |
| 266 |
if ( substr( $url, - 1 ) != '/' ) { |
| 267 |
$url .= '/'; |
| 268 |
} |
| 269 |
$url .= 'wp-admin/'; |
| 270 |
$newrslt = MainWP_Connect::try_visit( $url, $ssl_verifyhost, $http_user, $http_pass, $sslVersion, $forceUseIPv4 ); |
| 271 |
if ( isset( $newrslt['error'] ) && ( '' !== $rslt['error'] ) ) { |
| 272 |
$rslt = $newrslt; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if ( null != $name ) { |
| 277 |
$rslt['sitename'] = esc_html( $name ); |
| 278 |
} |
| 279 |
|
| 280 |
wp_send_json( $rslt ); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Method mainwp_removesite() |
| 285 |
* |
| 286 |
* Remove a website from MainWP. |
| 287 |
* |
| 288 |
* @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::remove_site() |
| 289 |
*/ |
| 290 |
public function mainwp_removesite() { |
| 291 |
if ( ! mainwp_current_user_have_right( 'dashboard', 'delete_sites' ) ) { |
| 292 |
die( wp_json_encode( array( 'error' => mainwp_do_not_have_permissions( esc_html__( 'delete sites', 'mainwp' ), false ) ) ) ); |
| 293 |
} |
| 294 |
|
| 295 |
$this->secure_request( 'mainwp_removesite' ); |
| 296 |
|
| 297 |
MainWP_Manage_Sites_Handler::remove_site(); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Method mainwp_reconnectwp() |
| 302 |
* |
| 303 |
* Reconnect to Child Site. |
| 304 |
* |
| 305 |
* @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::reconnect_site() |
| 306 |
*/ |
| 307 |
public function mainwp_reconnectwp() { |
| 308 |
$this->secure_request( 'mainwp_reconnectwp' ); |
| 309 |
|
| 310 |
MainWP_Manage_Sites_Handler::reconnect_site(); |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Method mainwp_updatechildsite_value() |
| 315 |
* |
| 316 |
* Update Child Site value. |
| 317 |
* |
| 318 |
* @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::update_child_site_value() |
| 319 |
*/ |
| 320 |
public function mainwp_updatechildsite_value() { |
| 321 |
$this->secure_request( 'mainwp_updatechildsite_value' ); |
| 322 |
|
| 323 |
MainWP_Manage_Sites_Handler::update_child_site_value(); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Method mainwp_syncsites() |
| 328 |
* |
| 329 |
* Sync Child Sites. |
| 330 |
* |
| 331 |
* @uses \MainWP\Dashboard\MainWP_Updates_Overview::dismiss_sync_errors() |
| 332 |
* @uses \MainWP\Dashboard\MainWP_Updates_Overview::sync_site() |
| 333 |
*/ |
| 334 |
public function mainwp_syncsites() { |
| 335 |
$this->secure_request( 'mainwp_syncsites' ); |
| 336 |
MainWP_Updates_Overview::dismiss_sync_errors( false ); |
| 337 |
MainWP_Updates_Overview::sync_site(); |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Method mainwp_checksites() |
| 342 |
* |
| 343 |
* Check Child Sites. |
| 344 |
* |
| 345 |
* @uses \MainWP\Dashboard\MainWP_Monitoring_Handler::ajax_check_status_site() |
| 346 |
*/ |
| 347 |
public function mainwp_checksites() { |
| 348 |
$this->secure_request( 'mainwp_checksites' ); |
| 349 |
MainWP_Monitoring_Handler::ajax_check_status_site(); |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Method mainwp_manage_sites_suspend_site() |
| 354 |
* |
| 355 |
* Check Child Sites. |
| 356 |
* |
| 357 |
* @uses \MainWP\Dashboard\MainWP_Sync::sync_site_icon() |
| 358 |
*/ |
| 359 |
public function manage_suspend_site() { |
| 360 |
$this->secure_request( 'mainwp_manage_sites_suspend_site' ); |
| 361 |
$siteId = null; |
| 362 |
if ( isset( $_POST['siteid'] ) ) { |
| 363 |
$siteId = intval( $_POST['siteid'] ); |
| 364 |
} |
| 365 |
|
| 366 |
$newValues = array( |
| 367 |
'suspended' => isset( $_POST['suspended'] ) && '1' === $_POST['suspended'] ? 1 : 0, |
| 368 |
); |
| 369 |
|
| 370 |
if ( $siteId ) { |
| 371 |
MainWP_DB::instance()->update_website_values( $siteId, $newValues ); |
| 372 |
wp_send_json( array( 'result' => 'success' ) ); |
| 373 |
} else { |
| 374 |
wp_send_json( array( 'error' => 'Error: site id empty' ) ); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|