public function process($do) { # Export a blog if ($do == 'export_blog' && $this->core->auth->check('admin', $this->core->blog->id)) { $fullname = $this->core->blog->public_path . '/.backup_' . sha1(uniqid()); $blog_id = $this->core->con->escape($this->core->blog->id); try { $exp = new flatExport($this->core->con, $fullname, $this->core->prefix); fwrite($exp->fp, '///DOTCLEAR|' . DC_VERSION . "|single\n"); $exp->export('category', 'SELECT * FROM ' . $this->core->prefix . 'category ' . "WHERE blog_id = '" . $blog_id . "'"); $exp->export('link', 'SELECT * FROM ' . $this->core->prefix . 'link ' . "WHERE blog_id = '" . $blog_id . "'"); $exp->export('setting', 'SELECT * FROM ' . $this->core->prefix . 'setting ' . "WHERE blog_id = '" . $blog_id . "'"); $exp->export('post', 'SELECT * FROM ' . $this->core->prefix . 'post ' . "WHERE blog_id = '" . $blog_id . "'"); $exp->export('meta', 'SELECT meta_id, meta_type, M.post_id ' . 'FROM ' . $this->core->prefix . 'meta M, ' . $this->core->prefix . 'post P ' . 'WHERE P.post_id = M.post_id ' . "AND P.blog_id = '" . $blog_id . "'"); $exp->export('media', 'SELECT * FROM ' . $this->core->prefix . "media WHERE media_path = '" . $this->core->con->escape($this->core->blog->settings->system->public_path) . "'"); $exp->export('post_media', 'SELECT media_id, M.post_id ' . 'FROM ' . $this->core->prefix . 'post_media M, ' . $this->core->prefix . 'post P ' . 'WHERE P.post_id = M.post_id ' . "AND P.blog_id = '" . $blog_id . "'"); $exp->export('ping', 'SELECT ping.post_id, ping_url, ping_dt ' . 'FROM ' . $this->core->prefix . 'ping ping, ' . $this->core->prefix . 'post P ' . 'WHERE P.post_id = ping.post_id ' . "AND P.blog_id = '" . $blog_id . "'"); $exp->export('comment', 'SELECT C.* ' . 'FROM ' . $this->core->prefix . 'comment C, ' . $this->core->prefix . 'post P ' . 'WHERE P.post_id = C.post_id ' . "AND P.blog_id = '" . $blog_id . "'"); # --BEHAVIOR-- exportSingle $this->core->callBehavior('exportSingle', $this->core, $exp, $blog_id); $_SESSION['export_file'] = $fullname; $_SESSION['export_filename'] = $_POST['file_name']; $_SESSION['export_filezip'] = !empty($_POST['file_zip']); http::redirect($this->getURL() . '&do=ok'); } catch (Exception $e) { @unlink($fullname); throw $e; } } # Export all content if ($do == 'export_all' && $this->core->auth->isSuperAdmin()) { $fullname = $this->core->blog->public_path . '/.backup_' . sha1(uniqid()); try { $exp = new flatExport($this->core->con, $fullname, $this->core->prefix); fwrite($exp->fp, '///DOTCLEAR|' . DC_VERSION . "|full\n"); $exp->exportTable('blog'); $exp->exportTable('category'); $exp->exportTable('link'); $exp->exportTable('setting'); $exp->exportTable('user'); $exp->exportTable('pref'); $exp->exportTable('permissions'); $exp->exportTable('post'); $exp->exportTable('meta'); $exp->exportTable('media'); $exp->exportTable('post_media'); $exp->exportTable('log'); $exp->exportTable('ping'); $exp->exportTable('comment'); $exp->exportTable('spamrule'); $exp->exportTable('version'); # --BEHAVIOR-- exportFull $this->core->callBehavior('exportFull', $this->core, $exp); $_SESSION['export_file'] = $fullname; $_SESSION['export_filename'] = $_POST['file_name']; $_SESSION['export_filezip'] = !empty($_POST['file_zip']); http::redirect($this->getURL() . '&do=ok'); } catch (Exception $e) { @unlink($fullname); throw $e; } } # Send file content if ($do == 'ok') { if (!file_exists($_SESSION['export_file'])) { throw new Exception(__('Export file not found.')); } ob_end_clean(); if (substr($_SESSION['export_filename'], -4) == '.zip') { $_SESSION['export_filename'] = substr($_SESSION['export_filename'], 0, -4); //.'.txt'; } # Flat export if (empty($_SESSION['export_filezip'])) { header('Content-Disposition: attachment;filename=' . $_SESSION['export_filename']); header('Content-Type: text/plain; charset=UTF-8'); readfile($_SESSION['export_file']); unlink($_SESSION['export_file']); unset($_SESSION['export_file'], $_SESSION['export_filename'], $_SESSION['export_filezip']); exit; } else { try { $file_zipname = $_SESSION['export_filename'] . '.zip'; $fp = fopen('php://output', 'wb'); $zip = new fileZip($fp); $zip->addFile($_SESSION['export_file'], $_SESSION['export_filename']); header('Content-Disposition: attachment;filename=' . $file_zipname); header('Content-Type: application/x-zip'); $zip->write(); unlink($_SESSION['export_file']); unset($zip, $_SESSION['export_file'], $_SESSION['export_filename'], $file_zipname); exit; } catch (Exception $e) { unset($zip, $_SESSION['export_file'], $_SESSION['export_filename'], $file_zipname); @unlink($_SESSION['export_file']); throw new Exception(__('Failed to compress export file.')); } } } }
/** * Backups changed files before an update. * * @param string $sZipFile * @param string $sZipDigests * @param string $sRoot * @param string $sRootDigests * @param string $sDest * @throws Exception * @return boolean */ public function backup($sZipFile, $sZipDigests, $sRoot, $sRootDigests, $sDest) { if (!is_readable($sZipFile)) { throw new Exception(__('c_a_update_archive_not_found')); } if (!is_readable($sRootDigests)) { @unlink($sZipFile); throw new Exception(__('c_a_update_unable_read_digests')); } # Stop everything if a backup already exists and can not be overrided if (!is_writable(dirname($sDest)) && !file_exists($sDest)) { throw new Exception(__('c_a_update_root_directory_not_writable')); } if (file_exists($sDest) && !is_writable($sDest)) { return false; } $b_fp = @fopen($sDest, 'wb'); if ($b_fp === false) { return false; } $oZip = new fileUnzip($sZipFile); $b_zip = new fileZip($b_fp); if (!$oZip->hasFile($sZipDigests)) { @unlink($sZipFile); throw new Exception(__('c_a_update_downloaded_file_not_valid_archive')); } $opts = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES; $cur_digests = file($sRootDigests, $opts); $new_digests = explode("\n", $oZip->unzip($sZipDigests)); $aNewFiles = $this->getNewFiles($cur_digests, $new_digests); $oZip->close(); unset($opts, $cur_digests, $new_digests, $oZip); $aNotReadable = array(); if (!empty($this->aForcedFiles)) { $aNewFiles = array_merge($aNewFiles, $this->aForcedFiles); } foreach ($aNewFiles as $file) { if (!$file || !file_exists($sRoot . '/' . $file)) { continue; } try { $b_zip->addFile($sRoot . '/' . $file, $file); } catch (Exception $e) { $aNotReadable[] = $file; } } # If only one file is not readable, stop everything now if (!empty($aNotReadable)) { $e = new Exception('Some files are not readable.', self::ERR_FILES_UNREADABLE); $e->bad_files = $aNotReadable; throw $e; } $b_zip->write(); fclose($b_fp); $b_zip->close(); return true; }
/** Backups changed files before an update. */ public function backup($zip_file, $zip_digests, $root, $root_digests, $dest) { if (!is_readable($zip_file)) { throw new Exception(__('Archive not found.')); } if (!is_readable($root_digests)) { @unlink($zip_file); throw new Exception(__('Unable to read current digests file.')); } # Stop everything if a backup already exists and can not be overrided if (!is_writable(dirname($dest)) && !file_exists($dest)) { throw new Exception(__('Root directory is not writable.')); } if (file_exists($dest) && !is_writable($dest)) { return false; } $b_fp = @fopen($dest, 'wb'); if ($b_fp === false) { return false; } $zip = new fileUnzip($zip_file); $b_zip = new fileZip($b_fp); if (!$zip->hasFile($zip_digests)) { @unlink($zip_file); throw new Exception(__('Downloaded file seems not to be a valid archive.')); } $opts = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES; $cur_digests = file($root_digests, $opts); $new_digests = explode("\n", $zip->unzip($zip_digests)); $new_files = $this->getNewFiles($cur_digests, $new_digests); $zip->close(); unset($opts, $cur_digests, $new_digests, $zip); $not_readable = array(); if (!empty($this->forced_files)) { $new_files = array_merge($new_files, $this->forced_files); } foreach ($new_files as $file) { if (!$file || !file_exists($root . '/' . $file)) { continue; } try { $b_zip->addFile($root . '/' . $file, $file); } catch (Exception $e) { $not_readable[] = $file; } } # If only one file is not readable, stop everything now if (!empty($not_readable)) { $e = new Exception('Some files are not readable.', self::ERR_FILES_UNREADABLE); $e->bad_files = $not_readable; throw $e; } $b_zip->write(); fclose($b_fp); $b_zip->close(); return true; }