-
Notifications
You must be signed in to change notification settings - Fork 3
/
Client.php
427 lines (386 loc) · 13.9 KB
/
Client.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
/**
* This file is part of the PEAR2\Services\Linkback package.
*
* PHP version 5
*
* @category Services
* @package PEAR2\Services\Linkback
* @author Christian Weiske <cweiske@php.net>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://pear2.php.net/package/Services_Linkback
*/
namespace PEAR2\Services\Linkback;
use HTTP_Request2;
/**
* Linkback client, allowing you to send pingbacks and webmentions
* to remote sites to tell them that you linked to them.
*
* @category Services
* @package PEAR2\Services\Linkback
* @author Christian Weiske <cweiske@php.net>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://pear2.php.net/package/Services_Linkback
*/
class Client
{
/**
* HTTP request object that's used to do the requests
*
* @var HTTP_Request2
*/
protected $request;
/**
* Debug mode
* If activated, the response object will contain the HTTP Response
*
* @var boolean
*/
protected $debug = false;
/**
* Full response of the linkback request, helpful for debugging
* in some error conditions.
* Gets set when $debug is enabled
*
* @var \HTTP_Request2_Response
*/
protected $debugResponse;
/**
* URL validation helper
*
* @var Url
*/
protected $urlValidator;
/**
* Initialize URL validator
*/
public function __construct()
{
$this->urlValidator = new Url();
}
/**
* Send a linkback (webmention or pingback), indicating a link from
* source to target.
* The target's linkback server will be discovered automatically.
*
* @param string $sourceUri URL on this side, it links to $targetUri
* @param string $targetUri Remote URL that shall be notified about source
*
* @return Response\Ping Pingback response object containing all error
* and status information.
*/
public function send($sourceUri, $targetUri)
{
$this->debugResponse = null;
if (!$this->urlValidator->validate($sourceUri)) {
return new Response\Ping(
'Source URI invalid: ' . $sourceUri,
States::INVALID_URI
);
}
if (!$this->urlValidator->validate($targetUri)) {
return new Response\Ping(
'Target URI invalid: ' . $targetUri,
States::INVALID_URI
);
}
$serverInfo = $this->discoverServer($targetUri);
if ($serverInfo instanceof Response\Ping) {
if ($this->debug) {
$serverInfo->setResponse($this->debugResponse);
}
return $serverInfo;
}
if ($serverInfo->type == 'pingback') {
return $this->sendPingback($serverInfo, $sourceUri, $targetUri);
}
return $this->sendWebmention($serverInfo, $sourceUri, $targetUri);
}
/**
* Autodiscover the linkback server for the given URI.
*
* @param string $targetUri Some URL to discover the linkback server of
*
* @return Server\Info|Response\Ping Server info on success, Ping response object
* on failure. Response object has debug
* response not set.
*/
public function discoverServer($targetUri)
{
//at first, try a HEAD request that does not transfer so much data
$req = $this->getRequest();
$req->setUrl($targetUri);
$req->setMethod(HTTP_Request2::METHOD_HEAD);
$res = $req->send();
if ($this->debug) {
$this->debugResponse = $res;
}
if (intval($res->getStatus() / 100) > 3
&& $res->getStatus() != 405 //method not supported/allowed
) {
return new Response\Ping(
'Error fetching target URI via HEAD', States::TARGET_URI_NOT_FOUND
);
}
$uTarget = new \Net_URL2($targetUri);
$info = $this->extractHeader($res, 'HEAD', $uTarget);
if ($info !== null) {
return $info;
}
list($type) = explode(';', $res->getHeader('Content-type'));
if ($type != 'text/html' && $type != 'text/xml'
&& $type != 'application/xhtml+xml'
&& $res->getStatus() != 405//method not allowed
) {
return new Response\Ping(
'No linkback server found for URI (HEAD only)',
States::PINGBACK_UNSUPPORTED
);
}
//HEAD failed, do a normal GET
$req->setMethod(HTTP_Request2::METHOD_GET);
$res = $req->send();
if ($this->debug) {
$this->debugResponse = $res;
}
if (intval($res->getStatus() / 100) > 3) {
return new Response\Ping(
'Error fetching target URI', States::TARGET_URI_NOT_FOUND
);
}
//yes, maybe the server does return this header now
// e.g. PHP's Phar::webPhar() does not work with HEAD
// https://bugs.php.net/bug.php?id=51918
$info = $this->extractHeader($res, 'GET', $uTarget);
if ($info !== null) {
return $info;
}
$body = $res->getBody();
$doc = DomLoader::load($body, $res);
$xpath = new \DOMXPath($doc);
$xpath->registerNamespace('h', 'http://www.w3.org/1999/xhtml');
$xpRel = 'concat(" ", normalize-space(@rel), " ")';
$nodeList = $xpath->query(
// <link> with rel=pingback|webmention is "body-ok" in HTML5
// https://html.spec.whatwg.org/multipage/semantics.html#body-ok
'//*[(self::link or self::h:link)'
. ' and @href'
. ' and ('
. ' contains(' . $xpRel . ', " webmention ")'
. ' or contains(' . $xpRel . ', " http://webmention.org/ ")'
. ' or @rel="pingback"'
. ')'
. ']'
// <a>
. ' | '
. '/*[self::html or self::h:html]'
. '/*[self::body or self::h:body]'
. '//*[(self::a or self::h:a)'
. ' and @href'
. ' and ('
. ' contains(' . $xpRel . ', " webmention ")'
. ' or contains(' . $xpRel . ', " http://webmention.org/ ")'
. ')'
. ']'
);
if ($nodeList->length == 0) {
//target resource is not pingback/webmention enabled
return new Response\Ping(
'No linkback server found for URI',
States::PINGBACK_UNSUPPORTED
);
}
$arLinks = array();
foreach ($nodeList as $link) {
$uri = $link->attributes->getNamedItem('href')->nodeValue;
$types = explode(
' ', $link->attributes->getNamedItem('rel')->nodeValue
);
if (array_search('http://webmention.org/', $types) !== false) {
$types[] = 'webmention';
}
if (array_search('webmention', $types) !== false
&& $this->urlValidator->relative($uri)
) {
//webmention spec allows relative URLs, pingback not
$uri = (string) $uTarget->resolve($uri);
}
if ($this->urlValidator->validate($uri)) {
foreach ($types as $type) {
if ($type == 'webmention' || $type == 'pingback') {
if (!isset($arLinks[$type])) {
$arLinks[$type] = $uri;
}
}
}
}
}
if (count($arLinks) == 0) {
return new Response\Ping(
'HTML head link server URI invalid', States::INVALID_URI
);
}
if (isset($arLinks['webmention'])) {
return new Server\Info('webmention', $arLinks['webmention']);
}
return new Server\Info('pingback', $arLinks['pingback']);
}
/**
* Extract webmention and pingback headers from the HTTP response.
* Create server info object if found
*
* @param \HTTP_Request2_Response $res HTTP response from the target
* @param string $method HTTP method used to fetch $res
* @param \Net_URL2 $url Base URL to resolve relative links
*
* @return Server\Info|Response\Ping Information about linkback endpoint
* or error information
* or NULL if no link found
*/
protected function extractHeader($res, $method, \Net_URL2 $url)
{
$http = new \HTTP2();
//webmention link header
$links = $http->parseLinks($res->getHeader('Link'));
foreach ($links as $link) {
if (isset($link['_uri']) && isset($link['rel'])
&& (array_search('webmention', $link['rel']) !== false
|| array_search('http://webmention.org/', $link['rel']) !== false)
) {
if ($this->urlValidator->relative($link['_uri'])) {
//relative URL
$link['_uri'] = (string) $url->resolve($link['_uri']);
}
if (!$this->urlValidator->validate($link['_uri'])) {
return new Response\Ping(
$method . ' Link webmention server URI invalid: '
. $link['_uri'],
States::INVALID_URI
);
}
return new Server\Info('webmention', $link['_uri']);
}
}
//pingback url header
//pingback 1.0 spec does not allow relative links
$headerUri = $res->getHeader('X-Pingback');
if ($headerUri !== null) {
if (!$this->urlValidator->validate($headerUri)) {
return new Response\Ping(
$method . ' X-Pingback server URI invalid: ' . $headerUri,
States::INVALID_URI
);
}
return new Server\Info('pingback', $headerUri);
}
return null;
}
/**
* Contacts the given pingback server and tells him that source links to
* target.
*
* @param object $serverInfo Information about the server that implements
* pingback
* @param string $sourceUri URL on this side, it links to $targetUri
* @param string $targetUri Remote URL that shall be notified about source
*
* @return Response\Ping Pingback response object containing all error
* and status information.
*/
protected function sendPingback(Server\Info $serverInfo, $sourceUri, $targetUri)
{
$encSourceUri = htmlspecialchars($sourceUri);
$encTargetUri = htmlspecialchars($targetUri);
$req = $this->getRequest();
$req->setUrl($serverInfo->uri)
->setMethod(HTTP_Request2::METHOD_POST)
->setHeader('Content-type: text/xml')
->setBody(
<<<XML
<?xml version="1.0" encoding="utf-8"?>
<methodCall>
<methodName>pingback.ping</methodName>
<params>
<param><value><string>$encSourceUri</string></value></param>
<param><value><string>$encTargetUri</string></value></param>
</params>
</methodCall>
XML
);
$res = $req->send();
$pres = new Response\Ping();
$pres->loadFromPingbackResponse($res, $this->debug);
return $pres;
}
/**
* Contacts the given webmention server and tells him that source links to
* target.
*
* @param object $serverInfo Information about the server that implements
* webmention
* @param string $sourceUri URL on this side, it links to $targetUri
* @param string $targetUri Remote URL that shall be notified about source
*
* @return Response\Ping Pingback response object containing all error
* and status information.
*/
protected function sendWebmention(
Server\Info $serverInfo, $sourceUri, $targetUri
) {
$req = $this->getRequest();
$req->setUrl($serverInfo->uri)
->setMethod(HTTP_Request2::METHOD_POST)
->setHeader('Accept: application/json;q=0.9, */*;q=0.1')
->addPostParameter('source', $sourceUri)
->addPostParameter('target', $targetUri);
$res = $req->send();
$pres = new Response\Ping();
$pres->loadFromWebmentionResponse($res, $this->debug);
return $pres;
}
//FIXME: implement http://old.aquarionics.com/misc/archives/blogite/0198.html
/**
* Enable debugging by collecting HTTP response objects.
*
* @param boolean $debug True to enable debugging, false to deactivate it.
*
* @return void
*/
public function setDebug($debug)
{
$this->debug = $debug;
}
/**
* Returns the HTTP request object clone that can be used
* for one HTTP request.
*
* @return HTTP_Request2 Clone of the setRequest() object
*/
public function getRequest()
{
if ($this->request === null) {
$request = new HTTP_Request2();
//yes, people redirect xmlrpc.php
$request->setConfig('follow_redirects', true);
//keep POST on redirect
$request->setConfig('strict_redirects', true);
$this->setRequestTemplate($request);
}
//we need to clone because previous requests could have
//set internal variables like POST data that we don't want now
return clone $this->request;
}
/**
* Sets a custom HTTP request object that will be used to do HTTP requests
*
* @param HTTP_Request2 $request Request object
*
* @return self
*/
public function setRequestTemplate(HTTP_Request2 $request)
{
$this->request = $request;
return $this;
}
}
?>