File indexing completed on 2024-12-22 05:36:41

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Filter
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Filter_Encrypt
0024  */
0025 // require_once 'Zend/Filter/Encrypt.php';
0026 
0027 /**
0028  * Encrypts a given file and stores the encrypted file content
0029  *
0030  * @category   Zend
0031  * @package    Zend_Filter
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Filter_File_Encrypt extends Zend_Filter_Encrypt
0036 {
0037     /**
0038      * New filename to set
0039      *
0040      * @var string
0041      */
0042     protected $_filename;
0043 
0044     /**
0045      * Returns the new filename where the content will be stored
0046      *
0047      * @return string
0048      */
0049     public function getFilename()
0050     {
0051         return $this->_filename;
0052     }
0053 
0054     /**
0055      * Sets the new filename where the content will be stored
0056      *
0057      * @param  string $filename (Optional) New filename to set
0058      * @return Zend_Filter_File_Encryt
0059      */
0060     public function setFilename($filename = null)
0061     {
0062         $this->_filename = $filename;
0063         return $this;
0064     }
0065 
0066     /**
0067      * Defined by Zend_Filter_Interface
0068      *
0069      * Encrypts the file $value with the defined settings
0070      *
0071      * @param  string $value Full path of file to change
0072      * @return string The filename which has been set, or false when there were errors
0073      */
0074     public function filter($value)
0075     {
0076         if (!file_exists($value)) {
0077             // require_once 'Zend/Filter/Exception.php';
0078             throw new Zend_Filter_Exception("File '$value' not found");
0079         }
0080 
0081         if (!isset($this->_filename)) {
0082             $this->_filename = $value;
0083         }
0084 
0085         if (file_exists($this->_filename) and !is_writable($this->_filename)) {
0086             // require_once 'Zend/Filter/Exception.php';
0087             throw new Zend_Filter_Exception("File '{$this->_filename}' is not writable");
0088         }
0089 
0090         $content = file_get_contents($value);
0091         if (!$content) {
0092             // require_once 'Zend/Filter/Exception.php';
0093             throw new Zend_Filter_Exception("Problem while reading file '$value'");
0094         }
0095 
0096         $encrypted = parent::filter($content);
0097         $result    = file_put_contents($this->_filename, $encrypted);
0098 
0099         if (!$result) {
0100             // require_once 'Zend/Filter/Exception.php';
0101             throw new Zend_Filter_Exception("Problem while writing file '{$this->_filename}'");
0102         }
0103 
0104         return $this->_filename;
0105     }
0106 }