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