Hi,
I am new to osTicket and really like it! One feature that isn't part of the release is a way for an administrator to see who is logged on and when.
While browsing the interweb, I came across this article written by Masino Sinaga (http://www.openscriptsolution.com/2009/10/31/add-whos-online-feature-into-osticket-v1-6-rc5).
This modification will add a new sub tab under Dashboard, called 'User Online', and you can access this from Admin Panel.
Now, I take no credit for this at all. Its a piece of work which I think highly of and something I would like to make other osTicket users aware of.
Installation...
1. First of all, add a new table named ost_useronline by using this following SQL script. Please note that the ost_ is the table prefix name. It could be different with yours. In case it differs with yours, please adjust it by yourself, and please be kind of it!
CREATE TABLE `ost_useronline` (
`date_online` datetime NOT NULL default '0000-00-00 00',
`username` varchar(20) NOT NULL default '',
`useragent` varchar(255) NOT NULL default '',
`ip` varchar(40) NOT NULL default '',
`location` varchar(255) NOT NULL default '',
`url` varchar(255) NOT NULL default '',
`referral` varchar(255) NOT NULL default '',
PRIMARY KEY (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2. Open your main.inc.php file, and find this code:
define('TICKET_LOCK_TABLE',TABLE_PREFIX.'ticket_lock');
after that line, please insert this following code:
define('USERONLINE_TABLE',TABLE_PREFIX.'useronline');
3. Open your \include\class.staff.php file, and find this code:
}
?>
before the first line of that code, please insert this following code:
function useronline() {
if (empty($_SERVER)) {
$ip_address = $_SERVER;
} else {
$ip_address = $_SERVER;
}
if(strpos($ip_address, ',') !== false) {
$ip_address = explode(',', $ip_address);
$ip_address = $ip_address;
}
$url = $_SERVER;
$referral = '';
$user_agent = $_SERVER;
$current_user = $this->getUserName();
if(!empty($_SERVER)) {
$referral = $_SERVER;
}
$user_agent = addslashes($user_agent);
if (!isset($_GET)) {
$str_url = explode('/', $url);
$arr_url = array_reverse($str_url);
$t=$arr_url;
} else {
$t = $_GET;
}
$page_title = $t;
$insert_user = db_query("REPLACE INTO ".USERONLINE_TABLE."
SET date_online=NOW(),
username='$current_user',
useragent='$user_agent',
ip='$ip_address',
location='$page_title',
url='$url',
referral='$referral'");
}
4. Open your \include\staff\header.inc.php file, and find this code:
<div id="content" width="100%">
after that line, please insert this following code:
<?php
$thisuser->useronline();
?>
5. Create a new file, copy-paste this following code into it, and then save it as useronline.inc.php file, and put this file into your \include\staff\ sub directory of your osTicket:
<?php
/**
* <USERMENTION username="filename">@filename</USERMENTION> \include\staff\useronline.inc.php
* <USERMENTION username="author">@author</USERMENTION> Masino Sinaga, http://www.openscriptsolution.com
* <USERMENTION username="copyright">@copyright</USERMENTION> October 31, 2009
*/
if(!defined('OSTSCPINC') || !@$thisuser->isStaff()) die('Access Denied');
$qstr='&t=useronline'; //Query string collector
$qwhere =' WHERE 1';
$qselect = 'SELECT * ';
$qfrom=' FROM '.USERONLINE_TABLE.' ';
$total=db_count("SELECT count(*) $qfrom $qwhere");
if($_GET){
$qstr.='&limit='.urlencode($_GET);
}
$pagelimit=$_GET?$_GET;
$page=($_GET && is_numeric($_GET))?$_GET;
$title='Who\'s Online';
//pagenate
$pageNav=new Pagenate($total,$page,$pagelimit);
$pageNav->setURL('admin.php',$qstr);
$query="$qselect $qfrom $qwhere ORDER BY date_online DESC LIMIT ".$pageNav->getStart().",".$pageNav->getLimit();
//echo $query;
$result = db_query($query);
$total = db_num_rows($result);
$showing=$total?$pageNav->showing():"";
?>
<div class="msg">User Online (<?php echo $total; ?>)</div>
<div style="margin-bottom; padding-top;">
<table width="100%" border="0" cellspacing=1 cellpadding=2>
<tr><td>
<table width="100%" border="0" cellspacing=0 cellpadding=2 class="logs" align="center"> <b><?=$showing?></b> <a href="admin.php?t=useronline" title="Reload"><span class="Icon refresh"> </span></a>
<tr><th> <?=$title?></th></tr>
<?
$class = "row1";
$total=0;
if($result && ($num=db_num_rows($result))):
while ($row = db_fetch_array($result)) {
$icon='assignedTicket';
?>
<tr class="<?=$class?>" id="<?=$row?>">
<td>
<a href="javascript('<?=$row?>');">
<IMG alt="blank" src="images/plus.gif"></IMG>
<span style="color; float: left; width;"><?=Format:($row)?></span>
<span class="Icon <?=$icon?>"><?=Format:($row)?></span></a>
<div id="msg_<?=$row?>" class="hide">
<hr>
<b>Location:</b> <?=Format:($row)?><br />
<b>Referral:</b> <?=Format:($row)?> <br />
<b>IP Address:</b> <?=Format:($row)?> <br />
</div>
</td>
</tr>
<?
$class = ($class =='row2') ?'row1':'row2';
} //end of while.
else: //no useronline found!! ?>
<tr class="<?=$class?>"><td><b>Query returned 0 results.</b></td></tr>
<?
endif; ?>
</table>
</td></tr>
<? if($num>0){ ?>
<tr><td style="padding-left">page:<?=$pageNav->getPageLinks()?></td></tr>
<? } ?>
</table>
</div>
6. Open your \scp\admin.php file, and find this code:
case 'dashboard':
case 'syslog':
$nav->setTabActive('dashboard');
$nav->addSubMenu(array('desc'=>'System Logs','href'=>'admin.php?t=syslog','iconclass'=>'syslogs'));
$page='syslogs.inc.php';
break;
and then replace with this following code:
case 'dashboard':
case 'syslog':
case 'useronline': // MOD User Online, Added by Masino Sinaga, October 31, 2009
$nav->setTabActive('dashboard');
$nav->addSubMenu(array('desc'=>'System Logs','href'=>'admin.php?t=syslog','iconclass'=>'syslogs'));
$nav->addSubMenu(array('desc'=>'User Online','href'=>'admin.php?t=useronline','iconclass'=>'syslogs'));
$page='syslogs.inc.php';
switch(strtolower($_REQUEST)):
case 'syslog':
$page='syslogs.inc.php';
break;
case 'useronline': // MOD User Online Added by Masino Sinaga, October 31, 2009
$page='useronline.inc.php';
break;
endswitch;
break;
Thats it!
At this stage I would like to call upon any developers out there to assist me! I am not a PHP/SQL developer. I have some programming knowledge but I couldn't get this to work.
While this code works great and the installation of it is flawless, when the user logs out of osTicket, the User Online page still states they are logged in and are not removed.
From what I can gather, when the user clicks 'logout' the row needs to be deleted from the table.
I created a function in class.staff.php like this
function useroffline(){
db_query("DELETE FROM ".USERONLINE_TABLE."
WHERE username='$current_user'");
I then called the function in logout.php
<?php
/*********************************************************************
logout.php
Destroy clients session.
Peter Rotich <peter@osticket.com>
Copyright (c) 2006-2010 osTicket
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
$Id: $
**********************************************************************/
require('client.inc.php');
//We are checking to make sure the user is logged in before a logout to avoid session reset tricks on excess logins
$_SESSION=array();
useroffline(); //Function to delete row from the ost_useronline table
session_unset();
session_destroy();
header('Location: index.php');
require('index.php');
?>
I have tried lots of different things, like adding require('client.staff.php') but none of them deleted the row.
I could have this completely wrong and the logic behind it is just pure stupidity on my behalf. If anyone could help me fix this, I would be eternally grateful!
Thanks