PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/cms/openid/Services/Yadis/Manager.php

https://github.com/akash6190/pragyan
PHP | 303 lines | 140 code | 40 blank | 123 comment | 17 complexity | 5d0abaa392b66cb2b27cc45e0b979d82 MD5 | raw file
  1. <?php
  2. /**
  3. * Yadis service manager to be used during yadis-driven authentication
  4. * attempts.
  5. *
  6. * @package Yadis
  7. */
  8. /**
  9. * The base session class used by the Services_Yadis_Manager. This
  10. * class wraps the default PHP session machinery and should be
  11. * subclassed if your application doesn't use PHP sessioning.
  12. *
  13. * @package Yadis
  14. */
  15. class Services_Yadis_PHPSession {
  16. /**
  17. * Set a session key/value pair.
  18. *
  19. * @param string $name The name of the session key to add.
  20. * @param string $value The value to add to the session.
  21. */
  22. function set($name, $value)
  23. {
  24. $_SESSION[$name] = $value;
  25. }
  26. /**
  27. * Get a key's value from the session.
  28. *
  29. * @param string $name The name of the key to retrieve.
  30. * @param string $default The optional value to return if the key
  31. * is not found in the session.
  32. * @return string $result The key's value in the session or
  33. * $default if it isn't found.
  34. */
  35. function get($name, $default=null)
  36. {
  37. if (array_key_exists($name, $_SESSION)) {
  38. return $_SESSION[$name];
  39. } else {
  40. return $default;
  41. }
  42. }
  43. /**
  44. * Remove a key/value pair from the session.
  45. *
  46. * @param string $name The name of the key to remove.
  47. */
  48. function del($name)
  49. {
  50. unset($_SESSION[$name]);
  51. }
  52. }
  53. /**
  54. * The Yadis service manager which stores state in a session and
  55. * iterates over <Service> elements in a Yadis XRDS document and lets
  56. * a caller attempt to use each one. This is used by the Yadis
  57. * library internally.
  58. *
  59. * @package Yadis
  60. */
  61. class Services_Yadis_Manager {
  62. /**
  63. * Intialize a new yadis service manager.
  64. *
  65. * @access private
  66. */
  67. function Services_Yadis_Manager($starting_url, $yadis_url,
  68. $services, $session_key)
  69. {
  70. // The URL that was used to initiate the Yadis protocol
  71. $this->starting_url = $starting_url;
  72. // The URL after following redirects (the identifier)
  73. $this->yadis_url = $yadis_url;
  74. // List of service elements
  75. $this->services = $services;
  76. $this->session_key = $session_key;
  77. // Reference to the current service object
  78. $this->_current = null;
  79. // Stale flag for cleanup if PHP lib has trouble.
  80. $this->stale = false;
  81. }
  82. /**
  83. * @access private
  84. */
  85. function length()
  86. {
  87. // How many untried services remain?
  88. return count($this->services);
  89. }
  90. /**
  91. * Return the next service
  92. *
  93. * $this->current() will continue to return that service until the
  94. * next call to this method.
  95. */
  96. function nextService()
  97. {
  98. if ($this->services) {
  99. $this->_current = array_shift($this->services);
  100. } else {
  101. $this->_current = null;
  102. }
  103. return $this->_current;
  104. }
  105. /**
  106. * @access private
  107. */
  108. function current()
  109. {
  110. // Return the current service.
  111. // Returns None if there are no services left.
  112. return $this->_current;
  113. }
  114. /**
  115. * @access private
  116. */
  117. function forURL($url)
  118. {
  119. return in_array($url, array($this->starting_url, $this->yadis_url));
  120. }
  121. /**
  122. * @access private
  123. */
  124. function started()
  125. {
  126. // Has the first service been returned?
  127. return $this->_current !== null;
  128. }
  129. }
  130. /**
  131. * State management for discovery.
  132. *
  133. * High-level usage pattern is to call .getNextService(discover) in
  134. * order to find the next available service for this user for this
  135. * session. Once a request completes, call .finish() to clean up the
  136. * session state.
  137. *
  138. * @package Yadis
  139. */
  140. class Services_Yadis_Discovery {
  141. /**
  142. * @access private
  143. */
  144. var $DEFAULT_SUFFIX = 'auth';
  145. /**
  146. * @access private
  147. */
  148. var $PREFIX = '_yadis_services_';
  149. /**
  150. * Initialize a discovery object.
  151. *
  152. * @param Services_Yadis_PHPSession $session An object which
  153. * implements the Services_Yadis_PHPSession API.
  154. * @param string $url The URL on which to attempt discovery.
  155. * @param string $session_key_suffix The optional session key
  156. * suffix override.
  157. */
  158. function Services_Yadis_Discovery(&$session, $url,
  159. $session_key_suffix = null)
  160. {
  161. /// Initialize a discovery object
  162. $this->session =& $session;
  163. $this->url = $url;
  164. if ($session_key_suffix === null) {
  165. $session_key_suffix = $this->DEFAULT_SUFFIX;
  166. }
  167. $this->session_key_suffix = $session_key_suffix;
  168. $this->session_key = $this->PREFIX . $this->session_key_suffix;
  169. }
  170. /**
  171. * Return the next authentication service for the pair of
  172. * user_input and session. This function handles fallback.
  173. */
  174. function getNextService($discover_cb, &$fetcher)
  175. {
  176. $manager = $this->getManager();
  177. if ((!$manager) ||
  178. $manager->stale) {
  179. $this->destroyManager();
  180. $http_response = array();
  181. $services = call_user_func($discover_cb, $this->url,
  182. $fetcher);
  183. $manager = $this->createManager($services, $this->url);
  184. }
  185. if ($manager) {
  186. $service = $manager->nextService();
  187. $this->session->set($this->session_key, serialize($manager));
  188. } else {
  189. $service = null;
  190. }
  191. return $service;
  192. }
  193. /**
  194. * Clean up Yadis-related services in the session and return the
  195. * most-recently-attempted service from the manager, if one
  196. * exists.
  197. */
  198. function cleanup()
  199. {
  200. $manager = $this->getManager();
  201. if ($manager) {
  202. $service = $manager->current();
  203. $this->destroyManager();
  204. } else {
  205. $service = null;
  206. }
  207. return $service;
  208. }
  209. /**
  210. * @access private
  211. */
  212. function getSessionKey()
  213. {
  214. // Get the session key for this starting URL and suffix
  215. return $this->PREFIX . $this->session_key_suffix;
  216. }
  217. /**
  218. * @access private
  219. */
  220. function getManager()
  221. {
  222. // Extract the YadisServiceManager for this object's URL and
  223. // suffix from the session.
  224. $manager_str = $this->session->get($this->getSessionKey());
  225. $manager = null;
  226. if ($manager_str !== null) {
  227. $manager = unserialize($manager_str);
  228. }
  229. if ($manager && $manager->forURL($this->url)) {
  230. return $manager;
  231. } else {
  232. return null;
  233. }
  234. }
  235. /**
  236. * @access private
  237. */
  238. function &createManager($services, $yadis_url = null)
  239. {
  240. $key = $this->getSessionKey();
  241. if ($this->getManager()) {
  242. return $this->getManager();
  243. }
  244. if (!$services) {
  245. return null;
  246. }
  247. $manager = new Services_Yadis_Manager($this->url, $yadis_url,
  248. $services, $key);
  249. $this->session->set($this->session_key, serialize($manager));
  250. return $manager;
  251. }
  252. /**
  253. * @access private
  254. */
  255. function destroyManager()
  256. {
  257. if ($this->getManager() !== null) {
  258. $key = $this->getSessionKey();
  259. $this->session->del($key);
  260. }
  261. }
  262. }
  263. ?>