Exemple #1
0
function PMBP_save_global_conf($global_conf_path = "")
{
    global $CONF;
    global $PMBP_SYS_VAR;
    // to ensure that all configuration settings are saved
    @ignore_user_abort(TRUE);
    // create content for global.conf
    $file = "<?php\n\n// This file is automatically generated and modified by phpMyBackupPro " . PMBP_VERSION . "\n\n";
    if (is_array($CONF)) {
        foreach ($CONF as $item => $conf) {
            // don't save multi server settings to gloabl_conf.php
            if ($item == "sql_host_s" || $item == "sql_user_s" || $item == "sql_passwd_s" || $item == "sql_db_s") {
                continue;
            }
            // don't store sql data in mu mode
            if ($_SESSION['multi_user_mode'] && ($item == "sql_passwd" || $item == "sql_host" || $item == "sql_user" || $item == "sql_db")) {
                continue;
            }
            // update $_SESSION['sql_host_org'] etc. if new sql data were entered on the config page
            if (basename($_SERVER['SCRIPT_NAME']) == "config.php") {
                $_SESSION['sql_host_org'] = $CONF['sql_host'];
                $_SESSION['sql_user_org'] = $CONF['sql_user'];
                $_SESSION['sql_passwd_org'] = $CONF['sql_passwd'];
                $_SESSION['sql_db_org'] = $CONF['sql_db'];
            }
            // save current $CONF['sql_...'] values only if we use the multi server mode
            if ($item == "sql_host" && count($CONF['sql_host_s'])) {
                $file .= "\$CONF['" . $item . "']=\"" . $_SESSION['sql_host_org'] . "\";\n";
            } elseif ($item == "sql_user" && count($CONF['sql_host_s'])) {
                $file .= "\$CONF['" . $item . "']=\"" . $_SESSION['sql_user_org'] . "\";\n";
            } elseif ($item == "sql_passwd" && count($CONF['sql_host_s'])) {
                $file .= "\$CONF['" . $item . "']=\"" . $_SESSION['sql_passwd_org'] . "\";\n";
            } elseif ($item == "sql_db" && count($CONF['sql_host_s'])) {
                $file .= "\$CONF['" . $item . "']=\"" . $_SESSION['sql_db_org'] . "\";\n";
            } else {
                // save the current values for all other settings
                $file .= "\$CONF['" . $item . "']=\"" . $conf . "\";\n";
            }
        }
    }
    // unset 'last_scheduled_' values in sys vars which no longer belong to an account
    foreach ($PMBP_SYS_VAR as $key => $value) {
        if (substr($key, 0, 15) == "last_scheduled_" && substr($key, 15) >= count($CONF['sql_host_s'])) {
            unset($PMBP_SYS_VAR[$key]);
        }
    }
    // add system variables
    $file .= "\n";
    foreach ($PMBP_SYS_VAR as $item => $sys_var) {
        $file .= "\$PMBP_SYS_VAR['" . $item . "']=\"" . $sys_var . "\";\n";
    }
    $file .= "\n?>";
    if (!$global_conf_path) {
        $global_conf_path = PMBP_GLOBAL_CONF;
    }
    return PMBP_save_to_file($global_conf_path, FALSE, $file, "w");
}
Exemple #2
0
function PMBP_dump($CONF, $PMBP_SYS_VAR, $db, $tables, $data, $drop, $zip, $comment)
{
    set_time_limit(0);
    $error = false;
    $time = date("Ymd");
    if ($zip == "gzip") {
        $backupfile = $db . "." . $time . "_utf8.sql.gz";
    } else {
        $backupfile = $db . "." . $time . "_utf8.sql";
    }
    $backupfile = PMBP_EXPORT_DIR . $backupfile;
    $con = @mysqli_connect($CONF['sql_host'], $CONF['sql_user'], $CONF['sql_passwd']);
    if (!$con) {
        return "DB_ERROR";
    }
    mysqli_set_charset($con, "utf8");
    mysqli_query($con, "SET SESSION sql_mode='';");
    //create comment
    $hout = "# MySQL dump of database '" . $db . "' on host '" . $CONF['sql_host'] . "'\n";
    $hout .= "# backup date and time: " . date("d.m.Y H:i:s") . "\n";
    $hout .= "# " . PMBP_WEBSITE . "\n\n";
    // write users comment
    if ($comment) {
        $hout .= "# comment:\n";
        $comment = preg_replace("'\n'", "\n# ", "# " . $comment);
        foreach (explode("\n", $comment) as $line) {
            $hout .= $line . "\n";
        }
        $hout .= "\n";
    }
    // print "use database" if more than one databas is available
    if (count(PMBP_get_db_list($CONF)) > 1) {
        $hout .= "CREATE DATABASE IF NOT EXISTS `" . $db . "`;\n\n";
        $hout .= "USE `" . $db . "`;\n";
    }
    PMBP_save_to_file($backupfile, $zip, $hout, "a");
    $hout = "";
    // select db
    mysqli_select_db($con, $db);
    // get auto_increment values and names of all tables
    $res = mysqli_query($con, "show table status");
    $all_tables = array();
    while ($row = mysqli_fetch_array($res)) {
        if ($row["Comment"] == "VIEW") {
            continue;
        }
        $all_tables[] = $row;
    }
    // get table structures
    foreach ($all_tables as $table) {
        $res1 = mysqli_query($con, "SHOW CREATE TABLE `" . $table['Name'] . "`");
        $tmp = mysqli_fetch_array($res1);
        $table_sql[$table['Name']] = $tmp["Create Table"];
    }
    // find foreign keys
    $fks = array();
    if (isset($table_sql)) {
        foreach ($table_sql as $tablenme => $table) {
            $tmp_table = $table;
            // save all tables, needed for creating this table in $fks
            while (($ref_pos = strpos($tmp_table, " REFERENCES ")) > 0) {
                $tmp_table = substr($tmp_table, $ref_pos + 12);
                $ref_pos = strpos($tmp_table, "(");
                $fks[$tablenme][] = substr($tmp_table, 0, $ref_pos);
            }
        }
    }
    foreach ($all_tables as $row) {
        $tablename = $row['Name'];
        $auto_incr[$tablename] = $row['Auto_increment'];
        $kout = "\n\n";
        if ($tables) {
            if ($drop) {
                $kout .= "DROP TABLE IF EXISTS `" . $tablename . "`;\n\n";
            }
            $kout .= $table_sql[$tablename];
            // add auto_increment value
            if ($auto_incr[$tablename] and strpos($kout, "AUTO_INCREMENT") === false) {
                $kout .= " AUTO_INCREMENT=" . $auto_incr[$tablename];
            }
            $kout .= ";";
        }
        $kout .= "\n\n\n";
        PMBP_save_to_file($backupfile, $zip, $kout, "a");
        $kout = "";
        if ($error) {
            @unlink("./" . PMBP_EXPORT_DIR . $backupfile);
            return false;
        }
        // export data
        if ($data && !$error) {
            // check if field types are NULL or NOT NULL
            #$res3 = mysqli_query($con, "show columns from `" . $tablename . "`");
            $res2 = mysqli_query($con, "SELECT * FROM `" . $tablename . "`", MYSQLI_USE_RESULT);
            while ($row2 = mysqli_fetch_row($res2)) {
                $sout = "INSERT INTO `" . $tablename . "` VALUES (";
                // run through each field
                foreach ($row2 as $k => $v) {
                    if (is_null($v)) {
                        $sout .= "null";
                    } else {
                        $sout .= "'" . mysqli_real_escape_string($con, $v) . "'";
                    }
                    if ($k < count($row2) - 1) {
                        $sout .= ", ";
                    }
                }
                $sout .= ");\n";
                PMBP_save_to_file($backupfile, $zip, $sout, "a");
                $sout = "";
            }
            mysqli_free_result($res2);
        }
    }
    $backupfile = PMBP_save_to_file($backupfile, $zip, "", "a");
    if ($backupfile) {
        if ($zip != "zip") {
            return basename($backupfile);
        }
    } else {
        @unlink("./" . $backupfile);
        return false;
    }
    // create zip file in file system
    include_once "pclzip.lib.php";
    $pclzip = new PclZip($backupfile . ".zip");
    $pclzip->create($backupfile, PCLZIP_OPT_REMOVE_PATH, PMBP_EXPORT_DIR);
    // remove temporary plain text backup file used for zip compression
    @unlink(substr($backupfile, 0, strlen($backupfile)));
    if ($pclzip->error_code == 0) {
        return basename($backupfile) . ".zip";
    } else {
        // print pclzip error message
        echo "<div class=\"red\">pclzip: " . $pclzip->error_string . "</div>";
        // remove temporary plain text backup file
        @unlink(substr($backupfile, 0, strlen($backupfile) - 4));
        @unlink("./" . $backupfile);
        return FALSE;
    }
}
Exemple #3
0
 printf(PMBP_EXS_INCL, $_POST['path'] . "???.php");
 echo ":\n<br><textarea name=\"code\" rows=\"16\" cols=\"120\" readOnly>" . $result . "</textarea>\n<br><br>";
 echo "<form name=\"save\" action=\"scheduled.php\" method=\"post\">\n";
 // list all post variables as hidden fields
 foreach ($_POST as $key => $value) {
     if ($key != "db" && $key != "dirs") {
         echo "<input type=\"hidden\" name=\"" . $key . "\" value=\"" . $value . "\">\n";
     } elseif (is_array($_POST[$key])) {
         foreach ($value as $dbname) {
             echo "<input type=\"hidden\" name=\"" . $key . "[]\" value=\"" . $dbname . "\">\n";
         }
     }
 }
 // save file including the backup script
 if (isset($_POST['filename'])) {
     if (PMBP_save_to_file($_POST['path'] . $_POST['filename'], "", $result, "w")) {
         echo PMBP_addOutput(EX_SAVED . " " . $_POST['path'] . $_POST['filename'], "green_left");
         @chmod($_POST['path'] . $_POST['filename'], 0644);
         // save specific settings for scheduled backups
         if ($PMBP_SYS_VAR['EXS_scheduled_file'] != $_POST['filename']) {
             $PMBP_SYS_VAR['EXS_scheduled_file'] = $_POST['filename'];
         }
     } else {
         echo PMBP_addOutput(C_WRITE . " " . $_POST['path'] . $_POST['filename'], "red_left");
     }
 }
 echo PMBP_EXS_SAVE . ":<br>\n";
 echo $_POST['path'] . "<input type=\"text\" name=\"filename\" value=\"" . $PMBP_SYS_VAR['EXS_scheduled_file'] . "\">&nbsp;";
 echo "<input type=\"submit\" value=\"" . C_SAVE . "\">";
 if ($PMBP_SYS_VAR['EXS_scheduled_file'] != "???.php") {
     echo " (<a href=\"\">" . PMBP_pop_up("get_file.php?view=" . $_POST['path'] . $PMBP_SYS_VAR['EXS_scheduled_file'], B_VIEW, "view") . "</a>)";
Exemple #4
0
function PMBP_dump($CONF, $PMBP_SYS_VAR, $db, $tables, $data, $drop, $zip, $comment)
{
    #global $CONF;
    #global $PMBP_SYS_VAR;
    $error = false;
    // set max string size before writing to file
    #if (@ini_get("memory_limit"))
    #	$max_size = Util::getMaxMemory() - memory_get_peak_usage(true) - 1024 * 1024  * 20;
    #else
    $max_size = 1024 * 1024 * 20;
    #echo "<br />";
    #die(Util::formatByte($max_size));
    #die();
    // set backupfile name
    $time = date("Ymd");
    if ($zip == "gzip") {
        $backupfile = $db . "." . $time . "_utf8.sql.gz";
    } else {
        $backupfile = $db . "." . $time . "_utf8.sql";
    }
    $backupfile = PMBP_EXPORT_DIR . $backupfile;
    $con = @mysqli_connect($CONF['sql_host'], $CONF['sql_user'], $CONF['sql_passwd']);
    if (!$con) {
        return "DB_ERROR";
    }
    mysqli_set_charset($con, "utf8");
    mysqli_query($con, "SET SESSION sql_mode='';");
    //create comment
    $out = "# MySQL dump of database '" . $db . "' on host '" . $CONF['sql_host'] . "'\n";
    $out .= "# backup date and time: " . strftime($CONF['date'], $time) . "\n";
    $out .= "# built by phpMyBackupPro " . PMBP_VERSION . "\n";
    $out .= "# " . PMBP_WEBSITE . "\n\n";
    // write users comment
    if ($comment) {
        $out .= "# comment:\n";
        $comment = preg_replace("'\n'", "\n# ", "# " . $comment);
        foreach (explode("\n", $comment) as $line) {
            $out .= $line . "\n";
        }
        $out .= "\n";
    }
    // print "use database" if more than one databas is available
    if (count(PMBP_get_db_list($CONF)) > 1) {
        $out .= "CREATE DATABASE IF NOT EXISTS `" . $db . "`;\n\n";
        $out .= "USE `" . $db . "`;\n";
    }
    // select db
    mysqli_select_db($con, $db);
    // get auto_increment values and names of all tables
    $res = mysqli_query($con, "show table status");
    $all_tables = array();
    while ($row = mysqli_fetch_array($res)) {
        if ($row["Comment"] == "VIEW") {
            continue;
        }
        $all_tables[] = $row;
    }
    // get table structures
    foreach ($all_tables as $table) {
        $res1 = mysqli_query($con, "SHOW CREATE TABLE `" . $table['Name'] . "`");
        $tmp = mysqli_fetch_array($res1);
        $table_sql[$table['Name']] = $tmp["Create Table"];
    }
    // find foreign keys
    $fks = array();
    if (isset($table_sql)) {
        foreach ($table_sql as $tablenme => $table) {
            $tmp_table = $table;
            // save all tables, needed for creating this table in $fks
            while (($ref_pos = strpos($tmp_table, " REFERENCES ")) > 0) {
                $tmp_table = substr($tmp_table, $ref_pos + 12);
                $ref_pos = strpos($tmp_table, "(");
                $fks[$tablenme][] = substr($tmp_table, 0, $ref_pos);
            }
        }
    }
    // order $all_tables and check for ring constraints
    #$all_tables_copy = $all_tables;
    #$all_tables = PMBP_order_sql_tables($all_tables, $fks);
    #$ring_contraints = false;
    // ring constraints found
    #if ($all_tables === false) {
    #	$ring_contraints = true;
    #	$all_tables = $all_tables_copy;
    #	$out.="\n# ring constraints workaround\n";
    #	$out.="SET FOREIGN_KEY_CHECKS=0;\n";
    #	$out.="SET AUTOCOMMIT=0;\n";
    #	$out.="START TRANSACTION;\n";
    #}
    #unset($all_tables_copy);
    // as long as no error occurred
    if ($error) {
        @unlink("./" . $backupfile);
        return false;
    }
    foreach ($all_tables as $row) {
        $tablename = $row['Name'];
        $auto_incr[$tablename] = $row['Auto_increment'];
        // don't backup tables in $PMBP_SYS_VAR['except_tables']
        if (in_array($tablename, explode(",", $PMBP_SYS_VAR['except_tables']))) {
            continue;
        }
        $out .= "\n\n";
        // export tables
        if ($tables) {
            $out .= "### structure of table `" . $tablename . "` ###\n\n";
            if ($drop) {
                $out .= "DROP TABLE IF EXISTS `" . $tablename . "`;\n\n";
            }
            $out .= $table_sql[$tablename];
            // add auto_increment value
            if ($auto_incr[$tablename]) {
                $out .= " AUTO_INCREMENT=" . $auto_incr[$tablename];
            }
            $out .= ";";
        }
        $out .= "\n\n\n";
        if ($error) {
            @unlink("./" . PMBP_EXPORT_DIR . $backupfile);
            return false;
        }
        // export data
        if ($data && !$error) {
            $out .= "### data of table `" . $tablename . "` ###\n\n";
            // check if field types are NULL or NOT NULL
            #$res3 = mysqli_query($con, "show columns from `" . $tablename . "`");
            $res2 = mysqli_query($con, "select * from `" . $tablename . "`");
            for ($j = 0; $j < mysqli_num_rows($res2); $j++) {
                $out .= "insert into `" . $tablename . "` values (";
                $row2 = mysqli_fetch_row($res2);
                // run through each field
                foreach ($row2 as $k => $v) {
                    #for ($k = 0; $k < $nf = mysqli_num_fields($res2); $k++) {
                    // identify null values and save them as null instead of ''
                    if (is_null($v)) {
                        $out .= "null";
                    } else {
                        $out .= "'" . mysqli_real_escape_string($con, $v) . "'";
                    }
                    if ($k < count($row2) - 1) {
                        $out .= ", ";
                    }
                }
                $out .= ");\n";
                // if saving is successful, then empty $out, else set error flag
                if (strlen($out) > $max_size) {
                    if ($out = PMBP_save_to_file($backupfile, $zip, $out, "a")) {
                        $out = "";
                    } else {
                        $error = true;
                    }
                }
            }
        }
        // if saving is successful, then empty $out, else set error flag
        if (strlen($out) > $max_size) {
            if ($out = PMBP_save_to_file($backupfile, $zip, $out, "a")) {
                $out = "";
            } else {
                $error = true;
            }
        }
    }
    // if db contained ring constraints
    #if ($ring_contraints) {
    #	$out.="\n\n# ring constraints workaround\n";
    #	$out .= "SET FOREIGN_KEY_CHECKS=1;\n";
    #	$out .= "COMMIT;\n";
    #}
    // save to file
    if ($backupfile = PMBP_save_to_file($backupfile, $zip, $out, "a")) {
        if ($zip != "zip") {
            return basename($backupfile);
        }
    } else {
        @unlink("./" . $backupfile);
        return false;
    }
    // create zip file in file system
    include_once "pclzip.lib.php";
    $pclzip = new PclZip($backupfile . ".zip");
    $pclzip->create($backupfile, PCLZIP_OPT_REMOVE_PATH, PMBP_EXPORT_DIR);
    // remove temporary plain text backup file used for zip compression
    @unlink(substr($backupfile, 0, strlen($backupfile)));
    if ($pclzip->error_code == 0) {
        return basename($backupfile) . ".zip";
    } else {
        // print pclzip error message
        echo "<div class=\"red\">pclzip: " . $pclzip->error_string . "</div>";
        // remove temporary plain text backup file
        @unlink(substr($backupfile, 0, strlen($backupfile) - 4));
        @unlink("./" . $backupfile);
        return FALSE;
    }
}