Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
BaseController.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  * Base controller for all controllers in application
11  */
12 class BaseController extends CController
13 {
14  private $_assetsPath;
15  private $_clientParams;
16 
17  public $layout = '//layouts/gportal';
18  public $mailLayout = '//layouts/mail';
19 
20  protected function beforeAction($action) {
21  $this->layout = Yii::app()->getModule('settings')->layoutForBackend;
22  return parent::beforeAction($action);
23  }
24  /**
25  * Temporary dummy method
26  *
27  * @return object with configuration info
28  */
29  public function getClient()
30  {
31  // parse domain
32  // get configuration for this domain
33  // return configuration object for domain
34  $this->_clientParams = new stdClass;
35  $this->_clientParams->username = Yii::app()->params['client']['username'];
36  $this->_clientParams->contentPath = Yii::app()->params['storageFolder'] . DIRECTORY_SEPARATOR . $this->_clientParams->username;
37  return $this->_clientParams;
38  }
39 
40  /**
41  * CAssetManager is a Web application component that manages private files
42  * (called assets) and makes them accessible by Web clients.
43  * It achieves this goal by copying assets to a Web-accessible directory
44  * and returns the corresponding URL for accessing them.
45  *
46  * @throws CException
47  * @return string the root directory storing the published asset files. Defaults to 'WebRoot/assets'.
48  */
49  public function getAssetsPath()
50  {
51  if (is_null($this->_assetsPath)) {
52  $assetsPath = ($this->module !== null) ? $this->module->name . '/assets' : Yii::app()->controller->client->contentPath;
53  if (($assetsPath = realpath($assetsPath)) === false || !is_dir($assetsPath) || !is_writable($assetsPath)) {
54  throw new CException(
55  Yii::t('yii', 'CAssetManager.basePath "{path}" is invalid. Please make sure the directory exists and is writable by the Web server process.', array('{path}' => $assetsPath))
56  );
57  }
58  $this->_assetsPath = Yii::app()->assetManager->publish($assetsPath, false, -1, false);
59  }
60  return $this->_assetsPath;
61  }
62 
63  /**
64  * Overridden parent method. Add possibility to load view files from /custom folder
65  *
66  * @param string $viewName name of view
67  *
68  * @return bool|string
69  */
70  public function getViewFile($viewName)
71  {
72  $viewFile = parent::getViewFile($viewName);
73 
74  if (isset(Yii::app()->params['customViews']) && Yii::app()->params['customViews'] == true) {
75  $customSection = realpath(Yii::getPathOfAlias('site.custom'));
76  $commonSection = realpath(Yii::getPathOfAlias('site.common'));
77  $frontendSection = realpath(Yii::getPathOfAlias('site.frontend'));
78  $customView = str_replace(array($commonSection, $frontendSection), $customSection, $viewFile);
79  if (file_exists($customView)) {
80  return $customView;
81  }
82  }
83 
84  return $viewFile;
85  }
86 
87  function getJs($path){
88  echo "<script src=".$path."></script>";
89  }
90 
91  public function renderMail($view, $title = null, $data)
92  {
93  $pathToView = Yii::getPathOfAlias($view);
94  $module = null;
95  preg_match("/\\\\modules\\\\(?P<module_id>\w+)\\\\/", $pathToView, $matches);
96  if(!empty($matches)){
97  $module = $matches['module_id'];
98  $module = Yii::app()->getModule($module);
99  $this->mailLayout = isset($module->mainMailTemplate)?$module->mainMailTemplate:$this->mailLayout;
100  }
101  $output = $this->renderPartial($view, $data, true);
102  if (($layoutFile = $this->getLayoutFile($this->mailLayout)) !== false)
103  $output = $this->renderFile($layoutFile, array('content' => $output, 'title' => $title), true);
104  return $output;
105  }
106 
107  public function redirect($url,$terminate=true,$statusCode=302)
108  {
109  //escaping from CRLF injection
110  $exceptModules = array('user');
111  $moduleId = isset(Yii::app()->controller->module->id)?Yii::app()->controller->module->id:false;
112  if(!$moduleId || ($moduleId && !in_array($moduleId, $exceptModules))){
113  new AppHelper(); //this file contains function http_build_url
114  $url = http_build_url('', parse_url($url));
115  }
116  parent::redirect($url, $terminate, $statusCode);
117  }
118 
119 }