ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilFileXMLWriter.php
Go to the documentation of this file.
1<?php
2
34{
35 public static int $CONTENT_ATTACH_NO = 0;
36 public static int $CONTENT_ATTACH_ENCODED = 1;
37 public static int $CONTENT_ATTACH_ZLIB_ENCODED = 2;
38 public static int $CONTENT_ATTACH_GZIP_ENCODED = 3;
39 public static int $CONTENT_ATTACH_COPY = 4;
40 // begin-patch fm
41 public static int $CONTENT_ATTACH_REST = 5;
42 // end-patch fm
43
48 public \ilObjFile $file;
52 public $omit_header = false;
53 protected ?string $target_dir_relative = null;
54 protected ?string $target_dir_absolute = null;
55
65 public function __construct()
66 {
68 $this->attachFileContents = ilFileXMLWriter::$CONTENT_ATTACH_NO;
69 }
70
71 public function setFile(ilObjFile $file): void
72 {
73 $this->file = &$file;
74 }
75
81 public function setOmitHeader($a_val): void
82 {
83 $this->omit_header = $a_val;
84 }
85
91 public function getOmitHeader(): bool
92 {
93 return $this->omit_header;
94 }
95
102 public function setFileTargetDirectories(?string $a_rel, ?string $a_abs): void
103 {
104 $this->target_dir_relative = $a_rel;
105 $this->target_dir_absolute = $a_abs;
106 }
107
115 {
116 if ($attachFileContents === ilFileXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED && !function_exists("gzencode")) {
117 throw new ilFileException(
118 "Inflating with gzip is not supported",
120 );
121 }
122 if ($attachFileContents === ilFileXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED && !function_exists("gzcompress")) {
123 throw new ilFileException(
124 "Inflating with zlib (compress/uncompress) is not supported",
126 );
127 }
128 $this->attachFileContents = $attachFileContents;
129 }
130
131 public function start(): bool
132 {
133 $this->__buildHeader();
134
135 $attribs = [
136 "obj_id" => "il_" . IL_INST_ID . "_file_" . $this->file->getId(),
137 "version" => $this->file->getVersion(),
138 "max_version" => $this->file->getMaxVersion(),
139 "size" => $this->file->getFileSize(),
140 "type" => $this->file->getFileType(),
141 "action" => $this->file->getAction()
142 ];
143
144 $this->xmlStartTag("File", $attribs);
145 $this->xmlElement("Filename", null, $this->file->getFileName());
146
147 $this->xmlElement("Title", null, $this->file->getTitle());
148 $this->xmlElement("Description", null, $this->file->getDescription());
149 $this->xmlElement("Rating", null, (int) $this->file->hasRating());
150
151 $versions = $this->file->getVersions();
152
153 if ($versions !== []) {
154 $this->xmlStartTag("Versions");
155
156 foreach ($versions as $version) {
157 $attribs = [
158 "version" => $version["version"],
159 "max_version" => $version["max_version"],
160 "date" => strtotime((string) $version["date"]),
161 "usr_id" => "il_" . IL_INST_ID . "_usr_" . $version["user_id"],
162 "action" => $version["action"],
163 "rollback_version" => $version["rollback_version"],
164 "rollback_user_id" => $version["rollback_user_id"]
165 ];
166
167 $content = "";
168
169 if ($this->attachFileContents !== 0) {
170 $filename = $this->file->getFile($version["version"]);
171
172 if (@is_file($filename)) {
173 if ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_COPY) {
174 $attribs ["mode"] = "COPY";
175 $content = "/" . $version["version"] . "_" . $this->file->getFileName();
176 copy($filename, $this->target_dir_absolute . $content);
177 $content = $this->target_dir_relative . $content;
178 } // begin-patch fm
179 elseif ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_REST) {
180 $attribs ['mode'] = "REST";
181 $fs = new ilRestFileStorage();
182 $content = $fs->storeFileForRest(base64_encode(@file_get_contents($filename)));
183 ;
184 } // end-patch fm
185 else {
186 $content = @file_get_contents($filename);
187 $attribs ["mode"] = "PLAIN";
188 if ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED) {
189 $attribs ["mode"] = "ZLIB";
190 $content = @gzcompress($content, 9);
191 } elseif ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED) {
192 $attribs ["mode"] = "GZIP";
193 $content = @gzencode($content, 9);
194 }
195 $content = base64_encode($content);
196 }
197 }
198 }
199
200 $this->xmlElement("Version", $attribs, $content);
201 }
202 $this->xmlEndTag("Versions");
203 }
204
205 $this->xmlEndTag("File");
206
207 $this->__buildFooter();
208
209 return true;
210 }
211
212 public function getXML(): string
213 {
214 return $this->xmlDumpMem(false);
215 }
216
217 public function __buildHeader(): bool
218 {
219 if (!$this->getOmitHeader()) {
220 $this->xmlSetDtdDef(
221 "<!DOCTYPE File PUBLIC \"-//ILIAS//DTD FileAdministration//EN\" \"" . ILIAS_HTTP_PATH . "/components/ILIAS/Export/xml/ilias_file_3_8.dtd\">"
222 );
223 $this->xmlSetGenCmt("Exercise Object");
224 $this->xmlHeader();
225 }
226
227 return true;
228 }
229
230 public function __buildFooter(): void
231 {
232 }
233}
$filename
Definition: buildRTE.php:78
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static int $ID_DEFLATE_METHOD_MISMATCH
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static int $CONTENT_ATTACH_COPY
setAttachFileContents(int $attachFileContents)
set attachment content mode
setFileTargetDirectories(?string $a_rel, ?string $a_abs)
Set file target directories.
static int $CONTENT_ATTACH_ENCODED
static int $CONTENT_ATTACH_REST
setOmitHeader($a_val)
Set omit header.
static int $CONTENT_ATTACH_GZIP_ENCODED
ilObjFile $file
Exercise Object.
static int $CONTENT_ATTACH_ZLIB_ENCODED
getOmitHeader()
Get omit header.
setFile(ilObjFile $file)
Class ilObjFile.
File storage handling.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlSetGenCmt(string $genCmt)
Sets generated comment.
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlHeader()
Writes xml header.
xmlEndTag(string $tag)
Writes an endtag.
xmlSetDtdDef(string $dtdDef)
Sets dtd definition.
xmlDumpMem(bool $format=true)
Returns xml document from memory.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
const IL_INST_ID
Definition: constants.php:40
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc