|
| 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 | +depends on: link_rel_parser.php (e.g. head_http_rels) |
| 8 | +depends on: https://github.com/tantek/cassis/cassis.js (e.g. contains, get_absolute_uri, is_html_type, xphasrel, strcat) |
| 9 | +*/ |
| 10 | + |
| 11 | +// Could move this function to cassis.js if more broadly useful |
| 12 | +function is_loopback($href) { |
| 13 | +// in: $href URL |
| 14 | +// out: boolean whether host of URL is a loopback address |
| 15 | + $host = hostname_of_uri($href); |
| 16 | + if ($host == 'localhost') { return true; } |
| 17 | + $host = explode('.', $host); |
| 18 | + return ($host.length == 4 && |
| 19 | + $host[0] == 127 && |
| 20 | + ctype_digit($host[1]) && |
| 21 | + ctype_digit($host[2]) && |
| 22 | + ctype_digit($host[3])); |
| 23 | +} |
| 24 | + |
| 25 | +function first_linknode_href($links, $spacedtagnames='a area link') { |
| 26 | +// in: DOMNodeList $links |
| 27 | +// $spacedtagnames - space separated tag names, null for any |
| 28 | +// out: href attribute as string |
| 29 | +// return href of first DOMNode in $links that is an a, area, link |
| 30 | +// with href that is not a loopback address |
| 31 | +// else return null |
| 32 | + if ($spacedtagnames) { |
| 33 | + $spacedtagnames = strcat(' ', $spacedtagnames, ' '); |
| 34 | + } |
| 35 | + foreach ($links as $link) { |
| 36 | + if (!$spacedtagnames || |
| 37 | + contains($spacedtagnames, |
| 38 | + strcat(' ', $link->nodeName, ' '))) { |
| 39 | + $href = $link->getAttribute('href'); |
| 40 | + if (!is_loopback($href)) |
| 41 | + { |
| 42 | + return $href; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + return null; |
| 47 | +} |
| 48 | + |
| 49 | +// in: $url of page that may or may not have a webmention endpoint |
| 50 | +// out: array of 'webmention' URL of webmention endpoint if any, |
| 51 | +// and 'pingback' URL of pingback endpoint if any |
| 52 | +function get_rel_webmention($url) { |
| 53 | + global $debug; |
| 54 | + $r = array(); |
| 55 | + $r['webmention'] = ''; |
| 56 | + $r['pingback'] = ''; |
| 57 | + |
| 58 | + $httprels = head_http_rels($url); |
| 59 | + if ($debug) { |
| 60 | + echo 'head_http_rels STATUS:"'.$httprels['status'].'"<br/>'; |
| 61 | + } |
| 62 | + if ($httprels['status'] != "200") { |
| 63 | + return $r; |
| 64 | + } |
| 65 | + |
| 66 | + if ($debug) { |
| 67 | + echo 'HEAD Content-Type: '.$httprels['type'].' '. |
| 68 | + string(is_html_type($httprels['type'])).'<br/>'; |
| 69 | + } |
| 70 | + $wm = ''; |
| 71 | + $pb = ''; |
| 72 | + if (array_key_exists('webmention', $httprels['rels'])) { |
| 73 | + $wm = $httprels['rels']['webmention'][0]; |
| 74 | + // just use the first one. |
| 75 | + } |
| 76 | + if (array_key_exists('pingback', $httprels['rels'])) { |
| 77 | + $pb = $httprels['rels']['pingback'][0]; |
| 78 | + // just use the first one. |
| 79 | + } |
| 80 | + if ($debug && $wm) { |
| 81 | + echo "HEAD LINK webmention: '$wm'<br/>"; |
| 82 | + } |
| 83 | + if ($debug && $pb) { |
| 84 | + echo "HEAD LINK pingback: '$pb'<br/>"; |
| 85 | + } |
| 86 | + if (!$wm && is_html_type($httprels['type'])) { |
| 87 | + // no webmention endpoint in HTTP headers, check HTML |
| 88 | + if ($debug) { |
| 89 | + echo "looking for wm endpoint in HTML $url<br/>"; |
| 90 | + } |
| 91 | + $ch = curl_init($url); |
| 92 | +// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 93 | +// commented out due to: |
| 94 | +// Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set |
| 95 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 96 | + $s = curl_exec($ch); |
| 97 | + curl_close($ch); |
| 98 | + if ($s != '') { |
| 99 | + $dom = new DOMDocument(); |
| 100 | + $dom->loadHTML($s); // ** maybe only load part of it? |
| 101 | + $domx = new DOMXPath($dom); |
| 102 | + $wms = $domx->query(xphasrel('webmention')); |
| 103 | + if ($wms) { $wms = first_linknode_href($wms); } |
| 104 | + if ($debug) { |
| 105 | + echo "query xphasrel webmention $wms<br/>"; |
| 106 | + } |
| 107 | + |
| 108 | + if ($wms !== null) { |
| 109 | + $wm = get_absolute_uri($wms, $url); |
| 110 | + } |
| 111 | + if ($debug && $wm) { |
| 112 | + echo "HTML rel=webmention returned '$wm'<br/>"; |
| 113 | + } |
| 114 | + $wms = $domx->query(xphasrel('pingback')); |
| 115 | + if ($wms) { $wms = first_linknode_href($wms, 'link'); } |
| 116 | + if ($debug) { |
| 117 | + echo "query xphasrel pingback $wms<br/>"; |
| 118 | + } |
| 119 | + |
| 120 | + if ($wms !== null) { |
| 121 | + $pb = get_absolute_uri($wms, $url); |
| 122 | + } |
| 123 | + if ($debug && $pb) { |
| 124 | + echo "HTML rel=pingback returned '$pb'<br/>"; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + $r['webmention'] = $wm; |
| 129 | + $r['pingback'] = $pb; |
| 130 | + return $r; |
| 131 | +} |
| 132 | + |
| 133 | +?> |
0 commit comments