Example #1
0
function sqlbackup($ip, $sql, $username, $password)
{
    global $meurl;
    if (class_exists('ZipArchive')) {
        maintop("MySQL备份");
        $database = $sql;
        //数据库名
        $options = array('hostname' => $ip, 'charset' => 'utf8', 'filename' => $database . '.sql', 'username' => $username, 'password' => $password);
        mysql_connect($options['hostname'], $options['username'], $options['password']) or die("不能连接数据库!");
        mysql_select_db($database) or die("数据库名称错误!");
        mysql_query("SET NAMES '{$options['charset']}'");
        $tables = list_tables($database);
        $filename = sprintf($options['filename'], $database);
        $fp = fopen($filename, 'w');
        foreach ($tables as $table) {
            dump_table($table, $fp);
        }
        fclose($fp);
        //压缩sql文件
        if (file_exists('mysql.zip')) {
            unlink('mysql.zip');
        }
        $file_name = $options['filename'];
        $zip = new ZipArchive();
        $res = $zip->open('mysql.zip', ZipArchive::CREATE);
        if ($res === TRUE) {
            $zip->addfile($file_name);
            $zip->close();
            //删除服务器上的sql文件
            unlink($file_name);
            echo '<div class="box">数据库导出并压缩完成!' . " <a href=\"" . $meurl . "?op=home&folder=" . $_SESSION['folder'] . "\">返回上次浏览的文件夹</a></div>\n";
        } else {
            printerror('无法压缩文件!');
        }
        exit;
        mainbottom();
    } else {
        printerror('此服务器上的PHP不支持ZipArchive,无法压缩文件!');
    }
}
    /**
     * Menu export render
     * @return void
     */
    function submenu_export()
    {
        global $wpdb;
        if (!is_dir(plugin_dir_path(__FILE__) . '../tmp')) {
            mkdir(plugin_dir_path(__FILE__) . '../tmp');
            chmod(plugin_dir_path(__FILE__) . '../tmp', 0747);
        }
        $destination = plugin_dir_path(__FILE__) . '../tmp/export_visual_chat.zip';
        $zip = new ZipArchive();
        if (file_exists($destination)) {
            unlink($destination);
        }
        if ($zip->open($destination, ZipArchive::CREATE) !== true) {
            return false;
        }
        $jsonExport = array();
        $table_name = $wpdb->prefix . "vcht_settings";
        $settings = $wpdb->get_results("SELECT * FROM {$table_name} WHERE id=1 LIMIT 1");
        $jsonExport['settings'] = $settings;
        $table_name = $wpdb->prefix . "vcht_logs";
        $steps = array();
        foreach ($wpdb->get_results("SELECT * FROM {$table_name}") as $key => $row) {
            $steps[] = $row;
        }
        $jsonExport['logs'] = $steps;
        $table_name = $wpdb->prefix . "vcht_messages";
        $items = array();
        foreach ($wpdb->get_results("SELECT * FROM {$table_name}") as $key => $row) {
            $items[] = $row;
        }
        $jsonExport['messages'] = $items;
        $table_name = $wpdb->prefix . "vcht_texts";
        $items = array();
        foreach ($wpdb->get_results("SELECT * FROM {$table_name}") as $key => $row) {
            $items[] = $row;
        }
        $jsonExport['texts'] = $items;
        $fp = fopen(plugin_dir_path(__FILE__) . '../tmp/export_visual_chat.json', 'w');
        fwrite($fp, json_encode($jsonExport));
        fclose($fp);
        $zip->addfile(plugin_dir_path(__FILE__) . '../tmp/export_visual_chat.json', 'export_visual_chat.json');
        $zip->close();
        ?>
        <div class="wrap wpeExport">
            <h2>Export data</h2>
            <p>
                Export all this plugin datas to a zip file will can be imported on another website.
            </p>
            <p>
                <a download class="button-primary" href="<?php 
        echo esc_url(trailingslashit(plugins_url('/', $this->parent->file))) . 'tmp/export_visual_chat.zip';
        ?>
">Export</a>
            </p>
        </div>
        <?php 
    }
 }
 //CRIA E ABRE ARQUIVO ZIP QUE SERÁ ENVIADO POR E-MAIL PARA O RESPONSÁVEL (OU ENVIADO PARA FTP DE FORMA OPCIONAL - VIDE CÓDIGO COMENTADO MAIS ABAIXO)
 $zip = new ZipArchive();
 $criou = $zip->open($caminhoServidor . "/{$EventoSemAcent}/certificados.zip", ZipArchive::CREATE);
 //CABEÇALHO DO E-MAIL (ALTERAR CAMPOS FROM E RETURN-PATH COM OS SEUS VALORES)
 $headers = "MIME-Version: 1.1\r\n";
 $headers .= "Content-type: text/plain; charset=utf-8\r\n";
 $headers .= "From: SEU-EMAIL <*****@*****.**>\r\n";
 // remetente
 $headers .= "Return-Path: seu@email.br\r\n";
 // return-path
 //GERA CERTIFICADO PARA CADA NOME FORNECIDO, INSERE NO ARQUIVO ZIP, ENVIA POR E-MAIL PARA O ALUNO E INSERE NO BANCO DE DADOS
 for ($i = 0; $i < $qtd_nomes; $i++) {
     geraCertificado($Data, $NomeAluno[$i], $Evento, $orador, $CargaHoraria, $organizadorEvento, $caminhoServidor);
     $NomeAlunoSemAcent = remove_acentuacao($NomeAluno[$i], true);
     $zip->addfile($caminhoServidor . "/{$EventoSemAcent}/" . $NomeAlunoSemAcent . ".pdf", $NomeAlunoSemAcent . ".pdf");
     $hash = md5($NomeAlunoSemAcent . $Evento);
     $data = date("y-m-d H:i:s");
     //ENVIA EMAIL, SE INFORMADO
     if (isset($EmailAluno) && $EmailAluno[$i] != "") {
         $msg = "Olá {$NomeAluno[$i]},\n\t\t\t\t\n\nSeu certificado está disponível no link abaixo.\n\t\t\t\t\n\nDownload do Certificado: {$downloadURL}/{$EventoSemAcent}/{$NomeAlunoSemAcent}.pdf\n\t\t\t\t\n\nPara verificar a veracidade dos certificados, acesse o link: {$downloadURL}\n\t\t\t\t\n\nObrigado pela sua participação!";
         mail($EmailAluno[$i], "Certificado - {$Evento}", $msg, $headers);
         $emailAluno = $EmailAluno[$i];
     } else {
         $emailAluno = "";
     }
     $sql = "INSERT INTO certificado (evento,organizador_evento,nome_participante,email_participante,hash_validacao,data) \n\t\t\t\t\t\tVALUES ('" . utf8_decode($Evento) . "','" . utf8_decode($organizadorEvento) . "','" . utf8_decode($NomeAluno[$i]) . "','" . $emailAluno . "','{$hash}','{$data}')";
     $conn->query($sql);
 }
 $zip->close();
 /**
function sqlbackup($ip, $sql, $username, $password)
{
    global $meurl;
    if (class_exists('ZipArchive')) {
        maintop("MySQL Backup");
        $database = $sql;
        $options = array('hostname' => $ip, 'charset' => 'utf8', 'filename' => $database . '.sql', 'username' => $username, 'password' => $password);
        mysql_connect($options['hostname'], $options['username'], $options['password']) or die("Fail to connect DB!");
        mysql_select_db($database) or die("Database name error!");
        mysql_query("SET NAMES '{$options['charset']}'");
        $tables = list_tables($database);
        $filename = sprintf($options['filename'], $database);
        $fp = fopen($filename, 'w');
        foreach ($tables as $table) {
            dump_table($table, $fp);
        }
        fclose($fp);
        if (file_exists('mysql.zip')) {
            unlink('mysql.zip');
        }
        $file_name = $options['filename'];
        $zip = new ZipArchive();
        $res = $zip->open('mysql.zip', ZipArchive::CREATE);
        if ($res === TRUE) {
            $zip->addfile($file_name);
            $zip->close();
            unlink($file_name);
            echo 'Database Export and compression jobs done!' . " <a href=\"" . $meurl . "?op=root&folder=" . $_SESSION['folder'] . "\">Back</a>\n";
        } else {
            printerror('Unable to compress files!');
        }
        exit;
        mainbottom();
    } else {
        printerror('PHP on this server does not support ZipArchive, unable to extract the zip files!');
    }
}