ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilObjFolder.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
26
27require_once "./Services/Container/classes/class.ilContainer.php";
28
38{
40 // bugfix mantis 24309: array for systematically numbering copied files
41 private static $duplicate_files = array();
42
43
52 public function __construct($a_id = 0, $a_call_by_reference = true)
53 {
54 global $DIC;
55
56 $this->tree = $DIC->repositoryTree();
57 $this->lng = $DIC->language();
58 $this->rbacsystem = $DIC->rbac()->system();
59 $this->access = $DIC->access();
60 $this->type = "fold";
61 parent::__construct($a_id, $a_call_by_reference);
62 $this->lng->loadLanguageModule('fold');
63 }
64
65
66 public function setFolderTree($a_tree)
67 {
68 $this->folder_tree =&$a_tree;
69 }
70
71
81 public function cloneObject($a_target_id, $a_copy_id = 0, $a_omit_tree = false)
82 {
83 $new_obj = parent::cloneObject($a_target_id, $a_copy_id, $a_omit_tree);
84
85 // Copy learning progress settings
86 include_once('Services/Tracking/classes/class.ilLPObjSettings.php');
87 $obj_settings = new ilLPObjSettings($this->getId());
88 $obj_settings->cloneSettings($new_obj->getId());
89 unset($obj_settings);
90
91 return $new_obj;
92 }
93
94
99 public function putInTree($a_parent)
100 {
102
103 if (!is_object($this->folder_tree)) {
104 $this->folder_tree =&$tree;
105 }
106
107 if ($this->withReferences()) {
108 // put reference id into tree
109 $this->folder_tree->insertNode($this->getRefId(), $a_parent);
110 } else {
111 // put object id into tree
112 $this->folder_tree->insertNode($this->getId(), $a_parent);
113 }
114 }
115
116
126 public function cloneDependencies($a_target_id, $a_copy_id)
127 {
128 parent::cloneDependencies($a_target_id, $a_copy_id);
129
130 include_once('Services/Object/classes/class.ilObjectActivation.php');
131 ilObjectActivation::cloneDependencies($this->getRefId(), $a_target_id, $a_copy_id);
132
133 return true;
134 }
135
136
151 private static function recurseFolder($ref_id, $title, $tmpdir)
152 {
153 global $DIC;
154
155 $tree = $DIC->repositoryTree();
156 $ilAccess = $DIC->access();
157
158 $tmpdir = $tmpdir . DIRECTORY_SEPARATOR . ilUtil::getASCIIFilename($title);
159
160 $temp_fs = $DIC->filesystem()->temp();
161 $temp_fs->createDir($tmpdir);
162
163 $subtree = $tree->getChildsByTypeFilter($ref_id, array("fold", "file"));
164
165 foreach ($subtree as $child) {
166 if (!$ilAccess->checkAccess("read", "", $child["ref_id"])) {
167 continue;
168 }
169 if (ilObject::_isInTrash($child["ref_id"])) {
170 continue;
171 }
172 if ($child["type"] == "fold") {
173 ilObjFolder::recurseFolder($child["ref_id"], $child["title"], $tmpdir);
174 } else {
175 // bugfix mantis 24309:
176 // changed the code of this else-statement for allowing to check for duplicates
177 // (identically named files) and renaming them to prevent an exception from occurring
178
179 // enhance new_filename to full relative path to enable checking for identically named files
180 $relative_path_tmp_dir = ilUtil::getDataDir() . "/temp/" . $tmpdir;
181 $new_filename = $relative_path_tmp_dir . DIRECTORY_SEPARATOR . ilUtil::getASCIIFilename($child["title"]);
182
183 // alter the filename if there are identically named files in the same folder
184 if (file_exists($new_filename)) {
185 $new_filename = self::renameDuplicateFile($new_filename);
186 }
187
188 // sanitize the new filename to enforce ILIAS' file extension whitelist
190 $new_filename = $sanitizer->sanitize($new_filename);
191
192 // copy to temporary directory
193 $old_filename = ilObjFile::_lookupAbsolutePath($child["obj_id"]);
194 if (!copy($old_filename, $new_filename)) {
195 throw new ilFileException("Could not copy " . $old_filename . " to " . $new_filename);
196 }
197 touch($new_filename, filectime($old_filename));
198 }
199 }
200 }
201
202
212 private static function renameDuplicateFile($duplicate_filename)
213 {
214 // determine the copy_number that will be added to the filename either by obtaining it from
215 // the entry of the current file in the duplicate_files-array or use 1 if there is no entry yet
216 $copy_number = 1;
217 $duplicate_has_array_entry = false;
218 foreach (self::$duplicate_files as &$duplicate_file) {
219 if ($duplicate_file['file_name'] == $duplicate_filename) {
220 $duplicate_has_array_entry = true;
221 $copy_number = $duplicate_file['copy_number'];
222 // increment the copy_number for correctly renaming the next duplicate of this file
223 $duplicate_file['copy_number']++;
224 }
225 }
226
227 // create an array entry for the duplicate file if there isn't one to ensure that the
228 // copy_number can be determined correctly for other duplicates of this file
229 if (!$duplicate_has_array_entry) {
230 self::$duplicate_files[] = [
231 'file_name' => $duplicate_filename,
232 'copy_number' => 2 // set as 2 because 1 is already used for this duplicate
233 ];
234 }
235
236 // rename the file
237 $path = pathinfo($duplicate_filename, PATHINFO_DIRNAME);
238 $filename = pathinfo($duplicate_filename, PATHINFO_FILENAME);
239 $extension = pathinfo($duplicate_filename, PATHINFO_EXTENSION);
240 $new_filename = $path . "/" . $filename . " (" . $copy_number . ")." . $extension;
241
242 return $new_filename;
243 }
244
245
246 public function downloadFolder()
247 {
248 $ilAccess = $this->access;
249
250 if (!$ilAccess->checkAccess("read", "", $this->getRefId())) {
251 $this->ilErr->raiseError(get_class($this) . "::downloadFolder(): missing read permission!", $this->ilErr->WARNING);
252 }
253 if (ilObject::_isInTrash($this->getRefId())) {
254 $this->ilErr->raiseError(get_class($this) . "::downloadFolder(): object is trashed!", $this->ilErr->WARNING);
255 }
256
257 $tmpdir = ilUtil::ilTempnam();
258 ilUtil::makeDir($tmpdir);
259 $basename = ilUtil::getAsciiFilename($this->getTitle());
260 $deliverFilename = $basename . ".zip";
261 $zipbasedir = $tmpdir . DIRECTORY_SEPARATOR . $basename;
262 $tmpzipfile = $tmpdir . DIRECTORY_SEPARATOR . $deliverFilename;
263
264 try {
265 ilObjFolder::recurseFolder($this->getRefId(), $this->getTitle(), LegacyPathHelper::createRelativePath($tmpdir));
266 ilUtil::zip($zipbasedir, $tmpzipfile);
267 rename($tmpzipfile, $zipfile = ilUtil::ilTempnam());
268 ilUtil::delDir($tmpdir);
269 ilUtil::deliverFile($zipfile, $deliverFilename, '', false, true);
270 } catch (ilFileException $e) {
271 ilUtil::sendInfo($e->getMessage(), true);
272 }
273 }
274
275
279 public function getViewMode()
280 {
282
283 // default: by type
285
286 // always inherit from
287 $container_ref_id = $tree->checkForParentType($this->ref_id, 'grp');
288 if (!$container_ref_id) {
289 $container_ref_id = $tree->checkForParentType($this->ref_id, 'crs');
290 }
291 if ($container_ref_id) {
292 include_once("./Modules/Course/classes/class.ilObjCourseAccess.php");
293 $view_mode = ilObjCourseAccess::_lookupViewMode(ilObject::_lookupObjId($container_ref_id));
294 if ($view_mode == ilContainer::VIEW_SESSIONS
295 || $view_mode == ilContainer::VIEW_BY_TYPE
296 || $view_mode == ilContainer::VIEW_SIMPLE
297 ) {
298 $view = $view_mode;
299 }
300 }
301
302 return $view;
303 }
304
305
310 public function addAdditionalSubItemInformation(&$a_item_data)
311 {
312 include_once './Services/Object/classes/class.ilObjectActivation.php';
314 }
315
316
326 public function read()
327 {
329
330 parent::read();
331
332 // Inherit order type from parent course (if exists)
333 include_once('./Services/Container/classes/class.ilContainerSortingSettings.php');
335 }
336} // END class.ilObjFolder
An exception for terminatinating execution or to throw for unit testing.
static _lookupSortMode($a_obj_id)
lookup sort mode
Class ilContainer.
setOrderType($a_value)
Class to report exception.
static _lookupViewMode($a_id)
Lookup view mode.
static _lookupAbsolutePath($obj_id, $a_version=null)
Class ilObjFolder.
static recurseFolder($ref_id, $title, $tmpdir)
private functions which iterates through all folders and files and create an according file structure...
getViewMode()
Get container view mode.
static $duplicate_files
__construct($a_id=0, $a_call_by_reference=true)
Constructor.
static renameDuplicateFile($duplicate_filename)
bugfix mantis 24309: add "_copy_" followed by a number to the filename (in front of the file-type-ext...
read()
Overwritten read method.
setFolderTree($a_tree)
putInTree($a_parent)
insert folder into grp_tree
addAdditionalSubItemInformation(&$a_item_data)
Add additional information to sub item, e.g.
cloneObject($a_target_id, $a_copy_id=0, $a_omit_tree=false)
Clone folder.
cloneDependencies($a_target_id, $a_copy_id)
Clone object dependencies (crs items, preconditions)
static cloneDependencies($a_ref_id, $a_target_id, $a_copy_id)
Clone dependencies.
static addAdditionalSubItemInformation(array &$a_item)
Parse item data for list entries.
static _lookupObjId($a_id)
withReferences()
determines wehter objects are referenced or not (got ref ids or not)
getRefId()
get reference id @access public
getId()
get object id @access public
static _isInTrash($a_ref_id)
checks wether object is in trash
getTitle()
get object title @access public
static getDataDir()
get data directory (outside webspace)
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
static deliverFile( $a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
global $DIC
Definition: saml.php:7
if(empty( $files_to_scan)) $sanitizer
Definition: svg-scanner.php:90