]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/linkheader.php
Fix for ticket 2756 - Calls to OAuth endpoints are redirected to the
[quix0rs-gnu-social.git] / plugins / OStatus / lib / linkheader.php
1 <?php
2
3 class LinkHeader
4 {
5     var $href;
6     var $rel;
7     var $type;
8
9     function __construct($str)
10     {
11         preg_match('/^<[^>]+>/', $str, $uri_reference);
12         //if (empty($uri_reference)) return;
13
14         $this->href = trim($uri_reference[0], '<>');
15         $this->rel = array();
16         $this->type = null;
17
18         // remove uri-reference from header
19         $str = substr($str, strlen($uri_reference[0]));
20
21         // parse link-params
22         $params = explode(';', $str);
23
24         foreach ($params as $param) {
25             if (empty($param)) continue;
26             list($param_name, $param_value) = explode('=', $param, 2);
27             $param_name = trim($param_name);
28             $param_value = preg_replace('(^"|"$)', '', trim($param_value));
29
30             // for now we only care about 'rel' and 'type' link params
31             // TODO do something with the other links-params
32             switch ($param_name) {
33             case 'rel':
34                 $this->rel = trim($param_value);
35                 break;
36
37             case 'type':
38                 $this->type = trim($param_value);
39             }
40         }
41     }
42
43     static function getLink($response, $rel=null, $type=null)
44     {
45         $headers = $response->getHeader('Link');
46         if ($headers) {
47             // Can get an array or string, so try to simplify the path
48             if (!is_array($headers)) {
49                 $headers = array($headers);
50             }
51
52             foreach ($headers as $header) {
53                 $lh = new LinkHeader($header);
54
55                 if ((is_null($rel) || $lh->rel == $rel) &&
56                     (is_null($type) || $lh->type == $type)) {
57                     return $lh->href;
58                 }
59             }
60         }
61         return null;
62     }
63 }