online.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. function LoginCheck($uid) {
  19. global $_course, $_configuration;
  20. $uid = (int) $uid;
  21. $online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  22. if (!empty($uid)) {
  23. $login_ip = '';
  24. if(!empty($_SERVER['REMOTE_ADDR'])) {
  25. $login_ip = Database::escape_string($_SERVER['REMOTE_ADDR']);
  26. }
  27. $reallyNow = time();
  28. $login_date = date("Y-m-d H:i:s",$reallyNow);
  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. * This function handles the logout and is called whenever there is a $_GET['logout']
  46. * @return void Directly redirects the user or leaves him where he is, but doesn't return anything
  47. * @author Fernando P. García <fernando@develcuy.com>
  48. */
  49. function online_logout() {
  50. global $_configuration, $extAuthSource;
  51. // variable initialisation
  52. $query_string='';
  53. if (!empty($_SESSION['user_language_choice'])) {
  54. $query_string='?language='.$_SESSION['user_language_choice'];
  55. }
  56. // Database table definition
  57. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  58. // selecting the last login of the user
  59. $uid = intval($_GET['uid']);
  60. $sql_last_connection="SELECT login_id, login_date FROM $tbl_track_login WHERE login_user_id='$uid' ORDER BY login_date DESC LIMIT 0,1";
  61. $q_last_connection=Database::query($sql_last_connection);
  62. if (Database::num_rows($q_last_connection)>0) {
  63. $i_id_last_connection=Database::result($q_last_connection,0,"login_id");
  64. }
  65. if (!isset($_SESSION['login_as'])) {
  66. $current_date=date('Y-m-d H:i:s',time());
  67. $s_sql_update_logout_date="UPDATE $tbl_track_login SET logout_date='".$current_date."' WHERE login_id='$i_id_last_connection'";
  68. Database::query($s_sql_update_logout_date);
  69. }
  70. LoginDelete($uid); //from inc/lib/online.inc.php - removes the "online" status
  71. //the following code enables the use of an external logout function.
  72. //example: define a $extAuthSource['ldap']['logout']="file.php" in configuration.php
  73. // then a function called ldap_logout() inside that file
  74. // (using *authent_name*_logout as the function name) and the following code
  75. // will find and execute it
  76. $uinfo = api_get_user_info($uid);
  77. if (($uinfo['auth_source'] != PLATFORM_AUTH_SOURCE) && is_array($extAuthSource)) {
  78. if (is_array($extAuthSource[$uinfo['auth_source']])) {
  79. $subarray = $extAuthSource[$uinfo['auth_source']];
  80. if (!empty($subarray['logout']) && file_exists($subarray['logout'])) {
  81. require_once($subarray['logout']);
  82. $logout_function = $uinfo['auth_source'].'_logout';
  83. if (function_exists($logout_function)) {
  84. $logout_function($uinfo);
  85. }
  86. }
  87. }
  88. }
  89. require_once api_get_path(SYS_PATH) . 'main/chat/chat_functions.lib.php';
  90. exit_of_chat($uid);
  91. api_session_destroy();
  92. global $logout_no_redirect;
  93. if (!$logout_no_redirect) {
  94. header("Location: index.php$query_string");
  95. return;
  96. }
  97. }
  98. /**
  99. * Remove all login records from the track_e_online stats table, for the given user ID.
  100. * @param int User ID
  101. * @return void
  102. */
  103. function LoginDelete($user_id) {
  104. $online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  105. $user_id = intval($user_id);
  106. $query = "DELETE FROM ".$online_table ." WHERE login_user_id = '".$user_id."'";
  107. @Database::query($query);
  108. }
  109. /**
  110. * Gives a list of people online now (and in the last $valid minutes)
  111. * @param int Number of minutes to account logins for
  112. * @param bool optionally if it's set to true shows who friends from social network is online otherwise just shows all users online
  113. * @return array For each line, a list of user IDs and login dates, or FALSE on error or empty results
  114. */
  115. function who_is_online($valid, $friends = false) {
  116. $valid = (int) $valid;
  117. $current_date = date('Y-m-d H:i:s',time());
  118. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  119. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  120. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  121. $query = '';
  122. if ($friends) {
  123. // who friends from social network is online
  124. $query = "SELECT DISTINCT login_user_id,login_date
  125. FROM $track_online_table INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  126. WHERE DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' AND friend_user_id <> '".api_get_user_id()."' AND relation_type='".USER_RELATION_TYPE_FRIEND."' AND user_id = '".api_get_user_id()."' ";
  127. } else {
  128. // all users online
  129. //$query = "SELECT login_user_id,login_date FROM ".$track_online_table ." WHERE DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' "; //WHERE DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."'
  130. $query = "SELECT login_user_id,login_date FROM ".$track_online_table ." e INNER JOIN ".$table_user ." u ON (u.user_id=e.login_user_id) WHERE DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' ORDER BY picture_uri DESC";
  131. }
  132. if (api_get_multiple_access_url()) {
  133. $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  134. $access_url_id = api_get_current_access_url_id();
  135. if ($access_url_id != -1) {
  136. if ($friends) {
  137. // friends from social network is online
  138. $query = "SELECT distinct login_user_id,login_date
  139. FROM $track_online_table track
  140. INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  141. WHERE track.access_url_id = $access_url_id AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' AND friend_user_id <> '".api_get_user_id()."' AND relation_type='".USER_RELATION_TYPE_FRIEND."' ";
  142. } else {
  143. // all users online
  144. $query = "SELECT login_user_id,login_date FROM ".$track_online_table ." track INNER JOIN ".$table_user ." u ON (u.user_id=track.login_user_id)
  145. WHERE track.access_url_id = $access_url_id AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' ORDER BY picture_uri DESC ";
  146. }
  147. }
  148. }
  149. //This query will show all registered users. Only for dev purposes.
  150. //$query = "SELECT DISTINCT u.user_id as login_user_id, login_date FROM ".$track_online_table ." e , $table_user u GROUP by u.user_id ORDER BY picture_uri DESC";
  151. $result = Database::query($query);
  152. if ($result) {
  153. $rtime = time();
  154. $rdate = date("Y-m-d H:i:s",$rtime);
  155. $validtime = mktime(date("H"),date("i")-$valid,date("s"),date("m"),date("d"),date("Y"));
  156. $rarray = array();
  157. while(list($login_user_id,$login_date)= Database::fetch_row($result)) {
  158. $barray = array();
  159. array_push($barray,$login_user_id);
  160. array_push($barray,$login_date);
  161. // YYYY-MM-DD HH:MM:SS, db date format
  162. $hour = substr($login_date,11,2);
  163. $minute = substr($login_date,14,2);
  164. $secund = substr($login_date,17,2);
  165. $month = substr($login_date,5,2);
  166. $day = substr($login_date,8,2);
  167. $year = substr($login_date,0,4);
  168. // db timestamp
  169. $dbtime = mktime($hour,$minute,$secund,$month,$day,$year);
  170. if ($dbtime>$validtime) {
  171. array_push($rarray,$barray);
  172. }
  173. }
  174. return $rarray;
  175. } else {
  176. return false;
  177. }
  178. }
  179. function who_is_online_count($valid, $friends = false) {
  180. $valid = (int) $valid;
  181. $current_date = date('Y-m-d H:i:s',time());
  182. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  183. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  184. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  185. $query = '';
  186. if ($friends) {
  187. // who friends from social network is online
  188. $query = "SELECT DISTINCT count(login_user_id) as count
  189. FROM $track_online_table INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  190. WHERE DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' AND friend_user_id <> '".api_get_user_id()."' AND relation_type='".USER_RELATION_TYPE_FRIEND."' AND user_id = '".api_get_user_id()."' ";
  191. } else {
  192. // all users online
  193. $query = "SELECT count(login_id) as count FROM ".$track_online_table ." WHERE DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' "; //WHERE DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."'
  194. }
  195. if (api_get_multiple_access_url()) {
  196. $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  197. $access_url_id = api_get_current_access_url_id();
  198. if ($access_url_id != -1) {
  199. if ($friends) {
  200. // friends from social network is online
  201. $query = "SELECT DISTINCT count(login_user_id) as count
  202. FROM $track_online_table track
  203. INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  204. WHERE track.access_url_id = $access_url_id AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' AND friend_user_id <> '".api_get_user_id()."' AND relation_type='".USER_RELATION_TYPE_FRIEND."' ";
  205. } else {
  206. // all users online
  207. $query = "SELECT count(login_id) as count FROM ".$track_online_table ." track
  208. WHERE track.access_url_id = $access_url_id AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' ";
  209. }
  210. }
  211. }
  212. $result = Database::query($query);
  213. if (Database::num_rows($result) > 0) {
  214. $row = Database::fetch_array($result);
  215. return $row['count'];
  216. } else {
  217. return false;
  218. }
  219. }
  220. /**
  221. * Gets the full user name for a given user ID
  222. * @param int User ID
  223. * @return string The full username, elements separated by an HTML space
  224. */
  225. function GetFullUserName($uid)
  226. {
  227. $uid = (int) $uid;
  228. $uid = Database::escape_string($uid);
  229. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  230. $query = "SELECT firstname, lastname FROM ".$user_table." WHERE user_id='$uid'";
  231. $result = @Database::query($query);
  232. if (count($result)>0) {
  233. $str = '';
  234. while(list($firstname,$lastname)= Database::fetch_array($result)) {
  235. $str = str_replace(' ', '&nbsp;', api_get_person_name($firstname, $lastname));
  236. return $str;
  237. }
  238. }
  239. }
  240. /**
  241. * Gets a list of chat calls made by others to the current user (info kept in main.user table)
  242. * @param none - taken from global space
  243. * @return string An HTML-formatted message
  244. */
  245. function chatcall() {
  246. global $_user, $_cid;
  247. if (!$_user['user_id']) {
  248. return (false);
  249. }
  250. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  251. $sql="select chatcall_user_id, chatcall_date from $track_user_table where ( user_id = '".$_user['user_id']."' )";
  252. $result=Database::query($sql);
  253. $row=Database::fetch_array($result);
  254. $login_date=$row['chatcall_date'];
  255. $hour = substr($login_date,11,2);
  256. $minute = substr($login_date,14,2);
  257. $secund = substr($login_date,17,2);
  258. $month = substr($login_date,5,2);
  259. $day = substr($login_date,8,2);
  260. $year = substr($login_date,0,4);
  261. $calltime = mktime($hour,$minute,$secund,$month,$day,$year);
  262. $time = time();
  263. $time = date("Y-m-d H:i:s", $time);
  264. $minute_passed=5; //within this limit, the chat call request is valid
  265. $limittime = mktime(date("H"),date("i")-$minute_passed,date("s"),date("m"),date("d"),date("Y"));
  266. if (($row['chatcall_user_id']) and ($calltime>$limittime)) {
  267. $webpath=api_get_path(WEB_CODE_PATH);
  268. $message=get_lang('YouWereCalled').' : '.GetFullUserName($row['chatcall_user_id'],'').'<br>'.get_lang('DoYouAccept')
  269. ."<p>"
  270. ."<a href=\"".$webpath."chat/chat.php?cidReq=".$_cid."&origin=whoisonlinejoin\">"
  271. . get_lang("Yes")
  272. ."</a>"
  273. ."&nbsp;&nbsp;|&nbsp;&nbsp;"
  274. ."<a href=\"".api_get_path(WEB_PATH)."webchatdeny.php\">"
  275. . get_lang("No")
  276. ."</a>"
  277. ."</p>";
  278. return($message);
  279. }
  280. else
  281. {
  282. return(false);
  283. }
  284. }
  285. /**
  286. * Returns a list (array) of users who are online and in this course.
  287. * @param int User ID
  288. * @param int Number of minutes
  289. * @param string Course code (could be empty, but then the function returns false)
  290. * @return array Each line gives a user id and a login time
  291. */
  292. function who_is_online_in_this_course($uid, $valid, $coursecode=null)
  293. {
  294. if(empty($coursecode)) return false;
  295. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  296. $coursecode = Database::escape_string($coursecode);
  297. $valid = Database::escape_string($valid);
  298. $query = "SELECT login_user_id,login_date FROM ".$track_online_table ." WHERE course='".$coursecode."' AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= NOW() ";
  299. $result = Database::query($query);
  300. if (count($result)>0) {
  301. $rtime = time();
  302. $rdate = date("Y-m-d H:i:s",$rtime);
  303. $validtime = mktime(date("H"),date("i")-$valid,date("s"),date("m"),date("d"),date("Y"));
  304. $rarray = array();
  305. while(list($login_user_id,$login_date)= Database::fetch_row($result)) {
  306. $barray = array();
  307. array_push($barray,$login_user_id);
  308. array_push($barray,$login_date);
  309. // YYYY-MM-DD HH:MM:SS, db date format
  310. $hour = substr($login_date,11,2);
  311. $minute = substr($login_date,14,2);
  312. $secund = substr($login_date,17,2);
  313. $month = substr($login_date,5,2);
  314. $day = substr($login_date,8,2);
  315. $year = substr($login_date,0,4);
  316. // db timestamp
  317. $dbtime = mktime($hour,$minute,$secund,$month,$day,$year);
  318. if ($dbtime >= $validtime)
  319. {
  320. array_push($rarray,$barray);
  321. }
  322. }
  323. return $rarray;
  324. } else {
  325. return false;
  326. }
  327. }
  328. function who_is_online_in_this_course_count($uid, $valid, $coursecode=null)
  329. {
  330. if(empty($coursecode)) return false;
  331. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  332. $coursecode = Database::escape_string($coursecode);
  333. $valid = Database::escape_string($valid);
  334. $query = "SELECT count(login_user_id) as count FROM ".$track_online_table ." WHERE course='".$coursecode."' AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= NOW() ";
  335. $result = Database::query($query);
  336. $result = Database::query($query);
  337. if (Database::num_rows($result) > 0) {
  338. $row = Database::fetch_array($result);
  339. return $row['count'];
  340. } else {
  341. return false;
  342. }
  343. }