Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller周辺のリファクタリング、型の厳格化、コードのデカップリング等 #108 #256 #321

Merged
merged 34 commits into from
Jun 5, 2021
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
64d32df
remove Controller::getDeviceType(), move to Request.
uzulla Jun 4, 2021
31d6d15
remove Controller::setToken().
uzulla Jun 4, 2021
093a2b1
Improve pseudo array model. add dynamic pattern(disabled normally).
uzulla Jun 4, 2021
28ceb2c
remove Controller::getBlog().
uzulla Jun 4, 2021
5831661
move Controller::tokenValidate() to EntriesController::captchaTokenVa…
uzulla Jun 4, 2021
0416157
Refactoring around captcha.
uzulla Jun 4, 2021
dcaf2b3
Decouple preprocessingDataForFc2Template from Controller.
uzulla Jun 4, 2021
d0e3902
remove unnecessary define variable.
uzulla Jun 4, 2021
7fb03cc
move renderByFc2Template() to UserController from (base)Controller.
uzulla Jun 4, 2021
122452f
refactoring, move getHostUrl() to Request.
uzulla Jun 4, 2021
56a3930
Avoid implicit 404 Notfound call.
uzulla Jun 4, 2021
dbc4b08
fix typo.
uzulla Jun 4, 2021
d880ced
Chore, reorder some methods in the class.
uzulla Jun 4, 2021
c9fac01
Decouple Fc2Template compile,syntax check,other methods from Model.
uzulla Jun 4, 2021
7345d07
remove unnecessary method.
uzulla Jun 4, 2021
04cf415
avoid unnecessary public access modifier.
uzulla Jun 4, 2021
f01b39a
remove unnecessary TODO.
uzulla Jun 4, 2021
12e323a
Avoid implicit 404 Notfound call.
uzulla Jun 4, 2021
74ef1cd
Explicit getter naming that get current blog id.
uzulla Jun 5, 2021
710aade
add type definition.
uzulla Jun 5, 2021
4282618
fix omission of corrections
uzulla Jun 5, 2021
6e916ea
chore
uzulla Jun 5, 2021
b4de194
use isValidSig() instead of raw Session::get... code.
uzulla Jun 5, 2021
b310ced
add type definition.
uzulla Jun 5, 2021
8228417
fix correct type definition.
uzulla Jun 5, 2021
8b874e6
add declare(strict_types=1); to all controllers. avoid auto type cast.
uzulla Jun 5, 2021
d624d2f
fix broken test.
uzulla Jun 5, 2021
0749041
comment out unused arg.
uzulla Jun 5, 2021
a479e95
Add return type.
uzulla Jun 5, 2021
815e05d
Improve 500 error page. #256
uzulla Jun 5, 2021
8efce71
change UserController::getBlogId to $request->getBlogId() and remove …
uzulla Jun 5, 2021
185f5dc
errorXXX method change access modifier to protected from public.
uzulla Jun 5, 2021
6cf0e8e
Refactoring Router. remove unnecessary validation. tidy up.
uzulla Jun 5, 2021
0899189
Refactoring Router.
uzulla Jun 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Decouple Fc2Template compile,syntax check,other methods from Model.
uzulla committed Jun 4, 2021
commit c9fac016c8a40d6b3c4906a1c5452050f6a574ce
5 changes: 3 additions & 2 deletions app/src/Model/BlogPluginsModel.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
use Fc2blog\App;
use Fc2blog\Config;
use Fc2blog\Util\PhpCodeLinter;
use Fc2blog\Web\Fc2BlogTemplate;
use Fc2blog\Web\Session;

class BlogPluginsModel extends Model
@@ -161,7 +162,7 @@ public static function fc2PluginSyntax(string $php_code)
}

// HTMLをPHPテンプレートに変換してテンプレートファイルの作成
$html = BlogTemplatesModel::convertFC2Template($php_code);
$html = Fc2BlogTemplate::convertToPhp($php_code);
file_put_contents($plugin_path, $html);
chmod($plugin_path, 0777);

@@ -426,7 +427,7 @@ public static function createPlugin(string $html, string $blog_id, string $id =
}

// HTMLをPHPテンプレートに変換してテンプレートファイルの作成
$html = BlogTemplatesModel::convertFC2Template($html);
$html = Fc2BlogTemplate::convertToPhp($html);
file_put_contents($plugin_path, $html);
chmod($plugin_path, 0777);
}
109 changes: 5 additions & 104 deletions app/src/Model/BlogTemplatesModel.php
Original file line number Diff line number Diff line change
@@ -4,8 +4,7 @@

use Fc2blog\App;
use Fc2blog\Config;
use Fc2blog\Util\PhpCodeLinter;
use Fc2blog\Web\Session;
use Fc2blog\Web\Fc2BlogTemplate;

class BlogTemplatesModel extends Model
{
@@ -68,37 +67,13 @@ public function validate(array $data, ?array &$valid_data = [], array $white_lis
}

/**
* FC2テンプレートの構文チェック
* バリデーションでつかうために、Proxy呼び出し
* @param string $php_code
* @return bool|string
*/
public static function fc2TemplateSyntax(string $php_code)
{
if (defined("THIS_IS_TEST")) {
// テンプレート検証用にテンポラリディレクトリが必要だが、テストやCLIでSessionを汚染したくないので
$blog_id = "unitTestOrCliExecute";
} else {
$blog_id = Session::get('blog_id');
}

// フォルダが存在しない場合作成
$templatePath = Config::get('BLOG_TEMPLATE_DIR') . App::getBlogLayer($blog_id) . '/syntax.php';
$templateDir = dirname($templatePath);
if (!file_exists($templateDir)) {
mkdir($templateDir, 0777, true);
}

// HTMLをPHPテンプレートに変換してテンプレートファイルの作成
$html = self::convertFC2Template($php_code);
file_put_contents($templatePath, $html);

chmod($templatePath, 0777);
// PHPのシンタックスチェック
if (PhpCodeLinter::isParsablePhpCode($html)) {
return true;
} else {
return __('There may be a problem with the template or plug-in, installed in the blog.');
}
return Fc2BlogTemplate::fc2TemplateSyntax($php_code);
}

/**
@@ -165,8 +140,8 @@ public static function createTemplate($templateId, $blog_id, $device_type, $html
}

// HTMLをPHPテンプレートに変換してテンプレートファイルの作成
$html = self::convertFC2Template($html);
file_put_contents($templatePath, $html);
$php_code = Fc2BlogTemplate::convertToPhp($html);
file_put_contents($templatePath, $php_code);
chmod($templatePath, 0777);

// pluginのPHPを再生成(すでにファイルがあれば不要な処理だが、このタイミングよりよい再生成タイミングがない)
@@ -272,80 +247,6 @@ public function updateByIdAndBlogId($values, $id, $blog_id, $options = array())
return true;
}

/**
* HTMLを解読しPHPテンプレートに変換する
* @param string $html 置換前HTML
* @return string 置換後PHPテンプレート
*/
public static function convertFC2Template(string $html)
{
// PHP文(PHP開始、終了タグ)のエスケープ `<?php〜?>` → `<?php echo '<?'; ?><?php echo 'php'; ?>〜<?php echo '?>' ?>`
$delimit = "\xFF";
$searches = ['<?', '?>'];
$replaces = [$delimit . 'START_TAG_ESCAPE', $delimit . 'END_TAG_ESCAPE'];
$html = str_replace($searches, $replaces, $html);

$html = preg_replace('/(php)/i', "<?php echo '$1'; ?>", $html);

$searches = $replaces;
$replaces = ['<?php echo \'<?\'; ?>', '<?php echo \'?>\'; ?>'];
$html = str_replace($searches, $replaces, $html);

// テンプレート置換用変数読み込み
Config::read('fc2_template.php');
$ambiguous = []; // 既存FC2テンプレートの曖昧置換用

// ループ文用の置き換え
$loop = Config::get('fc2_template_foreach');
foreach ($loop as $key => $value) {
$ambiguous[] = $key;
do {
$html = preg_replace('/<!--' . $key . '-->(.*?)<!--\/' . $key . '-->/s', $value . '$1<?php } ?>', $html, -1, $count);
} while ($count > 0);
}

// 条件判断文用の置き換え
$cond = Config::get('fc2_template_if');
foreach ($cond as $key => $value) {
$ambiguous[] = $key;
do {
$html = preg_replace('/<!--' . $key . '-->(.*?)<!--\/' . $key . '-->/s', $value . '$1<?php } ?>', $html, -1, $count);
} while ($count > 0);
}

// 既存FC2テンプレートの曖昧置換
// (置換しきれなかった(正しく記述されていない)テンプレートをできるだけ処理する)
$ambiguous = implode('|', $ambiguous);
// <!--topentry-->〜<!--/edit_area--> 左記を許容する
foreach ($loop as $key => $value) {
do {
$html = preg_replace('/<!--' . $key . '-->(.*?)<!--\/(' . $ambiguous . ')-->/s', $value . '$1<?php } ?>', $html, -1, $count);
} while ($count);
}
foreach ($cond as $key => $value) {
do {
$html = preg_replace('/<!--' . $key . '-->(.*?)<!--\/(' . $ambiguous . ')-->/s', $value . '$1<?php } ?>', $html, -1, $count);
} while ($count);
}
// <!--/topentry-->〜<!--topentry--> 左記を許容する(テンプレートによってシンタックスエラーが発生する可能性あり)
// 代わりに既存FC2で動作しているテンプレートでタグの相互がおかしいテンプレートも動作する
foreach ($loop as $key => $value) {
do {
$html = preg_replace('/<!--\/(' . $ambiguous . ')-->(.*?)<!--' . $key . '-->/s', '<?php } ?>$2' . $value, $html, -1, $count);
} while ($count);
}
foreach ($cond as $key => $value) {
do {
$html = preg_replace('/<!--\/(' . $ambiguous . ')-->(.*?)<!--' . $key . '-->/s', '<?php } ?>$2' . $value, $html, -1, $count);
} while ($count);
}

// 変数タグの置き換え
$html = str_replace(Config::get('fc2_template_var_search'), Config::get('fc2_template_var_replace'), $html);
// 処理されなかった(対応がなかった)変数タグの削除
return preg_replace('/<%[0-9a-zA-Z_]+?>/', '', $html);
}

static public function getPathDefaultTemplate(): string
{
return static::getPathDefaultTemplateWithDevice(Config::get('DEVICE_PC'));
7 changes: 4 additions & 3 deletions app/src/Web/Controller/User/EntriesController.php
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
use Fc2blog\Model\TagsModel;
use Fc2blog\Service\BlogService;
use Fc2blog\Util\Log;
use Fc2blog\Web\Fc2BlogTemplate;
use Fc2blog\Web\Request;
use Fc2blog\Web\Session;
use InvalidArgumentException;
@@ -317,7 +318,7 @@ private function preview_fc2_template(Request $request): string
$css = $template['css'];

// テンプレートのシンタックスチェック
$syntax = BlogTemplatesModel::fc2TemplateSyntax($html);
$syntax = Fc2BlogTemplate::fc2TemplateSyntax($html);
if ($syntax !== true) {
return 'user/entries/syntax_error.twig';
}
@@ -350,7 +351,7 @@ public function test_template(Request $request, string $html, string $css): stri
$this->setEntriesData($request, $options, $pages);

// テンプレートのシンタックスチェック
$syntax = BlogTemplatesModel::fc2TemplateSyntax($html);
$syntax = Fc2BlogTemplate::fc2TemplateSyntax($html);
if ($syntax !== true) {
throw new InvalidArgumentException("Syntax error in the generated template.");
}
@@ -386,7 +387,7 @@ private function preview_template(Request $request): string
}

// テンプレートのシンタックスチェック
$syntax = BlogTemplatesModel::fc2TemplateSyntax($html);
$syntax = Fc2BlogTemplate::fc2TemplateSyntax($html);
if ($syntax !== true) {
return 'user/entries/syntax_error.twig';
}
110 changes: 109 additions & 1 deletion app/src/Web/Fc2BlogTemplate.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@

use Fc2blog\App;
use Fc2blog\Config;
use Fc2blog\Util\PhpCodeLinter;
use Fc2blog\Web\Controller\User\UserController;

class Fc2BlogTemplate
@@ -106,4 +107,111 @@ static public function preprocessingData(Request $request, array $data): array
return $data;
}

}
/**
* FC2テンプレートの構文チェック
* @param string $php_code
* @return bool|string
*/
public static function fc2TemplateSyntax(string $php_code)
{
if (defined("THIS_IS_TEST")) {
// テンプレート検証用にテンポラリディレクトリが必要だが、テストやCLIでSessionを汚染したくないので
$blog_id = "unitTestOrCliExecute";
} else {
$blog_id = Session::get('blog_id');
}

// フォルダが存在しない場合作成
$templatePath = Config::get('BLOG_TEMPLATE_DIR') . App::getBlogLayer($blog_id) . '/syntax.php';
$templateDir = dirname($templatePath);
if (!file_exists($templateDir)) {
mkdir($templateDir, 0777, true);
}

// HTMLをPHPテンプレートに変換してテンプレートファイルの作成
$html = self::convertToPhp($php_code);
file_put_contents($templatePath, $html);

chmod($templatePath, 0777);
// PHPのシンタックスチェック
if (PhpCodeLinter::isParsablePhpCode($html)) {
return true;
} else {
return __('There may be a problem with the template or plug-in, installed in the blog.');
}
}

/**
* HTMLを解読しPHPテンプレートに変換する
* @param string $fc2template_html 置換前HTML
* @return string 置換後PHPテンプレート
*/
public static function convertToPhp(string $fc2template_html): string
{
// PHP文(PHP開始、終了タグ)のエスケープ `<?php〜?>` → `<?php echo '<?'; ?><?php echo 'php'; ?>〜<?php echo '?>' ?>`
$delimit = "\xFF";
$searches = ['<?', '?>'];
$replaces = [$delimit . 'START_TAG_ESCAPE', $delimit . 'END_TAG_ESCAPE'];
$fc2template_html = str_replace($searches, $replaces, $fc2template_html);

$fc2template_html = preg_replace('/(php)/i', "<?php echo '$1'; ?>", $fc2template_html);

$searches = $replaces;
$replaces = ['<?php echo \'<?\'; ?>', '<?php echo \'?>\'; ?>'];
$fc2template_html = str_replace($searches, $replaces, $fc2template_html);

// テンプレート置換用変数読み込み
Config::read('fc2_template.php');
$ambiguous = []; // 既存FC2テンプレートの曖昧置換用

// ループ文用の置き換え
$loop = Config::get('fc2_template_foreach');
foreach ($loop as $key => $value) {
$ambiguous[] = $key;
do {
$fc2template_html = preg_replace('/<!--' . $key . '-->(.*?)<!--\/' . $key . '-->/s', $value . '$1<?php } ?>', $fc2template_html, -1, $count);
} while ($count > 0);
}

// 条件判断文用の置き換え
$cond = Config::get('fc2_template_if');
foreach ($cond as $key => $value) {
$ambiguous[] = $key;
do {
$fc2template_html = preg_replace('/<!--' . $key . '-->(.*?)<!--\/' . $key . '-->/s', $value . '$1<?php } ?>', $fc2template_html, -1, $count);
} while ($count > 0);
}

// 既存FC2テンプレートの曖昧置換
// (置換しきれなかった(正しく記述されていない)テンプレートをできるだけ処理する)
$ambiguous = implode('|', $ambiguous);
// <!--topentry-->〜<!--/edit_area--> 左記を許容する
foreach ($loop as $key => $value) {
do {
$fc2template_html = preg_replace('/<!--' . $key . '-->(.*?)<!--\/(' . $ambiguous . ')-->/s', $value . '$1<?php } ?>', $fc2template_html, -1, $count);
} while ($count);
}
foreach ($cond as $key => $value) {
do {
$fc2template_html = preg_replace('/<!--' . $key . '-->(.*?)<!--\/(' . $ambiguous . ')-->/s', $value . '$1<?php } ?>', $fc2template_html, -1, $count);
} while ($count);
}
// <!--/topentry-->〜<!--topentry--> 左記を許容する(テンプレートによってシンタックスエラーが発生する可能性あり)
// 代わりに既存FC2で動作しているテンプレートでタグの相互がおかしいテンプレートも動作する
foreach ($loop as $key => $value) {
do {
$fc2template_html = preg_replace('/<!--\/(' . $ambiguous . ')-->(.*?)<!--' . $key . '-->/s', '<?php } ?>$2' . $value, $fc2template_html, -1, $count);
} while ($count);
}
foreach ($cond as $key => $value) {
do {
$fc2template_html = preg_replace('/<!--\/(' . $ambiguous . ')-->(.*?)<!--' . $key . '-->/s', '<?php } ?>$2' . $value, $fc2template_html, -1, $count);
} while ($count);
}

// 変数タグの置き換え
$fc2template_html = str_replace(Config::get('fc2_template_var_search'), Config::get('fc2_template_var_replace'), $fc2template_html);
// 処理されなかった(対応がなかった)変数タグの削除
return preg_replace('/<%[0-9a-zA-Z_]+?>/', '', $fc2template_html);
}
}
10 changes: 2 additions & 8 deletions tests/App/Fc2Template/Fc2TemplateIfTest.php
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

use ErrorException;
use Fc2blog\Config;
use Fc2blog\Model\BlogTemplatesModel;
use Fc2blog\Web\Fc2BlogTemplate;
use Fc2blog\Web\Request;
use ParseError;
use PHPUnit\Framework\TestCase;
@@ -362,17 +362,11 @@ public function test_android()
public function ifStateTester($tag, $expected, $env)
{
$input_template = "<!--{$tag}-->ok<!--/{$tag}-->";
$php_template = $this->convertFc2TemplateToPhpTemplate($input_template);
$php_template = Fc2BlogTemplate::convertToPhp($input_template);
$res = $this->evalPhpTemplate($php_template, $env);
$this->assertEquals($expected, $res);
}

public function convertFc2TemplateToPhpTemplate(string $input_template): string
{
$b = new BlogTemplatesModel();
return $b->convertFC2Template($input_template);
}

/**
* PHPのフラグメントをPHPとして評価してみる
* @param string $php_template
10 changes: 2 additions & 8 deletions tests/App/Fc2Template/Fc2TemplateLoopTest.php
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

use ErrorException;
use Fc2blog\Config;
use Fc2blog\Model\BlogTemplatesModel;
use Fc2blog\Web\Fc2BlogTemplate;
use ParseError;
use PHPUnit\Framework\TestCase;
use TypeError;
@@ -54,18 +54,12 @@ public function test_category_list()
public function loopStateTester($tag, $expected, $env)
{
$input_template = "<!--{$tag}-->ok<!--/{$tag}-->";
$php_template = $this->convertFc2TemplateToPhpTemplate($input_template);
$php_template = Fc2BlogTemplate::convertToPhp($input_template);
$res = $this->evalPhpTemplate($php_template, $env);
// var_dump($res);
$this->assertEquals($expected, $res);
}

public function convertFc2TemplateToPhpTemplate(string $input_template): string
{
$b = new BlogTemplatesModel();
return $b->convertFC2Template($input_template);
}

/**
* PHPのフラグメントをPHPとして評価してみる
* @param string $php_template
10 changes: 3 additions & 7 deletions tests/App/Fc2Template/Fc2TemplateTest.php
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@
use Fc2blog\Exception\RedirectExit;
use Fc2blog\Model\BlogSettingsModel;
use Fc2blog\Model\BlogsModel;
use Fc2blog\Model\BlogTemplatesModel;
use Fc2blog\Model\CommentsModel;
use Fc2blog\Model\EntriesModel;
use Fc2blog\Model\EntryCategoriesModel;
@@ -617,12 +616,11 @@ public function evalAll(Request $request, array $data): void
public function getAllPrintableTagEval(Request $request, array $data): void
{
$printable_tags = Config::get('fc2_template_var_search');
$b = new BlogTemplatesModel();
foreach ($printable_tags as $tag_str => $printable_tag) {
// タグの含まれたHTML
$input_html = "{$printable_tag}";
// 変換されたPHP
$converted_php = $b->convertFC2Template($input_html);
$converted_php = Fc2BlogTemplate::convertToPhp($input_html);
$this->fragmentRunner(Fc2BlogTemplate::preprocessingData($request, $data), $tag_str, $converted_php);
}
}
@@ -635,10 +633,9 @@ public function getAllPrintableTagEval(Request $request, array $data): void
public function getAllIfCondEval(Request $request, array $data): void
{
$fc2_template_if_list = Config::get('fc2_template_if');
$b = new BlogTemplatesModel();
foreach ($fc2_template_if_list as $tag_str => $php_code) {
$input_html = "<!--{$tag_str}-->BODY<!--/{$tag_str}-->";
$converted_php = $b->convertFC2Template($input_html);
$converted_php = Fc2BlogTemplate::convertToPhp($input_html);
$this->fragmentRunner(Fc2BlogTemplate::preprocessingData($request, $data), $tag_str, $converted_php);
}
}
@@ -651,10 +648,9 @@ public function getAllIfCondEval(Request $request, array $data): void
public function getAllForEachCondEval(Request $request, array $data): void
{
$fc2_template_if_list = Config::get('fc2_template_foreach');
$b = new BlogTemplatesModel();
foreach ($fc2_template_if_list as $tag_str => $php_code) {
$input_html = "<!--{$tag_str}-->BODY<!--/{$tag_str}-->";
$converted_php = $b->convertFC2Template($input_html);
$converted_php = Fc2BlogTemplate::convertToPhp($input_html);
$this->fragmentRunner(Fc2BlogTemplate::preprocessingData($request, $data), $tag_str, $converted_php);
}
}