123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- <?php
- class Auth_Yadis_PHPSession {
-
- function set($name, $value)
- {
- $_SESSION[$name] = $value;
- }
-
- function get($name, $default=null)
- {
- if (isset($_SESSION) && array_key_exists($name, $_SESSION)) {
- return $_SESSION[$name];
- } else {
- return $default;
- }
- }
-
- function del($name)
- {
- unset($_SESSION[$name]);
- }
-
- function contents()
- {
- return $_SESSION;
- }
- }
- class Auth_Yadis_SessionLoader {
-
- function check($data)
- {
- return true;
- }
-
- function fromSession($data)
- {
- if (!$data) {
- return null;
- }
- $required = $this->requiredKeys();
- foreach ($required as $k) {
- if (!array_key_exists($k, $data)) {
- return null;
- }
- }
- if (!$this->check($data)) {
- return null;
- }
- $data = array_merge($data, $this->prepareForLoad($data));
- $obj = $this->newObject($data);
- if (!$obj) {
- return null;
- }
- foreach ($required as $k) {
- $obj->$k = $data[$k];
- }
- return $obj;
- }
-
- function prepareForLoad($data)
- {
- return array();
- }
-
- function newObject($data)
- {
- return null;
- }
-
- function toSession($obj)
- {
- $data = array();
- foreach ($obj as $k => $v) {
- $data[$k] = $v;
- }
- $extra = $this->prepareForSave($obj);
- if ($extra && is_array($extra)) {
- foreach ($extra as $k => $v) {
- $data[$k] = $v;
- }
- }
- return $data;
- }
-
- function prepareForSave($obj)
- {
- return array();
- }
- }
- class Auth_OpenID_ServiceEndpointLoader extends Auth_Yadis_SessionLoader {
- function newObject($data)
- {
- return new Auth_OpenID_ServiceEndpoint();
- }
- function requiredKeys()
- {
- $obj = new Auth_OpenID_ServiceEndpoint();
- $data = array();
- foreach ($obj as $k => $v) {
- $data[] = $k;
- }
- return $data;
- }
- function check($data)
- {
- return is_array($data['type_uris']);
- }
- }
- class Auth_Yadis_ManagerLoader extends Auth_Yadis_SessionLoader {
- function requiredKeys()
- {
- return array('starting_url',
- 'yadis_url',
- 'services',
- 'session_key',
- '_current',
- 'stale');
- }
- function newObject($data)
- {
- return new Auth_Yadis_Manager($data['starting_url'],
- $data['yadis_url'],
- $data['services'],
- $data['session_key']);
- }
- function check($data)
- {
- return is_array($data['services']);
- }
- function prepareForLoad($data)
- {
- $loader = new Auth_OpenID_ServiceEndpointLoader();
- $services = array();
- foreach ($data['services'] as $s) {
- $services[] = $loader->fromSession($s);
- }
- return array('services' => $services);
- }
- function prepareForSave($obj)
- {
- $loader = new Auth_OpenID_ServiceEndpointLoader();
- $services = array();
- foreach ($obj->services as $s) {
- $services[] = $loader->toSession($s);
- }
- return array('services' => $services);
- }
- }
- class Auth_Yadis_Manager {
-
- function Auth_Yadis_Manager($starting_url, $yadis_url,
- $services, $session_key)
- {
-
- $this->starting_url = $starting_url;
-
- $this->yadis_url = $yadis_url;
-
- $this->services = $services;
- $this->session_key = $session_key;
-
- $this->_current = null;
-
- $this->stale = false;
- }
-
- function length()
- {
-
- return count($this->services);
- }
-
- function nextService()
- {
- if ($this->services) {
- $this->_current = array_shift($this->services);
- } else {
- $this->_current = null;
- }
- return $this->_current;
- }
-
- function current()
- {
-
-
- return $this->_current;
- }
-
- function forURL($url)
- {
- return in_array($url, array($this->starting_url, $this->yadis_url));
- }
-
- function started()
- {
-
- return $this->_current !== null;
- }
- }
- class Auth_Yadis_Discovery {
-
- var $DEFAULT_SUFFIX = 'auth';
-
- var $PREFIX = '_yadis_services_';
-
- function Auth_Yadis_Discovery($session, $url,
- $session_key_suffix = null)
- {
-
- $this->session = $session;
- $this->url = $url;
- if ($session_key_suffix === null) {
- $session_key_suffix = $this->DEFAULT_SUFFIX;
- }
- $this->session_key_suffix = $session_key_suffix;
- $this->session_key = $this->PREFIX . $this->session_key_suffix;
- }
-
- function getNextService($discover_cb, $fetcher)
- {
- $manager = $this->getManager();
- if (!$manager || (!$manager->services)) {
- $this->destroyManager();
- list($yadis_url, $services) = call_user_func_array($discover_cb,
- array(
- $this->url,
- $fetcher,
- ));
- $manager = $this->createManager($services, $yadis_url);
- }
- if ($manager) {
- $loader = new Auth_Yadis_ManagerLoader();
- $service = $manager->nextService();
- $this->session->set($this->session_key,
- serialize($loader->toSession($manager)));
- } else {
- $service = null;
- }
- return $service;
- }
-
- function cleanup($force=false)
- {
- $manager = $this->getManager($force);
- if ($manager) {
- $service = $manager->current();
- $this->destroyManager($force);
- } else {
- $service = null;
- }
- return $service;
- }
-
- function getSessionKey()
- {
-
- return $this->PREFIX . $this->session_key_suffix;
- }
-
- function getManager($force=false)
- {
-
-
- $manager_str = $this->session->get($this->getSessionKey());
- $manager = null;
- if ($manager_str !== null) {
- $loader = new Auth_Yadis_ManagerLoader();
- $manager = $loader->fromSession(unserialize($manager_str));
- }
- if ($manager && ($manager->forURL($this->url) || $force)) {
- return $manager;
- }
- }
-
- function createManager($services, $yadis_url = null)
- {
- $key = $this->getSessionKey();
- if ($this->getManager()) {
- return $this->getManager();
- }
- if ($services) {
- $loader = new Auth_Yadis_ManagerLoader();
- $manager = new Auth_Yadis_Manager($this->url, $yadis_url,
- $services, $key);
- $this->session->set($this->session_key,
- serialize($loader->toSession($manager)));
- return $manager;
- }
- }
-
- function destroyManager($force=false)
- {
- if ($this->getManager($force) !== null) {
- $key = $this->getSessionKey();
- $this->session->del($key);
- }
- }
- }
|