leftypol/inc/functions.php

1625 lines
54 KiB
PHP
Raw Normal View History

2010-11-02 11:54:50 +00:00
<?php
2011-04-13 12:21:07 +00:00
if($_SERVER['SCRIPT_FILENAME'] == str_replace('\\', '/', __FILE__)) {
// You cannot request this file directly.
header('Location: ../', true, 302);
exit;
}
2011-02-16 05:21:14 +00:00
loadConfig();
function loadConfig() {
2011-02-17 09:58:34 +00:00
global $board, $config, $__ip;
2011-02-16 05:21:14 +00:00
require 'config.php';
if (file_exists('inc/instance-config.php')) {
require 'instance-config.php';
}
2011-02-16 08:41:34 +00:00
if(isset($board['dir']) && file_exists($board['dir'] . '/config.php')) {
require $board['dir'] . '/config.php';
}
2011-02-16 05:21:14 +00:00
if(!isset($config['url_stylesheet']))
$config['url_stylesheet'] = $config['root'] . 'style.css';
if(!isset($config['url_javascript']))
2011-03-26 15:36:32 +00:00
$config['url_javascript'] = $config['root'] . 'main.js';
2011-02-16 05:24:23 +00:00
if(!isset($config['post_url']))
2011-02-16 05:26:02 +00:00
$config['post_url'] = $config['root'] . $config['file_post'];
2011-02-16 05:21:14 +00:00
2011-02-16 05:24:23 +00:00
if(!isset($config['url_match']))
$config['url_match'] = '/^' .
(preg_match($config['url_regex'], $config['root']) ? '' :
2011-04-22 14:48:07 +00:00
(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']?'https':'http') .
2011-02-16 05:24:23 +00:00
':\/\/'.$_SERVER['HTTP_HOST']) .
preg_quote($config['root'], '/') .
'(' .
str_replace('%s', '\w{1,8}', preg_quote($config['board_path'], '/')) .
'|' .
str_replace('%s', '\w{1,8}', preg_quote($config['board_path'], '/')) .
preg_quote($config['file_index'], '/') .
'|' .
str_replace('%s', '\w{1,8}', preg_quote($config['board_path'], '/')) .
str_replace('%d', '\d+', preg_quote($config['file_page'], '/')) .
'|' .
preg_quote($config['file_mod'], '/') .
'\?\/.+' .
')$/i';
2011-02-16 05:21:14 +00:00
if(!isset($config['cookies']['path']))
$config['cookies']['path'] = $config['root'];
2011-02-16 05:24:23 +00:00
if(!isset($config['dir']['static']))
2011-02-16 05:26:02 +00:00
$config['dir']['static'] = $config['root'] . 'static/';
2011-02-16 05:21:14 +00:00
2011-02-16 05:24:23 +00:00
if(!isset($config['image_sticky']))
2011-02-16 05:26:02 +00:00
$config['image_sticky'] = $config['dir']['static'] . 'sticky.gif';
2011-02-16 05:24:23 +00:00
if(!isset($config['image_locked']))
2011-02-16 05:26:02 +00:00
$config['image_locked'] = $config['dir']['static'] . 'locked.gif';
2011-02-16 05:24:23 +00:00
if(!isset($config['image_deleted']))
2011-02-16 05:26:02 +00:00
$config['image_deleted'] = $config['dir']['static'] . 'deleted.png';
2011-02-16 05:24:23 +00:00
if(!isset($config['image_zip']))
2011-02-16 05:26:02 +00:00
$config['image_zip'] = $config['dir']['static'] . 'zip.png';
2011-02-16 08:29:56 +00:00
if(!isset($config['uri_thumb']))
$config['uri_thumb'] = $config['root'] . $board['dir'] . $config['dir']['thumb'];
else
$config['uri_thumb'] = sprintf($config['uri_thumb'], $board['dir']);
if(!isset($config['uri_img']))
$config['uri_img'] = $config['root'] . $board['dir'] . $config['dir']['img'];
else
$config['uri_img'] = sprintf($config['uri_img'], $board['dir']);
2011-03-26 15:36:32 +00:00
if(!isset($config['uri_stylesheets']))
$config['uri_stylesheets'] = $config['root'];
2011-02-16 08:29:56 +00:00
if($config['root_file']) {
chdir($config['root_file']);
}
if($config['verbose_errors']) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
2011-02-17 09:58:34 +00:00
2011-03-21 08:48:43 +00:00
if($config['ipv6_ipv4'] && isset($_SERVER['REMOTE_ADDR'])) {
2011-02-17 09:59:36 +00:00
// Keep the original address to properly comply with other board configurations
2011-02-17 09:58:34 +00:00
if(!isset($__ip))
$__ip = $_SERVER['REMOTE_ADDR'];
// ::ffff:0.0.0.0
if(preg_match('/^\:\:(ffff\:)?(\d+\.\d+\.\d+\.\d+)$/', $__ip, $m))
2011-03-01 10:26:04 +00:00
$_SERVER['REMOTE_ADDR'] = $m[2];
2011-02-17 09:58:34 +00:00
}
if($config['memcached']['enabled'])
memcached_open();
// Test custom fields and stuff
if(isset($config['fields'])) {
$error_func = function_exists('error') ? 'error' : 'basic_error_function_because_the_other_isnt_loaded_yet';
foreach($config['fields'] as $index => $field) {
if(!is_array($field))
$error_func(sprintf("Error parsing custom fields. Field at index %d is not an array!", $index));
if(!isset($field['name']))
$error_func(sprintf("Error parsing custom fields. Field at index %d doesn't have a name!", $index));
}
}
}
function basic_error_function_because_the_other_isnt_loaded_yet($message) {
if(function_exists('sql_close')) sql_close();
// Yes, this is horrible.
die('<!DOCTYPE html><html><head><title>Error</title>' .
'<style type="text/css">' .
'body{text-align:center;font-family:arial, helvetica, sans-serif;font-size:10pt;}' .
'p{padding:0;margin:20px 0;}' .
'p.c{font-size:11px;}' .
'</style></head>' .
'<body><h2>Error</h2>' . $message . '<hr/>' .
'<p class="c">This alternative error page is being displayed because the other couldn\'t be found or hasn\'t loaded yet.</p></body></html>');
}
2011-04-22 14:45:32 +00:00
function fatal_error_handler() {
if($error = error_get_last()) {
2011-04-22 14:55:51 +00:00
if($error['type'] == E_ERROR) {
if(function_exists('error')) {
error('Caught fatal error: ' . $error['message'] . ' in <strong>' . $error['file'] . '</strong> on line ' . $error['line']);
} else {
header('Content-Type: text/plain');
echo 'Caught fatal error: ' . $error['message'] . ' in ' . $error['file'] . ' on line ' . $error['line'];
}
}
2011-04-22 14:45:32 +00:00
}
}
2011-04-22 14:55:51 +00:00
register_shutdown_function('fatal_error_handler');
2011-04-22 14:45:32 +00:00
// Memcached
function memcached_open() {
global $memcached, $config;
if($memcached) return;
$memcached = new Memcached();
$memcached->addServers($config['memcached']['servers']);
2011-02-16 05:21:14 +00:00
}
2011-04-13 13:47:47 +00:00
function loadThemeConfig($_theme) {
global $config;
2011-04-14 12:12:56 +00:00
if(!file_exists($config['dir']['homepage'] . '/' . $_theme . '/info.php'))
2011-04-14 07:10:59 +00:00
return false;
2011-04-13 13:47:47 +00:00
// Load theme information into $theme
2011-04-14 12:12:56 +00:00
include $config['dir']['homepage'] . '/' . $_theme . '/info.php';
2011-04-13 13:47:47 +00:00
return $theme;
}
2011-04-14 10:43:34 +00:00
function rebuildTheme($action) {
2011-04-14 18:59:04 +00:00
global $config, $_theme;
2011-04-14 12:12:56 +00:00
2011-04-14 07:03:12 +00:00
$query = query("SELECT `value` AS `theme` FROM `theme_settings` WHERE `name` = 'theme'") or error(db_error());
if($theme = $query->fetch()) {
// A theme is installed
2011-04-14 12:12:56 +00:00
$_theme = $theme['theme'];
$theme = loadThemeConfig($_theme);
2011-04-14 07:03:12 +00:00
2011-04-14 12:12:56 +00:00
if(file_exists($config['dir']['homepage'] . '/' . $_theme . '/theme.php')) {
include $config['dir']['homepage'] . '/' . $_theme . '/theme.php';
$theme['build_function']($action, themeSettings());
}
2011-04-14 07:03:12 +00:00
}
}
2011-04-13 13:47:47 +00:00
function themeSettings() {
$query = query("SELECT * FROM `theme_settings`") or error(db_error());
$settings = Array();
while($s = $query->fetch()) {
$settings[$s['name']] = $s['value'];
}
return $settings;
}
function sprintf3($str, $vars, $delim = '%') {
$replaces = array();
foreach($vars as $k => $v) {
$replaces[$delim . $k . $delim] = $v;
}
return str_replace(array_keys($replaces),
array_values($replaces), $str);
}
2010-11-30 09:24:09 +00:00
function setupBoard($array) {
2011-02-12 06:25:15 +00:00
global $board, $config;
2010-11-30 09:24:09 +00:00
$board = Array(
'id' => $array['id'],
'uri' => $array['uri'],
'name' => $array['title'],
'title' => $array['subtitle']);
2011-02-12 06:25:15 +00:00
$board['dir'] = sprintf($config['board_path'], $board['uri']);
$board['url'] = sprintf($config['board_abbreviation'], $board['uri']);
2010-11-30 09:24:09 +00:00
2011-02-16 08:41:34 +00:00
loadConfig();
2010-11-30 09:24:09 +00:00
if(!file_exists($board['dir'])) mkdir($board['dir'], 0777);
2011-02-12 06:25:15 +00:00
if(!file_exists($board['dir'] . $config['dir']['img'])) @mkdir($board['dir'] . $config['dir']['img'], 0777) or error("Couldn't create " . $config['dir']['img'] . ". Check permissions.", true);
if(!file_exists($board['dir'] . $config['dir']['thumb'])) @mkdir($board['dir'] . $config['dir']['thumb'], 0777) or error("Couldn't create " . $config['dir']['thumb'] . ". Check permissions.", true);
if(!file_exists($board['dir'] . $config['dir']['res'])) @mkdir($board['dir'] . $config['dir']['res'], 0777) or error("Couldn't create " . $config['dir']['res'] . ". Check permissions.", true);
2010-11-30 09:24:09 +00:00
}
2010-11-30 09:38:36 +00:00
function openBoard($uri) {
2010-11-30 12:12:54 +00:00
2010-12-17 14:18:03 +00:00
$query = prepare("SELECT * FROM `boards` WHERE `uri` = :uri LIMIT 1");
$query->bindValue(':uri', $uri);
$query->execute() or error(db_error($query));
if($board = $query->fetch()) {
setupBoard($board);
2010-11-30 09:38:36 +00:00
return true;
} else return false;
}
2010-12-02 07:15:50 +00:00
function listBoards() {
2011-01-20 08:24:23 +00:00
$query = query("SELECT * FROM `boards` ORDER BY `uri`") or error(db_error());
2010-12-17 14:18:03 +00:00
$boards = $query->fetchAll();
2010-12-02 07:15:50 +00:00
return $boards;
}
2011-01-18 06:11:28 +00:00
function checkFlood($post) {
2011-02-12 06:25:15 +00:00
global $board, $config;
2011-01-18 06:11:28 +00:00
$query = prepare(sprintf("SELECT * FROM `posts_%s` WHERE (`ip` = :ip AND `time` >= :floodtime) OR (`ip` = :ip AND `body` = :body AND `time` >= :floodsameiptime) OR (`body` = :body AND `time` >= :floodsametime) LIMIT 1", $board['uri']));
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
$query->bindValue(':body', $post['body'], PDO::PARAM_INT);
2011-02-12 06:25:15 +00:00
$query->bindValue(':floodtime', time()-$config['flood_time'], PDO::PARAM_INT);
$query->bindValue(':floodsameiptime', time()-$config['flood_time_ip'], PDO::PARAM_INT);
$query->bindValue(':floodsametime', time()-$config['flood_time_same'], PDO::PARAM_INT);
2011-01-18 06:11:28 +00:00
$query->execute() or error(db_error($query));
return (bool)$query->fetch();
}
2011-01-01 14:37:52 +00:00
function until($timestamp) {
$difference = $timestamp - time();
if($difference < 60) {
return $difference . ' second' . ($difference != 1 ? 's' : '');
} elseif($difference < 60*60) {
return ($num = round($difference/(60))) . ' minute' . ($num != 1 ? 's' : '');
} elseif($difference < 60*60*24) {
return ($num = round($difference/(60*60))) . ' hour' . ($num != 1 ? 's' : '');
} elseif($difference < 60*60*24*7) {
return ($num = round($difference/(60*60*24))) . ' day' . ($num != 1 ? 's' : '');
} elseif($difference < 60*60*24*365) {
2011-01-01 14:37:52 +00:00
return ($num = round($difference/(60*60*24*7))) . ' week' . ($num != 1 ? 's' : '');
2011-03-01 10:26:04 +00:00
} else {
return ($num = round($difference/(60*60*24*365))) . ' year' . ($num != 1 ? 's' : '');
}
}
function ago($timestamp) {
$difference = time() - $timestamp;
if($difference < 60) {
return $difference . ' second' . ($difference != 1 ? 's' : '');
} elseif($difference < 60*60) {
return ($num = round($difference/(60))) . ' minute' . ($num != 1 ? 's' : '');
} elseif($difference < 60*60*24) {
return ($num = round($difference/(60*60))) . ' hour' . ($num != 1 ? 's' : '');
} elseif($difference < 60*60*24*7) {
return ($num = round($difference/(60*60*24))) . ' day' . ($num != 1 ? 's' : '');
} elseif($difference < 60*60*24*365) {
return ($num = round($difference/(60*60*24*7))) . ' week' . ($num != 1 ? 's' : '');
2011-01-01 14:37:52 +00:00
} else {
return ($num = round($difference/(60*60*24*365))) . ' year' . ($num != 1 ? 's' : '');
2011-01-01 14:37:52 +00:00
}
}
function formatDate($timestamp) {
return date('jS F, Y', $timestamp);
}
function displayBan($ban) {
2011-02-12 06:25:15 +00:00
global $config;
$body = '<div class="ban">
2011-01-01 14:37:52 +00:00
<h2>You are banned! ;_;</h2>
<p>You have been banned ' .
($ban['reason'] ? 'for the following reason:' : 'for an unspecified reason.') .
'</p>' .
($ban['reason'] ?
'<p class="reason">' .
$ban['reason'] .
'</p>'
: '') .
'<p>Your ban was filed on <strong>' .
formatDate($ban['set']) .
2011-01-03 04:00:14 +00:00
'</strong>, and <span id="expires">' .
2011-01-01 14:37:52 +00:00
($ban['expires'] ?
2011-01-03 04:00:14 +00:00
'expires <span id="countdown">' . until($ban['expires']) . '</span> from now, which is on <strong>' .
2011-01-01 14:37:52 +00:00
formatDate($ban['expires']) .
2011-01-03 04:00:14 +00:00
'</strong>
<script>
// return date("jS F, Y", $timestamp);
var secondsLeft = ' . ($ban['expires'] - time()) . '
var end = new Date().getTime() + secondsLeft*1000;
function updateExpiresTime() {
2011-01-03 04:00:14 +00:00
countdown.firstChild.nodeValue = until(end);
}
function until(end) {
var now = new Date().getTime();
var diff = Math.round((end - now) / 1000); // in seconds
2011-01-03 04:00:14 +00:00
if (diff < 0) {
document.getElementById("expires").innerHTML = "has since expired. Refresh the page to continue.";
//location.reload(true);
clearInterval(int);
return "";
} else if (diff < 60) {
return diff + " second" + (diff == 1 ? "" : "s");
} else if (diff < 60*60) {
return (num = Math.round(diff/(60))) + " minute" + (num == 1 ? "" : "s");
} else if (diff < 60*60*24) {
return (num = Math.round(diff/(60*60))) + " hour" + (num == 1 ? "" : "s");
} else if (diff < 60*60*24*7) {
return (num = Math.round(diff/(60*60*24))) + " day" + (num == 1 ? "" : "s");
} else if (diff < 60*60*24*365) {
return (num = Math.round(diff/(60*60*24*7))) + " week" + (num == 1 ? "" : "s");
} else {
return (num = Math.round(diff/(60*60*24*365))) + " year" + (num == 1 ? "" : "s");
}
}
2011-01-03 04:00:14 +00:00
var countdown = document.getElementById("countdown");
updateExpiresTime();
2011-01-03 04:00:14 +00:00
var int = setInterval(updateExpiresTime, 1000);
</script>'
2011-01-03 04:00:14 +00:00
: '<em>will not expire</em>.' ) .
'</span></p>
2011-01-01 14:37:52 +00:00
<p>Your IP address is <strong>' . $_SERVER['REMOTE_ADDR'] . '</strong>.</p>
</div>';
// Show banned page and exit
die(Element('page.html', Array(
'config' => $config,
'title' => 'Banned',
'subtitle' => 'You are banned!',
'body' => $body
)
));
}
function checkBan() {
global $config, $memcached;
if(!isset($_SERVER['REMOTE_ADDR'])) {
// Server misconfiguration
return;
}
if($config['memcached']['enabled']) {
// Cached ban?
if($ban = $memcached->get("ban_${_SERVER['REMOTE_ADDR']}")) {
displayBan($ban);
}
}
$query = prepare("SELECT * FROM `bans` WHERE `ip` = :ip ORDER BY `expires` IS NULL DESC, `expires` DESC, `expires` DESC LIMIT 1");
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
$query->execute() or error(db_error($query));
if($query->rowCount() < 1 && $config['ban_range']) {
$query = prepare("SELECT * FROM `bans` WHERE :ip REGEXP CONCAT('^', REPLACE(REPLACE(`ip`, '.', '\\.'), '*', '[0-9]*'), '$') ORDER BY `expires` IS NULL DESC, `expires` DESC LIMIT 1");
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
$query->execute() or error(db_error($query));
}
if($ban = $query->fetch()) {
if($ban['expires'] && $ban['expires'] < time()) {
// Ban expired
$query = prepare("DELETE FROM `bans` WHERE `ip` = :ip AND `expires` = :expires LIMIT 1");
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
$query->bindValue(':expires', $ban['expires'], PDO::PARAM_INT);
$query->execute() or error(db_error($query));
if($config['ban_range']) {
$query = prepare("DELETE FROM `bans` WHERE :ip REGEXP CONCAT('^', REPLACE(REPLACE(`ip`, '.', '\\.'), '*', '[0-9a-f]*'), '$') AND `expires` = :expires LIMIT 1");
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
$query->bindValue(':expires', $ban['expires'], PDO::PARAM_INT);
$query->execute() or error(db_error($query));
}
return;
}
2011-01-01 14:37:52 +00:00
if($config['memcached']['enabled'])
$memcached->set("ban_${_SERVER['REMOTE_ADDR']}", $ban, $ban['expires']);
displayBan($ban);
2011-01-01 14:37:52 +00:00
}
}
function threadLocked($id) {
global $board;
$query = prepare(sprintf("SELECT `locked` FROM `posts_%s` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri']));
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute() or error(db_error());
if(!$post = $query->fetch()) {
// Non-existant, so it can't be locked...
return false;
}
return (bool) $post['locked'];
}
2010-11-30 12:12:54 +00:00
function threadExists($id) {
2010-12-17 14:18:03 +00:00
global $board;
$query = prepare(sprintf("SELECT 1 FROM `posts_%s` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri']));
2010-12-18 04:07:07 +00:00
$query->bindValue(':id', $id, PDO::PARAM_INT);
2010-12-17 14:18:03 +00:00
$query->execute() or error(db_error());
2010-11-30 12:12:54 +00:00
2010-12-17 14:18:03 +00:00
if($query->rowCount()) {
2010-11-30 12:12:54 +00:00
return true;
} else return false;
}
function post($post, $OP) {
2010-12-17 14:18:03 +00:00
global $pdo, $board;
$query = prepare(sprintf("INSERT INTO `posts_%s` VALUES ( NULL, :thread, :subject, :email, :name, :trip, :capcode, :body, :time, :time, :thumb, :thumbwidth, :thumbheight, :file, :width, :height, :filesize, :filename, :filehash, :password, :ip, :sticky, :locked)", $board['uri']));
2010-12-17 14:18:03 +00:00
// Basic stuff
$query->bindValue(':subject', $post['subject']);
$query->bindValue(':email', $post['email']);
$query->bindValue(':name', $post['name']);
$query->bindValue(':trip', $post['trip']);
$query->bindValue(':body', $post['body']);
$query->bindValue(':time', time(), PDO::PARAM_INT);
$query->bindValue(':password', $post['password']);
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
2011-01-02 10:15:59 +00:00
if($post['mod'] && $post['sticky']) {
$query->bindValue(':sticky', 1, PDO::PARAM_INT);
} else {
$query->bindValue(':sticky', 0, PDO::PARAM_INT);
}
2010-12-17 14:18:03 +00:00
2011-01-02 11:25:17 +00:00
if($post['mod'] && $post['locked']) {
$query->bindValue(':locked', 1, PDO::PARAM_INT);
} else {
$query->bindValue(':locked', 0, PDO::PARAM_INT);
}
if($post['mod'] && $post['capcode']) {
$query->bindValue(':capcode', $post['capcode'], PDO::PARAM_INT);
} else {
$query->bindValue(':capcode', NULL, PDO::PARAM_NULL);
}
if($OP) {
2010-12-17 14:18:03 +00:00
// No parent thread, image
$query->bindValue(':thread', null, PDO::PARAM_NULL);
} else {
2010-12-17 14:18:03 +00:00
$query->bindValue(':thread', $post['thread'], PDO::PARAM_INT);
}
2010-12-17 14:18:03 +00:00
if($post['has_file']) {
$query->bindValue(':thumb', $post['thumb']);
$query->bindValue(':thumbwidth', $post['thumbwidth'], PDO::PARAM_INT);
$query->bindValue(':thumbheight', $post['thumbheight'], PDO::PARAM_INT);
$query->bindValue(':file', $post['file']);
$query->bindValue(':width', $post['width'], PDO::PARAM_INT);
$query->bindValue(':height', $post['height'], PDO::PARAM_INT);
$query->bindValue(':filesize', $post['filesize'], PDO::PARAM_INT);
2011-01-01 09:45:18 +00:00
$query->bindValue(':filename', $post['filename']);
2011-04-13 10:57:41 +00:00
$query->bindValue(':filehash', $post['file']);
2010-12-17 14:18:03 +00:00
} else {
$query->bindValue(':thumb', null, PDO::PARAM_NULL);
$query->bindValue(':thumbwidth', null, PDO::PARAM_NULL);
$query->bindValue(':thumbheight', null, PDO::PARAM_NULL);
$query->bindValue(':file', null, PDO::PARAM_NULL);
$query->bindValue(':width', null, PDO::PARAM_NULL);
$query->bindValue(':height', null, PDO::PARAM_NULL);
$query->bindValue(':filesize', null, PDO::PARAM_NULL);
$query->bindValue(':filename', null, PDO::PARAM_NULL);
$query->bindValue(':filehash', null, PDO::PARAM_NULL);
}
$query->execute() or error(db_error($query));
return $pdo->lastInsertId();
}
function bumpThread($id) {
global $board;
$query = prepare(sprintf("UPDATE `posts_%s` SET `bump` = :time WHERE `id` = :id AND `thread` IS NULL", $board['uri']));
$query->bindValue(':time', time(), PDO::PARAM_INT);
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
}
2011-01-18 13:41:43 +00:00
2011-01-21 02:14:55 +00:00
// Remove file from post
function deleteFile($id, $remove_entirely_if_already=true) {
2011-02-12 06:25:15 +00:00
global $board, $config;
2011-01-21 02:14:55 +00: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) {
2011-02-12 06:25:15 +00:00
error($config['error']['invalidpost']);
2011-01-21 02:14:55 +00: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' && $remove_entirely_if_already) {
// Already deleted; remove file fully
$query->bindValue(':file', null, PDO::PARAM_NULL);
} else {
// Delete thumbnail
2011-02-12 06:25:15 +00:00
@unlink($board['dir'] . $config['dir']['thumb'] . $post['thumb']);
2011-01-21 02:14:55 +00:00
// Delete file
2011-02-12 06:25:15 +00:00
@unlink($board['dir'] . $config['dir']['img'] . $post['file']);
2011-01-21 02:14:55 +00:00
// 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));
buildThread($post['thread']);
}
2011-01-18 13:41:43 +00:00
// Delete a post (reply or thread)
function deletePost($id, $error_if_doesnt_exist=true) {
2011-02-12 06:25:15 +00:00
global $board, $config;
2011-01-18 13:41:43 +00:00
// Select post and replies (if thread) in one query
$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) {
if($error_if_doesnt_exist)
2011-02-12 06:25:15 +00:00
error($config['error']['invalidpost']);
else return false;
2011-01-18 13:41:43 +00:00
}
// Delete posts and maybe replies
while($post = $query->fetch()) {
if(!$post['thread']) {
// Delete thread HTML page
2011-02-12 06:25:15 +00:00
@unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['id']));
2011-01-18 13:41:43 +00:00
} elseif($query->rowCount() == 1) {
// Rebuild thread
$rebuild = $post['thread'];
}
if($post['thumb']) {
// Delete thumbnail
2011-02-12 06:25:15 +00:00
@unlink($board['dir'] . $config['dir']['thumb'] . $post['thumb']);
2011-01-18 13:41:43 +00:00
}
if($post['file']) {
// Delete file
2011-02-12 06:25:15 +00:00
@unlink($board['dir'] . $config['dir']['img'] . $post['file']);
2011-01-18 13:41:43 +00: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);
}
return true;
2011-01-18 13:41:43 +00:00
}
function clean() {
2011-02-12 06:25:15 +00:00
global $board, $config;
$offset = round($config['max_pages']*$config['threads_per_page']);
2011-01-18 13:41:43 +00:00
// I too wish there was an easier way of doing this...
$query = prepare(sprintf("SELECT `id` FROM `posts_%s` WHERE `thread` IS NULL ORDER BY `sticky` DESC, `bump` DESC LIMIT :offset, 9001", $board['uri']));
$query->bindValue(':offset', $offset, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
while($post = $query->fetch()) {
deletePost($post['id']);
}
}
function index($page, $mod=false) {
2011-02-12 06:25:15 +00:00
global $board, $config;
2010-11-04 14:39:02 +00:00
2010-11-02 10:57:33 +00:00
$body = '';
2011-02-12 06:25:15 +00:00
$offset = round($page*$config['threads_per_page']-$config['threads_per_page']);
2010-11-04 14:39:02 +00:00
2010-12-17 14:18:03 +00:00
$query = prepare(sprintf("SELECT * FROM `posts_%s` WHERE `thread` IS NULL ORDER BY `sticky` DESC, `bump` DESC LIMIT ?,?", $board['uri']));
$query->bindValue(1, $offset, PDO::PARAM_INT);
2011-02-12 06:25:15 +00:00
$query->bindValue(2, $config['threads_per_page'], PDO::PARAM_INT);
2010-12-17 14:18:03 +00:00
$query->execute() or error(db_error($query));
if($query->rowcount() < 1 && $page > 1) return false;
while($th = $query->fetch()) {
$thread = new Thread($th['id'], $th['subject'], $th['email'], $th['name'], $th['trip'], $th['capcode'], $th['body'], $th['time'], $th['thumb'], $th['thumbwidth'], $th['thumbheight'], $th['file'], $th['filewidth'], $th['fileheight'], $th['filesize'], $th['filename'], $th['ip'], $th['sticky'], $th['locked'], $mod ? '?/' : $config['root'], $mod);
2010-11-04 14:39:02 +00:00
$posts = prepare(sprintf("SELECT `id`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `time`, `thumb`, `thumbwidth`, `thumbheight`, `file`, `filewidth`, `fileheight`, `filesize`, `filename`,`ip` FROM `posts_%s` WHERE `thread` = ? ORDER BY `id` DESC LIMIT ?", $board['uri']));
2010-12-17 14:18:03 +00:00
$posts->bindValue(1, $th['id']);
2011-02-16 12:32:43 +00:00
$posts->bindValue(2, ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']), PDO::PARAM_INT);
2010-12-17 14:18:03 +00:00
$posts->execute() or error(db_error($posts));
2011-02-17 06:07:36 +00:00
$num_images = 0;
while($po = $posts->fetch()) {
if($po['file'])
$num_images++;
$thread->add(new Post($po['id'], $th['id'], $po['subject'], $po['email'], $po['name'], $po['trip'], $po['capcode'], $po['body'], $po['time'], $po['thumb'], $po['thumbwidth'], $po['thumbheight'], $po['file'], $po['filewidth'], $po['fileheight'], $po['filesize'], $po['filename'], $po['ip'], $mod ? '?/' : $config['root'], $mod));
2011-02-17 06:07:36 +00:00
}
2011-02-16 12:32:43 +00:00
if($posts->rowCount() == ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview'])) {
2011-02-17 06:07:36 +00:00
$count = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM `posts_%s` WHERE `thread` = :thread UNION ALL SELECT COUNT(`id`) FROM `posts_%s` WHERE `file` IS NOT NULL AND `thread` = :thread", $board['uri'], $board['uri']));
$count->bindValue(':thread', $th['id'], PDO::PARAM_INT);
2010-12-17 14:18:03 +00:00
$count->execute() or error(db_error($count));
2011-02-17 06:07:36 +00:00
$c = $count->fetch();
$thread->omitted = $c['num'] - ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']);
$c = $count->fetch();
$thread->omitted_images = $c['num'] - $num_images;
2010-11-02 10:57:33 +00:00
}
2010-12-17 14:18:03 +00:00
2010-11-02 10:57:33 +00:00
$thread->posts = array_reverse($thread->posts);
$body .= '<div id="thread_' . $thread->id . '">' . $thread->build(true) . '</div>';
2010-11-02 10:57:33 +00:00
}
2010-12-17 14:18:03 +00:00
2011-02-19 09:16:13 +00:00
return Array(
'board'=>$board,
'body'=>$body,
'post_url' => $config['post_url'],
'config' => $config,
2011-02-19 09:16:13 +00:00
'boardlist' => createBoardlist($mod)
);
2010-11-02 10:57:33 +00:00
}
2010-12-16 10:28:38 +00:00
2011-02-17 07:37:18 +00:00
function getPageButtons($pages, $mod=false) {
global $config, $board;
$btn = Array();
$root = ($mod ? '?/' : $config['root']) . $board['dir'];
foreach($pages as $num => $page) {
if(isset($page['selected'])) {
// Previous button
if($num == 0) {
// There is no previous page.
$btn['prev'] = 'Previous';
} else {
$loc = ($mod ? '?/' . $board['uri'] . '/' : '') .
($num == 1 ?
$config['file_index']
:
sprintf($config['file_page'], $num)
);
$btn['prev'] = '<form action="' . ($mod ? '' : $root . $loc) . '" method="get">' .
($mod ?
'<input type="hidden" name="status" value="301" />' .
'<input type="hidden" name="r" value="' . htmlentities($loc) . '" />'
:'') .
'<input type="submit" value="Previous" /></form>';
2011-02-17 07:37:18 +00:00
}
if($num == count($pages) - 1) {
// There is no next page.
$btn['next'] = 'Next';
} else {
$loc = ($mod ? '?/' . $board['uri'] . '/' : '') . sprintf($config['file_page'], $num + 2);
$btn['next'] = '<form action="' . ($mod ? '' : $root . $loc) . '" method="get">' .
($mod ?
'<input type="hidden" name="status" value="301" />' .
'<input type="hidden" name="r" value="' . htmlentities($loc) . '" />'
:'') .
'<input type="submit" value="Next" /></form>';
2011-02-17 07:37:18 +00:00
}
}
}
return $btn;
}
2010-12-16 10:28:38 +00:00
function getPages($mod=false) {
2011-02-12 06:25:15 +00:00
global $board, $config;
2010-12-16 10:28:38 +00:00
2010-12-17 14:18:03 +00:00
// Count threads
$query = query(sprintf("SELECT COUNT(`id`) as `num` FROM `posts_%s` WHERE `thread` IS NULL", $board['uri'])) or error(db_error());
$count = current($query->fetch());
2011-02-12 06:25:15 +00:00
$count = floor(($config['threads_per_page'] + $count - 1) / $config['threads_per_page']);
if($count < 1) $count = 1;