PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.1
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 5.2.1 All 155 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.1, at pages/page-mainwp-manage-sites-handler.php

411 lines 14.3 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 die( 'ERROR ' . $e->getMessage() ); // phpcs:ignore WordPress.Security.EscapeOutput
111 }
112
113 die( esc_html__( 'Site has been reconnected successfully!', 'mainwp' ) );
114 }
115
116
117 /**
118 * Method add_site()
119 *
120 * Add new Child Site.
121 *
122 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_View::add_site()
123 */
124 public static function add_site() {
125 $ret = array();
126 $error = '';
127 $message = '';
128 $site_id = 0;
129 $output = array();
130 $fetch_data = null;
131
132 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
133 if ( isset( $_POST['managesites_add_wpurl'] ) && isset( $_POST['managesites_add_wpadmin'] ) ) {
134 // Check if already in DB.
135 $website = MainWP_DB::instance()->get_websites_by_url( sanitize_text_field( wp_unslash( $_POST['managesites_add_wpurl'] ) ) );
136 list( $message, $error, $site_id, $fetch_data ) = MainWP_Manage_Sites_View::add_site( $website, $output );
137 }
138
139 $ret['add_me'] = ( isset( $_POST['add_me'] ) ? intval( $_POST['add_me'] ) : null );
140 if ( '' !== $error ) {
141
142 if ( ! empty( $fetch_data ) ) {
143 $ret['resp_data'] = $fetch_data;
144 }
145
146 $ret['response'] = 'ERROR ' . $error;
147 die( wp_json_encode( $ret ) );
148 }
149 $ret['response'] = $message;
150 $ret['siteid'] = $site_id;
151
152 if ( isset( $output['fetch_data'] ) ) {
153 $ret['resp_data'] = $output['fetch_data'];
154 } elseif ( ! empty( $fetch_data ) ) { //phpcs:ignore -- to valid.
155 $ret['resp_data'] = $fetch_data;
156 }
157 // phpcs:enable
158
159 if ( 1 === MainWP_DB::instance()->get_websites_count() ) {
160 $ret['redirectUrl'] = esc_url( admin_url( 'admin.php?page=managesites' ) );
161 }
162
163 die( wp_json_encode( $ret ) );
164 }
165
166 /**
167 * Method rest_api_add_site().
168 *
169 * Rest API add website.
170 *
171 * @param array $data fields array.
172 * @param array $output Output array.
173 *
174 * $data fields.
175 * 'url'.
176 * 'name'.
177 * 'admin'.
178 * 'uniqueid'.
179 * 'ssl_verify'.
180 * 'force_use_ipv4'.
181 * 'ssl_version'.
182 * 'http_user'.
183 * 'http_pass'.
184 * 'groupids'.
185 *
186 * @return mixed Results.
187 */
188 public static function rest_api_add_site( $data, &$output = array() ) {
189 $params['url'] = isset( $data['url'] ) ? sanitize_text_field( wp_unslash( $data['url'] ) ) : '';
190 $params['name'] = isset( $data['name'] ) ? sanitize_text_field( wp_unslash( $data['name'] ) ) : '';
191 $params['wpadmin'] = isset( $data['admin'] ) ? sanitize_text_field( wp_unslash( $data['admin'] ) ) : '';
192 $params['unique_id'] = isset( $data['uniqueid'] ) ? sanitize_text_field( wp_unslash( $data['uniqueid'] ) ) : '';
193 $params['ssl_verify'] = empty( $data['ssl_verify'] ) ? false : intval( $data['ssl_verify'] );
194 $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'] ) );
195 $params['http_user'] = isset( $data['http_user'] ) ? sanitize_text_field( wp_unslash( $data['http_user'] ) ) : '';
196 $params['http_pass'] = isset( $data['http_pass'] ) ? wp_unslash( $data['http_pass'] ) : '';
197 $params['groupids'] = isset( $data['groupids'] ) && ! empty( $data['groupids'] ) ? explode( ',', sanitize_text_field( wp_unslash( $data['groupids'] ) ) ) : array();
198 $website = MainWP_DB::instance()->get_websites_by_url( $params['url'] );
199 list( $message, $error, $site_id ) = MainWP_Manage_Sites_View::add_wp_site( $website, $params, $output );
200
201 if ( '' !== $error ) {
202 return array( 'error' => $error );
203 }
204 return array(
205 'response' => $message,
206 'siteid' => $site_id,
207 );
208 }
209
210 /**
211 * Method apply_plugin_settings()
212 *
213 * Apply plugin settings.
214 */
215 public static function apply_plugin_settings() {
216 $site_id = isset( $_POST['siteId'] ) ? intval( $_POST['siteId'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing
217 $ext_dir_slug = isset( $_POST['ext_dir_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['ext_dir_slug'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
218 if ( empty( $site_id ) ) {
219 die( wp_json_encode( array( 'error' => esc_html__( 'Invalid site ID. Please try again.', 'mainwp' ) ) ) );
220 }
221
222 /**
223 * Apply plugin settings
224 *
225 * Fires to apply certain plugin settigns automatically while adding a new site.
226 *
227 * @param int $site_id Child site ID.
228 *
229 * @since Unknown
230 */
231 do_action( 'mainwp_applypluginsettings_' . $ext_dir_slug, $site_id );
232 die( wp_json_encode( array( 'error' => esc_html__( 'Undefined error occurred. Please try again.', 'mainwp' ) ) ) );
233 }
234
235 /**
236 * Method save_note()
237 *
238 * Save Child Site Note.
239 *
240 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
241 * @uses \MainWP\Dashboard\MainWP_Utility::esc_content()
242 */
243 public static function save_note() {
244 if ( isset( $_POST['websiteid'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
245 $website = MainWP_DB::instance()->get_website_by_id( intval( $_POST['websiteid'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
246 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
247 $note = isset( $_POST['note'] ) ? wp_unslash( $_POST['note'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
248 $esc_note = MainWP_Utility::esc_content( $note );
249 MainWP_DB_Common::instance()->update_note( $website->id, $esc_note );
250
251 die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) );
252 } else {
253 die( wp_json_encode( array( 'error' => esc_html__( 'Are you sure this is your website?', 'mainwp' ) ) ) );
254 }
255 }
256 die( wp_json_encode( array( 'undefined_error' => true ) ) );
257 }
258
259 /**
260 * Method remove_site()
261 *
262 * Try to remove Child Site.
263 */
264 public static function remove_site() {
265 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
266 if ( isset( $_POST['id'] ) ) {
267
268 $result = self::remove_website( intval( $_POST['id'] ) );
269 $error = is_array( $result ) && isset( $result['error'] ) ? $result['error'] : '';
270
271 if ( 'NOMAINWP' === $error ) {
272 $error = esc_html__( 'Be sure to deactivate the child plugin on the child site to avoid potential security issues.', 'mainwp' );
273 }
274
275 if ( '' !== $error ) {
276 die( wp_json_encode( array( 'error' => esc_html( $error ) ) ) );
277 } elseif ( isset( $information['deactivated'] ) ) {
278 die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) );
279 } elseif ( isset( $information['removed'] ) ) {
280 die( wp_json_encode( array( 'result' => 'REMOVED' ) ) );
281 } else {
282 die( wp_json_encode( array( 'undefined_error' => true ) ) );
283 }
284 }
285 // phpcs:enable
286 die( wp_json_encode( array( 'result' => 'NOSITE' ) ) );
287 }
288
289 /**
290 * Method handle remove_site()
291 *
292 * Try to remove Child Site.
293 *
294 * @param object|int $site object or Child site ID.
295 *
296 * @return mixed|false result
297 * @throws \Exception Error message.
298 *
299 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
300 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
301 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_icons_dir()
302 */
303 public static function remove_website( $site ) {
304
305 if ( is_numeric( $site ) ) {
306 $website = MainWP_DB::instance()->get_website_by_id( intval( $site ) );
307 } else {
308 $website = $site;
309 }
310
311 $information = false;
312
313 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
314
315 $is_demo_wp = MainWP_Demo_Handle::get_instance()->is_demo_website( $website );
316
317 /**
318 * Deactive child plugin on live site only,
319 * DO NOT deactive child on staging site, it will deactive child plugin of source site.
320 */
321 if ( ! $website->is_staging && ! $is_demo_wp ) {
322 try {
323 $information = MainWP_Connect::fetch_url_authed( $website, 'deactivate' );
324 } catch ( MainWP_Exception $e ) {
325 $information['error'] = $e->getMessage();
326 }
327 } else {
328 $information['removed'] = true;
329 }
330
331 // Delete icon file.
332 $favi = MainWP_DB::instance()->get_website_option( $website, 'favi_icon', '' );
333 if ( ! empty( $favi ) && ( false !== strpos( $favi, 'favi-' . $website->id . '-' ) ) ) {
334
335 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
336
337 /**
338 * WordPress files system object.
339 *
340 * @global object
341 */
342 global $wp_filesystem;
343
344 $dirs = MainWP_System_Utility::get_icons_dir();
345 if ( $wp_filesystem->exists( $dirs[0] . $favi ) ) {
346 $wp_filesystem->delete( $dirs[0] . $favi );
347 }
348 }
349
350 // Remove from DB.
351 MainWP_DB::instance()->remove_website( $website->id );
352
353 /**
354 * Delete Child Sites
355 *
356 * Fires after a child site has been removed from MainWP Dashboard
357 *
358 * @param object $website Object containing child site data.
359 *
360 * @since 3.4
361 */
362 ! $is_demo_wp && do_action( 'mainwp_delete_site', $website );
363
364 /**
365 * Fires immediately after a new website is deleted.
366 *
367 * @since 4.5.1.1
368 *
369 * @param object $website website data.
370 */
371 ! $is_demo_wp && do_action( 'mainwp_site_deleted', $website );
372
373 }
374
375 return $information;
376 }
377
378 /**
379 * Method update_child_site_value()
380 *
381 * Update Child Site ID.
382 *
383 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
384 */
385 public static function update_child_site_value() {
386 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
387 if ( isset( $_POST['site_id'] ) ) {
388 $website = MainWP_DB::instance()->get_website_by_id( intval( $_POST['site_id'] ) );
389 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
390 $error = '';
391 $uniqueId = isset( $_POST['unique_id'] ) ? sanitize_text_field( wp_unslash( $_POST['unique_id'] ) ) : '';
392 try {
393 $information = MainWP_Connect::fetch_url_authed( $website, 'update_values', array( 'uniqueId' => $uniqueId ) );
394 } catch ( MainWP_Exception $e ) {
395 $error = $e->getMessage();
396 }
397
398 if ( '' !== $error ) {
399 die( wp_json_encode( array( 'error' => $error ) ) );
400 } elseif ( isset( $information['result'] ) && ( 'ok' === $information['result'] ) ) {
401 die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) );
402 } else {
403 die( wp_json_encode( array( 'undefined_error' => true ) ) );
404 }
405 }
406 }
407 // phpcs:enable
408 die( wp_json_encode( array( 'error' => 'NO_SIDE_ID' ) ) );
409 }
410 }
411