online.inc.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. $activeDirectoryPlugin = AzureActiveDirectory::create();
  151. $azureLogout = $activeDirectoryPlugin->getUrl(AzureActiveDirectory::URL_TYPE_SIGNOUT);
  152. if (!empty($azureLogout)) {
  153. $url = $azureLogout;
  154. }
  155. }
  156. api_delete_firstpage_parameter();
  157. Session::erase('last_id');
  158. CourseChatUtils::exitChat($user_id);
  159. session_regenerate_id();
  160. Session::destroy();
  161. $pluginKeycloak = api_get_plugin_setting('keycloak', 'tool_enable') === 'true';
  162. if ($pluginKeycloak && $uinfo['auth_source'] === 'keycloak') {
  163. $pluginUrl = api_get_path(WEB_PLUGIN_PATH).'keycloak/start.php?slo';
  164. header('Location: '.$pluginUrl);
  165. exit;
  166. }
  167. if ($logout_redirect) {
  168. header("Location: $url");
  169. exit;
  170. }
  171. }
  172. /**
  173. * @param int $user_id
  174. *
  175. * @return bool
  176. */
  177. function user_is_online($user_id)
  178. {
  179. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  180. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  181. $access_url_id = api_get_current_access_url_id();
  182. $time_limit = api_get_setting('time_limit_whosonline');
  183. $online_time = time() - $time_limit * 60;
  184. $limit_date = api_get_utc_datetime($online_time);
  185. $user_id = (int) $user_id;
  186. $query = " SELECT login_user_id, login_date
  187. FROM $track_online_table track
  188. INNER JOIN $table_user u
  189. ON (u.id=track.login_user_id)
  190. WHERE
  191. track.access_url_id = $access_url_id AND
  192. login_date >= '".$limit_date."' AND
  193. u.id = $user_id
  194. LIMIT 1 ";
  195. $result = Database::query($query);
  196. if (Database::num_rows($result)) {
  197. return true;
  198. }
  199. return false;
  200. }
  201. /**
  202. * Gives a list of people online now (and in the last $valid minutes).
  203. *
  204. * @param $from
  205. * @param $number_of_items
  206. * @param null $column
  207. * @param null $direction
  208. * @param null $time_limit
  209. * @param bool $friends
  210. *
  211. * @return array|bool For each line, a list of user IDs and login dates, or FALSE on error or empty results
  212. */
  213. function who_is_online(
  214. $from,
  215. $number_of_items,
  216. $column = null,
  217. $direction = null,
  218. $time_limit = null,
  219. $friends = false
  220. ) {
  221. // Time limit in seconds?
  222. if (empty($time_limit)) {
  223. $time_limit = api_get_setting('time_limit_whosonline');
  224. } else {
  225. $time_limit = intval($time_limit);
  226. }
  227. $from = intval($from);
  228. $number_of_items = intval($number_of_items);
  229. if (empty($column)) {
  230. $column = 'picture_uri';
  231. if ($friends) {
  232. $column = 'login_date';
  233. }
  234. }
  235. if (empty($direction)) {
  236. $direction = 'DESC';
  237. } else {
  238. if (!in_array(strtolower($direction), ['asc', 'desc'])) {
  239. $direction = 'DESC';
  240. }
  241. }
  242. $online_time = time() - $time_limit * 60;
  243. $current_date = api_get_utc_datetime($online_time);
  244. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  245. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  246. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  247. if ($friends) {
  248. // who friends from social network is online
  249. $query = "SELECT DISTINCT login_user_id, login_date
  250. FROM $track_online_table INNER JOIN $friend_user_table
  251. ON (friend_user_id = login_user_id)
  252. WHERE
  253. login_date >= '".$current_date."' AND
  254. friend_user_id <> '".api_get_user_id()."' AND
  255. relation_type='".USER_RELATION_TYPE_FRIEND."' AND
  256. user_id = '".api_get_user_id()."'
  257. ORDER BY $column $direction
  258. LIMIT $from, $number_of_items";
  259. } else {
  260. $query = "SELECT DISTINCT login_user_id, login_date
  261. FROM ".$track_online_table." e
  262. INNER JOIN ".$table_user." u ON (u.id = e.login_user_id)
  263. WHERE u.status != ".ANONYMOUS." AND login_date >= '".$current_date."'
  264. ORDER BY $column $direction
  265. LIMIT $from, $number_of_items";
  266. }
  267. if (api_get_multiple_access_url()) {
  268. $access_url_id = api_get_current_access_url_id();
  269. if ($access_url_id != -1) {
  270. if ($friends) {
  271. // friends from social network is online
  272. $query = "SELECT distinct login_user_id, login_date
  273. FROM $track_online_table track INNER JOIN $friend_user_table
  274. ON (friend_user_id = login_user_id)
  275. WHERE track.access_url_id = $access_url_id AND
  276. login_date >= '".$current_date."' AND
  277. friend_user_id <> '".api_get_user_id()."' AND
  278. relation_type='".USER_RELATION_TYPE_FRIEND."'
  279. ORDER BY $column $direction
  280. LIMIT $from, $number_of_items";
  281. } else {
  282. // all users online
  283. $query = "SELECT login_user_id, login_date
  284. FROM ".$track_online_table." track
  285. INNER JOIN ".$table_user." u
  286. ON (u.id=track.login_user_id)
  287. WHERE u.status != ".ANONYMOUS." AND track.access_url_id = $access_url_id AND
  288. login_date >= '".$current_date."'
  289. ORDER BY $column $direction
  290. LIMIT $from, $number_of_items";
  291. }
  292. }
  293. }
  294. //This query will show all registered users. Only for dev purposes.
  295. /*$query = "SELECT DISTINCT u.id as login_user_id, login_date
  296. FROM $track_online_table e, $table_user u
  297. GROUP by u.id
  298. ORDER BY $column $direction
  299. LIMIT $from, $number_of_items";*/
  300. $result = Database::query($query);
  301. if ($result) {
  302. $users_online = [];
  303. while (list($login_user_id, $login_date) = Database::fetch_row($result)) {
  304. $users_online[] = $login_user_id;
  305. }
  306. return $users_online;
  307. } else {
  308. return false;
  309. }
  310. }
  311. /**
  312. * @param string $time_limit
  313. */
  314. function who_is_online_count($time_limit = null, $friends = false)
  315. {
  316. if (empty($time_limit)) {
  317. $time_limit = api_get_setting('time_limit_whosonline');
  318. } else {
  319. $time_limit = intval($time_limit);
  320. }
  321. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  322. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  323. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  324. $online_time = time() - $time_limit * 60;
  325. $current_date = api_get_utc_datetime($online_time);
  326. if ($friends) {
  327. // who friends from social network is online
  328. $query = "SELECT DISTINCT count(login_user_id) as count
  329. FROM $track_online_table INNER JOIN $friend_user_table
  330. ON (friend_user_id = login_user_id)
  331. WHERE
  332. login_date >= '$current_date' AND
  333. friend_user_id <> '".api_get_user_id()."' AND
  334. relation_type='".USER_RELATION_TYPE_FRIEND."' AND
  335. user_id = '".api_get_user_id()."' ";
  336. } else {
  337. // All users online
  338. $query = "SELECT count(login_id) as count
  339. FROM $track_online_table track INNER JOIN $table_user u
  340. ON (u.id=track.login_user_id)
  341. WHERE u.status != ".ANONYMOUS." AND login_date >= '$current_date' ";
  342. }
  343. if (api_get_multiple_access_url()) {
  344. $access_url_id = api_get_current_access_url_id();
  345. if ($access_url_id != -1) {
  346. if ($friends) {
  347. // friends from social network is online
  348. $query = "SELECT DISTINCT count(login_user_id) as count
  349. FROM $track_online_table track
  350. INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  351. WHERE
  352. track.access_url_id = $access_url_id AND
  353. login_date >= '".$current_date."' AND
  354. friend_user_id <> '".api_get_user_id()."' AND
  355. relation_type='".USER_RELATION_TYPE_FRIEND."' ";
  356. } else {
  357. // all users online
  358. $query = "SELECT count(login_id) as count FROM $track_online_table track
  359. INNER JOIN $table_user u ON (u.id=track.login_user_id)
  360. WHERE
  361. u.status != ".ANONYMOUS." AND
  362. track.access_url_id = $access_url_id AND
  363. login_date >= '$current_date' ";
  364. }
  365. }
  366. }
  367. // Dev purposes show all users online
  368. /*$table_user = Database::get_main_table(TABLE_MAIN_USER);
  369. $query = "SELECT count(*) as count FROM ".$table_user;*/
  370. $result = Database::query($query);
  371. if (Database::num_rows($result) > 0) {
  372. $row = Database::fetch_array($result);
  373. return $row['count'];
  374. } else {
  375. return false;
  376. }
  377. }
  378. /**
  379. * Returns a list (array) of users who are online and in this course.
  380. *
  381. * @param int User ID
  382. * @param int Number of minutes
  383. * @param string Course code (could be empty, but then the function returns false)
  384. *
  385. * @return array Each line gives a user id and a login time
  386. */
  387. function who_is_online_in_this_course($from, $number_of_items, $uid, $time_limit, $course_code)
  388. {
  389. if (empty($course_code)) {
  390. return false;
  391. }
  392. $time_limit = (int) $time_limit;
  393. if (empty($time_limit)) {
  394. $time_limit = api_get_setting('time_limit_whosonline');
  395. }
  396. $online_time = time() - $time_limit * 60;
  397. $current_date = api_get_utc_datetime($online_time);
  398. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  399. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  400. $course_code = Database::escape_string($course_code);
  401. $courseInfo = api_get_course_info($course_code);
  402. $courseId = $courseInfo['real_id'];
  403. $from = (int) $from;
  404. $number_of_items = (int) $number_of_items;
  405. $urlCondition = '';
  406. $urlJoin = '';
  407. if (api_is_multiple_url_enabled()) {
  408. $accessUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  409. $urlId = api_get_current_access_url_id();
  410. $urlJoin = " INNER JOIN $accessUrlUser a ON (a.user_id = u.id) ";
  411. $urlCondition = " AND a.access_url_id = $urlId ";
  412. }
  413. $query = "SELECT o.login_user_id, o.login_date
  414. FROM $track_online_table o
  415. INNER JOIN $tableUser u
  416. ON (o.login_user_id = u.id)
  417. $urlJoin
  418. WHERE
  419. u.status <> '".ANONYMOUS."' AND
  420. o.c_id = $courseId AND
  421. o.login_date >= '$current_date'
  422. $urlCondition
  423. LIMIT $from, $number_of_items ";
  424. $result = Database::query($query);
  425. if ($result) {
  426. $users_online = [];
  427. while (list($login_user_id, $login_date) = Database::fetch_row($result)) {
  428. $users_online[] = $login_user_id;
  429. }
  430. return $users_online;
  431. } else {
  432. return false;
  433. }
  434. }
  435. /**
  436. * @param int $uid
  437. * @param string $time_limit
  438. */
  439. function who_is_online_in_this_course_count(
  440. $uid,
  441. $time_limit,
  442. $coursecode = null
  443. ) {
  444. if (empty($coursecode)) {
  445. return false;
  446. }
  447. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  448. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  449. $time_limit = Database::escape_string($time_limit);
  450. $online_time = time() - $time_limit * 60;
  451. $current_date = api_get_utc_datetime($online_time);
  452. $courseId = api_get_course_int_id($coursecode);
  453. if (empty($courseId)) {
  454. return false;
  455. }
  456. $urlCondition = '';
  457. $urlJoin = '';
  458. if (api_is_multiple_url_enabled()) {
  459. $accessUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  460. $urlId = api_get_current_access_url_id();
  461. $urlJoin = " INNER JOIN $accessUrlUser a ON (a.user_id = u.id) ";
  462. $urlCondition = " AND a.access_url_id = $urlId ";
  463. }
  464. $query = "SELECT count(login_user_id) as count
  465. FROM $track_online_table o
  466. INNER JOIN $tableUser u
  467. ON (login_user_id = u.id)
  468. $urlJoin
  469. WHERE
  470. u.status <> '".ANONYMOUS."' AND
  471. c_id = $courseId AND
  472. login_date >= '$current_date'
  473. $urlCondition
  474. ";
  475. $result = Database::query($query);
  476. if (Database::num_rows($result) > 0) {
  477. $row = Database::fetch_array($result);
  478. return $row['count'];
  479. } else {
  480. return false;
  481. }
  482. }
  483. /**
  484. * @param string $timeLimit
  485. * @param int $sessionId
  486. *
  487. * @return bool
  488. */
  489. function whoIsOnlineInThisSessionCount($timeLimit, $sessionId)
  490. {
  491. if (!$sessionId) {
  492. return 0;
  493. }
  494. $tblTrackOnline = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  495. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  496. $timeLimit = Database::escape_string($timeLimit);
  497. $online_time = time() - $timeLimit * 60;
  498. $current_date = api_get_utc_datetime($online_time);
  499. $urlCondition = '';
  500. $urlJoin = '';
  501. if (api_is_multiple_url_enabled()) {
  502. $accessUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  503. $urlId = api_get_current_access_url_id();
  504. $urlJoin = " INNER JOIN $accessUrlUser a ON (a.user_id = u.id) ";
  505. $urlCondition = " AND a.access_url_id = $urlId ";
  506. }
  507. $query = "SELECT count(login_user_id) as count
  508. FROM $tblTrackOnline o
  509. INNER JOIN $tableUser u
  510. ON (login_user_id = u.id)
  511. $urlJoin
  512. WHERE
  513. u.status <> '".ANONYMOUS."' AND
  514. session_id = $sessionId AND
  515. login_date >= '$current_date'
  516. $urlCondition
  517. ";
  518. $result = Database::query($query);
  519. if (Database::num_rows($result) > 0) {
  520. $row = Database::fetch_assoc($result);
  521. return $row['count'];
  522. }
  523. return 0;
  524. }