tinyib/imgboard.php

398 lines
14 KiB
PHP
Raw Normal View History

2009-09-20 02:53:15 +00:00
<?php
# TinyIB
#
# https://github.com/tslocum/TinyIB
2009-09-20 02:53:15 +00:00
error_reporting(E_ALL);
ini_set("display_errors", 1);
session_start();
ob_implicit_flush();
ob_end_flush();
2009-09-20 02:53:15 +00:00
if (get_magic_quotes_gpc()) {
2014-06-24 19:51:22 +00:00
foreach ($_GET as $key => $val) {
$_GET[$key] = stripslashes($val);
}
foreach ($_POST as $key => $val) {
$_POST[$key] = stripslashes($val);
}
}
if (get_magic_quotes_runtime()) {
set_magic_quotes_runtime(0);
2009-09-20 02:53:15 +00:00
}
function fancyDie($message) {
die('<body text="#800000" bgcolor="#FFFFEE" align="center"><br><div style="display: inline-block; background-color: #F0E0D6;font-size: 1.25em;font-family: Tahoma, Geneva, sans-serif;padding: 7px;border: 1px solid #D9BFB7;border-left: none;border-top: none;">' . $message . '</div><br><br>- <a href="javascript:history.go(-1)">Click here to go back</a> -</body>');
2009-09-20 02:53:15 +00:00
}
if (!file_exists('settings.php')) {
fancyDie('Please rename the file settings.default.php to settings.php');
}
require 'settings.php';
2009-09-20 02:53:15 +00:00
// Check directories are writable by the script
$writedirs = array("res", "src", "thumb");
2014-06-24 19:51:22 +00:00
if (TINYIB_DBMODE == 'flatfile') {
$writedirs[] = "inc/flatfile";
}
2009-09-20 02:53:15 +00:00
foreach ($writedirs as $dir) {
if (!is_writable($dir)) {
fancyDie("Directory '" . $dir . "' can not be written to. Please modify its permissions.");
2009-09-20 02:53:15 +00:00
}
}
$includes = array("inc/defines.php", "inc/functions.php", "inc/html.php");
2011-01-08 01:36:00 +00:00
if (in_array(TINYIB_DBMODE, array('flatfile', 'mysql', 'sqlite'))) {
$includes[] = 'inc/database_' . TINYIB_DBMODE . '.php';
2009-09-20 02:53:15 +00:00
} else {
fancyDie("Unknown database mode specificed");
}
foreach ($includes as $include) {
include $include;
}
if (TINYIB_TRIPSEED == '' || TINYIB_ADMINPASS == '') {
fancyDie('TINYIB_TRIPSEED and TINYIB_ADMINPASS must be configured');
2009-09-20 02:53:15 +00:00
}
$redirect = true;
// Check if the request is to make a post
2011-09-06 06:53:37 +00:00
if (isset($_POST['message']) || isset($_POST['file'])) {
list($loggedin, $isadmin) = manageCheckLogIn();
$rawpost = isRawPost();
if (!$loggedin) {
checkBanned();
2011-01-07 09:50:03 +00:00
checkMessageSize();
checkFlood();
2009-09-20 02:53:15 +00:00
}
2014-06-24 19:51:22 +00:00
$post = newPost(setParent());
2009-09-20 02:53:15 +00:00
$post['ip'] = $_SERVER['REMOTE_ADDR'];
2014-06-24 19:51:22 +00:00
2011-09-06 06:53:37 +00:00
list($post['name'], $post['tripcode']) = nameAndTripcode($_POST['name']);
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
$post['name'] = cleanString(substr($post['name'], 0, 75));
2011-09-06 06:53:37 +00:00
$post['email'] = cleanString(str_replace('"', '&quot;', substr($_POST['email'], 0, 75)));
$post['subject'] = cleanString(substr($_POST['subject'], 0, 75));
if ($rawpost) {
$rawposttext = ($isadmin) ? ' <span style="color: red;">## Admin</span>' : ' <span style="color: purple;">## Mod</span>';
2011-09-06 06:53:37 +00:00
$post['message'] = $_POST['message']; // Treat message as raw HTML
} else {
$rawposttext = '';
2011-09-06 06:53:37 +00:00
$post['message'] = str_replace("\n", '<br>', colorQuote(postLink(cleanString(rtrim($_POST['message'])))));
}
2010-12-31 23:24:55 +00:00
$post['password'] = ($_POST['password'] != '') ? md5(md5($_POST['password'])) : '';
$post['nameblock'] = nameBlock($post['name'], $post['tripcode'], $post['email'], time(), $rawposttext);
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
if (isset($_FILES['file'])) {
if ($_FILES['file']['name'] != "") {
validateFileUpload();
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
if (!is_file($_FILES['file']['tmp_name']) || !is_readable($_FILES['file']['tmp_name'])) {
fancyDie("File transfer failure. Please retry the submission.");
}
2014-06-24 19:51:22 +00:00
if ((TINYIB_MAXKB > 0) && (filesize($_FILES['file']['tmp_name']) > (TINYIB_MAXKB * 1024))) {
fancyDie("That file is larger than " . TINYIB_MAXKBDESC . ".");
}
2014-06-24 19:51:22 +00:00
$post['file_original'] = trim(htmlentities(substr($_FILES['file']['name'], 0, 50), ENT_QUOTES));
2009-09-20 02:53:15 +00:00
$post['file_hex'] = md5_file($_FILES['file']['tmp_name']);
$post['file_size'] = $_FILES['file']['size'];
$post['file_size_formatted'] = convertBytes($post['file_size']);
2014-06-24 19:51:22 +00:00
$file_type = strtolower(preg_replace('/.*(\..+)/', '\1', $_FILES['file']['name']));
if ($file_type == '.jpeg') {
$file_type = '.jpg';
}
if ($file_type == '.weba') {
$file_type = '.webm';
}
$file_name = time() . substr(microtime(), 2, 3);
2009-09-20 02:53:15 +00:00
$post['file'] = $file_name . $file_type;
2014-06-24 19:51:22 +00:00
$post['thumb'] = $file_name . "s" . ($file_type == '.webm' ? '.jpg' : $file_type);
2009-09-20 02:53:15 +00:00
$file_location = "src/" . $post['file'];
$thumb_location = "thumb/" . $post['thumb'];
2014-06-24 19:51:22 +00:00
checkDuplicateFile($post['file_hex']);
2009-09-20 02:53:15 +00:00
if (!move_uploaded_file($_FILES['file']['tmp_name'], $file_location)) {
fancyDie("Could not copy uploaded file.");
}
2014-06-24 19:51:22 +00:00
if ($file_type == '.webm') {
$file_mime_output = shell_exec('file --mime-type ' . $file_location);
$file_mime_split = explode(' ', $file_mime_output);
$file_mime = strtolower(trim(array_pop($file_mime_split)));
} else {
if (!@getimagesize($file_location)) {
@unlink($file_location);
fancyDie("Failed to read the size of the uploaded file. Please retry the submission.");
}
$file_info = getimagesize($file_location);
$file_mime = $file_info['mime'];
}
if (!($file_mime == "image/jpeg" || $file_mime == "image/gif" || $file_mime == "image/png" || (TINYIB_WEBM && ($file_mime == "video/webm" || $file_mime == "audio/webm")))) {
@unlink($file_location);
fancyDie("Only " . (TINYIB_WEBM ? "GIF, JPG, PNG, and WEBM" : "GIF, JPG, and PNG") . " files are allowed.");
}
2009-09-20 02:53:15 +00:00
if ($_FILES['file']['size'] != filesize($file_location)) {
2014-06-24 19:51:22 +00:00
@unlink($file_location);
2009-09-20 02:53:15 +00:00
fancyDie("File transfer failure. Please go back and try again.");
}
2014-06-24 19:51:22 +00:00
if ($file_mime == "audio/webm" || $file_mime == "video/webm") {
$post['image_width'] = intval(shell_exec('mediainfo --Inform="Video;%Width%" ' . $file_location));
$post['image_height'] = intval(shell_exec('mediainfo --Inform="Video;%Height%" ' . $file_location));
if ($post['image_width'] <= 0 || $post['image_height'] <= 0) {
$post['image_width'] = 0;
$post['image_height'] = 0;
$file_location_old = $file_location;
$file_location = substr($file_location, 0, -1) . 'a'; // replace webm with weba
rename($file_location_old, $file_location);
$post['file'] = substr($post['file'], 0, -1) . 'a'; // replace webm with weba
}
if ($file_mime == "video/webm") {
list($thumb_maxwidth, $thumb_maxheight) = thumbnailDimensions($post);
shell_exec("ffmpegthumbnailer -s " . max($thumb_maxwidth, $thumb_maxheight) . " -i $file_location -o $thumb_location") . '!';
$thumb_info = getimagesize($thumb_location);
$post['thumb_width'] = $thumb_info[0];
$post['thumb_height'] = $thumb_info[1];
if ($post['thumb_width'] <= 0 || $post['thumb_height'] <= 0) {
@unlink($file_location);
@unlink($thumb_location);
fancyDie("Sorry, your video appears to be corrupt.");
}
if (file_exists('video_overlay.png')) {
$thumbnail = imagecreatefromjpeg($thumb_location);
list($width, $height, $type, $attr) = getimagesize($thumb_location);
$overlay_play = imagecreatefrompng('video_overlay.png');
imagealphablending($overlay_play, false);
imagesavealpha($overlay_play, true);
list($overlay_width, $overlay_height, $overlay_type, $overlay_attr) = getimagesize('video_overlay.png');
$new_thumbnail = imagecreatetruecolor($width, $height);
imagecopyresampled($new_thumbnail, $thumbnail, 0, 0, 0, 0, $width, $height, $width, $height);
imagecopyresampled($new_thumbnail, $overlay_play, ($width / 2) - ($overlay_width / 2), ($height / 2) - ($overlay_height / 2), 0, 0, $overlay_width, $overlay_width, $overlay_width, $overlay_height);
imagejpeg($new_thumbnail, $thumb_location);
$thumb_info = getimagesize($thumb_location);
$post['thumb_width'] = $thumb_info[0];
$post['thumb_height'] = $thumb_info[1];
}
}
$duration = intval(shell_exec('mediainfo --Inform="' . ($file_mime == 'video/webm' ? 'Video' : 'Audio') . ';%Duration%" ' . $file_location));
$mins = floor(round($duration / 1000) / 60);
$secs = str_pad(floor(round($duration / 1000) % 60), 2, "0", STR_PAD_LEFT);
$post['file_original'] = "$mins:$secs" . ($post['file_original'] != '' ? (', ' . $post['file_original']) : '');
} else {
$file_info = getimagesize($file_location);
$post['image_width'] = $file_info[0];
$post['image_height'] = $file_info[1];
list($thumb_maxwidth, $thumb_maxheight) = thumbnailDimensions($post);
if (!createThumbnail($file_location, $thumb_location, $thumb_maxwidth, $thumb_maxheight)) {
fancyDie("Could not create thumbnail.");
}
$thumb_info = getimagesize($thumb_location);
$post['thumb_width'] = $thumb_info[0];
$post['thumb_height'] = $thumb_info[1];
}
}
2009-09-20 02:53:15 +00:00
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
if ($post['file'] == '') { // No file uploaded
if ($post['parent'] == TINYIB_NEWTHREAD) {
2009-09-20 02:53:15 +00:00
fancyDie("An image is required to start a thread.");
}
if (str_replace('<br>', '', $post['message']) == "") {
fancyDie("Please enter a message and/or upload an image to make a reply.");
}
} else {
echo $post['file_original'] . ' uploaded.<br>';
2009-09-20 02:53:15 +00:00
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
$post['id'] = insertPost($post);
2011-09-06 06:53:37 +00:00
if (strtolower($post['email']) == 'noko') {
$redirect = 'res/' . ($post['parent'] == TINYIB_NEWTHREAD ? $post['id'] : $post['parent']) . '.html#' . $post['id'];
2010-11-10 10:12:11 +00:00
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
trimThreads();
2014-06-24 19:51:22 +00:00
echo 'Updating thread...<br>';
if ($post['parent'] != TINYIB_NEWTHREAD) {
2009-09-20 02:53:15 +00:00
rebuildThread($post['parent']);
2014-06-24 19:51:22 +00:00
2011-09-06 06:53:37 +00:00
if (strtolower($post['email']) != 'sage') {
2013-04-16 03:07:31 +00:00
if (TINYIB_MAXREPLIES == 0 || numRepliesToThreadByID($post['parent']) <= TINYIB_MAXREPLIES) {
bumpThreadByID($post['parent']);
}
2009-09-20 02:53:15 +00:00
}
} else {
rebuildThread($post['id']);
}
2014-06-24 19:51:22 +00:00
echo 'Updating index...<br>';
2009-09-20 02:53:15 +00:00
rebuildIndexes();
// Check if the request is to delete a post and/or its associated image
} elseif (isset($_GET['delete']) && !isset($_GET['manage'])) {
2014-06-24 19:51:22 +00:00
if (!isset($_POST['delete'])) {
fancyDie('Tick the box next to a post and click "Delete" to delete it.');
}
2011-09-06 06:53:37 +00:00
$post = postByID($_POST['delete']);
if ($post) {
list($loggedin, $isadmin) = manageCheckLogIn();
2014-06-24 19:51:22 +00:00
2011-09-06 06:53:37 +00:00
if ($loggedin && $_POST['password'] == '') {
// Redirect to post moderation page
echo '--&gt; --&gt; --&gt;<meta http-equiv="refresh" content="0;url=' . basename($_SERVER['PHP_SELF']) . '?manage&moderate=' . $_POST['delete'] . '">';
} elseif ($post['password'] != '' && md5(md5($_POST['password'])) == $post['password']) {
deletePostByID($post['id']);
2014-06-24 19:51:22 +00:00
if ($post['parent'] == TINYIB_NEWTHREAD) {
threadUpdated($post['id']);
} else {
threadUpdated($post['parent']);
}
2011-09-06 06:53:37 +00:00
fancyDie('Post deleted.');
2009-09-20 02:53:15 +00:00
} else {
2011-09-06 06:53:37 +00:00
fancyDie('Invalid password.');
2009-09-20 02:53:15 +00:00
}
} else {
2011-09-06 06:53:37 +00:00
fancyDie('Sorry, an invalid post identifier was sent. Please go back, refresh the page, and try again.');
2009-09-20 02:53:15 +00:00
}
2011-09-06 06:53:37 +00:00
2009-09-20 02:53:15 +00:00
$redirect = false;
// Check if the request is to access the management area
2011-09-06 06:53:37 +00:00
} elseif (isset($_GET['manage'])) {
2014-06-24 19:51:22 +00:00
$text = '';
$onload = '';
$navbar = '&nbsp;';
$redirect = false;
$loggedin = false;
$isadmin = false;
2009-09-20 02:53:15 +00:00
$returnlink = basename($_SERVER['PHP_SELF']);
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
list($loggedin, $isadmin) = manageCheckLogIn();
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
if ($loggedin) {
if ($isadmin) {
2011-09-06 06:53:37 +00:00
if (isset($_GET['rebuildall'])) {
2009-09-20 02:53:15 +00:00
$allthreads = allThreads();
foreach ($allthreads as $thread) {
2011-09-06 06:53:37 +00:00
rebuildThread($thread['id']);
2009-09-20 02:53:15 +00:00
}
rebuildIndexes();
$text .= manageInfo('Rebuilt board.');
2011-09-06 06:53:37 +00:00
} elseif (isset($_GET['bans'])) {
2009-09-20 02:53:15 +00:00
clearExpiredBans();
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
if (isset($_POST['ip'])) {
if ($_POST['ip'] != '') {
$banexists = banByIP($_POST['ip']);
if ($banexists) {
fancyDie('Sorry, there is already a ban on record for that IP address.');
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
$ban = array();
$ban['ip'] = $_POST['ip'];
$ban['expire'] = ($_POST['expire'] > 0) ? (time() + $_POST['expire']) : 0;
$ban['reason'] = $_POST['reason'];
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
insertBan($ban);
$text .= manageInfo('Ban record added for ' . $ban['ip']);
2009-09-20 02:53:15 +00:00
}
} elseif (isset($_GET['lift'])) {
$ban = banByID($_GET['lift']);
if ($ban) {
deleteBanByID($_GET['lift']);
$text .= manageInfo('Ban record lifted for ' . $ban['ip']);
2009-09-20 02:53:15 +00:00
}
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
$onload = manageOnLoad('bans');
$text .= manageBanForm();
$text .= manageBansTable();
} else if (isset($_GET['update'])) {
if (is_dir('.git')) {
$git_output = shell_exec('git pull 2>&1');
$text .= '<blockquote class="reply" style="padding: 7px;font-size: 1.25em;">
2013-04-16 02:09:18 +00:00
<pre style="margin: 0px;padding: 0px;">Attempting update...' . "\n\n" . $git_output . '</pre>
</blockquote>
<p><b>Note:</b> If TinyIB updates and you have made custom modifications, <a href="https://github.com/tslocum/TinyIB/commits/master">review the changes</a> which have been merged into your installation.
Ensure that your modifications do not interfere with any new/modified files.
See the <a href="https://github.com/tslocum/TinyIB#readme">README</a> for more information.</p>';
} else {
$text .= '<p><b>TinyIB was not installed via Git.</b></p>
<p>If you installed TinyIB without Git, you must <a href="https://github.com/tslocum/TinyIB">update manually</a>. If you did install with Git, ensure the script has read and write access to the <b>.git</b> folder.</p>';
}
2009-09-20 02:53:15 +00:00
}
}
2014-06-24 19:51:22 +00:00
2011-09-06 06:53:37 +00:00
if (isset($_GET['delete'])) {
2009-09-20 02:53:15 +00:00
$post = postByID($_GET['delete']);
if ($post) {
deletePostByID($post['id']);
rebuildIndexes();
if ($post['parent'] != TINYIB_NEWTHREAD) {
2009-09-20 02:53:15 +00:00
rebuildThread($post['parent']);
}
$text .= manageInfo('Post No.' . $post['id'] . ' deleted.');
2009-09-20 02:53:15 +00:00
} else {
fancyDie("Sorry, there doesn't appear to be a post with that ID.");
}
2011-09-06 06:53:37 +00:00
} elseif (isset($_GET['moderate'])) {
2009-09-20 02:53:15 +00:00
if ($_GET['moderate'] > 0) {
$post = postByID($_GET['moderate']);
if ($post) {
$text .= manageModeratePost($post);
} else {
fancyDie("Sorry, there doesn't appear to be a post with that ID.");
}
} else {
$onload = manageOnLoad('moderate');
$text .= manageModeratePostForm();
}
} elseif (isset($_GET["rawpost"])) {
$onload = manageOnLoad("rawpost");
$text .= manageRawPostForm();
2009-09-20 02:53:15 +00:00
} elseif (isset($_GET["logout"])) {
$_SESSION['tinyib'] = '';
session_destroy();
die('--&gt; --&gt; --&gt;<meta http-equiv="refresh" content="0;url=' . $returnlink . '?manage">');
}
if ($text == '') {
$text = manageStatus();
}
2009-09-20 02:53:15 +00:00
} else {
$onload = manageOnLoad('login');
$text .= manageLogInForm();
}
echo managePage($text, $onload);
2011-09-06 06:53:37 +00:00
} elseif (!file_exists('index.html') || countThreads() == 0) {
2009-09-20 02:53:15 +00:00
rebuildIndexes();
}
if ($redirect) {
2010-12-31 23:24:55 +00:00
echo '--&gt; --&gt; --&gt;<meta http-equiv="refresh" content="0;url=' . (is_string($redirect) ? $redirect : 'index.html') . '">';
2009-09-20 02:53:15 +00:00
}