global.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. /**
  3. ==============================================================================
  4. * It is recommended that ALL dokeos 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 dokeos.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 Dokeos config page.
  20. *
  21. ==============================================================================
  22. */
  23. // Showing/hiding error codes in global error messages.
  24. define('SHOW_ERROR_CODES', false);
  25. // PHP version requirement.
  26. define('REQUIRED_PHP_VERSION', '5');
  27. // Determine the directory path where this current file lies.
  28. // This path will be useful to include the other intialisation files.
  29. $includePath = dirname(__FILE__);
  30. // PHP version check.
  31. if (!function_exists('version_compare') || version_compare(phpversion(), REQUIRED_PHP_VERSION, '<')) {
  32. $global_error_code = 1;
  33. // Incorrect PHP version.
  34. require $includePath.'/global_error_message.inc.php';
  35. die();
  36. }
  37. // @todo Isn't this file renamed to configuration.inc.php yet?
  38. // Include the main Dokeos platform configuration file.
  39. $main_configuration_file_path = $includePath.'/conf/configuration.php';
  40. $already_installed = false;
  41. if (file_exists($main_configuration_file_path)) {
  42. require_once $main_configuration_file_path;
  43. $already_installed = true;
  44. } else {
  45. $_configuration = array();
  46. }
  47. // Include the main Dokeos platform library file.
  48. require_once $includePath.'/lib/main_api.lib.php';
  49. // Do not over-use this variable. It is only for this script's local use.
  50. $lib_path = api_get_path(LIBRARY_PATH);
  51. // Start session.
  52. api_session_start($already_installed);
  53. if (!$already_installed) {
  54. $global_error_code = 2;
  55. // The system has not been installed yet.
  56. require $includePath.'/global_error_message.inc.php';
  57. die();
  58. }
  59. // Fix bug in IIS that doesn't fill the $_SERVER['REQUEST_URI'].
  60. api_request_uri();
  61. // Add the path to the pear packages to the include path
  62. ini_set('include_path', api_create_include_path_setting());
  63. // This is for compatibility with MAC computers.
  64. ini_set('auto_detect_line_endings', '1');
  65. // Include the libraries that are necessary everywhere
  66. require_once $lib_path.'database.lib.php';
  67. require_once $lib_path.'display.lib.php';
  68. require_once $lib_path.'text.lib.php';
  69. require_once $lib_path.'security.lib.php';
  70. require_once $lib_path.'events.lib.inc.php';
  71. // @todo: this shouldn't be done here. It should be stored correctly during installation.
  72. if (empty($_configuration['statistics_database']) && $already_installed) {
  73. $_configuration['statistics_database'] = $_configuration['main_database'];
  74. }
  75. // Connect to the server database and select the main dokeos database.
  76. if (!($dokeos_database_connection = @mysql_connect($_configuration['db_host'], $_configuration['db_user'], $_configuration['db_password']))) {
  77. $global_error_code = 3;
  78. // The database server is not available or credentials are invalid.
  79. require $includePath.'/global_error_message.inc.php';
  80. die();
  81. }
  82. if (!$_configuration['db_host']) {
  83. $global_error_code = 4;
  84. // A configuration option about database server is missing.
  85. require $includePath.'/global_error_message.inc.php';
  86. die();
  87. }
  88. // The Dokeos system has not been designed to use special SQL modes that were introduced since MySQL 5.
  89. Database::query("set session sql_mode='';", __FILE__, __LINE__);
  90. if (!mysql_select_db($_configuration['main_database'], $dokeos_database_connection)) {
  91. $global_error_code = 5;
  92. // Connection to the main Dokeos database is impossible, it might be missing or restricted or its configuration option might be incorrect.
  93. require $includePath.'/global_error_message.inc.php';
  94. die();
  95. }
  96. /*
  97. --------------------------------------------
  98. Initialization of the default encodings
  99. --------------------------------------------
  100. */
  101. // The platform's character set must be retrieved at this early moment.
  102. $sql = "SELECT selected_value FROM settings_current WHERE variable = 'platform_charset';";
  103. $result = Database::query($sql, __FILE__, __LINE__);
  104. while ($row = @Database::fetch_array($result)) {
  105. $charset = $row[0];
  106. }
  107. if (empty($charset)) {
  108. $charset = 'ISO-8859-15';
  109. }
  110. // Preserving the value of the global variable $charset.
  111. $charset_initial_value = $charset;
  112. // Initialization of the internationalization library.
  113. api_initialize_internationalization();
  114. // Initialization of the default encoding that will be used by the multibyte string routines in the internationalization library.
  115. api_set_internationalization_default_encoding($charset);
  116. /*
  117. --------------------------------------------
  118. RETRIEVING ALL THE DOKEOS CONFIG SETTINGS
  119. --------------------------------------------
  120. */
  121. if (!empty($_configuration['multiple_access_urls'])) {
  122. $_configuration['access_url'] = 1;
  123. $access_urls = api_get_access_urls();
  124. $protocol = ((!empty($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS']) != 'OFF') ? 'https' : 'http').'://';
  125. $request_url1 = $protocol.$_SERVER['SERVER_NAME'].'/';
  126. $request_url2 = $protocol.$_SERVER['HTTP_HOST'].'/';
  127. foreach ($access_urls as $details) {
  128. if ($request_url1 == $details['url'] or $request_url2 == $details['url']) {
  129. $_configuration['access_url'] = $details['id'];
  130. }
  131. }
  132. } else {
  133. $_configuration['access_url'] = 1;
  134. }
  135. // access_url == 1 is the default dokeos location
  136. if ($_configuration['access_url'] != 1) {
  137. $url_info = api_get_access_url($_configuration['access_url']);
  138. if ($url_info['active'] == 1) {
  139. $settings_by_access = api_get_settings(null, 'list', $_configuration['access_url'], 1);
  140. foreach ($settings_by_access as $row) {
  141. if (empty($row['variable'])) {
  142. $row['variable'] = 0;
  143. }
  144. if (empty($row['subkey'])) {
  145. $row['subkey'] = 0;
  146. }
  147. if (empty($row['category'])) {
  148. $row['category'] = 0;
  149. }
  150. $settings_by_access_list[$row['variable']][$row['subkey']][$row['category']] = $row;
  151. }
  152. }
  153. }
  154. $result = api_get_settings(null, 'list', 1);
  155. foreach ($result as $row) {
  156. if ($_configuration['access_url'] != 1) {
  157. if ($url_info['active'] == 1) {
  158. $var = empty($row['variable']) ? 0 : $row['variable'];
  159. $subkey = empty($row['subkey']) ? 0 : $row['subkey'];
  160. $category = empty($row['category']) ? 0 : $row['category'];
  161. }
  162. if ($row['access_url_changeable'] == 1 && $url_info['active'] == 1) {
  163. if ($settings_by_access_list[$var][$subkey][$category]['selected_value'] != '') {
  164. if ($row['subkey'] == null) {
  165. $_setting[$row['variable']]= $settings_by_access_list[$var][$subkey][$category]['selected_value'];
  166. } else {
  167. $_setting[$row['variable']][$row['subkey']] = $settings_by_access_list[$var][$subkey][$category]['selected_value'];
  168. }
  169. } else {
  170. if ($row['subkey'] == null) {
  171. $_setting[$row['variable']] = $row['selected_value'];
  172. } else {
  173. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  174. }
  175. }
  176. } else {
  177. if ($row['subkey'] == null) {
  178. $_setting[$row['variable']] = $row['selected_value'];
  179. } else {
  180. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  181. }
  182. }
  183. } else {
  184. if ($row['subkey'] == null) {
  185. $_setting[$row['variable']] = $row['selected_value'];
  186. } else {
  187. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  188. }
  189. }
  190. }
  191. $result = api_get_settings('Plugins', 'list', $_configuration['access_url']);
  192. $_plugins = array();
  193. foreach ($result as $row) {
  194. $key = $row['variable'];
  195. if (is_string($_setting[$key])) {
  196. $_setting[$key] = array();
  197. }
  198. $_setting[$key][] = $row['selected_value'];
  199. $_plugins[$key][] = $row['selected_value'];
  200. }
  201. //load array Kses for Htmlpurifier
  202. require_once $lib_path.'formvalidator/Rule/allowed_tags.inc.php';
  203. //load htmpurifier
  204. require_once $lib_path.'htmlpurifier/library/HTMLPurifier.auto.php';
  205. // include the local (contextual) parameters of this course or section
  206. require $includePath.'/local.inc.php';
  207. // ===== "who is logged in?" module section =====
  208. require_once $lib_path.'online.inc.php';
  209. // check and modify the date of user in the track.e.online table
  210. if (!$x = strpos($_SERVER['PHP_SELF'], 'whoisonline.php')) {
  211. LoginCheck(isset($_user['user_id']) ? $_user['user_id'] : '', $_configuration['statistics_database']);
  212. }
  213. // ===== end "who is logged in?" module section =====
  214. if (api_get_setting('server_type') == 'test') {
  215. /*
  216. --------------------------------------------
  217. Server type is test
  218. - high error reporting level
  219. - only do addslashes on $_GET and $_POST
  220. --------------------------------------------
  221. */
  222. if (IS_PHP_53) {
  223. error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
  224. } else {
  225. error_reporting(E_ALL & ~E_NOTICE);
  226. }
  227. //error_reporting(E_ALL);
  228. //Addslashes to all $_GET variables
  229. foreach ($_GET as $key => $val) {
  230. if (!ini_get('magic_quotes_gpc')) {
  231. if (is_string($val)) {
  232. $_GET[$key] = addslashes($val);
  233. }
  234. }
  235. }
  236. //Addslashes to all $_POST variables
  237. foreach ($_POST as $key => $val) {
  238. if (!ini_get('magic_quotes_gpc')) {
  239. if (is_string($val)) {
  240. $_POST[$key] = addslashes($val);
  241. }
  242. }
  243. }
  244. } else {
  245. /*
  246. --------------------------------------------
  247. Server type is not test
  248. - normal error reporting level
  249. - full fake register globals block
  250. --------------------------------------------
  251. */
  252. error_reporting(E_COMPILE_ERROR | E_ERROR | E_CORE_ERROR);
  253. // TODO: These obsolete variables $HTTP_* to be check whether they are actually used.
  254. if (!isset($HTTP_GET_VARS)) { $HTTP_GET_VARS = $_GET; }
  255. if (!isset($HTTP_POST_VARS)) { $HTTP_POST_VARS = $_POST; }
  256. if (!isset($HTTP_POST_FILES)) { $HTTP_POST_FILES = $_FILES; }
  257. if (!isset($HTTP_SESSION_VARS)) { $HTTP_SESSION_VARS = $_SESSION; }
  258. if (!isset($HTTP_SERVER_VARS)) { $HTTP_SERVER_VARS = $_SERVER; }
  259. // Register SESSION variables into $GLOBALS
  260. if (sizeof($HTTP_SESSION_VARS)) {
  261. if (!is_array($_SESSION)) {
  262. $_SESSION = array();
  263. }
  264. foreach ($HTTP_SESSION_VARS as $key => $val) {
  265. $_SESSION[$key] = $HTTP_SESSION_VARS[$key];
  266. $GLOBALS[$key] = $HTTP_SESSION_VARS[$key];
  267. }
  268. }
  269. // Register SERVER variables into $GLOBALS
  270. if (sizeof($HTTP_SERVER_VARS)) {
  271. $_SERVER = array();
  272. foreach ($HTTP_SERVER_VARS as $key => $val) {
  273. $_SERVER[$key] = $HTTP_SERVER_VARS[$key];
  274. if (!isset($_SESSION[$key]) && $key != 'includePath' && $key != 'rootSys' && $key!= 'clarolineRepositorySys' && $key!= 'lang_path' && $key!= 'extAuthSource' && $key!= 'thisAuthSource' && $key!= 'main_configuration_file_path' && $key!= 'phpDigIncCn' && $key!= 'drs') {
  275. $GLOBALS[$key]=$HTTP_SERVER_VARS[$key];
  276. }
  277. }
  278. }
  279. }
  280. /*
  281. -----------------------------------------------------------
  282. LOAD LANGUAGE FILES SECTION
  283. -----------------------------------------------------------
  284. */
  285. // if we use the javascript version (without go button) we receive a get
  286. // if we use the non-javascript version (with the go button) we receive a post
  287. $user_language = '';
  288. if (!empty($_GET['language'])) {
  289. $user_language = $_GET['language'];
  290. }
  291. if (!empty($_POST['language_list'])) {
  292. $user_language = str_replace('index.php?language=', '', $_POST['language_list']);
  293. }
  294. // Include all files (first english and then current interface language)
  295. $langpath = api_get_path(SYS_CODE_PATH).'lang/';
  296. /* This will only work if we are in the page to edit a sub_language */
  297. 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') {
  298. require_once '../admin/sub_language.class.php';
  299. // getting the arrays of files i.e notification, trad4all, etc
  300. $language_files_to_load = SubLanguageManager::get_all_data_of_dokeos_folder(api_get_path(SYS_LANG_PATH).'english', true);
  301. //getting parent info
  302. $parent_language = SubLanguageManager::get_all_information_of_language(intval($_REQUEST['id']));
  303. //getting sub language info
  304. $sub_language = SubLanguageManager::get_all_information_of_language(intval($_REQUEST['sub_language_id']));
  305. $english_language_array = $parent_language_array = $sub_language_array = array();
  306. foreach ($language_files_to_load as $language_file_item) {
  307. $lang_list_pre = array_keys($GLOBALS);
  308. include $langpath.'english/'.$language_file_item.'.inc.php'; //loading english
  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 (is_file($parent_file)) {
  320. include $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. $sub_file = $langpath.$sub_language['dokeos_folder'].'/'.$language_file_item.'.inc.php';
  329. if (is_file($sub_file)) {
  330. include $sub_file;
  331. }
  332. // ------ sub language array
  333. $sub_language_array[$language_file_item] = compact($lang_list_result);
  334. //cleaning the variables
  335. foreach($lang_list_result as $item) {
  336. unset(${$item});
  337. }
  338. }
  339. }
  340. // Checking if we have a valid language. If not we set it to the platform language.
  341. $valid_languages = api_get_languages();
  342. if (!in_array($user_language, $valid_languages['folder'])) {
  343. $user_language = api_get_setting('platformLanguage');
  344. }
  345. if (in_array($user_language, $valid_languages['folder']) && (isset($_GET['language']) || isset($_POST['language_list']))) {
  346. $user_selected_language = $user_language; // $_GET['language'];
  347. $_SESSION['user_language_choice'] = $user_selected_language;
  348. $platformLanguage = $user_selected_language;
  349. } else {
  350. $platformLanguage = api_get_setting('platformLanguage');
  351. }
  352. if (isset($_SESSION['user_language_choice'])) {
  353. $language_interface = $_SESSION['user_language_choice'];
  354. } else {
  355. $language_interface = api_get_setting('platformLanguage');
  356. }
  357. if (isset($_user['language'])) {
  358. $language_interface = $_user['language'];
  359. }
  360. if ($_course['language']) {
  361. $language_interface = $_course['language'];
  362. }
  363. // Sometimes the variable $language_interface is changed
  364. // temporarily for achieving translation in different language.
  365. // We need to save the genuine value of this variable and
  366. // to use it within the function get_lang(...).
  367. $language_interface_initial_value = $language_interface;
  368. /*
  369. * Include all necessary language files
  370. * - trad4all
  371. * - notification
  372. * - custom tool language files
  373. */
  374. $language_files = array();
  375. $language_files[] = 'trad4all';
  376. $language_files[] = 'notification';
  377. $language_files[] = 'accessibility';
  378. if (isset($language_file)) {
  379. if (!is_array($language_file)) {
  380. $language_files[] = $language_file;
  381. } else {
  382. $language_files = array_merge($language_files, $language_file);
  383. }
  384. }
  385. if (is_array($language_files)) {
  386. if (api_get_setting('allow_use_sub_language') == 'true') {
  387. foreach ($language_files as $index => $language_file) {
  388. include $langpath.'english/'.$language_file.'.inc.php';
  389. $langfile = $langpath.$language_interface.'/'.$language_file.'.inc.php';
  390. $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
  391. $sql_sub_language = 'SELECT dokeos_folder FROM '.$tbl_admin_languages.' WHERE parent_id=(SELECT id FROM '.$tbl_admin_languages.' WHERE dokeos_folder="'.Database::escape_string($language_interface).'" AND ISNULL(parent_id))';
  392. $rs_sub_language = Database::query($sql_sub_language,__FILE__,__LINE__);
  393. $num_row_sub_language = Database::num_rows($rs_sub_language);
  394. if (file_exists($langfile)) {
  395. include $langfile;
  396. for ($i = 0; $i < $num_row_sub_language; $i++) {
  397. $row_sub_language = Database::result($rs_sub_language, $i, 'dokeos_folder');
  398. $sub_langfile = $langpath.$row_sub_language.'/'.$language_file.'.inc.php';
  399. if (file_exists($sub_langfile)) {
  400. include $sub_langfile;
  401. }
  402. }
  403. }
  404. }
  405. } else {
  406. foreach ($language_files as $index => $language_file) {
  407. include $langpath.'english/'.$language_file.'.inc.php';
  408. $langfile = $langpath.$language_interface.'/'.$language_file.'.inc.php';
  409. if (file_exists($langfile)) {
  410. include $langfile;
  411. }
  412. }
  413. }
  414. }
  415. // The global variable $charset has been defined in a language file too (trad4all.inc.php), this is legacy situation.
  416. // So, we have to reassign this variable again in order to keep its value right.
  417. $charset = $charset_initial_value;
  418. //Update of the logout_date field in the table track_e_login (needed for the calculation of the total connection time)
  419. if ($_configuration['tracking_enabled'] && !isset($_SESSION['login_as']) && isset($_user)) {
  420. // if $_SESSION['login_as'] is set, then the user is an admin logged as the user
  421. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  422. $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";
  423. $q_last_connection = Database::query($sql_last_connection);
  424. if (Database::num_rows($q_last_connection) > 0) {
  425. $i_id_last_connection = Database::result($q_last_connection, 0, 'login_id');
  426. $s_sql_update_logout_date = "UPDATE $tbl_track_login SET logout_date=NOW() WHERE login_id='$i_id_last_connection'";
  427. Database::query($s_sql_update_logout_date);
  428. }
  429. }