ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 (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 
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 
248  {
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  {
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($discover_cb,
415  $this->url,
416  $fetcher);
417 
418  $manager = $this->createManager($services, $yadis_url);
419  }
420 
421  if ($manager) {
423  $service = $manager->nextService();
424  $this->session->set($this->session_key,
425  serialize($loader->toSession($manager)));
426  } else {
427  $service = null;
428  }
429 
430  return $service;
431  }
432 
441  function cleanup($force=false)
442  {
443  $manager = $this->getManager($force);
444  if ($manager) {
445  $service = $manager->current();
446  $this->destroyManager($force);
447  } else {
448  $service = null;
449  }
450 
451  return $service;
452  }
453 
457  function getSessionKey()
458  {
459  // Get the session key for this starting URL and suffix
460  return $this->PREFIX . $this->session_key_suffix;
461  }
462 
469  function getManager($force=false)
470  {
471  // Extract the YadisServiceManager for this object's URL and
472  // suffix from the session.
473 
474  $manager_str = $this->session->get($this->getSessionKey());
475  $manager = null;
476 
477  if ($manager_str !== null) {
479  $manager = $loader->fromSession(unserialize($manager_str));
480  }
481 
482  if ($manager && ($manager->forURL($this->url) || $force)) {
483  return $manager;
484  }
485  }
486 
490  function createManager($services, $yadis_url = null)
491  {
492  $key = $this->getSessionKey();
493  if ($this->getManager()) {
494  return $this->getManager();
495  }
496 
497  if ($services) {
499  $manager = new Auth_Yadis_Manager($this->url, $yadis_url,
500  $services, $key);
501  $this->session->set($this->session_key,
502  serialize($loader->toSession($manager)));
503  return $manager;
504  }
505  }
506 
513  function destroyManager($force=false)
514  {
515  if ($this->getManager($force) !== null) {
516  $key = $this->getSessionKey();
517  $this->session->del($key);
518  }
519  }
520 }
521