local.inc.php 46 KB

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