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