PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.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 / pages / page-mainwp-site-open.php

page-mainwp-site-open.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.1, at pages/page-mainwp-site-open.php

381 lines 13.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This Class takes the requested Child Sites,
4 * and then redirects to child site WP Admin.
5 *
6 * @package MainWP/Site_Open
7 */
8
9 namespace MainWP\Dashboard;
10
11 // Exit if accessed directly.
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Class MainWP_Site_Open
18 *
19 * @package MainWP\Dashboard
20 */
21 class MainWP_Site_Open { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
22
23 /**
24 * Get Class Name
25 *
26 * @return string __CLASS__
27 */
28 public static function get_class_name() {
29 return __CLASS__;
30 }
31
32 /**
33 * Method get_open_site_admin_link()
34 *
35 * @param mixed $siteId Site ID.
36 * @param mixed $prt_content Print url or not.
37 * @param mixed $newWindow Open in new window.
38 * @param mixed $opennonce Open nonce.
39 * @param mixed $addition Additional parameters.
40 *
41 * @return string Link to open site.
42 */
43 public static function get_open_site_admin_link( $siteId, $prt_content = false, $newWindow = true, $opennonce = '', $addition = '' ) {
44
45 /**
46 * Filter: mainwp_open_site_addition_url
47 * Filter additional URL parameters for open site URL.
48 *
49 * @since 6.0
50 */
51 $addition = apply_filters( 'mainwp_open_site_addition_url', $addition, $siteId, $newWindow, $opennonce );
52
53 $url = 'admin.php?page=SiteOpen&newWindow=' . ( $newWindow ? 'yes' : 'no' ) . '&websiteid=' . intval( $siteId ) . '&_opennonce=' . ( empty( $opennonce ) ? esc_attr( wp_create_nonce( 'mainwp-admin-nonce' ) ) : esc_attr( $opennonce ) ) . $addition;
54
55 if ( $prt_content ) {
56 echo esc_url( $url );
57 }
58
59 return $url;
60 }
61
62 /**
63 * Child Site Dashboard Link redirect handler.
64 *
65 * This method checks to see if the current user is allow to access the
66 * Child Site, then grabs the websiteid, location, openurl & passes it onto
67 * either open_site_location or open_site methods.
68 *
69 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
70 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
71 */
72 public static function render() { // phpcs:ignore -- NOSONAR - complex.
73
74 static::verify_open_nonce();
75
76 if ( ! \mainwp_current_user_can( 'dashboard', 'access_wpadmin_on_child_sites' ) ) {
77 \mainwp_do_not_have_permissions( esc_html__( 'WP-Admin on child sites', 'mainwp' ) );
78
79 return;
80 }
81 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
82 if ( ! isset( $_GET['websiteid'] ) ) {
83 exit();
84 }
85
86 $id = intval( $_GET['websiteid'] );
87 $website = MainWP_DB::instance()->get_website_by_id( $id );
88
89 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
90 exit();
91 }
92
93 $location = '';
94 if ( isset( $_GET['location'] ) ) {
95 $location = base64_decode( wp_unslash( $_GET['location'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_decode used for HTTP compatible char.
96 }
97
98 $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '';
99
100 $query = wp_parse_url( $request_uri, PHP_URL_QUERY );
101
102 $add_params = array();
103
104 if ( $query ) {
105 parse_str( $query, $add_params );
106 }
107
108 $exclude_keys = array( 'page', 'websiteid', 'location', '_opennonce', '_opennonce', 'openUrl', 'filedl', 'dirdl', 'closeWindow' );
109
110 if ( isset( $_GET['openUrl'] ) && 'yes' === $_GET['openUrl'] ) {
111
112 $postdata = MainWP_Connect::get_get_data_authed( $website, 'index.php', 'where', true );
113 $postdata['open_location'] = $location; // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
114 ?>
115 <div class="ui segment" style="padding: 25rem">
116 <div class="ui active dimmer">
117 <div class="ui massive double text loader"><?php esc_html_e( 'Redirecting...', 'mainwp' ); ?></div>
118 </div>
119 <?php
120 $url = ( isset( $website->url ) && '' !== $website->url ? $website->url : $website->siteurl );
121 $url .= ( '/' !== substr( $url, - 1 ) ? '/' : '' );
122 ?>
123 <form method="POST" action="<?php echo esc_url( $url ); ?>" id="redirectForm"> <?php // phpcs:ignore -- NOSONAR - dublicate id ok. ?>
124 <?php MainWP_UI::generate_wp_nonce( 'mainwp-admin-nonce' ); ?>
125 <?php
126 foreach ( $postdata as $name => $value ) {
127 echo '<input type="hidden" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" />';
128 }
129
130 if ( is_array( $add_params ) ) {
131 foreach ( $add_params as $name => $value ) {
132 if ( in_array( $name, $exclude_keys, true ) ) {
133 continue;
134 }
135 echo '<input type="hidden" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" />';
136 }
137 }
138
139 ?>
140 </form>
141 </div>
142 <?php
143 } else {
144 $allow_params = array();
145
146 if ( is_array( $add_params ) ) {
147 foreach ( $add_params as $name => $value ) {
148 if ( in_array( $name, $exclude_keys, true ) ) {
149 continue;
150 }
151 $allow_params[ $name ] = $value;
152 }
153 }
154
155 $allow_vars = array(
156 'filedl',
157 'dirdl',
158 );
159
160 $allow_vars = apply_filters( 'mainwp_open_site_allow_vars', $allow_vars );
161 if ( is_array( $allow_vars ) ) {
162 foreach ( $allow_vars as $var ) {
163 if ( is_string( $var ) && isset( $_GET[ $var ] ) ) {
164 $allow_params[ $var ] = $_GET[ $var ]; // phpcs:ignore -- ok.
165 }
166 }
167 }
168
169 static::open_site( $website, $location, $allow_params );
170 }
171 // phpcs:enable
172 }
173
174 /**
175 * This method opens the requested Child Site Admin.
176 *
177 * @param mixed $website Website ID.
178 * @param mixed $location Website Location.
179 * @param array $params others params.
180 *
181 * @uses \MainWP\Dashboard\MainWP_Connect::get_get_data_authed()
182 */
183 private static function open_site( $website, $location, $params = array() ) {
184 if ( MainWP_Demo_Handle::get_instance()->is_demo_website( $website ) ) {
185 $action = $website->url . 'wp-admin.html';
186 } else {
187 $action = MainWP_Connect::get_get_data_authed( $website, ( null === $location || '' === $location ) ? 'index.php' : $location, 'where', false, $params );
188 }
189 $open_download = ! empty( $params['filedl'] ) ? true : false;
190 $close_window = ! empty( $_GET['closeWindow'] ) ? true : false; //phpcs:ignore -- ok.
191
192 /**
193 * Action: mainwp_site_go_to_wpadmin
194 *
195 * Fire before go to wp admin child site.
196 *
197 * @since 5.5
198 */
199 do_action( 'mainwp_site_go_to_wpadmin', $website, $location, $params );
200
201 ?>
202 <div class="ui segment">
203 <div class="ui active page dimmer <?php echo $open_download || $close_window ? 'open-site-close-window' : ''; ?>" style="margin: 0 !important;">
204 <?php
205 if ( $open_download ) {
206 ?>
207 <div class="ui double text loader"><?php esc_html_e( 'Downloading...', 'mainwp' ); ?></div>
208 <?php
209 } else {
210 ?>
211 <div class="ui double text loader"><?php esc_html_e( 'Redirecting...', 'mainwp' ); ?></div>
212 <?php
213 }
214 ?>
215 </div>
216 <form method="POST" action="<?php echo $action; // phpcs:ignore WordPress.Security.EscapeOutput ?>" id="redirectForm">
217 <?php MainWP_UI::generate_wp_nonce( 'mainwp-admin-nonce' ); ?>
218 </form>
219 </div>
220 <?php
221 }
222
223 /**
224 * This renders the method open_site _restore()
225 *
226 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
227 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
228 */
229 public static function render_restore() {
230
231 static::verify_open_nonce();
232
233 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
234 if ( ! isset( $_GET['websiteid'] ) ) {
235 exit();
236 }
237
238 $id = intval( $_GET['websiteid'] );
239 $website = MainWP_DB::instance()->get_website_by_id( $id );
240
241 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
242 exit();
243 }
244
245 $file = '';
246 if ( isset( $_GET['f'] ) ) {
247 $file = base64_decode( esc_html( wp_unslash( $_GET['f'] ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
248 }
249
250 $site = isset( $_GET['size'] ) ? esc_html( wp_unslash( $_GET['size'] ) ) : '';
251 // phpcs:enable
252
253 static::open_site_restore( $website, $file, $site );
254 }
255
256 /**
257 * This opens the site restore.
258 *
259 * @param mixed $website Website ID.
260 * @param mixed $file Restore File.
261 * @param mixed $size Post data size.
262 *
263 * @uses \MainWP\Dashboard\MainWP_Connect::get_get_data_authed()
264 */
265 public static function open_site_restore( $website, $file, $size ) {
266 ?>
267 <div class="ui segment" style="padding: 25rem">
268 <div class="ui active dimmer">
269 <div class="ui massive double text loader"><?php esc_html_e( 'Redirecting...', 'mainwp' ); ?></div>
270 </div>
271 <?php
272
273 $url = ( isset( $website->url ) && '' !== $website->url ? $website->url : $website->siteurl );
274 $url .= ( '/' !== substr( $url, - 1 ) ? '/' : '' );
275
276 $postdata = MainWP_Connect::get_get_data_authed( $website, $file, 'f', true );
277 $postdata['size'] = $size;
278 ?>
279 <form method="POST" action="<?php echo esc_url( $url ); ?>" id="redirectForm">
280 <?php MainWP_UI::generate_wp_nonce( 'mainwp-admin-nonce' ); ?>
281 <?php
282 foreach ( $postdata as $name => $value ) {
283 echo '<input type="hidden" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" />';
284 }
285 ?>
286 </form>
287 </div>
288 <?php
289 }
290
291 /**
292 * This verify opens the site nonce.
293 */
294 public static function verify_open_nonce() {
295 $nonce = '_opennonce';
296 if ( isset( $_GET[ $nonce ] ) && wp_verify_nonce( sanitize_key( $_GET[ $nonce ] ), 'mainwp-admin-nonce' ) ) {
297 return true;
298 } else {
299 wp_die( esc_html__( 'Unauthorized request. Invalid or missing nonce, be sure you are using the current version of the MainWP Dashboard and Extensions.', 'mainwp' ) );
300 }
301 }
302
303 /**
304 * This opens the site location.
305 *
306 * @outdated
307 *
308 * @param mixed $website Website ID.
309 * @param mixed $open_location Website URL.
310 *
311 * @uses \MainWP\Dashboard\MainWP_Connect::get_get_data_authed()
312 */
313 public static function open_site_location( $website, $open_location ) {
314 ?>
315 <div class="ui segment" style="padding: 25rem">
316 <div class="ui active dimmer">
317 <div class="ui massive double text loader"><?php esc_html_e( 'Redirecting...', 'mainwp' ); ?></div>
318 </div>
319 <?php
320
321 $url = ( isset( $website->url ) && '' !== $website->url ? $website->url : $website->siteurl );
322 $url .= ( '/' !== substr( $url, - 1 ) ? '/' : '' );
323
324 $postdata = MainWP_Connect::get_get_data_authed( $website, 'index.php', 'where', true );
325 $postdata['open_location'] = $open_location; // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
326 ?>
327 <form method="POST" action="<?php echo esc_url( $url ); ?>" id="redirectForm">
328 <?php MainWP_UI::generate_wp_nonce( 'mainwp-admin-nonce' ); ?>
329 <?php
330 foreach ( $postdata as $name => $value ) {
331 echo '<input type="hidden" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" />';
332 }
333 ?>
334 </form>
335 </div>
336 <?php
337 }
338
339
340 /**
341 * Method get_open_site_url()
342 *
343 * Render render open site url.
344 *
345 * @param mixed $website Website ID.
346 * @param mixed $location open location.
347 * @param bool $echo_out Echo or not.
348 *
349 * @return mixed Render modal window for themes selection.
350 */
351 public static function get_open_site_url( $website, $location = '', $echo_out = true ) {
352
353 $site_id = 0;
354
355 if ( is_numeric( $website ) ) {
356 $site_id = $website;
357 } elseif ( is_object( $website ) ) {
358 $site_id = $website->id;
359 } else {
360 return '';
361 }
362
363 $open_url = '';
364
365 if ( MainWP_Demo_Handle::get_instance()->is_demo_website( $site_id ) ) {
366 $open_url = MainWP_Demo_Handle::get_instance()->get_open_site_demo_url( $site_id );
367 } else {
368 $open_url = self::get_open_site_admin_link( $site_id );
369 if ( ! empty( $location ) ) {
370 $open_url .= '&location=' . $location;
371 }
372 }
373
374 if ( $echo_out ) {
375 echo $open_url; //phpcs:ignore WordPress.Security.EscapeOutput
376 }
377
378 return $open_url;
379 }
380 }
381