global.inc.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. /** Loading configuration file */
  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. /** End loading configuration file */
  73. /** Setting Chamilo paths */
  74. $app['root_sys'] = isset($_configuration['root_sys']) ? $_configuration['root_sys'] : dirname(dirname(__DIR__)).'/';
  75. $app['sys_data_path'] = isset($_configuration['sys_data_path']) ? $_configuration['sys_data_path'] : $app['root_sys'].'data/';
  76. $app['sys_config_path'] = isset($_configuration['sys_config_path']) ? $_configuration['sys_config_path'] : $app['root_sys'].'config/';
  77. $app['sys_temp_path'] = isset($_configuration['sys_temp_path']) ? $_configuration['sys_temp_path'] : $app['root_sys'].'temp/';
  78. $app['sys_log_path'] = isset($_configuration['sys_log_path']) ? $_configuration['sys_log_path'] : $app['root_sys'].'logs/';
  79. /** Including legacy libs */
  80. // Include the main Chamilo platform library file.
  81. require_once $includePath.'/lib/main_api.lib.php';
  82. // Setting url_append
  83. $urlInfo = isset($_configuration['root_web']) ? parse_url($_configuration['root_web']) : null;
  84. $_configuration['url_append'] = null;
  85. if (isset($urlInfo['path'])) {
  86. $_configuration['url_append'] = '/'.basename($urlInfo['path']);
  87. }
  88. // Do not over-use this variable. It is only for this scripts local use.
  89. $libPath = $includePath.'/lib/';
  90. // Database constants
  91. require_once $libPath.'database.constants.inc.php';
  92. // @todo Rewrite the events.lib.inc.php in a class
  93. require_once $libPath.'events.lib.inc.php';
  94. // Load allowed tag definitions for kses and/or HTMLPurifier.
  95. require_once $libPath.'formvalidator/Rule/allowed_tags.inc.php';
  96. /** Loading config files */
  97. if ($alreadyInstalled) {
  98. $configPath = $app['sys_config_path'];
  99. $confFiles = array(
  100. 'auth.conf.php',
  101. 'events.conf.php',
  102. 'mail.conf.php',
  103. 'portfolio.conf.php',
  104. 'profile.conf.php'
  105. );
  106. foreach ($confFiles as $confFile) {
  107. if (file_exists($configPath.$confFile)) {
  108. require_once $configPath.$confFile;
  109. }
  110. }
  111. // Fixing $_configuration array
  112. //Fixes bug in Chamilo 1.8.7.1 array was not set
  113. $administrator['email'] = isset($administrator['email']) ? $administrator['email'] : 'admin@example.com';
  114. $administrator['name'] = isset($administrator['name']) ? $administrator['name'] : 'Admin';
  115. // Code for transitional purposes, it can be removed right before the 1.8.7 release.
  116. /*if (empty($_configuration['system_version'])) {
  117. $_configuration['system_version'] = (!empty($_configuration['dokeos_version']) ? $_configuration['dokeos_version'] : '');
  118. $_configuration['system_stable'] = (!empty($_configuration['dokeos_stable']) ? $_configuration['dokeos_stable'] : '');
  119. $_configuration['software_url'] = 'http://www.chamilo.org/';
  120. }*/
  121. // For backward compatibility.
  122. $_configuration['dokeos_version'] = $_configuration['system_version'];
  123. //$_configuration['dokeos_stable'] = $_configuration['system_stable'];
  124. $userPasswordCrypted = (!empty($_configuration['password_encryption']) ? $_configuration['password_encryption'] : 'sha1');
  125. }
  126. /* Retrieving all the chamilo config settings for multiple URLs feature*/
  127. if (isset($_configuration['multiple_access_urls']) && !empty($_configuration['multiple_access_urls'])) {
  128. $_configuration['access_url'] = 1;
  129. $access_urls = api_get_access_urls();
  130. $protocol = ((!empty($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS']) != 'OFF') ? 'https' : 'http').'://';
  131. $request_url1 = $protocol.$_SERVER['SERVER_NAME'].'/';
  132. $request_url2 = $protocol.$_SERVER['HTTP_HOST'].'/';
  133. foreach ($access_urls as & $details) {
  134. if ($request_url1 == $details['url'] or $request_url2 == $details['url']) {
  135. $_configuration['access_url'] = $details['id'];
  136. }
  137. }
  138. } else {
  139. $_configuration['access_url'] = 1;
  140. }
  141. $app['configuration'] = $_configuration;
  142. // Ensure that _configuration is in the global scope before loading
  143. // main_api.lib.php. This is particularly helpful for unit tests
  144. // @todo do not use $GLOBALS
  145. /*if (!isset($GLOBALS['_configuration'])) {
  146. $GLOBALS['_configuration'] = $_configuration;
  147. }*/
  148. // Add the path to the pear packages to the include path
  149. ini_set('include_path', api_create_include_path_setting());
  150. $app['configuration_file'] = $configurationFilePath;
  151. $app['configuration_yml_file'] = $configurationYMLFile;
  152. $app['languages_file'] = array();
  153. $app['installed'] = $alreadyInstalled;
  154. // Loading $app settings
  155. //require_once __DIR__.'/../../src/ChamiloLMS/Resources/config/prod.php';
  156. require_once __DIR__.'/../../src/ChamiloLMS/Resources/config/dev.php';
  157. // Classic way of render pages or the Controller approach
  158. $app['classic_layout'] = false;
  159. $app['full_width'] = false;
  160. $app['breadcrumb'] = array();
  161. // The script is allowed? This setting is modified when calling api_is_not_allowed()
  162. $app['allowed'] = true;
  163. // Template settings loaded in template.lib.php
  164. $app['template.show_header'] = true;
  165. $app['template.show_footer'] = true;
  166. $app['template.show_learnpath'] = false;
  167. $app['template.hide_global_chat'] = true;
  168. $app['template.load_plugins'] = true;
  169. // Default template style
  170. $app['template_style'] = 'default';
  171. // Default layout
  172. $app['default_layout'] = $app['template_style'].'/layout/layout_1_col.tpl';
  173. /** Including service providers */
  174. require_once 'services.php';
  175. // Connect to the server database and select the main chamilo database.
  176. if (!($conn_return = @Database::connect(
  177. array(
  178. 'server' => $app['configuration']['db_host'],
  179. 'username' => $app['configuration']['db_user'],
  180. 'password' => $app['configuration']['db_password'],
  181. 'persistent' => isset($app['configuration']['db_persistent_connection']) ? $app['configuration']['db_persistent_connection'] : null
  182. // When $app['configuration']['db_persistent_connection'] is set, it is expected to be a boolean type.
  183. )
  184. ))
  185. ) {
  186. //$app->abort(500, "Database is unavailable"); //error 3
  187. }
  188. /*
  189. if (!$app['configuration']['db_host']) {
  190. //$app->abort(500, "Database is unavailable"); //error 3
  191. }*/
  192. $charset = 'UTF-8';
  193. $checkConnection = false;
  194. if (isset($app['configuration']['main_database'])) {
  195. // The system has not been designed to use special SQL modes that were introduced since MySQL 5.
  196. Database::query("set session sql_mode='';");
  197. $checkConnection = @Database::select_db($app['configuration']['main_database'], $conn_return);
  198. if ($checkConnection) {
  199. // Initialization of the database encoding to be used.
  200. Database::query("SET SESSION character_set_server='utf8';");
  201. Database::query("SET SESSION collation_server='utf8_general_ci';");
  202. /* Initialization of the default encodings */
  203. // The platform's character set must be retrieved at this early moment.
  204. /*$sql = "SELECT selected_value FROM settings_current WHERE variable = 'platform_charset';";
  205. $result = Database::query($sql);
  206. while ($row = @Database::fetch_array($result)) {
  207. $charset = $row[0];
  208. }
  209. if (empty($charset)) {
  210. $charset = 'UTF-8';
  211. }*/
  212. //Charset is UTF-8
  213. /*
  214. if (api_is_utf8($charset)) {
  215. // See Bug #1802: For UTF-8 systems we prefer to use "SET NAMES 'utf8'" statement in order to avoid a bizarre problem with Chinese language.
  216. Database::query("SET NAMES 'utf8';");
  217. } else {
  218. Database::query("SET CHARACTER SET '".Database::to_db_encoding($charset)."';");
  219. }*/
  220. Database::query("SET NAMES 'utf8';");
  221. }
  222. }
  223. // Manage Chamilo error messages
  224. $app->error(
  225. function (\Exception $e, $code) use ($app) {
  226. if ($app['debug']) {
  227. //return;
  228. }
  229. if (isset($code)) {
  230. switch ($code) {
  231. case 401:
  232. $message = 'Unauthorized';
  233. break;
  234. case 404: // not found
  235. $message = 'The requested page could not be found.';
  236. break;
  237. default:
  238. //$message = 'We are sorry, but something went terribly wrong.';
  239. $message = $e->getMessage();
  240. }
  241. } else {
  242. $code = null;
  243. $message = null;
  244. }
  245. //$code = ($e instanceof HttpException) ? $e->getStatusCode() : 500;
  246. $app['twig']->addGlobal('error_code', $code);
  247. $app['twig']->addGlobal('error_message', $message);
  248. $response = $app['template']->render_layout('error.tpl');
  249. return new Response($response);
  250. }
  251. );
  252. // Preserving the value of the global variable $charset.
  253. $charset_initial_value = $charset;
  254. // Start session after the internationalization library has been initialized
  255. // @todo use silex session provider instead of a custom class
  256. Chamilo::session()->start($alreadyInstalled);
  257. // Loading chamilo settings
  258. /* @todo create a service provider to load plugins.
  259. Check how bolt add extensions (including twig templates, config with yml)*/
  260. $_plugins = array();
  261. if ($alreadyInstalled && $checkConnection) {
  262. $settings_refresh_info = api_get_settings_params_simple(array('variable = ?' => 'settings_latest_update'));
  263. $settings_latest_update = $settings_refresh_info ? $settings_refresh_info['selected_value'] : null;
  264. $_setting = isset($_SESSION['_setting']) ? $_SESSION['_setting'] : null;
  265. $_plugins = isset($_SESSION['_plugins']) ? $_SESSION['_plugins'] : null;
  266. if (empty($_setting)) {
  267. api_set_settings_and_plugins();
  268. } else {
  269. if (isset($_setting['settings_latest_update']) && $_setting['settings_latest_update'] != $settings_latest_update) {
  270. api_set_settings_and_plugins();
  271. $_setting = isset($_SESSION['_setting']) ? $_SESSION['_setting'] : null;
  272. $_plugins = isset($_SESSION['_plugins']) ? $_SESSION['_plugins'] : null;
  273. }
  274. }
  275. }
  276. $app['plugins'] = $_plugins;
  277. // Section (tabs in the main chamilo menu)
  278. $app['this_section'] = SECTION_GLOBAL;
  279. // Inclusion of internationalization libraries
  280. require_once $libPath.'internationalization.lib.php';
  281. // Functions for internal use behind this API
  282. require_once $libPath.'internationalization_internal.lib.php';
  283. // Setting languages
  284. $app['api_get_languages'] = api_get_languages();
  285. // Checking if we have a valid language. If not we set it to the platform language.
  286. if ($alreadyInstalled) {
  287. $app['language_interface'] = $language_interface = api_get_language_interface();
  288. } else {
  289. $app['language_interface'] = $language_interface = 'english';
  290. }
  291. // Initialization of the internationalization library.
  292. api_initialize_internationalization();
  293. // Initialization of the default encoding that will be used by the multibyte string routines in the internationalization library.
  294. api_set_internationalization_default_encoding($charset);
  295. // include the local (contextual) parameters of this course or section
  296. require $includePath.'/local.inc.php';
  297. // reconfigure templat now we know the user
  298. $app['template.hide_global_chat'] = !api_is_global_chat_enabled();
  299. /** Loading languages and sublanguages **/
  300. // @todo improve the language loading
  301. // if we use the javascript version (without go button) we receive a get
  302. // if we use the non-javascript version (with the go button) we receive a post
  303. // Include all files (first english and then current interface language)
  304. $app['this_script'] = isset($this_script) ? $this_script : null;
  305. // Sometimes the variable $language_interface is changed
  306. // temporarily for achieving translation in different language.
  307. // We need to save the genuine value of this variable and
  308. // to use it within the function get_lang(...).
  309. $language_interface_initial_value = $language_interface;
  310. $langPath = api_get_path(SYS_LANG_PATH);
  311. $this_script = $app['this_script'];
  312. $language_interface = $app['language_interface'];
  313. /* This will only work if we are in the page to edit a sub_language */
  314. if (isset($this_script) && $this_script == 'sub_language') {
  315. require_once api_get_path(SYS_CODE_PATH).'admin/sub_language.class.php';
  316. // getting the arrays of files i.e notification, trad4all, etc
  317. $language_files_to_load = SubLanguageManager:: get_lang_folder_files_list(
  318. api_get_path(SYS_LANG_PATH).'english',
  319. true
  320. );
  321. //getting parent info
  322. $languageId = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  323. $parent_language = SubLanguageManager::get_all_information_of_language($languageId);
  324. $subLanguageId = isset($_REQUEST['sub_language_id']) ? $_REQUEST['sub_language_id'] : null;
  325. //getting sub language info
  326. $sub_language = SubLanguageManager::get_all_information_of_language($subLanguageId);
  327. $english_language_array = $parent_language_array = $sub_language_array = array();
  328. if (!empty($language_files_to_load))
  329. foreach ($language_files_to_load as $language_file_item) {
  330. $lang_list_pre = array_keys($GLOBALS);
  331. //loading english
  332. $path = $langPath.'english/'.$language_file_item.'.inc.php';
  333. if (file_exists($path)) {
  334. include $path;
  335. }
  336. $lang_list_post = array_keys($GLOBALS);
  337. $lang_list_result = array_diff($lang_list_post, $lang_list_pre);
  338. unset($lang_list_pre);
  339. // english language array
  340. $english_language_array[$language_file_item] = compact($lang_list_result);
  341. //cleaning the variables
  342. foreach ($lang_list_result as $item) {
  343. unset(${$item});
  344. }
  345. $parent_file = $langPath.$parent_language['dokeos_folder'].'/'.$language_file_item.'.inc.php';
  346. if (file_exists($parent_file) && is_file($parent_file)) {
  347. include_once $parent_file;
  348. }
  349. // parent language array
  350. $parent_language_array[$language_file_item] = compact($lang_list_result);
  351. //cleaning the variables
  352. foreach ($lang_list_result as $item) {
  353. unset(${$item});
  354. }
  355. if (!empty($sub_language)) {
  356. $sub_file = $langPath.$sub_language['dokeos_folder'].'/'.$language_file_item.'.inc.php';
  357. if (file_exists($sub_file) && is_file($sub_file)) {
  358. include $sub_file;
  359. }
  360. }
  361. // sub language array
  362. $sub_language_array[$language_file_item] = compact($lang_list_result);
  363. //cleaning the variables
  364. foreach ($lang_list_result as $item) {
  365. unset(${$item});
  366. }
  367. }
  368. }
  369. /**
  370. * Include all necessary language files
  371. * - trad4all
  372. * - notification
  373. * - custom tool language files
  374. */
  375. $language_files = array();
  376. $language_files[] = 'trad4all';
  377. $language_files[] = 'notification';
  378. $language_files[] = 'accessibility';
  379. // @todo Added because userportal and index are loaded by a controller should be fixed when a $app['translator'] is configured
  380. $language_files[] = 'index';
  381. $language_files[] = 'courses';
  382. $language_files[] = 'course_home';
  383. if (isset($language_file)) {
  384. if (!is_array($language_file)) {
  385. $language_files[] = $language_file;
  386. } else {
  387. $language_files = array_merge($language_files, $language_file);
  388. }
  389. }
  390. if (isset($app['languages_file'])) {
  391. $language_files = array_merge($language_files, $app['languages_file']);
  392. }
  393. // if a set of language files has been properly defined
  394. if (is_array($language_files)) {
  395. // if the sub-language feature is on
  396. if (api_get_setting('allow_use_sub_language') == 'true') {
  397. require_once api_get_path(SYS_CODE_PATH).'admin/sub_language.class.php';
  398. $parent_path = SubLanguageManager::get_parent_language_path($language_interface);
  399. foreach ($language_files as $index => $language_file) {
  400. // include English
  401. include $langPath.'english/'.$language_file.'.inc.php';
  402. // prepare string for current language and its parent
  403. $lang_file = $langPath.$language_interface.'/'.$language_file.'.inc.php';
  404. $parent_lang_file = $langPath.$parent_path.'/'.$language_file.'.inc.php';
  405. // load the parent language file first
  406. if (file_exists($parent_lang_file)) {
  407. include $parent_lang_file;
  408. }
  409. // overwrite the parent language translations if there is a child
  410. if (file_exists($lang_file)) {
  411. include $lang_file;
  412. }
  413. }
  414. } else {
  415. // if the sub-languages feature is not on, then just load the
  416. // set language interface
  417. foreach ($language_files as $index => $language_file) {
  418. // include English
  419. include $langPath.'english/'.$language_file.'.inc.php';
  420. // prepare string for current language
  421. $langFile = $langPath.$language_interface.'/'.$language_file.'.inc.php';
  422. if (file_exists($langFile)) {
  423. include $langFile;
  424. }
  425. }
  426. }
  427. }
  428. // End loading languages
  429. // Specification for usernames:
  430. // 1. ASCII-letters, digits, "." (dot), "_" (underscore) are acceptable, 40 characters maximum length.
  431. // 2. Empty username is formally valid, but it is reserved for the anonymous user.
  432. // 3. Checking the login_is_email portal setting in order to accept 100 chars maximum
  433. // @todo this should be configured somewhere else usermanager.class.php? a users.yml setting?
  434. $default_username_length = 40;
  435. if (api_get_setting('login_is_email') == 'true') {
  436. $default_username_length = 100;
  437. }
  438. @define('USERNAME_MAX_LENGTH', $default_username_length);
  439. /** Silex Middlewares: */
  440. /** A "before" middleware allows you to tweak the Request before the controller is executed */
  441. $app->before(
  442. function () use ($app, $checkConnection) {
  443. if (!file_exists($app['configuration_file']) && !file_exists($app['configuration_yml_file'])) {
  444. return new RedirectResponse(api_get_path(WEB_CODE_PATH).'install');
  445. $app->abort(500, "Incorrect PHP version");
  446. }
  447. //Check the PHP version
  448. if (api_check_php_version() == false) {
  449. $app->abort(500, "Incorrect PHP version");
  450. }
  451. if ($checkConnection == false) {
  452. $app->abort(500, "Database not available");
  453. }
  454. if (!is_writable(api_get_path(SYS_ARCHIVE_PATH))) {
  455. $app->abort(500, "temp folder must be writable");
  456. }
  457. // Check and modify the date of user in the track.e.online table
  458. //if ($checkConnection && !$x = strpos($_SERVER['PHP_SELF'], 'whoisonline.php')) {
  459. Online::loginCheck(api_get_user_id());
  460. //}
  461. //$app['request']->getSession()->start();
  462. //var_dump($app['cidReset']);
  463. }
  464. );
  465. /** An after application middleware allows you to tweak the Response before it is sent to the client */
  466. $app->after(
  467. function (Request $request, Response $response) {
  468. }
  469. );
  470. /** A "finish" application middleware allows you to execute tasks after the Response has been sent to
  471. * the client (like sending emails or logging) */
  472. $app->finish(
  473. function (Request $request) use ($app) {
  474. }
  475. );
  476. // End Silex Middlewares
  477. // The global variable $charset has been defined in a language file too (trad4all.inc.php), this is legacy situation.
  478. // So, we have to reassign this variable again in order to keep its value right.
  479. $charset = $charset_initial_value;
  480. // The global variable $text_dir has been defined in the language file trad4all.inc.php.
  481. // For determing text direction correspondent to the current language we use now information from the internationalization library.
  482. $text_dir = api_get_text_direction();
  483. // Update of the logout_date field in the table track_e_login (needed for the calculation of the total connection time)
  484. /** "Login as user" custom script */
  485. if (!isset($_SESSION['login_as']) && isset($_user)) {
  486. // if $_SESSION['login_as'] is set, then the user is an admin logged as the user
  487. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  488. $sql_last_connection = "SELECT login_id, login_date FROM $tbl_track_login WHERE login_user_id='".$_user["user_id"]."' ORDER BY login_date DESC LIMIT 0,1";
  489. $q_last_connection = Database::query($sql_last_connection);
  490. if (Database::num_rows($q_last_connection) > 0) {
  491. $i_id_last_connection = Database::result($q_last_connection, 0, 'login_id');
  492. // is the latest logout_date still relevant?
  493. $sql_logout_date = "SELECT logout_date FROM $tbl_track_login WHERE login_id = $i_id_last_connection";
  494. $q_logout_date = Database::query($sql_logout_date);
  495. $res_logout_date = convert_sql_date(Database::result($q_logout_date, 0, 'logout_date'));
  496. if ($res_logout_date < time() - $app['configuration']['session_lifetime']) {
  497. // now that it's created, we can get its ID and carry on
  498. $q_last_connection = Database::query($sql_last_connection);
  499. $i_id_last_connection = Database::result($q_last_connection, 0, 'login_id');
  500. }
  501. $s_sql_update_logout_date = "UPDATE $tbl_track_login SET logout_date=NOW() WHERE login_id='$i_id_last_connection'";
  502. Database::query($s_sql_update_logout_date);
  503. } else {
  504. // it isn't, we should create a fresh entry
  505. event_login();
  506. }
  507. }
  508. // Add language_measure_frequency to your main/inc/conf/configuration.php in
  509. // order to generate language variables frequency measurements (you can then
  510. // see them through main/cron/lang/langstats.php)
  511. // The langstat object will then be used in the get_lang() function.
  512. // This block can be removed to speed things up a bit as it should only ever
  513. // be used in development versions.
  514. // @todo create a service provider to load this
  515. if (isset($app['configuration']['language_measure_frequency']) && $app['configuration']['language_measure_frequency'] == 1) {
  516. require_once api_get_path(SYS_CODE_PATH).'/cron/lang/langstats.class.php';
  517. $langstats = new langstats();
  518. }
  519. /** Setting the course quota */
  520. // @todo move this somewhere else
  521. // Default quota for the course documents folder
  522. $default_quota = api_get_setting('default_document_quotum');
  523. // Just in case the setting is not correctly set
  524. if (empty($default_quota)) {
  525. $default_quota = 100000000;
  526. }
  527. @define('DEFAULT_DOCUMENT_QUOTA', $default_quota);
  528. /** Setting the is_admin key */
  529. $app['is_admin'] = false;
  530. /** Including routes */
  531. require_once 'routes.php';
  532. //Fixes uses of $_course in the scripts
  533. $_course = api_get_course_info();
  534. $_cid = api_get_course_id();
  535. return $app;