- function getPingback($result) {
- if (array_key_exists('X-Pingback', $result->headers)) {
- return $result->headers['X-Pingback'];
- } else if(preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]*\/?>/i', $result->body, $match)
- || preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $result->body, $match)) {
+ // Based on https://github.com/indieweb/mention-client-php
+ // which is licensed Apache 2.0
+ function getWebmention(HTTP_Request2_Response $response) {
+ $link = $response->getHeader('Link');
+ if (!is_null($link)) {
+ // XXX: the fetcher gives back a comma-separated string of all Link headers, I hope the parsing works reliably
+ if (preg_match('~<([^>]+)>; rel="?(?:[^" ]* )*(?:http://webmention.org/|webmention)(?: [^" ]*)*"?~', $link, $match)) {
+ return $match[1];
+ }
+ }
+
+ // FIXME: Do proper DOM traversal
+ // Currently fails https://webmention.rocks/test/13, https://webmention.rocks/test/17
+ if(preg_match('~<(?:link|a)[ ]+href="([^"]*)"[ ]+rel="(?:[^" ]* )*(?:http://webmention.org/|webmention)(?: [^" ]*)*"[ ]*/?>~i', $response->getBody(), $match)
+ || preg_match('~<(?:link|a)[ ]+rel="(?:[^" ]* )*(?:http://webmention.org/|webmention)(?: [^" ]*)*"[ ]+href="([^"]*)"[ ]*/?>~i', $response->getBody(), $match)) {
+ return $match[1];
+ }
+
+ return NULL;
+ }
+
+ function webmention($url, $endpoint) {
+ $source = $this->notice->getUrl();
+
+ common_log(LOG_DEBUG,"Attempting webmention to $endpoint for $url from $source");
+
+ $payload = array(
+ 'source' => $source,
+ 'target' => $url
+ );
+
+ $request = HTTPClient::start();
+ try {
+ $response = $request->post($endpoint,
+ array(
+ 'Content-type: application/x-www-form-urlencoded',
+ 'Accept: application/json'
+ ),
+ $payload
+ );
+
+ if(!in_array($response->getStatus(), array(200,201,202))) {
+ common_log(LOG_WARNING,
+ "Webmention request failed for '$url' ($endpoint)");
+ }
+ } catch (Exception $e) {
+ common_log(LOG_WARNING, "Webmention request failed for '{$url}' ({$endpoint}): {$e->getMessage()}");
+ }
+ }
+
+ function getPingback(HTTP_Request2_Response $response) {
+ if ($response->getHeader('X-Pingback')) {
+ return $response->getHeader('X-Pingback');
+ } elseif (preg_match('/<(?:link|a)[ ]+href="([^"]+)"[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]*\/?>/i', $response->getBody(), $match)
+ || preg_match('/<(?:link|a)[ ]+rel="[^" ]* ?pingback ?[^" ]*"[ ]+href="([^"]+)"[ ]*\/?>/i', $response->getBody(), $match)) {