+
+ function handler_ipwatch(&$page, $action = 'list', $ip = null)
+ {
+ $page->changeTpl('admin/ipwatcher.tpl');
+
+ $states = array('safe' => 'Ne pas surveiller',
+ 'unsafe' => 'Surveiller les inscriptions',
+ 'dangerous' => 'Surveiller tous les accès',
+ 'ban' => 'Bannir cette adresse');
+ $page->assign('states', $states);
+
+ switch (Post::v('action')) {
+ case 'create':
+ if (trim(Post::v('ipN')) != '') {
+ S::assert_xsrf_token();
+ Xdb::execute('INSERT IGNORE INTO ip_watch (ip, mask, state, detection, last, uid, description)
+ VALUES ({?}, {?}, {?}, CURDATE(), NOW(), {?}, {?})',
+ ip_to_uint(trim(Post::v('ipN'))), ip_to_uint(trim(Post::v('maskN'))),
+ Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
+ };
+ break;
+
+ case 'edit':
+ S::assert_xsrf_token();
+ Xdb::execute('UPDATE ip_watch
+ SET state = {?}, last = NOW(), uid = {?}, description = {?}, mask = {?}
+ WHERE ip = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'),
+ ip_to_uint(Post::v('maskN')), ip_to_uint(Post::v('ipN')));
+ break;
+
+ default:
+ if ($action == 'delete' && !is_null($ip)) {
+ S::assert_xsrf_token();
+ Xdb::execute('DELETE FROM ip_watch WHERE ip = {?}', ip_to_uint($ip));
+ }
+ }
+ if ($action != 'create' && $action != 'edit') {
+ $action = 'list';
+ }
+ $page->assign('action', $action);
+
+ if ($action == 'list') {
+ $sql = "SELECT w.ip, IF(s.ip IS NULL,
+ IF(w.ip = s2.ip, s2.host, s2.forward_host),
+ IF(w.ip = s.ip, s.host, s.forward_host)),
+ w.mask, w.detection, w.state, u.hruid
+ FROM ip_watch AS w
+ LEFT JOIN logger.sessions AS s ON (s.ip = w.ip)
+ LEFT JOIN logger.sessions AS s2 ON (s2.forward_ip = w.ip)
+ LEFT JOIN auth_user_md5 AS u ON (u.user_id = s.uid)
+ GROUP BY w.ip, u.hruid
+ ORDER BY w.state, w.ip, u.hruid";
+ $it = Xdb::iterRow($sql);
+
+ $table = array();
+ $props = array();
+ while (list($ip, $host, $mask, $date, $state, $hruid) = $it->next()) {
+ $ip = uint_to_ip($ip);
+ $mask = uint_to_ip($mask);
+ if (count($props) == 0 || $props['ip'] != $ip) {
+ if (count($props) > 0) {
+ $table[] = $props;
+ }
+ $props = array('ip' => $ip,
+ 'mask' => $mask,
+ 'host' => $host,
+ 'detection' => $date,
+ 'state' => $state,
+ 'users' => array($hruid));
+ } else {
+ $props['users'][] = $hruid;
+ }
+ }
+ if (count($props) > 0) {
+ $table[] = $props;
+ }
+ $page->assign('table', $table);
+ } elseif ($action == 'edit') {
+ $sql = "SELECT w.detection, w.state, w.last, w.description, w.mask,
+ u1.hruid AS edit, u2.hruid AS hruid, s.host
+ FROM ip_watch AS w
+ LEFT JOIN auth_user_md5 AS u1 ON (u1.user_id = w.uid)
+ LEFT JOIN logger.sessions AS s ON (w.ip = s.ip)
+ LEFT JOIN auth_user_md5 AS u2 ON (u2.user_id = s.uid)
+ WHERE w.ip = {?}
+ GROUP BY u2.hruid
+ ORDER BY u2.hruid";
+ $it = Xdb::iterRow($sql, ip_to_uint($ip));
+
+ $props = array();
+ while (list($detection, $state, $last, $description, $mask, $edit, $hruid, $host) = $it->next()) {
+ if (count($props) == 0) {
+ $props = array('ip' => $ip,
+ 'mask' => uint_to_ip($mask),
+ 'host' => $host,
+ 'detection' => $detection,
+ 'state' => $state,
+ 'last' => $last,
+ 'description' => $description,
+ 'edit' => $edit,
+ 'users' => array($hruid));
+ } else {
+ $props['users'][] = $hruid;
+ }
+ }
+ $page->assign('ip', $props);
+ }
+ }
+
+ function handler_icons(&$page)
+ {
+ $page->changeTpl('admin/icons.tpl');
+ $dh = opendir('../htdocs/images/icons');
+ if (!$dh) {
+ $page->trigError('Dossier des icones introuvables.');
+ }
+ $icons = array();
+ while (($file = readdir($dh)) !== false) {
+ if (strlen($file) > 4 && substr($file,-4) == '.gif') {
+ array_push($icons, substr($file, 0, -4));
+ }
+ }
+ sort($icons);
+ $page->assign('icons', $icons);
+ }
+
+ function handler_accounts(&$page)
+ {
+ $page->changeTpl('admin/accounts.tpl');
+ $page->assign('disabled', XDB::iterator('SELECT u.nom, u.prenom, u.promo, u.comment, u.hruid
+ FROM auth_user_md5 AS u
+ WHERE perms = \'disabled\'
+ ORDER BY nom, prenom'));
+ $page->assign('admins', XDB::iterator('SELECT u.nom, u.prenom, u.promo, u.hruid
+ FROM auth_user_md5 AS u
+ WHERE perms = \'admin\'
+ ORDER BY nom, prenom'));
+ }