/**
  * (non-PHPdoc)
  * @see tao_helpers_form_FormContainer::initElements()
  */
 public function initElements()
 {
     /*
         	$descElt = tao_helpers_form_FormFactory::getElement('qtitest_desc', 'Label');
     $descElt->setValue(__('A qti testpackage'));
     $this->form->addElement($descElt);
     */
     //create file upload form box
     $fileElt = tao_helpers_form_FormFactory::getElement('source', 'AsyncFile');
     $fileElt->setDescription(__("Add the source file"));
     if (isset($_POST['import_sent_xhtml'])) {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
     } else {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty', array('message' => '')));
     }
     $fileElt->addValidators(array(tao_helpers_form_FormFactory::getValidator('FileMimeType', array('mimetype' => array('application/zip', 'application/x-zip', 'application/x-zip-compressed', 'application/octet-stream'), 'extension' => array('zip'))), tao_helpers_form_FormFactory::getValidator('FileSize', array('max' => tao_helpers_Environment::getFileUploadLimit()))));
     $this->form->addElement($fileElt);
     $this->form->createGroup('file', __('Upload a QTI 2.1 Test Package File'), array('source'));
     $element = tao_helpers_form_FormFactory::getElement('uri', 'Hidden');
     //$element->setValue();
     $this->getForm()->addElement($element);
     $xhtmlSentElt = tao_helpers_form_FormFactory::getElement('import_sent_qtitest', 'Hidden');
     $xhtmlSentElt->setValue(1);
     $this->form->addElement($xhtmlSentElt);
 }
 /**
  * overriden
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @return mixed
  */
 public function initElements()
 {
     //create file upload form box
     $fileElt = tao_helpers_form_FormFactory::getElement('source', 'AsyncFile');
     $fileElt->setDescription(__("Add an RDF/XML file"));
     if (isset($_POST['import_sent_rdf'])) {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
     } else {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty', array('message' => '')));
     }
     $fileElt->addValidators(array(tao_helpers_form_FormFactory::getValidator('FileMimeType', array('mimetype' => array('text/xml', 'application/rdf+xml', 'application/xml'), 'extension' => array('rdf', 'rdfs'))), tao_helpers_form_FormFactory::getValidator('FileSize', array('max' => tao_helpers_Environment::getFileUploadLimit()))));
     $this->form->addElement($fileElt);
     $this->form->createGroup('file', __('Import Metadata from RDF file'), array('rdf_desc', 'source'));
     $rdfSentElt = tao_helpers_form_FormFactory::getElement('import_sent_rdf', 'Hidden');
     $rdfSentElt->setValue(1);
     $this->form->addElement($rdfSentElt);
 }
 /**
  * overriden
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @return mixed
  */
 public function initElements()
 {
     //create file upload form box
     $fileElt = tao_helpers_form_FormFactory::getElement('source', 'AsyncFile');
     $fileElt->setDescription(__("Add a CSV file"));
     if (isset($_POST['import_sent_csv'])) {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
     } else {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty', array('message' => '')));
     }
     $fileElt->addValidators(array(tao_helpers_form_FormFactory::getValidator('FileMimeType', array('mimetype' => array('text/plain', 'text/csv', 'text/comma-separated-values', 'application/csv', 'application/csv-tab-delimited-table'), 'extension' => array('csv', 'txt'))), tao_helpers_form_FormFactory::getValidator('FileSize', array('max' => tao_helpers_Environment::getFileUploadLimit()))));
     $this->form->addElement($fileElt);
     $this->form->createGroup('file', __('Import Metadata from CSV file'), array('csv_desc', 'source'));
     $csvSentElt = tao_helpers_form_FormFactory::getElement('import_sent_csv', 'Hidden');
     $csvSentElt->setValue(1);
     $this->form->addElement($csvSentElt);
     // options
     $optDelimiter = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIELD_DELIMITER, 'Textbox');
     $optDelimiter->setDescription(__("Field delimiter"));
     $optDelimiter->setValue(';');
     $optDelimiter->addAttribute("size", 6);
     $optDelimiter->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
     $this->form->addElement($optDelimiter);
     $optEncloser = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIELD_ENCLOSER, 'Textbox');
     $optEncloser->setDescription(__("Field encloser"));
     $optEncloser->setValue('"');
     $optEncloser->addAttribute("size", 6);
     $optEncloser->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
     $this->form->addElement($optEncloser);
     /*
     $optMulti = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::MULTI_VALUES_DELIMITER, 'Textbox');
     $optMulti->setDescription(__("Multiple values delimiter"));
     $optMulti->setValue('|');
     $optMulti->addAttribute("size", 6);
     $optMulti->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
     $this->form->addElement($optMulti);
     */
     $optFirstColumn = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES, 'Checkbox');
     $optFirstColumn->setDescription(__("First row column names"));
     $optFirstColumn->setOptions(array(tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES => ''));
     $optFirstColumn->setValue(tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES);
     $this->form->addElement($optFirstColumn);
     $this->form->createGroup('options', __('CSV Options'), array($optDelimiter, $optEncloser, $optFirstColumn));
 }
 /**
  * overriden
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @return mixed
  */
 public function initElements()
 {
     $descElt = tao_helpers_form_FormFactory::getElement('rdf_desc', 'Label');
     $descElt->setValue(__('Please upload a published delivery assembly .'));
     $this->form->addElement($descElt);
     //create file upload form box
     $fileElt = tao_helpers_form_FormFactory::getElement('source', 'AsyncFile');
     $fileElt->setDescription(__("Add the source file."));
     if (isset($_POST['import_sent_rdf'])) {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
     } else {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty', array('message' => '')));
     }
     $fileElt->addValidators(array(tao_helpers_form_FormFactory::getValidator('FileMimeType', array('mimetype' => array('application/zip', 'application/x-zip', 'application/x-zip-compressed', 'application/octet-stream'), 'extension' => array('zip'))), tao_helpers_form_FormFactory::getValidator('FileSize', array('max' => tao_helpers_Environment::getFileUploadLimit()))));
     $this->form->addElement($fileElt);
     $this->form->createGroup('file', __('Upload an assembly'), array('rdf_desc', 'source'));
     $rdfSentElt = tao_helpers_form_FormFactory::getElement('import_sent_rdf', 'Hidden');
     $rdfSentElt->setValue(1);
     $this->form->addElement($rdfSentElt);
 }
 /**
  * Used to create the form elements and bind them to the form instance
  *
  * @access protected
  * @return mixed
  */
 protected function initElements()
 {
     //create file upload form box
     $fileElt = \tao_helpers_form_FormFactory::getElement('source', 'AsyncFile');
     $fileElt->setDescription(__("Add a media file"));
     if (isset($_POST['import_sent_file'])) {
         $fileElt->addValidator(\tao_helpers_form_FormFactory::getValidator('NotEmpty'));
     } else {
         $fileElt->addValidator(\tao_helpers_form_FormFactory::getValidator('NotEmpty', array('message' => '')));
     }
     $fileElt->addValidators(array(\tao_helpers_form_FormFactory::getValidator('FileSize', array('max' => \tao_helpers_Environment::getFileUploadLimit()))));
     $this->form->addElement($fileElt);
     $langService = \tao_models_classes_LanguageService::singleton();
     $dataUsage = new \core_kernel_classes_Resource(INSTANCE_LANGUAGE_USAGE_DATA);
     $dataLang = \common_session_SessionManager::getSession()->getDataLanguage();
     $dataLang = 'http://www.tao.lu/Ontologies/TAO.rdf#Lang' . $dataLang;
     if (!is_null($this->instanceUri)) {
         $instance = new \core_kernel_classes_Resource($this->instanceUri);
         $lang = $instance->getOnePropertyValue(new \core_kernel_classes_Property(MEDIA_LANGUAGE));
         if ($lang instanceof \core_kernel_classes_Resource) {
             $dataLang = $lang->getUri();
         }
     }
     $langOptions = array();
     foreach ($langService->getAvailableLanguagesByUsage($dataUsage) as $lang) {
         $langOptions[\tao_helpers_Uri::encode($lang->getUri())] = $lang->getLabel();
     }
     $langElt = \tao_helpers_form_FormFactory::getElement('lang', 'Combobox');
     $langElt->setValue(\tao_helpers_Uri::encode($dataLang));
     $langElt->setOptions($langOptions);
     $this->form->addElement($langElt);
     $this->form->createGroup('options', __('Media Options'), array($langElt));
     $fileSentElt = \tao_helpers_form_FormFactory::getElement('import_sent_file', 'Hidden');
     $fileSentElt->setValue(1);
     $this->form->addElement($fileSentElt);
     if (!is_null($this->instanceUri)) {
         $instanceElt = \tao_helpers_form_FormFactory::getElement('instanceUri', 'Hidden');
         $instanceElt->setValue($this->instanceUri);
         $this->form->addElement($instanceElt);
     }
 }
 /**
  * (non-PHPdoc)
  * @see tao_helpers_form_FormContainer::initElements()
  */
 public function initElements()
 {
     //create file upload form box
     $fileElt = tao_helpers_form_FormFactory::getElement('source', 'AsyncFile');
     $fileElt->setDescription(__("Add a zip file containing QTI/APIP tests and items"));
     if (isset($_POST['import_sent_qti'])) {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
     } else {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty', array('message' => '')));
     }
     $fileElt->addValidators(array(tao_helpers_form_FormFactory::getValidator('FileMimeType', array('mimetype' => array('application/zip', 'application/x-zip', 'application/x-zip-compressed', 'application/octet-stream'), 'extension' => array('zip'))), tao_helpers_form_FormFactory::getValidator('FileSize', array('max' => tao_helpers_Environment::getFileUploadLimit()))));
     $this->form->addElement($fileElt);
     /*
     $disableValidationElt = tao_helpers_form_FormFactory::getElement("disable_validation", 'Checkbox');
     $disableValidationElt->setDescription(__("Disable validation"));
     $disableValidationElt->setOptions(array("on" => ""));
     $this->form->addElement($disableValidationElt);
     */
     $this->form->createGroup('file', __('Import a QTI/APIP Content Package'), array('source'));
     $qtiSentElt = tao_helpers_form_FormFactory::getElement('import_sent_qti', 'Hidden');
     $qtiSentElt->setValue(1);
     $this->form->addElement($qtiSentElt);
 }
Esempio n. 7
0
 /**
  * Get the files data from an HTTP file upload (ie. from the $_FILES)
  * @author "Bertrand Chevrier <*****@*****.**>
  * @param string the file field name 
  * @return array the file data
  * @throws common_exception_Error in case of wrong upload
  */
 public static function getUploadedFile($name)
 {
     // for large file, the $_FILES may be empty so see this before checking for other updates
     $limit = tao_helpers_Environment::getFileUploadLimit();
     $contentLength = intval($_SERVER['CONTENT_LENGTH']);
     if ($limit > 0 && $contentLength > $limit && count(self::getFiles()) === 0) {
         throw new FileUploadException('Exceeded filesize limit of ' . tao_helpers_Environment::getFileUploadLimit());
     }
     $files = self::getFiles();
     $fileData = $files[$name];
     if (isset($files[$name])) {
         //check for upload errors
         if (isset($fileData['error']) && $fileData['error'] != UPLOAD_ERR_OK) {
             switch ($fileData['error']) {
                 case UPLOAD_ERR_NO_FILE:
                     throw new FileUploadException('No file sent.');
                 case UPLOAD_ERR_INI_SIZE:
                 case UPLOAD_ERR_FORM_SIZE:
                     throw new FileUploadException('Exceeded filesize limit of ' . tao_helpers_Environment::getFileUploadLimit());
                 default:
                     throw new common_exception_Error('Upload fails, check errors');
             }
         }
     }
     if (!is_uploaded_file($fileData['tmp_name'])) {
         throw new common_exception_Error('Non uploaded file in filedata, potential attack');
     }
     return $fileData;
 }
Esempio n. 8
0
    /**
     * Short description of method render
     *
     * @access public
     * @author Joel Bout, <*****@*****.**>
     * @return string
     */
    public function render()
    {
        $widgetName = 'Uploader_' . md5($this->name);
        $returnValue = $this->renderLabel();
        $returnValue .= "<input type='hidden' name='{$this->name}' id='{$this->name}' value='' />";
        $returnValue .= "<div id='{$widgetName}_container' class='form-elt-container file-uploader'>";
        // get the upload max size
        $fileSize = tao_helpers_Environment::getFileUploadLimit();
        $mimetypes = array();
        // add a client validation
        foreach ($this->validators as $validator) {
            // get the valid file extensions
            if ($validator instanceof tao_helpers_form_validators_FileMimeType) {
                $options = $validator->getOptions();
                if (isset($options['mimetype'])) {
                    $mimetypes = $options['mimetype'];
                }
            }
            // get the max file size
            if ($validator instanceof tao_helpers_form_validators_FileSize) {
                $options = $validator->getOptions();
                if (isset($options['max'])) {
                    $validatorMax = (int) $options['max'];
                    if ($validatorMax > 0 && $validatorMax < $fileSize) {
                        $fileSize = $validatorMax;
                    }
                }
            }
        }
        // default value for 'auto' is 'true':
        $auto = 'true';
        if (isset($this->attributes['auto'])) {
            if (!$this->attributes['auto'] || $this->attributes['auto'] === 'false') {
                $auto = 'false';
            }
            unset($this->attributes['auto']);
        }
        // initialize the Uploader Js component
        $returnValue .= '<script type="text/javascript">
				require([\'jquery\',  \'ui/feedback\', \'ui/uploader\'], function($, feedback){
					 $("#' . $widgetName . '_container").uploader({
							 uploadUrl: "' . ROOT_URL . 'tao/File/upload",
							inputName: "' . $widgetName . '",
							autoUpload: "' . $auto . '"  ,
							showResetButton: "' . !$auto . '" ,
							showUploadButton: "' . !$auto . '" ,
							fileSelect  : function(files, done){
											var error = [],
												files = files.filter(_.isObject),// due to Chrome drag\'n\'drop issue
												givenLength = files.length,
												filters = "' . implode(',', $mimetypes) . '".split(",").filter(function(e){return e.length});

												if (filters.length){

													files = _.filter(files, function(file){
														return !file.type || _.contains(filters, file.type.replace(/[\'"]+/g, \'\'));//IE9 doesnt detect type, so lets rely on server validation
													});

													if(files.length !== givenLength){
														error.push( "Unauthorized files have been removed");
													}

												}

												files = _.filter(files, function(file){
														return file.size <= ' . $fileSize . ';
													});

												if(files.length !== givenLength && !error.length){
													error.push( "Size limit is ' . $fileSize . ' bytes");
												}

												if (error.length){
													feedback().error(error.join(","));
												}

												done(files);
												if ( ' . $auto . ' ){
													$(this).uploader("upload");
												}
										 }

					 }).on("upload.uploader", function(e, file, result){
					 	if ( result && result.uploaded ){
							$("input[name=\'' . $this->getName() . '\']").val(result.data);
						}
					 })
			});
			</script>';
        $returnValue .= "</div>";
        return (string) $returnValue;
    }
Esempio n. 9
0
 /**
  * Produce a simple view use to display a file upload form in a popup
  */
 public function htmlUpload()
 {
     if ($this->hasRequestParameter('sizeLimit')) {
         $this->setData('sizeLimit', (int) $this->getRequestParameter('sizeLimit'));
     } else {
         $this->setData('sizeLimit', tao_helpers_Environment::getFileUploadLimit());
     }
     if ($this->hasRequestParameter('target')) {
         $this->setData('target', $this->getRequestParameter('target'));
     } else {
         $this->setData('target', "#source");
     }
     $this->setData('accept', '*');
     if ($this->hasRequestParameter('fileExt')) {
         $accept = '';
         foreach (explode(',', $this->getRequestParameter('fileExt')) as $fileExt) {
             $accept .= tao_helpers_File::getMimeType(str_replace('*', 'file', $fileExt));
         }
         $this->setData('accept', $accept);
     }
     $response = array('uploaded' => false, 'data' => '', 'name' => '', 'uploaded_file' => '');
     if (isset($_FILES) && isset($_POST['upload_sent'])) {
         if (isset($_FILES['Filedata'])) {
             $response = array_merge($response, $this->uploadFile($_FILES['Filedata'], '/'));
         }
     }
     $setLinear = true;
     if ($this->hasRequestParameter('format')) {
         if ($this->getRequestParameter('format') != 'linear') {
             $setLinear = false;
         }
     }
     $this->setData('setLinear', $setLinear);
     $this->setData('uploaded', $response['uploaded'] === true);
     $this->setData('uploadData', $response['data']);
     $this->setData('uploadFile', $response['name']);
     $this->setData('uploadFilePath', $response['uploaded_file']);
     $this->setView('form/html_upload.tpl');
 }
 /**
  * Get the files data from an HTTP file upload (ie. from the $_FILES)
  * @author "Bertrand Chevrier <*****@*****.**>
  * @param string the file field name 
  * @return array the file data
  * @throws common_exception_Error in case of wrong upload
  */
 public static function getUploadedFile($name)
 {
     $files = self::getFiles();
     $fileData = $files[$name];
     if (isset($files[$name])) {
         //check for upload errors
         if (isset($fileData['error']) && $fileData['error'] != UPLOAD_ERR_OK) {
             switch ($fileData['error']) {
                 case UPLOAD_ERR_NO_FILE:
                     throw new FileUploadException('No file sent.');
                 case UPLOAD_ERR_INI_SIZE:
                 case UPLOAD_ERR_FORM_SIZE:
                     throw new FileUploadException('Exceeded filesize limit of ' . tao_helpers_Environment::getFileUploadLimit());
                 default:
                     throw new common_exception_Error('Upload fails, check errors');
             }
         }
     }
     if (!is_uploaded_file($fileData['tmp_name'])) {
         throw new common_exception_Error('Non uploaded file in filedata, potential attack');
     }
     return $fileData;
 }
 /**
  * returns the fileupload limit in either Bytes or MB
  * default is Bytes
  *
  * @author CRP Henri Tudor - TAO Team - {@link http://www.tao.lu}
  * @param boolean $inMegabytes
  * @return number
  */
 private function getFileUploadLimit($inMegabytes = false)
 {
     $limit = min(tao_helpers_Environment::getFileUploadLimit(), UPLOAD_MAX_SIZE);
     return $inMegabytes ? round($limit / 1048576, 1) : $limit;
 }