PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.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 / includes / api.php

api.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 4.1, at includes/api.php

421 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Client Live Report Responder
4 *
5 * Legacy Client Reports Extension.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard;
11
12 /**
13 * MainWP Client Live Report Responder
14 *
15 * Legacy Client Reports Extension.
16 *
17 * @deprecated Moved to external Extension ( unprepared SQL ok )
18 * @see MainWP-Client-Reports-Extension
19 */
20
21 /**
22 * Check if user has access.
23 *
24 * @param $siteurl Child Site URL.
25 */
26 function check_live_reporting_access( $siteurl ) {
27 $siteurl = isset( $_POST['livereportingurl'] ) ? sanitize_text_field( wp_unslash( $_POST['livereportingurl'] ) ) : '';
28 $access = get_option( 'live-report-responder-provideaccess' );
29 return ( ( 'yes' == $access ) && ( get_option( 'live-report-responder-siteurl' ) == $siteurl ) );
30 }
31
32 /**
33 * Live Reports secure connection.
34 */
35 function live_reports_responder_secure_connection() {
36
37 $siteurl = isset( $_POST['livereportingurl'] ) ? sanitize_text_field( wp_unslash( $_POST['livereportingurl'] ) ) : '';
38 $securitykey = isset( $_POST['securitykey'] ) ? sanitize_text_field( wp_unslash( $_POST['securitykey'] ) ) : '';
39 $signature = isset( $_POST['signature'] ) ? sanitize_text_field( wp_unslash( $_POST['signature'] ) ) : '';
40 $action = isset( $_POST['action'] ) ? sanitize_text_field( wp_unslash( $_POST['action'] ) ) : '';
41 $timestamp = isset( $_POST['timestamp'] ) ? sanitize_text_field( wp_unslash( $_POST['timestamp'] ) ) : '';
42 $pubkey = isset( $_POST['pubkey'] ) ? sanitize_text_field( wp_unslash( $_POST['pubkey'] ) ) : null;
43
44 if ( ( null == $siteurl ) || ( null == $signature ) || ( null == $action ) || ( null == $timestamp ) ) {
45 return array( 'error' => 'Invalid request.' );
46 }
47
48 $access = get_option( 'live-report-responder-provideaccess' );
49 if ( ( 'yes' != $access ) || ( get_option( 'live-report-responder-siteurl' ) != $siteurl ) ) {
50 return array( 'error' => 'Error - Connection not allowed in the Managed Client Reports for WooCommerce Responder settings' );
51 }
52
53 if ( $timestamp < ( time() - 48 * 60 * 60 ) ) {
54 return array( 'error' => 'Outdated request.' );
55 }
56
57 $current_key = get_option( 'live-report-responder-pubkey' );
58 if ( ( null !== $pubkey ) ) {
59 if ( ! empty( $current_key ) ) {
60 return array( 'error' => 'The dashboard is already connected, release the connection on the dashboard please.' );
61 }
62
63 MainWP_Utility::update_option( 'live-report-responder-pubkey', $pubkey );
64 $current_key = $pubkey;
65 }
66
67 if ( empty( $current_key ) ) {
68 return array( 'error' => 'The dashboard is not connected, please reconnect to establish a secure connection.' );
69 }
70
71 $auth = openssl_verify( $action . $securitykey . $timestamp, base64_decode( $signature ), base64_decode( $current_key ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
72 if ( 0 === $auth ) {
73 return array( 'error' => 'An error occured while verifying the secure signature.' );
74 } elseif ( -1 === $auth ) {
75 return array( 'error' => 'Authentication failed, please reconnect the dashboard.' );
76 }
77
78 if ( ( 'on' == get_option( 'live-reports-responder-security-id' ) ) && ( get_option( 'live-reports-responder-security-code' ) !== base64_decode( $securitykey ) ) ) { // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
79 return array( 'error' => 'Invalid security ID.' );
80 }
81
82 define( 'DOING_CRON', true );
83
84 return true;
85 }
86
87 /**
88 * Check database to see if client exists.
89 *
90 * @param string $email Email address to check for.
91 * @param string $siteid Child Site ID.
92 *
93 * @return array
94 */
95 function check_if_valid_client( $email, $siteid ) {
96
97 $email = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '';
98 $siteid = isset( $_POST['siteid'] ) ? sanitize_text_field( wp_unslash( $_POST['siteid'] ) ) : false;
99
100 $checkPermission = check_live_reporting_access();
101 $result = array();
102 if ( $checkPermission ) {
103
104 /**
105 * WordPress database instance.
106 *
107 * @global object
108 */
109 global $wpdb;
110
111 $get_site_url = $wpdb->get_row( $wpdb->prepare( "SELECT `url` FROM {$wpdb->prefix}mainwp_wp WHERE id=%d", $siteid ) );
112 if ( ! empty( $get_site_url ) ) {
113 $get_site_details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mainwp_client_report_site_token WHERE token_id=%d AND token_value=%s AND site_url=%s", 12, $email, $get_site_url->url ) );
114 if ( $get_site_details ) {
115 $result['result'] = 'success';
116 $result['data'] = $get_site_details;
117 }
118 }
119 }
120
121 return $result;
122 }
123 $sites = ! empty( $siteid ) ? base64_encode( serialize( array( $siteid ) ) ) : ''; // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
124 if ( isset( $_POST['content'] ) && isset( $_POST['action'] ) && ( 'displaycontent' == $_POST['action'] ) ) {
125 $secureconnection = live_reports_responder_secure_connection();
126 if ( true === $secureconnection ) {
127 $checkPermission = check_live_reporting_access();
128 if ( $checkPermission ) {
129 $report = new \stdClass();
130 $report->title = 'Live Reports';
131 $report->date_from = strtotime( gmdate( 'Y-m-01' ) );
132 $report->date_to = strtotime( gmdate( 'Y-m-d' ) );
133 $report->client = '';
134 $report->client_id = 0;
135 $report->fname = '';
136 $report->fcompany = '';
137 $report->femail = '';
138 $report->name = '[client.name]';
139 $report->company = '';
140 $report->email = '';
141 $report->subject = 'Report for [client.site.name]';
142 $report->recurring_schedule = '';
143 $report->schedule_bcc_me = 0;
144 $report->header = wp_unslash( $_POST['content'] );
145 $report->body = '';
146 $report->footer = '';
147 $report->type = 0;
148 $report->sites = $sites;
149 $report->groups = '';
150 $report->schedule_nextsend = 0;
151 $filtered_reports = MainWP_Live_Reports::filter_report( $report, '' );
152 $site_id = isset( $_POST['siteid'] ) ? intval( $_POST['siteid'] ) : 0;
153 if ( ! empty( $site_id ) ) {
154 echo wp_json_encode(
155 array(
156 'result' => 'success',
157 'data' => html_entity_decode( stripslashes( $filtered_reports[ $site_id ]->filtered_header ) ),
158 )
159 );
160 }
161 exit;
162 } else {
163 echo wp_json_encode(
164 array(
165 'result' => 'error',
166 'message' => 'Permission Denied',
167 )
168 );
169 exit;
170 }
171 } elseif ( isset( $secureconnection['error'] ) ) {
172 echo wp_json_encode(
173 array(
174 'result' => 'error',
175 'message' => $secureconnection['error'],
176 )
177 );
178 exit;
179 } else {
180 echo wp_json_encode(
181 array(
182 'result' => 'error',
183 'message' => 'Error - Invalid Request',
184 )
185 );
186 exit;
187 }
188 }
189 if ( isset( $_POST['content'] ) && isset( $_POST['action'] ) && ( 'livereport' == $_POST['action'] ) ) {
190 $secureconnection = live_reports_responder_secure_connection();
191 if ( true === $secureconnection ) {
192 $checkPermission = check_live_reporting_access();
193 if ( $checkPermission ) {
194 $checkifvalidclient = check_if_valid_client();
195 $allAccess = isset( $_POST['allAccess'] ) ? sanitize_text_field( wp_unslash( $_POST['allAccess'] ) ) : false;
196 if ( ( isset( $checkifvalidclient['result'] ) && 'success' == $checkifvalidclient['result'] ) || $allAccess ) {
197 $report = new \stdClass();
198 $report->title = 'Live Report';
199 $report->date_from = isset( $_POST['date_from'] ) ? sanitize_text_field( wp_unslash( $_POST['date_from'] ) ) : '';
200 $report->date_to = isset( $_POST['date_to'] ) ? sanitize_text_field( wp_unslash( $_POST['date_to'] ) ) : '';
201 $report->client = '';
202 $report->client_id = 0;
203 $report->fname = '';
204 $report->fcompany = '';
205 $report->femail = '';
206 $report->name = '[client.name]';
207 $report->company = '';
208 $report->email = '';
209 $report->subject = 'Report for [client.site.name]';
210 $report->recurring_schedule = '';
211 $report->schedule_bcc_me = 0;
212 $report->header = wp_unslash( $_POST['content'] );
213 $report->body = '';
214 $report->footer = '';
215 $report->type = 0;
216 $report->sites = $sites;
217 $report->groups = '';
218 $report->schedule_nextsend = 0;
219 $allowed_tokens = isset( $_POST['allowed_tokens'] ) && is_array( $_POST['allowed_tokens'] ) ? sanitize_text_field( wp_unslash( $_POST['allowed_tokens'] ) ) : '';
220 $filtered_reports = MainWP_Live_Reports::filter_report( $report, $allowed_tokens );
221 echo wp_json_encode(
222 array(
223 'result' => 'success',
224 'data' => html_entity_decode( stripslashes( $filtered_reports[ $_POST['siteid'] ]->filtered_header ) ),
225 )
226 );
227 exit;
228 } else {
229 echo wp_json_encode(
230 array(
231 'result' => 'error',
232 'message' => 'No Report Found',
233 )
234 );
235 exit;
236 }
237 } else {
238 echo wp_json_encode(
239 array(
240 'result' => 'error',
241 'message' => 'Permission Denied',
242 )
243 );
244 exit;
245 }
246 } elseif ( isset( $secureconnection['error'] ) ) {
247 echo wp_json_encode(
248 array(
249 'result' => 'error',
250 'message' => $secureconnection['error'],
251 )
252 );
253 exit;
254 } else {
255 echo wp_json_encode(
256 array(
257 'result' => 'error',
258 'message' => 'Error - Invalid Request',
259 )
260 );
261 exit;
262 }
263 }
264 if ( isset( $_POST['email'] ) && isset( $_POST['action'] ) && ( 'getallsitesbyemail' == $_POST['action'] ) && ! empty( $_POST['email'] ) ) {
265 $secureconnection = live_reports_responder_secure_connection();
266 if ( $secureconnection ) {
267 $checkPermission = check_live_reporting_access();
268 if ( $checkPermission ) {
269
270 /**
271 * WordPress database instance.
272 *
273 * @global object
274 */
275 global $wpdb;
276
277 $result = array();
278 $get_allsites = $wpdb->get_results( $wpdb->prepare( "SELECT `site_url` FROM `{$wpdb->prefix}mainwp_client_report_site_token` WHERE token_id= %d AND token_value=%s ORDER BY `id` DESC", 12, sanitize_email( wp_unslash( $_POST['email'] ) ) ) );
279
280 if ( $get_allsites ) {
281 foreach ( $get_allsites as $site ) {
282 $get_site_details = $wpdb->get_row( $wpdb->prepare( "SELECT `id`,`name`,`url` FROM `{$wpdb->prefix}mainwp_wp` WHERE `url`=%s", $site->site_url ) );
283
284 if ( $get_site_details ) {
285 $result['result'] = 'success';
286 $result['data'][] = $get_site_details;
287 }
288 }
289 } else {
290 $result['result'] = 'error';
291 $result['message'] = 'No Site Found';
292 }
293 echo wp_json_encode( $result );
294 exit;
295 } else {
296 echo wp_json_encode(
297 array(
298 'result' => 'error',
299 'message' => 'Permission Denied',
300 )
301 );
302 exit;
303 }
304 } elseif ( isset( $secureconnection['error'] ) ) {
305 echo wp_json_encode(
306 array(
307 'result' => 'error',
308 'message' => $secureconnection['error'],
309 )
310 );
311 exit;
312 } else {
313 echo wp_json_encode(
314 array(
315 'result' => 'error',
316 'message' => 'Error - Invalid Request',
317 )
318 );
319 exit;
320 }
321 }
322 if ( isset( $_POST['action'] ) && ( 'getallsites' == $_POST['action'] ) ) {
323 $secureconnection = live_reports_responder_secure_connection();
324 if ( true === $secureconnection ) {
325 $checkPermission = check_live_reporting_access();
326 if ( $checkPermission ) {
327
328 /**
329 * WordPress database instance.
330 *
331 * @global object
332 */
333 global $wpdb;
334
335 $result = array();
336 $get_allsites = $wpdb->get_results( $wpdb->prepare( "SELECT `site_url` FROM `{$wpdb->prefix}mainwp_client_report_site_token` WHERE token_id= %d ORDER BY `id` DESC", 12 ) );
337
338 if ( $get_allsites ) {
339 foreach ( $get_allsites as $site ) {
340 $get_site_details = $wpdb->get_row( $wpdb->prepare( "SELECT `id`,`name`,`url` FROM `{$wpdb->prefix}mainwp_wp` WHERE `url`=%s", $site->site_url ) );
341
342 if ( $get_site_details ) {
343 $result['result'] = 'success';
344 $result['data'][] = $get_site_details;
345 }
346 }
347 } else {
348 $result['result'] = 'error';
349 $result['message'] = 'No Site Found';
350 }
351 echo wp_json_encode( $result );
352 exit;
353 } else {
354 echo wp_json_encode(
355 array(
356 'result' => 'error',
357 'message' => 'Permission Denied',
358 )
359 );
360 exit;
361 }
362 } elseif ( isset( $secureconnection['error'] ) ) {
363 echo wp_json_encode(
364 array(
365 'result' => 'error',
366 'message' => $secureconnection['error'],
367 )
368 );
369 exit;
370 } else {
371 echo wp_json_encode(
372 array(
373 'result' => 'error',
374 'message' => 'Error - Invalid Request',
375 )
376 );
377 exit;
378 }
379 }
380 if ( isset( $_POST['action'] ) && ( 'checkvalid_live_reports_responder_url' == $_POST['action'] ) ) {
381 $secureconnection = live_reports_responder_secure_connection();
382 if ( true === $secureconnection ) {
383 $checkPermission = check_live_reporting_access();
384 if ( $checkPermission ) {
385 echo wp_json_encode(
386 array(
387 'result' => 'success',
388 'message' => 'Access has been granted',
389 )
390 );
391 exit;
392 } else {
393 echo wp_json_encode(
394 array(
395 'result' => 'error',
396 'message' => 'Error - Connection not allowed in the Managed Client Reports for WooCommerce Responder settings',
397 )
398 );
399 exit;
400 }
401 } elseif ( isset( $secureconnection['error'] ) ) {
402 echo wp_json_encode(
403 array(
404 'result' => 'error',
405 'message' => $secureconnection['error'],
406 )
407 );
408 exit;
409 } else {
410 echo wp_json_encode(
411 array(
412 'result' => 'error',
413 'message' => 'Error - Invalid Request',
414 )
415 );
416 exit;
417 }
418 }
419
420 // phpcs:enable
421