-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.inc.php
139 lines (120 loc) · 3.35 KB
/
main.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
Plugin Name: Online Users
Version: auto
Description: Show, in the gallery, which users are currently online.
Plugin URI: auto
Author: plg
Author URI: https://piwigo.org
Has Settings: false
*/
// TODO
// * at logout, remove id-<user_id> from table
// * at login, remove the session-<session_id> from table (will be replaced by id-<user_id> afterwards)
defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
if (basename(dirname(__FILE__)) != 'online_users')
{
add_event_handler('init', 'online_users_error');
function online_users_error()
{
global $page;
$page['errors'][] = 'Online Users folder name is incorrect, uninstall the plugin and rename it to "online_users"';
}
return;
}
// +-----------------------------------------------------------------------+
// | Define plugin constants |
// +-----------------------------------------------------------------------+
global $prefixeTable;
define('ONUS_ID', basename(dirname(__FILE__)));
define('ONUS_PATH' , PHPWG_PLUGINS_PATH . ONUS_ID . '/');
define('ONUS_TABLE', $prefixeTable . 'online_users');
// +-----------------------------------------------------------------------+
// | Add event handlers |
// +-----------------------------------------------------------------------+
// init the plugin
add_event_handler('init', 'online_users_init');
/**
* plugin initialization
* - check for upgrades
* - unserialize configuration
* - load language
*/
function online_users_init()
{
global $user, $conf;
load_language('plugin.lang', ONUS_PATH);
$query = '
DELETE
FROM `'.ONUS_TABLE.'`
WHERE `last_visit` < SUBDATE(NOW(), INTERVAL 1 HOUR)
;';
pwg_query($query);
$user_uuid = 'id-'.$user['id'];
if ($conf['guest_id'] == $user['id'])
{
if (!empty(session_id()))
{
$user_uuid = 'session-'.session_id();
}
elseif (!empty($_SERVER['REMOTE_ADDR']))
{
$user_uuid = 'ip-'.$_SERVER['REMOTE_ADDR'];
}
else
{
$user_uuid = 'undefined';
}
}
$query = '
INSERT INTO
`'.ONUS_TABLE.'` (`user_uuid`, `last_visit`)
VALUES(\''.$user_uuid.'\', NOW())
ON DUPLICATE KEY UPDATE `last_visit` = NOW()
;';
pwg_query($query);
}
add_event_handler('loc_begin_page_tail', 'online_users_display');
function online_users_display()
{
global $template, $conf;
$template->set_filenames(array('online_users' => ONUS_PATH.'online_users.tpl'));
$query = '
SELECT
user_uuid
FROM `'.ONUS_TABLE.'`
;';
$online_users = query2array($query, null, 'user_uuid');
$connected_users = array();
$nb_anonymous = 0;
foreach ($online_users as $online_user)
{
if (preg_match('/^id-(\d+)$/', $online_user, $matches))
{
$connected_users[] = $matches[1];
}
else
{
$nb_anonymous++;
}
}
$template->assign(
array(
'ONLINE_USERS_NB_CONNECTED' => count($connected_users),
'ONLINE_USERS_NB_ANONYMOUS' => $nb_anonymous,
'ONUS_PATH' => ONUS_PATH,
)
);
if (!empty($connected_users))
{
$query = '
SELECT
'.$conf['user_fields']['username'].'
FROM '.USERS_TABLE.'
WHERE '.$conf['user_fields']['id'].' IN ('.implode(',', $connected_users).')
;';
$usernames = query2array($query, null, 'username');
$template->assign('ONLINE_USERS_LIST', join(', ', $usernames));
}
$template->pparse('online_users');
}