Passed
Push — master ( 88741a...6d5838 )
by Aimeos
05:22
created

AbstractController::checkSite()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * @license GPLv3, http://www.gnu.org/copyleft/gpl.html
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2014-2017
7
 * @package TYPO3
8
 */
9
10
namespace Aimeos\Aimeos\Controller;
11
12
13
use Aimeos\Aimeos\Base;
14
15
16
/**
17
 * Abstract class with common functionality for all controllers.
18
 *
19
 * @package TYPO3
20
 */
21
abstract class AbstractController
22
	extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
23
	implements \TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface
24
{
25
	private static $aimeos;
26
	private static $context;
27
	private $contextBE;
28
29
30
	/**
31
	 * Checks if the backend user is allowed to access the site
32
	 *
33
	 * @param array $sitePath List of siteid values
34
	 * @param mixed $userSiteId Site ID stored in the backend user record
35
	 * @return bool True if the user is allowed to access the site
36
	 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException If user doesn't have access
37
	 */
38
	protected function checkSite( array $sitePath, $userSiteId ) : bool
39
	{
40
		foreach( array_reverse( $sitePath ) as $siteid )
41
		{
42
			if( (string) $userSiteId === (string) $siteid ) {
43
				return true;
44
			}
45
		}
46
47
		throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException( 'Access not allowed' );
48
	}
49
50
51
	/**
52
	 * Returns the context item for the frontend
53
	 *
54
	 * @param string $templatePath Path for retrieving the template paths used in the view
55
	 * @param bool $withView True to add view to context object, false for no view
56
	 * @return \Aimeos\MShop\Context\Item\Iface Context item
57
	 */
58
	protected function getContext( string $templatePath = 'client/html/templates',
59
		bool $withView = true ) : \Aimeos\MShop\Context\Item\Iface
60
	{
61
		$config = Base::getConfig( (array) $this->settings );
62
63
		if( !isset( self::$context ) )
64
		{
65
			$context = Base::getContext( $config );
66
			$locale = Base::getLocale( $context, $this->request );
67
			$context->setI18n( Base::getI18n( [$locale->getLanguageId()], $config->get( 'i18n', [] ) ) );
68
			$context->setLocale( $locale );
69
70
			self::$context = $context;
71
		}
72
73
		// Use plugin specific configuration
74
		self::$context->setConfig( $config );
75
76
		foreach( self::$context->getLocale()->getSiteItem()->getConfig() as $key => $value ) {
77
			$config->set( $key, $value );
78
		}
79
80
		if( $withView === true )
81
		{
82
			$langid = self::$context->getLocale()->getLanguageId();
83
			$paths = self::$aimeos->getCustomPaths( $templatePath );
84
			$view = Base::getView( self::$context, $this->uriBuilder, $paths, $this->request, $langid );
85
86
			self::$context->setView( $view );
87
		}
88
89
		return self::$context;
90
	}
91
92
93
	/**
94
	 * Returns the context item for backend operations
95
	 *
96
	 * @param string $templatePath Path for retrieving the template paths used in the view
97
	 * @param bool $withView True to add view to context object, false for no view
98
	 * @return \Aimeos\MShop\Context\Item\Iface Context item
99
	 */
100
	protected function getContextBackend( string $templatePath = 'admin/jqadm/templates',
101
		bool $withView = true ) : \Aimeos\MShop\Context\Item\Iface
102
	{
103
		if( !isset( $this->contextBE ) )
104
		{
105
			$lang = 'en';
106
			$site = 'default';
107
108
			$config = Base::getConfig( (array) $this->settings );
109
			$context = Base::getContext( $config );
110
111
			if( $this->request->hasArgument( 'lang' ) && ( $value = $this->request->getArgument( 'lang' ) ) != '' ) {
112
				$lang = $value;
113
			} elseif( isset( $GLOBALS['BE_USER']->uc['lang'] ) && $GLOBALS['BE_USER']->uc['lang'] != '' ) {
114
				$lang = $GLOBALS['BE_USER']->uc['lang'];
115
			}
116
117
			if( $this->request->hasArgument( 'site' ) && ( $value = $this->request->getArgument( 'site' ) ) != '' ) {
118
				$site = $value;
119
			} elseif( isset( $GLOBALS['BE_USER']->user['siteid'] ) && $GLOBALS['BE_USER']->user['siteid'] != '' ) {
120
				$siteManager = \Aimeos\MShop::create( $context, 'locale/site' );
121
				$siteId = current( array_reverse( explode( '.', trim( $GLOBALS['BE_USER']->user['siteid'], '.' ) ) ) );
122
				$site = ( $siteId ? $siteManager->get( $siteId )->getCode() : 'default' );
123
			}
124
125
			$locale = Base::getLocaleBackend( $context, $site );
126
			$context->setLocale( $locale );
127
128
			if( isset( $GLOBALS['BE_USER']->user['siteid'] ) && $GLOBALS['BE_USER']->user['siteid'] != '' ) {
129
				$this->checkSite( $locale->getSitePath(), $GLOBALS['BE_USER']->user['siteid'] );
130
			}
131
132
			$i18n = Base::getI18n( [$lang, 'en'], $config->get( 'i18n', [] ) );
133
			$context->setI18n( $i18n );
134
135
			foreach( $locale->getSiteItem()->getConfig() as $key => $value ) {
136
				$config->set( $key, $value );
137
			}
138
139
			if( $withView )
140
			{
141
				$paths = self::$aimeos->getCustomPaths( $templatePath );
142
				$view = Base::getView( $context, $this->uriBuilder, $paths, $this->request, $lang, false );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Aimeos\Base::getView() has too many arguments starting with false. ( Ignorable by Annotation )

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

142
				/** @scrutinizer ignore-call */ 
143
    $view = Base::getView( $context, $this->uriBuilder, $paths, $this->request, $lang, false );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
143
				$context->setView( $view );
144
			}
145
146
			$this->contextBE = $context;
147
		}
148
149
		return $this->contextBE;
150
	}
151
152
153
	/**
154
	 * Returns the output of the client and adds the header.
155
	 *
156
	 * @param \Aimeos\Client\Html\Iface $client Html client object (no type hint to prevent reflection)
157
	 * @return string HTML code for inserting into the HTML body
158
	 */
159
	protected function getClientOutput( \Aimeos\Client\Html\Iface $client ) : string
160
	{
161
		$client->setView( $this->getContext()->getView() );
162
		$client->process();
163
164
		$this->response->addAdditionalHeaderData( (string) $client->getHeader() );
165
166
		return $client->getBody();
167
	}
168
169
170
	/**
171
	 * Initializes the object before the real action is called.
172
	 */
173
	protected function initializeAction()
174
	{
175
		$this->uriBuilder->setArgumentPrefix( 'ai' );
176
177
		// initialize bootstrapping
178
		self::$aimeos = Base::getAimeos();
179
	}
180
181
182
	/**
183
	 * Disables Fluid views for performance reasons
184
	 *
185
	 * return null
186
	 */
187
	protected function resolveView()
188
	{
189
		return null;
190
	}
191
}
192