254 lines
8.9 KiB
PHP
254 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace Animarr;
|
|
|
|
class Request{
|
|
|
|
private static $USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36";
|
|
|
|
private static $ch = [];
|
|
private static $cookie = [];
|
|
|
|
public static function setCookie($n, $c){
|
|
|
|
if($c === null){
|
|
self::$cookie[$n] = "";
|
|
}else{
|
|
self::$cookie[$n] .= $c;
|
|
}
|
|
unset(self::$ch[$n]);
|
|
curl_setopt(self::getHandlerInternal($n), CURLOPT_COOKIE, self::$cookie[$n]);
|
|
}
|
|
|
|
public static function getHandleId($url){
|
|
return parse_url($url)["host"];
|
|
}
|
|
|
|
public static function disableProxy($n){
|
|
$ch = self::getHandlerInternal($n);
|
|
curl_setopt($ch, CURLOPT_PROXY, "");
|
|
}
|
|
|
|
public static function setHTTPProxy($n, $address){
|
|
$ch = self::getHandlerInternal($n);
|
|
if(preg_match("/^([^@:]+:[^@:]+)@(.+)$/", $address, $matches) > 0){
|
|
curl_setopt($ch, CURLOPT_PROXY, $matches[2]);
|
|
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $matches[1]);
|
|
}else{
|
|
curl_setopt($ch, CURLOPT_PROXY, $address);
|
|
}
|
|
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
|
}
|
|
|
|
public static function setSocks4Proxy($n, $address){
|
|
$ch = self::getHandlerInternal($n);
|
|
if(preg_match("/^([^@:]+:[^@:]+)@(.+)$/", $address, $matches) > 0){
|
|
curl_setopt($ch, CURLOPT_PROXY, $matches[2]);
|
|
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $matches[1]);
|
|
}else{
|
|
curl_setopt($ch, CURLOPT_PROXY, $address);
|
|
}
|
|
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
|
|
}
|
|
|
|
public static function setSocks5Proxy($n, $address){
|
|
$ch = self::getHandlerInternal($n);
|
|
if(preg_match("/^([^@:]+:[^@:]+)@(.+)$/", $address, $matches) > 0){
|
|
curl_setopt($ch, CURLOPT_PROXY, $matches[2]);
|
|
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $matches[1]);
|
|
}else{
|
|
curl_setopt($ch, CURLOPT_PROXY, $address);
|
|
}
|
|
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
|
|
}
|
|
|
|
private static function getHandlerInternal($n, $page = null, $ignore = false){
|
|
if($ignore){
|
|
$ch = curl_init($page);
|
|
curl_setopt($ch, CURLOPT_COOKIE, self::$cookie[$n]);
|
|
return $ch;
|
|
}
|
|
|
|
if(!isset(self::$ch[$n]) or self::$ch[$n] === null){
|
|
self::$ch[$n] = curl_init($page);
|
|
}else{
|
|
curl_setopt(self::$ch[$n], CURLOPT_URL, $page);
|
|
}
|
|
|
|
return self::$ch[$n];
|
|
}
|
|
|
|
private static function getHandler($page, $ignore = false){
|
|
return self::getHandlerInternal(self::getHandleId($page), $page, $ignore);
|
|
}
|
|
|
|
public static function getHeaders($page, array $headers = [], $ignore = false, $timeout = 10){
|
|
$ch = self::getHandler($page, $ignore);
|
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NONE);
|
|
curl_setopt($ch, CURLOPT_POST, 0);
|
|
curl_setopt($ch, CURLOPT_NOPROGRESS, true);
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
|
curl_setopt($ch, CURLOPT_ENCODING, "");
|
|
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_COOKIEFILE, "");
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(["User-Agent: " . self::$USER_AGENT, "Accept-Language: *", "Connection: Keep-Alive", "Keep-Alive: 300"], $headers));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
|
curl_setopt($ch, CURLOPT_NOBODY, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
|
|
|
|
$ret = curl_exec($ch);
|
|
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200){
|
|
return "";
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public static function getHeaderURL($page, array $headers = [], $ignore = false, $timeout = 10){
|
|
$ch = self::getHandler($page, $ignore);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NONE);
|
|
curl_setopt($ch, CURLOPT_NOBODY, false);
|
|
curl_setopt($ch, CURLOPT_NOPROGRESS, true);
|
|
curl_setopt($ch, CURLOPT_POST, 0);
|
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
|
curl_setopt($ch, CURLOPT_ENCODING, "");
|
|
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_COOKIEFILE, "");
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(["User-Agent: " . self::$USER_AGENT, "Accept-Language: *"], $headers));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
|
|
$ret = curl_exec($ch);
|
|
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200){
|
|
return "";
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public static function getAuthURL($page, $user, $pwd, array $headers = [], $ignore = false, $timeout = 10){
|
|
//Downloader::log("$page", "getAuthURL");
|
|
$ch = self::getHandler($page, $ignore);
|
|
|
|
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pwd");
|
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
|
curl_setopt($ch, CURLOPT_NOBODY, false);
|
|
curl_setopt($ch, CURLOPT_NOPROGRESS, true);
|
|
curl_setopt($ch, CURLOPT_POST, 0);
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
|
curl_setopt($ch, CURLOPT_ENCODING, "");
|
|
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_COOKIEFILE, "");
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(["User-Agent: " . self::$USER_AGENT, "Accept-Language: *", "Accept: */*"], $headers));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
|
$ret = curl_exec($ch);
|
|
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200){
|
|
return "";
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public static function getURL($page, array $headers = [], $ignore = false, $timeout = 10){
|
|
//Downloader::log("$page", "getURL");
|
|
$ch = self::getHandler($page, $ignore);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NONE);
|
|
curl_setopt($ch, CURLOPT_NOBODY, false);
|
|
curl_setopt($ch, CURLOPT_NOPROGRESS, true);
|
|
curl_setopt($ch, CURLOPT_POST, 0);
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
|
curl_setopt($ch, CURLOPT_ENCODING, "");
|
|
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_COOKIEFILE, "");
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(["User-Agent: " . self::$USER_AGENT, "Accept-Language: *", "Accept: */*"], $headers));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
|
$ret = curl_exec($ch);
|
|
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200){
|
|
return "";
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public static function getFileURL($page, $fp, array $headers = [], $ignore = false, callable $progress = null, $timeout = 30){
|
|
$ch = self::getHandler($page, $ignore);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NONE);
|
|
curl_setopt($ch, CURLOPT_NOBODY, false);
|
|
curl_setopt($ch, CURLOPT_POST, 0);
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
|
curl_setopt($ch, CURLOPT_ENCODING, "");
|
|
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_COOKIEFILE, "");
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(["User-Agent: " . self::$USER_AGENT, "Accept-Language: *"], $headers));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
|
|
curl_setopt($ch, CURLOPT_FILE, $fp);
|
|
|
|
if($progress !== null){
|
|
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
|
|
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, $progress);
|
|
//curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024 * 512);
|
|
}
|
|
|
|
curl_exec($ch);
|
|
|
|
curl_setopt($ch, CURLOPT_NOPROGRESS, true);
|
|
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, null);
|
|
curl_setopt($ch, CURLOPT_FILE, null);
|
|
}
|
|
|
|
public static function postURL($page, $args, array $headers = [], $ignore = false, $timeout = 10){
|
|
$ch = self::getHandler($page, $ignore);
|
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NONE);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_NOBODY, false);
|
|
if(isset($args["returnheader"])){
|
|
unset($args["returnheader"]);
|
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
|
}else{
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
}
|
|
curl_setopt($ch, CURLOPT_NOPROGRESS, true);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
|
curl_setopt($ch, CURLOPT_ENCODING, "");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
|
|
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_COOKIEFILE, "");
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["User-Agent: " . self::$USER_AGENT, "Accept-Language: *"] + $headers);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
|
|
return curl_exec($ch);
|
|
}
|
|
|
|
}
|