online.inc.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Code library for showing Who is online.
  6. *
  7. * @author Istvan Mandak, principal author
  8. * @author Denes Nagy, principal author
  9. * @author Bart Mollet
  10. * @author Roan Embrechts, cleaning and bugfixing
  11. *
  12. * @package chamilo.whoisonline
  13. */
  14. /**
  15. * Insert a login reference for the current user into the track_e_online stats table.
  16. * This table keeps trace of the last login. Nothing else matters (we don't keep traces of anything older).
  17. *
  18. * @param int user id
  19. */
  20. function LoginCheck($uid)
  21. {
  22. $uid = (int) $uid;
  23. if (!empty($uid)) {
  24. $online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  25. $_course = api_get_course_info();
  26. $user_ip = '';
  27. if (!empty($_SERVER['REMOTE_ADDR'])) {
  28. $user_ip = Database::escape_string(api_get_real_ip());
  29. }
  30. $login_date = api_get_utc_datetime();
  31. $access_url_id = 1;
  32. if (api_get_multiple_access_url() && api_get_current_access_url_id() != -1) {
  33. $access_url_id = api_get_current_access_url_id();
  34. }
  35. $session_id = api_get_session_id();
  36. // if the $_course array exists this means we are in a course and we have to store this in the who's online table also
  37. // to have the x users in this course feature working
  38. if (is_array($_course) && count($_course) > 0 && !empty($_course['id'])) {
  39. $query = "REPLACE INTO ".$online_table." (login_id,login_user_id,login_date,user_ip, c_id, session_id, access_url_id)
  40. VALUES ($uid,$uid,'$login_date','$user_ip', '".$_course['real_id']."' , '$session_id' , '$access_url_id' )";
  41. } else {
  42. $query = "REPLACE INTO ".$online_table." (login_id,login_user_id,login_date,user_ip, c_id, session_id, access_url_id)
  43. VALUES ($uid,$uid,'$login_date','$user_ip', 0, '$session_id', '$access_url_id')";
  44. }
  45. Database::query($query);
  46. }
  47. }
  48. /**
  49. * @param int $userId
  50. */
  51. function preventMultipleLogin($userId)
  52. {
  53. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  54. $userId = intval($userId);
  55. if (api_get_setting('prevent_multiple_simultaneous_login') === 'true') {
  56. if (!empty($userId) && !api_is_anonymous()) {
  57. $isFirstLogin = Session::read('first_user_login');
  58. if (empty($isFirstLogin)) {
  59. $sql = "SELECT login_id FROM $table
  60. WHERE login_user_id = $userId
  61. LIMIT 1";
  62. $result = Database::query($sql);
  63. $loginData = [];
  64. if (Database::num_rows($result)) {
  65. $loginData = Database::fetch_array($result);
  66. }
  67. $userIsReallyOnline = user_is_online($userId);
  68. // Trying double login.
  69. if (!empty($loginData) && $userIsReallyOnline == true) {
  70. session_regenerate_id();
  71. Session::destroy();
  72. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=multiple_connection_not_allowed');
  73. exit;
  74. } else {
  75. // First time
  76. Session::write('first_user_login', 1);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * This function handles the logout and is called whenever there is a $_GET['logout'].
  84. *
  85. * @param int $user_id
  86. * @param bool $logout_redirect
  87. *
  88. * @author Fernando P. García <fernando@develcuy.com>
  89. */
  90. function online_logout($user_id = null, $logout_redirect = false)
  91. {
  92. global $extAuthSource;
  93. // Database table definition
  94. $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  95. if (empty($user_id)) {
  96. $user_id = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
  97. }
  98. // Changing global chat status to offline
  99. if (api_is_global_chat_enabled()) {
  100. $chat = new Chat();
  101. $chat->setUserStatus(0);
  102. }
  103. $chat = new Chat();
  104. $chat->close();
  105. // selecting the last login of the user
  106. $sql = "SELECT login_id, login_date
  107. FROM $tbl_track_login
  108. WHERE login_user_id = $user_id
  109. ORDER BY login_date DESC
  110. LIMIT 0,1";
  111. $q_last_connection = Database::query($sql);
  112. $i_id_last_connection = 0;
  113. if (Database::num_rows($q_last_connection) > 0) {
  114. $i_id_last_connection = Database::result($q_last_connection, 0, "login_id");
  115. }
  116. if (!isset($_SESSION['login_as']) && !empty($i_id_last_connection)) {
  117. $current_date = api_get_utc_datetime();
  118. $sql = "UPDATE $tbl_track_login SET logout_date='".$current_date."'
  119. WHERE login_id='$i_id_last_connection'";
  120. Database::query($sql);
  121. }
  122. $logInfo = [
  123. 'tool' => 'logout',
  124. 'tool_id' => 0,
  125. 'tool_id_detail' => 0,
  126. ];
  127. Event::registerLog($logInfo);
  128. UserManager::loginDelete($user_id);
  129. //the following code enables the use of an external logout function.
  130. //example: define a $extAuthSource['ldap']['logout']="file.php" in configuration.php
  131. // then a function called ldap_logout() inside that file
  132. // (using *authent_name*_logout as the function name) and the following code
  133. // will find and execute it
  134. $uinfo = api_get_user_info($user_id);
  135. if (($uinfo['auth_source'] != PLATFORM_AUTH_SOURCE) && is_array($extAuthSource)) {
  136. if (is_array($extAuthSource[$uinfo['auth_source']])) {
  137. $subarray = $extAuthSource[$uinfo['auth_source']];
  138. if (!empty($subarray['logout']) && file_exists($subarray['logout'])) {
  139. require_once $subarray['logout'];
  140. $logout_function = $uinfo['auth_source'].'_logout';
  141. if (function_exists($logout_function)) {
  142. $logout_function($uinfo);
  143. }
  144. }
  145. }
  146. }
  147. // After logout redirect to
  148. $url = api_get_path(WEB_PATH).'index.php';
  149. if ($logout_redirect && api_get_plugin_setting('azure_active_directory', 'enable') == 'true') {
  150. if (ChamiloSession::read('_user_auth_source') === 'azure_active_directory') {
  151. $activeDirectoryPlugin = AzureActiveDirectory::create();
  152. $azureLogout = $activeDirectoryPlugin->getUrl(AzureActiveDirectory::URL_TYPE_LOGOUT);
  153. if (!empty($azureLogout)) {
  154. $url = $azureLogout;
  155. }
  156. }
  157. }
  158. api_delete_firstpage_parameter();
  159. Session::erase('last_id');
  160. CourseChatUtils::exitChat($user_id);
  161. session_regenerate_id();
  162. Session::destroy();
  163. $pluginKeycloak = api_get_plugin_setting('keycloak', 'tool_enable') === 'true';
  164. if ($pluginKeycloak && $uinfo['auth_source'] === 'keycloak') {
  165. $pluginUrl = api_get_path(WEB_PLUGIN_PATH).'keycloak/start.php?slo';
  166. header('Location: '.$pluginUrl);
  167. exit;
  168. }
  169. if ($logout_redirect) {
  170. header("Location: $url");
  171. exit;
  172. }
  173. }
  174. /**
  175. * @param int $user_id
  176. *
  177. * @return bool
  178. */
  179. function user_is_online($user_id)
  180. {
  181. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  182. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  183. $access_url_id = api_get_current_access_url_id();
  184. $time_limit = api_get_setting('time_limit_whosonline');
  185. $online_time = time() - $time_limit * 60;
  186. $limit_date = api_get_utc_datetime($online_time);
  187. $user_id = (int) $user_id;
  188. $query = " SELECT login_user_id, login_date
  189. FROM $track_online_table track
  190. INNER JOIN $table_user u
  191. ON (u.id=track.login_user_id)
  192. WHERE
  193. track.access_url_id = $access_url_id AND
  194. login_date >= '".$limit_date."' AND
  195. u.id = $user_id
  196. LIMIT 1 ";
  197. $result = Database::query($query);
  198. if (Database::num_rows($result)) {
  199. return true;
  200. }
  201. return false;
  202. }
  203. /**
  204. * Gives a list of people online now (and in the last $valid minutes).
  205. *
  206. * @param $from
  207. * @param $number_of_items
  208. * @param null $column
  209. * @param null $direction
  210. * @param null $time_limit
  211. * @param bool $friends
  212. *
  213. * @return array|bool For each line, a list of user IDs and login dates, or FALSE on error or empty results
  214. */
  215. function who_is_online(
  216. $from,
  217. $number_of_items,
  218. $column = null,
  219. $direction = null,
  220. $time_limit = null,
  221. $friends = false
  222. ) {
  223. // Time limit in seconds?
  224. if (empty($time_limit)) {
  225. $time_limit = api_get_setting('time_limit_whosonline');
  226. } else {
  227. $time_limit = intval($time_limit);
  228. }
  229. $from = intval($from);
  230. $number_of_items = intval($number_of_items);
  231. if (empty($column)) {
  232. $column = 'picture_uri';
  233. if ($friends) {
  234. $column = 'login_date';
  235. }
  236. }
  237. if (empty($direction)) {
  238. $direction = 'DESC';
  239. } else {
  240. if (!in_array(strtolower($direction), ['asc', 'desc'])) {
  241. $direction = 'DESC';
  242. }
  243. }
  244. $online_time = time() - $time_limit * 60;
  245. $current_date = api_get_utc_datetime($online_time);
  246. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  247. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  248. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  249. if ($friends) {
  250. // who friends from social network is online
  251. $query = "SELECT DISTINCT login_user_id, login_date
  252. FROM $track_online_table INNER JOIN $friend_user_table
  253. ON (friend_user_id = login_user_id)
  254. WHERE
  255. login_date >= '".$current_date."' AND
  256. friend_user_id <> '".api_get_user_id()."' AND
  257. relation_type='".USER_RELATION_TYPE_FRIEND."' AND
  258. user_id = '".api_get_user_id()."'
  259. ORDER BY $column $direction
  260. LIMIT $from, $number_of_items";
  261. } else {
  262. $query = "SELECT DISTINCT login_user_id, login_date
  263. FROM ".$track_online_table." e
  264. INNER JOIN ".$table_user." u ON (u.id = e.login_user_id)
  265. WHERE u.status != ".ANONYMOUS." AND login_date >= '".$current_date."'
  266. ORDER BY $column $direction
  267. LIMIT $from, $number_of_items";
  268. }
  269. if (api_get_multiple_access_url()) {
  270. $access_url_id = api_get_current_access_url_id();
  271. if ($access_url_id != -1) {
  272. if ($friends) {
  273. // friends from social network is online
  274. $query = "SELECT distinct login_user_id, login_date
  275. FROM $track_online_table track INNER JOIN $friend_user_table
  276. ON (friend_user_id = login_user_id)
  277. WHERE track.access_url_id = $access_url_id AND
  278. login_date >= '".$current_date."' AND
  279. friend_user_id <> '".api_get_user_id()."' AND
  280. relation_type='".USER_RELATION_TYPE_FRIEND."'
  281. ORDER BY $column $direction
  282. LIMIT $from, $number_of_items";
  283. } else {
  284. // all users online
  285. $query = "SELECT login_user_id, login_date
  286. FROM ".$track_online_table." track
  287. INNER JOIN ".$table_user." u
  288. ON (u.id=track.login_user_id)
  289. WHERE u.status != ".ANONYMOUS." AND track.access_url_id = $access_url_id AND
  290. login_date >= '".$current_date."'
  291. ORDER BY $column $direction
  292. LIMIT $from, $number_of_items";
  293. }
  294. }
  295. }
  296. //This query will show all registered users. Only for dev purposes.
  297. /*$query = "SELECT DISTINCT u.id as login_user_id, login_date
  298. FROM $track_online_table e, $table_user u
  299. GROUP by u.id
  300. ORDER BY $column $direction
  301. LIMIT $from, $number_of_items";*/
  302. $result = Database::query($query);
  303. if ($result) {
  304. $users_online = [];
  305. while (list($login_user_id, $login_date) = Database::fetch_row($result)) {
  306. $users_online[] = $login_user_id;
  307. }
  308. return $users_online;
  309. } else {
  310. return false;
  311. }
  312. }
  313. /**
  314. * @param string $time_limit
  315. */
  316. function who_is_online_count($time_limit = null, $friends = false)
  317. {
  318. if (empty($time_limit)) {
  319. $time_limit = api_get_setting('time_limit_whosonline');
  320. } else {
  321. $time_limit = intval($time_limit);
  322. }
  323. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  324. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  325. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  326. $online_time = time() - $time_limit * 60;
  327. $current_date = api_get_utc_datetime($online_time);
  328. if ($friends) {
  329. // who friends from social network is online
  330. $query = "SELECT DISTINCT count(login_user_id) as count
  331. FROM $track_online_table INNER JOIN $friend_user_table
  332. ON (friend_user_id = login_user_id)
  333. WHERE
  334. login_date >= '$current_date' AND
  335. friend_user_id <> '".api_get_user_id()."' AND
  336. relation_type='".USER_RELATION_TYPE_FRIEND."' AND
  337. user_id = '".api_get_user_id()."' ";
  338. } else {
  339. // All users online
  340. $query = "SELECT count(login_id) as count
  341. FROM $track_online_table track INNER JOIN $table_user u
  342. ON (u.id=track.login_user_id)
  343. WHERE u.status != ".ANONYMOUS." AND login_date >= '$current_date' ";
  344. }
  345. if (api_get_multiple_access_url()) {
  346. $access_url_id = api_get_current_access_url_id();
  347. if ($access_url_id != -1) {
  348. if ($friends) {
  349. // friends from social network is online
  350. $query = "SELECT DISTINCT count(login_user_id) as count
  351. FROM $track_online_table track
  352. INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  353. WHERE
  354. track.access_url_id = $access_url_id AND
  355. login_date >= '".$current_date."' AND
  356. friend_user_id <> '".api_get_user_id()."' AND
  357. relation_type='".USER_RELATION_TYPE_FRIEND."' ";
  358. } else {
  359. // all users online
  360. $query = "SELECT count(login_id) as count FROM $track_online_table track
  361. INNER JOIN $table_user u ON (u.id=track.login_user_id)
  362. WHERE
  363. u.status != ".ANONYMOUS." AND
  364. track.access_url_id = $access_url_id AND
  365. login_date >= '$current_date' ";
  366. }
  367. }
  368. }
  369. // Dev purposes show all users online
  370. /*$table_user = Database::get_main_table(TABLE_MAIN_USER);
  371. $query = "SELECT count(*) as count FROM ".$table_user;*/
  372. $result = Database::query($query);
  373. if (Database::num_rows($result) > 0) {
  374. $row = Database::fetch_array($result);
  375. return $row['count'];
  376. } else {
  377. return false;
  378. }
  379. }
  380. /**
  381. * Returns a list (array) of users who are online and in this course.
  382. *
  383. * @param int User ID
  384. * @param int Number of minutes
  385. * @param string Course code (could be empty, but then the function returns false)
  386. *
  387. * @return array Each line gives a user id and a login time
  388. */
  389. function who_is_online_in_this_course($from, $number_of_items, $uid, $time_limit, $course_code)
  390. {
  391. if (empty($course_code)) {
  392. return false;
  393. }
  394. $time_limit = (int) $time_limit;
  395. if (empty($time_limit)) {
  396. $time_limit = api_get_setting('time_limit_whosonline');
  397. }
  398. $online_time = time() - $time_limit * 60;
  399. $current_date = api_get_utc_datetime($online_time);
  400. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  401. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  402. $course_code = Database::escape_string($course_code);
  403. $courseInfo = api_get_course_info($course_code);
  404. $courseId = $courseInfo['real_id'];
  405. $from = (int) $from;
  406. $number_of_items = (int) $number_of_items;
  407. $urlCondition = '';
  408. $urlJoin = '';
  409. if (api_is_multiple_url_enabled()) {
  410. $accessUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  411. $urlId = api_get_current_access_url_id();
  412. $urlJoin = " INNER JOIN $accessUrlUser a ON (a.user_id = u.id) ";
  413. $urlCondition = " AND a.access_url_id = $urlId ";
  414. }
  415. $query = "SELECT o.login_user_id, o.login_date
  416. FROM $track_online_table o
  417. INNER JOIN $tableUser u
  418. ON (o.login_user_id = u.id)
  419. $urlJoin
  420. WHERE
  421. u.status <> '".ANONYMOUS."' AND
  422. o.c_id = $courseId AND
  423. o.login_date >= '$current_date'
  424. $urlCondition
  425. LIMIT $from, $number_of_items ";
  426. $result = Database::query($query);
  427. if ($result) {
  428. $users_online = [];
  429. while (list($login_user_id, $login_date) = Database::fetch_row($result)) {
  430. $users_online[] = $login_user_id;
  431. }
  432. return $users_online;
  433. } else {
  434. return false;
  435. }
  436. }
  437. /**
  438. * @param int $uid
  439. * @param string $time_limit
  440. */
  441. function who_is_online_in_this_course_count(
  442. $uid,
  443. $time_limit,
  444. $coursecode = null
  445. ) {
  446. if (empty($coursecode)) {
  447. return false;
  448. }
  449. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  450. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  451. $time_limit = Database::escape_string($time_limit);
  452. $online_time = time() - $time_limit * 60;
  453. $current_date = api_get_utc_datetime($online_time);
  454. $courseId = api_get_course_int_id($coursecode);
  455. if (empty($courseId)) {
  456. return false;
  457. }
  458. $urlCondition = '';
  459. $urlJoin = '';
  460. if (api_is_multiple_url_enabled()) {
  461. $accessUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  462. $urlId = api_get_current_access_url_id();
  463. $urlJoin = " INNER JOIN $accessUrlUser a ON (a.user_id = u.id) ";
  464. $urlCondition = " AND a.access_url_id = $urlId ";
  465. }
  466. $query = "SELECT count(login_user_id) as count
  467. FROM $track_online_table o
  468. INNER JOIN $tableUser u
  469. ON (login_user_id = u.id)
  470. $urlJoin
  471. WHERE
  472. u.status <> '".ANONYMOUS."' AND
  473. c_id = $courseId AND
  474. login_date >= '$current_date'
  475. $urlCondition
  476. ";
  477. $result = Database::query($query);
  478. if (Database::num_rows($result) > 0) {
  479. $row = Database::fetch_array($result);
  480. return $row['count'];
  481. } else {
  482. return false;
  483. }
  484. }
  485. /**
  486. * @param string $timeLimit
  487. * @param int $sessionId
  488. *
  489. * @return bool
  490. */
  491. function whoIsOnlineInThisSessionCount($timeLimit, $sessionId)
  492. {
  493. if (!$sessionId) {
  494. return 0;
  495. }
  496. $tblTrackOnline = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  497. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  498. $timeLimit = Database::escape_string($timeLimit);
  499. $online_time = time() - $timeLimit * 60;
  500. $current_date = api_get_utc_datetime($online_time);
  501. $urlCondition = '';
  502. $urlJoin = '';
  503. if (api_is_multiple_url_enabled()) {
  504. $accessUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  505. $urlId = api_get_current_access_url_id();
  506. $urlJoin = " INNER JOIN $accessUrlUser a ON (a.user_id = u.id) ";
  507. $urlCondition = " AND a.access_url_id = $urlId ";
  508. }
  509. $query = "SELECT count(login_user_id) as count
  510. FROM $tblTrackOnline o
  511. INNER JOIN $tableUser u
  512. ON (login_user_id = u.id)
  513. $urlJoin
  514. WHERE
  515. u.status <> '".ANONYMOUS."' AND
  516. session_id = $sessionId AND
  517. login_date >= '$current_date'
  518. $urlCondition
  519. ";
  520. $result = Database::query($query);
  521. if (Database::num_rows($result) > 0) {
  522. $row = Database::fetch_assoc($result);
  523. return $row['count'];
  524. }
  525. return 0;
  526. }