File indexing completed on 2024-05-12 06:02:33

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Filter
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Filter_Interface
0024  */
0025 // require_once 'Zend/Filter/Interface.php';
0026 
0027 /**
0028  * @see Zend_Loader
0029  */
0030 // require_once 'Zend/Loader.php';
0031 
0032 /**
0033  * Encrypts a given string
0034  *
0035  * @category   Zend
0036  * @package    Zend_Filter
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_Filter_Encrypt implements Zend_Filter_Interface
0041 {
0042     /**
0043      * Encryption adapter
0044      */
0045     protected $_adapter;
0046 
0047     /**
0048      * Class constructor
0049      *
0050      * @param string|array $options (Optional) Options to set, if null mcrypt is used
0051      */
0052     public function __construct($options = null)
0053     {
0054         if ($options instanceof Zend_Config) {
0055             $options = $options->toArray();
0056         }
0057 
0058         $this->setAdapter($options);
0059     }
0060 
0061     /**
0062      * Returns the name of the set adapter
0063      *
0064      * @return string
0065      */
0066     public function getAdapter()
0067     {
0068         return $this->_adapter->toString();
0069     }
0070 
0071     /**
0072      * Sets new encryption options
0073      *
0074      * @param  string|array $options (Optional) Encryption options
0075      * @return Zend_Filter_Encrypt
0076      */
0077     public function setAdapter($options = null)
0078     {
0079         if (is_string($options)) {
0080             $adapter = $options;
0081         } else if (isset($options['adapter'])) {
0082             $adapter = $options['adapter'];
0083             unset($options['adapter']);
0084         } else {
0085             $adapter = 'Mcrypt';
0086         }
0087 
0088         if (!is_array($options)) {
0089             $options = array();
0090         }
0091 
0092         if (Zend_Loader::isReadable('Zend/Filter/Encrypt/' . ucfirst($adapter). '.php')) {
0093             $adapter = 'Zend_Filter_Encrypt_' . ucfirst($adapter);
0094         }
0095 
0096         if (!class_exists($adapter)) {
0097             Zend_Loader::loadClass($adapter);
0098         }
0099 
0100         $this->_adapter = new $adapter($options);
0101         if (!$this->_adapter instanceof Zend_Filter_Encrypt_Interface) {
0102             // require_once 'Zend/Filter/Exception.php';
0103             throw new Zend_Filter_Exception("Encoding adapter '" . $adapter . "' does not implement Zend_Filter_Encrypt_Interface");
0104         }
0105 
0106         return $this;
0107     }
0108 
0109     /**
0110      * Calls adapter methods
0111      *
0112      * @param string       $method  Method to call
0113      * @param string|array $options Options for this method
0114      */
0115     public function __call($method, $options)
0116     {
0117         $part = substr($method, 0, 3);
0118         if ((($part != 'get') and ($part != 'set')) or !method_exists($this->_adapter, $method)) {
0119             // require_once 'Zend/Filter/Exception.php';
0120             throw new Zend_Filter_Exception("Unknown method '{$method}'");
0121         }
0122 
0123         return call_user_func_array(array($this->_adapter, $method), $options);
0124     }
0125 
0126     /**
0127      * Defined by Zend_Filter_Interface
0128      *
0129      * Encrypts the content $value with the defined settings
0130      *
0131      * @param  string $value Content to encrypt
0132      * @return string The encrypted content
0133      */
0134     public function filter($value)
0135     {
0136         return $this->_adapter->encrypt($value);
0137     }
0138 }