Plugin Directory

Changeset 884215


Ignore:
Timestamp:
03/29/2014 03:37:06 PM (12 years ago)
Author:
barteled
Message:

0.4 - Add export feature

Location:
sandbox/trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • sandbox/trunk/admin-errors.php

    r846621 r884215  
    2929    $sandbox_errors['htaccess_denied'] = new Sandbox_Error('htaccess_denied', 'Could not access .htaccess. Please provide temporary .htaccess access during plugin activation.');
    3030    $sandbox_errors['htaccess_no_create'] = new Sandbox_Error('htaccess_no_create', 'Could not create .htaccess. Please create .htaccess in wordpress base directory and provide temporary edit permissions during activation.');
     31        $sandbox_errors['sqldump_no_create'] = new Sandbox_Error('sqldump_no_create', 'Could not create SQL dump file in Sandbox.');
     32        $sandbox_errors['export_dir_no_create'] = new Sandbox_Error('export_dir_no_create', 'Could not find or create export directory.');
     33        $sandbox_errors['zip_no_create'] = new Sandbox_Error('zip_no_create', 'Could not create zip file.');
    3134}
    3235
  • sandbox/trunk/admin-form.php

    r861276 r884215  
    164164}
    165165
     166function sandbox_export($sandbox = NULL){
     167    global $sandbox_errors;
     168   
     169    if($sandbox == NULL) {
     170        throw new Sandbox_Exception($sandbox_errors['invalid_action']);
     171    }
     172   
     173    $sandbox->create_export();
     174   
     175    echo '<div class="updated">
     176            <p>
     177            Export generated for '.$sandbox->name.'.<br/>
     178            <a href="'.admin_url('admin-ajax.php').'?action=export_download&shortname='.$sandbox->shortname.'">Download export</a>
     179            </p>
     180        </div>';
     181}
     182
    166183function sandbox_move_to_anchor($anchor){
    167184  ?>
  • sandbox/trunk/admin-menu.php

    r846621 r884215  
    5656          $sandbox->delete(true);         
    5757          break;
     58            case "export":
     59                sandbox_export($sandboxes[$_REQUEST['shortname']]);
     60                sandbox_list_sandboxes();
     61                break;
    5862      default:
    5963          sandbox_list_sandboxes();
     
    6367}
    6468
    65    
  • sandbox/trunk/admin-table.php

    r852359 r884215  
    2727            $actions = array(
    2828                'edit'      => sprintf('<a href="?page=%s&action=%s&shortname=%s">Edit</a>',$_REQUEST['page'],'edit',$item['shortname']),
     29                                'export'      => sprintf('<a href="?page=%s&action=%s&shortname=%s">Export</a>',$_REQUEST['page'],'export',$item['shortname']),
    2930                'delete'    => sprintf('<a href="?page=%s&action=%s&shortname=%s">Delete</a>',$_REQUEST['page'],'delete',$item['shortname']),
    3031            );
  • sandbox/trunk/class-sandbox.php

    r846621 r884215  
    325325            throw new Sandbox_Exception($sandbox_error );
    326326        }
    327         $paths = $this->recursive_listing($wp_dir, array($sandbox_dir));
     327               
     328                $exclusions = array($sandbox_dir);
     329                try {
     330                    $exclusions[] = $this->export_dir();
     331                } catch (Exception $ex) {} var_dump($exclusions);
     332        $paths = $this->recursive_listing($wp_dir, $exclusions);
    328333       
    329334        // Make Directory for sandbox
     
    375380    }
    376381   
    377     private function update_wp_config(){
     382        public function create_export(){
     383            global $wp_dir, $sandbox_dir, $sandbox_errors;
     384            $this->sql_dump();
     385           
     386            $export_dir = $this->export_dir(true);
     387           
     388            try {
     389                $zip_file = $export_dir."/".$this->shortname."-export.zip";
     390
     391                if(file_exists($zip_file)) unlink ($zip_file);
     392
     393                $zip = new ZipArchive();
     394                if ($zip->open($zip_file, ZipArchive::CREATE)!==TRUE) {
     395                    throw new Sandbox_Exception($sandbox_errors['zip_no_create']);
     396                }
     397
     398                $sandbox_files = $this->recursive_listing($this->dir);
     399               
     400                foreach($sandbox_files as $file){
     401                    $zip->addFile($file, str_replace($this->dir, "", $file));
     402                }
     403               
     404                $zip->close();
     405            } catch (Exception $e) {
     406                throw new Sandbox_Exception($sandbox_errors['zip_no_create']);
     407            }
     408        }
     409       
     410    private function sql_dump(){
     411            global $wpdb, $wp_dir, $sandbox_dir, $sandbox_errors;
     412           
     413            $sql_dump_file = $this->dir."/dump.sql";
     414            $handle = fopen($sql_dump_file, 'wb');
     415
     416            if (!$handle){
     417                $sandbox_error = $sandbox_errors['sqldump_no_create'];
     418                $sandbox_error->add_data("File: ".$sql_dump_file);
     419                throw new Sandbox_Exception($sandbox_error);
     420            }
     421           
     422            $sql = "
     423-- Sandbox SQL Dump for $this->name
     424--
     425-- Description: $this->description
     426-- ------------------------------------------------------
     427
     428/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
     429/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
     430/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
     431/*!40101 SET NAMES ".DB_CHARSET." */;
     432/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
     433/*!40103 SET TIME_ZONE='+00:00' */;
     434/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
     435/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
     436/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
     437/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
     438";
     439           
     440            if(empty($this->prefix)) throw new Sandbox_Exception($sandbox_errors['no_prefix']);
     441       
     442            $tables = $wpdb->get_results("SHOW TABLES LIKE '".$this->prefix."%'", ARRAY_N);
     443
     444            foreach($tables as $sandbox_table){
     445                $sandbox_table = $sandbox_table[0];
     446               
     447                $sql .= "
     448--
     449-- Table structure for table `".$sandbox_table."`
     450--
     451
     452DROP TABLE IF EXISTS `".$sandbox_table."`;
     453/*!40101 SET @saved_cs_client     = @@character_set_client */;
     454/*!40101 SET character_set_client = ".DB_CHARSET." */;
     455";
     456                $table_sql = $wpdb->get_var("SHOW CREATE TABLE ".$sandbox_table, 1);
     457                $sql .= $table_sql;
     458               
     459                $sql .= ";
     460/*!40101 SET character_set_client = @saved_cs_client */;
     461
     462--
     463-- Dumping data for table `".$sandbox_table."`
     464--
     465
     466LOCK TABLES `".$sandbox_table."` WRITE;
     467/*!40000 ALTER TABLE `".$sandbox_table."` DISABLE KEYS */;
     468";
     469               
     470                $limit = 1000;
     471                $offset = 0;
     472                $rows = $wpdb->get_results("SELECT * FROM ".$sandbox_table." LIMIT ".$limit, ARRAY_N);
     473                $sqlrows = array();
     474                while (!empty($rows)){
     475                    foreach($rows as $row){
     476                        $sqlvalues = array();
     477                        foreach($row as $value){
     478                            if ($value === NULL){
     479                                $sqlvalues[] = 'NULL';
     480                            } elseif($value == "" || $value === false){
     481                                $sqlvalues[] = "''";
     482                            } elseif(is_numeric($value)) {
     483                                $sqlvalues[] = sprintf("%d", $value);
     484                            } else {
     485                                $sqlvalues[] = "'".mysql_real_escape_string($value)."'";
     486                            }
     487                        }
     488                        $sqlrows[] = "(".implode(",", $sqlvalues).")";
     489                    }
     490                   
     491                    $offset += $limit;
     492                    $rows = $wpdb->query("SELECT * FROM ".$sandbox_table." LIMIT ".$offset.", ".$limit, ARRAY_N);
     493                }
     494                if(!empty($sqlrows)){
     495                    $sql .= "INSERT INTO `".$sandbox_table."` VALUES ";
     496                    $sql .= implode(",",$sqlrows);
     497                }
     498                $sql .= ";\n/*!40000 ALTER TABLE `".$sandbox_table."` ENABLE KEYS */;\nUNLOCK TABLES;\n";
     499            }
     500           
     501           
     502           
     503            $sql .= "
     504/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
     505
     506/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
     507/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
     508/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
     509/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
     510/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
     511/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
     512/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
     513";
     514           
     515            fwrite($handle, $sql);
     516           
     517            fclose($handle);       
     518        }
     519       
     520        public function export_dir($create_if_no_present = false){
     521            global $wp_dir, $sandbox_errors;
     522           
     523            try {
     524                $export_dir_base = "sandbox-export-";
     525                $wp_content_dir = $wp_dir."wp-content/";
     526                $wp_content_listing = scandir($wp_content_dir);
     527                $export_dir = NULL;
     528                foreach($wp_content_listing as $file){
     529                    if(is_dir($wp_content_dir.$file) && substr(basename($file), 0, strlen($export_dir_base)) == $export_dir_base){
     530                        $export_dir = $wp_content_dir.$file."/";
     531                    }
     532                }
     533
     534                if($export_dir == NULL){
     535                    if($create_if_no_present){
     536                        $export_dir = $export_dir_base.substr(md5(rand()), 0, 7);
     537                        mkdir($wp_dir."/wp-content/".$export_dir);
     538
     539                        file_put_contents($wp_dir."/wp-content/".$export_dir."/.htaccess", "Order allow,deny\nDeny from all");
     540                    } else {
     541                        throw new Sandbox_Exception($sandbox_errors['export_dir_no_create']);
     542                    }
     543                }
     544            } catch (Exception $e){
     545                throw new Sandbox_Exception($sandbox_errors['export_dir_no_create']);
     546            }
     547           
     548            return $export_dir;
     549        }
     550       
     551        public function export_file(){
     552            $export_file = $this->export_dir()."/".$this->shortname."-export.zip";
     553
     554            if(!file_exists($export_file) || !is_readable($export_file)) throw new Sandbox_Exception($sandbox_errors['zip_no_create']);
     555           
     556            return $export_file;
     557        }
     558       
     559        private function update_wp_config(){
    378560        global $wpdb;
    379561       
  • sandbox/trunk/readme.txt

    r861276 r884215  
    55Requires at least: 3.8
    66Tested up to: 3.8.1
    7 Stable tag: 0.3
     7Stable tag: 0.4
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    7575If you cannot get into the admin interface, there is a cookie in the browser called "sandbox". Delete this and you will be returned to the live site. This cookie is used to tell the plugin if you want to use a sandbox and which one. So for example if you activate the sandbox in one browser, you can start up a different browser and view the live site.
    7676
     77= How do I use the export capability? =
     78
     79The export capability provides a way to download a sandbox and install it somewhere. To download an export, go to Sandbox option on the sidebar of the Admin interface. Place your mouse over the row of the sandbox you would like to export and the select the export option. Once prompted, click the download link. Depending on the size of the sandbox and speed of your web server, it may take some time for the link to appear.
     80
     81The download file is a zip of all the files for the sandbox. Within the zip there is a SQL dump (dump.sql) of all the database tables related to the sandbox. The files need to be copied into the desired directory of the new server and the SQL file needs to be imported into the database.
     82
    7783= Why are my backups are now huge? =
    7884
     
    97103* Minor bug fix that could cause sandbox_edit() error on some servers
    98104
     105= 0.4 =
     106* Add capability to export sandbox for installation elsewhere.
    99107
    100108
     
    103111
    104112
     113
  • sandbox/trunk/sandbox-ajax.php

    r846621 r884215  
    1616    die();
    1717}
     18
     19add_action('wp_ajax_export_download', 'export_download_callback');
     20
     21function export_download_callback(){
     22    global $sandboxes;
     23    error_log("downlaod");
     24    if (!isset($sandboxes[$_REQUEST['shortname']])) die();
     25
     26    $sandbox = $sandboxes[$_REQUEST['shortname']];
     27   
     28    try {
     29        $export_file = $sandbox->export_file();
     30        header("Content-Disposition: attachment; filename=".basename(str_replace(' ', '_', $export_file)));
     31        header("Content-Type: application/force-download");
     32        header("Content-Type: application/octet-stream");
     33        header("Content-Type: application/download");
     34        header("Content-Description: File Transfer");
     35        header("Content-Length: " . filesize($export_file));
     36        flush(); // this doesn't really matter.
     37
     38        $fp = fopen($export_file, "r");
     39        while (!feof($fp))
     40        {
     41                echo fread($fp, 65536);
     42                flush();
     43        }
     44        fclose($fp);
     45       
     46        unlink($export_file);
     47    } catch (Exception $ex) {
     48        error_log($ex);
     49        die();
     50    }
     51   
     52    die();
     53}
     54
     55   
     56
    1857?>
  • sandbox/trunk/sandbox-plugin.php

    r861276 r884215  
    22/**
    33 * @package Sandbox-Plugin
    4  * @version 0.3
     4 * @version 0.4
    55 */
    66/*
     
    99Description: Creates a completely independent sandbox based on your existing live site that is not accessible to the general public and search engines.
    1010Author: Eric Bartel
    11 Version: 0.3
     11Version: 0.4
    1212Author URI: http://think-bowl.com
    1313
Note: See TracChangeset for help on using the changeset viewer.