/package/app/app/vendor/ZendFramework/library/Zend/Filter/Encrypt.php

https://github.com/richhl/kalturaCE · PHP · 134 lines · 54 code · 14 blank · 66 comment · 11 complexity · b413ca4be45365a0fa62571231071444 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-2009 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 16971 2009-07-22 18:05:45Z mikaelkael $
  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-2009 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. $this->setAdapter($options);
  51. }
  52. /**
  53. * Returns the name of the set adapter
  54. *
  55. * @return string
  56. */
  57. public function getAdapter()
  58. {
  59. return $this->_adapter->toString();
  60. }
  61. /**
  62. * Sets new encryption options
  63. *
  64. * @param string|array $options (Optional) Encryption options
  65. * @return Zend_Filter_Encrypt
  66. */
  67. public function setAdapter($options = null)
  68. {
  69. if (is_string($options)) {
  70. $adapter = $options;
  71. } else if (isset($options['adapter'])) {
  72. $adapter = $options['adapter'];
  73. unset($options['adapter']);
  74. } else {
  75. $adapter = 'Mcrypt';
  76. }
  77. if (!is_array($options)) {
  78. $options = array();
  79. }
  80. if (Zend_Loader::isReadable('Zend/Filter/Encrypt/' . ucfirst($adapter). '.php')) {
  81. $adapter = 'Zend_Filter_Encrypt_' . ucfirst($adapter);
  82. }
  83. if (!class_exists($adapter)) {
  84. Zend_Loader::loadClass($adapter);
  85. }
  86. $this->_adapter = new $adapter($options);
  87. if (!$this->_adapter instanceof Zend_Filter_Encrypt_Interface) {
  88. require_once 'Zend/Filter/Exception.php';
  89. throw new Zend_Filter_Exception("Encoding adapter '" . $adapter . "' does not implement Zend_Filter_Encrypt_Interface");
  90. }
  91. return $this;
  92. }
  93. /**
  94. * Calls adapter methods
  95. *
  96. * @param string $method Method to call
  97. * @param string|array $options Options for this method
  98. */
  99. public function __call($method, $options)
  100. {
  101. $part = substr($method, 0, 3);
  102. if ((($part != 'get') and ($part != 'set')) or !method_exists($this->_adapter, $method)) {
  103. require_once 'Zend/Filter/Exception.php';
  104. throw new Zend_Filter_Exception("Unknown method '{$method}'");
  105. }
  106. return call_user_func_array(array($this->_adapter, $method), $options);
  107. }
  108. /**
  109. * Defined by Zend_Filter_Interface
  110. *
  111. * Encrypts the content $value with the defined settings
  112. *
  113. * @param string $value Content to encrypt
  114. * @return string The encrypted content
  115. */
  116. public function filter($value)
  117. {
  118. return $this->_adapter->encrypt($value);
  119. }
  120. }