/Zend/Filter/Encrypt.php

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