ILIAS  eassessment Revision 61809
 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 '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 
34  var $file;
35 
42  var $obj_id;
43 
44 
50  var $result;
51 
57  var $mode;
58 
59 
65  //var $content;
66 
73 
74 
83  function ilFileXMLParser(& $file, $a_xml_data, $obj_id = -1, $mode = 0)
84  {
86  $this->file = $file;
87  $this->setXMLContent($a_xml_data);
88  $this->obj_id = $obj_id;
89  $this->result = false;
90  $this->mode = $mode;
91  }
92 
98  function setImportDirectory($a_val)
99  {
100  $this->importDirectory = $a_val;
101  }
102 
109  {
110  return $this->importDirectory;
111  }
118  function setHandlers($a_xml_parser)
119  {
120  xml_set_object($a_xml_parser,$this);
121  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
122  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
123  }
124 
134  function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
135  {
136  global $ilErr;
137 
138  global $ilLog;
139 
140  switch($a_name)
141  {
142  case 'File':
143  if (isset($a_attribs["obj_id"]))
144  {
145  $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
146  if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id)
147  {
148  throw new ilFileException ("Object IDs (xml $read_obj_id and argument ".$this->obj_id.") do not match!", ilFileException::$ID_MISMATCH);
149  }
150  }
151  if (isset($a_attribs["type"]))
152  {
153  $this->file->setFileType($a_attribs["type"]);
154  }
155  $this->file->setVersion($this->file->getVersion() + 1);
156  break;
157  case 'Content':
158  $this->tmpFilename = ilUtil::ilTempnam();
160  $this->isReadingFile = true;
161 #echo $a_attribs["mode"];
162  if (isset($a_attribs["mode"]))
163  {
164  if($a_attribs["mode"] == "GZIP")
165  {
166  if (!function_exists("gzread"))
167  throw new ilFileException ("Deflating with gzip is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
168 
170  }
171  elseif ($a_attribs["mode"] == "ZLIB")
172  {
173  if (!function_exists("gzuncompress"))
174  throw new ilFileException ("Deflating with zlib (compress/uncompress) is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
175 
177  }
178  elseif ($a_attribs["mode"] == "COPY")
179  {
180  $this->mode = ilFileXMLParser::$CONTENT_COPY;
181  }
182 
183  }
184  }
185  }
186 
187 
188 
195  function handlerEndTag($a_xml_parser,$a_name)
196  {
197  $this->cdata = trim($this->cdata);
198 
199  switch($a_name)
200  {
201  case 'File':
202  $this->result = true;
203  break;
204  case 'Filename':
205  if (strlen($this->cdata) == 0)
206  throw new ilFileException("Filename ist missing!");
207 
208  $this->file->setFilename($this->cdata);
209  $this->file->setTitle($this->cdata);
210 
211  break;
212  case 'Title':
213  $this->file->setTitle(trim($this->cdata));
214  break;
215  case 'Description':
216  $this->file->setDescription(trim($this->cdata));
217  break;
218  case 'Content':
219  $this->isReadingFile = false;
220  $baseDecodedFilename = ilUtil::ilTempnam();
221  if ($this->mode == ilFileXMLParser::$CONTENT_COPY)
222  {
223  $this->tmpFilename = $this->getImportDirectory()."/".$this->cdata;
224  }
225  else
226  {
227  if (!ilFileUtils::fastBase64Decode($this->tmpFilename, $baseDecodedFilename))
228  {
229  throw new ilFileException ("Base64-Decoding failed", ilFileException::$DECOMPRESSION_FAILED);
230  }
231  if ($this->mode == ilFileXMLParser::$CONTENT_GZ_COMPRESSED)
232  {
233  if (!ilFileUtils::fastGunzip ($baseDecodedFilename, $this->tmpFilename))
234  {
235  throw new ilFileException ("Deflating with fastzunzip failed", ilFileException::$DECOMPRESSION_FAILED);
236  }
237  unlink ($baseDecodedFilename);
238  }
239  elseif ($this->mode == ilFileXMLParser::$CONTENT_ZLIB_COMPRESSED)
240  {
241  if (!ilFileUtils::fastGunzip ($baseDecodedFilename, $this->tmpFilename))
242  {
243  throw new ilFileException ("Deflating with fastDecompress failed", ilFileException::$DECOMPRESSION_FAILED);
244  }
245  unlink ($baseDecodedFilename);
246  }
247  else
248  {
249  $this->tmpFilename = $baseDecodedFilename;
250  }
251  }
252  //$this->content = $content;
253  $this->file->setFileSize(filesize($this->tmpFilename)); // strlen($this->content));
254 
255  // if no file type is given => lookup mime type
256  if(!$this->file->getFileType())
257  {
258  global $ilLog;
259 
260  #$ilLog->write(__METHOD__.': Trying to detect mime type...');
261  include_once('./Services/Utilities/classes/class.ilFileUtils.php');
262  $this->file->setFileType(ilFileUtils::_lookupMimeType($this->tmpFilename));
263  }
264 
265  break;
266  }
267 
268  $this->cdata = '';
269 
270  return;
271  }
272 
279  function handlerCharacterData($a_xml_parser,$a_data)
280  {
281  if($a_data != "\n")
282  {
283  if ($this->isReadingFile && $this->mode != ilFileXMLParser::$CONTENT_COPY)
284  {
285  $handle = fopen ($this->tmpFilename, "a");
286  fwrite ($handle, $a_data);
287  fclose ($handle);
288  } else
289  $this->cdata .= $a_data;
290  }
291  }
292 
298  public function setFileContents ()
299  {
300  global $ilLog;
301 
302  #$ilLog->write(__METHOD__.' '.filesize($this->tmpFilename));
303 
304  if (filesize ($this->tmpFilename) == 0) {
305  return;
306  }
307 
308  $filedir = $this->file->getDirectory($this->file->getVersion());
309  #$ilLog->write(__METHOD__.' '.$filedir);
310 
311  if (!is_dir($filedir))
312  {
313  $this->file->createDirectory();
314  ilUtil::makeDir($filedir);
315  }
316 
317  $filename = $filedir."/".$this->file->getFileName();
318  if (file_exists($filename))
319  unlink($filename);
320 //echo "-".$this->tmpFilename."-".$filename."-"; exit;
321  return rename($this->tmpFilename, $filename);
322  // @file_put_contents($filename, $this->content);
323  }
324 
325 
331  public function updateFileContents ()
332  {
333  if ($this->setFileContents())
334  {
335  require_once("classes/class.ilHistory.php");
336  ilHistory::_createEntry($this->file->getId(), "replace", $this->file->getFilename().",".$this->file->getVersion());
337  $this->file->addNewsNotification("file_updated");
338  }
339  }
340 
348  public function start () {
349  $this->startParsing();
350  return $this->result > 0;
351  }
352 
353 }
354 ?>