online.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 (!empty($_configuration['multiple_access_urls']) && 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. global $_configuration;
  133. if ($_configuration['multiple_access_urls']) {
  134. $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  135. $access_url_id = api_get_current_access_url_id();
  136. if ($access_url_id != -1) {
  137. if ($friends) {
  138. // friends from social network is online
  139. $query = "SELECT distinct login_user_id,login_date
  140. FROM $track_online_table track
  141. INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  142. 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."' ";
  143. } else {
  144. // all users online
  145. $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)
  146. WHERE track.access_url_id = $access_url_id AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' ORDER BY picture_uri DESC ";
  147. }
  148. }
  149. }
  150. //This query will show all registered users. Only for dev purposes.
  151. //$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";
  152. $result = @Database::query($query);
  153. //@todo why we dont believe in db query results?
  154. if (count($result)>0) {
  155. $rtime = time();
  156. $rdate = date("Y-m-d H:i:s",$rtime);
  157. $validtime = mktime(date("H"),date("i")-$valid,date("s"),date("m"),date("d"),date("Y"));
  158. $rarray = array();
  159. while(list($login_user_id,$login_date)= Database::fetch_row($result)) {
  160. $barray = array();
  161. array_push($barray,$login_user_id);
  162. array_push($barray,$login_date);
  163. // YYYY-MM-DD HH:MM:SS, db date format
  164. $hour = substr($login_date,11,2);
  165. $minute = substr($login_date,14,2);
  166. $secund = substr($login_date,17,2);
  167. $month = substr($login_date,5,2);
  168. $day = substr($login_date,8,2);
  169. $year = substr($login_date,0,4);
  170. // db timestamp
  171. $dbtime = mktime($hour,$minute,$secund,$month,$day,$year);
  172. if ($dbtime>$validtime) {
  173. array_push($rarray,$barray);
  174. }
  175. }
  176. return $rarray;
  177. } else {
  178. return false;
  179. }
  180. }
  181. function who_is_online_count($valid, $friends = false) {
  182. $valid = (int) $valid;
  183. $current_date = date('Y-m-d H:i:s',time());
  184. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  185. $friend_user_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  186. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  187. $query = '';
  188. if ($friends) {
  189. // who friends from social network is online
  190. $query = "SELECT DISTINCT count(login_user_id) as count
  191. FROM $track_online_table INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  192. 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()."' ";
  193. } else {
  194. // all users online
  195. $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."'
  196. }
  197. global $_configuration;
  198. if ($_configuration['multiple_access_urls']) {
  199. $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  200. $access_url_id = api_get_current_access_url_id();
  201. if ($access_url_id != -1) {
  202. if ($friends) {
  203. // friends from social network is online
  204. $query = "SELECT DISTINCT count(login_user_id) as count
  205. FROM $track_online_table track
  206. INNER JOIN $friend_user_table ON (friend_user_id = login_user_id)
  207. 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."' ";
  208. } else {
  209. // all users online
  210. $query = "SELECT count(login_id) as count FROM ".$track_online_table ." track
  211. WHERE track.access_url_id = $access_url_id AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= '".$current_date."' ";
  212. }
  213. }
  214. }
  215. $result = Database::query($query);
  216. if (Database::num_rows($result) > 0) {
  217. $row = Database::fetch_array($result);
  218. return $row['count'];
  219. } else {
  220. return false;
  221. }
  222. }
  223. /**
  224. * Gets the full user name for a given user ID
  225. * @param int User ID
  226. * @return string The full username, elements separated by an HTML space
  227. */
  228. function GetFullUserName($uid)
  229. {
  230. $uid = (int) $uid;
  231. $uid = Database::escape_string($uid);
  232. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  233. $query = "SELECT firstname, lastname FROM ".$user_table." WHERE user_id='$uid'";
  234. $result = @Database::query($query);
  235. if (count($result)>0) {
  236. $str = '';
  237. while(list($firstname,$lastname)= Database::fetch_array($result)) {
  238. $str = str_replace(' ', '&nbsp;', api_get_person_name($firstname, $lastname));
  239. return $str;
  240. }
  241. }
  242. }
  243. /**
  244. * Gets a list of chat calls made by others to the current user (info kept in main.user table)
  245. * @param none - taken from global space
  246. * @return string An HTML-formatted message
  247. */
  248. function chatcall() {
  249. global $_user, $_cid;
  250. if (!$_user['user_id']) {
  251. return (false);
  252. }
  253. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  254. $sql="select chatcall_user_id, chatcall_date from $track_user_table where ( user_id = '".$_user['user_id']."' )";
  255. $result=Database::query($sql);
  256. $row=Database::fetch_array($result);
  257. $login_date=$row['chatcall_date'];
  258. $hour = substr($login_date,11,2);
  259. $minute = substr($login_date,14,2);
  260. $secund = substr($login_date,17,2);
  261. $month = substr($login_date,5,2);
  262. $day = substr($login_date,8,2);
  263. $year = substr($login_date,0,4);
  264. $calltime = mktime($hour,$minute,$secund,$month,$day,$year);
  265. $time = time();
  266. $time = date("Y-m-d H:i:s", $time);
  267. $minute_passed=5; //within this limit, the chat call request is valid
  268. $limittime = mktime(date("H"),date("i")-$minute_passed,date("s"),date("m"),date("d"),date("Y"));
  269. if (($row['chatcall_user_id']) and ($calltime>$limittime)) {
  270. $webpath=api_get_path(WEB_CODE_PATH);
  271. $message=get_lang('YouWereCalled').' : '.GetFullUserName($row['chatcall_user_id'],'').'<br>'.get_lang('DoYouAccept')
  272. ."<p>"
  273. ."<a href=\"".$webpath."chat/chat.php?cidReq=".$_cid."&origin=whoisonlinejoin\">"
  274. . get_lang("Yes")
  275. ."</a>"
  276. ."&nbsp;&nbsp;|&nbsp;&nbsp;"
  277. ."<a href=\"".api_get_path(WEB_PATH)."webchatdeny.php\">"
  278. . get_lang("No")
  279. ."</a>"
  280. ."</p>";
  281. return($message);
  282. }
  283. else
  284. {
  285. return(false);
  286. }
  287. }
  288. /**
  289. * Returns a list (array) of users who are online and in this course.
  290. * @param int User ID
  291. * @param int Number of minutes
  292. * @param string Course code (could be empty, but then the function returns false)
  293. * @return array Each line gives a user id and a login time
  294. */
  295. function who_is_online_in_this_course($uid, $valid, $coursecode=null)
  296. {
  297. if(empty($coursecode)) return false;
  298. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  299. $coursecode = Database::escape_string($coursecode);
  300. $valid = Database::escape_string($valid);
  301. $query = "SELECT login_user_id,login_date FROM ".$track_online_table ." WHERE course='".$coursecode."' AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= NOW() ";
  302. $result = Database::query($query);
  303. if (count($result)>0) {
  304. $rtime = time();
  305. $rdate = date("Y-m-d H:i:s",$rtime);
  306. $validtime = mktime(date("H"),date("i")-$valid,date("s"),date("m"),date("d"),date("Y"));
  307. $rarray = array();
  308. while(list($login_user_id,$login_date)= Database::fetch_row($result)) {
  309. $barray = array();
  310. array_push($barray,$login_user_id);
  311. array_push($barray,$login_date);
  312. // YYYY-MM-DD HH:MM:SS, db date format
  313. $hour = substr($login_date,11,2);
  314. $minute = substr($login_date,14,2);
  315. $secund = substr($login_date,17,2);
  316. $month = substr($login_date,5,2);
  317. $day = substr($login_date,8,2);
  318. $year = substr($login_date,0,4);
  319. // db timestamp
  320. $dbtime = mktime($hour,$minute,$secund,$month,$day,$year);
  321. if ($dbtime >= $validtime)
  322. {
  323. array_push($rarray,$barray);
  324. }
  325. }
  326. return $rarray;
  327. } else {
  328. return false;
  329. }
  330. }
  331. function who_is_online_in_this_course_count($uid, $valid, $coursecode=null)
  332. {
  333. if(empty($coursecode)) return false;
  334. $track_online_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  335. $coursecode = Database::escape_string($coursecode);
  336. $valid = Database::escape_string($valid);
  337. $query = "SELECT count(login_user_id) as count FROM ".$track_online_table ." WHERE course='".$coursecode."' AND DATE_ADD(login_date,INTERVAL $valid MINUTE) >= NOW() ";
  338. $result = Database::query($query);
  339. $result = Database::query($query);
  340. if (Database::num_rows($result) > 0) {
  341. $row = Database::fetch_array($result);
  342. return $row['count'];
  343. } else {
  344. return false;
  345. }
  346. }