-
Notifications
You must be signed in to change notification settings - Fork 452
/
PageController.php
294 lines (255 loc) · 9.14 KB
/
PageController.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
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
<?php
namespace Concrete\Core\Page\Controller;
use Concrete\Core\Block\Block;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Foundation\Environment;
use Concrete\Core\Routing\Redirect;
use Page;
use Request;
use Controller;
use Core;
use Concrete\Core\Page\View\PageView;
use Symfony\Component\HttpFoundation\Response;
class PageController extends Controller
{
protected $supportsPageCache = false;
protected $action;
protected $passThruBlocks = array();
protected $parameters = array();
/**
* array of method names that can't be called through the url
* @var array
*/
protected $restrictedMethods = array();
public function supportsPageCache()
{
return $this->supportsPageCache;
}
public function __construct(\Concrete\Core\Page\Page $c)
{
parent::__construct();
$this->c = $c;
$this->view = new PageView($this->c);
$this->set('html', Core::make('\Concrete\Core\Html\Service\Html'));
}
/**
* Given either a path or a Page object, this is a shortcut to
* 1. Grab the controller of THAT page.
* 2. Grab the view of THAT controller
* 3. Render that view.
* 4. Exit – so we immediately stop all other output in the controller that
* called render().
*
* @param @string|\Concrete\Core\Page\Page $var
*/
public function replace($var)
{
if (!($var instanceof \Concrete\Core\Page\Page)) {
$var = \Page::getByPath($var);
}
$request = \Request::getInstance();
$request->setCurrentPage($var);
$controller = $var->getPageController();
$controller->on_start();
$controller->runAction('view');
$controller->on_before_render();
$view = $controller->getViewObject();
print $view->render();
exit;
}
public function getSets()
{
$sets = parent::getSets();
$session = Core::make('session');
if ($session->getFlashBag()->has('page_message')) {
$value = $session->getFlashBag()->get('page_message');
foreach($value as $message) {
$sets[$message[0]] = $message[1];
}
}
return $sets;
}
/**
* Given a path to a single page, this command uses the CURRENT controller and renders
* the contents of the single page within this request. The current controller is not
* replaced, and has already fired (since it is meant to be called from within a view() or
* similar method).
*
* @param @string
*/
public function render($path, $pkgHandle = null)
{
$view = $this->getViewObject();
$env = Environment::get();
$path = trim($path, '/');
$a = $path . '/' . FILENAME_COLLECTION_VIEW;
$b = $path . '.php';
$r = $env->getRecord(DIRNAME_PAGES . '/' . $a);
if ($pkgHandle) {
$view->setPackageHandle($pkgHandle);
}
if ($r->exists()) {
$view->renderSinglePageByFilename($a);
} else {
$view->renderSinglePageByFilename($b);
}
}
public function getPageObject()
{
return $this->c;
}
public function flash($key, $value)
{
$session = Core::make('session');
$session->getFlashBag()->add('page_message', array($key, $value));
}
public function getTheme()
{
if ($this->theme === null) {
$theme = parent::getTheme();
if (!$theme) {
$theme = $this->c->getCollectionThemeObject();
if (is_object($theme)) {
$this->theme = $theme->getThemeHandle();
}
} else {
$this->theme = $theme;
}
}
return $this->theme;
}
public function getRequestAction()
{
return $this->action;
}
public function getRequestActionParameters()
{
return $this->parameters;
}
public function getControllerActionPath()
{
if (isset($this->controllerActionPath)) {
return $this->controllerActionPath;
}
if (is_object($this->view)) {
return $this->view->getViewPath();
}
}
public function setupRequestActionAndParameters(Request $request)
{
$task = substr($request->getPath(), strlen($this->c->getCollectionPath()) + 1);
$task = str_replace('-/', '', $task);
$taskparts = explode('/', $task);
if (isset($taskparts[0]) && $taskparts[0] !== '') {
$method = $taskparts[0];
} elseif (is_object($this->c) && is_callable(array($this, $this->c->getCollectionHandle()))) {
$method = $this->c->getCollectionHandle();
} else {
$method = 'view';
}
$foundTask = false;
$restrictedControllers = array(
'Concrete\Core\Controller\Controller',
'Concrete\Core\Controller\AbstractController',
'Concrete\Core\Page\Controller\PageController'
);
try {
$r = new \ReflectionMethod(get_class($this), $method);
$cl = $r->getDeclaringClass();
if (is_object($cl)) {
if (
!in_array($cl->getName(), $restrictedControllers)
&& strpos($method, 'on_') !== 0
&& strpos($method, '__') !== 0
&& $r->isPublic()
&& !$r->isConstructor()
&& (is_array($this->restrictedMethods) && !in_array($method, $this->restrictedMethods))
) {
$foundTask = true;
}
}
} catch (\Exception $e) {
}
if ($foundTask) {
$this->action = $method;
if (isset($taskparts[1])) {
array_shift($taskparts);
$this->parameters = $taskparts;
}
} else {
$this->action = 'view';
if ($taskparts[0] !== '') {
$this->parameters = $taskparts;
}
}
}
public function isValidControllerTask($action, $parameters = array())
{
$valid = true;
if (!is_callable(array($this, $this->action)) && count($this->parameters) > 0) {
$valid = false;
}
if (is_callable(array($this, $this->action)) && (get_class($this) != '\Concrete\Controller\PageForbidden')) {
// we use reflection to see if the task itself, which now much exist, takes fewer arguments than
// what is specified
$r = new \ReflectionMethod(get_class($this), $this->action);
if ($r->getNumberOfParameters() < count($this->parameters)) {
$valid = false;
}
}
return $valid;
}
/**
* @access private
*
* @param Block $b
* @param BlockController $controller
*/
public function setPassThruBlockController(Block $b, BlockController $controller)
{
$this->passThruBlocks[$b->getBlockID()] = $controller;
}
public function getPassThruBlockController(Block $b)
{
$bID = $b->getBlockID();
return isset($this->passThruBlocks[$bID]) ? $this->passThruBlocks[$bID] : null;
}
public function validateRequest()
{
$valid = true;
if (!$this->isValidControllerTask($this->action, $this->parameters)) {
$valid = false;
// we check the blocks on the page.
$blocks = array_merge($this->getPageObject()->getBlocks(), $this->getPageObject()->getGlobalBlocks());
foreach ($blocks as $b) {
$controller = $b->getController();
list($method, $parameters) = $controller->getPassThruActionAndParameters($this->parameters);
if ($controller->isValidControllerTask($method, $parameters)) {
$controller->on_start();
$response = $controller->runAction($method, $parameters);
if ($response instanceof Response) {
return $response;
}
// old school blocks have already terminated at this point. They are redirecting
// or exiting. But new blocks like topics, etc... can actually rely on their $set
// data persisting and being passed into the view.
// so if we make it down here we have to return true –so that we don't fire a 404.
$valid = true;
// then, we need to save the persisted data that may have been set.
$controller->setPassThruBlockController($this);
}
}
if (!$valid) {
// finally, we check additional page paths.
$paths = $this->getPageObject()->getAdditionalPagePaths();
foreach ($paths as $path) {
if ($path->getPagePath() == $this->request->getPath()) {
// This is an additional page path to a page. We 301 redirect.
return Redirect::page($this->getPageObject(), 301);
}
}
}
}
return $valid;
}
}