]> git.mxchange.org Git - friendica.git/blobdiff - include/network.php
Merge pull request #2016 from fabrixxm/template_vars_hook
[friendica.git] / include / network.php
index a07507da1fa09a51a277400d52b70d58fa1bffec..d0217e2a084fb45019788a7f49f6c89d991bc11d 100644 (file)
@@ -9,6 +9,47 @@
 if(! function_exists('fetch_url')) {
 function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_content=Null, $cookiejar = 0) {
 
+       $ret = z_fetch_url(
+               $url,
+               $binary,
+               $redirects,
+               array('timeout'=>$timeout,
+               'accept_content'=>$accept_content,
+               'cookiejar'=>$cookiejar
+               ));
+
+       return($ret['body']);
+}}
+
+if(!function_exists('z_fetch_url')){
+/**
+ * @brief fetches an URL.
+ *
+ * @param string $url
+ *    URL to fetch
+ * @param boolean $binary default false
+ *    TRUE if asked to return binary results (file download)
+ * @param int $redirects default 0
+ *    internal use, recursion counter
+ * @param array $opts (optional parameters) assoziative array with:
+ *  * \b accept_content => supply Accept: header with 'accept_content' as the value
+ *  * \b timeout => int seconds, default system config value or 60 seconds
+ *  * \b http_auth => username:password
+ *  * \b novalidate => do not validate SSL certs, default is to validate using our CA list
+ *  * \b nobody => only return the header
+ *     * \b cookiejar => path to cookie jar file
+ *
+ * @return array an assoziative array with:
+ *  * \e int \b return_code => HTTP return code or 0 if timeout or failure
+ *  * \e boolean \b success => boolean true (if HTTP 2xx result) or false
+ *  * \e string \b header => HTTP headers
+ *  * \e string \b body => fetched content
+ */
+function z_fetch_url($url,$binary = false, &$redirects = 0, $opts=array()) {
+
+       $ret = array('return_code' => 0, 'success' => false, 'header' => "", 'body' => "");
+
+
        $stamp1 = microtime(true);
 
        $a = get_app();
@@ -19,18 +60,18 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
 
        @curl_setopt($ch, CURLOPT_HEADER, true);
 
-       if($cookiejar) {
-               curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
-               curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);
+       if(x($opts,"cookiejar")) {
+               curl_setopt($ch, CURLOPT_COOKIEJAR, $opts["cookiejar"]);
+               curl_setopt($ch, CURLOPT_COOKIEFILE, $opts["cookiejar"]);
        }
 
 //  These settings aren't needed. We're following the location already.
 //     @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 //     @curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
 
-       if (!is_null($accept_content)){
+       if (x($opts,'accept_content')){
                curl_setopt($ch,CURLOPT_HTTPHEADER, array (
-                       "Accept: " . $accept_content
+                       "Accept: " . $opts['accept_content']
                ));
        }
 
@@ -38,10 +79,16 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
        @curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
 
 
-       if(intval($timeout)) {
-               @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
+
+       if(x($opts,'headers')){
+               @curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['headers']);
        }
-       else {
+       if(x($opts,'nobody')){
+               @curl_setopt($ch, CURLOPT_NOBODY, $opts['nobody']);
+       }
+       if(x($opts,'timeout')){
+               @curl_setopt($ch, CURLOPT_TIMEOUT, $opts['timeout']);
+       } else {
                $curl_time = intval(get_config('system','curl_timeout'));
                @curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
        }
@@ -72,7 +119,7 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
 
        $base = $s;
        $curl_info = @curl_getinfo($ch);
-       @curl_close($ch);
+
        $http_code = $curl_info['http_code'];
        logger('fetch_url '.$url.': '.$http_code." ".$s, LOGGER_DATA);
        $header = '';
@@ -103,19 +150,40 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
                        $newurl = $old_location_info["scheme"]."://".$old_location_info["host"].$newurl;
                if (filter_var($newurl, FILTER_VALIDATE_URL)) {
                        $redirects++;
-                       return fetch_url($newurl,$binary,$redirects,$timeout,$accept_content,$cookiejar);
+                       @curl_close($ch);
+                       return z_fetch_url($newurl,$binary, $redirects, $opts);
                }
        }
 
+
        $a->set_curl_code($http_code);
        $a->set_curl_content_type($curl_info['content_type']);
 
        $body = substr($s,strlen($header));
        $a->set_curl_headers($header);
 
+
+
+       $rc = intval($http_code);
+       $ret['return_code'] = $rc;
+       $ret['success'] = (($rc >= 200 && $rc <= 299) ? true : false);
+       if(! $ret['success']) {
+               $ret['error'] = curl_error($ch);
+               $ret['debug'] = $curl_info;
+               logger('z_fetch_url: error: ' . $url . ': ' . $ret['error'], LOGGER_DEBUG);
+               logger('z_fetch_url: debug: ' . print_r($curl_info,true), LOGGER_DATA);
+       }
+       $ret['body'] = substr($s,strlen($header));
+       $ret['header'] = $header;
+       if(x($opts,'debug')) {
+               $ret['debug'] = $curl_info;
+       }
+       @curl_close($ch);
+
        $a->save_timestamp($stamp1, "network");
 
-       return($body);
+       return($ret);
+
 }}
 
 // post request to $url. $params is an array of post variables.
@@ -240,16 +308,25 @@ function xml_status($st, $message = '') {
 
 
 if(! function_exists('http_status_exit')) {
-function http_status_exit($val) {
-
+function http_status_exit($val, $description = array()) {
     $err = '';
-       if($val >= 400)
+       if($val >= 400) {
                $err = 'Error';
+               if (!isset($description["title"]))
+                       $description["title"] = $err." ".$val;
+       }
        if($val >= 200 && $val < 300)
                $err = 'OK';
 
        logger('http_status_exit ' . $val);
        header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
+
+       if (isset($description["title"])) {
+               $tpl = get_markup_template('http_status.tpl');
+               echo replace_macros($tpl, array('$title' => $description["title"],
+                                               '$description' => $description["description"]));
+       }
+
        killme();
 
 }}