From 93e9c7ce337c9110ac5cc0f52fb698394ecb5e41 Mon Sep 17 00:00:00 2001 From: SJ Date: Wed, 24 Jul 2013 22:14:05 +0200 Subject: [PATCH] added online users feature --- util/db-mysql.sql | 7 ++++ util/db-upgrade-0.1.23-vs-0.1.24.sql | 8 +++- webui/config.php | 1 + webui/controller/health/worker.php | 6 +++ webui/controller/login/login.php | 5 +++ webui/controller/login/logout.php | 5 +++ webui/controller/stat/online.php | 33 +++++++++++++++++ webui/index.php | 9 +++++ webui/language/de/messages.php | 3 ++ webui/language/en/messages.php | 3 ++ webui/language/hu/messages.iso-8859-2.php | 2 + webui/language/hu/messages.php | 2 + webui/language/pt/messages.php | 2 + webui/model/saas/customer.php | 37 +++++++++++++++++++ .../theme/default/templates/health/worker.tpl | 7 ++++ .../theme/default/templates/stat/online.tpl | 30 +++++++++++++++ 16 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 webui/controller/stat/online.php create mode 100644 webui/view/theme/default/templates/stat/online.tpl diff --git a/util/db-mysql.sql b/util/db-mysql.sql index 012591eb..22fe18ee 100644 --- a/util/db-mysql.sql +++ b/util/db-mysql.sql @@ -370,3 +370,10 @@ create table if not exists `customer_settings` ( ) Engine=InnoDB; +create table if not exists `online` ( + `username` varchar(255) not null unique, + `ts` int default 0, + `last_activity` int default 0, + `ipaddr` varchar(255) default null +) Engine=InnoDB; + diff --git a/util/db-upgrade-0.1.23-vs-0.1.24.sql b/util/db-upgrade-0.1.23-vs-0.1.24.sql index 955f8384..4294f270 100644 --- a/util/db-upgrade-0.1.23-vs-0.1.24.sql +++ b/util/db-upgrade-0.1.23-vs-0.1.24.sql @@ -23,6 +23,12 @@ create table if not exists `customer_settings` ( `branding_logo` varchar(255) default null, `support_link` varchar(255) default null, `colour` varchar(255) default null -) Engine=InnoDB; +) Engine=InnoDB; +create table if not exists `online` ( + `username` varchar(255) not null unique, + `ts` int default 0, + `last_activity` int default 0, + `ipaddr` varchar(255) default null +) Engine=InnoDB; diff --git a/webui/config.php b/webui/config.php index 4c995aea..227d602c 100644 --- a/webui/config.php +++ b/webui/config.php @@ -272,6 +272,7 @@ define('TABLE_RETENTION_RULE', 'retention_rule'); define('TABLE_OPTION', 'option'); define('TABLE_LDAP', 'ldap'); define('TABLE_CUSTOMER_SETTINGS', 'customer_settings'); +define('TABLE_ONLINE', 'online'); define('TABLE_GOOGLE', 'google'); define('TABLE_GOOGLE_IMAP', 'google_imap'); define('VIEW_MESSAGES', 'v_messages'); diff --git a/webui/controller/health/worker.php b/webui/controller/health/worker.php index 76f3c78c..56c3dcb6 100644 --- a/webui/controller/health/worker.php +++ b/webui/controller/health/worker.php @@ -18,6 +18,9 @@ class ControllerHealthWorker extends Controller { $this->load->model('health/health'); $this->load->model('stat/counter'); + if(ENABLE_SAAS == 1) { + $this->load->model('saas/customer'); + } $request = Registry::get('request'); @@ -37,6 +40,9 @@ class ControllerHealthWorker extends Controller { $this->data['health'][] = $this->model_health_health->checksmtp($smtp, $lang->data['text_error']); } + if(ENABLE_SAAS == 1) { + $this->data['num_of_online_users'] = $this->model_saas_customer->count_online(); + } $this->data['processed_emails'] = $this->model_health_health->count_processed_emails(); diff --git a/webui/controller/login/login.php b/webui/controller/login/login.php index 1746be78..25d4e7e7 100644 --- a/webui/controller/login/login.php +++ b/webui/controller/login/login.php @@ -29,6 +29,7 @@ class ControllerLoginLogin extends Controller { if(ENABLE_SAAS == 1) { $this->load->model('saas/ldap'); + $this->load->model('saas/customer'); } $this->document->title = $this->data['text_login']; @@ -39,6 +40,10 @@ class ControllerLoginLogin extends Controller { $this->model_user_prefs->get_user_preferences($_SESSION['username']); + if(ENABLE_SAAS == 1) { + $this->model_saas_customer->online($_SESSION['username']); + } + LOGGER('logged in'); if(isAdminUser() == 1) { diff --git a/webui/controller/login/logout.php b/webui/controller/login/logout.php index f23c117b..c7274d09 100644 --- a/webui/controller/login/logout.php +++ b/webui/controller/login/logout.php @@ -18,6 +18,11 @@ class ControllerLoginLogout extends Controller { $this->document->title = $this->data['text_logout']; + if(ENABLE_SAAS == 1) { + $this->load->model('saas/customer'); + $this->model_saas_customer->offline(Registry::get('username')); + } + logout(); $this->render(); diff --git a/webui/controller/stat/online.php b/webui/controller/stat/online.php new file mode 100644 index 00000000..035e2d1f --- /dev/null +++ b/webui/controller/stat/online.php @@ -0,0 +1,33 @@ +id = "content"; + $this->template = "stat/online.tpl"; + $this->layout = "common/layout"; + + + $request = Registry::get('request'); + $db = Registry::get('db'); + + $this->load->model('saas/customer'); + + $this->document->title = $this->data['text_statistics']; + + if(Registry::get('admin_user') == 0) { + die("go away"); + } + + $this->data['users'] = $this->model_saas_customer->get_online_users(); + + $this->render(); + } + + +} + +?> diff --git a/webui/index.php b/webui/index.php index a7b41a67..ca5e633e 100644 --- a/webui/index.php +++ b/webui/index.php @@ -73,6 +73,15 @@ if(Registry::get('username')) { else { $action = new Router('search/search'); } + + if(ENABLE_SAAS == 1) { + $query = $db->query("UPDATE " . TABLE_ONLINE . " SET last_activity=? WHERE username=?", array(NOW, Registry::get('username'))); + + if($db->countAffected() == 0) { + $query = $db->query("INSERT INTO " . TABLE_ONLINE . " (username, ts, last_activity, ipaddr) VALUES(?,?,?,?)", array(Registry::get('username'), NOW, NOW, $_SERVER['REMOTE_ADDR'])); + } + } + } else { if(ENABLE_GOOGLE_LOGIN == 1 && isset($request->get['route']) && $request->get['route'] == 'login/google') { diff --git a/webui/language/de/messages.php b/webui/language/de/messages.php index 5f05a524..ef74321f 100644 --- a/webui/language/de/messages.php +++ b/webui/language/de/messages.php @@ -164,8 +164,10 @@ $_['text_invalid_policy_setting'] = "Ungültige Methodeneinstellung"; $_['text_invalid_uid'] = "Benutzerkennung ungültig"; $_['text_invalid_username'] = "Ungültiger Benutzername"; $_['text_ipaddr'] = "IP-Adresse"; + $_['text_language'] = "Sprache"; $_['text_last'] = "Letzte"; +$_['text_last_activity'] = "Letzte Aktivitat"; $_['text_last_update'] = "Letzte Aktualisierung"; $_['text_latest_emails'] = "Letzte Nachrichten"; $_['text_ldap'] = "LDAP"; @@ -177,6 +179,7 @@ $_['text_ldap_host'] = "LDAP-Host"; $_['text_ldap_type'] = "LDAP-Typ"; $_['text_load'] = "Laden"; $_['text_loading'] = "lade"; +$_['text_logged_in'] = "Logged in"; $_['text_logged_out'] = "Sie haben sich abgemeldet"; $_['text_login'] = "Anmeldung"; $_['text_login2'] = "Anmeldung"; diff --git a/webui/language/en/messages.php b/webui/language/en/messages.php index 9e20ab01..7fc80e60 100644 --- a/webui/language/en/messages.php +++ b/webui/language/en/messages.php @@ -164,8 +164,10 @@ $_['text_invalid_policy_setting'] = "Invalid policy setting"; $_['text_invalid_uid'] = "Invalid uid"; $_['text_invalid_username'] = "Invalid username"; $_['text_ipaddr'] = "IP address"; + $_['text_language'] = "Language"; $_['text_last'] = "Last"; +$_['text_last_activity'] = "Last activity"; $_['text_last_update'] = "Last update"; $_['text_latest_emails'] = "Latest emails"; $_['text_ldap'] = "LDAP"; @@ -177,6 +179,7 @@ $_['text_ldap_host'] = "LDAP host"; $_['text_ldap_type'] = "LDAP type"; $_['text_load'] = "Load"; $_['text_loading'] = "loading"; +$_['text_logged_in'] = "Logged in"; $_['text_logged_out'] = "You are logged out"; $_['text_login'] = "Login"; $_['text_login2'] = "login"; diff --git a/webui/language/hu/messages.iso-8859-2.php b/webui/language/hu/messages.iso-8859-2.php index e0590a57..2f89709e 100644 --- a/webui/language/hu/messages.iso-8859-2.php +++ b/webui/language/hu/messages.iso-8859-2.php @@ -167,6 +167,7 @@ $_['text_ipaddr'] = "IP c $_['text_language'] = "Nyelv"; $_['text_last'] = "Utolsó"; +$_['text_last_activity'] = "Utolsó aktivitás"; $_['text_latest_emails'] = "Legfrissebb emailek"; $_['text_last_update'] = "Utolsó frissítés"; $_['text_ldap'] = "LDAP"; @@ -178,6 +179,7 @@ $_['text_ldap_host'] = "LDAP kiszolg $_['text_ldap_type'] = "LDAP típus"; $_['text_load'] = "Betöltés"; $_['text_loading'] = "töltõdik"; +$_['text_logged_in'] = "Bejelentkezés"; $_['text_logged_out'] = "Ön kijelentkezett"; $_['text_login'] = "Bejelentkezés"; $_['text_login2'] = "bejelentkezés"; diff --git a/webui/language/hu/messages.php b/webui/language/hu/messages.php index 0afb8516..91cf22e3 100644 --- a/webui/language/hu/messages.php +++ b/webui/language/hu/messages.php @@ -167,6 +167,7 @@ $_['text_ipaddr'] = "IP cím"; $_['text_language'] = "Nyelv"; $_['text_last'] = "Utolsó"; +$_['text_last_activity'] = "Utolsó aktivitás"; $_['text_latest_emails'] = "Legfrissebb emailek"; $_['text_last_update'] = "Utolsó frissítés"; $_['text_ldap'] = "LDAP"; @@ -178,6 +179,7 @@ $_['text_ldap_host'] = "LDAP kiszolgáló"; $_['text_ldap_type'] = "LDAP típus"; $_['text_load'] = "Betöltés"; $_['text_loading'] = "töltÅ‘dik"; +$_['text_logged_in'] = "Bejelentkezés"; $_['text_logged_out'] = "Ön kijelentkezett"; $_['text_login'] = "Bejelentkezés"; $_['text_login2'] = "bejelentkezés"; diff --git a/webui/language/pt/messages.php b/webui/language/pt/messages.php index 2f390b67..91b4eb55 100644 --- a/webui/language/pt/messages.php +++ b/webui/language/pt/messages.php @@ -163,6 +163,7 @@ $_['text_ipaddr'] = "Endereço IP"; $_['text_language'] = "Idioma"; $_['text_last'] = "Último"; +$_['text_last_activity'] = "Last activity"; $_['text_last_update'] = "Última atualização"; $_['text_latest_emails'] = "Emails mais recentes"; $_['text_ldap_auditor_member_dn'] = "Auditor member DN"; @@ -173,6 +174,7 @@ $_['text_ldap_host'] = "LDAP host"; $_['text_ldap_type'] = "LDAP type"; $_['text_load'] = "Carregar"; $_['text_loading'] = "carregando"; +$_['text_logged_in'] = "Logged in"; $_['text_logged_out'] = "Você não está logado"; $_['text_login'] = "Login"; $_['text_login2'] = "login"; diff --git a/webui/model/saas/customer.php b/webui/model/saas/customer.php index 65219d4c..d70264c2 100644 --- a/webui/model/saas/customer.php +++ b/webui/model/saas/customer.php @@ -111,6 +111,43 @@ class ModelSaasCustomer extends Model return $data; } + + public function online($username = '') { + if($username == '') { return 0; } + + $query = $this->db->query("INSERT INTO " . TABLE_ONLINE . " (username, ts, last_activity, ipaddr) VALUES(?,?,?,?)", array($username, NOW, NOW, $_SERVER['REMOTE_ADDR'])); + + if($this->db->countAffected() == 0) { + $query = $this->db->query("UPDATE " . TABLE_ONLINE . " SET ts=?, last_activity=?, ipaddr=? WHERE username=?", array(NOW, $_SERVER['REMOTE_ADDR'], $username)); + } + + return 1; + } + + + public function offline($username = '') { + if($username == '') { return 0; } + + $query = $this->db->query("DELETE FROM " . TABLE_ONLINE . " WHERE username=?", array($username)); + + return 1; + } + + + public function count_online() { + $query = $this->db->query("SELECT COUNT(*) AS num FROM " . TABLE_ONLINE); + + return $query->row['num']; + } + + + public function get_online_users() { + $query = $this->db->query("SELECT * FROM " . TABLE_ONLINE . " ORDER BY username ASC"); + + return $query->rows; + } + + } ?> diff --git a/webui/view/theme/default/templates/health/worker.tpl b/webui/view/theme/default/templates/health/worker.tpl index ce209c5f..580f7d2c 100644 --- a/webui/view/theme/default/templates/health/worker.tpl +++ b/webui/view/theme/default/templates/health/worker.tpl @@ -26,6 +26,13 @@ :
 % / MB
+ + + + : + + + diff --git a/webui/view/theme/default/templates/stat/online.tpl b/webui/view/theme/default/templates/stat/online.tpl new file mode 100644 index 00000000..f934f16e --- /dev/null +++ b/webui/view/theme/default/templates/stat/online.tpl @@ -0,0 +1,30 @@ + +

+ +
+ + + + + + + + + + + + + + + + + + + + +
+ + + +
+