Plugin Directory

Changeset 2659243


Ignore:
Timestamp:
01/18/2022 10:58:34 AM (4 years ago)
Author:
globalprogramming
Message:

Wp remote request testing

Location:
whmcs-bridge/trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • whmcs-bridge/trunk/bridge.init.php

    r2653261 r2659243  
    6363
    6464require_once(dirname(__FILE__) . '/includes/shared.inc.php');
    65 require_once(dirname(__FILE__) . '/includes/http.class.php');
     65require_once(dirname(__FILE__) . '/includes/request.class.php');
    6666require_once(dirname(__FILE__) . '/includes/footer.inc.php');
    6767require_once(dirname(__FILE__) . '/includes/integrator.inc.php');
  • whmcs-bridge/trunk/includes/parser.inc.php

    r2653261 r2659243  
    330330    ], $buffer);
    331331
    332     $buffer = str_replace('whmcsBaseUrl+"/cart"', 'whmcsBaseUrl+"'.$home.'?ccce=cart"', $buffer);
     332    $buffer = str_replace('whmcsBaseUrl+"/cart"', 'whmcsBaseUrl+"?ccce=cart"', $buffer);
    333333
    334334    if (is_numeric($cache_setting) && $cache_setting > 0 && (
  • whmcs-bridge/trunk/includes/request.class.php

    r2535021 r2659243  
    11<?php
    2 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
     2class 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
    99    var $_path;
    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;
     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;
    1717    var $sid;
    1818    var $httpCode;
    19     var $repost                 = false;
     19    var $repost=false;
    2020    var $type; //content-type
    21     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
     21    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
    2323    var $errorMessage;
    24     var $httpHeaders            = array();//array('bridgeon: 1'); //avoid 417 errors
     24    var $httpHeaders=array('Expect:','bridgeon: 1'); //avoid 417 errors
    2525    var $debugFunction;
    2626    var $time;
    27     var $cookieArray            = array();
    28     var $cookieCache            = '';
     27    var $cookieArray=array();
     28    var $cookieCache='';
    2929
    3030    // constructor
    3131    function __construct($url = "", $sid = "", $repost = false) {
    3232        if (!$url) return;
    33         $this->sid = $sid;
     33        $this->sid=$sid;
    3434        $this->_url = $url;
    3535        $this->_scan_url();
    36         $this->post = $_POST;
    37         $this->repost = $repost;
     36        $this->post=$_POST;
     37        $this->repost=$repost;
    3838    }
    3939
    4040    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);
    4343        elseif ($action=='delta') return round(($t(true)-$this->time)*100,0);
    4444    }
     
    6666
    6767    private function processHeaders($headers) {
    68         $this->debug(0, 'Processing headers: '.print_r($headers, true));
     68        $this->debug(0, 'Processing headers: '.json_encode($headers));
    6969
    7070        // split headers, one per array element
    71         if (is_string($headers)) {
     71        if ( is_string($headers) ) {
    7272            // tolerate line terminator: CRLF = LF (RFC 2616 19.3)
    7373            $headers = str_replace("\r\n", "\n", $headers);
     
    8282        // If a redirection has taken place, The headers for each page request may have been passed.
    8383        // 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], ':') ) {
    8686                $headers = array_splice($headers, $i);
    8787                break;
     
    9292        $newheaders = array();
    9393
    94         foreach ($headers as $tempheader) {
    95             if (empty($tempheader))
     94        foreach ( $headers as $tempheader ) {
     95            if ( empty($tempheader) )
    9696                continue;
    9797
    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);
    100100                continue;
    101101            }
     
    103103            list($key, $value) = explode(':', $tempheader, 2);
    104104
    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]) )
    110110                        $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 );
    114114                }
    115115                if ('set-cookie' == $key) {
    116                     if ($cookies) $cookies .=' ;';
     116                    if ($cookies) $cookies.=' ;';
    117117                    $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;
    120120                    if (stristr($value,'=deleted'))
    121121                        unset($_SESSION[$this->sid]['cookie-array'][trim($k)]);
     
    129129
    130130    // scan url
    131     private function _scan_url()
    132     {
     131    private function _scan_url() {
    133132        $req = $this->_url;
    134133
     
    153152            $this->_uri = '/';
    154153        } 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);
    157156        }
    158157    }
     
    162161        //return true;
    163162        if (ip2long($this->_host)) return true; //in case using an IP instead of a host name
    164         $url = $this->_host;
     163        $url=$this->_host;
    165164        if (gethostbyname($url) == $url)
    166165            return false;
     
    174173        if (function_exists('finfo_open')) {
    175174            if ($finfo = finfo_open(FILEINFO_MIME_TYPE)) {
    176                 $mime = finfo_file($finfo, $file);
     175                $mime=finfo_file($finfo, $file);
    177176                finfo_close($finfo);
    178177            }
     
    198197    //error logging
    199198    function error($msg) {
    200         $this->errorMsg = $msg;
    201         $this->error = true;
     199        $this->errorMsg=$msg;
     200        $this->error=true;
    202201        //if (!$this->noErrors) trigger_error($msg,E_USER_WARNING);
    203         $this->debug(E_USER_WARNING, $msg);
     202        $this->debug(E_USER_WARNING,$msg);
    204203    }
    205204
     
    213212
    214213    // 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
    217220        return $html;
    218221    }
     
    243246    }
    244247
    245     function connect($url,$withHeaders=true,$withCookies=false) {
     248    function connect($url, $withHeaders=true, $withCookies=false) {
    246249        $this->time('reset');
    247250        global $wordpressPageName;
     
    273276        if (function_exists('cc_whmcsbridge_sso_session'))
    274277            cc_whmcsbridge_sso_session();
    275         if (session_status() == PHP_SESSION_NONE)
     278        if (session_status() == PHP_SESSION_NONE && !headers_sent())
    276279            session_start();
    277280
     
    289292        }
    290293
    291 //        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->httpHeaders); //avoid 417 errors
    292294        $http_args['headers'] = $this->httpHeaders;
    293295
     
    326328        if (isset($_SESSION[$this->sid]['cookie-array']) && count($_SESSION[$this->sid]['cookie-array']) > 0) {
    327329            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
    328335                if ($cookies) $cookies .= ';';
    329336                $cookies .= $v;
    330337            }
    331338        }
    332         //echo '<br />cookie before='.$cookies.'=';
     339
    333340        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);
    337346            $http_args['cookies'] = explode(";", $cookies);
    338347        }
     
    375384                }
    376385            }
    377             cc_whmcs_log(0, 'There are files:  '.print_r($newfiles, true));
     386            cc_whmcs_log(0, 'There are files:  '.json_encode($newfiles));
    378387        }*/
    379388
    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);
    383391        $apost = array();
    384392
    385393        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
    387396            $post = "";
    388397            $apost = array();
     
    410419                }
    411420            }
     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");
    412424        }
    413425
     
    432444                $http_args['body'] = $pfields;
    433445            }
    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
    437469
    438470        // Fix legacy headers
     
    454486        $data = wp_remote_request($url, $http_args);
    455487
    456 //        $data = curl_exec($ch); // run the whole process
    457 
    458488//        if (curl_errno($ch)) {
    459489//            $this->errno = curl_errno($ch);
    460490//            $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>';
    464495//        }
    465 
     496//
    466497//        $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));
    471498
    472499        if (!empty($data)) {
    473500//            $headerLength = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    474501//            $head = trim(substr($data, 0, $headerLength));
    475 //            $head = str_replace($data['body'], '', $data['raw']);
    476502//            if (strlen($data) > $headerLength) $body = substr($data, $headerLength);
    477503//            else $body = '';
    478 
    479504//            if (false !== strpos($head, "\r\n\r\n")) {
    480505//                $headerParts = explode("\r\n\r\n", $head);
    481506//                $head = $headerParts[count($headerParts) - 1];
    482507//            }
     508//
     509//            cc_whmcs_log(0, "Head: ".json_encode($head));
     510//
    483511//            $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'];
    486517
    487518            $headers = $data['headers']->getAll();
     
    514545
    515546        if ($cookies) {
    516             $this->debug(0, 'Cookie after:' . print_r($cookies, true));
     547            $this->debug(0, 'Cookie after:' . json_encode($cookies));
    517548
    518549            if (!isset($_SESSION[$this->sid])) $_SESSION[$this->sid] = array();
    519 
    520550            if (isset($_SESSION[$this->sid]['sessid'])) {
    521551                if (!strstr($cookies, 'PHPSESSID') && $cookies) $cookies .= ';' . $_SESSION[$this->sid]['sessid'];
     
    524554            $_SESSION[$this->sid]['cookies'] = $cookies;
    525555        }
    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);
    530559
    531560        //remove temporary upload files
     
    537566
    538567        $this->headers = $headers;
     568        //$this->data = $data;
    539569        $this->data = $data['raw'];
    540570        $this->cookies = $cookies;
     
    545575        }
    546576
     577        //$this->cookies = apply_filters('bridgeHttpRequest_post', $this->cookies);
     578
    547579        $this->debug(0, 'Call process completed in ' . $this->time('delta') . ' microseconds');
    548580
    549581        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));
    551583            $this->debug(0, 'XX: protocol='.$this->_protocol);
    552584            $this->debug(0, 'XX: path='.$this->_path);
     
    554586            $redir = $headers['location'];
    555587
    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')) {
    557592                if ($this->os() == 'WINDOWS') {
    558593                    if (strpos($redir, $this->_protocol . '://' . $this->_host . $this->_path) === 0) {
     
    580615                }
    581616            } 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                }
    583675            }
    584676            $fwd = $this->forceWithRedirectToString($redir);
     
    590682            }
    591683            $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
    595711                if ($redir != $newRedir || stristr($redir, '../viewinvoice')) {
    596712                    header('Location:' . $newRedir);
     
    606722            } else if (strstr($redir, 'cart.php?a=add&domain=register') || strstr($redir, 'cart.php?a=confproduct&i=')
    607723                || strstr($redir, 'cart.php?a=view')
     724                || strstr($redir, 'cart.php?a=complete')
    608725            ) {
    609726                $newRedir = cc_whmcs_bridge_parse_url($redir);
    610727                header('location: '.$newRedir);
    611728                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) {
    613730                header('location: '.$redir);
    614731                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            ) {
    616735                $newRedir =  cc_whmcs_bridge_parse_url($redir);
    617736                if ($wordpressPageName) $p = $wordpressPageName;
Note: See TracChangeset for help on using the changeset viewer.