local.inc.php 47 KB

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