|
4 | 4 | /* |
5 | 5 | first_linknode_href, get_rel_webmention by Tantek Çelik http://tantek.com/ |
6 | 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) |
7 | 9 | */ |
8 | 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 | + $host = explode('.', $host); |
| 17 | + return ($host.length == 4 && |
| 18 | + $host[0] == 127 && |
| 19 | + ctype_digit($host[1]) && |
| 20 | + ctype_digit($host[2]) && |
| 21 | + ctype_digit($host[3])); |
| 22 | +} |
| 23 | + |
9 | 24 | function first_linknode_href($links, $spacedtagnames='a area link') { |
10 | 25 | // in: DOMNodeList $links |
11 | 26 | // $spacedtagnames - space separated tag names, null for any |
12 | 27 | // out: href attribute as string |
13 | | -// return first DOMNode in $links that is an a, area, link with href |
| 28 | +// return href of first DOMNode in $links that is an a, area, link |
| 29 | +// with href that is not a loopback address |
14 | 30 | // else return null |
15 | 31 | if ($spacedtagnames) { |
16 | 32 | $spacedtagnames = strcat(' ', $spacedtagnames, ' '); |
17 | 33 | } |
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; |
| 34 | + foreach ($links as $link) { |
| 35 | + if (!$spacedtagnames || |
| 36 | + contains($spacedtagnames, |
| 37 | + strcat(' ', $link->nodeName, ' '))) { |
| 38 | + $href = $link->getAttribute('href'); |
| 39 | + if (!is_loopback($href)) |
| 40 | + { |
| 41 | + return $href; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + return null; |
26 | 46 | } |
27 | 47 |
|
28 | 48 | // in: $url of page that may or may not have a webmention endpoint |
|
0 commit comments