]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/linkheader.php
Merge remote branch 'gitorious/testing' into testing
[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
51 class LinkHeader
52 {
53     var $href;
54     var $rel;
55     var $type;
56
57     /**
58      * Initialize from a string
59      *
60      * @param string $str Link: header value
61      *
62      * @return LinkHeader self
63      */
64
65     function __construct($str)
66     {
67         preg_match('/^<[^>]+>/', $str, $uri_reference);
68         //if (empty($uri_reference)) return;
69
70         $this->href = trim($uri_reference[0], '<>');
71         $this->rel  = array();
72         $this->type = null;
73
74         // remove uri-reference from header
75         $str = substr($str, strlen($uri_reference[0]));
76
77         // parse link-params
78         $params = explode(';', $str);
79
80         foreach ($params as $param) {
81             if (empty($param)) { 
82                 continue;
83             }
84             list($param_name, $param_value) = explode('=', $param, 2);
85
86             $param_name  = trim($param_name);
87             $param_value = preg_replace('(^"|"$)', '', trim($param_value));
88
89             // for now we only care about 'rel' and 'type' link params
90             // TODO do something with the other links-params
91             switch ($param_name) {
92             case 'rel':
93                 $this->rel = trim($param_value);
94                 break;
95
96             case 'type':
97                 $this->type = trim($param_value);
98             }
99         }
100     }
101
102     /**
103      * Given an HTTP response, return the requested Link: header
104      *
105      * @param HTTP_Request2_Response $response response to check
106      * @param string                 $rel      relationship to look for
107      * @param string                 $type     media type to look for
108      *
109      * @return LinkHeader discovered header, or null on failure
110      */
111
112     static function getLink($response, $rel=null, $type=null)
113     {
114         $headers = $response->getHeader('Link');
115         if ($headers) {
116             // Can get an array or string, so try to simplify the path
117             if (!is_array($headers)) {
118                 $headers = array($headers);
119             }
120
121             foreach ($headers as $header) {
122                 $lh = new LinkHeader($header);
123
124                 if ((is_null($rel) || $lh->rel == $rel) &&
125                     (is_null($type) || $lh->type == $type)) {
126                     return $lh->href;
127                 }
128             }
129         }
130         return null;
131     }
132 }