lainchan/inc/mod.php

304 lines
9.9 KiB
PHP
Raw Normal View History

2010-12-02 04:57:15 -05:00
<?php
2011-04-13 08:21:07 -04:00
if($_SERVER['SCRIPT_FILENAME'] == str_replace('\\', '/', __FILE__)) {
// You cannot request this file directly.
header('Location: ../', true, 302);
exit;
}
// create a hash/salt pair for validate logins
function mkhash($username, $password, $salt = false) {
global $config;
if(!$salt) {
// create some sort of salt for the hash
$salt = substr(base64_encode(sha1(rand() . time(), true) . $config['cookies']['salt']), 0, 15);
$generated_salt = true;
}
// generate hash (method is not important as long as it's strong)
$hash = substr(base64_encode(md5($username . sha1($username . $password . $salt . ($config['mod']['lock_ip'] ? $_SERVER['REMOTE_ADDR'] : ''), true), true)), 0, 20);
if(isset($generated_salt))
return Array($hash, $salt);
else
return $hash;
2010-12-02 04:55:56 -05:00
}
2011-07-03 09:05:14 -04:00
function hasPermission($action = null, $board = null, $_mod = null) {
global $config;
2011-07-03 09:05:14 -04:00
if(isset($_mod))
$mod = &$_mod;
else
global $mod;
if(isset($action) && $mod['type'] < $action)
return false;
if(!isset($board))
return true;
if(!$config['mod']['skip_per_board'] && !in_array('*', $mod['boards']) && !in_array($board, $mod['boards']))
2011-07-03 09:05:14 -04:00
return false;
return true;
}
2010-12-02 04:55:56 -05:00
function login($username, $password, $makehash=true) {
2011-01-01 08:51:20 -05:00
global $mod;
2010-12-02 04:55:56 -05:00
// SHA1 password
if($makehash) {
$password = sha1($password);
}
2011-07-03 09:05:14 -04:00
$query = prepare("SELECT `id`,`type`,`boards` FROM `mods` WHERE `username` = :username AND `password` = :password LIMIT 1");
2010-12-17 09:18:03 -05:00
$query->bindValue(':username', $username);
$query->bindValue(':password', $password);
$query->execute() or error(db_error($query));
2010-12-02 04:55:56 -05:00
2010-12-17 09:18:03 -05:00
if($user = $query->fetch()) {
2010-12-02 04:55:56 -05:00
return $mod = Array(
'id' => $user['id'],
'type' => $user['type'],
'username' => $username,
'hash' => mkhash($username, $password),
2011-07-03 09:05:14 -04:00
'boards' => explode(',', $user['boards'])
2010-12-02 04:55:56 -05:00
);
} else return false;
}
function setCookies() {
2011-02-15 22:44:42 -05:00
global $mod, $config;
if(!$mod)
error('setCookies() was called for a non-moderator!');
2010-12-02 04:55:56 -05:00
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);
2010-12-02 04:55:56 -05:00
}
function destroyCookies() {
2011-03-25 21:38:07 -04:00
global $config;
2010-12-02 04:55:56 -05:00
// Delete the cookies
setcookie($config['cookies']['mod'], 'deleted', time() - $config['cookies']['expire'], $config['cookies']['jail']?$config['cookies']['path'] : '/', null, false, true);
2010-12-02 04:55:56 -05:00
}
2011-03-17 01:52:43 -04:00
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()) {
2011-10-05 04:41:47 -04:00
return Array('id' => $pm['id'], 'waiting' => $query->rowCount() - 1);
2011-03-17 01:52:43 -04:00
}
return false;
}
function modLog($action, $_board=null) {
global $mod, $board;
$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['id']);
else
$query->bindValue(':board', null, PDO::PARAM_NULL);
$query->execute() or error(db_error($query));
2011-12-05 11:00:59 -05:00
if($config['syslog'])
_syslog(LOG_INFO, '[mod/' . $mod['username'] . ']: ' . $action);
}
2010-12-02 02:26:09 -05:00
// Generates a <ul> element with a list of linked
2010-12-16 10:20:16 -05:00
// boards and their subtitles. (without the <ul> opening and ending tags)
2010-12-02 02:26:09 -05:00
function ulBoards() {
2011-02-15 22:44:42 -05:00
global $mod, $config;
2010-12-02 04:55:56 -05:00
2010-12-16 10:20:16 -05:00
$body = '';
2010-12-02 02:26:09 -05:00
// List of boards
$boards = listBoards();
foreach($boards as &$b) {
$body .= '<li>' .
'<a href="?/' .
2011-02-15 22:44:42 -05:00
sprintf($config['board_path'], $b['uri']) . $config['file_index'] .
2010-12-02 02:26:09 -05:00
'">' .
2011-02-15 22:44:42 -05:00
sprintf($config['board_abbreviation'], $b['uri']) .
2010-12-02 04:55:56 -05:00
'</a> - ' .
$b['title'] .
(isset($b['subtitle']) ? '<span class="unimportant"> — ' . $b['subtitle'] . '</span>' : '') .
2011-02-22 01:38:38 -05:00
($mod['type'] >= $config['mod']['manageboards'] ?
' <a href="?/board/' . $b['uri'] . '" class="unimportant">[manage]</a>' : '') .
2010-12-02 02:26:09 -05:00
'</li>';
}
2011-02-15 22:44:42 -05:00
if($mod['type'] >= $config['mod']['newboard']) {
2011-11-23 07:47:54 -05:00
$body .= '<li style="margin-top:15px;"><a href="?/new"><strong>' . _('Create new board') . '</strong></a></li>';
2010-12-02 04:55:56 -05:00
}
2010-12-16 10:20:16 -05:00
return $body;
2010-12-02 02:26:09 -05:00
}
2010-12-02 04:55:56 -05:00
2011-04-03 04:20:55 -04:00
function form_newBan($ip=null, $reason='', $continue=false, $delete=false, $board=false, $allow_public = false) {
global $config, $mod;
2011-07-26 23:40:27 -04:00
$boards = listBoards();
2011-11-23 07:47:54 -05:00
$__boards = '<li><input type="radio" checked="checked" name="board_id" id="board_*" value="-1"/> <label style="display:inline" for="board_*"><em>' . _('all boards') . '</em></label></li>';
2011-07-26 23:40:27 -04:00
foreach($boards as &$_board) {
$__boards .= '<li>' .
'<input type="radio" name="board_id" id="board_' . $_board['uri'] . '" value="' . $_board['id'] . '">' .
'<label style="display:inline" for="board_' . $_board['uri'] . '"> ' .
($_board['uri'] == '*' ?
'<em>"*"</em>'
:
sprintf($config['board_abbreviation'], $_board['uri'])
) .
' - ' . $_board['title'] .
'</label>' .
'</li>';
}
2011-01-01 08:27:30 -05:00
return '<fieldset><legend>New ban</legend>' .
2011-01-14 23:37:39 -05:00
'<form action="?/ban" method="post">' .
2011-01-01 08:27:30 -05:00
($continue ? '<input type="hidden" name="continue" value="' . htmlentities($continue) . '" />' : '') .
2011-04-03 04:20:55 -04:00
($delete || $allow_public ? '<input type="hidden" name="' . (!$allow_public ? 'delete' : 'post') . '" value="' . htmlentities($delete) . '" />' : '') .
2011-01-14 23:37:39 -05:00
($board ? '<input type="hidden" name="board" value="' . htmlentities($board) . '" />' : '') .
2011-01-01 08:27:30 -05:00
'<table>' .
'<tr>' .
2011-11-21 10:35:26 -05:00
'<th><label for="ip">IP ' .
($config['ban_cidr'] ? '<span class="unimportant">(or subnet)' : '') .
'</span></label></th>' .
'<td><input type="text" name="ip" id="ip" size="30" maxlength="30" ' .
2011-01-01 08:27:30 -05:00
(isset($ip) ?
'value="' . htmlentities($ip) . '" ' : ''
) .
'/></td>' .
'</tr>' .
'<tr>' .
'<th><label for="reason">Reason</label></th>' .
'<td><textarea name="reason" id="reason" rows="5" cols="30">' .
htmlentities($reason) .
'</textarea></td>' .
'</tr>' .
2011-04-03 04:20:55 -04:00
($mod['type'] >= $config['mod']['public_ban'] && $allow_public ?
'<tr>' .
'<th><label for="message">Message</label></th>' .
'<td><input type="checkbox" id="public_message" name="public_message"/>' .
' <input type="text" name="message" id="message" size="35" maxlength="200" value="' . htmlentities($config['mod']['default_ban_message']) . '" />' .
' <span class="unimportant">(public; attached to post)</span></td>' .
'<script type="text/javascript">' .
'document.getElementById(\'message\').disabled = true;' .
'document.getElementById(\'public_message\').onchange = function() {' .
'document.getElementById(\'message\').disabled = !this.checked;' .
'}' .
'</script>' .
'</tr>'
: '') .
2011-01-01 08:27:30 -05:00
'<tr>' .
'<th><label for="length">Length</label></th>' .
'<td><input type="text" name="length" id="length" size="20" maxlength="40" />' .
' <span class="unimportant">(eg. "2d1h30m" or "2 days")</span></td>' .
'</tr>' .
2011-07-26 23:40:27 -04:00
'<tr>' .
'<th>Board</th>' .
'<td><ul style="list-style:none;padding:2px 5px">' . $__boards . '</tl></td>' .
'</tr>' .
2011-01-01 08:27:30 -05:00
'<tr>' .
'<td></td>' .
'<td><input name="new_ban" type="submit" value="New Ban" /></td>' .
'</tr>' .
'</table>' .
'</form>' .
'</fieldset>';
}
2010-12-02 04:55:56 -05:00
function form_newBoard() {
return '<fieldset><legend>New board</legend>' .
'<form action="?/new" method="post">' .
'<table>' .
'<tr>' .
2011-01-01 08:27:30 -05:00
'<th><label for="board">URI</label></th>' .
2010-12-02 04:55:56 -05:00
'<td><input type="text" name="uri" id="board" size="3" maxlength="8" />' .
2011-01-01 08:27:30 -05:00
' <span class="unimportant">(eg. "b"; "mu")</span></td>' .
2010-12-02 04:55:56 -05:00
'</tr>' .
'<tr>' .
2011-01-01 08:27:30 -05:00
'<th><label for="title">Title</label></th>' .
2010-12-02 04:55:56 -05:00
'<td><input type="text" name="title" id="title" size="15" maxlength="20" />' .
2011-01-01 08:27:30 -05:00
' <span class="unimportant">(eg. "Random")</span></td>' .
2010-12-02 04:55:56 -05:00
'</tr>' .
'<tr>' .
2011-01-01 08:27:30 -05:00
'<th><label for="subtitle">Subtitle</label></th>' .
2010-12-02 04:55:56 -05:00
'<td><input type="text" name="subtitle" id="subtitle" size="20" maxlength="40" />' .
2011-01-01 08:27:30 -05:00
' <span class="unimportant">(optional)</span></td>' .
2010-12-02 04:55:56 -05:00
'</tr>' .
'<tr>' .
'<td></td>' .
'<td><input name="new_board" type="submit" value="New Board" /></td>' .
'</tr>' .
'</table>' .
'</form>' .
'</fieldset>';
}
function removeBan($id) {
2011-08-07 02:10:19 -04:00
global $config, $memcached;
$query = prepare("DELETE FROM `bans` WHERE `id` = :id");
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
2011-10-17 03:02:54 -04:00
//if($config['memcached']['enabled']) {
// Remove cached ban
// TODO
2011-10-17 03:02:54 -04:00
// $memcached->delete("ban_{$id}");
//}
}
// 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'])
);
}