--- /home/mosa/tmp/trunk/modules/ezodf/ezooimport.php	2009-01-20 22:35:40.573358875 +0100
+++ /home/mosa/Sources/Scuola/sito_cervignano/m2000/extension/ezodf/modules/ezodf/ezooimport.php	2009-01-24 21:59:20.810767786 +0100
@@ -317,13 +317,27 @@
         $unzipResult = "";
         $uniqueImportDir = $this->ImportDir;
         eZDir::mkdir( $uniqueImportDir, false, true );
-
+        //$uniqueImportDir="/tmp/test2"; 
         $http = eZHTTPTool::instance();
-
-        $archiveOptions = new ezcArchiveOptions( array( 'readOnly' => true ) );
-        $archive = ezcArchive::open( $file, null, $archiveOptions );
-        $archive->extract( $uniqueImportDir );
-
+        
+        //ezcArchive extracts empty xml file from odf archive
+        //$archiveOptions = new ezcArchiveOptions( array( 'readOnly' => true ) );
+        //$archive = ezcArchive::open( $file, ezcArchive::ZIP, $archiveOptions );
+        //$archive->extract( $uniqueImportDir );
+        
+        
+        // Check if zlib extension is loaded, if it's loaded use bundled ZIP library,
+        // if not rely on the unzip commandline version.
+        if ( !function_exists( 'gzopen' ) )
+        {
+            exec( "unzip -o $file -d " . $uniqueImportDir, $unzipResult );
+        }
+        else
+        {
+            require_once('extension/ezodf/lib/pclzip.lib.php');
+            $archive = new PclZip( $file );
+            $archive->extract( PCLZIP_OPT_PATH, $uniqueImportDir );
+        }
         $fileName = $uniqueImportDir . "content.xml";
         $dom = new DOMDocument( '1.0', 'UTF-8' );
         $success = $dom->load( $fileName );
@@ -907,6 +921,7 @@
             {
 
                 // Publish related images
+                if (isset($imageRootNode)){
                 $nodeAssignment = eZNodeAssignment::create( array(
                                                                  'contentobject_id' => $image['ID'],
                                                                  'contentobject_version' => 1,
@@ -915,11 +930,15 @@
                                                                  )
                                                              );
                 $nodeAssignment->store();
-
-                $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $image['ID'],
-                                                                                             'version' => 1 ) );
-
-                $object->addContentObjectRelation( $image['ID'], 1 );
+                //mosarg@gmail.com
+                $image_object=eZContentObject::fetch( $image['ID'] );
+                eZDebug::writeError( "Image Main Node Id". $image_object->attribute('main_node_id'). "<br>" );
+                //If image is not archived add it to related objects list
+                if($image_object->attribute('status')!=eZContentObject::STATUS_ARCHIVED){
+                        $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $image['ID'],'version' => 1 ) );
+                        $object->addContentObjectRelation( $image['ID'], 1 );
+                  }
+                }
             }
 
             $mainNode = $object->attribute( 'main_node' );
@@ -959,6 +978,7 @@
             switch ( $node->localName )
             {
                 case 'sequence-decls' :
+                case 'frame':
                 case 'forms' :
                 {
                     // do nothing
@@ -975,17 +995,16 @@
 
                 case 'h' :
                 {
+                    
                     $level = $node->getAttributeNS( self::NAMESPACE_TEXT, 'outline-level' );
-
                     if ( $level > 6 )
                         $level = 6;
 
                     if ( $level >= 1 && $level <= 6 )
                     {
-                        $levelDiff = $level - $sectionLevel;
+                        $levelDiff = $level-$sectionLevel;
                         $sectionLevel = $level;
                         $headerContent = "";
-
                         foreach ( $node->childNodes as $childNode )
                         {
                             // Alex 2008/04/21 - added initializations for $nextlineBreak and $prevlineBreak
@@ -998,7 +1017,6 @@
                             $headerContent .= self::handleInlineNode( $childNode, $nextLineBreak, $prevLineBreak );
                         }
                         $sectionLevel = $level;
-
                         if ( $levelDiff > 0 )
                             $xhtmlTextContent .= str_repeat( "<section>", $levelDiff );
 
@@ -1066,7 +1084,7 @@
 
                             // Get the parent style name, it's used to see if it's a
                             // header which comes from Word conversion
-                            $parentStyleName = $style->getAttributeNS( "parent-style-name" );
+                            $parentStyleName = $style->getAttributeNS( self::NAMESPACE_STYLE,"parent-style-name" );
 
                             // Check if we've got a header definition and which level Heading_20
                             // Header styles is either defined in style-name or parent-style-name when
@@ -1206,8 +1224,10 @@
 
                 case 'list' :
                 {
-                    $styleName = $node->getAttributeNS( self::NAMESPACE_TEXT, 'style-name' );
-
+                    $styleName     = $node->getAttributeNS( self::NAMESPACE_TEXT, 'style-name' );
+                    $listNumbering = $node->getAttributeNS( self::NAMESPACE_TEXT,'restart-numbering');
+                    eZDebug::writeError( "errore list ".$listNumbering." ".$styleName);
+                    
                     // Check list style for unordered/ordered list
                     $listType = false;
                     foreach ( $this->AutomaticStyles as $style )
@@ -1253,6 +1273,7 @@
                     $this->IsSubList = true;
                     foreach ( $node->childNodes as $itemNode )
                     {
+                        eZDebug::writeError( "sub list item node ".$itemNode->localName );
                         if ( $itemNode->localName == 'list-item' )
                         {
                             foreach ( $itemNode->childNodes as $childNode )
@@ -1300,7 +1321,7 @@
                         }
                     }
                 }break;
-
+                case 'table-of-content':
                 case 'table' :
                 {
                     $tableContent = "";
@@ -1315,6 +1336,7 @@
                                     $rowContent = "";
                                     foreach ( $headerRow->childNodes as $tableCell )
                                     {
+                                        if(isset($tableCell->localName)){
                                         $colSpan = $tableCell->getAttributeNS( self::NAMESPACE_TABLE, 'number-columns-spanned' );
 
                                         if ( $tableCell->localName == 'table-cell' )
@@ -1331,6 +1353,7 @@
                                             }
                                             $rowContent .= "<th $colSpanXML>" . $cellContent . "</th>";
                                         }
+                                    }else{$rowContent=$tableCell->wholeText;}//mosarg@gmail.com If child object is an instance of DOMText read wholeText attribute
                                     }
                                     $tableContent .= "<tr>" . $rowContent . "</tr>";
                                 }
@@ -1373,6 +1396,21 @@
         return $xhtmlTextContent;
     }
 
+   
+   /*
+    mosarg@gmail.com 
+     Escape xml illegal characters (everywhere)
+   */
+   
+   function escapeIn($text){
+        $text = str_replace("&", "&amp;", $text);
+        $text = str_replace("<", "&lt;", $text);
+        $text = str_replace(">", "&gt;", $text);
+        $text = str_replace("\"", "&quot;", $text);
+        $text = str_replace( "'", "&apos;", $text );
+    return $text;
+   }
+   
     /*!
       Handles the rendering of line nodes, e.g. inside paragraphs and headers.
 
@@ -1381,6 +1419,25 @@
     function handleInlineNode( $childNode, $nextLineBreak = false, $prevLineBreak = false )
     {
         $paragraphContent = "";
+       
+       /*mosarg@gmail.com*/
+       /* If child node is an instance of DOMText read wholeText Attribute*/
+        if (!isset($childNode->localName)){
+        
+        $paragraphContent=$childNode->wholeText;
+        
+         if ( $nextLineBreak )
+        {
+            $paragraphContent = '<line>' . $paragraphContent . '</line>';
+        }
+        elseif ( $prevLineBreak && $paragraphContent )
+        {
+            $paragraphContent = '<line>' . $paragraphContent . '</line>';
+        }
+        return self::escapeIn($paragraphContent);
+        }
+        /*mosarg@gmail.com*/
+        
         switch ( $childNode->localName )
         {
             case "frame":
@@ -1523,7 +1580,7 @@
 
                                 $contentObject = eZContentObject::fetchByRemoteID( $remoteID );
 
-                                // If image does not already exist, create it as an object
+                                // If image object does not already exist, create it as an object
                                 if ( !$contentObject )
                                 {
 
@@ -1580,8 +1637,13 @@
                                     $this->RelatedImageArray[] = array( "ID" => $contentObjectID,
                                                                         "ContentObject" => $contentObject );
                                 }
-                                else
+                                else{
+                                    //mosarg@gmail.com
+                                    //if image object already exists insert it inside related image array
                                     $contentObjectID = $contentObject->attribute( 'id' );
+                                    $this->RelatedImageArray[] = array( "ID" => $contentObjectID,
+                                                                        "ContentObject" => $contentObject );
+                                }
 
 
                                 $frameContent .= "<embed object_id='$contentObjectID' align='$imageAlignment' size='$imageSize' />";
@@ -1603,16 +1665,18 @@
                 {
                     $boxContent .= self::handleNode( $textBoxNode, $sectionLevel );
                 }
-
+                $boxContent=self::escapeIn($boxContent);
                 // Textboxes are defined inside paragraphs.
                 $paragraphContent .= "</paragraph>$boxContent<paragraph>";
             }break;
 
             case 'sequence':
+            case 'soft-page-break':
             case 'date':
+            case 'tab':
             case 'initial-creator':
             {
-                $paragraphContent .= $childNode->textContent;
+                $paragraphContent .= self::escapeIn($childNode->textContent);
             }break;
 
             case "s" :
@@ -1623,17 +1687,19 @@
             case "a" :
             {
                 $href = $childNode->getAttributeNS( self::NAMESPACE_XLINK, 'href' );
-                $paragraphContent .= "<link href='$href'>" . $childNode->textContent . "</link>";
+                $paragraphContent .= "<link href='$href'>" .self::escapeIn($childNode->textContent) . "</link>";
             }break;
 
             case "#text" :
             {
-                $tagContent = str_replace( "&", "&amp;", $childNode->textContent );
+                /*mosarg@gmail.com*/
+                $tagContent = $childNode->textContent;
+                $tagContent = self::escapeIn($tagContent);
+               /* str_replace( "&", "&amp;", $childNode->textContent );
                 $tagContent = str_replace( ">", "&gt;", $tagContent );
                 $tagContent = str_replace( "<", "&lt;", $tagContent );
                 $tagContent = str_replace( "'", "&apos;", $tagContent );
-                $tagContent = str_replace( '"', "&quot;", $tagContent );
-
+                $tagContent = str_replace( '"', "&quot;", $tagContent );*/
                 $paragraphContent .= $tagContent;
             }break;
 
@@ -1679,7 +1745,7 @@
                 if ( $fontStyle == "italic" )
                     $paragraphContent .= "<emphasize>";
 
-                $paragraphContent .= $childNode->textContent;
+                $paragraphContent .= self::escapeIn($childNode->textContent);
 
                 if ( $fontStyle == "italic" )
                     $paragraphContent .= "</emphasize>";
@@ -1694,8 +1760,8 @@
 
             default:
             {
-                eZDebug::writeError( "Unsupported node: " . $childNode->localName . "<br>" );
-            }break;
+                eZDebug::writeError( "Unsupported node (default): " . $childNode->localName . "<br>" );
+                }break;
 
         }
 
@@ -1734,7 +1800,7 @@
             {
                 $creatorID = $this->currentUserID;
                 //$creatorID = 14; // 14 == admin
-                $parentNodeID = $placeNodeID;
+                //$parentNodeID = $placeNodeID;
                 $contentObject = $class->instantiate( $creatorID, 1 );
 
                 $nodeAssignment = eZNodeAssignment::create( array(
