<?php
header("Content-type:text/html;charset=utf-8");
define('IN_DISCUZ', true);
define('FCPATH', dirname(__FILE__).'/');
define('DISCUZ_ROOT', '../../');
// if(file_exists('./uploads/alter310.lock'))
// {
// 	echo "重新安装，请删除根目录下的alter310.lock";
// 	exit;
// }
$table310=array('mocuz_card_topic','mocuz_card_post','mocuz_card_post_img','mocuz_card_topic_post','mocuz_card_comment','mocuz_card_topic_participation','mocuz_card_post_like','mocuz_card_comment_like','mocuz_card_follow','mocuz_card_keywords','mocuz_card_news','mocuz_card_home_channel','mocuz_card_discovery_channel','mocuz_card_channel_floor');
$table311=array('mocuz_ommon_member_wechat_bind','mocuz_member_password_status');

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");

$is303=false;
$sql="select * from mocuz_card_setting where `key`='link_switch'";
if(mysql_num_rows(mysql_query($sql))==1){
	$is303=true;
}
if(!$is303){
	echo '===========303脚本==============<br/>';
	echo '您当前版本还不是303版本,请将代码上传完毕后依次执行以下脚本文件:<br/>';
	echo '1.<a target="_blank" href="http://'.$_SERVER['HTTP_HOST'].'/mocuz/3.0/install/install_303.php">执行install_303.php脚本</a>';
	echo '2.<a target="_blank" href="http://'.$_SERVER['HTTP_HOST'].'/mocuz/manage/alter.php">执行alter.php脚本</a>';
}
//表添加字段s
echo '===========310脚本==============<br/>';
$is310=false;
foreach($table310 as $table){
    $query_sql = "SHOW TABLES LIKE '{$table}'";
    if(mysql_num_rows(mysql_query($query_sql))==1){
		$is310=true;
        break;
    }
	
}
if(!$is310){
	echo '您当前版本还不是310版本,请将代码上传完毕后依次执行以下脚本文件:<br/>';
	echo '1.<a target="_blank" href="http://'.$_SERVER['HTTP_HOST'].'/mocuz/manage/alter306.php">执行alter306.php脚本</a><br/>';
	echo '2.<a target="_blank" href="http://'.$_SERVER['HTTP_HOST'].'/mocuz/manage/alter310.php">执行alter310.php脚本</a><br/>';
	echo '3.<a target="_blank" href="http://'.$_SERVER['HTTP_HOST'].'/mocuz/manage/install310.php">执行install310.php脚本</a><br/>';
	echo '4.<a target="_blank" href="http://'.$_SERVER['HTTP_HOST'].'/mocuz/3.0/install/mocuz_smiles310.php">执行mocuz_smiles310.php脚本</a><br/>';
}
echo '===========311脚本==============<br/>';
$is311=false;
foreach($table311 as $table){
    $query_sql = "SHOW TABLES LIKE '{$table}'";
    if(mysql_num_rows(mysql_query($query_sql))==1){
		$is311=true;
        break;
    }
	
}

if(!$is311){
	echo '您当前版本还不是311版本,请将代码上传完毕后依次执行以下脚本文件:<br/>';
	echo '1.<a target="_blank" href="http://'.$_SERVER['HTTP_HOST'].'/mocuz/manage/addtable311.php">执行addtable311.php脚本</a>';
}

if($is310&&$is311&&$is303){
	echo '您当前版本已经是最新版本';
}

/**
 * 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];
}
