ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjFileImplementationLegacy.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
5
11{
15 protected $obj_id;
16
17 protected $version;
25 protected $file_name;
26
33 public function __construct(int $obj_id, int $version, string $file_name)
34 {
35 $this->obj_id = $obj_id;
36 $this->version = $version;
37 $this->file_name = $file_name;
38 $this->file_storage = new ilFSStorageFile($obj_id);
39 $this->file_storage->create();
40 }
41
45 public function getDirectory($a_version = 0)
46 {
47 $version_subdir = "";
48
49 if ($a_version) {
50 $version_subdir = sprintf("%03d", $a_version);
51 }
52
53 return $this->file_storage->getAbsolutePath() . '/' . $version_subdir;
54 }
55
56 public function handleChangedObjectTitle(string $new_title)
57 {
58 // noting to do here
59 }
60
64 public function createDirectory()
65 {
67 }
68
72 public function clearDataDirectory()
73 {
75 $this->createDirectory();
76 }
77
81 public function deleteVersions($a_hist_entry_ids = null)
82 {
83 if ($a_hist_entry_ids == null || count($a_hist_entry_ids) < 1) {
84 $this->clearDataDirectory();
85
87 } else {
88 $actualVersionDeleted = false;
89
90 // get all versions
91 $versions = $this->getVersions();
92
93 // delete each version
94 foreach ($a_hist_entry_ids as $hist_id) {
95 $entry = null;
96
97 // get version
98 foreach ($versions as $index => $version) {
99 if ($version["hist_entry_id"] == $hist_id) {
100 // remove each history entry
102
103 // delete directory
104 $version_dir = $this->getDirectory($version["version"]);
105 ilUtil::delDir($version_dir);
106
107 // is actual version?
108 if ($version["version"] == $this->getVersion()) {
109 $actualVersionDeleted = true;
110 }
111
112 // remove from array
113 unset($versions[$index]);
114 break;
115 }
116 }
117 }
118
119 // update actual version if it was deleted before
120 if ($actualVersionDeleted) {
121 // get newest version (already sorted by getVersions)
122 $version = reset($versions);
123 $version['max_version'] = $this->getMaxVersion();
124 $this->updateWithVersion($version);
125 }
126 }
127 }
128
132 public function sendFile($a_hist_entry_id = null)
133 {
134 if (is_null($a_hist_entry_id)) {
135 $file = $this->getFile();
136 $file = ilFileUtils::getValidFilename($file);
137 } else {
138 $entry = ilHistory::_getEntryByHistoryID($a_hist_entry_id);
140 $file = $this->getDirectory($data["version"]) . "/" . $data["filename"];
141 }
142 global $DIC;
143 if ($this->file_storage->fileExists($file)) {
144 $ilClientIniFile = $DIC['ilClientIniFile'];
149 $ilFileDelivery = new \ilFileDelivery($file);
150 $ilFileDelivery->setDisposition($this->isInline() ? ilFileDelivery::DISP_INLINE : ilFileDelivery::DISP_ATTACHMENT);
151 $ilFileDelivery->setConvertFileNameToAsci((bool) !$ilClientIniFile->readVariable(
152 'file_access',
153 'disable_ascii'
154 ));
155
156 // also returning the 'real' filename if a history file is delivered
157 if ($ilClientIniFile->readVariable('file_access', 'download_with_uploaded_filename')
158 != '1'
159 && is_null($a_hist_entry_id)
160 ) {
161 $ilFileDelivery->setDownloadFileName(ilFileUtils::getValidFilename($this->getFileName()));
162 } else {
163 // $download_file_name = basename($file);
164 /* FSX Info: basename has a Bug with Japanese and other characters, see:
165 * http://stackoverflow.com/questions/32115609/basename-fail-when-file-name-start-by-an-accent
166 * Therefore we can no longer use basename();
167 */
168 $parts = explode(DIRECTORY_SEPARATOR, $file);
169 $download_file_name = end($parts);
170 $download_file_name = ilFileUtils::getValidFilename($download_file_name);
171 $ilFileDelivery->setDownloadFileName($download_file_name);
172 }
173 $ilFileDelivery->deliver();
174 }
175
176 throw new FileNotFoundException($DIC->language()->txt('file_not_found_sec'));
177 }
178
182 public function getFileExtension()
183 {
184 return ilObjFileAccess::_getFileExtension($this->getTitle());
185 }
186
195 protected function compareVersions($v1, $v2)
196 {
197 // v2 - v1 because version should be descending
198 return (int) $v2["version"] - (int) $v1["version"];
199 }
200
201 public function export(string $target_dir) : void
202 {
203 $subdir = "il_" . IL_INST_ID . "_file_" . $this->getId();
204 ilUtil::makeDir($target_dir . "/objects/" . $subdir);
205
206 $filedir = $this->getDirectory($this->getVersion()); // FSX
207
208 if (@!is_dir($filedir)) {
209 $filedir = $this->getDirectory();
210 }
211
212 ilUtil::rCopy($filedir, $target_dir . "/objects/" . $subdir);
213 }
214
237 public function getVersions($version_ids = null) : array
238 {
239 $versions = (array) ilHistory::_getEntriesForObject($this->obj_id, 'file');
240
241 if ($version_ids != null && count($version_ids) > 0) {
242 foreach ($versions as $index => $version) {
243 if (!in_array($version["hist_entry_id"], $version_ids, true)) {
244 unset($versions[$index]);
245 }
246 }
247 }
248
249 // add custom entries
250 foreach ($versions as $index => $version) {
252 $versions[$index] = array_merge($version, $params);
253 }
254
255 // sort by version number (hist_entry_id will do for that)
256 usort($versions, array($this, "compareVersions"));
257
258 $version_objects = [];
259 foreach ($versions as $version) {
260 $file = $this->getDirectory($version["version"]) . "/" . $version["filename"];
261 $filesize = @filesize($file);
262 $version['size'] = $filesize;
263 $version_objects[] = new ilObjFileVersion($version);
264 }
265
266 return $version_objects;
267 }
268
275 public static function parseInfoParams($entry) : array
276 {
277 $data = explode(",", $entry["info_params"]);
278
279 // bugfix: first created file had no version number
280 // this is a workaround for all files created before the bug was fixed
281 if (empty($data[1])) {
282 $data[1] = "1";
283 }
284
285 if (empty($data[2])) {
286 $data[2] = "1";
287 }
288
289 // BEGIN bugfix #31730
290 // if more than 2 commas are detected, the need for reassembling the filename is: possible to necessary
291 if (sizeof($data) > 2) {
292 $last = sizeof($data) - 1;
293 for ($n = 1; $n < $last - 1; $n++) {
294 $data[0] .= "," . $data[$n];
295 }
296
297 // trying to distinguish the next-to-last being a 'last part of the filename'
298 // or a 'version information', based on having a dot included or not
299 if (strpos($data[$last - 1], ".") !== false) {
300 $data[0] .= "," . $data[$last - 1];
301 $data[1] = $data[$last];
302 $data[2] = $data[$last];
303 } else {
304 $data[1] = $data[$last - 1];
305 $data[2] = $data[$last];
306 }
307 }
308 // END bugfix #31730
309
310 $result = array(
311 "filename" => $data[0],
312 "version" => $data[1],
313 "max_version" => $data[2],
314 "rollback_version" => "",
315 "rollback_user_id" => "",
316 );
317
318 // if rollback, the version contains the rollback version as well
319 if ($entry["action"] == "rollback") {
320 $tokens = explode("|", $result["max_version"]);
321 if (count($tokens) > 1) {
322 $result["max_version"] = $tokens[0];
323 $result["rollback_version"] = $tokens[1];
324
325 if (count($tokens) > 2) {
326 $result["rollback_user_id"] = $tokens[2];
327 }
328 }
329 }
330
331 return $result;
332 }
333
337 public function getSpecificVersion($version_id)
338 {
339 include_once("./Services/History/classes/class.ilHistory.php");
341 if ($version === false) {
342 return false;
343 }
344
345 // ilHistory returns different keys in _getEntryByHistoryID and _getEntriesForObject
346 // so this makes it the same
347 $version["hist_entry_id"] = $version["id"];
348 $version["user_id"] = $version["usr_id"];
349 $version["date"] = $version["hdate"];
350 unset($version["id"], $version["usr_id"], $version["hdate"]);
351
352 // parse params
354
355 return array_merge($version, $params);
356 }
357
361 public function getFile($a_hist_entry_id = null)
362 {
363 if (is_null($a_hist_entry_id)) {
364 $file = $this->getDirectory($this->getVersion()) . "/" . $this->getFileName(); // FSX
365 } else {
366 require_once("./Services/History/classes/class.ilHistory.php");
367 $entry = ilHistory::_getEntryByHistoryID($a_hist_entry_id);
368
369 if ($entry === false) {
370 return false;
371 }
372
374 $file = $this->getDirectory($data["version"]) . "/" . $data["filename"];
375 }
376
377 return $file;
378 }
379
380 public function getVersion()
381 {
382 return $this->version;
383 }
384
385 private function getFileName()
386 {
387 return $this->file_name;
388 }
389
394 private function isInline()
395 {
397 }
398
402 public function getFileType()
403 {
404 return '';
405 }
406
407 public function getStorageID() : ?string
408 {
409 return '-';
410 }
411}
$result
$n
Definition: RandomTest.php:85
An exception for terminatinating execution or to throw for unit testing.
Class FileNotFoundException Indicates that a file is missing or not found.
Class ilFSStorageFile.
static getValidFilename($a_filename)
Get valid filename.
static _removeEntryByHistoryID($a_hist_entry_id)
Removes a single entry from the history.
static _removeEntriesForObject($a_obj_id)
remove all history entries for an object
static _getEntriesForObject($a_obj_id, $a_obj_type="")
get all history entries for an object
static _getEntryByHistoryID($a_hist_entry_id)
returns a single history entry
static _getFileExtension($a_file_name)
Gets the file extension of the specified file name.
static _isFileInline($a_file_name)
Returns true, if the specified file shall be displayed inline in the browser.
Class ilObjFileImplementationAbstract.
Class ilObjFileImplementationLegacy.
compareVersions($v1, $v2)
Compares two file versions.
static parseInfoParams($entry)
Parses the info parameters ("info_params") of the specified history entry.
deleteVersions($a_hist_entry_ids=null)
@inheritDoc
__construct(int $obj_id, int $version, string $file_name)
ilObjFileImplementationAbstract constructor.
getVersions($version_ids=null)
@inheritDoc array ( 0 => array ( 'date' => '2020-11-05 09:49:18', 'user_id' => '6',...
Class ilObjFileVersion.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static rCopy($a_sdir, $a_tdir, $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
static makeDirParents($a_dir)
Create a new directory and all parent directories.
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
const IL_INST_ID
Definition: constants.php:38
global $DIC
Definition: goto.php:24
Interface ilObjFileImplementationInterface.
sendFile($a_hist_entry_id=null)
$index
Definition: metadata.php:128
$data
Definition: storeScorm.php:23