Skip to content

Commit 295420e

Browse files
committed
Merge branch 'master' of github.com:indieweb/link-rel-parser-php
2 parents d10448c + 7c4c729 commit 295420e

File tree

4 files changed

+161
-2
lines changed

4 files changed

+161
-2
lines changed

CODE_OF_CONDUCT.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Contributor Code of Conduct
2+
3+
See the Code of Conduct on the IndieWebCamp wiki.
4+
5+
https://indiewebcamp.com/code-of-conduct

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
link-rel-parser-php
2-
===================
1+
# link-rel-parser-php
32

43
[![Build Status](https://travis-ci.org/indieweb/link-rel-parser-php.png?branch=master)](http://travis-ci.org/indieweb/link-rel-parser-php)
54

@@ -12,3 +11,24 @@ To run the tests, run this on your command line:
1211
```
1312
./phpunit.phar
1413
```
14+
15+
16+
## Contributing
17+
18+
Bug reports and pull requests are welcome on GitHub at https://github.com/indieweb/link_rel_parser-ruby/issues. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [IndieWebCamp Code of Conduct](https://indiewebcamp.com/code-of-conduct).
19+
20+
1. Fork it
21+
2. Get it running
22+
3. Create your feature branch (`git checkout -b my-new-feature`)
23+
4. Write your code and **tests**
24+
5. Commit your changes (`git commit -am 'Add some feature'`)
25+
6. Push to the branch (`git push origin my-new-feature`)
26+
7. Create new Pull Request
27+
28+
If you find bugs, have feature requests or questions, please
29+
[file an issue](https://github.com/indieweb/link-rel-parser-php/issues).
30+
31+
32+
## Code of Conduct
33+
34+
Everyone interacting in the link-rel-parser-php codebase, issue tracker, chat room, and mailing lists is expected to follow the [link-rel-parser-php code of conduct](https://indiewebcamp.com/code-of-conduct).
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
?>

src/IndieWeb/link_rel_parser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/*
55
http_rels, head_http_rels by Tantek Çelik http://tantek.com/
66
license: http://creativecommons.org/publicdomain/zero/1.0/
7+
depends on: get_absolute_uri in https://github.com/tantek/cassis/cassis.js
78
*/
89

910
/**

0 commit comments

Comments
 (0)