PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-post-site-handler.php

class-mainwp-post-site-handler.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.1, at class/class-mainwp-post-site-handler.php

470 lines 17.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
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 === static::$instance ) {
38 static::$instance = new self();
39 }
40 return static::$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
66 $this->add_action( 'mainwp_group_getsites', array( &$this, 'mainwp_group_getsites' ) );
67 $this->add_action( 'mainwp_group_updategroup', array( &$this, 'mainwp_group_updategroup' ) );
68
69 // Widget: RightNow.
70 $this->add_action( 'mainwp_syncsites', array( &$this, 'mainwp_syncsites' ) );
71
72 $this->add_action( 'mainwp_checksites', array( &$this, 'mainwp_checksites' ) );
73 $this->add_action( 'mainwp_manage_sites_suspend_site', array( &$this, 'manage_suspend_site' ) );
74 $this->add_action( 'mainwp_group_sites_add', array( &$this, 'ajax_group_sites_add' ) );
75 }
76
77 /**
78 * Method mainwp_group_rename()
79 *
80 * Rename Group.
81 *
82 * @uses \MainWP\Dashboard\MainWP_Manage_Groups::rename_group()
83 */
84 public function mainwp_group_rename() {
85 $this->secure_request( 'mainwp_group_rename' );
86
87 MainWP_Manage_Groups::rename_group();
88 }
89
90 /**
91 * Method mainwp_group_delete()
92 *
93 * Delete Group.
94 *
95 * @uses \MainWP\Dashboard\MainWP_Manage_Groups::delete_group()
96 */
97 public function mainwp_group_delete() {
98 $this->secure_request( 'mainwp_group_delete' );
99
100 MainWP_Manage_Groups::delete_group();
101 }
102
103 /**
104 * Method mainwp_group_add()
105 *
106 * Add Group.
107 *
108 * @uses \MainWP\Dashboard\MainWP_Manage_Groups::add_group()
109 */
110 public function mainwp_group_add() {
111 $this->secure_request( 'mainwp_group_add' );
112 MainWP_Manage_Groups::add_group();
113 }
114
115 /**
116 * Method ajax_group_sites_add()
117 *
118 * Add Group in modal.
119 */
120 public function ajax_group_sites_add() {
121 $this->secure_request( 'mainwp_group_sites_add' );
122 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
123 $newName = isset( $_POST['newName'] ) ? sanitize_text_field( wp_unslash( $_POST['newName'] ) ) : '';
124 $newColor = isset( $_POST['newColor'] ) ? sanitize_hex_color( wp_unslash( $_POST['newColor'] ) ) : '';
125 if ( empty( $newColor ) ) {
126 $newColor = '#cce2ff'; // default.
127 }
128 $selected_sites = isset( $_POST['selected_sites'] ) && is_array( $_POST['selected_sites'] ) ? array_map( 'intval', wp_unslash( $_POST['selected_sites'] ) ) : array();
129 $selected_sites = array_filter( $selected_sites );
130 // phpcs:enable
131 $success = false;
132 if ( ! empty( $newName ) ) {
133 $success = MainWP_Manage_Groups::add_group_sites( $newName, $selected_sites, $newColor );
134 }
135 if ( ! $success ) {
136 wp_die( wp_json_encode( array( 'error' => esc_html__( 'Unexpected error occurred. Please try again.', 'mainwp' ) ) ) );
137 } else {
138 wp_die( wp_json_encode( array( 'success' => 1 ) ) );
139 }
140 }
141
142
143 /**
144 * Method mainwp_group_getsites()
145 *
146 * Get Child Sites in group.
147 */
148 public function mainwp_group_getsites() {
149
150 $this->secure_request( 'mainwp_group_getsites' );
151 //phpcs:disable WordPress.Security.NonceVerification.Missing
152 $groupid = isset( $_POST['groupId'] ) && ! empty( $_POST['groupId'] ) ? intval( $_POST['groupId'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
153 //phpcs:enable
154
155 if ( $groupid ) {
156 $group = MainWP_DB_Common::instance()->get_group_by_id( $groupid );
157 if ( ! empty( $group ) ) {
158 $websites = MainWP_DB::instance()->get_websites_by_group_id( $group->id );
159 $websiteIds = array();
160 if ( ! empty( $websites ) ) {
161 foreach ( $websites as $website ) {
162 $websiteIds[] = $website->id;
163 }
164 }
165
166 die( wp_json_encode( $websiteIds ) ); // phpcs:ignore WordPress.Security.EscapeOutput
167 }
168 }
169 die( 'ERROR' );
170 }
171
172 /**
173 * Method mainwp_group_updategroup()
174 *
175 * Update Group.
176 *
177 * @uses \MainWP\Dashboard\MainWP_Manage_Groups::update_group()
178 */
179 public function mainwp_group_updategroup() {
180 $this->secure_request( 'mainwp_group_updategroup' );
181
182 MainWP_Manage_Groups::update_group();
183 }
184
185 /**
186 * Method mainwp_checkwp()
187 *
188 * Check if WP can be added.
189 *
190 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::check_site()
191 */
192 public function mainwp_checkwp() {
193 $this->check_security( 'mainwp_checkwp', 'security' );
194 MainWP_Manage_Sites_Handler::check_site();
195 }
196
197 /**
198 * Method mainwp_addwp()
199 *
200 * Add WP to the database.
201 *
202 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::add_site()
203 */
204 public function mainwp_addwp() {
205 $this->check_security( 'mainwp_addwp', 'security' );
206 MainWP_Manage_Sites_Handler::add_site();
207 }
208
209 /**
210 * Method get_site_icon()
211 *
212 * Get Child Site Favicon.
213 *
214 * @uses \MainWP\Dashboard\MainWP_Sync::get_wp_icon()
215 */
216 public function get_site_icon() {
217 $this->check_security( 'mainwp_get_site_icon', 'security' );
218 $siteId = isset( $_POST['siteId'] ) ? intval( $_POST['siteId'] ) : null; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
219 $result = MainWP_Sync::get_wp_icon( $siteId );
220 wp_send_json( $result );
221 }
222
223 /**
224 * Method check_abandoned()
225 *
226 * Check abandoned plugins or themes.
227 */
228 public function check_abandoned() {
229 $this->check_security( 'mainwp_check_abandoned', 'security' );
230 $siteId = isset( $_POST['siteId'] ) ? intval( $_POST['siteId'] ) : null; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
231 $which = isset( $_POST['which'] ) ? sanitize_text_field( wp_unslash( $_POST['which'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
232 $result = MainWP_Utility::check_abandoned( $siteId, $which );
233 wp_send_json( $result );
234 }
235
236 /**
237 * Method mainwp_testwp()
238 *
239 * Test if Child Site can be reached.
240 *
241 * @uses \MainWP\Dashboard\MainWP_Connect::try_visit()
242 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
243 * @uses \MainWP\Dashboard\MainWP_Utility::remove_http_prefix()
244 */
245 public function mainwp_testwp() { // phpcs:ignore -- NOSONAR - complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
246 $this->secure_request( 'mainwp_testwp' );
247
248 $url = null;
249 $name = null;
250 $http_user = null;
251 $http_pass = null;
252 $verifyCertificate = 1;
253 $sslVersion = 0;
254
255 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
256 if ( isset( $_POST['url'] ) ) {
257 $url = sanitize_text_field( wp_unslash( $_POST['url'] ) );
258 $url = urldecode( $url );
259
260 $invalid = false;
261 $info = wp_parse_url( $url );
262
263 $def_not_allow = array( 21, 22 ); // not allow ports 21, 22.
264 $not_allow_ports = apply_filters( 'mainwp_connect_sites_not_allow_ports', $def_not_allow, $url );
265
266 if ( ! is_array( $not_allow_ports ) ) {
267 $not_allow_ports = $def_not_allow;
268 }
269
270 if ( is_array( $info ) && ! empty( $info['port'] ) && ( in_array( intval( $info['port'] ), $not_allow_ports, true ) ) ) {
271 $invalid = true;
272 }
273
274 $temp_url = MainWP_Utility::remove_http_prefix( $url, true );
275
276 if ( $invalid || false !== strpos( $url, '?=' ) ) {
277 die( wp_json_encode( array( 'error' => esc_html__( 'Invalid URL.', 'mainwp' ) ) ) );
278 }
279
280 if ( strpos( $temp_url, ':' ) ) {
281 $invalid = true;
282 $allow_ports = apply_filters( 'mainwp_connect_sites_allow_ports', array(), $url );
283 if ( ! empty( $allow_ports ) && is_array( $allow_ports ) && is_array( $info ) && ! empty( $info['port'] ) && ( in_array( intval( $info['port'] ), $allow_ports, true ) ) ) {
284 $invalid = false;
285 }
286 if ( $invalid ) {
287 die( wp_json_encode( array( 'error' => esc_html__( 'Invalid URL.', 'mainwp' ) ) ) );
288 }
289 }
290
291 $verifyCertificate = isset( $_POST['test_verify_cert'] ) ? intval( $_POST['test_verify_cert'] ) : 1;
292 $forceUseIPv4 = apply_filters( 'mainwp_manage_sites_force_use_ipv4', false, $url );
293 $sslVersion = isset( $_POST['ssl_version'] ) ? intval( $_POST['ssl_version'] ) : 0;
294 $http_user = isset( $_POST['http_user'] ) ? sanitize_text_field( wp_unslash( $_POST['http_user'] ) ) : '';
295 $http_pass = isset( $_POST['http_pass'] ) ? wp_unslash( $_POST['http_pass'] ) : '';
296
297 } elseif ( isset( $_POST['siteid'] ) ) {
298 $website = MainWP_DB::instance()->get_website_by_id( intval( $_POST['siteid'] ) );
299 if ( $website ) {
300 $url = $website->url;
301 $name = $website->name;
302 $verifyCertificate = (int) $website->verify_certificate;
303 $forceUseIPv4 = $website->force_use_ipv4;
304 $sslVersion = $website->ssl_version;
305 $http_user = $website->http_user;
306 $http_pass = $website->http_pass;
307 }
308 }
309 // phpcs:enable
310
311 $ssl_verifyhost = false;
312
313 if ( 1 === $verifyCertificate ) {
314 $ssl_verifyhost = true;
315 } elseif ( 2 === $verifyCertificate ) {
316 if ( ( false === get_option( 'mainwp_sslVerifyCertificate' ) ) || ( 1 === (int) get_option( 'mainwp_sslVerifyCertificate' ) ) ) {
317 $ssl_verifyhost = true;
318 }
319 }
320
321 $rslt = MainWP_Connect::try_visit( $url, $ssl_verifyhost, $http_user, $http_pass, $sslVersion, $forceUseIPv4 );
322
323 if ( isset( $rslt['error'] ) && ( '' !== $rslt['error'] ) && ( 'wp-admin/' !== substr( $url, - 9 ) ) ) {
324 if ( substr( $url, - 1 ) !== '/' ) {
325 $url .= '/';
326 }
327 $url .= 'wp-admin/';
328 $newrslt = MainWP_Connect::try_visit( $url, $ssl_verifyhost, $http_user, $http_pass, $sslVersion, $forceUseIPv4 );
329 if ( isset( $newrslt['error'] ) && ( '' !== $rslt['error'] ) ) {
330 $rslt = $newrslt;
331 }
332 }
333
334 if ( null !== $name ) {
335 $rslt['sitename'] = esc_html( $name );
336 }
337
338 wp_send_json( $rslt );
339 }
340
341 /**
342 * Method mainwp_removesite()
343 *
344 * Remove a website from MainWP.
345 *
346 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::remove_site()
347 */
348 public function mainwp_removesite() {
349 if ( ! mainwp_current_user_have_right( 'dashboard', 'delete_sites' ) ) {
350 die( wp_json_encode( array( 'error' => mainwp_do_not_have_permissions( esc_html__( 'delete sites', 'mainwp' ), false ) ) ) );
351 }
352
353 $this->secure_request( 'mainwp_removesite' );
354
355 MainWP_Manage_Sites_Handler::remove_site();
356 }
357
358 /**
359 * Method mainwp_reconnectwp()
360 *
361 * Reconnect to Child Site.
362 *
363 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::reconnect_site()
364 */
365 public function mainwp_reconnectwp() {
366 $this->secure_request( 'mainwp_reconnectwp' );
367
368 MainWP_Manage_Sites_Handler::reconnect_site();
369 }
370
371 /**
372 * Method mainwp_updatechildsite_value()
373 *
374 * Update Child Site value.
375 *
376 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::update_child_site_value()
377 */
378 public function mainwp_updatechildsite_value() {
379 $this->secure_request( 'mainwp_updatechildsite_value' );
380
381 MainWP_Manage_Sites_Handler::update_child_site_value();
382 }
383
384 /**
385 * Method mainwp_syncsites()
386 *
387 * Sync Child Sites.
388 *
389 * @uses \MainWP\Dashboard\MainWP_Updates_Overview::dismiss_sync_errors()
390 */
391 public function mainwp_syncsites() {
392 $this->secure_request( 'mainwp_syncsites' );
393 MainWP_Updates_Overview::dismiss_sync_errors( false );
394
395 $website = null;
396 $wp_id = isset( $_POST['wp_id'] ) ? intval( $_POST['wp_id'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
397 if ( $wp_id ) {
398 $website = MainWP_DB::instance()->get_website_by_id( $wp_id );
399 }
400
401 if ( null === $website ) {
402 die( wp_json_encode( array( 'error' => esc_html__( 'Site ID not found. Please reload the page and try again.', 'mainwp' ) ) ) );
403 }
404
405 if ( MainWP_Sync::sync_website( $website ) ) {
406 $website = MainWP_DB::instance()->get_website_by_id( $website->id ); // reload.
407 /**
408 * Fires immediately after website synced successfully.
409 *
410 * @since 4.6
411 *
412 * @param object $website website data.
413 */
414 do_action( 'mainwp_after_sync_site_success', $website );
415 die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) );
416 }
417
418 $website = MainWP_DB::instance()->get_website_by_id( $website->id );
419
420 die( wp_json_encode( array( 'error' => esc_html( wp_strip_all_tags( $website->sync_errors ) ) ) ) );
421 }
422
423 /**
424 * Method mainwp_checksites()
425 *
426 * Check Child Sites.
427 *
428 * @uses \MainWP\Dashboard\MainWP_Monitoring_Handler::ajax_check_status_site()
429 */
430 public function mainwp_checksites() {
431 $this->secure_request( 'mainwp_checksites' );
432 MainWP_Monitoring_Handler::ajax_check_status_site();
433 }
434
435 /**
436 * Method mainwp_manage_sites_suspend_site()
437 *
438 * Check Child Sites.
439 *
440 * @uses \MainWP\Dashboard\MainWP_Sync::get_wp_icon()
441 */
442 public function manage_suspend_site() {
443 $this->secure_request( 'mainwp_manage_sites_suspend_site' );
444 $siteId = isset( $_POST['siteid'] ) ? intval( $_POST['siteid'] ) : null; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
445 $suspended = isset( $_POST['suspended'] ) && '1' === $_POST['suspended'] ? 1 : 0; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
446 $newValues = array(
447 'suspended' => $suspended,
448 );
449
450 if ( $siteId ) {
451 $website = MainWP_DB::instance()->get_website_by_id( $siteId );
452 if ( $website && $website->suspended != $suspended ) { //phpcs:ignore -- to valid.
453 MainWP_DB::instance()->update_website_values( $siteId, $newValues );
454 /**
455 * Fires immediately after website suspended/unsuspend.
456 *
457 * @since 4.5.1.1
458 *
459 * @param object $website website data.
460 * @param int $suspended The new suspended value.
461 */
462 do_action( 'mainwp_site_suspended', $website, $suspended );
463 }
464 wp_send_json( array( 'result' => 'success' ) );
465 } else {
466 wp_send_json( array( 'error' => 'Error: site id empty' ) );
467 }
468 }
469 }
470