Location: PHPKode > scripts > Benson Bank CMS > index.php
<?php
	@include "inc.page.php";
	@main($setup,$action);

	function main($setup,$action)
	{
		if (@exist("","select 1 from bsb_book limit 1")){
			@header("Location:home.php");//already v3.0
			return;
		}
		@uppe();
		echo "<div style='width:600px;margin:0 auto'>";
		@nav1_guest($tab);
		$desc_book = "CREATE TABLE bsb_book(
his_id int(10) unsigned NOT NULL auto_increment PRIMARY KEY,
pid mediumint(8) unsigned NOT NULL default 0,
created timestamp NOT NULL default CURRENT_TIMESTAMP,
credit smallint(5) unsigned NOT NULL default 0,
debit smallint(5) unsigned NOT NULL default 0,
remark varchar(50) NOT NULL default '',
ccy char(3) NOT NULL default 'CNY',
amt float(9,2) NOT NULL default 0.00)";
		$desc_term = "CREATE TABLE bsb_term(
his_id int(10) unsigned NOT NULL default 0 PRIMARY KEY,
remark varchar(50) NOT NULL default '',
dateto date NOT NULL default '0000-00-00',
rate_y float(5,3) NOT NULL default 0.000)";
		echo "<p class='bt bb bl br bg' style='padding:5px'><span class='green'>"hide@address.com(1001)."</span><br />"hide@address.com(1002,@array("<b class='green'>inc.func.php</b>","<b class='green'>function db_conn()</b>","<b class='blue'>host user pass db</b>"))."</p>";
		if (@exist("","select 1 from bsb_cashbook_cd limit 1")) @main_upgrade($setup,$action,$desc_book,$desc_term);
		else{//setup
			if ($action) $err = @main_save($setup,$desc_book,$desc_term);
			if (!$action || $err) @main_form($setup,$err);
		}
		echo "</div>";
		@down();
	}
	function main_upgrade($setup,$action,$desc_book,$desc_term)//upgrade from v2 to v3
	{
		if ($action){
			$err = @main_upgrade_save($setup,$desc_book,$desc_term);
			if (!$err) return;
		}
		echo @html("form"),"<p class='err'>$err</p>
			<p class='bt bb bl br bg' style='padding:5px'><span class='green'>"hide@address.com(1042)."</span></p>
			<p>A. "hide@address.com(1044).": <i class='green'>create table</i></p>
			<table>
			<tr><td>"hide@address.com(1005).":</td><td>"hide@address.com("text","setup[root_user]",$setup['root_user'],100)."</td><td>"hide@address.com(1008).": root</td></tr>
			<tr><td>"hide@address.com(1006).":</td><td>"hide@address.com("password","setup[root_pass]",$setup['root_pass'],100)."</td><td></td></tr>
			</table>
			<p align='center'>"hide@address.com("reset","action",@lang(1019),80)
			." "hide@address.com("submit","action",@lang(1043),80)."</p></form>";
	}
	function main_upgrade_save($setup,$desc_book,$desc_term)
	{
		$setup = @strip($setup,1,1);
		if (!$setup['root_user']) $err .= @lang(1022)."<br />";
		$db = @get_row("","select database()");
		$conn = @mysql_connect($setup['host'],$setup['root_user'],$setup['root_pass']);
		if (!$conn) $err .= @lang(1023)."<br />";
		if ($err) return $err;

		//create table
		@mysql_query("use `$db[0]`");
		$err = @mysql_errno();
		if ($err) return "Err $err : "hide@address.com();
		$tab = "bsb_book";
		$err = @main_save_table($tab,$desc_book);
		if ($err) return $err;

		$tab = "bsb_term";
		$err = @main_save_table($tab,$desc_term);
		if ($err) return $err;

		//import old data - cashbook + cd > book
		@mysql_query("insert into bsb_book
select a.his_id,a.pid,a.created,a.cat_id credit,c.cat_id debit,a.remark,a.ccy,a.amt
from bsb_cashbook a,bsb_cashbook_cd b,bsb_cashbook c
where a.his_id=b.credit_his_id and c.his_id=b.debit_his_id order by 1");
		$err1 = @mysql_errno();
		if ($err1) echo "<p class='err'>Err $err1 : ",@mysql_error(),"</p>";

		//impoer old data - future > term
		@mysql_query("insert into bsb_term select b.credit_his_id,a.remark,a.dateto,a.rate_y
from bsb_future a,bsb_cashbook_cd b where a.his_id=b.debit_his_id");
		$err2 = @mysql_errno();
		if ($err2) echo "<p class='err'>Err $err2 : ",@mysql_error(),"</p>";

		//err checking: Err if negative
		$row = @get_row("","select min(amt) from bsb_book");
		if ($row[0]<0) echo "<p class='err'>Err: ",@lang(1045,'bsb_book'),"</p>";
		else echo "<p class='ok'>",@lang(1046,'bsb_cashbook, bsb_cashbook_cd'),"</p>";

		//err checking: if no records returned - ok
		$row = @get_row("","select count(*) from bsb_term where his_id not in (select his_id from bsb_book)");
		if ($row[0]) echo "<p class='err'>Err: ",@lang(1047,@array($row[0],'bsb_term','bsb_future')),"</p>";
		else echo "<p class='ok'>",@lang(1046,'bsb_future'),"</p>";

		if (!$err1 && !$err2) echo "<p class='bsb'>"hide@address.com(1037)."</p>";
		echo "<p class='bsb'><a href='home.php'>"hide@address.com(1038)."</a></p>";
	}
	function main_form($setup,$err)
	{
		echo @html("form")."<p class='err'>$err</p>
			<p>A: "hide@address.com(1003)."</p><p>"hide@address.com(1004).": <i class='green'>create database; create user; create table</i></p>
			<table>
			<tr><td>"hide@address.com(1005).":</td><td>"hide@address.com("text","setup[root_user]",$setup['root_user'],100)."</td><td>"hide@address.com(1008).": root</td></tr>
			<tr><td>"hide@address.com(1006).":</td><td>"hide@address.com("password","setup[root_pass]",$setup['root_pass'],100)."</td><td></td></tr>
			<tr><td>"hide@address.com(1007).":</td><td>"hide@address.com("text","setup[host]",$setup['host'],100)."</td><td>"hide@address.com(1008).": localhost</td></tr>
			<tr><td colspan='3'><br />B. "hide@address.com(1009)." a-z_0-9</td></tr>
			<tr><td>"hide@address.com("radio","setup[db]",$setup['db'],"","","",@array(@lang(1010).":"))."</td><td>"hide@address.com("text","setup[db_new]",$setup['db_new'],100,30)."</td><td>"hide@address.com(1011)."</td></tr>
			<tr><td>"hide@address.com("radio","setup[db]",$setup['db'],"","","",@array(1=>@lang(1012).":"))."</td><td>"hide@address.com("text","setup[db_old]",$setup['db_old'],100)."</td><td>"hide@address.com(1013)."</td></tr>
			<tr><td colspan='3'><br />C. "hide@address.com(1014)."</td></tr>
			<tr><td>"hide@address.com("radio","setup[bsb_user]",$setup['bsb_user'],"","","",@array(@lang(1015).":"))."</td><td>"hide@address.com("text","setup[bsb_user_new]",$setup['bsb_user_new'],100,30)."</td><td>"hide@address.com(1011)."</td></tr>
			<tr><td align='right'>"hide@address.com(1016).":</td><td>"hide@address.com("text","setup[bsb_pass_new]",$setup['bsb_pass_new'],100,30)."</td><td></td></tr>
			<tr><td>"hide@address.com("radio","setup[bsb_user]",$setup['bsb_user'],"","","",@array(1=>@lang(1017).":"))."</td><td>"hide@address.com("text","setup[bsb_user_old]",$setup['bsb_user_old'],100)."</td><td>"hide@address.com(1013)."</td></tr>
			<tr><td align='right'>"hide@address.com(1016).":</td><td>"hide@address.com("text","setup[bsb_pass_old]",$setup['bsb_pass_old'],100)."</td><td></td></tr>
			</table>
			<p>"hide@address.com(1018,@array("<i class='green'>select insert delete update</i>","<b>B</b>"))."</p>
			<p align='center'>"hide@address.com("reset","action",@lang(1019),80)
			." "hide@address.com("submit","action",@lang(1020),80)."</p></form>";
	}
	function main_save(&$setup,$desc_book,$desc_term)
	{
		$setup = @strip($setup,1,1);
		if (!$setup['host']) $err = @lang(1021)."<br />";
		if (!$setup['root_user']) $err .= @lang(1022)."<br />";
		$conn = @mysql_connect($setup['host'],$setup['root_user'],$setup['root_pass']);
		if (!$conn) $err .= @lang(1023)."<br />";
		if ($err) return $err;

		if (!$setup['db']){//new db
			unset($setup['db_old']);
			$setup['db_new'] = @main_valid_name($setup['db_new']);
			$db = $setup['db_new'];
			if (!$db) return @lang(1024);
			@mysql_query("use `$db`");
			if (!@mysql_errno()) return @lang(1025,$db);
		}else{
			unset($setup['db_new']);
			$db = $setup['db_old'];
			if (!$db) return "B: "hide@address.com(1026);
			@mysql_query("use `$db`");
			if (@mysql_errno()) return @lang(1027,"$setup[db_old]").":<br />"hide@address.com();
		}

		if (!$setup['bsb_user']){//new user
			unset($setup['bsb_user_old']); unset($setup['bsb_pass_old']);
			$setup['bsb_user_new'] = @main_valid_name($setup['bsb_user_new']);
			$user = $setup['bsb_user_new'];
			if (!$user) return "C: "hide@address.com(1028);
			$res = @mysql_query("select 1 from mysql.user where user='$user' and host='$setup[host]' limit 1");
			if (@mysql_errno()) return "A: "hide@address.com(1029);
			$num = @mysql_num_rows($res);
			if ($num) return "C: "hide@address.com(1030,$user);
		}else{
			unset($setup['bsb_user_new']); unset($setup['bsb_pass_new']);
			$user = $setup['bsb_user_old'];
			if (!$user) return "C: "hide@address.com(1031);
			$res = @mysql_query("select 1 from mysql.user where user='$user' and host='$setup[host]' limit 1");
			if (@mysql_errno()) return "A: "hide@address.com(1029);
			$num = @mysql_num_rows($res);
			if (!$num) return "C: "hide@address.com(1032,$user);
		}

		//now no err so far -- start to run script
		if (!$setup['db']){//new db
			@mysql_query("create database `$db`");
			$err = @mysql_errno();
			if ($err) return "Err $err : "hide@address.com();
			echo "<p>"hide@address.com(1033,$db)."</p>";
		}

		if (!$setup['bsb_user']){//new user
			$pass = $setup['bsb_pass_new'];
			@mysql_query("insert into mysql.user(Host,User,Password) values('$setup[host]','$user',password('$pass'))");
			$err = @mysql_errno();
			if ($err) return "Err $err : "hide@address.com();
			echo "<p>"hide@address.com(1034,$user)."</p>";
			@mysql_query("flush privileges");
			$err = @mysql_errno();
			if ($err) return "Err $err : "hide@address.com();
			@mysql_query("GRANT SELECT, INSERT, UPDATE, DELETE ON $db".".* TO '$user'@'$setup[host]'");
			$err = @mysql_errno();
			if ($err) return "Err $err : "hide@address.com();
			@mysql_query("flush privileges");
			echo "<p>"hide@address.com(1035,$user).": select insert delete update</p>";
		}else $pass = $setup['bsb_pass_old'];
		echo "<p>"hide@address.com(1036,@array("<b>inc.func.php</b>","<b>function db_conn()</b>")).":<font color='#990000'>
			<br />\$host = \"$setup[host]\";<br />\$user = \"$user\";<br />\$pass = \"$pass\";<br />\$db = \"$db\";</font></p>";

		//create table
		@mysql_query("use `$db`");
		$tab = "bsb_account";
		$desc = "CREATE TABLE bsb_account(
pid mediumint(8) unsigned NOT NULL default 0 PRIMARY KEY,
ccy_default char(3) NOT NULL default 'CNY',
ccy_list varchar(51) NOT NULL default 'AUD,CAD,CHF,CNY,EUR,GBP,HKD,JPY,MOP,NZD,SGD,USD',
fin_month tinyint(2) unsigned NOT NULL default 0,
rep_round tinyint(1) NOT NULL default 0,
cat_income varchar(15) NOT NULL default 1,
cat_invest varchar(15) NOT NULL default 3,
pass varchar(32) NOT NULL default '',
created date NOT NULL default '0000-00-00',
updated timestamp NOT NULL default '0000-00-00 00:00:00',
sid varchar(32) NOT NULL default '',
ip int(10) unsigned NOT NULL default 0,
error tinyint(2) unsigned NOT NULL default 0,
UNIQUE KEY sid (sid))";
		$data = "INSERT INTO bsb_account VALUES
(1,'CNY','AUD,CNY,HKD,USD',0,0,1,3,'','2007-08-28','',1,0,0),
(2,'USD','AUD,CAD,CNY,GBP,USD',0,0,1,3,'','2008-04-02','',2,0,0),
(100,'CNY','AUD,CNY,HKD,USD',0,0,1,3,'0c4eff1e12a54b1971355765316b8701','2007-08-28','',100,0,0),
(101,'USD','AUD,CAD,CNY,GBP,USD',0,0,1,3,'b630ab06203bec7aa80be97b27c6df16','2008-04-02','',101,0,0)";
		$err = @main_save_table($tab,$desc,$data);
		if ($err) return $err;

		$tab = "bsb_book";
		$data = "INSERT INTO bsb_book VALUES
(1,100,'2008-02-18',102,30201,'阿弥陀佛红包','CNY',900),
(2,100,'2008-02-28',101,30101,'','CNY',8000),
(3,100,'2008-03-18',30101,30202,12566,'CNY',5000),
(4,100,'2008-03-28',30101,202,'','CNY',1000),
(5,101,'2008-03-18',101,30101,'','AUD',8000),
(6,101,'2008-03-18',30101,30201,1568,'AUD',5000),
(7,101,'2008-03-28',30101,201,'Food','AUD',200)";
		$err = @main_save_table($tab,$desc_book,$data);
		if ($err) return $err;

		$tab = "bsb_category";
		$desc = "CREATE TABLE bsb_category(
pid mediumint(8) unsigned NOT NULL default 0,
cat_id smallint(5) unsigned NOT NULL default 0,
category varchar(20) NOT NULL default '',
PRIMARY KEY (pid,cat_id),
UNIQUE KEY category (pid,category))";
		$data = "INSERT INTO bsb_category VALUES
(1,1,'总收入'),(1,2,'总支出'),(1,3,'总资金'),
(1,101,'工资'),(1,102,'收益'),(1,201,'伙食'),(1,202,'房租'),(1,203,'交通'),(1,204,'医疗'),(1,205,'教育'),(1,206,'交际'),(1,207,'税金'),(1,301,'活期'),(1,302,'定期'),(1,303,'借贷'),(1,304,'公积'),
(1,30101,'现金'),(1,30103,'农行活期'),(1,30201,'浦发理财'),(1,30202,'招行定期'),(1,30401,'养老金'),(1,30402,'住房公积'),
(2,1,'Income'),(2,2,'Expense'),(2,3,'Invest'),
(2,101,'Salary'),(2,102,'Profit'),(2,201,'Living'),(2,202,'Rent'),(2,203,'Travel'),(2,204,'Health'),(2,205,'Educate'),(2,206,'Other'),(2,207,'Tax'),(2,301,'Saving'),(2,302,'Term'),(2,303,'Loan'),(2,304,'Super'),
(2,30101,'Cash'),(2,30102,'HSBC Cash'),(2,30201,'HSBC CD'),(2,30202,'ING CD')";
		$err = @main_save_table($tab,$desc,$data);
		if ($err) return $err;
		else @mysql_query("insert into bsb_category select pid+99,cat_id,category from bsb_category where pid<3 order by 1,2");

		$tab = "bsb_ccy";
		$desc = "CREATE TABLE bsb_ccy(ccy char(3) NOT NULL default '' PRIMARY KEY,ccy_cn varchar(20) NOT NULL default '')";
		$data = "INSERT INTO bsb_ccy VALUES('AUD','澳大利亚元'),('CAD','加拿大元'),('CHF','瑞士法郎'),('CNY','人民币'),('DKK','丹麦克朗'),('EUR','欧元'),('GBP','英镑'),('HKD','港币'),('JPY','日元'),('MOP','澳门元'),('NOK','挪威克朗'),('NZD','新西兰元'),('SEK','瑞典克朗'),('SGD','新加坡元'),('USD','美元')";
		$err = @main_save_table($tab,$desc,$data);
		if ($err) return $err;

		$tab = "bsb_term";
		$data = "INSERT INTO bsb_term VALUES(1,'Auto','2009-01-01',12),(3,'CDOD','2009-01-01',15),(6,'Renew','2010-01-01',8.8)";
		$err = @main_save_table($tab,$desc_term,$data);
		if ($err) return $err;
		echo "<p class='bsb'>"hide@address.com(1037)."</p><p class='bsb'><a href='home.php'>"hide@address.com(1038)."</a></p>";
	}
	function main_save_table($tab,$desc,$data)
	{
		@mysql_query("select 1 from $tab limit 1");
		if (!@mysql_errno()) echo "<br /><font color='#cc0000'>"hide@address.com(1039,$tab)."</font>";
		else{
			@mysql_query($desc);
			$err = @mysql_errno();
			if ($err) return "Err $err : "hide@address.com();
			echo "<br />"hide@address.com(1040,$tab);
			if ($data){
				@mysql_query($data);
				$err = @mysql_errno();
				if ($err) return "Err $err : "hide@address.com();
				echo "<br />"hide@address.com(1041,$tab);
			}
		}
	}
	function main_valid_name($str)
	{
		$len = @strlen($str);
		for ($i=0;$i<$len;$i++){
			$cur = @strtolower(@substr($str,$i,1));
			$ord = @ord($cur);
			if ((($cur=="_" || ($ord>47 && $ord<58)) && $STR) || ($ord>96 && $ord<123)) $STR .= $cur;
		}
		return $STR;
	}
?>
Return current item: Benson Bank CMS