Skip to content

Commit

Permalink
Add single tenant mode (EXPERIMENTAL NOW) fc2blog#211
Browse files Browse the repository at this point in the history
- NO TEST CODE AVAILABLE NOW for single tenant mode.
- big refactoring in router.
- add FC2_DEFAULT_BLOG_ID env.
  • Loading branch information
uzulla committed Feb 11, 2021
1 parent 1090601 commit 6fee3c5
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 47 deletions.
8 changes: 8 additions & 0 deletions app/config/init_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,12 @@
),
);

$config['DEFAULT_BLOG_ID'] = defined("DEFAULT_BLOG_ID") ? DEFAULT_BLOG_ID : null;
if (
defined("THIS_IS_TEST") ||
preg_match("/THIS_IS_TEST/u", $_SERVER['HTTP_USER_AGENT'])
) {// TODO シングルテナントモードのテスト系が出来てからはずす
$config['DEFAULT_BLOG_ID'] = null;
}

return $config;
3 changes: 3 additions & 0 deletions app/config_read_from_env.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@
// Please edit the path when change `app` and `public` relative path condition.
define('WWW_DIR', (string)getenv("FC2_DOCUMENT_ROOT_PATH"));

//define('DEFAULT_BLOG_ID', (string)getenv("DEFAULT_BLOG_ID"));
define("DEFAULT_BLOG_ID", (string)getenv("FC2_DEFAULT_BLOG_ID"));

// 設定クラス読み込み
require(__DIR__ . '/core/bootstrap.php');
6 changes: 3 additions & 3 deletions app/src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public static function userURL(Request $request, array $args = [], bool $reused
if (count($params)) {
$url .= '?' . implode('&', $params);
}
if ($blog_id) {
if ($blog_id && $blog_id !== Config::get('DEFAULT_BLOG_ID')) {
$url = '/' . $blog_id . $url;
}
$url = ($abs ? $full_domain : '') . $url;
Expand All @@ -385,7 +385,7 @@ public static function userURL(Request $request, array $args = [], bool $reused
if (count($params) > 0) {
$url .= '?' . implode('&', $params);
}
if ($blog_id) {
if ($blog_id && $blog_id !== Config::get('DEFAULT_BLOG_ID')) {
$url = '/' . $blog_id . $url;
}
$url = ($abs ? $full_domain : '') . $url;
Expand All @@ -406,7 +406,7 @@ public static function userURL(Request $request, array $args = [], bool $reused
if (count($params)) {
$url .= '?' . implode('&', $params);
}
if ($blog_id) {
if ($blog_id && $blog_id !== Config::get('DEFAULT_BLOG_ID')) {
$url = '/' . $blog_id . $url;
}
$url = ($abs ? $full_domain : '') . $url;
Expand Down
29 changes: 29 additions & 0 deletions app/src/Model/BlogsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,4 +727,33 @@ static public function getRedirectStatusCodeByBlogId(string $blog_id): int

return $blog_array['redirect_status_code'];
}

/**
* @return string|null
*/
static public function getDefaultBlogId(): ?string
{
if (strlen(Config::get("DEFAULT_BLOG_ID", ""))>0) {
return Config::get("DEFAULT_BLOG_ID");
} else {
return null;
}
}

/**
* @param Request $request
* @return string|null
*/
static public function getBlogIdByRequestOrDefault(Request $request): ?string
{
if($request->getBlogId()){
return $request->getBlogId();

} else if (Config::get("DEFAULT_BLOG_ID", "") && !is_null(BlogsModel::getDefaultBlogId())) {
return BlogsModel::getDefaultBlogId();

} else {
return null;
}
}
}
2 changes: 1 addition & 1 deletion app/src/Web/Controller/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class UserController extends AppController
*/
public static function getBlogId(Request $request): ?string
{
return $request->get('blog_id');
return $request->getBlogId();
}

/**
Expand Down
10 changes: 9 additions & 1 deletion app/src/Web/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ public static function url(Request $request, $args = array(), $reused = false, $
if (count($params)) {
$url .= '?' . implode('&', $params);
}
if ($blog_id) {
// シングルテナントモードのために、/testblog2/〜〜 のブログ指定部分を省略するか?を決定
if (
$blog_id && (
// Default Blog IDが未指定か
strlen(Config::get('DEFAULT_BLOG_ID'))===0 ||
// 指定されたblog_idがDefault blog idと異なる場合
$blog_id !== Config::get('DEFAULT_BLOG_ID')
)
) {
$url = '/' . $blog_id . $url;
}

Expand Down
90 changes: 50 additions & 40 deletions app/src/Web/Router/Router.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Fc2blog\Web\Router;

use Fc2blog\Model\BlogsModel;
use Fc2blog\Util\StringCaseConverter;
use Fc2blog\Web\Controller\Admin\AdminController;
use Fc2blog\Web\Controller\User\BlogsController;
Expand Down Expand Up @@ -57,37 +59,61 @@ public function __construct(Request $request)
$request->urlRewrite = false;
$request->baseDirectory = '/';

$default_blog_id = "testblog2";// TODO なんらか設定から引くように
$default_blog_id = null;

// 対象となるblogを決定する
if ($path === "/" && !is_null($default_blog_id)) {
// `/`であり(blog_idがURLに無く)、デフォルトblog_idが存在する場合、デフォルトblog_idのトップを表示する
$blog_id = $default_blog_id;
if (!is_null($request->getBlogId())) {
// URLからDefault Blog IDが取得できる場合
$blog_id = $request->getBlogId();
$request->set('blog_id', $blog_id);
if (isset($paths[1])) {
$sub_path = $paths[1];
}
} else if (!is_null($request->get('blog_id'))) {
// パラメタからBlog idが取得できる場合
$blog_id = $request->get('blog_id');
$request->set('blog_id', $blog_id);
if (isset($paths[0])) {
$sub_path = $paths[0];
}

} else if ($path === "/" && is_null($default_blog_id)) {
// `/`であり(blog_idがURLに無く)、デフォルトblog_idが存在しない場合
$this->className = BlogsController::class;
$this->methodName = 'index';

} else if (!is_null($request->getBlogId())) {
// blog_idがURLから特定できる場合
$blog_id = $request->getBlogId();
if (isset($paths[1])) {
$sub_path = $paths[1];
} else {
// blog_idがリクエストから特定できない場合、デフォルトblog_idを利用する
$blog_id = BlogsModel::getDefaultBlogId();
$request->set('blog_id', $blog_id);
if (isset($paths[0])) {
$sub_path = $paths[0];
}
}
// TODO: blog_idがURLから特定できない場合でsub_pathを規定しないといけない?

if (isset($blog_id) && $request->isArgs('xml')) { // `/?xml`
// RSS feed
$this->className = BlogsController::class;
$this->methodName = 'feed';

} else if (preg_match('!\A/uploads/[0-9a-zA-Z]/[0-9a-zA-Z]/[0-9a-zA-Z]/([0-9a-zA-Z]+)/file/([0-9]+)_([wh]?)([0-9]{1,4})\.(png|gif|jpeg|jpg)\z!u', $path, $matches)) {
// サムネイル画像 (入力値制限あり
// http://localhost:8080/uploads/t/e/s/testblog2/file/2_72.png?
// http://localhost:8080/uploads/t/e/s/testblog2/file/2_w300.png?
// http://localhost:8080/uploads/t/e/s/testblog2/file/2_w400.png?
// http://localhost:8080/uploads/t/e/s/testblog2/file/2_w600.png?
$this->className = CommonController::class;
$this->methodName = 'thumbnail';
$request->set('blog_id', $matches[1]);
$request->set('id', $matches[2]);
$request->set('whs', $matches[3]);
$request->set('size', $matches[4]);
$request->set('ext', $matches[5]);

} else if (preg_match('!\A/uploads/[0-9a-zA-Z]/[0-9a-zA-Z]/[0-9a-zA-Z]/([0-9a-zA-Z]+)/file/([0-9]+)_(wh)([0-9]+)_([0-9]+)\.(png|gif|jpe?g)\z!u', $path, $matches)) {
// サムネイル画像 (入力値制限あり
// http://localhost:8080/uploads/t/e/s/testblog2/file/2_wh760_420.png
$this->className = CommonController::class;
$this->methodName = 'thumbnail';
$request->set('blog_id', $matches[1]);
$request->set('id', $matches[2]);
$request->set('whs', $matches[3]);
$request->set('width', $matches[4]);
$request->set('height', $matches[5]);
$request->set('ext', $matches[6]);

} else if (isset($blog_id) && !$request->isArgs($args_action)) {
$this->className = EntriesController::class;

Expand Down Expand Up @@ -130,28 +156,8 @@ public function __construct(Request $request)
} else {
// トップページ
$this->methodName = 'index';
}

} else if (preg_match('{/uploads/[0-9a-zA-Z]/[0-9a-zA-Z]/[0-9a-zA-Z]/([0-9a-zA-Z]+)/file/([0-9]+)_([wh]?)([0-9]+)\.(png|gif|jpe?g)$}', $path, $matches)) {
// サムネイル画像
$this->className = CommonController::class;
$this->methodName = 'thumbnail';
$request->set('blog_id', $matches[1]);
$request->set('id', $matches[2]);
$request->set('whs', $matches[3]);
$request->set('size', $matches[4]);
$request->set('ext', $matches[5]);

} else if (preg_match('{/uploads/[0-9a-zA-Z]/[0-9a-zA-Z]/[0-9a-zA-Z]/([0-9a-zA-Z]+)/file/([0-9]+)_(wh)([0-9]+)_([0-9]+)\.(png|gif|jpe?g)$}', $path, $matches)) {
// サムネイル画像
$this->className = CommonController::class;
$this->methodName = 'thumbnail';
$request->set('blog_id', $matches[1]);
$request->set('id', $matches[2]);
$request->set('whs', $matches[3]);
$request->set('width', $matches[4]);
$request->set('height', $matches[5]);
$request->set('ext', $matches[6]);
}

} else if ($request->get($args_controller) === "common" && $request->get($args_action) === "captcha") {
// captcha画像生成
Expand All @@ -169,7 +175,11 @@ public function __construct(Request $request)
$this->methodName = 'lang';

} else if ($request->get($args_controller) === "blogs" && $request->get($args_action) === "index") {
// 言語切り替え
$this->className = BlogsController::class;
$this->methodName = 'index';

} else if (!isset($blog_id)) {
// blog_idが存在しない場合
$this->className = BlogsController::class;
$this->methodName = 'index';

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ services:
FC2_HTTPS_PORT: "8480"
FC2_DOCUMENT_ROOT_PATH: "/fc2blog/public/"
DEBUG_FORCE_CAPTCHA_KEY: "1234" # デバッグ用にCAPTCHAの値を固定する
# FC2_DEFAULT_BLOG_ID: "testblog2" # experimental single tenant mode.
depends_on:
- db
working_dir: "/fc2blog"
Expand Down
4 changes: 2 additions & 2 deletions e2e_test/tests/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Helper {
async init(): Promise<void> {
this.requestHttpHeaders = {
'Accept-Language': 'ja,en-US;q=0.9,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36'
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 THIS_IS_TEST'
};
this.browser = await puppeteer.launch({
args: ['--lang=ja'],
Expand All @@ -42,7 +42,7 @@ export class Helper {

async setSpUserAgent(): Promise<void> {
let spRequestHttpHeaders = this.requestHttpHeaders;
spRequestHttpHeaders['User-Agent'] = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Mobile Safari/537.36';
spRequestHttpHeaders['User-Agent'] = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Mobile Safari/537.36 THIS_IS_TEST';
await this.page.setExtraHTTPHeaders(spRequestHttpHeaders);
await this.page.setViewport({
width: 400,
Expand Down

0 comments on commit 6fee3c5

Please sign in to comment.