TYPO3 CMS  TYPO3_6-2
Manager.php
Go to the documentation of this file.
1 <?php
2 
24  function set($name, $value)
25  {
26  $_SESSION[$name] = $value;
27  }
28 
38  function get($name, $default=null)
39  {
40  if (isset($_SESSION) && array_key_exists($name, $_SESSION)) {
41  return $_SESSION[$name];
42  } else {
43  return $default;
44  }
45  }
46 
52  function del($name)
53  {
54  unset($_SESSION[$name]);
55  }
56 
60  function contents()
61  {
62  return $_SESSION;
63  }
64 }
65 
82  function check($data)
83  {
84  return true;
85  }
86 
97  function fromSession($data)
98  {
99  if (!$data) {
100  return null;
101  }
102 
103  $required = $this->requiredKeys();
104 
105  foreach ($required as $k) {
106  if (!array_key_exists($k, $data)) {
107  return null;
108  }
109  }
110 
111  if (!$this->check($data)) {
112  return null;
113  }
114 
115  $data = array_merge($data, $this->prepareForLoad($data));
116  $obj = $this->newObject($data);
117 
118  if (!$obj) {
119  return null;
120  }
121 
122  foreach ($required as $k) {
123  $obj->$k = $data[$k];
124  }
125 
126  return $obj;
127  }
128 
136  function prepareForLoad($data)
137  {
138  return array();
139  }
140 
149  function newObject($data)
150  {
151  return null;
152  }
153 
162  function toSession($obj)
163  {
164  $data = array();
165  foreach ($obj as $k => $v) {
166  $data[$k] = $v;
167  }
168 
169  $extra = $this->prepareForSave($obj);
170 
171  if ($extra && is_array($extra)) {
172  foreach ($extra as $k => $v) {
173  $data[$k] = $v;
174  }
175  }
176 
177  return $data;
178  }
179 
185  function prepareForSave($obj)
186  {
187  return array();
188  }
189 }
190 
197  function newObject($data)
198  {
199  return new Auth_OpenID_ServiceEndpoint();
200  }
201 
202  function requiredKeys()
203  {
204  $obj = new Auth_OpenID_ServiceEndpoint();
205  $data = array();
206  foreach ($obj as $k => $v) {
207  $data[] = $k;
208  }
209  return $data;
210  }
211 
212  function check($data)
213  {
214  return is_array($data['type_uris']);
215  }
216 }
217 
224  function requiredKeys()
225  {
226  return array('starting_url',
227  'yadis_url',
228  'services',
229  'session_key',
230  '_current',
231  'stale');
232  }
233 
234  function newObject($data)
235  {
236  return new Auth_Yadis_Manager($data['starting_url'],
237  $data['yadis_url'],
238  $data['services'],
239  $data['session_key']);
240  }
241 
242  function check($data)
243  {
244  return is_array($data['services']);
245  }
246 
247  function prepareForLoad($data)
248  {
249  $loader = new Auth_OpenID_ServiceEndpointLoader();
250  $services = array();
251  foreach ($data['services'] as $s) {
252  $services[] = $loader->fromSession($s);
253  }
254  return array('services' => $services);
255  }
256 
257  function prepareForSave($obj)
258  {
259  $loader = new Auth_OpenID_ServiceEndpointLoader();
260  $services = array();
261  foreach ($obj->services as $s) {
262  $services[] = $loader->toSession($s);
263  }
264  return array('services' => $services);
265  }
266 }
267 
277 
283  function Auth_Yadis_Manager($starting_url, $yadis_url,
284  $services, $session_key)
285  {
286  // The URL that was used to initiate the Yadis protocol
287  $this->starting_url = $starting_url;
288 
289  // The URL after following redirects (the identifier)
290  $this->yadis_url = $yadis_url;
291 
292  // List of service elements
293  $this->services = $services;
294 
295  $this->session_key = $session_key;
296 
297  // Reference to the current service object
298  $this->_current = null;
299 
300  // Stale flag for cleanup if PHP lib has trouble.
301  $this->stale = false;
302  }
303 
307  function length()
308  {
309  // How many untried services remain?
310  return count($this->services);
311  }
312 
319  function nextService()
320  {
321 
322  if ($this->services) {
323  $this->_current = array_shift($this->services);
324  } else {
325  $this->_current = null;
326  }
327 
328  return $this->_current;
329  }
330 
334  function current()
335  {
336  // Return the current service.
337  // Returns None if there are no services left.
338  return $this->_current;
339  }
340 
344  function forURL($url)
345  {
346  return in_array($url, array($this->starting_url, $this->yadis_url));
347  }
348 
352  function started()
353  {
354  // Has the first service been returned?
355  return $this->_current !== null;
356  }
357 }
358 
370 
374  var $DEFAULT_SUFFIX = 'auth';
375 
379  var $PREFIX = '_yadis_services_';
380 
390  function Auth_Yadis_Discovery($session, $url,
391  $session_key_suffix = null)
392  {
394  $this->session = $session;
395  $this->url = $url;
396  if ($session_key_suffix === null) {
397  $session_key_suffix = $this->DEFAULT_SUFFIX;
398  }
399 
400  $this->session_key_suffix = $session_key_suffix;
401  $this->session_key = $this->PREFIX . $this->session_key_suffix;
402  }
403 
408  function getNextService($discover_cb, $fetcher)
409  {
410  $manager = $this->getManager();
411  if (!$manager || (!$manager->services)) {
412  $this->destroyManager();
413 
414  list($yadis_url, $services) = call_user_func_array($discover_cb,
415  array(
416  $this->url,
417  $fetcher,
418  ));
419 
420  $manager = $this->createManager($services, $yadis_url);
421  }
422 
423  if ($manager) {
424  $loader = new Auth_Yadis_ManagerLoader();
425  $service = $manager->nextService();
426  $this->session->set($this->session_key,
427  serialize($loader->toSession($manager)));
428  } else {
429  $service = null;
430  }
431 
432  return $service;
433  }
434 
443  function cleanup($force=false)
444  {
445  $manager = $this->getManager($force);
446  if ($manager) {
447  $service = $manager->current();
448  $this->destroyManager($force);
449  } else {
450  $service = null;
451  }
452 
453  return $service;
454  }
455 
459  function getSessionKey()
460  {
461  // Get the session key for this starting URL and suffix
462  return $this->PREFIX . $this->session_key_suffix;
463  }
464 
471  function getManager($force=false)
472  {
473  // Extract the YadisServiceManager for this object's URL and
474  // suffix from the session.
475 
476  $manager_str = $this->session->get($this->getSessionKey());
477  $manager = null;
478 
479  if ($manager_str !== null) {
480  $loader = new Auth_Yadis_ManagerLoader();
481  $manager = $loader->fromSession(unserialize($manager_str));
482  }
483 
484  if ($manager && ($manager->forURL($this->url) || $force)) {
485  return $manager;
486  }
487  }
488 
492  function createManager($services, $yadis_url = null)
493  {
494  $key = $this->getSessionKey();
495  if ($this->getManager()) {
496  return $this->getManager();
497  }
498 
499  if ($services) {
500  $loader = new Auth_Yadis_ManagerLoader();
501  $manager = new Auth_Yadis_Manager($this->url, $yadis_url,
502  $services, $key);
503  $this->session->set($this->session_key,
504  serialize($loader->toSession($manager)));
505  return $manager;
506  }
507  }
508 
515  function destroyManager($force=false)
516  {
517  if ($this->getManager($force) !== null) {
518  $key = $this->getSessionKey();
519  $this->session->del($key);
520  }
521  }
522 }
523 
Auth_Yadis_Discovery($session, $url, $session_key_suffix=null)
Definition: Manager.php:390
cleanup($force=false)
Definition: Manager.php:443
Auth_Yadis_Manager($starting_url, $yadis_url, $services, $session_key)
Definition: Manager.php:283
getNextService($discover_cb, $fetcher)
Definition: Manager.php:408
destroyManager($force=false)
Definition: Manager.php:515
getManager($force=false)
Definition: Manager.php:471
createManager($services, $yadis_url=null)
Definition: Manager.php:492