PageRenderTime 50ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/OpenId/Extension/Sreg.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 300 lines | 178 code | 16 blank | 106 comment | 38 complexity | aadd77b9f078a755f07640c846589856 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_OpenId
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Sreg.php 20096 2010-01-06 02:05:09Z bkarwin $
  20. */
  21. /**
  22. * @see Zend_OpenId_Extension
  23. */
  24. #require_once "Zend/OpenId/Extension.php";
  25. /**
  26. * 'Simple Refistration Extension' for Zend_OpenId
  27. *
  28. * @category Zend
  29. * @package Zend_OpenId
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_OpenId_Extension_Sreg extends Zend_OpenId_Extension
  34. {
  35. /**
  36. * SREG 1.1 namespace. All OpenID SREG 1.1 messages MUST contain variable
  37. * openid.ns.sreg with its value.
  38. */
  39. const NAMESPACE_1_1 = "http://openid.net/extensions/sreg/1.1";
  40. private $_props;
  41. private $_policy_url;
  42. private $_version;
  43. /**
  44. * Creates SREG extension object
  45. *
  46. * @param array $props associative array of SREG variables
  47. * @param string $policy_url SREG policy URL
  48. * @param float $version SREG version
  49. * @return array
  50. */
  51. public function __construct(array $props=null, $policy_url=null, $version=1.0)
  52. {
  53. $this->_props = $props;
  54. $this->_policy_url = $policy_url;
  55. $this->_version = $version;
  56. }
  57. /**
  58. * Returns associative array of SREG variables
  59. *
  60. * @return array
  61. */
  62. public function getProperties() {
  63. if (is_array($this->_props)) {
  64. return $this->_props;
  65. } else {
  66. return array();
  67. }
  68. }
  69. /**
  70. * Returns SREG policy URL
  71. *
  72. * @return string
  73. */
  74. public function getPolicyUrl() {
  75. return $this->_policy_url;
  76. }
  77. /**
  78. * Returns SREG protocol version
  79. *
  80. * @return float
  81. */
  82. public function getVersion() {
  83. return $this->_version;
  84. }
  85. /**
  86. * Returns array of allowed SREG variable names.
  87. *
  88. * @return array
  89. */
  90. public static function getSregProperties()
  91. {
  92. return array(
  93. "nickname",
  94. "email",
  95. "fullname",
  96. "dob",
  97. "gender",
  98. "postcode",
  99. "country",
  100. "language",
  101. "timezone"
  102. );
  103. }
  104. /**
  105. * Adds additional SREG data to OpenId 'checkid_immediate' or
  106. * 'checkid_setup' request.
  107. *
  108. * @param array &$params request's var/val pairs
  109. * @return bool
  110. */
  111. public function prepareRequest(&$params)
  112. {
  113. if (is_array($this->_props) && count($this->_props) > 0) {
  114. foreach ($this->_props as $prop => $req) {
  115. if ($req) {
  116. if (isset($required)) {
  117. $required .= ','.$prop;
  118. } else {
  119. $required = $prop;
  120. }
  121. } else {
  122. if (isset($optional)) {
  123. $optional .= ','.$prop;
  124. } else {
  125. $optional = $prop;
  126. }
  127. }
  128. }
  129. if ($this->_version >= 1.1) {
  130. $params['openid.ns.sreg'] = Zend_OpenId_Extension_Sreg::NAMESPACE_1_1;
  131. }
  132. if (!empty($required)) {
  133. $params['openid.sreg.required'] = $required;
  134. }
  135. if (!empty($optional)) {
  136. $params['openid.sreg.optional'] = $optional;
  137. }
  138. if (!empty($this->_policy_url)) {
  139. $params['openid.sreg.policy_url'] = $this->_policy_url;
  140. }
  141. }
  142. return true;
  143. }
  144. /**
  145. * Parses OpenId 'checkid_immediate' or 'checkid_setup' request,
  146. * extracts SREG variables and sets ovject properties to corresponding
  147. * values.
  148. *
  149. * @param array $params request's var/val pairs
  150. * @return bool
  151. */
  152. public function parseRequest($params)
  153. {
  154. if (isset($params['openid_ns_sreg']) &&
  155. $params['openid_ns_sreg'] === Zend_OpenId_Extension_Sreg::NAMESPACE_1_1) {
  156. $this->_version= 1.1;
  157. } else {
  158. $this->_version= 1.0;
  159. }
  160. if (!empty($params['openid_sreg_policy_url'])) {
  161. $this->_policy_url = $params['openid_sreg_policy_url'];
  162. } else {
  163. $this->_policy_url = null;
  164. }
  165. $props = array();
  166. if (!empty($params['openid_sreg_optional'])) {
  167. foreach (explode(',', $params['openid_sreg_optional']) as $prop) {
  168. $prop = trim($prop);
  169. $props[$prop] = false;
  170. }
  171. }
  172. if (!empty($params['openid_sreg_required'])) {
  173. foreach (explode(',', $params['openid_sreg_required']) as $prop) {
  174. $prop = trim($prop);
  175. $props[$prop] = true;
  176. }
  177. }
  178. $props2 = array();
  179. foreach (self::getSregProperties() as $prop) {
  180. if (isset($props[$prop])) {
  181. $props2[$prop] = $props[$prop];
  182. }
  183. }
  184. $this->_props = (count($props2) > 0) ? $props2 : null;
  185. return true;
  186. }
  187. /**
  188. * Adds additional SREG data to OpenId 'id_res' response.
  189. *
  190. * @param array &$params response's var/val pairs
  191. * @return bool
  192. */
  193. public function prepareResponse(&$params)
  194. {
  195. if (is_array($this->_props) && count($this->_props) > 0) {
  196. if ($this->_version >= 1.1) {
  197. $params['openid.ns.sreg'] = Zend_OpenId_Extension_Sreg::NAMESPACE_1_1;
  198. }
  199. foreach (self::getSregProperties() as $prop) {
  200. if (!empty($this->_props[$prop])) {
  201. $params['openid.sreg.' . $prop] = $this->_props[$prop];
  202. }
  203. }
  204. }
  205. return true;
  206. }
  207. /**
  208. * Parses OpenId 'id_res' response and sets object's properties according
  209. * to 'openid.sreg.*' variables in response
  210. *
  211. * @param array $params response's var/val pairs
  212. * @return bool
  213. */
  214. public function parseResponse($params)
  215. {
  216. if (isset($params['openid_ns_sreg']) &&
  217. $params['openid_ns_sreg'] === Zend_OpenId_Extension_Sreg::NAMESPACE_1_1) {
  218. $this->_version= 1.1;
  219. } else {
  220. $this->_version= 1.0;
  221. }
  222. $props = array();
  223. foreach (self::getSregProperties() as $prop) {
  224. if (!empty($params['openid_sreg_' . $prop])) {
  225. $props[$prop] = $params['openid_sreg_' . $prop];
  226. }
  227. }
  228. if (isset($this->_props) && is_array($this->_props)) {
  229. foreach (self::getSregProperties() as $prop) {
  230. if (isset($this->_props[$prop]) &&
  231. $this->_props[$prop] &&
  232. !isset($props[$prop])) {
  233. return false;
  234. }
  235. }
  236. }
  237. $this->_props = (count($props) > 0) ? $props : null;
  238. return true;
  239. }
  240. /**
  241. * Addes SREG properties that are allowed to be send to consumer to
  242. * the given $data argument.
  243. *
  244. * @param array &$data data to be stored in tusted servers database
  245. * @return bool
  246. */
  247. public function getTrustData(&$data)
  248. {
  249. $data[get_class()] = $this->getProperties();
  250. return true;
  251. }
  252. /**
  253. * Check if given $data contains necessury SREG properties to sutisfy
  254. * OpenId request. On success sets SREG response properties from given
  255. * $data and returns true, on failure returns false.
  256. *
  257. * @param array $data data from tusted servers database
  258. * @return bool
  259. */
  260. public function checkTrustData($data)
  261. {
  262. if (is_array($this->_props) && count($this->_props) > 0) {
  263. $props = array();
  264. $name = get_class();
  265. if (isset($data[$name])) {
  266. $props = $data[$name];
  267. } else {
  268. $props = array();
  269. }
  270. $props2 = array();
  271. foreach ($this->_props as $prop => $req) {
  272. if (empty($props[$prop])) {
  273. if ($req) {
  274. return false;
  275. }
  276. } else {
  277. $props2[$prop] = $props[$prop];
  278. }
  279. }
  280. $this->_props = (count($props2) > 0) ? $props2 : null;
  281. }
  282. return true;
  283. }
  284. }