2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 * Created on Mar 21, 2007
40 * To change the template for this generated file go to
41 * Window - Preferences - PHPeclipse - PHP - Code Templates
43 require_once('include/MVC/Controller/ControllerFactory.php');
44 require_once('include/MVC/View/ViewFactory.php');
46 class SugarApplication
48 var $controller = null;
49 var $headerDisplayed = false;
50 var $default_module = 'Home';
51 var $default_action = 'index';
53 function SugarApplication()
57 * Perform execution of the application. This method is called from index2.php
61 if(!empty($sugar_config['default_module']))
62 $this->default_module = $sugar_config['default_module'];
63 $module = $this->default_module;
64 if(!empty($_REQUEST['module']))$module = $_REQUEST['module'];
65 insert_charset_header();
67 $this->controller = ControllerFactory::getController($module);
68 // if the entry point is defined to not need auth, then don't authenicate
69 if( empty($_REQUEST['entryPoint'])
70 || $this->controller->checkEntryPointRequiresAuth($_REQUEST['entryPoint']) ){
74 $this->controller->preProcess();
75 $this->checkHTTPReferer();
78 SugarThemeRegistry::buildRegistry();
79 $this->loadLanguages();
80 $this->checkDatabaseVersion();
81 $this->loadDisplaySettings();
84 $this->setupResourceManagement($module);
85 $this->controller->execute();
90 * Load the authenticated user. If there is not an authenticated user then redirect to login screen.
93 global $authController, $sugar_config;
94 // Double check the server's unique key is in the session. Make sure this is not an attempt to hijack a session
95 $user_unique_key = (isset($_SESSION['unique_key'])) ? $_SESSION['unique_key'] : '';
96 $server_unique_key = (isset($sugar_config['unique_key'])) ? $sugar_config['unique_key'] : '';
97 $allowed_actions = (!empty($this->controller->allowed_actions)) ? $this->controller->allowed_actions : $allowed_actions = array('Authenticate', 'Login',);
99 if(($user_unique_key != $server_unique_key) && (!in_array($this->controller->action, $allowed_actions)) &&
100 (!isset($_SESSION['login_error'])))
103 $post_login_nav = '';
105 if(!empty($this->controller->module)){
106 $post_login_nav .= '&login_module='.$this->controller->module;
108 if(!empty($this->controller->action)){
109 if(in_array(strtolower($this->controller->action), array('delete')))
110 $post_login_nav .= '&login_action=DetailView';
111 elseif(in_array(strtolower($this->controller->action), array('save')))
112 $post_login_nav .= '&login_action=EditView';
113 elseif(isset($_REQUEST['massupdate'])|| isset($_GET['massupdate']) || isset($_POST['massupdate']))
114 $post_login_nav .= '&login_action=index';
116 $post_login_nav .= '&login_action='.$this->controller->action;
118 if(!empty($this->controller->record)){
119 $post_login_nav .= '&login_record='.$this->controller->record;
122 header('Location: index.php?action=Login&module=Users'.$post_login_nav);
126 $authController = new AuthenticationController((!empty($GLOBALS['sugar_config']['authenticationClass'])? $GLOBALS['sugar_config']['authenticationClass'] : 'SugarAuthenticate'));
127 $GLOBALS['current_user'] = new User();
128 if(isset($_SESSION['authenticated_user_id'])){
129 // set in modules/Users/Authenticate.php
130 if(!$authController->sessionAuthenticate()){
131 // if the object we get back is null for some reason, this will break - like user prefs are corrupted
132 $GLOBALS['log']->fatal('User retrieval for ID: ('.$_SESSION['authenticated_user_id'].') does not exist in database or retrieval failed catastrophically. Calling session_destroy() and sending user to Login page.');
134 SugarApplication::redirect('index.php?action=Login&module=Users');
137 }elseif(!($this->controller->module == 'Users' && in_array($this->controller->action, $allowed_actions))){
139 SugarApplication::redirect('index.php?action=Login&module=Users');
142 $GLOBALS['log']->debug('Current user is: '.$GLOBALS['current_user']->user_name);
145 if(isset($_SESSION['authenticated_user_id'])){
146 $GLOBALS['log']->debug("setting cookie ck_login_id_20 to ".$_SESSION['authenticated_user_id']);
147 self::setCookie('ck_login_id_20', $_SESSION['authenticated_user_id'], time() + 86400 * 90);
149 if(isset($_SESSION['authenticated_user_theme'])){
150 $GLOBALS['log']->debug("setting cookie ck_login_theme_20 to ".$_SESSION['authenticated_user_theme']);
151 self::setCookie('ck_login_theme_20', $_SESSION['authenticated_user_theme'], time() + 86400 * 90);
153 if(isset($_SESSION['authenticated_user_theme_color'])){
154 $GLOBALS['log']->debug("setting cookie ck_login_theme_color_20 to ".$_SESSION['authenticated_user_theme_color']);
155 self::setCookie('ck_login_theme_color_20', $_SESSION['authenticated_user_theme_color'], time() + 86400 * 90);
157 if(isset($_SESSION['authenticated_user_theme_font'])){
158 $GLOBALS['log']->debug("setting cookie ck_login_theme_font_20 to ".$_SESSION['authenticated_user_theme_font']);
159 self::setCookie('ck_login_theme_font_20', $_SESSION['authenticated_user_theme_font'], time() + 86400 * 90);
161 if(isset($_SESSION['authenticated_user_language'])){
162 $GLOBALS['log']->debug("setting cookie ck_login_language_20 to ".$_SESSION['authenticated_user_language']);
163 self::setCookie('ck_login_language_20', $_SESSION['authenticated_user_language'], time() + 86400 * 90);
165 //check if user can access
169 function ACLFilter(){
170 ACLController :: filterModuleList($GLOBALS['moduleList']);
174 * setupResourceManagement
175 * This function initialize the ResourceManager and calls the setup method
176 * on the ResourceManager instance.
179 function setupResourceManagement($module) {
180 require_once('include/resource/ResourceManager.php');
181 $resourceManager = ResourceManager::getInstance();
182 $resourceManager->setup($module);
185 function setupPrint() {
186 $GLOBALS['request_string'] = '';
188 // merge _GET and _POST, but keep the results local
189 // this handles the issues where values come in one way or the other
190 // without affecting the main super globals
191 $merged = array_merge($_GET, $_POST);
192 foreach ($merged as $key => $val)
196 foreach ($val as $k => $v)
198 //If an array, then skip the urlencoding. This should be handled with stringify instead.
202 $GLOBALS['request_string'] .= urlencode($key).'['.$k.']='.urlencode($v).'&';
207 $GLOBALS['request_string'] .= urlencode($key).'='.urlencode($val).'&';
210 $GLOBALS['request_string'] .= 'print=true';
213 function preProcess(){
214 $config = new Administration;
215 $config->retrieveSettings();
216 if(!empty($_SESSION['authenticated_user_id'])){
217 if(isset($_SESSION['hasExpiredPassword']) && $_SESSION['hasExpiredPassword'] == '1'){
218 if( $this->controller->action!= 'Save' && $this->controller->action != 'Logout') {
219 $this->controller->module = 'Users';
220 $this->controller->action = 'ChangePassword';
221 $record = $GLOBALS['current_user']->id;
223 $this->handleOfflineClient();
226 $ut = $GLOBALS['current_user']->getPreference('ut');
228 && $this->controller->action != 'AdminWizard'
229 && $this->controller->action != 'EmailUIAjax'
230 && $this->controller->action != 'Wizard'
231 && $this->controller->action != 'SaveAdminWizard'
232 && $this->controller->action != 'SaveUserWizard'
233 && $this->controller->action != 'SaveTimezone'
234 && $this->controller->action != 'Logout') {
235 $this->controller->module = 'Users';
236 $this->controller->action = 'SetTimezone';
237 $record = $GLOBALS['current_user']->id;
239 if($this->controller->action != 'AdminWizard'
240 && $this->controller->action != 'EmailUIAjax'
241 && $this->controller->action != 'Wizard'
242 && $this->controller->action != 'SaveAdminWizard'
243 && $this->controller->action != 'SaveUserWizard'){
244 $this->handleOfflineClient();
249 $this->handleAccessControl();
252 function handleOfflineClient(){
253 if(isset($GLOBALS['sugar_config']['disc_client']) && $GLOBALS['sugar_config']['disc_client']){
254 if(isset($_REQUEST['action']) && $_REQUEST['action'] != 'SaveTimezone'){
255 if (!file_exists('modules/Sync/file_config.php')){
256 if($_REQUEST['action'] != 'InitialSync' && $_REQUEST['action'] != 'Logout' &&
257 ($_REQUEST['action'] != 'Popup' && $_REQUEST['module'] != 'Sync')){
258 //echo $_REQUEST['action'];
260 $this->controller->module = 'Sync';
261 $this->controller->action = 'InitialSync';
264 require_once ('modules/Sync/file_config.php');
265 if(isset($file_sync_info['is_first_sync']) && $file_sync_info['is_first_sync']){
266 if($_REQUEST['action'] != 'InitialSync' && $_REQUEST['action'] != 'Logout' &&
267 ( $_REQUEST['action'] != 'Popup' && $_REQUEST['module'] != 'Sync')){
268 $this->controller->module = 'Sync';
269 $this->controller->action = 'InitialSync';
274 global $moduleList, $sugar_config, $sync_modules;
275 require_once('modules/Sync/SyncController.php');
276 $GLOBALS['current_user']->is_admin = '0'; //No admins for disc client
281 * Handles everything related to authorization.
283 function handleAccessControl(){
284 if($GLOBALS['current_user']->isDeveloperForAnyModule())
286 if(!empty($_REQUEST['action']) && $_REQUEST['action']=="RetrieveEmail")
288 if(!is_admin($GLOBALS['current_user']) && !empty($GLOBALS['adminOnlyList'][$this->controller->module])
289 && !empty($GLOBALS['adminOnlyList'][$this->controller->module]['all'])
290 && (empty($GLOBALS['adminOnlyList'][$this->controller->module][$this->controller->action]) || $GLOBALS['adminOnlyList'][$this->controller->module][$this->controller->action] != 'allow')) {
291 $this->controller->hasAccess = false;
295 // Bug 20916 - Special case for check ACL access rights for Subpanel QuickCreates
296 if(isset($_POST['action']) && $_POST['action'] == 'SubpanelCreates') {
297 $actual_module = $_POST['target_module'];
298 if(!empty($GLOBALS['modListHeader']) && !in_array($actual_module,$GLOBALS['modListHeader'])) {
299 $this->controller->hasAccess = false;
305 if(!empty($GLOBALS['current_user']) && empty($GLOBALS['modListHeader']))
306 $GLOBALS['modListHeader'] = query_module_access_list($GLOBALS['current_user']);
308 if(in_array($this->controller->module, $GLOBALS['modInvisList']) &&
309 ((in_array('Activities', $GLOBALS['moduleList']) &&
310 in_array('Calendar',$GLOBALS['moduleList'])) &&
311 in_array($this->controller->module, $GLOBALS['modInvisListActivities']))
313 $this->controller->hasAccess = false;
319 * Load only bare minimum of language that can be done before user init and MVC stuff
321 static function preLoadLanguages()
323 if(!empty($_SESSION['authenticated_user_language'])) {
324 $GLOBALS['current_language'] = $_SESSION['authenticated_user_language'];
327 $GLOBALS['current_language'] = $GLOBALS['sugar_config']['default_language'];
329 $GLOBALS['log']->debug('current_language is: '.$GLOBALS['current_language']);
330 //set module and application string arrays based upon selected language
331 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
335 * Load application wide languages as well as module based languages so they are accessible
338 function loadLanguages(){
339 if(!empty($_SESSION['authenticated_user_language'])) {
340 $GLOBALS['current_language'] = $_SESSION['authenticated_user_language'];
343 $GLOBALS['current_language'] = $GLOBALS['sugar_config']['default_language'];
345 $GLOBALS['log']->debug('current_language is: '.$GLOBALS['current_language']);
346 //set module and application string arrays based upon selected language
347 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
348 if(empty($GLOBALS['current_user']->id))$GLOBALS['app_strings']['NTC_WELCOME'] = '';
349 if(!empty($GLOBALS['system_config']->settings['system_name']))$GLOBALS['app_strings']['LBL_BROWSER_TITLE'] = $GLOBALS['system_config']->settings['system_name'];
350 $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
351 $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], $this->controller->module);
354 * checkDatabaseVersion
355 * Check the db version sugar_version.php and compare to what the version is stored in the config table.
356 * Ensure that both are the same.
358 function checkDatabaseVersion($dieOnFailure = true)
360 $row_count = sugar_cache_retrieve('checkDatabaseVersion_row_count');
361 if ( empty($row_count) ) {
362 global $sugar_db_version;
363 $version_query = 'SELECT count(*) as the_count FROM config WHERE category=\'info\' AND name=\'sugar_version\'';
365 if($GLOBALS['db']->dbType == 'oci8'){
367 else if ($GLOBALS['db']->dbType == 'mssql'){
368 $version_query .= " AND CAST(value AS varchar(8000)) = '$sugar_db_version'";
371 $version_query .= " AND value = '$sugar_db_version'";
374 $result = $GLOBALS['db']->query($version_query);
375 $row = $GLOBALS['db']->fetchByAssoc($result, -1, true);
376 $row_count = $row['the_count'];
377 sugar_cache_put('checkDatabaseVersion_row_count', $row_count);
380 if($row_count == 0 && empty($GLOBALS['sugar_config']['disc_client'])){
381 $sugar_version = $GLOBALS['sugar_version'];
383 sugar_die("Sugar CRM $sugar_version Files May Only Be Used With A Sugar CRM $sugar_db_version Database.");
392 * Load the themes/images.
394 function loadDisplaySettings()
398 // load the user's default theme
399 $theme = $GLOBALS['current_user']->getPreference('user_theme');
401 if (is_null($theme)) {
402 $theme = $GLOBALS['sugar_config']['default_theme'];
403 if(!empty($_SESSION['authenticated_user_theme'])){
404 $theme = $_SESSION['authenticated_user_theme'];
406 else if(!empty($_COOKIE['sugar_user_theme'])){
407 $theme = $_COOKIE['sugar_user_theme'];
410 if(isset($_SESSION['authenticated_user_theme']) && $_SESSION['authenticated_user_theme'] != '') {
411 $_SESSION['theme_changed'] = false;
415 if(!is_null($theme) && !headers_sent())
417 setcookie('sugar_user_theme', $theme, time() + 31536000); // expires in a year
420 SugarThemeRegistry::set($theme);
421 require_once('include/utils/layout_utils.php');
422 $GLOBALS['image_path'] = SugarThemeRegistry::current()->getImagePath().'/';
423 if ( defined('TEMPLATE_URL') )
424 $GLOBALS['image_path'] = TEMPLATE_URL . '/'. $GLOBALS['image_path'];
426 if ( isset($GLOBALS['current_user']) ) {
427 $GLOBALS['gridline'] = (int) ($GLOBALS['current_user']->getPreference('gridline') == 'on');
428 $GLOBALS['current_user']->setPreference('user_theme', $theme, 0, 'global');
432 function loadLicense(){
434 global $user_unique_key, $server_unique_key;
435 $user_unique_key = (isset($_SESSION['unique_key'])) ? $_SESSION['unique_key'] : '';
436 $server_unique_key = (isset($sugar_config['unique_key'])) ? $sugar_config['unique_key'] : '';
439 function loadGlobals(){
440 global $currentModule;
441 $currentModule = $this->controller->module;
442 if($this->controller->module == $this->default_module){
443 $_REQUEST['module'] = $this->controller->module;
444 if(empty($_REQUEST['action']))
445 $_REQUEST['action'] = $this->default_action;
450 * Actions that modify data in this controller's instance and thus require referrers
453 protected $modifyActions = array();
455 * Actions that always modify data and thus require referrers
456 * save* and delete* hardcoded as modified
459 private $globalModifyActions = array(
460 'massupdate', 'configuredashlet', 'import', 'importvcardsave', 'inlinefieldsave',
461 'wlsave', 'quicksave'
465 * Modules that modify data and thus require referrers for all actions
467 private $modifyModules = array(
468 'Administration' => true,
469 'UpgradeWizard' => true,
470 'Configurator' => true,
472 'ModuleBuilder' => true,
474 'DCETemplates' => true,
475 'DCEInstances' => true,
476 'DCEActions' => true,
477 'Trackers' => array('trackersettings'),
478 'SugarFavorites' => array('tag'),
479 'Import' => array('last', 'undo'),
482 protected function isModifyAction()
484 $action = strtolower($this->controller->action);
485 if(substr($action, 0, 4) == "save" || substr($action, 0, 6) == "delete") {
488 if(isset($this->modifyModules[$this->controller->module])) {
489 if($this->modifyModules[$this->controller->module] == true) {
492 if(in_array($this->controller->action, $this->modifyModules[$this->controller->module])) {
497 if(in_array($this->controller->action, $this->globalModifyActions)) {
500 if(in_array($this->controller->action, $this->modifyActions)) {
508 * Checks a request to ensure the request is coming from a valid source or it is for one of the white listed actions
510 protected function checkHTTPReferer($dieIfInvalid = true)
512 global $sugar_config;
513 $whiteListActions = (!empty($sugar_config['http_referer']['actions']))?$sugar_config['http_referer']['actions']:array('index', 'ListView', 'DetailView', 'EditView','oauth', 'Authenticate', 'Login', 'SupportPortal');
515 $strong = empty($sugar_config['http_referer']['weak']);
517 // Bug 39691 - Make sure localhost and 127.0.0.1 are always valid HTTP referers
518 $whiteListReferers = array('127.0.0.1','localhost');
519 if(!empty($_SERVER['SERVER_ADDR']))$whiteListReferers[] = $_SERVER['SERVER_ADDR'];
520 if ( !empty($sugar_config['http_referer']['list']) ) {
521 $whiteListReferers = array_merge($whiteListReferers,$sugar_config['http_referer']['list']);
524 if($strong && empty($_SERVER['HTTP_REFERER']) && !in_array($this->controller->action, $whiteListActions) && $this->isModifyAction()) {
525 $http_host = explode(':', $_SERVER['HTTP_HOST']);
527 $whiteListActions[] = $this->controller->action;
528 $whiteListString = "'" . implode("', '", $whiteListActions) . "'";
529 if ( $dieIfInvalid ) {
530 header("Cache-Control: no-cache, must-revalidate");
531 $ss = new Sugar_Smarty;
532 $ss->assign('host', $http_host[0]);
533 $ss->assign('action',$this->controller->action);
534 $ss->assign('whiteListString',$whiteListString);
535 $ss->display('include/MVC/View/tpls/xsrf.tpl');
540 if(!empty($_SERVER['HTTP_REFERER']) && !empty($_SERVER['SERVER_NAME'])){
541 $http_ref = parse_url($_SERVER['HTTP_REFERER']);
542 if($http_ref['host'] !== $_SERVER['SERVER_NAME'] && !in_array($this->controller->action, $whiteListActions) &&
544 (empty($whiteListReferers) || !in_array($http_ref['host'], $whiteListReferers))){
545 if ( $dieIfInvalid ) {
546 header("Cache-Control: no-cache, must-revalidate");
547 $whiteListActions[] = $this->controller->action;
548 $whiteListString = "'" . implode("', '", $whiteListActions) . "'";
550 $ss = new Sugar_Smarty;
551 $ss->assign('host',$http_ref['host']);
552 $ss->assign('action',$this->controller->action);
553 $ss->assign('whiteListString',$whiteListString);
554 $ss->display('include/MVC/View/tpls/xsrf.tpl');
562 function startSession()
564 $sessionIdCookie = isset($_COOKIE['PHPSESSID']) ? $_COOKIE['PHPSESSID'] : null;
565 if(isset($_REQUEST['MSID'])) {
566 session_id($_REQUEST['MSID']);
568 if(isset($_SESSION['user_id']) && isset($_SESSION['seamless_login'])){
569 unset ($_SESSION['seamless_login']);
571 if(isset($_COOKIE['PHPSESSID'])){
572 self::setCookie('PHPSESSID', '', time()-42000, '/');
574 sugar_cleanup(false);
576 exit('Not a valid entry method');
579 if(can_start_session()){
584 if ( isset($_REQUEST['login_module']) && isset($_REQUEST['login_action'])
585 && !($_REQUEST['login_module'] == 'Home' && $_REQUEST['login_action'] == 'index') ) {
586 if ( !is_null($sessionIdCookie) && empty($_SESSION) ) {
587 self::setCookie('loginErrorMessage', 'LBL_SESSION_EXPIRED', time()+30, '/');
593 function endSession(){
597 * Redirect to another URL
600 * @param string $url The URL to redirect to
607 * If the headers have been sent, then we cannot send an additional location header
608 * so we will output a javascript redirect statement.
610 if (headers_sent()) {
611 echo "<script>document.location.href='$url';</script>\n";
613 //@ob_end_clean(); // clear output buffer
614 session_write_close();
615 header( 'HTTP/1.1 301 Moved Permanently' );
616 header( "Location: ". $url );
622 * Redirect to another URL
625 * @param string $url The URL to redirect to
627 public static function appendErrorMessage($error_message)
629 if (empty($_SESSION['user_error_message']) || !is_array($_SESSION['user_error_message'])){
630 $_SESSION['user_error_message'] = array();
632 $_SESSION['user_error_message'][] = $error_message;
635 public static function getErrorMessages()
637 if (isset($_SESSION['user_error_message']) && is_array($_SESSION['user_error_message']) ) {
638 $msgs = $_SESSION['user_error_message'];
639 unset($_SESSION['user_error_message']);
647 * Wrapper for the PHP setcookie() function, to handle cases where headers have
650 public static function setCookie(
660 if ( is_null($domain) )
661 if ( isset($_SERVER["HTTP_HOST"]) )
662 $domain = $_SERVER["HTTP_HOST"];
664 $domain = 'localhost';
667 setcookie($name,$value,$expire,$path,$domain,$secure,$httponly);
669 $_COOKIE[$name] = $value;