qc.ifish7.com/Application/Install/Common/function.php

190 lines
5.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
function sp_testwrite($d) {
$tfile = "_test.txt";
$fp = @fopen($d . "/" . $tfile, "w");
if (!$fp) {
return false;
}
fclose($fp);
$rs = @unlink($d . "/" . $tfile);
if ($rs) {
return true;
}
return false;
}
function sp_dir_create($path, $mode = 0777) {
if (is_dir($path))
return true;
$ftp_enable = 0;
$path = sp_dir_path($path);
$temp = explode('/', $path);
$cur_dir = '';
$max = count($temp) - 1;
for ($i = 0; $i < $max; $i++) {
$cur_dir .= $temp[$i] . '/';
if (@is_dir($cur_dir))
continue;
@mkdir($cur_dir, 0777, true);
@chmod($cur_dir, 0777);
}
return is_dir($path);
}
function sp_dir_path($path) {
$path = str_replace('\\', '/', $path);
if (substr($path, -1) != '/')
$path = $path . '/';
return $path;
}
function sp_execute_sql($db,$file,$tablepre){
//读取SQL文件
$sql = file_get_contents(MODULE_PATH . 'Data/'.$file);
$sql = str_replace("\r", "\n", $sql);
//替换存储类型
$result=$db->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 "<script type=\"text/javascript\">showmsg(\"{$msg}\", \"{$class}\")</script>";
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="<?php \n return ".var_export($data,true).";";//配置文件str
$setting_path=APP_PATH."Common/Conf/setting.php";//配置文件路径
file_put_contents($setting_path,$settingstr);//保存到配置文件里
sp_show_msg("网站信息配置成功!");
}
function sp_create_admin_account($db,$table_prefix){
$username=I("post.manager");
$password=md6(I("post.manager_pwd"));
$email=I("post.manager_email");
$create_date=date("Y-m-d h:i:s");
$ip=get_client_ip(0,true);
//删除之前已存在的userid为1或者用户名为admin的用户
$db->execute("delete from `{$table_prefix}user` where user_id=1 or user_name='$username'");
//
$sql =<<<hello
INSERT INTO `{$table_prefix}user`
(user_id,user_name,user_password,is_admin,status) VALUES
('1', '{$username}', '{$password}', '1', '1');;
hello;
$db->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;
}