login.lib.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User;
  4. use ChamiloSession as Session;
  5. /**
  6. * Class Login.
  7. *
  8. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  9. * @author Julio Montoya <gugli100@gmail.com>
  10. *
  11. * @package chamilo.login
  12. */
  13. class Login
  14. {
  15. /**
  16. * Get user account list.
  17. *
  18. * @param array $user array with keys: email, password, uid, loginName
  19. * @param bool $reset
  20. * @param bool $by_username
  21. *
  22. * @return string
  23. */
  24. public static function get_user_account_list($user, $reset = false, $by_username = false)
  25. {
  26. $portal_url = api_get_path(WEB_PATH);
  27. if (api_is_multiple_url_enabled()) {
  28. $access_url_id = api_get_current_access_url_id();
  29. if ($access_url_id != -1) {
  30. $url = api_get_access_url($access_url_id);
  31. $portal_url = $url['url'];
  32. }
  33. }
  34. if ($reset) {
  35. if ($by_username) {
  36. $secret_word = self::get_secret_word($user['email']);
  37. if ($reset) {
  38. $reset_link = $portal_url."main/auth/lostPassword.php?reset=".$secret_word."&id=".$user['uid'];
  39. $reset_link = Display::url($reset_link, $reset_link);
  40. } else {
  41. $reset_link = get_lang('Pass')." : $user[password]";
  42. }
  43. $user_account_list = get_lang('YourRegistrationData')." : \n".
  44. get_lang('UserName').' : '.$user['loginName']."\n".
  45. get_lang('ResetLink').' : '.$reset_link;
  46. if ($user_account_list) {
  47. $user_account_list = "\n-----------------------------------------------\n".$user_account_list;
  48. }
  49. } else {
  50. foreach ($user as $this_user) {
  51. $secret_word = self::get_secret_word($this_user['email']);
  52. if ($reset) {
  53. $reset_link = $portal_url."main/auth/lostPassword.php?reset=".$secret_word."&id=".$this_user['uid'];
  54. $reset_link = Display::url($reset_link, $reset_link);
  55. } else {
  56. $reset_link = get_lang('Pass')." : $this_user[password]";
  57. }
  58. $user_account_list[] =
  59. get_lang('YourRegistrationData')." : \n".
  60. get_lang('UserName').' : '.$this_user['loginName']."\n".
  61. get_lang('ResetLink').' : '.$reset_link;
  62. }
  63. if ($user_account_list) {
  64. $user_account_list = implode("\n-----------------------------------------------\n", $user_account_list);
  65. }
  66. }
  67. } else {
  68. if (!$by_username) {
  69. $user = $user[0];
  70. }
  71. $reset_link = get_lang('Pass')." : $user[password]";
  72. $user_account_list =
  73. get_lang('YourRegistrationData')." : \n".
  74. get_lang('UserName').' : '.$user['loginName']."\n".
  75. $reset_link.'';
  76. }
  77. return $user_account_list;
  78. }
  79. /**
  80. * This function sends the actual password to the user.
  81. *
  82. * @param int $user
  83. *
  84. * @return string
  85. *
  86. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  87. */
  88. public static function send_password_to_user($user, $by_username = false)
  89. {
  90. $email_subject = "[".api_get_setting('siteName')."] ".get_lang('LoginRequest'); // SUBJECT
  91. if ($by_username) { // Show only for lost password
  92. $user_account_list = self::get_user_account_list($user, false, $by_username); // BODY
  93. $email_to = $user['email'];
  94. } else {
  95. $user_account_list = self::get_user_account_list($user); // BODY
  96. $email_to = $user[0]['email'];
  97. }
  98. $portal_url = api_get_path(WEB_PATH);
  99. if (api_is_multiple_url_enabled()) {
  100. $access_url_id = api_get_current_access_url_id();
  101. if ($access_url_id != -1) {
  102. $url = api_get_access_url($access_url_id);
  103. $portal_url = $url['url'];
  104. }
  105. }
  106. $email_body = get_lang('YourAccountParam')." ".$portal_url."\n\n$user_account_list";
  107. // SEND MESSAGE
  108. $sender_name = api_get_person_name(
  109. api_get_setting('administratorName'),
  110. api_get_setting('administratorSurname'),
  111. null,
  112. PERSON_NAME_EMAIL_ADDRESS
  113. );
  114. $email_admin = api_get_setting('emailAdministrator');
  115. if (api_mail_html('', $email_to, $email_subject, $email_body, $sender_name, $email_admin) == 1) {
  116. return get_lang('YourPasswordHasBeenReset');
  117. } else {
  118. $admin_email = Display:: encrypted_mailto_link(
  119. api_get_setting('emailAdministrator'),
  120. api_get_person_name(
  121. api_get_setting('administratorName'),
  122. api_get_setting('administratorSurname')
  123. )
  124. );
  125. return sprintf(
  126. get_lang('ThisPlatformWasUnableToSendTheEmailPleaseContactXForMoreInformation'),
  127. $admin_email
  128. );
  129. }
  130. }
  131. /**
  132. * Handle encrypted password, send an email to a user with his password.
  133. *
  134. * @param int user id
  135. * @param bool $by_username
  136. *
  137. * @return string
  138. *
  139. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  140. */
  141. public static function handle_encrypted_password($user, $by_username = false)
  142. {
  143. $email_subject = "[".api_get_setting('siteName')."] ".get_lang('LoginRequest'); // SUBJECT
  144. if ($by_username) {
  145. // Show only for lost password
  146. $user_account_list = self::get_user_account_list($user, true, $by_username); // BODY
  147. $email_to = $user['email'];
  148. } else {
  149. $user_account_list = self::get_user_account_list($user, true); // BODY
  150. $email_to = $user[0]['email'];
  151. }
  152. $email_body = get_lang('DearUser')." :\n".get_lang('password_request')."\n";
  153. $email_body .= $user_account_list."\n-----------------------------------------------\n\n";
  154. $email_body .= get_lang('PasswordEncryptedForSecurity');
  155. $email_body .= "\n\n".
  156. get_lang('SignatureFormula').",\n".
  157. api_get_setting('administratorName')." ".
  158. api_get_setting('administratorSurname')."\n".
  159. get_lang('PlataformAdmin')." - ".
  160. api_get_setting('siteName');
  161. $sender_name = api_get_person_name(
  162. api_get_setting('administratorName'),
  163. api_get_setting('administratorSurname'),
  164. null,
  165. PERSON_NAME_EMAIL_ADDRESS
  166. );
  167. $email_admin = api_get_setting('emailAdministrator');
  168. $email_body = nl2br($email_body);
  169. $result = @api_mail_html(
  170. '',
  171. $email_to,
  172. $email_subject,
  173. $email_body,
  174. $sender_name,
  175. $email_admin
  176. );
  177. if ($result == 1) {
  178. return get_lang('YourPasswordHasBeenEmailed');
  179. } else {
  180. $admin_email = Display:: encrypted_mailto_link(
  181. api_get_setting('emailAdministrator'),
  182. api_get_person_name(
  183. api_get_setting('administratorName'),
  184. api_get_setting('administratorSurname')
  185. )
  186. );
  187. $message = sprintf(
  188. get_lang('ThisPlatformWasUnableToSendTheEmailPleaseContactXForMoreInformation'),
  189. $admin_email
  190. );
  191. return $message;
  192. }
  193. }
  194. /**
  195. * @param User $user
  196. */
  197. public static function sendResetEmail(User $user)
  198. {
  199. $uniqueId = api_get_unique_id();
  200. $user->setConfirmationToken($uniqueId);
  201. $user->setPasswordRequestedAt(new \DateTime());
  202. Database::getManager()->persist($user);
  203. Database::getManager()->flush();
  204. $url = api_get_path(WEB_CODE_PATH).'auth/reset.php?token='.$uniqueId;
  205. $mailSubject = get_lang('ResetPasswordInstructions');
  206. $mailBody = sprintf(
  207. get_lang('ResetPasswordCommentWithUrl'),
  208. $url
  209. );
  210. api_mail_html(
  211. UserManager::formatUserFullName($user),
  212. $user->getEmail(),
  213. $mailSubject,
  214. $mailBody
  215. );
  216. Display::addFlash(Display::return_message(get_lang('CheckYourEmailAndFollowInstructions')));
  217. }
  218. /**
  219. * Gets the secret word.
  220. *
  221. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  222. */
  223. public static function get_secret_word($add)
  224. {
  225. return $secret_word = sha1($add);
  226. }
  227. /**
  228. * Resets a password.
  229. *
  230. * @author Olivier Cauberghe <olivier.cauberghe@UGent.be>, Ghent University
  231. */
  232. public static function reset_password($secret, $id, $by_username = false)
  233. {
  234. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  235. $id = intval($id);
  236. $sql = "SELECT
  237. user_id AS uid,
  238. lastname AS lastName,
  239. firstname AS firstName,
  240. username AS loginName,
  241. password,
  242. email,
  243. auth_source
  244. FROM ".$tbl_user."
  245. WHERE user_id = $id";
  246. $result = Database::query($sql);
  247. $num_rows = Database::num_rows($result);
  248. if ($result && $num_rows > 0) {
  249. $user = Database::fetch_array($result);
  250. if ($user['auth_source'] == 'extldap') {
  251. return get_lang('CouldNotResetPassword');
  252. }
  253. } else {
  254. return get_lang('CouldNotResetPassword');
  255. }
  256. if (self::get_secret_word($user['email']) == $secret) {
  257. // OK, secret word is good. Now change password and mail it.
  258. $user['password'] = api_generate_password();
  259. UserManager::updatePassword($id, $user['password']);
  260. return self::send_password_to_user($user, $by_username);
  261. } else {
  262. return get_lang('NotAllowed');
  263. }
  264. }
  265. /**
  266. * @global bool $is_platformAdmin
  267. * @global bool $is_allowedCreateCourse
  268. * @global object $_user
  269. *
  270. * @param bool $reset
  271. */
  272. public static function init_user($user_id, $reset)
  273. {
  274. global $is_platformAdmin;
  275. global $is_allowedCreateCourse;
  276. global $_user;
  277. if (isset($reset) && $reset) { // session data refresh requested
  278. unset($_SESSION['_user']['uidReset']);
  279. $is_platformAdmin = false;
  280. $is_allowedCreateCourse = false;
  281. $_user['user_id'] = $user_id;
  282. if (isset($_user['user_id']) && $_user['user_id'] && !api_is_anonymous()) {
  283. // a uid is given (log in succeeded)
  284. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  285. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  286. $track_e_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  287. $sql = "SELECT user.*, a.user_id is_admin, UNIX_TIMESTAMP(login.login_date) login_date
  288. FROM $user_table
  289. LEFT JOIN $admin_table a
  290. ON user.user_id = a.user_id
  291. LEFT JOIN $track_e_login login
  292. ON user.user_id = login.login_user_id
  293. WHERE user.user_id = '".$_user['user_id']."'
  294. ORDER BY login.login_date DESC LIMIT 1";
  295. $result = Database::query($sql);
  296. if (Database::num_rows($result) > 0) {
  297. // Extracting the user data
  298. $uData = Database::fetch_array($result);
  299. $_user['firstName'] = $uData['firstname'];
  300. $_user['lastName'] = $uData['lastname'];
  301. $_user['mail'] = $uData['email'];
  302. $_user['official_code'] = $uData['official_code'];
  303. $_user['picture_uri'] = $uData['picture_uri'];
  304. $_user['user_id'] = $uData['user_id'];
  305. $_user['language'] = $uData['language'];
  306. $_user['auth_source'] = $uData['auth_source'];
  307. $_user['theme'] = $uData['theme'];
  308. $_user['status'] = $uData['status'];
  309. $is_platformAdmin = (bool) (!is_null($uData['is_admin']));
  310. $is_allowedCreateCourse = (bool) (($uData['status'] == 1) or (api_get_setting('drhCourseManagerRights') and $uData['status'] == 4));
  311. ConditionalLogin::check_conditions($uData);
  312. Session::write('_user', $_user);
  313. UserManager::update_extra_field_value($_user['user_id'], 'already_logged_in', 'true');
  314. Session::write('is_platformAdmin', $is_platformAdmin);
  315. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  316. } else {
  317. header('location:'.api_get_path(WEB_PATH));
  318. //exit("WARNING UNDEFINED UID !! ");
  319. }
  320. } else { // no uid => logout or Anonymous
  321. Session::erase('_user');
  322. Session::erase('_uid');
  323. }
  324. Session::write('is_platformAdmin', $is_platformAdmin);
  325. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  326. } else { // continue with the previous values
  327. $_user = $_SESSION['_user'];
  328. $is_platformAdmin = $_SESSION['is_platformAdmin'];
  329. $is_allowedCreateCourse = $_SESSION['is_allowedCreateCourse'];
  330. }
  331. }
  332. /**
  333. * @deprecated
  334. *
  335. * @global bool $is_platformAdmin
  336. * @global bool $is_allowedCreateCourse
  337. * @global object $_user
  338. * @global int $_cid
  339. * @global array $_course
  340. * @global int $_real_cid
  341. * @global type $_courseUser
  342. * @global type $is_courseAdmin
  343. * @global type $is_courseTutor
  344. * @global type $is_session_general_coach
  345. * @global type $is_courseMember
  346. * @global type $is_sessionAdmin
  347. * @global type $is_allowed_in_course
  348. *
  349. * @param type $course_id
  350. * @param bool $reset
  351. */
  352. public static function init_course($course_id, $reset)
  353. {
  354. global $is_platformAdmin;
  355. global $_user;
  356. global $_cid;
  357. global $_course;
  358. global $_real_cid;
  359. global $is_courseAdmin; //course teacher
  360. global $is_courseTutor; //course teacher - some rights
  361. global $is_session_general_coach; //course coach
  362. global $is_courseMember; //course student
  363. global $is_sessionAdmin;
  364. global $is_allowed_in_course;
  365. if ($reset) {
  366. // Course session data refresh requested or empty data
  367. if ($course_id) {
  368. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  369. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  370. $sql = "SELECT course.*, course_category.code faCode, course_category.name faName
  371. FROM $course_table
  372. LEFT JOIN $course_cat_table
  373. ON course.category_code = course_category.code
  374. WHERE course.code = '$course_id'";
  375. $result = Database::query($sql);
  376. if (Database::num_rows($result) > 0) {
  377. $course_data = Database::fetch_array($result);
  378. //@TODO real_cid should be cid, for working with numeric course id
  379. $_real_cid = $course_data['id'];
  380. $_cid = $course_data['code'];
  381. $_course = [];
  382. $_course['real_id'] = $course_data['id'];
  383. $_course['id'] = $course_data['code']; //auto-assigned integer
  384. $_course['code'] = $course_data['code'];
  385. $_course['name'] = $course_data['title'];
  386. $_course['title'] = $course_data['title'];
  387. $_course['official_code'] = $course_data['visual_code']; // use in echo
  388. $_course['sysCode'] = $course_data['code']; // use as key in db
  389. $_course['path'] = $course_data['directory']; // use as key in path
  390. $_course['titular'] = $course_data['tutor_name']; // this should be deprecated and use the table course_rel_user
  391. $_course['language'] = $course_data['course_language'];
  392. $_course['extLink']['url'] = $course_data['department_url'];
  393. $_course['extLink']['name'] = $course_data['department_name'];
  394. $_course['categoryCode'] = $course_data['faCode'];
  395. $_course['categoryName'] = $course_data['faName'];
  396. $_course['visibility'] = $course_data['visibility'];
  397. $_course['subscribe_allowed'] = $course_data['subscribe'];
  398. $_course['unsubscribe'] = $course_data['unsubscribe'];
  399. $_course['activate_legal'] = $course_data['activate_legal'];
  400. $_course['show_score'] = $course_data['show_score']; //used in the work tool
  401. Session::write('_cid', $_cid);
  402. Session::write('_course', $_course);
  403. //@TODO real_cid should be cid, for working with numeric course id
  404. Session::write('_real_cid', $_real_cid);
  405. // if a session id has been given in url, we store the session
  406. // Database Table Definitions
  407. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  408. if (!empty($_GET['id_session'])) {
  409. $_SESSION['id_session'] = intval($_GET['id_session']);
  410. $sql = 'SELECT name FROM '.$tbl_session.' WHERE id="'.intval($_SESSION['id_session']).'"';
  411. $rs = Database::query($sql);
  412. if ($rs != null) {
  413. list($_SESSION['session_name']) = Database::fetch_array($rs);
  414. }
  415. } else {
  416. Session::erase('session_name');
  417. Session::erase('id_session');
  418. }
  419. if (!isset($_SESSION['login_as'])) {
  420. // Course login
  421. if (isset($_user['user_id'])) {
  422. Event::eventCourseLogin(
  423. api_get_course_int_id(),
  424. $_user['user_id'],
  425. api_get_session_id()
  426. );
  427. }
  428. }
  429. } else {
  430. //exit("WARNING UNDEFINED CID !! ");
  431. header('location:'.api_get_path(WEB_PATH));
  432. }
  433. } else {
  434. Session::erase('_cid');
  435. Session::erase('_real_cid');
  436. Session::erase('_course');
  437. if (!empty($_SESSION)) {
  438. foreach ($_SESSION as $key => $session_item) {
  439. if (strpos($key, 'lp_autolaunch_') === false) {
  440. continue;
  441. } else {
  442. if (isset($_SESSION[$key])) {
  443. Session::erase($key);
  444. }
  445. }
  446. }
  447. }
  448. //Deleting session info
  449. if (api_get_session_id()) {
  450. Session::erase('id_session');
  451. Session::erase('session_name');
  452. }
  453. }
  454. } else {
  455. // Continue with the previous values
  456. if (empty($_SESSION['_course']) or empty($_SESSION['_cid'])) { //no previous values...
  457. $_cid = -1; //set default values that will be caracteristic of being unset
  458. $_course = -1;
  459. } else {
  460. $_cid = $_SESSION['_cid'];
  461. $_course = $_SESSION['_course'];
  462. // these lines are usefull for tracking. Indeed we can have lost the id_session and not the cid.
  463. // Moreover, if we want to track a course with another session it can be usefull
  464. if (!empty($_GET['id_session'])) {
  465. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  466. $sql = 'SELECT name FROM '.$tbl_session.' WHERE id="'.intval($_SESSION['id_session']).'"';
  467. $rs = Database::query($sql);
  468. if ($rs != null) {
  469. list($_SESSION['session_name']) = Database::fetch_array($rs);
  470. }
  471. $_SESSION['id_session'] = intval($_GET['id_session']);
  472. }
  473. if (!isset($_SESSION['login_as'])) {
  474. $save_course_access = true;
  475. //The value $_dont_save_user_course_access should be added before the call of global.inc.php see the main/inc/chat.ajax.php file
  476. //Disables the updates in the TRACK_E_COURSE_ACCESS table
  477. global $_dont_save_user_course_access;
  478. if (isset($_dont_save_user_course_access) && $_dont_save_user_course_access == true) {
  479. $save_course_access = false;
  480. } else {
  481. Event::courseLogout(
  482. [
  483. 'uid' => intval($_user['user_id']),
  484. 'cid' => api_get_course_int_id(),
  485. 'sid' => api_get_session_id(),
  486. ]
  487. );
  488. }
  489. }
  490. }
  491. }
  492. /* COURSE / USER REL. INIT */
  493. $session_id = api_get_session_id();
  494. $user_id = isset($_user['user_id']) ? $_user['user_id'] : null;
  495. //Course permissions
  496. $is_courseAdmin = false; //course teacher
  497. $is_courseTutor = false; //course teacher - some rights
  498. $is_courseMember = false; //course student
  499. //Course - User permissions
  500. $is_sessionAdmin = false;
  501. if ($reset) {
  502. if (isset($user_id) && $user_id && isset($_cid) && $_cid) {
  503. //Check if user is subscribed in a course
  504. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  505. $sql = "SELECT * FROM $course_user_table
  506. WHERE
  507. user_id = '".$user_id."' AND
  508. relation_type <> ".COURSE_RELATION_TYPE_RRHH." AND
  509. c_id = '".$_real_cid."'";
  510. $result = Database::query($sql);
  511. $cuData = null;
  512. if (Database::num_rows($result) > 0) {
  513. // this user have a recorded state for this course
  514. $cuData = Database::fetch_array($result, 'ASSOC');
  515. $is_courseAdmin = (bool) $cuData['status'] == 1;
  516. $is_courseTutor = (bool) $cuData['is_tutor'] == 1;
  517. $is_courseMember = true;
  518. // Checking if the user filled the course legal agreement
  519. if ($_course['activate_legal'] == 1 && !api_is_platform_admin()) {
  520. $user_is_subscribed = CourseManager::is_user_accepted_legal(
  521. $user_id,
  522. $_course['id'],
  523. $session_id
  524. );
  525. if (!$user_is_subscribed) {
  526. $url = api_get_path(WEB_CODE_PATH).'course_info/legal.php?course_code='.$_course['code'].'&session_id='.$session_id;
  527. header('Location: '.$url);
  528. exit;
  529. }
  530. }
  531. }
  532. //We are in a session course? Check session permissions
  533. if (!empty($session_id)) {
  534. //I'm not the teacher of the course
  535. if ($is_courseAdmin == false) {
  536. // this user has no status related to this course
  537. // The user is subscribed in a session? The user is a Session coach a Session admin ?
  538. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  539. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  540. //Session coach, session admin, course coach admin
  541. $sql = "SELECT session.id_coach, session_admin_id, session_rcru.user_id
  542. FROM $tbl_session session, $tbl_session_course_user session_rcru
  543. WHERE
  544. session_rcru.session_id = session.id AND
  545. session_rcru.c_id = '$_real_cid' AND
  546. session_rcru.user_id = '$user_id' AND
  547. session_rcru.session_id = $session_id AND
  548. session_rcru.status = 2";
  549. $result = Database::query($sql);
  550. $row = Database::store_result($result);
  551. //I'm a session admin?
  552. if (isset($row) && isset($row[0]) && $row[0]['session_admin_id'] == $user_id) {
  553. $is_courseMember = false;
  554. $is_courseTutor = false;
  555. $is_courseAdmin = false;
  556. $is_session_general_coach = false;
  557. $is_sessionAdmin = true;
  558. } else {
  559. //Im a coach or a student?
  560. $sql = "SELECT user_id, status
  561. FROM ".$tbl_session_course_user."
  562. WHERE
  563. c_id = '$_cid' AND
  564. user_id = '".$user_id."' AND
  565. session_id = '".$session_id."'
  566. LIMIT 1";
  567. $result = Database::query($sql);
  568. if (Database::num_rows($result)) {
  569. $row = Database::fetch_array($result, 'ASSOC');
  570. $session_course_status = $row['status'];
  571. switch ($session_course_status) {
  572. case '2': // coach - teacher
  573. $is_courseMember = true;
  574. $is_courseTutor = true;
  575. $is_session_general_coach = true;
  576. $is_sessionAdmin = false;
  577. if (api_get_setting('extend_rights_for_coach') == 'true') {
  578. $is_courseAdmin = true;
  579. } else {
  580. $is_courseAdmin = false;
  581. }
  582. break;
  583. case '0': //student
  584. $is_courseMember = true;
  585. $is_courseTutor = false;
  586. $is_courseAdmin = false;
  587. $is_sessionAdmin = false;
  588. break;
  589. default:
  590. //unregister user
  591. $is_courseMember = false;
  592. $is_courseTutor = false;
  593. $is_courseAdmin = false;
  594. $is_sessionAdmin = false;
  595. break;
  596. }
  597. } else {
  598. //unregister user
  599. $is_courseMember = false;
  600. $is_courseTutor = false;
  601. $is_courseAdmin = false;
  602. $is_sessionAdmin = false;
  603. }
  604. }
  605. }
  606. //If I'm the admin platform i'm a teacher of the course
  607. if ($is_platformAdmin) {
  608. $is_courseAdmin = true;
  609. }
  610. }
  611. } else { // keys missing => not anymore in the course - user relation
  612. // course
  613. $is_courseMember = false;
  614. $is_courseAdmin = false;
  615. $is_courseTutor = false;
  616. $is_session_general_coach = false;
  617. $is_sessionAdmin = false;
  618. }
  619. //Checking the course access
  620. $is_allowed_in_course = false;
  621. if (isset($_course)) {
  622. switch ($_course['visibility']) {
  623. case COURSE_VISIBILITY_OPEN_WORLD: //3
  624. $is_allowed_in_course = true;
  625. break;
  626. case COURSE_VISIBILITY_OPEN_PLATFORM: //2
  627. if (isset($user_id) && !api_is_anonymous($user_id)) {
  628. $is_allowed_in_course = true;
  629. }
  630. break;
  631. case COURSE_VISIBILITY_REGISTERED: //1
  632. if ($is_platformAdmin || $is_courseMember) {
  633. $is_allowed_in_course = true;
  634. }
  635. break;
  636. case COURSE_VISIBILITY_CLOSED: //0
  637. if ($is_platformAdmin || $is_courseAdmin) {
  638. $is_allowed_in_course = true;
  639. }
  640. break;
  641. case COURSE_VISIBILITY_HIDDEN: //4
  642. if ($is_platformAdmin) {
  643. $is_allowed_in_course = true;
  644. }
  645. break;
  646. }
  647. }
  648. // check the session visibility
  649. if ($is_allowed_in_course == true) {
  650. //if I'm in a session
  651. if ($session_id != 0) {
  652. if (!$is_platformAdmin) {
  653. // admin and session coach are *not* affected to the invisible session mode
  654. // the coach is not affected because he can log in some days after the end date of a session
  655. $session_visibility = api_get_session_visibility($session_id);
  656. switch ($session_visibility) {
  657. case SESSION_INVISIBLE:
  658. $is_allowed_in_course = false;
  659. break;
  660. }
  661. //checking date
  662. }
  663. }
  664. }
  665. // save the states
  666. Session::write('is_courseAdmin', $is_courseAdmin);
  667. Session::write('is_courseMember', $is_courseMember);
  668. Session::write('is_courseTutor', $is_courseTutor);
  669. Session::write('is_session_general_coach', $is_session_general_coach);
  670. Session::write('is_allowed_in_course', $is_allowed_in_course);
  671. Session::write('is_sessionAdmin', $is_sessionAdmin);
  672. } else {
  673. // continue with the previous values
  674. $is_courseAdmin = Session::read('is_courseAdmin');
  675. $is_courseTutor = Session::read('is_courseTutor');
  676. $is_session_general_coach = Session::read('is_session_general_coach');
  677. $is_courseMember = Session::read('is_courseMember');
  678. $is_allowed_in_course = Session::read('is_allowed_in_course');
  679. }
  680. }
  681. /**
  682. * @global int $_cid
  683. * @global array $_course
  684. * @global int $_gid
  685. *
  686. * @param int $group_id
  687. * @param bool $reset
  688. */
  689. public static function init_group($group_id, $reset)
  690. {
  691. global $_cid;
  692. global $_course;
  693. global $_gid;
  694. if ($reset) { // session data refresh requested
  695. if ($group_id && $_cid && !empty($_course['real_id'])) { // have keys to search data
  696. $group_table = Database::get_course_table(TABLE_GROUP);
  697. $sql = "SELECT * FROM $group_table WHERE c_id = ".$_course['real_id']." AND id = '$group_id'";
  698. $result = Database::query($sql);
  699. if (Database::num_rows($result) > 0) { // This group has recorded status related to this course
  700. $gpData = Database::fetch_array($result);
  701. $_gid = $gpData['id'];
  702. Session::write('_gid', $_gid);
  703. } else {
  704. Session::erase('_gid');
  705. }
  706. } elseif (isset($_SESSION['_gid']) || isset($_gid)) {
  707. // Keys missing => not anymore in the group - course relation
  708. Session::erase('_gid');
  709. }
  710. } elseif (isset($_SESSION['_gid'])) { // continue with the previous values
  711. $_gid = $_SESSION['_gid'];
  712. } else { //if no previous value, assign caracteristic undefined value
  713. $_gid = -1;
  714. }
  715. //set variable according to student_view_enabled choices
  716. if (api_get_setting('student_view_enabled') == "true") {
  717. if (isset($_GET['isStudentView'])) {
  718. if ($_GET['isStudentView'] == 'true') {
  719. if (isset($_SESSION['studentview'])) {
  720. if (!empty($_SESSION['studentview'])) {
  721. // switching to studentview
  722. $_SESSION['studentview'] = 'studentview';
  723. }
  724. }
  725. } elseif ($_GET['isStudentView'] == 'false') {
  726. if (isset($_SESSION['studentview'])) {
  727. if (!empty($_SESSION['studentview'])) {
  728. // switching to teacherview
  729. $_SESSION['studentview'] = 'teacherview';
  730. }
  731. }
  732. }
  733. //} elseif (!empty($_SESSION['studentview'])) {
  734. //all is fine, no change to that, obviously
  735. } elseif (empty($_SESSION['studentview'])) {
  736. // We are in teacherview here
  737. $_SESSION['studentview'] = 'teacherview';
  738. }
  739. }
  740. }
  741. /**
  742. * Returns true if user exists in the platform when asking the password.
  743. *
  744. * @param string $username (email or username)
  745. *
  746. * @return array|bool
  747. */
  748. public static function get_user_accounts_by_username($username)
  749. {
  750. if (strpos($username, '@')) {
  751. $username = api_strtolower($username);
  752. $email = true;
  753. } else {
  754. $username = api_strtolower($username);
  755. $email = false;
  756. }
  757. if ($email) {
  758. $condition = "LOWER(email) = '".Database::escape_string($username)."' ";
  759. } else {
  760. $condition = "LOWER(username) = '".Database::escape_string($username)."'";
  761. }
  762. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  763. $query = "SELECT
  764. user_id AS uid,
  765. lastname AS lastName,
  766. firstname AS firstName,
  767. username AS loginName,
  768. password,
  769. email,
  770. status AS status,
  771. official_code,
  772. phone,
  773. picture_uri,
  774. creator_id,
  775. auth_source
  776. FROM $tbl_user
  777. WHERE ( $condition AND active = 1) ";
  778. $result = Database::query($query);
  779. $num_rows = Database::num_rows($result);
  780. if ($result && $num_rows > 0) {
  781. return Database::fetch_assoc($result);
  782. }
  783. return false;
  784. }
  785. }