forked from fc2blog/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntriesController.php
More file actions
1205 lines (1049 loc) · 40.9 KB
/
Copy pathEntriesController.php
File metadata and controls
1205 lines (1049 loc) · 40.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Fc2blog\Web\Controller\User;
use Fc2blog\App;
use Fc2blog\Config;
use Fc2blog\Model\BlogPluginsModel;
use Fc2blog\Model\BlogSettingsModel;
use Fc2blog\Model\BlogsModel;
use Fc2blog\Model\BlogTemplatesModel;
use Fc2blog\Model\CategoriesModel;
use Fc2blog\Model\CommentsModel;
use Fc2blog\Model\EntriesModel;
use Fc2blog\Model\Model;
use Fc2blog\Model\TagsModel;
use Fc2blog\Util\Log;
use Fc2blog\Web\Request;
use Fc2blog\Web\Session;
use InvalidArgumentException;
use LogicException;
use Tuupola\Base62Proxy;
class EntriesController extends UserController
{
/**
* 記事系統の前処理
* @param Request $request
*/
protected function beforeFilter(Request $request)
{
parent::beforeFilter($request);
// ブログID指定があるかチェック
$blog_id = $this->getBlogId($request);
if (!$blog_id) {
Log::notice("missing blog_id parameter. redirect to top.");
$this->redirect($request, ['controller' => 'Blogs', 'action' => 'index']);
}
$this->set('blog_id', $blog_id);
// 指定のブログが実在するかチェック
if (!($blog = $this->getBlog($blog_id)) || !is_array($blog)) {
Log::notice("not found blog, redirect to top. blog_id: {$blog_id}");
$this->redirect($request, ['controller' => 'Blogs', 'action' => 'index']);
}
// BlogのSSL_Enableの設定と食い違うなら強制リダイレクトする
// URL構造そのままでリダイレクトするためにRequestUriを用いているがもっとベターな方法があるかもしれない
if (!BlogsModel::isCorrectHttpSchemaByBlogArray($request, $blog)) {
Log::debug("mismatch access schema and blog's schema. redirect to correct schema. blog_id:{$blog['id']}");
$this->redirect($request, $request->uri, '', true, $blog['id']);
}
$this->set('blog', $blog);
$blog_settings_model = new BlogSettingsModel();
$this->set('blog_setting', $blog_settings_model->findByBlogId($blog_id));
// 自身の所持しているブログ判定
$self_blog = $this->isLoginBlog($request);
$this->set('self_blog', $self_blog);
// 非公開モードの場合はパスワード認証画面へ遷移
if ($blog['open_status'] == Config::get('BLOG.OPEN_STATUS.PRIVATE')
&& !Session::get($this->getBlogPasswordKey($blog['id']))
&& $request->methodName != 'blog_password'
&& !$self_blog
) {
Log::debug("password required. redirect to password auth page. blog_id:{$blog['id']}");
$this->redirect($request, array('action' => 'blog_password', 'blog_id' => $blog_id));
}
// 予約投稿と期間投稿エントリーの更新処理
// TODO: ここにあるのがふさわしいのか?
if (Config::get('CRON') === false) {
$entries_model = new EntriesModel();
$entries_model->updateReservation($blog_id);
$entries_model->updateLimited($blog_id);
}
}
/**
* 一覧表示 アクション
* @param Request $request
* @return string
*/
public function index(Request $request)
{
$blog_id = $this->getBlogId($request);
if (!$blog_id) {
Log::notice("missing blog_id parameter. redirect to top. blog_id: {$blog_id}");
$this->redirect($request, ['controller' => 'Blogs', 'action' => 'index']);
}
// 記事一覧データ設定
$options = [
'where' => 'blog_id=?',
'params' => [$blog_id],
];
$areas = $request->get('page') ? [] : ['index_area'];
$this->setEntriesData($request, $options, $areas);
return $this->getFc2TemplatePath($this->getBlogId($request));
}
/**
* 検索
* @param Request $request
* @return string
*/
public function search(Request $request)
{
$where = 'blog_id=?';
$params = array($this->getBlogId($request));
// 検索ワード取得
if ($keyword = $request->get('q')) {
$this->set('sub_title', $request->get('q'));
$keyword = Model::escape_wildcard($keyword);
$keyword = "%{$keyword}%";
$where .= ' AND (title LIKE ? OR body LIKE ?)';
$params = array_merge($params, array($keyword, $keyword));
}
$options = array(
'where' => $where,
'params' => $params,
);
$this->setEntriesData($request, $options, array('search_area'));
return $this->getFc2TemplatePath($this->getBlogId($request));
}
/**
* カテゴリー検索
* @param Request $request
* @return string
*/
public function category(Request $request)
{
$blog_id = $this->getBlogId($request);
$category_id = $request->get('cat');
// カテゴリー名取得
$category = Model::load('Categories')->findByIdAndBlogId($category_id, $blog_id);
$this->set('sub_title', $category['name']);
// 記事一覧データ設定
$where = 'entries.blog_id=?';
$where .= ' AND entry_categories.blog_id=?';
$where .= ' AND entry_categories.category_id=?';
$where .= ' AND entries.id=entry_categories.entry_id';
$params = array($blog_id, $blog_id, $category_id);
$order = $category['category_order'] == Config::get('CATEGORY.ORDER.ASC') ? 'ASC' : 'DESC';
$options = array(
'fields' => 'entries.*',
'where' => $where,
'from' => 'entry_categories',
'params' => $params,
'order' => 'entries.posted_at ' . $order . ', entries.id ' . $order,
);
$this->setEntriesData($request, $options, array('category_area'));
return $this->getFc2TemplatePath($this->getBlogId($request));
}
/**
* タグ検索
* @param Request $request
* @return string
*/
public function tag(Request $request)
{
// タグ検索
$blog_id = $this->getBlogId($request);
$tag_name = $request->get('tag');
$tag = Model::load('Tags')->findByNameAndBlogId($tag_name, $blog_id);
$tag_id = empty($tag) ? 0 : $tag['id'];
$this->set('sub_title', $tag_name);
// 記事一覧データ設定
$where = 'entries.blog_id=?';
$where .= ' AND entry_tags.blog_id=?';
$where .= ' AND entry_tags.tag_id=?';
$where .= ' AND entries.id=entry_tags.entry_id';
$params = array($blog_id, $blog_id, $tag_id);
$options = array(
'fields' => 'entries.*',
'where' => $where,
'from' => 'entry_tags',
'params' => $params,
);
$this->setEntriesData($request, $options, array('tag_area'));
return $this->getFc2TemplatePath($this->getBlogId($request));
}
/**
* 年別,月別,日別表示
* @param Request $request
* @return string
*/
public function date(Request $request)
{
// 開始日付と終了日付の計算
preg_match('/^([0-9]{4})([0-9]{2})?([0-9]{2})?$/', $request->get('date'), $matches);
$dates = $matches + array('', date('Y'), 0, 0);
list($start, $end) = App::calcStartAndEndDate($dates[1], $dates[2], $dates[3]);
// 記事一覧データ設定
$where = 'blog_id=? AND ?<=posted_at AND posted_at<=?';
$params = array($this->getBlogId($request), $start, $end);
$options = array(
'where' => $where,
'params' => $params,
);
$this->setEntriesData($request, $options, array('date_area'));
$this->set('now_date', date('Y-m-d', strtotime($start)));
return $this->getFc2TemplatePath($this->getBlogId($request));
}
/**
* アーカイブ表示
* @param Request $request
* @return string
*/
public function archive(Request $request)
{
// 記事一覧データ設定
$options = array(
'fields' => array(
'id', 'blog_id', 'title', 'posted_at', 'comment_count',
Config::get('ENTRY.AUTO_LINEFEED.NONE') . ' as auto_linefeed',
'SUBSTRING(body, 1, 20) as body'
),
'where' => 'blog_id=?',
'params' => array($this->getBlogId($request)),
);
$this->setEntriesData($request, $options, array('titlelist_area'));
$this->set('sub_title', __("List of articles"));
return $this->getFc2TemplatePath($this->getBlogId($request));
}
/**
* プレビュー表示
* @param Request $request
* @return string
*/
public function preview(Request $request)
{
// XSS-Protection無効
if (!headers_sent()) {
header("X-XSS-Protection: 0");
}
// preview処理用
$blog_id = $this->getBlogId($request);
// 投稿者のブログIDチェック
if ($blog_id != $this->getAdminBlogId() && !Model::load('Blogs')->isUserHaveBlogId($this->getAdminUserId(), $blog_id)) {
return $this->error404();
}
// 記事のプレビュー
if ($request->get('entry')) {
return $this->preview_entry($request);
}
// FC2テンプレートのプレビュー
if ($request->get('fc2_id') && $request->get('device_type')) {
return $this->preview_fc2_template($request);
}
// テンプレートのプレビュー
if ($request->get('blog_template') || $request->get('template_id')) {
return $this->preview_template($request);
}
// プラグインのプレビュー
if ($request->get('blog_plugin') || $request->get('plugin_id')) {
return $this->preview_plugin($request);
}
// 当てはまらない場合は404画面を表示
return $this->error404();
}
/**
* FC2テンプレート用のプレビュー
* @param Request $request
* @return string
*/
private function preview_fc2_template(Request $request)
{
$blog_id = $this->getBlogId($request);
// 記事一覧データ設定
$options = array(
'where' => 'blog_id=?',
'params' => array($this->getBlogId($request)),
);
$pages = $request->get('page') ? array() : array('index_area');
$this->setEntriesData($request, $options, $pages);
// テンプレートのプレビュー
$device_key = Config::get('DEVICE_FC2_KEY.' . $request->get('device_type'));
$template = Model::load('Fc2Templates')->findByIdAndDevice($request->get('fc2_id'), $device_key);
if (empty($template)) {
return $this->error404();
}
$html = $template['html'];
$css = $template['css'];
// テンプレートのシンタックスチェック
$syntax = BlogTemplatesModel::fc2TemplateSyntax($html);
if ($syntax !== true) {
return 'user/entries/syntax_error.twig';
}
// FC2用のテンプレートで表示
return $this->getFc2TemplatePath($blog_id, $html, $css, true);
}
/**
* 一時的なテンプレートファイルの生成と実行(テスト用
* @param Request $request
* @param string $html テンプレートのString
* @param string $css テンプレートのCSS
* @return string temporary compiled template file path
*/
public function test_template(Request $request, string $html, string $css)
{
if (!defined("THIS_IS_TEST")) { // テスト以外ではブロックする
throw new LogicException("the method is allowed only in testing.");
}
$blog_id = $this->getBlogId($request);
// 記事一覧データ設定
$options = [
'where' => 'blog_id=?',
'params' => [$blog_id],
];
$pages = $request->get('page') ? [] : ['index_area'];
$this->setEntriesData($request, $options, $pages);
// テンプレートのシンタックスチェック
$syntax = BlogTemplatesModel::fc2TemplateSyntax($html);
if ($syntax !== true) {
throw new InvalidArgumentException("Syntax error in the generated template.");
}
return $this->getFc2TemplatePath($blog_id, $html, $css, true);
}
/**
* テンプレート用のプレビュー
* @param Request $request
* @return string
*/
private function preview_template(Request $request)
{
$blog_id = $this->getBlogId($request);
// 記事一覧データ設定
$options = array(
'where' => 'blog_id=?',
'params' => array($this->getBlogId($request)),
);
$pages = $request->get('page') ? array() : array('index_area');
$this->setEntriesData($request, $options, $pages);
// テンプレートのプレビュー
if ($request->get('template_id')) {
$blog_template = Model::load('BlogTemplates')->findByIdAndBlogId($request->get('template_id'), $blog_id);
$html = $blog_template['html'];
$css = $blog_template['css'];
} else {
$html = $request->get('blog_template.html');
$css = $request->get('blog_template.css');
}
// テンプレートのシンタックスチェック
$syntax = BlogTemplatesModel::fc2TemplateSyntax($html);
if ($syntax !== true) {
return 'user/entries/syntax_error.twig';
}
// FC2用のテンプレートで表示
return $this->getFc2TemplatePath($blog_id, $html, $css, true);
}
/**
* プラグイン用のプレビュー
* @param Request $request
* @return string
*/
private function preview_plugin(Request $request)
{
$blog_id = $this->getBlogId($request);
// プラグインのプレビュー情報取得
$preview_plugin = null;
if ($request->get('plugin_id')) {
// DBからプレビュー情報取得
$preview_plugin = Model::load('Plugins')->findById($request->get('plugin_id'));
$preview_plugin['category'] = $request->get('category');
} else {
// リクエストパラメータからプレビュー情報取得
$preview_plugin = $request->get('blog_plugin');
$preview_plugin['list'] = ''; // TODO:リストスタイルは未作成
$preview_plugin['attribute'] = ''; // TODO:属性は未作成
}
$contents = $preview_plugin['contents'];
// テンプレートのシンタックスチェック
$syntax = BlogPluginsModel::fc2PluginSyntax($contents);
if ($syntax !== true) {
return 'user/entries/syntax_error.twig';
}
// プラグインのPHPファイル作成
BlogPluginsModel::createPlugin($contents, $blog_id);
// 入力データからデータを作成
$category = $preview_plugin['category'];
$device_type = $preview_plugin['device_type'];
$plugin = array(
'id' => 'preview',
'blog_id' => $blog_id,
'title' => $preview_plugin['title'],
'title_align' => $preview_plugin['title_align'],
'title_color' => $preview_plugin['title_color'],
'list' => $preview_plugin['list'],
'contents' => $preview_plugin['contents'],
'contents_align' => $preview_plugin['contents_align'],
'contents_color' => $preview_plugin['contents_color'],
'attribute' => $preview_plugin['attribute'],
'device_type' => $device_type,
'category' => $category,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
);
// スマフォ版のプラグインのプレビュー表示
if ($device_type == Config::get('DEVICE_SP')) {
$this->set('s_plugin', $plugin);
$this->setAreaData(array('spplugin_area'));
return $this->getFc2TemplatePath($blog_id);
}
// 記事一覧データ設定(スマフォ版以外のプレビュー表示)
$options = array(
'where' => 'blog_id=?',
'params' => array($this->getBlogId($request)),
);
$pages = $request->get('page') ? array() : array('index_area');
$this->setEntriesData($request, $options, $pages);
// 通常のプラグインリストに追加する
$plugins = Model::load('BlogPlugins')->findByDeviceTypeAndCategory($this->getDeviceType(), $category, $blog_id);
$id = $request->get('id');
if (empty($id)) {
// 新規プラグインは最後尾に追加する
$plugins[] = $plugin;
} else {
// 編集の場合は上書きする
foreach ($plugins as $key => $value) {
if ($value['id'] == $id) {
$plugins[$key] = $plugin;
}
}
}
$this->set('t_plugins_' . $category, $plugins);
// FC2用のテンプレートで表示
return $this->getFc2TemplatePath($blog_id);
}
/**
* 記事用のプレビュー
* @param Request $request
* @return string
*/
private function preview_entry(Request $request)
{
$blog_id = $this->getBlogId($request);
// DBの代わりにリクエストから取得
$entry = array(
'id' => 0,
'blog_id' => $blog_id,
'title' => $request->get('entry.title'),
'body' => $request->get('entry.body'),
'extend' => $request->get('entry.extend'),
'posted_at' => $request->get('entry.posted_at', date('Y-m-d H:i:s')),
'auto_linefeed' => $request->get('entry.auto_linefeed'),
'open_status' => Config::get('ENTRY.OPEN_STATUS.OPEN'),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
);
$entry['categories'] = Model::load('Categories')->findByIdsAndBlogId($request->get('entry_categories.category_id'), $blog_id);
foreach ($entry['categories'] as $key => $value) {
$entry['categories'][$key]['entry_id'] = 0;
}
$entry['tags'] = array();
$tags = $request->get('entry_tags');
if (is_countable($tags) && count($tags)) {
foreach ($tags as $tag) {
$entry['tags'][] = array(
'id' => 0,
'blog_id' => $blog_id,
'name' => $tag,
'count' => 0,
);
}
}
$this->set('entry', $entry);
$this->set('comments', array());
$this->set('sub_title', $entry['title']);
// FC2用のテンプレートで表示
$areas = array('permanent_area');
if (App::isPC($request)) {
$areas[] = 'comment_area';
}
$this->setAreaData($areas);
return $this->getFc2TemplatePath($entry['blog_id']);
}
/**
* 詳細(1エントリ)表示 アクション
* @param Request $request
* @return string
*/
public function view(Request $request): string
{
$blog_id = $this->getBlogId($request);
$entry_id = $request->get('id');
// 記事詳細取得
$entries_model = new EntriesModel();
$entry = $entries_model->getEntry($entry_id, $blog_id);
if (!$entry) return $this->error404();
$this->set('entry', $entry);
$this->set('sub_title', $entry['title']); // HTMLのサブタイトル
// 表示エリア、データセット分岐処理
// (スマホはコメントと、コメント投稿画面が別々)
$sp_comment_view_mode = $request->get('m2'); // スマホ用、フォーム、返信画面判定パラメタ
$areas = [];
switch ($sp_comment_view_mode) {
case 'form': // コメント投稿表示(スマフォ)
$areas[] = 'form_area';
break;
case 'res': // コメント一覧表示(スマフォ)
$this->setCommentsData($request, $blog_id, $entry_id, $entry, true);
$areas[] = 'comment_area';
break;
default: // PCや上記以外
// PC場合、記事のコメント必要
if (App::isPC($request)) {
$is_comment_settled = $this->setCommentsData($request, $blog_id, $entry_id, $entry, false);
if ($is_comment_settled) {
$areas[] = 'comment_area';
}
}
$this->setNextPrevEntryData($blog_id, $entry);
$areas[] = 'permanent_area';
break;
}
$this->setAreaData($areas);
// FC2用のテンプレートで表示
return $this->getFc2TemplatePath($entry['blog_id']);
}
/**
* 記事の「次の記事」「前の記事」情報のセット
* @param string $blog_id
* @param array $entry
*/
private function setNextPrevEntryData(string $blog_id, array $entry)
{
$entries_model = new EntriesModel();
$blog_settings_model = new BlogSettingsModel();
// ブログの設定情報取得
$blog_setting = $blog_settings_model->findByBlogId($blog_id);
// 前後の記事取得
$is_asc = $blog_setting['entry_order'] == Config::get('ENTRY.ORDER.ASC');
$this->set('next_entry', $is_asc ? $entries_model->nextEntry($entry) : $entries_model->prevEntry($entry));
$this->set('prev_entry', $is_asc ? $entries_model->prevEntry($entry) : $entries_model->nextEntry($entry));
}
/**
* 記事のコメント情報のセット(パスワード制限時はセットしない)
* @param Request $request
* @param string $blog_id
* @param int $entry_id
* @param array $entry
* @param bool $is_need_paging
* @return bool セットしたか(パスワード制限でブロックされなかったか)
*/
private function setCommentsData(Request $request, string $blog_id, int $entry_id, array $entry, bool $is_need_paging): bool
{
if ($this->isEntryNeedAuth($entry) && !$this->isUserAuthedToEntry($request, $entry)) {
return false;
}
$comments_model = new CommentsModel();
$blog_settings_model = new BlogSettingsModel();
// ブログの設定情報取得
// TODO あるエントリのcommentsを取得するこれらはモデルに移動したほうがよいのではないか?
$blog_setting = $blog_settings_model->findByBlogId($blog_id);
$options = $comments_model->getCommentListOptionsByBlogSetting($blog_id, $entry_id, $blog_setting);
if ($is_need_paging) {
// コメント一覧を取得(ページング用)
$options['page'] = $request->get('page', 0, Request::VALID_UNSIGNED_INT);
$this->set('paging', $comments_model->getPaging($options));
}
$comments = $comments_model->find('all', $options);
$comments = $comments_model->decorateByBlogSetting($request, $comments, $blog_setting, $this->isLoginBlog($request));
$this->set('comments', $comments);
return true;
}
/**
* アクセスユーザーが該当エントリのアクセス権があるか?
* @param $request
* @param $entry
* @return bool
*/
private function isUserAuthedToEntry($request, $entry): bool
{
return (
$this->isLoginBlog($request) || // 管理者であるか
Session::get($this->getEntryPasswordKey($entry['blog_id'], $entry['id']), false) // パスワードアクセスで許可されているか
);
}
/**
* 指定のエントリはアクセス権が必要か?
* @param $entry
* @return bool
*/
private function isEntryNeedAuth($entry): bool
{
if (!isset($entry['open_status'])) {
throw new InvalidArgumentException('missing open_status');
}
return $entry['open_status'] == Config::get('ENTRY.OPEN_STATUS.PASSWORD');
}
/**
* プラグインページの表示
* @param Request $request
* @return string
*/
public function plugin(Request $request)
{
$blog_id = $this->getBlogId($request);
$id = $request->get('id');
// プラグイン取得
$plugin = Model::load('BlogPlugins')->findByIdAndBlogId($id, $blog_id);
$this->set('s_plugin', $plugin);
// FC2用のテンプレートで表示
$this->setAreaData(array('spplugin_area'));
return $this->getFc2TemplatePath($blog_id);
}
/**
* 記事のパスワード認証
* @param Request $request
*/
public function password(Request $request)
{
$blog_id = $this->getBlogId($request);
$id = $request->get('id');
// 記事詳細取得
$entry = Model::load('Entries')->findByIdAndBlogId($id, $blog_id);
if (!$entry) {
$this->redirect($request, array('action' => 'index', 'blog_id' => $blog_id));
}
// パスワード入力チェック
if ($entry['password'] === '') {
// パスワード未設定の場合は全体のパスワードを設定
$blog_setting = Model::load('BlogSettings')->findByBlogId($blog_id);
$entry['password'] = $blog_setting['entry_password'];
}
if ($entry['password'] === $request->get('password', '')) {
// パスワードが合致すればセッションに記録
Session::set($this->getEntryPasswordKey($entry['blog_id'], $entry['id']), true);
}
$this->redirect($request, array('action' => 'view', 'blog_id' => $blog_id, 'id' => $id));
}
/**
* ブログのパスワード認証
* @param Request $request
* @return string
*/
public function blog_password(Request $request): string
{
$blog_id = $this->getBlogId($request);
$blog = $this->getBlog($blog_id);
// プライベートブログではない、あるいは認証済み、ログイン済みならリダイレクト
if ($blog['open_status'] != Config::get('BLOG.OPEN_STATUS.PRIVATE') || Session::get($this->getBlogPasswordKey($blog['id'])) || $this->isLoginBlog($request)) {
$this->redirect($request, ['action' => 'index', 'blog_id' => $blog_id]);
}
// 認証処理
if ($request->get('blog')) {
if (password_verify($request->get('blog.password'), $blog['blog_password'])) {
Session::set($this->getBlogPasswordKey($blog['id']), true);
$this->set('auth_success', true); // for testing.
$this->redirect($request, ['action' => 'index', 'blog_id' => $blog_id]);
}
$this->set('errors', ['password' => __('The password is incorrect!')]);
}
$this->set('blog', $blog);
return "user/entries/blog_password.twig";
}
/**
* コメント投稿
* @param Request $request
* @return string
*/
public function comment_regist(Request $request): string
{
$blog_id = $this->getBlogId($request);
// ブログの設定情報取得(captchaの使用可否で画面切り替え)
$blog_setting = (new BlogSettingsModel())->findByBlogId($blog_id);
$blog = (new BlogsModel())->findById($blog_id);
$is_captcha = $blog_setting['comment_captcha'] == Config::get('COMMENT.COMMENT_CAPTCHA.USE');
// FC2テンプレートにリクエスト情報を合わせる
if (!$is_captcha || !$request->isArgs('token')) {
$pattern = [
'comment.no' => 'comment.entry_id',
'comment.pass' => 'comment.password',
'comment.himitu' => 'comment.open_status',
];
$request->combine($pattern); // 引数のキーを入れ替える
if ($request->get('comment.open_status') == 'on') {
$request->set('comment.open_status', Config::get('COMMENT.OPEN_STATUS.PRIVATE'));
}
}
$entry_id = $request->get('comment.entry_id');
// 記事詳細取得
$entries_model = new EntriesModel();
$entry = $entries_model->getCommentAcceptedEntry($entry_id, $blog_id);
if (!$entry) {
$this->redirect($request, ['action' => 'view', 'blog_id' => $blog_id, 'id' => $entry_id]);
}
// 公開非公開のプルダウン用バリエーション(固定)
$this->set('open_status_user_list', CommentsModel::getOpenStatusUserList());
// CAPTCHA用に確認画面を挟む
if ($is_captcha && !$request->isArgs('token')) {
return "user/entries/register_comment_form.twig";
}
// 記事のカテゴリ一覧を取得 TODO:後でcacheを使用する形に
$entry['categories'] = (new CategoriesModel)->getEntryCategories($blog_id, $entry_id);
$entry['tags'] = (new TagsModel())->getEntryTags($blog_id, $entry_id);
$this->set('entry', $entry);
// 入力チェック処理
$comments_model = new CommentsModel();
$errors = array();
$white_list = array('entry_id', 'name', 'title', 'mail', 'url', 'body', 'password', 'open_status');
$errors['comment'] = $comments_model->registerValidate($request->get('comment'), $data, $white_list);
$errors['token'] = $is_captcha ? $this->tokenValidate($request) : array(); // Token用のバリデート
if (empty($errors['comment']) && empty($errors['token'])) {
$data['blog_id'] = $blog_id; // ブログIDの設定
// trip_hashの生成
if (isset($data['password']) && strlen($data['password'] > 0)) {
$stretch_num = strlen($data['password']) % 10 + 1;
$trip_salt = $blog['trip_salt'];
$trip_hash = $trip_salt . $data['password'];
for ($i = 0; $i < $stretch_num; $i++) {
$trip_hash = Base62Proxy::encode(hash('sha256', $trip_hash, true));
}
$trip_hash_length = 8;
$data['trip_hash'] = substr($trip_hash, 0, $trip_hash_length);
} else {
$data['trip_hash'] = '';
}
if ($id = $comments_model->insertByBlogSetting($request, $data, $blog_setting)) {
$this->redirect($request, array('action' => 'view', 'blog_id' => $blog_id, 'id' => $entry_id), '#comment' . $id);
}
}
// Captcha使用時のエラー画面
if ($is_captcha) {
$this->set('errors', $errors);
return "user/entries/register_comment_form.twig";
}
// コメント投稿エラー
$this->fc2CommentError('comment', $errors['comment'], $data);
// FC2用のテンプレートで表示
$this->setAreaData(array(App::isPC($request) ? 'comment_area' : 'form_area'));
return $this->getFc2TemplatePath($entry['blog_id']);
}
/**
* コメント編集画面
* @param Request $request
* @return string
*/
public function comment_edit(Request $request): string
{
$blog_id = $this->getBlogId($request);
// ブログの設定情報を取得
$blog_setting = (new BlogSettingsModel())->findByBlogId($blog_id);
$is_captcha = $blog_setting['comment_captcha'] == Config::get('COMMENT.COMMENT_CAPTCHA.USE');
// FC2テンプレートの引数を受け側で合わせる
if (!$is_captcha || !$request->isArgs('token')) {
$pattern = [
'edit.rno' => 'comment.id',
'edit.name' => 'comment.name',
'edit.title' => 'comment.title',
'edit.mail' => 'comment.mail',
'edit.url' => 'comment.url',
'edit.body' => 'comment.body',
'edit.pass' => 'comment.password',
'edit.himitu' => 'comment.open_status',
'edit.delete' => 'comment.delete',
];
$request->combine($pattern);
if ($request->get('comment.open_status') == 'on') {
$request->set('comment.open_status', Config::get('COMMENT.OPEN_STATUS.PRIVATE'));
}
}
$comment_id = $request->get('id', $request->get('comment.id'));
// 編集対象のコメント取得
$comments_model = new CommentsModel();
$comment = $comments_model->getEditableComment($comment_id, $blog_id);
if (empty($comment)) {
$this->redirect($request, array('action' => 'index', 'blog_id' => $blog_id));
}
// 編集対象の親記事
$entry_id = $comment['entry_id'];
if (!($entry = (new EntriesModel())->getCommentAcceptedEntry($entry_id, $blog_id))) {
$this->redirect($request, ['action' => 'view', 'blog_id' => $blog_id, 'id' => $entry_id]);
}
$this->set('edit_entry', $entry);
// 初期表示処理
if (!$request->get('comment.id')) {
$this->set('edit_comment', $comment);
// FC2用のテンプレートで表示
$this->setAreaData(['edit_area']);
return $this->getFc2TemplatePath($blog_id);
}
// 削除ボタンを押された場合の処理(comment_deleteに処理を移譲)
if ($request->get('comment.delete')) {
return $this->comment_delete($request);
}
// 公開非公開のプルダウン用バリエーション(固定)
$this->set('open_status_user_list', CommentsModel::getOpenStatusUserList());
// Captcha画面の初期表示処理
if ($is_captcha && !$request->isArgs('token')) {
return "user/entries/edit_comment_form.twig";
}
// FC2テンプレート編集時
if (!$is_captcha) {
$this->set('edit_comment', $request->get('comment'));
}
// コメント投稿処理
$errors = [];
$white_list = ['name', 'title', 'mail', 'url', 'body', 'password', 'open_status'];
$errors['comment'] = $comments_model->editValidate($request->get('comment'), $data, $white_list, $comment);
$errors['token'] = $is_captcha ? $this->tokenValidate($request) : []; // Token用のバリデート
if (empty($errors['comment']) && empty($errors['token'])) {
if ($comments_model->updateByIdAndBlogIdAndBlogSetting($request, $data, $comment_id, $blog_id, $blog_setting)) {
$this->redirect($request, ['action' => 'view', 'blog_id' => $blog_id, 'id' => $entry_id], '#comment' . $comment_id);
}
}
// Captcha使用時のエラー画面
if ($is_captcha) {
$this->set('errors', $errors);
return "user/entries/edit_comment_form.twig";
}
// コメント投稿エラー
$this->fc2CommentError('edit', $errors['comment'], ['open_status' => $data['open_status']]);
// FC2用のテンプレートで表示
$this->setAreaData(['edit_area']);
return $this->getFc2TemplatePath($blog_id);
}
/**
* コメントの削除処理
* @param Request $request
* @return string
*/
public function comment_delete(Request $request)
{
$comments_model = new CommentsModel();
$blog_id = $this->getBlogId($request);
$comment_id = $request->get('comment.id');
$comment = "";
if (!$comment_id || !($comment = $comments_model->findByIdAndBlogId($comment_id, $blog_id)) || empty($comment['password'])) {
$this->redirect($request, ['controller' => 'Entries', 'action' => 'index', 'blog_id' => $blog_id]);
}
// コメント削除処理
$errors = [];
$errors['comment'] = $comments_model->editValidate($request->get('comment'), $data, array('password'), $comment);
if (empty($errors['comment'])) {
$comments_model->deleteByIdAndBlogId($comment['id'], $comment['blog_id']);
$this->redirect($request, ['action' => 'view', 'blog_id' => $comment['blog_id'], 'id' => $comment['entry_id']]);
}
// コメント投稿エラー
$this->fc2CommentError('edit', $errors['comment'], ['open_status' => $comment['open_status']]);
// FC2用のテンプレートで表示
$this->setAreaData(['edit_area']);
return $this->getFc2TemplatePath($blog_id);
}
/**
* 一覧情報設定
* @param Request $request
* @param array $options
* @param array $areas
* TODO: これはコントローラが持つべきなのか?Modelでは?
*/
private function setEntriesData(Request $request, $options = [], $areas = []): void
{
$blog_id = $this->getBlogId($request);
$page_num = $request->get('page', 0, Request::VALID_UNSIGNED_INT);
// 検索条件生成
$options = $this->getEntriesQueryOptions($blog_id, $options, $page_num);
// 記事一覧取得
$this->set('entries', $this->getEntriesArray($blog_id, $options));
// paging取得
$this->set('paging', $this->getPaging($options));
// area引数がない場合 TOPページ判定
$this->setAreaData($areas);
}
/**
* @param array $options
* @return array
* TODO: これはコントローラが持つべきなのか?Modelでは?
*/
public static function getPaging(array $options): array
{
$entries_model = new EntriesModel();
return $entries_model->getPaging($options);
}