2010-12-02 04:57:15 -05:00
|
|
|
<?php
|
2010-12-10 05:03:20 -05:00
|
|
|
|
2010-12-02 04:55:56 -05:00
|
|
|
// Creates a small random string for validating moderators' cookies
|
|
|
|
function mkhash($length=12) {
|
|
|
|
// The method here isn't really important,
|
|
|
|
// but I think this generates a relatively
|
|
|
|
// unique string that looks cool.
|
|
|
|
// If you choose to change this, make sure it cannot include a ':' character.
|
|
|
|
return substr(base64_encode(sha1(rand() . time(), true)), 0, $length);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-12-17 09:18:03 -05:00
|
|
|
$query = prepare("SELECT `id`,`type` FROM `mods` WHERE `username` = :username AND `password` = :password LIMIT 1");
|
|
|
|
$query->bindValue(':username', $username);
|
|
|
|
$query->bindValue(':password', $password);
|
2011-02-11 07:02:30 -05:00
|
|
|
$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,
|
|
|
|
'password' => $password,
|
|
|
|
'hash' => isset($_SESSION['mod']['hash']) ? $_SESSION['mod']['hash'] : mkhash()
|
|
|
|
);
|
|
|
|
} else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setCookies() {
|
2011-02-15 22:44:42 -05:00
|
|
|
global $mod, $config;
|
2010-12-02 04:55:56 -05:00
|
|
|
if(!$mod) error('setCookies() was called for a non-moderator!');
|
|
|
|
|
2011-02-15 22:44:42 -05:00
|
|
|
// $config['cookies']['mod'] contains username:hash
|
|
|
|
setcookie($config['cookies']['mod'], $mod['username'] . ':' . $mod['hash'], time()+$config['cookies']['expire'], $config['cookies']['jail']?$config['root']:'/', null, false, true);
|
2010-12-02 04:55:56 -05:00
|
|
|
|
|
|
|
// Put $mod in the session
|
|
|
|
$_SESSION['mod'] = $mod;
|
|
|
|
|
|
|
|
// Lock sessions to IP addresses
|
2011-02-16 06:04:53 -05:00
|
|
|
if($config['mod']['lock_ip'])
|
2010-12-02 04:55:56 -05:00
|
|
|
$_SESSION['mod']['ip'] = $_SERVER['REMOTE_ADDR'];
|
|
|
|
}
|
|
|
|
|
|
|
|
function destroyCookies() {
|
2011-03-25 21:38:07 -04:00
|
|
|
global $config;
|
2010-12-02 04:55:56 -05:00
|
|
|
// Delete the cookies
|
2011-02-15 22:44:42 -05:00
|
|
|
setcookie($config['cookies']['mod'], 'deleted', time()-$config['cookies']['expire'], $config['cookies']['jail']?$config['root']:'/', null, false, true);
|
2010-12-02 04:55:56 -05:00
|
|
|
|
|
|
|
// Unset the session
|
|
|
|
unset($_SESSION['mod']);
|
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
return 'You have <a href="?/PM/' . $pm['id'] . '">an unread PM</a>' .
|
|
|
|
($query->rowCount() > 1 ?
|
|
|
|
', plus ' . ($query->rowCount()-1) . ' more waiting'
|
|
|
|
: '') . '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-11 07:02:30 -05:00
|
|
|
function modLog($action) {
|
|
|
|
global $mod;
|
|
|
|
$query = prepare("INSERT INTO `modlogs` VALUES (:id, :ip, :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);
|
|
|
|
$query->execute() or error(db_error($query));
|
|
|
|
}
|
|
|
|
|
2010-12-02 04:55:56 -05:00
|
|
|
if(isset($_COOKIE['mod']) && isset($_SESSION['mod']) && is_array($_SESSION['mod'])) {
|
|
|
|
// Should be username:session hash
|
|
|
|
$cookie = explode(':', $_COOKIE['mod']);
|
|
|
|
if(count($cookie) != 2) {
|
|
|
|
destroyCookies();
|
2011-02-15 22:44:42 -05:00
|
|
|
error($config['error']['malformed']);
|
2010-12-02 04:55:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate session
|
|
|
|
if( $cookie[0] != $_SESSION['mod']['username'] ||
|
|
|
|
$cookie[1] != $_SESSION['mod']['hash']) {
|
|
|
|
// Malformed cookies
|
|
|
|
destroyCookies();
|
2011-02-15 22:44:42 -05:00
|
|
|
error($config['error']['malformed']);
|
2010-12-02 04:55:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open connection
|
|
|
|
sql_open();
|
|
|
|
|
|
|
|
// Check username/password
|
|
|
|
if(!login($_SESSION['mod']['username'], $_SESSION['mod']['password'], false)) {
|
|
|
|
destroyCookies();
|
2011-02-15 22:44:42 -05:00
|
|
|
error($config['error']['invalidafter']);
|
2010-12-02 04:55:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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']) {
|
2010-12-02 04:55:56 -05:00
|
|
|
$body .= '<li style="margin-top:15px;"><a href="?/new"><strong>Create new board</strong></a></li>';
|
|
|
|
}
|
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-01-14 23:37:39 -05:00
|
|
|
function form_newBan($ip=null, $reason='', $continue=false, $delete=false, $board=false) {
|
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-01-14 23:37:39 -05:00
|
|
|
($delete ? '<input type="hidden" name="delete" value="' . htmlentities($delete) . '" />' : '') .
|
|
|
|
($board ? '<input type="hidden" name="board" value="' . htmlentities($board) . '" />' : '') .
|
2011-01-01 08:27:30 -05:00
|
|
|
'<table>' .
|
|
|
|
'<tr>' .
|
|
|
|
'<th><label for="ip">IP</label></th>' .
|
|
|
|
'<td><input type="text" name="ip" id="ip" size="15" maxlength="15" ' .
|
|
|
|
(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>' .
|
|
|
|
'<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>' .
|
|
|
|
'<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>';
|
|
|
|
}
|
2011-01-20 21:14:55 -05:00
|
|
|
|
2010-12-02 02:26:09 -05:00
|
|
|
?>
|