Exemplo n.º 1
0
function liveServerDeploy_automate($settings)
{
    extract($settings);
    if (strpos($site_url, 'http://') === false) {
        $site_url = 'http://' . $site_url;
    }
    if (substr($site_url, -1) == '/') {
        $site_url = substr($site_url, 0, -1);
    }
    if ($ftp_server && $ftp_username && $ftp_pass && $db_name && $db_username) {
        liveServerDeploy_update_status(null, 'success', '1');
    } else {
        liveServerDeploy_update_status("Please provide your Live Server Settings", 'error', '1');
        return;
    }
    liveServerDeploy_update_status(null, 'warning', '2');
    liveServerDeploy_update_status("Start Deploying ...");
    liveServerDeploy_update_status("Connecting to ftp server ...");
    $root = $ftp_root;
    // connect to server
    $ftph = @ftp_connect($ftp_server, $ftp_port ? "{$ftp_port}" : 21);
    if ($ftph === FALSE) {
        liveServerDeploy_update_status("Failed to connected to the ftp server", 'error', 2);
        liveServerDeploy_update_status("Failed to Deploy", 'error', 2);
        return;
    } else {
        liveServerDeploy_update_status("Succesfully connected to the ftp server!", 'success');
    }
    // login to server
    if (!ftp_login($ftph, $ftp_username, $ftp_pass)) {
        liveServerDeploy_update_status("Failed to login to ftp server ...", 'error');
        ftp_close($ftph);
        return;
    }
    if (!@ftp_put($ftph, $root . "/capability.php", dirname(__FILE__) . '/remote-capabilities.php', FTP_BINARY)) {
        liveServerDeploy_update_status("Failed to determine remote capabilities ...", 'error', 2);
        ftp_close($ftph);
        return;
    }
    $capabilities = @liveServerDeploy_get_contents($site_url . '/capability.php');
    if (!$capabilities) {
        liveServerDeploy_update_status("Failed to determine remote capabilities!", 'error', 2);
        ftp_close($ftph);
        return;
    }
    // clean up
    @ftp_delete($ftph, $root . "/capability.php");
    // get capabilities
    $capabilities = unserialize($capabilities);
    $archive = $capabilities['archiving'] && LSD_ARCHIVING_ENABLED;
    $mysql_exec = LSD_MYSQL_DUMP_USE_EXEC;
    // || $capabilities['mysql_exec']; // if we can dump using exec then thats all we need
    $mysql_dump = true;
    if ($archive) {
        liveServerDeploy_update_status("Archiving enabled", 'success', 2);
    } else {
        liveServerDeploy_update_status("Archiving disabled", 'warning', 2);
    }
    if ($mysql_exec && $capabilities['mysql_exec']) {
        liveServerDeploy_update_status("System mysql dump and import enabled", 'success', 2);
    } else {
        if ($mysql_exec) {
            liveServerDeploy_update_status("System mysql dump enabled", 'success', 2);
        } else {
            if (LSD_MYSQL_DUMP_USE_EXEC && !LSD_MYSQL_DUMP_FALLBACKS) {
                liveServerDeploy_update_status("Live server does not support executing system mysql import - you must manually install the MySQL export on your server. See \"Manual Export\" section.", 'error', 2);
                $mysql_dump = false;
            } else {
                liveServerDeploy_update_status("Live Server Deploy mysql dump/import enabled", 'warning', 2);
            }
        }
    }
    liveServerDeploy_update_status("Preparing Export ...", 'notice', 3);
    liveServerDeploy_update_status("Checking file permissions ...");
    $permissions_ok = @liveServerDeploy_check_permissions(ABSPATH);
    if (!$permissions_ok) {
        ftp_close($ftph);
        return;
    }
    liveServerDeploy_update_status("File permissions OK", 'success');
    $ignore_list = liveServerDeploy_parse_ignore_list($ignore_list);
    if ($archive) {
        $archivedump = new ArchiveTransfer();
        $count = $archivedump->dump($ignore_list);
        if ($count < 0) {
            liveServerDeploy_update_status("Failed to archive files!", 'error');
            $archive = false;
        } else {
            liveServerDeploy_update_status("Archived {$count} Files ...");
        }
        unset($archivedump);
    }
    if ($mysql_dump) {
        $mysqldump = new MySQLTransfer(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
        if ($ret = $mysqldump->dump(get_option('siteurl'), $site_url, $mysql_exec)) {
            liveServerDeploy_update_status("Failed to export MySQL database - you must manually install the MySQL export on your server. See \"Manual Export\" section.", 'error', 2);
        } else {
            liveServerDeploy_update_status("Export MySQL database ...");
        }
        unset($mysqldump);
    }
    liveServerDeploy_update_status(null, 'success', 3);
    if ($archive) {
        liveServerDeploy_update_status("Uploading compressed archive to server - this might take a while ...");
        ini_set("max_execution_time", "0");
        if (!@ftp_put($ftph, $root . "/lsd_dump.zip", LSD_DIR . '/lsd_dump.zip', FTP_BINARY)) {
            liveServerDeploy_update_status("Failed to upload compressed archive ...", 'error', 4);
            ftp_close($ftph);
            $archive = false;
        } else {
            liveServerDeploy_update_status("Successfully Uploaded Archive to Server!", 'success', 4);
        }
    }
    if (!$archive) {
        liveServerDeploy_update_status("Opening additional ftp connections ...", 'notice', 4);
        $ftp_bulk = new FTPBulkTransfer($ftp_server, $ftp_port, $ftp_username, $ftp_pass);
        if ($ftp_bulk->is_open()) {
            liveServerDeploy_update_status("Opened " . $ftp_bulk->connection_count() . " ftp connections to server ...", 'notice', 4);
        }
        global $liveServerDeploy_file_count;
        $liveServerDeploy_file_count = $count = liveServerDeploy_upload_dir($ftph, $ftp_bulk, ABSPATH, '', $root, $counting = true, 0, $ignore_list);
        liveServerDeploy_update_status("Uploading {$count} files to server - this might take a while ...", null, 4);
        ini_set("max_execution_time", "0");
        if ($ftp_bulk->is_open()) {
            while ($ret = $ftp_bulk->poll()) {
                ftp_pwd($ftph);
            }
        } else {
            $count = liveServerDeploy_upload_dir($ftph, null, ABSPATH, '', $root, $count, $count, $ignore_list);
        }
        liveServerDeploy_update_status($count . " files uploaded ...", 'success');
        // free up ftp connections
        $ftp_bulk->close();
    }
    if ($mysql_dump) {
        liveServerDeploy_update_status("Uploading mysql dump ..");
        if (!@ftp_put($ftph, $root . "/lsd_dump.sql", LSD_DIR . '/lsd_dump.sql', FTP_BINARY)) {
            liveServerDeploy_update_status("Failed to upload mysql dump ...", 'error', 4);
            ftp_close($ftph);
            $archive = false;
        } else {
            liveServerDeploy_update_status("Successfully uploaded MySQL dump to server!", 'success');
        }
    } else {
        liveServerDeploy_update_status("No mysql dump ..");
    }
    liveServerDeploy_update_status(null, 'success', 4);
    // send scripts that will take care of extracting the archive (TODO: and importing the mysql)
    liveServerDeploy_update_status("Prepare importing ...", 'notice', 5);
    if ($archive) {
        if (!@ftp_put($ftph, $root . "/decompress.php", dirname(__FILE__) . '/remote-decompress.php', FTP_BINARY)) {
            liveServerDeploy_update_status("Failed to decompress archive on remote!", 'error', 5);
            ftp_close($ftph);
            return;
        }
        $decompress = @liveServerDeploy_get_contents($site_url . '/decompress.php');
        if (!$decompress) {
            liveServerDeploy_update_status("Failed to decompress archive on remote!", 'error', 5);
            ftp_close($ftph);
            return;
        }
        // clean up
        @ftp_delete($ftph, $root . "/decompress.php");
        liveServerDeploy_update_status("Successfully decompressed archive, your files are ready!", 'success');
    }
    if ($mysql_dump) {
        if (!@ftp_put($ftph, $root . "/import_mysql.php", dirname(__FILE__) . '/remote-mysql.php', FTP_BINARY)) {
            liveServerDeploy_update_status("Failed to import mysql database on remote - you must manually install the MySQL export on your server. See \"Manual Export\" section.", 'error', 5);
            ftp_close($ftph);
            return;
        }
        $get = 'mysql_exec=' . $mysql_exec;
        $get .= '&db_host=' . urlencode($db_host ? $db_host : 'localhost');
        $get .= '&db_name=' . urlencode($db_name);
        $get .= '&db_username='******'&db_password='******'/import_mysql.php?' . $get);
        if ($import_mysql) {
            liveServerDeploy_update_status("Failed to import mysql dump on remote - you must manually install the MySQL export on your server. See \"Manual Export\" section.", 'error', 5);
            ftp_close($ftph);
            return;
        }
        // clean up
        @ftp_delete($ftph, $root . "/import_mysql.php");
        @ftp_delete($ftph, $root . "/lsd_dump.sql");
        liveServerDeploy_update_status("Successfully imported database!", 'success');
    }
    liveServerDeploy_update_status(null, 'success', 5);
    liveServerDeploy_update_status(null, 'notice', 6);
    // finalize by editing the wp-config.php
    @ftp_rename($ftph, $root . "/wp-config.php", $root . "/wp-config.development.php");
    file_put_contents(ABSPATH . "/wp-config.lsd-live.php", liveServerDeploy_new_config($db_name, $db_username, $db_password, $db_host));
    if (!@ftp_put($ftph, $root . "/wp-config.php", ABSPATH . "/wp-config.lsd-live.php", FTP_BINARY)) {
        liveServerDeploy_update_status("Unable to upload edit wp-config.php file, please update manually", 'notice', 5);
        ftp_close($ftph);
        unlink(ABSPATH . "/wp-config.lsd-live.php");
        return;
    }
    unlink(ABSPATH . "/wp-config.lsd-live.php");
    $finalize = @liveServerDeploy_get_contents($site_url . '/?liveServerDeployComplete=1');
    ftp_close($ftph);
    ?>
        <script>
            jQuery('.liveServerDeploy_status').remove();
        </script>
        <?php 
    if (!$finalize) {
        liveServerDeploy_update_status("Permalinks still need to be resaved manually", 'notice');
    }
    liveServerDeploy_update_status("Successfully Deployed", 'success', 6);
}
function lsd_download_mysql_dump()
{
    $memory_limit = ini_get('memory_limit');
    $memory_limit_size = (int) $memory_limit;
    $memory_limit_size_type = trim(str_replace($memory_limit_size, '', $memory_limit));
    $memory_limit_size *= 2;
    $new_memory_limit = $memory_limit_size . $memory_limit_size_type;
    ini_set('memory_limit', $new_memory_limit);
    if (!$_POST['site_url']) {
        wp_die("ERROR: Please supply a URL");
    }
    if (strpos($_POST['site_url'], 'http://') === false) {
        $_POST['site_url'] = 'http://' . $_POST['site_url'];
    }
    $mysql_dump = new MySQLTransfer();
    $ret = $mysql_dump->dump(get_option('siteurl'), $_POST['site_url']);
    if ($ret) {
        wp_die($mysql_dump->last_result);
    }
    // Set headers
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=" . DB_NAME . ".sql");
    header("Content-Type: application/sql");
    header("Content-Transfer-Encoding: binary");
    @readfile(LSD_DIR . "/lsd_dump.sql");
    @unlink(LSD_DIR . "/lsd_dump.sql");
    exit;
}