1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-28 09:20:58 +01:00
vichan/inc/mod.php

251 lines
7.7 KiB
PHP
Raw Normal View History

2010-12-02 10:57:15 +01:00
<?php
2010-12-10 11:03:20 +01:00
2010-12-02 10:55:56 +01: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 14:51:20 +01:00
global $mod;
2010-12-02 10:55:56 +01:00
// SHA1 password
if($makehash) {
$password = sha1($password);
}
2010-12-17 15:18:03 +01:00
$query = prepare("SELECT `id`,`type` FROM `mods` WHERE `username` = :username AND `password` = :password LIMIT 1");
$query->bindValue(':username', $username);
$query->bindValue(':password', $password);
$query->execute();
2010-12-02 10:55:56 +01:00
2010-12-17 15:18:03 +01:00
if($user = $query->fetch()) {
2010-12-02 10:55:56 +01: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() {
global $mod;
if(!$mod) error('setCookies() was called for a non-moderator!');
// MOD_COOKIE contains username:hash
setcookie(MOD_COOKIE, $mod['username'] . ':' . $mod['hash'], time()+COOKIE_EXPIRE, JAIL_COOKIES?ROOT:'/', null, false, true);
// Put $mod in the session
$_SESSION['mod'] = $mod;
// Lock sessions to IP addresses
if(MOD_LOCK_IP)
$_SESSION['mod']['ip'] = $_SERVER['REMOTE_ADDR'];
}
function destroyCookies() {
// Delete the cookies
setcookie(MOD_COOKIE, 'deleted', time()-COOKIE_EXPIRE, JAIL_COOKIES?ROOT:'/', null, false, true);
// Unset the session
unset($_SESSION['mod']);
}
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();
error(ERROR_MALFORMED);
}
// Validate session
if( $cookie[0] != $_SESSION['mod']['username'] ||
$cookie[1] != $_SESSION['mod']['hash']) {
// Malformed cookies
destroyCookies();
error(ERROR_MALFORMED);
}
// Open connection
sql_open();
// Check username/password
if(!login($_SESSION['mod']['username'], $_SESSION['mod']['password'], false)) {
destroyCookies();
error(ERROR_INVALIDAFTER);
}
}
2010-12-02 08:26:09 +01:00
// Generates a <ul> element with a list of linked
2010-12-16 16:20:16 +01:00
// boards and their subtitles. (without the <ul> opening and ending tags)
2010-12-02 08:26:09 +01:00
function ulBoards() {
2010-12-02 10:55:56 +01:00
global $mod;
2010-12-16 16:20:16 +01:00
$body = '';
2010-12-02 08:26:09 +01:00
// List of boards
$boards = listBoards();
foreach($boards as &$b) {
$body .= '<li>' .
'<a href="?/' .
sprintf(BOARD_PATH, $b['uri']) . FILE_INDEX .
'">' .
sprintf(BOARD_ABBREVIATION, $b['uri']) .
2010-12-02 10:55:56 +01:00
'</a> - ' .
$b['title'] .
(isset($b['subtitle']) ? '<span class="unimportant"> — ' . $b['subtitle'] . '</span>' : '') .
2010-12-02 08:26:09 +01:00
'</li>';
}
2010-12-16 16:20:16 +01:00
if($mod['type'] >= MOD_NEWBOARD) {
2010-12-02 10:55:56 +01:00
$body .= '<li style="margin-top:15px;"><a href="?/new"><strong>Create new board</strong></a></li>';
}
2010-12-16 16:20:16 +01:00
return $body;
2010-12-02 08:26:09 +01:00
}
2010-12-02 10:55:56 +01:00
2011-01-01 14:27:30 +01:00
function form_newBan($ip=null, $reason='', $continue=false) {
return '<fieldset><legend>New ban</legend>' .
'<form action="" method="post">' .
($continue ? '<input type="hidden" name="continue" value="' . htmlentities($continue) . '" />' : '') .
'<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 10:55:56 +01:00
function form_newBoard() {
return '<fieldset><legend>New board</legend>' .
'<form action="?/new" method="post">' .
'<table>' .
'<tr>' .
2011-01-01 14:27:30 +01:00
'<th><label for="board">URI</label></th>' .
2010-12-02 10:55:56 +01:00
'<td><input type="text" name="uri" id="board" size="3" maxlength="8" />' .
2011-01-01 14:27:30 +01:00
' <span class="unimportant">(eg. "b"; "mu")</span></td>' .
2010-12-02 10:55:56 +01:00
'</tr>' .
'<tr>' .
2011-01-01 14:27:30 +01:00
'<th><label for="title">Title</label></th>' .
2010-12-02 10:55:56 +01:00
'<td><input type="text" name="title" id="title" size="15" maxlength="20" />' .
2011-01-01 14:27:30 +01:00
' <span class="unimportant">(eg. "Random")</span></td>' .
2010-12-02 10:55:56 +01:00
'</tr>' .
'<tr>' .
2011-01-01 14:27:30 +01:00
'<th><label for="subtitle">Subtitle</label></th>' .
2010-12-02 10:55:56 +01:00
'<td><input type="text" name="subtitle" id="subtitle" size="20" maxlength="40" />' .
2011-01-01 14:27:30 +01:00
' <span class="unimportant">(optional)</span></td>' .
2010-12-02 10:55:56 +01:00
'</tr>' .
'<tr>' .
'<td></td>' .
'<td><input name="new_board" type="submit" value="New Board" /></td>' .
'</tr>' .
'</table>' .
'</form>' .
'</fieldset>';
}
// Remove file from post
function deleteFile($id) {
global $board;
2011-01-01 11:44:11 +01:00
$query = prepare(sprintf("SELECT `thread`,`thumb`,`file` FROM `posts_%s` WHERE `id` = :id AND `thread` IS NOT NULL LIMIT 1", $board['uri']));
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
if($query->rowCount() < 1) {
error(ERROR_INVALIDPOST);
}
2011-01-01 11:44:11 +01:00
$post = $query->fetch();
$query = prepare(sprintf("UPDATE `posts_%s` SET `thumb` = NULL, `thumbwidth` = NULL, `thumbheight` = NULL, `filewidth` = NULL, `fileheight` = NULL, `filesize` = NULL, `filename` = NULL, `filehash` = NULL, `file` = :file WHERE `id` = :id OR `thread` = :id", $board['uri']));
if($post['file'] == 'deleted') {
// Already deleted; remove file fully
$query->bindValue(':file', null, PDO::PARAM_NULL);
} else {
// Delete thumbnail
@unlink($board['dir'] . DIR_THUMB . $post['thumb']);
// Delete file
@unlink($board['dir'] . DIR_IMG . $post['file']);
// Set file to 'deleted'
$query->bindValue(':file', 'deleted', PDO::PARAM_INT);
}
// Update database
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
2011-01-01 11:44:11 +01:00
buildThread($post['thread']);
}
2010-12-16 06:36:33 +01:00
// Delete a post (reply or thread)
function deletePost($id) {
2010-12-17 15:18:03 +01:00
global $board;
2010-12-16 06:36:33 +01:00
// Select post and replies (if thread) in one query
2010-12-17 15:18:03 +01:00
$query = prepare(sprintf("SELECT `id`,`thread`,`thumb`,`file` FROM `posts_%s` WHERE `id` = :id OR `thread` = :id", $board['uri']));
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
if($query->rowCount() < 1) {
2010-12-16 07:05:29 +01:00
error(ERROR_INVALIDPOST);
}
2010-12-16 06:36:33 +01:00
// Delete posts and maybe replies
2010-12-17 15:18:03 +01:00
while($post = $query->fetch()) {
2010-12-16 06:36:33 +01:00
if(!$post['thread']) {
// Delete thread HTML page
@unlink($board['dir'] . DIR_RES . sprintf(FILE_PAGE, $post['id']));
} elseif($query->rowCount() == 1) {
// Rebuild thread
$rebuild = $post['thread'];
2010-12-16 06:36:33 +01:00
}
if($post['thumb']) {
// Delete thumbnail
@unlink($board['dir'] . DIR_THUMB . $post['thumb']);
}
if($post['file']) {
// Delete file
@unlink($board['dir'] . DIR_IMG . $post['file']);
}
}
2010-12-16 10:29:35 +01:00
2010-12-17 15:18:03 +01:00
$query = prepare(sprintf("DELETE FROM `posts_%s` WHERE `id` = :id OR `thread` = :id", $board['uri']));
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
if(isset($rebuild)) {
buildThread($rebuild);
}
2010-12-16 06:36:33 +01:00
}
2010-12-02 08:26:09 +01:00
?>