ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFileXMLParser.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
36 include_once 'classes/class.ilSaxParser.php';
37 include_once 'Modules/File/classes/class.ilFileException.php';
38 include_once 'Services/Utilities/classes/class.ilFileUtils.php';
39 
40 
42 {
46 
52  var $file;
53 
60  var $obj_id;
61 
62 
68  var $result;
69 
75  var $mode;
76 
77 
83  //var $content;
84 
91 
92 
101  function ilFileXMLParser(& $file, $a_xml_data, $obj_id = -1, $mode = 0)
102  {
104  $this->file = $file;
105  $this->setXMLContent($a_xml_data);
106  $this->obj_id = $obj_id;
107  $this->result = false;
108  $this->mode = $mode;
109  }
110 
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  if ($a_attribs["mode"] == "GZIP")
164  {
165  if (!function_exists("gzread"))
166  throw new ilFileException ("Deflating with gzip is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
167 
169  } elseif ($a_attribs["mode"] == "ZLIB")
170  {
171  if (!function_exists("gzuncompress"))
172  throw new ilFileException ("Deflating with zlib (compress/uncompress) is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
173 
175  }
176 
177  }
178  }
179  }
180 
181 
182 
189  function handlerEndTag($a_xml_parser,$a_name)
190  {
191  $this->cdata = trim($this->cdata);
192  switch($a_name)
193  {
194  case 'File':
195  $this->result = true;
196  break;
197  case 'Filename':
198  if (strlen($this->cdata) == 0)
199  throw new ilFileException("Filename ist missing!");
200 
201  $this->file->setFilename($this->cdata);
202  $this->file->setTitle($this->cdata);
203 
204  break;
205  case 'Title':
206  $this->file->setTitle(trim($this->cdata));
207  break;
208  case 'Description':
209  $this->file->setDescription(trim($this->cdata));
210  break;
211  case 'Content':
212  $this->isReadingFile = false;
213  $baseDecodedFilename = ilUtil::ilTempnam();
214  if (!ilFileUtils::fastBase64Decode($this->tmpFilename, $baseDecodedFilename))
215  {
216  throw new ilFileException ("Base64-Decoding failed", ilFileException::$DECOMPRESSION_FAILED);
217  }
218  if ($this->mode == ilFileXMLParser::$CONTENT_GZ_COMPRESSED)
219  {
220  if (!ilFileUtils::fastGunzip ($baseDecodedFilename, $this->tmpFilename))
221  {
222  throw new ilFileException ("Deflating with fastzunzip failed", ilFileException::$DECOMPRESSION_FAILED);
223  }
224  unlink ($baseDecodedFilename);
225  }
226  elseif ($this->mode == ilFileXMLParser::$CONTENT_ZLIB_COMPRESSED)
227  {
228  if (!ilFileUtils::fastGunzip ($baseDecodedFilename, $this->tmpFilename))
229  {
230  throw new ilFileException ("Deflating with fastDecompress failed", ilFileException::$DECOMPRESSION_FAILED);
231  }
232  unlink ($baseDecodedFilename);
233  }
234  else
235  {
236  $this->tmpFilename = $baseDecodedFilename;
237  }
238  //$this->content = $content;
239  $this->file->setFileSize(filesize($this->tmpFilename)); // strlen($this->content));
240 
241  // if no file type is given => lookup mime type
242  if(!$this->file->getFileType())
243  {
244  global $ilLog;
245 
246  #$ilLog->write(__METHOD__.': Trying to detect mime type...');
247  include_once('./Services/Utilities/classes/class.ilFileUtils.php');
248  $this->file->setFileType(ilFileUtils::_lookupMimeType($this->tmpFilename));
249  }
250 
251  break;
252  }
253 
254  $this->cdata = '';
255 
256  return;
257  }
258 
265  function handlerCharacterData($a_xml_parser,$a_data)
266  {
267  if($a_data != "\n")
268  {
269  if ($this->isReadingFile)
270  {
271  $handle = fopen ($this->tmpFilename, "a");
272  fwrite ($handle, $a_data);
273  fclose ($handle);
274  } else
275  $this->cdata .= $a_data;
276  }
277  }
278 
284  public function setFileContents ()
285  {
286  global $ilLog;
287 
288  #$ilLog->write(__METHOD__.' '.filesize($this->tmpFilename));
289 
290  if (filesize ($this->tmpFilename) == 0) {
291  return;
292  }
293 
294  $filedir = $this->file->getDirectory($this->file->getVersion());
295  #$ilLog->write(__METHOD__.' '.$filedir);
296 
297  if (!is_dir($filedir))
298  {
299  $this->file->createDirectory();
300  ilUtil::makeDir($filedir);
301  }
302 
303  $filename = $filedir."/".$this->file->getFileName();
304  if (file_exists($filename))
305  unlink($filename);
306  return rename($this->tmpFilename, $filename);
307  // @file_put_contents($filename, $this->content);
308  }
309 
310 
316  public function updateFileContents ()
317  {
318  if ($this->setFileContents())
319  {
320  require_once("classes/class.ilHistory.php");
321  ilHistory::_createEntry($this->file->getId(), "replace", $this->file->getFilename().",".$this->file->getVersion());
322  $this->file->addNewsNotification("file_updated");
323  }
324  }
325 
333  public function start () {
334  $this->startParsing();
335  return $this->result > 0;
336  }
337 
338 }
339 ?>