/** * 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; }
/** * processes errorhandling etc for uploaded archive * @param string $tmpFile path and filename to uploaded file * @param string $storageMethod deliverFile or storeUploadedFile * @param boolean $persistentErrorMessage Defines whether sendInfo will be persistent or not */ function processUploadedFile($fileTmp, $storageMethod, $persistentErrorMessage, $a_ass_id) { global $lng, $ilUser; // Create unzip-directory $newDir = ilUtil::ilTempnam(); ilUtil::makeDir($newDir); include_once "Services/Utilities/classes/class.ilFileUtils.php"; try { $processDone = ilFileUtils::processZipFile($newDir, $fileTmp, false); ilFileUtils::recursive_dirscan($newDir, $filearray); foreach ($filearray["file"] as $key => $filename) { $a_http_post_files["name"] = ilFileUtils::utf8_encode($filename); $a_http_post_files["type"] = "other"; $a_http_post_files["tmp_name"] = $filearray["path"][$key] . "/" . $filename; $a_http_post_files["error"] = 0; $a_http_post_files["size"] = filesize($filearray["path"][$key] . "/" . $filename); if ($storageMethod == "deliverFile") { $this->{$storageMethod}($a_http_post_files, $a_ass_id, $ilUser->id, true); } else { if ($storageMethod == "storeUploadedFile") { $this->file_obj->{$storageMethod}($a_http_post_files, true, true); } } } ilExerciseMembers::_writeReturned($this->getId(), $ilUser->id, 1); ilUtil::sendSuccess($this->lng->txt("file_added"), $persistentErrorMessage); } catch (ilFileUtilsException $e) { ilUtil::sendFailure($e->getMessage(), $persistentErrorMessage); } ilUtil::delDir($newDir); return $processDone; }
/** * saveUnzip object * * @access public */ function saveUnzip() { $zip_form_gui = $this->initZipUploadForm(); if ($this->checkPermissionBool("create", "", "file")) { if ($zip_form_gui->checkInput()) { $zip_file = $zip_form_gui->getInput("zip_file"); $adopt_structure = $zip_form_gui->getInput("adopt_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; } // 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?) try { $processDone = ilFileUtils::processZipFile($newDir, $zip_file["tmp_name"], $adopt_structure && $permission, $this->parent_id, $containerType, $tree, $access_handler); ilUtil::sendSuccess($this->lng->txt("file_added"), true); } catch (ilFileUtilsException $e) { ilUtil::sendFailure($e->getMessage(), true); } ilUtil::delDir($newDir); $this->ctrl->returnToParent($this); } else { $zip_form_gui->setValuesByPost(); $this->tpl->setContent($zip_form_gui->getHTML()); } } else { $this->ilErr->raiseError($this->lng->txt("permission_denied"), $this->ilErr->MESSAGE); } }