|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Saito - The Threaded Web Forum |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
|
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
|
10
|
|
|
* @license http://opensource.org/licenses/MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace App\Controller; |
|
14
|
|
|
|
|
15
|
|
|
use Api\Controller\ApiAppController; |
|
16
|
|
|
use App\Model\Entity\Entry; |
|
17
|
|
|
use App\Model\Table\EntriesTable; |
|
18
|
|
|
use Cake\Core\Configure; |
|
19
|
|
|
use Cake\Http\Exception\BadRequestException; |
|
20
|
|
|
use Cake\Http\Exception\HttpException; |
|
21
|
|
|
use Cake\Http\Exception\NotFoundException; |
|
22
|
|
|
use Saito\Exception\SaitoForbiddenException; |
|
23
|
|
|
use Saito\Posting\PostingInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @property EntriesTable $Entries |
|
27
|
|
|
*/ |
|
28
|
|
|
class PostingsController extends ApiAppController |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritDoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function initialize() |
|
34
|
|
|
{ |
|
35
|
|
|
parent::initialize(); |
|
36
|
|
|
$this->loadModel('Entries'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Add new posting. |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function add() |
|
45
|
|
|
{ |
|
46
|
|
|
$data = $this->request->getData(); |
|
47
|
|
|
|
|
48
|
|
|
/** @var Entry */ |
|
49
|
|
|
$posting = $this->Entries->createPosting($data, $this->CurrentUser); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if (empty($posting)) { |
|
52
|
|
|
throw new BadRequestException(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$errors = $posting->getErrors(); |
|
56
|
|
|
|
|
57
|
|
|
if (!count($errors)) { |
|
58
|
|
|
$this->set(compact('posting')); |
|
59
|
|
|
|
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->set(compact('errors')); |
|
64
|
|
|
$this->viewBuilder()->setTemplate('/Error/json/entityValidation'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Edit an existing posting |
|
69
|
|
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function edit() |
|
73
|
|
|
{ |
|
74
|
|
|
$data = $this->request->getData(); |
|
75
|
|
|
|
|
76
|
|
|
if (empty($data['id'])) { |
|
77
|
|
|
throw new BadRequestException('No posting-id provided.'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$id = $data['id']; |
|
81
|
|
|
$posting = $this->Entries->get($id, ['return' => 'Entity']); |
|
82
|
|
|
if (!$posting) { |
|
83
|
|
|
throw new NotFoundException('Posting not found.'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$updatedPosting = $this->Entries->updatePosting($posting, $data, $this->CurrentUser); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
if (!$updatedPosting) { |
|
89
|
|
|
throw new BadRequestException('Posting could not be saved.'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (!$updatedPosting->hasErrors()) { |
|
93
|
|
|
$this->set('posting', $updatedPosting); |
|
94
|
|
|
$this->render('/Postings/json/add'); |
|
95
|
|
|
|
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$errors = $updatedPosting->getErrors(); |
|
100
|
|
|
$this->set(compact('errors')); |
|
101
|
|
|
$this->viewBuilder()->setTemplate('/Error/json/entityValidation'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Serves meta information required to add or edit a posting |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
public function meta() |
|
110
|
|
|
{ |
|
111
|
|
|
$id = $this->getRequest()->getQuery('id', null); |
|
112
|
|
|
$isEdit = !empty($id); |
|
113
|
|
|
$pid = $this->getRequest()->getQuery('pid', null); |
|
114
|
|
|
$isAnswer = !empty($pid); |
|
115
|
|
|
|
|
116
|
|
View Code Duplication |
if ($isAnswer) { |
|
|
|
|
|
|
117
|
|
|
/** @var PostingInterface */ |
|
118
|
|
|
$parent = $this->Entries->get($pid); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
// Don't leak content of forbidden categories |
|
121
|
|
|
if ($parent->isAnsweringForbidden()) { |
|
122
|
|
|
throw new SaitoForbiddenException( |
|
123
|
|
|
'Access to parent in PostingsController:meta() forbidden.', |
|
124
|
|
|
['CurrentUser' => $this->CurrentUser] |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$this->set('parent', $parent); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
View Code Duplication |
if ($isEdit) { |
|
|
|
|
|
|
132
|
|
|
/** @var PostingInterface */ |
|
133
|
|
|
$posting = $this->Entries->get($id); |
|
|
|
|
|
|
134
|
|
|
if (!$posting->isEditingAllowed()) { |
|
135
|
|
|
throw new SaitoForbiddenException( |
|
136
|
|
|
'Access to posting in PostingsController:meta() forbidden.', |
|
137
|
|
|
['CurrentUser' => $this->CurrentUser] |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
$this->set('posting', $posting); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$settings = Configure::read('Saito.Settings'); |
|
144
|
|
|
|
|
145
|
|
|
$this->set(compact('isAnswer', 'isEdit', 'settings')); |
|
146
|
|
|
|
|
147
|
|
|
$action = $isAnswer ? 'answer' : 'thread'; |
|
148
|
|
|
$categories = $this->CurrentUser->getCategories()->getAll($action, 'list'); |
|
149
|
|
|
$this->set('categories', $categories); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.