|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace XoopsModules\Tag; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
You may not change or alter any portion of this comment or credits of |
|
7
|
|
|
supporting developers from this source code or any supporting source code |
|
8
|
|
|
which is considered copyrighted (c) material of the original comment or credit |
|
9
|
|
|
authors. |
|
10
|
|
|
|
|
11
|
|
|
This program is distributed in the hope that it will be useful, but |
|
12
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
14
|
|
|
*/ |
|
15
|
|
|
/** |
|
16
|
|
|
* Module: Tag |
|
17
|
|
|
* |
|
18
|
|
|
* @package XoopsModules\Tag |
|
19
|
|
|
* @author ZySpec <[email protected]> |
|
20
|
|
|
* @copyright Copyright (c) 2001-2019 {@link https://xoops.org XOOPS Project}} |
|
21
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU Public License |
|
22
|
|
|
* @since 2.00 |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Issues class to collect information from GitHub |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
class Issues { |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array $hdrs |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $hdrs; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var string $dirname module directory name |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $dirname; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var string $curl_respones response from involking curl |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $curl_response; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var int $hdrSize Curl response header size |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $hdrSize; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var string $serviceUrl Service URL for curl |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $serviceUrl; |
|
50
|
|
|
/** |
|
51
|
|
|
* @var string $sessPrefix prefix for all SESSION vars |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $sessPrefix; |
|
54
|
|
|
/** |
|
55
|
|
|
* @var string $err class error text |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $err; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Constructor |
|
61
|
|
|
* |
|
62
|
|
|
* return void |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->hdrs = []; |
|
67
|
|
|
$this->curl_response = ''; |
|
68
|
|
|
$this->hdrSize = 0; |
|
69
|
|
|
$this->dirname = basename(dirname(__DIR__)); |
|
70
|
|
|
//$this->serviceUrl = 'https://api.github.com/repos/xoops/xoopscore25/issues?state=open'; |
|
71
|
|
|
//$this->serviceUrl = 'https://github.com/zyspec/' . $this->dirname . '/issues?state=open'; |
|
72
|
|
|
$this->serviceUrl = 'https://api.github.com/repos/XoopsModules25x/' . $this->dirname . '/issues?state=open'; |
|
73
|
|
|
$this->setSessPrefix($this->dirname); |
|
74
|
|
|
$this->err = ''; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Function to put HTTP headers in an array |
|
79
|
|
|
* |
|
80
|
|
|
* @param $curl |
|
81
|
|
|
* @param string $hdrLine |
|
82
|
|
|
* |
|
83
|
|
|
* @return int length of header line put into array |
|
84
|
|
|
*/ |
|
85
|
|
|
public function handleHeaderLine($curl, $hdrLine) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->hdrs[] = trim($hdrLine); |
|
88
|
|
|
return strlen($hdrLine); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Function to get a header from the header array |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $hdr |
|
95
|
|
|
* @param bool $asArray |
|
96
|
|
|
* |
|
97
|
|
|
* @return array|false array($hdr => value) or false if not found |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getHeaderFromArray($hdr, $asArray = false) |
|
100
|
|
|
{ |
|
101
|
|
|
$val = ''; |
|
102
|
|
|
foreach ($this->hdrs as $thisHdr) { |
|
103
|
|
|
if (preg_match("/^{$hdr}/i", $thisHdr)) { |
|
104
|
|
|
$val = substr($thisHdr, strlen($hdr)); |
|
105
|
|
|
break; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
return (bool)$asArray ? [$hdr => trim($val)] : trim($val); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
/** |
|
111
|
|
|
* Returns response from involking Curl |
|
112
|
|
|
* |
|
113
|
|
|
* @param bool $serialized (default = false) |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getCurlResponse($serialized = false) |
|
118
|
|
|
{ |
|
119
|
|
|
return (bool)$serialized ? serialize(base64_encode($this->curl_response)) : $this->curl_response; |
|
120
|
|
|
} |
|
121
|
|
|
/** |
|
122
|
|
|
* Get the size of curl response headers |
|
123
|
|
|
* |
|
124
|
|
|
* @return int size of header in bytes |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getHdrSize() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->hdrSize; |
|
129
|
|
|
} |
|
130
|
|
|
/** |
|
131
|
|
|
* Get the URL for curl |
|
132
|
|
|
* |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getServiceUrl() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->serviceUrl; |
|
138
|
|
|
} |
|
139
|
|
|
/** |
|
140
|
|
|
* Get the Prefix for SESSION variable |
|
141
|
|
|
* |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getSessPrefix() |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->sessPrefix(); |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
/** |
|
149
|
|
|
* Set the Prefix for SESSION variable |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $prefix string to prepend to session variable |
|
152
|
|
|
* |
|
153
|
|
|
* @return string prefix |
|
154
|
|
|
*/ |
|
155
|
|
|
public function setSessPrefix($prefix) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->sessPrefix = htmlspecialchars($prefix, ENT_QUOTES | ENT_HTML5) . '_'; |
|
158
|
|
|
return $this->sessPrefix; |
|
159
|
|
|
} |
|
160
|
|
|
/** |
|
161
|
|
|
* Get the SESSION variable name for Etag key |
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getsKeyEtag() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->sessPrefix . 'github_etag'; |
|
168
|
|
|
} |
|
169
|
|
|
/** |
|
170
|
|
|
* Get the SESSION variable name for Header Size key |
|
171
|
|
|
* |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getsKeyHdrSize() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->sessPrefix . 'github_hdr_size'; |
|
177
|
|
|
} |
|
178
|
|
|
/** |
|
179
|
|
|
* Get the SESSION variable name for Response key |
|
180
|
|
|
* |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getsKeyResponse() |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->sessPrefix . 'github_curl_response'; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Get the SESSION variable name for Array key |
|
190
|
|
|
* |
|
191
|
|
|
* @return array |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getsKeyArray() |
|
194
|
|
|
{ |
|
195
|
|
|
return [$this->getsKeyEtag(), $this->getsKeyHdrSize(), $this->getsKeyResponse()]; |
|
196
|
|
|
} |
|
197
|
|
|
/** |
|
198
|
|
|
* Get the SESSION cached Etag key contents |
|
199
|
|
|
* |
|
200
|
|
|
* @return string|bool Etag key or false if tag not set |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getCachedEtag() |
|
203
|
|
|
{ |
|
204
|
|
|
return isset($_SESSION[$this->getsKeyEtag()]) ? base64_decode(unserialize($_SESSION[$this->getsKeyEtag()])) : false; |
|
205
|
|
|
} |
|
206
|
|
|
/** |
|
207
|
|
|
* Set the error message associated with the latest Curl operation |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $msg the error message to save |
|
210
|
|
|
* |
|
211
|
|
|
* @return void |
|
212
|
|
|
*/ |
|
213
|
|
|
public function setError($msg) |
|
214
|
|
|
{ |
|
215
|
|
|
$this->err = $msg; |
|
216
|
|
|
} |
|
217
|
|
|
/** |
|
218
|
|
|
* Get the error message associated with the latest Curl operation |
|
219
|
|
|
* |
|
220
|
|
|
* @return string the current error message |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getError() |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->err; |
|
225
|
|
|
} |
|
226
|
|
|
/** |
|
227
|
|
|
* Execute a curl operation to retrieve data from GitHub server |
|
228
|
|
|
* |
|
229
|
|
|
* Also sets the SESSION vars for the curl operation |
|
230
|
|
|
* |
|
231
|
|
|
* @return int the current header size from curl |
|
232
|
|
|
*/ |
|
233
|
|
|
public function execCurl() |
|
234
|
|
|
{ |
|
235
|
|
|
$curl = curl_init($this->getServiceUrl()); |
|
236
|
|
|
curl_setopt_array($curl, [ |
|
|
|
|
|
|
237
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
238
|
|
|
CURLOPT_HEADER => true, |
|
239
|
|
|
CURLOPT_VERBOSE => true, |
|
240
|
|
|
CURLOPT_TIMEOUT => 5, |
|
241
|
|
|
CURLOPT_HTTPGET => true, |
|
242
|
|
|
CURLOPT_USERAGENT => 'XOOPS-' . $this->dirname, |
|
243
|
|
|
CURLOPT_HTTPHEADER => [ |
|
244
|
|
|
'Content-type:application/json', |
|
245
|
|
|
'If-None-Match: ' . $this->getCachedEtag() |
|
|
|
|
|
|
246
|
|
|
], |
|
247
|
|
|
CURLINFO_HEADER_OUT => true, |
|
248
|
|
|
CURLOPT_HEADERFUNCTION => [$this, 'handleHeaderLine'] |
|
249
|
|
|
] |
|
250
|
|
|
); |
|
251
|
|
|
// execute the session |
|
252
|
|
|
$this->curl_response = curl_exec($curl); |
|
|
|
|
|
|
253
|
|
|
// get the header size and finish off the session |
|
254
|
|
|
$this->hdrSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
|
|
|
|
|
|
255
|
|
|
$this->hdrSize = (int)$this->hdrSize; |
|
256
|
|
|
curl_close($curl); |
|
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
$hdrEtag = $this->getHeaderFromArray('Etag: '); |
|
259
|
|
|
|
|
260
|
|
|
$_SESSION[$this->getsKeyEtag()] = serialize(base64_encode($hdrEtag)); |
|
|
|
|
|
|
261
|
|
|
$_SESSION[$this->getsKeyHdrSize()] = serialize($this->hdrSize); |
|
262
|
|
|
$_SESSION[$this->getsKeyResponse()] = serialize(base64_encode($this->curl_response)); |
|
263
|
|
|
return $this->hdrSize; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|