<?php
header("Content-type:text/html;charset=utf-8");
// define('IN_DISCUZ', true);
define('FCPATH', dirname(__FILE__).'/');
// define('DISCUZ_ROOT', '../');
require '../../source/class/class_core.php';
header('Content-Type:text/html; charset=utf-8');
error_reporting(0);
C::app()->init();
// 运行环境检查
$required_exts = array('mysqli');
foreach($required_exts as $ext_name) {
    if(!extension_loaded($ext_name)){
        echo '<li style="color:#ff0000">必要扩展 '.$ext_name.' 没有安装</li>';
        if($ext_name == 'mysqli'){
            echo '<p style="color:#999999;">服务器不支持 '.$ext_name.' 扩展，您可以将数据库连接方式修改为 mysql.</p>';
        }
    }
}

if(file_exists('./uploads/install310.lock'))
{
	echo "重新安装，请删除根目录下的install310.lock";
	exit;
}

if(file_exists('../../config/config_global.php')){
	@include '../../config/config_global.php';
	$discuz_db = $_config['db'][1];
}else{
	echo '无法找到discuz配置文件,请确认discuz目录正确';
}



$cardpathscore = dirname(FCPATH).'/card/uploads/score';

$writable = array(FCPATH.'uploads', FCPATH.'uploads/qrcode', FCPATH.'captcha',$cardpathscore);
$writable_nums = 0;
foreach($writable as $dir){
	if(!is_really_writable($dir)){
		echo $dir.'不可写,请检查目录是否存在或是否有权限<br/>';
		$writable_nums++;
	}
}

if($writable_nums > 0){
	exit;
}


echo '开始建立数据表<br/>';
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());


$db_sql = file_get_contents('./database/sharecircle.sql');
$db_sql_arr = explode(';', $db_sql);

foreach($db_sql_arr as $sql){
	$sql = trim($sql);
	if(empty($sql)){
		continue;
	}
	if(!mysql_query($sql)){
		echo $sql;
		echo mysql_error().'<br/>';
		echo '建表失败,请确认discuz目录配置是否正确，数据库文件引用是否正确';
		exit;
	}
}

echo '<font color="blue">数据表建立完毕<br/><br/><br/></font>';
echo '开始更新数据表<br/>';
file_put_contents('./uploads/install310.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];
}
