]> git.mxchange.org Git - friendica.git/blobdiff - include/network.php
The CSS code for shared messages is moved from "vier" to the global.css, so that...
[friendica.git] / include / network.php
index 4bc50974b0786dc8d062d3dec959884f8f17f605..310b988cb3460fcee23b4af08be4e83ae6688d8c 100644 (file)
@@ -189,7 +189,7 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0)
                $base = substr($base,strlen($chunk));
        }
 
-       if($http_code == 301 || $http_code == 302 || $http_code == 303) {
+       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));
@@ -1105,3 +1105,95 @@ function xml2array($contents, $namespaces = true, $get_attributes=1, $priority =
 
     return($xml_array);
 }
+
+function original_url($url, $depth=1, $fetchbody = false) {
+        if ($depth > 10)
+                return($url);
+
+        $url = trim($url, "'");
+
+        $siteinfo = array();
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_HEADER, 1);
+
+        if ($fetchbody)
+                curl_setopt($ch, CURLOPT_NOBODY, 0);
+        else
+                curl_setopt($ch, CURLOPT_NOBODY, 1);
+
+        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0');
+
+        $header = curl_exec($ch);
+        $curl_info = @curl_getinfo($ch);
+        $http_code = $curl_info['http_code'];
+        curl_close($ch);
+
+        if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302"))
+                AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
+                if ($curl_info['redirect_url'] != "")
+                        return(original_url($curl_info['redirect_url'], ++$depth, $fetchbody));
+                else
+                        return(original_url($curl_info['location'], ++$depth, $fetchbody));
+        }
+
+        $pos = strpos($header, "\r\n\r\n");
+
+        if ($pos)
+                $body = trim(substr($header, $pos));
+        else
+                $body = $header;
+
+        if (trim($body) == "")
+                return(original_url($url, ++$depth, true));
+
+        $doc = new DOMDocument();
+        @$doc->loadHTML($body);
+
+        $xpath = new DomXPath($doc);
+
+        $list = $xpath->query("//meta[@content]");
+        foreach ($list as $node) {
+                $attr = array();
+                if ($node->attributes->length)
+                        foreach ($node->attributes as $attribute)
+                                $attr[$attribute->name] = $attribute->value;
+
+                if (@$attr["http-equiv"] == 'refresh') {
+                        $path = $attr["content"];
+                        $pathinfo = explode(";", $path);
+                        $content = "";
+                        foreach ($pathinfo AS $value)
+                                if (substr(strtolower($value), 0, 4) == "url=")
+                                        return(original_url(substr($value, 4), ++$depth));
+                }
+        }
+
+        return($url);
+}
+
+if (!function_exists('short_link')) {
+function short_link($url) {
+       require_once('library/slinky.php');
+       $slinky = new Slinky($url);
+       $yourls_url = get_config('yourls','url1');
+       if ($yourls_url) {
+               $yourls_username = get_config('yourls','username1');
+               $yourls_password = get_config('yourls', 'password1');
+               $yourls_ssl = get_config('yourls', 'ssl1');
+               $yourls = new Slinky_YourLS();
+               $yourls->set('username', $yourls_username);
+               $yourls->set('password', $yourls_password);
+               $yourls->set('ssl', $yourls_ssl);
+               $yourls->set('yourls-url', $yourls_url);
+               $slinky->set_cascade( array($yourls, new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL()));
+       } else {
+               // setup a cascade of shortening services
+               // try to get a short link from these services
+               // in the order ur1.ca, trim, id.gd, tinyurl
+               $slinky->set_cascade(array(new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL()));
+       }
+       return $slinky->short();
+}};