|
| 1 | +<?php |
| 2 | +/* sample functions for using the link rel parser library to get first webmention (and pingback) endpoints */ |
| 3 | + |
| 4 | +/* |
| 5 | +first_linknode_href, get_rel_webmention by Tantek Çelik http://tantek.com/ |
| 6 | +license: http://creativecommons.org/publicdomain/zero/1.0/ |
| 7 | +*/ |
| 8 | + |
| 9 | +function first_linknode_href($links, $spacedtagnames='a area link') { |
| 10 | +// in: DOMNodeList $links |
| 11 | +// $spacedtagnames - space separated tag names, null for any |
| 12 | +// out: href attribute as string |
| 13 | +// return first DOMNode in $links that is an a, area, link with href |
| 14 | +// else return null |
| 15 | + if ($spacedtagnames) { |
| 16 | + $spacedtagnames = strcat(' ', $spacedtagnames, ' '); |
| 17 | + } |
| 18 | + foreach ($links as $link) { |
| 19 | + if (!$spacedtagnames || |
| 20 | + contains($spacedtagnames, |
| 21 | + strcat(' ', $link->nodeName, ' '))) { |
| 22 | + return $link->getAttribute('href'); |
| 23 | + } |
| 24 | + } |
| 25 | + return null; |
| 26 | +} |
| 27 | + |
| 28 | +// in: $url of page that may or may not have a webmention endpoint |
| 29 | +// out: array of 'webmention' URL of webmention endpoint if any, |
| 30 | +// and 'pingback' URL of pingback endpoint if any |
| 31 | +function get_rel_webmention($url) { |
| 32 | + global $debug; |
| 33 | + $r = array(); |
| 34 | + $r['webmention'] = ''; |
| 35 | + $r['pingback'] = ''; |
| 36 | + |
| 37 | + $httprels = head_http_rels($url); |
| 38 | + if ($debug) { |
| 39 | + echo 'head_http_rels STATUS:"'.$httprels['status'].'"<br/>'; |
| 40 | + } |
| 41 | + if ($httprels['status'] != "200") { |
| 42 | + return $r; |
| 43 | + } |
| 44 | + |
| 45 | + if ($debug) { |
| 46 | + echo 'HEAD Content-Type: '.$httprels['type'].' '. |
| 47 | + string(is_html_type($httprels['type'])).'<br/>'; |
| 48 | + } |
| 49 | + $wm = ''; |
| 50 | + $pb = ''; |
| 51 | + if (array_key_exists('webmention', $httprels['rels'])) { |
| 52 | + $wm = $httprels['rels']['webmention'][0]; |
| 53 | + // just use the first one. |
| 54 | + } |
| 55 | + if (array_key_exists('pingback', $httprels['rels'])) { |
| 56 | + $pb = $httprels['rels']['pingback'][0]; |
| 57 | + // just use the first one. |
| 58 | + } |
| 59 | + if ($debug && $wm) { |
| 60 | + echo "HEAD LINK webmention: '$wm'<br/>"; |
| 61 | + } |
| 62 | + if ($debug && $pb) { |
| 63 | + echo "HEAD LINK pingback: '$pb'<br/>"; |
| 64 | + } |
| 65 | + if (!$wm && is_html_type($httprels['type'])) { |
| 66 | + // no webmention endpoint in HTTP headers, check HTML |
| 67 | + if ($debug) { |
| 68 | + echo "looking for wm endpoint in HTML $url<br/>"; |
| 69 | + } |
| 70 | + $ch = curl_init($url); |
| 71 | +// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 72 | +// commented out due to: |
| 73 | +// Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set |
| 74 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 75 | + $s = curl_exec($ch); |
| 76 | + curl_close($ch); |
| 77 | + if ($s != '') { |
| 78 | + $dom = new DOMDocument(); |
| 79 | + $dom->loadHTML($s); // ** maybe only load part of it? |
| 80 | + $domx = new DOMXPath($dom); |
| 81 | + $wms = $domx->query(xphasrel('webmention')); |
| 82 | + if ($wms) { $wms = first_linknode_href($wms); } |
| 83 | + if ($debug) { |
| 84 | + echo "query xphasrel webmention $wms<br/>"; |
| 85 | + } |
| 86 | + |
| 87 | + if ($wms !== null) { |
| 88 | + $wm = get_absolute_uri($wms, $url); |
| 89 | + } |
| 90 | + if ($debug && $wm) { |
| 91 | + echo "HTML rel=webmention returned '$wm'<br/>"; |
| 92 | + } |
| 93 | + $wms = $domx->query(xphasrel('pingback')); |
| 94 | + if ($wms) { $wms = first_linknode_href($wms, 'link'); } |
| 95 | + if ($debug) { |
| 96 | + echo "query xphasrel pingback $wms<br/>"; |
| 97 | + } |
| 98 | + |
| 99 | + if ($wms !== null) { |
| 100 | + $pb = get_absolute_uri($wms, $url); |
| 101 | + } |
| 102 | + if ($debug && $pb) { |
| 103 | + echo "HTML rel=pingback returned '$pb'<br/>"; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + $r['webmention'] = $wm; |
| 108 | + $r['pingback'] = $pb; |
| 109 | + return $r; |
| 110 | +} |
| 111 | + |
| 112 | +?> |
0 commit comments