global.inc.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * It is recommended that ALL Chamilo scripts include this important file.
  5. * This script manages
  6. * - http get, post, post_files, session, server-vars extraction into global namespace;
  7. * (which doesn't occur anymore when servertype config setting is set to test,
  8. * and which will disappear completely in Dokeos 1.6.1)
  9. * - include of /conf/configuration.php;
  10. * - include of several libraries: main_api, database, display, text, security;
  11. * - selecting the main database;
  12. * - include of language files.
  13. *
  14. * @package chamilo.include
  15. * @todo isn't configuration.php renamed to configuration.inc.php yet?
  16. * @todo use the $_configuration array for all the needed variables
  17. * @todo remove the code that displays the button that links to the install page
  18. * but use a redirect immediately. By doing so the $already_installed variable can be removed.
  19. * @todo make it possible to enable / disable the tracking through the Chamilo config page.
  20. *
  21. */
  22. // Showing/hiding error codes in global error messages.
  23. define('SHOW_ERROR_CODES', false);
  24. // PHP version requirement.
  25. define('REQUIRED_PHP_VERSION', '5');
  26. // Determine the directory path where this current file lies.
  27. // This path will be useful to include the other intialisation files.
  28. $includePath = dirname(__FILE__);
  29. // PHP version check.
  30. if (!function_exists('version_compare') || version_compare(phpversion(), REQUIRED_PHP_VERSION, '<')) {
  31. $global_error_code = 1;
  32. // Incorrect PHP version.
  33. require $includePath.'/global_error_message.inc.php';
  34. die();
  35. }
  36. // @todo Isn't this file renamed to configuration.inc.php yet?
  37. // Include the main Chamilo platform configuration file.
  38. $main_configuration_file_path = $includePath.'/conf/configuration.php';
  39. $already_installed = false;
  40. if (file_exists($main_configuration_file_path)) {
  41. require_once $main_configuration_file_path;
  42. $already_installed = true;
  43. } else {
  44. $_configuration = array();
  45. }
  46. //Redirects to the main/install/ page
  47. if (!$already_installed) {
  48. $global_error_code = 2;
  49. // The system has not been installed yet.
  50. require $includePath.'/global_error_message.inc.php';
  51. die();
  52. }
  53. // Ensure that _configuration is in the global scope before loading
  54. // main_api.lib.php. This is particularly helpful for unit tests
  55. if (!isset($GLOBALS['_configuration'])) {
  56. $GLOBALS['_configuration'] = $_configuration;
  57. }
  58. // Code for trnasitional purposes, it can be removed right before the 1.8.7 release.
  59. if (empty($_configuration['system_version'])) {
  60. $_configuration['system_version'] = $_configuration['dokeos_version'];
  61. $_configuration['system_stable'] = $_configuration['dokeos_stable'];
  62. $_configuration['software_url'] = 'http://www.chamilo.org/';
  63. }
  64. // For backward compatibility.
  65. $_configuration['dokeos_version'] = $_configuration['system_version'];
  66. $_configuration['dokeos_stable'] = $_configuration['system_stable'];
  67. // Include the main Chamilo platform library file.
  68. require_once $includePath.'/lib/main_api.lib.php';
  69. // Do not over-use this variable. It is only for this script's local use.
  70. $lib_path = api_get_path(LIBRARY_PATH);
  71. // Fix bug in IIS that doesn't fill the $_SERVER['REQUEST_URI'].
  72. api_request_uri();
  73. // Add the path to the pear packages to the include path
  74. ini_set('include_path', api_create_include_path_setting());
  75. // This is for compatibility with MAC computers.
  76. ini_set('auto_detect_line_endings', '1');
  77. // Include the libraries that are necessary everywhere
  78. require_once $lib_path.'database.lib.php';
  79. require_once $lib_path.'template.lib.php';
  80. require_once $lib_path.'display.lib.php';
  81. require_once $lib_path.'text.lib.php';
  82. require_once $lib_path.'image.lib.php';
  83. require_once $lib_path.'array.lib.php';
  84. require_once $lib_path.'security.lib.php';
  85. require_once $lib_path.'events.lib.inc.php';
  86. require_once $lib_path.'debug.lib.php';
  87. require_once $lib_path.'rights.lib.php';
  88. require_once $lib_path.'model.lib.php';
  89. require_once $lib_path.'sortabletable.class.php';
  90. require_once $lib_path.'usermanager.lib.php';
  91. require_once $lib_path.'message.lib.php';
  92. require_once $lib_path.'social.lib.php';
  93. require_once $lib_path.'notification.lib.php';
  94. require_once $lib_path.'course.lib.php';
  95. require_once $lib_path.'sessionmanager.lib.php';
  96. require_once $lib_path.'tracking.lib.php';
  97. require_once $lib_path.'formvalidator/FormValidator.class.php';
  98. require_once $lib_path.'online.inc.php';
  99. /* DATABASE CONNECTION */
  100. // @todo: this shouldn't be done here. It should be stored correctly during installation.
  101. if (empty($_configuration['statistics_database']) && $already_installed) {
  102. $_configuration['statistics_database'] = $_configuration['main_database'];
  103. }
  104. global $database_connection;
  105. // Connect to the server database and select the main chamilo database.
  106. if (!($conn_return = @Database::connect(
  107. array(
  108. 'server' => $_configuration['db_host'],
  109. 'username' => $_configuration['db_user'],
  110. 'password' => $_configuration['db_password'],
  111. 'persistent' => $_configuration['db_persistent_connection'] // When $_configuration['db_persistent_connection'] is set, it is expected to be a boolean type.
  112. )))) {
  113. $global_error_code = 3;
  114. // The database server is not available or credentials are invalid.
  115. require $includePath.'/global_error_message.inc.php';
  116. die();
  117. }
  118. if (!$_configuration['db_host']) {
  119. $global_error_code = 4;
  120. // A configuration option about database server is missing.
  121. require $includePath.'/global_error_message.inc.php';
  122. die();
  123. }
  124. /* RETRIEVING ALL THE CHAMILO CONFIG SETTINGS FOR MULTIPLE URLs FEATURE*/
  125. if (!empty($_configuration['multiple_access_urls'])) {
  126. $_configuration['access_url'] = 1;
  127. $access_urls = api_get_access_urls();
  128. $protocol = ((!empty($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS']) != 'OFF') ? 'https' : 'http').'://';
  129. $request_url1 = $protocol.$_SERVER['SERVER_NAME'].'/';
  130. $request_url2 = $protocol.$_SERVER['HTTP_HOST'].'/';
  131. foreach ($access_urls as & $details) {
  132. if ($request_url1 == $details['url'] or $request_url2 == $details['url']) {
  133. $_configuration['access_url'] = $details['id'];
  134. }
  135. }
  136. } else {
  137. $_configuration['access_url'] = 1;
  138. }
  139. // The system has not been designed to use special SQL modes that were introduced since MySQL 5.
  140. Database::query("set session sql_mode='';");
  141. if (!Database::select_db($_configuration['main_database'], $database_connection)) {
  142. $global_error_code = 5;
  143. // Connection to the main Chamilo database is impossible, it might be missing or restricted or its configuration option might be incorrect.
  144. require $includePath.'/global_error_message.inc.php';
  145. die();
  146. }
  147. /* Initialization of the default encodings */
  148. // The platform's character set must be retrieved at this early moment.
  149. $sql = "SELECT selected_value FROM settings_current WHERE variable = 'platform_charset';";
  150. $result = Database::query($sql);
  151. while ($row = @Database::fetch_array($result)) {
  152. $charset = $row[0];
  153. }
  154. if (empty($charset)) {
  155. $charset = 'UTF-8';
  156. }
  157. // Preserving the value of the global variable $charset.
  158. $charset_initial_value = $charset;
  159. // Initialization of the internationalization library.
  160. api_initialize_internationalization();
  161. // Initialization of the default encoding that will be used by the multibyte string routines in the internationalization library.
  162. api_set_internationalization_default_encoding($charset);
  163. // Initialization of the database encoding to be used.
  164. Database::query("SET SESSION character_set_server='utf8';");
  165. Database::query("SET SESSION collation_server='utf8_general_ci';");
  166. if (api_is_utf8($charset)) {
  167. // 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.
  168. Database::query("SET NAMES 'utf8';");
  169. } else {
  170. Database::query("SET CHARACTER SET '" . Database::to_db_encoding($charset) . "';");
  171. }
  172. // Start session after the internationalization library has been initialized.
  173. api_session_start($already_installed);
  174. // Remove quotes added by PHP - get_magic_quotes_gpc() is deprecated in PHP 5 see #2970
  175. if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
  176. array_walk_recursive_limited($_GET, 'stripslashes', true);
  177. array_walk_recursive_limited($_POST, 'stripslashes', true);
  178. array_walk_recursive_limited($_COOKIE, 'stripslashes', true);
  179. array_walk_recursive_limited($_REQUEST, 'stripslashes', true);
  180. }
  181. // access_url == 1 is the default chamilo location
  182. if ($_configuration['access_url'] != 1) {
  183. $url_info = api_get_access_url($_configuration['access_url']);
  184. if ($url_info['active'] == 1) {
  185. $settings_by_access = & api_get_settings(null, 'list', $_configuration['access_url'], 1);
  186. foreach ($settings_by_access as & $row) {
  187. if (empty($row['variable'])) {
  188. $row['variable'] = 0;
  189. }
  190. if (empty($row['subkey'])) {
  191. $row['subkey'] = 0;
  192. }
  193. if (empty($row['category'])) {
  194. $row['category'] = 0;
  195. }
  196. $settings_by_access_list[$row['variable']][$row['subkey']][$row['category']] = $row;
  197. }
  198. }
  199. }
  200. $result = & api_get_settings(null, 'list', 1);
  201. foreach ($result as & $row) {
  202. if ($_configuration['access_url'] != 1) {
  203. if ($url_info['active'] == 1) {
  204. $var = empty($row['variable']) ? 0 : $row['variable'];
  205. $subkey = empty($row['subkey']) ? 0 : $row['subkey'];
  206. $category = empty($row['category']) ? 0 : $row['category'];
  207. }
  208. if ($row['access_url_changeable'] == 1 && $url_info['active'] == 1) {
  209. if ($settings_by_access_list[$var][$subkey][$category]['selected_value'] != '') {
  210. if ($row['subkey'] == null) {
  211. $_setting[$row['variable']] = $settings_by_access_list[$var][$subkey][$category]['selected_value'];
  212. } else {
  213. $_setting[$row['variable']][$row['subkey']] = $settings_by_access_list[$var][$subkey][$category]['selected_value'];
  214. }
  215. } else {
  216. if ($row['subkey'] == null) {
  217. $_setting[$row['variable']] = $row['selected_value'];
  218. } else {
  219. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  220. }
  221. }
  222. } else {
  223. if ($row['subkey'] == null) {
  224. $_setting[$row['variable']] = $row['selected_value'];
  225. } else {
  226. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  227. }
  228. }
  229. } else {
  230. if ($row['subkey'] == null) {
  231. $_setting[$row['variable']] = $row['selected_value'];
  232. } else {
  233. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  234. }
  235. }
  236. }
  237. $result = & api_get_settings('Plugins', 'list', $_configuration['access_url']);
  238. $_plugins = array();
  239. foreach ($result as & $row) {
  240. $key = & $row['variable'];
  241. if (is_string($_setting[$key])) {
  242. $_setting[$key] = array();
  243. }
  244. $_setting[$key][] = $row['selected_value'];
  245. $_plugins[$key][] = $row['selected_value'];
  246. }
  247. // Load allowed tag definitions for kses and/or HTMLPurifier.
  248. require_once $lib_path.'formvalidator/Rule/allowed_tags.inc.php';
  249. // Load HTMLPurifier.
  250. //require_once $lib_path.'htmlpurifier/library/HTMLPurifier.auto.php'; // It will be loaded later, in a lazy manner.
  251. // Before we call local.inc.php, let's define a global $this_section variable
  252. // which will then be usable from the banner and header scripts
  253. $this_section = SECTION_GLOBAL;
  254. // include the local (contextual) parameters of this course or section
  255. require $includePath.'/local.inc.php';
  256. //Include Chamilo Mail conf this is added here because the api_get_setting works
  257. $mail_conf = api_get_path(CONFIGURATION_PATH).'mail.conf.php';
  258. if (file_exists($mail_conf)) {
  259. require_once $mail_conf;
  260. }
  261. // ===== "who is logged in?" module section =====
  262. // check and modify the date of user in the track.e.online table
  263. if (!$x = strpos($_SERVER['PHP_SELF'], 'whoisonline.php')) {
  264. LoginCheck(isset($_user['user_id']) ? $_user['user_id'] : '');
  265. }
  266. // ===== end "who is logged in?" module section =====
  267. if (api_get_setting('server_type') == 'test') {
  268. /*
  269. Server type is test
  270. - high error reporting level
  271. - only do addslashes on $_GET and $_POST
  272. */
  273. if (IS_PHP_53) {
  274. error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
  275. } else {
  276. error_reporting(E_ALL & ~E_NOTICE);
  277. }
  278. } else {
  279. /*
  280. Server type is not test
  281. - normal error reporting level
  282. - full fake register globals block
  283. */
  284. error_reporting(E_COMPILE_ERROR | E_ERROR | E_CORE_ERROR);
  285. // TODO: These obsolete variables $HTTP_* to be check whether they are actually used.
  286. if (!isset($HTTP_GET_VARS)) { $HTTP_GET_VARS = $_GET; }
  287. if (!isset($HTTP_POST_VARS)) { $HTTP_POST_VARS = $_POST; }
  288. if (!isset($HTTP_POST_FILES)) { $HTTP_POST_FILES = $_FILES; }
  289. if (!isset($HTTP_SESSION_VARS)) { $HTTP_SESSION_VARS = $_SESSION; }
  290. if (!isset($HTTP_SERVER_VARS)) { $HTTP_SERVER_VARS = $_SERVER; }
  291. // Register SESSION variables into $GLOBALS
  292. if (sizeof($HTTP_SESSION_VARS)) {
  293. if (!is_array($_SESSION)) {
  294. $_SESSION = array();
  295. }
  296. foreach ($HTTP_SESSION_VARS as $key => $val) {
  297. $_SESSION[$key] = $HTTP_SESSION_VARS[$key];
  298. $GLOBALS[$key] = $HTTP_SESSION_VARS[$key];
  299. }
  300. }
  301. // Register SERVER variables into $GLOBALS
  302. if (sizeof($HTTP_SERVER_VARS)) {
  303. $_SERVER = array();
  304. foreach ($HTTP_SERVER_VARS as $key => $val) {
  305. $_SERVER[$key] = $HTTP_SERVER_VARS[$key];
  306. if (!isset($_SESSION[$key]) && $key != 'includePath' && $key != 'rootSys' && $key!= 'lang_path' && $key!= 'extAuthSource' && $key!= 'thisAuthSource' && $key!= 'main_configuration_file_path' && $key!= 'phpDigIncCn' && $key!= 'drs') {
  307. $GLOBALS[$key]=$HTTP_SERVER_VARS[$key];
  308. }
  309. }
  310. }
  311. }
  312. /* LOAD LANGUAGE FILES SECTION */
  313. // if we use the javascript version (without go button) we receive a get
  314. // if we use the non-javascript version (with the go button) we receive a post
  315. $user_language = '';
  316. if (!empty($_GET['language'])) {
  317. $user_language = $_GET['language'];
  318. }
  319. if (!empty($_POST['language_list'])) {
  320. $user_language = str_replace('index.php?language=', '', $_POST['language_list']);
  321. }
  322. // Include all files (first english and then current interface language)
  323. $langpath = api_get_path(SYS_LANG_PATH);
  324. /* This will only work if we are in the page to edit a sub_language */
  325. if (api_get_self() == api_get_path(REL_PATH).'main/admin/sub_language.php' || api_get_self() == api_get_path(REL_PATH).'main/admin/sub_language_ajax.inc.php') {
  326. require_once '../admin/sub_language.class.php';
  327. // getting the arrays of files i.e notification, trad4all, etc
  328. $language_files_to_load = SubLanguageManager:: get_lang_folder_files_list(api_get_path(SYS_LANG_PATH).'english', true);
  329. //getting parent info
  330. $parent_language = SubLanguageManager::get_all_information_of_language(intval($_REQUEST['id']));
  331. //getting sub language info
  332. $sub_language = SubLanguageManager::get_all_information_of_language(intval($_REQUEST['sub_language_id']));
  333. $english_language_array = $parent_language_array = $sub_language_array = array();
  334. foreach ($language_files_to_load as $language_file_item) {
  335. $lang_list_pre = array_keys($GLOBALS);
  336. include $langpath.'english/'.$language_file_item.'.inc.php'; //loading english
  337. $lang_list_post = array_keys($GLOBALS);
  338. $lang_list_result = array_diff($lang_list_post, $lang_list_pre);
  339. unset($lang_list_pre);
  340. // english language array
  341. $english_language_array[$language_file_item] = compact($lang_list_result);
  342. //cleaning the variables
  343. foreach($lang_list_result as $item) {
  344. unset(${$item});
  345. }
  346. $parent_file = $langpath.$parent_language['dokeos_folder'].'/'.$language_file_item.'.inc.php';
  347. if (is_file($parent_file)) {
  348. include_once $parent_file;
  349. }
  350. // parent language array
  351. $parent_language_array[$language_file_item] = compact($lang_list_result);
  352. //cleaning the variables
  353. foreach($lang_list_result as $item) {
  354. unset(${$item});
  355. }
  356. $sub_file = $langpath.$sub_language['dokeos_folder'].'/'.$language_file_item.'.inc.php';
  357. if (is_file($sub_file)) {
  358. include $sub_file;
  359. }
  360. // sub language array
  361. $sub_language_array[$language_file_item] = compact($lang_list_result);
  362. //cleaning the variables
  363. foreach($lang_list_result as $item) {
  364. unset(${$item});
  365. }
  366. }
  367. }
  368. // Checking if we have a valid language. If not we set it to the platform language.
  369. $valid_languages = api_get_languages();
  370. if (!empty($valid_languages)) {
  371. if (!in_array($user_language, $valid_languages['folder'])) {
  372. $user_language = api_get_setting('platformLanguage');
  373. }
  374. $language_priority1 = api_get_setting('languagePriority1');
  375. $language_priority2 = api_get_setting('languagePriority2');
  376. $language_priority3 = api_get_setting('languagePriority3');
  377. $language_priority4 = api_get_setting('languagePriority4');
  378. if (in_array($user_language, $valid_languages['folder']) && (isset($_GET['language']) || isset($_POST['language_list']))) {
  379. $user_selected_language = $user_language; // $_GET['language'];
  380. $_SESSION['user_language_choice'] = $user_selected_language;
  381. $platformLanguage = $user_selected_language;
  382. }
  383. if (!empty($language_priority4) && api_get_language_from_type($language_priority4) !== false ) {
  384. $language_interface = api_get_language_from_type($language_priority4);
  385. }
  386. else {
  387. $language_interface = api_get_setting('platformLanguage');
  388. }
  389. if (!empty($language_priority3) && api_get_language_from_type($language_priority3) !== false ) {
  390. $language_interface = api_get_language_from_type($language_priority3);
  391. }else {
  392. if (isset($_SESSION['user_language_choice'])) {
  393. $language_interface = $_SESSION['user_language_choice'];
  394. }
  395. }
  396. if (!empty($language_priority2) && api_get_language_from_type($language_priority2) !== false ) {
  397. $language_interface = api_get_language_from_type($language_priority2);
  398. }else {
  399. if (isset($_user['language'])) {
  400. $language_interface = $_user['language'];
  401. }
  402. }
  403. if (!empty($language_priority1) && api_get_language_from_type($language_priority1) !== false ) {
  404. $language_interface = api_get_language_from_type($language_priority1);
  405. }else {
  406. if ($_course['language']) {
  407. $language_interface = $_course['language'];
  408. }
  409. }
  410. }
  411. // Sometimes the variable $language_interface is changed
  412. // temporarily for achieving translation in different language.
  413. // We need to save the genuine value of this variable and
  414. // to use it within the function get_lang(...).
  415. $language_interface_initial_value = $language_interface;
  416. /**
  417. * Include all necessary language files
  418. * - trad4all
  419. * - notification
  420. * - custom tool language files
  421. */
  422. $language_files = array();
  423. $language_files[] = 'trad4all';
  424. $language_files[] = 'notification';
  425. $language_files[] = 'accessibility';
  426. if (isset($language_file)) {
  427. if (!is_array($language_file)) {
  428. $language_files[] = $language_file;
  429. } else {
  430. $language_files = array_merge($language_files, $language_file);
  431. }
  432. }
  433. // if a set of language files has been properly defined
  434. if (is_array($language_files)) {
  435. // if the sub-language feature is on
  436. if (api_get_setting('allow_use_sub_language') == 'true') {
  437. require_once api_get_path(SYS_CODE_PATH).'admin/sub_language.class.php';
  438. $parent_path = SubLanguageManager::get_parent_language_path($language_interface);
  439. foreach ($language_files as $index => $language_file) {
  440. // include English
  441. include $langpath.'english/'.$language_file.'.inc.php';
  442. // prepare string for current language and its parent
  443. $lang_file = $langpath.$language_interface.'/'.$language_file.'.inc.php';
  444. $parent_lang_file = $langpath.$parent_path.'/'.$language_file.'.inc.php';
  445. // load the parent language file first
  446. if (file_exists($parent_lang_file)) {
  447. include $parent_lang_file;
  448. }
  449. // overwrite the parent language translations if there is a child
  450. if (file_exists($lang_file)) {
  451. include $lang_file;
  452. }
  453. }
  454. } else {
  455. // if the sub-languages feature is not on, then just load the
  456. // set language interface
  457. foreach ($language_files as $index => $language_file) {
  458. // include English
  459. include $langpath.'english/'.$language_file.'.inc.php';
  460. // prepare string for current language
  461. $langfile = $langpath.$language_interface.'/'.$language_file.'.inc.php';
  462. if (file_exists($langfile)) {
  463. include $langfile;
  464. }
  465. }
  466. }
  467. }
  468. // The global variable $charset has been defined in a language file too (trad4all.inc.php), this is legacy situation.
  469. // So, we have to reassign this variable again in order to keep its value right.
  470. $charset = $charset_initial_value;
  471. // The global variable $text_dir has been defined in the language file trad4all.inc.php.
  472. // For determing text direction correspondent to the current language we use now information from the internationalization library.
  473. $text_dir = api_get_text_direction();
  474. //Update of the logout_date field in the table track_e_login (needed for the calculation of the total connection time)
  475. if (!isset($_SESSION['login_as']) && isset($_user)) {
  476. // if $_SESSION['login_as'] is set, then the user is an admin logged as the user
  477. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  478. $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";
  479. $q_last_connection = Database::query($sql_last_connection);
  480. if (Database::num_rows($q_last_connection) > 0) {
  481. $i_id_last_connection = Database::result($q_last_connection, 0, 'login_id');
  482. // is the latest logout_date still relevant?
  483. $sql_logout_date = "SELECT logout_date FROM $tbl_track_login WHERE login_id=$i_id_last_connection";
  484. $q_logout_date = Database::query($sql_logout_date);
  485. $res_logout_date = convert_sql_date(Database::result($q_logout_date,0,'logout_date'));
  486. if ($res_logout_date < time() - $_configuration['session_lifetime']) {
  487. // it isn't, we should create a fresh entry
  488. event_login();
  489. // now that it's created, we can get its ID and carry on
  490. $q_last_connection = Database::query($sql_last_connection);
  491. $i_id_last_connection = Database::result($q_last_connection, 0, 'login_id');
  492. }
  493. $s_sql_update_logout_date = "UPDATE $tbl_track_login SET logout_date=NOW() WHERE login_id='$i_id_last_connection'";
  494. Database::query($s_sql_update_logout_date);
  495. }
  496. }