]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Network.php
Merge pull request #6452 from annando/composer
[friendica.git] / src / Util / Network.php
index f8e4ee9e5bfe246292e40cb0d60175bb5ad03c92..0acad3e082af14ffeec1684618d9099568803d1d 100644 (file)
@@ -5,9 +5,11 @@
 namespace Friendica\Util;
 
 use Friendica\Core\Addon;
+use Friendica\Core\Logger;
 use Friendica\Core\System;
 use Friendica\Core\Config;
-use Friendica\Network\Curl;
+use Friendica\Network\CurlResult;
+use Friendica\Util\Strings;
 use DOMDocument;
 use DomXPath;
 
@@ -53,7 +55,7 @@ class Network
         * @param string  $accept_content supply Accept: header with 'accept_content' as the value
         * @param string  $cookiejar      Path to cookie jar file
         *
-        * @return Curl With all relevant information, 'body' contains the actual fetched content.
+        * @return CurlResult With all relevant information, 'body' contains the actual fetched content.
         */
        public static function fetchUrlFull($url, $binary = false, &$redirects = 0, $timeout = 0, $accept_content = null, $cookiejar = '')
        {
@@ -82,8 +84,9 @@ class Network
         *                           'novalidate' => do not validate SSL certs, default is to validate using our CA list
         *                           'nobody' => only return the header
         *                           'cookiejar' => path to cookie jar file
+        *                           'header' => header array
         *
-        * @return Curl
+        * @return CurlResult
         */
        public static function curl($url, $binary = false, &$redirects = 0, $opts = [])
        {
@@ -91,7 +94,7 @@ class Network
 
                $stamp1 = microtime(true);
 
-               $a = get_app();
+               $a = \get_app();
 
                $parts = parse_url($url);
                $path_parts = explode('/', defaults($parts, 'path', ''));
@@ -106,19 +109,19 @@ class Network
                $url = self::unparseURL($parts);
 
                if (self::isUrlBlocked($url)) {
-                       logger('domain of ' . $url . ' is blocked', LOGGER_DATA);
-                       return Curl::createErrorCurl($url);
+                       Logger::log('domain of ' . $url . ' is blocked', Logger::DATA);
+                       return CurlResult::createErrorCurl($url);
                }
 
                $ch = @curl_init($url);
 
                if (($redirects > 8) || (!$ch)) {
-                       return Curl::createErrorCurl($url);
+                       return CurlResult::createErrorCurl($url);
                }
 
                @curl_setopt($ch, CURLOPT_HEADER, true);
 
-               if (x($opts, "cookiejar")) {
+               if (!empty($opts['cookiejar'])) {
                        curl_setopt($ch, CURLOPT_COOKIEJAR, $opts["cookiejar"]);
                        curl_setopt($ch, CURLOPT_COOKIEFILE, $opts["cookiejar"]);
                }
@@ -127,7 +130,7 @@ class Network
                //      @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                //      @curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
 
-               if (x($opts, 'accept_content')) {
+               if (!empty($opts['accept_content'])) {
                        curl_setopt(
                                $ch,
                                CURLOPT_HTTPHEADER,
@@ -135,6 +138,10 @@ class Network
                        );
                }
 
+               if (!empty($opts['header'])) {
+                       curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['header']);
+               }
+
                @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                @curl_setopt($ch, CURLOPT_USERAGENT, $a->getUserAgent());
 
@@ -149,15 +156,15 @@ class Network
                /// @todo  We could possibly set this value to "gzip" or something similar
                curl_setopt($ch, CURLOPT_ENCODING, '');
 
-               if (x($opts, 'headers')) {
+               if (!empty($opts['headers'])) {
                        @curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['headers']);
                }
 
-               if (x($opts, 'nobody')) {
+               if (!empty($opts['nobody'])) {
                        @curl_setopt($ch, CURLOPT_NOBODY, $opts['nobody']);
                }
 
-               if (x($opts, 'timeout')) {
+               if (!empty($opts['timeout'])) {
                        @curl_setopt($ch, CURLOPT_TIMEOUT, $opts['timeout']);
                } else {
                        $curl_time = Config::get('system', 'curl_timeout', 60);
@@ -208,15 +215,11 @@ class Network
                        $curl_info = @curl_getinfo($ch);
                }
 
-               if (curl_errno($ch) !== CURLE_OK) {
-                       logger('error fetching ' . $url . ': ' . curl_error($ch), LOGGER_INFO);
-               }
-
-               $curlResponse = new Curl($url, $s, $curl_info, curl_errno($ch), curl_error($ch));
+               $curlResponse = new CurlResult($url, $s, $curl_info, curl_errno($ch), curl_error($ch));
 
                if ($curlResponse->isRedirectUrl()) {
                        $redirects++;
-                       logger('curl: redirect ' . $url . ' to ' . $curlResponse->getRedirectUrl());
+                       Logger::log('curl: redirect ' . $url . ' to ' . $curlResponse->getRedirectUrl());
                        @curl_close($ch);
                        return self::curl($curlResponse->getRedirectUrl(), $binary, $redirects, $opts);
                }
@@ -237,25 +240,25 @@ class Network
         * @param integer $redirects Recursion counter for internal use - default = 0
         * @param integer $timeout   The timeout in seconds, default system config value or 60 seconds
         *
-        * @return Curl The content
+        * @return CurlResult The content
         */
        public static function post($url, $params, $headers = null, &$redirects = 0, $timeout = 0)
        {
                $stamp1 = microtime(true);
 
                if (self::isUrlBlocked($url)) {
-                       logger('post_url: domain of ' . $url . ' is blocked', LOGGER_DATA);
-                       return Curl::createErrorCurl($url);
+                       Logger::log('post_url: domain of ' . $url . ' is blocked', Logger::DATA);
+                       return CurlResult::createErrorCurl($url);
                }
 
-               $a = get_app();
+               $a = \get_app();
                $ch = curl_init($url);
 
                if (($redirects > 8) || (!$ch)) {
-                       return Curl::createErrorCurl($url);
+                       return CurlResult::createErrorCurl($url);
                }
 
-               logger('post_url: start ' . $url, LOGGER_DATA);
+               Logger::log('post_url: start ' . $url, Logger::DATA);
 
                curl_setopt($ch, CURLOPT_HEADER, true);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -314,11 +317,11 @@ class Network
                $base = $s;
                $curl_info = curl_getinfo($ch);
 
-               $curlResponse = new Curl($url, $s, $curl_info, curl_errno($ch), curl_error($ch));
+               $curlResponse = new CurlResult($url, $s, $curl_info, curl_errno($ch), curl_error($ch));
 
                if ($curlResponse->isRedirectUrl()) {
                        $redirects++;
-                       logger('post_url: redirect ' . $url . ' to ' . $curlResponse->getRedirectUrl());
+                       Logger::log('post_url: redirect ' . $url . ' to ' . $curlResponse->getRedirectUrl());
                        curl_close($ch);
                        return self::post($curlResponse->getRedirectUrl(), $params, $headers, $redirects, $timeout);
                }
@@ -327,7 +330,7 @@ class Network
 
                $a->saveTimestamp($stamp1, 'network');
 
-               logger('post_url: end ' . $url, LOGGER_DATA);
+               Logger::log('post_url: end ' . $url, Logger::DATA);
 
                return $curlResponse;
        }
@@ -359,7 +362,7 @@ class Network
                /// @TODO Really suppress function outcomes? Why not find them + debug them?
                $h = @parse_url($url);
 
-               if ((is_array($h)) && (@dns_get_record($h['host'], DNS_A + DNS_CNAME) || filter_var($h['host'], FILTER_VALIDATE_IP) )) {
+               if (!empty($h['host']) && (@dns_get_record($h['host'], DNS_A + DNS_CNAME) || filter_var($h['host'], FILTER_VALIDATE_IP) )) {
                        return $url;
                }
 
@@ -486,7 +489,7 @@ class Network
                }
 
                $str_allowed = Config::get('system', 'allowed_email', '');
-               if (!x($str_allowed)) {
+               if (empty($str_allowed)) {
                        return true;
                }
 
@@ -520,7 +523,7 @@ class Network
 
        public static function lookupAvatarByEmail($email)
        {
-               $avatar['size'] = 175;
+               $avatar['size'] = 300;
                $avatar['email'] = $email;
                $avatar['url'] = '';
                $avatar['success'] = false;
@@ -528,10 +531,10 @@ class Network
                Addon::callHooks('avatar_lookup', $avatar);
 
                if (! $avatar['success']) {
-                       $avatar['url'] = System::baseUrl() . '/images/person-175.jpg';
+                       $avatar['url'] = System::baseUrl() . '/images/person-300.jpg';
                }
 
-               logger('Avatar: ' . $avatar['email'] . ' ' . $avatar['url'], LOGGER_DEBUG);
+               Logger::log('Avatar: ' . $avatar['email'] . ' ' . $avatar['url'], Logger::DEBUG);
                return $avatar['url'];
        }
 
@@ -601,7 +604,7 @@ class Network
         */
        public static function finalUrl($url, $depth = 1, $fetchbody = false)
        {
-               $a = get_app();
+               $a = \get_app();
 
                $url = self::stripTrackingQueryParams($url);
 
@@ -716,8 +719,8 @@ class Network
                        return "";
                }
 
-               $url1 = normalise_link($url1);
-               $url2 = normalise_link($url2);
+               $url1 = Strings::normaliseLink($url1);
+               $url2 = Strings::normaliseLink($url2);
 
                $parts1 = parse_url($url1);
                $parts2 = parse_url($url2);
@@ -788,7 +791,7 @@ class Network
 
                $match .= $path;
 
-               return normalise_link($match);
+               return Strings::normaliseLink($match);
        }
 
        /**