ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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 $this->xmlElement("ImportantInformation", null, (string) $this->file->getImportantInfo());
151 $this->xmlElement("OnClickMode", null, (int) $this->file->getOnClickMode());
152
153 $versions = $this->file->getVersions();
154
155 if ($versions !== []) {
156 $this->xmlStartTag("Versions");
157
158 foreach ($versions as $version) {
159 $attribs = [
160 "version" => $version["version"],
161 "max_version" => $version["max_version"],
162 "date" => strtotime((string) $version["date"]),
163 "usr_id" => "il_" . IL_INST_ID . "_usr_" . $version["user_id"],
164 "action" => $version["action"],
165 "rollback_version" => $version["rollback_version"],
166 "rollback_user_id" => $version["rollback_user_id"]
167 ];
168
169 $content = "";
170
171 if ($this->attachFileContents !== 0) {
172 $filename = $this->file->getFile($version["version"]);
173
174 if (@is_file($filename)) {
175 if ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_COPY) {
176 $attribs ["mode"] = "COPY";
177 $content = "/" . $version["version"] . "_" . $this->file->getFileName();
178 copy($filename, $this->target_dir_absolute . $content);
179 $content = $this->target_dir_relative . $content;
180 } // begin-patch fm
181 elseif ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_REST) {
182 $attribs ['mode'] = "REST";
183 $fs = new ilRestFileStorage();
184 $content = $fs->storeFileForRest(base64_encode(@file_get_contents($filename)));
185 ;
186 } // end-patch fm
187 else {
188 $content = @file_get_contents($filename);
189 $attribs ["mode"] = "PLAIN";
190 if ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED) {
191 $attribs ["mode"] = "ZLIB";
192 $content = @gzcompress($content, 9);
193 } elseif ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED) {
194 $attribs ["mode"] = "GZIP";
195 $content = @gzencode($content, 9);
196 }
197 $content = base64_encode($content);
198 }
199 }
200 }
201
202 $this->xmlElement("Version", $attribs, $content);
203 }
204 $this->xmlEndTag("Versions");
205 }
206
207 $this->xmlEndTag("File");
208
209 $this->__buildFooter();
210
211 return true;
212 }
213
214 public function getXML(): string
215 {
216 return $this->xmlDumpMem(false);
217 }
218
219 public function __buildHeader(): bool
220 {
221 if (!$this->getOmitHeader()) {
222 $this->xmlSetDtdDef(
223 "<!DOCTYPE File PUBLIC \"-//ILIAS//DTD FileAdministration//EN\" \"" . ILIAS_HTTP_PATH . "/components/ILIAS/Export/xml/ilias_file_3_8.dtd\">"
224 );
225 $this->xmlSetGenCmt("Exercise Object");
226 $this->xmlHeader();
227 }
228
229 return true;
230 }
231
232 public function __buildFooter(): void
233 {
234 }
235}
$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