PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.3.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.3.2
6.2.3 6.2.2 6.2.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 All 156 releases
mainwp / pages / page-mainwp-manage-sites-handler.php

page-mainwp-manage-sites-handler.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0.3.2, at pages/page-mainwp-manage-sites-handler.php

416 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Manage Sites Handler.
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 /**
11 * Class MainWP_Manage_Sites_Handler
12 *
13 * @package MainWP\Dashboard
14 */
15 class MainWP_Manage_Sites_Handler {
16
17 /**
18 * Get Class Name
19 *
20 * @return string __CLASS__
21 */
22 public static function get_class_name() {
23 return __CLASS__;
24 }
25
26 /**
27 * Method check_site()
28 *
29 * Check to add site.
30 *
31 * @return mixed send json encode data
32 *
33 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
34 * @uses \MainWP\Dashboard\MainWP_Utility::esc_content()
35 */
36 public static function check_site() {
37 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
38 $url = isset( $_POST['url'] ) ? sanitize_text_field( wp_unslash( $_POST['url'] ) ) : '';
39 $url = urldecode( $url );
40
41 $invalid = false;
42 $info = wp_parse_url( $url );
43
44 if ( is_array( $info ) && ! empty( $info['port'] ) && ( 21 === intval( $info['port'] ) || 22 === intval( $info['port'] ) ) ) { // port 21, 22.
45 $invalid = true;
46 }
47
48 if ( $invalid || ( 0 !== strpos( $url, 'http://' ) && 0 !== strpos( $url, 'https://' ) ) || false !== strpos( $url, '?=' ) ) { // to fix: valid url to check.
49 die( wp_json_encode( array( 'error' => esc_html__( 'Invalid URL! Please enter valid URL to the Site URL field.', 'mainwp' ) ) ) );
50 }
51
52 $website = MainWP_DB::instance()->get_websites_by_url( $url );
53 $ret = array();
54
55 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
56 $ret['response'] = esc_html__( 'ERROR Site is already connected to your MainWP Dashboard.', 'mainwp' );
57 } else {
58 try {
59 $verify_cert = empty( $_POST['verify_certificate'] ) ? false : intval( $_POST['verify_certificate'] );
60 $ssl_version = empty( $_POST['ssl_version'] ) ? 0 : intval( $_POST['ssl_version'] );
61 $force_use_ipv4 = apply_filters( 'mainwp_manage_sites_force_use_ipv4', null, $url );
62 $http_user = ( isset( $_POST['http_user'] ) ? sanitize_text_field( wp_unslash( $_POST['http_user'] ) ) : '' );
63 $http_pass = ( isset( $_POST['http_pass'] ) ? wp_unslash( $_POST['http_pass'] ) : '' );
64 $admin = ( isset( $_POST['admin'] ) ? sanitize_text_field( wp_unslash( $_POST['admin'] ) ) : '' );
65
66 $output = array();
67
68 $information = MainWP_Connect::fetch_url_not_authed( $url, $admin, 'stats', null, false, $verify_cert, $http_user, $http_pass, $ssl_version, $others = array( 'force_use_ipv4' => $force_use_ipv4 ), $output ); // Fetch the stats with the given admin name.
69
70 if ( isset( $information['wpversion'] ) ) {
71 $ret['response'] = 'OK';
72 } elseif ( isset( $information['error'] ) ) {
73 $ret['response'] = 'ERROR ' . MainWP_Utility::esc_content( $information['error'] );
74 } else {
75 $ret['response'] = 'ERROR';
76 $ret['resp_data'] = isset( $output['fetch_data'] ) ? $output['fetch_data'] : '';
77 }
78 } catch ( MainWP_Exception $e ) {
79 $ret['response'] = $e->getMessage();
80 $ret['resp_data'] = $e->get_data();
81 }
82 }
83 $ret['check_me'] = ( isset( $_POST['check_me'] ) ? intval( $_POST['check_me'] ) : null );
84 die( wp_json_encode( $ret ) );
85 // phpcs:enable
86 }
87
88 /**
89 * Method reconnect_site()
90 *
91 * Try to reconnect to Child Site.
92 *
93 * @throws \Exception Error message.
94 *
95 * @uses \MainWP\Dashboard\MainWP_Manage_DB::get_website_by_id()
96 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_View::m_reconnect_site()
97 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
98 */
99 public static function reconnect_site() {
100 $siteId = isset( $_POST['siteid'] ) ? intval( $_POST['siteid'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing
101
102 try {
103 if ( MainWP_Utility::ctype_digit( $siteId ) ) {
104 $website = MainWP_DB::instance()->get_website_by_id( $siteId );
105 MainWP_Manage_Sites_View::m_reconnect_site( $website );
106 } else {
107 throw new \Exception( esc_html__( 'Site could not be connected. Please check the Status page and be sure that all system requirments pass.', 'mainwp' ) );
108 }
109 } catch ( \Exception $e ) {
110 $msg = $e->getMessage();
111 $arr_msg = MainWP_Utility::parse_html_error_message( $msg );
112 if ( is_array( $arr_msg ) ) {
113 die( 'ERROR ' . wp_json_encode( $arr_msg ) ); // phpcs:ignore WordPress.Security.EscapeOutput
114 }
115 die( 'ERROR ' . $msg ); // phpcs:ignore WordPress.Security.EscapeOutput
116 }
117
118 die( esc_html__( 'Site has been reconnected successfully!', 'mainwp' ) );
119 }
120
121
122 /**
123 * Method add_site()
124 *
125 * Add new Child Site.
126 *
127 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_View::add_site()
128 */
129 public static function add_site() {
130 $ret = array();
131 $error = '';
132 $message = '';
133 $site_id = 0;
134 $output = array();
135 $fetch_data = null;
136
137 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
138 if ( isset( $_POST['managesites_add_wpurl'] ) && isset( $_POST['managesites_add_wpadmin'] ) ) {
139 // Check if already in DB.
140 $website = MainWP_DB::instance()->get_websites_by_url( sanitize_text_field( wp_unslash( $_POST['managesites_add_wpurl'] ) ) );
141 list( $message, $error, $site_id, $fetch_data ) = MainWP_Manage_Sites_View::add_site( $website, $output );
142 }
143
144 $ret['add_me'] = ( isset( $_POST['add_me'] ) ? intval( $_POST['add_me'] ) : null );
145 if ( '' !== $error ) {
146
147 if ( ! empty( $fetch_data ) ) {
148 $ret['resp_data'] = $fetch_data;
149 }
150
151 $ret['response'] = 'ERROR ' . $error;
152 die( wp_json_encode( $ret ) );
153 }
154 $ret['response'] = $message;
155 $ret['siteid'] = $site_id;
156
157 if ( isset( $output['fetch_data'] ) ) {
158 $ret['resp_data'] = $output['fetch_data'];
159 } elseif ( ! empty( $fetch_data ) ) { //phpcs:ignore -- to valid.
160 $ret['resp_data'] = $fetch_data;
161 }
162 // phpcs:enable
163
164 if ( 1 === MainWP_DB::instance()->get_websites_count() ) {
165 $ret['redirectUrl'] = esc_url( admin_url( 'admin.php?page=managesites' ) );
166 }
167
168 die( wp_json_encode( $ret ) );
169 }
170
171 /**
172 * Method rest_api_add_site().
173 *
174 * Rest API add website.
175 *
176 * @param array $data fields array.
177 * @param array $output Output array.
178 *
179 * $data fields.
180 * 'url'.
181 * 'name'.
182 * 'admin'.
183 * 'uniqueid'.
184 * 'ssl_verify'.
185 * 'force_use_ipv4'.
186 * 'ssl_version'.
187 * 'http_user'.
188 * 'http_pass'.
189 * 'groupids'.
190 *
191 * @return mixed Results.
192 */
193 public static function rest_api_add_site( $data, &$output = array() ) {
194 $params['url'] = isset( $data['url'] ) ? sanitize_text_field( wp_unslash( $data['url'] ) ) : '';
195 $params['name'] = isset( $data['name'] ) ? sanitize_text_field( wp_unslash( $data['name'] ) ) : '';
196 $params['wpadmin'] = isset( $data['admin'] ) ? sanitize_text_field( wp_unslash( $data['admin'] ) ) : '';
197 $params['unique_id'] = isset( $data['uniqueid'] ) ? sanitize_text_field( wp_unslash( $data['uniqueid'] ) ) : '';
198 $params['ssl_verify'] = empty( $data['ssl_verify'] ) ? false : intval( $data['ssl_verify'] );
199 $params['force_use_ipv4'] = ( ! isset( $data['force_use_ipv4'] ) || ( empty( $data['force_use_ipv4'] ) && ( '0' !== $data['force_use_ipv4'] ) ) ? null : intval( $data['force_use_ipv4'] ) );
200 $params['http_user'] = isset( $data['http_user'] ) ? sanitize_text_field( wp_unslash( $data['http_user'] ) ) : '';
201 $params['http_pass'] = isset( $data['http_pass'] ) ? wp_unslash( $data['http_pass'] ) : '';
202 $params['groupids'] = isset( $data['groupids'] ) && ! empty( $data['groupids'] ) ? explode( ',', sanitize_text_field( wp_unslash( $data['groupids'] ) ) ) : array();
203 $website = MainWP_DB::instance()->get_websites_by_url( $params['url'] );
204 list( $message, $error, $site_id ) = MainWP_Manage_Sites_View::add_wp_site( $website, $params, $output );
205
206 if ( '' !== $error ) {
207 return array( 'error' => $error );
208 }
209 return array(
210 'response' => $message,
211 'siteid' => $site_id,
212 );
213 }
214
215 /**
216 * Method apply_plugin_settings()
217 *
218 * Apply plugin settings.
219 */
220 public static function apply_plugin_settings() {
221 $site_id = isset( $_POST['siteId'] ) ? intval( $_POST['siteId'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing
222 $ext_dir_slug = isset( $_POST['ext_dir_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['ext_dir_slug'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
223 if ( empty( $site_id ) ) {
224 die( wp_json_encode( array( 'error' => esc_html__( 'Invalid site ID. Please try again.', 'mainwp' ) ) ) );
225 }
226
227 /**
228 * Apply plugin settings
229 *
230 * Fires to apply certain plugin settigns automatically while adding a new site.
231 *
232 * @param int $site_id Child site ID.
233 *
234 * @since Unknown
235 */
236 do_action( 'mainwp_applypluginsettings_' . $ext_dir_slug, $site_id );
237 die( wp_json_encode( array( 'error' => esc_html__( 'Undefined error occurred. Please try again.', 'mainwp' ) ) ) );
238 }
239
240 /**
241 * Method save_note()
242 *
243 * Save Child Site Note.
244 *
245 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
246 * @uses \MainWP\Dashboard\MainWP_Utility::esc_content()
247 */
248 public static function save_note() {
249 if ( isset( $_POST['websiteid'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
250 $website = MainWP_DB::instance()->get_website_by_id( intval( $_POST['websiteid'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
251 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
252 $note = isset( $_POST['note'] ) ? wp_unslash( $_POST['note'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
253 $esc_note = MainWP_Utility::esc_content( $note );
254 MainWP_DB_Common::instance()->update_note( $website->id, $esc_note );
255
256 die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) );
257 } else {
258 die( wp_json_encode( array( 'error' => esc_html__( 'Are you sure this is your website?', 'mainwp' ) ) ) );
259 }
260 }
261 die( wp_json_encode( array( 'undefined_error' => true ) ) );
262 }
263
264 /**
265 * Method remove_site()
266 *
267 * Try to remove Child Site.
268 */
269 public static function remove_site() {
270 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
271 if ( isset( $_POST['id'] ) ) {
272
273 $result = self::remove_website( intval( $_POST['id'] ) );
274 $error = is_array( $result ) && isset( $result['error'] ) ? $result['error'] : '';
275
276 if ( 'NOMAINWP' === $error ) {
277 $error = esc_html__( 'Be sure to deactivate the child plugin on the child site to avoid potential security issues.', 'mainwp' );
278 }
279
280 if ( '' !== $error ) {
281 die( wp_json_encode( array( 'error' => esc_html( $error ) ) ) );
282 } elseif ( isset( $information['deactivated'] ) ) {
283 die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) );
284 } elseif ( isset( $information['removed'] ) ) {
285 die( wp_json_encode( array( 'result' => 'REMOVED' ) ) );
286 } else {
287 die( wp_json_encode( array( 'undefined_error' => true ) ) );
288 }
289 }
290 // phpcs:enable
291 die( wp_json_encode( array( 'result' => 'NOSITE' ) ) );
292 }
293
294 /**
295 * Method handle remove_site()
296 *
297 * Try to remove Child Site.
298 *
299 * @param object|int $site object or Child site ID.
300 *
301 * @return mixed|false result
302 * @throws \Exception Error message.
303 *
304 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
305 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
306 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_icons_dir()
307 */
308 public static function remove_website( $site ) {
309
310 if ( is_numeric( $site ) ) {
311 $website = MainWP_DB::instance()->get_website_by_id( intval( $site ) );
312 } else {
313 $website = $site;
314 }
315
316 $information = false;
317
318 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
319
320 $is_demo_wp = MainWP_Demo_Handle::get_instance()->is_demo_website( $website );
321
322 /**
323 * Deactive child plugin on live site only,
324 * DO NOT deactive child on staging site, it will deactive child plugin of source site.
325 */
326 if ( ! $website->is_staging && ! $is_demo_wp ) {
327 try {
328 $information = MainWP_Connect::fetch_url_authed( $website, 'deactivate' );
329 } catch ( MainWP_Exception $e ) {
330 $information['error'] = $e->getMessage();
331 }
332 } else {
333 $information['removed'] = true;
334 }
335
336 // Delete icon file.
337 $favi = MainWP_DB::instance()->get_website_option( $website, 'favi_icon', '' );
338 if ( ! empty( $favi ) && ( false !== strpos( $favi, 'favi-' . $website->id . '-' ) ) ) {
339
340 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
341
342 /**
343 * WordPress files system object.
344 *
345 * @global object
346 */
347 global $wp_filesystem;
348
349 $dirs = MainWP_System_Utility::get_icons_dir();
350 if ( $wp_filesystem->exists( $dirs[0] . $favi ) ) {
351 $wp_filesystem->delete( $dirs[0] . $favi );
352 }
353 }
354
355 // Remove from DB.
356 MainWP_DB::instance()->remove_website( $website->id );
357
358 /**
359 * Delete Child Sites
360 *
361 * Fires after a child site has been removed from MainWP Dashboard
362 *
363 * @param object $website Object containing child site data.
364 *
365 * @since 3.4
366 */
367 ! $is_demo_wp && do_action( 'mainwp_delete_site', $website );
368
369 /**
370 * Fires immediately after a new website is deleted.
371 *
372 * @since 4.5.1.1
373 *
374 * @param object $website website data.
375 */
376 ! $is_demo_wp && do_action( 'mainwp_site_deleted', $website );
377
378 }
379
380 return $information;
381 }
382
383 /**
384 * Method update_child_site_value()
385 *
386 * Update Child Site ID.
387 *
388 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
389 */
390 public static function update_child_site_value() {
391 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
392 if ( isset( $_POST['site_id'] ) ) {
393 $website = MainWP_DB::instance()->get_website_by_id( intval( $_POST['site_id'] ) );
394 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
395 $error = '';
396 $uniqueId = isset( $_POST['unique_id'] ) ? sanitize_text_field( wp_unslash( $_POST['unique_id'] ) ) : '';
397 try {
398 $information = MainWP_Connect::fetch_url_authed( $website, 'update_values', array( 'uniqueId' => $uniqueId ) );
399 } catch ( MainWP_Exception $e ) {
400 $error = $e->getMessage();
401 }
402
403 if ( '' !== $error ) {
404 die( wp_json_encode( array( 'error' => $error ) ) );
405 } elseif ( isset( $information['result'] ) && ( 'ok' === $information['result'] ) ) {
406 die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) );
407 } else {
408 die( wp_json_encode( array( 'undefined_error' => true ) ) );
409 }
410 }
411 }
412 // phpcs:enable
413 die( wp_json_encode( array( 'error' => 'NO_SIDE_ID' ) ) );
414 }
415 }
416