]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/linkheader.php
* fix some i18n and L10n issues
[quix0rs-gnu-social.git] / lib / linkheader.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Parse HTTP response for interesting Link: headers
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Discovery
24  * @package   StatusNet
25  * @author    James Walker <james@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Class to represent Link: headers in an HTTP response
37  *
38  * Since these are a fairly important part of Hammer-stack discovery, they're
39  * reified and implemented here.
40  *
41  * @category  Discovery
42  * @package   StatusNet
43  * @author    James Walker <james@status.net>
44  * @copyright 2010 StatusNet, Inc.
45  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
46  * @link      http://status.net/
47  *
48  * @see       Discovery
49  */
50 class LinkHeader
51 {
52     var $href;
53     var $rel;
54     var $type;
55
56     /**
57      * Initialize from a string
58      *
59      * @param string $str Link: header value
60      *
61      * @return LinkHeader self
62      */
63     function __construct($str)
64     {
65         preg_match('/^<[^>]+>/', $str, $uri_reference);
66         //if (empty($uri_reference)) return;
67
68         $this->href = trim($uri_reference[0], '<>');
69         $this->rel  = array();
70         $this->type = null;
71
72         // remove uri-reference from header
73         $str = substr($str, strlen($uri_reference[0]));
74
75         // parse link-params
76         $params = explode(';', $str);
77
78         foreach ($params as $param) {
79             if (empty($param)) {
80                 continue;
81             }
82             list($param_name, $param_value) = explode('=', $param, 2);
83
84             $param_name  = trim($param_name);
85             $param_value = preg_replace('(^"|"$)', '', trim($param_value));
86
87             // for now we only care about 'rel' and 'type' link params
88             // TODO do something with the other links-params
89             switch ($param_name) {
90             case 'rel':
91                 $this->rel = trim($param_value);
92                 break;
93
94             case 'type':
95                 $this->type = trim($param_value);
96             }
97         }
98     }
99
100     /**
101      * Given an HTTP response, return the requested Link: header
102      *
103      * @param HTTP_Request2_Response $response response to check
104      * @param string                 $rel      relationship to look for
105      * @param string                 $type     media type to look for
106      *
107      * @return LinkHeader discovered header, or null on failure
108      */
109     static function getLink($response, $rel=null, $type=null)
110     {
111         $headers = $response->getHeader('Link');
112         if ($headers) {
113             // Can get an array or string, so try to simplify the path
114             if (!is_array($headers)) {
115                 $headers = array($headers);
116             }
117
118             foreach ($headers as $header) {
119                 $lh = new LinkHeader($header);
120
121                 if ((is_null($rel) || $lh->rel == $rel) &&
122                     (is_null($type) || $lh->type == $type)) {
123                     return $lh->href;
124                 }
125             }
126         }
127         return null;
128     }
129 }