Skip to content

Commit

Permalink
Merge branch 'master' of github.com:indieweb/link-rel-parser-php
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpk committed Jan 11, 2017
2 parents d10448c + 7c4c729 commit 295420e
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Contributor Code of Conduct

See the Code of Conduct on the IndieWebCamp wiki.

https://indiewebcamp.com/code-of-conduct
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
link-rel-parser-php
===================
# link-rel-parser-php

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

Expand All @@ -12,3 +11,24 @@ To run the tests, run this on your command line:
```
./phpunit.phar
```


## Contributing

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).

1. Fork it
2. Get it running
3. Create your feature branch (`git checkout -b my-new-feature`)
4. Write your code and **tests**
5. Commit your changes (`git commit -am 'Add some feature'`)
6. Push to the branch (`git push origin my-new-feature`)
7. Create new Pull Request

If you find bugs, have feature requests or questions, please
[file an issue](https://github.com/indieweb/link-rel-parser-php/issues).


## Code of Conduct

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).
133 changes: 133 additions & 0 deletions src/IndieWeb/get_rel_webmention.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/* sample functions for using the link rel parser library to get first webmention (and pingback) endpoints */

/*
first_linknode_href, get_rel_webmention by Tantek Çelik http://tantek.com/
license: http://creativecommons.org/publicdomain/zero/1.0/
depends on: link_rel_parser.php (e.g. head_http_rels)
depends on: https://github.com/tantek/cassis/cassis.js (e.g. contains, get_absolute_uri, is_html_type, xphasrel, strcat)
*/

// Could move this function to cassis.js if more broadly useful
function is_loopback($href) {
// in: $href URL
// out: boolean whether host of URL is a loopback address
$host = hostname_of_uri($href);
if ($host == 'localhost') { return true; }
$host = explode('.', $host);
return ($host.length == 4 &&
$host[0] == 127 &&
ctype_digit($host[1]) &&
ctype_digit($host[2]) &&
ctype_digit($host[3]));
}

function first_linknode_href($links, $spacedtagnames='a area link') {
// in: DOMNodeList $links
// $spacedtagnames - space separated tag names, null for any
// out: href attribute as string
// return href of first DOMNode in $links that is an a, area, link
// with href that is not a loopback address
// else return null
if ($spacedtagnames) {
$spacedtagnames = strcat(' ', $spacedtagnames, ' ');
}
foreach ($links as $link) {
if (!$spacedtagnames ||
contains($spacedtagnames,
strcat(' ', $link->nodeName, ' '))) {
$href = $link->getAttribute('href');
if (!is_loopback($href))
{
return $href;
}
}
}
return null;
}

// in: $url of page that may or may not have a webmention endpoint
// out: array of 'webmention' URL of webmention endpoint if any,
// and 'pingback' URL of pingback endpoint if any
function get_rel_webmention($url) {
global $debug;
$r = array();
$r['webmention'] = '';
$r['pingback'] = '';

$httprels = head_http_rels($url);
if ($debug) {
echo 'head_http_rels STATUS:"'.$httprels['status'].'"<br/>';
}
if ($httprels['status'] != "200") {
return $r;
}

if ($debug) {
echo 'HEAD Content-Type: '.$httprels['type'].' '.
string(is_html_type($httprels['type'])).'<br/>';
}
$wm = '';
$pb = '';
if (array_key_exists('webmention', $httprels['rels'])) {
$wm = $httprels['rels']['webmention'][0];
// just use the first one.
}
if (array_key_exists('pingback', $httprels['rels'])) {
$pb = $httprels['rels']['pingback'][0];
// just use the first one.
}
if ($debug && $wm) {
echo "HEAD LINK webmention: '$wm'<br/>";
}
if ($debug && $pb) {
echo "HEAD LINK pingback: '$pb'<br/>";
}
if (!$wm && is_html_type($httprels['type'])) {
// no webmention endpoint in HTTP headers, check HTML
if ($debug) {
echo "looking for wm endpoint in HTML $url<br/>";
}
$ch = curl_init($url);
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// commented out due to:
// Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$s = curl_exec($ch);
curl_close($ch);
if ($s != '') {
$dom = new DOMDocument();
$dom->loadHTML($s); // ** maybe only load part of it?
$domx = new DOMXPath($dom);
$wms = $domx->query(xphasrel('webmention'));
if ($wms) { $wms = first_linknode_href($wms); }
if ($debug) {
echo "query xphasrel webmention $wms<br/>";
}

if ($wms !== null) {
$wm = get_absolute_uri($wms, $url);
}
if ($debug && $wm) {
echo "HTML rel=webmention returned '$wm'<br/>";
}
$wms = $domx->query(xphasrel('pingback'));
if ($wms) { $wms = first_linknode_href($wms, 'link'); }
if ($debug) {
echo "query xphasrel pingback $wms<br/>";
}

if ($wms !== null) {
$pb = get_absolute_uri($wms, $url);
}
if ($debug && $pb) {
echo "HTML rel=pingback returned '$pb'<br/>";
}
}
}
$r['webmention'] = $wm;
$r['pingback'] = $pb;
return $r;
}

?>
1 change: 1 addition & 0 deletions src/IndieWeb/link_rel_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/*
http_rels, head_http_rels by Tantek Çelik http://tantek.com/
license: http://creativecommons.org/publicdomain/zero/1.0/
depends on: get_absolute_uri in https://github.com/tantek/cassis/cassis.js
*/

/**
Expand Down

0 comments on commit 295420e

Please sign in to comment.