online.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Code library for showing Who is online
  5. *
  6. * @author Istvan Mandak, principal author
  7. * @author Denes Nagy, principal author
  8. * @author Bart Mollet
  9. * @author Roan Embrechts, cleaning and bugfixing
  10. * @package chamilo.whoisonline
  11. */
  12. /**
  13. * Insert a login reference for the current user into the track_e_online stats table.
  14. * This table keeps trace of the last login. Nothing else matters (we don't keep traces of anything older)
  15. * @param int user id
  16. * @return void
  17. */
  18. use ChamiloSession as Session;
  19. function LoginCheck($uid)
  20. {
  21. $_course = api_get_course_info();
  22. $uid = (int) $uid;
  23. $online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  24. if (!empty($uid)) {
  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, session_id, access_url_id)
  42. VALUES ($uid,$uid,'$login_date','$user_ip', '$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_settings('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 . " LIMIT 1";
  60. $result = Database::query($sql);
  61. $loginData = array();
  62. if (Database::num_rows($result)) {
  63. $loginData = Database::fetch_array($result);
  64. }
  65. $userIsReallyOnline = user_is_online($userId);
  66. // Trying double login.
  67. if (!empty($loginData) && $userIsReallyOnline == true) {
  68. session_regenerate_id();
  69. Session::destroy();
  70. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=multiple_connection_not_allowed');
  71. exit;
  72. } else {
  73. // First time
  74. Session::write('first_user_login', 1);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * This function handles the logout and is called whenever there is a $_GET['logout']
  82. * @return void Directly redirects the user or leaves him where he is, but doesn't return anything
  83. * @author Fernando P. García <fernando@develcuy.com>
  84. */
  85. function online_logout($user_id = null, $logout_redirect = false)
  86. {
  87. global $extAuthSource;
  88. // Database table definition
  89. $tbl_track_login = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  90. if (empty($user_id)) {
  91. $user_id = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
  92. }
  93. //Changing global chat status to offline
  94. if (api_is_global_chat_enabled()) {
  95. $chat = new Chat();
  96. $chat->setUserStatus(0);
  97. }
  98. // selecting the last login of the user
  99. $sql = "SELECT login_id, login_date
  100. FROM $tbl_track_login
  101. WHERE login_user_id = $user_id
  102. ORDER BY login_date DESC
  103. LIMIT 0,1";
  104. $q_last_connection = Database::query($sql);
  105. if (Database::num_rows($q_last_connection)>0) {
  106. $i_id_last_connection = Database::result($q_last_connection,0,"login_id");
  107. }
  108. if (!isset($_SESSION['login_as'])) {
  109. $current_date = api_get_utc_datetime();
  110. $sql = "UPDATE $tbl_track_login SET logout_date='".$current_date."'
  111. WHERE login_id='$i_id_last_connection'";
  112. Database::query($sql);
  113. }
  114. LoginDelete($user_id); //from inc/lib/online.inc.php - removes the "online" status
  115. //the following code enables the use of an external logout function.
  116. //example: define a $extAuthSource['ldap']['logout']="file.php" in configuration.php
  117. // then a function called ldap_logout() inside that file
  118. // (using *authent_name*_logout as the function name) and the following code
  119. // will find and execute it
  120. $uinfo = api_get_user_info($user_id);
  121. if (($uinfo['auth_source'] != PLATFORM_AUTH_SOURCE) && is_array($extAuthSource)) {
  122. if (is_array($extAuthSource[$uinfo['auth_source']])) {
  123. $subarray = $extAuthSource[$uinfo['auth_source']];
  124. if (!empty($subarray['logout']) && file_exists($subarray['logout'])) {
  125. require_once($subarray['logout']);
  126. $logout_function = $uinfo['auth_source'].'_logout';
  127. if (function_exists($logout_function)) {
  128. $logout_function($uinfo);
  129. }
  130. }
  131. }
  132. }
  133. require_once api_get_path(SYS_PATH) . 'main/chat/chat_functions.lib.php';
  134. exit_of_chat($user_id);
  135. session_regenerate_id();
  136. Session::destroy();
  137. if ($logout_redirect) {
  138. header("Location: index.php");
  139. return;
  140. }
  141. }
  142. /**
  143. * Remove all login records from the track_e_online stats table, for the given user ID.
  144. * @param int User ID
  145. * @return void
  146. */
  147. function LoginDelete($user_id)
  148. {
  149. $online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  150. $user_id = intval($user_id);
  151. $query = "DELETE FROM " . $online_table . " WHERE login_user_id = $user_id";
  152. Database::query($query);
  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 ON (u.id=track.login_user_id)
  170. WHERE
  171. track.access_url_id = $access_url_id AND
  172. login_date >= '".$limit_date."' AND
  173. u.id = $user_id
  174. LIMIT 1 ";
  175. $result = Database::query($query);
  176. if (Database::num_rows($result)) {
  177. return true;
  178. }
  179. return false;
  180. }
  181. /**
  182. * Gives a list of people online now (and in the last $valid minutes)
  183. * @return array For each line, a list of user IDs and login dates, or FALSE on error or empty results
  184. */
  185. function who_is_online($from, $number_of_items, $column = null, $direction = null, $time_limit = null, $friends = false)
  186. {
  187. // Time limit in seconds?
  188. if (empty($time_limit)) {
  189. $time_limit = api_get_setting('time_limit_whosonline');
  190. } else {
  191. $time_limit = intval($time_limit);
  192. }
  193. $from = intval($from);
  194. $number_of_items = intval($number_of_items);
  195. if (empty($column)) {
  196. $column = 'picture_uri';
  197. if ($friends) {
  198. $column = 'login_date';
  199. }
  200. }
  201. if (empty($direction)) {
  202. $direction = 'DESC';
  203. } else {
  204. if (!in_array(strtolower($direction), array('asc', 'desc'))) {
  205. $direction = 'DESC';
  206. }
  207. }
  208. $online_time = time() - $time_limit * 60;
  209. $current_date = api_get_utc_datetime($online_time);
  210. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  211. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  212. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  213. if ($friends) {
  214. // who friends from social network is online
  215. $query = "SELECT DISTINCT login_user_id, login_date
  216. FROM $track_online_table INNER JOIN $friend_user_table
  217. ON (friend_user_id = login_user_id)
  218. WHERE
  219. login_date >= '".$current_date."' AND
  220. friend_user_id <> '".api_get_user_id()."' AND
  221. relation_type='".USER_RELATION_TYPE_FRIEND."' AND
  222. user_id = '".api_get_user_id()."'
  223. ORDER BY $column $direction
  224. LIMIT $from, $number_of_items";
  225. } else {
  226. $query = "SELECT DISTINCT login_user_id, login_date
  227. FROM ".$track_online_table ." e
  228. INNER JOIN ".$table_user ." u ON (u.id = e.login_user_id)
  229. WHERE u.status != ".ANONYMOUS." AND login_date >= '".$current_date."'
  230. ORDER BY $column $direction
  231. LIMIT $from, $number_of_items";
  232. }
  233. if (api_get_multiple_access_url()) {
  234. $access_url_id = api_get_current_access_url_id();
  235. if ($access_url_id != -1) {
  236. if ($friends) {
  237. // friends from social network is online
  238. $query = "SELECT distinct login_user_id, login_date
  239. FROM $track_online_table track INNER JOIN $friend_user_table
  240. ON (friend_user_id = login_user_id)
  241. WHERE track.access_url_id = $access_url_id AND
  242. login_date >= '".$current_date."' AND
  243. friend_user_id <> '".api_get_user_id()."' AND
  244. relation_type='".USER_RELATION_TYPE_FRIEND."'
  245. ORDER BY $column $direction
  246. LIMIT $from, $number_of_items";
  247. } else {
  248. // all users online
  249. $query = "SELECT login_user_id, login_date
  250. FROM ".$track_online_table ." track
  251. INNER JOIN ".$table_user ." u
  252. ON (u.id=track.login_user_id)
  253. WHERE u.status != ".ANONYMOUS." AND track.access_url_id = $access_url_id AND
  254. login_date >= '".$current_date."'
  255. ORDER BY $column $direction
  256. LIMIT $from, $number_of_items";
  257. }
  258. }
  259. }
  260. //This query will show all registered users. Only for dev purposes.
  261. /*$query = "SELECT DISTINCT u.id as login_user_id, login_date FROM ".$track_online_table ." e , $table_user u
  262. GROUP by u.id
  263. ORDER BY $column $direction
  264. LIMIT $from, $number_of_items";*/
  265. $result = Database::query($query);
  266. if ($result) {
  267. $users_online = array();
  268. while (list($login_user_id, $login_date) = Database::fetch_row($result)) {
  269. $users_online[] = $login_user_id;
  270. }
  271. return $users_online;
  272. } else {
  273. return false;
  274. }
  275. }
  276. function who_is_online_count($time_limit = null, $friends = false)
  277. {
  278. if (empty($time_limit)) {
  279. $time_limit = api_get_setting('time_limit_whosonline');
  280. } else {
  281. $time_limit = intval($time_limit);
  282. }
  283. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  284. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  285. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  286. $online_time = time() - $time_limit * 60;
  287. $current_date = api_get_utc_datetime($online_time);
  288. if ($friends) {
  289. // who friends from social network is online
  290. $query = "SELECT DISTINCT count(login_user_id) as count
  291. FROM $track_online_table INNER JOIN $friend_user_table
  292. ON (friend_user_id = login_user_id)
  293. WHERE
  294. login_date >= '$current_date' AND
  295. friend_user_id <> '".api_get_user_id()."' AND
  296. relation_type='".USER_RELATION_TYPE_FRIEND."' AND
  297. user_id = '".api_get_user_id()."' ";
  298. } else {
  299. // All users online
  300. $query = "SELECT count(login_id) as count
  301. FROM $track_online_table track INNER JOIN $table_user u
  302. ON (u.id=track.login_user_id)
  303. WHERE u.status != ".ANONYMOUS." AND login_date >= '$current_date' ";
  304. }
  305. if (api_get_multiple_access_url()) {
  306. $access_url_id = api_get_current_access_url_id();
  307. if ($access_url_id != -1) {
  308. if ($friends) {
  309. // friends from social network is online
  310. $query = "SELECT DISTINCT count(login_user_id) as count
  311. FROM $track_online_table track
  312. INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  313. WHERE
  314. track.access_url_id = $access_url_id AND
  315. login_date >= '".$current_date."' AND
  316. friend_user_id <> '".api_get_user_id()."' AND
  317. relation_type='".USER_RELATION_TYPE_FRIEND."' ";
  318. } else {
  319. // all users online
  320. $query = "SELECT count(login_id) as count FROM $track_online_table track
  321. INNER JOIN $table_user u ON (u.id=track.login_user_id)
  322. WHERE
  323. u.status != ".ANONYMOUS." AND
  324. track.access_url_id = $access_url_id AND
  325. login_date >= '$current_date' ";
  326. }
  327. }
  328. }
  329. // Dev purposes show all users online
  330. /*$table_user = Database::get_main_table(TABLE_MAIN_USER);
  331. $query = "SELECT count(*) as count FROM ".$table_user;*/
  332. $result = Database::query($query);
  333. if (Database::num_rows($result) > 0) {
  334. $row = Database::fetch_array($result);
  335. return $row['count'];
  336. } else {
  337. return false;
  338. }
  339. }
  340. /**
  341. * Returns a list (array) of users who are online and in this course.
  342. * @param int User ID
  343. * @param int Number of minutes
  344. * @param string Course code (could be empty, but then the function returns false)
  345. * @return array Each line gives a user id and a login time
  346. */
  347. function who_is_online_in_this_course($from, $number_of_items, $uid, $time_limit, $course_code)
  348. {
  349. if (empty($course_code)) return false;
  350. if (empty($time_limit)) {
  351. $time_limit = api_get_setting('time_limit_whosonline');
  352. } else {
  353. $time_limit = intval($time_limit);
  354. }
  355. $online_time = time() - $time_limit*60;
  356. $current_date = api_get_utc_datetime($online_time);
  357. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  358. $course_code = Database::escape_string($course_code);
  359. $courseInfo = api_get_course_info($course_code);
  360. $courseId = $courseInfo['real_id'];
  361. $from = intval($from);
  362. $number_of_items = intval($number_of_items);
  363. $query = "SELECT login_user_id, login_date FROM $track_online_table
  364. WHERE login_user_id <> 2 AND c_id = $courseId AND login_date >= '$current_date'
  365. LIMIT $from, $number_of_items ";
  366. $result = Database::query($query);
  367. if ($result) {
  368. $users_online = array();
  369. while(list($login_user_id, $login_date) = Database::fetch_row($result)) {
  370. $users_online[] = $login_user_id;
  371. }
  372. return $users_online;
  373. } else {
  374. return false;
  375. }
  376. }
  377. function who_is_online_in_this_course_count($uid, $time_limit, $coursecode=null)
  378. {
  379. if (empty($coursecode)) {
  380. return false;
  381. }
  382. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  383. $coursecode = Database::escape_string($coursecode);
  384. $time_limit = Database::escape_string($time_limit);
  385. $online_time = time() - $time_limit * 60;
  386. $current_date = api_get_utc_datetime($online_time);
  387. $courseId = api_get_course_int_id($coursecode);
  388. if (empty($courseId)) {
  389. return false;
  390. }
  391. $query = "SELECT count(login_user_id) as count
  392. FROM $track_online_table
  393. WHERE login_user_id <> 2 AND c_id = $courseId AND login_date >= '$current_date' ";
  394. $result = Database::query($query);
  395. if (Database::num_rows($result) > 0) {
  396. $row = Database::fetch_array($result);
  397. return $row['count'];
  398. } else {
  399. return false;
  400. }
  401. }
  402. /**
  403. * Gets the full user name for a given user ID
  404. * @param int User ID
  405. * @return string The full username, elements separated by an HTML space
  406. * @deprecated user api_get_user_info($user_id)
  407. */
  408. function GetFullUserName($uid) {
  409. $uid = (int) $uid;
  410. $uid = intval($uid);
  411. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  412. $query = "SELECT firstname, lastname FROM ".$user_table." WHERE id=$uid";
  413. $result = @Database::query($query);
  414. if (count($result)>0) {
  415. $str = '';
  416. while(list($firstname,$lastname)= Database::fetch_array($result)) {
  417. $str = str_replace(' ', '&nbsp;', api_get_person_name($firstname, $lastname));
  418. return $str;
  419. }
  420. }
  421. }
  422. /**
  423. * Gets a list of chat calls made by others to the current user (info kept in main.user table)
  424. * @param none - taken from global space
  425. * @return string An HTML-formatted message
  426. */
  427. function chatcall() {
  428. $_cid = api_get_course_id();
  429. $_user = api_get_user_info();
  430. if (!$_user['user_id']) {
  431. return (false);
  432. }
  433. $userId = intval($_user['user_id']);
  434. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  435. $sql="SELECT chatcall_user_id, chatcall_date FROM $track_user_table
  436. WHERE ( id = $userId )";
  437. $result=Database::query($sql);
  438. $row=Database::fetch_array($result);
  439. $login_date=$row['chatcall_date'];
  440. $hour = substr($login_date,11,2);
  441. $minute = substr($login_date,14,2);
  442. $second = substr($login_date,17,2);
  443. $month = substr($login_date,5,2);
  444. $day = substr($login_date,8,2);
  445. $year = substr($login_date,0,4);
  446. $calltime = mktime($hour,$minute,$second,$month,$day,$year);
  447. $time = api_get_utc_datetime();
  448. $minute_passed=5; //within this limit, the chat call request is valid
  449. $limittime = mktime(date("H"),date("i")-$minute_passed,date("s"),date("m"),date("d"),date("Y"));
  450. if (($row['chatcall_user_id']) and ($calltime>$limittime)) {
  451. $webpath=api_get_path(WEB_CODE_PATH);
  452. $message=get_lang('YouWereCalled').' : '.GetFullUserName($row['chatcall_user_id'],'').'<br>'.get_lang('DoYouAccept')
  453. ."<p>"
  454. ."<a href=\"".$webpath."chat/chat.php?cidReq=".$_cid."&origin=whoisonlinejoin\">"
  455. . get_lang("Yes")
  456. ."</a>"
  457. ."&nbsp;&nbsp;|&nbsp;&nbsp;"
  458. ."<a href=\"".api_get_path(WEB_PATH)."webchatdeny.php\">"
  459. . get_lang("No")
  460. ."</a>"
  461. ."</p>";
  462. return($message);
  463. } else {
  464. return false;
  465. }
  466. }