Skip to content

Commit

Permalink
Implement software update feature. fc2blog#39
Browse files Browse the repository at this point in the history
  • Loading branch information
uzulla committed Mar 16, 2021
1 parent f194dac commit 26d67e1
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 16 deletions.
Binary file modified app/locale/en_US.UTF-8/LC_MESSAGES/messages.mo
Binary file not shown.
9 changes: 9 additions & 0 deletions app/locale/en_US.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -2520,4 +2520,13 @@ msgid "Release information query failed. Please try again later."
msgstr ""

msgid "Please backup your site before update."
msgstr "更新前にはかならずサイトのバックアップを作成してください。"

msgid "Request failed: invalid sig, please retry."
msgstr ""

msgid "Request failed: notfound request version."
msgstr ""

msgid "Update success."
msgstr ""
Binary file modified app/locale/ja_JP.UTF-8/LC_MESSAGES/messages.mo
Binary file not shown.
9 changes: 9 additions & 0 deletions app/locale/ja_JP.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -2568,3 +2568,12 @@ msgstr "情報取得に失敗しました、時間をおいてから再度お試

msgid "Please backup your site before update."
msgstr "更新前にはかならずサイトのバックアップを作成してください。"

msgid "Request failed: invalid sig, please retry."
msgstr "失敗しました、再度試してください。"

msgid "Request failed: notfound request version."
msgstr "失敗しました、指定のバージョンがみつかりませんでした。"

msgid "Update success."
msgstr "更新成功しました。"
42 changes: 33 additions & 9 deletions app/src/Model/SystemUpdateModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function getReleaseInfo(): ?array
try {
return json_decode($releases_json, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
error_log("invalid json. but on going.");
error_log("Invalid cached release json. but on going.");
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ public static function updateSystemByLocalZip(string $dist_zip_path)
$tmp_path = tempnam(sys_get_temp_dir(), "fc2blog_dist_");
unlink($tmp_path); // use to directory. file is not important.

if (!mkdir($tmp_path)) {
if (!mkdir($tmp_path, 0777, true)) {
throw new RuntimeException("mkdir({$tmp_path}) failed");
}

Expand All @@ -229,24 +229,47 @@ public static function updateSystemByLocalZip(string $dist_zip_path)

// decide app dir.
$app_dir = APP_DIR;
$public_dir = WWW_DIR;

// deploy files
// TODO remove src dir
$files_in_tmp_dir_app = glob($tmp_dir_app . '/{*,.[!.]*,..?*}', GLOB_BRACE);
foreach ($files_in_tmp_dir_app as $files_in_tmp_dir_app_row) {
// `temp` and `config.php` will skip delete/copy.
if ($tmp_dir_app . "/temp" === $files_in_tmp_dir_app_row) continue;
if ($tmp_dir_app . "/config.php" === $files_in_tmp_dir_app_row) continue;
// delete/reset dir/files.
static::rm_r($app_dir . substr($files_in_tmp_dir_app_row, strlen($tmp_dir_app)+1));
static::copy_r($files_in_tmp_dir_app_row, $app_dir); // todo error handling
}

$files_in_tmp_dir_public = glob($tmp_dir_public . '/{*,.[!.]*,..?*}', GLOB_BRACE);
foreach ($files_in_tmp_dir_public as $files_in_tmp_dir_public_row) {
static::copy_r($files_in_tmp_dir_public_row, __DIR__); // todo error handling
// `user_uploads` will skip delete/copy.
if ($tmp_dir_app . "/user_uploads" === $files_in_tmp_dir_public_row) continue;
// delete/reset dir/files.
static::rm_r($public_dir . substr($files_in_tmp_dir_public_row, strlen($tmp_dir_public)+1));
static::copy_r($files_in_tmp_dir_public_row, WWW_DIR); // todo error handling
}

// index.phpのみ、環境によって動的なので更新する
$index_php = file_get_contents(WWW_DIR."/index.php");
$index_php = preg_replace('/\n\$app_dir_path.+;/u', "\n\$app_dir_path = '" . APP_DIR . "';", $index_php);
file_put_contents(WWW_DIR."/index.php", $index_php);

} finally {
static::rmdir_r($tmp_path);
}

}

private static function rm_r(string $path): bool
{
if (!file_exists($path)) return false;
if (!is_dir($path)) return unlink($path);
if (is_dir($path)) return static::rmdir_r($path);
return false;
}

private static function rmdir_r(string $dirPath): bool
{
if (!empty($dirPath) && is_dir($dirPath)) {
Expand Down Expand Up @@ -297,22 +320,23 @@ private static function copy_r(string $src_path, string $dest_dir)
$files = new RecursiveIteratorIterator($dirObj, RecursiveIteratorIterator::CHILD_FIRST);

foreach ($files as $path) {
$relative_path = substr($path->getPath(), strlen($src_base_dir) + 1);
$relative_path = substr($path->getRealPath(), strlen($src_base_dir) + 1);
$src_full_path = $src_base_dir . "/" . $relative_path;
$dest_full_path = $dest_dir . "/" . $relative_path;

if (is_dir($dest_full_path) && file_exists($dest_full_path)) {
if (file_exists($dest_full_path) && is_dir($dest_full_path)) {
// ディレクトリで、すでにディレクトリが存在しているならスキップ
touch($dest_full_path); // update mtime
continue;

} else if (is_dir($src_full_path) && !file_exists($dest_full_path)) {
} else if (!file_exists($dest_full_path) && is_dir($src_full_path)) {
// ディレクトリを作成
mkdir($dest_full_path);
mkdir($dest_full_path, 0777, true);

} else if (is_dir($src_full_path) && file_exists($dest_full_path) && !is_dir($dest_full_path)) {
// ファイルがディレクトリになっているので、削除してディレクトリへ
unlink($dest_full_path);
mkdir($dest_full_path);
mkdir($dest_full_path, 0777, true);

} else if (is_file($src_full_path) && file_exists($dest_full_path) && is_dir($dest_full_path)) {
// ディレクトリがファイルになっているので、削除してファイルへ
Expand Down
14 changes: 9 additions & 5 deletions app/src/Web/Controller/Admin/SystemUpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class SystemUpdateController extends AdminController
{
public function index(Request $request): string
{
// TODO unit test

$request->generateNewSig();
$release_list = SystemUpdateModel::getReleaseInfo();
$this->set('release_list', $release_list);
Expand All @@ -22,29 +24,31 @@ public function index(Request $request): string

public function update(Request $request): string
{
// TODO unit test

// check sig
if(!$request->isValidSig()){
$this->setWarnMessage("request failed. invalid sig. please retry."); // TODO i18n
$this->setWarnMessage(__("Request failed: invalid sig, please retry."));
$this->redirect($request, Html::url($request, ['controller'=>'system_update', 'action'=>'index']));
}

// check request
$request_version = $request->get('version');
if(is_null($request_version)){
$this->setWarnMessage("request failed. missing version."); // TODO i18n
$this->setWarnMessage(__("Request failed: notfound request version."));
$this->redirect($request, Html::url($request, ['controller'=>'system_update', 'action'=>'index']));
}

$release_list = SystemUpdateModel::getReleaseInfo();
// get request version
$release = SystemUpdateModel::findByVersionFromReleaseList($release_list, $request_version);

$zip_url = SystemUpdateModel::getZipDownloadUrl($release);

// do update system.
SystemUpdateModel::updateSystemByUrl($zip_url);

// add flash
$this->setInfoMessage("updated");
// set flash message
$this->setInfoMessage(__("Update success."));

// redirect to index
$this->redirect($request, Html::url($request, ['controller'=>'system_update', 'action'=>'index']));
Expand Down
3 changes: 1 addition & 2 deletions dist_zip/installer/fc2blog_installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,10 @@ function copy_r(string $src_path, string $dest_dir)
}

$dirObj = new RecursiveDirectoryIterator($src_path, RecursiveDirectoryIterator::SKIP_DOTS);
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator($dirObj, RecursiveIteratorIterator::CHILD_FIRST);

foreach ($files as $path) {
$relative_path = substr($path->getPath(), strlen($src_base_dir) + 1);
$relative_path = substr($path, strlen($src_base_dir) + 1);
$src_full_path = $src_base_dir . "/" . $relative_path;
$dest_full_path = $dest_dir . "/" . $relative_path;

Expand Down

0 comments on commit 26d67e1

Please sign in to comment.