local.inc.php 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
  4. use ChamiloSession as Session;
  5. /**
  6. * SCRIPT PURPOSE.
  7. *
  8. * This script initializes and manages Chamilo session information. It
  9. * keeps available session information up to date.
  10. *
  11. * You can request a course id. It will check if the course Id requested is the
  12. * same as the current one. If it isn't it will update session information from
  13. * the database. You can also force the course reset if you want ($cidReset).
  14. *
  15. * All the course information is stored in the $_course array.
  16. *
  17. * You can request a group id. The script will check if the group id requested is the
  18. * same as the current one. If it isn't it will update session information from
  19. * the database. You can also force the course reset if you want ($gidReset).
  20. *
  21. * The course id is stored in $_cid session variable.
  22. * The group id is stored in $_gid session variable.
  23. *
  24. *
  25. * VARIABLES AFFECTING THE SCRIPT BEHAVIOR
  26. *
  27. * string $login
  28. * string $password
  29. * boolean $logout
  30. *
  31. * string $cidReq : course id requested
  32. * boolean $cidReset : ask for a course Reset, if no $cidReq is provided in the
  33. * same time, all course informations is removed from the
  34. * current session
  35. *
  36. * int $gidReq : group Id requested
  37. * boolean $gidReset : ask for a group Reset, if no $gidReq is provided in the
  38. * same time, all group informations is removed from the
  39. * current session
  40. *
  41. *
  42. * VARIABLES SET AND RETURNED BY THE SCRIPT
  43. *
  44. * All the variables below are set and returned by this script.
  45. *
  46. * USER VARIABLES
  47. *
  48. * string $_user ['firstName' ]
  49. * string $_user ['lastName' ]
  50. * string $_user ['mail' ]
  51. * string $_user ['lastLogin' ]
  52. * string $_user ['official_code']
  53. * string $_user ['picture_uri' ]
  54. * string $_user['user_id']
  55. *
  56. * boolean $is_platformAdmin
  57. * boolean $is_allowedCreateCourse
  58. *
  59. * COURSE VARIABLES
  60. * see the function get_course_info_with_category
  61. * boolean $is_courseMember
  62. * boolean $is_courseTutor
  63. * boolean $is_courseAdmin
  64. *
  65. *
  66. * GROUP VARIABLES
  67. *
  68. * int $_gid (the group id)
  69. *
  70. *
  71. * IMPORTANT ADVICE FOR DEVELOPERS
  72. *
  73. * We strongly encourage developers to use a connection layer at the top of
  74. * their scripts rather than use these variables, as they are, inside the core
  75. * of their scripts. It will make code maintenance much easier.
  76. *
  77. * Many if the functions you need you can already find in the
  78. * main_api.lib.php
  79. *
  80. * We encourage you to use functions to access these global "kernel" variables.
  81. * You can add them to e.g. the main API library.
  82. *
  83. *
  84. * SCRIPT STRUCTURE
  85. *
  86. * 1. The script determines if there is an authentication attempt. This part
  87. * only chek if the login name and password are valid. Afterwards, it set the
  88. * $_user['user_id'] (user id) and the $uidReset flag. Other user informations are retrieved
  89. * later. It's also in this section that optional external authentication
  90. * devices step in.
  91. *
  92. * 2. The script determines what other session informations have to be set or
  93. * reset, setting correctly $cidReset (for course) and $gidReset (for group).
  94. *
  95. * 3. If needed, the script retrieves the other user informations (first name,
  96. * last name, ...) and stores them in session.
  97. *
  98. * 4. If needed, the script retrieves the course information and stores them
  99. * in session
  100. *
  101. * 5. The script initializes the user permission status and permission for the
  102. * course level
  103. *
  104. * 6. If needed, the script retrieves group informations an store them in
  105. * session.
  106. *
  107. * 7. The script initializes the user status and permission for the group level.
  108. *
  109. * @package chamilo.include
  110. */
  111. // Verified if exists the username and password in session current
  112. // Facebook connexion, if activated
  113. if (api_is_facebook_auth_activated() && !api_get_user_id()) {
  114. require_once api_get_path(SYS_PATH).'main/auth/external_login/facebook.inc.php';
  115. if (isset($facebook_config['appId']) && isset($facebook_config['secret'])) {
  116. facebookConnect();
  117. }
  118. }
  119. // Conditional login
  120. if (isset($_SESSION['conditional_login']['uid']) && $_SESSION['conditional_login']['can_login'] === true) {
  121. $uData = api_get_user_info($_SESSION['conditional_login']['uid']);
  122. ConditionalLogin::check_conditions($uData);
  123. $_user['user_id'] = $_SESSION['conditional_login']['uid'];
  124. $_user['status'] = $uData['status'];
  125. Session::write('_user', $_user);
  126. Session::erase('conditional_login');
  127. $uidReset = true;
  128. Event::eventLogin($_user['user_id']);
  129. }
  130. // parameters passed via GET
  131. $logout = isset($_GET['logout']) ? $_GET['logout'] : '';
  132. $gidReq = isset($_GET['gidReq']) ? (int) $_GET['gidReq'] : '';
  133. // Keep a trace of the course and session from which we are getting out, to
  134. // enable proper course logout tracking in courseLogout()
  135. $logoutInfo = [];
  136. if (!empty($logout) || !empty($cidReset)) {
  137. $uid = 0;
  138. if (!empty($_SESSION['_user']) && !empty($_SESSION['_user']['user_id'])) {
  139. $uid = $_SESSION['_user']['user_id'];
  140. }
  141. $cid = 0;
  142. if (!empty($_SESSION['_cid'])) {
  143. $cid = api_get_course_int_id($_SESSION['_cid']);
  144. }
  145. $logoutInfo = [
  146. 'uid' => $uid,
  147. 'cid' => $cid,
  148. 'sid' => api_get_session_id(),
  149. ];
  150. }
  151. $courseCodeFromSession = api_get_course_id();
  152. // $cidReq can be set in URL-parameter
  153. $cidReq = isset($_GET['cidReq']) ? Database::escape_string($_GET['cidReq']) : '';
  154. $cidReset = isset($cidReset) ? (bool) $cidReset : false;
  155. // $cDir is a special url param sent from a redirection from /courses/[DIR]/index.php...
  156. // It replaces cidReq in some opportunities
  157. $cDir = isset($_GET['cDir']) && !empty($_GET['cDir']) ? $_GET['cDir'] : '';
  158. // if there is a cDir parameter in the URL and $cidReq could not be determined
  159. if (!empty($cDir) && empty($cidReq)) {
  160. $courseCode = CourseManager::getCourseCodeFromDirectory($cDir);
  161. if (!empty($courseCode)) {
  162. $cidReq = $courseCode;
  163. }
  164. }
  165. if (empty($cidReq) && !empty($courseCodeFromSession)) {
  166. $cidReq = $courseCodeFromSession;
  167. }
  168. if (empty($cidReset)) {
  169. if ($courseCodeFromSession != $cidReq) {
  170. $cidReset = true;
  171. }
  172. } else {
  173. $cidReq = null;
  174. }
  175. $gidReset = isset($gidReset) ? $gidReset : '';
  176. // $gidReset can be set in URL-parameter
  177. // parameters passed via POST
  178. $login = isset($_POST["login"]) ? $_POST["login"] : '';
  179. // register if the user is just logging in, in order to redirect him
  180. $logging_in = false;
  181. /* MAIN CODE */
  182. if (!empty($_SESSION['_user']['user_id']) && !($login || $logout)) {
  183. // uid is in session => login already done, continue with this value
  184. $_user['user_id'] = $_SESSION['_user']['user_id'];
  185. //Check if we have to reset user data
  186. //This param can be used to reload user data if user has been logged by external script
  187. if (isset($_SESSION['_user']['uidReset']) && $_SESSION['_user']['uidReset']) {
  188. $uidReset = true;
  189. }
  190. } else {
  191. if (isset($_user['user_id'])) {
  192. unset($_user['user_id']);
  193. }
  194. $termsAndCondition = Session::read('term_and_condition');
  195. // Platform legal terms and conditions
  196. if (api_get_setting('allow_terms_conditions') === 'true' &&
  197. api_get_setting('load_term_conditions_section') === 'login'
  198. ) {
  199. if (isset($_POST['login']) && isset($_POST['password']) &&
  200. isset($termsAndCondition['user_id'])
  201. ) {
  202. // user id
  203. $user_id = $termsAndCondition['user_id'];
  204. // Update the terms & conditions
  205. $legal_type = null;
  206. //verify type of terms and conditions
  207. if (isset($_POST['legal_info'])) {
  208. $info_legal = explode(':', $_POST['legal_info']);
  209. $legal_type = LegalManager::get_type_of_terms_and_conditions(
  210. $info_legal[0],
  211. $info_legal[1]
  212. );
  213. }
  214. // is necessary verify check
  215. if ($legal_type == 1) {
  216. if ((isset($_POST['legal_accept']) && $_POST['legal_accept'] == '1')) {
  217. $legal_option = true;
  218. } else {
  219. $legal_option = false;
  220. }
  221. }
  222. // no is check option
  223. if ($legal_type == 0) {
  224. $legal_option = true;
  225. }
  226. if (isset($_POST['legal_accept_type']) && $legal_option === true) {
  227. $cond_array = explode(':', $_POST['legal_accept_type']);
  228. if (!empty($cond_array[0]) && !empty($cond_array[1])) {
  229. $time = time();
  230. $condition_to_save = intval($cond_array[0]).':'.intval($cond_array[1]).':'.$time;
  231. UserManager::update_extra_field_value(
  232. $user_id,
  233. 'legal_accept',
  234. $condition_to_save
  235. );
  236. }
  237. }
  238. }
  239. }
  240. // IF cas is activated and user isn't logged in
  241. if (api_get_setting('cas_activate') == 'true') {
  242. $cas_activated = true;
  243. } else {
  244. $cas_activated = false;
  245. }
  246. $cas_login = false;
  247. if ($cas_activated && !isset($_user['user_id']) && !isset($_POST['login']) && !$logout) {
  248. require_once api_get_path(SYS_PATH).'main/auth/cas/authcas.php';
  249. $cas_login = cas_is_authenticated();
  250. }
  251. if ((isset($_POST['login']) && isset($_POST['password'])) || ($cas_login)) {
  252. // $login && $password are given to log in
  253. if ($cas_login && empty($_POST['login'])) {
  254. $login = $cas_login;
  255. } else {
  256. $login = $_POST['login'];
  257. $password = $_POST['password'];
  258. }
  259. $userManager = UserManager::getManager();
  260. $userRepository = UserManager::getRepository();
  261. // Lookup the user in the main database
  262. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  263. $sql = "SELECT user_id, username, password, auth_source, active, expiration_date, status, salt
  264. FROM $user_table
  265. WHERE username = '".Database::escape_string($login)."'";
  266. $result = Database::query($sql);
  267. $captchaValidated = true;
  268. $captcha = api_get_setting('allow_captcha');
  269. $allowCaptcha = $captcha == 'true';
  270. if (Database::num_rows($result) > 0) {
  271. $uData = Database::fetch_array($result, 'ASSOC');
  272. if ($allowCaptcha) {
  273. // Checking captcha
  274. if (isset($_POST['captcha'])) {
  275. // Check captcha
  276. $captchaText = $_POST['captcha'];
  277. /** @var Text_CAPTCHA $obj */
  278. $obj = isset($_SESSION['template.lib']) ? $_SESSION['template.lib'] : null;
  279. if ($obj) {
  280. $obj->getPhrase();
  281. if ($obj->getPhrase() != $captchaText) {
  282. $captchaValidated = false;
  283. } else {
  284. $captchaValidated = true;
  285. }
  286. }
  287. if (isset($_SESSION['captcha_question'])) {
  288. $captcha_question = $_SESSION['captcha_question'];
  289. $captcha_question->destroy();
  290. }
  291. }
  292. // Redirect to login page
  293. if ($captchaValidated == false) {
  294. $loginFailed = true;
  295. Session::erase('_uid');
  296. Session::write('loginFailed', '1');
  297. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=wrong_captcha');
  298. exit;
  299. }
  300. // Check if account is blocked by captcha user extra field see function api_block_account_captcha()
  301. $blockedUntilDate = api_get_user_blocked_by_captcha($login);
  302. if (isset($blockedUntilDate) && !empty($blockedUntilDate)) {
  303. if (time() > api_strtotime($blockedUntilDate, 'UTC')) {
  304. api_clean_account_captcha($login);
  305. } else {
  306. $loginFailed = true;
  307. Session::erase('_uid');
  308. Session::write('loginFailed', '1');
  309. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=blocked_by_captcha');
  310. exit;
  311. }
  312. }
  313. }
  314. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE ||
  315. $uData['auth_source'] == CAS_AUTH_SOURCE
  316. ) {
  317. $validPassword = UserManager::isPasswordValid(
  318. $uData['password'],
  319. $password,
  320. $uData['salt']
  321. );
  322. $checkUserFromExternalWebservice = false;
  323. // If user can't connect directly to chamilo then check the webservice setting
  324. if ($validPassword === false) {
  325. // Use external webservice to
  326. $options = api_get_configuration_value('webservice_validation');
  327. if (!empty($options) && isset($options['options']) && !empty($options['options'])) {
  328. $options = $options['options'];
  329. $soapclient = new nusoap_client($options['wsdl']);
  330. $function = $options['check_login_function'];
  331. $params = [
  332. 'login' => $uData['username'],
  333. 'password' => $password,
  334. ];
  335. $result = $soapclient->call($function, [serialize($params)]);
  336. if ($error = $soapclient->getError()) {
  337. error_log('error');
  338. error_log(print_r($error, 1));
  339. } elseif ((int) $result === 1) {
  340. $checkUserFromExternalWebservice = true;
  341. }
  342. }
  343. }
  344. // Check the user's password
  345. if (($validPassword || $cas_login || $checkUserFromExternalWebservice) &&
  346. (trim($login) == $uData['username'])
  347. ) {
  348. // Means that the login was loaded in a different page than index.php
  349. // we force the reload of the course access using cidReset just in case
  350. if (isset($_REQUEST['redirect_after_not_allow_page']) &&
  351. $_REQUEST['redirect_after_not_allow_page'] == 1
  352. ) {
  353. $cidReset = true;
  354. }
  355. $update_type = UserManager::get_extra_user_data_by_field(
  356. $uData['user_id'],
  357. 'update_type'
  358. );
  359. $update_type = isset($update_type['update_type']) ? $update_type['update_type'] : '';
  360. if (!empty($extAuthSource[$update_type]['updateUser'])
  361. && file_exists($extAuthSource[$update_type]['updateUser'])
  362. ) {
  363. include_once $extAuthSource[$update_type]['updateUser'];
  364. }
  365. // Check if the account is active (not locked)
  366. if ($uData['active'] == '1') {
  367. // Check if the expiration date has not been reached
  368. if ($uData['expiration_date'] > date('Y-m-d H:i:s')
  369. || empty($uData['expiration_date'])
  370. ) {
  371. global $_configuration;
  372. if (api_is_multiple_url_enabled()) {
  373. // Check if user is an admin
  374. $my_user_is_admin = UserManager::is_admin($uData['user_id']);
  375. // This user is subscribed in these sites => $my_url_list
  376. $my_url_list = api_get_access_url_from_user($uData['user_id']);
  377. //Check the access_url configuration setting if
  378. // the user is registered in the access_url_rel_user table
  379. //Getting the current access_url_id of the platform
  380. $current_access_url_id = api_get_current_access_url_id();
  381. if ($my_user_is_admin === false) {
  382. // the user have the permissions to enter at this site
  383. if (is_array($my_url_list) &&
  384. in_array($current_access_url_id, $my_url_list)
  385. ) {
  386. ConditionalLogin::check_conditions($uData);
  387. $_user['user_id'] = $uData['user_id'];
  388. $_user['status'] = $uData['status'];
  389. Session::write('_user', $_user);
  390. Event::eventLogin($_user['user_id']);
  391. $logging_in = true;
  392. } else {
  393. $loginFailed = true;
  394. Session::erase('_uid');
  395. Session::write('loginFailed', '1');
  396. // Fix cas redirection loop
  397. // https://support.chamilo.org/issues/6124
  398. $location = api_get_path(WEB_PATH)
  399. .'index.php?loginFailed=1&error=access_url_inactive';
  400. if ($cas_login) {
  401. cas_logout(null, $location);
  402. Event::courseLogout($logoutInfo);
  403. } else {
  404. header('Location: '.$location);
  405. }
  406. exit;
  407. }
  408. } else {
  409. //Only admins of the "main" (first) Chamilo portal can login wherever they want
  410. if (in_array(1, $my_url_list)) {
  411. //Check if this admin have the access_url_id = 1 which means the principal
  412. ConditionalLogin::check_conditions($uData);
  413. $_user['user_id'] = $uData['user_id'];
  414. $_user['status'] = $uData['status'];
  415. Session::write('_user', $_user);
  416. Event::eventLogin($_user['user_id']);
  417. $logging_in = true;
  418. } else {
  419. //This means a secondary admin wants to login so we check as he's a normal user
  420. if (in_array($current_access_url_id, $my_url_list)) {
  421. $_user['user_id'] = $uData['user_id'];
  422. $_user['status'] = $uData['status'];
  423. Session::write('_user', $_user);
  424. Event::eventLogin($_user['user_id']);
  425. $logging_in = true;
  426. } else {
  427. $loginFailed = true;
  428. Session::erase('_uid');
  429. Session::write('loginFailed', '1');
  430. header(
  431. 'Location: '.api_get_path(WEB_PATH)
  432. .'index.php?loginFailed=1&error=access_url_inactive'
  433. );
  434. exit;
  435. }
  436. }
  437. }
  438. } else {
  439. ConditionalLogin::check_conditions($uData);
  440. $_user['user_id'] = $uData['user_id'];
  441. $_user['status'] = $uData['status'];
  442. Session::write('_user', $_user);
  443. Event::eventLogin($uData['user_id']);
  444. $logging_in = true;
  445. }
  446. } else {
  447. $loginFailed = true;
  448. Session::erase('_uid');
  449. Session::write('loginFailed', '1');
  450. header(
  451. 'Location: '.api_get_path(WEB_PATH)
  452. .'index.php?loginFailed=1&error=account_expired'
  453. );
  454. exit;
  455. }
  456. } else {
  457. $loginFailed = true;
  458. Session::erase('_uid');
  459. Session::write('loginFailed', '1');
  460. header(
  461. 'Location: '.api_get_path(WEB_PATH)
  462. .'index.php?loginFailed=1&error=account_inactive'
  463. );
  464. exit;
  465. }
  466. } else {
  467. // login failed: username or password incorrect
  468. $loginFailed = true;
  469. Session::erase('_uid');
  470. Session::write('loginFailed', '1');
  471. if ($allowCaptcha) {
  472. if (isset($_SESSION['loginFailedCount'])) {
  473. $_SESSION['loginFailedCount']++;
  474. } else {
  475. $_SESSION['loginFailedCount'] = 1;
  476. }
  477. $numberMistakesToBlockAccount = api_get_setting('captcha_number_mistakes_to_block_account');
  478. if (isset($_SESSION['loginFailedCount'])) {
  479. if ($_SESSION['loginFailedCount'] >= $numberMistakesToBlockAccount) {
  480. api_block_account_captcha($login);
  481. }
  482. }
  483. }
  484. header(
  485. 'Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=user_password_incorrect'
  486. );
  487. exit;
  488. }
  489. if (isset($uData['creator_id']) && $_user['user_id'] != $uData['creator_id']) {
  490. //first login for a not self registred
  491. //e.g. registered by a teacher
  492. //do nothing (code may be added later)
  493. }
  494. } elseif (!empty($extAuthSource[$uData['auth_source']]['login'])
  495. && file_exists($extAuthSource[$uData['auth_source']]['login'])
  496. ) {
  497. /*
  498. * Process external authentication
  499. * on the basis of the given login name
  500. */
  501. $loginFailed = true; // Default initialisation. It could
  502. // change after the external authentication
  503. $key = $uData['auth_source']; //'ldap','shibboleth'...
  504. // Check if organisationemail email exist for this user and replace the current login with
  505. $extraFieldValue = new ExtraFieldValue('user');
  506. $newLogin = $extraFieldValue->get_values_by_handler_and_field_variable(
  507. $uData['user_id'],
  508. 'organisationemail'
  509. );
  510. if (!empty($newLogin) && isset($newLogin['value'])) {
  511. $login = $newLogin['value'];
  512. }
  513. /* >>>>>>>> External authentication modules <<<<<<<<< */
  514. // see configuration.php to define these
  515. include_once $extAuthSource[$key]['login'];
  516. /* >>>>>>>> External authentication modules <<<<<<<<< */
  517. } else { // no standard Chamilo login - try external authentification
  518. //huh... nothing to do... we shouldn't get here
  519. error_log(
  520. 'Chamilo Authentication file defined in'.
  521. ' $extAuthSource could not be found - this might prevent'.
  522. ' your system from doing the corresponding authentication'.
  523. ' process',
  524. 0
  525. );
  526. }
  527. } else {
  528. $extraFieldValue = new ExtraFieldValue('user');
  529. $uData = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
  530. 'organisationemail',
  531. $login
  532. );
  533. if (!empty($uData)) {
  534. $uData = api_get_user_info($uData['item_id']);
  535. if (!empty($extAuthSource[$uData['auth_source']]['login'])
  536. && file_exists($extAuthSource[$uData['auth_source']]['login'])
  537. ) {
  538. /*
  539. * Process external authentication
  540. * on the basis of the given login name
  541. */
  542. $loginFailed = true; // Default initialisation. It could
  543. // change after the external authentication
  544. $key = $uData['auth_source']; //'ldap','shibboleth'...
  545. /* >>>>>>>> External authentication modules <<<<<<<<< */
  546. // see configuration.php to define these
  547. include_once $extAuthSource[$key]['login'];
  548. }
  549. } else {
  550. // change after the external authentication
  551. // login failed, Database::num_rows($result) <= 0
  552. $loginFailed = true; // Default initialisation. It could
  553. }
  554. // login failed, Database::num_rows($result) <= 0
  555. $loginFailed = true; // Default initialisation. It could
  556. // change after the external authentication
  557. /*
  558. * In this section:
  559. * there is no entry for the $login user in the Chamilo
  560. * database. This also means there is no auth_source for the user.
  561. * We let all external procedures attempt to add him/her
  562. * to the system.
  563. *
  564. * Process external login on the basis
  565. * of the authentication source list
  566. * provided by the configuration settings.
  567. * If the login succeeds, for going further,
  568. * Chamilo needs the $_user['user_id'] variable to be
  569. * set and registered in the session. It's the
  570. * responsability of the external login script
  571. * to provide this $_user['user_id'].
  572. */
  573. if (isset($extAuthSource) && is_array($extAuthSource)) {
  574. foreach ($extAuthSource as $thisAuthSource) {
  575. if (!empty($thisAuthSource['login']) && file_exists($thisAuthSource['login'])) {
  576. include_once $thisAuthSource['login'];
  577. }
  578. if (isset($thisAuthSource['newUser']) && file_exists($thisAuthSource['newUser'])) {
  579. include_once $thisAuthSource['newUser'];
  580. } else {
  581. error_log(
  582. 'Chamilo Authentication external file'.
  583. ' could not be found - this might prevent your system from using'.
  584. ' the authentication process in the user creation process',
  585. 0
  586. );
  587. }
  588. }
  589. } //end if is_array($extAuthSource)
  590. $checkUserInfo = Session::read('_user');
  591. if ($loginFailed && empty($checkUserInfo)) {
  592. //If we are here username given is wrong
  593. Session::write('loginFailed', '1');
  594. header(
  595. 'Location: '.api_get_path(WEB_PATH)
  596. .'index.php?loginFailed=1&error=user_password_incorrect'
  597. );
  598. exit;
  599. }
  600. } //end else login failed
  601. } elseif (api_get_setting('sso_authentication') === 'true'
  602. && !in_array('webservices', explode('/', $_SERVER['REQUEST_URI']))
  603. ) {
  604. /**
  605. * TODO:
  606. * - Work on a better validation for webservices paths. Current is very poor and exit.
  607. */
  608. $subsso = api_get_setting('sso_authentication_subclass');
  609. if (!empty($subsso)) {
  610. require_once api_get_path(SYS_CODE_PATH).'auth/sso/sso.'.$subsso.'.class.php';
  611. $subsso = 'sso'.$subsso;
  612. $osso = new $subsso(); //load the subclass
  613. } else {
  614. $osso = new sso();
  615. }
  616. if (isset($_SESSION['_user']['user_id'])) {
  617. if ($logout) {
  618. // Make custom redirect after logout
  619. online_logout($_SESSION['_user']['user_id'], false);
  620. Event::courseLogout($logoutInfo);
  621. $osso->logout(); //redirects and exits
  622. }
  623. } elseif (!$logout) {
  624. // Handle cookie from Master Server
  625. $forceSsoRedirect = api_get_setting('sso_force_redirect');
  626. if ($forceSsoRedirect === 'true') {
  627. // all users to be redirected unless they are connected (removed req on sso_cookie)
  628. $redirectToMasterConditions = !isset($_REQUEST['sso_referer']) && !isset($_GET['loginFailed']);
  629. } else {
  630. // Users to still see the homepage without connecting
  631. $redirectToMasterConditions = !isset($_REQUEST['sso_referer']) && !isset($_GET['loginFailed']) && isset($_GET['sso_cookie']);
  632. }
  633. if ($redirectToMasterConditions) {
  634. // Redirect to master server
  635. $osso->ask_master();
  636. } elseif (isset($_REQUEST['sso_cookie'])) {
  637. // Here we are going to check the origin of
  638. // what the call says should be used for
  639. // authentication, and ensure we know it
  640. $matches_domain = false;
  641. if (isset($_REQUEST['sso_referer'])) {
  642. $protocol = api_get_setting('sso_authentication_protocol');
  643. // sso_authentication_domain can list
  644. // several, comma-separated, domains
  645. $master_urls = preg_split('/,/', api_get_setting('sso_authentication_domain'));
  646. if (!empty($master_urls)) {
  647. $master_auth_uri = api_get_setting('sso_authentication_auth_uri');
  648. foreach ($master_urls as $mu) {
  649. if (empty($mu)) {
  650. continue;
  651. }
  652. // For each URL, check until we find *one* that matches the $_GET['sso_referer'],
  653. // then skip other possibilities
  654. // Do NOT compare the whole referer, as this might cause confusing errors with friendly urls,
  655. // like in Drupal /?q=user& vs /user?
  656. $referrer = substr($_REQUEST['sso_referer'], 0, strrpos($_REQUEST['sso_referer'], '/'));
  657. if ($protocol.trim($mu) === $referrer) {
  658. $matches_domain = true;
  659. break;
  660. }
  661. }
  662. } else {
  663. error_log(
  664. 'Your sso_authentication_master param is empty. '.
  665. 'Check the platform configuration, security section. '.
  666. 'It can be a list of comma-separated domains'
  667. );
  668. }
  669. }
  670. if ($matches_domain) {
  671. //make all the process of checking
  672. //if the user exists (delegated to the sso class)
  673. $osso->check_user();
  674. } else {
  675. error_log('Check the sso_referer URL in your script, it doesn\'t match any of the possibilities');
  676. //Request comes from unknown source
  677. $loginFailed = true;
  678. Session::erase('_uid');
  679. Session::write('loginFailed', '1');
  680. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=unrecognize_sso_origin');
  681. exit;
  682. }
  683. }
  684. //end logout ... else ... login
  685. } elseif ($logout) {
  686. //if there was an attempted logout without a previous login, log
  687. // this anonymous user out as well but avoid redirect
  688. online_logout(null, false);
  689. Event::courseLogout($logoutInfo);
  690. $osso->logout(); //redirects and exits
  691. }
  692. } elseif (api_get_setting('openid_authentication') == 'true') {
  693. if (!empty($_POST['openid_url'])) {
  694. include api_get_path(SYS_CODE_PATH).'auth/openid/login.php';
  695. openid_begin(trim($_POST['openid_url']), api_get_path(WEB_PATH).'index.php');
  696. //this last function should trigger a redirect, so we can die here safely
  697. die('Openid login redirection should be in progress');
  698. } elseif (!empty($_GET['openid_identity'])) { //it's usual for PHP to replace '.' (dot) by '_' (underscore) in URL parameters
  699. include api_get_path(SYS_CODE_PATH).'auth/openid/login.php';
  700. $res = openid_complete($_GET);
  701. if ($res['status'] == 'success') {
  702. $id1 = Database::escape_string($res['openid.identity']);
  703. //have another id with or without the final '/'
  704. $id2 = (substr($id1, -1, 1) == '/' ? substr($id1, 0, -1) : $id1.'/');
  705. //lookup the user in the main database
  706. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  707. $sql = "SELECT user_id, username, password, auth_source, active, expiration_date
  708. FROM $user_table
  709. WHERE openid = '$id1'
  710. OR openid = '$id2' ";
  711. $result = Database::query($sql);
  712. if ($result !== false) {
  713. if (Database::num_rows($result) > 0) {
  714. $uData = Database::fetch_array($result);
  715. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
  716. //the authentification of this user is managed by Chamilo itself
  717. // check if the account is active (not locked)
  718. if ($uData['active'] == '1') {
  719. // check if the expiration date has not been reached
  720. if ($uData['expiration_date'] > date('Y-m-d H:i:s')
  721. || empty($uData['expiration_date'])
  722. ) {
  723. $_user['user_id'] = $uData['user_id'];
  724. $_user['status'] = $uData['status'];
  725. Session::write('_user', $_user);
  726. Event::eventLogin($_user['user_id']);
  727. } else {
  728. $loginFailed = true;
  729. Session::erase('_uid');
  730. Session::write('loginFailed', '1');
  731. header('Location: index.php?loginFailed=1&error=account_expired');
  732. exit;
  733. }
  734. } else {
  735. $loginFailed = true;
  736. Session::erase('_uid');
  737. Session::write('loginFailed', '1');
  738. header('Location: index.php?loginFailed=1&error=account_inactive');
  739. exit;
  740. }
  741. if (isset($uData['creator_id']) && $_user['user_id'] != $uData['creator_id']) {
  742. //first login for a not self registred
  743. //e.g. registered by a teacher
  744. //do nothing (code may be added later)
  745. }
  746. }
  747. } else {
  748. // Redirect to the subscription form
  749. Session::write('loginFailed', '1');
  750. header(
  751. 'Location: '.api_get_path(WEB_CODE_PATH)
  752. .'auth/inscription.php?username='.$res['openid.sreg.nickname']
  753. .'&email='.$res['openid.sreg.email']
  754. .'&openid='.$res['openid.identity']
  755. .'&openid_msg=idnotfound'
  756. );
  757. exit;
  758. //$loginFailed = true;
  759. }
  760. } else {
  761. $loginFailed = true;
  762. }
  763. } else {
  764. $loginFailed = true;
  765. }
  766. }
  767. } elseif (KeyAuth::is_enabled()) {
  768. $success = KeyAuth::instance()->login();
  769. if ($success) {
  770. $use_anonymous = false;
  771. }
  772. }
  773. $uidReset = true;
  774. $cidReset = true;
  775. $gidReset = true;
  776. } // end else
  777. $maxAnons = api_get_configuration_value('max_anonymous_users');
  778. // Now check for anonymous user mode
  779. if (isset($use_anonymous) && $use_anonymous) {
  780. //if anonymous mode is set, then try to set the current user as anonymous
  781. //if he doesn't have a login yet
  782. $anonResult = api_set_anonymous();
  783. if ($maxAnons >= 2 && $anonResult) {
  784. $uidReset = true;
  785. Event::eventLogin($_user['user_id']);
  786. }
  787. } else {
  788. //if anonymous mode is not set, then check if this user is anonymous. If it
  789. //is, clean it from being anonymous (make him a nobody :-))
  790. api_clear_anonymous();
  791. }
  792. // if the requested course is different from the course in session
  793. if (!empty($cidReq) && (!isset($_SESSION['_cid']) ||
  794. (isset($_SESSION['_cid']) && $cidReq != $_SESSION['_cid']))
  795. ) {
  796. $cidReset = true;
  797. $gidReset = true; // As groups depend from courses, group id is reset
  798. }
  799. /* USER INIT */
  800. if (isset($uidReset) && $uidReset) {
  801. // session data refresh requested
  802. unset($_SESSION['_user']['uidReset']);
  803. $is_platformAdmin = false;
  804. $is_allowedCreateCourse = false;
  805. $isAnonymous = api_is_anonymous();
  806. if ($maxAnons >= 2) {
  807. $isAnonymous = false;
  808. }
  809. if (isset($_user['user_id']) && $_user['user_id'] && !$isAnonymous) {
  810. // a uid is given (log in succeeded)
  811. $_SESSION['loginFailed'] = false;
  812. unset($_SESSION['loginFailedCount']);
  813. unset($_SESSION['loginToBlock']);
  814. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  815. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  816. $track_e_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  817. $sql = "SELECT user.*, a.user_id is_admin, login.login_date
  818. FROM $user_table
  819. LEFT JOIN $admin_table a
  820. ON user.user_id = a.user_id
  821. LEFT JOIN $track_e_login login
  822. ON user.user_id = login.login_user_id
  823. WHERE user.user_id = '".$_user['user_id']."'
  824. ORDER BY login.login_date DESC LIMIT 1";
  825. $result = Database::query($sql);
  826. if (Database::num_rows($result) > 0) {
  827. // Extracting the user data
  828. $uData = Database::fetch_array($result);
  829. $_user = _api_format_user($uData, false);
  830. $is_platformAdmin = (bool) (!is_null($uData['is_admin']));
  831. $is_allowedCreateCourse = (bool) (($uData['status'] == COURSEMANAGER) || (api_get_setting('drhCourseManagerRights') && $uData['status'] == DRH));
  832. ConditionalLogin::check_conditions($uData);
  833. Session::write('_user', $_user);
  834. UserManager::update_extra_field_value($_user['user_id'], 'already_logged_in', 'true');
  835. Session::write('is_platformAdmin', $is_platformAdmin);
  836. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  837. } else {
  838. header('Location:'.api_get_path(WEB_PATH));
  839. exit;
  840. //exit("WARNING UNDEFINED UID !! ");
  841. }
  842. } else {
  843. if (!api_is_anonymous()) {
  844. // no uid => logout or Anonymous
  845. Session::erase('_user');
  846. Session::erase('_uid');
  847. }
  848. }
  849. Session::write('is_platformAdmin', $is_platformAdmin);
  850. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  851. } else { // continue with the previous values
  852. $_user = $_SESSION['_user'];
  853. $is_platformAdmin = isset($_SESSION['is_platformAdmin']) ? $_SESSION['is_platformAdmin'] : false;
  854. $is_allowedCreateCourse = isset($_SESSION['is_allowedCreateCourse']) ? $_SESSION['is_allowedCreateCourse'] : false;
  855. }
  856. $logoutCourseCalled = false;
  857. if (!isset($_SESSION['login_as'])) {
  858. $save_course_access = true;
  859. $_course = Session::read('_course');
  860. if ($_course && isset($_course['real_id'])) {
  861. // The value $_dont_save_user_course_access should be added before
  862. // the call of global.inc.php see the main/inc/chat.ajax.php file
  863. // Disables the updates in the TRACK_E_COURSE_ACCESS table
  864. if (isset($_dont_save_user_course_access) && $_dont_save_user_course_access == true) {
  865. $save_course_access = false;
  866. } else {
  867. $logoutCourseCalled = true;
  868. Event::courseLogout($logoutInfo);
  869. }
  870. }
  871. }
  872. $sessionIdFromGet = isset($_GET['id_session']) ? (int) $_GET['id_session'] : false;
  873. // if a session id has been given in url, we store the session if course was set:
  874. $sessionIdFromSession = api_get_session_id();
  875. $checkFromDatabase = false;
  876. // User change from session id
  877. if (($sessionIdFromGet !== false && $sessionIdFromGet !== $sessionIdFromSession) || $cidReset) {
  878. $cidReset = true;
  879. $checkFromDatabase = true;
  880. Session::erase('session_name');
  881. Session::erase('id_session');
  882. // Deleting session from $_SESSION means also deleting $_SESSION['_course'] and group info
  883. Session::erase('_real_cid');
  884. Session::erase('_cid');
  885. Session::erase('_course');
  886. Session::erase('_gid');
  887. }
  888. if ($checkFromDatabase && !empty($sessionIdFromGet)) {
  889. $sessionInfo = api_get_session_info($sessionIdFromGet);
  890. if (!empty($sessionInfo)) {
  891. Session::write('session_name', $sessionInfo['name']);
  892. Session::write('id_session', $sessionInfo['id']);
  893. } else {
  894. $cidReset = true;
  895. $gidReset = true;
  896. Session::erase('session_name');
  897. Session::erase('id_session');
  898. // Deleting session from $_SESSION means also deleting $_SESSION['_course'] and group info
  899. Session::erase('_real_cid');
  900. Session::erase('_cid');
  901. Session::erase('_course');
  902. Session::erase('_gid');
  903. api_not_allowed(true);
  904. }
  905. }
  906. /* COURSE INIT */
  907. if ($cidReset) {
  908. // Course session data refresh requested or empty data
  909. if ($cidReq) {
  910. $_course = api_get_course_info($cidReq);
  911. if (!empty($_course)) {
  912. //@TODO real_cid should be cid, for working with numeric course id
  913. $_real_cid = $_course['real_id'];
  914. $_cid = $_course['code'];
  915. Session::write('_real_cid', $_real_cid);
  916. Session::write('_cid', $_cid);
  917. Session::write('_course', $_course);
  918. if (!empty($_GET['gidReq'])) {
  919. $_SESSION['_gid'] = (int) $_GET['gidReq'];
  920. } else {
  921. Session::erase('_gid');
  922. }
  923. // Course login
  924. if (isset($_user['user_id'])) {
  925. Event::eventCourseLogin(
  926. api_get_course_int_id(),
  927. api_get_user_id(),
  928. api_get_session_id()
  929. );
  930. }
  931. } else {
  932. //exit("WARNING UNDEFINED CID !! ");
  933. header('Location:'.api_get_path(WEB_PATH));
  934. exit;
  935. }
  936. } else {
  937. // Leave a logout time in the track_e_course_access table if we were in a course
  938. if ($logoutCourseCalled == false) {
  939. Event::courseLogout($logoutInfo);
  940. }
  941. Session::erase('_cid');
  942. Session::erase('_real_cid');
  943. Session::erase('_course');
  944. Session::erase('session_name');
  945. Session::erase('id_session');
  946. if (!empty($_SESSION)) {
  947. foreach ($_SESSION as $key => $session_item) {
  948. if (strpos($key, 'lp_autolaunch_') === false) {
  949. continue;
  950. } else {
  951. if (isset($_SESSION[$key])) {
  952. Session::erase($key);
  953. }
  954. }
  955. }
  956. }
  957. if (api_get_group_id()) {
  958. Session::erase('_gid');
  959. }
  960. if (api_is_in_gradebook()) {
  961. api_remove_in_gradebook();
  962. }
  963. }
  964. } else {
  965. // Continue with the previous values
  966. if (empty($_SESSION['_course']) && !empty($_SESSION['_cid'])) {
  967. //Just in case $_course is empty we try to load if the c_id still exists
  968. $_course = api_get_course_info($_SESSION['_cid']);
  969. if (!empty($_course)) {
  970. $_real_cid = $_course['real_id'];
  971. $_cid = $_course['code'];
  972. Session::write('_real_cid', $_real_cid);
  973. Session::write('_cid', $_cid);
  974. Session::write('_course', $_course);
  975. }
  976. }
  977. if (empty($_SESSION['_course']) || empty($_SESSION['_cid'])) { //no previous values...
  978. $_cid = -1; // Set default values
  979. $_course = -1;
  980. } else {
  981. $_cid = $_SESSION['_cid'];
  982. $_course = $_SESSION['_course'];
  983. if (!empty($_REQUEST['gidReq'])) {
  984. $_SESSION['_gid'] = (int) $_REQUEST['gidReq'];
  985. $group_table = Database::get_course_table(TABLE_GROUP);
  986. $sql = "SELECT * FROM $group_table
  987. WHERE c_id = ".$_course['real_id']." AND id = '$gidReq'";
  988. $result = Database::query($sql);
  989. if (Database::num_rows($result) > 0) { // This group has recorded status related to this course
  990. $gpData = Database::fetch_array($result);
  991. $_gid = $gpData['id'];
  992. Session::write('_gid', $_gid);
  993. }
  994. } else {
  995. Session::write('_gid', 0);
  996. }
  997. }
  998. }
  999. /* COURSE / USER REL. INIT */
  1000. $session_id = api_get_session_id();
  1001. $user_id = isset($_user['user_id']) ? $_user['user_id'] : null;
  1002. //Course permissions
  1003. //if this code is uncommented in some platforms the is_courseAdmin is not correctly saved see BT#5789
  1004. /*$is_courseAdmin = false; //course teacher
  1005. $is_courseTutor = false; //course teacher - some rights
  1006. $is_courseMember = false; //course student
  1007. $is_session_general_coach = false; //course coach
  1008. */
  1009. // Course - User permissions
  1010. $is_sessionAdmin = false;
  1011. $is_session_general_coach = false; //course coach
  1012. $is_courseAdmin = false;
  1013. $is_courseTutor = false;
  1014. $is_courseMember = false;
  1015. if ((isset($uidReset) && $uidReset) || $cidReset) {
  1016. if (isset($_cid) && $_cid) {
  1017. $my_user_id = isset($user_id) ? (int) $user_id : 0;
  1018. $variable = 'accept_legal_'.$my_user_id.'_'.$_course['real_id'].'_'.$session_id;
  1019. $user_pass_open_course = false;
  1020. if (api_check_user_access_to_legal($_course['visibility']) && Session::read($variable)) {
  1021. $user_pass_open_course = true;
  1022. }
  1023. // Checking if the user filled the course legal agreement
  1024. if ($_course['activate_legal'] == 1 && !api_is_platform_admin() && !api_is_anonymous()) {
  1025. $user_is_subscribed = CourseManager::is_user_accepted_legal(
  1026. $user_id,
  1027. $_course['id'],
  1028. $session_id
  1029. ) || $user_pass_open_course;
  1030. if (!$user_is_subscribed) {
  1031. $url = api_get_path(WEB_CODE_PATH).'course_info/legal.php?course_code='.$_course['code'].'&session_id='.$session_id;
  1032. header('Location: '.$url);
  1033. exit;
  1034. }
  1035. }
  1036. // Platform legal terms and conditions
  1037. if (api_get_setting('allow_terms_conditions') === 'true' &&
  1038. api_get_setting('load_term_conditions_section') === 'course'
  1039. ) {
  1040. $termAndConditionStatus = api_check_term_condition($user_id);
  1041. // @todo not sure why we need the login password and update_term_status
  1042. if ($termAndConditionStatus === false) {
  1043. Session::write('term_and_condition', ['user_id' => $user_id]);
  1044. } else {
  1045. Session::erase('term_and_condition');
  1046. }
  1047. $termsAndCondition = Session::read('term_and_condition');
  1048. if (isset($termsAndCondition['user_id'])) {
  1049. // user id
  1050. $user_id = $termsAndCondition['user_id'];
  1051. // Update the terms & conditions
  1052. $legal_type = null;
  1053. // Verify type of terms and conditions
  1054. if (isset($_POST['legal_info'])) {
  1055. $info_legal = explode(':', $_POST['legal_info']);
  1056. $legal_type = LegalManager::get_type_of_terms_and_conditions(
  1057. $info_legal[0],
  1058. $info_legal[1]
  1059. );
  1060. }
  1061. // is necessary verify check
  1062. if ($legal_type === 1) {
  1063. if (isset($_POST['legal_accept']) && $_POST['legal_accept'] == '1') {
  1064. $legal_option = true;
  1065. } else {
  1066. $legal_option = false;
  1067. }
  1068. }
  1069. // no is check option
  1070. if ($legal_type == 0) {
  1071. $legal_option = true;
  1072. }
  1073. if (isset($_POST['legal_accept_type']) && $legal_option === true) {
  1074. $cond_array = explode(':', $_POST['legal_accept_type']);
  1075. if (!empty($cond_array[0]) && !empty($cond_array[1])) {
  1076. $time = time();
  1077. $condition_to_save = intval($cond_array[0]).':'.intval($cond_array[1]).':'.$time;
  1078. UserManager::update_extra_field_value(
  1079. $user_id,
  1080. 'legal_accept',
  1081. $condition_to_save
  1082. );
  1083. }
  1084. }
  1085. $redirect = true;
  1086. $allow = api_get_configuration_value('allow_public_course_with_no_terms_conditions');
  1087. if ($allow === true &&
  1088. isset($_course['visibility']) &&
  1089. $_course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  1090. ) {
  1091. $redirect = false;
  1092. }
  1093. if ($redirect && !api_is_platform_admin()) {
  1094. $url = api_get_path(WEB_CODE_PATH).'auth/inscription.php';
  1095. header('Location:'.$url);
  1096. exit;
  1097. }
  1098. }
  1099. }
  1100. }
  1101. if (isset($user_id) && $user_id && isset($_real_cid) && $_real_cid) {
  1102. // Check if user is subscribed in a course
  1103. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1104. $sql = "SELECT * FROM $course_user_table
  1105. WHERE
  1106. user_id = '".$user_id."' AND
  1107. relation_type <> ".COURSE_RELATION_TYPE_RRHH." AND
  1108. c_id = '$_real_cid'";
  1109. $result = Database::query($sql);
  1110. $cuData = null;
  1111. if (Database::num_rows($result) > 0) { // this user have a recorded state for this course
  1112. $cuData = Database::fetch_array($result, 'ASSOC');
  1113. $is_courseAdmin = $cuData['status'] == 1;
  1114. $is_courseTutor = $cuData['is_tutor'] == 1;
  1115. $is_courseMember = true;
  1116. }
  1117. // We are in a session course? Check session permissions
  1118. if (!empty($session_id)) {
  1119. if (!empty($session_id) && !empty($_course)) {
  1120. if (!SessionManager::relation_session_course_exist($session_id, $_course['real_id'])) {
  1121. api_not_allowed(true);
  1122. }
  1123. }
  1124. // I'm not the teacher of the course
  1125. if ($is_courseAdmin == false) {
  1126. // This user has no status related to this course
  1127. // The user is subscribed in a session? The user is a Session coach a Session admin?
  1128. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1129. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1130. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1131. // Session coach, session admin or course coach admin
  1132. $sql = "SELECT session.id_coach, session_admin_id, session_rcru.user_id
  1133. FROM $tbl_session session, $tbl_session_course_user session_rcru
  1134. WHERE
  1135. session_rcru.session_id = session.id AND
  1136. session_rcru.c_id = '$_real_cid' AND
  1137. session_rcru.user_id = '$user_id' AND
  1138. session_rcru.session_id = $session_id AND
  1139. session_rcru.status = 2
  1140. ";
  1141. $result = Database::query($sql);
  1142. $row = Database::store_result($result);
  1143. // Am I a session admin?
  1144. if (isset($row) && isset($row[0]) && $row[0]['session_admin_id'] == $user_id) {
  1145. $is_courseMember = false;
  1146. $is_courseTutor = false;
  1147. $is_courseAdmin = false;
  1148. $is_session_general_coach = false;
  1149. $is_sessionAdmin = true;
  1150. } else {
  1151. // Am I a session coach for this session?
  1152. $sql = "SELECT session.id, session.id_coach
  1153. FROM $tbl_session session
  1154. INNER JOIN $tbl_session_course sc
  1155. ON sc.session_id = session.id
  1156. WHERE session.id = $session_id
  1157. AND session.id_coach = $user_id
  1158. AND sc.c_id = '$_real_cid'";
  1159. $result = Database::query($sql);
  1160. if (Database::num_rows($result)) {
  1161. $is_courseMember = true;
  1162. $is_courseTutor = false;
  1163. $is_session_general_coach = true;
  1164. $is_sessionAdmin = false;
  1165. } else {
  1166. // Am I a course coach or a student?
  1167. $sql = "SELECT cu.user_id, cu.status
  1168. FROM $tbl_session_course_user cu
  1169. WHERE
  1170. c_id = '$_real_cid' AND
  1171. cu.user_id = '".$user_id."' AND
  1172. cu.session_id = '".$session_id."'
  1173. LIMIT 1";
  1174. $result = Database::query($sql);
  1175. if (Database::num_rows($result)) {
  1176. $row = Database::fetch_array($result, 'ASSOC');
  1177. $session_course_status = $row['status'];
  1178. switch ($session_course_status) {
  1179. case '2': // coach - teacher
  1180. $is_courseMember = true;
  1181. $is_courseTutor = true;
  1182. $is_session_general_coach = false;
  1183. $is_sessionAdmin = false;
  1184. if (api_get_setting('extend_rights_for_coach') == 'true') {
  1185. $is_courseAdmin = true;
  1186. } else {
  1187. $is_courseAdmin = false;
  1188. }
  1189. break;
  1190. case '0': //Student
  1191. $is_courseMember = true;
  1192. $is_courseTutor = false;
  1193. $is_courseAdmin = false;
  1194. $is_session_general_coach = false;
  1195. $is_sessionAdmin = false;
  1196. break;
  1197. default:
  1198. //unregister user
  1199. $is_courseMember = false;
  1200. $is_courseTutor = false;
  1201. $is_courseAdmin = false;
  1202. $is_sessionAdmin = false;
  1203. $is_session_general_coach = false;
  1204. break;
  1205. }
  1206. } else {
  1207. // Unregister user
  1208. $is_courseMember = false;
  1209. $is_courseTutor = false;
  1210. $is_courseAdmin = false;
  1211. $is_sessionAdmin = false;
  1212. $is_session_general_coach = false;
  1213. }
  1214. }
  1215. }
  1216. // Drh can enter to a course as an student see BT#6770
  1217. if (api_drh_can_access_all_session_content()) {
  1218. $sessionInfo = SessionManager::getSessionFollowedByDrh($user_id, $session_id);
  1219. if (!empty($sessionInfo) && !empty($sessionInfo['course_list'])) {
  1220. if (isset($sessionInfo['course_list'][$_course['real_id']])) {
  1221. $is_courseMember = true;
  1222. $is_courseTutor = false;
  1223. $is_session_general_coach = false;
  1224. $is_sessionAdmin = false;
  1225. }
  1226. }
  1227. }
  1228. }
  1229. // If I'm the admin platform i'm a teacher of the course
  1230. if ($is_platformAdmin) {
  1231. $is_courseAdmin = true;
  1232. }
  1233. } else {
  1234. // User has not access to the course
  1235. // This will check if the course was added in one of his sessions
  1236. // Then it will be redirected to that course-session
  1237. if ($is_courseMember === false && $is_platformAdmin === false) {
  1238. // Search session
  1239. $courseSession = SessionManager::searchCourseInSessionsFromUser(
  1240. $user_id,
  1241. $_course['real_id']
  1242. );
  1243. $priorityList = [];
  1244. if (!empty($courseSession)) {
  1245. foreach ($courseSession as $courseSessionItem) {
  1246. if (isset($courseSessionItem['session_id'])) {
  1247. $customSessionId = $courseSessionItem['session_id'];
  1248. $visibility = api_get_session_visibility($customSessionId, $_course['real_id']);
  1249. if ($visibility == SESSION_INVISIBLE) {
  1250. continue;
  1251. }
  1252. switch ($visibility) {
  1253. case SESSION_AVAILABLE:
  1254. $priorityList[1][] = $customSessionId;
  1255. break;
  1256. case SESSION_VISIBLE:
  1257. $priorityList[2][] = $customSessionId;
  1258. break;
  1259. case SESSION_VISIBLE_READ_ONLY:
  1260. $priorityList[3][] = $customSessionId;
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. }
  1266. if (!empty($priorityList)) {
  1267. ksort($priorityList);
  1268. foreach ($priorityList as $sessionList) {
  1269. if (empty($sessionList)) {
  1270. continue;
  1271. }
  1272. foreach ($sessionList as $customSessionId) {
  1273. $currentUrl = htmlentities($_SERVER['REQUEST_URI']);
  1274. $currentUrl = str_replace('id_session=0', '', $currentUrl);
  1275. $currentUrl = str_replace('&amp;', '&', $currentUrl);
  1276. if (strpos($currentUrl, '?') !== false) {
  1277. $currentUrl = rtrim($currentUrl, '&');
  1278. $url = $currentUrl.'&id_session='.$customSessionId;
  1279. } else {
  1280. $url = $currentUrl.'?id_session='.$customSessionId;
  1281. }
  1282. $url = str_replace('&&', '&', $url);
  1283. //$url = $_course['course_public_url'].'?id_session='.$customSessionId;
  1284. Session::erase('_real_cid');
  1285. Session::erase('_cid');
  1286. Session::erase('_course');
  1287. header('Location: '.$url);
  1288. exit;
  1289. }
  1290. }
  1291. }
  1292. }
  1293. }
  1294. } else { // keys missing => not anymore in the course - user relation
  1295. // course
  1296. $is_courseMember = false;
  1297. $is_courseAdmin = false;
  1298. $is_courseTutor = false;
  1299. $is_session_general_coach = false;
  1300. $is_sessionAdmin = false;
  1301. }
  1302. // Checking the course access
  1303. $is_allowed_in_course = false;
  1304. if (isset($_course) && isset($_course['visibility'])) {
  1305. switch ($_course['visibility']) {
  1306. case COURSE_VISIBILITY_OPEN_WORLD: //3
  1307. $is_allowed_in_course = true;
  1308. break;
  1309. case COURSE_VISIBILITY_OPEN_PLATFORM: //2
  1310. $userAccess = api_get_configuration_value('block_registered_users_access_to_open_course_contents');
  1311. // If this setting is not set or equals false, allow registered users to access content from any open
  1312. // course
  1313. if ($userAccess == false) {
  1314. if (isset($user_id) && !api_is_anonymous($user_id)) {
  1315. $is_allowed_in_course = true;
  1316. }
  1317. } else {
  1318. // If the setting == true, then only allow users to access the content of an open course if they are
  1319. // directly subscribed to the course (so first check the registration to the course)
  1320. $courseCode = $_course['code'];
  1321. $isUserSubscribedInCourse = CourseManager::is_user_subscribed_in_course(
  1322. $user_id,
  1323. $courseCode,
  1324. $session_id
  1325. );
  1326. if (isset($user_id) && ($is_platformAdmin || $isUserSubscribedInCourse === true) && !api_is_anonymous($user_id)) {
  1327. $is_allowed_in_course = true;
  1328. }
  1329. }
  1330. break;
  1331. case COURSE_VISIBILITY_REGISTERED: //1
  1332. if ($is_platformAdmin || $is_courseMember) {
  1333. $is_allowed_in_course = true;
  1334. }
  1335. break;
  1336. case COURSE_VISIBILITY_CLOSED: //0
  1337. if ($is_platformAdmin || $is_courseAdmin) {
  1338. $is_allowed_in_course = true;
  1339. }
  1340. break;
  1341. case COURSE_VISIBILITY_HIDDEN: //4
  1342. if ($is_platformAdmin) {
  1343. $is_allowed_in_course = true;
  1344. }
  1345. break;
  1346. }
  1347. }
  1348. if (!$is_platformAdmin) {
  1349. if (!$is_courseMember &&
  1350. isset($_course['registration_code']) &&
  1351. !empty($_course['registration_code']) &&
  1352. !Session::read('course_password_'.$_course['real_id'], false)
  1353. ) {
  1354. // if we are here we try to access to a course requiring password
  1355. if ($is_allowed_in_course) {
  1356. // the course visibility allows to access the course
  1357. // with a password
  1358. $url = api_get_path(WEB_CODE_PATH).'auth/set_temp_password.php?course_id='.$_course['real_id'].'&session_id='.$session_id;
  1359. header('Location: '.$url);
  1360. exit;
  1361. } else {
  1362. $is_courseMember = false;
  1363. $is_courseAdmin = false;
  1364. $is_courseTutor = false;
  1365. $is_session_general_coach = false;
  1366. $is_sessionAdmin = false;
  1367. $is_allowed_in_course = false;
  1368. }
  1369. }
  1370. } // check the session visibility
  1371. if ($is_allowed_in_course == true) {
  1372. //if I'm in a session
  1373. if ($session_id != 0) {
  1374. if (!$is_platformAdmin) {
  1375. // admin is not affected to the invisible session mode
  1376. $session_visibility = api_get_session_visibility($session_id);
  1377. switch ($session_visibility) {
  1378. case SESSION_INVISIBLE:
  1379. $is_allowed_in_course = false;
  1380. break;
  1381. }
  1382. }
  1383. }
  1384. }
  1385. // save the states
  1386. if (isset($is_courseAdmin)) {
  1387. Session::write('is_courseAdmin', $is_courseAdmin);
  1388. if ($is_courseAdmin) {
  1389. $is_allowed_in_course = true;
  1390. }
  1391. }
  1392. if (isset($is_courseMember)) {
  1393. Session::write('is_courseMember', $is_courseMember);
  1394. }
  1395. if (isset($is_courseTutor)) {
  1396. Session::write('is_courseTutor', $is_courseTutor);
  1397. if ($is_courseTutor) {
  1398. $is_allowed_in_course = true;
  1399. }
  1400. }
  1401. Session::write('is_session_general_coach', $is_session_general_coach);
  1402. Session::write('is_allowed_in_course', $is_allowed_in_course);
  1403. Session::write('is_sessionAdmin', $is_sessionAdmin);
  1404. } else {
  1405. // Continue with the previous values
  1406. $is_courseAdmin = isset($_SESSION['is_courseAdmin']) ? $_SESSION['is_courseAdmin'] : false;
  1407. $is_courseTutor = isset($_SESSION['is_courseTutor']) ? $_SESSION['is_courseTutor'] : false;
  1408. $is_session_general_coach = isset($_SESSION['is_session_general_coach']) ? $_SESSION['is_session_general_coach'] : false;
  1409. $is_courseMember = isset($_SESSION['is_courseMember']) ? $_SESSION['is_courseMember'] : false;
  1410. $is_allowed_in_course = isset($_SESSION['is_allowed_in_course']) ? $_SESSION['is_allowed_in_course'] : false;
  1411. }
  1412. //set variable according to student_view_enabled choices
  1413. if (api_get_setting('student_view_enabled') == "true") {
  1414. if (isset($_GET['isStudentView'])) {
  1415. if ($_GET['isStudentView'] == 'true') {
  1416. if (isset($_SESSION['studentview'])) {
  1417. if (!empty($_SESSION['studentview'])) {
  1418. // switching to studentview
  1419. $_SESSION['studentview'] = 'studentview';
  1420. }
  1421. }
  1422. } elseif ($_GET['isStudentView'] == 'false') {
  1423. if (isset($_SESSION['studentview'])) {
  1424. if (!empty($_SESSION['studentview'])) {
  1425. // switching to teacherview
  1426. $_SESSION['studentview'] = 'teacherview';
  1427. }
  1428. }
  1429. }
  1430. } elseif (!empty($_SESSION['studentview'])) {
  1431. //all is fine, no change to that, obviously
  1432. } elseif (empty($_SESSION['studentview'])) {
  1433. // We are in teacherview here
  1434. $_SESSION['studentview'] = 'teacherview';
  1435. }
  1436. }
  1437. if (isset($_cid)) {
  1438. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1439. $time = api_get_utc_datetime();
  1440. $sql = "UPDATE $tbl_course SET last_visit = '$time' WHERE code='$_cid'";
  1441. Database::query($sql);
  1442. }
  1443. // direct login to course
  1444. if ((isset($cas_login) && $cas_login && exist_firstpage_parameter()) ||
  1445. ($logging_in && exist_firstpage_parameter())
  1446. ) {
  1447. $redirectCourseDir = api_get_firstpage_parameter();
  1448. api_delete_firstpage_parameter(); // delete the cookie
  1449. if (!isset($_SESSION['request_uri'])) {
  1450. if (CourseManager::getCourseCodeFromDirectory($redirectCourseDir)) {
  1451. $_SESSION['noredirection'] = false;
  1452. $_SESSION['request_uri'] = api_get_path(WEB_COURSE_PATH).$redirectCourseDir.'/';
  1453. }
  1454. }
  1455. } elseif (api_user_is_login() && exist_firstpage_parameter()) {
  1456. $redirectCourseDir = api_get_firstpage_parameter();
  1457. api_delete_firstpage_parameter(); // delete the cookie
  1458. if (CourseManager::getCourseCodeFromDirectory($redirectCourseDir)) {
  1459. $_SESSION['noredirection'] = false;
  1460. $_SESSION['request_uri'] = api_get_path(WEB_COURSE_PATH).$redirectCourseDir.'/';
  1461. }
  1462. }
  1463. Event::eventCourseLoginUpdate(
  1464. api_get_course_int_id(),
  1465. api_get_user_id(),
  1466. api_get_session_id()
  1467. );
  1468. Redirect::session_request_uri($logging_in, $user_id);
  1469. if (!ChamiloApi::isAjaxRequest() && api_get_configuration_value('allow_mandatory_survey')) {
  1470. SurveyManager::protectByMandatory();
  1471. }