Skip to content

Commit 1558cf1

Browse files
committed
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
1 parent eda9800 commit 1558cf1

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/IndieWeb/link_rel_parser.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77
*/
88

99
/**
10-
* @param string $h HTTP headers
11-
* @return array $rels rel values as indices to arrays of URLs
10+
* @param string $h HTTP headers as a string
11+
* @param string $url optional base URL to resolve relative URLs
12+
* @return array $rels rel values as indices to arrays of URLs, empty array if no rels at all
1213
*/
13-
function http_rels($h) {
14+
function http_rels($h, $url = '') {
1415
$h = preg_replace("/(\r\n|\r)/", "\n", $h);
1516
$h = explode("\n", preg_replace("/(\n)[ \t]+/", " ", $h));
1617
$rels = array();
1718
foreach ($h as $f) {
18-
if (!strncmp($f, 'X-Pingback: ', 12)) {
19+
if (!strncasecmp($f, 'X-Pingback: ', 12)) {
1920
// convert to a link header and have common code handle it
2021
$f = 'Link: <' . trim(substr($f, 12)) . '>; rel="pingback"';
2122
}
22-
if (!strncmp($f, 'Link: ', 6)) {
23+
if (!strncasecmp($f, 'Link: ', 6)) {
2324
$links = explode(', ', trim(substr($f, 6)));
2425
foreach ($links as $link) {
2526
$hrefandrel = explode('; ', $link);
@@ -38,6 +39,9 @@ function http_rels($h) {
3839
if (!array_key_exists($rel, $rels)) {
3940
$rels[$rel] = array();
4041
}
42+
if ($url) {
43+
$href = get_absolute_uri($href, $url);
44+
}
4145
if (!in_array($href, $rels[$rel])) {
4246
$rels[$rel][] = $href;
4347
}
@@ -52,7 +56,7 @@ function http_rels($h) {
5256

5357
/**
5458
* @param $url URL to get HTTP HEAD Link (and effective/x-extended) rels
55-
* @return array "status"=> HTTP status code, "type"=> HTTP Content-Type, "rels" array with http_rels return value
59+
* @return array "status"=> HTTP status code, "type"=> HTTP Content-Type, "rels" array with http_rels return value. empty array if no rels
5660
*/
5761
function head_http_rels($url) {
5862
$c = curl_init();
@@ -61,20 +65,22 @@ function head_http_rels($url) {
6165
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 2);
6266
curl_setopt($c, CURLOPT_TIMEOUT, 4);
6367
curl_setopt($c, CURLOPT_USERAGENT, 'head_http_rels function');
64-
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
68+
// curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
69+
// commented out due to:
70+
// Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set
6571
curl_setopt($c, CURLOPT_SSL_VERIFYPEER , false );
6672
curl_setopt($c, CURLOPT_SSL_VERIFYHOST , false );
6773
curl_setopt($c, CURLOPT_HEADER, true);
6874
curl_setopt($c, CURLOPT_NOBODY, true);
6975
$h = curl_exec($c);
70-
$i = curl_getinfo($c);
76+
$i = curl_getinfo($c);
7177
curl_close($c);
7278
unset($c);
7379

7480
$r = array();
75-
$r['status'] = $i['http_code'];
81+
$r['status'] = string($i['http_code']);
7682
$r['type'] = $i['content_type'];
77-
$r['rels'] = http_rels($h);
83+
$r['rels'] = http_rels($h, $url);
7884
return $r;
7985
}
8086
?>

0 commit comments

Comments
 (0)