online.inc.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. use ChamiloSession as Session;
  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. $_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, 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_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: ".api_get_path(WEB_PATH)."index.php");
  139. return;
  140. }
  141. }
  142. /**
  143. * Returns a list (array) of users who are online and in this course.
  144. * @param int User ID
  145. * @param int Number of minutes
  146. * @param string Course code (could be empty, but then the function returns false)
  147. * @return array Each line gives a user id and a login time
  148. */
  149. function who_is_online_in_this_course($from, $number_of_items, $uid, $time_limit, $course_code)
  150. {
  151. if (empty($course_code)) return false;
  152. if (empty($time_limit)) {
  153. $time_limit = api_get_setting('display.time_limit_whosonline');
  154. } else {
  155. $time_limit = intval($time_limit);
  156. }
  157. $online_time = time() - $time_limit * 60;
  158. $current_date = api_get_utc_datetime($online_time);
  159. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  160. $course_code = Database::escape_string($course_code);
  161. $courseInfo = api_get_course_info($course_code);
  162. $courseId = $courseInfo['real_id'];
  163. $from = intval($from);
  164. $number_of_items = intval($number_of_items);
  165. $query = "SELECT login_user_id, login_date FROM $track_online_table
  166. WHERE login_user_id <> 2 AND c_id = $courseId AND login_date >= '$current_date'
  167. LIMIT $from, $number_of_items ";
  168. $result = Database::query($query);
  169. if ($result) {
  170. $users_online = array();
  171. while(list($login_user_id, $login_date) = Database::fetch_row($result)) {
  172. $users_online[] = $login_user_id;
  173. }
  174. return $users_online;
  175. } else {
  176. return false;
  177. }
  178. }
  179. function who_is_online_in_this_course_count($uid, $time_limit, $coursecode=null)
  180. {
  181. if (empty($coursecode)) {
  182. return false;
  183. }
  184. $track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  185. $coursecode = Database::escape_string($coursecode);
  186. $time_limit = Database::escape_string($time_limit);
  187. $online_time = time() - $time_limit * 60;
  188. $current_date = api_get_utc_datetime($online_time);
  189. $courseId = api_get_course_int_id($coursecode);
  190. if (empty($courseId)) {
  191. return false;
  192. }
  193. $query = "SELECT count(login_user_id) as count
  194. FROM $track_online_table
  195. WHERE login_user_id <> 2 AND c_id = $courseId AND login_date >= '$current_date' ";
  196. $result = Database::query($query);
  197. if (Database::num_rows($result) > 0) {
  198. $row = Database::fetch_array($result);
  199. return $row['count'];
  200. } else {
  201. return false;
  202. }
  203. }
  204. /**
  205. * Gets a list of chat calls made by others to the current user (info kept in main.user table)
  206. * @param none - taken from global space
  207. * @return string An HTML-formatted message
  208. */
  209. function chatcall() {
  210. $_cid = api_get_course_id();
  211. $_user = api_get_user_info();
  212. if (!$_user['user_id']) {
  213. return (false);
  214. }
  215. $userId = intval($_user['user_id']);
  216. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  217. $sql="SELECT chatcall_user_id, chatcall_date FROM $track_user_table
  218. WHERE ( id = $userId )";
  219. $result=Database::query($sql);
  220. $row=Database::fetch_array($result);
  221. $login_date=$row['chatcall_date'];
  222. $hour = substr($login_date,11,2);
  223. $minute = substr($login_date,14,2);
  224. $second = substr($login_date,17,2);
  225. $month = substr($login_date,5,2);
  226. $day = substr($login_date,8,2);
  227. $year = substr($login_date,0,4);
  228. $calltime = mktime($hour,$minute,$second,$month,$day,$year);
  229. $time = api_get_utc_datetime();
  230. $minute_passed=5; //within this limit, the chat call request is valid
  231. $limittime = mktime(date("H"),date("i")-$minute_passed,date("s"),date("m"),date("d"),date("Y"));
  232. if (($row['chatcall_user_id']) and ($calltime>$limittime)) {
  233. $webpath = api_get_path(WEB_CODE_PATH);
  234. $userInfo = api_get_user_info($row['chatcall_user_id']);
  235. $message=get_lang('YouWereCalled').' : '.$userInfo['complete_name'].'<br>'.get_lang('DoYouAccept')
  236. ."<p>"
  237. ."<a href=\"".$webpath."chat/chat.php?cidReq=".$_cid."&origin=whoisonlinejoin\">"
  238. . get_lang("Yes")
  239. ."</a>"
  240. ."&nbsp;&nbsp;|&nbsp;&nbsp;"
  241. ."<a href=\"".api_get_path(WEB_PATH)."webchatdeny.php\">"
  242. . get_lang("No")
  243. ."</a>"
  244. ."</p>";
  245. return($message);
  246. } else {
  247. return false;
  248. }
  249. }