online.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. global $_course, $_configuration;
  21. $uid = (int) $uid;
  22. $online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  23. if (!empty($uid)) {
  24. $login_ip = '';
  25. if (!empty($_SERVER['REMOTE_ADDR'])) {
  26. $login_ip = Database::escape_string($_SERVER['REMOTE_ADDR']);
  27. }
  28. $login_date = api_get_utc_datetime();
  29. $access_url_id = 1;
  30. if (api_get_multiple_access_url() && api_get_current_access_url_id()!=-1) {
  31. $access_url_id = api_get_current_access_url_id();
  32. }
  33. $session_id = api_get_session_id();
  34. // 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
  35. // to have the x users in this course feature working
  36. if (is_array($_course) && count($_course)>0 && !empty($_course['id'])) {
  37. $query = "REPLACE INTO ".$online_table ." (login_id,login_user_id,login_date,login_ip, course, session_id, access_url_id) VALUES ($uid,$uid,'$login_date','$login_ip', '".$_course['id']."' , '$session_id' , '$access_url_id' )";
  38. } else {
  39. $query = "REPLACE INTO ".$online_table ." (login_id,login_user_id,login_date,login_ip, session_id, access_url_id) VALUES ($uid,$uid,'$login_date','$login_ip', '$session_id', '$access_url_id')";
  40. }
  41. @Database::query($query);
  42. }
  43. }
  44. /**
  45. * @param int $userId
  46. */
  47. function preventMultipleLogin($userId)
  48. {
  49. global $_configuration;
  50. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  51. $userId = intval($userId);
  52. if (isset($_configuration['prevent_multiple_simultaneous_login']) &&
  53. $_configuration['prevent_multiple_simultaneous_login']
  54. ) {
  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. global $_configuration, $extAuthSource;
  87. // Database table definition
  88. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  89. if (empty($user_id)) {
  90. $user_id = intval($_GET['uid']);
  91. }
  92. //Changing global chat status to offline
  93. if (api_is_global_chat_enabled()) {
  94. $chat = new Chat();
  95. $chat->set_user_status(0);
  96. }
  97. // selecting the last login of the user
  98. $sql_last_connection="SELECT login_id, login_date FROM $tbl_track_login WHERE login_user_id='$user_id' ORDER BY login_date DESC LIMIT 0,1";
  99. $q_last_connection=Database::query($sql_last_connection);
  100. if (Database::num_rows($q_last_connection)>0) {
  101. $i_id_last_connection=Database::result($q_last_connection,0,"login_id");
  102. }
  103. if (!isset($_SESSION['login_as'])) {
  104. $current_date = api_get_utc_datetime();
  105. $s_sql_update_logout_date="UPDATE $tbl_track_login SET logout_date='".$current_date."' WHERE login_id='$i_id_last_connection'";
  106. Database::query($s_sql_update_logout_date);
  107. }
  108. LoginDelete($user_id); //from inc/lib/online.inc.php - removes the "online" status
  109. //the following code enables the use of an external logout function.
  110. //example: define a $extAuthSource['ldap']['logout']="file.php" in configuration.php
  111. // then a function called ldap_logout() inside that file
  112. // (using *authent_name*_logout as the function name) and the following code
  113. // will find and execute it
  114. $uinfo = api_get_user_info($user_id);
  115. if (($uinfo['auth_source'] != PLATFORM_AUTH_SOURCE) && is_array($extAuthSource)) {
  116. if (is_array($extAuthSource[$uinfo['auth_source']])) {
  117. $subarray = $extAuthSource[$uinfo['auth_source']];
  118. if (!empty($subarray['logout']) && file_exists($subarray['logout'])) {
  119. require_once($subarray['logout']);
  120. $logout_function = $uinfo['auth_source'].'_logout';
  121. if (function_exists($logout_function)) {
  122. $logout_function($uinfo);
  123. }
  124. }
  125. }
  126. }
  127. require_once api_get_path(SYS_PATH) . 'main/chat/chat_functions.lib.php';
  128. exit_of_chat($user_id);
  129. session_regenerate_id();
  130. Session::destroy();
  131. if ($logout_redirect) {
  132. header("Location: index.php");
  133. return;
  134. }
  135. }
  136. /**
  137. * Remove all login records from the track_e_online stats table, for the given user ID.
  138. * @param int User ID
  139. * @return void
  140. */
  141. function LoginDelete($user_id)
  142. {
  143. $online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  144. $user_id = intval($user_id);
  145. $query = "DELETE FROM ".$online_table ." WHERE login_user_id = '".$user_id."'";
  146. @Database::query($query);
  147. }
  148. /**
  149. * @param int $user_id
  150. * @return bool
  151. */
  152. function user_is_online($user_id)
  153. {
  154. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  155. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  156. $access_url_id = api_get_current_access_url_id();
  157. $time_limit = api_get_setting('time_limit_whosonline');
  158. $online_time = time() - $time_limit*60;
  159. $limit_date = api_get_utc_datetime($online_time);
  160. $user_id = intval($user_id);
  161. $query = " SELECT login_user_id,login_date
  162. FROM $track_online_table track
  163. INNER JOIN $table_user u ON (u.user_id=track.login_user_id)
  164. WHERE
  165. track.access_url_id = $access_url_id AND
  166. login_date >= '".$limit_date."' AND
  167. u.user_id = $user_id
  168. LIMIT 1 ";
  169. $result = Database::query($query);
  170. if (Database::num_rows($result)) {
  171. return true;
  172. }
  173. return false;
  174. }
  175. /**
  176. * Gives a list of people online now (and in the last $valid minutes)
  177. * @return array For each line, a list of user IDs and login dates, or FALSE on error or empty results
  178. */
  179. function who_is_online($from, $number_of_items, $column = null, $direction = null, $time_limit = null, $friends = false) {
  180. // Time limit in seconds?
  181. if (empty($time_limit)) {
  182. $time_limit = api_get_setting('time_limit_whosonline');
  183. } else {
  184. $time_limit = intval($time_limit);
  185. }
  186. $from = intval($from);
  187. $number_of_items = intval($number_of_items);
  188. if (empty($column)) {
  189. $column = 'picture_uri';
  190. if ($friends) {
  191. $column = 'login_date';
  192. }
  193. }
  194. if (empty($direction)) {
  195. $direction = 'DESC';
  196. } else {
  197. if (!in_array(strtolower($direction), array('asc', 'desc'))) {
  198. $direction = 'DESC';
  199. }
  200. }
  201. $online_time = time() - $time_limit*60;
  202. $current_date = api_get_utc_datetime($online_time);
  203. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  204. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  205. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  206. $query = '';
  207. if ($friends) {
  208. // who friends from social network is online
  209. $query = "SELECT DISTINCT login_user_id, login_date
  210. FROM $track_online_table INNER JOIN $friend_user_table
  211. ON (friend_user_id = login_user_id)
  212. WHERE
  213. login_date >= '".$current_date."' AND
  214. friend_user_id <> '".api_get_user_id()."' AND
  215. relation_type='".USER_RELATION_TYPE_FRIEND."' AND
  216. user_id = '".api_get_user_id()."'
  217. ORDER BY $column $direction
  218. LIMIT $from, $number_of_items";
  219. } else {
  220. $query = "SELECT DISTINCT login_user_id, login_date
  221. FROM ".$track_online_table ." e
  222. INNER JOIN ".$table_user ." u ON (u.user_id=e.login_user_id)
  223. WHERE u.status != ".ANONYMOUS." AND login_date >= '".$current_date."'
  224. ORDER BY $column $direction
  225. LIMIT $from, $number_of_items";
  226. }
  227. if (api_get_multiple_access_url()) {
  228. $access_url_id = api_get_current_access_url_id();
  229. if ($access_url_id != -1) {
  230. if ($friends) {
  231. // friends from social network is online
  232. $query = "SELECT distinct login_user_id,login_date
  233. FROM $track_online_table track INNER JOIN $friend_user_table
  234. ON (friend_user_id = login_user_id)
  235. WHERE track.access_url_id = $access_url_id AND
  236. login_date >= '".$current_date."' AND
  237. friend_user_id <> '".api_get_user_id()."' AND
  238. relation_type='".USER_RELATION_TYPE_FRIEND."'
  239. ORDER BY $column $direction
  240. LIMIT $from, $number_of_items";
  241. } else {
  242. // all users online
  243. $query = "SELECT login_user_id, login_date FROM ".$track_online_table ." track
  244. INNER JOIN ".$table_user ." u
  245. ON (u.user_id=track.login_user_id)
  246. WHERE u.status != ".ANONYMOUS." AND track.access_url_id = $access_url_id AND
  247. login_date >= '".$current_date."'
  248. ORDER BY $column $direction
  249. LIMIT $from, $number_of_items";
  250. }
  251. }
  252. }
  253. //This query will show all registered users. Only for dev purposes.
  254. /*$query = "SELECT DISTINCT u.user_id as login_user_id, login_date FROM ".$track_online_table ." e , $table_user u
  255. GROUP by u.user_id
  256. ORDER BY $column $direction
  257. LIMIT $from, $number_of_items";*/
  258. $result = Database::query($query);
  259. if ($result) {
  260. $users_online = array();
  261. while (list($login_user_id, $login_date) = Database::fetch_row($result)) {
  262. $users_online[] = $login_user_id;
  263. }
  264. return $users_online;
  265. } else {
  266. return false;
  267. }
  268. }
  269. function who_is_online_count($time_limit = null, $friends = false)
  270. {
  271. if (empty($time_limit)) {
  272. $time_limit = api_get_setting('time_limit_whosonline');
  273. } else {
  274. $time_limit = intval($time_limit);
  275. }
  276. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  277. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  278. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  279. $query = '';
  280. $online_time = time() - $time_limit*60;
  281. $current_date = api_get_utc_datetime($online_time);
  282. if ($friends) {
  283. // who friends from social network is online
  284. $query = "SELECT DISTINCT count(login_user_id) as count
  285. FROM $track_online_table INNER JOIN $friend_user_table
  286. ON (friend_user_id = login_user_id)
  287. WHERE
  288. login_date >= '$current_date' AND
  289. friend_user_id <> '".api_get_user_id()."' AND
  290. relation_type='".USER_RELATION_TYPE_FRIEND."' AND
  291. user_id = '".api_get_user_id()."' ";
  292. } else {
  293. // All users online
  294. $query = "SELECT count(login_id) as count
  295. FROM $track_online_table track INNER JOIN $table_user u
  296. ON (u.user_id=track.login_user_id)
  297. WHERE u.status != ".ANONYMOUS." AND login_date >= '$current_date' ";
  298. }
  299. if (api_get_multiple_access_url()) {
  300. $access_url_id = api_get_current_access_url_id();
  301. if ($access_url_id != -1) {
  302. if ($friends) {
  303. // friends from social network is online
  304. $query = "SELECT DISTINCT count(login_user_id) as count
  305. FROM $track_online_table track
  306. INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  307. WHERE
  308. track.access_url_id = $access_url_id AND
  309. login_date >= '".$current_date."' AND
  310. friend_user_id <> '".api_get_user_id()."' AND
  311. relation_type='".USER_RELATION_TYPE_FRIEND."' ";
  312. } else {
  313. // all users online
  314. $query = "SELECT count(login_id) as count FROM $track_online_table track
  315. INNER JOIN $table_user u ON (u.user_id=track.login_user_id)
  316. WHERE
  317. u.status != ".ANONYMOUS." AND
  318. track.access_url_id = $access_url_id AND
  319. login_date >= '$current_date' ";
  320. }
  321. }
  322. }
  323. // Dev purposes show all users online
  324. /*$table_user = Database::get_main_table(TABLE_MAIN_USER);
  325. $query = "SELECT count(*) as count FROM ".$table_user;*/
  326. $result = Database::query($query);
  327. if (Database::num_rows($result) > 0) {
  328. $row = Database::fetch_array($result);
  329. return $row['count'];
  330. } else {
  331. return false;
  332. }
  333. }
  334. /**
  335. * Returns a list (array) of users who are online and in this course.
  336. * @param int User ID
  337. * @param int Number of minutes
  338. * @param string Course code (could be empty, but then the function returns false)
  339. * @return array Each line gives a user id and a login time
  340. */
  341. function who_is_online_in_this_course($from, $number_of_items, $uid, $time_limit, $course_code)
  342. {
  343. if (empty($course_code)) return false;
  344. if (empty($time_limit)) {
  345. $time_limit = api_get_setting('time_limit_whosonline');
  346. } else {
  347. $time_limit = intval($time_limit);
  348. }
  349. $online_time = time() - $time_limit*60;
  350. $current_date = api_get_utc_datetime($online_time);
  351. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  352. $course_code = Database::escape_string($course_code);
  353. $from = intval($from);
  354. $number_of_items = intval($number_of_items);
  355. $query = "SELECT login_user_id, login_date FROM $track_online_table
  356. WHERE login_user_id <> 2 AND course='$course_code' AND login_date >= '$current_date'
  357. LIMIT $from, $number_of_items ";
  358. $result = Database::query($query);
  359. if ($result) {
  360. /*$valid_date_time = new DateTime();
  361. $diff = "PT".$time_limit.'M';
  362. $valid_date_time->sub(new DateInterval($diff));*/
  363. $users_online = array();
  364. while(list($login_user_id, $login_date) = Database::fetch_row($result)) {
  365. /*$user_login_date = new DateTime($login_date);
  366. if ($user_login_date > $valid_date_time->format('Y-m-d H:i:s')) {*/
  367. $users_online[] = $login_user_id;
  368. //}
  369. }
  370. return $users_online;
  371. } else {
  372. return false;
  373. }
  374. }
  375. function who_is_online_in_this_course_count($uid, $time_limit, $coursecode=null) {
  376. if(empty($coursecode)) return false;
  377. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  378. $coursecode = Database::escape_string($coursecode);
  379. $time_limit = Database::escape_string($time_limit);
  380. $online_time = time() - $time_limit * 60;
  381. $current_date = api_get_utc_datetime($online_time);
  382. $query = "SELECT count(login_user_id) as count
  383. FROM ".$track_online_table ."
  384. WHERE login_user_id <> 2 AND course='".$coursecode."' AND login_date >= '$current_date' ";
  385. $result = Database::query($query);
  386. if (Database::num_rows($result) > 0) {
  387. $row = Database::fetch_array($result);
  388. return $row['count'];
  389. } else {
  390. return false;
  391. }
  392. }
  393. /**
  394. * Gets the full user name for a given user ID
  395. * @param int User ID
  396. * @return string The full username, elements separated by an HTML space
  397. * @deprecated user api_get_user_info($user_id)
  398. */
  399. function GetFullUserName($uid) {
  400. $uid = (int) $uid;
  401. $uid = intval($uid);
  402. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  403. $query = "SELECT firstname, lastname FROM ".$user_table." WHERE user_id='$uid'";
  404. $result = @Database::query($query);
  405. if (count($result)>0) {
  406. $str = '';
  407. while(list($firstname,$lastname)= Database::fetch_array($result)) {
  408. $str = str_replace(' ', '&nbsp;', api_get_person_name($firstname, $lastname));
  409. return $str;
  410. }
  411. }
  412. }
  413. /**
  414. * Gets a list of chat calls made by others to the current user (info kept in main.user table)
  415. * @param none - taken from global space
  416. * @return string An HTML-formatted message
  417. */
  418. function chatcall() {
  419. global $_user, $_cid;
  420. if (!$_user['user_id']) {
  421. return (false);
  422. }
  423. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  424. $sql="SELECT chatcall_user_id, chatcall_date FROM $track_user_table
  425. WHERE ( user_id = '".$_user['user_id']."' )";
  426. $result=Database::query($sql);
  427. $row=Database::fetch_array($result);
  428. $login_date=$row['chatcall_date'];
  429. $hour = substr($login_date,11,2);
  430. $minute = substr($login_date,14,2);
  431. $secund = substr($login_date,17,2);
  432. $month = substr($login_date,5,2);
  433. $day = substr($login_date,8,2);
  434. $year = substr($login_date,0,4);
  435. $calltime = mktime($hour,$minute,$secund,$month,$day,$year);
  436. $time = api_get_utc_datetime($time);
  437. $minute_passed=5; //within this limit, the chat call request is valid
  438. $limittime = mktime(date("H"),date("i")-$minute_passed,date("s"),date("m"),date("d"),date("Y"));
  439. if (($row['chatcall_user_id']) and ($calltime>$limittime)) {
  440. $webpath=api_get_path(WEB_CODE_PATH);
  441. $message=get_lang('YouWereCalled').' : '.GetFullUserName($row['chatcall_user_id'],'').'<br>'.get_lang('DoYouAccept')
  442. ."<p>"
  443. ."<a href=\"".$webpath."chat/chat.php?cidReq=".$_cid."&origin=whoisonlinejoin\">"
  444. . get_lang("Yes")
  445. ."</a>"
  446. ."&nbsp;&nbsp;|&nbsp;&nbsp;"
  447. ."<a href=\"".api_get_path(WEB_PATH)."webchatdeny.php\">"
  448. . get_lang("No")
  449. ."</a>"
  450. ."</p>";
  451. return($message);
  452. } else {
  453. return false;
  454. }
  455. }