<?php
header("Content-type:text/html;charset=utf-8");
define('IN_DISCUZ', true);
define('FCPATH', dirname(__FILE__).'/');
define('DISCUZ_ROOT', '../../');
if(file_exists('./uploads/alter302.lock'))
{
	echo "重新安装，请删除根目录下的alter302.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('link_switch',1)","INSERT INTO `mocuz_card_setting` (`key`, `value`) values('link_value','')");
foreach($insert_sql as $key => $v){
	if(!mysql_query($v)){
		echo "数据插入失败，请确认是否已存在"."</br>";
	}else{
	  echo "数据插入成功</br>";
	}
}
$query_sql = 'alter table `mocuz_card_channel` add ordernum int(2) not Null default 0;';
if(!mysql_query($query_sql)){
	echo "mocuz_card_channel数据表更新失败"."</br>";
}else{
  echo "mocuz_card_channel数据表更新成功";
}
file_put_contents('./uploads/alter302.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];
}
