database.php

来自「全新且完善的强大网上商店系统」· PHP 代码 · 共 463 行 · 第 1/2 页

PHP
463
字号
<?php

/*
  [SOOBIC!] admin/database.php 

	Version: 1.5
	Author: soolan (soolan@qq.com)
	Copyright: soolan (www.soobic.com)

	Last Modified: 2005/4/9 10:00
  
*/



if(!defined("IN_SOOBIC")) {
        exit("Access Denied");
}

if(!$allowadmindata){
    cpheader();
		cpmsg("对不起,您的管理权限受限,不能进行此操作。");
}


if($act == "export" && $exportsubmit && $type) {
	$db->query("SET SQL_QUOTE_SHOW_CREATE = 0");
	$sqldump = "";
	$time = gmdate(DATE_FORMAT.'  '. TIME_FORMAT, $timestamp + TIME_OFFSET * 3600);
	if($type == "all") {
		$tables = array('usergroups','configuration','configuration_group','styles','stylevars','templates','categories','categories_description','tax_class','tax_rates','zones_to_geo_zones','manufacturers',
                'products','products_description','reviews','manufacturers','manufacturers_info','reviews','reviews_description','specials','orders','orders_total','orders_status','orders_products','orders_products_attributes','orders_status_history',
								'countries','zones','geo_zones','customers','address_book','currencies','languages','featured','products_options','products_attributes','products_notifications','products_options_values',
								'customers_basket','customers_basket_attributes');
	} elseif($type == "standard") {
		$tables = array('usergroups','configuration','styles','stylevars','templates','categories','categories_description','tax_class','tax_rates','zones_to_geo_zones','manufacturers',
                'products','products_description','reviews','manufacturers','manufacturers_info','reviews','reviews_description','specials','orders','orders_total','orders_status','orders_products','orders_products_attributes','orders_status_history',
								'geo_zones','customers','address_book','featured','products_options','products_attributes','products_notifications','products_options_values',
								'customers_basket','customers_basket_attributes');
	} elseif($type == "majority") {
		$tables = array('usergroups','categories','categories_description','manufacturers',
                'products','products_description','reviews','manufacturers','manufacturers_info','reviews','reviews_description','specials','orders','orders_total','orders_status','orders_products','orders_products_attributes','orders_status_history',
								'customers','address_book','products_options','products_attributes','products_notifications','products_options_values',
								'customers_basket','customers_basket_attributes');
	} elseif($type == "mini") {
		$tables = array('usergroups','categories','categories_description',
                'products','products_description','products_to_categories','reviews','manufacturers','manufacturers_info','reviews','reviews_description','specials','orders','orders_total','orders_status','orders_products','orders_products_attributes','orders_status_history',
								'customers','address_book','products_options','products_attributes','products_notifications','products_options_values');
	}
//echo DB_DATABASE_PRE;
//exit;
	$sqldump = '';
	if($multivol) {
		if($saveto == 'server') {
			$volume = intval($volume) + 1;
			$tableid = $tableid ? $tableid - 1 : 0;
			$startfrom = intval($startfrom);
			for($i = $tableid; $i < count($tables) && strlen($sqldump) < $sizelimit * 1000; $i++) {
				$sqldump .= sqldumptable(DB_DATABASE_PRE.$tables[$i], $startfrom, strlen($sqldump));
				$startfrom = 0;
			}
			$tableid = $i;
		} else {
			cpheader();
			cpmsg("只有备份到服务器才能使用分卷备份功能。");
		}
	} else {
		foreach($tables as $table) {
			$sqldump .= sqldumptable(DB_DATABASE_PRE.$table);
		}
	}

	$dumpfile = substr($filename, 0, strrpos($filename, "."))."-%s".strrchr($filename, ".");
	if(trim($sqldump)) {
		$dumpversion = strip_tags(STORE_VERSION);
		$sqldump = "# Identify: ".base64_encode("$timestamp,$dumpversion,$type,$multivol,$volume")."\n".
			"#\n".
			"# Soobic! Data Dump".($multivol ? " Volume $volume" : NULL)."\n".
			"# Version: Soobic! $dumpversion\n".
			"# Time: $time\n".
			"# Type: $type\n".
			"# Tablepre: ".DB_DATABASE_PRE."\n".
			"#\n".
			"# Soolan! Community: http://www.Soobic.com\n".
			"# Please visit our website for newest infomation about Soobic!\n".
			"# --------------------------------------------------------\n\n\n".
			$sqldump;

		if($saveto == "local") {
			ob_end_clean();
			header('Content-Encoding: none');
			header('Content-Type: '.(strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 'MSIE') ? 'application/octetstream' : 'application/octet-stream'));
			header('Content-Disposition: '.(strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 'MSIE') ? 'inline; ' : 'attachment; ').'filename="soobic_'.date('ymd').'.sql"');
			header('Content-Length: '.strlen($sqldump));  //fix: 备份文件长度计算逻辑错误
			header('Pragma: no-cache');
			header('Expires: 0');
			echo $sqldump;
			soobic_exit();
		} elseif($saveto == "server") {
			cpheader();
			if($filename != "") {
				@$fp = fopen(($multivol ? sprintf($dumpfile, $volume) : $filename), "w");
				@flock($fp, 3);
				if(@!fwrite($fp, $sqldump)) {
					@fclose($fp);
					cpmsg("数据文件无法保存到服务器,请检查目录属性。");
				} elseif($multivol) {
					cpmsg("分卷备份:数据文件 #$volume 成功创建,程序将自动继续。", "admincp.php?act=export&type=$type&saveto=server&filename=$filename&multivol=1&sizelimit=$sizelimit&volume=$volume&tableid=$tableid&startfrom=$startrow&exportsubmit=yes");
				} else {
					cpmsg("数据成功备份至服务器 <a href=\"$filename\">$filename</a> 中。");
				}
			} else {
				cpmsg("您没有输入备份文件名,请返回修改。");
			}
		}
	} else {
		if($multivol) {
			$volume--;
			$filelist = "<ul>";
			for($i = 1; $i <= $volume; $i++) {
				$filename = sprintf($dumpfile, $i);
				$filelist .= "<li><a href=\"$filename\">$filename\n";
			}
			cpheader();
			cpmsg("恭喜您,全部 $volume 个备份文件成功创建,备份完成。\n<br>$filelist");
		} else {
			cpheader();
			cpmsg("备份出错,数据表没有内容。");
		}
	}
}

cpheader();

if($act == "export") {
	if(!$exportsubmit) {

?>
<table cellspacing="0" cellpadding="0" border="0" width="90%" align="center">
<tr><td bgcolor="<?=BORDERCOLOR?>">
<table border="0" cellspacing="<?=BORDERWIDTH?>" cellpadding="<?=TABLESPACE?>" width="100%">
<tr class="header"><td>特别提示</td></tr>
<tr bgcolor="<?=ALTBG1?>"><td>
<br><ul><li>数据备份功能根据您的选择备份全部商店商品和设置数据,导出的数据文件可用“数据恢复”功能或 phpMyAdmin 导入。</ul>
<ul><li>强烈建议:备份到服务器请使用 .sql 作为扩展名,这将给日后的维护带来很大方便。</ul>
</td></tr></table></td></tr></table>

<br><br><form name="backup" method="post" action="<?=tep_href_link('admincp.php','act=export')?>">
<table cellspacing="0" cellpadding="0" border="0" width="90%" align="center">
<tr><td bgcolor="<?=BORDERCOLOR?>">
<table border="0" cellspacing="<?=BORDERWIDTH?>" cellpadding="<?=TABLESPACE?>" width="100%">
<tr class="header"><td colspan="2">数据备份方式</td></tr>
<tr>
<td bgcolor="<?=ALTBG1?>" width="40%"><input type="radio" value="all" name="type"> 全部备份</td>
<td bgcolor="<?=ALTBG2?>" width="60%">包括商店全部数据表数据(模板和附件文件除外)</td></tr>

<tr>
<td bgcolor="<?=ALTBG1?>"><input type="radio" value="standard" checked name="type"> 标准备份(推荐)</td>
<td bgcolor="<?=ALTBG2?>">包括常用的全部数据表数据</td></tr>

<tr>
<td bgcolor="<?=ALTBG1?>"><input type="radio" value="majority" name="type"> 精简备份</td>
<td bgcolor="<?=ALTBG2?>">仅包括用户、板块设置及贴子数据</td></tr>

<tr>
<td bgcolor="<?=ALTBG1?>"><input type="radio" value="mini" name="type" > 最小备份</td>
<td bgcolor="<?=ALTBG2?>">仅包括用户、板块设置及系统设置数据</td></tr>

<tr bgcolor="<?=ALTBG2?>" class="header"><td colspan="2">选择目标位置</td></tr>

<tr bgcolor="<?=ALTBG2?>">
<td colspan="2"><input type="radio" value="local" name="saveto" onclick="this.form.filename.disabled=this.checked;if(this.form.multivol.checked) {alert('注意:\n\n备份到本地无法使用分卷备份功能。');this.form.multivol.checked=false;this.form.sizelimit.disabled=true;}"> 备份到本地</td></tr>
<tr bgcolor="<?=ALTBG2?>"><td><input type="radio" value="server" checked name="saveto" onclick="this.form.filename.disabled=!this.checked"> 备份到服务器</td>
<td><input type="text" size="40" name="filename" value="./oscdata/soobic_<?=date('md').'_'.random(5)?>.sql" onclick="alert('注意:\n\n数据文件保存在服务器的可见目录下,其他人有    \n可能下载得到这些文件,这是不安全的。因此请    \n在使用随机文件名的同时,及时删除备份文件。');"></td>
</tr>


<tr class="header"><td colspan="2">使用分卷备份</td></tr>
<tr bgcolor="<?=ALTBG2?>">
<td><input type="checkbox" name="multivol" value="1" onclick="this.form.sizelimit.disabled=!this.checked;if(this.checked && this.form.saveto[1].checked!=true) {alert('注意:\n\n只有选择备份到服务器才能使用分卷备份功能。');this.form.saveto[1].checked=true;this.form.filename.disabled=false;}"> 文件长度限制(KB)</td>
<td><input type="text" size="40" name="sizelimit" value="1024" disabled></td>
</tr></table></td></tr></table><br><center>
<input type="submit" name="exportsubmit" value="备份数据"></center></form>
<?

	}

} elseif($act == 'import') {

	 if(!$importsubmit && !$deletesubmit) {
	 	$exportlog = array();
	 	if(is_dir($soobic.'./oscdata')) {
	 		$dir = dir($soobic.'./oscdata');
			while($entry = $dir->read()) {
				$entry = "./oscdata/$entry";
				if (is_file($entry) && strtolower(strrchr($entry, ".")) == ".sql") {
					$filesize = filesize($entry);
					$fp = fopen($entry, "r");
					$identify = explode(",", base64_decode(preg_replace("/^# Identify:\s*(\w+).*/s", "\\1", fgets($fp, 256))));
					fclose ($fp);
 
					$exportlog[$identify[0]] = array(	"version" => $identify[1],
										"type" => $identify[2],
										"multivol" => $identify[3],
										"volume" => $identify[4],
										"filename" => $entry,
										"size" => $filesize);
				}
			}
			$dir->close();
		} else {
			cpmsg("目录不存在或无法访问,请检查 ./oscdata/ 目录。");
		}
		krsort($exportlog);
		reset($exportlog);

		$exportinfo = "";
		foreach($exportlog as $dateline => $info) {
			$info[dateline] = is_int($dateline) ? gmdate(DATE_FORMAT.'  '. TIME_FORMAT, $dateline + TIME_OFFSET * 3600) : "未知";
			switch($info[type]) {
				case all: $info[type] = "全部"; break;
				case standard: $info[type] = "标准"; break;
				case majority: $info[type] = "精简"; break;
				case mini: $info[type] = "最小"; break;
			}
			$info[size] = sizecount($info[size]);
			$info[multivol] = $info[multivol] ? "是" : "否";
			$info[volume] = $info[multivol] ? $info[volume] : "";
			$exportinfo .= "<tr align=\"center\"><td bgcolor=\"".ALTBG1."\"><input type=\"checkbox\" name=\"delete[]\" value=\"$info[filename]\"></td>\n".
				"<td bgcolor=\"".ALTBG2."\"><a href=\"$info[filename]\">".substr(strrchr($info[filename], "/"), 1)."</a></td>\n".
				"<td bgcolor=\"".ALTBG1."\">$info[version]</td>\n".
				"<td bgcolor=\"".ALTBG2."\">$info[dateline]</td>\n".

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?