ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
Yadis.php
Go to the documentation of this file.
1 <?php
2 
20 require_once "Auth/Yadis/PlainHTTPFetcher.php";
21 require_once "Auth/Yadis/ParanoidHTTPFetcher.php";
22 
26 require_once "Auth/Yadis/ParseHTML.php";
27 
31 require_once "Auth/Yadis/XRDS.php";
32 
36 define('Auth_Yadis_CONTENT_TYPE', 'application/xrds+xml');
37 
41 define('Auth_Yadis_HEADER_NAME', 'X-XRDS-Location');
42 
49 
50  // The URI that was passed to the fetcher
51  var $request_uri = null;
52 
53  // The result of following redirects from the request_uri
54  var $normalized_uri = null;
55 
56  // The URI from which the response text was returned (set to
57  // None if there was no XRDS document found)
58  var $xrds_uri = null;
59 
60  var $xrds = null;
61 
62  // The content-type returned with the response_text
63  var $content_type = null;
64 
65  // The document returned from the xrds_uri
66  var $response_text = null;
67 
68  // Did the discovery fail miserably?
69  var $failed = false;
70 
72  {
73  // Initialize the state of the object
74  // sets all attributes to None except the request_uri
75  $this->request_uri = $request_uri;
76  }
77 
78  function fail()
79  {
80  $this->failed = true;
81  }
82 
83  function isFailure()
84  {
85  return $this->failed;
86  }
87 
96  function services()
97  {
98  if ($this->xrds) {
99  return $this->xrds->services();
100  }
101 
102  return null;
103  }
104 
105  function usedYadisLocation()
106  {
107  // Was the Yadis protocol's indirection used?
108  return ($this->xrds_uri && $this->normalized_uri != $this->xrds_uri);
109  }
110 
111  function isXRDS()
112  {
113  // Is the response text supposed to be an XRDS document?
114  return ($this->usedYadisLocation() ||
115  $this->content_type == Auth_Yadis_CONTENT_TYPE);
116  }
117 }
118 
136 function Auth_Yadis_getServiceEndpoints($input_url, $xrds_parse_func,
137  $discover_func=null, $fetcher=null)
138 {
139  if ($discover_func === null) {
140  $discover_function = array('Auth_Yadis_Yadis', 'discover');
141  }
142 
143  $yadis_result = call_user_func_array($discover_func,
144  array($input_url, &$fetcher));
145 
146  if ($yadis_result === null) {
147  return array($input_url, array());
148  }
149 
150  $endpoints = call_user_func_array($xrds_parse_func,
151  array($yadis_result->normalized_uri,
152  $yadis_result->response_text));
153 
154  if ($endpoints === null) {
155  $endpoints = array();
156  }
157 
158  return array($yadis_result->normalized_uri, $endpoints);
159 }
160 
243 
253  static function getHTTPFetcher($timeout = 20)
254  {
256  (!defined('Auth_Yadis_CURL_OVERRIDE'))) {
257  $fetcher = new Auth_Yadis_ParanoidHTTPFetcher($timeout);
258  } else {
259  $fetcher = new Auth_Yadis_PlainHTTPFetcher($timeout);
260  }
261  return $fetcher;
262  }
263 
264  static function curlPresent()
265  {
266  return function_exists('curl_init');
267  }
268 
272  static function _getHeader($header_list, $names)
273  {
274  foreach ($header_list as $name => $value) {
275  foreach ($names as $n) {
276  if (strtolower($name) == strtolower($n)) {
277  return $value;
278  }
279  }
280  }
281 
282  return null;
283  }
284 
288  static function _getContentType($content_type_header)
289  {
290  if ($content_type_header) {
291  $parts = explode(";", $content_type_header);
292  return strtolower($parts[0]);
293  }
294  }
295 
320  static function discover($uri, $fetcher,
321  $extra_ns_map = null, $timeout = 20)
322  {
324 
325  $request_uri = $uri;
326  $headers = array("Accept: " . Auth_Yadis_CONTENT_TYPE .
327  ', text/html; q=0.3, application/xhtml+xml; q=0.5');
328 
329  if ($fetcher === null) {
330  $fetcher = Auth_Yadis_Yadis::getHTTPFetcher($timeout);
331  }
332 
333  $response = $fetcher->get($uri, $headers);
334 
335  if (!$response || ($response->status != 200 and
336  $response->status != 206)) {
337  $result->fail();
338  return $result;
339  }
340 
341  $result->normalized_uri = $response->final_url;
342  $result->content_type = Auth_Yadis_Yadis::_getHeader(
343  $response->headers,
344  array('content-type'));
345 
346  if ($result->content_type &&
347  (Auth_Yadis_Yadis::_getContentType($result->content_type) ==
349  $result->xrds_uri = $result->normalized_uri;
350  } else {
351  $yadis_location = Auth_Yadis_Yadis::_getHeader(
352  $response->headers,
353  array(Auth_Yadis_HEADER_NAME));
354 
355  if (!$yadis_location) {
356  $parser = new Auth_Yadis_ParseHTML();
357  $yadis_location = $parser->getHTTPEquiv($response->body);
358  }
359 
360  if ($yadis_location) {
361  $result->xrds_uri = $yadis_location;
362 
363  $response = $fetcher->get($yadis_location);
364 
365  if ((!$response) || ($response->status != 200 and
366  $response->status != 206)) {
367  $result->fail();
368  return $result;
369  }
370 
371  $result->content_type = Auth_Yadis_Yadis::_getHeader(
372  $response->headers,
373  array('content-type'));
374  }
375  }
376 
377  $result->response_text = $response->body;
378  return $result;
379  }
380 }
381 
382