<?php
/************************************************************************************/
/* MassCRM客户关系管理系统							   								*/
/* ========================      						   							*/
/* 欢迎使用 MassCRM ,在您开始使用之前,请务必完整阅读本软件附带的全部说明档	   			*/
/************************************************************************************/
/* Welcome to http://www.massboxes.com & http://masscrm.massboxes.com		   		*/
/************************************************************************************/

//备份
global $dumpstring;
global $c_num;

//连接数据库
include "../account/boxes_create.boxes.inc.php";
//包含adodb.inc.php
require_once('../adodb/adodb.inc.php');
//mysql数据库
$conn = &ADONewConnection('mysql');

//显示debug过程
$conn->debug=true;

//不显示debug过程
$conn->debug=false;

//连接数据库
$conn->PConnect($MYSQL_HOSTNAME, $MYSQL_USERNAME, $MYSQL_PASSWORD, $DATABASE);

require_once '../includes/BiaoCommon.class.php';

$boxesDB=new BiaoCommon($conn); 

function escape_string($str){
	$str=mysql_escape_string($str);
	$str=str_replace('\\\'','\'\'',$str);
	$str=str_replace("\\\\","\\\\\\\\",$str);
	$str=str_replace('$','\$',$str);
	return $str;
}
function sqldumptable($table_name) {
	global $boxesDB;
	$dumpstring = "\$sql=\"CREATE TABLE `$table_name`";
	$dumpstring .= "(";
	$firstfield=1;
	
	$boxesDB->setTableName($table_name);
	$fields=$boxesDB->showFields();
	for($i=0;$i<count($fields);$i++){
		$field =$fields[$i];
		if (!$firstfield) {
			$dumpstring .= ",";
		}
		else {
			$firstfield=0;
		}
		$dumpstring .= " $field[Field] $field[Type]"; 
		if (!empty($field["Default"])) {
			$dumpstring .= " DEFAULT '$field[Default]'";
		}
		if ($field['Null'] != "YES") {
			$dumpstring .= " NOT NULL";
		}
		if ($field['Extra'] != "") {
			$dumpstring .= " $field[Extra]";
		}
	}
	
	$boxesDB->setTableName($table_name);
	$keys=$boxesDB->showKeys();
	for($i=0;$i<count($keys);$i++){
		$key = $keys[$i];
		$kname=$key['Key_name'];
		if ($kname != "PRIMARY" and $key['Non_unique'] == 0) {
			$kname="UNIQUE|$kname";
		}
		if(!is_array($index[$kname])) {
			$index[$kname] = array();
		}
		$index[$kname][] = $key['Column_name'];
	}
		
	while(list($kname, $columns) = @each($index)){
		$dumpstring .= ",";
		$colnames=implode($columns,",");
		if($kname == "PRIMARY"){
			$dumpstring .= " PRIMARY KEY ($colnames)";
		}
		else {
			if (substr($kname,0,6) == "UNIQUE") {
				$kname=substr($kname,7); 
			}
			$dumpstring .= " KEY $kname ($colnames)";
		}
	}
	$dumpstring .= ") ";
	
	$statuses=$boxesDB->showTable();
	for($i=0;$i<count($statuses);$i++){
		$status=$statuses[$i];
		if($status[Name]==$table_name){
			$dumpstring .=" Type=$status[Type] COMMENT='$status[Comment]'\";\n\$conn->Execute(\$sql);\n";
			break;
		}
		else
		continue;
	}
	
	$boxesDB->setTableName($table_name);
	$rows =$boxesDB->selectResult();
	for($j=0;$j<count($rows);$j++){
		$row=$rows[$j];
		$dumpstring .= "\$sql=\"INSERT INTO `$table_name` VALUES (";
		$firstfield=1;
		for ($i=0;$i<count($fields);$i++) {
			if($part) $HTTP_POST_VARS[fin]+=strlen($row[$i]); //这个方法计算完成量不是很准确
			if (!$firstfield) {
				$dumpstring.=",";
			}
			else {
				$firstfield=0;
			}
			
			if (!isset($row[$i])) {
				$dumpstring .= "NULL";
			}
			else {
				$dumpstring .= "'".escape_string($row[$i])."'";
			}
		}
		$dumpstring .= ")\";\n\$conn->Execute(\$sql);\n";
	}
	
	return $dumpstring;
}
	$showResult=$boxesDB->showTable();
	for($i=0;$i<count($showResult);$i++){
		$row=$showResult[$i];
		$alldumpstring.=sqldumptable($row[Name]);
	}
	
	
	$file="build_database.php";
	$fp=fopen($file,"w");
	flock($fp,2);
	fwrite($fp,"<?\n");
	fwrite($fp,$alldumpstring);
	fwrite($fp,"?>");
	fclose($fp);
?>