local.inc.php 48 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'; moved to autologin
  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 || $uData['auth_source'] == CAS_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. //error_log($_GET['sso_referer']);error_log($protocol.$master_url);
  447. if (isset($_GET['sso_referer']) ? $_GET['sso_referer'] === $protocol.$master_url : FALSE) {
  448. //make all the process of checking
  449. //if the user exists (delegated to the sso class)
  450. $osso->check_user();
  451. } else {
  452. error_log('Check the sso_referer URL in your script');
  453. //Request comes from unknown source
  454. $loginFailed = true;
  455. api_session_unregister('_uid');
  456. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=unrecognize_sso_origin');
  457. exit;
  458. }
  459. }
  460. }//end logout
  461. } elseif (api_get_setting('openid_authentication')=='true') {
  462. if (!empty($_POST['openid_url'])) {
  463. include 'main/auth/openid/login.php';
  464. openid_begin(trim($_POST['openid_url']),api_get_path(WEB_PATH).'index.php');
  465. //this last function should trigger a redirect, so we can die here safely
  466. die('Openid login redirection should be in progress');
  467. } elseif (!empty($_GET['openid_identity'])) {
  468. //it's usual for PHP to replace '.' (dot) by '_' (underscore) in URL parameters
  469. include('main/auth/openid/login.php');
  470. $res = openid_complete($_GET);
  471. if ($res['status'] == 'success') {
  472. $id1 = Database::escape_string($res['openid.identity']);
  473. //have another id with or without the final '/'
  474. $id2 = (substr($id1,-1,1)=='/'?substr($id1,0,-1):$id1.'/');
  475. //lookup the user in the main database
  476. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  477. $sql = "SELECT user_id, username, password, auth_source, active, expiration_date
  478. FROM $user_table
  479. WHERE openid = '$id1'
  480. OR openid = '$id2' ";
  481. $result = Database::query($sql);
  482. if ($result !== false) {
  483. if (Database::num_rows($result)>0) {
  484. //$row = Database::fetch_array($res);
  485. $uData = Database::fetch_array($result);
  486. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
  487. //the authentification of this user is managed by Chamilo itself
  488. // check if the account is active (not locked)
  489. if ($uData['active']=='1') {
  490. // check if the expiration date has not been reached
  491. if ($uData['expiration_date']>date('Y-m-d H:i:s') OR $uData['expiration_date']=='0000-00-00 00:00:00') {
  492. $_user['user_id'] = $uData['user_id'];
  493. $_user['status'] = $uData['status'];
  494. api_session_register('_user');
  495. event_login();
  496. } else {
  497. $loginFailed = true;
  498. api_session_unregister('_uid');
  499. header('Location: index.php?loginFailed=1&error=account_expired');
  500. exit;
  501. }
  502. } else {
  503. $loginFailed = true;
  504. api_session_unregister('_uid');
  505. header('Location: index.php?loginFailed=1&error=account_inactive');
  506. exit;
  507. }
  508. if (isset($uData['creator_id']) && $_user['user_id'] != $uData['creator_id']) {
  509. //first login for a not self registred
  510. //e.g. registered by a teacher
  511. //do nothing (code may be added later)
  512. }
  513. }
  514. } else {
  515. //Redirect to the subscription form
  516. 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');
  517. //$loginFailed = true;
  518. }
  519. } else {
  520. $loginFailed = true;
  521. }
  522. } else {
  523. $loginFailed = true;
  524. }
  525. }
  526. } elseif (KeyAuth::is_enabled()) {
  527. $success = KeyAuth::instance()->login();
  528. if($success)
  529. {
  530. $use_anonymous = false;
  531. }
  532. }
  533. // else {} => continue as anonymous user
  534. $uidReset = true;
  535. // $cidReset = true;
  536. // $gidReset = true;
  537. } // end else
  538. //Now check for anonymous user mode
  539. if (isset($use_anonymous) && $use_anonymous) {
  540. //if anonymous mode is set, then try to set the current user as anonymous
  541. //if he doesn't have a login yet
  542. api_set_anonymous();
  543. } else {
  544. //if anonymous mode is not set, then check if this user is anonymous. If it
  545. //is, clean it from being anonymous (make him a nobody :-))
  546. api_clear_anonymous();
  547. }
  548. // if there is a cDir parameter in the URL (coming from courses/.htaccess redirection)
  549. if (!empty($cDir)) {
  550. $c = CourseManager::get_course_id_from_path($cDir);
  551. if ($c) { $cidReq = $c; }
  552. }
  553. // if the requested course is different from the course in session
  554. if (!empty($cidReq) && (!isset($_SESSION['_cid']) or (isset($_SESSION['_cid']) && $cidReq != $_SESSION['_cid']))) {
  555. $cidReset = true;
  556. $gidReset = true; // As groups depend from courses, group id is reset
  557. }
  558. // if the requested group is different from the group in session
  559. $gid = isset($_SESSION['_gid']) ? $_SESSION['_gid'] : '';
  560. if ($gidReq && $gidReq != $gid) {
  561. $gidReset = true;
  562. }
  563. /* USER INIT */
  564. if (isset($uidReset) && $uidReset) { // session data refresh requested
  565. unset($_SESSION['_user']['uidReset']);
  566. $is_platformAdmin = false;
  567. $is_allowedCreateCourse = false;
  568. if (isset($_user['user_id']) && $_user['user_id'] && ! api_is_anonymous()) {
  569. // a uid is given (log in succeeded)
  570. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  571. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  572. $track_e_login = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  573. $sql = "SELECT user.*, a.user_id is_admin, UNIX_TIMESTAMP(login.login_date) login_date
  574. FROM $user_table
  575. LEFT JOIN $admin_table a
  576. ON user.user_id = a.user_id
  577. LEFT JOIN $track_e_login login
  578. ON user.user_id = login.login_user_id
  579. WHERE user.user_id = '".$_user['user_id']."'
  580. ORDER BY login.login_date DESC LIMIT 1";
  581. $result = Database::query($sql);
  582. if (Database::num_rows($result) > 0) {
  583. // Extracting the user data
  584. $uData = Database::fetch_array($result);
  585. $_user ['firstName'] = $uData ['firstname' ];
  586. $_user ['lastName' ] = $uData ['lastname' ];
  587. $_user ['mail' ] = $uData ['email' ];
  588. $_user ['lastLogin'] = $uData ['login_date'];
  589. $_user ['official_code'] = $uData ['official_code'];
  590. $_user ['picture_uri'] = $uData ['picture_uri'];
  591. $_user ['user_id'] = $uData ['user_id'];
  592. $_user ['language'] = $uData ['language'];
  593. $_user ['auth_source'] = $uData ['auth_source'];
  594. $_user ['theme'] = $uData ['theme'];
  595. $_user ['status'] = $uData ['status'];
  596. $is_platformAdmin = (bool) (! is_null( $uData['is_admin']));
  597. $is_allowedCreateCourse = (bool) (($uData ['status'] == 1) or (api_get_setting('drhCourseManagerRights') and $uData['status'] == 4));
  598. ConditionalLogin::check_conditions($uData);
  599. api_session_register('_user');
  600. UserManager::update_extra_field_value($_user['user_id'], 'already_logged_in', 'true');
  601. api_session_register('is_platformAdmin');
  602. api_session_register('is_allowedCreateCourse');
  603. // If request_uri is setted we have to go further to have course permissions
  604. if (empty($_SESSION['request_uri']) || !isset($_SESSION['request_uri'])) {
  605. if (isset($_SESSION['noredirection'])) {
  606. //If we just want to reset info without redirecting user
  607. unset($_SESSION['noredirection']);
  608. } else {
  609. //require_once api_get_path(LIBRARY_PATH).'loginredirection.lib.php'; moved to autologin
  610. LoginRedirection::redirect();
  611. }
  612. }
  613. } else {
  614. header('location:'.api_get_path(WEB_PATH));
  615. //exit("WARNING UNDEFINED UID !! ");
  616. }
  617. } else { // no uid => logout or Anonymous
  618. api_session_unregister('_user');
  619. api_session_unregister('_uid');
  620. }
  621. api_session_register('is_platformAdmin');
  622. api_session_register('is_allowedCreateCourse');
  623. } else { // continue with the previous values
  624. $_user = $_SESSION['_user'];
  625. $is_platformAdmin = $_SESSION['is_platformAdmin'];
  626. $is_allowedCreateCourse = $_SESSION['is_allowedCreateCourse'];
  627. }
  628. /* COURSE INIT */
  629. if (isset($cidReset) && $cidReset) {
  630. // Course session data refresh requested or empty data
  631. if ($cidReq) {
  632. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  633. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  634. $sql = "SELECT course.*, course_category.code faCode, course_category.name faName
  635. FROM $course_table
  636. LEFT JOIN $course_cat_table
  637. ON course.category_code = course_category.code
  638. WHERE course.code = '$cidReq'";
  639. $result = Database::query($sql);
  640. if (Database::num_rows($result) > 0) {
  641. $course_data = Database::fetch_array($result);
  642. //@TODO real_cid should be cid, for working with numeric course id
  643. $_real_cid = $course_data['id'];
  644. $_cid = $course_data['code'];
  645. $_course = array();
  646. $_course['real_id'] = $course_data['id'];
  647. $_course['id'] = $course_data['code']; //auto-assigned integer
  648. $_course['code'] = $course_data['code'];
  649. $_course['name'] = $course_data['title'];
  650. $_course['official_code'] = $course_data['visual_code']; // use in echo
  651. $_course['sysCode'] = $course_data['code']; // use as key in db
  652. $_course['path'] = $course_data['directory']; // use as key in path
  653. $_course['dbName'] = $course_data['db_name']; // use as key in db list
  654. $_course['db_name'] = $course_data['db_name']; // not needed in Chamilo 1.9
  655. $_course['dbNameGlu'] = $_configuration['table_prefix'] . $course_data['db_name'] . $_configuration['db_glue']; // use in all queries //not needed in Chamilo 1.9
  656. $_course['titular'] = $course_data['tutor_name'];// this should be deprecated and use the table course_rel_user
  657. $_course['language'] = $course_data['course_language'];
  658. $_course['extLink']['url' ] = $course_data['department_url'];
  659. $_course['extLink']['name'] = $course_data['department_name'];
  660. $_course['categoryCode'] = $course_data['faCode'];
  661. $_course['categoryName'] = $course_data['faName'];
  662. $_course['visibility'] = $course_data['visibility'];
  663. $_course['subscribe_allowed'] = $course_data['subscribe'];
  664. $_course['unubscribe_allowed'] = $course_data['unsubscribe'];
  665. $_course['activate_legal'] = $course_data['activate_legal'];
  666. $_course['show_score'] = $course_data['show_score']; //used in the work tool
  667. api_session_register('_cid');
  668. api_session_register('_course');
  669. //@TODO real_cid should be cid, for working with numeric course id
  670. api_session_register('_real_cid');
  671. // if a session id has been given in url, we store the session
  672. if (api_get_setting('use_session_mode') == 'true') {
  673. // Database Table Definitions
  674. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  675. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  676. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  677. if (!empty($_GET['id_session'])) {
  678. $_SESSION['id_session'] = intval($_GET['id_session']);
  679. $sql = 'SELECT name FROM '.$tbl_session . ' WHERE id="'.intval($_SESSION['id_session']) . '"';
  680. $rs = Database::query($sql);
  681. list($_SESSION['session_name']) = Database::fetch_array($rs);
  682. } else {
  683. api_session_unregister('session_name');
  684. api_session_unregister('id_session');
  685. }
  686. }
  687. if (!isset($_SESSION['login_as'])) {
  688. //Course login
  689. if (isset($_user['user_id'])) {
  690. event_course_login($_course['sysCode'], $_user['user_id'], api_get_session_id());
  691. }
  692. }
  693. } else {
  694. //exit("WARNING UNDEFINED CID !! ");
  695. header('location:'.api_get_path(WEB_PATH));
  696. }
  697. } else {
  698. api_session_unregister('_cid');
  699. api_session_unregister('_real_cid');
  700. api_session_unregister('_course');
  701. if (!empty($_SESSION)) {
  702. foreach($_SESSION as $key=>$session_item) {
  703. if (strpos($key,'lp_autolunch_') === false) {
  704. continue;
  705. } else {
  706. if(isset($_SESSION[$key])) {
  707. api_session_unregister($key);
  708. }
  709. }
  710. }
  711. }
  712. //Deleting session info
  713. if (api_get_session_id()) {
  714. api_session_unregister('id_session');
  715. api_session_unregister('session_name');
  716. }
  717. }
  718. } else {
  719. // Continue with the previous values
  720. if (empty($_SESSION['_course']) OR empty($_SESSION['_cid'])) { //no previous values...
  721. $_cid = -1; //set default values that will be caracteristic of being unset
  722. $_course = -1;
  723. } else {
  724. $_cid = $_SESSION['_cid' ];
  725. $_course = $_SESSION['_course'];
  726. // these lines are usefull for tracking. Indeed we can have lost the id_session and not the cid.
  727. // Moreover, if we want to track a course with another session it can be usefull
  728. if (!empty($_GET['id_session'])) {
  729. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  730. $sql = 'SELECT name FROM '.$tbl_session . ' WHERE id="'.intval($_SESSION['id_session']). '"';
  731. $rs = Database::query($sql);
  732. list($_SESSION['session_name']) = Database::fetch_array($rs);
  733. $_SESSION['id_session'] = intval($_GET['id_session']);
  734. }
  735. if (!isset($_SESSION['login_as'])) {
  736. $save_course_access = true;
  737. //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
  738. //Disables the updates in the TRACK_E_COURSE_ACCESS table
  739. if (isset($_dont_save_user_course_access) && $_dont_save_user_course_access == true) {
  740. $save_course_access = false;
  741. }
  742. if ($save_course_access) {
  743. $course_tracking_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  744. /*
  745. * When $_configuration['session_lifetime'] is too big 100 hours (in order to let users take exercises with no problems)
  746. * the function Tracking::get_time_spent_on_the_course() returns big values (200h) due the condition:
  747. * login_course_date > now() - INTERVAL $session_lifetime SECOND
  748. *
  749. */
  750. /*
  751. if (isset($_configuration['session_lifetime'])) {
  752. $session_lifetime = $_configuration['session_lifetime'];
  753. } else {
  754. $session_lifetime = 3600; // 1 hour
  755. }*/
  756. $session_lifetime = 3600; // 1 hour
  757. $course_code = $_course['sysCode'];
  758. $time = api_get_datetime();
  759. if (isset($_user['user_id']) && !empty($_user['user_id'])) {
  760. //We select the last record for the current course in the course tracking table
  761. //But only if the login date is < than now + max_life_time
  762. $sql = "SELECT course_access_id FROM $course_tracking_table
  763. WHERE user_id = ".intval($_user ['user_id'])." AND
  764. course_code = '$course_code' AND
  765. session_id = ".api_get_session_id()." AND
  766. login_course_date > now() - INTERVAL $session_lifetime SECOND
  767. ORDER BY login_course_date DESC LIMIT 0,1";
  768. $result = Database::query($sql);
  769. if (Database::num_rows($result) > 0) {
  770. $i_course_access_id = Database::result($result,0,0);
  771. //We update the course tracking table
  772. $sql = "UPDATE $course_tracking_table SET logout_course_date = '$time', counter = counter+1
  773. WHERE course_access_id = ".intval($i_course_access_id)." AND session_id = ".api_get_session_id();
  774. //error_log($sql);
  775. Database::query($sql);
  776. } else {
  777. $sql="INSERT INTO $course_tracking_table (course_code, user_id, login_course_date, logout_course_date, counter, session_id)" .
  778. "VALUES('".$course_code."', '".$_user['user_id']."', '$time', '$time', '1','".api_get_session_id()."')";
  779. //error_log($sql);
  780. Database::query($sql);
  781. }
  782. }
  783. }
  784. }
  785. }
  786. }
  787. /* COURSE / USER REL. INIT */
  788. $session_id = api_get_session_id();
  789. $user_id = isset($_user['user_id']) ? $_user['user_id'] : null;
  790. //Course permissions
  791. $is_courseAdmin = false; //course teacher
  792. $is_courseTutor = false; //course teacher - some rights
  793. $is_courseMember = false; //course student
  794. //Course - User permissions
  795. $is_sessionAdmin = false;
  796. if ((isset($uidReset) && $uidReset) || (isset($cidReset) && $cidReset)) {
  797. if (isset($user_id) && $user_id && isset($_cid) && $_cid) {
  798. //Check if user is subscribed in a course
  799. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  800. $sql = "SELECT * FROM $course_user_table
  801. WHERE user_id = '".$user_id."' AND relation_type <> ".COURSE_RELATION_TYPE_RRHH."
  802. AND course_code = '$cidReq'";
  803. $result = Database::query($sql);
  804. $cuData = null;
  805. if (Database::num_rows($result) > 0) { // this user have a recorded state for this course
  806. $cuData = Database::fetch_array($result, 'ASSOC');
  807. $is_courseAdmin = (bool) ($cuData['status'] == 1 );
  808. $is_courseTutor = (bool) ($cuData['tutor_id' ] == 1 );
  809. $is_courseMember = true;
  810. //Checking if the user filled the course legal agreement
  811. if ($_course['activate_legal'] == 1 && !api_is_platform_admin()) {
  812. $user_is_subscribed = CourseManager::is_user_accepted_legal($user_id, $_course['id'], $session_id);
  813. if (!$user_is_subscribed) {
  814. $url = api_get_path(WEB_CODE_PATH).'course_info/legal.php?course_code='.$_course['code'].'&session_id='.$session_id;
  815. header('Location: '.$url);
  816. exit;
  817. }
  818. }
  819. $_courseUser['role'] = $cuData['role'];
  820. api_session_register('_courseUser');
  821. }
  822. //We are in a session course? Check session permissions
  823. if (!empty($session_id)) {
  824. //I'm not the teacher of the course
  825. if ($is_courseAdmin == false) {
  826. // this user has no status related to this course
  827. // The user is subscribed in a session? The user is a Session coach a Session admin ?
  828. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  829. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  830. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  831. //Session coach, session admin, course coach admin
  832. $sql = "SELECT session.id_coach, session_admin_id, session_rcru.id_user
  833. FROM $tbl_session session, $tbl_session_course_user session_rcru
  834. WHERE session_rcru.id_session = session.id AND
  835. session_rcru.course_code = '$_cid' AND
  836. session_rcru.id_user = '$user_id' AND
  837. session_rcru.id_session = $session_id AND
  838. session_rcru.status = 2";
  839. $result = Database::query($sql);
  840. $row = Database::store_result($result);
  841. //I'm a session admin?
  842. if (isset($row) && isset($row[0]) && $row[0]['session_admin_id'] == $user_id) {
  843. $_courseUser['role'] = 'Professor';
  844. $is_courseMember = false;
  845. $is_courseTutor = false;
  846. $is_courseAdmin = false;
  847. $is_courseCoach = false;
  848. $is_sessionAdmin = true;
  849. } else {
  850. //Im a coach or a student?
  851. $sql = "SELECT id_user, status FROM ".$tbl_session_course_user."
  852. WHERE course_code = '$_cid' AND
  853. id_user = '".$user_id."' AND
  854. id_session = '".$session_id."'
  855. LIMIT 1";
  856. $result = Database::query($sql);
  857. if (Database::num_rows($result)) {
  858. $row = Database::fetch_array($result, 'ASSOC');
  859. $session_course_status = $row['status'];
  860. switch ($session_course_status) {
  861. case '2': // coach - teacher
  862. $_courseUser['role'] = 'Professor';
  863. $is_courseMember = true;
  864. $is_courseTutor = true;
  865. $is_courseCoach = true;
  866. $is_sessionAdmin = false;
  867. if (api_get_setting('extend_rights_for_coach') == 'true') {
  868. $is_courseAdmin = true;
  869. } else {
  870. $is_courseAdmin = false;
  871. }
  872. api_session_register('_courseUser');
  873. break;
  874. case '0': //student
  875. $is_courseMember = true;
  876. $is_courseTutor = false;
  877. $is_courseAdmin = false;
  878. $is_sessionAdmin = false;
  879. api_session_register('_courseUser');
  880. break;
  881. default:
  882. //unregister user
  883. $is_courseMember = false;
  884. $is_courseTutor = false;
  885. $is_courseAdmin = false;
  886. $is_sessionAdmin = false;
  887. api_session_unregister('_courseUser');
  888. break;
  889. }
  890. } else {
  891. //unregister user
  892. $is_courseMember = false;
  893. $is_courseTutor = false;
  894. $is_courseAdmin = false;
  895. $is_sessionAdmin = false;
  896. api_session_unregister('_courseUser');
  897. }
  898. }
  899. }
  900. //If I'm the admin platform i'm a teacher of the course
  901. if ($is_platformAdmin) {
  902. $is_courseAdmin = true;
  903. }
  904. }
  905. } else { // keys missing => not anymore in the course - user relation
  906. // course
  907. $is_courseMember = false;
  908. $is_courseAdmin = false;
  909. $is_courseTutor = false;
  910. $is_courseCoach = false;
  911. $is_sessionAdmin = false;
  912. api_session_unregister('_courseUser');
  913. }
  914. //Checking the course access
  915. $is_allowed_in_course = false;
  916. if (isset($_course)) {
  917. switch ($_course['visibility']) {
  918. case COURSE_VISIBILITY_OPEN_WORLD: //3
  919. $is_allowed_in_course = true;
  920. break;
  921. case COURSE_VISIBILITY_OPEN_PLATFORM : //2
  922. if (isset($user_id) && !api_is_anonymous($user_id)) {
  923. $is_allowed_in_course = true;
  924. }
  925. break;
  926. case COURSE_VISIBILITY_REGISTERED: //1
  927. if ($is_platformAdmin || $is_courseMember) {
  928. $is_allowed_in_course = true;
  929. }
  930. break;
  931. case COURSE_VISIBILITY_CLOSED: //0
  932. if ($is_platformAdmin || $is_courseAdmin) {
  933. $is_allowed_in_course = true;
  934. }
  935. break;
  936. }
  937. }
  938. // check the session visibility
  939. if ($is_allowed_in_course == true) {
  940. //if I'm in a session
  941. if ($session_id != 0) {
  942. if (!$is_platformAdmin) {
  943. // admin and session coach are *not* affected to the invisible session mode
  944. // the coach is not affected because he can log in some days after the end date of a session
  945. $session_visibility = api_get_session_visibility($session_id);
  946. switch ($session_visibility) {
  947. case SESSION_INVISIBLE:
  948. $is_allowed_in_course = false;
  949. break;
  950. }
  951. //checking date
  952. }
  953. }
  954. }
  955. // save the states
  956. api_session_register('is_courseAdmin');
  957. api_session_register('is_courseMember');
  958. api_session_register('is_courseTutor');
  959. api_session_register('is_courseCoach');
  960. api_session_register('is_allowed_in_course');
  961. api_session_register('is_sessionAdmin');
  962. } else { // continue with the previous values
  963. if (isset($_SESSION ['_courseUser'])) {
  964. $_courseUser = $_SESSION ['_courseUser'];
  965. }
  966. $is_courseAdmin = $_SESSION ['is_courseAdmin'];
  967. $is_courseTutor = $_SESSION ['is_courseTutor'];
  968. $is_courseCoach = $_SESSION ['is_courseCoach'];
  969. $is_courseMember = $_SESSION ['is_courseMember'];
  970. $is_allowed_in_course = $_SESSION ['is_allowed_in_course'];
  971. }
  972. /* GROUP INIT */
  973. if ((isset($gidReset) && $gidReset) || (isset($cidReset) && $cidReset)) { // session data refresh requested
  974. if ($gidReq && $_cid && !empty($_course['real_id'])) { // have keys to search data
  975. $group_table = Database::get_course_table(TABLE_GROUP);
  976. $sql = "SELECT * FROM $group_table WHERE c_id = ".$_course['real_id']." AND id = '$gidReq'";
  977. $result = Database::query($sql);
  978. if (Database::num_rows($result) > 0) { // This group has recorded status related to this course
  979. $gpData = Database::fetch_array($result);
  980. $_gid = $gpData ['id'];
  981. api_session_register('_gid');
  982. } else {
  983. api_session_unregister('_gid');
  984. }
  985. } elseif (isset($_SESSION['_gid']) or isset($_gid)) { // Keys missing => not anymore in the group - course relation
  986. api_session_unregister('_gid');
  987. }
  988. } elseif (isset($_SESSION['_gid'])) { // continue with the previous values
  989. $_gid = $_SESSION ['_gid'];
  990. } else { //if no previous value, assign caracteristic undefined value
  991. $_gid = -1;
  992. }
  993. //set variable according to student_view_enabled choices
  994. if (api_get_setting('student_view_enabled') == "true") {
  995. if (isset($_GET['isStudentView'])) {
  996. if ($_GET['isStudentView'] == 'true') {
  997. if (isset($_SESSION['studentview'])) {
  998. if (!empty($_SESSION['studentview'])) {
  999. // switching to studentview
  1000. $_SESSION['studentview'] = 'studentview';
  1001. }
  1002. }
  1003. } elseif ($_GET['isStudentView'] == 'false') {
  1004. if (isset($_SESSION['studentview'])) {
  1005. if (!empty($_SESSION['studentview'])) {
  1006. // switching to teacherview
  1007. $_SESSION['studentview'] = 'teacherview';
  1008. }
  1009. }
  1010. }
  1011. } elseif (!empty($_SESSION['studentview'])) {
  1012. //all is fine, no change to that, obviously
  1013. } elseif (empty($_SESSION['studentview'])) {
  1014. // We are in teacherview here
  1015. $_SESSION['studentview'] = 'teacherview';
  1016. }
  1017. }
  1018. if (isset($_cid)) {
  1019. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1020. $time = api_get_datetime();
  1021. $sql="UPDATE $tbl_course SET last_visit= '$time' WHERE code='$_cid'";
  1022. Database::query($sql);
  1023. }
  1024. $no_redirection = isset($no_redirection) ? $no_redirection : false;
  1025. if (!$no_redirection && (isset($_SESSION['request_uri']) && !empty($_SESSION['request_uri']))){
  1026. $req= $_SESSION['request_uri'];
  1027. unset($_SESSION['request_uri']);
  1028. header('Location: '.$req);
  1029. exit;
  1030. }