online.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. @Database::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=Database::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. Database::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 = '".Database::escape_string($user_id)."'";
  126. @Database::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 = @Database::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. $uid = Database::escape_string($uid);
  195. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  196. $query = "SELECT firstname,lastname FROM ".$user_table." WHERE user_id='$uid'";
  197. $result = @Database::query($query,__FILE__,__LINE__);
  198. if (count($result)>0)
  199. {
  200. $str = '';
  201. while(list($firstname,$lastname)= Database::fetch_array($result))
  202. {
  203. $str = str_replace(' ', '&nbsp;', api_get_person_name($firstname, $lastname));
  204. return $str;
  205. }
  206. }
  207. }
  208. /**
  209. * Gets a list of chat calls made by others to the current user (info kept in main.user table)
  210. * @param none - taken from global space
  211. * @return string An HTML-formatted message
  212. */
  213. function chatcall() {
  214. global $_user, $_cid;
  215. if (!$_user['user_id'])
  216. {
  217. return (false);
  218. }
  219. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  220. $sql="select chatcall_user_id, chatcall_date from $track_user_table where ( user_id = '".$_user['user_id']."' )";
  221. $result=Database::query($sql,__FILE__,__LINE__);
  222. $row=Database::fetch_array($result);
  223. $login_date=$row['chatcall_date'];
  224. $hour = substr($login_date,11,2);
  225. $minute = substr($login_date,14,2);
  226. $secund = substr($login_date,17,2);
  227. $month = substr($login_date,5,2);
  228. $day = substr($login_date,8,2);
  229. $year = substr($login_date,0,4);
  230. $calltime = mktime($hour,$minute,$secund,$month,$day,$year);
  231. $time = time();
  232. $time = date("Y-m-d H:i:s", $time);
  233. $minute_passed=5; //within this limit, the chat call request is valid
  234. $limittime = mktime(date("H"),date("i")-$minute_passed,date("s"),date("m"),date("d"),date("Y"));
  235. if (($row['chatcall_user_id']) and ($calltime>$limittime)) {
  236. $webpath=api_get_path(WEB_CODE_PATH);
  237. $message=get_lang('YouWereCalled').' : '.GetFullUserName($row['chatcall_user_id'],'').'<br>'.get_lang('DoYouAccept')
  238. ."<p>"
  239. ."<a href=\"".$webpath."chat/chat.php?cidReq=".$_cid."&origin=whoisonlinejoin\">"
  240. . get_lang("Yes")
  241. ."</a>"
  242. ."&nbsp;&nbsp;|&nbsp;&nbsp;"
  243. ."<a href=\"".api_get_path('WEB_PATH')."webchatdeny.php\">"
  244. . get_lang("No")
  245. ."</a>"
  246. ."</p>";
  247. return($message);
  248. }
  249. else
  250. {
  251. return(false);
  252. }
  253. }
  254. /**
  255. * Returns a list (array) of users who are online and in this course.
  256. * @param int User ID
  257. * @param int Number of minutes
  258. * @param string Course code (could be empty, but then the function returns false)
  259. * @return array Each line gives a user id and a login time
  260. */
  261. function who_is_online_in_this_course($uid, $valid, $coursecode=null)
  262. {
  263. if(empty($coursecode)) return false;
  264. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  265. $coursecode = Database::escape_string($coursecode);
  266. $valid = Database::escape_string($valid);
  267. $query = "SELECT login_user_id,login_date FROM ".$track_online_table ." WHERE course='".$coursecode."' AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= NOW() ";
  268. $result = Database::query($query,__FILE__,__LINE__);
  269. if (count($result)>0)
  270. {
  271. $rtime = time();
  272. $rdate = date("Y-m-d H:i:s",$rtime);
  273. $validtime = mktime(date("H"),date("i")-$valid,date("s"),date("m"),date("d"),date("Y"));
  274. $rarray = array();
  275. while(list($login_user_id,$login_date)= Database::fetch_row($result))
  276. {
  277. $barray = array();
  278. array_push($barray,$login_user_id);
  279. array_push($barray,$login_date);
  280. // YYYY-MM-DD HH:MM:SS, db date format
  281. $hour = substr($login_date,11,2);
  282. $minute = substr($login_date,14,2);
  283. $secund = substr($login_date,17,2);
  284. $month = substr($login_date,5,2);
  285. $day = substr($login_date,8,2);
  286. $year = substr($login_date,0,4);
  287. // db timestamp
  288. $dbtime = mktime($hour,$minute,$secund,$month,$day,$year);
  289. if ($dbtime >= $validtime)
  290. {
  291. array_push($rarray,$barray);
  292. }
  293. }
  294. return $rarray;
  295. }
  296. else
  297. {
  298. return false;
  299. }
  300. }