Location: PHPKode > projects > WB News > includes/backup.php
<?php

/*========================================================*\
||########################################################||
||#                                                      #||
||#     WB News v2.0.0                                   #||
||# ---------------------------------------------------- #||
||#     Copyright (c) 2004-2007                          #||
||#     Created: 19th August 2005                        #||
||#     Filename: backup.php                             #||
||#                                                      #||
||########################################################||
/*========================================================*/

/**
 *  @author $Author: pmcilwaine $
 *	@version $Id: backup.php,v 1.1.2.1 2008/02/03 09:37:25 pmcilwaine Exp $
 */

require_once( LIBDIR . "/DBase.php" );

class backup extends DBase
{

	var $content = NULL;
	var $backup_type = "sql";

	function backup( $backup_type=NULL )
	{
		global $config;
		
		if ( NULL == $backup_type )
		{
			$backup_type = "sql";
		}

		switch ( $backup_type )
		{
		case "sql":
		case "xml":
			$this->backup_type = $backup_type;
			break;
		default:
			return FALSE;
		}

		$this->DBase( $config["dbhost"], $config["dbuser"], $config["dbpass"], $config["dbname"], $config["dbport"] );

		return TRUE;
	}

	function GetHeaders()
	{
		global $config;

		$contents = array();
		$contents[] = "-- WBNews SQL Dump\n-- version " . $config["version"];
		$contents[] = "-- http://webmobo.com";
		$contents[] = "--";
		$contents[] = "-- Host: ". $this->dbhost;
		$contents[] = "--";
		$contents[] = "-- Time Generated: " . date("M d, Y \a\\t h:i A");
		$contents[] = "-- Server Version: " . $this->getVersion();
		$contents[] = "-- ";
		$contents[] = "-- PHP Version: " . phpversion();
		$contents[] = "--";
		$contents[] = "-- Database: `".$this->dbname."`";
		$contents[] = "--";
		$contents[] = "";

		return join( "\n", $contents );
	}

    /**
	 * @access private
	 * @return String
	 */
	function sqlTable()
	{
		$string = "";
		
		$tableList = $this->GetTableList();
		$numTables = count($tableList);
		
		for ( $i = 0; $i < $numTables; $i++ )
		{
			$string .= "--\n-- Table structure for `" . $tableList[$i] . "`". "\n--\n\n";
			$string .= "CREATE TABLE `" . $tableList[$i] . "` (\n";
            // get fields and keys
			$fields = $this->GetTableFields($tableList[$i]);
			$numFields = count( $fields );
			
			for ($j = 0; $j < $numFields; $j++)
			{
				$string .= "   `" . $fields[$j]["Field"] . "` " . $fields[$j]["Type"];
				$string .= (empty($fields[$j]["Null"]) || $fields[$j]["Null"] == "No" ? " NOT NULL" : "");
				$string .= ($fields[$j]["Extra"] != "auto_increment" && $fields[$j]["Type"] != "text" ? " default ".$fields[$j]["Default"]. "" : "");
				$string .= (!empty($fields[$j]["Extra"]) ? " " . $fields[$j]["Extra"] : "");
				$string .= ",\n";
			}
			
            // get keys
			$string .= $this->sqlKeys($tableList[$i]);
			$string .= ");\n\n";
			$string .= $this->sqlData($tableList[$i]);
		}
		
		return $string;
	}
	
    /**
	 * @access private
	 * @param String table - The Table name to get Records From
	 * @return String
	 */
	function sqlData($table)
	{
		$string = "";
		$string .= "--\n-- Dumping data for table `".$table."`\n--\n";
		$records = $this->getRecords( $table );
		$numRecords = count( $records );
		
		if ( is_array($records[0]) )
		{
			for ( $i = 0; $i < $numRecords; $i++ )
			{
				$string .= "INSERT INTO `".$table."` VALUES (";
				if ( is_array($records[$i]) )
				{
					$j = 1;
					$numFields = count( $records[$i] );
					foreach ( $records[$i] as $key => $value )
					{
						if ( empty($value) )
						{
							$string .= "'', ";
						}
						else if ( is_numeric($value) )
						{
							$string .= $value . ", ";
						}
						else
						{
							$string .= "'" . str_replace(array("\n", "\r", "'"), array("\\n", "\\r", "\\\'"), addslashes($value)) . "', ";
						}
						$j++;
					}
					
					$string = substr($string, 0, -2);
				}
				
				$string .= ");\n";
			}
		}
		
		$string .= "\n-- --------------------------------------------------------\n\n";
		return $string;
	}

	/**
	 * 
	 * @param String $table
	 */
	function getRecords( $table )
	{
		if ( NULL == $table )
		{
			return FALSE;
		}
		
		$this->query( "SELECT * FROM \"$table\"" );

		$ret = array();
		while ( $this->next_record() )
		{
			$ret[] = $this->current_record;
		}

		return $ret;
	}

    /**
	 * @access private
	 * @param String table - The Table name to get Records From
	 * @return String
	 */
	function sqlKeys($table)
	{
		$keys = $this->getTableKeys($table);
		$numKeys = sizeof($keys);
		
		$primaryKey = "";
		$uniqKey = array();
		$fulltextKey = array();
		$indexKeys = array();

		for ($i = 0; $i < $numKeys; $i++)
		{
			if ($keys[$i]["Key_name"] == "PRIMARY")
			{
                // primary key
				$primaryKey = $keys[$i]["Column_name"];
			}
			else if ($keys[$i]["Non_unique"] == 0)
			{
                // unique key
				if ( isset( $uniqKey[$keys[$i]["Key_name"]] ) )
				{
					array_push($uniqKey[$keys[$i]["Key_name"]], $keys[$i]["Column_name"]);
				}
				else
				{
					$uniqKey[$keys[$i]["Key_name"]] = array($keys[$i]["Column_name"]);
				}
			}
			else if ($keys[$i]["Non_unique"] == 1 && $keys[$i]["Index_type"] == "FULLTEXT")
			{
                // fulltext key
				if (isset($fulltextKey[$keys[$i]["Key_name"]]))
				{
					array_push($fulltextKey[$keys[$i]["Key_name"]], $keys[$i]["Column_name"]);
				}
				else
				{
					$fulltextKey[$keys[$i]["Key_name"]] = array($keys[$i]["Column_name"]);
				}
			}
			else
			{
                // index key
				if (isset($indexKeys[$keys[$i]["Key_name"]]))
				{
					array_push( $indexKeys[$keys[$i]["Key_name"]], $keys[$i]["Column_name"] );
				}
				else
				{
					$indexKeys[$keys[$i]["Key_name"]] = array($keys[$i]["Column_name"]);
				}
			}
		}
		
		$string = "";
		$string .= (!empty($primaryKey) ? "   PRIMARY KEY (`".$primaryKey."`),\n" : "");
		
        // unqiue key
		foreach ($uniqKey as $key => $value)
		{
			$string .= "   UNIQUE KEY `" . $key. "` (";
			
			$numUni = sizeof($value);
			for ($i = 0; $i < $numUni; $i++)
			{
				$string .= "`".$value[$i]."`, ";
			}
			$string = substr($string, 0, -2);
			$string .= "),\n";
		}
		
		// index
		foreach ( $indexKeys as $key => $value )
		{
			$string .= "   KEY `" . $key. "` (";
			$numUni = sizeof($value);
			for ( $i = 0; $i < $numUni; $i++ )
			{
				$string .= "`".$value[$i]."`, ";
			}
			
            // remove (, ) without ()
			$string = substr($string, 0, -2);
			$string .= "),\n";
		}
		
        // full text
		foreach ( $fulltextKey as $key => $value )
		{
			$string .= "   KEY `" . $key. "` (";
			
			$numUni = sizeof($value);
			for ( $i = 0; $i < $numUni; $i++ )
			{
				$string .= "`".$value[$i]."`, ";
			}
			
            // remove (, ) without ()
			$string = substr($string, 0, -2);
			$string .= "),\n";
		}

		return substr($string, 0, -2) . "\n";
	}
	
	function GetSQL()
	{
		$ret = array();
		$ret[] = $this->GetHeaders();
		$ret[] = $this->sqlTable();
		return join( "\n", $ret );
	}

	function GetXML()
	{
		$ret = array();
		$ret[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		$ret[] = "<wbsml application=\"wbnews\" version=\"2.0\">";
		
		foreach ( $this->GetTableList() as $table )
		{
			$ret[] = "\t<table name=\"$table\">";

			$this->query( "SELECT * FROM \"$table\"" );
			while ( $this->next_record() )
			{
				$ret[] = "\t\t<record>";
				foreach ( $this->current_record as $fieldname => $data )
				{
					if ((preg_match("/<([^>])+>/is", $data) === 1) || (preg_match("/(&gt;|&lt;|&)/is", $data) === 1))
					{
						$data = "<![CDATA[$data]]>";
					}
					$ret[] = "\t\t\t<field name=\"$fieldname\">$data</field>";
				}
				$ret[] = "\t\t</record>";
			}
			
			$ret[] = "\t</table>";
		}

		$ret[] = "</wbsml>";
		return join( "\n", $ret );
	}

	function SendFile()
	{
		header( "Content-Type: application/octetstream" );
		header( "Content-Type: application/force-download" );
		header( "Pragma: public" );
		header( "Expires: 0" );
		header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
		header( "Content-Disposition: inline; filename=\"backup-" . date( "Y-m-d" ) . "." . $this->backup_type );
		
		if ( $this->backup_type == "sql" )
		{
			print $this->GetSQL();
		}
		else
		{
			print $this->GetXML();
		}
		return;
	}

}
 
return;

?>
Return current item: WB News