diff --git a/bbs.php b/bbs.php index e724ffd..3becf2a 100644 --- a/bbs.php +++ b/bbs.php @@ -1,25 +1,60 @@ - - +'); // uid that is able to delete anything + * define('UID_SALT', ''); + * define('MAX_POSTS', 50); // Max posts after which old posts will be auto deleted. Only existing posts count, not manually deleted ones. + * define('DISCORD_WEBHOOK', 'https://discord.com/api/webhooks//'); // A message will be sent to this webhook on post, for easier moderation + * + * // Database constants for PostgreSQL database + * $DB_HOST = 'localhost'; + * $DB_NAME = ''; + * $DB_USER = ''; + * $DB_PASSWORD = ''; + * + * You also need to make the following table: + * + * CEATE TABLE posts ( + * post_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + * post_time TIMESTAMPTZ NOT NULL DEFAULT NOW(), + * user_id VARCHAR(40), + * name VARCHAR(256), + * comment VARCHAR(2048), + * img VARCHAR(256) + * ); + */ + require_once('vars.php'); +?> @@ -36,6 +71,7 @@

'.bmp', @@ -56,13 +92,13 @@ if(!move_uploaded_file($img['tmp_name'], $target)) { // The new image file move failed, so delete the temporary file and return an error @unlink($img['tmp_name']); - return 'Sorry, there was a problem uploading your image.'; + return 'Unable to upload image, please contact the webmaster.'; } } } else { - // The new picture file is not valid, so delete the temporary file and return an error + // The image is not valid, so delete the temporary file and return an error @unlink($img['tmp_name']); - return "Your picture must be a PNG, GIF, JPEG, or BMP image file no greater than {MM_MAXFILESIZE >> 10} KiB."; + return 'Your image must be a PNG, GIF, JPEG, or BMP image file no greater than ' . (MAX_FILE_SIZE >> 10) . ' KiB.'; } } @@ -91,6 +127,7 @@ return ""; // Success, no error } + // Regex callback, makes >>quotes into links function quote_link($match) { $query = "SELECT post_id FROM posts WHERE post_id=$1"; $result = pg_query_params($query, [$match[1]]) or die('Query failed: ' . pg_last_error()); @@ -102,9 +139,8 @@ return "{$match[0]}"; } + // Prints the post list function show_posts() { - $show_delete = FALSE; - $query = 'SELECT post_id, user_id, name, comment, img, TO_CHAR(post_time, \'YYYY-MM-DD HH24:MI (TZ)\') AS post_time FROM posts'; $result = pg_query($query) or die('Query failed: ' . pg_last_error()); @@ -117,12 +153,14 @@ } } + // Print posts + $show_delete = FALSE; echo '
'; while ($row = pg_fetch_array($result)) { echo "
"; echo ''; - if((!empty($row['user_id']) && ($row['user_id'] == $_COOKIE['uid'])) || $_COOKIE['uid'] == ADMIN_ID) { + if((!empty($row['user_id']) && ($row['user_id'] == $_COOKIE['uid'])) || (!empty(ADMIN_ID) && ($_COOKIE['uid'] == ADMIN_ID))) { echo ' '; $show_delete = TRUE; } @@ -130,40 +168,50 @@ echo "#{$row['post_id']}"; echo ''; - if($row['img']){ + if(!empty($row['img'])){ echo ''; echo '' . $row['img'] . ''; echo ''; } - $comment = $row['comment']; - $comment = preg_replace('/^>[^>].*/m', '$0', $comment); - $comment = preg_replace('/https?:\/\/[^\s]+/m', '$0', $comment); - $comment = preg_replace_callback('/>>\s*(\d+)/', quote_link, $comment); - $comment = str_replace("\n", "
", $comment); - echo "

$comment

"; + // Process quotes, links, and newlines + if(!empty($row['comment'])) { + $comment = $row['comment']; + $comment = preg_replace('/^>[^>].*/m', '$0', $comment); + $comment = preg_replace('/https?:\/\/[^\s]+/m', '$0', $comment); + $comment = preg_replace_callback('/>>\s*(\d+)/', quote_link, $comment); + $comment = str_replace("\n", "
", $comment); + echo "

$comment

"; + } echo '
'; } + pg_free_result($result); + if($show_delete) echo '

'; echo '
'; } + // Removes a post from the database and its image function cleanup($id, $force = FALSE) { $query = "SELECT user_id, img FROM posts WHERE post_id=$1"; $result = pg_query_params($query, [$id]) or die('Query failed: ' . pg_last_error()); $row = pg_fetch_array($result); pg_free_result($result); - if($force || $row['user_id'] == $_COOKIE['uid'] || $_COOKIE['uid'] == ADMIN_ID) { + if($force || $row['user_id'] == $_COOKIE['uid'] || (!empty(ADMIN_ID) && ($_COOKIE['uid'] == ADMIN_ID))) { unlink(UPLOAD_PATH . $row['img']); $query = "DELETE FROM posts WHERE post_id=$1"; pg_query_params($query, [$id]) or die('Query failed: ' . pg_last_error()); } } + // Sends a webhook to Discord function webhook($name, $message, $img) { + if(empty(DISCORD_WEBHOOK)) + return; + $data = [ 'username' => $name, 'embeds' => [ @@ -191,11 +239,6 @@ die("Error: Sending webhook failed with status $status."); } - require_once('appvars.php'); - require_once('connectvars.php'); - - $err = ""; - // Connect to the database $dbc = pg_connect("host=$DB_HOST dbname=$DB_NAME user=$DB_USER password=$DB_PASSWORD") or die('Could not connect: ' . pg_last_error()); @@ -216,8 +259,6 @@ show_posts(); - pg_free_result($result); - pg_close($dbc); ?> @@ -237,7 +278,7 @@ - + (Limit: > 10; ?> KiB) @@ -254,11 +295,13 @@

- Old posts are automatically deleted once there are more than 50, anything inappropriate will be deleted. + Old posts are automatically deleted once there are more than , anything inappropriate will be deleted.

- [top] [reload] + [top] + [reload] + [source]