Dotclear


Ignore:
Timestamp:
07/09/12 14:44:20 (13 years ago)
Author:
JcDenis
Branch:
default
Message:

Clean up structure, added (un)zip to flat import/export, fixes #1319

File:
1 edited

Legend:

Unmodified
Added
Removed
  • plugins/importExport/inc/class.dc.import.flat.php

    r557 r840  
    22# -- BEGIN LICENSE BLOCK --------------------------------------- 
    33# 
    4 # This file is part of Dotclear 2. 
     4# This file is part of importExport, a plugin for DotClear2. 
    55# 
    6 # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear 
     6# Copyright (c) 2003-2012 Olivier Meunier & Association Dotclear 
    77# Licensed under the GPL version 2.0 license. 
    88# See LICENSE file or 
     
    1818     public function setInfo() 
    1919     { 
    20           $this->type = 'i'; 
     20          $this->type = 'import'; 
    2121          $this->name = __('Flat file import'); 
    2222          $this->description = __('Imports a blog or a full Dotclear installation from flat file.'); 
     
    5555                
    5656               try { 
    57                     $bk = new dcImport($this->core,$file); 
     57                    # Try to unzip file 
     58                    $unzip_file = $this->unzip($file); 
     59                    if (false !== $unzip_file) { 
     60                         $bk = new flatImport($this->core,$unzip_file); 
     61                    } 
     62                    # Else this is a normal file 
     63                    else { 
     64                         $bk = new flatImport($this->core,$file); 
     65                    } 
     66                     
    5867                    $bk->importSingle(); 
    5968               } catch (Exception $e) { 
     69                    @unlink($unzip_file); 
    6070                    if ($to_unlink) { 
    6171                         @unlink($file); 
     
    6373                    throw $e; 
    6474               } 
     75               @unlink($unzip_file); 
    6576               if ($to_unlink) { 
    6677                    @unlink($file); 
     
    95106                
    96107               try { 
    97                     $bk = new dcImport($this->core,$file); 
     108                    # Try to unzip file 
     109                    $unzip_file = $this->unzip($file); 
     110                    if (false !== $unzip_file) { 
     111                         $bk = new flatImport($this->core,$unzip_file); 
     112                    } 
     113                    # Else this is a normal file 
     114                    else { 
     115                         $bk = new flatImport($this->core,$file); 
     116                    } 
     117                     
    98118                    $bk->importFull(); 
    99119               } catch (Exception $e) { 
     120                    @unlink($unzip_file); 
    100121                    if ($to_unlink) { 
    101122                         @unlink($file); 
     
    103124                    throw $e; 
    104125               } 
     126               @unlink($unzip_file); 
    105127               if ($to_unlink) { 
    106128                    @unlink($file); 
     
    128150               return; 
    129151          } 
     152           
     153          $public_files = array_merge(array('-' => ''),$this->getPublicFiles()); 
     154          $has_files = (boolean) (count($public_files) - 1); 
    130155           
    131156          echo 
     
    155180           
    156181          echo 
    157           '<h3>'.__('Import a single blog').'</h3>'. 
    158           '<p>'.sprintf(__('This will import a single blog backup as new content in the current blog: %s.'), 
    159           '<strong>'.html::escapeHTML($this->core->blog->name).'</strong>').'</p>'. 
    160182          '<form action="'.$this->getURL(true).'" method="post" enctype="multipart/form-data">'. 
    161            
    162           '<div class="fieldset">'. 
     183          '<fieldset><legend>'.__('Single blog').'</legend>'. 
     184          '<p>'.sprintf(__('This will import a single blog backup as new content in the current blog: %s.'),html::escapeHTML($this->core->blog->name)).'</p>'. 
     185           
     186          '<p><label for="up_single_file">'.__('Upload a backup file').'</label>'. 
     187          '<input type="file" id="up_single_file" name="up_single_file" size="20" />'. 
     188          '</p>'; 
     189           
     190          if ($has_files) { 
     191               echo  
     192               '<p><label for="public_single_file">'.__('or pick up a local file in your public directory').' '. 
     193               form::combo('public_single_file',$public_files). 
     194               '</label></p>'; 
     195          } 
     196           
     197          echo  
     198          '<p>'. 
    163199          $this->core->formNonce(). 
    164200          form::hidden(array('do'),1). 
    165201          form::hidden(array('MAX_FILE_SIZE'),DC_MAX_UPLOAD_SIZE). 
    166           '<p><label for="up_single_file">'.__('Upload a backup file').'</label>'. 
    167           '<input type="file" id="up_single_file" name="up_single_file" size="20" />'. 
    168           '</p>'; 
    169            
    170           $public_files = $this->getPublicFiles(); 
    171            
    172           $empty = empty($public_files); 
    173           $public_files = array_merge(array('-' => ''),$public_files); 
    174           echo 
    175           '<p><label for="public_single_file">'.__('or pick up a local file in your public directory').' '. 
    176           form::combo('public_single_file',$public_files, '', '', '', $empty). 
    177           '</label></p>'; 
    178            
    179           echo 
    180           '<p><input type="submit" value="'.__('Import').'" /></p>'. 
    181           '</div>'. 
     202          '<input type="submit" value="'.__('Import').'" /></p>'. 
     203           
     204          '</fieldset>'. 
    182205          '</form>'; 
    183206           
     
    185208          { 
    186209               echo 
    187                '<h3>'.__('Import a full backup file').'</h3>'. 
    188210               '<form action="'.$this->getURL(true).'" method="post" enctype="multipart/form-data" id="formfull">'. 
    189                '<div>'.form::hidden(array('MAX_FILE_SIZE'),DC_MAX_UPLOAD_SIZE).'</div>'. 
    190                 
    191                '<div class="fieldset">'. 
     211               '<fieldset><legend>'.__('Multiple blogs').'</legend>'. 
     212               '<p>'.__('This will reset all the content of your database, except users.').'</p>'. 
     213                
     214               '<p><label for="up_full_file">'.__('Upload a backup file').'</label>'. 
     215               '<input type="file" id="up_full_file" name="up_full_file" size="20" />'. 
     216               '</p>'; 
     217                
     218               if ($has_files) { 
     219                    echo  
     220                    '<p><label for="public_full_file">'.__('or pick up a local file in your public directory').'</label>'. 
     221                    form::combo('public_full_file',$public_files). 
     222                    '</p>'; 
     223               } 
     224                
     225               echo 
     226               '<p><label for="your_pwd" class="required"><abbr title="'.__('Required field').'">*</abbr> '.__('Your password:').'</label>'. 
     227               form::password('your_pwd',20,255).'</p>'. 
     228                
     229               '<p>'. 
    192230               $this->core->formNonce(). 
    193231               form::hidden(array('do'),1). 
    194232               form::hidden(array('MAX_FILE_SIZE'),DC_MAX_UPLOAD_SIZE). 
    195                '<p><label for="up_full_file">'.__('Upload a backup file').'</label>'. 
    196                '<input type="file" id="up_full_file" name="up_full_file" size="20" />'. 
    197                '</p>'; 
    198                 
    199                echo 
    200                '<p><label for="public_full_file">'.__('or pick up a local file in your public directory').'</label>'. 
    201                form::combo('public_full_file',$public_files, '', '', '', $empty). 
    202                '</p>'; 
    203                 
    204                echo 
    205                '<p class="form-note warning"><strong>'.__('Warning: This will reset all the content of your database, except users.').'</strong></p>'. 
    206                 
    207                '<p><label for="your_pwd" class="required"><abbr title="'.__('Required field').'">*</abbr> '.__('Your password:').'</label>'. 
    208                form::password('your_pwd',20,255).'</p>'. 
    209                 
    210                '<p><input type="submit" value="'.__('Import').'" /></p>'. 
    211                '</div>'. 
     233               '<input type="submit" value="'.__('Import').'" /></p>'. 
     234                
     235               '</fieldset>'. 
    212236               '</form>'; 
    213237          } 
     
    225249                    if (is_file($entry_path) && is_readable($entry_path)) 
    226250                    { 
    227                          $fp = fopen($entry_path,'rb'); 
    228                          if (strpos(fgets($fp),'///DOTCLEAR|') === 0) { 
     251                         # Do not test each zip file content here, its too long 
     252                         if (substr($entry_path,-4) == '.zip') { 
     253                                   $public_files[$entry] = $entry_path; 
     254                         } 
     255                         elseif (self::checkFileContent($entry_path)) { 
    229256                              $public_files[$entry] = $entry_path; 
    230257                         } 
    231                          fclose($fp); 
    232258                    } 
    233259               } 
    234260          } 
    235261          return $public_files; 
     262     } 
     263      
     264     protected static function checkFileContent($entry_path) 
     265     { 
     266          $ret = false; 
     267           
     268          $fp = fopen($entry_path,'rb'); 
     269          $ret = strpos(fgets($fp),'///DOTCLEAR|') === 0; 
     270          fclose($fp); 
     271           
     272          return $ret; 
     273     } 
     274      
     275     private function unzip($file) 
     276     { 
     277          $zip = new fileUnzip($file); 
     278           
     279          if ($zip->isEmpty()) { 
     280               $zip->close(); 
     281               return false;//throw new Exception(__('File is empty or not a compressed file.')); 
     282          } 
     283           
     284          foreach($zip->getFilesList() as $zip_file) 
     285          { 
     286               # Check zipped file name 
     287               if (substr($zip_file,-4) != '.txt') { 
     288                    continue; 
     289               } 
     290                
     291               # Check zipped file contents 
     292               $content = $zip->unzip($zip_file); 
     293               if (strpos($content,'///DOTCLEAR|') !== 0) { 
     294                    unset($content); 
     295                    continue; 
     296               } 
     297                
     298               $target = path::fullFromRoot($zip_file,dirname($file)); 
     299                
     300               # Check existing files with same name 
     301               if (file_exists($target)) { 
     302                    $zip->close(); 
     303                    unset($content); 
     304                    throw new Exception(__('Another file with same name exists.')); 
     305               } 
     306                
     307               # Extract backup content 
     308               if (file_put_contents($target,$content) === false) { 
     309                    $zip->close(); 
     310                    unset($content); 
     311                    throw new Exception(__('Failed to extract backup file.')); 
     312               } 
     313                
     314               $zip->close(); 
     315               unset($content); 
     316                
     317               # Return extracted file name 
     318               return $target; 
     319          } 
     320           
     321          $zip->close(); 
     322          throw new Exception(__('No backup in compressed file.')); 
    236323     } 
    237324} 
Note: See TracChangeset for help on using the changeset viewer.

Sites map