/
process.php
206 lines (180 loc) · 6.87 KB
/
process.php
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
<?php
namespace Concrete\Controller\Backend\Block;
use Concrete\Core\Area\Area;
use Concrete\Core\Block\Block;
use Concrete\Core\Block\BlockType\BlockType;
use Concrete\Core\Controller\AbstractController;
use Concrete\Core\Error\UserMessageException;
use Concrete\Core\Http\ResponseFactoryInterface;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Stack\Pile\Pile;
use Concrete\Core\Page\Stack\Pile\PileContent;
use Concrete\Core\Page\Stack\Stack;
use Concrete\Core\Permission\Checker;
use Concrete\Core\User\User;
use Concrete\Core\Validation\CSRF\Token;
use Symfony\Component\HttpFoundation\Response as SymphonyResponse;
defined('C5_EXECUTE') or die('Access Denied.');
class Process extends AbstractController
{
/**
* @param int $cID
* @param string $arHandle
* @param int $bID
* @param int $pcID
* @param int|null $dragAreaBlockID
* @param int|null $orphanedBlockID
*
* @throws \Concrete\Core\Error\UserMessageException
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function alias($cID, $arHandle, $pcID, $dragAreaBlockID = null, $orphanedBlockID = null, $stackBlockID = null): SymphonyResponse
{
$isOrphanedBlock = (int) $orphanedBlockID > 0;
$isStackBlock = (int) $stackBlockID > 0;
$token = $this->app->make('token');
if (!$token->validate()) {
throw new UserMessageException($token->getErrorMessage());
}
$c = Page::getByID($cID);
if (!$c || $c->isError()) {
throw new UserMessageException(t('Unable to find the specified page'));
}
$this->request->setCurrentPage($c);
$a = Area::getOrCreate($c, $arHandle);
if ($a->isGlobalArea()) {
$cx = Stack::getByName($arHandle);
$ax = Area::get($c, STACKS_AREA_NAME);
} else {
$cx = $c;
$ax = $a;
}
if ($isStackBlock) {
$b = Block::getByID($stackBlockID);
} else if ($isOrphanedBlock) {
$b = Block::getByID($orphanedBlockID);
} else {
$pc = PileContent::get($pcID);
if (!$pc || $pc->isError() || $pc->getItemType() !== 'BLOCK') {
throw new UserMessageException(t('Unable to find the specified block'));
}
$b = Block::getByID($pc->getItemID());
}
if (!$b) {
throw new UserMessageException(t('Unable to find the specified block'));
}
$bID = $b->getBlockID();
$b->setBlockAreaObject($ax);
$bt = BlockType::getByHandle($b->getBlockTypeHandle());
$ap = new Checker($ax);
if (!$ap->canAddBlock($bt)) {
throw new UserMessageException(t('Access Denied'));
}
$nvc = $cx->getVersionToModify();
if ($a->isGlobalArea()) {
$xvc = $c->getVersionToModify(); // we need to create a new version of THIS page as well.
$xvc->relateVersionEdits($nvc);
}
if ($isOrphanedBlock) {
$nb = $b->duplicate($nvc);
$nb->move($nvc, $ax);
if (!$nb) {
throw new UserMessageException(t('Unable to find the specified block'));
}
$b->delete(true);
} else {
if (!$bt->isCopiedWhenPropagated()) {
$btx = BlockType::getByHandle(BLOCK_HANDLE_SCRAPBOOK_PROXY);
$nb = $nvc->addBlock($btx, $ax, ['bOriginalID' => $bID]);
} else {
$nb = $b->duplicate($nvc, 'duplicate_clipboard');
$nb->move($nvc, $ax);
if (!$nb) {
throw new UserMessageException(t('Unable to find the specified block'));
}
}
}
$nb->refreshCache();
$dragAreaBlockID = (int) $dragAreaBlockID;
if ($dragAreaBlockID !== 0) {
$db = Block::getByID($dragAreaBlockID, $cx, $ax);
} else {
$db = null;
}
$nb->moveBlockToDisplayOrderPosition($db ?: null);
$nb->refreshCache();
return $this->app->make(ResponseFactoryInterface::class)->json([
'aID' => (int) $a->getAreaID(),
'arHandle' => $a->getAreaHandle(),
'cID' => (int) $c->getCollectionID(),
'bID' => (int) $nb->getBlockID(),
]);
}
public function copy(int $cID, string $arHandle, int $bID): SymphonyResponse
{
$valt = $this->app->make(Token::class);
if (!($valt->validate('tools/clipboard/to'))) {
throw new UserMessageException($valt->getErrorMessage());
}
$u = $this->app->make(User::class);
if (!$u->isRegistered()) {
throw new UserMessageException(t('Access Denied.'));
}
$c = Page::getByID($cID);
$cp = new Checker($c);
if (!$cp->canViewPage()) {
throw new UserMessageException(t('Access Denied.'));
}
$a = Area::get($c, $arHandle);
if (!$a || $a->isError()) {
throw new UserMessageException(t('Unable to find the area specified.'));
}
$ap = new Checker($a);
if (!$ap->canViewArea()) {
throw new UserMessageException(t('Access Denied.'));
}
if ($a->isGlobalArea()) {
$ax = STACKS_AREA_NAME;
$cx = Stack::getByName($arHandle);
if (!$cx || $cx->isError()) {
throw new UserMessageException(t('Unable to find the stack specified.'));
}
} else {
$cx = $ax = null;
}
$b = Block::getByID($bID, $cx, $ax);
if ($b && !$b->isError() && $b->getBlockTypeHandle() === BLOCK_HANDLE_SCRAPBOOK_PROXY) {
$bi = $b->getInstance();
$b = Block::getByID($bi->getOriginalBlockID());
}
if (!$b || $b->isError()) {
throw new UserMessageException(t('Unable to find the block specified.'));
}
$p = Pile::getDefault();
$p->add($b);
return $this->app->make(ResponseFactoryInterface::class)->json(true);
}
public function removeFromClipboard($pcID, $cID)
{
$u = $this->app->make(User::class);
if (!$u->isRegistered()) {
throw new UserMessageException(t('Access Denied.'));
}
$valt = $this->app->make(Token::class);
if (!($valt->validate('tools/clipboard/from'))) {
throw new UserMessageException($valt->getErrorMessage());
}
$c = Page::getByID($cID);
$cp = new Checker($c);
if (!$cp->canViewPage()) {
die(t('Access Denied.'));
}
$pileContent = PileContent::get($pcID);
if (!$pileContent || !$pileContent->getPileContentID() || !$pileContent->getPile()->isMyPile()) {
throw new UserMessageException(t('Access Denied.'));
}
$pileContent->delete();
return $this->app->make(ResponseFactoryInterface::class)->json(true);
}
}