From 96495504631d6a4d466b93a835d7d66853e2e12b Mon Sep 17 00:00:00 2001 From: Michael Save Date: Fri, 13 Apr 2012 02:11:41 +1000 Subject: start on mod interface rewrite --- inc/mod/auth.php | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 inc/mod/auth.php (limited to 'inc/mod/auth.php') diff --git a/inc/mod/auth.php b/inc/mod/auth.php new file mode 100644 index 00000000..2e90b757 --- /dev/null +++ b/inc/mod/auth.php @@ -0,0 +1,124 @@ +bindValue(':username', $username); + $query->bindValue(':password', $password); + $query->execute() or error(db_error($query)); + + if ($user = $query->fetch()) { + return $mod = Array( + 'id' => $user['id'], + 'type' => $user['type'], + 'username' => $username, + 'hash' => mkhash($username, $password), + 'boards' => explode(',', $user['boards']) + ); + } else return false; +} + +function setCookies() { + global $mod, $config; + if (!$mod) + error('setCookies() was called for a non-moderator!'); + + setcookie($config['cookies']['mod'], + $mod['username'] . // username + ':' . + $mod['hash'][0] . // password + ':' . + $mod['hash'][1], // salt + time() + $config['cookies']['expire'], $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, false, true); +} + +function destroyCookies() { + global $config; + // Delete the cookies + setcookie($config['cookies']['mod'], 'deleted', time() - $config['cookies']['expire'], $config['cookies']['jail']?$config['cookies']['path'] : '/', null, false, true); +} + +function modLog($action, $_board=null) { + global $mod, $board, $config; + $query = prepare("INSERT INTO `modlogs` VALUES (:id, :ip, :board, :time, :text)"); + $query->bindValue(':id', $mod['id'], PDO::PARAM_INT); + $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); + $query->bindValue(':time', time(), PDO::PARAM_INT); + $query->bindValue(':text', $action); + if (isset($_board)) + $query->bindValue(':board', $_board); + elseif (isset($board)) + $query->bindValue(':board', $board['uri']); + else + $query->bindValue(':board', null, PDO::PARAM_NULL); + $query->execute() or error(db_error($query)); + + if ($config['syslog']) + _syslog(LOG_INFO, '[mod/' . $mod['username'] . ']: ' . $action); +} + +// Validate session + +if (isset($_COOKIE[$config['cookies']['mod']])) { + // Should be username:hash:salt + $cookie = explode(':', $_COOKIE[$config['cookies']['mod']]); + if (count($cookie) != 3) { + destroyCookies(); + error($config['error']['malformed']); + } + + $query = prepare("SELECT `id`, `type`, `boards`, `password` FROM `mods` WHERE `username` = :username LIMIT 1"); + $query->bindValue(':username', $cookie[0]); + $query->execute() or error(db_error($query)); + $user = $query->fetch(); + + // validate password hash + if ($cookie[1] != mkhash($cookie[0], $user['password'], $cookie[2])) { + // Malformed cookies + destroyCookies(); + error($config['error']['malformed']); + } + + $mod = Array( + 'id' => $user['id'], + 'type' => $user['type'], + 'username' => $cookie[0], + 'boards' => explode(',', $user['boards']) + ); +} + -- cgit v1.3 From e49ece459e2ee225ba677f8e0758a38a98b930f5 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Fri, 13 Apr 2012 22:00:40 +1000 Subject: new PM --- inc/mod/auth.php | 13 ++++++++++++ inc/mod/pages.php | 33 +++++++++++++++++++++++++++-- mod.php | 2 +- templates/mod/new_pm.html | 22 +++++++++++++++++++ templates/mod/users.html | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 templates/mod/new_pm.html create mode 100644 templates/mod/users.html (limited to 'inc/mod/auth.php') diff --git a/inc/mod/auth.php b/inc/mod/auth.php index 2e90b757..f9b863c8 100644 --- a/inc/mod/auth.php +++ b/inc/mod/auth.php @@ -122,3 +122,16 @@ if (isset($_COOKIE[$config['cookies']['mod']])) { ); } +function create_pm_header() { + global $mod; + $query = prepare("SELECT `id` FROM `pms` WHERE `to` = :id AND `unread` = 1"); + $query->bindValue(':id', $mod['id'], PDO::PARAM_INT); + $query->execute() or error(db_error($query)); + + if ($pm = $query->fetch()) { + return Array('id' => $pm['id'], 'waiting' => $query->rowCount() - 1); + } + + return false; +} + diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 2b5487fd..4903567a 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -252,13 +252,42 @@ function mod_delete($board, $post) { function mod_users() { global $config; - if(!hasPermission($config['mod']['manageusers'])) + + if (!hasPermission($config['mod']['manageusers'])) error($config['error']['noaccess']); $args = array(); $query = query("SELECT *, (SELECT `time` FROM `modlogs` WHERE `mod` = `id` ORDER BY `time` DESC LIMIT 1) AS `last`, (SELECT `text` FROM `modlogs` WHERE `mod` = `id` ORDER BY `time` DESC LIMIT 1) AS `action` FROM `mods` ORDER BY `type` DESC,`id`") or error(db_error()); $args['users'] = $query->fetchAll(PDO::FETCH_ASSOC); - mod_page("Manage users", 'mod/users.html', $args); + mod_page('Manage users', 'mod/users.html', $args); +} + +function mod_new_pm($username) { + global $config, $mod; + + if (!hasPermission($config['mod']['create_pm'])) + error($config['error']['noaccess']); + + $query = prepare("SELECT `id` FROM `mods` WHERE `username` = :username"); + $query->bindValue(':username', $username); + $query->execute() or error(db_error($query)); + if (!$id = $query->fetchColumn(0)) + error($config['error']['404']); + + if (isset($_POST['message'])) { + markup($_POST['message']); + + $query = prepare("INSERT INTO `pms` VALUES (NULL, :me, :id, :message, :time, 1)"); + $query->bindValue(':me', $mod['id']); + $query->bindValue(':id', $id); + $query->bindValue(':message', $_POST['message']); + $query->bindValue(':time', time()); + $query->execute() or error(db_error($query)); + + header('Location: ?/', true, $config['redirect_http']); + } + + mod_page("New PM for {$username}", 'mod/new_pm.html', array('username' => $username, 'id' => $id)); } diff --git a/mod.php b/mod.php index d95f9552..e7134603 100644 --- a/mod.php +++ b/mod.php @@ -28,7 +28,7 @@ $pages = array( '!^/log/(\d+)$!' => 'log', // modlog '!^/users$!' => 'users', // manage users - + '!^/new_PM/([^/]+)$!' => 'new_pm', // create a new pm '!^/ban$!' => 'ban', // new ban '!^/IP/([\w.:]+)$!' => 'ip', // view ip address diff --git a/templates/mod/new_pm.html b/templates/mod/new_pm.html new file mode 100644 index 00000000..d694bc26 --- /dev/null +++ b/templates/mod/new_pm.html @@ -0,0 +1,22 @@ +{#{% if id == mod.id %} + {% set username = 'me' %} +{% endif %}#} + +
+ + + + {% if mod|hasPermission(config.mod.editusers) %} + + {% else %} + + {% endif %} + + + + + +
To{{ username|e }}{{ username|e }}
Message
+ +

+
diff --git a/templates/mod/users.html b/templates/mod/users.html new file mode 100644 index 00000000..aeacc8b5 --- /dev/null +++ b/templates/mod/users.html @@ -0,0 +1,54 @@ + + + + + + + + + + + {% for user in users %} + + + + + + + + + {% endfor %} +
IDUsernameTypeBoardsLast action…
{{ user.id }}{{ user.username }} + {% if user.type == constant('JANITOR') %}Janitor + {% elseif user.type == constant('MOD') %}Mod + {% elseif user.type == constant('ADMIN') %}Admin + {% endif %} + + {# This is really messy, but IMO it beats doing it in PHP. #} + {% set boards = user.boards|split(',') %} + {% set _boards = [] %} + {% for board in boards %} + {% set _boards = _boards|push(board == '*' ? '*' : config.board_abbreviation|sprintf(board)) %} + {% endfor %} + {% set _boards = _boards|sort %} + {{ _boards|join(', ') }} + + {% if mod|hasPermission(config.mod.modlog) %} + {{ user.last|ago }} + {% else %} + – + {% endif %} + + {% if mod|hasPermission(config.mod.promoteusers) and user.type < constant('ADMIN') %} + ▲ + {% endif %} + {% if mod|hasPermission(config.mod.promoteusers) and user.type > constant('JANITOR') %} + ▼ + {% endif %} + {% if mod|hasPermission(config.editusers) or (mod|hasPermission(config.change_password) and mod.id == user.id) %} + [edit] + {% endif %} + {% if mod|hasPermission(config.mod.create_pm) %} + [PM] + {% endif %} +
-- cgit v1.3 From 0f041170372b9dfcba7645d8b05066867c0d3f75 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Sun, 20 May 2012 19:06:27 +1000 Subject: Cache unread PM notices --- inc/cache.php | 2 +- inc/config.php | 1 - inc/mod/auth.php | 25 ++++++++++++++++++++----- inc/mod/pages.php | 28 ++++++++++++++++++++++++---- 4 files changed, 45 insertions(+), 11 deletions(-) (limited to 'inc/mod/auth.php') diff --git a/inc/cache.php b/inc/cache.php index 19e6b356..0bb73589 100644 --- a/inc/cache.php +++ b/inc/cache.php @@ -48,7 +48,7 @@ class Cache { } // debug - if ($data && $config['debug']) { + if ($data !== false && $config['debug']) { $debug['cached'][] = $key; } diff --git a/inc/config.php b/inc/config.php index d39f1158..75475a9a 100644 --- a/inc/config.php +++ b/inc/config.php @@ -38,7 +38,6 @@ $config['check_updates_time'] = 43200; // 12 hours // Shows some extra information at the bottom of pages. Good for debugging development. - // Also experimental. $config['debug'] = false; // For development purposes. Turns 'display_errors' on. Not recommended for production. $config['verbose_errors'] = true; diff --git a/inc/mod/auth.php b/inc/mod/auth.php index f9b863c8..d200c23f 100644 --- a/inc/mod/auth.php +++ b/inc/mod/auth.php @@ -123,15 +123,30 @@ if (isset($_COOKIE[$config['cookies']['mod']])) { } function create_pm_header() { - global $mod; + global $mod, $config; + + if ($config['cache']['enabled'] && ($header = cache::get('pm_unread_' . $mod['id'])) !== false) { + if ($header === true) + return false; + + return $header; + } + $query = prepare("SELECT `id` FROM `pms` WHERE `to` = :id AND `unread` = 1"); $query->bindValue(':id', $mod['id'], PDO::PARAM_INT); $query->execute() or error(db_error($query)); - if ($pm = $query->fetch()) { - return Array('id' => $pm['id'], 'waiting' => $query->rowCount() - 1); - } + if ($pm = $query->fetch()) + $header = Array('id' => $pm['id'], 'waiting' => $query->rowCount() - 1); + else + $header = true; + + if ($config['cache']['enabled']) + cache::set('pm_unread_' . $mod['id'], $header); + + if ($header === true) + return false; - return false; + return $header; } diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 6c79e21f..6b644867 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -88,10 +88,15 @@ function mod_dashboard() { } } - $query = prepare('SELECT COUNT(*) FROM `pms` WHERE `to` = :id AND `unread` = 1'); - $query->bindValue(':id', $mod['id']); - $query->execute() or error(db_error($query)); - $args['unread_pms'] = $query->fetchColumn(0); + if (!$config['cache']['enabled'] || ($args['unread_pms'] = cache::get('pm_unreadcount_' . $mod['id'])) === false) { + $query = prepare('SELECT COUNT(*) FROM `pms` WHERE `to` = :id AND `unread` = 1'); + $query->bindValue(':id', $mod['id']); + $query->execute() or error(db_error($query)); + $args['unread_pms'] = $query->fetchColumn(0); + + if ($config['cache']['enabled']) + cache::set('pm_unreadcount_' . $mod['id'], $args['unread_pms']); + } if ($mod['type'] >= ADMIN && $config['check_updates']) { if (!$config['version']) @@ -1247,6 +1252,11 @@ function mod_pm($id, $reply = false) { $query->bindValue(':id', $id); $query->execute() or error(db_error($query)); + if ($config['cache']['enabled']) { + cache::delete('pm_unread_' . $mod['id']); + cache::delete('pm_unreadcount_' . $mod['id']); + } + header('Location: ?/', true, $config['redirect_http']); return; } @@ -1256,6 +1266,11 @@ function mod_pm($id, $reply = false) { $query->bindValue(':id', $id); $query->execute() or error(db_error($query)); + if ($config['cache']['enabled']) { + cache::delete('pm_unread_' . $mod['id']); + cache::delete('pm_unreadcount_' . $mod['id']); + } + modLog('Read a PM'); } @@ -1325,6 +1340,11 @@ function mod_new_pm($username) { $query->bindValue(':time', time()); $query->execute() or error(db_error($query)); + if ($config['cache']['enabled']) { + cache::delete('pm_unread_' . $id); + cache::delete('pm_unreadcount_' . $id); + } + modLog('Sent a PM to ' . utf8tohtml($username)); header('Location: ?/', true, $config['redirect_http']); -- cgit v1.3