/
MetaModelBackend.php
200 lines (177 loc) · 5.85 KB
/
MetaModelBackend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* The MetaModels extension allows the creation of multiple collections of custom items,
* each with its own unique set of selectable attributes, with attribute extendability.
* The Front-End modules allow you to build powerful listing and filtering of the
* data in each collection.
*
* PHP version 5
* @package MetaModels
* @subpackage Core
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @copyright The MetaModels team.
* @license LGPL.
* @filesource
*/
/**
* This is the MetaModel backend interface.
* It is used in the backend to build the menu, pack all the
*
* @package MetaModels
* @subpackage Core
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
*/
class MetaModelBackend
{
/**
* Returns the proper user object for the current context.
*
* @return BackendUser|FrontendUser|null the BackendUser when TL_MODE == 'BE', the FrontendUser when TL_MODE == 'FE' or null otherwise
*/
protected static function getUser()
{
if(TL_MODE=='BE')
{
return BackendUser::getInstance();
} else if(TL_MODE=='FE')
{
return FrontendUser::getInstance();
}
return null;
}
/**
* This initializes the Contao Singleton object stack as it must be,
* when using singletons within the config.php file of an Extension.
*
* @return bool
*/
protected static function initializeContaoObjectStack()
{
if (!file_exists(TL_ROOT . '/system/config/localconfig.php'))
{
return false;
}
// all of these getInstance calls are neccessary to keep the instance stack intact
// and therefore prevent an Exception in unknown on line 0.
// Hopefully this will get fixed with Contao Reloaded or Contao 3.
require_once(TL_ROOT . '/system/config/localconfig.php');
Config::getInstance();
Environment::getInstance();
Input::getInstance();
self::getUser();
Database::getInstance();
return true;
}
protected static function isDBInitialized()
{
// When coming from install.php or somewhere else when localconfig.php
// has not yet completely been initialized, we will run into an exception here.
try
{
$objDB = Database::getInstance();
return $objDB && $objDB->tableExists('tl_metamodel');
}
catch (Exception $e)
{
return false;
}
}
protected static function authenticateBackendUser()
{
$objUser = self::getUser();
// work around as the TL_PATH constant is set after this routine has been run.
// if this is not in place, BackendUser::authenticate() will redirect us to
// http://domain.tldtl_path/contao/index.php
// if no user is properly logged in (note the missing slash in the middle right after .tld).
// We also have to fix up the "script" parameter, as this one will otherwise try to redirect from
// "contao/index.php" to "/contao/index.php" therefore creating an infinite redirect loop.
$Env = Environment::getInstance();
// issue #66 - contao/install.php is not working anymore. Thanks to Stefan Lindecke (@lindesbs)
if (strpos($Env->request,"install.php") !== false)
{
return;
}
// Fix issue #397 - the security patch rendered our redirect method non working (websitePath can now be null).
$path = constant('TL_PATH') ?: $GLOBALS['TL_CONFIG']['websitePath'];
$Env->base = $Env->url . $path . '/';
$Env->script = preg_replace('/^' . preg_quote($path, '/') . '\/?/i', '', $Env->scriptName);
// Bugfix: If the user is not authenticated, contao will redirect to contao/index.php
// But in this moment the TL_PATH is not defined, so the $this->Environment->request
// generate a url without replacing the basepath(TL_PATH) with an empty string.
if (!defined(TL_PATH))
{
define('TL_PATH', $path);
}
// TODO: double, triple and quadro check that this is really safe context here.
$objUser->authenticate();
// restore initial settings.
$Env->base = null;
$Env->script = null;
}
public function getBackendIcon($strBackendIcon)
{
// determine image to use.
if ($strBackendIcon && file_exists(TL_ROOT . '/' . $strBackendIcon))
{
return MetaModelController::getImage(MetaModelController::urlEncode($strBackendIcon), 16, 16);;
} else {
return 'system/modules/metamodels/html/metamodels.png';
}
}
/**
* Add the child tables to the DCA as operation (if any child tables are present).
*/
public function createDataContainer($strTable)
{
if (self::isDBInitialized())
{
MetaModelDcaBuilder::getInstance()->injectChildTablesIntoDCA($strTable, $GLOBALS['TL_DCA'][$strTable]);
}
}
public static function checkBackendLoad($strClass)
{
if ($strClass == 'Backend')
{
MetaModelDcaBuilder::getInstance()->injectIntoBackendModules();
spl_autoload_unregister(array('MetaModelBackend', 'checkBackendLoad'));
}
return false;
}
protected static function registerLateConfig()
{
// register a autoloader which will transport the config variables and unregister itself when loading class Backend.
spl_autoload_register(array('MetaModelBackend', 'checkBackendLoad'), true, true);
if (version_compare(VERSION, '3.0', '<') && !in_array('__autoload', spl_autoload_functions()))
{
spl_autoload_register('__autoload');
}
}
/**
* Called from config.php in TL_MODE == 'BE' to register everything neccessary for the backend.
*
* @return void
*/
public static function buildBackendMenu()
{
if (!self::initializeContaoObjectStack())
{
return;
}
try
{
if (self::isDBInitialized())
{
// if no backend user authenticated, we will get redirected.
self::authenticateBackendUser();
MetaModelDcaBuilder::getInstance()->injectBackendMenu();
self::registerLateConfig();
}
}
catch (Exception $exc)
{
// Note: do NOT use the logging prvided by class System here as that one logs into the DB
// which is pretty useless as the DB most likely was the one throwing the exception.
log_message('Exception in MetaModelBackend::buildBackendMenu() - ' . $exc->getMessage());
}
}
}