Move upload types into settings.php

This commit is contained in:
Trevor Slocum 2016-09-30 00:17:24 -07:00
parent d784dcc072
commit b2c0b69947
5 changed files with 99 additions and 112 deletions

View File

@ -41,7 +41,6 @@ Installing
- To allow WebM upload:
- Ensure your web host is running Linux.
- Install [mediainfo](http://mediaarea.net/en/MediaInfo) and [ffmpegthumbnailer](https://code.google.com/p/ffmpegthumbnailer/). On Ubuntu, run ``sudo apt-get install mediainfo ffmpegthumbnailer``.
- Set ``TINYIB_WEBM`` to ``true``.
- To require moderation before displaying posts:
- Ensure your ``TINYIB_DBMODE`` is set to ``mysql``, ``mysqli``, or ``pdo``.
- Set ``TINYIB_REQMOD`` to ``files`` to require moderation for posts with files attached.

View File

@ -157,53 +157,32 @@ if (isset($_POST['message']) || isset($_POST['file'])) {
$post['file_size'] = $_FILES['file']['size'];
$post['file_size_formatted'] = convertBytes($post['file_size']);
// Uploaded file type
$file_type = strtolower(preg_replace('/.*(\..+)/', '\1', $_FILES['file']['name']));
if ($file_type == '.jpeg') {
$file_type = '.jpg';
}
// Thumbnail type
if ($file_type == '.webm') {
$thumb_type = '.jpg';
} else if ($file_type == '.swf') {
$thumb_type = '.png';
} else {
$thumb_type = $file_type;
}
$file_name = time() . substr(microtime(), 2, 3);
$post['file'] = $file_name . $file_type;
$post['thumb'] = $file_name . "s" . $thumb_type;
$file_location = "src/" . $post['file'];
$thumb_location = "thumb/" . $post['thumb'];
checkDuplicateFile($post['file_hex']);
if (!move_uploaded_file($_FILES['file']['tmp_name'], $file_location)) {
fancyDie("Could not copy uploaded file.");
}
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)));
$file_mime_split = explode(' ', trim(@shell_exec('file --mime-type ' . $_FILES['file']['tmp_name'])));
if (count($file_mime_split) > 0) {
$file_mime = strtolower(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.");
if (!@getimagesize($_FILES['file']['tmp_name'])) {
fancyDie("Failed to read the MIME type and size of the uploaded file. Please retry the submission.");
}
$file_info = getimagesize($file_location);
$file_info = getimagesize($_FILES['file']['tmp_name']);
$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")) || (TINYIB_SWF && ($file_mime == "application/x-shockwave-flash")))) {
@unlink($file_location);
if (empty($file_mime) || !isset($tinyib_uploads[$file_mime])) {
fancyDie(supportedFileTypes());
}
$file_name = time() . substr(microtime(), 2, 3);
$post['file'] = $file_name . "." . $tinyib_uploads[$file_mime][0];
$file_location = "src/" . $post['file'];
if (!move_uploaded_file($_FILES['file']['tmp_name'], $file_location)) {
fancyDie("Could not copy uploaded file.");
}
if ($_FILES['file']['size'] != filesize($file_location)) {
@unlink($file_location);
fancyDie("File transfer failure. Please go back and try again.");
@ -215,19 +194,20 @@ if (isset($_POST['message']) || isset($_POST['file'])) {
if ($post['image_width'] > 0 && $post['image_height'] > 0) {
list($thumb_maxwidth, $thumb_maxheight) = thumbnailDimensions($post);
shell_exec("ffmpegthumbnailer -s " . max($thumb_maxwidth, $thumb_maxheight) . " -i $file_location -o $thumb_location");
$post['thumb'] = $file_name . "s.jpg";
shell_exec("ffmpegthumbnailer -s " . max($thumb_maxwidth, $thumb_maxheight) . " -i $file_location -o thumb/{$post['thumb']}");
$thumb_info = getimagesize($thumb_location);
$thumb_info = getimagesize("thumb/" . $post['thumb']);
$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);
@unlink("thumb/" . $post['thumb']);
fancyDie("Sorry, your video appears to be corrupt.");
}
addVideoOverlay($thumb_location);
addVideoOverlay("thumb/" . $post['thumb']);
}
$duration = intval(shell_exec('mediainfo --Inform="General;%Duration%" ' . $file_location));
@ -237,41 +217,47 @@ if (isset($_POST['message']) || isset($_POST['file'])) {
$post['file_original'] = "$mins:$secs" . ($post['file_original'] != '' ? (', ' . $post['file_original']) : '');
}
} else {
} else if (in_array($file_mime, array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif', 'application/x-shockwave-flash'))) {
$file_info = getimagesize($file_location);
$post['image_width'] = $file_info[0];
$post['image_height'] = $file_info[1];
}
if (isset($tinyib_uploads[$file_mime][1])) {
$thumbfile_split = explode(".", $tinyib_uploads[$file_mime][1]);
$post['thumb'] = $file_name . "s." . array_pop($thumbfile_split);
if (!copy($tinyib_uploads[$file_mime][1], "thumb/" . $post['thumb'])) {
@unlink($file_location);
fancyDie("Could not create thumbnail.");
}
if ($file_mime == "application/x-shockwave-flash") {
if (!copy('swf_thumbnail.png', $thumb_location)) {
@unlink($file_location);
fancyDie("Could not create thumbnail.");
}
addVideoOverlay("thumb/" . $post['thumb']);
}
} else if (in_array($file_mime, array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'))) {
$post['thumb'] = $file_name . "s." . $tinyib_uploads[$file_mime][0];
list($thumb_maxwidth, $thumb_maxheight) = thumbnailDimensions($post);
addVideoOverlay($thumb_location);
} else {
list($thumb_maxwidth, $thumb_maxheight) = thumbnailDimensions($post);
if (!createThumbnail($file_location, $thumb_location, $thumb_maxwidth, $thumb_maxheight)) {
@unlink($file_location);
fancyDie("Could not create thumbnail.");
}
if (!createThumbnail($file_location, "thumb/" . $post['thumb'], $thumb_maxwidth, $thumb_maxheight)) {
@unlink($file_location);
fancyDie("Could not create thumbnail.");
}
}
$thumb_info = getimagesize($thumb_location);
$post['thumb_width'] = $thumb_info[0];
$post['thumb_height'] = $thumb_info[1];
if ($post['thumb'] != '') {
$thumb_info = getimagesize("thumb/" . $post['thumb']);
$post['thumb_width'] = $thumb_info[0];
$post['thumb_height'] = $thumb_info[1];
}
}
}
if ($post['file'] == '') { // No file uploaded
$allowed = "";
if (TINYIB_PIC || TINYIB_SWF || TINYIB_WEBM) {
if (!empty($tinyib_uploads)) {
$allowed = "file";
}
if (TINYIB_EMBED) {
if (!empty($tinyib_embeds)) {
if ($allowed != "") {
$allowed .= " or ";
}

View File

@ -18,18 +18,6 @@ if (!defined('TINYIB_MAXWOP')) {
if (!defined('TINYIB_MAXHOP')) {
define('TINYIB_MAXHOP', TINYIB_MAXH);
}
if (!defined('TINYIB_PIC')) {
define('TINYIB_PIC', true);
}
if (!defined('TINYIB_SWF')) {
define('TINYIB_SWF', false);
}
if (!defined('TINYIB_WEBM')) {
define('TINYIB_WEBM', false);
}
if (!defined('TINYIB_EMBED')) {
define('TINYIB_EMBED', false);
}
if (!defined('TINYIB_THUMBNAIL')) {
define('TINYIB_THUMBNAIL', 'gd');
}
@ -54,6 +42,27 @@ if (!defined('TINYIB_DBDRIVER')) {
if (!defined('TINYIB_DBDSN')) {
define('TINYIB_DBDSN', '');
}
if (!isset($tinyib_embeds)) {
$tinyib_embeds = array('SoundCloud' => 'http://soundcloud.com/oembed?format=json&url=TINYIBEMBED', 'Vimeo' => 'http://vimeo.com/api/oembed.json?url=TINYIBEMBED', 'YouTube' => 'http://www.youtube.com/oembed?url=TINYIBEMBED&format=json');
if (!isset($tinyib_uploads)) {
$tinyib_uploads = array();
if (defined('TINYIB_PIC') && TINYIB_PIC) {
$tinyib_uploads['image/jpeg'] = array('jpg');
$tinyib_uploads['image/pjpeg'] = array('jpg');
$tinyib_uploads['image/png'] = array('png');
$tinyib_uploads['image/gif'] = array('gif');
}
if (defined('TINYIB_SWF') && TINYIB_SWF) {
$tinyib_uploads['application/x-shockwave-flash'] = array('swf', 'swf_thumbnail.png');
}
if (defined('TINYIB_WEBM') && TINYIB_WEBM) {
$tinyib_uploads['video/webm'] = array('webm');
$tinyib_uploads['adio/webm'] = array('webm');
}
}
if (!isset($tinyib_embeds)) {
$tinyib_embeds = array();
if (defined('TINYIB_EMBED') && TINYIB_EMBED) {
$tinyib_embeds['SoundCloud'] = 'http://soundcloud.com/oembed?format=json&url=TINYIBEMBED';
$tinyib_embeds['Vimeo'] = 'http://vimeo.com/api/oembed.json?url=TINYIBEMBED';
$tinyib_embeds['YouTube'] = 'http://www.youtube.com/oembed?url=TINYIBEMBED&format=json';
}
}

View File

@ -47,33 +47,18 @@ EOF;
}
function supportedFileTypes() {
$types_allowed = array();
if (TINYIB_PIC) {
array_push($types_allowed, "GIF", "JPG", "PNG");
}
if (TINYIB_SWF) {
array_push($types_allowed, "SWF");
}
if (TINYIB_WEBM) {
array_push($types_allowed, "WebM");
global $tinyib_uploads;
if (empty($tinyib_uploads)) {
return "";
}
$i = 0;
$types_count = count($types_allowed);
$types_formatted = "";
foreach ($types_allowed as $type) {
if (++$i >= $types_count - 1) {
$types_formatted .= $type . ($i == $types_count - 1 && $types_count > 1 ? " and " : "");
} else {
$types_formatted .= $type . ", ";
}
}
$types_allowed = array_map('strtoupper', array_unique(array_column($tinyib_uploads, 0)));
$types_last = array_pop($types_allowed);
$types_formatted = $types_allowed
? implode(', ', $types_allowed) . ' and ' . $types_last
: $types_last;
if ($types_formatted != "") {
return "Supported file type" . ($types_count != 1 ? "s are " : " is ") . $types_formatted . ".";
}
return $types_formatted;
return "Supported file type" . (count($tinyib_uploads) != 1 ? "s are " : " is ") . $types_formatted . ".";
}
function makeLinksClickable($text) {
@ -119,16 +104,16 @@ function buildPost($post, $res) {
if ($post['image_width'] > 0 && $post['image_height'] > 0) {
$dimensions = 'width="' . $post['image_width'] . '" height="' . $post['image_height'] . '"';
}
$expandhtml = <<<EOF
$expandhtml = <<<EOF
<video $dimensions style="position: static; pointer-events: inherit; display: inline; max-width: 100%; max-height: 100%;" controls autoplay loop>
<source src="$direct_link"></source>
</video>
EOF;
} else if ($post["file"] != '') {
} else if (in_array(substr($post['file'], -4), array('.jpg', '.png', '.gif'))) {
$expandhtml = "<a href=\"src/${post["file"]}\" onclick=\"return expandFile(event, '${post['id']}');\"><img src=\"" . ($res == TINYIB_RESPAGE ? "../" : "") . "src/${post["file"]}\" width=\"${post["image_width"]}\" style=\"max-width: 100%;height: auto;\"></a>";
}
$thumblink = "<a href=\"$direct_link\" target=\"_blank\"" . ((substr($post["file"], -4) != '.swf') ? " onclick=\"return expandFile(event, '${post['id']}');\"" : "") . ">";
$thumblink = "<a href=\"$direct_link\" target=\"_blank\"" . ((isEmbed($post["file_hex"]) || in_array(substr($post['file'], -4), array('.jpg', '.png', '.gif', 'webm'))) ? " onclick=\"return expandFile(event, '${post['id']}');\"" : "") . ">";
$expandhtml = rawurlencode($expandhtml);
if (isEmbed($post["file_hex"])) {
@ -237,6 +222,8 @@ EOF;
}
function buildPage($htmlposts, $parent, $pages = 0, $thispage = 0) {
global $tinyib_uploads, $tinyib_embeds;
$managelink = basename($_SERVER['PHP_SELF']) . "?manage";
$maxdimensions = TINYIB_MAXWOP . 'x' . TINYIB_MAXHOP;
if (TINYIB_MAXW != TINYIB_MAXWOP || TINYIB_MAXH != TINYIB_MAXHOP) {
@ -323,7 +310,7 @@ EOF;
EOF;
}
if (TINYIB_PIC || TINYIB_WEBM || TINYIB_SWF) {
if (!empty($tinyib_uploads)) {
if (TINYIB_MAXKB > 0) {
$max_file_size_input_html = '<input type="hidden" name="MAX_FILE_SIZE" value="' . strval(TINYIB_MAXKB * 1024) . '">';
$max_file_size_rules_html = '<li>Maximum file size allowed is ' . TINYIB_MAXKBDESC . '.</li>';
@ -343,7 +330,7 @@ EOF;
EOF;
}
if (TINYIB_EMBED) {
if (!empty($tinyib_embeds)) {
$embed_input_html = <<<EOF
<tr>
<td class="postblock">
@ -361,7 +348,7 @@ EOF;
}
$thumbnails_html = '';
if (TINYIB_PIC) {
if (isset($tinyib_uploads['image/jpeg']) || isset($tinyib_uploads['image/pjpeg']) || isset($tinyib_uploads['image/png']) || isset($tinyib_uploads['image/gif'])) {
$thumbnails_html = "<li>Images greater than $maxdimensions will be thumbnailed.</li>";
}

View File

@ -30,9 +30,21 @@ define('TINYIB_MAXTHREADS', 100); // Oldest threads are discarded when the t
define('TINYIB_MAXREPLIES', 0); // Maximum replies before a thread stops bumping [0 to disable]
// Upload types
define('TINYIB_PIC', true); // Enable .jpg, .png and .gif image file upload
define('TINYIB_SWF', false); // Enable .swf Flash file upload
define('TINYIB_WEBM', false); // Enable .webm audio/video file upload (see README for instructions)
// Empty array to disable
// Format: MIME type => (extension, optional thumbnail)
$tinyib_uploads = array('image/jpeg' => array('jpg'),
'image/pjpeg' => array('jpg'),
'image/png' => array('png'),
'image/gif' => array('gif'));
# 'application/x-shockwave-flash' => array('swf', 'swf_thumbnail.png'));
# 'video/webm' => array('webm')); // WebM upload requires mediainfo and ffmpegthumbnailer (see README for instructions)
# 'audio/webm' => array('webm'));
// oEmbed APIs
// Empty array to disable
$tinyib_embeds = array('SoundCloud' => 'http://soundcloud.com/oembed?format=json&url=TINYIBEMBED',
'Vimeo' => 'http://vimeo.com/api/oembed.json?url=TINYIBEMBED',
'YouTube' => 'http://www.youtube.com/oembed?url=TINYIBEMBED&format=json');
// File control
define('TINYIB_MAXKB', 2048); // Maximum file size in kilobytes [0 to disable]
@ -48,12 +60,6 @@ define('TINYIB_MAXHOP', 250); // Height
define('TINYIB_MAXW', 250); // Width
define('TINYIB_MAXH', 250); // Height
// Embedding - oEmbed API
define('TINYIB_EMBED', true); // Enable embedding (e.g. YouTube, Vimeo, SoundCloud)
$tinyib_embeds = array('SoundCloud' => 'http://soundcloud.com/oembed?format=json&url=TINYIBEMBED',
'Vimeo' => 'http://vimeo.com/api/oembed.json?url=TINYIBEMBED',
'YouTube' => 'http://www.youtube.com/oembed?url=TINYIBEMBED&format=json');
// Tripcode seed - Must not change once set!
define('TINYIB_TRIPSEED', ''); // Enter some random text (used when generating secure tripcodes)