wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.Application.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.Application.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once("base_dir.php");
20 
21 require_once(BASE."wcmf/lib/util/class.Message.php");
22 require_once(BASE."wcmf/lib/util/class.SessionData.php");
23 require_once(BASE."wcmf/lib/output/class.LogOutputStrategy.php");
24 require_once(BASE."wcmf/lib/presentation/class.WCMFInifileParser.php");
25 require_once(BASE."wcmf/lib/persistence/class.PersistenceFacade.php");
26 require_once(BASE."wcmf/lib/security/class.AuthUser.php");
27 require_once(BASE."wcmf/lib/presentation/ListboxFunctions.php");
28 require_once(BASE."wcmf/lib/util/class.JSONUtil.php");
29 require_once(BASE."wcmf/lib/util/class.Log.php");
30 
31 /**
32  * Function to be called when an php error should be handled.
33  * Set with set_error_handler.
34  */
35 function globalErrorHandler($errno, $errstr, $errfile, $errline)
36 {
37  switch ($errno)
38  {
39  case E_WARNING:
40  case E_NOTICE:
41  case E_USER_NOTICE:
42  case E_USER_WARNING:
43  Log::warn($errstr." in ".$errfile.":".$errline, 'Application');
44  break;
45  case E_USER_ERROR:
46  WCMFException::throwEx($errstr, $errfile, $errline);
47  break;
48  }
49 }
50 
51 /**
52  * @class Application
53  * @ingroup Presentation
54  * @brief The main application class. Does all the initialization.
55  *
56  * @author ingo herwig <ingo@wemove.com>
57  */
59 {
60  var $_data = null;
61 
62  /**
63  * Returns an instance of the class.
64  * @return A reference to the only instance of the Singleton object
65  */
66  function &getInstance()
67  {
68  static $instance = null;
69 
70  if (!isset($instance)) {
71  $instance = new Application();
72  }
73  return $instance;
74  }
75  /**
76  * Constructor
77  */
78  function Application()
79  {
80  // collect all request data
81  $this->_data = array_merge($_GET, $_POST, $_COOKIE, $_FILES);
82  $json = JSONUtil::decode(file_get_contents('php://input'), true);
83  if (is_array($json))
84  {
85  foreach ($json as $key => $value) {
86  $this->_data[$key] = $value;
87  }
88  }
89  // store data in global variables to make them accessible by error handlers
90  $GLOBALS['data'] = &$this->_data;
91  }
92  /**
93  * Initialize the application.
94  * - Parses and processes the configuration
95  * - Sets global variables
96  * - Initialize the session and other main application classes
97  * - Extracts the application parameters
98  * @param configPath The path where config files reside (as seen from main.php), optional [default: 'include/']
99  * @param mainConfigFile The main configuration file to use, optional [default: 'config.ini']
100  * @param defaultController The controller to call if none is given in request parameters, optional [default: 'LoginController']
101  * @param defaultContext The context to set if none is given in request parameters, optional [default: '']
102  * @param defaultAction The action to perform if none is given in request parameters, optional [default: 'login']
103  * @param defaultResponseFormat The response format if none is given in request parameters, optional [default: MSG_FORMAT_HTML]
104  * @return An associative array with keys 'action', 'context', 'controller', 'data'
105  */
106  function initialize($configPath='include/', $mainConfigFile='config.ini',
107  $defaultController='LoginController', $defaultContext='', $defaultAction='login',
108  $defaultResponseFormat=MSG_FORMAT_HTML)
109  {
110  Application::setupGlobals($configPath, $mainConfigFile);
111  $parser = &WCMFInifileParser::getInstance();
112 
113  // include files from implementation section
114  // NOTE: this must be done before the session is started to avoid incomplete object definitions
115  $objectFactory = &ObjectFactory::getInstance();
116  $implementationFiles = array_values($parser->getSection("implementation"));
117  foreach($implementationFiles as $implementationFile)
118  {
119  $impl = BASE.$objectFactory->getClassfileFromConfig($implementationFile);
120  if (is_file($impl))
121  require_once(BASE.$objectFactory->getClassfileFromConfig($implementationFile));
122  else
123  WCMFException::throwEx("Implementation file ".$implementationFile." not found.", __FILE__, __LINE__);
124  }
125 
126  // initialize session with session id if given
127  $sessionId = Application::getCallParameter('sid', false);
128  if ($sessionId === false) {
129  $sessionId = Application::getCallParameter('PHPSESSID', false);
130  }
131  if ($sessionId !== false) {
132  SessionData::init($sessionId);
133  }
134 
135  // clear errors
136  $session = &SessionData::getInstance();
137  $session->clearErrors();
138 
139  // get controller/context/action triple
140  // (defaults to /LoginController//login in this application)
141  $controller = Application::getCallParameter('controller', $defaultController);
142  $context = Application::getCallParameter('context', $defaultContext);
143  $action = Application::getCallParameter('usr_action', $defaultAction);
144  $readonly = Application::getCallParameter('readonly', false);
145  $requestFormat = Application::getCallParameter('request_format', $defaultResponseFormat);
146  $responseFormat = Application::getCallParameter('response_format', $defaultResponseFormat);
147 
148  // load user configuration
149  $rightsManager = &RightsManager::getInstance();
150  $authUser = &$rightsManager->getAuthUser();
151  if ($authUser && strlen($authUser->getConfig()) > 0)
152  {
153  if (!$parser->parseIniFile($GLOBALS['CONFIG_PATH'].$authUser->getConfig(), true))
154  WCMFException::throwEx($parser->getErrorMsg(), __FILE__, __LINE__);
155  }
156 
157  // prepare PersistenceFacade
158  $persistenceFacade = &PersistenceFacade::getInstance();
159  if ($parser->getValue('logDBActions', 'cms') == 1)
160  $persistenceFacade->enableLogging(new LogOutputStrategy());
161  if ($readonly)
162  $persistenceFacade->setReadOnly(true);
163 
164  // store data in global variables to make them accessible by error handlers
165  $GLOBALS['controller'] = $controller;
166  $GLOBALS['context'] = $context;
167  $GLOBALS['action'] = $action;
168  $GLOBALS['requestFormat'] = $requestFormat;
169  $GLOBALS['responseFormat'] = $responseFormat;
170 
171  // return the parameters needed to process the requested action
172  return array('action' => $action, 'context' => $context, 'controller' => $controller,
173  'data' => &$this->_data, 'requestFormat' => $requestFormat, 'responseFormat' => $responseFormat);
174  }
175  /**
176  * Get a value from the call parameters (GET, POST variables)
177  * @param name The name of the parameter
178  * @param default The value to return, if no value is given
179  * @return The value
180  */
181  function getCallParameter($name, $default)
182  {
183  $value = $default;
184  $application = &Application::getInstance();
185  if (array_key_exists($name, $application->_data))
186  $value = $application->_data[$name];
187  return $value;
188  }
189  /**
190  * Setup global variables. Does nothing related to session, users and persistence.
191  * Use this method if you just want to have all global variables initialized and need access to config values.
192  * @param configPath The path where config files reside (as seen from main.php), maybe null [default: include/]
193  * @param mainConfigFile The main configuration file to use, maybe null [default: config.ini]
194  */
195  function setupGlobals($configPath='include/', $mainConfigFile='config.ini')
196  {
197  // globals
198  $GLOBALS['CONFIG_PATH'] = $configPath;
199  $GLOBALS['CONFIG_EXTENSION'] = "ini";
200  $GLOBALS['MAIN_CONFIG_FILE'] = $mainConfigFile;
201 
202  // get configuration from file
203  $parser = &WCMFInifileParser::getInstance();
204  if (!$parser->parseIniFile($GLOBALS['CONFIG_PATH'].$GLOBALS['MAIN_CONFIG_FILE'], true))
205  WCMFException::throwEx($parser->getErrorMsg(), __FILE__, __LINE__);
206 
207  // message globals
208  $GLOBALS['MESSAGE_LOCALE_DIR'] = $parser->getValue('localeDir', 'cms');
209  $GLOBALS['MESSAGE_LANGUAGE'] = $parser->getValue('language', 'cms');
210 
211  // set locale
212  if ($GLOBALS['MESSAGE_LANGUAGE'] !== false)
213  setlocale(LC_ALL, $GLOBALS['MESSAGE_LANGUAGE']);
214 
215  // we do our own error handling
216  set_error_handler("globalErrorHandler", error_reporting());
217  }
218  /**
219  * Get an unique id for the application based on the installation location.
220  * @return The id
221  */
222  function getId()
223  {
224  return md5($_SERVER['SERVER_ADDR'].__FILE__);
225  }
226 }
227 ?>
warn($message, $category)
Definition: class.Log.php:59
setupGlobals($configPath='include/', $mainConfigFile='config.ini')
throwEx($message, $file='', $line='')
static decode($value, $assoc=false)
This OutputStrategy outputs an object's content to the logger category LogOutputStrategy, loglevel info Used classes must implement the toString() method.
init($sessionID)
initialize($configPath='include/', $mainConfigFile='config.ini', $defaultController='LoginController', $defaultContext='', $defaultAction='login', $defaultResponseFormat=MSG_FORMAT_HTML)
The main application class. Does all the initialization.
globalErrorHandler($errno, $errstr, $errfile, $errline)
$GLOBALS['gJSONData']
const MSG_FORMAT_HTML
getCallParameter($name, $default)