]> git.mxchange.org Git - friendica.git/blobdiff - include/network.php
The "vier"-theme is now working again. But there is still work needed
[friendica.git] / include / network.php
old mode 100644 (file)
new mode 100755 (executable)
index e89eb94..9e1ed20
@@ -17,12 +17,12 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
        
        if (!is_null($accept_content)){
                curl_setopt($ch,CURLOPT_HTTPHEADER, array (
-                       "Accept: "+$accept_content
+                       "Accept: " . $accept_content
                ));
        }
        
        @curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
-       @curl_setopt($ch, CURLOPT_USERAGENT, "Friendika");
+       @curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
 
 
        if(intval($timeout)) {
@@ -60,6 +60,7 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
        $curl_info = @curl_getinfo($ch);
        $http_code = $curl_info['http_code'];
 
+//     logger('fetch_url:' . $http_code . ' data: ' . $s);
        $header = '';
 
        // Pull out multiple headers, e.g. proxy and continuation headers
@@ -74,11 +75,13 @@ 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);
-        $url = trim(array_pop($matches));
-        $url_parsed = @parse_url($url);
+        $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($url,$binary,$redirects,$timeout);
+            return fetch_url($newurl,$binary,$redirects,$timeout);
         }
     }
 
@@ -105,7 +108,7 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0)
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
-       curl_setopt($ch, CURLOPT_USERAGENT, "Friendika");
+       curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
 
        if(intval($timeout)) {
                curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
@@ -163,11 +166,13 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0)
        if($http_code == 301 || $http_code == 302 || $http_code == 303) {
         $matches = array();
         preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
-        $url = trim(array_pop($matches));
-        $url_parsed = @parse_url($url);
+        $newurl = trim(array_pop($matches));
+               if(strpos($newurl,'/') === 0)
+                       $newurl = $url . $newurl;
+        $url_parsed = @parse_url($newurl);
         if (isset($url_parsed)) {
             $redirects++;
-            return post_url($url,$params,$headers,$redirects,$timeout);
+            return fetch_url($newurl,$binary,$redirects,$timeout);
         }
     }
        $a->set_curl_code($http_code);
@@ -298,7 +303,7 @@ function webfinger_dfrn($s,&$hcard) {
 
 
 if(! function_exists('webfinger')) {
-function webfinger($s) {
+function webfinger($s, $debug = false) {
        $host = '';
        if(strstr($s,'@')) {
                $host = substr($s,strpos($s,'@') + 1);
@@ -323,7 +328,7 @@ function webfinger($s) {
 }}
 
 if(! function_exists('lrdd')) {
-function lrdd($uri) {
+function lrdd($uri, $debug = false) {
 
        $a = get_app();
 
@@ -359,6 +364,9 @@ function lrdd($uri) {
 
        logger('lrdd: host_meta: ' . $xml, LOGGER_DATA);
 
+       if(! stristr($xml,'<xrd'))
+               return array();
+
        $h = parse_xml_string($xml);
        if(! $h)
                return array();
@@ -535,6 +543,9 @@ function fetch_xrd_links($url) {
        if ((! $xml) || (! stristr($xml,'<xrd')))
                return array();
 
+       // fix diaspora's bad xml
+       $xml = str_replace(array('href=&quot;','&quot;/>'),array('href="','"/>'),$xml);
+
        $h = parse_xml_string($xml);
        if(! $h)
                return array();
@@ -576,6 +587,9 @@ function fetch_xrd_links($url) {
 
 if(! function_exists('validate_url')) {
 function validate_url(&$url) {
+       // no naked subdomains
+       if(strpos($url,'.') === false)
+               return false;
        if(substr($url,0,4) != 'http')
                $url = 'http://' . $url;
        $h = @parse_url($url);
@@ -770,3 +784,43 @@ function add_fcontact($arr,$update = false) {
 
        return $r;
 }
+
+
+function scale_external_images($s,$include_link = true) {
+
+       $a = get_app();
+
+       $matches = null;
+       $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]);
+                       if($i) {
+                               $ph = new Photo($i);
+                               if($ph->is_valid()) {
+                                       $orig_width = $ph->getWidth();
+                                       $orig_height = $ph->getHeight();
+
+                                       if($orig_width > 640 || $orig_height > 640) {
+
+                                               $ph->scaleImage(640);
+                                               $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]'
+                                                       . "\n" . (($include_link) 
+                                                               ? '[url=' . $mtch[1] . ']' . t('view full size') . '[/url]' . "\n"
+                                                               : ''),$s);
+                                               logger('scale_external_images: new string: ' . $s, LOGGER_DEBUG);
+                                       }
+                               }
+                       }
+               }
+       }
+       return $s;
+}