Add option to parse HTTP header only
This commit is contained in:
parent
62a9f6f0c8
commit
a771bb5223
1 changed files with 37 additions and 36 deletions
|
|
@ -21,41 +21,31 @@ abstract class PatchBase {
|
||||||
return $this->patch;
|
return $this->patch;
|
||||||
}
|
}
|
||||||
abstract function check() : bool;
|
abstract function check() : bool;
|
||||||
protected function fetch(string $url, bool $json = false) : bool {
|
protected function fetch(string $url, bool $text = true) : bool {
|
||||||
$host = parse_url($url, PHP_URL_HOST);
|
// HTTP GET
|
||||||
/*$opt = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:1.0) Gecko/20200101 Patchbot/1.0'));
|
if ($text)
|
||||||
if ($str = HostOption::get($host))
|
$str = $this->curl($url);
|
||||||
$opt['http'] += array('header' => $str);
|
// HTTP HEAD
|
||||||
$ctx = stream_context_create($opt);
|
else
|
||||||
if ($str = @file_get_contents($url, false, $ctx)) {*/
|
$str = $this->curl($url, array(array(CURLOPT_HEADER, true), array(CURLOPT_NOBODY, true)));
|
||||||
if ($ch = curl_init()) {
|
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
|
||||||
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:1.0) Gecko/20200101 Patchbot/1.0');
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
||||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
||||||
if (!$json)
|
|
||||||
curl_setopt($ch, CURLOPT_HEADER, true);
|
|
||||||
if ($opt = HostOption::get($host))
|
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $opt);
|
|
||||||
$str = curl_exec($ch);
|
|
||||||
curl_close($ch);
|
|
||||||
if ($str) {
|
if ($str) {
|
||||||
$this->data = $str;
|
$this->data = $str;
|
||||||
if ($json) {
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
protected function fetch_json(string $url) : bool {
|
||||||
|
$str = $this->curl($url);
|
||||||
|
if ($str) {
|
||||||
if (!($this->data = json_decode($str, true)))
|
if (!($this->data = json_decode($str, true)))
|
||||||
return false;
|
return false;
|
||||||
// get first element only
|
// get first element only
|
||||||
if ($this->array_isMulti($this->data))
|
if ($this->array_isMulti($this->data))
|
||||||
$this->data = array_shift($this->data);
|
$this->data = array_shift($this->data);
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
protected function fetch_json(string $url) : bool {
|
|
||||||
return $this->fetch($url, true);
|
|
||||||
}
|
|
||||||
protected function parse(string $re) : bool {
|
protected function parse(string $re) : bool {
|
||||||
if ($str = $this->regex_str($re)) {
|
if ($str = $this->regex_str($re)) {
|
||||||
$this->patch->setVersion($str, true);
|
$this->patch->setVersion($str, true);
|
||||||
|
|
@ -78,12 +68,6 @@ abstract class PatchBase {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/*protected function array_extract(string $search) {
|
|
||||||
foreach ($this->data as $val) {
|
|
||||||
if (array_search($search, array_values($val)))
|
|
||||||
$this->data = $val;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
protected function str_crop(string $head = '', string $tail = '') {
|
protected function str_crop(string $head = '', string $tail = '') {
|
||||||
if (!empty($head)) {
|
if (!empty($head)) {
|
||||||
if ($str = strstr($this->data, $head))
|
if ($str = strstr($this->data, $head))
|
||||||
|
|
@ -94,6 +78,23 @@ abstract class PatchBase {
|
||||||
$this->data = $str;
|
$this->data = $str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private function curl(string $url, array $opts = array()) {
|
||||||
|
if ($ch = curl_init()) {
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:1.0) Gecko/20200101 Patchbot/1.0');
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||||
|
foreach ($opts as $opt)
|
||||||
|
curl_setopt($ch, $opt[0], $opt[1]);
|
||||||
|
if ($opt = HostOption::get(parse_url($url, PHP_URL_HOST)))
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $opt);
|
||||||
|
$str = curl_exec($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
if ($str)
|
||||||
|
return $str;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
private function regex(string $pattern) {
|
private function regex(string $pattern) {
|
||||||
if (!preg_match($pattern, $this->data, $m))
|
if (!preg_match($pattern, $this->data, $m))
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue