Esempio n. 1
0
 /** -------------------------------------------
     /**  Do SQL backup
     /** -------------------------------------------*/
 function do_sql_backup($type = '')
 {
     global $IN, $DSP, $DB, $LANG, $LOC;
     return;
     if (!$DSP->allowed_group('can_admin_utilities')) {
         return $DSP->no_access_message();
     }
     // Names of tables we do not want the data backed up from
     if ($IN->GBL('ignore_noncritical', 'POST') == 'y') {
         $ignore = array('exp_security_hashes ', 'exp_sessions', 'exp_cp_log', 'exp_revision_tracker', 'exp_search', 'exp_email_console_cache');
     } else {
         $ignore = array();
     }
     /** ---------------------------------------------------------
         /**  Are we backing up the full database or separate tables?
         /** ---------------------------------------------------------*/
     if ($type == '') {
         $type = $_POST['type'];
         $file = $IN->GBL('file', 'POST') == 'y' ? TRUE : FALSE;
     } else {
         switch ($_POST['table_action']) {
             case 'BACKUP_F':
                 $type = 'text';
                 $file = TRUE;
                 break;
             case 'BACKUP_Z':
                 $type = 'zip';
                 $file = TRUE;
                 break;
             case 'BACKUP_G':
                 $type = 'gzip';
                 $file = TRUE;
                 break;
             default:
                 $type = 'text';
                 $file = FALSE;
                 break;
         }
     }
     /** ------------------------------------------------------------
         /**  Build the output headers only if we are downloading a file
         /** ------------------------------------------------------------*/
     ob_start();
     if ($file) {
         // Assign the name of the of the backup file
         $now = $LOC->set_localized_time();
         $filename = $DB->database . '_' . date('y', $now) . date('m', $now) . date('d', $now);
         switch ($type) {
             case 'zip':
                 if (!@function_exists('gzcompress')) {
                     return $DSP->error_message($LANG->line('unsupported_compression'));
                 }
                 $ext = 'zip';
                 $mime = 'application/x-zip';
                 break;
             case 'gzip':
                 if (!@function_exists('gzencode')) {
                     return $DSP->error_message($LANG->line('unsupported_compression'));
                 }
                 $ext = 'gz';
                 $mime = 'application/x-gzip';
                 break;
             default:
                 $ext = 'sql';
                 if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE") || strstr($_SERVER['HTTP_USER_AGENT'], "OPERA")) {
                     $mime = 'application/octetstream';
                 } else {
                     $mime = 'application/octet-stream';
                 }
                 break;
         }
         if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
             header('Content-Type: ' . $mime);
             header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
             header('Expires: 0');
             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
             header('Pragma: public');
         } else {
             header('Content-Type: ' . $mime);
             header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
             header('Expires: 0');
             header('Pragma: no-cache');
         }
     } else {
         echo $DSP->qdiv('tableHeading', $LANG->line('sql_backup'));
         echo '<pre>';
     }
     /** -------------------------------------------
         /**  Fetch the table names
         /** -------------------------------------------*/
     $DB->fetch_fields = TRUE;
     // Individual tables
     if (isset($_POST['table_action'])) {
         foreach ($_POST['table'] as $key => $val) {
             $tables[] = $key;
         }
     } else {
         $tables = $DB->fetch_tables();
     }
     $i = 0;
     foreach ($tables as $table) {
         /** -------------------------------------------
             /**  Fetch the table structure
             /** -------------------------------------------*/
         echo NL . NL . '#' . NL . '# TABLE STRUCTURE FOR: ' . $table . NL . '#' . NL . NL;
         echo 'DROP TABLE IF EXISTS ' . $table . ';' . NL . NL;
         $query = $DB->query("SHOW CREATE TABLE `" . $DB->database . '`.' . $table);
         foreach ($query->result['0'] as $val) {
             if ($i++ % 2) {
                 //$val = str_replace('`', '', $val).NL.NL;
                 //$val = preg_replace('/CREATE(.*\))/s', "CREATE\\1;", $val);
                 //$val = str_replace('TYPE=MyISAM', '',	$val);
                 echo $val . ';' . NL . NL;
             }
         }
         if (!in_array($table, $ignore)) {
             /** -------------------------------------------
                 /**  Fetch the data in the table
                 /** -------------------------------------------*/
             $query = $DB->query("SELECT * FROM {$table}");
             if ($query->num_rows == 0) {
                 continue;
             }
             /** -------------------------------------------
                 /**  Assign the field name
                 /** -------------------------------------------*/
             $fields = '';
             foreach ($query->fields as $f) {
                 $fields .= $f . ', ';
             }
             $fields = preg_replace("/, \$/", "", $fields);
             /** -------------------------------------------
                 /**  Assign the value in each field
                 /** -------------------------------------------*/
             foreach ($query->result as $val) {
                 $values = '';
                 foreach ($val as $v) {
                     $v = str_replace(array("", "\n", "\r", ""), array('\\0', '\\n', '\\r', '\\Z'), $v);
                     $v = str_replace(array("\n", "\r", "\t"), array('\\n', '\\r', '\\t'), $v);
                     $v = str_replace('\\', '\\\\', $v);
                     $v = str_replace('\'', '\\\'', $v);
                     $v = str_replace('\\\\n', '\\n', $v);
                     $v = str_replace('\\\\r', '\\r', $v);
                     $v = str_replace('\\\\t', '\\t', $v);
                     $values .= "'" . $v . "'" . ', ';
                 }
                 $values = preg_replace("/, \$/", "", $values);
                 if ($file == FALSE) {
                     $values = htmlspecialchars($values);
                 }
                 // Build the INSERT string
                 echo 'INSERT INTO ' . $table . ' (' . $fields . ') VALUES (' . $values . ');' . NL;
             }
         }
     }
     // END WHILE LOOP
     if ($file == FALSE) {
         echo '</pre>';
     }
     $buffer = ob_get_contents();
     ob_end_clean();
     /** -------------------------------------------
         /**  Create the selected output file
         /** -------------------------------------------*/
     if ($file) {
         switch ($type) {
             case 'zip':
                 $zip = new Zipper();
                 $zip->add_file($buffer, $filename . '.sql');
                 echo $zip->output_zipfile();
                 break;
             case 'gzip':
                 echo gzencode($buffer);
                 break;
             default:
                 echo $buffer;
                 break;
         }
         exit;
     } else {
         $DSP->title = $LANG->line('utilities');
         $DSP->crumb = $DSP->anchor(BASE . AMP . 'C=admin' . AMP . 'area=utilities', $LANG->line('utilities')) . $DSP->crumb_item($LANG->line('utilities'));
         $DSP->body = $buffer;
     }
 }
Esempio n. 2
0
 /** -----------------------------
     /**  Export templates
     /** -----------------------------*/
 function export_templates($type = 'zip')
 {
     global $IN, $SESS, $DSP, $DB, $LOC, $FNS, $LANG;
     if (!($group_id = $IN->GBL('id'))) {
         return false;
     }
     /** --------------------------------------
         /**  Is the user allowed to export?
         /** --------------------------------------*/
     if ($SESS->userdata['tmpl_group_id'] != 0) {
         $group_id = $SESS->userdata['tmpl_group_id'];
     }
     if (!$this->template_access_privs(array('group_id' => $group_id))) {
         return $DSP->no_access_message();
     }
     /** --------------------------------------
         /**  No templates?  Bounce them back
         /** --------------------------------------*/
     if (!isset($_POST['template'])) {
         return $this->export_templates_form($group_id);
     }
     /** --------------------------------------
         /**  Is the selected compression supported?
         /** --------------------------------------*/
     if (!@function_exists('gzcompress') && $type == 'zip') {
         return $DSP->error_message($LANG->line('unsupported_compression'));
     }
     /** --------------------------------------
         /**  Assign the name of the of the folder
         /** --------------------------------------*/
     $query = $DB->query("SELECT group_name, is_site_default FROM exp_template_groups WHERE group_id = '{$group_id}'");
     $directory = $query->row['group_name'] . '_tmpl';
     /** --------------------------------------
         /**  Fetch the template data and zip it
         /** --------------------------------------*/
     if ($type == 'default' && sizeof($_POST['template']) == 1) {
         $directory = $query->row['group_name'] . '_';
         $query = $DB->query("SELECT template_data, template_name, template_type FROM exp_templates WHERE template_id = '" . $DB->escape_str(array_pop(array_keys($_POST['template']))) . "'");
         $output = (!isset($_POST['export_data']) or $_POST['export_data'] == '') ? $query->row['template_data'] : stripslashes($_POST['export_data']);
         $directory .= $query->row['template_name'];
         switch ($query->row['template_type']) {
             case 'css':
                 $suffix = 'css';
                 $content_type = 'application/force-download';
                 break;
             case 'js':
                 $suffix = 'js';
                 $content_type = 'application/force-download';
                 break;
             case 'rss':
                 $suffix = 'xml';
                 $content_type = 'application/force-download';
                 break;
             case 'static':
                 $suffix = 'txt';
                 $content_type = 'application/force-download';
                 break;
             case 'webpage':
                 $suffix = 'html';
                 $content_type = 'application/force-download';
                 break;
             case 'xml':
                 $suffix = 'xml';
                 $content_type = 'application/force-download';
                 break;
             default:
                 $suffix = 'txt';
                 $content_type = 'application/force-download';
                 break;
         }
     } else {
         require PATH_CP . 'cp.utilities' . EXT;
         $zip = new Zipper();
         $temp_data = array();
         // $zip->add_dir($directory.'/');
         foreach ($_POST['template'] as $key => $val) {
             $query = $DB->query("SELECT template_data, template_name FROM exp_templates WHERE template_id = '" . $DB->escape_str($key) . "'");
             $zip->add_file($query->row['template_data'], $directory . '/' . $query->row['template_name'] . '.txt');
         }
         $suffix = 'zip';
         $content_type = 'application/x-zip';
     }
     /** -------------------------------------------
         /**  Write out the headers
         /** -------------------------------------------*/
     ob_start();
     if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
         header('Content-Type: ' . $content_type);
         header('Content-Disposition: inline; filename="' . $directory . '.' . $suffix . '"');
         header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Pragma: public');
     } else {
         header('Content-Type: ' . $content_type);
         header('Content-Disposition: attachment; filename="' . $directory . '.' . $suffix . '"');
         header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
         header('Pragma: no-cache');
     }
     if (isset($output)) {
         echo $output;
     } else {
         echo $zip->output_zipfile();
     }
     $buffer = ob_get_contents();
     ob_end_clean();
     echo $buffer;
     exit;
 }