index.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // Copyright (C) 2010-2017 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Required constants :
  20. * - PORTAL_MODULE_ID : Name of the portal instance module
  21. * - PORTAL_ID : Name of the portal instance module design (Configuration)
  22. */
  23. // Silex framework and components
  24. require_once APPROOT . '/lib/silex/vendor/autoload.php';
  25. // iTop application requirements
  26. //require_once __DIR__.'/../../../../approot.inc.php'; // Required by the instanciation module
  27. //require_once APPROOT.'/application/startup.inc.php'; // Required by the instanciation module
  28. require_once APPROOT . '/core/moduledesign.class.inc.php';
  29. require_once APPROOT . '/application/loginwebpage.class.inc.php';
  30. require_once APPROOT . '/sources/autoload.php';
  31. // Portal
  32. // Note: This could be prevented by adding namespaces to composer
  33. require_once __DIR__ . '/../src/providers/urlgeneratorserviceprovider.class.inc.php';
  34. require_once __DIR__ . '/../src/helpers/urlgeneratorhelper.class.inc.php';
  35. require_once __DIR__ . '/../src/providers/contextmanipulatorserviceprovider.class.inc.php';
  36. require_once __DIR__ . '/../src/helpers/contextmanipulatorhelper.class.inc.php';
  37. require_once __DIR__ . '/../src/providers/scopevalidatorserviceprovider.class.inc.php';
  38. require_once __DIR__ . '/../src/helpers/scopevalidatorhelper.class.inc.php';
  39. require_once __DIR__ . '/../src/providers/lifecyclevalidatorserviceprovider.class.inc.php';
  40. require_once __DIR__ . '/../src/helpers/lifecyclevalidatorhelper.class.inc.php';
  41. require_once __DIR__ . '/../src/helpers/securityhelper.class.inc.php';
  42. require_once __DIR__ . '/../src/helpers/applicationhelper.class.inc.php';
  43. use \Silex\Application;
  44. use \Combodo\iTop\Portal\Helper\ApplicationHelper;
  45. // Stacking context tag so it knows we are in the portal
  46. $oContex = new ContextTag('GUI:Portal');
  47. $oContex2 = new ContextTag('Portal:' . PORTAL_MODULE_ID);
  48. // Checking if debug param is on
  49. $bDebug = (isset($_REQUEST['debug']) && ($_REQUEST['debug'] === 'true') );
  50. if($bDebug)
  51. {
  52. $oContexDebug = new ContextTag('debug');
  53. }
  54. // Initializing Silex framework
  55. $oKPI = new ExecutionKPI();
  56. $oApp = new Application();
  57. // Registring optional silex components
  58. $oApp->register(new Combodo\iTop\Portal\Provider\UrlGeneratorServiceProvider());
  59. $oApp->register(new Combodo\iTop\Portal\Provider\ContextManipulatorServiceProvider());
  60. $oApp->register(new Combodo\iTop\Portal\Provider\ScopeValidatorServiceProvider(), array(
  61. 'scope_validator.scopes_path' => utils::GetCachePath(),
  62. 'scope_validator.scopes_filename' => PORTAL_ID . '.scopes.php',
  63. 'scope_validator.instance_name' => PORTAL_ID
  64. ));
  65. $oApp->register(new Combodo\iTop\Portal\Provider\LifecycleValidatorServiceProvider(), array(
  66. 'lifecycle_validator.lifecycle_path' => utils::GetCachePath(),
  67. 'lifecycle_validator.lifecycle_filename' => PORTAL_ID . '.lifecycle.php',
  68. 'lifecycle_validator.instance_name' => PORTAL_ID
  69. ));
  70. $oApp->register(new Silex\Provider\TwigServiceProvider(), array(
  71. 'twig.path' => MODULESROOT,
  72. 'twig.options' => array(
  73. 'cache' => ($bDebug) ? false : utils::GetCachePath() . 'twig/',
  74. )
  75. ));
  76. $oApp->register(new Silex\Provider\HttpFragmentServiceProvider());
  77. $oKPI->ComputeAndReport('Initialization of the Silex application');
  78. $oApp->before(function(Symfony\Component\HttpFoundation\Request $oRequest, Silex\Application $oApp) use ($bDebug){
  79. // Checking user rights and prompt if needed (401 HTTP code returned if XHR request)
  80. $iExitMethod = ($oRequest->isXmlHttpRequest()) ? LoginWebPage::EXIT_RETURN : LoginWebPage::EXIT_PROMPT;
  81. $iLogonRes = LoginWebPage::DoLoginEx(PORTAL_ID, false, $iExitMethod);
  82. if( ($iExitMethod === LoginWebPage::EXIT_RETURN) && ($iLogonRes != 0) )
  83. {
  84. $oApp->abort(401, Dict::S('Portal:ErrorUserLoggedOut'));
  85. }
  86. if (UserRights::GetContactId() == 0)
  87. {
  88. $oApp->abort(500, Dict::S('Portal:ErrorNoContactForThisUser'));
  89. }
  90. // Enable archived data
  91. utils::InitArchiveMode();
  92. // Enabling datalocalizer if needed
  93. if (!defined('DISABLE_DATA_LOCALIZER_PORTAL'))
  94. {
  95. ApplicationContext::SetPluginProperty('QueryLocalizerPlugin', 'language_code', UserRights::GetUserLanguage());
  96. }
  97. // Configuring Silex application
  98. $oApp['debug'] = $bDebug;
  99. $oApp['combodo.current_environment'] = utils::GetCurrentEnvironment();
  100. $oApp['combodo.absolute_url'] = utils::GetAbsoluteUrlAppRoot();
  101. $oApp['combodo.modules.absolute_url'] = utils::GetAbsoluteUrlAppRoot() . 'env-' . utils::GetCurrentEnvironment();
  102. $oApp['combodo.portal.base.absolute_url'] = utils::GetAbsoluteUrlAppRoot() . 'env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/web/';
  103. $oApp['combodo.portal.base.absolute_path'] = MODULESROOT . '/itop-portal-base/portal/web/';
  104. $oApp['combodo.portal.instance.absolute_url'] = utils::GetAbsoluteUrlAppRoot() . 'env-' . utils::GetCurrentEnvironment() . '/' . PORTAL_MODULE_ID . '/';
  105. $oApp['combodo.portal.instance.id'] = PORTAL_MODULE_ID;
  106. $oApp['combodo.portal.instance.conf'] = array();
  107. $oApp['combodo.portal.instance.routes'] = array();
  108. // Registering error/exception handler in order to transform php error to exception
  109. ApplicationHelper::RegisterExceptionHandler($oApp);
  110. // Preparing portal foundations (Can't use Silex autoload through composer as we don't follow PSR conventions -filenames, functions-)
  111. $oKPI = new ExecutionKPI();
  112. ApplicationHelper::LoadControllers();
  113. ApplicationHelper::LoadRouters();
  114. ApplicationHelper::RegisterRoutes($oApp);
  115. ApplicationHelper::LoadBricks();
  116. ApplicationHelper::LoadFormManagers();
  117. ApplicationHelper::RegisterTwigExtensions($oApp['twig']);
  118. $oKPI->ComputeAndReport('Loading portal files (routers, controllers, ...)');
  119. // Loading portal configuration from the module design
  120. $oKPI = new ExecutionKPI();
  121. ApplicationHelper::LoadPortalConfiguration($oApp);
  122. $oKPI->ComputeAndReport('Parsing portal configuration');
  123. // Loading current user
  124. ApplicationHelper::LoadCurrentUser($oApp);
  125. // Checking that user is allowed this portal
  126. $bAllowed = false;
  127. foreach($oApp['combodo.portal.instance.conf']['portals'] as $aAllowedPortal)
  128. {
  129. if($aAllowedPortal['id'] === PORTAL_ID)
  130. {
  131. $bAllowed = true;
  132. break;
  133. }
  134. }
  135. if(!$bAllowed)
  136. {
  137. $oApp->abort(404);
  138. }
  139. }, Application::EARLY_EVENT);
  140. // Running application
  141. $oKPI = new ExecutionKPI();
  142. $oApp->run();
  143. $oKPI->ComputeAndReport('Page execution and rendering');
  144. // Logging trace and stats
  145. DBSearch::RecordQueryTrace();
  146. ExecutionKPI::ReportStats();