global.inc.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php
  2. /** For licensing terms, see /license.txt */
  3. /**
  4. * This is a bootstrap file that loads all Chamilo dependencies including:
  5. *
  6. * - Chamilo settings in main/inc/configuration.php or main/inc/configuration.yml
  7. * - mysql database (Using Doctrine DBAL/ORM or the Classic way: Database::query())
  8. * - Templates (Using Twig)
  9. * - Loading language files (No Symfony component)
  10. * - Loading mail settings (SwiftMailer smtp/sendmail/mail)
  11. * - Debug (Using Monolog)
  12. *
  13. * ALL Chamilo scripts must include this file in order to have the $app container
  14. * This script returns a $app Application instance so you have access to all the services.
  15. *
  16. * @package chamilo.include
  17. *
  18. */
  19. // Fix bug in IIS that doesn't fill the $_SERVER['REQUEST_URI'].
  20. // @todo not sure if we need this
  21. // api_request_uri();
  22. // This is for compatibility with MAC computers.
  23. //ini_set('auto_detect_line_endings', '1');
  24. // Composer auto loader.
  25. require_once __DIR__.'../../../vendor/autoload.php';
  26. use Silex\Application;
  27. use \ChamiloSession as Session;
  28. use Symfony\Component\HttpFoundation\RedirectResponse;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\HttpFoundation\Request;
  31. use Symfony\Component\Yaml\Parser;
  32. // Determine the directory path for this file.
  33. $includePath = dirname(__FILE__);
  34. // Start Silex.
  35. $app = new Application();
  36. // @todo add a helper to read the configuration file once!
  37. // Include the main Chamilo platform configuration file.
  38. // @todo use a service provider to load configuration files:
  39. /*
  40. $app->register(new Igorw\Silex\ConfigServiceProvider($settingsFile));
  41. */
  42. /** Reading configuration files */
  43. // Reading configuration file from main/inc/conf/configuration.php or app/config/configuration.yml
  44. $configurationFilePath = $includePath.'/conf/configuration.php';
  45. $configurationYMLFile = $includePath.'/../../config/configuration.yml';
  46. $configurationFileAppPath = $includePath.'/../../config/configuration.php';
  47. $alreadyInstalled = false;
  48. if (file_exists($configurationFilePath) || file_exists($configurationYMLFile) || file_exists($configurationFileAppPath)) {
  49. if (file_exists($configurationFilePath)) {
  50. require_once $configurationFilePath;
  51. }
  52. if (file_exists($configurationFileAppPath)) {
  53. $configurationFilePath = $configurationFileAppPath;
  54. require_once $configurationFileAppPath;
  55. }
  56. $alreadyInstalled = true;
  57. } else {
  58. $_configuration = array();
  59. }
  60. // Overwriting $_configuration
  61. if (file_exists($configurationYMLFile)) {
  62. $yaml = new Parser();
  63. $configurationYML = $yaml->parse(file_get_contents($configurationYMLFile));
  64. if (is_array($configurationYML) && !empty($configurationYML)) {
  65. if (isset($_configuration)) {
  66. $_configuration = array_merge($_configuration, $configurationYML);
  67. } else {
  68. $_configuration = $configurationYML;
  69. }
  70. }
  71. }
  72. /** Setting Chamilo paths */
  73. $app['root_sys'] = isset($_configuration['root_sys']) ? $_configuration['root_sys'] : dirname(dirname(__DIR__)).'/';
  74. $app['sys_data_path'] = isset($_configuration['sys_data_path']) ? $_configuration['sys_data_path'] : $app['root_sys'].'data/';
  75. $app['sys_config_path'] = isset($_configuration['sys_config_path']) ? $_configuration['sys_config_path'] : $app['root_sys'].'config/';
  76. $app['sys_temp_path'] = isset($_configuration['sys_temp_path']) ? $_configuration['sys_temp_path'] : $app['root_sys'].'temp/';
  77. $app['sys_log_path'] = isset($_configuration['sys_log_path']) ? $_configuration['sys_log_path'] : $app['root_sys'].'logs/';
  78. /** Loading config files (mail, auth, profile) */
  79. if ($alreadyInstalled) {
  80. $configPath = $app['sys_config_path'];
  81. $confFiles = array(
  82. 'auth.conf.php',
  83. 'events.conf.php',
  84. 'mail.conf.php',
  85. 'portfolio.conf.php',
  86. 'profile.conf.php'
  87. );
  88. foreach ($confFiles as $confFile) {
  89. if (file_exists($configPath.$confFile)) {
  90. require_once $configPath.$confFile;
  91. }
  92. }
  93. // Fixing $_configuration array
  94. // Fixes bug in Chamilo 1.8.7.1 array was not set
  95. $administrator['email'] = isset($administrator['email']) ? $administrator['email'] : 'admin@example.com';
  96. $administrator['name'] = isset($administrator['name']) ? $administrator['name'] : 'Admin';
  97. // Code for transitional purposes, it can be removed right before the 1.8.7 release.
  98. /*if (empty($_configuration['system_version'])) {
  99. $_configuration['system_version'] = (!empty($_configuration['dokeos_version']) ? $_configuration['dokeos_version'] : '');
  100. $_configuration['system_stable'] = (!empty($_configuration['dokeos_stable']) ? $_configuration['dokeos_stable'] : '');
  101. $_configuration['software_url'] = 'http://www.chamilo.org/';
  102. }*/
  103. // For backward compatibility.
  104. $_configuration['dokeos_version'] = $_configuration['system_version'];
  105. //$_configuration['dokeos_stable'] = $_configuration['system_stable'];
  106. $userPasswordCrypted = (!empty($_configuration['password_encryption']) ? $_configuration['password_encryption'] : 'sha1');
  107. }
  108. /** End loading config files */
  109. /** Including legacy libs */
  110. require_once $includePath.'/lib/main_api.lib.php';
  111. // Setting $_configuration['url_append']
  112. $urlInfo = isset($_configuration['root_web']) ? parse_url($_configuration['root_web']) : null;
  113. $_configuration['url_append'] = null;
  114. if (isset($urlInfo['path'])) {
  115. $_configuration['url_append'] = '/'.basename($urlInfo['path']);
  116. }
  117. $libPath = $includePath.'/lib/';
  118. $langPath = api_get_path(SYS_LANG_PATH);
  119. // Database constants
  120. require_once $libPath.'database.constants.inc.php';
  121. // @todo Rewrite the events.lib.inc.php in a class
  122. require_once $libPath.'events.lib.inc.php';
  123. // Load allowed tag definitions for kses and/or HTMLPurifier.
  124. require_once $libPath.'formvalidator/Rule/allowed_tags.inc.php';
  125. // Ensure that _configuration is in the global scope before loading
  126. // main_api.lib.php. This is particularly helpful for unit tests
  127. // @todo do not use $GLOBALS
  128. /*if (!isset($GLOBALS['_configuration'])) {
  129. $GLOBALS['_configuration'] = $_configuration;
  130. }*/
  131. // Add the path to the pear packages to the include path
  132. ini_set('include_path', api_create_include_path_setting());
  133. $app['configuration_file'] = $configurationFilePath;
  134. $app['configuration_yml_file'] = $configurationYMLFile;
  135. $app['languages_file'] = array();
  136. $app['installed'] = $alreadyInstalled;
  137. $app['app.theme'] = 'chamilo';
  138. // Developer options relies in the configuration.php file
  139. $app['debug'] = isset($_configuration['debug']) ? $_configuration['debug'] : false;
  140. $app['show_profiler'] = isset($_configuration['show_profiler']) ? $_configuration['show_profiler'] : false;
  141. // Enables assetic in order to load 1 compressed stylesheet or split files
  142. $app['assetic.enabled'] = false;
  143. // Dumps assets
  144. $app['assetic.auto_dump_assets'] = false;
  145. // Loading $app settings depending of the debug option
  146. if ($app['debug']) {
  147. require_once __DIR__.'/../../src/ChamiloLMS/Resources/config/dev.php';
  148. } else {
  149. require_once __DIR__.'/../../src/ChamiloLMS/Resources/config/prod.php';
  150. }
  151. // Classic way of render pages or the Controller approach
  152. $app['classic_layout'] = false;
  153. $app['full_width'] = false;
  154. $app['breadcrumb'] = array();
  155. // The script is allowed? This setting is modified when calling api_is_not_allowed()
  156. $app['allowed'] = true;
  157. // Start session after the internationalization library has been initialized
  158. // @todo use silex session provider instead of a custom class
  159. //Chamilo::session()->start($alreadyInstalled);
  160. $app->register(new Silex\Provider\SessionServiceProvider());
  161. // Session settings
  162. $app['session.storage.options'] = array(
  163. 'name' => 'chamilo_session',
  164. //'cookie_lifetime' => 30, //Cookie lifetime
  165. //'cookie_path' => null, //Cookie path
  166. //'cookie_domain' => null, //Cookie domain
  167. //'cookie_secure' => null, //Cookie secure (HTTPS)
  168. 'cookie_httponly' => true //Whether the cookie is http only
  169. );
  170. // Loading chamilo settings
  171. /* @todo create a service provider to load plugins.
  172. Check how bolt add extensions (including twig templates, config with yml)*/
  173. // Template settings loaded in template.lib.php
  174. $app['template.show_header'] = true;
  175. $app['template.show_footer'] = true;
  176. $app['template.show_learnpath'] = false;
  177. $app['template.hide_global_chat'] = true;
  178. $app['template.load_plugins'] = true;
  179. $app['configuration'] = $_configuration;
  180. /** Including service providers */
  181. require_once 'services.php';
  182. $_plugins = array();
  183. if ($alreadyInstalled) {
  184. // Setting the static database class
  185. $database = $app['database'];
  186. // Retrieving all the chamilo config settings for multiple URLs feature
  187. $_configuration['access_url'] = 1;
  188. if (api_get_multiple_access_url()) {
  189. $access_urls = api_get_access_urls();
  190. $protocol = ((!empty($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS']) != 'OFF') ? 'https' : 'http').'://';
  191. $request_url1 = $protocol.$_SERVER['SERVER_NAME'].'/';
  192. $request_url2 = $protocol.$_SERVER['HTTP_HOST'].'/';
  193. foreach ($access_urls as & $details) {
  194. if ($request_url1 == $details['url'] or $request_url2 == $details['url']) {
  195. $_configuration['access_url'] = $details['id'];
  196. }
  197. }
  198. Session::write('url_id', $_configuration['access_url']);
  199. Session::write('url_info', api_get_current_access_url_info($_configuration['access_url']));
  200. } else {
  201. Session::write('url_id', 1);
  202. }
  203. $settings_refresh_info = api_get_settings_params_simple(array('variable = ?' => 'settings_latest_update'));
  204. $settings_latest_update = $settings_refresh_info ? $settings_refresh_info['selected_value'] : null;
  205. $_setting = Session::read('_setting');
  206. if (empty($_setting)) {
  207. api_set_settings_and_plugins();
  208. } else {
  209. if (isset($_setting['settings_latest_update']) && $_setting['settings_latest_update'] != $settings_latest_update) {
  210. api_set_settings_and_plugins();
  211. }
  212. }
  213. $_setting = Session::read('_setting');
  214. $_plugins = Session::read('_plugins');
  215. // Default template style
  216. $templateStyle = api_get_setting('template');
  217. $templateStyle = isset($templateStyle) && !empty($templateStyle) ? $templateStyle : 'default';
  218. $app['template_style'] = $templateStyle;
  219. // Default layout
  220. $app['default_layout'] = $app['template_style'].'/layout/layout_1_col.tpl';
  221. $app['plugins'] = $_plugins;
  222. }
  223. $charset = 'UTF-8';
  224. // Manage Chamilo error messages
  225. $app->error(
  226. function (\Exception $e, $code) use ($app) {
  227. if ($app['debug']) {
  228. //return;
  229. }
  230. if (isset($code)) {
  231. switch ($code) {
  232. case 401:
  233. $message = 'Unauthorized';
  234. break;
  235. case 404: // not found
  236. $message = 'The requested page could not be found.';
  237. break;
  238. default:
  239. //$message = 'We are sorry, but something went terribly wrong.';
  240. $message = $e->getMessage();
  241. }
  242. } else {
  243. $code = null;
  244. $message = null;
  245. }
  246. //$code = ($e instanceof HttpException) ? $e->getStatusCode() : 500;
  247. $app['twig']->addGlobal('error_code', $code);
  248. $app['twig']->addGlobal('error_message', $message);
  249. $response = $app['template']->render_layout('error.tpl');
  250. return new Response($response);
  251. }
  252. );
  253. // Preserving the value of the global variable $charset.
  254. $charset_initial_value = $charset;
  255. // Section (tabs in the main chamilo menu)
  256. $app['this_section'] = SECTION_GLOBAL;
  257. // Inclusion of internationalization libraries
  258. require_once $libPath.'internationalization.lib.php';
  259. // Functions for internal use behind this API
  260. require_once $libPath.'internationalization_internal.lib.php';
  261. // Checking if we have a valid language. If not we set it to the platform language.
  262. if ($alreadyInstalled) {
  263. // Setting languages
  264. $app['api_get_languages'] = api_get_languages();
  265. $app['language_interface'] = $language_interface = api_get_language_interface();
  266. // Initialization of the internationalization library.
  267. api_initialize_internationalization();
  268. // Initialization of the default encoding that will be used by the multibyte string routines in the internationalization library.
  269. api_set_internationalization_default_encoding($charset);
  270. // include the local (contextual) parameters of this course or section
  271. require $includePath.'/local.inc.php';
  272. // reconfigure template now we know the user
  273. $app['template.hide_global_chat'] = !api_is_global_chat_enabled();
  274. /** Loading languages and sublanguages **/
  275. // @todo improve the language loading
  276. // if we use the javascript version (without go button) we receive a get
  277. // if we use the non-javascript version (with the go button) we receive a post
  278. // Include all files (first english and then current interface language)
  279. $app['this_script'] = isset($this_script) ? $this_script : null;
  280. // Sometimes the variable $language_interface is changed
  281. // temporarily for achieving translation in different language.
  282. // We need to save the genuine value of this variable and
  283. // to use it within the function get_lang(...).
  284. $language_interface_initial_value = $language_interface;
  285. $this_script = $app['this_script'];
  286. /* This will only work if we are in the page to edit a sub_language */
  287. if (isset($this_script) && $this_script == 'sub_language') {
  288. require_once api_get_path(SYS_CODE_PATH).'admin/sub_language.class.php';
  289. // getting the arrays of files i.e notification, trad4all, etc
  290. $language_files_to_load = SubLanguageManager:: get_lang_folder_files_list(
  291. api_get_path(SYS_LANG_PATH).'english',
  292. true
  293. );
  294. //getting parent info
  295. $languageId = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  296. $parent_language = SubLanguageManager::get_all_information_of_language($languageId);
  297. $subLanguageId = isset($_REQUEST['sub_language_id']) ? $_REQUEST['sub_language_id'] : null;
  298. //getting sub language info
  299. $sub_language = SubLanguageManager::get_all_information_of_language($subLanguageId);
  300. $english_language_array = $parent_language_array = $sub_language_array = array();
  301. if (!empty($language_files_to_load)) {
  302. foreach ($language_files_to_load as $language_file_item) {
  303. $lang_list_pre = array_keys($GLOBALS);
  304. //loading english
  305. $path = $langPath.'english/'.$language_file_item.'.inc.php';
  306. if (file_exists($path)) {
  307. include $path;
  308. }
  309. $lang_list_post = array_keys($GLOBALS);
  310. $lang_list_result = array_diff($lang_list_post, $lang_list_pre);
  311. unset($lang_list_pre);
  312. // english language array
  313. $english_language_array[$language_file_item] = compact($lang_list_result);
  314. //cleaning the variables
  315. foreach ($lang_list_result as $item) {
  316. unset(${$item});
  317. }
  318. $parent_file = $langPath.$parent_language['dokeos_folder'].'/'.$language_file_item.'.inc.php';
  319. if (file_exists($parent_file) && is_file($parent_file)) {
  320. include_once $parent_file;
  321. }
  322. // parent language array
  323. $parent_language_array[$language_file_item] = compact($lang_list_result);
  324. //cleaning the variables
  325. foreach ($lang_list_result as $item) {
  326. unset(${$item});
  327. }
  328. if (!empty($sub_language)) {
  329. $sub_file = $langPath.$sub_language['dokeos_folder'].'/'.$language_file_item.'.inc.php';
  330. if (file_exists($sub_file) && is_file($sub_file)) {
  331. include $sub_file;
  332. }
  333. }
  334. // sub language array
  335. $sub_language_array[$language_file_item] = compact($lang_list_result);
  336. //cleaning the variables
  337. foreach ($lang_list_result as $item) {
  338. unset(${$item});
  339. }
  340. }
  341. }
  342. }
  343. } else {
  344. $app['language_interface'] = $language_interface = $language_interface_initial_value = 'english';
  345. }
  346. /**
  347. * Include all necessary language files
  348. * - trad4all
  349. * - notification
  350. * - custom tool language files
  351. */
  352. $language_files = array();
  353. $language_files[] = 'trad4all';
  354. $language_files[] = 'notification';
  355. $language_files[] = 'accessibility';
  356. // @todo Added because userportal and index are loaded by a controller should be fixed when a $app['translator'] is configured
  357. $language_files[] = 'index';
  358. $language_files[] = 'courses';
  359. $language_files[] = 'course_home';
  360. if (isset($language_file)) {
  361. if (!is_array($language_file)) {
  362. $language_files[] = $language_file;
  363. } else {
  364. $language_files = array_merge($language_files, $language_file);
  365. }
  366. }
  367. if (isset($app['languages_file'])) {
  368. $language_files = array_merge($language_files, $app['languages_file']);
  369. }
  370. // if a set of language files has been properly defined
  371. if (is_array($language_files)) {
  372. // if the sub-language feature is on
  373. if (api_get_setting('allow_use_sub_language') == 'true') {
  374. require_once api_get_path(SYS_CODE_PATH).'admin/sub_language.class.php';
  375. $parent_path = SubLanguageManager::get_parent_language_path($language_interface);
  376. foreach ($language_files as $index => $language_file) {
  377. // include English
  378. include $langPath.'english/'.$language_file.'.inc.php';
  379. // prepare string for current language and its parent
  380. $lang_file = $langPath.$language_interface.'/'.$language_file.'.inc.php';
  381. $parent_lang_file = $langPath.$parent_path.'/'.$language_file.'.inc.php';
  382. // load the parent language file first
  383. if (file_exists($parent_lang_file)) {
  384. include $parent_lang_file;
  385. }
  386. // overwrite the parent language translations if there is a child
  387. if (file_exists($lang_file)) {
  388. include $lang_file;
  389. }
  390. }
  391. } else {
  392. // if the sub-languages feature is not on, then just load the
  393. // set language interface
  394. foreach ($language_files as $index => $language_file) {
  395. // include English
  396. include $langPath.'english/'.$language_file.'.inc.php';
  397. // prepare string for current language
  398. $langFile = $langPath.$language_interface.'/'.$language_file.'.inc.php';
  399. if (file_exists($langFile)) {
  400. include $langFile;
  401. }
  402. }
  403. }
  404. }
  405. // End loading languages
  406. // Specification for usernames:
  407. // 1. ASCII-letters, digits, "." (dot), "_" (underscore) are acceptable, 40 characters maximum length.
  408. // 2. Empty username is formally valid, but it is reserved for the anonymous user.
  409. // 3. Checking the login_is_email portal setting in order to accept 100 chars maximum
  410. // @todo this should be configured somewhere else usermanager.class.php? a users.yml setting?
  411. $default_username_length = 40;
  412. if (api_get_setting('login_is_email') == 'true') {
  413. $default_username_length = 100;
  414. }
  415. @define('USERNAME_MAX_LENGTH', $default_username_length);
  416. /** Silex Middlewares: */
  417. /** A "before" middleware allows you to tweak the Request before the controller is executed */
  418. $app->before(
  419. function () use ($app) {
  420. if (!file_exists($app['configuration_file']) && !file_exists($app['configuration_yml_file'])) {
  421. return new RedirectResponse(api_get_path(WEB_CODE_PATH).'install');
  422. $app->abort(500, "Configuration file was not found");
  423. }
  424. //Check the PHP version
  425. if (api_check_php_version() == false) {
  426. $app->abort(500, "Incorrect PHP version");
  427. }
  428. if (!is_writable(api_get_path(SYS_ARCHIVE_PATH))) {
  429. $app->abort(500, "temp folder must be writable");
  430. }
  431. // Loop in the folder array and create temp folders.
  432. /** @var ChamiloLMS\Component\DataFilesystem\DataFilesystem $filesystem */
  433. $filesystem = $app['chamilo.filesystem'];
  434. // @todo improvement create temp folders during installation not everytime
  435. $filesystem->createFolders($app['temp.paths']->folders);
  436. if ($app['assetic.auto_dump_assets']) {
  437. $filesystem->copyFolders($app['temp.paths']->copyFolders);
  438. }
  439. // Check and modify the date of user in the track.e.online table
  440. Online::loginCheck(api_get_user_id());
  441. $app['request']->getSession()->start();
  442. }
  443. );
  444. /** An after application middleware allows you to tweak the Response before it is sent to the client */
  445. $app->after(
  446. function (Request $request, Response $response) {
  447. }
  448. );
  449. /** A "finish" application middleware allows you to execute tasks after the Response has been sent to
  450. * the client (like sending emails or logging) */
  451. $app->finish(
  452. function (Request $request) use ($app) {
  453. }
  454. );
  455. // End Silex Middlewares
  456. // The global variable $charset has been defined in a language file too (trad4all.inc.php), this is legacy situation.
  457. // So, we have to reassign this variable again in order to keep its value right.
  458. $charset = $charset_initial_value;
  459. // The global variable $text_dir has been defined in the language file trad4all.inc.php.
  460. // For determing text direction correspondent to the current language we use now information from the internationalization library.
  461. $text_dir = api_get_text_direction();
  462. // Update of the logout_date field in the table track_e_login (needed for the calculation of the total connection time)
  463. /** "Login as user" custom script */
  464. // @todo move this code in a controller
  465. if (!isset($_SESSION['login_as']) && isset($_user)) {
  466. // if $_SESSION['login_as'] is set, then the user is an admin logged as the user
  467. $tbl_track_login = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  468. $sql_last_connection = "SELECT login_id, login_date FROM $tbl_track_login
  469. WHERE login_user_id = '".api_get_user_id()."'
  470. ORDER BY login_date DESC LIMIT 0,1";
  471. $q_last_connection = Database::query($sql_last_connection);
  472. if (Database::num_rows($q_last_connection) > 0) {
  473. $i_id_last_connection = Database::result($q_last_connection, 0, 'login_id');
  474. // is the latest logout_date still relevant?
  475. $sql_logout_date = "SELECT logout_date FROM $tbl_track_login WHERE login_id = $i_id_last_connection";
  476. $q_logout_date = Database::query($sql_logout_date);
  477. $res_logout_date = convert_sql_date(Database::result($q_logout_date, 0, 'logout_date'));
  478. if ($res_logout_date < time() - $app['configuration']['session_lifetime']) {
  479. // now that it's created, we can get its ID and carry on
  480. $q_last_connection = Database::query($sql_last_connection);
  481. $i_id_last_connection = Database::result($q_last_connection, 0, 'login_id');
  482. }
  483. $now = api_get_utc_datetime();
  484. $s_sql_update_logout_date = "UPDATE $tbl_track_login SET logout_date = '$now' WHERE login_id = $i_id_last_connection";
  485. Database::query($s_sql_update_logout_date);
  486. } else {
  487. // it isn't, we should create a fresh entry
  488. event_login();
  489. }
  490. }
  491. // Add language_measure_frequency to your main/inc/conf/configuration.php in
  492. // order to generate language variables frequency measurements (you can then
  493. // see them through main/cron/lang/langstats.php)
  494. // The langstat object will then be used in the get_lang() function.
  495. // This block can be removed to speed things up a bit as it should only ever
  496. // be used in development versions.
  497. // @todo create a service provider to load this
  498. if (isset($app['configuration']['language_measure_frequency']) && $app['configuration']['language_measure_frequency'] == 1) {
  499. require_once api_get_path(SYS_CODE_PATH).'/cron/lang/langstats.class.php';
  500. $langstats = new langstats();
  501. }
  502. /** Setting the course quota */
  503. // @todo move this somewhere else
  504. // Default quota for the course documents folder
  505. $default_quota = api_get_setting('default_document_quotum');
  506. // Just in case the setting is not correctly set
  507. if (empty($default_quota)) {
  508. $default_quota = 100000000;
  509. }
  510. @define('DEFAULT_DOCUMENT_QUOTA', $default_quota);
  511. /** Setting the is_admin key */
  512. $app['is_admin'] = false;
  513. /** Including routes */
  514. require_once 'routes.php';
  515. // Setting doctrine2 extensions
  516. if (isset($app['configuration']['main_database']) && isset($app['db.event_manager'])) {
  517. // @todo improvement do not create every time this objects
  518. $sortableGroup = new Gedmo\Mapping\Annotation\SortableGroup(array());
  519. $sortablePosition = new Gedmo\Mapping\Annotation\SortablePosition(array());
  520. $tree = new Gedmo\Mapping\Annotation\Tree(array());
  521. $tree = new Gedmo\Mapping\Annotation\TreeParent(array());
  522. $tree = new Gedmo\Mapping\Annotation\TreeLeft(array());
  523. $tree = new Gedmo\Mapping\Annotation\TreeRight(array());
  524. $tree = new Gedmo\Mapping\Annotation\TreeRoot(array());
  525. $tree = new Gedmo\Mapping\Annotation\TreeLevel(array());
  526. $tree = new Gedmo\Mapping\Annotation\Versioned(array());
  527. $tree = new Gedmo\Mapping\Annotation\Loggable(array());
  528. $tree = new Gedmo\Loggable\Entity\LogEntry();
  529. // Setting Doctrine2 extensions
  530. $timestampableListener = new \Gedmo\Timestampable\TimestampableListener();
  531. // $app['db.event_manager']->addEventSubscriber($timestampableListener);
  532. $app['dbs.event_manager']['db_read']->addEventSubscriber($timestampableListener);
  533. $app['dbs.event_manager']['db_write']->addEventSubscriber($timestampableListener);
  534. $sluggableListener = new \Gedmo\Sluggable\SluggableListener();
  535. // $app['db.event_manager']->addEventSubscriber($sluggableListener);
  536. $app['dbs.event_manager']['db_read']->addEventSubscriber($sluggableListener);
  537. $app['dbs.event_manager']['db_write']->addEventSubscriber($sluggableListener);
  538. $sortableListener = new Gedmo\Sortable\SortableListener();
  539. // $app['db.event_manager']->addEventSubscriber($sortableListener);
  540. $app['dbs.event_manager']['db_read']->addEventSubscriber($sortableListener);
  541. $app['dbs.event_manager']['db_write']->addEventSubscriber($sortableListener);
  542. $treeListener = new \Gedmo\Tree\TreeListener();
  543. //$treeListener->setAnnotationReader($cachedAnnotationReader);
  544. // $app['db.event_manager']->addEventSubscriber($treeListener);
  545. $app['dbs.event_manager']['db_read']->addEventSubscriber($treeListener);
  546. $app['dbs.event_manager']['db_write']->addEventSubscriber($treeListener);
  547. $loggableListener = new \Gedmo\Loggable\LoggableListener();
  548. $userInfo = api_get_user_info();
  549. if (isset($userInfo) && !empty($userInfo['username'])) {
  550. $loggableListener->setUsername($userInfo['username']);
  551. }
  552. //$app['db.event_manager']->addEventSubscriber($loggableListener);
  553. $app['dbs.event_manager']['db_read']->addEventSubscriber($loggableListener);
  554. $app['dbs.event_manager']['db_write']->addEventSubscriber($loggableListener);
  555. }
  556. // Fixes uses of $_course in the scripts.
  557. $_course = api_get_course_info();
  558. $_cid = api_get_course_id();
  559. return $app;