/** * Determines whether the specified preview object is supported by the renderer. * * @param ilPreview $preview The preview object to check. * @return bool true, if the renderer supports the specified preview object; otherwise, false. */ public function supports($preview) { // let parent check first if (!parent::supports($preview)) { return false; } // get file extension require_once "./Modules/File/classes/class.ilObjFile.php"; include_once './Modules/File/classes/class.ilObjFileAccess.php'; $filename = ilObjFile::_lookupFileName($preview->getObjId()); $ext = ilObjFileAccess::_getFileExtension($filename); // contains that extension? return in_array($ext, $this->getSupportedFileFormats()); }
/** * Appends the text " - Copy" to a filename in the language of * the current user. * * If the provided $nth_copy parameter is greater than 1, then * is appended in round brackets. If $nth_copy parameter is null, then * the function determines the copy number on its own. * * If this function detects, that the filename already ends with " - Copy", * or with "- Copy ($nth_copy), it only appends the number of the copy to * the filename. * * This function retains the extension of the filename. * * Examples: * - Calling ilObjFileAccess::_appendCopyToTitle('Hello.txt', 1) * returns: "Hello - Copy.txt". * * - Calling ilObjFileAccess::_appendCopyToTitle('Hello.txt', 2) * returns: "Hello - Copy (2).txt". * * - Calling ilObjFileAccess::_appendCopyToTitle('Hello - Copy (3).txt', 2) * returns: "Hello - Copy (2).txt". * * - Calling ilObjFileAccess::_appendCopyToTitle('Hello - Copy (3).txt', null) * returns: "Hello - Copy (4).txt". */ public static function _appendNumberOfCopyToFilename($a_file_name, $nth_copy = null) { global $lng; // Get the extension and the filename without the extension $extension = ilObjFileAccess::_getFileExtension($a_file_name); if (strlen($extension) > 0) { $extension = '.' . $extension; $filenameWithoutExtension = substr($a_file_name, 0, -strlen($extension)); } else { $filenameWithoutExtension = $a_file_name; } // create a regular expression from the language text copy_n_of_suffix, so that // we can match it against $filenameWithoutExtension, and retrieve the number of the copy. // for example, if copy_n_of_suffix is 'Copy (%1s)', this creates the regular // expression '/ Copy \\([0-9]+)\\)$/'. $nthCopyRegex = preg_replace('/([\\^$.\\[\\]|()?*+{}])/', '\\\\${1}', ' ' . $lng->txt('copy_n_of_suffix')); $nthCopyRegex = '/' . preg_replace('/%1\\\\\\$s/', '([0-9]+)', $nthCopyRegex) . '$/'; // Get the filename without any previously added number of copy. // Determine the number of copy, if it has not been specified. if (preg_match($nthCopyRegex, $filenameWithoutExtension, $matches)) { // this is going to be at least the third copy of the filename $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen($matches[0])); if ($nth_copy == null) { $nth_copy = $matches[1] + 1; } } else { if (substr($filenameWithoutExtension, -strlen(' ' . $lng->txt('copy_of_suffix'))) == ' ' . $lng->txt('copy_of_suffix')) { // this is going to be the second copy of the filename $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen(' ' . $lng->txt('copy_of_suffix'))); if ($nth_copy == null) { $nth_copy = 2; } } else { // this is going to be the first copy of the filename $filenameWithoutCopy = $filenameWithoutExtension; if ($nth_copy == null) { $nth_copy = 1; } } } // Construct the new filename if ($nth_copy > 1) { // this is at least the second copy of the filename, append " - Copy ($nth_copy)" $newFilename = $filenameWithoutCopy . sprintf(' ' . $lng->txt('copy_n_of_suffix'), $nth_copy) . $extension; } else { // this is the first copy of the filename, append " - Copy" $newFilename = $filenameWithoutCopy . ' ' . $lng->txt('copy_of_suffix') . $extension; } return $newFilename; }
/** * 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; }
/** * Check if the file extension does still exist after an update of the title * @return */ public function checkFileExtension($new_filename, $new_title) { include_once './Modules/File/classes/class.ilObjFileAccess.php'; $fileExtension = ilObjFileAccess::_getFileExtension($new_filename); $titleExtension = ilObjFileAccess::_getFileExtension($new_title); if ($titleExtension != $fileExtension && strlen($fileExtension) > 0) { // remove old extension $pi = pathinfo($this->getFileName()); $suffix = $pi["extension"]; if ($suffix != "") { if (substr($new_title, strlen($new_title) - strlen($suffix) - 1) == "." . $suffix) { $new_title = substr($new_title, 0, strlen($new_title) - strlen($suffix) - 1); } } $new_title .= '.' . $fileExtension; } return $new_title; }
/** * save object * * @access public */ function save() { global $objDefinition, $ilUser; if (!$this->checkPermissionBool("create", "", "file")) { $this->ilErr->raiseError($this->lng->txt("permission_denied"), $this->ilErr->MESSAGE); } $single_form_gui = $this->initSingleUploadForm(); if ($single_form_gui->checkInput()) { $title = $single_form_gui->getInput("title"); $description = $single_form_gui->getInput("description"); $upload_file = $single_form_gui->getInput("upload_file"); if (trim($title) == "") { $title = $upload_file["name"]; } else { // BEGIN WebDAV: Ensure that object title ends with the filename extension $fileExtension = ilObjFileAccess::_getFileExtension($upload_file["name"]); $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($upload_file["name"]); //$fileObj->setFileType($upload_file["type"]); include_once "./Services/Utilities/classes/class.ilMimeTypeUtil.php"; $fileObj->setFileType(ilMimeTypeUtil::getMimeType("", $upload_file["name"], $upload_file["type"])); $fileObj->setFileSize($upload_file["size"]); $this->object_id = $fileObj->create(); $this->putObjectInTree($fileObj, $this->parent_id); // upload file to filesystem $fileObj->createDirectory(); $fileObj->getUploadFile($upload_file["tmp_name"], $upload_file["name"]); // 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. ilUtil::sendSuccess($this->lng->txt("file_added"), true); if ($this->ctrl->getCmd() == "saveAndMeta") { $this->ctrl->setParameter($this, "new_type", ""); $target = $this->ctrl->getLinkTargetByClass(array("ilobjfilegui", "ilmdeditorgui"), "listSection", "", false, false); ilUtil::redirect($target); } else { $this->ctrl->returnToParent($this); } } else { $single_form_gui->setValuesByPost(); $this->tpl->setContent($single_form_gui->getHTML()); } }
/** * Get item properties * * @return array array of property arrays: * "alert" (boolean) => display as an alert property (usually in red) * "property" (string) => property name * "value" (string) => property value */ function getProperties() { global $lng, $ilUser; // BEGIN WebDAV: Get parent properties $props = parent::getProperties(); // END WebDAV: Get parent properties // to do: implement extra smaller file info object include_once "./Modules/File/classes/class.ilObjFileAccess.php"; // Display a warning if a file is not a hidden Unix file, and // the filename extension is missing if (!preg_match('/^\\.|\\.[a-zA-Z0-9]+$/', $this->title)) { $props[] = array("alert" => false, "property" => $lng->txt("filename_interoperability"), "value" => $lng->txt("filename_extension_missing"), 'propertyNameVisible' => false); } // BEGIN WebDAV: Only display relevant information. $props[] = array("alert" => false, "property" => $lng->txt("type"), "value" => ilObjFileAccess::_getFileExtension($this->title), 'propertyNameVisible' => false); $fileData = ilObjFileAccess::_lookupFileData($this->obj_id); $props[] = array("alert" => false, "property" => $lng->txt("size"), "value" => ilFormat::formatSize($fileData['file_size'], 'short'), 'propertyNameVisible' => false); $version = $fileData['version']; if ($version > 1) { $props[] = array("alert" => false, "property" => $lng->txt("version"), "value" => $version); } $props[] = array("alert" => false, "property" => $lng->txt("last_update"), "value" => ilObject::_lookupLastUpdate($this->obj_id, true), 'propertyNameVisible' => false); // END WebDAV: Only display relevant information. return $props; }
/** * Get item properties * * @return array array of property arrays: * "alert" (boolean) => display as an alert property (usually in red) * "property" (string) => property name * "value" (string) => property value */ function getProperties() { global $lng, $ilUser; // BEGIN WebDAV: Get parent properties $props = parent::getProperties(); // END WebDAV: Get parent properties // to do: implement extra smaller file info object include_once "./Modules/File/classes/class.ilObjFileAccess.php"; // Display a warning if a file is not a hidden Unix file, and // the filename extension is missing if (!preg_match('/^\\.|\\.[a-zA-Z0-9]+$/', $this->title)) { $props[] = array("alert" => false, "property" => $lng->txt("filename_interoperability"), "value" => $lng->txt("filename_extension_missing"), 'propertyNameVisible' => false); } // BEGIN WebDAV: Only display relevant information. $props[] = array("alert" => false, "property" => $lng->txt("type"), "value" => ilObjFileAccess::_getFileExtension($this->title), 'propertyNameVisible' => false); $fileData = ilObjFileAccess::getListGUIData($this->obj_id); if (is_array($fileData)) { $props[] = array("alert" => false, "property" => $lng->txt("size"), "value" => ilFormat::formatSize($fileData['size'], 'short'), 'propertyNameVisible' => false); $version = $fileData['version']; if ($version > 1) { // add versions link if (parent::checkCommandAccess("write", "versions", $this->ref_id, $this->type)) { $link = $this->getCommandLink("versions"); $value = "<a href=\"{$link}\">" . $lng->txt("version") . ": {$version}</a>"; } else { $value = $lng->txt("version") . ": {$version}"; } $props[] = array("alert" => false, "property" => $lng->txt("version"), "value" => $value, "propertyNameVisible" => false); } // #6040 if ($fileData["date"]) { $props[] = array("alert" => false, "property" => $lng->txt("last_update"), "value" => ilDatePresentation::formatDate(new ilDateTime($fileData["date"], IL_CAL_DATETIME)), 'propertyNameVisible' => false); } } // END WebDAV: Only display relevant information. return $props; }