Controller::getWebrootDirectory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Controller is the customized base controller class.
5
 * All controller classes for this application should extend from this.
6
 * 
7
 * @copyright Copyright &copy; Sam Stenvall 2013-
8
 * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
9
 */
10
class Controller extends CController
11
{
12
13
	/**
14
	 * Initializes the controller. The application name and language is set here.
15
	 */
16
	public function init()
17
	{
18
		// Use the defined request timeout as execution time limit
19
		$requestTimeout = Setting::getInteger('requestTimeout');
20
		set_time_limit($requestTimeout);
21
		
22
		// Fallback to XBMC Video Server if the user has removed the 
23
		// application name
24
		$name = Setting::getString('applicationName');
25
26
		if (!$name)
27
			$name = 'XBMC Video Server';
28
29
		Yii::app()->name = $name;
30
		Yii::app()->language = Yii::app()->languageManager->getCurrent();
31
32
		parent::init();
33
	}
34
	
35
	/**
36
	 * Setter for _pageTitle
37
	 * @param string $pageTitle
38
	 */
39
	public function setPageTitle($pageTitle)
40
	{
41
		$this->pageTitle = $pageTitle.' - '.Yii::app()->name;
42
	}
43
44
	/**
45
	 * @return array the filter definitions for this controller
46
	 */
47
	public function filters()
48
	{
49
		return array(
50
			'requireLogin',
51
			'checkConfiguration',
52
			array('ChangeLanguageFilter'),
53
		);
54
	}
55
56
57
	/**
58
	 * Releases the current session locks to allow for new requests from the same client
59
	 * @param CFilterChain $filterChain
60
	 */
61
	public function filterReleaseSessionLock($filterChain)
62
	{
63
		session_write_close();
64
65
		$filterChain->run();
66
	}
67
68
	/**
69
	 * Checks that someone is logged in and if not redirects to the login page
70
	 * @param CFilterChain $filterChain
71
	 */
72
	public function filterRequireLogin($filterChain)
73
	{
74
		if (Yii::app()->user->isGuest)
75
			$this->redirect(array('site/login'));
76
		else
77
		{
78
			// Check that the logged in user account still exists, if not log 
79
			// out the user immediately
80
			$user = User::model()->findByPk(Yii::app()->user->id);
81
82
			if ($user === null)
83
				$this->redirect(array('site/logout'));
84
		}
85
86
		$filterChain->run();
87
	}
88
89
	/**
90
	 * Checks that the application has been configured, and if not redirects 
91
	 * to the "create backend" page
92
	 * @param CFilterChain $filterChain
93
	 */
94
	public function filterCheckConfiguration($filterChain)
95
	{
96
		if (Yii::app()->backendManager->getCurrent() === null)
97
		{
98
			Yii::app()->user->setFlash('error', Yii::t('Backend', 'You must configure a backend before you can use the application'));
99
100
			$this->redirect(array('backend/create'));
101
		}
102
103
		$filterChain->run();
104
	}
105
	
106
	/**
107
	 * Logs the message using Yii:log(). Before the message is logged it is 
108
	 * run through sprintf(), which means this method takes an unlimited 
109
	 * amount of parameters, e.g. $this->log('This is %s', 'magic');
110
	 */
111
	public function log()
112
	{
113
		$message = call_user_func_array('sprintf', func_get_args());
114
115
		Yii::log($message, CLogger::LEVEL_INFO, get_class($this));
116
	}
117
	
118
	/**
119
	 * Registers the specified custom stylesheet if it exists
120
	 * @param string the stylesheet filename
0 ignored issues
show
Bug introduced by
The type the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
121
	 */
122
	public function registerCustomCss($styleheet)
123
	{
124
		$customCss = $this->getWebrootDirectory('css').DIRECTORY_SEPARATOR.$styleheet;
125
126
		if (is_readable($customCss))
127
		{
128
			$cs = Yii::app()->clientScript;
129
			$cs->registerCssFile(Yii::app()->baseUrl.'/css/'.$styleheet.'?'.filemtime($customCss));
130
		}
131
	}
132
	
133
	/**
134
	 * The absolute path to the specified directory (relative to webroot)
135
	 * @param string $directory the sought directory
136
	 * @return string the absolute path to it
137
	 */
138
	public function getWebrootDirectory($directory)
139
	{
140
		return realpath(Yii::getPathOfAlias('webroot').DIRECTORY_SEPARATOR.$directory);
0 ignored issues
show
Bug introduced by
Are you sure Yii::getPathOfAlias('webroot') of type false|mixed|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
		return realpath(/** @scrutinizer ignore-type */ Yii::getPathOfAlias('webroot').DIRECTORY_SEPARATOR.$directory);
Loading history...
141
	}
142
	
143
	/**
144
	 * @param string $fallback the fallback location, can be anything that can 
145
	 * be passed to CController::redirect()
146
	 * @return string the previous location, or the specified fallback if no 
147
	 * previous location can be determined
148
	 */
149
	public function getPreviousLocation($fallback)
150
	{
151
		$referrer = Yii::app()->request->urlReferrer;
152
153
		return !empty($referrer) ? $referrer : $fallback;
154
	}
155
156
	/**
157
	 * Redirects to the user's URL referrer, or to the specified fallback URL 
158
	 * if no referrer is available. $fallback can be anything that can be 
159
	 * passed to CController::redirect().
160
	 * @see CController::redirect()
161
	 * @param string $fallback the fallback URL
162
	 */
163
	public function redirectToPrevious($fallback)
164
	{
165
		$this->redirect($this->getPreviousLocation($fallback));
166
	}
167
168
}