/**
  * get mime type for file
  *
  * @param	string		$a_file		file name
  * @return	string					mime type
  * static
  */
 static function getMimeType($a_file)
 {
     include_once "./Services/Utilities/classes/class.ilMimeTypeUtil.php";
     $mime = ilMimeTypeUtil::getMimeType($a_file);
     return $mime;
 }
 /**
  * PUT method handler
  *
  * @param  array  parameter passing array
  * @return bool   true on success
  */
 public function PUT(&$options)
 {
     global $ilUser;
     $this->writelog('PUT(' . var_export($options, true) . ')');
     $path = $this->davDeslashify($options['path']);
     $parent = dirname($path);
     $name = $this->davBasename($path);
     // get dav object for path
     $parentDAV =& $this->getObject($parent);
     // sanity check
     if (is_null($parentDAV) || !$parentDAV->isCollection()) {
         return '409 Conflict';
     }
     // Prevent putting of files which exceed upload limit
     // FIXME: since this is an optional parameter, we should to do the
     // same check again in function PUTfinished.
     if ($options['content_length'] != null && $options['content_length'] > $this->getUploadMaxFilesize()) {
         $this->writelog('PUT is forbidden, because content length=' . $options['content_length'] . ' is larger than upload_max_filesize=' . $this->getUploadMaxFilesize() . 'in php.ini');
         return '403 Forbidden';
     }
     // determine mime type
     include_once "./Services/Utilities/classes/class.ilMimeTypeUtil.php";
     $mime = ilMimeTypeUtil::getMimeType("", $name, $options['content_type']);
     $objDAV =& $this->getObject($path);
     if (is_null($objDAV)) {
         $ttype = $parentDAV->getILIASFileType();
         $isperm = $parentDAV->isPermitted('create', $ttype);
         if (!$isperm) {
             $this->writelog('PUT is forbidden, because user has no create permission');
             return '403 Forbidden';
         }
         $options["new"] = true;
         $objDAV =& $parentDAV->createFile($name);
         $this->writelog('PUT obj=' . $objDAV . ' name=' . $name . ' content_type=' . $options['content_type']);
         //$objDAV->setContentType($options['content_type']);
         $objDAV->setContentType($mime);
         if ($options['content_length'] != null) {
             $objDAV->setContentLength($options['content_length']);
         }
         $objDAV->write();
         // Record write event
         ilChangeEvent::_recordWriteEvent($objDAV->getObjectId(), $ilUser->getId(), 'create', $parentDAV->getObjectId());
     } else {
         if ($objDAV->isNullResource()) {
             if (!$parentDAV->isPermitted('create', $parentDAV->getILIASFileType())) {
                 $this->writelog('PUT is forbidden, because user has no create permission');
                 return '403 Forbidden';
             }
             $options["new"] = false;
             $objDAV =& $parentDAV->createFileFromNull($name, $objDAV);
             $this->writelog('PUT obj=' . $objDAV . ' name=' . $name . ' content_type=' . $options['content_type']);
             //$objDAV->setContentType($options['content_type']);
             $objDAV->setContentType($mime);
             if ($options['content_length'] != null) {
                 $objDAV->setContentLength($options['content_length']);
             }
             $objDAV->write();
             // Record write event
             ilChangeEvent::_recordWriteEvent($objDAV->getObjectId(), $ilUser->getId(), 'create', $parentDAV->getObjectId());
         } else {
             if (!$objDAV->isPermitted('write')) {
                 $this->writelog('PUT is forbidden, because user has no write permission');
                 return '403 Forbidden';
             }
             $options["new"] = false;
             $this->writelog('PUT obj=' . $objDAV . ' name=' . $name . ' content_type=' . $options['content_type'] . ' content_length=' . $options['content_length']);
             // Create a new version if the previous version is not empty
             if ($objDAV->getContentLength() != 0) {
                 $objDAV->createNewVersion();
             }
             //$objDAV->setContentType($options['content_type']);
             $objDAV->setContentType($mime);
             if ($options['content_length'] != null) {
                 $objDAV->setContentLength($options['content_length']);
             }
             $objDAV->write();
             // Record write event
             ilChangeEvent::_recordWriteEvent($objDAV->getObjectId(), $ilUser->getId(), 'update');
             ilChangeEvent::_catchupWriteEvents($objDAV->getObjectId(), $ilUser->getId(), 'update');
         }
     }
     // store this object, we reuse it in method PUTfinished
     $this->putObjDAV = $objDAV;
     $out =& $objDAV->getContentOutputStream();
     $this->writelog('PUT outputstream=' . $out);
     return $out;
 }
Exemple #3
0
         break;
     case UPLOAD_ERR_NO_TMP_DIR:
         $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_missing_tmp_dir"));
         break;
     case UPLOAD_ERR_CANT_WRITE:
         $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_cannot_write_to_disk"));
         break;
     case UPLOAD_ERR_EXTENSION:
         $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_upload_stopped_ext"));
         break;
 }
 // check suffixes
 if (!$errors->fields && !$errors->general) {
     $finfo = pathinfo($_FILES['img_file']['name']);
     require_once 'Services/Utilities/classes/class.ilMimeTypeUtil.php';
     $mime_type = ilMimeTypeUtil::getMimeType($_FILES['img_file']['tmp_name'], $_FILES['img_file']['name'], $_FILES['img_file']['type']);
     if (!in_array(strtolower($finfo['extension']), $tinyMCE_valid_imgs) || !in_array($mime_type, array('image/gif', 'image/jpeg', 'image/png'))) {
         $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_wrong_file_type"));
     }
 }
 // virus handling
 if (!$errors->fields && !$errors->general) {
     if ($_FILES['img_file']["tmp_name"] != "") {
         $vir = ilUtil::virusHandling($_FILES['img_file']["tmp_name"], $_FILES['img_file']["name"]);
         if ($vir[0] == false) {
             $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
         }
     }
 }
 if (!$errors->fields && !$errors->general) {
     include_once 'webservice/soap/include/inc.soap_functions.php';
 /**
  * Function to parse incoming data from form input value $value. returns the strin/number/etc. to store in the database.
  * @param $value
  * @param ilDataCollectionRecordField $record_field
  * @return int|string
  */
 public function parseValue($value, ilDataCollectionRecordField $record_field)
 {
     $return = false;
     if ($this->id == ilDataCollectionDatatype::INPUTFORMAT_FILE) {
         $file = $value;
         if ($file['tmp_name']) {
             $file_obj = new ilObjFile();
             $file_obj->setType("file");
             $file_obj->setTitle($file["name"]);
             $file_obj->setFileName($file["name"]);
             $file_obj->setFileType(ilMimeTypeUtil::getMimeType("", $file["name"], $file["type"]));
             $file_obj->setFileSize($file["size"]);
             $file_obj->setMode("object");
             $file_obj->create();
             $file_obj->getUploadFile($file["tmp_name"], $file["name"]);
             $file_id = $file_obj->getId();
             $return = $file_id;
         } else {
             $return = $record_field->getValue();
         }
     } elseif ($this->id == ilDataCollectionDatatype::INPUTFORMAT_MOB) {
         if ($value == -1) {
             //marked for deletion.
             return 0;
         }
         $media = $value;
         if ($media['tmp_name']) {
             $mob = new ilObjMediaObject();
             $mob->setTitle($media['name']);
             $mob->create();
             $mob_dir = ilObjMediaObject::_getDirectory($mob->getId());
             if (!is_dir($mob_dir)) {
                 $mob->createDirectory();
             }
             $media_item = new ilMediaItem();
             $mob->addMediaItem($media_item);
             $media_item->setPurpose("Standard");
             $file_name = ilUtil::getASCIIFilename($media['name']);
             $file_name = str_replace(" ", "_", $file_name);
             $file = $mob_dir . "/" . $file_name;
             $title = $file_name;
             ilUtil::moveUploadedFile($media['tmp_name'], $file_name, $file);
             ilUtil::renameExecutables($mob_dir);
             list($width, $height, $type, $attr) = getimagesize($file);
             $arr_properties = $record_field->getField()->getProperties();
             $new_width = $arr_properties[ilDataCollectionField::PROPERTYID_WIDTH];
             $new_height = $arr_properties[ilDataCollectionField::PROPERTYID_HEIGHT];
             if ($new_width || $new_height) {
                 //only resize if it is bigger, not if it is smaller
                 if ($new_height < $height && $new_width < $width) {
                     //resize proportional
                     if (!$new_height || !$new_width) {
                         $format = ilObjMediaObject::getMimeType($file);
                         $wh = ilObjMediaObject::_determineWidthHeight("", "", $format, "File", $file, "", true, false, $arr_properties[ilDataCollectionField::PROPERTYID_WIDTH], (int) $arr_properties[ilDataCollectionField::PROPERTYID_HEIGHT]);
                     } else {
                         $wh['width'] = (int) $arr_properties[ilDataCollectionField::PROPERTYID_WIDTH];
                         $wh['height'] = (int) $arr_properties[ilDataCollectionField::PROPERTYID_HEIGHT];
                     }
                 }
                 $location = ilObjMediaObject::_resizeImage($file, $wh['width'], $wh['height'], false);
             } else {
                 $location = $title;
             }
             ilObjMediaObject::_saveUsage($mob->getId(), "dcl:html", $record_field->getRecord()->getTable()->getCollectionObject()->getId());
             $format = ilObjMediaObject::getMimeType($file);
             $media_item->setFormat($format);
             $media_item->setLocation($location);
             $media_item->setLocationType("LocalFile");
             $mob->update();
             $return = $mob->getId();
         } else {
             $return = $record_field->getValue();
         }
     } elseif ($this->id == ilDataCollectionDatatype::INPUTFORMAT_DATETIME) {
         return $value["date"] . " " . $value["time"];
     } elseif ($this->id == ilDataCollectionDatatype::INPUTFORMAT_BOOLEAN) {
         $return = $value ? 1 : 0;
     } elseif ($this->id == ilDataCollectionDatatype::INPUTFORMAT_TEXT) {
         $arr_properties = $record_field->getField()->getProperties();
         if ($arr_properties[ilDataCollectionField::PROPERTYID_TEXTAREA]) {
             $return = nl2br($value);
         } else {
             $return = $value;
         }
     } else {
         if ($this->id == ilDataCollectionDatatype::INPUTFORMAT_NUMBER) {
             $return = $value == '' ? null : $value;
             //SW, Ilias Mantis #0011799: Return null otherwise '' is casted to 0 in DB
         } else {
             $return = $value;
         }
     }
     return $return;
 }
 /**
  * Handles the upload of a single file and adds it to the parent object.
  * 
  * @param array $file_upload An array containing the file upload parameters.
  * @return object The response object.
  */
 protected function handleFileUpload($file_upload)
 {
     global $ilUser;
     // file upload params
     $filename = $file_upload["name"];
     $type = $file_upload["type"];
     $size = $file_upload["size"];
     $temp_name = $file_upload["tmp_name"];
     // additional params
     $title = $file_upload["title"];
     $description = $file_upload["description"];
     $extract = $file_upload["extract"];
     $keep_structure = $file_upload["keep_structure"];
     // create answer object
     $response = new stdClass();
     $response->fileName = $filename;
     $response->fileSize = intval($size);
     $response->fileType = $type;
     $response->fileUnzipped = $extract;
     $response->error = null;
     // extract archive?
     if ($extract) {
         $zip_file = $filename;
         $adopt_structure = $keep_structure;
         include_once "Services/Utilities/classes/class.ilFileUtils.php";
         // Create unzip-directory
         $newDir = ilUtil::ilTempnam();
         ilUtil::makeDir($newDir);
         // Check if permission is granted for creation of object, if necessary
         if ($this->id_type != self::WORKSPACE_NODE_ID) {
             $type = ilObject::_lookupType((int) $this->parent_id, true);
         } else {
             $type = ilObject::_lookupType($this->tree->lookupObjectId($this->parent_id), false);
         }
         $tree = $access_handler = null;
         switch ($type) {
             // workspace structure
             case 'wfld':
             case 'wsrt':
                 $permission = $this->checkPermissionBool("create", "", "wfld");
                 $containerType = "WorkspaceFolder";
                 $tree = $this->tree;
                 $access_handler = $this->getAccessHandler();
                 break;
                 // use categories as structure
             // use categories as structure
             case 'cat':
             case 'root':
                 $permission = $this->checkPermissionBool("create", "", "cat");
                 $containerType = "Category";
                 break;
                 // use folders as structure (in courses)
             // use folders as structure (in courses)
             default:
                 $permission = $this->checkPermissionBool("create", "", "fold");
                 $containerType = "Folder";
                 break;
         }
         try {
             // 	processZipFile (
             //		Dir to unzip,
             //		Path to uploaded file,
             //		should a structure be created (+ permission check)?
             //		ref_id of parent
             //		object that contains files (folder or category)
             //		should sendInfo be persistent?)
             ilFileUtils::processZipFile($newDir, $temp_name, $adopt_structure && $permission, $this->parent_id, $containerType, $tree, $access_handler);
         } catch (ilFileUtilsException $e) {
             $response->error = $e->getMessage();
         } catch (Exception $ex) {
             $response->error = $ex->getMessage();
         }
         ilUtil::delDir($newDir);
         // #15404
         if ($this->id_type != self::WORKSPACE_NODE_ID) {
             foreach (ilFileUtils::getNewObjects() as $parent_ref_id => $objects) {
                 if ($parent_ref_id != $this->parent_id) {
                     continue;
                 }
                 foreach ($objects as $object) {
                     $this->after_creation_callback_objects[] = $object;
                 }
             }
         }
     } else {
         if (trim($title) == "") {
             $title = $filename;
         } else {
             // BEGIN WebDAV: Ensure that object title ends with the filename extension
             $fileExtension = ilObjFileAccess::_getFileExtension($filename);
             $titleExtension = ilObjFileAccess::_getFileExtension($title);
             if ($titleExtension != $fileExtension && strlen($fileExtension) > 0) {
                 $title .= '.' . $fileExtension;
             }
             // END WebDAV: Ensure that object title ends with the filename extension
         }
         // create and insert file in grp_tree
         include_once "./Modules/File/classes/class.ilObjFile.php";
         $fileObj = new ilObjFile();
         $fileObj->setTitle($title);
         $fileObj->setDescription($description);
         $fileObj->setFileName($filename);
         include_once "./Services/Utilities/classes/class.ilMimeTypeUtil.php";
         $fileObj->setFileType(ilMimeTypeUtil::getMimeType("", $filename, $type));
         $fileObj->setFileSize($size);
         $this->object_id = $fileObj->create();
         $this->putObjectInTree($fileObj, $this->parent_id);
         // see uploadFiles()
         if (is_array($this->after_creation_callback_objects)) {
             $this->after_creation_callback_objects[] = $fileObj;
         }
         // upload file to filesystem
         $fileObj->createDirectory();
         $fileObj->raiseUploadError(false);
         $fileObj->getUploadFile($temp_name, $filename);
         $this->handleAutoRating($fileObj);
         // BEGIN ChangeEvent: Record write event.
         require_once './Services/Tracking/classes/class.ilChangeEvent.php';
         ilChangeEvent::_recordWriteEvent($fileObj->getId(), $ilUser->getId(), 'create');
         // END ChangeEvent: Record write event.
     }
     return $response;
 }
 /**
  * upload file
  */
 function uploadFile()
 {
     global $lng;
     // determine directory
     $cur_subdir = str_replace(".", "", ilUtil::stripSlashes($_GET["cdir"]));
     $cur_dir = !empty($cur_subdir) ? $this->main_dir . "/" . $cur_subdir : $this->main_dir;
     $tgt_file = null;
     if (is_file($_FILES["new_file"]["tmp_name"])) {
         $tgt_file = $cur_dir . "/" . ilUtil::stripSlashes($_FILES["new_file"]["name"]);
         move_uploaded_file($_FILES["new_file"]["tmp_name"], $tgt_file);
     } elseif ($_POST["uploaded_file"]) {
         include_once 'Services/FileSystem/classes/class.ilUploadFiles.php';
         // check if the file is in the ftp directory and readable
         if (ilUploadFiles::_checkUploadFile($_POST["uploaded_file"])) {
             $tgt_file = $cur_dir . "/" . ilUtil::stripSlashes($_POST["uploaded_file"]);
             // copy uploaded file to data directory
             ilUploadFiles::_copyUploadFile($_POST["uploaded_file"], $tgt_file);
         }
     } else {
         if (trim($_FILES["new_file"]["name"]) == "") {
             ilUtil::sendFailure($lng->txt("cont_enter_a_file"), true);
         }
     }
     if ($tgt_file && is_file($tgt_file)) {
         $unzip = null;
         // extract zip?
         include_once "./Services/Utilities/classes/class.ilMimeTypeUtil.php";
         if (ilMimeTypeUtil::getMimeType($tgt_file) == "application/zip") {
             $this->ctrl->setParameter($this, "upfile", basename($tgt_file));
             $url = $this->ctrl->getLinkTarget($this, "unzipFile");
             $this->ctrl->setParameter($this, "upfile", "");
             include_once "Services/UIComponent/Button/classes/class.ilLinkButton.php";
             $unzip = ilLinkButton::getInstance();
             $unzip->setCaption("unzip");
             $unzip->setUrl($url);
             $unzip = " " . $unzip->render();
         }
         ilUtil::sendSuccess($lng->txt("cont_file_created") . $unzip, true);
         $this->setPerformedCommand("create_file", array("name" => substr($tgt_file, strlen($this->main_dir) + 1)));
     }
     $this->ctrl->saveParameter($this, "cdir");
     ilUtil::renameExecutables($this->main_dir);
     $this->ctrl->redirect($this, "listFiles");
 }
 /**
  * Get the mime type of the requested file
  * @param    string      default type
  * @return   string      mime type
  * @access	public
  */
 public function getMimeType($default = 'application/octet-stream')
 {
     // take a previously set mimetype
     if (isset($this->mimetype)) {
         return $this->mimetype;
     }
     $mime = '';
     // alex: changed due to bug http://www.ilias.de/mantis/view.php?id=9332
     /*		if (extension_loaded('Fileinfo'))
     		{
     			$finfo = finfo_open(FILEINFO_MIME);
     			$mime = finfo_file($finfo, $this->file);
     			finfo_close($finfo);
     			if ($pos = strpos($mime, ' '))
     			{
     				$mime = substr($mime, 0, $pos);
     			}
     		}
     		else
     		{*/
     include_once "./Services/Utilities/classes/class.ilMimeTypeUtil.php";
     $mime = ilMimeTypeUtil::getMimeType($this->file);
     //$mime = ilObjMediaObject::getMimeType($this->file);
     //		}
     // set and return the mime type
     $this->mimetype = $mime ? $mime : $default;
     return $this->mimetype;
 }
 /**	
  * Creates a dav file as a child of this object.
  *
  * @param	string		the name of the file.
  * @return	ilObjectDAV	returns the created object, or null if creation failed.
  */
 function createFile($name)
 {
     global $tree;
     // create and insert Folder in tree
     require_once 'Modules/File/classes/class.ilObjFile.php';
     $newObj = new ilObjFile(0);
     $newObj->setType($this->getILIASFileType());
     $newObj->setTitle($name);
     $newObj->setFileName($name);
     include_once "./Services/Utilities/classes/class.ilMimeTypeUtil.php";
     $mime = ilMimeTypeUtil::getMimeType("", $name, 'application/octet-stream');
     //$newObj->setFileType('application/octet-stream');
     $newObj->setFileType($mime);
     //$newObj->setDescription('');
     $newObj->create();
     $newObj->createReference();
     $newObj->setPermissions($this->getRefId());
     $newObj->putInTree($this->getRefId());
     //$newObj->createDirectory();
     require_once 'class.ilObjFileDAV.php';
     $objDAV = new ilObjFileDAV($newObj->getRefId(), $newObj);
     /*		
     $fs = $objDAV->getContentOutputStream();
     fwrite($fs,' ');
     fclose($fs);
     */
     return $objDAV;
 }
Exemple #9
0
 /**
  * Guesses the file type based on the current values returned by getFileType()
  * and getFileExtension().
  * If getFileType() returns 'application/octet-stream', the file extension is
  * used to guess a more accurate file type.
  */
 function guessFileType($a_file = "")
 {
     $path = pathinfo($a_file);
     if ($path["extension"] != "") {
         $filename = $path["basename"];
     } else {
         $filename = "dummy." . $this->getFileExtension();
     }
     include_once "./Services/Utilities/classes/class.ilMimeTypeUtil.php";
     $mime = ilMimeTypeUtil::getMimeType($a_file, $filename, $this->getFileType());
     return $mime;
     /*
     		$fileType = $this->getFileType();
     		if (strlen($fileType) == 0) {	
     			$fileType = 'application/octet-stream';
     		}
     
     		// Firefox browser assigns 'application/x-pdf' to PDF files, but
     		// it can only handle them if the have the mime-type 'application/pdf'.
     		if ($fileType == 'application/x-pdf')
     		{
     			$fileType = 'application/pdf';
     		}
     
     		if ($fileType == 'application/octet-stream')
     		{
     			$fileExtension = $this->getFileExtension();
     			$mimeArray = array(
     				'mpeg' => 'video/mpeg',
     				'mp3' => 'audio/mpeg',
     				'pdf' => 'application/pdf',
     				'gif' => 'image/gif',
     				'jpg' => 'image/jpg',
     				'png' => 'image/png',
     				'htm' => 'text/html',
     				'html' => 'text/html',
     				'wma' => 'video/x-ms-wma',
     				'wmv' => 'video/x-ms-wmv',
     				'swf' => 'application/x-shockwave-flash',
     			);
     			if (array_key_exists($fileExtension, $mimeArray))
     			{
     				$fileType = $mimeArray[$fileExtension];
     			}
     		}
     		return $fileType;
     */
 }
 /**
  * updates object entry in object_data
  *
  * @access	public
  */
 function update()
 {
     global $ilTabs;
     $form = $this->initPropertiesForm();
     if (!$form->checkInput()) {
         $ilTabs->activateTab("settings");
         $form->setValuesByPost();
         $this->tpl->setContent($form->getHTML());
         return false;
     }
     $data = $form->getInput('file');
     // delete trailing '/' in filename
     while (substr($data["name"], -1) == '/') {
         $data["name"] = substr($data["name"], 0, -1);
     }
     $filename = empty($data["name"]) ? $this->object->getFileName() : $data["name"];
     $title = $form->getInput('title');
     if (strlen(trim($title)) == 0) {
         $title = $filename;
     } else {
         $title = $this->object->checkFileExtension($filename, $title);
     }
     $this->object->setTitle($title);
     if (!empty($data["name"])) {
         switch ($form->getInput('replace')) {
             case 1:
                 $this->object->deleteVersions();
                 $this->object->clearDataDirectory();
                 $this->object->replaceFile($data['tmp_name'], $data['name']);
                 break;
             case 0:
                 $this->object->addFileVersion($data['tmp_name'], $data['name']);
                 break;
         }
         $this->object->setFileName($data['name']);
         include_once "./Services/Utilities/classes/class.ilMimeTypeUtil.php";
         $this->object->setFileType(ilMimeTypeUtil::getMimeType("", $data["name"], $data["type"]));
         $this->object->setFileSize($data['size']);
     }
     $this->object->setDescription($form->getInput('description'));
     $this->update = $this->object->update();
     // BEGIN ChangeEvent: Record update event.
     if (!empty($data["name"])) {
         require_once 'Services/Tracking/classes/class.ilChangeEvent.php';
         global $ilUser;
         ilChangeEvent::_recordWriteEvent($this->object->getId(), $ilUser->getId(), 'update');
         ilChangeEvent::_catchupWriteEvents($this->object->getId(), $ilUser->getId());
     }
     // END ChangeEvent: Record update event.
     // Update ecs export settings
     include_once 'Modules/File/classes/class.ilECSFileSettings.php';
     $ecs = new ilECSFileSettings($this->object);
     $ecs->handleSettingsUpdate();
     ilUtil::sendSuccess($this->lng->txt("msg_obj_modified"), true);
     ilUtil::redirect($this->ctrl->getLinkTarget($this, 'edit', '', false, false));
 }