local.inc.php 46 KB

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