]> git.mxchange.org Git - friendica.git/blobdiff - include/network.php
Redirects sometimes only seems to send the hostname and no path. "fetch_url" respects...
[friendica.git] / include / network.php
index 446413cd834c5cd81ebd4e02ca6d8861c9f2e6fc..df23c2549c8cff319fdc466c147bb21dfa96329c 100644 (file)
@@ -14,15 +14,19 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
                return false;
 
        @curl_setopt($ch, CURLOPT_HEADER, true);
-       
+
+       @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+       @curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
+
        if (!is_null($accept_content)){
                curl_setopt($ch,CURLOPT_HTTPHEADER, array (
                        "Accept: " . $accept_content
                ));
        }
-       
+
        @curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
-       @curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
+       //@curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
+       @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
 
 
        if(intval($timeout)) {
@@ -59,7 +63,6 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
        $base = $s;
        $curl_info = @curl_getinfo($ch);
        $http_code = $curl_info['http_code'];
-
 //     logger('fetch_url:' . $http_code . ' data: ' . $s);
        $header = '';
 
@@ -73,24 +76,30 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
        }
 
        if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
-        $matches = array();
-        preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
-        $newurl = trim(array_pop($matches));
+               $new_location_info = @parse_url($curl_info["redirect_url"]);
+               $old_location_info = @parse_url($curl_info["url"]);
+
+               $newurl = $curl_info["redirect_url"];
+
+               if (($new_location_info["path"] == "") AND ($new_location_info["host"] != ""))
+                       $newurl = $new_location_info["scheme"]."://".$new_location_info["host"].$old_location_info["path"];
+
+               //$matches = array();
+               //preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
+               //$newurl = trim(array_pop($matches));
                if(strpos($newurl,'/') === 0)
                        $newurl = $url . $newurl;
-        $url_parsed = @parse_url($newurl);
-        if (isset($url_parsed)) {
-            $redirects++;
-            return fetch_url($newurl,$binary,$redirects,$timeout);
-        }
-    }
+               $url_parsed = @parse_url($newurl);
+               if (isset($url_parsed)) {
+                       $redirects++;
+                       return fetch_url($newurl,$binary,$redirects,$timeout);
+               }
+       }
 
        $a->set_curl_code($http_code);
 
        $body = substr($s,strlen($header));
-
        $a->set_curl_headers($header);
-
        @curl_close($ch);
        return($body);
 }}
@@ -101,7 +110,7 @@ if(! function_exists('post_url')) {
 function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) {
        $a = get_app();
        $ch = curl_init($url);
-       if(($redirects > 8) || (! $ch)) 
+       if(($redirects > 8) || (! $ch))
                return false;
 
        curl_setopt($ch, CURLOPT_HEADER, true);
@@ -607,6 +616,9 @@ function validate_url(&$url) {
 if(! function_exists('validate_email')) {
 function validate_email($addr) {
 
+       if(get_config('system','disable_email_validation'))
+               return true;
+
        if(! strpos($addr,'@'))
                return false;
        $h = substr($addr,strpos($addr,'@') + 1);
@@ -793,23 +805,42 @@ function add_fcontact($arr,$update = false) {
 }
 
 
-function scale_external_images($s,$include_link = true) {
+function scale_external_images($s, $include_link = true, $scale_replace = false) {
 
        $a = get_app();
 
+       // Picture addresses can contain special characters
+       $s = htmlspecialchars_decode($s);
+
        $matches = null;
-       $c = preg_match_all('/\[img\](.*?)\[\/img\]/ism',$s,$matches,PREG_SET_ORDER);
+       $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism',$s,$matches,PREG_SET_ORDER);
        if($c) {
                require_once('include/Photo.php');
                foreach($matches as $mtch) {
                        logger('scale_external_image: ' . $mtch[1]);
+
                        $hostname = str_replace('www.','',substr($a->get_baseurl(),strpos($a->get_baseurl(),'://')+3));
                        if(stristr($mtch[1],$hostname))
                                continue;
-                       $i = fetch_url($mtch[1]);
+
+                       // $scale_replace, if passed, is an array of two elements. The
+                       // first is the name of the full-size image. The second is the
+                       // name of a remote, scaled-down version of the full size image.
+                       // This allows Friendica to display the smaller remote image if
+                       // one exists, while still linking to the full-size image
+                       if($scale_replace)
+                               $scaled = str_replace($scale_replace[0], $scale_replace[1], $mtch[1]);
+                       else
+                               $scaled = $mtch[1];
+                       $i = fetch_url($scaled);
+
+                       $cachefile = get_cachefile(hash("md5", $scaled));
+                       if ($cachefile != '')
+                               file_put_contents($cachefile, $i);
+
                        // guess mimetype from headers or filename
                        $type = guess_image_type($mtch[1],true);
-                       
+
                        if($i) {
                                $ph = new Photo($i, $type);
                                if($ph->is_valid()) {
@@ -822,7 +853,7 @@ function scale_external_images($s,$include_link = true) {
                                                $new_width = $ph->getWidth();
                                                $new_height = $ph->getHeight();
                                                logger('scale_external_images: ' . $orig_width . '->' . $new_width . 'w ' . $orig_height . '->' . $new_height . 'h' . ' match: ' . $mtch[0], LOGGER_DEBUG);
-                                               $s = str_replace($mtch[0],'[img=' . $new_width . 'x' . $new_height. ']' . $mtch[1] . '[/img]'
+                                               $s = str_replace($mtch[0],'[img=' . $new_width . 'x' . $new_height. ']' . $scaled . '[/img]'
                                                        . "\n" . (($include_link) 
                                                                ? '[url=' . $mtch[1] . ']' . t('view full size') . '[/url]' . "\n"
                                                                : ''),$s);
@@ -832,6 +863,9 @@ function scale_external_images($s,$include_link = true) {
                        }
                }
        }
+
+       // replace the special char encoding
+       $s = htmlspecialchars($s,ENT_NOQUOTES,'UTF-8');
        return $s;
 }
 
@@ -892,7 +926,7 @@ function fix_contact_ssl_policy(&$contact,$new_policy) {
  * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
  * Examples: $array =  xml2array(file_get_contents('feed.xml'));
  *              $array =  xml2array(file_get_contents('feed.xml', true, 1, 'attribute'));
- */ 
+ */
 
 function xml2array($contents, $namespaces = true, $get_attributes=1, $priority = 'attribute') {
     if(!$contents) return array();
@@ -950,7 +984,7 @@ function xml2array($contents, $namespaces = true, $get_attributes=1, $priority =
 
         $result = array();
         $attributes_data = array();
-        
+
         if(isset($value)) {
             if($priority == 'tag') $result = $value;
             else $result['value'] = $value; // Put the value in a assoc array if we are in the 'Attribute' mode
@@ -966,7 +1000,7 @@ function xml2array($contents, $namespaces = true, $get_attributes=1, $priority =
 
         // See tag status and do the needed.
                if($namespaces && strpos($tag,':')) {
-                       $namespc = substr($tag,0,strrpos($tag,':')); 
+                       $namespc = substr($tag,0,strrpos($tag,':'));
                        $tag = strtolower(substr($tag,strlen($namespc)+1));
                        $result['@namespace'] = $namespc;
                }
@@ -989,7 +1023,7 @@ function xml2array($contents, $namespaces = true, $get_attributes=1, $priority =
                 } else { // This section will make the value an array if multiple tags with the same name appear together
                     $current[$tag] = array($current[$tag],$result); // This will combine the existing item and the new item together to make an array
                     $repeated_tag_index[$tag.'_'.$level] = 2;
-                    
+
                     if(isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
                         $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                         unset($current[$tag.'_attr']);
@@ -1012,7 +1046,7 @@ function xml2array($contents, $namespaces = true, $get_attributes=1, $priority =
 
                     // ...push the new element into that array.
                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
-                    
+
                     if($priority == 'tag' and $get_attributes and $attributes_data) {
                         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                     }
@@ -1023,11 +1057,11 @@ function xml2array($contents, $namespaces = true, $get_attributes=1, $priority =
                     $repeated_tag_index[$tag.'_'.$level] = 1;
                     if($priority == 'tag' and $get_attributes) {
                         if(isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
-                            
+
                             $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                             unset($current[$tag.'_attr']);
                         }
-                        
+
                         if($attributes_data) {
                             $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                         }
@@ -1040,6 +1074,6 @@ function xml2array($contents, $namespaces = true, $get_attributes=1, $priority =
             $current = &$parent[$level-1];
         }
     }
-    
+
     return($xml_array);
-}  
+}