1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The most common kind of controller; effectively a controller linked to a {@link DataObject}. |
4
|
|
|
* |
5
|
|
|
* ContentControllers are most useful in the content-focused areas of a site. This is generally |
6
|
|
|
* the bulk of a site; however, they may be less appropriate in, for example, the user management |
7
|
|
|
* section of an application. |
8
|
|
|
* |
9
|
|
|
* On its own, content controller does very little. Its constructor is passed a {@link DataObject} |
10
|
|
|
* which is stored in $this->dataRecord. Any unrecognised method calls, for example, Title() |
11
|
|
|
* and Content(), will be passed along to the data record, |
12
|
|
|
* |
13
|
|
|
* Subclasses of ContentController are generally instantiated by ModelAsController; this will create |
14
|
|
|
* a controller based on the URLSegment action variable, by looking in the SiteTree table. |
15
|
|
|
* |
16
|
|
|
* @todo Can this be used for anything other than SiteTree controllers? |
17
|
|
|
* |
18
|
|
|
* @package cms |
19
|
|
|
* @subpackage control |
20
|
|
|
*/ |
21
|
|
|
class ContentController extends Controller { |
22
|
|
|
|
23
|
|
|
protected $dataRecord; |
24
|
|
|
|
25
|
|
|
private static $extensions = array('OldPageRedirector'); |
|
|
|
|
26
|
|
|
|
27
|
|
|
private static $allowed_actions = array( |
|
|
|
|
28
|
|
|
'successfullyinstalled', |
29
|
|
|
'deleteinstallfiles', // secured through custom code |
30
|
|
|
'LoginForm' |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The ContentController will take the URLSegment parameter from the URL and use that to look |
35
|
|
|
* up a SiteTree record. |
36
|
|
|
*/ |
37
|
|
|
public function __construct($dataRecord = null) { |
38
|
|
|
if(!$dataRecord) { |
39
|
|
|
$dataRecord = new Page(); |
40
|
|
|
if($this->hasMethod("Title")) $dataRecord->Title = $this->Title(); |
|
|
|
|
41
|
|
|
$dataRecord->URLSegment = get_class($this); |
42
|
|
|
$dataRecord->ID = -1; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->dataRecord = $dataRecord; |
46
|
|
|
$this->failover = $this->dataRecord; |
47
|
|
|
parent::__construct(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return the link to this controller, but force the expanded link to be returned so that form methods and |
52
|
|
|
* similar will function properly. |
53
|
|
|
* |
54
|
|
|
* @param string|null $action Action to link to. |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function Link($action = null) { |
58
|
|
|
return $this->data()->Link(($action ? $action : true)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
//----------------------------------------------------------------------------------// |
62
|
|
|
// These flexible data methods remove the need for custom code to do simple stuff |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return the children of a given page. The parent reference can either be a page link or an ID. |
66
|
|
|
* |
67
|
|
|
* @param string|int $parentRef |
68
|
|
|
* @return SS_List |
69
|
|
|
*/ |
70
|
|
|
public function ChildrenOf($parentRef) { |
71
|
|
|
$parent = SiteTree::get_by_link($parentRef); |
72
|
|
|
|
73
|
|
|
if(!$parent && is_numeric($parentRef)) { |
74
|
|
|
$parent = DataObject::get_by_id('SiteTree', $parentRef); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if($parent) return $parent->Children(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $link |
82
|
|
|
* @return SiteTree |
83
|
|
|
*/ |
84
|
|
|
public function Page($link) { |
85
|
|
|
return SiteTree::get_by_link($link); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function init() { |
89
|
|
|
parent::init(); |
90
|
|
|
|
91
|
|
|
// If we've accessed the homepage as /home/, then we should redirect to /. |
92
|
|
|
if($this->dataRecord && $this->dataRecord instanceof SiteTree |
93
|
|
|
&& RootURLController::should_be_on_root($this->dataRecord) && (!isset($this->urlParams['Action']) || !$this->urlParams['Action'] ) |
94
|
|
|
&& !$_POST && !$_FILES && !$this->redirectedTo() ) { |
95
|
|
|
$getVars = $_GET; |
96
|
|
|
unset($getVars['url']); |
97
|
|
|
if($getVars) $url = "?" . http_build_query($getVars); |
98
|
|
|
else $url = ""; |
99
|
|
|
$this->redirect($url, 301); |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if($this->dataRecord) $this->dataRecord->extend('contentcontrollerInit', $this); |
104
|
|
|
else singleton('SiteTree')->extend('contentcontrollerInit', $this); |
105
|
|
|
|
106
|
|
|
if($this->redirectedTo()) return; |
107
|
|
|
|
108
|
|
|
// Check page permissions |
109
|
|
|
if($this->dataRecord && $this->URLSegment != 'Security' && !$this->dataRecord->canView()) { |
|
|
|
|
110
|
|
|
return Security::permissionFailure($this); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Draft/Archive security check - only CMS users should be able to look at stage/archived content |
114
|
|
|
if( |
115
|
|
|
$this->URLSegment != 'Security' |
|
|
|
|
116
|
|
|
&& !Session::get('unsecuredDraftSite') |
117
|
|
|
&& ( |
118
|
|
|
Versioned::current_archived_date() |
119
|
|
|
|| (Versioned::current_stage() && Versioned::current_stage() != 'Live') |
120
|
|
|
) |
121
|
|
|
) { |
122
|
|
|
if(!$this->dataRecord->canView()) { |
123
|
|
|
Session::clear('currentStage'); |
124
|
|
|
Session::clear('archiveDate'); |
125
|
|
|
|
126
|
|
|
$permissionMessage = sprintf( |
127
|
|
|
_t( |
128
|
|
|
"ContentController.DRAFT_SITE_ACCESS_RESTRICTION", |
129
|
|
|
'You must log in with your CMS password in order to view the draft or archived content. '. |
130
|
|
|
'<a href="%s">Click here to go back to the published site.</a>' |
131
|
|
|
), |
132
|
|
|
Controller::join_links($this->Link(), "?stage=Live") |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
return Security::permissionFailure($this, $permissionMessage); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Use theme from the site config |
141
|
|
|
if(($config = SiteConfig::current_site_config()) && $config->Theme) { |
142
|
|
|
Config::inst()->update('SSViewer', 'theme', $config->Theme); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* This acts the same as {@link Controller::handleRequest()}, but if an action cannot be found this will attempt to |
148
|
|
|
* fall over to a child controller in order to provide functionality for nested URLs. |
149
|
|
|
* |
150
|
|
|
* @param SS_HTTPRequest $request |
151
|
|
|
* @param DataModel $model |
152
|
|
|
* @return SS_HTTPResponse |
153
|
|
|
* @throws SS_HTTPResponse_Exception |
154
|
|
|
*/ |
155
|
|
|
public function handleRequest(SS_HTTPRequest $request, DataModel $model = null) { |
156
|
|
|
$child = null; |
157
|
|
|
$action = $request->param('Action'); |
158
|
|
|
$this->setDataModel($model); |
159
|
|
|
|
160
|
|
|
// If nested URLs are enabled, and there is no action handler for the current request then attempt to pass |
161
|
|
|
// control to a child controller. This allows for the creation of chains of controllers which correspond to a |
162
|
|
|
// nested URL. |
163
|
|
|
if($action && SiteTree::config()->nested_urls && !$this->hasAction($action)) { |
164
|
|
|
// See ModelAdController->getNestedController() for similar logic |
165
|
|
|
if(class_exists('Translatable')) Translatable::disable_locale_filter(); |
166
|
|
|
// look for a page with this URLSegment |
167
|
|
|
$child = $this->model->SiteTree->filter(array( |
168
|
|
|
'ParentID' => $this->ID, |
|
|
|
|
169
|
|
|
'URLSegment' => rawurlencode($action) |
170
|
|
|
))->first(); |
171
|
|
|
if(class_exists('Translatable')) Translatable::enable_locale_filter(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// we found a page with this URLSegment. |
175
|
|
|
if($child) { |
176
|
|
|
$request->shiftAllParams(); |
177
|
|
|
$request->shift(); |
178
|
|
|
|
179
|
|
|
$response = ModelAsController::controller_for($child)->handleRequest($request, $model); |
180
|
|
|
} else { |
181
|
|
|
// If a specific locale is requested, and it doesn't match the page found by URLSegment, |
182
|
|
|
// look for a translation and redirect (see #5001). Only happens on the last child in |
183
|
|
|
// a potentially nested URL chain. |
184
|
|
|
if(class_exists('Translatable')) { |
185
|
|
|
if($request->getVar('locale') && $this->dataRecord && $this->dataRecord->Locale != $request->getVar('locale')) { |
186
|
|
|
$translation = $this->dataRecord->getTranslation($request->getVar('locale')); |
187
|
|
|
if($translation) { |
188
|
|
|
$response = new SS_HTTPResponse(); |
189
|
|
|
$response->redirect($translation->Link(), 301); |
190
|
|
|
throw new SS_HTTPResponse_Exception($response); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
Director::set_current_page($this->data()); |
196
|
|
|
|
197
|
|
|
try { |
198
|
|
|
$response = parent::handleRequest($request, $model); |
|
|
|
|
199
|
|
|
|
200
|
|
|
Director::set_current_page(null); |
|
|
|
|
201
|
|
|
} catch(SS_HTTPResponse_Exception $e) { |
202
|
|
|
$this->popCurrent(); |
203
|
|
|
|
204
|
|
|
Director::set_current_page(null); |
|
|
|
|
205
|
|
|
|
206
|
|
|
throw $e; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $response; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get the project name |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
|
|
public function project() { |
219
|
|
|
global $project; |
220
|
|
|
return $project; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Returns the associated database record |
225
|
|
|
*/ |
226
|
|
|
public function data() { |
227
|
|
|
return $this->dataRecord; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/*--------------------------------------------------------------------------------*/ |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Returns a fixed navigation menu of the given level. |
234
|
|
|
* @param int $level Menu level to return. |
235
|
|
|
* @return ArrayList |
236
|
|
|
*/ |
237
|
|
|
public function getMenu($level = 1) { |
238
|
|
|
if($level == 1) { |
239
|
|
|
$result = SiteTree::get()->filter(array( |
240
|
|
|
"ShowInMenus" => 1, |
241
|
|
|
"ParentID" => 0 |
242
|
|
|
)); |
243
|
|
|
|
244
|
|
|
} else { |
245
|
|
|
$parent = $this->data(); |
246
|
|
|
$stack = array($parent); |
247
|
|
|
|
248
|
|
|
if($parent) { |
249
|
|
|
while($parent = $parent->Parent) { |
250
|
|
|
array_unshift($stack, $parent); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if(isset($stack[$level-2])) $result = $stack[$level-2]->Children(); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$visible = array(); |
258
|
|
|
|
259
|
|
|
// Remove all entries the can not be viewed by the current user |
260
|
|
|
// We might need to create a show in menu permission |
261
|
|
|
if(isset($result)) { |
262
|
|
|
foreach($result as $page) { |
263
|
|
|
if($page->canView()) { |
264
|
|
|
$visible[] = $page; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return new ArrayList($visible); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function Menu($level) { |
273
|
|
|
return $this->getMenu($level); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Returns the default log-in form. |
278
|
|
|
* |
279
|
|
|
* @todo Check if here should be returned just the default log-in form or |
280
|
|
|
* all available log-in forms (also OpenID...) |
281
|
|
|
*/ |
282
|
|
|
public function LoginForm() { |
283
|
|
|
return MemberAuthenticator::get_login_form($this); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function SilverStripeNavigator() { |
287
|
|
|
$member = Member::currentUser(); |
288
|
|
|
$items = ''; |
289
|
|
|
$message = ''; |
290
|
|
|
|
291
|
|
|
if(Director::isDev() || Permission::check('CMS_ACCESS_CMSMain') || Permission::check('VIEW_DRAFT_CONTENT')) { |
292
|
|
|
if($this->dataRecord) { |
293
|
|
|
Requirements::css(CMS_DIR . '/css/SilverStripeNavigator.css'); |
294
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js'); |
295
|
|
|
Requirements::javascript(CMS_DIR . '/javascript/SilverStripeNavigator.js'); |
296
|
|
|
|
297
|
|
|
$return = $nav = SilverStripeNavigator::get_for_record($this->dataRecord); |
|
|
|
|
298
|
|
|
$items = $return['items']; |
299
|
|
|
$message = $return['message']; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
if($member) { |
303
|
|
|
$firstname = Convert::raw2xml($member->FirstName); |
304
|
|
|
$surname = Convert::raw2xml($member->Surname); |
305
|
|
|
$logInMessage = _t('ContentController.LOGGEDINAS', 'Logged in as') ." {$firstname} {$surname} - <a href=\"Security/logout\">". _t('ContentController.LOGOUT', 'Log out'). "</a>"; |
306
|
|
|
} else { |
307
|
|
|
$logInMessage = sprintf( |
308
|
|
|
'%s - <a href="%s">%s</a>' , |
309
|
|
|
_t('ContentController.NOTLOGGEDIN', 'Not logged in') , |
310
|
|
|
Config::inst()->get('Security', 'login_url'), |
311
|
|
|
_t('ContentController.LOGIN', 'Login') ."</a>" |
312
|
|
|
); |
313
|
|
|
} |
314
|
|
|
$viewPageIn = _t('ContentController.VIEWPAGEIN', 'View Page in:'); |
315
|
|
|
|
316
|
|
|
return <<<HTML |
317
|
|
|
<div id="SilverStripeNavigator"> |
318
|
|
|
<div class="holder"> |
319
|
|
|
<div id="logInStatus"> |
320
|
|
|
$logInMessage |
321
|
|
|
</div> |
322
|
|
|
|
323
|
|
|
<div id="switchView" class="bottomTabs"> |
324
|
|
|
$viewPageIn |
325
|
|
|
$items |
326
|
|
|
</div> |
327
|
|
|
</div> |
328
|
|
|
</div> |
329
|
|
|
$message |
330
|
|
|
HTML; |
331
|
|
|
|
332
|
|
|
// On live sites we should still see the archived message |
333
|
|
|
} else { |
334
|
|
|
if($date = Versioned::current_archived_date()) { |
335
|
|
|
Requirements::css(CMS_DIR . '/css/SilverStripeNavigator.css'); |
336
|
|
|
$dateObj = Datetime::create($date, null); |
|
|
|
|
337
|
|
|
// $dateObj->setVal($date); |
338
|
|
|
return "<div id=\"SilverStripeNavigatorMessage\">". _t('ContentController.ARCHIVEDSITEFROM') ."<br>" . $dateObj->Nice() . "</div>"; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function SiteConfig() { |
344
|
|
|
if(method_exists($this->dataRecord, 'getSiteConfig')) { |
345
|
|
|
return $this->dataRecord->getSiteConfig(); |
346
|
|
|
} else { |
347
|
|
|
return SiteConfig::current_site_config(); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Returns an RFC1766 compliant locale string, e.g. 'fr-CA'. |
353
|
|
|
* Inspects the associated {@link dataRecord} for a {@link SiteTree->Locale} value if present, |
354
|
|
|
* and falls back to {@link Translatable::get_current_locale()} or {@link i18n::default_locale()}, |
355
|
|
|
* depending if Translatable is enabled. |
356
|
|
|
* |
357
|
|
|
* Suitable for insertion into lang= and xml:lang= |
358
|
|
|
* attributes in HTML or XHTML output. |
359
|
|
|
* |
360
|
|
|
* @return string |
361
|
|
|
*/ |
362
|
|
|
public function ContentLocale() { |
363
|
|
|
if($this->dataRecord && $this->dataRecord->hasExtension('Translatable')) { |
364
|
|
|
$locale = $this->dataRecord->Locale; |
365
|
|
|
} elseif(class_exists('Translatable') && SiteTree::has_extension('Translatable')) { |
366
|
|
|
$locale = Translatable::get_current_locale(); |
367
|
|
|
} else { |
368
|
|
|
$locale = i18n::get_locale(); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
return i18n::convert_rfc1766($locale); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Return an SSViewer object to render the template for the current page. |
377
|
|
|
* |
378
|
|
|
* @param $action string |
379
|
|
|
* |
380
|
|
|
* @return SSViewer |
381
|
|
|
*/ |
382
|
|
|
public function getViewer($action) { |
383
|
|
|
// Manually set templates should be dealt with by Controller::getViewer() |
384
|
|
|
if(isset($this->templates[$action]) && $this->templates[$action] |
|
|
|
|
385
|
|
|
|| (isset($this->templates['index']) && $this->templates['index']) |
|
|
|
|
386
|
|
|
|| $this->template |
|
|
|
|
387
|
|
|
) { |
388
|
|
|
return parent::getViewer($action); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
// Prepare action for template search |
392
|
|
|
if($action == "index") $action = ""; |
393
|
|
|
else $action = '_' . $action; |
394
|
|
|
|
395
|
|
|
$templates = array_merge( |
396
|
|
|
// Find templates by dataRecord |
397
|
|
|
SSViewer::get_templates_by_class(get_class($this->dataRecord), $action, "SiteTree"), |
398
|
|
|
// Next, we need to add templates for all controllers |
399
|
|
|
SSViewer::get_templates_by_class(get_class($this), $action, "Controller"), |
400
|
|
|
// Fail-over to the same for the "index" action |
401
|
|
|
SSViewer::get_templates_by_class(get_class($this->dataRecord), "", "SiteTree"), |
402
|
|
|
SSViewer::get_templates_by_class(get_class($this), "", "Controller") |
403
|
|
|
); |
404
|
|
|
|
405
|
|
|
return new SSViewer($templates); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* This action is called by the installation system |
411
|
|
|
*/ |
412
|
|
|
public function successfullyinstalled() { |
413
|
|
|
// Return 410 Gone if this site is not actually a fresh installation |
414
|
|
|
if (!file_exists(BASE_PATH . '/install.php')) { |
415
|
|
|
$this->httpError(410); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
// TODO Allow this to work when allow_url_fopen=0 |
419
|
|
|
if(isset($_SESSION['StatsID']) && $_SESSION['StatsID']) { |
420
|
|
|
$url = 'http://ss2stat.silverstripe.com/Installation/installed?ID=' . $_SESSION['StatsID']; |
421
|
|
|
@file_get_contents($url); |
|
|
|
|
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
global $project; |
425
|
|
|
$data = new ArrayData(array( |
426
|
|
|
'Project' => Convert::raw2xml($project), |
427
|
|
|
'Username' => Convert::raw2xml(Session::get('username')), |
428
|
|
|
'Password' => Convert::raw2xml(Session::get('password')), |
429
|
|
|
)); |
430
|
|
|
|
431
|
|
|
return array( |
432
|
|
|
"Title" => _t("ContentController.INSTALL_SUCCESS", "Installation Successful!"), |
433
|
|
|
"Content" => $data->renderWith('Install_successfullyinstalled'), |
434
|
|
|
); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
public function deleteinstallfiles() { |
438
|
|
|
if(!Permission::check("ADMIN")) return Security::permissionFailure($this); |
439
|
|
|
|
440
|
|
|
$title = new Varchar("Title"); |
441
|
|
|
$content = new HTMLText('Content'); |
442
|
|
|
|
443
|
|
|
// We can't delete index.php as it might be necessary for URL routing without mod_rewrite. |
444
|
|
|
// There's no safe way to detect usage of mod_rewrite across webservers, |
445
|
|
|
// so we have to assume the file is required. |
446
|
|
|
$installfiles = array( |
447
|
|
|
'install.php', |
448
|
|
|
'config-form.css', |
449
|
|
|
'config-form.html', |
450
|
|
|
'index.html' |
451
|
|
|
); |
452
|
|
|
|
453
|
|
|
$unsuccessful = new ArrayList(); |
454
|
|
|
foreach($installfiles as $installfile) { |
455
|
|
|
if(file_exists(BASE_PATH . '/' . $installfile)) { |
456
|
|
|
@unlink(BASE_PATH . '/' . $installfile); |
|
|
|
|
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
if(file_exists(BASE_PATH . '/' . $installfile)) { |
460
|
|
|
$unsuccessful->push(new ArrayData(array('File' => $installfile))); |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
$data = new ArrayData(array( |
465
|
|
|
'Username' => Convert::raw2xml(Session::get('username')), |
466
|
|
|
'Password' => Convert::raw2xml(Session::get('password')), |
467
|
|
|
'UnsuccessfulFiles' => $unsuccessful |
468
|
|
|
)); |
469
|
|
|
$content->setValue($data->renderWith('Install_deleteinstallfiles')); |
470
|
|
|
|
471
|
|
|
return array( |
472
|
|
|
"Title" => $title, |
473
|
|
|
"Content" => $content, |
474
|
|
|
); |
475
|
|
|
} |
476
|
|
|
} |
477
|
|
|
|