<?php
/**
 * LiangJing TEAM1
 * ============================================================================
 * * COPYRIGHT LiangJing 2012-2013.
 * http://www.liangjing.org;
 * ----------------------------------------------------------------------------
 * Author:LiangJing
 * Id: backup.php 2012-10-23
 */
ob_start();
require_once 'include/install.inc.php';
require_once 'include/install.function.php';
require_once 'header.php';
checkInstall();
function PassGen($length = 8)
{
    $str = 'abcdefghijkmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    for ($i = 0, $passwd = ''; $i < $length; $i++)
        $passwd .= substr($str, mt_rand(0, strlen($str) - 1), 1);
    return $passwd;
}
$rootpath    = '../';
$selfurl     = $_SERVER['PHP_SELF'];
$installroot = str_replace("install/index.php", "", $selfurl);
$installroot = str_replace("install/", "", $installroot);
define('IN_LiangJing', true);
include_once('./include/common.php');
//开启SESSION
session_start();
/* step操作项的初始化 */
if (empty ($_REQUEST['step'])) {
    $step = 'welcome';
} else {
    $step = trim($_REQUEST['step']);
}
if ($step == 'welcome') {
    $title = $_LANG['welcome'];
    include $install->tpl('welcome');
} elseif ($step == 'check') {
    $title = $_LANG['LJPHPCMS'] . " &rsaquo; " . $_LANG['check'];
    /* 系统信息 */
    $sys_info['os'] = PHP_OS;
    $sys_info['web_server'] = $_SERVER['SERVER_SOFTWARE'];
    $sys_info['php_ver'] = PHP_VERSION;
    $sys_info['mysql_ver'] = extension_loaded('mysql') ? $_LANG['yes'] : $_LANG['no'];
    $sys_info['zlib'] = function_exists('gzclose') ? $_LANG['yes'] : $_LANG['no'];
    $sys_info['timezone'] = function_exists("date_default_timezone_get") ? date_default_timezone_get() : $_LANG['no_timezone'];
    $sys_info['socket'] = function_exists('fsockopen') ? $_LANG['yes'] : $_LANG['no'];
    $sys_info['gd'] = extension_loaded("gd") ? $_LANG['yes'] : $_LANG['no'];
    /* 检查目录 */
    $check_dirs = array(
        //'admin',
        'install',
        //'css',
        'data',
        //	'rewrite',
        //	'images',
        'source',
        'tpl'
    );
    foreach ($check_dirs AS $dir) {
        $full_dir = ROOT_PATH . $dir;
        $writeable = $install->check_writeable($full_dir);
        if ($writeable == '1') {
            $if_write = "<b class='write'>" . $_LANG['write'] . "</b>";
        } elseif ($writeable == '0') {
            $if_write = "<b class='noWrite'>" . $_LANG['no_write'] . "</b>";
            $no_write = true;
        } elseif ($writeable == '2') {
            $if_write = "<b class='noWrite'>" . $_LANG['not_exist'] . "</b>";
            $no_write = true;
        }
        $writeable_dir[$dir] = $if_write;
    }
    //根据 Web 服务器 信息配置伪静态文件
    if (stristr($sys_info['web_server'], "Apache")) {
        $rewrite_file = ".htaccess.txt";
    } elseif (stristr($sys_info['web_server'], "nginx")) {
        $rewrite_file = ".nginx.txt";
    } elseif (stristr($sys_info['web_server'], "IIS")) {
        $iis_exp = explode("/", $sys_info['web_server']);
        $iis_ver = $iis_exp['1'];
        if ($iis_ver >= 7.0) {
            $rewrite_file = "web.config.txt";
        } else {
            $rewrite_file = "httpd.ini.txt";
        }
    }
    //复制rewrite文件到站点根目录
    if ($rewrite_file) {
        $source = ROOT_PATH . "install/rewrite/" . $rewrite_file;
        $destination = ROOT_PATH . $rewrite_file;
        @copy($source, $destination);
    }
    include $install->tpl('check');
} elseif ($step == 'setting') {
    $is_english = $_POST['is_english'];
    $title      = $_LANG['LJPHPCMS'] . " &rsaquo; " . $_LANG['setting'];
    //如果提交表单执行以下
    if ($_POST['install']) {
        //生成config文件内容
        $config_str = "<?php\n";
        $config_str .= "/**\n";
        $config_str .= " * LiangJing TEAM 豆壳\n";
        $config_str .= " * ============================================================================\n";
        $config_str .= " * * COPYRIGHT LiangJing 2012-2013.\n";
        $config_str .= " * http://www.liangjing.org;\n";
        $config_str .= " * ----------------------------------------------------------------------------\n";
        $config_str .= " * Author:LiangJing\n";
        $config_str .= " * Id: config.php 2012-10-19\n";
        $config_str .= "*/\n\n";
        $config_str .= "///数据库类型\n";
        $config_str .= "define('DB_TYPE','mysql');\n";
        $config_str .= "///数据库编码\n";
        $config_str .= "define('DB_CHARSET','utf8');\n";
        $config_str .= "///数据库服务器\n";
        $config_str .= "define('DB_HOST','" . trim($_POST[dbhost]) . "');\n";
        $config_str .= "///数据库名\n";
        $config_str .= "define('DB_DATA','" . trim($_POST[dbname]) . "');\n";
        $config_str .= "///数据库登录帐号\n";
        $config_str .= "define('DB_USER','" . trim($_POST[dbuser]) . "');\n";
        $config_str .= "///数据库登录密码\n";
        $config_str .= "define('DB_PASS','" . trim($_POST[dbpass]) . "');\n";
        $config_str .= "///数据表扩展\n";
        $config_str .= "define('DB_PREFIX','" . trim($_POST[prefix]) . "');\n";
        $config_str .= "///数据库持久连接 0=关闭, 1=打开\n";
        $config_str .= "define('DB_PCONNECT',0);\n";
        $config_str .= "?>";
        $fopen_config = fopen($file_config, "w+");
        fwrite($fopen_config, $config_str);
        /* 生成配置文件 */
        $config_contents = "<?php
/**
 * @CopyRight  (C)2006-2011 LiangJing Development team Inc.
 * @WebSite    www.liangjing.org www.asp99.cn
 * @Author     Liangjing.org <asp3721@hotmail.com>
**/
///设置时区
date_default_timezone_set('Asia/Shanghai');
///系统安装目录
define('LJCMS_ROOT','" . $installroot . "');
///系统Cookies名
define('LJCMS_COOKIENAME','" . PassGen(8) . "');
///系统编码
define('LJCMS_CHARSET','utf-8');
///自定义链接路径
define('PATH_URL','" . $installroot . "');
///前台默认模版目录 不需要填写tpl目录
define('__TPLDIR__', 'default');
///后台默认模版目录 不需要填写tpl目录
define('__ADMIN_TPLDIR__', 'admincp');
///后台管理文件
define('__ADMIN_FILE__', 'admincp.php');
///系统随机码
define('LJCMS_RANDKEY', '5ei2y2xmybdj8hps');
///设置文件体积大小
define('LJCMS_MEMORY_LIMIT', '32M');
///类型
define('LJCMS_STYPE', '1');
///设置OA系统表前缀
define('DB_PREFIXOA','oa_');
///设置库存系统表前缀
define('DB_PREFIXSA','sale_');
///安装标识
define('LJCMS', true);
?>";
        $configfilenum   = fopen($rootpath . "source/conf/config.inc.php", "w");
        ftruncate($configfilenum, 0);
        fwrite($configfilenum, $config_contents);
        fclose($configfilenum);
        //嵌入config配置文件
        include_once($file_config);
        //检查表单
        if (!@$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS)) {
            $cue = $_LANG['cue_connect'];
        } elseif (!$_POST['username']) {
            $cue = $_LANG['cue_username_empty'];
        } elseif (!$install->is_username($_POST['username'])) {
            $cue = $_LANG['cue_username_wrong'];
        } elseif (!$_POST['password']) {
            $cue = $_LANG['cue_password_empty'];
        } elseif (!$install->is_password($_POST['password'])) {
            $cue = $_LANG['cue_password_wrong'];
        } elseif (!$_POST['password_confirm']) {
            $cue = $_LANG['cue_password_confirm_empty'];
        } elseif ($_POST['password'] != $_POST['password_confirm']) {
            $cue = $_LANG['cue_password_confirm_wrong'];
        }
        //如果存在错误信息则不执行数据库操作
        if (!$cue) {

            mysqli_query($link,"CREATE DATABASE IF NOT EXISTS `" . DB_DATA . "` default charset utf8 COLLATE utf8_general_ci");
            $link=mysqli_select_db($link,DB_DATA);
            //读取SQL文件到一个字符串中
            $sql = file_get_contents('./data/8cms.sql');
            //进行安装的常规替换
            $sql = str_replace('ljcms_', $_POST[prefix], $sql);
            $sql_en = file_get_contents('./data/8cms_en.sql');
            //进行安装的常规替换
            $sql_en = str_replace('ljcms_', $_POST[prefix], $sql_en);
            //进行安装的常规替换
            $sql_head = "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';\n";
            $sql_head .= "SET time_zone = '+00:00';\n\n\n";
            $sql_head .= "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n";
            $sql_head .= "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n";
            $sql_head .= "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n";
            $sql_head .= "/*!40101 SET NAMES utf8 */;\n\n";
            $sql    = $sql_head . $sql;
            $sql_en = $sql_head . $sql_en;
            //生成管理员
            $username = $_POST['username'];
            $password = md5($_POST['password']);
            $add_time = time();
            //导入数据
            $install->sql_execute($sql);
            /* 写入 hash_code，做为网站唯一性密钥 */
            //	$hash_code = md5(md5(time()) . md5(md5(ROOT_URL . $dbhost . $dbname . $dbuser . $dbpass)));
            //初始化数据
            $table_admin = $prefix . "admin";
            $table_config = $prefix . "config";
            $build_date = time();
            $dbhost = $_POST['dbhost'] . "/";
            //初始化管理员账号
            //$install->sql_execute("UPDATE php8cms_admin SET `password`='{$password}',adminname='{$username}' WHERE adminid=1");
            $install->sql_execute("UPDATE 8cms_admin SET adminname='{$username}',password='{$password}' WHERE adminname='admin'");
//            $install->sql_execute("INSERT INTO 8cms_admin VALUES ('', '{$username}', '{$password}', 0, 1, " . time() . ", 1, 0, 0, '', '')");
            //  $basesql="INSERT INTO `8cms_options` VALUES \r\nvar hm = document.createElement(\"script\");hm.src = \"https://hm.baidu.com/hm.js?f3629ea9febe137b2cbf94c870fa85ce\";var s = document.getElementsByTagName(\"script\")[0];s.parentNode.insertBefore(hm, s);})();\r\n</script>')";
            $basesql = "INSERT INTO `8cms_options` VALUES (3, 'site_base', 'a:14:{s:8:\"sitename\";s:33:\"良精PHP企业网站管理系统\";s:7:\"siteurl\";s:23:\"http://localhost/ljcms/\";s:4:\"logo\";s:46:\"data/attachment/201811/01/0b4affc887ed2b08.png\";s:9:\"logowidth\";i:232;s:10:\"logoheight\";i:60;s:8:\"timezone\";i:8;s:7:\"icpcode\";s:20:\"京ICP备08002262号\";s:9:\"sitephone\";s:12:\"010-81991660\";s:11:\"siteaddress\";s:46:\"北京市丰台区科技园诺德中心4号楼\";s:9:\"siteemail\";s:15:\"82993936@qq.com\";s:6:\"siteqq\";s:10:\"1665976568\";s:6:\"qrcode\";s:67:\"http://demo.8cms.com/data/attachment/201811/27/de903b0223fcd647.jpg\";s:9:\"longitude\";s:10:\"116.304724\";s:8:\"latitude\";s:9:\"39.835519\";}', '站点基本设置', '');";
            //  $basesql_en="INSERT INTO `8cms_options` VALUES (10, 'site_base_en', 'a:14:{s:8:\"sitename\";s:51:\"Liang Jing PHP enterprise website management system\";s:7:\"siteurl\";s:23:\"$dbhost\";s:4:\"logo\";s:46:\"data/attachment/201811/01/0b4affc887ed2b08.png\";s:9:\"logowidth\";i:0;s:10:\"logoheight\";i:0;s:8:\"timezone\";i:8;s:7:\"icpcode\";s:20:\"Beijing ICP 08002262\";s:9:\"sitephone\";s:12:\"010-81991660\";s:11:\"siteaddress\";s:78:\"Building 4, Nord center, Fengtai District science and Technology Park, Beijing\";s:9:\"siteemail\";s:15:\"82993936@qq.com\";s:6:\"siteqq\";s:10:\"1665976568\";s:6:\"qrcode\";s:67:\"http://demo.8cms.com/data/attachment/201811/27/de903b0223fcd647.jpg\";s:9:\"longitude\";s:10:\"116.304724\";s:8:\"latitude\";s:9:\"39.835519\";}', '站点基本设置', '');";
            $basesql_en = "INSERT INTO `8cms_options` VALUES (10, 'site_base_en', 'a:14:{s:8:\"sitename\";s:51:\"Liang Jing PHP enterprise website management system\";s:7:\"siteurl\";s:23:\"/\";s:4:\"logo\";s:46:\"data/attachment/201811/01/0b4affc887ed2b08.png\";s:9:\"logowidth\";i:0;s:10:\"logoheight\";i:0;s:8:\"timezone\";i:8;s:7:\"icpcode\";s:20:\"Beijing ICP 08002262\";s:9:\"sitephone\";s:12:\"010-81991660\";s:11:\"siteaddress\";s:78:\"Building 4, Nord center, Fengtai District science and Technology Park, Beijing\";s:9:\"siteemail\";s:15:\"82993936@qq.com\";s:6:\"siteqq\";s:10:\"1665976568\";s:6:\"qrcode\";s:67:\"http://demo.8cms.com/data/attachment/201811/27/de903b0223fcd647.jpg\";s:9:\"longitude\";s:10:\"116.304724\";s:8:\"latitude\";s:9:\"39.835519\";}', '站点基本设置', '');";
            $install->sql_execute($basesql);
            $install->sql_execute($basesql_en);
            if ($is_english == 'on') {
                $install->sql_execute($sql_en);
            }
            //初始化系统信息
            //$install->sql_execute("UPDATE $table_config SET value = '0' WHERE name = 'rewrite'");
            //$install->sql_execute("UPDATE $table_config SET value = '$build_date' WHERE name = 'build_date'");
            //$install->sql_execute("UPDATE $table_config SET value = '$hash_code' WHERE name = 'hash_code'");
            $_SESSION['cue'] = '';
            $_SESSION['username'] = $_POST['username'];
            header("Location: index.php?step=finish");
        } else {
            $_SESSION['cue'] = $cue;
        }
    }
    include $install->tpl('setting');
} elseif ($step == 'finish') {
    //生成install.lock文件
    $fopen_lock = fopen(INSTALL_ROOT . './install/data/install.lock', "w+");
    fwrite($fopen_lock, "LJPHPCMS INSTALLED");
    /* 生成数据库链接文件 */
    $title = $_LANG['LJPHPCMS'] . " &rsaquo; " . $_LANG['finish'];
    include $install->tpl('finish');
}
ob_end_flush();
?>