-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathPage.php
More file actions
614 lines (504 loc) · 20.3 KB
/
Page.php
File metadata and controls
614 lines (504 loc) · 20.3 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
<?php
/**
* Copyright 2014 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
class Page extends SiteTree
{
const PageCustomTitle = 'OpenStack is open source software for creating private and public clouds.';
private static $db = [
'IncludeJquery' => 'Boolean',
'PageJavaScript' => 'Text',
'IncludeShadowBox' => 'Boolean',
'MetaTitle' => 'Varchar(255)',
];
private static $has_one = [
'MetaImage' => 'CloudImage'
];
public function InPast($fieldname)
{
return $this->$fieldname < date('Y-m-d H:i:s');
}
public function InFuture($fieldname)
{
return $this->$fieldname > date('Y-m-d H:i:s');
}
public function TimeRightNow()
{
return date('Y-m-d H:i:s');
}
function getCMSFields()
{
$fields = parent::getCMSFields();
// metadata
$fields->fieldByName('Root.Main.Metadata')->push(UploadField::create("MetaImage",$this->fieldLabel('MetaImage')));
$fields->fieldByName('Root.Main.Metadata')->push(new TextField("MetaTitle",$this->fieldLabel('MetaTitle')));
$dev_tools = '<div class="field text">
<label class="left">Dev Tools: '.$this->AbsoluteLink().'</label>
<div class="middleColumn">
<a href="https://cards-dev.twitter.com/validator" target="_blank">twitter card</a><br>
<a href="https://developers.facebook.com/tools/debug/sharing/?q='.urlencode($this->AbsoluteLink()).'" target="_blank"> facebook card </a>
</div>
</div>';
$fields->fieldByName('Root.Main.Metadata')->push(new LiteralField("DevTools",$dev_tools));
$fields->addFieldToTab('Root.Settings', new TextField ('PageCSS', 'Custom CSS File For This Page (must be in CSS directory)'));
$fields->addFieldToTab('Root.Settings', new CheckboxField ('IncludeJquery', 'Include JQuery In This Page'));
$fields->addFieldToTab('Root.Settings', new CheckboxField ('IncludeShadowBox', 'Include ShadowBox Javascript and CSS'));
$fields->addFieldToTab('Root.Settings', new TextareaField ('PageJavaScript', 'Custom JavaScript For This Page'));
return $fields;
}
public static function SchedShortCodeHandler($arguments, $caption = null, $parser = null)
{
$customise = array();
/*** SET DEFAULTS ***/
$customise['schedule'] = 'http://openstackconferencespring2012.sched.org/';
//overide the defaults with the arguments supplied
$customise = array_merge($customise, $arguments);
//get our Sched template
$template = new SSViewer('Sched');
//return the customized template
return $template->process(new ArrayData($customise));
}
public static function ExternalLinkShortCodeHandler($arguments, $caption = null, $parser = null)
{
// first things first, if we dont have a url, then we don't need to
// go any further
if (empty($arguments['url'])) {
return;
}
$customise = [];
/*** SET DEFAULTS ***/
$customise['category'] = 'Outbound Links';
//if no name is provided as an option, we'll use the URL instead
$customise['name'] = $arguments['url'];
$customise['newwindow'] = FALSE;
$customise['cssclass'] = FALSE;
//set the caption
$customise['caption'] = $caption ? Convert::raw2xml($caption) : false;
//override the defaults with the arguments supplied
$customise = array_merge($customise, $arguments);
//get our ExternalLink template
$template = new SSViewer('ExternalLinkShortCode');
//return the customised template
return $template->process(new ArrayData($customise));
}
public static function IconShortCodeHandler($arguments, $caption = null, $parser = null)
{
$customise = array();
/*** SET DEFAULTS ***/
$customise['type'] = 'fa-check';
//overide the defaults with the arguments supplied
$customise = array_merge($customise, $arguments);
//get our Sched template
$template = new SSViewer('Icon');
//return the customized template
return $template->process(new ArrayData($customise));
}
function requireDefaultRecords()
{
parent::requireDefaultRecords();
// create a 400 ErrorPage
if ($this->class == 'ErrorPage') {
// Ensure that an assets path exists before we do any error page creation
if (!file_exists(ASSETS_PATH)) {
mkdir(ASSETS_PATH);
}
$ErrorPage400 = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '400'");
$ErrorPage400Exists = ($ErrorPage400 && $ErrorPage400->exists()) ? true : false;
$ErrorPage400Path = ErrorPage::get_filepath_for_errorcode(400);
if (!($ErrorPage400Exists && file_exists($ErrorPage400Path))) {
if (!$ErrorPage400Exists) {
$ErrorPage400 = new ErrorPage();
$ErrorPage400->ErrorCode = 400;
$ErrorPage400->Title = _t('ErrorPage.ERRORPAGE400TITLE', '400 Error');
$ErrorPage400->Content = _t(
'ErrorPage.ERRORPAGE400CONTENT',
'<p>An error occurred while processing your request.</p>'
);
$ErrorPage400->Status = 'New page';
$ErrorPage400->write();
$ErrorPage400->publish('Stage', 'Live');
}
// Ensure a static error page is created from latest error page content
$response = Director::test(Director::makeRelative($ErrorPage400->Link()));
if ($fh = fopen($ErrorPage400Path, 'w')) {
$written = fwrite($fh, $response->getBody());
fclose($fh);
}
if ($written) {
DB::alteration_message('400 error page created', 'created');
} else {
DB::alteration_message(sprintf(
'400 error page could not be created at %s. Please check permissions',
$ErrorPage400Path), 'error');
}
}
$ErrorPage412 = DataObject::get_one('ErrorPage', "\"ErrorCode\" = '412'");
$ErrorPage412Exists = ($ErrorPage412 && $ErrorPage412->exists()) ? true : false;
$ErrorPage412Path = ErrorPage::get_filepath_for_errorcode(412);
if (!($ErrorPage412Exists && file_exists($ErrorPage412Path))) {
if (!$ErrorPage412Exists) {
$ErrorPage412 = new ErrorPage();
$ErrorPage412->ErrorCode = 412;
$ErrorPage412->Title = _t('ErrorPage.ERRORPAGE412TITLE', '412 Error');
$ErrorPage412->Content = _t(
'ErrorPage.ERRORPAGE412CONTENT',
'<p>Your Session has expired!.</p>'
);
$ErrorPage412->Status = 'New page';
$ErrorPage412->write();
$ErrorPage412->publish('Stage', 'Live');
}
// Ensure a static error page is created from latest error page content
$response = Director::test(Director::makeRelative($ErrorPage412->Link()));
if ($fh = fopen($ErrorPage412Path, 'w')) {
$written = fwrite($fh, $response->getBody());
fclose($fh);
}
if ($written) {
DB::alteration_message('412 error page created', 'created');
} else {
DB::alteration_message(sprintf(
'412 error page could not be created at %s. Please check permissions',
$ErrorPage412Path), 'error');
}
}
}
}
public function removeExtension($extension){
if($this->hasExtension($extension))
unset($this->extension_instances[$extension]);
}
}
class Page_Controller extends ContentController
{
/**
* An array of actions that can be accessed via a request. Each array element should be an action name, and the
* permissions or conditions required to allow the user to access it.
*
* <code>
* array (
* 'action', // anyone can access this action
* 'action' => true, // same as above
* 'action' => 'ADMIN', // you must have ADMIN permissions to access this action
* 'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
* );
* </code>
*
* @var array
*/
private static $allowed_actions = [
'logout',
'FeedbackForm',
'getNavigationMenu',
'getNavigationFooter',
'dismissUpdateProfileModal',
];
/**
* @var bool
*/
protected $use_jquery_ui;
/**
* @param bool $enable
* @return $this
*/
public function useJqueryUI($enable)
{
$this->use_jquery_ui = $enable;
return $this;
}
public function getTime()
{
return time();
}
public function getSecurityToken()
{
return SecurityToken::inst() ? str_replace('"', '\\"', SecurityToken::inst()->getValue()) : null;
}
public function getIsSSOBootstrapEnabled()
{
$enabled = (bool) (defined('SHELL_SSO_BOOTSTRAP_ENABLED') and SHELL_SSO_BOOTSTRAP_ENABLED);
return $enabled and Member::currentUserID() !== null;
}
public function getMemberIsLoggedIn()
{
return Member::currentUserID() !== null;
}
protected function CustomScripts()
{
$js_files = [
"themes/openstack/javascript/jquery.ticker.js",
"themes/openstack/javascript/jquery.tools.min.js",
"themes/openstack/javascript/jcarousellite.min.js",
"themes/openstack/javascript/navigation.js",
"themes/openstack/javascript/filetracking.jquery.js",
"themes/openstack/javascript/updateProfileModal.js"
];
if($this->use_jquery_ui)
{
JQueryUIDependencies::renderRequirements();
};
$filename = 'themes/openstack/javascript/' . $this->URLSegment . '.js';
if (file_exists(Director::baseFolder() . '/' . $filename)) {
array_push($js_files, $filename);
}
//shadowbox
if (Director::get_current_page()->IncludeShadowBox) {
array_push($js_files, "themes/openstack/javascript/shadowbox/shadowbox.js");
}
foreach($js_files as $js_file)
Requirements::javascript($js_file);
$filename = 'themes/openstack/css/' . $this->URLSegment . '.css';
if (file_exists(Director::baseFolder() . '/' . $filename)) {
Requirements::css($filename);
}
}
protected static function getCssIncludes(){
return [
"themes/openstack/css/combined.css",
"themes/openstack/css/navigation_menu.css",
"themes/openstack/css/dropdown.css",
];
}
public static function AddRequirements()
{
Requirements::css('//fonts.googleapis.com/css?family=Open+Sans:300,400,700');
FontAwesomeDependencies::renderRequirements();
JQueryCoreDependencies::renderRequirements();
BootstrapDependencies::renderRequirements();
$css_files = static::getCssIncludes();
if (Director::get_current_page()->IncludeShadowBox) {
array_push($css_files, "themes/openstack/javascript/shadowbox/shadowbox.css");
}
foreach($css_files as $css_file)
Requirements::css($css_file);
$js_files = [
"node_modules/js-cookie/src/js.cookie.js",
'themes/openstack/javascript/querystring.jquery.js',
'themes/openstack/javascript/shadowbox/shadowbox.js',
];
foreach($js_files as $js_file)
Requirements::javascript($js_file);
}
public function init()
{
parent::init();
// Summit Landing Page Redirects
// Looks to see if ?source is set and redirects to either English or Chinese landing page
// based on the source
if (isset($this->request)) $getVars = $this->request->getVars();
$chineseLangCampaigns = array("o2", "o4", "o6", "o8", "o17", "o18", "o22");
$this->AddRequirements();
$use_shadow_box = Director::get_current_page()->IncludeShadowBox;
$use_shadow_box = empty($use_shadow_box) || intval($use_shadow_box) == 0 ? 'false':'true';
$this->CustomScripts();
//this will be include inline on body after requirements
Requirements::javascriptTemplate('themes/openstack/javascript/page.js', array('UseShadowBox' => $use_shadow_box));
}
public function index(SS_HTTPRequest $request) {
return $this->getViewer('index')->process($this);
}
public function Menu($level) {
$items = parent::Menu($level);
$isDraftPreview = 'Stage' === $this->request->getVar('stage');
if ($isDraftPreview) {
return $items;
}
$now = strtotime('now');
$visible = array();
foreach ($items as $page) {
if ($page->PublishDate && strtotime($page->PublishDate) <= $now) {
$visible[] = $page;
} elseif (!$page->PublishDate) {
$visible[] = $page;
}
}
return new ArrayList($visible);
}
public function LoginForm() {
$back_url = '';
if($this->request->getVar('BackURL')){
$back_url = $this->request->getVar('BackURL');
}
return OpenStackIdFormsFactory::buildLoginForm($this, $back_url);
}
public function DateSortedChildren()
{
$children = $this->Children();
if (!$children)
return null;
$children->sort('EventStartDate', 'DESC');
return $children;
}
// Feedback form in site footer
function FeedbackForm()
{
$FeedbackForm = new feedbackForm($this, 'FeedbackForm');
// Since we are not handling sensitive data with logged in users,
// it's fine to disable the CSFR security token.
$FeedbackForm->enableSecurityToken();
return $FeedbackForm;
}
// Simple methods used to get & set messages that display on the page.
public function setMessage($type, $message)
{
switch (strtolower($type)) {
case 'error':
$type = 'danger';
break;
case 'success':
$type = 'success';
break;
case 'warning':
$type = 'warning';
break;
case 'info':
$type = 'info';
break;
default:
$type = 'success';
break;
}
Session::set('Message', array(
'MessageType' => $type,
'Message' => $message
));
}
public function getMessage()
{
if ($message = Session::get('Message')) {
Session::clear('Message');
$array = new ArrayData($message);
return $array->renderWith('Message');
}
}
public function logout()
{
Security::logout(true);
}
public function CurrentProtocol()
{
return Director::protocol();
}
public function EncodedLink()
{
return urlencode($this->link());
}
public function CurrentUrl()
{
// Manipulate the URL So we can maintain GET Params from the Search Form
$req = Controller::curr()->getRequest(); // get the current http request object
$url = $req->getURL(TRUE); // get the url back but with query intact.
return $url;
}
function getEtag($body)
{
return md5($body);
}
public function validationError($messages)
{
$response = new SS_HTTPResponse();
$response->setStatusCode(412);
$response->addHeader('Content-Type', 'application/json');
if (!is_array($messages))
$messages = array(array('message' => $messages));
$response->setBody(json_encode(
array('error' => 'validation', 'messages' => $messages)
));
return $response;
}
public function serverError()
{
$response = new SS_HTTPResponse();
$response->setStatusCode(500);
$response->addHeader('Content-Type', 'application/json');
$response->setBody(json_encode("Server Error"));
return $response;
}
public function forbiddenError()
{
$response = new SS_HTTPResponse();
$response->setStatusCode(403);
$response->addHeader('Content-Type', 'application/json');
$response->setBody(json_encode("Security Error"));
return $response;
}
protected function ok(array $res = null)
{
$response = new SS_HTTPResponse();
$response->setStatusCode(200);
$response->addHeader('Content-Type', 'application/json');
if (is_null($res)) $res = array();
$response->setBody(json_encode($res));
//conditional get Request (etags)
$request = Controller::curr()->getRequest();
if ($request->isGET()) {
$etag = md5($response->getBody());
$requestETag = $request->getHeader('If-None-Match');
if (!empty($requestETag) && $requestETag == $etag) {
$response->setStatusCode(304);
foreach (array('Allow', 'Content-Encoding', 'Content-Language', 'Content-Length', 'Content-MD5', 'Content-Type', 'Last-Modified') as $header) {
$response->removeHeader($header);
}
$response->setBody(null);
} else
$response->addHeader('ETag', $etag);
}
return $response;
}
// this returns the navigation menu as a jsonp response, it is used to feed this menu to other sites as docs.openstack.org
// the script calling this function is in themes/openstack/javascript/menu-widget.js
public function getNavigationMenu() {
$menu_html = $this->renderWith('Navigation_menu',array('WidgetCall'=>true))->getValue();
$data = array('html'=>$menu_html);
$jsonp = "jsonCallback(".json_encode($data).")";
$response = new SS_HTTPResponse();
$response->setStatusCode(200);
$response->addHeader('Content-Type', 'application/javascript');
$response->setBody($jsonp);
return $response;
}
public function getNavigationFooter(){
$footer_html = $this->renderWith('Navigation_Footer')->getValue();
$data = array('html'=> $footer_html);
$jsonp = "jsonCallback(".json_encode($data).")";
$response = new SS_HTTPResponse();
$response->setStatusCode(200);
$response->addHeader('Content-Type', 'application/javascript');
$response->setBody($jsonp);
return $response;
}
public function MetaTags()
{
$tags = "<meta name=\"title\" content=\"" . Convert::raw2att($this->getPageTitle()) . "\" />".PHP_EOL;
$tags .= parent::MetaTags(false);
return $tags;
}
/**
* @return string
*/
public function getPageTitle()
{
$meta_title = $this->MetaTitle;
if(!empty($meta_title))
return trim($meta_title);
return parent::getTitle(). ' - ' .Page::PageCustomTitle;
}
public function showUpdateProfileModal(){
return (Member::currentUser() && Session::get("Member.showUpdateProfileModal"));
}
public function getApiUrl() {
return OPENSTACK_RESOURCE_API_BASE_URL;
}
public function getCurrentSponsoredProject() {
return OPENSTACK_CURRENT_SPONSORED_PROJECT;
}
}