global.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. // Determine the directory path where this current file lies
  24. // This path will be useful to include the other intialisation files
  25. $includePath = dirname(__FILE__);
  26. // @todo isn't this file renamed to configuration.inc.php yet?
  27. // include the main Dokeos platform configuration file
  28. $main_configuration_file_path = $includePath . "/conf/configuration.php";
  29. $already_installed = false;
  30. if(file_exists($main_configuration_file_path))
  31. {
  32. require_once($main_configuration_file_path);
  33. $already_installed = true;
  34. }
  35. else
  36. {
  37. $_configuration = array();
  38. }
  39. // include the main Dokeos platform library file
  40. require_once($includePath.'/lib/main_api.lib.php');
  41. //fix bug in IIS that doesn't fill the $_SERVER['REQUEST_URI']
  42. api_request_uri();
  43. // Start session
  44. api_session_start($already_installed);
  45. $error_message_not_installed = <<<EOM
  46. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  47. <html>
  48. <head>
  49. <title>Dokeos not installed!</title>
  50. <style type="text/css" media="screen, projection">
  51. /*<![CDATA[*/
  52. @import "main/css/public_admin/default.css";
  53. /*]]>*/
  54. </style>
  55. </head>
  56. <body>
  57. <div id="header">
  58. <div id="header1"><a href="http://www.dokeos.com">Dokeos Homepage</a></div>
  59. <div class="clear"></div>
  60. <div id="header2">&nbsp;</div>
  61. <div id="header3">&nbsp;</div>
  62. </div>
  63. <div style="text-align: center;"><br /><br />
  64. <form action="main/install/index.php" method="get"><button class="save" type="submit" value="&nbsp;&nbsp; Click to INSTALL DOKEOS &nbsp;&nbsp;" >Click to INSTALL DOKEOS</button></form><br />
  65. or <a href="documentation/installation_guide.html" target="_blank">read the installation guide</a><br /><br />
  66. </div>
  67. <div id="footer">
  68. <div class="copyright">Platform <a href="http://www.dokeos.com"> Dokeos </a> &copy; 2008 </div>
  69. &nbsp;
  70. </div>
  71. </body>
  72. </html>
  73. EOM;
  74. $error_message_db_problem = <<<EOM
  75. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  76. <html>
  77. <head>
  78. <title>Dokeos database unavailable!</title>
  79. <style type="text/css" media="screen, projection">
  80. /*<![CDATA[*/
  81. @import "main/css/public_admin/default.css";
  82. /*]]>*/
  83. </style>
  84. </head>
  85. <body>
  86. <div id="header">
  87. <div id="header1"><a href="http://www.dokeos.com">Dokeos Homepage</a></div>
  88. <div class="clear"></div>
  89. <div id="header2">&nbsp;</div>
  90. <div id="header3">&nbsp;</div>
  91. </div>
  92. EOM;
  93. $error_message_db_problem .= '
  94. <div style="text-align: center; font-size: large; margin-bottom: 2em;"><br /><br />
  95. This portal is currently experiencing database issues. Please report this to the portal administrator. Thank you for your help.</a>
  96. </div>
  97. <div id="footer">
  98. <div class="copyright">Platform <a href="http://www.dokeos.com"> Dokeos </a> &copy; 2008 </div>
  99. &nbsp;
  100. </div>
  101. </body>
  102. </html>';
  103. if (!$already_installed)
  104. {
  105. //require('installedVersion.inc.php');
  106. die($error_message_not_installed);
  107. }
  108. //Assigning a variable to avoid several useless calls to the database setting.
  109. // Do not over-user. This is only for this script's local use.
  110. $lib_path = api_get_path(LIBRARY_PATH);
  111. // Add the path to the pear packages to the include path
  112. //ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.$lib_path.'pear');
  113. ini_set('include_path', api_create_include_path_setting());
  114. // This is for compatibility with MAC computers.
  115. ini_set('auto_detect_line_endings', '1');
  116. // Include the libraries that are necessary everywhere
  117. require_once($lib_path.'database.lib.php');
  118. require_once($lib_path.'display.lib.php');
  119. require_once($lib_path.'text.lib.php');
  120. require_once($lib_path.'security.lib.php');
  121. // @todo: this shouldn't be done here. It should be stored correctly during installation
  122. if(empty($_configuration['statistics_database']) && $already_installed)
  123. {
  124. $_configuration['statistics_database'] = $_configuration['main_database'];
  125. }
  126. // connect to the server database and select the main dokeos database
  127. $dokeos_database_connection = @mysql_connect($_configuration['db_host'], $_configuration['db_user'], $_configuration['db_password']) or die ($error_message_db_problem);
  128. if (! $_configuration['db_host'])
  129. {
  130. die($error_message_db_problem);
  131. }
  132. unset($error_message_db_problem);
  133. unset($error_message_not_installed);
  134. // The Dokeos system has not been designed to use special SQL modes that were introduced since MySQL 5
  135. api_sql_query("set session sql_mode='';", __FILE__, __LINE__);
  136. $selectResult = mysql_select_db($_configuration['main_database'],$dokeos_database_connection) or die ('<center>WARNING ! SYSTEM UNABLE TO SELECT THE MAIN DOKEOS DATABASE</center>');
  137. /*
  138. --------------------------------------------
  139. Initialization of the default encodings
  140. --------------------------------------------
  141. */
  142. // The platform's character set must be retrieved at this early moment.
  143. $sql = "SELECT selected_value FROM settings_current WHERE variable = 'platform_charset';";
  144. $result = api_sql_query($sql, __FILE__, __LINE__);
  145. while ($row = @mysql_fetch_array($result)) {
  146. $charset = $row[0];
  147. }
  148. if (empty($charset)) {
  149. $charset = "ISO-8859-15";
  150. }
  151. api_mb_internal_encoding($charset);
  152. api_mb_regex_encoding($charset);
  153. api_iconv_set_encoding('iconv_internal_encoding', $charset);
  154. api_iconv_set_encoding('iconv_input_encoding', $charset);
  155. api_iconv_set_encoding('iconv_output_encoding', $charset);
  156. /*
  157. --------------------------------------------
  158. RETRIEVING ALL THE DOKEOS CONFIG SETTINGS
  159. --------------------------------------------
  160. */
  161. if(!empty($_configuration['multiple_access_urls']))
  162. {
  163. $_configuration['access_url'] = 1;
  164. $access_urls = api_get_access_urls();
  165. $protocol = ((!empty($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS'])!='OFF')?'https':'http').'://';
  166. $request_url1 = $protocol.$_SERVER['SERVER_NAME'].'/';
  167. $request_url2 = $protocol.$_SERVER['HTTP_HOST'].'/';
  168. foreach($access_urls as $details)
  169. {
  170. if($request_url1 == $details['url'] or $request_url2 == $details['url'])
  171. {
  172. $_configuration['access_url'] = $details['id'];
  173. }
  174. }
  175. }
  176. else
  177. {
  178. $_configuration['access_url'] = 1;
  179. }
  180. //$sql="SELECT * FROM settings_current";
  181. //$result=mysql_query($sql) or die(mysql_error());
  182. // access_url == 1 is the default dokeos location
  183. if ($_configuration['access_url']!=1)
  184. {
  185. $url_info = api_get_access_url($_configuration['access_url']);
  186. if ($url_info['active']==1)
  187. {
  188. $settings_by_access = api_get_settings(null,'list',$_configuration['access_url'],1);
  189. foreach($settings_by_access as $row)
  190. {
  191. if (empty($row['variable']))
  192. $row['variable']=0;
  193. if (empty($row['subkey']))
  194. $row['subkey']=0;
  195. if (empty($row['category']))
  196. $row['category']=0;
  197. $settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ] = $row;
  198. }
  199. }
  200. }
  201. //echo '<pre>';print_r($result_other_site);
  202. $result = api_get_settings(null,'list',1);
  203. //while ($row=mysql_fetch_array($result))
  204. foreach($result as $row)
  205. {
  206. if ($_configuration['access_url']!=1)
  207. {
  208. if ($url_info['active']==1)
  209. {
  210. if (empty($row['variable']))
  211. $var=0;
  212. else
  213. $var=$row['variable'];
  214. if (empty($row['subkey']))
  215. $subkey=0;
  216. else
  217. $subkey=$row['subkey'];
  218. if (empty($row['category']))
  219. $category=0;
  220. else
  221. $category=$row['category'];
  222. }
  223. if ($row['access_url_changeable']==1 && $url_info['active']==1)
  224. {
  225. if ($settings_by_access_list[ $var ] [ $subkey ] [$category ]['selected_value'] !='')
  226. {
  227. if ($row['subkey']==NULL)
  228. {
  229. $_setting[$row['variable']]= $settings_by_access_list[ $var ] [ $subkey ] [$category ]['selected_value'];
  230. }
  231. else
  232. {
  233. $_setting[$row['variable']][$row['subkey']]=$settings_by_access_list[ $var ] [ $subkey ] [$category ]['selected_value'];
  234. }
  235. }
  236. else
  237. {
  238. if ($row['subkey']==NULL)
  239. {
  240. $_setting[$row['variable']]=$row['selected_value'];
  241. }
  242. else
  243. {
  244. $_setting[$row['variable']][$row['subkey']]=$row['selected_value'];
  245. }
  246. }
  247. }
  248. else
  249. {
  250. if ($row['subkey']==NULL)
  251. {
  252. $_setting[$row['variable']]=$row['selected_value'];
  253. }
  254. else
  255. {
  256. $_setting[$row['variable']][$row['subkey']]=$row['selected_value'];
  257. }
  258. }
  259. }
  260. else
  261. {
  262. if ($row['subkey']==NULL)
  263. {
  264. $_setting[$row['variable']]=$row['selected_value'];
  265. }
  266. else
  267. {
  268. $_setting[$row['variable']][$row['subkey']]=$row['selected_value'];
  269. }
  270. }
  271. }
  272. //echo '<pre>';print_r($_setting);echo '</pre>';
  273. // we have to store the settings for the plugins differently because it expects an array
  274. //$sql="SELECT * FROM settings_current WHERE category='plugins'";
  275. //$result=mysql_query($sql) or die(mysql_error());
  276. $result = api_get_settings('Plugins','list',$_configuration['access_url']);
  277. $_plugins=array();
  278. //while ($row=mysql_fetch_array($result))
  279. foreach($result as $row)
  280. {
  281. $key= $row['variable'];
  282. if (is_string($_setting[$key]))
  283. {
  284. $_setting[$key]=array();
  285. }
  286. $_setting[$key][]=$row['selected_value'];
  287. $_plugins[$key][]=$row['selected_value'];
  288. }
  289. // include the local (contextual) parameters of this course or section
  290. require($includePath."/local.inc.php");
  291. // ===== "who is logged in?" module section =====
  292. require_once($includePath."/lib/online.inc.php");
  293. // check and modify the date of user in the track.e.online table
  294. if (!$x=strpos($_SERVER['PHP_SELF'],'whoisonline.php'))
  295. {
  296. LoginCheck(isset($_user['user_id']) ? $_user['user_id'] : '',$_configuration['statistics_database']);
  297. }
  298. //load array Kses for Htmlpurifier
  299. require_once $includePath."/lib/formvalidator/Rule/allowed_tags.inc.php";
  300. //load htmpurifier
  301. require_once $includePath."/lib/htmlpurifier/library/HTMLPurifier.auto.php";
  302. global $config_purifier;
  303. $charset = api_get_setting('platform_charset');
  304. $config_purifier = HTMLPurifier_Config::createDefault();
  305. $config_purifier->set('Core', 'Encoding',$charset);
  306. $config_purifier->set('HTML', 'Doctype', 'XHTML 1.0 Transitional');
  307. // ===== end "who is logged in?" module section =====
  308. if(get_setting('server_type') == 'test')
  309. {
  310. /*
  311. --------------------------------------------
  312. Server type is test
  313. - high error reporting level
  314. - only do addslashes on $_GET and $_POST
  315. --------------------------------------------
  316. */
  317. error_reporting(E_ALL & ~E_NOTICE);
  318. //error_reporting(E_ALL);
  319. //Addslashes to all $_GET variables
  320. foreach($_GET as $key=>$val)
  321. {
  322. if(!ini_get('magic_quotes_gpc'))
  323. {
  324. if(is_string($val))
  325. {
  326. $_GET[$key]=addslashes($val);
  327. }
  328. }
  329. }
  330. //Addslashes to all $_POST variables
  331. foreach($_POST as $key=>$val)
  332. {
  333. if(!ini_get('magic_quotes_gpc'))
  334. {
  335. if(is_string($val))
  336. {
  337. $_POST[$key]=addslashes($val);
  338. }
  339. }
  340. }
  341. }
  342. else
  343. {
  344. /*
  345. --------------------------------------------
  346. Server type is not test
  347. - normal error reporting level
  348. - full fake register globals block
  349. --------------------------------------------
  350. */
  351. error_reporting(E_COMPILE_ERROR | E_ERROR | E_CORE_ERROR);
  352. if(!isset($HTTP_GET_VARS)) { $HTTP_GET_VARS=$_GET; }
  353. if(!isset($HTTP_POST_VARS)) { $HTTP_POST_VARS=$_POST; }
  354. if(!isset($HTTP_POST_FILES)) { $HTTP_POST_FILES=$_FILES; }
  355. if(!isset($HTTP_SESSION_VARS)) { $HTTP_SESSION_VARS=$_SESSION; }
  356. if(!isset($HTTP_SERVER_VARS)) { $HTTP_SERVER_VARS=$_SERVER; }
  357. // Register SESSION variables into $GLOBALS
  358. if(sizeof($HTTP_SESSION_VARS))
  359. {
  360. if(!is_array($_SESSION))
  361. {
  362. $_SESSION=array();
  363. }
  364. foreach($HTTP_SESSION_VARS as $key=>$val)
  365. {
  366. $_SESSION[$key]=$HTTP_SESSION_VARS[$key];
  367. $GLOBALS[$key]=$HTTP_SESSION_VARS[$key];
  368. }
  369. }
  370. // Register SERVER variables into $GLOBALS
  371. if(sizeof($HTTP_SERVER_VARS))
  372. {
  373. $_SERVER=array();
  374. foreach($HTTP_SERVER_VARS as $key=>$val)
  375. {
  376. $_SERVER[$key]=$HTTP_SERVER_VARS[$key];
  377. 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')
  378. {
  379. $GLOBALS[$key]=$HTTP_SERVER_VARS[$key];
  380. }
  381. }
  382. }
  383. }
  384. /*
  385. -----------------------------------------------------------
  386. LOAD LANGUAGE FILES SECTION
  387. -----------------------------------------------------------
  388. */
  389. // if we use the javascript version (without go button) we receive a get
  390. // if we use the non-javascript version (with the go button) we receive a post
  391. $user_language = '';
  392. if(!empty($_GET['language']))
  393. {
  394. $user_language = $_GET["language"];
  395. }
  396. if (!empty($_POST["language_list"]))
  397. {
  398. $user_language = str_replace("index.php?language=","",$_POST["language_list"]);
  399. }
  400. // Checking if we have a valid language. If not we set it to the platform language.
  401. $valid_languages=api_get_languages();
  402. if (!in_array($user_language,$valid_languages['folder']))
  403. {
  404. $user_language=get_setting('platformLanguage');
  405. }
  406. if (in_array($user_language,$valid_languages['folder']) and (isset($_GET['language']) OR isset($_POST['language_list'])))
  407. {
  408. $user_selected_language = $user_language; // $_GET["language"];
  409. $_SESSION["user_language_choice"] = $user_selected_language;
  410. $platformLanguage = $user_selected_language;
  411. }else{
  412. $platformLanguage = get_setting('platformLanguage');
  413. }
  414. if (isset($_SESSION["user_language_choice"]))
  415. {
  416. $language_interface = $_SESSION["user_language_choice"];
  417. }
  418. else
  419. {
  420. $language_interface = get_setting('platformLanguage');
  421. }
  422. if (isset($_user['language']))
  423. {
  424. $language_interface = $_user['language'];
  425. }
  426. if ($_course['language'])
  427. {
  428. $language_interface = $_course['language'];
  429. }
  430. // Sometimes the variable $language_interface is changed
  431. // temporarily for achieving translation in different language.
  432. // We need to save the genuine value of this variable and
  433. // to use it within the function get_lang(...).
  434. $language_interface_initial_value = $language_interface;
  435. /*
  436. * Include all necessary language files
  437. * - trad4all
  438. * - notification
  439. * - custom tool language files
  440. */
  441. $language_files = array();
  442. $language_files[] = 'trad4all';
  443. $language_files[] = 'notification';
  444. $language_files[] = 'accessibility';
  445. if( isset($language_file) )
  446. {
  447. if( !is_array($language_file))
  448. {
  449. $language_files[] = $language_file;
  450. }
  451. else
  452. {
  453. $language_files = array_merge($language_files,$language_file);
  454. }
  455. }
  456. // Include all files (first english and then current interface language)
  457. $langpath = api_get_path(SYS_CODE_PATH).'lang/';
  458. if (is_array($language_files)) {
  459. foreach($language_files as $index => $language_file) {
  460. include($langpath.'english/'.$language_file.'.inc.php');
  461. $langfile = $langpath.$language_interface.'/'.$language_file.'.inc.php';
  462. if (file_exists($langfile)) {
  463. include($langfile);
  464. }
  465. }
  466. }
  467. //load the charset param after langs because the $charset variable in
  468. //trad4all.inc.php might have set it and we don't want that
  469. $charset = api_get_setting('platform_charset');
  470. if (empty($charset)) {
  471. $charset = 'ISO-8859-15';
  472. }
  473. //Update of the logout_date field in the table track_e_login (needed for the calculation of the total connection time)
  474. if($_configuration['tracking_enabled'] && !isset($_SESSION['login_as']) && isset($_user))
  475. { // if $_SESSION['login_as'] is set, then the user is an admin logged as the user
  476. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  477. $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";
  478. $q_last_connection=api_sql_query($sql_last_connection);
  479. if(Database::num_rows($q_last_connection) > 0)
  480. {
  481. $i_id_last_connection=Database::result($q_last_connection,0,"login_id");
  482. $s_sql_update_logout_date="UPDATE $tbl_track_login SET logout_date=NOW() WHERE login_id='$i_id_last_connection'";
  483. api_sql_query($s_sql_update_logout_date);
  484. }
  485. }
  486. ?>