|
1 | 1 | <?php |
2 | 2 | namespace IndieWeb; |
3 | 3 |
|
4 | | -function http_rels($headerString) { |
| 4 | +/* |
| 5 | +http_rels, head_http_rels by Tantek Çelik http://tantek.com/ |
| 6 | +license: http://creativecommons.org/publicdomain/zero/1.0/ |
| 7 | +*/ |
5 | 8 |
|
6 | | - // TODO: Implement this |
| 9 | +// in $h: HTTP headers as a string |
| 10 | +// returns: array of rel values as indices to arrays of URLs |
| 11 | +function http_rels($h) { |
| 12 | + $h = explode("\r\n", preg_replace("/(\r\n|\r|\n)[ \t]+/", " ", $h)); |
| 13 | + $rels = array(); |
| 14 | + foreach ($h as $f) { |
| 15 | + if (!strncmp($f, 'X-Pingback: ', 12)) { |
| 16 | + // convert to a link header and have common code handle it |
| 17 | + $f = 'Link: <' . trim(substr($f, 12)) . '>; rel="pingback"'; |
| 18 | + } |
| 19 | + if (!strncmp($f, 'Link: ', 6)) { |
| 20 | + $links = explode(', ', trim(substr($f, 6))); |
| 21 | + foreach ($links as $link) { |
| 22 | + $hrefandrel = explode('; ', $link); |
| 23 | + $href = trim($hrefandrel[0], '<>'); |
| 24 | + $relarray = ''; |
| 25 | + foreach ($hrefandrel as $p) { |
| 26 | + if (!strncmp($p, 'rel=', 4)) { |
| 27 | + $relarray = explode(' ', trim(substr($p, 4), '"\'')); |
| 28 | + break; |
| 29 | + } |
| 30 | + } |
| 31 | + if ($relarray !== '') { // ignore Link: headers without rel |
| 32 | + foreach ($relarray as $rel) { |
| 33 | + $rel = strtolower(trim($rel)); |
| 34 | + if ($rel != '') { |
| 35 | + if (!$rels[$rel]) { |
| 36 | + $rels[$rel] = array(); |
| 37 | + } |
| 38 | + if (!in_array($href, $rels[$rel])) { |
| 39 | + $rels[$rel][] = $href; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + return $rels; |
| 48 | +} |
| 49 | + |
| 50 | +// in $url: URL to get HTTP HEAD Link (and effective/x-extended) rels |
| 51 | +// returns: array of |
| 52 | +// "status"=> HTTP status code, |
| 53 | +// "rels" array with |
| 54 | +function head_http_rels($url) { |
| 55 | + $c = curl_init(); |
| 56 | + curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); |
| 57 | + curl_setopt($c, CURLOPT_URL, $url); |
| 58 | + curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 2); |
| 59 | + curl_setopt($c, CURLOPT_TIMEOUT, 4); |
| 60 | + curl_setopt($c, CURLOPT_USERAGENT, 'head_http_rels function'); |
| 61 | + curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); |
| 62 | + curl_setopt($c, CURLOPT_SSL_VERIFYPEER , false ); |
| 63 | + curl_setopt($c, CURLOPT_SSL_VERIFYHOST , false ); |
| 64 | + curl_setopt($c, CURLOPT_HEADER, true); |
| 65 | + curl_setopt($c, CURLOPT_NOBODY, true); |
| 66 | + $h = curl_exec($c); |
| 67 | + $i = curl_getinfo($c); |
| 68 | + curl_close($c); |
| 69 | + unset($c); |
7 | 70 |
|
8 | | - return array( |
9 | | - 'rels' => array() |
10 | | - ); |
| 71 | + $r = array(); |
| 72 | + $r['status'] = $i['http_code']; |
| 73 | + $r['rels'] = http_rels($h); |
| 74 | + return $r; |
11 | 75 | } |
| 76 | +?> |
0 commit comments