Skip to content

Commit

Permalink
fix #2483 特定の文字を送信すると文字化けする
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Nov 4, 2023
1 parent 85643c0 commit f4180c0
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 76 deletions.
47 changes: 0 additions & 47 deletions plugins/baser-core/src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public function beforeFilter(EventInterface $event)
$response = $this->redirectIfIsRequireMaintenance();
if ($response) return $response;

$this->__convertEncodingHttpInput();
$this->__cleanupQueryParams();

// インストーラー、アップデーターの場合はテーマを設定して終了
Expand Down Expand Up @@ -259,52 +258,6 @@ public function _blackHoleCallback($err, $exception)
throw new BadRequestException($message);
}

/**
* http経由で送信されたデータを変換する
* とりあえず、UTF-8で固定
*
* @return void
* @checked
* @noTodo
* @unitTest
*/
private function __convertEncodingHttpInput(): void
{
if ($this->getRequest()->getData()) {
$this->setRequest($this->request->withParsedBody($this->_autoConvertEncodingByArray($this->getRequest()->getData(), 'UTF-8')));
}
}

/**
* 配列の文字コードを変換する
*
* @param array $data 変換前データ
* @param string $outenc 変換後の文字コード
* @return array 変換後データ
* @checked
* @noTodo
* @unitTest
*/
protected function _autoConvertEncodingByArray($data, $outenc = 'UTF-8'): array
{
if (!$data) return [];
foreach($data as $key => $value) {
if (is_array($value)) {
$data[$key] = $this->_autoConvertEncodingByArray($value, $outenc);
continue;
}
$inenc = mb_detect_encoding((string)$value);
if (!$inenc) continue;
if (!in_array($inenc, Configure::read('BcEncode.detectOrder'))) continue;
if ($inenc === $outenc) continue;
// 半角カナは一旦全角に変換する
$value = mb_convert_kana($value, 'KV', $inenc);
$value = mb_convert_encoding($value, $outenc, $inenc);
$data[$key] = $value;
}
return $data;
}

/**
* クエリーパラメーターの調整
* 環境によって?キーにamp;が付加されてしまうため
Expand Down
29 changes: 0 additions & 29 deletions plugins/baser-core/tests/TestCase/Controller/AppControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,35 +223,6 @@ public function testRedirectIfIsRequireMaintenance()
Configure::write('debug', true);
}

/**
* test _autoConvertEncodingByArray
*/
public function test_autoConvertEncodingByArray()
{
$data = [
'test' => [
'test' => mb_convert_encoding('あいうえお', 'EUC-JP')
]
];
$result = $this->execPrivateMethod($this->AppController, '_autoConvertEncodingByArray', [$data, 'UTF-8']);
$this->assertEquals('あいうえお', $result['test']['test']);
}

/**
* test __convertEncodingHttpInput
*/
public function test__convertEncodingHttpInput()
{
$data = [
'test' => [
'test' => mb_convert_encoding('あいうえお', 'EUC-JP')
]
];
$this->AppController->setRequest($this->AppController->getRequest()->withParsedBody($data));
$this->execPrivateMethod($this->AppController, '__convertEncodingHttpInput');
$this->assertEquals('あいうえお', $this->AppController->getRequest()->getData('test.test'));
}

/**
* test __cleanupQueryParams
*/
Expand Down

0 comments on commit f4180c0

Please sign in to comment.