Skip to content

Commit

Permalink
Use explicit device_type instead of req.get('device_type'). fc2blog#338
Browse files Browse the repository at this point in the history
  • Loading branch information
uzulla committed Aug 8, 2021
1 parent bf3f24a commit 9baf7fc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
19 changes: 12 additions & 7 deletions app/src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,7 @@ public static function getDeviceType(Request $request): int

// Cookieからデバイスタイプを取得
$device_type = $request->rawCookie('device');
$devices = [
App::DEVICE_PC,
App::DEVICE_SP,
];
if (!empty($device_type) && in_array($device_type, $devices)) {
if (!empty($device_type) && static::isExistsDeviceId($device_type)) {
return (int)$device_type;
}

Expand All @@ -261,6 +257,16 @@ public static function getDeviceType(Request $request): int
return App::DEVICE_PC;
}

/**
* デバイスタイプが既知のものか?(許可されているか?)
* @param string $id
* @return bool
*/
public static function isExistsDeviceId(string $id): bool
{
return in_array($id, self::ALLOW_DEVICES);
}

/**
* デバイスタイプを取得する
* @param Request $request
Expand All @@ -269,8 +275,7 @@ public static function getDeviceType(Request $request): int
public static function getDeviceTypeStr(Request $request): string
{
$device_id = static::getDeviceType($request);
$device_table = App::DEVICE_FC2_KEY;
return $device_table[$device_id];
return App::DEVICE_FC2_KEY[$device_id] ?? App::DEVICE_FC2_KEY[App::DEVICE_PC];
}

/**
Expand Down
17 changes: 17 additions & 0 deletions app/src/Web/Controller/Admin/BlogTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Fc2blog\Model\Fc2TemplatesModel;
use Fc2blog\Model\Model;
use Fc2blog\Service\BlogService;
use Fc2blog\Util\Log;
use Fc2blog\Web\Request;

class BlogTemplatesController extends AdminController
Expand Down Expand Up @@ -45,6 +46,11 @@ public function index(Request $request): string
}
$this->set('device_blog_templates', $device_blog_templates);
$this->set('devices', BlogTemplatesModel::DEVICE_NAME);
if (!App::isExistsDeviceId($request->get("device_type", (string)App::DEVICE_PC))) {
Log::notice("invalid device_type params :" . $request->get("device_type"));
return $this->error400();
}
$this->set('req_device_type', $request->get("device_type"));

return "admin/blog_templates/index.twig";
}
Expand Down Expand Up @@ -77,6 +83,11 @@ public function fc2_index(Request $request): string
$this->set('templates', $templates);
$this->set('paging', $paging);
$this->set('devices', BlogTemplatesModel::DEVICE_NAME);
if (!App::isExistsDeviceId($request->get("device_type", (string)App::DEVICE_PC))) {
Log::notice("invalid device_type params :" . $request->get("device_type"));
return $this->error400();
}
$this->set('req_device_type', $request->get("device_type"));

return "admin/blog_templates/fc2_index.twig";
}
Expand All @@ -101,6 +112,12 @@ public function fc2_view(Request $request): string
$device_type = $request->get('device_type', (string)App::DEVICE_PC);
$request->set('device_type', $device_type);

if (!App::isExistsDeviceId($request->get("device_type", (string)App::DEVICE_PC))) {
Log::notice("invalid device_type params :" . $request->get("device_type"));
return $this->error400();
}
$this->set('req_device_type', $request->get("device_type"));

// テンプレート取得
$device_key = App::getDeviceFc2Key($device_type);
$template = Model::load('Fc2Templates')->findByIdAndDevice($request->get('fc2_id'), $device_key);
Expand Down
6 changes: 3 additions & 3 deletions app/twig_templates/admin/blog_templates/fc2_index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{% block content %}

<header><h2>{{ _('FC2 Template list') }}[{{ _(attribute(constant('Fc2blog\\App::DEVICE_FC2_KEY'), req.get('device_type'))) }}]</h2></header>
<header><h2>{{ _('FC2 Template list') }}[{{ _(attribute(constant('Fc2blog\\App::DEVICE_FC2_KEY'), req_device_type)) }}]</h2></header>

{% if templates %}
{% for template in templates %}
Expand All @@ -18,11 +18,11 @@
</tr>
<tr>
<td class="btn">
<a class="admin_common_btn create_btn" href="{{ url(req, 'Entries', 'preview', {blog_id: blog.id, fc2_id:template.id, device_type: req.get('device_type')}, false, true) }}" target="_blank">{{ _('Preview') }}</a>
<a class="admin_common_btn create_btn" href="{{ url(req, 'Entries', 'preview', {blog_id: blog.id, fc2_id:template.id, device_type: req_device_type}, false, true) }}" target="_blank">{{ _('Preview') }}</a>
<form action="{{ url(req, 'blog_templates', 'download') }}" method="post" style="display: inline">
<input type="hidden" name="sig" value="{{ sig }}">
<input type="hidden" name="fc2_id" value="{{ template.id }}">
<input type="hidden" name="device_type" value="{{ req.get('device_type') }}">
<input type="hidden" name="device_type" value="{{ req_device_type }}">
<button type="submit" class="admin_common_btn create_btn">{{ _('Download') }}</button>
</form>

Expand Down
4 changes: 2 additions & 2 deletions app/twig_templates/admin/blog_templates/fc2_index_sp.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

{% block content %}

<header><h1 class="sh_heading_main_b">{{ _('FC2 Template list') }}[{{ _(attribute(constant('Fc2blog\\App::DEVICE_FC2_KEY'), req.get('device_type'))) }}]</h1></header>
<header><h1 class="sh_heading_main_b">{{ _('FC2 Template list') }}[{{ _(attribute(constant('Fc2blog\\App::DEVICE_FC2_KEY'), req_device_type)) }}]</h1></header>

{% if templates %}
<ul class="template_list">
{% for template in templates %}
<li class="template_list_item">
<a href="{{ url(req, 'blog_templates', 'fc2_view', {fc2_id: template.id, device_type: req.get('device_type')}) }}">
<a href="{{ url(req, 'blog_templates', 'fc2_view', {fc2_id: template.id, device_type: req_device_type}) }}">
<img class="template_img" src="{{ template.image }}" alt="{{ template.name }}">
<p class="template_name">{{ template.name }}</p>
</a>
Expand Down
6 changes: 3 additions & 3 deletions app/twig_templates/admin/blog_templates/fc2_view_sp.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

{% block content %}

<header><h1 class="sh_heading_main_b">{{ _('FC2 Template detail') }}[{{ _(attribute(constant('Fc2blog\\App::DEVICE_FC2_KEY'), req.get('device_type'))) }}]</h1></header>
<header><h1 class="sh_heading_main_b">{{ _('FC2 Template detail') }}[{{ _(attribute(constant('Fc2blog\\App::DEVICE_FC2_KEY'), req_device_type)) }}]</h1></header>
<h2><span class="h2_inner">テンプレートの詳細</span></h2>

<div class="template_detail">
<form action="{{ url(req, 'blog_templates', 'download') }}" method="post" id="template_download_form">
<input type="hidden" name="sig" value="{{ sig }}">
<input type="hidden" name="fc2_id" value="{{ template.id }}">
<input type="hidden" name="device_type" value="{{ req.get('device_type') }}">
<input type="hidden" name="device_type" value="{{ req_device_type }}">
</form>
<div class="left_column">
<p class="template_img">
Expand All @@ -19,7 +19,7 @@
</div>
<div class="right_column">
<p>
<a class="btn_contents touch" href="{{ url(req, 'Entries', 'preview', {blog_id: blog.id, fc2_id: template.id, device_type: req.get('device_type')}, false, true) }}" target="_blank">{{ _('Preview') }}</a>
<a class="btn_contents touch" href="{{ url(req, 'Entries', 'preview', {blog_id: blog.id, fc2_id: template.id, device_type: req_device_type}, false, true) }}" target="_blank">{{ _('Preview') }}</a>
</p>
<p>
<button class="btn_contents touch" onclick="$('#template_download_form').submit()">{{ _('Download') }}</button>
Expand Down
2 changes: 1 addition & 1 deletion app/twig_templates/admin/blog_templates/index_sp.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="form_contents">
<select onchange="location.href=$(this).val();">
{% for key, device_en in devices %}
<option value="{{ url(req, 'BlogTemplates', 'index', {device_type:key}) }}" {% if req.get('device_type') == key %}selected="selected"{% endif %}>{{ _(device_en) }}</option>
<option value="{{ url(req, 'BlogTemplates', 'index', {device_type:key}) }}" {% if req_device_type == key %}selected="selected"{% endif %}>{{ _(device_en) }}</option>
{% endfor %}
</select>
</div>
Expand Down

0 comments on commit 9baf7fc

Please sign in to comment.