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\NotFoundException; |
21
|
|
|
use Saito\Exception\SaitoForbiddenException; |
22
|
|
|
use Saito\Posting\PostingInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Endpoint for adding/POST and editing/PUT posting |
26
|
|
|
* |
27
|
|
|
* @property EntriesTable $Entries |
28
|
|
|
*/ |
29
|
|
|
class PostingsController extends ApiAppController |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritDoc} |
33
|
|
|
*/ |
34
|
|
|
public function initialize() |
35
|
|
|
{ |
36
|
|
|
parent::initialize(); |
37
|
|
|
$this->loadModel('Entries'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Add a a new posting |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function add(): void |
46
|
|
|
{ |
47
|
|
|
$data = $this->request->getData(); |
48
|
|
|
|
49
|
|
|
/** @var Entry */ |
50
|
|
|
$posting = $this->Entries->createPosting($data, $this->CurrentUser); |
|
|
|
|
51
|
|
|
|
52
|
|
|
if (empty($posting)) { |
53
|
|
|
throw new BadRequestException(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$errors = $posting->getErrors(); |
57
|
|
|
|
58
|
|
|
if (!count($errors)) { |
59
|
|
|
$this->set(compact('posting')); |
60
|
|
|
|
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->set(compact('errors')); |
65
|
|
|
$this->viewBuilder()->setTemplate('/Error/json/entityValidation'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Edit an existing posting |
70
|
|
|
* |
71
|
|
|
* @param string $id Unused in favor of request-data. |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function edit(string $id): void |
75
|
|
|
{ |
76
|
|
|
$data = $this->request->getData(); |
77
|
|
|
|
78
|
|
|
if (empty($data['id'])) { |
79
|
|
|
throw new BadRequestException('No posting-id provided.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$id = $data['id']; |
83
|
|
|
$posting = $this->Entries->get($id, ['return' => 'Entity']); |
84
|
|
|
if (!$posting) { |
85
|
|
|
throw new NotFoundException('Posting not found.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$updatedPosting = $this->Entries->updatePosting($posting, $data, $this->CurrentUser); |
|
|
|
|
89
|
|
|
|
90
|
|
|
if (!$updatedPosting) { |
91
|
|
|
throw new BadRequestException('Posting could not be saved.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (!$updatedPosting->hasErrors()) { |
95
|
|
|
$this->set('posting', $updatedPosting); |
96
|
|
|
$this->render('/Postings/json/add'); |
97
|
|
|
|
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$errors = $updatedPosting->getErrors(); |
102
|
|
|
$this->set(compact('errors')); |
103
|
|
|
$this->viewBuilder()->setTemplate('/Error/json/entityValidation'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Serves meta information required to add or edit a posting |
108
|
|
|
* |
109
|
|
|
* @param string|null $id ID of the posting (send on edit) |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public function meta(?string $id = null): void |
113
|
|
|
{ |
114
|
|
|
$id = (int)$id; |
115
|
|
|
$isEdit = !empty($id); |
116
|
|
|
$pid = $this->getRequest()->getQuery('pid', null); |
117
|
|
|
$isAnswer = !empty($pid); |
118
|
|
|
|
119
|
|
|
if ($isAnswer) { |
120
|
|
|
/** @var PostingInterface */ |
121
|
|
|
$parent = $this->Entries->get($pid); |
|
|
|
|
122
|
|
|
|
123
|
|
|
// Don't leak content of forbidden categories |
124
|
|
|
if ($parent->isAnsweringForbidden()) { |
125
|
|
|
throw new SaitoForbiddenException( |
126
|
|
|
'Access to parent in PostingsController:meta() forbidden.', |
127
|
|
|
['CurrentUser' => $this->CurrentUser] |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->set('parent', $parent); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($isEdit) { |
135
|
|
|
/** @var PostingInterface */ |
136
|
|
|
$posting = $this->Entries->get($id); |
137
|
|
|
if (!$posting->isEditingAllowed()) { |
138
|
|
|
throw new SaitoForbiddenException( |
139
|
|
|
'Access to posting in PostingsController:meta() forbidden.', |
140
|
|
|
['CurrentUser' => $this->CurrentUser] |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
$this->set('posting', $posting); |
144
|
|
|
} else { |
145
|
|
|
/// We currently don't save drafts for edits |
146
|
|
|
$where = ['user_id' => $this->CurrentUser->getId()]; |
147
|
|
|
($pid) ? $where['pid'] = $pid : $where[] = 'pid IS NULL'; |
148
|
|
|
$draft = $this->Entries->Drafts->find()->where($where)->first(); |
149
|
|
|
|
150
|
|
|
if ($draft) { |
151
|
|
|
$this->set('draft', $draft); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$settings = Configure::read('Saito.Settings'); |
156
|
|
|
|
157
|
|
|
$this->set(compact('isAnswer', 'isEdit', 'settings')); |
158
|
|
|
|
159
|
|
|
$action = $isAnswer ? 'answer' : 'thread'; |
160
|
|
|
$categories = $this->CurrentUser->getCategories()->getAll($action, 'list'); |
161
|
|
|
$this->set('categories', $categories); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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.