private function __export() { $sql_schema = $sql_data = NULL; require_once dirname(__FILE__) . '/lib/class.mysqldump.php'; $dump = new MySQLDump($this->_Parent->Database); $tables = array('tbl_authors', 'tbl_cache', 'tbl_entries', 'tbl_extensions', 'tbl_extensions_delegates', 'tbl_fields', 'tbl_fields_%', 'tbl_forgotpass', 'tbl_pages', 'tbl_pages_types', 'tbl_sections', 'tbl_sections_association'); ## Grab the schema foreach ($tables as $t) { $sql_schema .= $dump->export($t, MySQLDump::STRUCTURE_ONLY); } $sql_schema = str_replace('`' . $this->_Parent->Configuration->get('tbl_prefix', 'database'), '`tbl_', $sql_schema); $sql_schema = preg_replace('/AUTO_INCREMENT=\\d+/i', '', $sql_schema); $tables = array('tbl_entries', 'tbl_extensions', 'tbl_extensions_delegates', 'tbl_fields', 'tbl_pages', 'tbl_pages_types', 'tbl_sections', 'tbl_sections_association'); ## Field data and entry data schemas needs to be apart of the workspace sql dump $sql_data = $dump->export('tbl_fields_%', MySQLDump::ALL); $sql_data .= $dump->export('tbl_entries_%', MySQLDump::ALL); ## Grab the data foreach ($tables as $t) { $sql_data .= $dump->export($t, MySQLDump::DATA_ONLY); } $sql_data = str_replace('`' . $this->_Parent->Configuration->get('tbl_prefix', 'database'), '`tbl_', $sql_data); $config_string = NULL; $config = $this->_Parent->Configuration->get(); unset($config['symphony']['build']); unset($config['symphony']['cookie_prefix']); unset($config['general']['useragent']); unset($config['file']['write_mode']); unset($config['directory']['write_mode']); unset($config['database']['host']); unset($config['database']['port']); unset($config['database']['user']); unset($config['database']['password']); unset($config['database']['db']); unset($config['database']['tbl_prefix']); unset($config['region']['timezone']); foreach ($config as $group => $set) { foreach ($set as $key => $val) { $config_string .= "\t\t\$conf['" . $group . "']['" . $key . "'] = '" . $val . "';" . self::CRLF; } } $install_template = str_replace(array('<!-- BUILD -->', '<!-- VERSION -->', '<!-- ENCODED SQL SCHEMA DUMP -->', '<!-- ENCODED SQL DATA DUMP -->', '<!-- CONFIGURATION -->'), array($this->_Parent->Configuration->get('build', 'symphony'), $this->_Parent->Configuration->get('version', 'symphony'), base64_encode($sql_schema), base64_encode($sql_data), trim($config_string)), file_get_contents(dirname(__FILE__) . '/lib/installer.tpl')); $archive = new ZipArchive(); $res = $archive->open(TMP . '/install.tmp.zip', ZipArchive::CREATE); if ($res === TRUE) { $archive->addFromString('workspace/install.sql', $sql_data); } $archive->close(); header('Content-type: application/octet-stream'); header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Content-disposition: attachment; filename=' . Lang::createFilename($this->_Parent->Configuration->get('sitename', 'general')) . '-install.zip'); header('Pragma: no-cache'); readfile(TMP . '/install.tmp.zip'); unlink(TMP . '/install.tmp.zip'); exit; }
/** /** * Backup the current state of the Symphony database * This function can only be called from the Symphony backend, the CDI extension must be enabled and running in Database Synchronisation mode. * @param $mode The mode of backup: 'manual' or 'automatic'. This is used to determine which configuration setting should apply. */ public static function backup($mode) { // We should only backup the database when the extension is enabled and version 1.09 of the Dump_DB extension is installed. if((!class_exists('Administration')) || !CdiUtil::isEnabled()) { throw new Exception("You can only import the Database Synchroniser file from the Preferences page"); } if(!CdiUtil::hasDumpDBInstalled()) { throw new Exception('No valid version of <a href="http://symphony-cms.com/download/extensions/view/40986/">Dump DB</a> found. Please make sure it is installed.'); } else { require_once(EXTENSIONS . '/dump_db/lib/class.mysqldump.php'); // Prevent the CdiLogQuery::log() from persisting queries that are executed by CDI itself CdiLogQuery::isUpdating(true); // COPIED FROM Dump_DB version 1.09 // Adjust to only support FULL database dump $sql = CdiUtil::getMetaData(); $dump = new MySQLDump(Symphony::Database()); $rows = Symphony::Database()->fetch("SHOW TABLES LIKE 'tbl_%';"); $rows = array_map (create_function ('$x', 'return array_values ($x);'), $rows); $tables = array_map (create_function ('$x', 'return $x[0];'), $rows); // Get DATA from all tables foreach ($tables as $table){ $table = str_replace(Symphony::Configuration()->get('tbl_prefix', 'database'), 'tbl_', $table); $sql .= $dump->export($table, MySQLDump::ALL); $sql = str_replace(Symphony::Configuration()->get('tbl_prefix', 'database'), 'tbl_', $sql); } // Persist SQL data to file if(($mode == 'automatic' && Symphony::Configuration()->get('backup-overwrite', 'cdi') == 'yes') || ($mode == 'manual' && Symphony::Configuration()->get('manual-backup-overwrite', 'cdi') == 'yes')) { self::uninstall(); } $filename = self::getFileName($mode); file_put_contents($filename,$sql); // Re-enable CdiLogQuery::log() to persist queries CdiLogQuery::isUpdating(false); // Return the filename of the backup for automatic restore return $filename; } }
private function __export() { $sql_schema = $sql_data = NULL; require_once dirname(__FILE__) . '/lib/class.mysqldump.php'; $dump = new MySQLDump(Symphony::Database()); $tables = array('tbl_authors', 'tbl_cache', 'tbl_entries', 'tbl_extensions', 'tbl_extensions_delegates', 'tbl_fields', 'tbl_fields_%', 'tbl_forgotpass', 'tbl_pages', 'tbl_pages_types', 'tbl_sections', 'tbl_sections_association'); ## Grab the schema foreach ($tables as $t) { $sql_schema .= $dump->export($t, MySQLDump::STRUCTURE_ONLY); } $sql_schema = str_replace('`' . Symphony::Configuration()->get('tbl_prefix', 'database'), '`tbl_', $sql_schema); $sql_schema = preg_replace('/AUTO_INCREMENT=\\d+/i', NULL, $sql_schema); $tables = array('tbl_entries', 'tbl_extensions', 'tbl_extensions_delegates', 'tbl_fields', 'tbl_pages', 'tbl_pages_types', 'tbl_sections', 'tbl_sections_association'); ## Field data and entry data schemas needs to be apart of the workspace sql dump $sql_data = $dump->export('tbl_fields_%', MySQLDump::ALL); $sql_data .= $dump->export('tbl_entries_%', MySQLDump::ALL); ## Grab the data foreach ($tables as $t) { $sql_data .= $dump->export($t, MySQLDump::DATA_ONLY); } $sql_data = str_replace('`' . Symphony::Configuration()->get('tbl_prefix', 'database'), '`tbl_', $sql_data); $config_string = NULL; $config = Symphony::Configuration()->get(); unset($config['symphony']['build']); unset($config['symphony']['cookie_prefix']); unset($config['general']['useragent']); unset($config['file']['write_mode']); unset($config['directory']['write_mode']); unset($config['database']['host']); unset($config['database']['port']); unset($config['database']['user']); unset($config['database']['password']); unset($config['database']['db']); unset($config['database']['tbl_prefix']); unset($config['region']['timezone']); foreach ($config as $group => $set) { foreach ($set as $key => $val) { $config_string .= "\t\t\$conf['{$group}']['{$key}'] = '{$val}';" . self::CRLF; } } $install_template = str_replace(array('<!-- VERSION -->', '<!-- CONFIGURATION -->'), array(Symphony::Configuration()->get('version', 'symphony'), trim($config_string)), file_get_contents(dirname(__FILE__) . '/lib/installer.tpl')); $archive = new ZipArchive(); $res = $archive->open(TMP . '/ensemble.tmp.zip', ZipArchive::CREATE); if ($res === TRUE) { $this->__addFolderToArchive($archive, EXTENSIONS, DOCROOT); $this->__addFolderToArchive($archive, SYMPHONY, DOCROOT); $this->__addFolderToArchive($archive, WORKSPACE, DOCROOT); $archive->addFromString('install.php', $install_template); $archive->addFromString('install.sql', $sql_schema); $archive->addFromString('workspace/install.sql', $sql_data); $archive->addFile(DOCROOT . '/index.php', 'index.php'); $readme_files = glob(DOCROOT . '/README.*'); if (is_array($readme_files) && !empty($readme_files)) { foreach ($readme_files as $filename) { $archive->addFile($filename, basename($filename)); } } if (is_file(DOCROOT . '/README')) { $archive->addFile(DOCROOT . '/README', 'README'); } if (is_file(DOCROOT . '/LICENCE')) { $archive->addFile(DOCROOT . '/LICENCE', 'LICENCE'); } if (is_file(DOCROOT . '/update.php')) { $archive->addFile(DOCROOT . '/update.php', 'update.php'); } } $archive->close(); header('Content-type: application/octet-stream'); header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header(sprintf('Content-disposition: attachment; filename=%s-ensemble.zip', Lang::createFilename(Symphony::Configuration()->get('sitename', 'general')))); header('Pragma: no-cache'); readfile(TMP . '/ensemble.tmp.zip'); unlink(TMP . '/ensemble.tmp.zip'); exit; }
private function __dump($context) { $sql_schema = $sql_data = NULL; require_once dirname(__FILE__) . '/lib/class.mysqldump.php'; $dump = new MySQLDump(Symphony::Database()); $rows = Symphony::Database()->fetch("SHOW TABLES LIKE 'tbl_%';"); $rows = array_map(create_function('$x', 'return array_values ($x);'), $rows); $tables = array_map(create_function('$x', 'return $x[0];'), $rows); $mode = NULL; $mode = isset($_POST['action']['dump']['authors']) ? 'authors' : 'data'; if ($mode == NULL) { return; } $filename = $this->generateFilename($mode); foreach ($tables as $table) { $table = str_replace(Symphony::Configuration()->get('tbl_prefix', 'database'), 'tbl_', $table); if ($mode == 'authors') { switch ($table) { case 'tbl_authors': case 'tbl_forgotpass': $sql_data .= $dump->export($table, MySQLDump::ALL); break; case 'tbl_sessions': $sql_data .= $dump->export($table, MySQLDump::STRUCTURE_ONLY); break; default: // ignore everything but the authors break; } } elseif ($mode == 'data') { switch ($table) { case 'tbl_authors': // ignore authors // ignore authors case 'tbl_forgotpass': case 'tbl_sessions': break; case 'tbl_cache': case 'tbl_search_index': case 'tbl_search_index_entry_keywords': case 'tbl_search_index_keywords': case 'tbl_search_index_logs': $sql_data .= $dump->export($table, MySQLDump::STRUCTURE_ONLY); break; default: $sql_data .= $dump->export($table, MySQLDump::ALL); } } } if (Symphony::Configuration()->get('dump', 'dump_db') === 'download') { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: binary"); header("Content-Disposition: attachment; filename=" . $mode . ".sql"); echo $sql_data; die; } elseif (Symphony::Configuration()->get('dump', 'dump_db') === 'text') { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: text/plain; charset=UTF-8"); echo $sql_data; die; } else { if (FALSE !== @file_put_contents(DOCROOT . $this->path . '/' . $filename, $sql_data)) { Administration::instance()->Page->pageAlert(__('%s successfully dumped into <code>%s/%s</code>.', array(__(ucfirst($mode)), $this->path, $filename)), Alert::SUCCESS); Symphony::Configuration()->set('last_sync', date('c'), 'dump_db'); Symphony::Configuration()->write(); } else { Administration::instance()->Page->pageAlert(__('An error occurred while trying to write <code>%s/%s</code>.', array($this->path, $filename)), Alert::ERROR); } } }