/library/Zend/Filter/File/Decrypt.php

https://github.com/MontmereLimited/zf2 · PHP · 105 lines · 39 code · 11 blank · 55 comment · 7 complexity · 9a46d51e79599b63846396346742d471 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Filter\File;
  24. use Zend\Filter,
  25. Zend\Filter\Exception;
  26. /**
  27. * Decrypts a given file and stores the decrypted file content
  28. *
  29. * @uses \Zend\Filter\Decrypt
  30. * @uses \Zend\Filter\Exception
  31. * @category Zend
  32. * @package Zend_Filter
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Decrypt extends Filter\Decrypt
  37. {
  38. /**
  39. * New filename to set
  40. *
  41. * @var string
  42. */
  43. protected $_filename;
  44. /**
  45. * Returns the new filename where the content will be stored
  46. *
  47. * @return string
  48. */
  49. public function getFilename()
  50. {
  51. return $this->_filename;
  52. }
  53. /**
  54. * Sets the new filename where the content will be stored
  55. *
  56. * @param string $filename (Optional) New filename to set
  57. * @return Zend_Filter_File_Encryt
  58. */
  59. public function setFilename($filename = null)
  60. {
  61. $this->_filename = $filename;
  62. return $this;
  63. }
  64. /**
  65. * Defined by Zend_Filter_Interface
  66. *
  67. * Decrypts the file $value with the defined settings
  68. *
  69. * @param string $value Full path of file to change
  70. * @return string The filename which has been set, or false when there were errors
  71. */
  72. public function filter($value)
  73. {
  74. if (!file_exists($value)) {
  75. throw new Exception\InvalidArgumentException("File '$value' not found");
  76. }
  77. if (!isset($this->_filename)) {
  78. $this->_filename = $value;
  79. }
  80. if (file_exists($this->_filename) and !is_writable($this->_filename)) {
  81. throw new Exception\RuntimeException("File '{$this->_filename}' is not writable");
  82. }
  83. $content = file_get_contents($value);
  84. if (!$content) {
  85. throw new Exception\RuntimeException("Problem while reading file '$value'");
  86. }
  87. $decrypted = parent::filter($content);
  88. $result = file_put_contents($this->_filename, $decrypted);
  89. if (!$result) {
  90. throw new Exception\RuntimeException("Problem while writing file '{$this->_filename}'");
  91. }
  92. return $this->_filename;
  93. }
  94. }