Files
Kleeja/includes/FetchFile.php

195 lines
4.8 KiB
PHP
Raw Normal View History

2019-05-19 02:40:43 +03:00
<?php
/**
*
* @package Kleeja
2020-04-11 22:45:48 +02:00
* @copyright (c) 2007 Kleeja.net
* @license http://www.kleeja.net/license
2019-05-19 02:40:43 +03:00
*
*/
//no for directly open
if (! defined('IN_COMMON'))
{
exit;
}
class FetchFile
{
private $url;
2019-05-25 00:30:55 +03:00
private $timeout = 60;
2019-05-19 04:22:59 +03:00
private $destinationPath = '';
2019-05-25 00:30:55 +03:00
private $maxRedirects = 3;
private $binary = false;
2019-05-19 02:40:43 +03:00
public function __construct($url)
{
$this->url = $url;
}
public static function make($url)
{
return new static($url);
}
public function setTimeOut($seconds)
{
$this->timeout = $seconds;
return $this;
}
public function setDestinationPath($path)
{
$this->destinationPath = $path;
return $this;
}
public function setMaxRedirects($limit)
{
$this->maxRedirects = $limit;
return $this;
}
public function isBinaryFile($val)
{
$this->binary = $val;
return $this;
}
public function get()
{
$fetchType = '';
$allow_url_fopen = function_exists('ini_get')
? strtolower(@ini_get('allow_url_fopen'))
: strtolower(@get_cfg_var('allow_url_fopen'));
2019-05-19 04:23:18 +03:00
if (function_exists('curl_init'))
2019-05-19 02:40:43 +03:00
{
$fetchType = 'curl';
}
elseif (in_array($allow_url_fopen, ['on', 'true', '1']))
{
$fetchType = 'fopen';
}
session_write_close();
$result = null;
is_array($plugin_run_result = Plugins::getInstance()->run('kleeja_fetch_file_start', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
if (! empty($fetchType))
{
$result = $this->{$fetchType}();
}
$this->finishUp();
return $result;
}
protected function finishUp()
{
2019-05-25 00:30:55 +03:00
if (defined('KJ_SESSION'))
2019-05-23 01:03:12 +03:00
{
2019-05-25 00:30:55 +03:00
session_id(constant('KJ_SESSION'));
2019-05-23 01:03:12 +03:00
}
2019-05-19 02:40:43 +03:00
session_start();
}
protected function curl()
{
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
2019-05-23 01:03:12 +03:00
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
2019-05-19 04:22:59 +03:00
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
2019-05-23 01:03:12 +03:00
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
2019-05-19 02:40:43 +03:00
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; Kleeja)');
2019-05-23 01:03:12 +03:00
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
2019-05-19 02:40:43 +03:00
if ($this->binary)
{
curl_setopt($ch, CURLOPT_ENCODING, '');
}
//let's open new file to save it in.
if (! empty($this->destinationPath))
{
2019-05-19 04:22:59 +03:00
$out = fopen($this->destinationPath, 'w');
2019-05-19 02:40:43 +03:00
curl_setopt($ch, CURLOPT_FILE, $out);
2019-05-23 01:03:12 +03:00
$result = curl_exec($ch);
if ($result === false)
{
2022-08-25 18:42:16 +01:00
$error = true;
2019-05-23 01:03:12 +03:00
kleeja_log(sprintf("cUrl error (#%d): %s\n", curl_errno($ch), htmlspecialchars(curl_error($ch))));
}
2022-08-25 18:42:16 +01:00
curl_close($ch);
fclose($out);
2019-05-23 01:03:12 +03:00
2022-08-25 18:42:16 +01:00
return isset($error) ? false : true;
2019-05-19 02:40:43 +03:00
}
2019-05-19 04:22:59 +03:00
else
2019-05-19 02:40:43 +03:00
{
2019-05-19 04:22:59 +03:00
$data = curl_exec($ch);
2019-05-25 00:30:55 +03:00
2019-05-23 01:03:12 +03:00
if ($data === false)
{
2022-08-25 18:42:16 +01:00
$error = true;
2019-05-23 01:03:12 +03:00
kleeja_log(sprintf("FetchFile error (curl: #%d): %s\n", curl_errno($ch), htmlspecialchars(curl_error($ch))));
}
2022-08-25 18:42:16 +01:00
curl_close($ch);
2019-05-23 01:03:12 +03:00
2022-08-25 18:42:16 +01:00
return isset($error) ? false : $data;
2019-05-19 02:40:43 +03:00
}
}
protected function fopen()
{
2019-05-25 00:30:55 +03:00
// Setup a stream context
$stream_context = stream_context_create(
2019-05-19 02:40:43 +03:00
[
'http' => [
2019-05-19 04:22:59 +03:00
'method' => 'GET',
2019-05-19 02:40:43 +03:00
'user_agent' => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; Kleeja)',
2019-05-19 04:22:59 +03:00
'max_redirects' => $this->maxRedirects + 1,
'timeout' => $this->timeout
2019-05-19 02:40:43 +03:00
]
]
);
2019-05-19 04:22:59 +03:00
$content = @file_get_contents($this->url, false, $stream_context);
2019-05-19 02:40:43 +03:00
// Did we get anything?
if ($content !== false)
{
if (! empty($this->destinationPath))
{
$fp2 = fopen($this->destinationPath, 'w' . ($this->binary ? 'b' : ''));
@fwrite($fp2, $content);
@fclose($fp2);
unset($content);
return true;
}
2019-05-19 04:22:59 +03:00
return $content;
2019-05-19 02:40:43 +03:00
}
2019-05-25 00:30:55 +03:00
else
{
$error = error_get_last();
2019-05-25 00:53:53 +03:00
kleeja_log(sprintf("FetchFile error (stream: #%s): %s\n", $error['type'], $error['message']));
2019-05-25 00:30:55 +03:00
}
2019-05-19 02:40:43 +03:00
return false;
}
}