Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
RendererController.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  * Controller which recieve all requests to Content Repository
11  *
12  * All request for pages which should be rendered(html, php) handled by actionDynamic()
13  * All other requests, for static content (img, doc, pdf, etc.) handled by actionStatic()
14  * Css and js files also handled by actionStatic() if they are not wrapped
15  * in Yii::app()->clientScript->registerCssFile() or Yii::app()->clientScript->registerScriptFile().
16  * If they are wrapped then they will compressed and stored in /assets folder.
17  *
18  * To handle request to specific node from content repository add these lines into routers.php
19  * 'rules' => array(
20 '<path:nodename\/.+\.(php|html)>' => 'contentRepository/dynamic',
21 '<path:nodename\/.+>' => 'contentRepository/static',
22 ),
23  * nodename - name of node(for exaple - gportal)
24  *
25  */
27 {
28  /**
29  * Default action
30  *
31  * @return void
32  */
33  function actionIndex()
34  {
35  $this->redirect(Yii::app()->createUrl(Yii::app()->getModule('user')->homePageUrl));
36  }
37 
38  /**
39  * Action which render all static content: js, css, doc, pdf files etc.
40  *
41  * @throws CHttpException
42  * @throws Exception
43  *
44  * @return void
45  */
46  function actionStatic()
47  {
48  if (Yii::app()->request->getParam('path',false)) {
49  $path = '/' . Yii::app()->request->getParam('path','');
50 
51  $content = $this->_getContentSource()->getContent($path);
52  if ($content) {
53  if ($content->hasAccess(Yii::app()->user)) {
54  $fileContent = file_get_contents($content->getFile());
55  array_map(
56  function($header)
57  {
58  header($header);
59  },
60  array_merge(
61  HeadersHelper::forFile($path, $fileContent), HeadersHelper::cache(3600)
62  )
63  );
64  echo $fileContent;
65  die;
66  } else {
67  $this->_noAccess($content);
68  }
69  } else {
70  if (YII_DEBUG) {
71  throw new Exception('Path not exists: ' . $this->_getContentSource()->contentSource->contentFile($path));
72  } else {
73  throw new CHttpException(404);
74  }
75  }
76  }
77  }
78 
79  /**
80  * Action which render all dynamic content: php, html files
81  *
82  * @throws CHttpException
83  * @throws Exception
84  *
85  * @return void
86  */
87  function actionDynamic()
88  {
89  if (Yii::app()->request->getParam('path',false)) {
90  $path = '/' . Yii::app()->request->getParam('path','');
91 
92  $content = $this->_getContentSource()->getContent($path);
93  if ($content) {
94 
95 
96  if ($content->hasAccess(Yii::app()->user)) {
97  $content = $this->renderFile($content->getFile(), null, true);
98  echo $this->processOutput($content);
99  } else {
100  $this->_noAccess($content);
101  }
102  } else {
103  if (YII_DEBUG) {
104  throw new Exception('Path not exists: ' . $this->_getContentSource()->contentSource->contentFile($path));
105  } else {
106  throw new CHttpException(404);
107  }
108  }
109  }
110  }
111 
112  /**
113  * Handle folder start page. In case user make request to folder like /Content.Node/service/
114  *
115  * @return void
116  * @author Me
117  **/
118  public function actionStartPage()
119  {
120  if($this->module->getContentSource() instanceof FileSystemContentSource) {
121  throw new CHttpException(404);
122  }
123 
124  $folder = Yii::app()->request->getParam('folder', '').'/';
125  $startPage = $this->module->getContentSource()->getStartPage($folder, substr(Yii::app()->locale->id, 0, 2));
126 
127  if($startPage === false) {
128  $startPage = '/' . Yii::app()->request->getParam('path','') . $folder . $this->module->startPageFallback;
129  }
130 
131  if($this->module->startPageDynamic) {
132  $_GET['path'] = substr($startPage,1);
133  $this->actionDynamic();
134  } else {
135  $this->redirect($startPage);
136  }
137  }
138  /**
139  * Sets corresponding messages and performs redirect if user don't have access to requested content
140  *
141  * @param string $path content path
142  *
143  * @return void
144  */
145  private function _noAccess($content)
146  {
147  if (Yii::app()->user->isGuest) {
148  $message = UserModule::t('Please login to access required content.');
149  } else {
150  $message = UserModule::t("You don't have enough rights to access this content. You should have such permissions: {permissions}", array('{permissions}'=> implode(', ', $content->getPersonalisationAttributes())));
151  }
152 
153  Yii::app()->user->setFlash(
154  Yii::app()->getModule('user')->userLoginWidgetFlashError,
155  $message
156  );
157  Yii::app()->user->setReturnUrl($content->getPath());
158  $this->redirect(Yii::app()->getModule('user')->loginUrl);
159  }
160 
161  public function processOutput($output){
162  $processed = parent::processOutput($output);
163  $headPosition = strpos($processed, '<head>');
164  $is_head = false;
165  preg_match("/<head>(?P<head_content>.*)<\/head>/s", $processed, $is_head);
166  if(!empty($is_head)){
167  $is_head = trim($is_head['head_content']);
168  $is_head = strlen($is_head) ? true: false;
169  }else{
170  $is_head = false;
171  }
172  if($headPosition && $is_head){
173  $processed = substr_replace($processed, AppHelper::footprint('<!-- generated by Gentics Portal.Node -->'), $headPosition, 0);
174  }
175  return $processed;
176  }
177 
178  /**
179  * Register css file in page
180  *
181  * @param string $file filename
182  * @param null $timestamp last update timestamp of this file
183  * @param bool $combine if combine and compress this file
184  * @param string $media media that the CSS code should be applied to. If empty, it means all media types.
185  *
186  * @return void
187  */
188  public function getCss($file, $timestamp = null, $combine = true, $media = '')
189  {
190  Yii::app()->clientScript->registerCssFile($file, $media, $timestamp, $combine, true, true);
191  }
192 
193  /**
194  * Register js file in page
195  *
196  * @param string $file filename
197  * @param null $timestamp last update timestamp of this file
198  * @param bool $combine if combine and compress this file
199  *
200  * @return void
201  */
202  public function getJs($file, $timestamp = null, $combine = true)
203  {
204  Yii::app()->clientScript->registerScriptFile($file, CClientScript::POS_HEAD, $timestamp, $combine, true, true);
205  }
206 
207  /**
208  * Register general javascript library which is used by page and widgets and which should be of the same version in many palces.
209  * For example jQuery library
210  *
211  * @param string $file filename
212  *
213  * @return void
214  */
215  public function getCoreJs($file)
216  {
217  Yii::app()->clientScript->registerCoreScript($file, true);
218  }
219 
220  /**
221  * Get current content source for Static or Dynamic content renderer
222  *
223  * @return ContentSourceModule
224  */
225  private function _getContentSource()
226  {
227  return Yii::app()->getModule('contentSource');
228  }
229 }