-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPatchBase.php
162 lines (159 loc) · 4.4 KB
/
PatchBase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
require('HostOption.php');
abstract class PatchBase {
protected $patch;
protected $data = '';
function __construct(string $vendor, string $product, string $url) {
$this->patch = new PatchObject(array('id' => $this->id(), 'vendor' => $vendor, 'product' => $product, 'url' => $url));
}
function __toString() : string {
return (string)$this->patch;
}
protected function dump() {
var_dump($this->data);
}
function id() : string {
return get_class($this);
}
function getPatch() : PatchObject {
return $this->patch;
}
abstract function check() : bool;
protected function fetch(string $url, array $opts = array()) : bool {
$str = $this->curl($url, $opts);
if ($str) {
$this->data = $str;
return true;
}
return false;
}
protected function fetch_header(string $url, array $opts = array()) : bool {
$str = $this->curl($url, array_merge($opts, array('CURLOPT_HEADER' => true, 'CURLOPT_NOBODY' => true)));
if ($str) {
$this->data = $str;
return true;
}
return false;
}
protected function fetch_json(string $url, array $opts = array()) : bool {
$str = $this->curl($url, $opts);
if ($str) {
if (!($this->data = json_decode($str, true)))
return false;
// get first element only
if ($this->array_isMulti($this->data))
$this->data = array_shift($this->data);
return true;
}
return false;
}
protected function fetch_xml(string $url, array $opts = array()) : bool {
$str = $this->curl($url, $opts);
if ($str) {
if (!($this->data = simplexml_load_string($str)))
return false;
$ns = $this->data->getDocNamespaces(true);
foreach ($ns as $n => $u)
$this->data->registerXPathNamespace($n, $u);
return true;
}
return false;
}
protected function fetch_yaml(string $url, array $opts = array()) : bool {
$str = $this->curl($url, $opts);
if ($str) {
if (!($this->data = yaml_parse($str)))
return false;
//if ($this->array_isMulti($this->data))
// $this->data = array_shift($this->data);
return true;
}
return false;
}
protected function fetch_gzip(string $url, array $opts = array()) : bool {
$str = $this->curl($url, $opts);
if ($str) {
if (!($this->data = gzdecode($str)))
return false;
return true;
}
return false;
}
protected function parse(string $re) : bool {
if ($str = $this->regex_str($re)) {
$this->patch->setVersion($str, true);
return true;
}
return false;
}
protected function parse_json(string $key, string $re = '/(.*)/') : bool {
$it = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->data));
foreach ($it as $k => $v) {
if ($key == $k) {
$this->data = $v;
return $this->parse($re);
}
}
return false;
}
protected function parse_xml(string $key, string $re = '/(.*)/') : bool {
if ($res = $this->data->xpath($key)) {
$this->data = (string)$res[0];
return $this->parse($re);
}
return false;
}
protected function parse_yaml(string $key, string $re = '/(.*)/') : bool {
return $this->parse_json($key, $re);
}
private function array_isMulti(array $arr) : bool {
foreach ($arr as $val) {
if (!is_array($val))
return false;
}
return true;
}
protected function str_crop(string $head = '', string $tail = '') {
if (!empty($head)) {
if ($str = strstr($this->data, $head))
$this->data = $str;
}
if (!empty($tail)) {
if ($str = strstr($this->data, $tail, true))
$this->data = $str;
}
}
private function curl(string $url, array $opts) {
if ($ch = curl_init()) {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; +http://www.patchbot.de/) Gecko/20100101 Patchbot/1.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
foreach ($opts as $key => $val)
curl_setopt($ch, constant($key), $val);
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) {
if (!preg_match_all($pattern, $this->data, $m, PREG_PATTERN_ORDER))
return false;
// suppress full pattern match
$m = $m[1];
// TODO - add option: $m = array_reverse($m);
return $m;
}
private function regex_str(string $pattern) {
$m = $this->regex($pattern);
if ($m)
return $m[0];
return false;
}
}
?>