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); }
public function validateSubmission($value, $params) { $validator = new YValidatorUrl(array('required' => $params->get('required')), array('required' => 'Please enter an URL.')); $url = $validator->clean($value->get('url')); if ($url) { // get video format $format = $this->getVideoFormat($url); // filter file formats $formats = array_filter($this->_getVideoFormats(), create_function('$a', 'return isset($a["regex"]);')); if (!in_array($format, array_keys($formats))) { throw new YValidatorException('Not a valid video format.'); } } $validator = new YValidatorInteger(array('required' => false), array('number' => 'The Width needs to be a number.')); $width = $validator->clean($value->get('width')); $width = empty($width) ? '' : $width; $validator = new YValidatorInteger(array('required' => false), array('number' => 'The Height needs to be a number.')); $height = $validator->clean($value->get('height')); $height = empty($height) ? '' : $height; $autoplay = $value->get('autoplay'); return compact('url', 'format', 'width', 'height', 'autoplay'); }