thinkphp.png
在开发项目时,如果面向的是不太熟悉服务器操作和编程的用户,开发一个安装页面是很有必要的。本文将基于ThinkPHP 6.0介绍项目安装页面的开发过程。

一、入口文件判断

首先从入口文件入手,通过判断是否存在install.lock文件来确定项目是否已安装。如果不存在该文件,则跳转到安装页面。相关代码如下:

// [ 应用入口文件 ]
namespace think;
// 定义目录分隔符
define('DS', DIRECTORY_SEPARATOR);
// 定义根目录
define('ROOT_PATH', __DIR__. DS. '..'. DS);
// 定义应用目录
define('APP_PATH', ROOT_PATH. 'app'. DS);
// 判断是否安装了项目
if(!is_file(APP_PATH. 'install/install.lock'))
{
    header("location:./install.php");
    exit;
}
require __DIR__. '/../vendor/autoload.php';
// 执行HTTP应用并响应
$http = (new App())->http;
$response = $http->run();
$response->send();
$http->end($response);

二、安装页面相关设置

  1. 文件和文件夹准备

    • 需要在public文件夹下新建install.php文件,在app文件夹下创建install文件夹,并将项目的数据库命名为install.sql放到install文件夹下。
  2. 常量和变量定义

    • 定义一些常量和变量,如目录分隔符、根目录、应用目录、安装包目录、项目名称、链接、检测目录、缓存目录、错误信息、数据库配置文件、后台入口文件和锁定文件等。代码示例:

      // 定义目录分隔符
      define('DS', DIRECTORY_SEPARATOR);
      // 定义根目录
      define('ROOT_PATH', __DIR__. DS. '..'. DS);
      // 定义应用目录
      define('APP_PATH', ROOT_PATH. 'app'. DS);
      // 安装包目录
      define('INSTALL_PATH', APP_PATH. 'install'. DS);
      //项目名称
      $sitename = "tp6";
      //链接
      $link = array(
       'qqun' => "",
       'home' => '',
       'doc' => '',
      );
      // 检测目录是否存在,这个可以按自己的需求进行修改
      $checkDirs = [
       'vendor'
      ];
      //缓存目录
      $runtimeDir = APP_PATH. 'runtime';
      //错误信息
      $errInfo = '';
      //数据库配置文件
      $dbConfigFile = ROOT_PATH. 'config'. DS. 'database.php';
      //后台入口文件
      $adminFile = ROOT_PATH. 'public'. DS. 'admin.php';
      // 锁定的文件
      $lockFile = INSTALL_PATH. 'install.lock';
  3. 文件或目录权限判断函数

    • 定义函数is_really_writable来判断特定文件或文件夹的读写权限,对于Linux系统可能需要修改权限为777。代码如下:

      function is_really_writable($file)
      {
       if(DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == false) {
         return is_writable($file);
       }
       if(!is_file($file) OR ($fp = @fopen($file, "r+")) === false) {
         return false;
       }
       fclose($fp);
       return true;
      }

三、安装过程中的判断和操作

  1. 环境和条件判断

    • 在安装过程中,需要进行一系列判断,包括是否已安装项目、PHP版本是否大于或等于7.1.0、是否开启PDO、数据库配置文件是否可读写等。
  2. POST请求处理

    • 当接收到POST请求时,进行以下操作:

      • 首先检查错误信息,如果有则输出并退出。
      • 获取并验证用户输入的数据库相关信息(地址、端口、用户名、密码、数据库名、前缀)以及后台管理者相关信息(用户名、密码、邮箱)。
      • 尝试读取install.sql文件,如果失败则抛出异常。
      • 连接数据库,检测是否支持innodb存储引擎,如果不支持则抛出异常。
      • 创建数据库(如果不存在),使用数据库,并执行install.sql文件中的SQL语句。
      • 读取并修改数据库配置文件,将相关信息写入配置文件,如果失败则抛出异常。
      • 尝试写入install.lock文件,如果失败则抛出异常。
      • 更新后台管理者的用户名、密码和盐值等信息。
      • 如果后台入口文件存在,则修改其文件名。
      • 最后根据操作结果输出相应信息。

以下是完整的install.php文件代码:

<?php
/**
 * 安装完成后建议删除此文件
 */
// error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// ini_set('display_errors', '1');
// 定义目录分隔符
define('DS', DIRECTORY_SEPARATOR);
 
// 定义根目录
define('ROOT_PATH', __DIR__ . DS . '..' . DS);
 
// 定义应用目录
define('APP_PATH', ROOT_PATH . 'app' . DS);
 
// 安装包目录
define('INSTALL_PATH', APP_PATH . 'install' . DS);
 
// 判断文件或目录是否有写的权限
function is_really_writable($file)
{
    if (DIRECTORY_SEPARATOR == '/' AND @ ini_get("safe_mode") == false) {
        return is_writable($file);
    }
    if (!is_file($file) OR ($fp = @fopen($file, "r+")) === false) {
        return false;
    }
    fclose($fp);
    return true;
}
 
$sitename = "tp6";
 
$link = array(
    'qqun'  => "",
    'home'  => '',
    'doc'   => '',
);
 
// 检测目录是否存在
$checkDirs = [
    'vendor'
    // 'public' . DS . 'assets' . DS . 'libs'
];
//缓存目录
$runtimeDir = APP_PATH . 'runtime';
 
//错误信息
$errInfo = '';
 
//数据库配置文件
$dbConfigFile = ROOT_PATH. 'config'. DS. 'database.php';
 
//后台入口文件
$adminFile = ROOT_PATH . 'public' . DS . 'admin.php';
 
// 锁定的文件
$lockFile = INSTALL_PATH . 'install.lock';
 
if (is_file($lockFile)) {
    $errInfo = "当前已经安装{$sitename},如果需要重新安装,请手动移除app/install/install.lock文件";
} else {
    if (version_compare(PHP_VERSION, '7.1.0', '<')) {
        $errInfo = "当前版本(" . PHP_VERSION . ")过低,请使用PHP7.1.0以上版本";
    } else {
        if (!extension_loaded("PDO")) {
            $errInfo = "当前未开启PDO,无法进行安装";
        } else {
            if (!is_really_writable($dbConfigFile)) {
                $open_basedir = ini_get('open_basedir');
                if ($open_basedir) {
                    $dirArr = explode(PATH_SEPARATOR, $open_basedir);
                    if ($dirArr && in_array(__DIR__, $dirArr)) {
                        $errInfo = '当前服务器因配置了open_basedir,导致无法读取父目录<br>';
                    }
                }
                if (!$errInfo) {
                    $errInfo = '当前权限不足,无法写入配置文件config/database.php<br>';
                }
            } else {
                $dirArr = [];
                foreach ($checkDirs as $k => $v) {
                    if (!is_dir(ROOT_PATH . $v)) {
                        $errInfo = '当前代码仅包含核心代码,请前往官网下载完整包或资源包覆盖后再尝试安装,';
                        break;
                    }
                }
            }
        }
    }
}
 
// 当前是POST请求
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($errInfo) {//错误信息
        echo $errInfo;
        exit;
    }
    $err = '';
    $mysqlHostname = isset($_POST['mysqlHost']) ? $_POST['mysqlHost'] : '127.0.0.1';//数据库地址
    $mysqlHostport = isset($_POST['mysqlHostport']) ? $_POST['mysqlHostport'] : 3306;//端口号
    $hostArr = explode(':', $mysqlHostname);//如果数据库地址中填写了端口号,将他们分开
    if (count($hostArr) > 1) {
        $mysqlHostname = $hostArr[0];
        $mysqlHostport = $hostArr[1];
    }
    $mysqlUsername = isset($_POST['mysqlUsername']) ? $_POST['mysqlUsername'] : 'root';//用户名
    $mysqlPassword = isset($_POST['mysqlPassword']) ? $_POST['mysqlPassword'] : '';//密码
    $mysqlDatabase = isset($_POST['mysqlDatabase']) ? $_POST['mysqlDatabase'] : 'tp6';//数据库名
    $mysqlPrefix = isset($_POST['mysqlPrefix']) ? $_POST['mysqlPrefix'] : 'tp_';//前缀
    $adminUsername = isset($_POST['adminUsername']) ? $_POST['adminUsername'] : 'admin';//后台管理者用户名
    $adminPassword = isset($_POST['adminPassword']) ? $_POST['adminPassword'] : '123456';//后台管理者密码
    $adminPasswordConfirmation = isset($_POST['adminPasswordConfirmation']) ? $_POST['adminPasswordConfirmation'] : '123456';//重复密码
    $adminEmail = isset($_POST['adminEmail']) ? $_POST['adminEmail'] : 'admin@admin.com';//邮箱
 
    if (!preg_match("/^\w{3,12}$/", $adminUsername)) {
        echo "用户名只能由3-12位数字、字母、下划线组合";
        exit;
    }
    if (!preg_match("/^[\S]{6,16}$/", $adminPassword)) {
        echo "密码长度必须在6-16位之间,不能包含空格";
        exit;
    }
    if ($adminPassword !== $adminPasswordConfirmation) {
        echo "两次输入的密码不一致";
        exit;
    }
 
    try {
        //检测能否读取安装文件
        $sql = @file_get_contents(INSTALL_PATH . 'install.sql');
        if (!$sql) {
            throw new Exception("无法读取app/install/install.sql文件,请检查是否有读权限");
        }
        $sql = str_replace("`fa_", "`{$mysqlPrefix}", $sql);
        $pdo = new PDO("mysql:host={$mysqlHostname};port={$mysqlHostport}", $mysqlUsername, $mysqlPassword, array(
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
        ));
 
        //检测是否支持innodb存储引擎
        $pdoStatement = $pdo->query("SHOW VARIABLES LIKE 'innodb_version'");
        $result = $pdoStatement->fetch();
        if (!$result) {
            throw new Exception("当前数据库不支持innodb存储引擎,请开启后再重新尝试安装");
        }
 
        $pdo->query("CREATE DATABASE IF NOT EXISTS `{$mysqlDatabase}` CHARACTER SET utf8 COLLATE utf8_general_ci;");
 
        $pdo->query("USE `{$mysqlDatabase}`");
 
        $pdo->exec($sql);
 
        $config = @file_get_contents($dbConfigFile);
        $callback = function ($matches) use ($mysqlHostname, $mysqlHostport, $mysqlUsername, $mysqlPassword, $mysqlDatabase, $mysqlPrefix) {
            $field = ucfirst($matches[1]);//首字符大写
            $replace = ${"mysql{$field}"};//$mysqlHostname,$mysqlHostport,$mysqlUsername,$mysqlPassword,$mysqlDatabase,$mysqlPrefix
            if ($matches[1] == 'hostport' && $mysqlHostport == 3306) {
                $replace = '';
            }
            return "'{$matches[1]}'{$matches[2]}=>{$matches[3]}env('database.{$matches[1]}', '{$replace}'),";
        };
        $config = preg_replace_callback("/'(hostname|database|username|password|hostport|prefix)'(\s+)=>(\s+)env\((.*)\)\,/", $callback, $config);
 
        //检测能否成功写入数据库配置
        $result = @file_put_contents($dbConfigFile, $config);
        if (!$result) {
            throw new Exception("无法写入数据库信息到config/database.php文件,请检查是否有写权限");
        }
 
        //检测能否成功写入lock文件
        $result = @file_put_contents($lockFile, 1);
        if (!$result) {
            throw new Exception("无法写入安装锁定到app/install/install.lock文件,请检查是否有写权限");
        }
 
        $newSalt = substr(md5(uniqid(true)), 0, 6);
        $newPassword = md5(md5($adminPassword) . $newSalt);
        $pdo->query("UPDATE {$mysqlPrefix}admin SET username = '{$adminUsername}', email = '{$adminEmail}',password = '{$newPassword}', salt = '{$newSalt}' WHERE username = 'admin'");
 
        $adminName = '';
        if (is_file($adminFile)) {//修改后台入口文件名
            $x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $adminName = substr(str_shuffle(str_repeat($x, ceil(10 / strlen($x)))), 1, 10) . '.php';
            rename($adminFile, ROOT_PATH . 'public' . DS . $adminName);
        }
        echo "success|{$adminName}";
    } catch (PDOException $e) {
        $err = $e->getMessage();
    } catch (Exception $e) {
        $err = $e->getMessage();
    }
    echo $err;
    exit;
}
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>安装<?php echo $sitename; ?></title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1">
    <meta name="renderer" content="webkit">
 
    <style>
        body {
            background: #fff;
            margin: 0;
            padding: 0;
            line-height: 1.5;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
        }
 
        body, input, button {
            font-family: 'Source Sans Pro', 'Helvetica Neue', Helvetica, 'Microsoft Yahei', Arial, sans-serif;
            font-size: 14px;
            color: #7E96B3;
        }
 
        .container {
            max-width: 480px;
            margin: 0 auto;
            padding: 20px;
            text-align: center;
        }
 
        a {
            color: #18bc9c;
            text-decoration: none;
        }
 
        a:hover {
            text-decoration: underline;
        }
 
        h1 {
            margin-top: 0;
            margin-bottom: 10px;
        }
 
        h2 {
            font-size: 28px;
            font-weight: normal;
            color: #3C5675;
            margin-bottom: 0;
            margin-top: 0;
        }
 
        form {
            margin-top: 40px;
        }
 
        .form-group {
            margin-bottom: 20px;
        }
 
        .form-group .form-field:first-child input {
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
        }
 
        .form-group .form-field:last-child input {
            border-bottom-left-radius: 4px;
            border-bottom-right-radius: 4px;
        }
 
        .form-field input {
            background: #EDF2F7;
            margin: 0 0 1px;
            border: 2px solid transparent;
            transition: background 0.2s, border-color 0.2s, color 0.2s;
            width: 100%;
            padding: 15px 15px 15px 180px;
            box-sizing: border-box;
        }
 
        .form-field input:focus {
            border-color: #18bc9c;
            background: #fff;
            color: #444;
            outline: none;
        }
 
        .form-field label {
            float: left;
            width: 160px;
            text-align: right;
            margin-right: -160px;
            position: relative;
            margin-top: 18px;
            font-size: 14px;
            pointer-events: none;
            opacity: 0.7;
        }
 
        button, .btn {
            background: #3C5675;
            color: #fff;
            border: 0;
            font-weight: bold;
            border-radius: 4px;
            cursor: pointer;
            padding: 15px 30px;
            -webkit-appearance: none;
        }
 
        button[disabled] {
            opacity: 0.5;
        }
 
        .form-buttons {
            height: 52px;
            line-height: 52px;
        }
 
        .form-buttons .btn {
            margin-right: 5px;
        }
 
        #error, .error, #success, .success, #warmtips, .warmtips {
            background: #D83E3E;
            color: #fff;
            padding: 15px 20px;
            border-radius: 4px;
            margin-bottom: 20px;
        }
 
        #success {
            background: #3C5675;
        }
 
        #error a, .error a {
            color: white;
            text-decoration: underline;
        }
 
        #warmtips {
            background: #ffcdcd;
            font-size: 14px;
            color: #e74c3c;
        }
 
        #warmtips a {
            background: #ffffff7a;
            display: block;
            height: 30px;
            line-height: 30px;
            margin-top: 10px;
            color: #e21a1a;
            border-radius: 3px;
        }
    </style>
</head>
 
<body>
<div class="container">
    <h1>
        <svg width="80px" height="96px" viewBox="0 0 768 830" version="1.1" xmlns="http://www.w3.org/2000/svg"
             xmlns:xlink="http://www.w3.org/1999/xlink">
            <g id="logo" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
                <path d="M64.433651,605.899968 C20.067302,536.265612 0,469.698785 0,389.731348 C0,174.488668 171.922656,0 384,0 C596.077344,0 768,174.488668 768,389.731348 C768,469.698785 747.932698,536.265612 703.566349,605.899968 C614.4,753.480595 441.6,870.4 384,870.4 C326.4,870.4 153.6,753.480595 64.433651,605.899968 L64.433651,605.899968 Z"
                      id="body" fill="#18BC9C"></path>
                <path d="M429.648991,190.816 L430.160991,190.816 L429.648991,190.816 L429.648991,190.816 Z M429.648991,156 L427.088991,156 C419.408991,157.024 411.728991,160.608 404.560991,168.8 L403.024991,170.848 L206.928991,429.92 C198.736991,441.184 197.712991,453.984 204.368991,466.784 C210.512991,478.048 222.288991,485.728 235.600991,485.728 L336.464991,486.24 L304.208991,673.632 C301.648991,689.504 310.352991,705.376 325.200991,712.032 C329.808991,714.08 334.416991,714.592 339.536991,714.592 C349.776991,714.592 358.992991,709.472 366.160991,700.256 L561.744991,419.168 C569.936991,407.904 570.960991,395.104 564.304991,382.304 C557.648991,369.504 547.408991,363.36 533.072991,363.36 L432.208991,363.36 L463.952991,199.008 C464.464991,196.448 464.976991,193.376 464.976991,190.816 C464.976991,171.872 449.104991,156 431.184991,156 L429.648991,156 L429.648991,156 Z"
                      id="flash" fill="#FFFFFF"></path>
            </g>
        </svg>
    </h1>
    <h2>安装 <?php echo $sitename; ?></h2>
    <div>
 
        <p>若你在安装中遇到麻烦可点击 
            <a href="<?php echo $link['doc']; ?>" target="_blank">安装文档</a>
            <a href="<?php echo $link['qqun']; ?>" target="_blank">问答社区</a>
            <a href="<?php echo $link['qqun']; ?>">QQ交流群</a>
        </p>
        <!--<p><?php echo $sitename; ?>还支持在命令行php think install一键安装</p>-->
 
        <form method="post">
            <?php if ($errInfo): ?>
                <div class="error">
                    <?php echo $errInfo; ?>
                </div>
            <?php endif; ?>
            <div id="error" style="display:none"></div>
            <div id="success" style="display:none"></div>
            <div id="warmtips" style="display:none"></div>
 
            <div class="form-group">
                <div class="form-field">
                    <label>MySQL 数据库地址</label>
                    <input type="text" name="mysqlHost" value="127.0.0.1" required="">
                </div>
 
                <div class="form-field">
                    <label>MySQL 数据库名</label>
                    <input type="text" name="mysqlDatabase" value="tp6" required="">
                </div>
 
                <div class="form-field">
                    <label>MySQL 用户名</label>
                    <input type="text" name="mysqlUsername" value="root" required="">
                </div>
 
                <div class="form-field">
                    <label>MySQL 密码</label>
                    <input type="password" name="mysqlPassword">
                </div>
 
                <div class="form-field">
                    <label>MySQL 数据表前缀</label>
                    <input type="text" name="mysqlPrefix" value="fa_">
                </div>
 
                <div class="form-field">
                    <label>MySQL 端口号</label>
                    <input type="number" name="mysqlHostport" value="3306">
                </div>
            </div>
 
            <div class="form-group">
                <div class="form-field">
                    <label>管理者用户名</label>
                    <input name="adminUsername" value="admin" required=""/>
                </div>
 
                <div class="form-field">
                    <label>管理者Email</label>
                    <input name="adminEmail" value="admin@admin.com" required="">
                </div>
 
                <div class="form-field">
                    <label>管理者密码</label>
                    <input type="password" name="adminPassword" required="">
                </div>
 
                <div class="form-field">
                    <label>重复密码</label>
                    <input type="password" name="adminPasswordConfirmation" required="">
                </div>
            </div>
 
            <div class="form-buttons">
                <button type="submit" <?php echo $errInfo ? 'disabled' : '' ?>>点击安装</button>
            </div>
        </form>
 
        <!-- jQuery -->
        <script src="https://cdn.staticfile.org/jquery/2.1.4/jquery.min.js"></script>
 
        <script>
            $(function () {
                $('form :input:first').select();
 
                $('form').on('submit', function (e) {
                    e.preventDefault();
                    var form = this;
                    var $button = $(this).find('button')
                        .text('安装中...')
                        .prop('disabled', true);
 
                    $.post('', $(this).serialize())
                        .done(function (ret) {
                            if (ret.substr(0, 7) === 'success') {
                                var retArr = ret.split(/\|/);
                                $('#error').hide();
                                $(".form-group", form).remove();
                                $button.remove();
                                $("#success").text("安装成功!开始你的<?php echo $sitename; ?>之旅吧!").show();
 
                                $buttons = $(".form-buttons", form);
                                $('<a class="btn" href="./">访问首页</a>').appendTo($buttons);
 
                                if (typeof retArr[1] !== 'undefined' && retArr[1] !== '') {
                                    var url = location.href.replace(/install\.php/, retArr[1]);
                                    $("#warmtips").html('温馨提示:请将以下后台登录入口添加到你的收藏夹,为了你的安全,不要泄漏或发送给他人!如有泄漏请及时修改!<a href="' + url + '">' + url + '</a>').show();
                                    $('<a class="btn" href="' + url + '" id="btn-admin" style="background:#18bc9c">访问后台</a>').appendTo($buttons);
                                }
                                localStorage.setItem("fastep", "installed");
                            } else {
                                $('#error').show().text(ret);
                                $button.prop('disabled', false).text('点击安装');
                                $("html,body").animate({
                                    scrollTop: 0
                                }, 500);
                            }
                        })
                        .fail(function (data) {
                            $('#error').show().text('发生错误:\n\n' + data.responseText);
                            $button.prop('disabled', false).text('点击安装');
                            $("html,body").animate({
                                scrollTop: 0
                            }, 500);
                        });
 
                    return false;
                });
            });
        </script>
    </div>
</div>
</body>
</html>

通过以上步骤,可以为ThinkPHP 6.0项目开发一个基本的安装页面,方便用户进行项目的安装和配置。

分类: 技术 标签: 暂无标签

评论

暂无评论数据

暂无评论数据

目录