PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/InfoCard/Claims.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 307 lines | 124 code | 33 blank | 150 comment | 10 complexity | cd2c6866974c12f9dcd706fd51f8bf87 MD5 | raw file
  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_InfoCard
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Claims.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * Result value of the InfoCard component, contains any error messages and claims
  23. * from the processing of an information card.
  24. *
  25. * @category Zend
  26. * @package Zend_InfoCard
  27. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_InfoCard_Claims
  31. {
  32. /**
  33. * Successful validation and extraion of claims
  34. */
  35. const RESULT_SUCCESS = 1;
  36. /**
  37. * Indicates there was an error processing the XML document
  38. */
  39. const RESULT_PROCESSING_FAILURE = 2;
  40. /**
  41. * Indicates that the signature values within the XML document failed verification
  42. */
  43. const RESULT_VALIDATION_FAILURE = 3;
  44. /**
  45. * The default namespace to assume in these claims
  46. *
  47. * @var string
  48. */
  49. protected $_defaultNamespace = null;
  50. /**
  51. * A boolean indicating if the claims should be consider "valid" or not based on processing
  52. *
  53. * @var bool
  54. */
  55. protected $_isValid = true;
  56. /**
  57. * The error message if any
  58. *
  59. * @var string
  60. */
  61. protected $_error = "";
  62. /**
  63. * An array of claims taken from the information card
  64. *
  65. * @var array
  66. */
  67. protected $_claims;
  68. /**
  69. * The result code of processing the information card as defined by the constants of this class
  70. *
  71. * @var integer
  72. */
  73. protected $_code;
  74. /**
  75. * Override for the safeguard which ensures that you don't use claims which failed validation.
  76. * Used in situations when there was a validation error you'd like to ignore
  77. *
  78. * @return Zend_InfoCard_Claims
  79. */
  80. public function forceValid()
  81. {
  82. trigger_error("Forcing Claims to be valid although it is a security risk", E_USER_WARNING);
  83. $this->_isValid = true;
  84. return $this;
  85. }
  86. /**
  87. * Retrieve the PPI (Private Personal Identifier) associated with the information card
  88. *
  89. * @return string the private personal identifier
  90. */
  91. public function getCardID()
  92. {
  93. return $this->getClaim('http://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier');
  94. }
  95. /**
  96. * Retrieves the default namespace used in this information card. If a default namespace was not
  97. * set, it figures out which one to consider 'default' by taking the first namespace sorted by use-count
  98. * in claims
  99. *
  100. * @throws Zend_InfoCard_Exception
  101. * @return string The default namespace
  102. */
  103. public function getDefaultNamespace()
  104. {
  105. if($this->_defaultNamespace === null) {
  106. $namespaces = array();
  107. $leader = '';
  108. foreach($this->_claims as $claim) {
  109. if(!isset($namespaces[$claim['namespace']])) {
  110. $namespaces[$claim['namespace']] = 1;
  111. } else {
  112. $namespaces[$claim['namespace']]++;
  113. }
  114. if(empty($leader) || ($namespaces[$claim['namespace']] > $leader)) {
  115. $leader = $claim['namespace'];
  116. }
  117. }
  118. if(empty($leader)) {
  119. require_once 'Zend/InfoCard/Exception.php';
  120. throw new Zend_InfoCard_Exception("Failed to determine default namespace");
  121. }
  122. $this->setDefaultNamespace($leader);
  123. }
  124. return $this->_defaultNamespace;
  125. }
  126. /**
  127. * Set the default namespace, overriding any existing default
  128. *
  129. * @throws Zend_InfoCard_Exception
  130. * @param string $namespace The default namespace to use
  131. * @return Zend_InfoCard_Claims
  132. */
  133. public function setDefaultNamespace($namespace)
  134. {
  135. foreach($this->_claims as $claim) {
  136. if($namespace == $claim['namespace']) {
  137. $this->_defaultNamespace = $namespace;
  138. return $this;
  139. }
  140. }
  141. require_once 'Zend/InfoCard/Exception.php';
  142. throw new Zend_InfoCard_Exception("At least one claim must exist in specified namespace to make it the default namespace");
  143. }
  144. /**
  145. * Indicates if this claim object contains validated claims or not
  146. *
  147. * @return bool
  148. */
  149. public function isValid()
  150. {
  151. return $this->_isValid;
  152. }
  153. /**
  154. * Set the error message contained within the claims object
  155. *
  156. * @param string $error The error message
  157. * @return Zend_InfoCard_Claims
  158. */
  159. public function setError($error)
  160. {
  161. $this->_error = $error;
  162. $this->_isValid = false;
  163. return $this;
  164. }
  165. /**
  166. * Retrieve the error message contained within the claims object
  167. *
  168. * @return string The error message
  169. */
  170. public function getErrorMsg()
  171. {
  172. return $this->_error;
  173. }
  174. /**
  175. * Set the claims for the claims object. Can only be set once and is done
  176. * by the component itself. Internal use only.
  177. *
  178. * @throws Zend_InfoCard_Exception
  179. * @param array $claims
  180. * @return Zend_InfoCard_Claims
  181. */
  182. public function setClaims(Array $claims)
  183. {
  184. if($this->_claims !== null) {
  185. require_once 'Zend/InfoCard/Exception.php';
  186. throw new Zend_InfoCard_Exception("Claim objects are read-only");
  187. }
  188. $this->_claims = $claims;
  189. return $this;
  190. }
  191. /**
  192. * Set the result code of the claims object.
  193. *
  194. * @throws Zend_InfoCard_Exception
  195. * @param int $code The result code
  196. * @return Zend_InfoCard_Claims
  197. */
  198. public function setCode($code)
  199. {
  200. switch($code) {
  201. case self::RESULT_PROCESSING_FAILURE:
  202. case self::RESULT_SUCCESS:
  203. case self::RESULT_VALIDATION_FAILURE:
  204. $this->_code = $code;
  205. return $this;
  206. }
  207. require_once 'Zend/InfoCard/Exception.php';
  208. throw new Zend_InfoCard_Exception("Attempted to set unknown error code");
  209. }
  210. /**
  211. * Gets the result code of the claims object
  212. *
  213. * @return integer The result code
  214. */
  215. public function getCode()
  216. {
  217. return $this->_code;
  218. }
  219. /**
  220. * Get a claim by providing its complete claim URI
  221. *
  222. * @param string $claimURI The complete claim URI to retrieve
  223. * @return mixed The claim matching that specific URI or null if not found
  224. */
  225. public function getClaim($claimURI)
  226. {
  227. if($this->claimExists($claimURI)) {
  228. return $this->_claims[$claimURI]['value'];
  229. }
  230. return null;
  231. }
  232. /**
  233. * Indicates if a specific claim URI exists or not within the object
  234. *
  235. * @param string $claimURI The complete claim URI to check
  236. * @return bool true if the claim exists, false if not found
  237. */
  238. public function claimExists($claimURI)
  239. {
  240. return isset($this->_claims[$claimURI]);
  241. }
  242. /**
  243. * Magic helper function
  244. * @throws Zend_InfoCard_Exception
  245. */
  246. public function __unset($k)
  247. {
  248. require_once 'Zend/InfoCard/Exception.php';
  249. throw new Zend_InfoCard_Exception("Claim objects are read-only");
  250. }
  251. /**
  252. * Magic helper function
  253. */
  254. public function __isset($k)
  255. {
  256. return $this->claimExists("{$this->getDefaultNamespace()}/$k");
  257. }
  258. /**
  259. * Magic helper function
  260. */
  261. public function __get($k)
  262. {
  263. return $this->getClaim("{$this->getDefaultNamespace()}/$k");
  264. }
  265. /**
  266. * Magic helper function
  267. * @throws Zend_InfoCard_Exception
  268. */
  269. public function __set($k, $v)
  270. {
  271. require_once 'Zend/InfoCard/Exception.php';
  272. throw new Zend_InfoCard_Exception("Claim objects are read-only");
  273. }
  274. }