<?php

header("Content-type:text/html;charset=utf-8");

define('IN_DISCUZ', true);

define('FCPATH', dirname(__FILE__).'/');

define('DISCUZ_ROOT', '../../');

if(file_exists('./uploads/alter306.lock'))

{

	echo "重新安装，请删除根目录下的alter306.lock";

	exit;

}



if(file_exists(DISCUZ_ROOT.'./config/config_global.php')){

	@include DISCUZ_ROOT.'./config/config_global.php';

	$discuz_db = $_config['db'][1];

}else{

	echo '无法找到discuz配置文件,请确认discuz目录正确';

}





$writable = array(FCPATH.'uploads');

$writable_nums = 0;

foreach($writable as $dir){

	if(!is_really_writable($dir)){

		echo $dir.'不可写,请检查目录是否存在或是否有权限<br/>';

		$writable_nums++;

	}

}



if($writable_nums > 0){

	exit;

}





if(!$conn = @mysql_connect($discuz_db['dbhost'], $discuz_db['dbuser'], $discuz_db['dbpw'])){

	echo mysql_error();exit;

}



mysql_select_db($discuz_db['dbname']) or die(mysql_error());



mysql_query("SET NAMES UTF8");

//表添加字段

$insert_sql = array("INSERT INTO `mocuz_card_setting` (`key`, `value`) values('share_content','')");

foreach($insert_sql as $key => $v){

	if(!mysql_query($v)){

		echo "数据插入失败，请确认是否已存在"."</br>";

	}else{

	  echo "数据插入成功</br>";

	}

}


$insert_arr = array();
$insert_arr['mocuz_card_home_channel'] = "CREATE TABLE `mocuz_card_home_channel` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `cid` int(6) NOT NULL,
  `title` varchar(16) DEFAULT NULL,
  `goto_value` varchar(255) DEFAULT NULL,
  `icon` varchar(255) DEFAULT NULL,
  `requiredlogin` tinyint(1) NOT NULL DEFAULT '0',
  `ordernum` tinyint(2) DEFAULT 0,
  `flag` char(10) DEFAULT NULL COMMENT '右上角热帖',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$insert_arr['mocuz_card_discovery_channel'] = "CREATE TABLE `mocuz_card_discovery_channel` (
`id` int(6) NOT NULL AUTO_INCREMENT,
  `cid` int(6) NOT NULL,
  `fid` tinyint(2) NOT NULL default 1,
  `title` varchar(16) DEFAULT NULL,
  `goto_value` varchar(255) DEFAULT NULL,
  `icon` varchar(255) DEFAULT NULL,
  `requiredlogin` tinyint(1) NOT NULL DEFAULT '0',
  `ordernum` tinyint(2) DEFAULT 0,
  `flag` char(10) DEFAULT NULL COMMENT '右上角热帖',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$insert_arr['mocuz_card_channel_floor'] = "CREATE TABLE `mocuz_card_channel_floor` (
`id` int(6) NOT NULL AUTO_INCREMENT,
  `name` char(12) NOT NULL COMMENT '楼层名称',
  `ordernum` tinyint(2) DEFAULT 0,
  `dateline` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
foreach($insert_arr as $key => $value){
	if(!mysql_query($value)){
		echo $key . "表创建失败</br>";
	}else{
		echo $key . "表创建成功</br>";
	}
}

file_put_contents('./uploads/alter306.lock', '123456');







/**

 * Tests for file writability

 *

 * is_writable() returns TRUE on Windows servers when you really can't write to

 * the file, based on the read-only attribute. is_writable() is also unreliable

 * on Unix servers if safe_mode is on.

 *

 * @link	https://bugs.php.net/bug.php?id=54709

 * @param	string

 * @return	bool

 */

function is_really_writable($file)

{

	// If we're on a Unix server with safe_mode off we call is_writable

	if (DIRECTORY_SEPARATOR === '/' && (is_php('5.4') OR ! ini_get('safe_mode')))

	{

		return is_writable($file);

	}



	/* For Windows servers and safe_mode "on" installations we'll actually

	 * write a file then read it. Bah...

	 */

	if (is_dir($file))

	{

		$file = rtrim($file, '/').'/'.md5(mt_rand());

		if (($fp = @fopen($file, 'ab')) === FALSE)

		{

			return FALSE;

		}



		fclose($fp);

		@chmod($file, 0777);

		@unlink($file);

		return TRUE;

	}

	elseif ( ! is_file($file) OR ($fp = @fopen($file, 'ab')) === FALSE)

	{

		return FALSE;

	}



	fclose($fp);

	return TRUE;

}



function is_php($version)

{

	static $_is_php;

	$version = (string) $version;



	if ( ! isset($_is_php[$version]))

	{

		$_is_php[$version] = version_compare(PHP_VERSION, $version, '>=');

	}



	return $_is_php[$version];

}

