*/
namespace Friendica\Util;
-use Friendica\App;
use Friendica\Core\Addon;
-use Friendica\Core\L10n;
use Friendica\Core\System;
use Friendica\Core\Config;
-use Friendica\Network\Probe;
-use Friendica\Object\Image;
-use Friendica\Util\XML;
+use Friendica\Network\Curl;
use DOMDocument;
use DomXPath;
class Network
{
+ /**
+ * @var Curl The latest Curl output
+ */
+ private static $curl;
+
+ /**
+ * Returns the latest Curl output
+ *
+ * @return Curl The latest Curl output
+ */
+ public static function getCurl()
+ {
+ if (empty(self::$curl)) {
+ self::$curl = new Curl();
+ }
+
+ return self::$curl;
+ }
+
/**
* Curl wrapper
*
*
* @return string The fetched content
*/
- public static function fetchUrl($url, $binary = false, &$redirects = 0, $timeout = 0, $accept_content = null, $cookiejar = 0)
+ public static function fetchUrl($url, $binary = false, &$redirects = 0, $timeout = 0, $accept_content = null, $cookiejar = '')
{
$ret = self::fetchUrlFull($url, $binary, $redirects, $timeout, $accept_content, $cookiejar);
*
* @return array 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 = 0)
+ public static function fetchUrlFull($url, $binary = false, &$redirects = 0, $timeout = 0, $accept_content = null, $cookiejar = '')
{
return self::curl(
$url,
}
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- @curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
+ @curl_setopt($ch, CURLOPT_USERAGENT, $a->getUserAgent());
$range = intval(Config::get('system', 'curl_range_bytes', 0));
@curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
}
- $a->set_curl_code(0);
-
// don't let curl abort the entire application
// if it throws any errors.
$base = substr($base, strlen($chunk));
}
- $a->set_curl_code($http_code);
- $a->set_curl_content_type($curl_info['content_type']);
- $a->set_curl_headers($header);
+ self::$curl = new Curl($http_code, (isset($curl_info['content_type']) ? $curl_info['content_type'] : ''), $header);
if ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
$new_location_info = @parse_url($curl_info['redirect_url']);
}
}
- $a->set_curl_code($http_code);
- $a->set_curl_content_type($curl_info['content_type']);
+ self::$curl->setCode($http_code);
+ if (isset($curl_info['content_type'])) {
+ self::$curl->setContentType($curl_info['content_type']);
+ }
$rc = intval($http_code);
$ret['return_code'] = $rc;
@curl_close($ch);
- $a->save_timestamp($stamp1, 'network');
+ $a->saveTimestamp($stamp1, 'network');
return($ret);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
- curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
+ curl_setopt($ch, CURLOPT_USERAGENT, $a->getUserAgent());
if (Config::get('system', 'ipv4_resolve', false)) {
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
}
}
- $a->set_curl_code(0);
+ self::getCurl()->setCode(0);
// don't let curl abort the entire application
// if it throws any errors.
}
}
- $a->set_curl_code($http_code);
+ self::getCurl()->setCode($http_code);
$body = substr($s, strlen($header));
- $a->set_curl_headers($header);
+ self::getCurl()->setHeaders($header);
curl_close($ch);
- $a->save_timestamp($stamp1, 'network');
+ $a->saveTimestamp($stamp1, 'network');
logger('post_url: end ' . $url, LOGGER_DATA);
public static function isUrlBlocked($url)
{
$host = @parse_url($url, PHP_URL_HOST);
- if (! $host) {
- return true;
+ if (!$host) {
+ return false;
}
$domain_blocklist = Config::get('system', 'blocklist', []);
- if (! $domain_blocklist) {
+ if (!$domain_blocklist) {
return false;
}
$url = self::stripTrackingQueryParams($url);
if ($depth > 10) {
- return($url);
+ return $url;
}
$url = trim($url, "'");
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
+ curl_setopt($ch, CURLOPT_USERAGENT, $a->getUserAgent());
curl_exec($ch);
$curl_info = @curl_getinfo($ch);
$http_code = $curl_info['http_code'];
curl_close($ch);
- $a->save_timestamp($stamp1, "network");
+ $a->saveTimestamp($stamp1, "network");
if ($http_code == 0) {
- return($url);
+ return $url;
}
- if ((($curl_info['http_code'] == "301") || ($curl_info['http_code'] == "302"))
- && (($curl_info['redirect_url'] != "") || ($curl_info['location'] != ""))
- ) {
- if ($curl_info['redirect_url'] != "") {
- return(self::finalUrl($curl_info['redirect_url'], ++$depth, $fetchbody));
- } else {
- return(self::finalUrl($curl_info['location'], ++$depth, $fetchbody));
+ if (in_array($http_code, ['301', '302'])) {
+ if (!empty($curl_info['redirect_url'])) {
+ return self::finalUrl($curl_info['redirect_url'], ++$depth, $fetchbody);
+ } elseif (!empty($curl_info['location'])) {
+ return self::finalUrl($curl_info['location'], ++$depth, $fetchbody);
}
}
// if the file is too large then exit
if ($curl_info["download_content_length"] > 1000000) {
- return($url);
+ return $url;
}
// if it isn't a HTML file then exit
- if (($curl_info["content_type"] != "") && !strstr(strtolower($curl_info["content_type"]), "html")) {
- return($url);
+ if (!empty($curl_info["content_type"]) && !strstr(strtolower($curl_info["content_type"]), "html")) {
+ return $url;
}
$stamp1 = microtime(true);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
+ curl_setopt($ch, CURLOPT_USERAGENT, $a->getUserAgent());
$body = curl_exec($ch);
curl_close($ch);
- $a->save_timestamp($stamp1, "network");
+ $a->saveTimestamp($stamp1, "network");
if (trim($body) == "") {
- return($url);
+ return $url;
}
// Check for redirect in meta elements
$pathinfo = explode(";", $path);
foreach ($pathinfo as $value) {
if (substr(strtolower($value), 0, 4) == "url=") {
- return(self::finalUrl(substr($value, 4), ++$depth));
+ return self::finalUrl(substr($value, 4), ++$depth);
}
}
}