online.inc.php 18 KB

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