Site-specific HTTP header fields, e.g. GitHub authentication

This commit is contained in:
Steffen Lange 2021-01-14 22:30:40 +01:00
parent 6493b97182
commit c384a1e845
2 changed files with 18 additions and 1 deletions

12
HostOption.php Normal file
View file

@ -0,0 +1,12 @@
<?php
class HostOption {
private static $values = array(
'api.github.com' => array('Authorization: token TOKEN')
);
public static function get(string $key) {
return array_key_exists($key, self::$values) ? self::$values[$key] : false;
}
}
?>

View file

@ -1,5 +1,7 @@
<?php <?php
require('HostOption.php');
abstract class PatchBase { abstract class PatchBase {
protected $patch; protected $patch;
protected $data = ''; protected $data = '';
@ -17,9 +19,12 @@ abstract class PatchBase {
} }
abstract function check() : bool; abstract function check() : bool;
protected function fetch(string $url, bool $json = false) : bool { protected function fetch(string $url, bool $json = false) : bool {
$host = parse_url($url, PHP_URL_HOST);
$opt = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:1.0) Gecko/20200101 Patchbot/1.0')); $opt = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:1.0) Gecko/20200101 Patchbot/1.0'));
if (HostOption::get($host))
$opt['http'] += array('header' => HostOption::get($host));
$ctx = stream_context_create($opt); $ctx = stream_context_create($opt);
if ($str = file_get_contents($url, false, $ctx)) { if ($str = @file_get_contents($url, false, $ctx)) {
$this->data = $str; $this->data = $str;
if ($json) { if ($json) {
if (!($this->data = json_decode($str, true))) if (!($this->data = json_decode($str, true)))