PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Filter/Encrypt.php

http://github.com/zendframework/zf2
PHP | 168 lines | 134 code | 7 blank | 27 comment | 3 complexity | 9c85b75d0fb8cfc71e7cbb6cb5038804 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Filter;
  10. use Traversable;
  11. use Zend\Stdlib\ArrayUtils;
  12. /**
  13. * Encrypts a given string
  14. */
  15. class Encrypt extends AbstractFilter
  16. {
  17. /**
  18. * Encryption adapter
  19. *
  20. * @param Encrypt\EncryptionAlgorithmInterface
  21. */
  22. protected $adapter;
  23. /**
  24. * Class constructor
  25. *
  26. * @param string|array|Traversable $options (Optional) Options to set, if null mcrypt is used
  27. */
  28. public function __construct($options = null)
  29. {
  30. if ($options instanceof Traversable) {
  31. $options = ArrayUtils::iteratorToArray($options);
  32. }
  33. $this->setAdapter($options);
  34. }
  35. /**
  36. * Returns the adapter instance
  37. *
  38. * @throws Exception\RuntimeException
  39. * @throws Exception\InvalidArgumentException
  40. * @return Encrypt\EncryptionAlgorithmInterface
  41. */
  42. public function getAdapterInstance()
  43. {
  44. if ($this->adapter instanceof Encrypt\EncryptionAlgorithmInterface) {
  45. return $this->adapter;
  46. }
  47. $adapter = $this->adapter;
  48. $options = $this->getOptions();
  49. if (! class_exists($adapter)) {
  50. $adapter = __CLASS__ . '\\' . ucfirst($adapter);
  51. if (! class_exists($adapter)) {
  52. throw new Exception\RuntimeException(sprintf(
  53. '%s unable to load adapter; class "%s" not found',
  54. __METHOD__,
  55. $this->adapter
  56. ));
  57. }
  58. }
  59. $this->adapter = new $adapter($options);
  60. if (! $this->adapter instanceof Encrypt\EncryptionAlgorithmInterface) {
  61. throw new Exception\InvalidArgumentException(sprintf(
  62. 'Encryption adapter "%s" does not implement %s\\EncryptionAlgorithmInterface',
  63. $adapter,
  64. __CLASS__
  65. ));
  66. }
  67. return $this->adapter;
  68. }
  69. /**
  70. * Returns the name of the set adapter
  71. *
  72. * @return string
  73. */
  74. public function getAdapter()
  75. {
  76. return $this->adapter->toString();
  77. }
  78. /**
  79. * Sets new encryption options
  80. *
  81. * @param string|array $options (Optional) Encryption options
  82. * @return self
  83. * @throws Exception\DomainException
  84. * @throws Exception\InvalidArgumentException
  85. */
  86. public function setAdapter($options = null)
  87. {
  88. if (is_string($options)) {
  89. $adapter = $options;
  90. } elseif (isset($options['adapter'])) {
  91. $adapter = $options['adapter'];
  92. unset($options['adapter']);
  93. } else {
  94. $adapter = 'BlockCipher';
  95. }
  96. if (!is_array($options)) {
  97. $options = array();
  98. }
  99. if (class_exists('Zend\Filter\Encrypt\\' . ucfirst($adapter))) {
  100. $adapter = 'Zend\Filter\Encrypt\\' . ucfirst($adapter);
  101. } elseif (!class_exists($adapter)) {
  102. throw new Exception\DomainException(
  103. sprintf(
  104. '%s expects a valid registry class name; received "%s", which did not resolve',
  105. __METHOD__,
  106. $adapter
  107. )
  108. );
  109. }
  110. $this->adapter = new $adapter($options);
  111. if (!$this->adapter instanceof Encrypt\EncryptionAlgorithmInterface) {
  112. throw new Exception\InvalidArgumentException(
  113. "Encoding adapter '" . $adapter
  114. . "' does not implement Zend\\Filter\\Encrypt\\EncryptionAlgorithmInterface"
  115. );
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Calls adapter methods
  121. *
  122. * @param string $method Method to call
  123. * @param string|array $options Options for this method
  124. * @return mixed
  125. * @throws Exception\BadMethodCallException
  126. */
  127. public function __call($method, $options)
  128. {
  129. $part = substr($method, 0, 3);
  130. if ((($part != 'get') && ($part != 'set')) || !method_exists($this->adapter, $method)) {
  131. throw new Exception\BadMethodCallException("Unknown method '{$method}'");
  132. }
  133. return call_user_func_array(array($this->adapter, $method), $options);
  134. }
  135. /**
  136. * Defined by Zend\Filter\Filter
  137. *
  138. * Encrypts the content $value with the defined settings
  139. *
  140. * @param string $value Content to encrypt
  141. * @return string The encrypted content
  142. */
  143. public function filter($value)
  144. {
  145. if (!is_string($value) && !is_numeric($value)) {
  146. return $value;
  147. }
  148. return $this->adapter->encrypt($value);
  149. }
  150. }