local.inc.php 45 KB

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