Skip to content
Browse files

handle case-insensitive LinK, relative URLs

handle case-insensitive LinK header; added optional $url param to
http_rels so it can turn relative Link header URLs into absolute URLs;
disable CURLOPT_FOLLOWLOCATION because it causes PHP warnings
  • Loading branch information...
1 parent eda9800 commit 1558cf145091268f95c9a7e4f2813d5e2a9fa0d1 @tantek tantek committed Apr 11, 2016
Showing with 16 additions and 10 deletions.
  1. +16 −10 src/IndieWeb/link_rel_parser.php
View
26 src/IndieWeb/link_rel_parser.php
@@ -7,19 +7,20 @@
*/
/**
- * @param string $h HTTP headers
- * @return array $rels rel values as indices to arrays of URLs
+ * @param string $h HTTP headers as a string
+ * @param string $url optional base URL to resolve relative URLs
+ * @return array $rels rel values as indices to arrays of URLs, empty array if no rels at all
*/
-function http_rels($h) {
+function http_rels($h, $url = '') {
$h = preg_replace("/(\r\n|\r)/", "\n", $h);
$h = explode("\n", preg_replace("/(\n)[ \t]+/", " ", $h));
$rels = array();
foreach ($h as $f) {
- if (!strncmp($f, 'X-Pingback: ', 12)) {
+ if (!strncasecmp($f, 'X-Pingback: ', 12)) {
// convert to a link header and have common code handle it
$f = 'Link: <' . trim(substr($f, 12)) . '>; rel="pingback"';
}
- if (!strncmp($f, 'Link: ', 6)) {
+ if (!strncasecmp($f, 'Link: ', 6)) {
$links = explode(', ', trim(substr($f, 6)));
foreach ($links as $link) {
$hrefandrel = explode('; ', $link);
@@ -38,6 +39,9 @@ function http_rels($h) {
if (!array_key_exists($rel, $rels)) {
$rels[$rel] = array();
}
+ if ($url) {
+ $href = get_absolute_uri($href, $url);
+ }
if (!in_array($href, $rels[$rel])) {
$rels[$rel][] = $href;
}
@@ -52,7 +56,7 @@ function http_rels($h) {
/**
* @param $url URL to get HTTP HEAD Link (and effective/x-extended) rels
- * @return array "status"=> HTTP status code, "type"=> HTTP Content-Type, "rels" array with http_rels return value
+ * @return array "status"=> HTTP status code, "type"=> HTTP Content-Type, "rels" array with http_rels return value. empty array if no rels
*/
function head_http_rels($url) {
$c = curl_init();
@@ -61,20 +65,22 @@ function head_http_rels($url) {
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($c, CURLOPT_TIMEOUT, 4);
curl_setopt($c, CURLOPT_USERAGENT, 'head_http_rels function');
- curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
+// curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
+// commented out due to:
+// Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set
curl_setopt($c, CURLOPT_SSL_VERIFYPEER , false );
curl_setopt($c, CURLOPT_SSL_VERIFYHOST , false );
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_NOBODY, true);
$h = curl_exec($c);
- $i = curl_getinfo($c);
+ $i = curl_getinfo($c);
curl_close($c);
unset($c);
$r = array();
- $r['status'] = $i['http_code'];
+ $r['status'] = string($i['http_code']);
$r['type'] = $i['content_type'];
- $r['rels'] = http_rels($h);
+ $r['rels'] = http_rels($h, $url);
return $r;
}
?>

0 comments on commit 1558cf1

Please sign in to comment.
Something went wrong with that request. Please try again.