ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilFileXMLWriter.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once "./Services/Xml/classes/class.ilXmlWriter.php";
6 
22 {
23  public static $CONTENT_ATTACH_NO = 0;
24  public static $CONTENT_ATTACH_ENCODED = 1;
25  public static $CONTENT_ATTACH_ZLIB_ENCODED = 2;
26  public static $CONTENT_ATTACH_GZIP_ENCODED = 3;
27  public static $CONTENT_ATTACH_COPY = 4;
28  // begin-patch fm
29  public static $CONTENT_ATTACH_REST = 5;
30  // end-patch fm
42  public $file;
43  public $omit_header = false;
44 
45 
55  public function __construct()
56  {
57  parent::__construct();
58  $this->attachFileContents = ilFileXMLWriter::$CONTENT_ATTACH_NO;
59  }
60 
61 
62  public function setFile(ilObjFile $file)
63  {
64  $this->file = &$file;
65  }
66 
67 
73  public function setOmitHeader($a_val)
74  {
75  $this->omit_header = $a_val;
76  }
77 
78 
84  public function getOmitHeader()
85  {
86  return $this->omit_header;
87  }
88 
89 
96  public function setFileTargetDirectories($a_rel, $a_abs)
97  {
98  $this->target_dir_relative = $a_rel;
99  $this->target_dir_absolute = $a_abs;
100  }
101 
102 
111  {
112  if ($attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED && !function_exists("gzencode")) {
113  throw new ilFileException("Inflating with gzip is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
114  }
115  if ($attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED && !function_exists("gzcompress")) {
116  throw new ilFileException("Inflating with zlib (compress/uncompress) is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
117  }
118  $this->attachFileContents = $attachFileContents;
119  }
120 
121 
122  public function start()
123  {
124  $this->__buildHeader();
125 
126  $attribs = array(
127  "obj_id" => "il_" . IL_INST_ID . "_file_" . $this->file->getId(),
128  "version" => $this->file->getVersion(),
129  "max_version" => $this->file->getMaxVersion(),
130  "size" => $this->file->getFileSize(),
131  "type" => $this->file->getFileType(),
132  "action" => $this->file->getAction(),
133  );
134 
135  $this->xmlStartTag("File", $attribs);
136  $this->xmlElement("Filename", null, $this->file->getFileName());
137 
138  $this->xmlElement("Title", null, $this->file->getTitle());
139  $this->xmlElement("Description", null, $this->file->getDescription());
140  $this->xmlElement("Rating", null, (int) $this->file->hasRating());
141 
142  include_once("./Services/History/classes/class.ilHistory.php");
143 
144  $versions = $this->file->getVersions();
145 
146  if (count($versions)) {
147  $this->xmlStartTag("Versions");
148 
149  foreach ($versions as $version) {
150  $attribs = array(
151  "version" => $version["version"],
152  "max_version" => $version["max_version"],
153  "date" => ilUtil::date_mysql2time($version["date"]),
154  "usr_id" => "il_" . IL_INST_ID . "_usr_" . $version["user_id"],
155  "action" => $version["action"],
156  "rollback_version" => $version["rollback_version"],
157  "rollback_user_id" => $version["rollback_user_id"],
158  );
159 
160  $content = "";
161 
162  if ($this->attachFileContents) {
163  $filename = $this->file->getDirectory($version["version"]) . "/" . $this->file->getFileName();
164 
165  if (@is_file($filename)) {
166  if ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_COPY) {
167  $attribs ["mode"] = "COPY";
168  $content = "/" . $version["version"] . "_" . $this->file->getFileName();
169  copy($filename, $this->target_dir_absolute . $content);
170  $content = $this->target_dir_relative . $content;
171  } // begin-patch fm
172  elseif ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_REST) {
173  $attribs ['mode'] = "REST";
174  include_once './Services/WebServices/Rest/classes/class.ilRestFileStorage.php';
175  $fs = new ilRestFileStorage();
176  $content = $fs->storeFileForRest(base64_encode(@file_get_contents($filename)));
177  ;
178  } // end-patch fm
179  else {
180  $content = @file_get_contents($filename);
181  $attribs ["mode"] = "PLAIN";
182  if ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED) {
183  $attribs ["mode"] = "ZLIB";
184  $content = @gzcompress($content, 9);
185  } elseif ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED) {
186  $attribs ["mode"] = "GZIP";
187  $content = @gzencode($content, 9);
188  }
189  $content = base64_encode($content);
190  }
191  }
192  }
193 
194  $this->xmlElement("Version", $attribs, $content);
195  }
196  $this->xmlEndTag("Versions");
197  }
198 
199  $this->xmlEndTag("File");
200 
201  $this->__buildFooter();
202 
203  return true;
204  }
205 
206 
207  public function getXML()
208  {
209  return $this->xmlDumpMem(false);
210  }
211 
212 
213  public function __buildHeader()
214  {
215  if (!$this->getOmitHeader()) {
216  $this->xmlSetDtdDef("<!DOCTYPE File PUBLIC \"-//ILIAS//DTD FileAdministration//EN\" \"" . ILIAS_HTTP_PATH . "/xml/ilias_file_3_8.dtd\">");
217  $this->xmlSetGenCmt("Exercise Object");
218  $this->xmlHeader();
219  }
220 
221  return true;
222  }
223 
224 
225  public function __buildFooter()
226  {
227  }
228 }
xmlStartTag($tag, $attrs=null, $empty=false, $encode=true, $escape=true)
Writes a starttag.
xmlSetGenCmt($genCmt)
Sets generated comment.
setAttachFileContents($attachFileContents)
set attachment content mode
xmlSetDtdDef($dtdDef)
Sets dtd definition.
__construct()
constructor
xmlDumpMem($format=true)
Returns xml document from memory.
XML writer class.
setFile(ilObjFile $file)
setOmitHeader($a_val)
Set omit header.
getOmitHeader()
Get omit header.
xmlEndTag($tag)
Writes an endtag.
static date_mysql2time($mysql_date_time)
make time object from mysql_date_time
Class to report exception.
xmlHeader()
Writes xml header public.
$filename
Definition: buildRTE.php:89
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
XML writer class.
File storage handling.
setFileTargetDirectories($a_rel, $a_abs)
Set file target directories.