ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFileXMLParser.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 
17 include_once './Services/Xml/classes/class.ilSaxParser.php';
18 include_once 'Modules/File/classes/class.ilFileException.php';
19 include_once 'Services/Utilities/classes/class.ilFileUtils.php';
20 
21 
23 {
27  static $CONTENT_COPY = 4;
28  // begin-patch fm
29  static $CONTENT_REST = 5;
30  // end-patch fm
31 
37  var $file;
38 
45  var $obj_id;
46 
47 
53  var $result;
54 
60  var $mode;
61 
62 
68  //var $content;
69 
76 
77 
86  function ilFileXMLParser(& $file, $a_xml_data, $obj_id = -1, $mode = 0)
87  {
89  $this->file = $file;
90  $this->setXMLContent($a_xml_data);
91  $this->obj_id = $obj_id;
92  $this->result = false;
93  $this->mode = $mode;
94  }
95 
101  function setImportDirectory($a_val)
102  {
103  $this->importDirectory = $a_val;
104  }
105 
112  {
113  return $this->importDirectory;
114  }
121  function setHandlers($a_xml_parser)
122  {
123  xml_set_object($a_xml_parser,$this);
124  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
125  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
126  }
127 
137  function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
138  {
139  global $ilErr;
140 
141  global $ilLog;
142 
143  switch($a_name)
144  {
145  case 'File':
146  if (isset($a_attribs["obj_id"]))
147  {
148  $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
149  if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id)
150  {
151  throw new ilFileException ("Object IDs (xml $read_obj_id and argument ".$this->obj_id.") do not match!", ilFileException::$ID_MISMATCH);
152  }
153  }
154  if (isset($a_attribs["type"]))
155  {
156  $this->file->setFileType($a_attribs["type"]);
157  }
158  $this->file->setVersion($this->file->getVersion() + 1);
159  break;
160  case 'Content':
161  $this->tmpFilename = ilUtil::ilTempnam();
163  $this->isReadingFile = true;
164 #echo $a_attribs["mode"];
165  if (isset($a_attribs["mode"]))
166  {
167  if($a_attribs["mode"] == "GZIP")
168  {
169  if (!function_exists("gzread"))
170  throw new ilFileException ("Deflating with gzip is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
171 
173  }
174  elseif ($a_attribs["mode"] == "ZLIB")
175  {
176  if (!function_exists("gzuncompress"))
177  throw new ilFileException ("Deflating with zlib (compress/uncompress) is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
178 
180  }
181  elseif ($a_attribs["mode"] == "COPY")
182  {
183  $this->mode = ilFileXMLParser::$CONTENT_COPY;
184  }
185  // begin-patch fm
186  elseif($a_attribs['mode'] == 'REST')
187  {
188  $this->mode = ilFileXMLParser::$CONTENT_REST;
189  }
190  // end-patch fm
191  }
192  }
193  }
194 
195 
196 
203  function handlerEndTag($a_xml_parser,$a_name)
204  {
205  $this->cdata = trim($this->cdata);
206 
207  $GLOBALS['ilLog']->write(__METHOD__.': '.$this->cdata);
208 
209  switch($a_name)
210  {
211  case 'File':
212  $this->result = true;
213  break;
214  case 'Filename':
215  if (strlen($this->cdata) == 0)
216  throw new ilFileException("Filename ist missing!");
217 
218  $this->file->setFilename($this->cdata);
219  $this->file->setTitle($this->cdata);
220 
221  break;
222  case 'Title':
223  $this->file->setTitle(trim($this->cdata));
224  break;
225  case 'Description':
226  $this->file->setDescription(trim($this->cdata));
227  break;
228  case 'Content':
229  $GLOBALS['ilLog']->write($this->mode);
230  $this->isReadingFile = false;
231  $baseDecodedFilename = ilUtil::ilTempnam();
232  if ($this->mode == ilFileXMLParser::$CONTENT_COPY)
233  {
234  $this->tmpFilename = $this->getImportDirectory()."/".$this->cdata;
235  }
236  // begin-patch fm
237  elseif($this->mode == ilFileXMLParser::$CONTENT_REST)
238  {
239  include_once './Services/WebServices/Rest/classes/class.ilRestFileStorage.php';
240  $storage = new ilRestFileStorage();
241  $this->tmpFilename = $storage->getStoredFilePath($this->cdata);
242  if(!ilFileUtils::fastBase64Decode($this->tmpFilename, $baseDecodedFilename))
243  {
244  throw new ilFileException("Base64-Decoding failed", ilFileException::$DECOMPRESSION_FAILED);
245  }
246  $this->tmpFilename = $baseDecodedFilename;
247  }
248  // end-patch fm
249  else
250  {
251  if (!ilFileUtils::fastBase64Decode($this->tmpFilename, $baseDecodedFilename))
252  {
253  throw new ilFileException ("Base64-Decoding failed", ilFileException::$DECOMPRESSION_FAILED);
254  }
255  if ($this->mode == ilFileXMLParser::$CONTENT_GZ_COMPRESSED)
256  {
257  if (!ilFileUtils::fastGunzip ($baseDecodedFilename, $this->tmpFilename))
258  {
259  throw new ilFileException ("Deflating with fastzunzip failed", ilFileException::$DECOMPRESSION_FAILED);
260  }
261  unlink ($baseDecodedFilename);
262  }
263  elseif ($this->mode == ilFileXMLParser::$CONTENT_ZLIB_COMPRESSED)
264  {
265  if (!ilFileUtils::fastGunzip ($baseDecodedFilename, $this->tmpFilename))
266  {
267  throw new ilFileException ("Deflating with fastDecompress failed", ilFileException::$DECOMPRESSION_FAILED);
268  }
269  unlink ($baseDecodedFilename);
270  }
271  else
272  {
273  $this->tmpFilename = $baseDecodedFilename;
274  }
275  }
276  //$this->content = $content;
277  $this->file->setFileSize(filesize($this->tmpFilename)); // strlen($this->content));
278 
279  // if no file type is given => lookup mime type
280  if(!$this->file->getFileType())
281  {
282  global $ilLog;
283 
284  #$ilLog->write(__METHOD__.': Trying to detect mime type...');
285  include_once('./Services/Utilities/classes/class.ilFileUtils.php');
286  $this->file->setFileType(ilFileUtils::_lookupMimeType($this->tmpFilename));
287  }
288 
289  break;
290  }
291 
292  $this->cdata = '';
293 
294  return;
295  }
296 
303  function handlerCharacterData($a_xml_parser,$a_data)
304  {
305  if($a_data != "\n")
306  {
307  // begin-patch fm
308  if ($this->isReadingFile && $this->mode != ilFileXMLParser::$CONTENT_COPY && $this->mode != ilFileXMLParser::$CONTENT_REST)
309  // begin-patch fm
310  {
311  $handle = fopen($this->tmpFilename, "a");
312  fwrite ($handle, $a_data);
313  fclose ($handle);
314  } else
315  $this->cdata .= $a_data;
316  }
317  }
318 
324  public function setFileContents ()
325  {
326  global $ilLog;
327 
328  #$ilLog->write(__METHOD__.' '.filesize($this->tmpFilename));
329 
330  if (filesize ($this->tmpFilename) == 0) {
331  return;
332  }
333 
334  $filedir = $this->file->getDirectory($this->file->getVersion());
335  #$ilLog->write(__METHOD__.' '.$filedir);
336 
337  if (!is_dir($filedir))
338  {
339  $this->file->createDirectory();
340  ilUtil::makeDir($filedir);
341  }
342 
343  $filename = $filedir."/".$this->file->getFileName();
344  if (file_exists($filename))
345  unlink($filename);
346 //echo "-".$this->tmpFilename."-".$filename."-"; exit;
347  return rename($this->tmpFilename, $filename);
348  // @file_put_contents($filename, $this->content);
349  }
350 
351 
357  public function updateFileContents ()
358  {
359  if ($this->setFileContents())
360  {
361  require_once("./Services/History/classes/class.ilHistory.php");
362  ilHistory::_createEntry($this->file->getId(), "replace", $this->file->getFilename().",".$this->file->getVersion());
363  $this->file->addNewsNotification("file_updated");
364  }
365  }
366 
374  public function start () {
375  $this->startParsing();
376  return $this->result > 0;
377  }
378 
379 }
380 ?>