真是累,每次安裝測試帝國模板都需要手動刪除install.lock文件,然後再下一步下一步!
下一步完成後,還需要你自己填寫數據庫和初始密碼!
真是煩死了!
於是乎,我寫了一個PHP,大致功能如下:
不用手動訪問/e/install目錄和刪除lock文件
運行PHP,自動安裝帝國cms,並且跳轉帝國後臺登錄!
初始賬號:admin
初始密碼:123456
目前只適合帝國cms7.5,以後可能會增加帝國cms7.2
上代碼:
<?php
$sql_name = "test_4s5_cn";
$sql_pass = "ggggggggggggg";
// 下載帝國安裝包和配置文件
downloadAndExtract(__DIR__."/e/install/","http://api.4s5.cn/cdn/ecms/zip/install.zip");
downloadAndExtract(__DIR__."/e/config/","http://api.4s5.cn/cdn/ecms/zip/config.zip");
importSqlZip("http://api.4s5.cn/cdn/ecms/zip/mysql.sql.zip",$sql_name,$sql_name,$sql_pass);
// 配置數據庫config文件
$config_file = __DIR__."/e/config/config.php";
modifyDatabaseConfig($config_file,['dbusername' => $sql_name, 'dbpassword' => $sql_pass, 'dbname' => $sql_name, 'dbtbpre' => "phome_"]);
// 安裝install的數據庫
// 獲取當前域名
$domain = $_SERVER['HTTP_HOST'];
// 拼接 URL
$url = "http://{$domain}/e/admin";
// 跳轉到 URL
header("Location: $url");
// 自定義函數
/**
* 從遠程URL下載包含SQL文件的ZIP壓縮文件,解壓縮SQL文件到指定目錄,然後將數據導入到MySQL數據庫中。
*
* @param string $remoteUrl 遠程ZIP文件的URL地址
* @param string $dbName 目標MySQL數據庫的名稱
* @param string $dbUser 目標MySQL數據庫的用戶名
* @param string $dbPass 目標MySQL數據庫的密碼
* @param string $dbHost 目標MySQL數據庫的主機地址,默認爲localhost
* @param string $sqlDir 解壓縮SQL文件的目錄路徑,默認爲當前文件所在目錄下的path/目錄
* @throws Exception 如果下載、解壓縮、數據庫操作失敗,則會拋出異常
*/
function importSqlZip($remoteUrl, $dbName, $dbUser, $dbPass, $dbHost = 'localhost', $sqlDir = __DIR__.'/path/') {
// 創建一個臨時文件用於保存下載的 Zip 文件
$tempZip = tempnam(sys_get_temp_dir(), 'sql_zip_');
// 下載遠程 Zip 文件並保存到本地
$fp = fopen($tempZip, 'w');
$ch = curl_init($remoteUrl);
curl_setopt($ch, CURLOPT_FILE, $fp);
$success = curl_exec($ch);
curl_close($ch);
fclose($fp);
if (!$success) {
throw new Exception('Failed to download the remote SQL ZIP file.');
}
// 創建指定目錄
if (!mkdir($sqlDir, 0777, true)) {
throw new Exception('Failed to create directory.');
}
// 解壓縮文件到指定目錄
$zip = new ZipArchive();
if ($zip->open($tempZip) === TRUE) {
$zip->extractTo($sqlDir);
$zip->close();
} else {
throw new Exception('Failed to extract the SQL ZIP file.');
}
// 清空數據庫
$dsn = "mysql:host=$dbHost;dbname=$dbName";
$pdo = new PDO($dsn, $dbUser, $dbPass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
foreach ($tables as $table) {
$pdo->query("DROP TABLE IF EXISTS $table");
}
// 導入 SQL 文件
$files = glob($sqlDir . '*.sql');
foreach ($files as $file) {
$pdo->exec(file_get_contents($file));
}
// 刪除臨時文件和 SQL 文件
unlink($tempZip);
$files = glob($sqlDir . '*.sql');
foreach ($files as $file) {
unlink($file);
}
rmdir($sqlDir);
}
function downloadAndExtract($installDir, $remoteZipUrl) {
// 刪除指定目錄及其下所有文件
if (is_dir($installDir)) {
$files = glob($installDir . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
} elseif (is_dir($file)) {
$innerFiles = glob($file . '/*');
foreach ($innerFiles as $innerFile) {
if (is_file($innerFile)) {
unlink($innerFile);
}
}
rmdir($file);
}
}
rmdir($installDir);
}
// 創建指定目錄
if (!mkdir($installDir, 0777, true)) {
throw new Exception('Failed to create install directory.');
}
// 下載遠程 Zip 文件並保存到本地
$tempZip = tempnam(sys_get_temp_dir(), 'download_');
$fp = fopen($tempZip, 'w');
$ch = curl_init($remoteZipUrl);
curl_setopt($ch, CURLOPT_FILE, $fp);
$success = curl_exec($ch);
curl_close($ch);
fclose($fp);
if (!$success) {
throw new Exception('Failed to download the remote ZIP file.');
}
// 解壓縮文件到指定目錄
$zip = new ZipArchive();
if ($zip->open($tempZip) === TRUE) {
$zip->extractTo($installDir);
$zip->close();
} else {
throw new Exception('Failed to extract the ZIP file.');
}
// 刪除臨時文件
unlink($tempZip);
}
// 讀取數據庫文件,然後傳入需要替換的數組
function modifyDatabaseConfig($filename, $newConfig) {
// 讀取配置文件內容
$file = file_get_contents($filename);
// 替換配置內容
// 配置文件裏的變量名前綴
$prefix = '$ecms_config[\'db\']';
// 遍歷新配置,將對應的變量替換爲新值
foreach ($newConfig as $key => $value) {
$pattern = '/' . preg_quote($prefix) . '\[\'' . preg_quote($key) . '\'\]\s*=\s*\'[^;]+;/';
$replace = $prefix . '[\'' . $key . '\'] = \'' . $value . '\';';
$file = preg_replace($pattern, $replace, $file);
}
// 保存文件
file_put_contents($filename, $file);
}
第二次更新
本次更新了帝國cms7.2版本的安裝,運行PHP自動判斷帝國版本,然後自動安裝
<?php
$sql_name = "test_4s5_cn";
$sql_pass = "wnxxtJTa3y4SPPrr";
if(file_exists(__DIR__."/e/admin/index.php")){
$string = file_get_contents(__DIR__."/e/admin/index.php");
// 開始檢查帝國cms版本
if (strpos($myString, "7.2") !== false) {
$ecms = 7.5;
} else {
$ecms = 7.2;
}
}else{
die("未檢測到帝國admin後臺");
}
// 下載帝國安裝包和配置文件
downloadAndExtract(__DIR__."/e/install/","http://api.4s5.cn/cdn/ecms/zip/{$ecms}/install.zip");
downloadAndExtract(__DIR__."/e/config/","http://api.4s5.cn/cdn/ecms/zip/{$ecms}/config.zip");
importSqlZip("http://api.4s5.cn/cdn/ecms/zip/{$ecms}/mysql.sql.zip",$sql_name,$sql_name,$sql_pass);
// 配置數據庫config文件
$config_file = __DIR__."/e/config/config.php";
modifyDatabaseConfig($config_file,['dbusername' => $sql_name, 'dbpassword' => $sql_pass, 'dbname' => $sql_name, 'dbtbpre' => "phome_"]);
// 獲取當前域名
$domain = $_SERVER['HTTP_HOST'];
// 拼接 URL
$url = "http://{$domain}/e/admin";
echo "安裝成功!帝國版本:{$ecms}";
// 停頓三秒鐘後跳轉後臺
echo " <meta http-equiv=\"refresh\" content=\"3;url={$url}\" />";
// 自定義函數
/**
* 從遠程URL下載包含SQL文件的ZIP壓縮文件,解壓縮SQL文件到指定目錄,然後將數據導入到MySQL數據庫中。
*
* @param string $remoteUrl 遠程ZIP文件的URL地址
* @param string $dbName 目標MySQL數據庫的名稱
* @param string $dbUser 目標MySQL數據庫的用戶名
* @param string $dbPass 目標MySQL數據庫的密碼
* @param string $dbHost 目標MySQL數據庫的主機地址,默認爲localhost
* @param string $sqlDir 解壓縮SQL文件的目錄路徑,默認爲當前文件所在目錄下的path/目錄
* @throws Exception 如果下載、解壓縮、數據庫操作失敗,則會拋出異常
*/
function importSqlZip($remoteUrl, $dbName, $dbUser, $dbPass, $dbHost = 'localhost', $sqlDir = __DIR__.'/path/') {
// 創建一個臨時文件用於保存下載的 Zip 文件
$tempZip = tempnam(sys_get_temp_dir(), 'sql_zip_');
// 下載遠程 Zip 文件並保存到本地
$fp = fopen($tempZip, 'w');
$ch = curl_init($remoteUrl);
curl_setopt($ch, CURLOPT_FILE, $fp);
$success = curl_exec($ch);
curl_close($ch);
fclose($fp);
if (!$success) {
throw new Exception('Failed to download the remote SQL ZIP file.');
}
// 創建指定目錄
if (!mkdir($sqlDir, 0777, true)) {
throw new Exception('Failed to create directory.');
}
// 解壓縮文件到指定目錄
$zip = new ZipArchive();
if ($zip->open($tempZip) === TRUE) {
$zip->extractTo($sqlDir);
$zip->close();
} else {
throw new Exception('Failed to extract the SQL ZIP file.');
}
// 清空數據庫
$dsn = "mysql:host=$dbHost;dbname=$dbName";
$pdo = new PDO($dsn, $dbUser, $dbPass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
foreach ($tables as $table) {
$pdo->query("DROP TABLE IF EXISTS $table");
}
// 導入 SQL 文件
$files = glob($sqlDir . '*.sql');
foreach ($files as $file) {
$pdo->exec(file_get_contents($file));
}
// 刪除臨時文件和 SQL 文件
unlink($tempZip);
$files = glob($sqlDir . '*.sql');
foreach ($files as $file) {
unlink($file);
}
rmdir($sqlDir);
}
function downloadAndExtract($installDir, $remoteZipUrl) {
// 刪除指定目錄及其下所有文件
if (is_dir($installDir)) {
$files = glob($installDir . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
} elseif (is_dir($file)) {
$innerFiles = glob($file . '/*');
foreach ($innerFiles as $innerFile) {
if (is_file($innerFile)) {
unlink($innerFile);
}
}
rmdir($file);
}
}
rmdir($installDir);
}
// 創建指定目錄
if (!mkdir($installDir, 0777, true)) {
throw new Exception('Failed to create install directory.');
}
// 下載遠程 Zip 文件並保存到本地
$tempZip = tempnam(sys_get_temp_dir(), 'download_');
$fp = fopen($tempZip, 'w');
$ch = curl_init($remoteZipUrl);
curl_setopt($ch, CURLOPT_FILE, $fp);
$success = curl_exec($ch);
curl_close($ch);
fclose($fp);
if (!$success) {
throw new Exception('Failed to download the remote ZIP file.');
}
// 解壓縮文件到指定目錄
$zip = new ZipArchive();
if ($zip->open($tempZip) === TRUE) {
$zip->extractTo($installDir);
$zip->close();
} else {
throw new Exception('Failed to extract the ZIP file.');
}
// 刪除臨時文件
unlink($tempZip);
}
// 讀取數據庫文件,然後傳入需要替換的數組
function modifyDatabaseConfig($filename, $newConfig) {
// 讀取配置文件內容
$file = file_get_contents($filename);
// 替換配置內容
// 配置文件裏的變量名前綴
$prefix = '$ecms_config[\'db\']';
// 遍歷新配置,將對應的變量替換爲新值
foreach ($newConfig as $key => $value) {
$pattern = '/' . preg_quote($prefix) . '\[\'' . preg_quote($key) . '\'\]\s*=\s*\'[^;]+;/';
$replace = $prefix . '[\'' . $key . '\'] = \'' . $value . '\';';
$file = preg_replace($pattern, $replace, $file);
}
// 保存文件
file_put_contents($filename, $file);
}