local.inc.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. *
  5. * SCRIPT PURPOSE
  6. *
  7. * This script initializes and manages Chamilo session information. It
  8. * keeps available session information up to date.
  9. *
  10. * You can request a course id. It will check if the course Id requested is the
  11. * same as the current one. If it isn't it will update session information from
  12. * the database. You can also force the course reset if you want ($cidReset).
  13. *
  14. * All the course information is stored in the $_course array.
  15. *
  16. * You can request a group id. The script will check if the group id requested is the
  17. * same as the current one. If it isn't it will update session information from
  18. * the database. You can also force the course reset if you want ($gidReset).
  19. *
  20. * The course id is stored in $_cid session variable.
  21. * The group id is stored in $_gid session variable.
  22. *
  23. *
  24. * VARIABLES AFFECTING THE SCRIPT BEHAVIOR
  25. *
  26. * string $login
  27. * string $password
  28. * boolean $logout
  29. *
  30. * string $cidReq : course id requested
  31. * boolean $cidReset : ask for a course Reset, if no $cidReq is provided in the
  32. * same time, all course informations is removed from the
  33. * current session
  34. *
  35. * int $gidReq : group Id requested
  36. * boolean $gidReset : ask for a group Reset, if no $gidReq is provided in the
  37. * same time, all group informations is removed from the
  38. * current session
  39. *
  40. *
  41. * VARIABLES SET AND RETURNED BY THE SCRIPT
  42. *
  43. * All the variables below are set and returned by this script.
  44. *
  45. * USER VARIABLES
  46. *
  47. * string $_user ['firstName' ]
  48. * string $_user ['lastName' ]
  49. * string $_user ['mail' ]
  50. * string $_user ['lastLogin' ]
  51. * string $_user ['official_code']
  52. * string $_user ['picture_uri' ]
  53. * string $_user['user_id']
  54. *
  55. * boolean $is_platformAdmin
  56. * boolean $is_allowedCreateCourse
  57. *
  58. * COURSE VARIABLES
  59. *
  60. * string $_cid (the course id)
  61. *
  62. * int $_course['id' ] - auto-assigned integer
  63. * string $_course['name' ] - the title of the course
  64. * string $_course['official_code'] - the visual / fake / official code
  65. * string $_course['sysCode' ]
  66. * string $_course['path' ]
  67. * string $_course['dbName' ]
  68. * string $_course['dbNameGlu' ]
  69. * string $_course['titular' ]
  70. * string $_course['language' ]
  71. * string $_course['extLink' ]['url' ]
  72. * string $_course['extLink' ]['name']
  73. * string $_course['categoryCode']
  74. * string $_course['categoryName']
  75. * boolean $is_courseMember
  76. * boolean $is_courseTutor
  77. * boolean $is_courseAdmin
  78. *
  79. *
  80. * GROUP VARIABLES
  81. *
  82. * int $_gid (the group id)
  83. *
  84. *
  85. * IMPORTANT ADVICE FOR DEVELOPERS
  86. *
  87. * We strongly encourage developers to use a connection layer at the top of
  88. * their scripts rather than use these variables, as they are, inside the core
  89. * of their scripts. It will make code maintenance much easier.
  90. *
  91. * Many if the functions you need you can already find in the
  92. * main_api.lib.php
  93. *
  94. * We encourage you to use functions to access these global "kernel" variables.
  95. * You can add them to e.g. the main API library.
  96. *
  97. *
  98. * SCRIPT STRUCTURE
  99. *
  100. * 1. The script determines if there is an authentication attempt. This part
  101. * only chek if the login name and password are valid. Afterwards, it set the
  102. * $_user['user_id'] (user id) and the $uidReset flag. Other user informations are retrieved
  103. * later. It's also in this section that optional external authentication
  104. * devices step in.
  105. *
  106. * 2. The script determines what other session informations have to be set or
  107. * reset, setting correctly $cidReset (for course) and $gidReset (for group).
  108. *
  109. * 3. If needed, the script retrieves the other user informations (first name,
  110. * last name, ...) and stores them in session.
  111. *
  112. * 4. If needed, the script retrieves the course information and stores them
  113. * in session
  114. *
  115. * 5. The script initializes the user permission status and permission for the
  116. * course level
  117. *
  118. * 6. If needed, the script retrieves group informations an store them in
  119. * session.
  120. *
  121. * 7. The script initializes the user status and permission for the group level.
  122. *
  123. * @package chamilo.include
  124. */
  125. /*
  126. INIT SECTION
  127. variables should be initialised here
  128. */
  129. require_once (api_get_path(LIBRARY_PATH).'conditionallogin.lib.php');
  130. // verified if exists the username and password in session current
  131. if (isset($_SESSION['info_current_user'][1]) && isset($_SESSION['info_current_user'][2])) {
  132. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  133. require_once api_get_path(LIBRARY_PATH).'legal.lib.php';
  134. }
  135. //Conditional login
  136. if (isset($_SESSION['conditional_login']['uid']) && $_SESSION['conditional_login']['can_login']=== true){
  137. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  138. $uData = UserManager::get_user_info_by_id($_SESSION['conditional_login']['uid']);
  139. ConditionalLogin::check_conditions($uData);
  140. $_user['user_id'] = $_SESSION['conditional_login']['uid'];
  141. api_session_register('_user');
  142. api_session_unregister('conditional_login');
  143. $uidReset=true;
  144. event_login();
  145. }
  146. // parameters passed via GET
  147. $logout = isset($_GET["logout"]) ? $_GET["logout"] : '';
  148. $gidReq = isset($_GET["gidReq"]) ? Database::escape_string($_GET["gidReq"]) : '';
  149. //this fixes some problems with generic functionalities like
  150. //My Agenda & What's New icons linking to courses
  151. // $cidReq can be set in the index.php file of a course-area
  152. $cidReq = isset($cidReq) ? Database::escape_string($cidReq) : '';
  153. // $cidReq can be set in URL-parameter
  154. $cidReq = isset($_GET["cidReq"]) ? Database::escape_string($_GET["cidReq"]) : $cidReq;
  155. $cidReset = isset($cidReset) ? Database::escape_string($cidReset) : '';
  156. // $cidReset can be set in URL-parameter
  157. $cidReset = (isset($_GET['cidReq']) && ((isset($_SESSION['_cid']) && $_GET['cidReq']!=$_SESSION['_cid']) || (!isset($_SESSION['_cid'])))) ? Database::escape_string($_GET["cidReq"]) : $cidReset;
  158. // $cDir is a special url param sent by courses/.htaccess
  159. $cDir = (!empty($_GET['cDir']) ? $_GET['cDir'] : null);
  160. $gidReset = isset($gidReset) ? $gidReset : '';
  161. // $gidReset can be set in URL-parameter
  162. // parameters passed via POST
  163. $login = isset($_POST["login"]) ? $_POST["login"] : '';
  164. // passed through other means
  165. //$cidReq -- passed from course folder index.php
  166. /*
  167. MAIN CODE
  168. */
  169. if (!empty($_SESSION['_user']['user_id']) && ! ($login || $logout)) {
  170. // uid is in session => login already done, continue with this value
  171. $_user['user_id'] = $_SESSION['_user']['user_id'];
  172. } else {
  173. if (isset($_user['user_id'])) {
  174. unset($_user['user_id']);
  175. }
  176. //$_SESSION['info_current_user'][1] is user name
  177. //$_SESSION['info_current_user'][2] is current password encrypted
  178. //$_SESSION['update_term_and_condition'][1] is current user id, of user in session
  179. if (api_get_setting('allow_terms_conditions')=='true') {
  180. if (isset($_POST['login']) && isset($_POST['password']) && isset($_SESSION['update_term_and_condition'][1])) {
  181. $user_id=$_SESSION['update_term_and_condition'][1]; // user id
  182. // update the terms & conditions
  183. //verify type of terms and conditions
  184. $info_legal = explode(':',$_POST['legal_info']);
  185. $legal_type = LegalManager::get_type_of_terms_and_conditions($info_legal[0],$info_legal[1]);
  186. //is necessary verify check
  187. if ($legal_type==1) {
  188. if ((isset($_POST['legal_accept']) && $_POST['legal_accept']=='1')) {
  189. $legal_option=true;
  190. } else {
  191. $legal_option=false;
  192. }
  193. }
  194. //no is check option
  195. if ($legal_type==0) {
  196. $legal_option=true;
  197. }
  198. if (isset($_POST['legal_accept_type']) && $legal_option===true) {
  199. $cond_array = explode(':',$_POST['legal_accept_type']);
  200. if (!empty($cond_array[0]) && !empty($cond_array[1])){
  201. $time = time();
  202. $condition_to_save = intval($cond_array[0]).':'.intval($cond_array[1]).':'.$time;
  203. UserManager::update_extra_field_value($user_id,'legal_accept',$condition_to_save);
  204. }
  205. }
  206. }
  207. }
  208. //IF cas is activated and user isn't logged in
  209. if (api_get_setting('cas_activate') == 'true') {
  210. $cas_activated = true;
  211. } else {
  212. $cas_activated = false;
  213. }
  214. $cas_login=false;
  215. if ($cas_activated AND !isset($_user['user_id']) and !isset($_POST['login']) && !$logout) {
  216. require_once(api_get_path(SYS_PATH).'main/auth/cas/authcas.php');
  217. $cas_login = cas_is_authenticated();
  218. }
  219. if ( ( isset($_POST['login']) AND isset($_POST['password']) ) OR ($cas_login) ) {
  220. // $login && $password are given to log in
  221. if ( $cas_login && empty($_POST['login']) ) {
  222. $login = $cas_login;
  223. } else {
  224. $login = $_POST['login'];
  225. $password = $_POST['password'];
  226. }
  227. //lookup the user in the main database
  228. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  229. $sql = "SELECT user_id, username, password, auth_source, active, expiration_date
  230. FROM $user_table
  231. WHERE username = '".Database::escape_string($login)."'";
  232. $result = Database::query($sql);
  233. if (Database::num_rows($result) > 0) {
  234. $uData = Database::fetch_array($result);
  235. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
  236. //the authentification of this user is managed by Chamilo itself
  237. $password = trim(stripslashes($password));
  238. if (api_get_setting('allow_terms_conditions')=='true') {
  239. if (isset($_POST['password']) && isset($_SESSION['info_current_user'][2]) && $_POST['password']==$_SESSION['info_current_user'][2]) {
  240. $password=$_POST['password'];
  241. } else {
  242. $password = api_get_encrypted_password($password);
  243. }
  244. } else {
  245. $password = api_get_encrypted_password($password);
  246. }
  247. if (api_get_setting('allow_terms_conditions')=='true') {
  248. if ($password == $uData['password'] AND (trim($login) == $uData['username']) OR $cas_login ) {
  249. $temp_user_id = $uData['user_id'];
  250. $term_and_condition_status=api_check_term_condition($temp_user_id);//false or true
  251. if ($term_and_condition_status===false) {
  252. $_SESSION['update_term_and_condition']=array(true,$temp_user_id);
  253. $_SESSION['info_current_user']=array(true,$login,$password);
  254. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/inscription.php');
  255. exit;
  256. } else {
  257. unset($_SESSION['update_term_and_condition']);
  258. unset($_SESSION['info_current_user']);
  259. }
  260. }
  261. }
  262. // Check the user's password
  263. if ( ($password == $uData['password'] OR $cas_login) AND (trim($login) == $uData['username'])) {
  264. require_once(api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  265. $update_type = UserManager::get_extra_user_data_by_field($uData['user_id'], 'update_type');
  266. $update_type= $update_type['update_type'];
  267. if (!empty($extAuthSource[$update_type]['updateUser']) && file_exists($extAuthSource[$update_type]['updateUser'])) {
  268. include_once($extAuthSource[$update_type]['updateUser']);
  269. }
  270. // Check if the account is active (not locked)
  271. if ($uData['active']=='1') {
  272. // Check if the expiration date has not been reached
  273. if ($uData['expiration_date'] > date('Y-m-d H:i:s') OR $uData['expiration_date'] == '0000-00-00 00:00:00') {
  274. global $_configuration;
  275. if (isset($_configuration['multiple_access_urls']) && $_configuration['multiple_access_urls']) {
  276. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  277. //Check if user is an admin
  278. $sql = "SELECT user_id FROM $admin_table
  279. WHERE user_id = '".intval($uData['user_id'])."' LIMIT 1";
  280. $result = Database::query($sql);
  281. $my_user_is_admin = false;
  282. if (Database::num_rows($result) > 0) {
  283. $my_user_is_admin = true;
  284. }
  285. // This user is subscribed in these sites => $my_url_list
  286. $my_url_list = api_get_access_url_from_user($uData['user_id']);
  287. //Check the access_url configuration setting if the user is registered in the access_url_rel_user table
  288. //Getting the current access_url_id of the platform
  289. $current_access_url_id = api_get_current_access_url_id();
  290. if ($my_user_is_admin === false) {
  291. if (is_array($my_url_list) && count($my_url_list)>0 ){
  292. // the user have the permissions to enter at this site
  293. if (in_array($current_access_url_id, $my_url_list)) {
  294. ConditionalLogin::check_conditions($uData);
  295. $_user['user_id'] = $uData['user_id'];
  296. session_regenerate_id();
  297. api_session_register('_user');
  298. event_login();
  299. } else {
  300. $loginFailed = true;
  301. api_session_unregister('_uid');
  302. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
  303. exit;
  304. }
  305. } else {
  306. $loginFailed = true;
  307. api_session_unregister('_uid');
  308. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
  309. exit;
  310. }
  311. } else { //Only admins of the "main" (first) Chamilo portal can login wherever they want
  312. //var_dump($current_access_url_id, $my_url_list); exit;
  313. if (in_array(1, $my_url_list)) { //Check if this admin have the access_url_id = 1 which means the principal
  314. ConditionalLogin::check_conditions($uData);
  315. $_user['user_id'] = $uData['user_id'];
  316. session_regenerate_id();
  317. api_session_register('_user');
  318. event_login();
  319. } else {
  320. //This means a secondary admin wants to login so we check as he's a normal user
  321. if (in_array($current_access_url_id, $my_url_list)) {
  322. $_user['user_id'] = $uData['user_id'];
  323. session_regenerate_id();
  324. api_session_register('_user');
  325. event_login();
  326. } else {
  327. $loginFailed = true;
  328. api_session_unregister('_uid');
  329. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
  330. exit;
  331. }
  332. }
  333. }
  334. } else {
  335. ConditionalLogin::check_conditions($uData);
  336. $_user['user_id'] = $uData['user_id'];
  337. session_regenerate_id();
  338. api_session_register('_user');
  339. event_login();
  340. }
  341. } else {
  342. $loginFailed = true;
  343. api_session_unregister('_uid');
  344. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_expired');
  345. exit;
  346. }
  347. } else {
  348. $loginFailed = true;
  349. api_session_unregister('_uid');
  350. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_inactive');
  351. exit;
  352. }
  353. } else {
  354. // login failed: username or password incorrect
  355. $loginFailed = true;
  356. api_session_unregister('_uid');
  357. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=user_password_incorrect');
  358. exit;
  359. }
  360. if (isset($uData['creator_id']) && $_user['user_id'] != $uData['creator_id']) {
  361. //first login for a not self registred
  362. //e.g. registered by a teacher
  363. //do nothing (code may be added later)
  364. }
  365. } elseif (!empty($extAuthSource[$uData['auth_source']]['login']) && file_exists($extAuthSource[$uData['auth_source']]['login'])) {
  366. /*
  367. * Process external authentication
  368. * on the basis of the given login name
  369. */
  370. $loginFailed = true; // Default initialisation. It could
  371. // change after the external authentication
  372. $key = $uData['auth_source']; //'ldap','shibboleth'...
  373. /* >>>>>>>> External authentication modules <<<<<<<<< */
  374. // see configuration.php to define these
  375. include_once($extAuthSource[$key]['login']);
  376. /* >>>>>>>> External authentication modules <<<<<<<<< */
  377. } else { // no standard Chamilo login - try external authentification
  378. //huh... nothing to do... we shouldn't get here
  379. error_log('Chamilo Authentication file '. $extAuthSource[$uData['auth_source']]['login']. ' could not be found - this might prevent your system from doing the corresponding authentication process',0);
  380. }
  381. } else {
  382. // login failed, Database::num_rows($result) <= 0
  383. $loginFailed = true; // Default initialisation. It could
  384. // change after the external authentication
  385. /*
  386. * In this section:
  387. * there is no entry for the $login user in the Chamilo
  388. * database. This also means there is no auth_source for the user.
  389. * We let all external procedures attempt to add him/her
  390. * to the system.
  391. *
  392. * Process external login on the basis
  393. * of the authentication source list
  394. * provided by the configuration settings.
  395. * If the login succeeds, for going further,
  396. * Chamilo needs the $_user['user_id'] variable to be
  397. * set and registered in the session. It's the
  398. * responsability of the external login script
  399. * to provide this $_user['user_id'].
  400. */
  401. if (isset($extAuthSource) && is_array($extAuthSource)) {
  402. foreach($extAuthSource as $thisAuthSource) {
  403. if (!empty($thisAuthSource['newUser']) && file_exists($thisAuthSource['newUser'])) {
  404. include_once($thisAuthSource['newUser']);
  405. } else {
  406. error_log('Chamilo Authentication file '. $thisAuthSource['newUser']. ' could not be found - this might prevent your system from using the authentication process in the user creation process',0);
  407. }
  408. }
  409. } //end if is_array($extAuthSource)
  410. if ($loginFailed) { //If we are here username given is wrong
  411. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=user_password_incorrect');
  412. }
  413. } //end else login failed
  414. } elseif (api_get_setting('sso_authentication')==='true' && !in_array('webservices', explode('/', $_SERVER['REQUEST_URI']))) {
  415. /**
  416. * TODO:
  417. * - Work on a better validation for webservices paths. Current is very poor and exit
  418. */
  419. $subsso = api_get_setting('sso_authentication_subclass');
  420. require_once(api_get_path(SYS_CODE_PATH).'auth/sso/sso.class.php');
  421. if (!empty($subsso)) {
  422. require_once(api_get_path(SYS_CODE_PATH).'auth/sso/sso.'.$subsso.'.class.php');
  423. $subsso = 'sso'.$subsso;
  424. $osso = new $subsso(); //load the subclass
  425. } else {
  426. $osso = new sso();
  427. }
  428. if (isset($_SESSION['_user']['user_id'])) {
  429. if ($logout) {
  430. // Prevent index.php to redirect
  431. global $logout_no_redirect;
  432. $logout_no_redirect = TRUE;
  433. // Make custom redirect after logout
  434. online_logout();
  435. $osso->logout(); //redirects and exits
  436. }
  437. } elseif(!$logout) {
  438. // Handle cookie comming from Master Server
  439. if (!isset($_GET['sso_referer']) && !isset($_GET['loginFailed'])) {
  440. // Redirect to master server
  441. $osso->ask_master();
  442. } elseif (isset($_GET['sso_cookie'])) {
  443. $protocol = api_get_setting('sso_authentication_protocol');
  444. $master_url = api_get_setting('sso_authentication_domain').api_get_setting('sso_authentication_auth_uri');
  445. if (isset($_GET['sso_referer']) ? $_GET['sso_referer'] === $protocol.$master_url : FALSE) {
  446. //make all the process of checking
  447. //if the user exists (delegated to the sso class)
  448. $osso->check_user();
  449. } else {
  450. //Request comes from unknown source
  451. $loginFailed = true;
  452. api_session_unregister('_uid');
  453. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=unrecognize_sso_origin');
  454. exit;
  455. }
  456. }
  457. }//end logout
  458. } elseif (api_get_setting('openid_authentication')=='true') {
  459. if (!empty($_POST['openid_url'])) {
  460. include 'main/auth/openid/login.php';
  461. openid_begin(trim($_POST['openid_url']),api_get_path(WEB_PATH).'index.php');
  462. //this last function should trigger a redirect, so we can die here safely
  463. die('Openid login redirection should be in progress');
  464. } elseif (!empty($_GET['openid_identity'])) {
  465. //it's usual for PHP to replace '.' (dot) by '_' (underscore) in URL parameters
  466. include('main/auth/openid/login.php');
  467. $res = openid_complete($_GET);
  468. if ($res['status'] == 'success') {
  469. $id1 = Database::escape_string($res['openid.identity']);
  470. //have another id with or without the final '/'
  471. $id2 = (substr($id1,-1,1)=='/'?substr($id1,0,-1):$id1.'/');
  472. //lookup the user in the main database
  473. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  474. $sql = "SELECT user_id, username, password, auth_source, active, expiration_date
  475. FROM $user_table
  476. WHERE openid = '$id1'
  477. OR openid = '$id2' ";
  478. $result = Database::query($sql);
  479. if ($result !== false) {
  480. if (Database::num_rows($result)>0) {
  481. //$row = Database::fetch_array($res);
  482. $uData = Database::fetch_array($result);
  483. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
  484. //the authentification of this user is managed by Chamilo itself
  485. // check if the account is active (not locked)
  486. if ($uData['active']=='1') {
  487. // check if the expiration date has not been reached
  488. if ($uData['expiration_date']>date('Y-m-d H:i:s') OR $uData['expiration_date']=='0000-00-00 00:00:00') {
  489. $_user['user_id'] = $uData['user_id'];
  490. session_regenerate_id();
  491. api_session_register('_user');
  492. event_login();
  493. } else {
  494. $loginFailed = true;
  495. api_session_unregister('_uid');
  496. header('Location: index.php?loginFailed=1&error=account_expired');
  497. exit;
  498. }
  499. } else {
  500. $loginFailed = true;
  501. api_session_unregister('_uid');
  502. header('Location: index.php?loginFailed=1&error=account_inactive');
  503. exit;
  504. }
  505. if (isset($uData['creator_id']) && $_user['user_id'] != $uData['creator_id']) {
  506. //first login for a not self registred
  507. //e.g. registered by a teacher
  508. //do nothing (code may be added later)
  509. }
  510. }
  511. } else {
  512. //Redirect to the subscription form
  513. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/inscription.php?username='.$res['openid.sreg.nickname'].'&email='.$res['openid.sreg.email'].'&openid='.$res['openid.identity'].'&openid_msg=idnotfound');
  514. //$loginFailed = true;
  515. }
  516. } else {
  517. $loginFailed = true;
  518. }
  519. } else {
  520. $loginFailed = true;
  521. }
  522. }
  523. }
  524. // else {} => continue as anonymous user
  525. $uidReset = true;
  526. // $cidReset = true;
  527. // $gidReset = true;
  528. } // end else
  529. //Now check for anonymous user mode
  530. if (isset($use_anonymous) && $use_anonymous) {
  531. //if anonymous mode is set, then try to set the current user as anonymous
  532. //if he doesn't have a login yet
  533. api_set_anonymous();
  534. } else {
  535. //if anonymous mode is not set, then check if this user is anonymous. If it
  536. //is, clean it from being anonymous (make him a nobody :-))
  537. api_clear_anonymous();
  538. }
  539. // if there is a cDir parameter in the URL (coming from courses/.htaccess redirection)
  540. if (!empty($cDir)) {
  541. $c = CourseManager::get_course_id_from_path($cDir);
  542. if ($c) { $cidReq = $c; }
  543. }
  544. // if the requested course is different from the course in session
  545. if (!empty($cidReq) && (!isset($_SESSION['_cid']) or (isset($_SESSION['_cid']) && $cidReq != $_SESSION['_cid']))) {
  546. $cidReset = true;
  547. $gidReset = true; // As groups depend from courses, group id is reset
  548. }
  549. // if the requested group is different from the group in session
  550. $gid = isset($_SESSION['_gid'])?$_SESSION['_gid']:'';
  551. if ($gidReq && $gidReq != $gid) {
  552. $gidReset = true;
  553. }
  554. /* USER INIT */
  555. if (isset($uidReset) && $uidReset) { // session data refresh requested
  556. $is_platformAdmin = false;
  557. $is_allowedCreateCourse = false;
  558. if (isset($_user['user_id']) && $_user['user_id']) {
  559. // a uid is given (log in succeeded)
  560. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  561. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  562. $track_e_login = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  563. $sql = "SELECT user.*, a.user_id is_admin, UNIX_TIMESTAMP(login.login_date) login_date
  564. FROM $user_table
  565. LEFT JOIN $admin_table a
  566. ON user.user_id = a.user_id
  567. LEFT JOIN $track_e_login login
  568. ON user.user_id = login.login_user_id
  569. WHERE user.user_id = '".$_user['user_id']."'
  570. ORDER BY login.login_date DESC LIMIT 1";
  571. $result = Database::query($sql);
  572. if (Database::num_rows($result) > 0) {
  573. // Extracting the user data
  574. $uData = Database::fetch_array($result);
  575. $_user ['firstName'] = $uData ['firstname' ];
  576. $_user ['lastName' ] = $uData ['lastname' ];
  577. $_user ['mail' ] = $uData ['email' ];
  578. $_user ['lastLogin'] = $uData ['login_date'];
  579. $_user ['official_code'] = $uData ['official_code'];
  580. $_user ['picture_uri'] = $uData ['picture_uri'];
  581. $_user ['user_id'] = $uData ['user_id'];
  582. $_user ['language'] = $uData ['language'];
  583. $_user ['auth_source'] = $uData ['auth_source'];
  584. $_user ['theme'] = $uData ['theme'];
  585. $_user ['status'] = $uData ['status'];
  586. $is_platformAdmin = (bool) (! is_null( $uData['is_admin']));
  587. $is_allowedCreateCourse = (bool) (($uData ['status'] == 1) or (api_get_setting('drhCourseManagerRights') and $uData['status'] == 4));
  588. ConditionalLogin::check_conditions($uData);
  589. api_session_register('_user');
  590. UserManager::update_extra_field_value($_user['user_id'], 'already_logged_in', 'true');
  591. api_session_register('is_platformAdmin');
  592. api_session_register('is_allowedCreateCourse');
  593. // If request_uri is settd we have to go further to have course permissions
  594. if (empty($_SESSION['request_uri']) || !isset($_SESSION['request_uri'])) {
  595. require_once api_get_path(LIBRARY_PATH).'loginredirection.lib.php';
  596. LoginRedirection::redirect();
  597. }
  598. } else {
  599. header('location:'.api_get_path(WEB_PATH));
  600. //exit("WARNING UNDEFINED UID !! ");
  601. }
  602. } else { // no uid => logout or Anonymous
  603. api_session_unregister('_user');
  604. api_session_unregister('_uid');
  605. }
  606. api_session_register('is_platformAdmin');
  607. api_session_register('is_allowedCreateCourse');
  608. } else { // continue with the previous values
  609. $_user = $_SESSION['_user'];
  610. $is_platformAdmin = $_SESSION['is_platformAdmin'];
  611. $is_allowedCreateCourse = $_SESSION['is_allowedCreateCourse'];
  612. }
  613. /* COURSE INIT */
  614. if (isset($cidReset) && $cidReset) { // course session data refresh requested or empty data
  615. if ($cidReq) {
  616. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  617. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  618. $sql = "SELECT course.*, course_category.code faCode, course_category.name faName
  619. FROM $course_table
  620. LEFT JOIN $course_cat_table
  621. ON course.category_code = course_category.code
  622. WHERE course.code = '$cidReq'";
  623. $result = Database::query($sql);
  624. if (Database::num_rows($result)>0) {
  625. $course_data = Database::fetch_array($result);
  626. //@TODO real_cid should be cid, for working with numeric course id
  627. $_real_cid = $course_data['id'];
  628. $_cid = $course_data['code'];
  629. $_course = array();
  630. $_course['real_id'] = $course_data['id'];
  631. $_course['id'] = $course_data['code']; //auto-assigned integer
  632. $_course['code'] = $course_data['code'];
  633. $_course['name'] = $course_data['title'];
  634. $_course['official_code'] = $course_data['visual_code']; // use in echo
  635. $_course['sysCode'] = $course_data['code']; // use as key in db
  636. $_course['path'] = $course_data['directory']; // use as key in path
  637. $_course['dbName'] = $course_data['db_name']; // use as key in db list
  638. $_course['db_name'] = $course_data['db_name']; //
  639. $_course['dbNameGlu'] = $_configuration['table_prefix'] . $course_data['db_name'] . $_configuration['db_glue']; // use in all queries
  640. $_course['titular'] = $course_data['tutor_name'];
  641. $_course['language'] = $course_data['course_language'];
  642. $_course['extLink']['url' ] = $course_data['department_url'];
  643. $_course['extLink']['name'] = $course_data['department_name'];
  644. $_course['categoryCode'] = $course_data['faCode'];
  645. $_course['categoryName'] = $course_data['faName'];
  646. $_course['visibility'] = $course_data['visibility'];
  647. $_course['subscribe_allowed'] = $course_data['subscribe'];
  648. $_course['unubscribe_allowed'] = $course_data['unsubscribe'];
  649. api_session_register('_cid');
  650. api_session_register('_course');
  651. //@TODO real_cid should be cid, for working with numeric course id
  652. api_session_register('_real_cid');
  653. // if a session id has been given in url, we store the session
  654. if (api_get_setting('use_session_mode')=='true') {
  655. // Database Table Definitions
  656. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  657. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  658. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  659. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  660. if (!empty($_GET['id_session'])) {
  661. $_SESSION['id_session'] = intval($_GET['id_session']);
  662. $sql = 'SELECT name FROM '.$tbl_session . ' WHERE id="'.intval($_SESSION['id_session']) . '"';
  663. $rs = Database::query($sql);
  664. list($_SESSION['session_name']) = Database::fetch_array($rs);
  665. } else {
  666. api_session_unregister('session_name');
  667. api_session_unregister('id_session');
  668. }
  669. }
  670. if (!isset($_SESSION['login_as'])) {
  671. //We add a new record in the course tracking table
  672. $course_tracking_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  673. $time = api_get_datetime();
  674. $sql="INSERT INTO $course_tracking_table(course_code, user_id, login_course_date, logout_course_date, counter, session_id)" .
  675. "VALUES('".$_course['sysCode']."', '".$_user['user_id']."', '$time', '$time', '1', '".api_get_session_id()."')";
  676. //error_log($sql);
  677. Database::query($sql);
  678. }
  679. } else {
  680. //exit("WARNING UNDEFINED CID !! ");
  681. header('location:'.api_get_path(WEB_PATH));
  682. }
  683. } else {
  684. api_session_unregister('_cid');
  685. api_session_unregister('_real_cid');
  686. api_session_unregister('_course');
  687. if (!empty($_SESSION)) {
  688. foreach($_SESSION as $key=>$session_item) {
  689. if (strpos($key,'lp_autolunch_') === false) {
  690. continue;
  691. } else {
  692. if(isset($_SESSION[$key])) {
  693. api_session_unregister($key);
  694. }
  695. }
  696. }
  697. }
  698. //Deleting session info
  699. if (api_get_session_id()) {
  700. api_session_unregister('id_session');
  701. api_session_unregister('session_name');
  702. }
  703. }
  704. } else { // continue with the previous values
  705. if (empty($_SESSION['_course']) OR empty($_SESSION['_cid'])) { //no previous values...
  706. $_cid = -1; //set default values that will be caracteristic of being unset
  707. $_course = -1;
  708. } else {
  709. $_cid = $_SESSION['_cid' ];
  710. $_course = $_SESSION['_course'];
  711. // these lines are usefull for tracking. Indeed we can have lost the id_session and not the cid.
  712. // Moreover, if we want to track a course with another session it can be usefull
  713. if (!empty($_GET['id_session'])) {
  714. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  715. $_SESSION['id_session'] = intval($_GET['id_session']);
  716. $sql = 'SELECT name FROM '.$tbl_session . ' WHERE id="'.intval($_SESSION['id_session']). '"';
  717. $rs = Database::query($sql);
  718. list($_SESSION['session_name']) = Database::fetch_array($rs);
  719. }
  720. if (!isset($_SESSION['login_as'])) {
  721. $course_tracking_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  722. if (isset($_configuration['session_lifetime'])) {
  723. $session_lifetime = $_configuration['session_lifetime'];
  724. } else {
  725. $session_lifetime = 3600;
  726. }
  727. $course_code=$_course['sysCode'];
  728. $time = api_get_datetime();
  729. //We select the last record for the current course in the course tracking table
  730. // But only if the login date is < thant now + max_life_time
  731. $sql="SELECT course_access_id FROM $course_tracking_table
  732. WHERE user_id=".intval($_user ['user_id'])."
  733. AND course_code='$course_code' AND session_id = ".api_get_session_id()."
  734. AND login_course_date > now() - INTERVAL $session_lifetime SECOND
  735. ORDER BY login_course_date DESC LIMIT 0,1";
  736. $result=Database::query($sql);
  737. //error_log($sql);
  738. if (Database::num_rows($result)>0) {
  739. $i_course_access_id = Database::result($result,0,0);
  740. //We update the course tracking table
  741. $sql="UPDATE $course_tracking_table SET logout_course_date = '$time', counter = counter+1 ".
  742. "WHERE course_access_id=".intval($i_course_access_id)." AND session_id = ".api_get_session_id();
  743. //error_log($sql);
  744. Database::query($sql);
  745. } else {
  746. $sql="INSERT INTO $course_tracking_table (course_code, user_id, login_course_date, logout_course_date, counter, session_id)" .
  747. "VALUES('".$course_code."', '".$_user['user_id']."', '$time', '$time', '1','".api_get_session_id()."')";
  748. //error_log($sql);
  749. Database::query($sql);
  750. }
  751. }
  752. }
  753. }
  754. /* COURSE / USER REL. INIT */
  755. if ((isset($uidReset) && $uidReset) || (isset($cidReset) && $cidReset)) { // session data refresh requested
  756. if (isset($_user['user_id']) && $_user['user_id'] && isset($_cid) && $_cid) { // have keys to search data
  757. if (api_get_setting('use_session_mode') != 'true') {
  758. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  759. $sql = "SELECT * FROM $course_user_table
  760. WHERE user_id = '".$_user['user_id']."' AND relation_type<>".COURSE_RELATION_TYPE_RRHH."
  761. AND course_code = '$cidReq'";
  762. $result = Database::query($sql);
  763. if (Database::num_rows($result) > 0) { // this user have a recorded state for this course
  764. $cuData = Database::fetch_array($result);
  765. $is_courseMember = true;
  766. $is_courseTutor = (bool) ($cuData['tutor_id' ] == 1 );
  767. $is_courseAdmin = (bool) ($cuData['status'] == 1 );
  768. api_session_register('_courseUser');
  769. } else { // this user has no status related to this course
  770. $is_courseMember = false;
  771. $is_courseAdmin = false;
  772. $is_courseTutor = false;
  773. }
  774. $is_courseAdmin = (bool) ($is_courseAdmin || $is_platformAdmin);
  775. } else {
  776. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  777. $sql = "SELECT * FROM ".$tbl_course_user."
  778. WHERE user_id = '".$_user['user_id']."' AND relation_type<>".COURSE_RELATION_TYPE_RRHH."
  779. AND course_code = '$cidReq'";
  780. $result = Database::query($sql);
  781. if (Database::num_rows($result) > 0) { // this user have a recorded state for this course
  782. $cuData = Database::fetch_array($result);
  783. $_courseUser['role'] = $cuData['role' ];
  784. $is_courseMember = true;
  785. $is_courseTutor = (bool) ($cuData['tutor_id' ] == 1 );
  786. $is_courseAdmin = (bool) ($cuData['status'] == 1 );
  787. api_session_register('_courseUser');
  788. }
  789. if (!isset($is_courseAdmin)) { // this user has no status related to this course
  790. // is it the session coach or the session admin ?
  791. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  792. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  793. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  794. //Session coach, session admin, course coach admin
  795. $sql = " SELECT session.id_coach, session_admin_id, session_rcru.id_user
  796. FROM $tbl_session session,$tbl_session_course_user session_rcru
  797. WHERE session_rcru.id_session = session.id AND session_rcru.course_code = '$_cid' AND session_rcru.id_user='{$_user['user_id']}' AND session_rcru.status = 2";
  798. $result = Database::query($sql);
  799. $row = Database::store_result($result);
  800. if (isset($row) && $row[0]['id_coach'] == $_user['user_id']) {
  801. $_courseUser['role'] = 'Professor';
  802. $is_courseMember = true;
  803. $is_courseTutor = true;
  804. $is_courseCoach = true;
  805. $is_sessionAdmin = false;
  806. if (api_get_setting('extend_rights_for_coach')=='true') {
  807. $is_courseAdmin = true;
  808. } else {
  809. $is_courseAdmin = false;
  810. }
  811. api_session_register('_courseUser');
  812. } elseif(isset($row) && $row[0]['session_admin_id']==$_user['user_id']) {
  813. $_courseUser['role'] = 'Professor';
  814. $is_courseMember = false;
  815. $is_courseTutor = false;
  816. $is_courseAdmin = false;
  817. $is_courseCoach = false;
  818. $is_sessionAdmin = true;
  819. } else {
  820. // Check if the current user is the course coach
  821. $sql = "SELECT 1
  822. FROM ".$tbl_session_course_user."
  823. WHERE course_code='$_cid'
  824. AND id_user = '".$_user['user_id']."'
  825. AND id_session = '".api_get_session_id()."'
  826. AND status = 2";
  827. $result = Database::query($sql);
  828. if ($row = Database::fetch_array($result)) {
  829. $_courseUser['role'] = 'Professor';
  830. $is_courseMember = true;
  831. $is_courseTutor = true;
  832. $is_courseCoach = true;
  833. $is_sessionAdmin = false;
  834. if (api_get_setting('extend_rights_for_coach')=='true') {
  835. $is_courseAdmin = true;
  836. } else {
  837. $is_courseAdmin = false;
  838. }
  839. api_session_register('_courseUser');
  840. } else {
  841. if (api_get_session_id() != 0) {
  842. // Check if the user is a student is this session
  843. $sql = "SELECT * FROM ".$tbl_session_course_user."
  844. WHERE id_user = '".$_user['user_id']."'
  845. AND id_session = '".api_get_session_id()."'
  846. AND course_code = '$cidReq' AND status NOT IN(2)";
  847. $result = Database::query($sql);
  848. if (Database::num_rows($result) > 0) { // this user have a recorded state for this course
  849. while($row = Database::fetch_array($result)){
  850. $is_courseMember = true;
  851. $is_courseTutor = false;
  852. $is_courseAdmin = false;
  853. $is_sessionAdmin = false;
  854. api_session_register('_courseUser');
  855. }
  856. }
  857. } else {
  858. //unregister user
  859. $is_courseMember = false;
  860. $is_courseTutor = false;
  861. $is_courseAdmin = false;
  862. $is_sessionAdmin = false;
  863. api_session_unregister('_courseUser');
  864. //$_course['visibility'] = 0; this depends the
  865. }
  866. }
  867. }
  868. }
  869. }
  870. } else { // keys missing => not anymore in the course - user relation
  871. // course
  872. $is_courseMember = false;
  873. $is_courseAdmin = false;
  874. $is_courseTutor = false;
  875. $is_courseCoach = false;
  876. $is_sessionAdmin = false;
  877. api_session_unregister('_courseUser');
  878. }
  879. //DEPRECATED
  880. //$is_courseAllowed=($_cid && ($_course['visibility'] || $is_courseMember || $is_platformAdmin))?true:false;
  881. //NEW
  882. if (isset($_course)) {
  883. if ($_course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)
  884. $is_allowed_in_course = true;
  885. elseif ($_course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM && isset($_user['user_id']) && !api_is_anonymous($_user['user_id']))
  886. $is_allowed_in_course = true;
  887. elseif ($_course['visibility'] == COURSE_VISIBILITY_REGISTERED && ($is_platformAdmin || $is_courseMember))
  888. $is_allowed_in_course = true;
  889. elseif ($_course['visibility'] == COURSE_VISIBILITY_CLOSED && ($is_platformAdmin || $is_courseAdmin))
  890. $is_allowed_in_course = true;
  891. else $is_allowed_in_course = false;
  892. }
  893. // requires testing!!!
  894. // check the session visibility
  895. if (!empty($is_allowed_in_course)) {
  896. $my_session_id = api_get_session_id();
  897. //if I'm in a session
  898. //var_dump($is_platformAdmin, $is_courseTutor,api_is_coach());
  899. if ($my_session_id!=0)
  900. if (!$is_platformAdmin) {
  901. // admin and session coach are *not* affected to the invisible session mode
  902. // the coach is not affected because he can log in some days after the end date of a session
  903. $session_visibility = api_get_session_visibility($my_session_id);
  904. if ($session_visibility==SESSION_INVISIBLE)
  905. $is_allowed_in_course =false;
  906. }
  907. }
  908. // save the states
  909. api_session_register('is_courseMember');
  910. api_session_register('is_courseAdmin');
  911. //api_session_register('is_courseAllowed'); //deprecated old permission var
  912. api_session_register('is_courseTutor');
  913. api_session_register('is_allowed_in_course'); //new permission var
  914. api_session_register('is_courseCoach');
  915. api_session_register('is_sessionAdmin');
  916. } else { // continue with the previous values
  917. if (isset($_SESSION ['_courseUser'])) {
  918. $_courseUser = $_SESSION ['_courseUser'];
  919. }
  920. $is_courseMember = $_SESSION ['is_courseMember' ];
  921. $is_courseAdmin = $_SESSION ['is_courseAdmin' ];
  922. //$is_courseAllowed = $_SESSION ['is_courseAllowed']; //deprecated
  923. $is_allowed_in_course = $_SESSION ['is_allowed_in_course'];
  924. $is_courseTutor = $_SESSION ['is_courseTutor' ];
  925. $is_courseCoach = $_SESSION ['is_courseCoach' ];
  926. }
  927. /* GROUP INIT */
  928. if ((isset($gidReset) && $gidReset) || (isset($cidReset) && $cidReset)) { // session data refresh requested
  929. if ($gidReq && $_cid ) { // have keys to search data
  930. $group_table = Database::get_course_table(TABLE_GROUP);
  931. $sql = "SELECT * FROM $group_table WHERE id = '$gidReq'";
  932. $result = Database::query($sql);
  933. if (Database::num_rows($result) > 0) { // This group has recorded status related to this course
  934. $gpData = Database::fetch_array($result);
  935. $_gid = $gpData ['id'];
  936. api_session_register('_gid');
  937. } else {
  938. exit("WARNING UNDEFINED GID !! ");
  939. }
  940. } elseif (isset($_SESSION['_gid']) or isset($_gid)) { // Keys missing => not anymore in the group - course relation
  941. api_session_unregister('_gid');
  942. }
  943. } elseif (isset($_SESSION['_gid'])) { // continue with the previous values
  944. $_gid = $_SESSION ['_gid' ];
  945. } else { //if no previous value, assign caracteristic undefined value
  946. $_gid = -1;
  947. }
  948. //set variable according to student_view_enabled choices
  949. if (api_get_setting('student_view_enabled') == "true") {
  950. if (isset($_GET['isStudentView'])) {
  951. if ($_GET['isStudentView'] == 'true') {
  952. if (isset($_SESSION['studentview'])) {
  953. if (!empty($_SESSION['studentview'])) {
  954. // switching to studentview
  955. $_SESSION['studentview'] = 'studentview';
  956. }
  957. }
  958. } elseif ($_GET['isStudentView'] == 'false') {
  959. if (isset($_SESSION['studentview'])) {
  960. if (!empty($_SESSION['studentview'])) {
  961. // switching to teacherview
  962. $_SESSION['studentview'] = 'teacherview';
  963. }
  964. }
  965. }
  966. } elseif (!empty($_SESSION['studentview'])) {
  967. //all is fine, no change to that, obviously
  968. } elseif (empty($_SESSION['studentview'])) {
  969. // We are in teacherview here
  970. $_SESSION['studentview'] = 'teacherview';
  971. }
  972. }
  973. if (isset($_cid)) {
  974. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  975. $time = api_get_datetime();
  976. $sql="UPDATE $tbl_course SET last_visit= '$time' WHERE code='$_cid'";
  977. Database::query($sql);
  978. }
  979. if (!empty($_SESSION['request_uri'])){
  980. $req= $_SESSION['request_uri'];
  981. unset($_SESSION['request_uri']);
  982. header('Location: '.$req);
  983. }