local.inc.php 51 KB

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