[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
| 6 | # Copyright (c) 2003-2010 Olivier Meunier & Association Dotclear |
---|
| 7 | # Licensed under the GPL version 2.0 license. |
---|
| 8 | # See LICENSE file or |
---|
| 9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
| 10 | # |
---|
| 11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
| 12 | if (!defined('DC_RC_PATH')) { return; } |
---|
| 13 | |
---|
| 14 | class dcImportFlat extends dcIeModule |
---|
| 15 | { |
---|
| 16 | protected $status = false; |
---|
| 17 | |
---|
| 18 | public function setInfo() |
---|
| 19 | { |
---|
| 20 | $this->type = 'i'; |
---|
| 21 | $this->name = __('Flat file import'); |
---|
| 22 | $this->description = __('Imports a blog or a full Dotclear installation from flat file.'); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | public function process($do) |
---|
| 26 | { |
---|
| 27 | if ($do == 'single' || $do == 'full') { |
---|
| 28 | $this->status = $do; |
---|
| 29 | return; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | $to_unlink = false; |
---|
| 33 | |
---|
| 34 | # Single blog import |
---|
| 35 | $files = $this->getPublicFiles(); |
---|
| 36 | $single_upl = null; |
---|
| 37 | if (!empty($_POST['public_single_file']) && in_array($_POST['public_single_file'],$files)) { |
---|
| 38 | $single_upl = false; |
---|
| 39 | } elseif (!empty($_FILES['up_single_file'])) { |
---|
| 40 | $single_upl = true; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | if ($single_upl !== null) |
---|
| 44 | { |
---|
| 45 | if ($single_upl) { |
---|
| 46 | files::uploadStatus($_FILES['up_single_file']); |
---|
| 47 | $file = DC_TPL_CACHE.'/'.md5(uniqid()); |
---|
| 48 | if (!move_uploaded_file($_FILES['up_single_file']['tmp_name'],$file)) { |
---|
| 49 | throw new Exception(__('Unable to move uploaded file.')); |
---|
| 50 | } |
---|
| 51 | $to_unlink = true; |
---|
| 52 | } else { |
---|
| 53 | $file = $_POST['public_single_file']; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | try { |
---|
| 57 | $bk = new dcImport($this->core,$file); |
---|
| 58 | $bk->importSingle(); |
---|
| 59 | } catch (Exception $e) { |
---|
| 60 | if ($to_unlink) { |
---|
| 61 | @unlink($file); |
---|
| 62 | } |
---|
| 63 | throw $e; |
---|
| 64 | } |
---|
| 65 | if ($to_unlink) { |
---|
| 66 | @unlink($file); |
---|
| 67 | } |
---|
| 68 | http::redirect($this->getURL().'&do=single'); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | # Full import |
---|
| 72 | $full_upl = null; |
---|
| 73 | if (!empty($_POST['public_full_file']) && in_array($_POST['public_full_file'],$files)) { |
---|
| 74 | $full_upl = false; |
---|
| 75 | } elseif (!empty($_FILES['up_full_file'])) { |
---|
| 76 | $full_upl = true; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | if ($full_upl !== null && $this->core->auth->isSuperAdmin()) |
---|
| 80 | { |
---|
| 81 | if (empty($_POST['your_pwd']) || !$this->core->auth->checkPassword(crypt::hmac(DC_MASTER_KEY,$_POST['your_pwd']))) { |
---|
| 82 | throw new Exception(__('Password verification failed')); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | if ($full_upl) { |
---|
| 86 | files::uploadStatus($_FILES['up_full_file']); |
---|
| 87 | $file = DC_TPL_CACHE.'/'.md5(uniqid()); |
---|
| 88 | if (!move_uploaded_file($_FILES['up_full_file']['tmp_name'],$file)) { |
---|
| 89 | throw new Exception(__('Unable to move uploaded file.')); |
---|
| 90 | } |
---|
| 91 | $to_unlink = true; |
---|
| 92 | } else { |
---|
| 93 | $file = $_POST['public_full_file']; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | try { |
---|
| 97 | $bk = new dcImport($this->core,$file); |
---|
| 98 | $bk->importFull(); |
---|
| 99 | } catch (Exception $e) { |
---|
| 100 | if ($to_unlink) { |
---|
| 101 | @unlink($file); |
---|
| 102 | } |
---|
| 103 | throw $e; |
---|
| 104 | } |
---|
| 105 | if ($to_unlink) { |
---|
| 106 | @unlink($file); |
---|
| 107 | } |
---|
| 108 | http::redirect($this->getURL().'&do=full'); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | header('content-type:text/plain'); |
---|
| 112 | var_dump($_POST); |
---|
| 113 | exit; |
---|
| 114 | |
---|
| 115 | $this->status = true; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | public function gui() |
---|
| 119 | { |
---|
| 120 | if ($this->status == 'single') |
---|
| 121 | { |
---|
| 122 | echo '<p class="message">'.__('Single blog successfully imported.').'</p>'; |
---|
| 123 | return; |
---|
| 124 | } |
---|
| 125 | if ($this->status == 'full') |
---|
| 126 | { |
---|
| 127 | echo '<p class="message">'.__('Content successfully imported.').'</p>'; |
---|
| 128 | return; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | echo |
---|
| 132 | '<script type="text/javascript">'."\n". |
---|
| 133 | "//<![CDATA[\n". |
---|
| 134 | dcPage::jsVar('dotclear.msg.confirm_full_import', |
---|
| 135 | __('Are you sure you want to import a full backup file?')). |
---|
| 136 | "$(function() {". |
---|
| 137 | "$('#up_single_file').change(function() { ". |
---|
| 138 | "if (this.value != '') { $('#public_single_file').val(''); } ". |
---|
| 139 | "}); ". |
---|
| 140 | "$('#public_single_file').change(function() { ". |
---|
| 141 | "if (this.value != '') { $('#up_single_file').val(''); } ". |
---|
| 142 | "}); ". |
---|
| 143 | "$('#up_full_file').change(function() { ". |
---|
| 144 | "if (this.value != '') { $('#public_full_file').val(''); } ". |
---|
| 145 | "}); ". |
---|
| 146 | "$('#public_full_file').change(function() { ". |
---|
| 147 | "if (this.value != '') { $('#up_full_file').val(''); } ". |
---|
| 148 | "}); ". |
---|
| 149 | "$('#formfull').submit(function() { ". |
---|
| 150 | "return window.confirm(dotclear.msg.confirm_full_import); ". |
---|
| 151 | "}); ". |
---|
| 152 | "});\n". |
---|
| 153 | "//]]>\n". |
---|
| 154 | "</script>\n"; |
---|
| 155 | |
---|
| 156 | 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>'. |
---|
| 160 | '<form action="'.$this->getURL(true).'" method="post" enctype="multipart/form-data">'. |
---|
| 161 | |
---|
| 162 | '<fieldset>'. |
---|
| 163 | $this->core->formNonce(). |
---|
| 164 | form::hidden(array('do'),1). |
---|
| 165 | form::hidden(array('MAX_FILE_SIZE'),DC_MAX_UPLOAD_SIZE). |
---|
| 166 | '<p><label>'.__('Upload a backup file').' '. |
---|
| 167 | '<input type="file" id="up_single_file" name="up_single_file" size="20" />'. |
---|
| 168 | '</label></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>'.__('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="'.__('Send').'" /></p>'. |
---|
| 181 | '</fieldset>'. |
---|
| 182 | '</form>'; |
---|
| 183 | |
---|
| 184 | if ($this->core->auth->isSuperAdmin()) |
---|
| 185 | { |
---|
| 186 | echo |
---|
| 187 | '<h3>'.__('Import a full backup file').'</h3>'. |
---|
| 188 | '<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 | '<fieldset>'. |
---|
| 192 | $this->core->formNonce(). |
---|
| 193 | form::hidden(array('do'),1). |
---|
| 194 | form::hidden(array('MAX_FILE_SIZE'),DC_MAX_UPLOAD_SIZE). |
---|
| 195 | '<p><label>'.__('Upload a backup file').' '. |
---|
| 196 | '<input type="file" id="up_full_file" name="up_full_file" size="20" />'. |
---|
| 197 | '</label></p>'; |
---|
| 198 | |
---|
| 199 | echo |
---|
| 200 | '<p><label>'.__('or pick up a local file in your public directory').' '. |
---|
| 201 | form::combo('public_full_file',$public_files, '', '', '', $empty). |
---|
| 202 | '</label></p>'; |
---|
| 203 | |
---|
| 204 | echo |
---|
| 205 | '<p><strong>'.__('Warning: This will reset all the content of your database, except users.').'</strong></p>'. |
---|
| 206 | |
---|
| 207 | '<p><label>'.__('Your password:'). |
---|
| 208 | form::password('your_pwd',20,255).'</label></p>'. |
---|
| 209 | |
---|
| 210 | '<p><input type="submit" value="'.__('Send').'" /></p>'. |
---|
| 211 | '</fieldset>'. |
---|
| 212 | '</form>'; |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | protected function getPublicFiles() |
---|
| 217 | { |
---|
| 218 | $public_files = array(); |
---|
| 219 | $dir = @dir($this->core->blog->public_path); |
---|
| 220 | if ($dir) |
---|
| 221 | { |
---|
| 222 | while (($entry = $dir->read()) !== false) { |
---|
| 223 | $entry_path = $dir->path.'/'.$entry; |
---|
| 224 | |
---|
| 225 | if (is_file($entry_path) && is_readable($entry_path)) |
---|
| 226 | { |
---|
| 227 | $fp = fopen($entry_path,'rb'); |
---|
| 228 | if (strpos(fgets($fp),'///DOTCLEAR|') === 0) { |
---|
| 229 | $public_files[$entry] = $entry_path; |
---|
| 230 | } |
---|
| 231 | fclose($fp); |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | return $public_files; |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | ?> |
---|