Changeset 2659243
- Timestamp:
- 01/18/2022 10:58:34 AM (4 years ago)
- Location:
- whmcs-bridge/trunk
- Files:
-
- 1 added
- 3 edited
-
bridge.init.php (modified) (1 diff)
-
includes/parser.inc.php (modified) (1 diff)
-
includes/request.class.old.php (added)
-
includes/request.class.php (modified) (27 diffs)
Legend:
- Unmodified
- Added
- Removed
-
whmcs-bridge/trunk/bridge.init.php
r2653261 r2659243 63 63 64 64 require_once(dirname(__FILE__) . '/includes/shared.inc.php'); 65 require_once(dirname(__FILE__) . '/includes/ http.class.php');65 require_once(dirname(__FILE__) . '/includes/request.class.php'); 66 66 require_once(dirname(__FILE__) . '/includes/footer.inc.php'); 67 67 require_once(dirname(__FILE__) . '/includes/integrator.inc.php'); -
whmcs-bridge/trunk/includes/parser.inc.php
r2653261 r2659243 330 330 ], $buffer); 331 331 332 $buffer = str_replace('whmcsBaseUrl+"/cart"', 'whmcsBaseUrl+" '.$home.'?ccce=cart"', $buffer);332 $buffer = str_replace('whmcsBaseUrl+"/cart"', 'whmcsBaseUrl+"?ccce=cart"', $buffer); 333 333 334 334 if (is_numeric($cache_setting) && $cache_setting > 0 && ( -
whmcs-bridge/trunk/includes/request.class.php
r2535021 r2659243 1 1 <?php 2 class bridgeHttpRequest {3 var $_fp; // HTTP socket4 var $_url; // full URL5 var $_host; // HTTP host6 var $_protocol; // protocol (HTTP/HTTPS)7 var $_uri; // request URI8 var $_port; // port2 class bridgeHttpRequest { 3 var $_fp; // HTTP socket 4 var $_url; // full URL 5 var $_host; // HTTP host 6 var $_protocol; // protocol (HTTP/HTTPS) 7 var $_uri; // request URI 8 var $_port; // port 9 9 var $_path; 10 var $error =false;11 var $errno =false;12 var $post =array(); //post variables, defaults to $_POST13 var $redirect =false;14 var $forceWithRedirect =array();15 var $errors =array();16 var $countRedirects =0;10 var $error=false; 11 var $errno=false; 12 var $post=array(); //post variables, defaults to $_POST 13 var $redirect=false; 14 var $forceWithRedirect=array(); 15 var $errors=array(); 16 var $countRedirects=0; 17 17 var $sid; 18 18 var $httpCode; 19 var $repost =false;19 var $repost=false; 20 20 var $type; //content-type 21 var $follow =true; //whether to follow redirect links or not22 var $noErrors =false; //whether to trigger an error in case of a curl error21 var $follow=true; //whether to follow redirect links or not 22 var $noErrors=false; //whether to trigger an error in case of a curl error 23 23 var $errorMessage; 24 var $httpHeaders = array();//array('bridgeon: 1'); //avoid 417 errors24 var $httpHeaders=array('Expect:','bridgeon: 1'); //avoid 417 errors 25 25 var $debugFunction; 26 26 var $time; 27 var $cookieArray =array();28 var $cookieCache ='';27 var $cookieArray=array(); 28 var $cookieCache=''; 29 29 30 30 // constructor 31 31 function __construct($url = "", $sid = "", $repost = false) { 32 32 if (!$url) return; 33 $this->sid =$sid;33 $this->sid=$sid; 34 34 $this->_url = $url; 35 35 $this->_scan_url(); 36 $this->post =$_POST;37 $this->repost =$repost;36 $this->post=$_POST; 37 $this->repost=$repost; 38 38 } 39 39 40 40 private function time($action) { 41 $t =function_exists('microtime') ? 'microtime' :'time';42 if ($action == 'reset') $this->time =$t(true);41 $t=function_exists('microtime') ? 'microtime' :'time'; 42 if ($action=='reset') $this->time=$t(true); 43 43 elseif ($action=='delta') return round(($t(true)-$this->time)*100,0); 44 44 } … … 66 66 67 67 private function processHeaders($headers) { 68 $this->debug(0, 'Processing headers: '. print_r($headers, true));68 $this->debug(0, 'Processing headers: '.json_encode($headers)); 69 69 70 70 // split headers, one per array element 71 if ( is_string($headers)) {71 if ( is_string($headers) ) { 72 72 // tolerate line terminator: CRLF = LF (RFC 2616 19.3) 73 73 $headers = str_replace("\r\n", "\n", $headers); … … 82 82 // If a redirection has taken place, The headers for each page request may have been passed. 83 83 // In this case, determine the final HTTP header and parse from there. 84 for ( $i = count($headers)-1; $i >= 0; $i--) {85 if ( !empty($headers[$i]) && false === strpos($headers[$i], ':')) {84 for ( $i = count($headers)-1; $i >= 0; $i-- ) { 85 if ( !empty($headers[$i]) && false === strpos($headers[$i], ':') ) { 86 86 $headers = array_splice($headers, $i); 87 87 break; … … 92 92 $newheaders = array(); 93 93 94 foreach ( $headers as $tempheader) {95 if ( empty($tempheader))94 foreach ( $headers as $tempheader ) { 95 if ( empty($tempheader) ) 96 96 continue; 97 97 98 if ( false === strpos($tempheader, ':')) {99 list( , $response['code'], $response['message']) = explode(' ', $tempheader, 3);98 if ( false === strpos($tempheader, ':') ) { 99 list( , $response['code'], $response['message']) = explode(' ', $tempheader, 3); 100 100 continue; 101 101 } … … 103 103 list($key, $value) = explode(':', $tempheader, 2); 104 104 105 if ( !empty($value)) {106 $key = strtolower( $key);107 108 if ( isset($newheaders[$key])) {109 if ( !is_array($newheaders[$key]))105 if ( !empty( $value ) ) { 106 $key = strtolower( $key ); 107 108 if ( isset( $newheaders[$key] ) ) { 109 if ( !is_array($newheaders[$key]) ) 110 110 $newheaders[$key] = array($newheaders[$key]); 111 $newheaders[$key][] = trim( $value);112 } else { 113 $newheaders[$key] = trim( $value);111 $newheaders[$key][] = trim( $value ); 112 } else { 113 $newheaders[$key] = trim( $value ); 114 114 } 115 115 if ('set-cookie' == $key) { 116 if ($cookies) $cookies .=' ;';116 if ($cookies) $cookies.=' ;'; 117 117 $cookies .= $value; 118 list($k,$rest) =explode('=',$value,2);119 $this->cookieArray[trim($k)] =$value;118 list($k,$rest)=explode('=',$value,2); 119 $this->cookieArray[trim($k)]=$value; 120 120 if (stristr($value,'=deleted')) 121 121 unset($_SESSION[$this->sid]['cookie-array'][trim($k)]); … … 129 129 130 130 // scan url 131 private function _scan_url() 132 { 131 private function _scan_url() { 133 132 $req = $this->_url; 134 133 … … 153 152 $this->_uri = '/'; 154 153 } else { 155 $params =substr(strrchr($this->_uri,'/'),1);156 $this->_path =str_replace($params,'',$this->_uri);154 $params=substr(strrchr($this->_uri,'/'),1); 155 $this->_path=str_replace($params,'',$this->_uri); 157 156 } 158 157 } … … 162 161 //return true; 163 162 if (ip2long($this->_host)) return true; //in case using an IP instead of a host name 164 $url =$this->_host;163 $url=$this->_host; 165 164 if (gethostbyname($url) == $url) 166 165 return false; … … 174 173 if (function_exists('finfo_open')) { 175 174 if ($finfo = finfo_open(FILEINFO_MIME_TYPE)) { 176 $mime =finfo_file($finfo, $file);175 $mime=finfo_file($finfo, $file); 177 176 finfo_close($finfo); 178 177 } … … 198 197 //error logging 199 198 function error($msg) { 200 $this->errorMsg =$msg;201 $this->error =true;199 $this->errorMsg=$msg; 200 $this->error=true; 202 201 //if (!$this->noErrors) trigger_error($msg,E_USER_WARNING); 203 $this->debug(E_USER_WARNING, $msg);202 $this->debug(E_USER_WARNING,$msg); 204 203 } 205 204 … … 213 212 214 213 // download URL to string 215 function DownloadToString($withHeaders=true, $withCookies=false) { 216 $html = $this->connect($this->_protocol.'://'.$this->_host.$this->_uri,$withHeaders, $withCookies); 214 function DownloadToString($withHeaders=true,$withCookies=false) { 215 if ($this->_port == 80 || $this->_port == 443) 216 $html = $this->connect($this->_protocol.'://'.$this->_host.$this->_uri,$withHeaders,$withCookies); 217 else 218 $html = $this->connect($this->_protocol.'://'.$this->_host.':'.$this->_port.$this->_uri,$withHeaders,$withCookies); 219 217 220 return $html; 218 221 } … … 243 246 } 244 247 245 function connect($url, $withHeaders=true,$withCookies=false) {248 function connect($url, $withHeaders=true, $withCookies=false) { 246 249 $this->time('reset'); 247 250 global $wordpressPageName; … … 273 276 if (function_exists('cc_whmcsbridge_sso_session')) 274 277 cc_whmcsbridge_sso_session(); 275 if (session_status() == PHP_SESSION_NONE )278 if (session_status() == PHP_SESSION_NONE && !headers_sent()) 276 279 session_start(); 277 280 … … 289 292 } 290 293 291 // curl_setopt($ch, CURLOPT_HTTPHEADER, $this->httpHeaders); //avoid 417 errors292 294 $http_args['headers'] = $this->httpHeaders; 293 295 … … 326 328 if (isset($_SESSION[$this->sid]['cookie-array']) && count($_SESSION[$this->sid]['cookie-array']) > 0) { 327 329 foreach ($_SESSION[$this->sid]['cookie-array'] as $n => $v) { 330 // CloudFlare 331 if (stristr($n, '__cfduid') !== false) continue; 332 if (stristr($n, '_cflb') !== false) continue; 333 if (stristr($n, '_cf_bm') !== false) continue; 334 328 335 if ($cookies) $cookies .= ';'; 329 336 $cookies .= $v; 330 337 } 331 338 } 332 //echo '<br />cookie before='.$cookies.'='; 339 333 340 if ($cookies) { 334 $this->debug(0, 'Cookie before:' . print_r($cookies, true)); 335 // curl_setopt($ch, CURLOPT_COOKIE, $cookies); 336 341 $this->debug(0, 'Cookie before:' . json_encode(explode("\r\n", $cookies))); 342 if (stristr($cookies, '__cfduid') !== false) { 343 $cookies = 'WHMCS'.substr($cookies, strpos($cookies, "WHMCS") + 1); 344 } 345 //curl_setopt($ch, CURLOPT_COOKIE, $cookies); 337 346 $http_args['cookies'] = explode(";", $cookies); 338 347 } … … 375 384 } 376 385 } 377 cc_whmcs_log(0, 'There are files: '. print_r($newfiles, true));386 cc_whmcs_log(0, 'There are files: '.json_encode($newfiles)); 378 387 }*/ 379 388 380 //cc_whmcs_log(0, 'Post: '.print_r($this->post, true)); 381 382 389 $rawPost = file_get_contents('php://input'); 390 cc_whmcs_log(0, "RAW data: ".$rawPost); 383 391 $apost = array(); 384 392 385 393 if (count($this->post) > 0) { 386 // curl_setopt($ch, CURLOPT_POST, 1); // set POST method 394 cc_whmcs_log(0, "HTTP Method POST 2"); 395 //curl_setopt($ch, CURLOPT_POST, 1); // set POST method 387 396 $post = ""; 388 397 $apost = array(); … … 410 419 } 411 420 } 421 } else if (stristr($url, 'two-factor') !== false && stristr($url, 'totp') === false) { 422 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // set POST method 423 cc_whmcs_log(0, "HTTP customrequest Method POST 1"); 412 424 } 413 425 … … 432 444 $http_args['body'] = $pfields; 433 445 } 434 } else { 435 $http_args['method'] = 'GET'; 436 } 446 447 } else if (!empty($rawPost)) { 448 $http_args['method'] = 'POST'; 449 450 if (in_array(substr($rawPost, 0, 1), ['[', '{', '"'])) { 451 // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // set POST method 452 } else { 453 parse_str($rawPost, $rawPost); 454 } 455 // curl_setopt($ch, CURLOPT_POSTFIELDS, $rawPost); 456 457 $http_args['body'] = $rawPost; 458 459 cc_whmcs_log(0, "Posting RAW: ".$rawPost); 460 // cc_whmcs_log(0, "HTTP customrequest Method POST 2"); 461 } else if (strtolower($_SERVER['REQUEST_METHOD']) == "post" && strstr($url, 'viewinvoice.php') === false) { 462 $http_args['method'] = 'POST'; 463 //curl_setopt($ch, CURLOPT_POST, 1); // set POST method 464 cc_whmcs_log(0, "HTTP Method POST 1"); 465 } 466 467 // $data = curl_exec($ch); // run the whole process 468 437 469 438 470 // Fix legacy headers … … 454 486 $data = wp_remote_request($url, $http_args); 455 487 456 // $data = curl_exec($ch); // run the whole process457 458 488 // if (curl_errno($ch)) { 459 489 // $this->errno = curl_errno($ch); 460 490 // $this->error = curl_error($ch); 461 // $error_msg = 'HTTP Error:' . $this->errno . '/' . $this->error . ' at ' . $url; 462 // $this->error($error_msg); 463 // return '<body>'.$error_msg.'</body>'; 491 // $error_msg = 'An error has occurred: ' . $this->error; 492 // $this->error($this->errno . '/' . $error_msg.' ('.$url.')'); 493 // cc_whmcs_log(0, 'HTTP Error: '.$this->errno . '/' . $error_msg.' ('.$url.')'); 494 // return '<body>'.$error_msg.'<br>Please try again later.</body>'; 464 495 // } 465 496 // 466 497 // $info = curl_getinfo($ch); 467 // $this->debug(0, 'Call information: ' . print_r($info, true));468 #$this->debug(0, 'Call response: ' . print_r(htmlentities($data), true));469 470 // $this->debug(0, 'Response: ' . json_encode($data));471 498 472 499 if (!empty($data)) { 473 500 // $headerLength = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 474 501 // $head = trim(substr($data, 0, $headerLength)); 475 // $head = str_replace($data['body'], '', $data['raw']);476 502 // if (strlen($data) > $headerLength) $body = substr($data, $headerLength); 477 503 // else $body = ''; 478 479 504 // if (false !== strpos($head, "\r\n\r\n")) { 480 505 // $headerParts = explode("\r\n\r\n", $head); 481 506 // $head = $headerParts[count($headerParts) - 1]; 482 507 // } 508 // 509 // cc_whmcs_log(0, "Head: ".json_encode($head)); 510 // 483 511 // $head = $this->processHeaders($head); 484 485 // cc_whmcs_log(0, 'Headers: '.print_r($head, true)); 512 // $headers = $head['headers']; 513 // $cookies = $head['cookies']; 514 // 515 // if (empty($cookies)) 516 // $cookies = $_SESSION['cookieCache']; 486 517 487 518 $headers = $data['headers']->getAll(); … … 514 545 515 546 if ($cookies) { 516 $this->debug(0, 'Cookie after:' . print_r($cookies, true));547 $this->debug(0, 'Cookie after:' . json_encode($cookies)); 517 548 518 549 if (!isset($_SESSION[$this->sid])) $_SESSION[$this->sid] = array(); 519 520 550 if (isset($_SESSION[$this->sid]['sessid'])) { 521 551 if (!strstr($cookies, 'PHPSESSID') && $cookies) $cookies .= ';' . $_SESSION[$this->sid]['sessid']; … … 524 554 $_SESSION[$this->sid]['cookies'] = $cookies; 525 555 } 526 //echo '<br />cookie after='.print_r($_SESSION[$this->sid]['cookies'],true).'='; 527 if (is_array($cookies)) $this->debug(0, 'Cookie after:' . print_r($cookies, true)); 528 529 // curl_close($ch); 556 if (is_array($cookies)) $this->debug(0, 'Cookie after:' . json_encode($cookies)); 557 558 //curl_close($ch); 530 559 531 560 //remove temporary upload files … … 537 566 538 567 $this->headers = $headers; 568 //$this->data = $data; 539 569 $this->data = $data['raw']; 540 570 $this->cookies = $cookies; … … 545 575 } 546 576 577 //$this->cookies = apply_filters('bridgeHttpRequest_post', $this->cookies); 578 547 579 $this->debug(0, 'Call process completed in ' . $this->time('delta') . ' microseconds'); 548 580 549 581 if ($this->follow && isset ($headers['location']) && $headers['location']) { 550 $this->debug(0, 'XX: redirect to:'. print_r($headers,true));582 $this->debug(0, 'XX: redirect to:'.json_encode($headers)); 551 583 $this->debug(0, 'XX: protocol='.$this->_protocol); 552 584 $this->debug(0, 'XX: path='.$this->_path); … … 554 586 $redir = $headers['location']; 555 587 556 if (strstr($this->_path, '/store/order') === false) { 588 $main_whmcs_url = parse_url(cc_whmcs_bridge_url()); 589 $this->debug(0, 'S0: '.json_encode($main_whmcs_url)); 590 591 if (strstr($this->_path, '/store/order') === false && strstr($this->_path, '/password/reset/change')) { 557 592 if ($this->os() == 'WINDOWS') { 558 593 if (strpos($redir, $this->_protocol . '://' . $this->_host . $this->_path) === 0) { … … 580 615 } 581 616 } else { 582 $redir = $this->_protocol . '://' . $this->_host . $redir; 617 if (substr($redir, 0, 1) != '/' && stristr($redir, ':208') === false 618 && substr($redir, 0, 4) != 'http') 619 $redir = '/' . $redir; 620 621 $redir_parts = parse_url($redir); 622 if (!empty($redir_parts['path'])) { 623 $redir_parts = pathinfo($redir_parts['path']); 624 if (!empty($redir_parts['dirname'])) 625 $redir_parts = $redir_parts['dirname']; 626 else 627 $redir_parts = $redir; 628 } else 629 $redir_parts = $redir; 630 631 $this->debug(0, "Redir: ".$redir); 632 633 if ((stristr($this->_protocol . '://' . $this->_host . $this->_path, $redir) === false 634 || ( 635 stristr($redir, $main_whmcs_url['host']) === false && 636 stristr($redir, $main_whmcs_url['path']) === false 637 )) && strstr($redir, '://') !== false 638 ) { 639 // do nothing 640 $bounce = true; 641 $this->debug(0, 'S2: ' . $redir); 642 } else if (stristr($redir, ':208') === false 643 && (!empty($main_whmcs_url['path']) && $main_whmcs_url['path'] != $redir_parts) 644 && stristr($redir, 'password/reset') === false 645 && strstr($redir, 'account/') === false 646 && strstr($redir, 'user/') === false 647 && strstr($redir, 'login/challenge') === false 648 && strstr($redir, 'store/') === false 649 && strstr($redir, 'clientarea.php') === false 650 && strstr($redir, 'rp=/login') === false 651 && stristr($redir, '://') === false 652 ) { 653 $redir = $this->_host . $this->_path . $redir; 654 $this->debug(0, 'S3: '.$redir); 655 } else if (stristr($redir, ':208') !== false) { 656 $bounce = true; 657 $this->debug(0, 'S4: ' . $redir); 658 } else if ($redir == '/clientarea.php') { 659 if (empty($rawPost)) 660 $bounce = true; 661 if (stristr($this->_path, '/user/accounts') !== false) 662 $redir = $this->_protocol . '://' .$this->_host .'/'. $redir; 663 else 664 $redir = $this->_protocol . '://' .$this->_host .$this->_path. $redir; 665 $this->debug(0, 'S4.1: '.$redir); 666 } else { 667 $redir = $this->_host . $redir; 668 $this->debug(0, 'S5: '.$redir); 669 } 670 671 if (empty($bounce) && substr($redir, -15) != '/clientarea.php') { 672 $redir = $this->_protocol . '://' . str_replace('//', '/', $redir); 673 $this->debug(0, 'S6: '.$redir); 674 } 583 675 } 584 676 $fwd = $this->forceWithRedirectToString($redir); … … 590 682 } 591 683 $this->debug(0, '[3] Redirect to: ' . $redir); 592 if (strstr($redir, 'viewinvoice.php') || (strstr($this->_path, '/store/order') && strstr($redir, 'cart.php'))) { 593 $newRedir = cc_whmcs_bridge_parse_url($redir); 594 $this->debug(0, '[XX] New Redirect: ' . $newRedir . ' (' . $redir . ')'); 684 685 if (strstr($redir, 'viewinvoice.php') || 686 (strstr($this->_path, '/store/order') && strstr($redir, 'cart.php')) || 687 (strstr($redir, 'action=details&success')) || 688 !empty($rawPost) 689 ) { 690 if (empty($bounce)) { 691 $opt = 0; 692 if (strstr($redir, 'action=details&success') || (!empty($rawPost) && !strstr($redir, 'clientarea.php'))) { 693 $newRedir = cc_whmcs_bridge_parse_url($redir, true); 694 $opt = 1; 695 } else { 696 $newRedir = cc_whmcs_bridge_parse_url($redir); 697 $opt = 2; 698 } 699 if (strstr($this->_path, '/store/order') && strstr($redir, 'cart.php')) { 700 $newRedir = str_replace('/store/order', '', $newRedir); 701 $newRedir = cc_whmcs_bridge_parse_url($newRedir); 702 $opt = 3; 703 } 704 705 $this->debug(0, '[XX - '.$opt.'] New Redirect: ' . $newRedir . ' (' . $redir . ')'); 706 } else { 707 $newRedir = $redir; 708 $redir = false; 709 } 710 595 711 if ($redir != $newRedir || stristr($redir, '../viewinvoice')) { 596 712 header('Location:' . $newRedir); … … 606 722 } else if (strstr($redir, 'cart.php?a=add&domain=register') || strstr($redir, 'cart.php?a=confproduct&i=') 607 723 || strstr($redir, 'cart.php?a=view') 724 || strstr($redir, 'cart.php?a=complete') 608 725 ) { 609 726 $newRedir = cc_whmcs_bridge_parse_url($redir); 610 727 header('location: '.$newRedir); 611 728 die(); 612 } else if (strstr($redir, 'cpsess') || strstr($redir, 'service-name') ) {729 } else if (strstr($redir, 'cpsess') || strstr($redir, 'service-name') || stristr($redir, $main_whmcs_url['host']) === false) { 613 730 header('location: '.$redir); 614 731 die(); 615 } else if (strstr($redir, 'custom_page=reissue') || strstr($redir, 'custom_page=manage_validation') || (strstr($url, 'login') !== false && !isset($this->post['bg']))) { 732 } else if (strstr($redir, 'custom_page=reissue') || 733 strstr($redir, 'custom_page=manage_validation') || (strstr($url, 'login') !== false && !isset($this->post['bg'])) 734 ) { 616 735 $newRedir = cc_whmcs_bridge_parse_url($redir); 617 736 if ($wordpressPageName) $p = $wordpressPageName;
Note: See TracChangeset
for help on using the changeset viewer.