• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Modules/File/classes/class.ilFileXMLParser.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 
00036 include_once 'classes/class.ilSaxParser.php';
00037 include_once 'Modules/File/classes/class.ilFileException.php';
00038 include_once 'Services/Utilities/classes/class.ilFileUtils.php';
00039 
00040 
00041 class ilFileXMLParser extends ilSaxParser
00042 {
00043     static $CONTENT_NOT_COMPRESSED = 0;
00044     static $CONTENT_GZ_COMPRESSED = 1;
00045     static $CONTENT_ZLIB_COMPRESSED = 2;
00046 
00052     var $file;
00053 
00060     var $obj_id;
00061 
00062 
00068     var $result;
00069 
00075     var $mode;
00076 
00077 
00083     //var $content;
00084     
00090     var $tmpFilename;
00091     
00092 
00101         function ilFileXMLParser(& $file, $a_xml_data, $obj_id = -1, $mode = 0)
00102         {
00103                 parent::ilSaxParser();
00104                 $this->file = $file;
00105                 $this->setXMLContent($a_xml_data);
00106                 $this->obj_id = $obj_id;
00107                 $this->result = false;
00108                 $this->mode = $mode;
00109         }
00110 
00111 
00118         function setHandlers($a_xml_parser)
00119         {
00120                 xml_set_object($a_xml_parser,$this);
00121                 xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
00122                 xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
00123         }
00124 
00134         function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
00135         {
00136                 global $ilErr;
00137 
00138                 switch($a_name)
00139                 {
00140                         case 'File':
00141                             if (isset($a_attribs["obj_id"]))
00142                             {
00143                    $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
00144                                if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id)
00145                                {
00146                        throw new ilFileException ("Object IDs (xml $read_obj_id and argument ".$this->obj_id.") do not match!", ilFileException::$ID_MISMATCH);
00147                    }
00148                    if (isset($a_attribs["type"]))
00149                    {
00150                         $this->file->setFileType($a_attribs["type"]);
00151                    }
00152                    $this->file->setVersion($this->file->getVersion() + 1);
00153                             }
00154                                 break;
00155                         case 'Content':
00156                                         $this->tmpFilename = ilUtil::ilTempnam();
00157                             $this->mode = ilFileXMLParser::$CONTENT_NOT_COMPRESSED;
00158                             $this->isReadingFile = true;
00159 #echo $a_attribs["mode"];
00160                             if (isset($a_attribs["mode"])) {
00161                                 if ($a_attribs["mode"] == "GZIP")
00162                                 {
00163                         if (!function_exists("gzread"))
00164                             throw new ilFileException ("Deflating with gzip is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
00165 
00166                                     $this->mode = ilFileXMLParser::$CONTENT_GZ_COMPRESSED;
00167                                 } elseif ($a_attribs["mode"] == "ZLIB")
00168                                 {
00169                         if (!function_exists("gzuncompress"))
00170                              throw new ilFileException ("Deflating with zlib (compress/uncompress) is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
00171 
00172                                     $this->mode = ilFileXMLParser::$CONTENT_ZLIB_COMPRESSED;
00173                                 }
00174 
00175                             }
00176                 }
00177         }
00178 
00179 
00180 
00187         function handlerEndTag($a_xml_parser,$a_name)
00188         {
00189                 switch($a_name)
00190                 {
00191                           case 'File':
00192            $this->result = true;
00193                                 break;
00194                         case 'Filename':
00195                             $this->file->setFilename(trim($this->cdata));
00196                             $this->file->setTitle(trim($this->cdata));
00197                                 break;
00198                         case 'Title':
00199                             $this->file->setTitle(trim($this->cdata));
00200                         case 'Description':
00201                             $this->file->setDescription(trim($this->cdata));
00202                                 break;
00203                         case 'Content':
00204                                         $this->isReadingFile = false;
00205                                         $baseDecodedFilename = ilUtil::ilTempnam();
00206                             if (!ilFileUtils::fastBase64Decode($this->tmpFilename, $baseDecodedFilename)) 
00207                             {
00208                                         throw new ilFileException ("Base64-Decoding failed", ilFileException::$DECOMPRESSION_FAILED);                                                                   
00209                             }
00210                 if ($this->mode == ilFileXMLParser::$CONTENT_GZ_COMPRESSED) 
00211                 {
00212                 if (!ilFileUtils::fastGunzip ($baseDecodedFilename, $this->tmpFilename)) 
00213                                                 {
00214                                                         throw new ilFileException ("Deflating with fastzunzip failed", ilFileException::$DECOMPRESSION_FAILED);                         
00215                                                 }
00216                                                 unlink ($baseDecodedFilename);
00217                 }elseif ($this->mode == ilFileXMLParser::$CONTENT_ZLIB_COMPRESSED) {
00218                 if (!ilFileUtils::fastGunzip ($baseDecodedFilename, $this->tmpFilename)) 
00219                                                 {
00220                                                         throw new ilFileException ("Deflating with fastDecompress failed", ilFileException::$DECOMPRESSION_FAILED);                     
00221                                                 }
00222                                                 unlink ($baseDecodedFilename);
00223                                         }else{
00224                                                 $this->tmpFilename = $baseDecodedFilename;
00225                                         }                    
00226                                  //$this->content = $content;
00227                                  $this->file->setFileSize(filesize($this->tmpFilename)); // strlen($this->content));
00228                                 
00229                                 // if no file type is given => lookup mime type
00230                                 if(!$this->file->getFileType())
00231                                 {
00232                                         global $ilLog;
00233                                         
00234                                         #$ilLog->write(__METHOD__.': Trying to detect mime type...');
00235                                         include_once('./Services/Utilities/classes/class.ilFileUtils.php');
00236                                         $this->file->setFileType(ilFileUtils::_lookupMimeType($this->tmpFilename));
00237                                 }
00238                                 
00239                                 break;
00240                 }
00241 
00242                 $this->cdata = '';
00243 
00244                 return;
00245         }
00246 
00253         function handlerCharacterData($a_xml_parser,$a_data)
00254         {
00255                 if($a_data != "\n")
00256                 {
00257                         if ($this->isReadingFile)
00258                         {
00259                         $handle = fopen ($this->tmpFilename, "a");
00260                                 fwrite ($handle, $a_data);
00261                                 fclose ($handle);
00262                         } else
00263                                 $this->cdata .= $a_data;
00264                 }
00265         }
00266 
00272         public function setFileContents ()
00273         {
00274        if (filesize ($this->tmpFilename) == 0) {
00275            return;
00276        }
00277 
00278        $filedir = $this->file->getDirectory($this->file->getVersion());
00279                         if (!is_dir($filedir))
00280                    {
00281                                 $this->file->createDirectory();
00282                                 ilUtil::makeDir($filedir);
00283                    }
00284 
00285            $filename = $filedir."/".$this->file->getFileName();
00286            return rename($this->tmpFilename, $filename);
00287            //@file_put_contents($filename, $this->content);
00288         }
00289 
00290 
00296         public function updateFileContents ()
00297         {
00298       if ($this->setFileContents()) 
00299       {
00300                 require_once("classes/class.ilHistory.php");
00301                                 ilHistory::_createEntry($this->file->getId(), "replace", $this->file->getFilename().",".$this->file->getVersion());
00302                                 $this->file->addNewsNotification("file_updated");       
00303                         }
00304         }
00305 
00313         public function start () {
00314             $this->startParsing();
00315             return $this->result > 0;
00316         }
00317 
00318 }
00319 ?>

Generated on Fri Dec 13 2013 17:56:50 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1