online.inc.php 15 KB

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