local.inc.php 51 KB

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