online.inc.php 12 KB

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