query("show variables like '%storage_engine%'");
$engine=$result[0][value];
$sql=preg_replace("/(?i)ENGINE=([a-zA-Z])+ /", "ENGINE=".$engine." ", $sql);
//
$sql = explode(";\n", $sql);
//替换表前缀
$default_tablepre = "cms_";
$sql = str_replace(" `{$default_tablepre}", " `{$tablepre}", $sql);
//开始安装
sp_show_msg('开始安装数据库...');
foreach ($sql as $item) {
$item = trim($item);
if(empty($item)) continue;
preg_match('/CREATE TABLE `([^ ]*)`/', $item, $matches);
if($matches) {
$table_name = $matches[1];
$msg = "创建数据表{$table_name}";
if(false !== $db->execute($item)){
sp_show_msg($msg . ' 完成');
} else {
sp_show_msg($msg . ' 失败!', 'error');
}
} else {
$db->execute($item);
}
}
}
/**
* 显示提示信息
* @param string $msg 提示信息
*/
function sp_show_msg($msg, $class = ''){
echo "";
flush();
ob_flush();
}
function sp_update_site_configs($db,$table_prefix){
$data=array();
$data["sitename"]=I("post.sitename");
$data["pagetitle"]=I("post.sitename");
$data["pagekey"]=I("post.sitekeywords");
$data["pagedes"]=I("post.siteinfo");
$data["domain"]=I("post.siteurl");
$data["indexFileName"]="index.html";
$data["indexType"]="dynamic";
$settingstr="execute("delete from `{$table_prefix}user` where user_id=1 or user_name='$username'");
//
$sql =<<execute($sql);
sp_show_msg("管理员账号创建成功!");
}
/**
* 写入配置文件
* @param array $config 配置信息
*/
function sp_create_config($config){
if(is_array($config)){
//读取配置内容
$conf = file_get_contents(MODULE_PATH . 'Data/config.php');
//替换配置项
foreach ($config as $key => $value) {
$conf = str_replace("#{$key}#", $value, $conf);
}
$conf = str_replace('#COOKIE_PREFIX#', sp_random_string(6) . "_", $conf);
//写入应用配置文件
if(file_put_contents( APP_PATH.'Common/Conf/db.php', $conf)){
sp_show_msg('配置文件写入成功');
} else {
sp_show_msg('配置文件写入失败!', 'error');
}
return '';
}
}
/**
* 返回带协议的域名
*/
function sp_get_host(){
$host=$_SERVER["HTTP_HOST"];
$protocol=is_ssl()?"https://":"http://";
return $protocol.$host;
}
/**
* 随机字符串生成
* @param int $len 生成的字符串长度
* @return string
*/
function sp_random_string($len = 6) {
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars); // 将数组打乱
$output = "";
for ($i = 0; $i < $len; $i++) {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
/**
* CMF密码加密方法
* @param string $pw 要加密的字符串
* @return string
*/
function sp_password($pw,$authcode=''){
if(empty($authcode)){
$authcode=C("AUTHCODE");
}
$result="###".md5(md5($authcode.$pw));
return $result;
}