<?php
!defined('R_P') && exit('Forbidden');
define('M_P', R_P.'mode/dianpu/');
require_once(M_P . 'require/core.php');
error_reporting(0);
if (empty($step)) {
	@set_time_limit(888);
	//建立商家导航信息数据库表
	$sqlCreateTable = array(
	    'pw_dianpu_shangjia' => array("shangjiaid mediumint(8) unsigned NOT NULL auto_increment, uid int (10) unsigned NOT NULL default 0, username varchar (15) NOT NULL default '',
								    company varchar (100) NOT NULL default '', delegate varchar (15) NOT NULL default '', delegatephone varchar (30) NOT NULL default '',
								    companyaddress varchar (100) NOT NULL default '', companyphone varchar (30) NOT NULL default '', companyfax varchar (30) NOT NULL default '',
								    companyzipcode varchar (10) NOT NULL default '0', createtime int (10) NOT NULL default 0, PRIMARY KEY (shangjiaid)"),
	    'pw_dianpu_dianpubase' => array("dianpuid int (10) unsigned NOT NULL auto_increment, shangjiaid mediumint (8) unsigned NOT NULL default 0, title varchar (30) NOT NULL default '', 
	    						    state tinyint (3) NOT NULL default 0, ifcheck tinyint (3) unsigned NOT NULL default 0, checkway varchar (30) NOT NULL default '', checktime int (10) NOT NULL default 0, 
	    						    address varchar (255) NOT NULL default '', qq varchar (20) NOT NULL default '', msn varchar (100) NOT NULL default '', wangwang varchar (100) NOT NULL default '', 
	    						    email varchar (100) NOT NULL default '', phone varchar (20) NOT NULL default '', map_x varchar (20) NOT NULL default '', map_y varchar (20) NOT NULL default '',
									map_z tinyint (1) NOT NULL default 13, logo varchar (255) NOT NULL default '', bulletin text NOT NULL, recommendlevel tinyint (3) NOT NULL default 0,
									ifattach tinyint (3) NOT NULL default 0, createtime int (11) NOT NULL default 0, modifytime int (11) NOT NULL default 0, overtime int (11) NOT NULL default 0,
									PRIMARY KEY (dianpuid), INDEX idx_state (state), INDEX idx_createtime (createtime), INDEX idx_modifytime (modifytime)"),
	   'pw_dianpu_dianpuextend' => array("dianpuid int (10) unsigned NOT NULL DEFAULT 0, uid int (10) unsigned NOT NULL DEFAULT 0, username varchar (15) NOT NULL DEFAULT '', url varchar (255) NOT NULL DEFAULT '',
									areaid mediumint (8) unsigned NOT NULL DEFAULT 0, categoryid smallint (6) unsigned NOT NULL DEFAULT 0, operator varchar (15) NOT NULL DEFAULT '', operatorphone varchar (20) NOT NULL DEFAULT 0,
									groupid tinyint (3) unsigned NOT NULL DEFAULT 0, businfo varchar (255) NOT NULL DEFAULT '', tempstyle varchar (10) NOT NULL DEFAULT '', contentorder text NOT NULL,
									KEY idx_dianpuid (`dianpuid`)"),
       'pw_dianpu_dianpuindex' => array("dianpuid int (10) unsigned NOT NULL, title varchar (30) NOT NULL default '', state tinyint (3) NOT NULL default 0, areaid mediumint (8) unsigned NOT NULL default 0,
									categoryid int (10) unsigned NOT NULL default 0, parentid int (10) unsigned NOT NULL default 0, groupid smallint (6) unsigned NOT NULL default 0, ifcheck tinyint (3) unsigned NOT NULL default 0,
									checkway varchar (30) NOT NULL default '', recommendlevel tinyint (3) NOT NULL default 0, modifytime int (11) NOT NULL default 0, hit int (10) NOT NULL default 0,
									KEY idx_dianpuid (dianpuid), KEY idx_normal_categoryid (categoryid,areaid,recommendlevel), KEY idx_normal_parentid (parentid,areaid,recommendlevel), KEY idx_normal_areaid (areaid,recommendlevel)"),
	   'pw_dianpu_dianpucontent' => array("contentid int (11) unsigned NOT NULL AUTO_INCREMENT, dianpuid int (10) unsigned NOT NULL DEFAULT 0, sort tinyint (3) unsigned NOT NULL DEFAULT 0, titles text NOT NULL,
									contents text NOT NULL, PRIMARY KEY (contentid), INDEX idx_dianpuid_sort (dianpuid)"),
	   'pw_dianpu_groups' => array("groupid tinyint (3) unsigned NOT NULL auto_increment, title varchar (50) NOT NULL default '', state tinyint (3) NOT NULL default 0, styles text NOT NULL,
									createtime int (10) NOT NULL default 0, configinfo text NOT NULL, PRIMARY KEY (`groupid`)"),
	   'pw_dianpu_recommends' => array("recommendid int (10) unsigned NOT NULL auto_increment, objid int (10) unsigned NOT NULL default 0, objtype tinyint (3) unsigned NOT NULL default 0, createtime int (11) NOT NULL default 0,
									levels tinyint (3) NOT NULL default 0, overtime int (11) NOT NULL default 0, PRIMARY KEY (recommendid), KEY idx_dianpuid_level (objid, levels)"),
	   'pw_dianpu_commentcontents' => array("commentid int(10) unsigned NOT NULL auto_increment, title varchar(50) NOT NULL default '', content text NOT NULL, objectid int(10) UNSIGNED NOT NULL default 0,
									typeid int(10) UNSIGNED NOT NULL default 0, uid int(10) UNSIGNED NOT NULL default 0, username varchar(15) NOT NULL default '', postdate int(10) UNSIGNED NOT NULL default 0,
									state tinyint(3) NOT NULL default 0, isanswer tinyint(3) NOT NULL default 0, PRIMARY KEY ( commentid ), KEY idx_objectid_postdate (objectid, postdate)"),
	   'pw_dianpu_comments' => array("commentid int(10) UNSIGNED NOT NULL default 0, objectid int(10) UNSIGNED NOT NULL default 0, typeid int(10) UNSIGNED NOT NULL default 0, postdate int(10) UNSIGNED NOT NULL default 0,
									KEY idx_commentid (commentid), KEY idx_objectid_postdate (objectid,postdate)"),
	   'pw_dianpu_commentreplys' => array("commentid int(10) UNSIGNED NOT NULL default 0, parentid int(10) UNSIGNED NOT NULL default 0, rootid int(10) UNSIGNED NOT NULL default 0, objectid int(10) UNSIGNED NOT NULL default 0,
									postdate int(10) UNSIGNED NOT NULL default 0, KEY idx_parentid_postdate (parentid,postdate), KEY idx_rootid_postdate (rootid,postdate)"),
	   'pw_dianpu_products' => array("productid int(10) unsigned NOT NULL auto_increment, objectid int(10) unsigned NOT NULL default 0, title varchar(255) NOT NULL default '', description text NOT NULL,
									recommend tinyint(3) NOT NULL default 0, market_price varchar(30) NOT NULL default '', member_price varchar(30) NOT NULL default '', shelf tinyint(3) NOT NULL default 0,
									photoid int(10) unsigned NOT NULL default 0, createdtime int(10) NOT NULL default 0, modifiedtime int(10) NOT NULL default 0, hits int(10) NOT NULL default 0,
									PRIMARY KEY (productid), KEY idx_objectid_createdtime (objectid,createdtime)"),
	   'pw_dianpu_photos' => array("photoid int(10) unsigned NOT NULL auto_increment, objectid int(10) NOT NULL default 0, belongid int(10) NOT NULL default 0, typeid tinyint(3) NOT NULL default 0,
									filename varchar(100) NOT NULL default '', path varchar(255) NOT NULL default '', createdtime int(10) NOT NULL default 0, PRIMARY KEY (photoid),
									KEY idx_typeid_objectid_createdtime (typeid,objectid,createdtime)"),
	   'pw_dianpu_categories' => array("categoryid int(10) unsigned NOT NULL auto_increment, parentid int(10) unsigned NOT NULL default 0, name varchar(20) NOT NULL default '', sort tinyint(3) NOT NULL default 0,
									createdtime int(10) NOT NULL default 0, PRIMARY KEY (categoryid), KEY idx_parentid_sort (parentid,sort)"),
	   'pw_dianpu_areas' => array("areaid int(10) unsigned NOT NULL auto_increment, name varchar(20) NOT NULL default '', sort tinyint(3) NOT NULL default 0, createdtime int(10) NOT NULL default 0,
									PRIMARY KEY (areaid), KEY idx_sort (sort)"),
	   'pw_dianpu_styles' => array("styleid smallint(6) unsigned NOT NULL auto_increment, title varchar(10) NOT NULL default '', filename varchar(10) NOT NULL default '', status tinyint(3) NOT NULL default 0,
									createdtime int(10) NOT NULL default 0, PRIMARY KEY (styleid), KEY idx_filename (filename)"),
	   'pw_dianpu_producthits' => array("id int(10) unsigned NOT NULL , hits int(10) NOT NULL default 0, PRIMARY KEY (id), KEY idx_hits (hits)"),
	   'pw_dianpu_dianpuhits' => array("id int(10) unsigned NOT NULL , hits int(10) NOT NULL default 0, PRIMARY KEY (id), KEY idx_hits (hits)")
    );
	N_createtable($sqlCreateTable,0);
    adminmsg('本次安装分四个步骤，已完成1/4：数据库更新',$basename . '&mode=' . $mode . '&action=setting&step=insertDefaultDianpuData');
	
} elseif ($step == 'insertDefaultDianpuData') {
	@set_time_limit(888);
	$recommendLevel = array('3' => '三钻', '4' => '四钻', '5' => '五钻');
	$checkWay = array( 'mobile' => '电话认证', 'verify' => '工商认证', 'vip' => 'VIP');
	//插入默认的配置信息
	$db->update("REPLACE INTO pw_hack (hk_name, vtype, hk_value,decrip) VALUES ('dianpu_cancomment','string','1','是否能发表评论'),
				('dianpu_listnum','string',15,'前太列表默认显示的数量'), ('dianpu_mapcity','string','杭州','地域设置'),
				('dianpu_mapkey','string','','MAPABC KEY 设置'), ('dianpu_starremark','string','0','是否开启星级点评'),
				('dianpu_recommendLevel','array'," . S::sqlEscape(serialize($recommendLevel)) . ",'推荐默认数据'),
				('dianpu_sitename','string','商家导航','导航名字'),
				('dianpu_checkway','array'," . S::sqlEscape(serialize($checkWay)) . ",'认证默认类型')");
	//插入广告位
	$db->update("INSERT INTO pw_advert (`type`, uid, ckey, stime, etime, ifshow, orderby, descrip, config) VALUES 
				(0, 0, 'Mode.Dianpu.Index.Header', 0, 0, 1, 0, '商家导航首页头部广告位~	~商家导航首页头部广告位', 'a:1:{s:7:\"display\";s:4:\"rand\";}'),
				(0, 0, 'Mode.Dianpu.Index.Right1', 0, 0, 1, 0, '商家导航首页右1~	~商家导航首页右1', 'a:1:{s:7:\"display\";s:4:\"rand\";}'),
				(0, 0, 'Mode.Dianpu.Index.Right2', 0, 0, 1, 0, '商家导航首页右2~	~商家导航首页右边上面开始第2个', 'a:1:{s:7:\"display\";s:4:\"rand\";}'),
				(0, 0, 'Mode.Dianpu.Index.Center1', 0, 0, 1, 0, '商家导航首页中1~ ~商家导航首页中间的广告位', 'a:1:{s:7:\"display\";s:4:\"rand\";}'),
				(0, 0, 'Mode.Dianpu.List.Right1', 0, 0, 1, 0, '商家导航列表页右边下角~	~商家导航列表页右边下角处的广告', 'a:1:{s:7:\"display\";s:4:\"rand\";}'),
				(0, 0, 'Mode.Dianpu.Proinfo.Right1', 0, 0, 1, 0, '商家导航商品内容页右下角~	~商家导航商品内容页右下角', 'a:1:{s:7:\"display\";s:4:\"rand\";}'),
				(0, 0, 'Mode.Dianpu.Info.Right1', 0, 0, 1, 0, '商家导航店铺内容页右下角~	~商家导航店铺信息内容页右下角', 'a:1:{s:7:\"display\";s:4:\"rand\";}')");
	
	//插入默认的店铺组数据
	$db->update("REPLACE INTO pw_dianpu_groups (groupid,title,state,styles,createtime,configinfo) VALUES 
				('1','普通','1','a:1:{i:0;s:1:\"1\";}',UNIX_TIMESTAMP(),'a:8:{s:10:\"uploadlogo\";s:1:\"0\";s:8:\"comments\";s:1:\"0\";s:10:\"dianpushow\";s:1:\"0\";s:11:\"productshow\";s:1:\"0\";s:8:\"wangwang\";s:1:\"0\";s:2:\"qq\";s:1:\"0\";s:10:\"maxproduct\";s:2:\"10\";s:10:\"maxexplain\";s:1:\"1\";}'),
				('2','VIP','1','a:2:{i:0;s:1:\"1\";i:1;s:1:\"2\";}',UNIX_TIMESTAMP(),'a:8:{s:10:\"uploadlogo\";s:1:\"1\";s:8:\"comments\";s:1:\"1\";s:10:\"dianpushow\";s:1:\"1\";s:11:\"productshow\";s:1:\"1\";s:8:\"wangwang\";s:1:\"1\";s:2:\"qq\";s:1:\"1\";s:10:\"maxproduct\";s:2:\"50\";s:10:\"maxexplain\";s:1:\"5\";}')");
	
	if(!$db->get_value("SELECT COUNT(areaid) FROM pw_dianpu_areas")) {
		//插入默认的地区数据
		$db->update("REPLACE INTO pw_dianpu_areas (areaid, name,createdtime) VALUES ('1','西湖区', UNIX_TIMESTAMP()),
					('2','下城区', UNIX_TIMESTAMP()), ('3','萧山区', UNIX_TIMESTAMP()), ('4','江干区', UNIX_TIMESTAMP()),
					('5','上城区', UNIX_TIMESTAMP()), ('6','拱墅区', UNIX_TIMESTAMP()), ('7','余杭区', UNIX_TIMESTAMP()),
					('8','滨江区', UNIX_TIMESTAMP())");
	}
	if(!$db->get_value("SELECT COUNT(categoryid) FROM pw_dianpu_categories")) {
		//插入默认的行业分类数据
		$db->update("REPLACE INTO pw_dianpu_categories (categoryid, name,createdtime) VALUES ('1','休闲娱乐', UNIX_TIMESTAMP()),
					('2','餐饮美食', UNIX_TIMESTAMP()), ('3','旅游酒店', UNIX_TIMESTAMP()), ('4','便民服务', UNIX_TIMESTAMP()),
					('5','美容保健', UNIX_TIMESTAMP()), ('6','装修公司', UNIX_TIMESTAMP()), ('7','购物服务', UNIX_TIMESTAMP()),
					('8','咨询中介', UNIX_TIMESTAMP()), ('9','房产家居', UNIX_TIMESTAMP()), ('10','教育培训', UNIX_TIMESTAMP()),
					('11','交通物流', UNIX_TIMESTAMP()), ('12','车辆服务', UNIX_TIMESTAMP())");
		$db->update("REPLACE INTO pw_dianpu_categories (categoryid, parentid, name, createdtime) VALUES  ('13', '1', '酒吧', UNIX_TIMESTAMP()),
					('14', '2', '中餐馆', UNIX_TIMESTAMP()), ('15', '3', '旅行社', UNIX_TIMESTAMP()), ('16', '4', '搬家', UNIX_TIMESTAMP()),
					('17', '5', '美发', UNIX_TIMESTAMP()), ('18', '6', '现代风格', UNIX_TIMESTAMP()), ('19', '7', '通讯数码', UNIX_TIMESTAMP()),
					('20', '8', '婚介', UNIX_TIMESTAMP()), ('21', '9', '房产经纪', UNIX_TIMESTAMP()), ('22', '10', '学前教育', UNIX_TIMESTAMP()),
					('23', '11', '快递', UNIX_TIMESTAMP()), ('24', '12', '租车', UNIX_TIMESTAMP())");
	}
	adminmsg('本次安装分四个步骤，已完成2/4：默认数据的安装', $basename . '&mode=' . $mode . '&action=setting&step=addstyles');
} elseif ($step == 'addstyles') {
	//风格文件的安装
	@set_time_limit(888);
	define('MODE', M_P);
	$styleHelper = DP::loadClass('stylehelper', 'helper');
	$styleHelper->addStyles($styleHelper->getUnUserStyle());
	adminmsg('本次安装分四个步骤，已完成3/4：风格文件的安装', $basename . '&mode=' . $mode . '&action=setting&step=finish');
} elseif ($step == 'finish') {
	//更新config缓存
	updatecache_conf('dianpu',true);//更新店铺设置的缓存
	$groupService = DP::loadClass("groupservice", 'service');
	$groupService->updateDianpuGroupCache();//更新店铺组的缓存
	$categoriesService = DP::loadClass('categoriesservice', 'service');
	$categoriesService->refreshCategoryCache();//更新分类的缓存
	$areasService = DP::loadClass("areasservice", 'service');
	$areasService->refreshAreaCache();//更新地区的缓存文件
	$stylesService = DP::loadClass("stylesservice", 'service');
	$stylesService->updateDianpuStyleCache();//更新风格的缓存文件	
	updatecache();
	/* 门户化模板文件权限判断 */
	$_portalpage = M_P . 'config/portalpages.php';
	if (file_exists($_portalpage)) {
		$_portalPath = R_P . 'html/portal/';
		$_portalDir = require_once $_portalpage;
		foreach ($_portalDir as $key => $value) {
			if (file_exists($_portalPath.$value)) @chmod($_portalPath.$value,'0777');
		}
	}
	adminmsg('本次安装分四个步骤,已完成4/4: 缓存文件的更新！<br/>恭喜您，安装成功');
}

function N_createtable($array,$start,$records=null){
	global $db,$charset,$times,$record;
	$record || $record = 800;
	$records && $record = $records;
	!is_array($array) && $array = array();
	$array = array_slice($array,$start,$record);
	foreach ($array as $key => $value) {
		!$value[1] && $value[1] = 'MyISAM';
		$value[0] = "CREATE TABLE IF NOT EXISTS $key ($value[0]) ";
		if ($db->server_info() > '4.1') {
			$value[0] .= "ENGINE=$value[1]".($charset ? " DEFAULT CHARSET=$charset" : '');
		} else {
			$value[0] .= "TYPE=$value[1]";
		}
		!empty($value[2]) && $value[0] .= "  AUTO_INCREMENT=$value[2]";
		$db->query($value[0]);
		$times++;
	}
}
?>	   