Exemple #1
0
 public function validateSubmission($value, $params)
 {
     // init vars
     $trusted_mode = $params->get('trusted_mode');
     // get old file value
     $element = new ElementImage();
     $element->identifier = $this->identifier;
     $old_file = $element->setData($this->_item->elements)->getElementData()->get('file');
     $file = '';
     // get file from select list
     if ($trusted_mode && ($file = $value->get('image'))) {
         if (!$this->_inUploadPath($file) && $file != $old_file) {
             throw new YValidatorException(sprintf('This file is not located in the upload directory.'));
         }
         if (!JFile::exists($file)) {
             throw new YValidatorException(sprintf('This file does not exist.'));
         }
         // get file from upload
     } else {
         try {
             // get the uploaded file information
             $userfile = JRequest::getVar('elements_' . $this->identifier, array(), 'files', 'array');
             $max_upload_size = $this->_config->get('max_upload_size', '512') * 1024;
             $max_upload_size = empty($max_upload_size) ? null : $max_upload_size;
             $validator = new YValidatorFile(array('mime_type_group' => 'image', 'max_size' => $max_upload_size));
             $file = $validator->addMessage('mime_type_group', 'Uploaded file is not an image.')->clean($userfile);
         } catch (YValidatorException $e) {
             if ($e->getCode() != UPLOAD_ERR_NO_FILE) {
                 throw $e;
             }
             if (!$trusted_mode && $old_file && $value->get('image')) {
                 $file = $old_file;
             }
         }
     }
     if ($params->get('required') && empty($file)) {
         throw new YValidatorException('Please select an image to upload.');
     }
     return array('file' => $file);
 }
Exemple #2
0
 public function validateSubmission($value, $params)
 {
     // init vars
     $trusted_mode = $params->get('trusted_mode');
     // get old file value
     $element = new ElementDownload();
     $element->identifier = $this->identifier;
     $old_file = $element->setData($this->_item->elements)->getElementData()->get('file');
     $file = '';
     // get file from select list
     if ($trusted_mode && ($file = $value->get('upload'))) {
         if (!$this->_inUploadPath($file) && $file != $old_file) {
             throw new YValidatorException(sprintf('This file is not located in the upload directory.'));
         }
         if (!JFile::exists($file)) {
             throw new YValidatorException(sprintf('This file does not exist.'));
         }
         // get file from upload
     } else {
         try {
             // get the uploaded file information
             $userfile = JRequest::getVar('elements_' . $this->identifier, array(), 'files', 'array');
             // get legal extensions
             $extensions = explode(',', $this->_config->get('upload_extensions', 'png,jpg,doc,mp3,mov,avi,mpg,zip,rar,gz'));
             $extensions = array_map(create_function('$ext', 'return strtolower(trim($ext));'), $extensions);
             //get legal mime types
             $legal_mime_types = new YArray(array_intersect_key(YFile::getMimeMapping(), array_flip($extensions)));
             $legal_mime_types = $legal_mime_types->flattenRecursive();
             // get max upload size
             $max_upload_size = $this->_config->get('max_upload_size', '512') * 1024;
             $max_upload_size = empty($max_upload_size) ? null : $max_upload_size;
             // validate
             $validator = new YValidatorFile(array('mime_types' => $legal_mime_types, 'max_size' => $max_upload_size));
             $file = $validator->addMessage('mime_types', 'Uploaded file is not of a permitted type.')->clean($userfile);
         } catch (YValidatorException $e) {
             if ($e->getCode() != UPLOAD_ERR_NO_FILE) {
                 throw $e;
             }
             if (!$trusted_mode && $old_file && $value->get('upload')) {
                 $file = $old_file;
             }
         }
     }
     if ($params->get('required') && empty($file)) {
         throw new YValidatorException('Please select a file to upload.');
     }
     $validator = new YValidatorInteger(array('required' => false), array('number' => 'The Download Limit needs to be a number.'));
     $download_limit = $validator->clean($value->get('download_limit'));
     $download_limit = empty($download_limit) ? '' : $download_limit;
     return array('file' => $file, 'download_limit' => $download_limit);
 }