web/Zend/Filter/Encrypt.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     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_Filter
       
    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: Encrypt.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Filter_Interface
       
    24  */
       
    25 require_once 'Zend/Filter/Interface.php';
       
    26 
       
    27 /**
       
    28  * @see Zend_Loader
       
    29  */
       
    30 require_once 'Zend/Loader.php';
       
    31 
       
    32 /**
       
    33  * Encrypts a given string
       
    34  *
       
    35  * @category   Zend
       
    36  * @package    Zend_Filter
       
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    39  */
       
    40 class Zend_Filter_Encrypt implements Zend_Filter_Interface
       
    41 {
       
    42     /**
       
    43      * Encryption adapter
       
    44      */
       
    45     protected $_adapter;
       
    46 
       
    47     /**
       
    48      * Class constructor
       
    49      *
       
    50      * @param string|array $options (Optional) Options to set, if null mcrypt is used
       
    51      */
       
    52     public function __construct($options = null)
       
    53     {
       
    54         if ($options instanceof Zend_Config) {
       
    55             $options = $options->toArray();
       
    56         }
       
    57 
       
    58         $this->setAdapter($options);
       
    59     }
       
    60 
       
    61     /**
       
    62      * Returns the name of the set adapter
       
    63      *
       
    64      * @return string
       
    65      */
       
    66     public function getAdapter()
       
    67     {
       
    68         return $this->_adapter->toString();
       
    69     }
       
    70 
       
    71     /**
       
    72      * Sets new encryption options
       
    73      *
       
    74      * @param  string|array $options (Optional) Encryption options
       
    75      * @return Zend_Filter_Encrypt
       
    76      */
       
    77     public function setAdapter($options = null)
       
    78     {
       
    79         if (is_string($options)) {
       
    80             $adapter = $options;
       
    81         } else if (isset($options['adapter'])) {
       
    82             $adapter = $options['adapter'];
       
    83             unset($options['adapter']);
       
    84         } else {
       
    85             $adapter = 'Mcrypt';
       
    86         }
       
    87 
       
    88         if (!is_array($options)) {
       
    89             $options = array();
       
    90         }
       
    91 
       
    92         if (Zend_Loader::isReadable('Zend/Filter/Encrypt/' . ucfirst($adapter). '.php')) {
       
    93             $adapter = 'Zend_Filter_Encrypt_' . ucfirst($adapter);
       
    94         }
       
    95 
       
    96         if (!class_exists($adapter)) {
       
    97             Zend_Loader::loadClass($adapter);
       
    98         }
       
    99 
       
   100         $this->_adapter = new $adapter($options);
       
   101         if (!$this->_adapter instanceof Zend_Filter_Encrypt_Interface) {
       
   102             require_once 'Zend/Filter/Exception.php';
       
   103             throw new Zend_Filter_Exception("Encoding adapter '" . $adapter . "' does not implement Zend_Filter_Encrypt_Interface");
       
   104         }
       
   105 
       
   106         return $this;
       
   107     }
       
   108 
       
   109     /**
       
   110      * Calls adapter methods
       
   111      *
       
   112      * @param string       $method  Method to call
       
   113      * @param string|array $options Options for this method
       
   114      */
       
   115     public function __call($method, $options)
       
   116     {
       
   117         $part = substr($method, 0, 3);
       
   118         if ((($part != 'get') and ($part != 'set')) or !method_exists($this->_adapter, $method)) {
       
   119             require_once 'Zend/Filter/Exception.php';
       
   120             throw new Zend_Filter_Exception("Unknown method '{$method}'");
       
   121         }
       
   122 
       
   123         return call_user_func_array(array($this->_adapter, $method), $options);
       
   124     }
       
   125 
       
   126     /**
       
   127      * Defined by Zend_Filter_Interface
       
   128      *
       
   129      * Encrypts the content $value with the defined settings
       
   130      *
       
   131      * @param  string $value Content to encrypt
       
   132      * @return string The encrypted content
       
   133      */
       
   134     public function filter($value)
       
   135     {
       
   136         return $this->_adapter->encrypt($value);
       
   137     }
       
   138 }