Exemple #1
0
 /**
  * Defined by Zend\Filter\FilterInterface
  *
  * Decrypts the file $value with the defined settings
  *
  * @param  string|array $value Full path of file to change or $_FILES data array
  * @return string|array The filename which has been set
  * @throws Exception\InvalidArgumentException
  * @throws Exception\RuntimeException
  */
 public function filter($value)
 {
     // An uploaded file? Retrieve the 'tmp_name'
     $isFileUpload = is_array($value) && isset($value['tmp_name']);
     if ($isFileUpload) {
         $uploadData = $value;
         $value = $value['tmp_name'];
     }
     if (!file_exists($value)) {
         throw new Exception\InvalidArgumentException("File '{$value}' not found");
     }
     if (!isset($this->filename)) {
         $this->filename = $value;
     }
     if (file_exists($this->filename) and !is_writable($this->filename)) {
         throw new Exception\RuntimeException("File '{$this->filename}' is not writable");
     }
     $content = file_get_contents($value);
     if (!$content) {
         throw new Exception\RuntimeException("Problem while reading file '{$value}'");
     }
     $decrypted = parent::filter($content);
     $result = file_put_contents($this->filename, $decrypted);
     if (!$result) {
         throw new Exception\RuntimeException("Problem while writing file '{$this->filename}'");
     }
     if ($isFileUpload) {
         $uploadData['tmp_name'] = $this->filename;
         return $uploadData;
     }
     return $this->filename;
 }
Exemple #2
0
 /**
  * Defined by Zend\Filter\FilterInterface
  *
  * Decrypts the file $value with the defined settings
  *
  * @param  string $value Full path of file to change
  * @return string The filename which has been set, or false when there were errors
  * @throws Exception\InvalidArgumentException
  * @throws Exception\RuntimeException
  */
 public function filter($value)
 {
     if (!file_exists($value)) {
         throw new Exception\InvalidArgumentException("File '{$value}' not found");
     }
     if (!isset($this->_filename)) {
         $this->_filename = $value;
     }
     if (file_exists($this->_filename) and !is_writable($this->_filename)) {
         throw new Exception\RuntimeException("File '{$this->_filename}' is not writable");
     }
     $content = file_get_contents($value);
     if (!$content) {
         throw new Exception\RuntimeException("Problem while reading file '{$value}'");
     }
     $decrypted = parent::filter($content);
     $result = file_put_contents($this->_filename, $decrypted);
     if (!$result) {
         throw new Exception\RuntimeException("Problem while writing file '{$this->_filename}'");
     }
     return $this->_filename;
 }
Exemple #3
0
    /**
     * Ensures that the filter allows de/encryption
     *
     * @return void
     */
    public function testEncryptionWithDecryptionOpenssl()
    {
        if (!extension_loaded('openssl')) {
            $this->markTestSkipped('Openssl extension not installed');
        }
        $filter = new EncryptFilter(array('adapter' => 'Openssl'));
        $filter->setPublicKey(__DIR__ . '/_files/publickey.pem');
        $output = $filter('teststring');
        $envelopekeys = $filter->getEnvelopeKey();
        $this->assertNotEquals('teststring', $output);
        $filter = new DecryptFilter(array('adapter' => 'Openssl'));
        $filter->setPassphrase('zPUp9mCzIrM7xQOEnPJZiDkBwPBV9UlITY0Xd3v4bfIwzJ12yPQCAkcR5BsePGVw
RK6GS5RwXSLrJu9Qj8+fk0wPj6IPY5HvA9Dgwh+dptPlXppeBm3JZJ+92l0DqR2M
ccL43V3Z4JN9OXRAfGWXyrBJNmwURkq7a2EyFElBBWK03OLYVMevQyRJcMKY0ai+
tmnFUSkH2zwnkXQfPUxg9aV7TmGQv/3TkK1SziyDyNm7GwtyIlfcigCCRz3uc77U
Izcez5wgmkpNElg/D7/VCd9E+grTfPYNmuTVccGOes+n8ISJJdW0vYX1xwWv5l
bK22CwD/l7SMBOz4M9XH0Jb0OhNxLza4XMDu0ANMIpnkn1KOcmQ4gB8fmAbBt');
        $filter->setPrivateKey(__DIR__ . '/_files/privatekey.pem');
        $filter->setEnvelopeKey($envelopekeys);
        $input = $filter($output);
        $this->assertEquals('teststring', trim($input));
    }
Exemple #4
0
 /**
  * @return void
  */
 public function testCallingUnknownMethod()
 {
     if (!extension_loaded('mcrypt')) {
         $this->markTestSkipped('Mcrypt extension not installed');
     }
     $this->setExpectedException('\\Zend\\Filter\\Exception\\BadMethodCallException', 'Unknown method');
     $filter = new DecryptFilter();
     $filter->getUnknownMethod();
 }
Exemple #5
0
 /**
  * @dataProvider returnUnfilteredDataProvider
  * @return void
  */
 public function testReturnUnfiltered($input)
 {
     if (!extension_loaded('mcrypt')) {
         $this->markTestSkipped('Mcrypt extension not installed');
     }
     $decrypt = new DecryptFilter(array('adapter' => 'BlockCipher', 'key' => 'testkey'));
     $decrypt->setVector('1234567890123456890');
     $decrypted = $decrypt->filter($input);
     $this->assertEquals($input, $decrypted);
 }