File indexing completed on 2025-01-19 05:21:20

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_OpenId
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_OpenId_Extension
0025  */
0026 // require_once "Zend/OpenId/Extension.php";
0027 
0028 /**
0029  * 'Simple Refistration Extension' for Zend_OpenId
0030  *
0031  * @category   Zend
0032  * @package    Zend_OpenId
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  */
0036 class Zend_OpenId_Extension_Sreg extends Zend_OpenId_Extension
0037 {
0038     /**
0039      * SREG 1.1 namespace. All OpenID SREG 1.1 messages MUST contain variable
0040      * openid.ns.sreg with its value.
0041      */
0042     const NAMESPACE_1_1 = "http://openid.net/extensions/sreg/1.1";
0043 
0044     private $_props;
0045     private $_policy_url;
0046     private $_version;
0047 
0048     /**
0049      * Creates SREG extension object
0050      *
0051      * @param array $props associative array of SREG variables
0052      * @param string $policy_url SREG policy URL
0053      * @param float $version SREG version
0054      * @return array
0055      */
0056     public function __construct(array $props=null, $policy_url=null, $version=1.0)
0057     {
0058         $this->_props = $props;
0059         $this->_policy_url = $policy_url;
0060         $this->_version = $version;
0061     }
0062 
0063     /**
0064      * Returns associative array of SREG variables
0065      *
0066      * @return array
0067      */
0068     public function getProperties() {
0069         if (is_array($this->_props)) {
0070             return $this->_props;
0071         } else {
0072             return array();
0073         }
0074     }
0075 
0076     /**
0077      * Returns SREG policy URL
0078      *
0079      * @return string
0080      */
0081     public function getPolicyUrl() {
0082         return $this->_policy_url;
0083     }
0084 
0085     /**
0086      * Returns SREG protocol version
0087      *
0088      * @return float
0089      */
0090     public function getVersion() {
0091         return $this->_version;
0092     }
0093 
0094     /**
0095      * Returns array of allowed SREG variable names.
0096      *
0097      * @return array
0098      */
0099     public static function getSregProperties()
0100     {
0101         return array(
0102             "nickname",
0103             "email",
0104             "fullname",
0105             "dob",
0106             "gender",
0107             "postcode",
0108             "country",
0109             "language",
0110             "timezone"
0111         );
0112     }
0113 
0114     /**
0115      * Adds additional SREG data to OpenId 'checkid_immediate' or
0116      * 'checkid_setup' request.
0117      *
0118      * @param array &$params request's var/val pairs
0119      * @return bool
0120      */
0121     public function prepareRequest(&$params)
0122     {
0123         if (is_array($this->_props) && count($this->_props) > 0) {
0124             foreach ($this->_props as $prop => $req) {
0125                 if ($req) {
0126                     if (isset($required)) {
0127                         $required .= ','.$prop;
0128                     } else {
0129                         $required = $prop;
0130                     }
0131                 } else {
0132                     if (isset($optional)) {
0133                         $optional .= ','.$prop;
0134                     } else {
0135                         $optional = $prop;
0136                     }
0137                 }
0138             }
0139             if ($this->_version >= 1.1) {
0140                 $params['openid.ns.sreg'] = Zend_OpenId_Extension_Sreg::NAMESPACE_1_1;
0141             }
0142             if (!empty($required)) {
0143                 $params['openid.sreg.required'] = $required;
0144             }
0145             if (!empty($optional)) {
0146                 $params['openid.sreg.optional'] = $optional;
0147             }
0148             if (!empty($this->_policy_url)) {
0149                 $params['openid.sreg.policy_url'] = $this->_policy_url;
0150             }
0151         }
0152         return true;
0153     }
0154 
0155     /**
0156      * Parses OpenId 'checkid_immediate' or 'checkid_setup' request,
0157      * extracts SREG variables and sets ovject properties to corresponding
0158      * values.
0159      *
0160      * @param array $params request's var/val pairs
0161      * @return bool
0162      */
0163     public function parseRequest($params)
0164     {
0165         if (isset($params['openid_ns_sreg']) &&
0166             $params['openid_ns_sreg'] === Zend_OpenId_Extension_Sreg::NAMESPACE_1_1) {
0167             $this->_version= 1.1;
0168         } else {
0169             $this->_version= 1.0;
0170         }
0171         if (!empty($params['openid_sreg_policy_url'])) {
0172             $this->_policy_url = $params['openid_sreg_policy_url'];
0173         } else {
0174             $this->_policy_url = null;
0175         }
0176         $props = array();
0177         if (!empty($params['openid_sreg_optional'])) {
0178             foreach (explode(',', $params['openid_sreg_optional']) as $prop) {
0179                 $prop = trim($prop);
0180                 $props[$prop] = false;
0181             }
0182         }
0183         if (!empty($params['openid_sreg_required'])) {
0184             foreach (explode(',', $params['openid_sreg_required']) as $prop) {
0185                 $prop = trim($prop);
0186                 $props[$prop] = true;
0187             }
0188         }
0189         $props2 = array();
0190         foreach (self::getSregProperties() as $prop) {
0191             if (isset($props[$prop])) {
0192                 $props2[$prop] = $props[$prop];
0193             }
0194         }
0195 
0196         $this->_props = (count($props2) > 0) ? $props2 : null;
0197         return true;
0198     }
0199 
0200     /**
0201      * Adds additional SREG data to OpenId 'id_res' response.
0202      *
0203      * @param array &$params response's var/val pairs
0204      * @return bool
0205      */
0206     public function prepareResponse(&$params)
0207     {
0208         if (is_array($this->_props) && count($this->_props) > 0) {
0209             if ($this->_version >= 1.1) {
0210                 $params['openid.ns.sreg'] = Zend_OpenId_Extension_Sreg::NAMESPACE_1_1;
0211             }
0212             foreach (self::getSregProperties() as $prop) {
0213                 if (!empty($this->_props[$prop])) {
0214                     $params['openid.sreg.' . $prop] = $this->_props[$prop];
0215                 }
0216             }
0217         }
0218         return true;
0219     }
0220 
0221     /**
0222      * Parses OpenId 'id_res' response and sets object's properties according
0223      * to 'openid.sreg.*' variables in response
0224      *
0225      * @param array $params response's var/val pairs
0226      * @return bool
0227      */
0228     public function parseResponse($params)
0229     {
0230         if (isset($params['openid_ns_sreg']) &&
0231             $params['openid_ns_sreg'] === Zend_OpenId_Extension_Sreg::NAMESPACE_1_1) {
0232             $this->_version= 1.1;
0233         } else {
0234             $this->_version= 1.0;
0235         }
0236         $props = array();
0237         foreach (self::getSregProperties() as $prop) {
0238             if (!empty($params['openid_sreg_' . $prop])) {
0239                 $props[$prop] = $params['openid_sreg_' . $prop];
0240             }
0241         }
0242         if (isset($this->_props) && is_array($this->_props)) {
0243             foreach (self::getSregProperties() as $prop) {
0244                 if (isset($this->_props[$prop]) &&
0245                     $this->_props[$prop] &&
0246                     !isset($props[$prop])) {
0247                     return false;
0248                 }
0249             }
0250         }
0251         $this->_props = (count($props) > 0) ? $props : null;
0252         return true;
0253     }
0254 
0255     /**
0256      * Addes SREG properties that are allowed to be send to consumer to
0257      * the given $data argument.
0258      *
0259      * @param array &$data data to be stored in tusted servers database
0260      * @return bool
0261      */
0262     public function getTrustData(&$data)
0263     {
0264         $data[get_class()] = $this->getProperties();
0265         return true;
0266     }
0267 
0268     /**
0269      * Check if given $data contains necessury SREG properties to sutisfy
0270      * OpenId request. On success sets SREG response properties from given
0271      * $data and returns true, on failure returns false.
0272      *
0273      * @param array $data data from tusted servers database
0274      * @return bool
0275      */
0276     public function checkTrustData($data)
0277     {
0278         if (is_array($this->_props) && count($this->_props) > 0) {
0279             $props = array();
0280             $name = get_class();
0281             if (isset($data[$name])) {
0282                 $props = $data[$name];
0283             } else {
0284                 $props = array();
0285             }
0286             $props2 = array();
0287             foreach ($this->_props as $prop => $req) {
0288                 if (empty($props[$prop])) {
0289                     if ($req) {
0290                         return false;
0291                     }
0292                 } else {
0293                     $props2[$prop] = $props[$prop];
0294                 }
0295             }
0296             $this->_props = (count($props2) > 0) ? $props2 : null;
0297         }
0298         return true;
0299     }
0300 }