online.inc.php 11 KB

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