Example #1
0
 function connect($url, $withHeaders = true, $withCookies = false)
 {
     $this->time('reset');
     global $wordpressPageName;
     $newfiles = array();
     $process = true;
     $cache_setting = (int) get_option("cc_whmcs_bridge_sso_cache");
     if (is_numeric($cache_setting) && $cache_setting > 0 && (substr($url, -4) == '.css' || substr($url, -3) == '.js' || substr($url, -4) == '.png' || substr($url, -4) == '.jpg' || substr($url, -5) == '.jpeg' || substr($url, -4) == '.gif')) {
         $cache_dir = dirname(__FILE__) . '/../cache/';
         $this->debug(0, 'Cacheable file - ' . $url);
         if (file_exists($cache_dir) && is_writable($cache_dir)) {
             $prefix = md5($url);
             foreach (glob($cache_dir . $prefix . '*') as $filename) {
                 if (strpos($filename, '_') !== false) {
                     $expires = explode('_', $filename);
                     if (time() > $expires[count($expires) - 1]) {
                         unlink($filename);
                         $this->debug(0, 'Cache expired on - ' . date('Y-m-d H:i:s', $expires[count($expires) - 1]) . ' - ' . $url);
                     } else {
                         $this->debug(0, 'Cache found for - ' . $url . ', fetching content from ' . $filename);
                         $body = file_get_contents($filename);
                         $this->body = $body;
                         $process = false;
                     }
                 }
             }
         }
     }
     if ($process) {
         $this->debug(0, 'Not cached, processing file - ' . $url);
         if (function_exists('cc_whmcsbridge_sso_session')) {
             cc_whmcsbridge_sso_session();
         }
         if (!session_id()) {
             @session_start();
         }
         if (isset($_SESSION[$this->sid])) {
             $this->debug(0, 'session:' . print_r($_SESSION[$this->sid], true));
         }
         $ch = curl_init();
         // initialize curl handle
         //echo '<br />call:'.$url;echo '<br />post='.print_r($this->post,true).'=<br />headers='.print_r($this->httpHeaders,true).'<br />';
         $this->debug(0, 'CURL call: ' . $url . (is_array($this->post) ? ' with ' . print_r($this->post, true) : ''));
         curl_setopt($ch, CURLOPT_URL, $url);
         // set url to post to
         curl_setopt($ch, CURLOPT_FAILONERROR, 1);
         if ($withHeaders) {
             curl_setopt($ch, CURLOPT_HEADER, 1);
         }
         if (get_option("cc_whmcs_bridge_affiliate_id") && is_numeric(get_option("cc_whmcs_bridge_affiliate_id")) && get_option("cc_whmcs_bridge_affiliate_id") > 0) {
             $this->httpHeaders[] = 'bridgeaffiliate: ' . get_option("cc_whmcs_bridge_affiliate_id");
         }
         $this->httpHeaders[] = 'bridgeon: 1';
         curl_setopt($ch, CURLOPT_HTTPHEADER, $this->httpHeaders);
         //avoid 417 errors
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         // return into a variable
         curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
         //cloudflare debug
         curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
         curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
         //CURLOPT_REFERER -  The contents of the "Referer: " header to be used in a HTTP request.
         //CURLOPT_INTERFACE -  The name of the outgoing network interface to use. This can be an interface name, an IP address or a host name.
         curl_setopt($ch, CURLOPT_TIMEOUT, 120);
         // times out after 120s
         if ($this->_protocol == "https") {
             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
             curl_setopt($ch, CURLOPT_CAINFO, NULL);
             curl_setopt($ch, CURLOPT_CAPATH, NULL);
             //curl_setopt($ch, CURLOPT_SSLVERSION, 3);
         }
         // gzip
         //curl_setopt($ch, CURLOPT_ENCODING, "");
         // quicker lookup
         //curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
         $cookies = "";
         $cookies = apply_filters('bridgeHttpRequest_pre', $cookies);
         if (isset($_SESSION[$this->sid]['cookie-array']) && count($_SESSION[$this->sid]['cookie-array']) > 0) {
             foreach ($_SESSION[$this->sid]['cookie-array'] as $n => $v) {
                 if ($cookies) {
                     $cookies .= ';';
                 }
                 $cookies .= $v;
             }
         }
         //echo '<br />cookie before='.$cookies.'=';
         if ($cookies) {
             $this->debug(0, 'Cookie before:' . print_r($cookies, true));
             curl_setopt($ch, CURLOPT_COOKIE, $cookies);
         }
         $_SESSION['cookieCache'] = $cookies;
         if (count($_FILES) > 0) {
             foreach ($_FILES as $name => $file) {
                 if (is_array($file['tmp_name']) && count($file['tmp_name']) > 0) {
                     $c = count($file['tmp_name']);
                     for ($i = 0; $i < $c; $i++) {
                         if ($file['tmp_name'][$i]) {
                             $newfile = BLOGUPLOADDIR . $file['name'][$i];
                             $newfiles[] = $newfile;
                             copy($file['tmp_name'][$i], $newfile);
                             if ($file['tmp_name'][$i]) {
                                 $this->post[$name][$i] = '@' . $newfile . $this->mimeType($newfile);
                             }
                         }
                     }
                 } elseif ($file['tmp_name']) {
                     $newfile = BLOGUPLOADDIR . $file['name'];
                     $newfiles[] = $newfile;
                     copy($file['tmp_name'], $newfile);
                     if ($file['tmp_name']) {
                         $this->post[$name] = '@' . $newfile . $this->mimeType($newfile);
                     }
                 }
             }
         }
         $apost = array();
         if (count($this->post) > 0) {
             curl_setopt($ch, CURLOPT_POST, 1);
             // set POST method
             $post = "";
             $apost = array();
             foreach ($this->post as $k => $v) {
                 if (is_array($v)) {
                     foreach ($v as $k2 => $v2) {
                         if (is_array($v2)) {
                             foreach ($v2 as $k3 => $v3) {
                                 if (is_array($v3)) {
                                     foreach ($v3 as $k4 => $v4) {
                                         $apost[$k . '[' . $k2 . ']' . '[' . $k3 . '][' . $k4 . ']'] = $v4;
                                     }
                                 } else {
                                     $apost[$k . '[' . $k2 . ']' . '[' . $k3 . ']'] = $v3;
                                 }
                             }
                         } else {
                             $apost[$k . '[' . $k2 . ']'] = $v2;
                         }
                     }
                 } else {
                     $apost[$k] = $v;
                 }
             }
         }
         if (count($apost) > 0) {
             // waiting for php 5.5's curlfiles support to be standard
             if (count($newfiles) > 0) {
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $apost);
             } else {
                 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apost));
             }
         }
         $data = curl_exec($ch);
         // run the whole process
         if (curl_errno($ch)) {
             $this->errno = curl_errno($ch);
             $this->error = curl_error($ch);
             $this->error('HTTP Error:' . $this->errno . '/' . $this->error . ' at ' . $url);
             return false;
         }
         $info = curl_getinfo($ch);
         $this->debug(0, 'Call information: ' . print_r($info, true));
         if (!empty($data)) {
             $headerLength = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
             $head = trim(substr($data, 0, $headerLength));
             if (strlen($data) > $headerLength) {
                 $body = substr($data, $headerLength);
             } else {
                 $body = '';
             }
             if (false !== strpos($head, "\r\n\r\n")) {
                 $headerParts = explode("\r\n\r\n", $head);
                 $head = $headerParts[count($headerParts) - 1];
             }
             $head = $this->processHeaders($head);
             $headers = $head['headers'];
             $cookies = $head['cookies'];
         } else {
             $headers = array();
             $cookies = '';
             $body = '';
             $this->error('An undefined error occured');
             return false;
         }
         if (isset($this->cookieArray['PHPSESSID']) && $this->cookieArray['PHPSESSID']) {
             $_SESSION[$this->sid]['sessid'] = $this->cookieArray['PHPSESSID'];
         }
         if ($cookies) {
             $this->debug(0, 'Cookie after:' . print_r($cookies, true));
             if (!isset($_SESSION[$this->sid])) {
                 $_SESSION[$this->sid] = array();
             }
             if (isset($_SESSION[$this->sid]['sessid'])) {
                 if (!strstr($cookies, 'PHPSESSID') && $cookies) {
                     $cookies .= ';' . $_SESSION[$this->sid]['sessid'];
                 } elseif (!strstr($cookies, 'PHPSESSID')) {
                     $cookies = $_SESSION[$this->sid]['sessid'];
                 }
             }
             $_SESSION[$this->sid]['cookies'] = $cookies;
         }
         //echo '<br />cookie after='.print_r($_SESSION[$this->sid]['cookies'],true).'=';
         if (is_array($cookies)) {
             $this->debug(0, 'Cookie after:' . print_r($cookies, true));
         }
         curl_close($ch);
         //remove temporary upload files
         if (count($newfiles) > 0) {
             foreach ($newfiles as $file) {
                 unlink($file);
             }
         }
         $this->headers = $headers;
         $this->data = $data;
         $this->cookies = $cookies;
         $this->body = $body;
         if ($headers['content-type']) {
             $this->type = $headers['content-type'];
         }
         $this->cookies = apply_filters('bridgeHttpRequest_post', $this->cookies);
         $this->debug(0, 'Call process completed in ' . $this->time('delta') . ' microseconds');
         if ($this->follow && isset($headers['location']) && $headers['location']) {
             //echo '<br />redirect to:'.print_r($headers,true);
             //echo '<br />protocol='.$this->_protocol;
             //echo '<br />path='.$this->_path;
             $redir = $headers['location'];
             if ($this->os() == 'WINDOWS') {
                 if (strpos($redir, $this->_protocol . '://' . $this->_host . $this->_path) === 0) {
                     //do nothing
                 } elseif (strstr($this->_protocol . '://' . $this->_host . $redir, $this->_protocol . '://' . $this->_host . $this->_path)) {
                     $new_redir = $this->_protocol . '://' . $this->_host . $this->_path;
                     if (strstr($new_redir, $redir) === false) {
                         $new_redir .= $redir;
                     }
                     $redir = $new_redir;
                 } elseif (!strstr($redir, $this->_host)) {
                     $redir = $this->_protocol . '://' . $this->_host . $this->_path . $redir;
                 }
             } else {
                 if (strpos($redir, $this->_protocol . '://' . $this->_host . $this->_path) === 0) {
                     //do nothing
                 } elseif (strstr($this->_protocol . '://' . $this->_host . $redir, $this->_protocol . '://' . $this->_host . $this->_path)) {
                     $redir = $this->_protocol . '://' . $this->_host . $redir;
                 } elseif ((strpos($redir, 'http://') === 0 || strpos($redir, 'https://') === 0) && !strstr($redir, $this->_host)) {
                     $this->redirect = true;
                     return $redir;
                 } elseif (!strstr($redir, $this->_host)) {
                     $redir = $this->_protocol . '://' . $this->_host . $this->_path . $redir;
                 }
             }
             $fwd = $this->forceWithRedirectToString($redir);
             if ($fwd) {
                 if (strstr($redir, '&')) {
                     $redir .= '&';
                 } elseif (strstr($redir, '?')) {
                     $redir .= '&';
                 } else {
                     $redir .= '?';
                 }
                 $redir .= $fwd;
             }
             $this->debug(0, 'Redirect to: ' . $redir);
             if (strstr($redir, 'viewinvoice.php')) {
                 $newRedir = cc_whmcs_bridge_parse_url($redir);
                 if ($redir != $newRedir) {
                     header('Location:' . $newRedir);
                     die;
                 }
             } else {
                 if (strstr($redir, 'cpsess')) {
                     header('location: ' . $redir);
                     die;
                 } else {
                     if (strstr($redir, 'custom_page=reissue') || strstr($redir, 'custom_page=manage_validation') || strstr($url, 'dologin') !== false && !isset($this->post['bg'])) {
                         $newRedir = cc_whmcs_bridge_parse_url($redir);
                         if ($wordpressPageName) {
                             $p = $wordpressPageName;
                         } else {
                             $p = '/';
                         }
                         $this->debug(0, 'Processing redirect...');
                         if (strstr($url, 'dologin') !== false && class_exists('wpusers')) {
                             $this->debug(0, 'Logging in to WordPress with ' . $this->post['username'] . '/' . $this->post['password']);
                             $wpusers = new wpusers();
                             $wpusers->loginWpUser($this->post['username'], $this->post['password']);
                         }
                         if (get_option('cc_whmcs_bridge_permalinks') && function_exists('cc_whmcs_bridge_parser_with_permalinks')) {
                             $f[] = '/.*\\/([a-zA-Z\\_]*?).php.(.*?)/';
                             $r[] = get_option('home') . $p . '/$1?$2';
                             $f[] = "/([a-zA-Z0-9\\_]*?).php.(.*?)/";
                             $r[] = get_option('home') . $p . '/$1?$2';
                         } else {
                             $f[] = '/.*\\/([a-zA-Z\\_]*?).php.(.*?)/';
                             $r[] = get_option('home') . $p . '?ccce=$1&$2';
                             $f[] = "/([a-zA-Z0-9\\_]*?).php.(.*?)/";
                             $r[] = get_option('home') . $p . '?ccce=$1&$2';
                         }
                         $this->debug(0, 'Location: ' . $newRedir);
                         $newRedir = preg_replace($f, $r, $newRedir, -1, $count);
                         header('Location:' . $newRedir);
                         die;
                     }
                 }
             }
             if (!$this->repost) {
                 $this->post = array();
             }
             $this->countRedirects++;
             if ($this->countRedirects < 10) {
                 //if ($redir != $url) {
                 return $this->connect($redir, $withHeaders, $withCookies);
                 //}
             } else {
                 $this->error('ERROR: Too many redirects ' . $url . ' > ' . $headers['location'], E_USER_ERROR);
                 return false;
             }
         }
         if (is_numeric($cache_setting) && $cache_setting > 0 && (substr($url, -4) == '.css' || substr($url, -3) == '.js' || substr($url, -4) == '.png' || substr($url, -4) == '.jpg' || substr($url, -5) == '.jpeg' || substr($url, -4) == '.gif')) {
             $cache_dir = dirname(__FILE__) . '/../cache/';
             if (file_exists($cache_dir) && is_writable($cache_dir)) {
                 $filename = md5($url) . '_' . strtotime('+' . $cache_setting . ' minutes');
                 file_put_contents($cache_dir . $filename, $body);
                 $this->debug(0, 'Cache written for ' . $url);
             }
         }
         return $body;
     } else {
         return $body;
     }
 }
Example #2
0
function cc_whmcs_bridge_init()
{
    ob_start();
    if (function_exists('cc_whmcsbridge_sso_session')) {
        cc_whmcsbridge_sso_session();
    }
    if (!session_id()) {
        @session_start();
    }
    register_sidebars(1, array('name' => 'WHMCS Top Page Widget Area', 'id' => 'whmcs-top-page'));
    //register_sidebars(1,array('name'=>'WHMCS Bottom Page Widget Area','id'=>'whmcs-top-page',));
    if (get_option('cc_whmcs_bridge_jquery') == 'wp') {
        wp_enqueue_script(array('jquery', 'jquery-ui', 'jquery-ui-slider', 'jquery-ui-button'));
    }
    if (is_admin() && isset($_REQUEST['page']) && $_REQUEST['page'] == 'cc-ce-bridge-cp') {
        wp_enqueue_script(array('jquery-ui-tabs'));
        wp_enqueue_style('jquery-style', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/flick/jquery-ui.css');
    }
}