| 1 |
<?php |
| 2 |
function liveReportsResponderClasses() { |
| 3 |
if ( file_exists( '../class/class-mainwp-creport.php' ) ) { |
| 4 |
include_once '../class/class-mainwp-creport.php'; |
| 5 |
} |
| 6 |
} |
| 7 |
|
| 8 |
function checkLiveReportingAccess($siteurl) { |
| 9 |
$access = get_option( 'live-report-responder-provideaccess' ); |
| 10 |
return ( ( 'yes' == $access ) && ( get_option('live-report-responder-siteurl') == $siteurl ) ); |
| 11 |
} |
| 12 |
|
| 13 |
function LiveReportsResponderSecureConnection( $siteurl = null, $securitykey = null, $signature = null, $action = null, $timestamp = null, $pubkey = null ) { |
| 14 |
if ( ( $siteurl == null ) || ( $signature == null ) || ( $action == null ) || ( $timestamp == null ) ) { |
| 15 |
return array( 'error' => 'Invalid request.' ); |
| 16 |
} |
| 17 |
|
| 18 |
// to fix conflict with divi theme |
| 19 |
define('DOING_CRON', true); // to fix conflict issue with the team control extension |
| 20 |
|
| 21 |
$access = get_option( 'live-report-responder-provideaccess' ); |
| 22 |
if ( ( 'yes' != $access ) || ( get_option('live-report-responder-siteurl') != $siteurl ) ) { |
| 23 |
return array( 'error' => 'Error - Connection not allowed in the Managed Client Reports for WooCommerce Responder settings' ); |
| 24 |
} |
| 25 |
|
| 26 |
if ( $timestamp < ( time() - 48 * 60 * 60 ) ) { |
| 27 |
return array( 'error' => 'Outdated request.' ); |
| 28 |
} |
| 29 |
|
| 30 |
$current_key = get_option( 'live-report-responder-pubkey' ); |
| 31 |
if ( ( $pubkey !== null ) ) { |
| 32 |
if ( !empty( $current_key ) ) { |
| 33 |
return array( 'error' => 'The dashboard is already connected, release the connection on the dashboard please.' ); |
| 34 |
} |
| 35 |
|
| 36 |
MainWP_Utility::update_option( 'live-report-responder-pubkey', $pubkey ); |
| 37 |
$current_key = $pubkey; |
| 38 |
} |
| 39 |
|
| 40 |
if ( empty( $current_key ) ) { |
| 41 |
return array( 'error' => 'The dashboard is not connected, please reconnect to establish a secure connection.' ); |
| 42 |
} |
| 43 |
|
| 44 |
$auth = openssl_verify( $action . $securitykey . $timestamp, base64_decode( $signature ), base64_decode( $current_key ) ); |
| 45 |
if ( 0 === $auth ) { |
| 46 |
return array( 'error' => 'An error occured while verifying the secure signature.' ); |
| 47 |
} else if ( -1 === $auth ) { |
| 48 |
return array( 'error' => 'Authentication failed, please reconnect the dashboard.' ); |
| 49 |
} |
| 50 |
|
| 51 |
if ( ( get_option( 'live-reports-responder-security-id' ) == 'on' ) && ( get_option( 'live-reports-responder-security-code' ) !== base64_decode( $securitykey ) ) ) { |
| 52 |
return array( 'error' => 'Invalid security ID.' ); |
| 53 |
} |
| 54 |
|
| 55 |
return TRUE; |
| 56 |
} |
| 57 |
|
| 58 |
function checkifvalidclient( $email, $siteid ) { |
| 59 |
$checkPermission = checkLiveReportingAccess( $_POST[ 'livereportingurl' ] ); |
| 60 |
$result = array(); |
| 61 |
if ( $checkPermission ) { |
| 62 |
liveReportsResponderClasses(); |
| 63 |
global $wpdb; |
| 64 |
|
| 65 |
$get_site_url = $wpdb->get_row( $wpdb->prepare( "SELECT `url` FROM {$wpdb->prefix}mainwp_wp WHERE id=%d",$siteid)); |
| 66 |
if(!empty($get_site_url)){ |
| 67 |
$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='".$get_site_url->url."'", 12, $email ) ); |
| 68 |
if ( $get_site_details ) { |
| 69 |
$result['result'] = 'success'; |
| 70 |
$result['data'] = $get_site_details; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return $result; |
| 76 |
} |
| 77 |
|
| 78 |
if ( isset( $_POST[ 'content' ] ) && isset( $_POST[ 'action' ] ) && ( 'displaycontent' == $_POST[ 'action'] ) ) { |
| 79 |
$secureconnection = LiveReportsResponderSecureConnection( $_POST[ 'livereportingurl' ], ( isset( $_POST[ 'securitykey' ] ) ) ? $_POST['securitykey'] : '', isset( $_POST[ 'signature'] ) ? $_POST['signature'] : null, isset( $_POST[ 'action'] ) ? $_POST['action'] : null, isset( $_POST[ 'timestamp'] ) ? $_POST['timestamp'] : null); |
| 80 |
if ( $secureconnection === true ) { |
| 81 |
$checkPermission = checkLiveReportingAccess( $_POST['livereportingurl'] ); |
| 82 |
if ( $checkPermission ) { |
| 83 |
liveReportsResponderClasses(); |
| 84 |
$report = new stdClass(); |
| 85 |
$report->title = 'Live Reports'; |
| 86 |
$report->date_from = strtotime( date( "Y-m-01" ) ); |
| 87 |
$report->date_to = strtotime( date( 'Y-m-d' ) ); |
| 88 |
$report->client = ""; |
| 89 |
$report->client_id = 0; |
| 90 |
$report->fname = ""; |
| 91 |
$report->fcompany = ""; |
| 92 |
$report->femail = ""; |
| 93 |
$report->name = "[client.name]"; |
| 94 |
$report->company = ""; |
| 95 |
$report->email = ""; |
| 96 |
$report->subject = "Report for [client.site.name]"; |
| 97 |
$report->recurring_schedule = ""; |
| 98 |
$report->schedule_bcc_me = 0; |
| 99 |
$report->header = $_POST['content']; |
| 100 |
$report->body = ""; |
| 101 |
$report->footer = ""; |
| 102 |
$report->type = 0; |
| 103 |
$sites = base64_encode( serialize( array( $_POST['siteid'] ) ) ); |
| 104 |
$report->sites = $sites; |
| 105 |
$report->groups = ""; |
| 106 |
$report->schedule_nextsend = 0; |
| 107 |
$filtered_reports = MainWP_Live_Reports_Class::filter_report( $report, '' ); |
| 108 |
echo json_encode( array( |
| 109 |
"result" => "success", |
| 110 |
"data" => html_entity_decode( stripslashes( $filtered_reports[ $_POST['siteid'] ]->filtered_header ) ) |
| 111 |
) ); |
| 112 |
exit; |
| 113 |
} else { |
| 114 |
echo json_encode( array( "result" => "error", "message" => "Permission Denied" ) ); |
| 115 |
exit; |
| 116 |
} |
| 117 |
} else if ( isset( $secureconnection['error'] ) ) { |
| 118 |
echo json_encode( array( 'result' => "error", "message" => $secureconnection['error'] ) ); |
| 119 |
exit; |
| 120 |
} else { |
| 121 |
echo json_encode( array( 'result' => "error", "message" => "Error - Invalid Request" ) ); |
| 122 |
exit; |
| 123 |
} |
| 124 |
} |
| 125 |
if ( isset( $_POST[ 'content' ] ) && isset( $_POST[ 'action' ] ) && ( 'livereport' == $_POST[ 'action' ] ) ) { |
| 126 |
$secureconnection = LiveReportsResponderSecureConnection( $_POST[ 'livereportingurl' ], ( isset( $_POST[ 'securitykey' ] ) ) ? $_POST['securitykey'] : '', isset( $_POST[ 'signature'] ) ? $_POST['signature'] : null, isset( $_POST[ 'action'] ) ? $_POST['action'] : null, isset( $_POST[ 'timestamp'] ) ? $_POST['timestamp'] : null ); |
| 127 |
if ( $secureconnection === true ) { |
| 128 |
$checkPermission = checkLiveReportingAccess( $_POST[ 'livereportingurl' ] ); |
| 129 |
if ( $checkPermission ) { |
| 130 |
liveReportsResponderClasses(); |
| 131 |
$checkifvalidclient = checkifvalidclient( $_POST[ 'email' ], $_POST[ 'siteid' ] ); |
| 132 |
$allAccess = isset( $_POST[ 'allAccess' ] ) ? $_POST[ 'allAccess' ] : false; |
| 133 |
if ( ( isset( $checkifvalidclient[ 'result' ] ) && 'success' == $checkifvalidclient[ 'result' ] ) || $allAccess ) { |
| 134 |
$report = new stdClass(); |
| 135 |
$report->title = "Live Report"; |
| 136 |
$report->date_from = $_POST['date_from']; |
| 137 |
$report->date_to = $_POST['date_to']; |
| 138 |
$report->client = ""; |
| 139 |
$report->client_id = 0; |
| 140 |
$report->fname = ""; |
| 141 |
$report->fcompany = ""; |
| 142 |
$report->femail = ""; |
| 143 |
$report->name = "[client.name]"; |
| 144 |
$report->company = ""; |
| 145 |
$report->email = ""; |
| 146 |
$report->subject = "Report for [client.site.name]"; |
| 147 |
$report->recurring_schedule = ""; |
| 148 |
$report->schedule_bcc_me = 0; |
| 149 |
$report->header = $_POST['content']; |
| 150 |
$report->body = ""; |
| 151 |
$report->footer = ""; |
| 152 |
$report->type = 0; |
| 153 |
$sites = base64_encode( serialize( array( $_POST['siteid'] ) ) ); |
| 154 |
$report->sites = $sites; |
| 155 |
$report->groups = ""; |
| 156 |
$report->schedule_nextsend = 0; |
| 157 |
$filtered_reports = MainWP_Live_Reports_Class::filter_report( $report, $_POST[ 'allowed_tokens' ] ); |
| 158 |
echo json_encode( array( "result" => "success", "data" => html_entity_decode( stripslashes( $filtered_reports[ $_POST[ 'siteid' ] ]->filtered_header ) ) ) ); |
| 159 |
exit; |
| 160 |
} else { |
| 161 |
echo json_encode( array( "result" => "error", "message" => "No Report Found" ) ); |
| 162 |
exit; |
| 163 |
} |
| 164 |
} else { |
| 165 |
echo json_encode( array( "result" => "error", "message" => "Permission Denied" ) ); |
| 166 |
exit; |
| 167 |
} |
| 168 |
} else if ( isset( $secureconnection['error'] ) ) { |
| 169 |
echo json_encode( array( 'result' => "error", "message" => $secureconnection['error'] ) ); |
| 170 |
exit; |
| 171 |
} else { |
| 172 |
echo json_encode( array( 'result' => "error", "message" => "Error - Invalid Request" ) ); |
| 173 |
exit; |
| 174 |
} |
| 175 |
} |
| 176 |
if ( isset( $_POST[ 'email' ] ) && isset( $_POST[ 'action' ] ) && ( 'getallsitesbyemail' == $_POST[ 'action' ] ) && !empty( $_POST[ 'email' ] ) ) { |
| 177 |
$secureconnection = LiveReportsResponderSecureConnection( $_POST[ 'livereportingurl' ], ( isset( $_POST[ 'securitykey' ] ) ) ? $_POST[ 'securitykey' ] : '', isset( $_POST[ 'signature'] ) ? $_POST['signature'] : null, isset( $_POST[ 'action'] ) ? $_POST['action'] : null, isset( $_POST[ 'timestamp'] ) ? $_POST['timestamp'] : null ); |
| 178 |
if ( $secureconnection ) { |
| 179 |
$checkPermission = checkLiveReportingAccess( $_POST[ 'livereportingurl' ] ); |
| 180 |
if ( $checkPermission ) { |
| 181 |
liveReportsResponderClasses(); |
| 182 |
global $wpdb; |
| 183 |
$result = array(); |
| 184 |
$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, $_POST[ 'email' ] ) ); |
| 185 |
|
| 186 |
if ( $get_allsites ) { |
| 187 |
foreach ( $get_allsites as $site ) { |
| 188 |
$get_site_details = $wpdb->get_row( $wpdb->prepare( "SELECT `id`,`name`,`url` FROM `{$wpdb->prefix}mainwp_wp` WHERE `url`=%s", $site->site_url ) ); |
| 189 |
|
| 190 |
if ( $get_site_details ) { |
| 191 |
$result['result'] = 'success'; |
| 192 |
$result['data'][] = $get_site_details; |
| 193 |
} |
| 194 |
} |
| 195 |
} else { |
| 196 |
$result['result'] = 'error'; |
| 197 |
$result['message'] = 'No Site Found'; |
| 198 |
} |
| 199 |
echo json_encode( $result ); |
| 200 |
exit; |
| 201 |
} else { |
| 202 |
echo json_encode( array( "result" => "error", "message" => "Permission Denied" ) ); |
| 203 |
exit; |
| 204 |
} |
| 205 |
} else if ( isset( $secureconnection['error'] ) ) { |
| 206 |
echo json_encode( array( 'result' => "error", "message" => $secureconnection['error'] ) ); |
| 207 |
exit; |
| 208 |
} else { |
| 209 |
echo json_encode( array( 'result' => "error", "message" => "Error - Invalid Request" ) ); |
| 210 |
exit; |
| 211 |
} |
| 212 |
} |
| 213 |
if ( isset( $_POST[ 'action' ] ) && ( 'getallsites' == $_POST['action'] ) ) { |
| 214 |
$secureconnection = LiveReportsResponderSecureConnection( $_POST[ 'livereportingurl' ], ( isset( $_POST[ 'securitykey' ] ) ) ? $_POST[ 'securitykey' ] : '', isset( $_POST[ 'signature'] ) ? $_POST['signature'] : null, isset( $_POST[ 'action'] ) ? $_POST['action'] : null, isset( $_POST[ 'timestamp'] ) ? $_POST['timestamp'] : null ); |
| 215 |
if ( $secureconnection === true ) { |
| 216 |
$checkPermission = checkLiveReportingAccess( $_POST[ 'livereportingurl' ] ); |
| 217 |
if ( $checkPermission ) { |
| 218 |
liveReportsResponderClasses(); |
| 219 |
global $wpdb; |
| 220 |
$result = array(); |
| 221 |
$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 ) ); |
| 222 |
|
| 223 |
if ( $get_allsites ) { |
| 224 |
foreach ( $get_allsites as $site ) { |
| 225 |
$get_site_details = $wpdb->get_row( $wpdb->prepare( "SELECT `id`,`name`,`url` FROM `{$wpdb->prefix}mainwp_wp` WHERE `url`=%s", $site->site_url ) ); |
| 226 |
|
| 227 |
if ( $get_site_details ) { |
| 228 |
$result['result'] = 'success'; |
| 229 |
$result['data'][] = $get_site_details; |
| 230 |
} |
| 231 |
} |
| 232 |
} else { |
| 233 |
$result['result'] = 'error'; |
| 234 |
$result['message'] = 'No Site Found'; |
| 235 |
} |
| 236 |
echo json_encode( $result ); |
| 237 |
exit; |
| 238 |
} else { |
| 239 |
echo json_encode( array( 'result' => "error", "message" => "Permission Denied" ) ); |
| 240 |
exit; |
| 241 |
} |
| 242 |
} else if ( isset( $secureconnection['error'] ) ) { |
| 243 |
echo json_encode( array( 'result' => "error", "message" => $secureconnection['error'] ) ); |
| 244 |
exit; |
| 245 |
} else { |
| 246 |
echo json_encode( array( 'result' => "error", "message" => "Error - Invalid Request" ) ); |
| 247 |
exit; |
| 248 |
} |
| 249 |
} |
| 250 |
if ( isset( $_POST[ 'action' ] ) && ('checkvalid_live_reports_responder_url' == $_POST[ 'action' ] ) ) { |
| 251 |
$secureconnection = LiveReportsResponderSecureConnection( $_POST[ 'livereportingurl' ], ( isset( $_POST[ 'securitykey' ] ) ) ? $_POST[ 'securitykey' ] : '', isset( $_POST[ 'signature'] ) ? $_POST['signature'] : null, isset( $_POST[ 'action'] ) ? $_POST['action'] : null, isset( $_POST[ 'timestamp'] ) ? $_POST['timestamp'] : null, isset( $_POST[ 'pubkey'] ) ? $_POST['pubkey'] : null ); |
| 252 |
if ( $secureconnection === true ) { |
| 253 |
$checkPermission = checkLiveReportingAccess( $_POST[ 'livereportingurl' ] ); |
| 254 |
if ( $checkPermission ) { |
| 255 |
echo json_encode( array( 'result' => "success", "message" => "Access has been granted" ) ); |
| 256 |
exit; |
| 257 |
} else { |
| 258 |
echo json_encode( array( 'result' => "error", "message" => "Error - Connection not allowed in the Managed Client Reports for WooCommerce Responder settings" ) ); |
| 259 |
exit; |
| 260 |
} |
| 261 |
} else if ( isset( $secureconnection['error'] ) ) { |
| 262 |
echo json_encode( array( 'result' => "error", "message" => $secureconnection['error'] ) ); |
| 263 |
exit; |
| 264 |
} else { |
| 265 |
echo json_encode( array( 'result' => "error", "message" => "Error - Invalid Request" ) ); |
| 266 |
exit; |
| 267 |
} |
| 268 |
} |
| 269 |
?> |