public function testZF3891()
 {
     $files = array('name' => 'testsize.mo', 'type' => 'text', 'size' => 200, 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo', 'error' => 0);
     $validator = new Zend_Validate_File_Extension(array('MO', 'case' => true));
     $this->assertEquals(false, $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo', $files));
     $validator = new Zend_Validate_File_Extension(array('MO', 'case' => false));
     $this->assertEquals(true, $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo', $files));
 }
Esempio n. 2
0
 /**
  * Ensures that the validator follows expected behavior
  *
  * @return void
  */
 public function testBasic()
 {
     $valuesExpected = array(array('mo', true), array('gif', false), array(array('mo'), true), array(array('gif'), false), array(array('gif', 'pdf', 'mo', 'pict'), true), array(array('gif', 'gz', 'hint'), false));
     foreach ($valuesExpected as $element) {
         $validator = new Zend_Validate_File_Extension($element[0]);
         $this->assertEquals($element[1], $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo'));
     }
 }
Esempio n. 3
0
 /**
  * Defined by Zend_Validate_Interface
  *
  * Returns true if and only if the imagesize of $value is at least min and
  * not bigger than max
  *
  * @param  string $value Real file to check for image size
  * @param  array  $file  File data from Zend_File_Transfer
  * @return boolean
  */
 public function isValid($value, $file = null)
 {
     if ($file === null) {
         $file = array('type' => null, 'name' => $value);
     }
     if (isset($this->_dir)) {
         $value = $this->_dir . "/" . $value;
     }
     return parent::isValid($value, $file);
 }
Esempio n. 4
0
 /**
  * Returns true if and only if the fileextension of $value is included in 
  * the set extension list.
  *
  * @param string $value Real file to check for extension.
  * @param array $file File data from Zend_File_Transfer.
  * @return boolean
  */
 public function isValid($value, $file = null)
 {
     // Is file readable ?
     require_once 'Zend/Loader.php';
     if (!Zend_Loader::isReadable($value)) {
         return $this->_throw($file, self::NOT_FOUND);
     }
     if ($file !== null) {
         $info['extension'] = substr($file['name'], strrpos($file['name'], '.') + 1);
     } else {
         $info = pathinfo($value);
     }
     //set the target extension
     $this->_targetExtension = $info['extension'];
     return parent::isValid($value, $file);
 }
Esempio n. 5
0
 /**
  * Defined by Zend_Validate_Interface
  *
  * Returns true if and only if the imagesize of $value is at least min and
  * not bigger than max
  *
  * @param  string $value Real file to check for image size
  * @param  array  $file  File data from Zend_File_Transfer
  * @return boolean
  */
 public function isValid($value, $file = null)
 {
     $this->_loadParams();
     $extensions = $this->_fields[$this->_fieldId]['params']['extensions'];
     if (is_array($extensions) && count($extensions)) {
         foreach ($extensions as $ext) {
             $this->addExtension($ext);
         }
     }
     if ($file === null) {
         $file = array('type' => null, 'name' => $value);
     }
     if (isset($this->_dir)) {
         $value = $this->_dir . "/" . $value;
     }
     return parent::isValid($value, $file);
 }
Esempio n. 6
0
 /**
  * comments
  */
 public function renderImage($src, $height = '', $width = '', $attribs = array('class' => 'icon'))
 {
     $absPath = BASE_PATH . '/' . $src;
     $validator = new Zend_Validate_File_Extension(array('jpg', 'jpeg', 'gif', 'png'));
     if (!is_file($absPath) || !$validator->isValid($src)) {
         return '<span>' . $src . '</span>';
     }
     if ($src != '' && is_file($absPath)) {
         $imageSize = getimagesize($absPath);
         $srcHeight = $imageSize[0];
         $srcWidth = $imageSize[1];
         $percentage = 1.0;
         //if the height is greater than the width then adjust by the height
         //otherwise adjust by the width
         if (isset($height) && !empty($height) && $srcHeight > $srcWidth) {
             $percentage = $height / $srcHeight;
         } elseif (isset($width) && !empty($width)) {
             $percentage = $width / $srcWidth;
         }
         if (isset($height) && !empty($height)) {
             $height = 'height:' . round($srcHeight * $percentage) . 'px; ';
         } else {
             $height = '';
         }
         if (isset($width) && !empty($width)) {
             //gets the new value and applies the percentage, then rounds the value
             $width = 'width:' . round($srcWidth * $percentage) . 'px; ';
         } else {
             $width = '';
         }
         $attributes = null;
         if (is_array($attribs)) {
             foreach ($attribs as $k => $v) {
                 $attributes .= $k . "='" . $v . "' ";
             }
         }
         return '<img style="' . $width . $height . '" src="' . $this->view->getBaseUrl() . '/' . $src . '" ' . $attributes . ' />';
     }
 }
Esempio n. 7
0
 /**
  * Unzip file
  *
  * @param string $path
  * @return boolean
  */
 public function unzip($path, $newDir = false)
 {
     $filePath = $this->getRealPath($path);
     if (!file_exists($filePath)) {
         $this->returnError('File does not exists ' . $path);
         return false;
     }
     // get the absolute path to $file
     $dir = pathinfo($filePath, PATHINFO_DIRNAME);
     $packageName = pathinfo($filePath, PATHINFO_FILENAME);
     if ($newDir) {
         $dir .= DIRECTORY_SEPARATOR . $packageName;
     }
     $zip = new ZipArchive();
     $res = $zip->open($filePath);
     if (!$res) {
         $this->returnError('Cannot extract file');
         return false;
     }
     $tmpDir = APPLICATION_PATH . '/../tmp/' . session_id() . '-' . $packageName;
     $zip->extractTo($tmpDir);
     $zip->close();
     //check size
     $extractSize = $this->_helper->getDirSize($tmpDir);
     if ($extractSize > $this->_helper->getFreeSpace()) {
         $this->_rrmdir($tmpDir);
         $this->returnError('Not enough space on disk');
         return false;
     }
     $files = array();
     $this->_rlistFiles($tmpDir, $files);
     $mimeValidator = new Zend_Validate_File_MimeType($this->_helper->getDefaultMimeTypes());
     $extValidator = new Zend_Validate_File_Extension($this->_helper->getDefaultExtensions());
     foreach ($files as $currFile) {
         if (!$extValidator->isValid($currFile)) {
             $this->_rrmdir($tmpDir);
             $this->returnError(sprintf('File [%s] has invalid extension.', basename($currFile)));
             return false;
         }
         if (!$mimeValidator->isValid($currFile)) {
             $this->_rrmdir($tmpDir);
             $this->returnError(sprintf('File [%s] has invalid mime type.', basename($currFile)));
             return false;
         }
     }
     $this->_rcopy($tmpDir, $dir);
     $this->_rrmdir($tmpDir);
     return true;
 }