1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace neon\cms\controllers; |
4
|
|
|
|
5
|
|
|
use neon\cms\models\CmsPage; |
6
|
|
|
use neon\cms\models\CmsUrl; |
7
|
|
|
use neon\core\web\Controller; |
8
|
|
|
use neon\core\web\Response; |
9
|
|
|
use yii\filters\AccessControl; |
10
|
|
|
use \neon\user\services\apiTokenManager\JwtTokenAuth; |
11
|
|
|
use yii\web\HttpException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The RenderController will draw a page using Cobe. It needs to be both |
15
|
|
|
* non-secure for public pages and secure for private role-based pages |
16
|
|
|
*/ |
17
|
|
|
class RenderController extends Controller |
18
|
|
|
{ |
19
|
|
|
private $_secure = false; |
20
|
|
|
private $_roles = []; |
21
|
|
|
|
22
|
|
|
public function __construct($id, $module, $config = []) |
23
|
|
|
{ |
24
|
|
|
profile_begin('__construct'); |
25
|
|
|
parent::__construct($id, $module, $config); |
26
|
|
|
// determine whether or not the page request needs to be secure |
27
|
|
|
if (neon('user')->routeHasRoles('/'.neon()->getRequest()->getPathInfo(), $roles)) { |
|
|
|
|
28
|
|
|
$this->_secure = true; |
29
|
|
|
$this->_roles = $roles; |
30
|
|
|
} |
31
|
|
|
profile_end('__construct'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function beforeAction($action) |
38
|
|
|
{ |
39
|
|
|
profile_begin('controller-before-action'); |
40
|
|
|
if ($this->_secure) { |
41
|
|
|
// Generate the JWT api token - allows logged in users to have access |
42
|
|
|
// to REST api methods without the need to generate an API token |
43
|
|
|
JwtTokenAuth::createCookie(neon()->request, neon()->response); |
44
|
|
|
} |
45
|
|
|
$return = parent::beforeAction($action); |
46
|
|
|
profile_end('controller-before-action'); |
47
|
|
|
return $return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function behaviors() |
54
|
|
|
{ |
55
|
|
|
$behaviours = parent::behaviors(); |
56
|
|
|
if ($this->_secure) { |
57
|
|
|
$behaviours = array_merge($behaviours, [ |
58
|
|
|
'access' => [ |
59
|
|
|
'class' => AccessControl::class, |
60
|
|
|
'rules' => [ |
61
|
|
|
// allow authenticated users |
62
|
|
|
[ |
63
|
|
|
'allow' => true, |
64
|
|
|
'roles' => $this->_roles |
65
|
|
|
], |
66
|
|
|
// everything else is denied |
67
|
|
|
], |
68
|
|
|
], |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $behaviours; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Render a CMS Page |
77
|
|
|
* @return string |
78
|
|
|
* @throws \yii\web\HttpException |
79
|
|
|
* @param string $nice_id |
80
|
|
|
* @param string $slug - this is necessary for canonical urls or list pages for example blog-post |
81
|
|
|
* nice id is impossible to get the overridden pretty url without extra data - the uuid of the blog post or the |
82
|
|
|
* slug it uses - without this property the canonical function will return the url of the first blog-post |
83
|
|
|
* (collection page) it finds. |
84
|
|
|
*/ |
85
|
|
|
public function actionPage($nice_id, $slug='') |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
\Neon::beginProfile('COBE::RENDER_ACTION', 'cobe'); |
88
|
|
|
$cms = $this->getCms(); |
89
|
|
|
$page = $cms->getPage(); |
90
|
|
|
// check the page with the nice_id exists |
91
|
|
|
if (! $page->setById($nice_id)) { |
92
|
|
|
$this->pageNotFound(); |
93
|
|
|
} |
94
|
|
|
//return neon()->view->injectHtml(''); |
95
|
|
|
// Why? Good question! |
96
|
|
|
if (($theme = neon()->request->get('theme')) && !neon()->user->isGuest) { |
97
|
|
|
// override either default or config settings via get |
98
|
|
|
$cms->setThemeName($theme); |
99
|
|
|
} |
100
|
|
|
// set the page mode |
101
|
|
|
if (isset($_GET['edit']) && !neon()->user->isGuest) { |
102
|
|
|
$page->setEditMode((bool) neon()->request->get('edit')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Don't display draft pages unless in edit mode. |
106
|
|
|
if ($page->isStatusDraft() && ! $page->isInEditMode()) { |
107
|
|
|
$this->pageNotFound(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$render = $page->render(); |
111
|
|
|
if ($render === false) |
112
|
|
|
throw new \yii\web\HttpException(404, 'No template found'); |
113
|
|
|
|
114
|
|
|
\Neon::endProfile('COBE::RENDER_ACTION', 'cobe'); |
115
|
|
|
|
116
|
|
|
return $render; |
117
|
|
|
// to make this work the same as everything else |
118
|
|
|
// this would then enable the option to change the template and template renderer via the template extension |
119
|
|
|
// e.g. .neon or .tpl or .twig etc |
120
|
|
|
// $this->layout = 'layout'; |
121
|
|
|
// $this->setViewPath('@root/themes/default'); |
122
|
|
|
// return $this->render($page->getTemplate(), [ |
123
|
|
|
// 'page' => $page->getPageData() |
124
|
|
|
// ]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Generate an xml sitemap for the website |
129
|
|
|
*/ |
130
|
|
|
public function actionSitemap() |
131
|
|
|
{ |
132
|
|
|
$urls = collect( |
133
|
|
|
CmsUrl::find() |
134
|
|
|
->select('url') |
135
|
|
|
->innerJoin(CmsPage::tableName(), 'cms_page.nice_id = cms_url.nice_id') |
136
|
|
|
->where("status='PUBLISHED'") |
137
|
|
|
->all() |
138
|
|
|
)->reduce(function($carry, $url){ |
139
|
|
|
return $carry . "\t<url>\n\t\t<loc>" . url($url['url'], true) . "</loc>\n\t</url>" . "\n"; |
140
|
|
|
}); |
141
|
|
|
header('Content-Type:application/rss+xml'); |
142
|
|
|
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n"; |
143
|
|
|
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n".$urls.'</urlset>'; |
144
|
|
|
exit; |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set up the page from the routing information |
149
|
|
|
* @return string |
150
|
|
|
* @throws \yii\web\HttpException 404 |
151
|
|
|
*/ |
152
|
|
|
public function actionPageFromRoute() |
153
|
|
|
{ |
154
|
|
|
$request = neon()->request; |
155
|
|
|
$url = $request->get('page'); |
156
|
|
|
if (!$url) |
157
|
|
|
$this->pageNotFound(); |
158
|
|
|
if (($urlEntry = CmsUrl::findByUrl($url))) |
159
|
|
|
return $this->actionPage($urlEntry['nice_id']); |
160
|
|
|
$this->pageNotFound(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return \neon\cms\App |
165
|
|
|
*/ |
166
|
|
|
public function getCms() |
167
|
|
|
{ |
168
|
|
|
return neon('cms'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Throw a page not found exception |
173
|
|
|
* @throws \yii\web\HttpException |
174
|
|
|
*/ |
175
|
|
|
protected function pageNotFound() |
176
|
|
|
{ |
177
|
|
|
throw new \yii\web\HttpException(404, 'No page found'); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|