Source code of Leftypol imageboard
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
19 KiB

<?php
11 years ago
/*
* Copyright (c) 2010-2012 Tinyboard Development Group
*/
require 'inc/functions.php';
require 'inc/anti-bot.php';
11 years ago
// Fix for magic quotes
if (get_magic_quotes_gpc()) {
function strip_array($var) {
return is_array($var) ? array_map('strip_array', $var) : stripslashes($var);
}
11 years ago
$_GET = strip_array($_GET);
$_POST = strip_array($_POST);
}
11 years ago
if (isset($_POST['delete'])) {
11 years ago
// Delete
11 years ago
if (!isset($_POST['board'], $_POST['password']))
11 years ago
error($config['error']['bot']);
$password = &$_POST['password'];
11 years ago
if ($password == '')
11 years ago
error($config['error']['invalidpassword']);
$delete = array();
11 years ago
foreach ($_POST as $post => $value) {
if (preg_match('/^delete_(\d+)$/', $post, $m)) {
11 years ago
$delete[] = (int)$m[1];
13 years ago
}
11 years ago
}
checkDNSBL();
13 years ago
11 years ago
// Check if board exists
11 years ago
if (!openBoard($_POST['board']))
11 years ago
error($config['error']['noboard']);
// Check if banned
checkBan($board['uri']);
11 years ago
if (empty($delete))
11 years ago
error($config['error']['nodelete']);
13 years ago
11 years ago
foreach ($delete as &$id) {
11 years ago
$query = prepare(sprintf("SELECT `thread`, `time`,`password` FROM `posts_%s` WHERE `id` = :id", $board['uri']));
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
12 years ago
11 years ago
if ($post = $query->fetch()) {
if ($password != '' && $post['password'] != $password)
11 years ago
error($config['error']['invalidpassword']);
13 years ago
11 years ago
if ($post['time'] >= time() - $config['delete_time']) {
11 years ago
error(sprintf($config['error']['delete_too_soon'], until($post['time'] + $config['delete_time'])));
}
13 years ago
11 years ago
if (isset($_POST['file'])) {
11 years ago
// Delete just the file
deleteFile($id);
} else {
// Delete entire post
deletePost($id);
13 years ago
}
11 years ago
_syslog(LOG_INFO, 'Deleted post: ' .
'/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $id) . ($post['thread'] ? '#' . $id : '')
);
13 years ago
}
11 years ago
}
11 years ago
buildIndex();
$is_mod = isset($_POST['mod']) && $_POST['mod'];
$root = $is_mod ? $config['root'] . $config['file_mod'] . '?/' : $config['root'];
header('Location: ' . $root . $board['dir'] . $config['file_index'], true, $config['redirect_http']);
11 years ago
} elseif (isset($_POST['report'])) {
if (!isset($_POST['board'], $_POST['password'], $_POST['reason']))
11 years ago
error($config['error']['bot']);
$report = array();
11 years ago
foreach ($_POST as $post => $value) {
if (preg_match('/^delete_(\d+)$/', $post, $m)) {
11 years ago
$report[] = (int)$m[1];
}
11 years ago
}
checkDNSBL();
11 years ago
// Check if board exists
11 years ago
if (!openBoard($_POST['board']))
11 years ago
error($config['error']['noboard']);
// Check if banned
checkBan($board['uri']);
11 years ago
if (empty($report))
11 years ago
error($config['error']['noreport']);
11 years ago
if (count($report) > $config['report_limit'])
11 years ago
error($config['error']['toomanyreports']);
$reason = &$_POST['reason'];
markup($reason);
11 years ago
foreach ($report as &$id) {
11 years ago
$query = prepare(sprintf("SELECT `thread` FROM `posts_%s` WHERE `id` = :id", $board['uri']));
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
$post = $query->fetch();
11 years ago
if ($post) {
if ($config['syslog'])
11 years ago
_syslog(LOG_INFO, 'Reported post: ' .
'/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $id) . ($post['thread'] ? '#' . $id : '') .
' for "' . $reason . '"'
);
$query = prepare("INSERT INTO `reports` VALUES (NULL, :time, :ip, :board, :post, :reason)");
$query->bindValue(':time', time(), PDO::PARAM_INT);
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
$query->bindValue(':board', $board['uri'], PDO::PARAM_INT);
11 years ago
$query->bindValue(':post', $id, PDO::PARAM_INT);
$query->bindValue(':reason', $reason, PDO::PARAM_STR);
$query->execute() or error(db_error($query));
}
11 years ago
}
$is_mod = isset($_POST['mod']) && $_POST['mod'];
$root = $is_mod ? $config['root'] . $config['file_mod'] . '?/' : $config['root'];
header('Location: ' . $root . $board['dir'] . $config['file_index'], true, $config['redirect_http']);
11 years ago
} elseif (isset($_POST['post'])) {
11 years ago
11 years ago
if (!isset($_POST['subject'], $_POST['body'], $_POST['board']))
11 years ago
error($config['error']['bot']);
11 years ago
if (!isset($_POST['name']))
11 years ago
$_POST['name'] = $config['anonymous'];
11 years ago
if (!isset($_POST['email']))
11 years ago
$_POST['email'] = '';
11 years ago
if (!isset($_POST['password']))
11 years ago
$_POST['password'] = '';
$post = array('board' => $_POST['board']);
11 years ago
if (isset($_POST['thread'])) {
11 years ago
$post['op'] = false;
$post['thread'] = round($_POST['thread']);
11 years ago
} elseif ($config['quick_reply'] && isset($_POST['quick-reply'])) {
11 years ago
$post['op'] = false;
$post['thread'] = round($_POST['quick-reply']);
} else
$post['op'] = true;
11 years ago
if (!(($post['op'] && $_POST['post'] == $config['button_newtopic']) ||
11 years ago
(!$post['op'] && $_POST['post'] == $config['button_reply'])))
error($config['error']['bot']);
// Check the referrer
11 years ago
if (!isset($_SERVER['HTTP_REFERER']) || !preg_match($config['referer_match'], $_SERVER['HTTP_REFERER']))
11 years ago
error($config['error']['referer']);
checkDNSBL();
11 years ago
// Check if board exists
11 years ago
if (!openBoard($post['board']))
11 years ago
error($config['error']['noboard']);
// Check if banned
checkBan($board['uri']);
// Check for CAPTCHA right after opening the board so the "return" link is in there
11 years ago
if ($config['recaptcha']) {
if (!isset($_POST['recaptcha_challenge_field']) || !isset($_POST['recaptcha_response_field']))
error($config['error']['bot']);
11 years ago
// Check what reCAPTCHA has to say...
$resp = recaptcha_check_answer($config['recaptcha_private'],
$_SERVER['REMOTE_ADDR'],
$_POST['recaptcha_challenge_field'],
$_POST['recaptcha_response_field']);
11 years ago
if (!$resp->is_valid) {
11 years ago
error($config['error']['captcha']);
}
11 years ago
}
11 years ago
if ($post['mod'] = isset($_POST['mod']) && $_POST['mod']) {
require 'inc/mod.php';
11 years ago
if (!$mod) {
// Liar. You're not a mod.
error($config['error']['notamod']);
}
$post['sticky'] = $post['op'] && isset($_POST['sticky']);
$post['locked'] = $post['op'] && isset($_POST['lock']);
$post['raw'] = isset($_POST['raw']);
11 years ago
if ($post['sticky'] && !hasPermission($config['mod']['sticky'], $board['uri']))
error($config['error']['noaccess']);
11 years ago
if ($post['locked'] && !hasPermission($config['mod']['lock'], $board['uri']))
error($config['error']['noaccess']);
11 years ago
if ($post['raw'] && !hasPermission($config['mod']['rawhtml'], $board['uri']))
error($config['error']['noaccess']);
}
11 years ago
if (!$post['mod'] && checkSpam(array($board['uri'], isset($post['thread']) && !($config['quick_reply'] && isset($_POST['quick-reply'])) ? $post['thread'] : null)))
11 years ago
error($config['error']['spam']);
11 years ago
if ($config['robot_enable'] && $config['robot_mute']) {
11 years ago
checkMute();
}
//Check if thread exists
11 years ago
if (!$post['op']) {
11 years ago
$query = prepare(sprintf("SELECT `sticky`,`locked`,`sage` FROM `posts_%s` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri']));
$query->bindValue(':id', $post['thread'], PDO::PARAM_INT);
$query->execute() or error(db_error());
11 years ago
if (!$thread = $query->fetch()) {
11 years ago
// Non-existant
error($config['error']['nonexistant']);
}
11 years ago
}
12 years ago
11 years ago
// Check for an embed field
11 years ago
if ($config['enable_embedding'] && isset($_POST['embed']) && !empty($_POST['embed'])) {
11 years ago
// yep; validate it
$value = $_POST['embed'];
11 years ago
foreach ($config['embedding'] as &$embed) {
if ($html = preg_replace($embed[0], $embed[1], $value)) {
if ($html == $value) {
11 years ago
// Nope.
continue;
12 years ago
}
11 years ago
// Width and height
$html = str_replace('%%tb_width%%', $config['embed_width'], $html);
$html = str_replace('%%tb_height%%', $config['embed_height'], $html);
// Validated. It works.
$post['embed'] = $html;
// This looks messy right now, I know. I'll work on a better alternative later.
$post['no_longer_require_an_image_for_op'] = true;
break;
12 years ago
}
}
11 years ago
if (!isset($post['embed'])) {
11 years ago
error($config['error']['invalid_embed']);
}
11 years ago
}
11 years ago
if (!hasPermission($config['mod']['bypass_field_disable'], $board['uri'])) {
if ($config['field_disable_name'])
11 years ago
$_POST['name'] = $config['anonymous']; // "forced anonymous"
11 years ago
if ($config['field_disable_email'])
11 years ago
$_POST['email'] = '';
11 years ago
if ($config['field_disable_password'])
11 years ago
$_POST['password'] = '';
}
// Check for a file
11 years ago
if ($post['op'] && !isset($post['no_longer_require_an_image_for_op'])) {
if (!isset($_FILES['file']['tmp_name']) || $_FILES['file']['tmp_name'] == '' && $config['force_image_op'])
11 years ago
error($config['error']['noimage']);
}
$post['name'] = $_POST['name'] != '' ? $_POST['name'] : $config['anonymous'];
$post['subject'] = $_POST['subject'];
$post['email'] = utf8tohtml($_POST['email']);
$post['body'] = $_POST['body'];
$post['password'] = $_POST['password'];
$post['has_file'] = !isset($post['embed']) && (($post['op'] && !isset($post['no_longer_require_an_image_for_op']) && $config['force_image_op']) || (isset($_FILES['file']) && $_FILES['file']['tmp_name'] != ''));
11 years ago
if ($post['has_file'])
11 years ago
$post['filename'] = utf8tohtml(get_magic_quotes_gpc() ? stripslashes($_FILES['file']['name']) : $_FILES['file']['name']);
11 years ago
if (!($post['has_file'] || isset($post['embed'])) || (($post['op'] && $config['force_body_op']) || (!$post['op'] && $config['force_body']))) {
11 years ago
$stripped_whitespace = preg_replace('/[\s]/u', '', $post['body']);
11 years ago
if ($stripped_whitespace == '') {
11 years ago
error($config['error']['tooshort_body']);
13 years ago
}
11 years ago
}
// Check if thread is locked
// but allow mods to post
11 years ago
if (!$post['op'] && !hasPermission($config['mod']['postinlocked'], $board['uri'])) {
if ($thread['locked'])
11 years ago
error($config['error']['locked']);
}
11 years ago
if ($post['has_file']) {
11 years ago
$size = $_FILES['file']['size'];
11 years ago
if ($size > $config['max_filesize'])
11 years ago
error(sprintf3($config['error']['filesize'], array(
'sz' => number_format($size),
'filesz' => number_format($size),
'maxsz' => number_format($config['max_filesize'])
)));
}
11 years ago
if ($mod && $mod['type'] >= MOD && preg_match('/^((.+) )?## (.+)$/', $post['name'], $match)) {
if (($mod['type'] == MOD && $match[3] == 'Mod') || $mod['type'] >= ADMIN) {
11 years ago
$post['capcode'] = utf8tohtml($match[3]);
$post['name'] = $match[2] != '' ? $match[2] : $config['anonymous'];
}
11 years ago
} else {
$post['capcode'] = false;
}
$trip = generate_tripcode($post['name']);
$post['name'] = $trip[0];
$post['trip'] = isset($trip[1]) ? $trip[1] : '';
11 years ago
if (strtolower($post['email']) == 'noko') {
11 years ago
$noko = true;
$post['email'] = '';
} else $noko = false;
11 years ago
if ($post['has_file']) {
11 years ago
$post['extension'] = strtolower(substr($post['filename'], strrpos($post['filename'], '.') + 1));
11 years ago
if (isset($config['filename_func']))
11 years ago
$post['file_id'] = $config['filename_func']($post);
else
$post['file_id'] = time() . substr(microtime(), 2, 3);
$post['file'] = $board['dir'] . $config['dir']['img'] . $post['file_id'] . '.' . $post['extension'];
$post['thumb'] = $board['dir'] . $config['dir']['thumb'] . $post['file_id'] . '.' . ($config['thumb_ext'] ? $config['thumb_ext'] : $post['extension']);
}
// Check string lengths
11 years ago
if (mb_strlen($post['name']) > 35)
11 years ago
error(sprintf($config['error']['toolong'], 'name'));
11 years ago
if (mb_strlen($post['email']) > 40)
11 years ago
error(sprintf($config['error']['toolong'], 'email'));
11 years ago
if (mb_strlen($post['subject']) > 100)
11 years ago
error(sprintf($config['error']['toolong'], 'subject'));
11 years ago
if (!$mod && mb_strlen($post['body']) > $config['max_body'])
11 years ago
error($config['error']['toolong_body']);
11 years ago
if (mb_strlen($post['password']) > 20)
11 years ago
error(sprintf($config['error']['toolong'], 'password'));
wordfilters($post['body']);
$post['body_nomarkup'] = $post['body'];
11 years ago
if (!($mod && isset($post['raw']) && $post['raw']))
11 years ago
$post['tracked_cites'] = markup($post['body'], true);
// Check for a flood
11 years ago
if (!hasPermission($config['mod']['flood'], $board['uri']) && checkFlood($post)) {
11 years ago
error($config['error']['flood']);
}
require_once 'inc/filters.php';
do_filters($post);
11 years ago
if ($post['has_file']) {
if (!in_array($post['extension'], $config['allowed_ext']) && !in_array($post['extension'], $config['allowed_ext_files']))
11 years ago
error($config['error']['unknownext']);
11 years ago
$is_an_image = !in_array($post['extension'], $config['allowed_ext_files']);
12 years ago
11 years ago
// Truncate filename if it is too long
$post['filename'] = substr($post['filename'], 0, $config['max_filename_len']);
13 years ago
11 years ago
$upload = $_FILES['file']['tmp_name'];
11 years ago
if (!is_readable($upload))
11 years ago
error($config['error']['nomove']);
13 years ago
11 years ago
$post['filehash'] = $config['file_hash']($upload);
$post['filesize'] = filesize($upload);
11 years ago
if ($is_an_image) {
11 years ago
// Check IE MIME type detection XSS exploit
$buffer = file_get_contents($upload, null, null, null, 255);
11 years ago
if (preg_match($config['ie_mime_type_detection'], $buffer)) {
11 years ago
undoImage($post);
error($config['error']['mime_exploit']);
}
11 years ago
require_once 'inc/image.php';
11 years ago
if ($config['thumb_method'] == 'imagick') {
11 years ago
// This is tricky, because Imagick won't let us find
// an image's dimensions without loading it all into
// memory first, unlike GD which provides the
// getimagesize() to do exactly that. This section
// is why GD is required, even when using Imagick
// instead. There doesn't seem to be an alternative.
// Necessary for security, as Imagick even ignores
// PHP's memory limit.
11 years ago
// first try GD's getimagesize()
11 years ago
if ($size = @getimagesize($upload)) {
if ($size[0] > $config['max_width'] || $size[1] > $config['max_height']) {
11 years ago
error($config['error']['maxsize']);
}
12 years ago
} else {
11 years ago
// GD failed
// TODO?
12 years ago
}
} else {
11 years ago
// find dimensions of an image using GD
11 years ago
if (!$size = @getimagesize($upload)) {
11 years ago
error($config['error']['invalidimg']);
}
11 years ago
if ($size[0] > $config['max_width'] || $size[1] > $config['max_height']) {
11 years ago
error($config['error']['maxsize']);
}
}
// create image object
$image = new Image($upload, $post['extension']);
11 years ago
if ($image->size->width > $config['max_width'] || $image->size->height > $config['max_height']) {
11 years ago
$image->delete();
error($config['error']['maxsize']);
}
$post['width'] = $image->size->width;
$post['height'] = $image->size->height;
11 years ago
if ($config['spoiler_images'] && isset($_POST['spoiler'])) {
11 years ago
$post['thumb'] = 'spoiler';
11 years ago
$size = @getimagesize($config['spoiler_image']);
12 years ago
$post['thumbwidth'] = $size[0];
$post['thumbheight'] = $size[1];
11 years ago
} elseif ($config['minimum_copy_resize'] &&
11 years ago
$image->size->width <= $config['thumb_width'] &&
$image->size->height <= $config['thumb_height'] &&
$post['extension'] == ($config['thumb_ext'] ? $config['thumb_ext'] : $post['extension'])) {
// Copy, because there's nothing to resize
copy($upload, $post['thumb']);
$post['thumbwidth'] = $image->size->width;
$post['thumbheight'] = $image->size->height;
} else {
11 years ago
$thumb = $image->resize(
$config['thumb_ext'] ? $config['thumb_ext'] : $post['extension'],
$post['op'] ? $config['thumb_op_width'] : $config['thumb_width'],
$post['op'] ? $config['thumb_op_height'] : $config['thumb_height']
);
$thumb->to($post['thumb']);
$post['thumbwidth'] = $thumb->width;
$post['thumbheight'] = $thumb->height;
$thumb->_destroy();
}
11 years ago
11 years ago
if ($config['redraw_image']) {
11 years ago
$image->to($post['file']);
$dont_copy_file = true;
}
11 years ago
$image->destroy();
} else {
11 years ago
// not an image
//copy($config['file_thumb'], $post['thumb']);
$post['thumb'] = 'file';
$size = @getimagesize($config['file_thumb']);
$post['thumbwidth'] = $size[0];
$post['thumbheight'] = $size[1];
}
if (!isset($dont_copy_file) || !$dont_copy_file) {
if ([email protected]_uploaded_file($_FILES['file']['tmp_name'], $post['file']))
error($config['error']['nomove']);
}
11 years ago
}
11 years ago
if ($post['has_file'] && $config['image_reject_repost'] && $p = getPostByHash($post['filehash'])) {
11 years ago
undoImage($post);
error(sprintf($config['error']['fileexists'],
$post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root'] .
$board['dir'] . $config['dir']['res'] .
($p['thread'] ?
$p['thread'] . '.html#' . $p['id']
:
$p['id'] . '.html'
)
));
}
11 years ago
if (!hasPermission($config['mod']['postunoriginal'], $board['uri']) && $config['robot_enable'] && checkRobot($post['body_nomarkup'])) {
11 years ago
undoImage($post);
11 years ago
if ($config['robot_mute']) {
11 years ago
error(sprintf($config['error']['muted'], mute()));
} else {
11 years ago
error($config['error']['unoriginal']);
}
}
11 years ago
// Remove board directories before inserting them into the database.
11 years ago
if ($post['has_file']) {
11 years ago
$post['file_path'] = $post['file'];
$post['file'] = substr_replace($post['file'], '', 0, mb_strlen($board['dir'] . $config['dir']['img']));
11 years ago
if ($is_an_image && $post['thumb'] != 'spoiler')
11 years ago
$post['thumb'] = substr_replace($post['thumb'], '', 0, mb_strlen($board['dir'] . $config['dir']['thumb']));
}
$post = (object)$post;
11 years ago
if ($error = event('post', $post)) {
11 years ago
undoImage((array)$post);
error($error);
}
$post = (array)$post;
$id = post($post);
11 years ago
if (isset($post['tracked_cites'])) {
foreach ($post['tracked_cites'] as $cite) {
11 years ago
$query = prepare('INSERT INTO `cites` VALUES (:board, :post, :target_board, :target)');
$query->bindValue(':board', $board['uri']);
$query->bindValue(':post', $id, PDO::PARAM_INT);
$query->bindValue(':target_board',$cite[0]);
$query->bindValue(':target', $cite[1], PDO::PARAM_INT);
$query->execute() or error(db_error($query));
}
}
buildThread($post['op'] ? $id : $post['thread']);
11 years ago
if (!$post['op'] && strtolower($post['email']) != 'sage' && !$thread['sage'] && ($config['reply_limit'] == 0 || numPosts($post['thread']) < $config['reply_limit'])) {
11 years ago
bumpThread($post['thread']);
}
11 years ago
if ($post['op'])
11 years ago
clean();
event('post-after', $post);
buildIndex();
11 years ago
if (isset($_SERVER['HTTP_REFERER'])) {
11 years ago
// Tell Javascript that we posted successfully
11 years ago
if (isset($_COOKIE[$config['cookies']['js']]))
11 years ago
$js = json_decode($_COOKIE[$config['cookies']['js']]);
else
$js = (object) array();
// Tell it to delete the cached post for referer
$js->{$_SERVER['HTTP_REFERER']} = true;
// Encode and set cookie
setcookie($config['cookies']['js'], json_encode($js), 0, $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, false, false);
}
$root = $post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root'];
11 years ago
if ($config['always_noko'] || $noko) {
11 years ago
$redirect = $root . $board['dir'] . $config['dir']['res'] .
sprintf($config['file_page'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : '');
} else {
$redirect = $root . $board['dir'] . $config['file_index'];
}
11 years ago
if ($config['syslog'])
11 years ago
_syslog(LOG_INFO, 'New post: /' . $board['dir'] . $config['dir']['res'] .
11 years ago
sprintf($config['file_page'], $post['op'] ? $id : $post['thread']) . (!$post['op'] ? '#' . $id : ''));
11 years ago
rebuildThemes('post');
header('Location: ' . $redirect, true, $config['redirect_http']);
} else {
11 years ago
if (!file_exists($config['has_installed'])) {
11 years ago
header('Location: install.php', true, $config['redirect_http']);
} else {
// They opened post.php in their browser manually.
error($config['error']['nopost']);
}
}