2009-09-20 02:53:15 +00:00
< ? php
2014-06-24 19:51:22 +00:00
if ( ! defined ( 'TINYIB_BOARD' )) {
die ( '' );
}
2009-09-20 02:53:15 +00:00
2020-08-10 21:52:27 +00:00
if ( ! function_exists ( 'array_column' )) {
function array_column ( $array , $column_name ) {
return array_map ( function ( $element ) use ( $column_name ) {
return $element [ $column_name ];
}, $array );
}
}
2020-11-13 20:32:42 +00:00
function hashData ( $data ) {
global $bcrypt_salt ;
if ( substr ( $data , 0 , 4 ) == '$2y$' ) {
return $data ;
}
return crypt ( $data , $bcrypt_salt );
}
2009-09-20 02:53:15 +00:00
function cleanString ( $string ) {
2018-07-24 23:46:58 +00:00
$search = array ( " & " , " < " , " > " );
$replace = array ( " & " , " < " , " > " );
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
return str_replace ( $search , $replace , $string );
}
2020-09-06 16:42:29 +00:00
function plural ( $count , $singular , $plural ) {
2010-11-10 10:34:41 +00:00
if ( $plural == 's' ) {
2014-06-24 19:51:22 +00:00
$plural = $singular . $plural ;
}
return ( $count == 1 ? $singular : $plural );
2010-11-10 10:34:41 +00:00
}
2009-09-20 02:53:15 +00:00
function threadUpdated ( $id ) {
rebuildThread ( $id );
rebuildIndexes ();
}
2011-08-07 07:24:39 +00:00
function newPost ( $parent = TINYIB_NEWTHREAD ) {
return array ( 'parent' => $parent ,
2014-06-24 19:51:22 +00:00
'timestamp' => '0' ,
'bumped' => '0' ,
'ip' => '' ,
'name' => '' ,
'tripcode' => '' ,
'email' => '' ,
'nameblock' => '' ,
'subject' => '' ,
'message' => '' ,
'password' => '' ,
'file' => '' ,
'file_hex' => '' ,
'file_original' => '' ,
'file_size' => '0' ,
'file_size_formatted' => '' ,
'image_width' => '0' ,
'image_height' => '0' ,
'thumb' => '' ,
'thumb_width' => '0' ,
2014-10-17 01:12:52 +00:00
'thumb_height' => '0' ,
'stickied' => '0' ,
2020-08-11 13:09:11 +00:00
'locked' => '0' ,
2014-10-17 01:12:52 +00:00
'moderated' => '1' );
2009-09-20 02:53:15 +00:00
}
function convertBytes ( $number ) {
$len = strlen ( $number );
if ( $len < 4 ) {
return sprintf ( " %dB " , $number );
} elseif ( $len <= 6 ) {
2014-06-24 19:51:22 +00:00
return sprintf ( " %0.2fKB " , $number / 1024 );
2009-09-20 02:53:15 +00:00
} elseif ( $len <= 9 ) {
2014-06-24 19:51:22 +00:00
return sprintf ( " %0.2fMB " , $number / 1024 / 1024 );
2009-09-20 02:53:15 +00:00
}
2014-06-24 19:51:22 +00:00
return sprintf ( " %0.2fGB " , $number / 1024 / 1024 / 1024 );
2009-09-20 02:53:15 +00:00
}
function nameAndTripcode ( $name ) {
2010-06-22 07:46:00 +00:00
if ( preg_match ( " /(#|!)(.*)/ " , $name , $regs )) {
2009-09-20 02:53:15 +00:00
$cap = $regs [ 2 ];
$cap_full = '#' . $regs [ 2 ];
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
if ( function_exists ( 'mb_convert_encoding' )) {
$recoded_cap = mb_convert_encoding ( $cap , 'SJIS' , 'UTF-8' );
if ( $recoded_cap != '' ) {
$cap = $recoded_cap ;
}
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
if ( strpos ( $name , '#' ) === false ) {
$cap_delimiter = '!' ;
} elseif ( strpos ( $name , '!' ) === false ) {
$cap_delimiter = '#' ;
} else {
$cap_delimiter = ( strpos ( $name , '#' ) < strpos ( $name , '!' )) ? '#' : '!' ;
}
2014-06-24 19:51:22 +00:00
2010-06-22 07:46:00 +00:00
if ( preg_match ( " /(.*)( " . $cap_delimiter . " )(.*)/ " , $cap , $regs_secure )) {
2009-09-20 02:53:15 +00:00
$cap = $regs_secure [ 1 ];
$cap_secure = $regs_secure [ 3 ];
$is_secure_trip = true ;
} else {
$is_secure_trip = false ;
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
$tripcode = " " ;
2011-01-08 01:36:00 +00:00
if ( $cap != " " ) { // Copied from Futabally
2009-09-20 02:53:15 +00:00
$cap = strtr ( $cap , " & " , " & " );
$cap = strtr ( $cap , " , " , " , " );
2014-06-24 19:51:22 +00:00
$salt = substr ( $cap . " H. " , 1 , 2 );
2010-06-22 07:46:00 +00:00
$salt = preg_replace ( " /[^ \ .-z]/ " , " . " , $salt );
2014-06-24 19:51:22 +00:00
$salt = strtr ( $salt , " :;<=>?@[ \\ ]^_` " , " ABCDEFGabcdef " );
2009-09-20 02:53:15 +00:00
$tripcode = substr ( crypt ( $cap , $salt ), - 10 );
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
if ( $is_secure_trip ) {
if ( $cap != " " ) {
$tripcode .= " ! " ;
}
2014-06-24 19:51:22 +00:00
2010-12-03 10:11:14 +00:00
$tripcode .= " ! " . substr ( md5 ( $cap_secure . TINYIB_TRIPSEED ), 2 , 10 );
2009-09-20 02:53:15 +00:00
}
2014-06-24 19:51:22 +00:00
2010-06-22 07:46:00 +00:00
return array ( preg_replace ( " /( " . $cap_delimiter . " )(.*)/ " , " " , $name ), $tripcode );
2009-09-20 02:53:15 +00:00
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
return array ( $name , " " );
}
2011-08-07 07:24:39 +00:00
function nameBlock ( $name , $tripcode , $email , $timestamp , $rawposttext ) {
2021-01-31 08:09:51 +00:00
global $tinyib_anonymous ;
$anonymous = $tinyib_anonymous [ array_rand ( $tinyib_anonymous )];
2009-09-22 21:29:27 +00:00
$output = '<span class="postername">' ;
2021-01-31 08:09:51 +00:00
$output .= ( $name == '' && $tripcode == '' ) ? $anonymous : $name ;
2014-06-24 19:51:22 +00:00
2011-09-06 06:53:37 +00:00
if ( $tripcode != '' ) {
2009-09-20 02:53:15 +00:00
$output .= '</span><span class="postertrip">!' . $tripcode ;
}
2014-06-24 19:51:22 +00:00
2009-09-22 21:29:27 +00:00
$output .= '</span>' ;
2014-06-24 19:51:22 +00:00
2011-09-06 06:53:37 +00:00
if ( $email != '' && strtolower ( $email ) != 'noko' ) {
2009-09-20 02:53:15 +00:00
$output = '<a href="mailto:' . $email . '">' . $output . '</a>' ;
}
2009-09-22 21:29:27 +00:00
2020-10-22 15:24:11 +00:00
return $output . $rawposttext . ' ' . strftime ( TINYIB_DATEFMT , $timestamp );
2009-09-20 02:53:15 +00:00
}
function writePage ( $filename , $contents ) {
2010-12-03 10:11:14 +00:00
$tempfile = tempnam ( 'res/' , TINYIB_BOARD . 'tmp' ); /* Create the temporary file */
2009-09-20 02:53:15 +00:00
$fp = fopen ( $tempfile , 'w' );
fwrite ( $fp , $contents );
fclose ( $fp );
/* If we aren't able to use the rename function, try the alternate method */
if ( !@ rename ( $tempfile , $filename )) {
copy ( $tempfile , $filename );
unlink ( $tempfile );
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
chmod ( $filename , 0664 ); /* it was created 0600 */
}
function fixLinksInRes ( $html ) {
2020-10-08 18:02:25 +00:00
$search = array ( ' href="css/' , ' src="js/' , ' href="src/' , ' href="thumb/' , ' href="res/' , ' href="imgboard.php' , ' href="catalog.html' , ' href="favicon.ico' , 'src="thumb/' , 'src="inc/' , 'src="sticky.png' , 'src="lock.png' , ' action="imgboard.php' , ' action="catalog.html' );
2020-08-11 13:01:00 +00:00
$replace = array ( ' href="../css/' , ' src="../js/' , ' href="../src/' , ' href="../thumb/' , ' href="../res/' , ' href="../imgboard.php' , ' href="../catalog.html' , ' href="../favicon.ico' , 'src="../thumb/' , 'src="../inc/' , 'src="../sticky.png' , 'src="../lock.png' , ' action="../imgboard.php' , ' action="../catalog.html' );
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
return str_replace ( $search , $replace , $html );
}
2011-01-07 08:56:59 +00:00
function _postLink ( $matches ) {
$post = postByID ( $matches [ 1 ]);
if ( $post ) {
2011-08-07 07:24:39 +00:00
return '<a href="res/' . ( $post [ 'parent' ] == TINYIB_NEWTHREAD ? $post [ 'id' ] : $post [ 'parent' ]) . '.html#' . $matches [ 1 ] . '">' . $matches [ 0 ] . '</a>' ;
2011-01-07 08:56:59 +00:00
}
return $matches [ 0 ];
}
function postLink ( $message ) {
return preg_replace_callback ( '/>>([0-9]+)/' , '_postLink' , $message );
}
2018-07-24 23:46:58 +00:00
function _finishWordBreak ( $matches ) {
return '<a' . $matches [ 1 ] . 'href="' . str_replace ( TINYIB_WORDBREAK_IDENTIFIER , '' , $matches [ 2 ]) . '"' . $matches [ 3 ] . '>' . str_replace ( TINYIB_WORDBREAK_IDENTIFIER , '<br>' , $matches [ 4 ]) . '</a>' ;
}
function finishWordBreak ( $message ) {
return str_replace ( TINYIB_WORDBREAK_IDENTIFIER , '<br>' , preg_replace_callback ( '/<a(.*?)href="([^"]*?)"(.*?)>(.*?)<\/a>/' , '_finishWordBreak' , $message ));
}
2009-09-20 02:53:15 +00:00
function colorQuote ( $message ) {
2014-06-24 19:51:22 +00:00
if ( substr ( $message , - 1 , 1 ) != " \n " ) {
$message .= " \n " ;
}
2009-09-20 02:53:15 +00:00
return preg_replace ( '/^(>[^\>](.*))\n/m' , '<span class="unkfunc">\\1</span>' . " \n " , $message );
}
function deletePostImages ( $post ) {
2016-09-22 00:47:58 +00:00
if ( ! isEmbed ( $post [ 'file_hex' ]) && $post [ 'file' ] != '' ) {
2014-06-24 19:51:22 +00:00
@ unlink ( 'src/' . $post [ 'file' ]);
}
if ( $post [ 'thumb' ] != '' ) {
@ unlink ( 'thumb/' . $post [ 'thumb' ]);
}
2009-09-20 02:53:15 +00:00
}
2020-11-13 20:32:42 +00:00
function deletePost ( $id ) {
$id = intval ( $id );
$posts = postsInThreadByID ( $id , false );
$op = array ();
foreach ( $posts as $post ) {
if ( $post [ 'parent' ] == TINYIB_NEWTHREAD ) {
$op = $post ;
continue ;
}
deletePostImages ( $post );
deleteReportsByPost ( $post [ 'id' ]);
deletePostByID ( $post [ 'id' ]);
}
if ( ! empty ( $op )) {
deletePostImages ( $op );
deleteReportsByPost ( $op [ 'id' ]);
deletePostByID ( $op [ 'id' ]);
}
@ unlink ( 'res/' . $id . '.html' );
}
2020-10-30 16:36:44 +00:00
function checkCAPTCHA ( $mode ) {
2021-03-10 18:45:54 +00:00
if ( $mode === 'hcaptcha' ) {
$captcha = isset ( $_POST [ 'h-captcha-response' ]) ? $_POST [ 'h-captcha-response' ] : '' ;
if ( $captcha == '' ) {
fancyDie ( 'Failed CAPTCHA. Reason:<br>Please click the checkbox labeled "I am human".' );
}
$data = array (
'secret' => TINYIB_HCAPTCHA_SECRET ,
'response' => $captcha
);
$verify = curl_init ();
curl_setopt ( $verify , CURLOPT_URL , " https://hcaptcha.com/siteverify " );
curl_setopt ( $verify , CURLOPT_POST , true );
curl_setopt ( $verify , CURLOPT_POSTFIELDS , http_build_query ( $data ));
curl_setopt ( $verify , CURLOPT_RETURNTRANSFER , true );
$verifyResponse = curl_exec ( $verify );
$responseData = json_decode ( $verifyResponse );
if ( ! isset ( $responseData -> success ) || ! $responseData -> success ) {
fancyDie ( 'Failed CAPTCHA.' );
}
} else if ( $mode === 'recaptcha' ) {
2016-07-19 21:14:54 +00:00
require_once 'inc/recaptcha/autoload.php' ;
$captcha = isset ( $_POST [ 'g-recaptcha-response' ]) ? $_POST [ 'g-recaptcha-response' ] : '' ;
$failed_captcha = true ;
2014-11-06 11:34:19 +00:00
2016-07-19 21:14:54 +00:00
$recaptcha = new \ReCaptcha\ReCaptcha ( TINYIB_RECAPTCHA_SECRET );
$resp = $recaptcha -> verify ( $captcha , $_SERVER [ 'REMOTE_ADDR' ]);
if ( $resp -> isSuccess ()) {
$failed_captcha = false ;
}
2014-11-06 11:34:19 +00:00
2016-07-19 21:14:54 +00:00
if ( $failed_captcha ) {
$captcha_error = 'Failed CAPTCHA.' ;
2017-06-13 00:11:59 +00:00
$error_reason = '' ;
if ( count ( $resp -> getErrorCodes ()) == 1 ) {
$error_codes = $resp -> getErrorCodes ();
$error_reason = $error_codes [ 0 ];
}
if ( $error_reason == 'missing-input-response' ) {
2016-07-19 21:14:54 +00:00
$captcha_error .= ' Please click the checkbox labeled "I\'m not a robot".' ;
} else {
$captcha_error .= ' Reason:' ;
foreach ( $resp -> getErrorCodes () as $error ) {
$captcha_error .= '<br>' . $error ;
}
}
fancyDie ( $captcha_error );
}
2020-10-30 16:36:44 +00:00
} else if ( $mode ) { // Simple CAPTCHA
2016-07-19 21:14:54 +00:00
$captcha = isset ( $_POST [ 'captcha' ]) ? strtolower ( trim ( $_POST [ 'captcha' ])) : '' ;
$captcha_solution = isset ( $_SESSION [ 'tinyibcaptcha' ]) ? strtolower ( trim ( $_SESSION [ 'tinyibcaptcha' ])) : '' ;
if ( $captcha == '' ) {
2020-09-01 03:51:14 +00:00
fancyDie ( __ ( 'Please enter the CAPTCHA text.' ));
2016-07-19 21:14:54 +00:00
} else if ( $captcha != $captcha_solution ) {
2020-09-01 03:51:14 +00:00
fancyDie ( __ ( 'Incorrect CAPTCHA text entered. Please try again.<br>Click the image to retrieve a new CAPTCHA.' ));
2016-07-19 21:14:54 +00:00
}
2014-11-06 11:34:19 +00:00
}
}
2010-12-31 23:24:55 +00:00
function checkBanned () {
$ban = banByIP ( $_SERVER [ 'REMOTE_ADDR' ]);
if ( $ban ) {
if ( $ban [ 'expire' ] == 0 || $ban [ 'expire' ] > time ()) {
2020-10-22 15:24:11 +00:00
$expire = ( $ban [ 'expire' ] > 0 ) ? ( '<br>This ban will expire ' . strftime ( TINYIB_DATEFMT , $ban [ 'expire' ])) : '<br>This ban is permanent and will not expire.' ;
2011-08-07 07:24:39 +00:00
$reason = ( $ban [ 'reason' ] == '' ) ? '' : ( '<br>Reason: ' . $ban [ 'reason' ]);
2020-11-13 21:19:07 +00:00
fancyDie ( 'Your IP address ' . $_SERVER [ 'REMOTE_ADDR' ] . ' has been banned from posting on this image board. ' . $expire . $reason );
2010-12-31 23:24:55 +00:00
} else {
clearExpiredBans ();
}
}
}
2020-11-23 20:44:02 +00:00
function checkKeywords ( $text ) {
$keywords = allKeywords ();
foreach ( $keywords as $keyword ) {
if ( stripos ( $text , $keyword [ 'text' ]) !== false ) {
return $keyword ;
}
}
return array ();
}
2010-12-31 23:24:55 +00:00
function checkFlood () {
2011-08-07 07:24:39 +00:00
if ( TINYIB_DELAY > 0 ) {
$lastpost = lastPostByIP ();
if ( $lastpost ) {
if (( time () - $lastpost [ 'timestamp' ]) < TINYIB_DELAY ) {
2020-09-06 16:42:29 +00:00
fancyDie ( " Please wait a moment before posting again. You will be able to make another post in " . ( TINYIB_DELAY - ( time () - $lastpost [ 'timestamp' ])) . " " . plural ( TINYIB_DELAY - ( time () - $lastpost [ 'timestamp' ]), " second " , " seconds " ) . " . " );
2011-08-07 07:24:39 +00:00
}
2010-12-31 23:24:55 +00:00
}
}
}
2011-01-07 09:50:03 +00:00
function checkMessageSize () {
2021-01-28 23:03:30 +00:00
if ( TINYIB_MAXMESSAGE > 0 && strlen ( $_POST [ 'message' ]) > TINYIB_MAXMESSAGE ) {
fancyDie ( sprintf ( __ ( 'Please shorten your message, or post it in multiple parts. Your message is %1$d characters long, and the maximum allowed is %2$d.' ), strlen ( $_POST [ 'message' ]), TINYIB_MAXMESSAGE ));
2011-01-07 09:50:03 +00:00
}
}
2021-03-28 18:06:43 +00:00
function manageCheckLogIn ( $requireKey ) {
$loggedin = false ;
$isadmin = false ;
2021-03-14 02:49:09 +00:00
$key = ( isset ( $_GET [ 'manage' ]) && $_GET [ 'manage' ] != '' ) ? hashData ( $_GET [ 'manage' ]) : '' ;
if ( $key == '' && isset ( $_SESSION [ 'tinyib_key' ])) {
$key = $_SESSION [ 'tinyib_key' ];
}
if ( TINYIB_MANAGEKEY != '' && $key !== hashData ( TINYIB_MANAGEKEY )) {
$_SESSION [ 'tinyib' ] = '' ;
$_SESSION [ 'tinyib_key' ] = '' ;
session_destroy ();
2021-03-28 18:06:43 +00:00
if ( $requireKey ) {
fancyDie ( __ ( 'Invalid key.' ));
}
return array ( $loggedin , $isadmin );
2021-03-14 02:49:09 +00:00
}
2015-10-27 04:01:01 +00:00
if ( isset ( $_POST [ 'managepassword' ])) {
2020-10-30 16:36:44 +00:00
checkCAPTCHA ( TINYIB_MANAGECAPTCHA );
2015-10-27 04:01:01 +00:00
if ( $_POST [ 'managepassword' ] === TINYIB_ADMINPASS ) {
2020-11-13 20:32:42 +00:00
$_SESSION [ 'tinyib' ] = hashData ( TINYIB_ADMINPASS );
2021-03-14 02:49:09 +00:00
$_SESSION [ 'tinyib_key' ] = hashData ( TINYIB_MANAGEKEY );
2015-10-27 04:01:01 +00:00
} elseif ( TINYIB_MODPASS != '' && $_POST [ 'managepassword' ] === TINYIB_MODPASS ) {
2020-11-13 20:32:42 +00:00
$_SESSION [ 'tinyib' ] = hashData ( TINYIB_MODPASS );
2021-03-14 02:49:09 +00:00
$_SESSION [ 'tinyib_key' ] = hashData ( TINYIB_MANAGEKEY );
2020-10-30 16:36:44 +00:00
} else {
fancyDie ( __ ( 'Invalid password.' ));
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 ( isset ( $_SESSION [ 'tinyib' ])) {
2020-11-13 20:32:42 +00:00
if ( $_SESSION [ 'tinyib' ] === hashData ( TINYIB_ADMINPASS )) {
2009-09-20 02:53:15 +00:00
$loggedin = true ;
$isadmin = true ;
2020-11-13 20:32:42 +00:00
} elseif ( TINYIB_MODPASS != '' && $_SESSION [ 'tinyib' ] === hashData ( TINYIB_MODPASS )) {
2009-09-20 02:53:15 +00:00
$loggedin = true ;
}
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
return array ( $loggedin , $isadmin );
}
2010-12-03 10:31:51 +00:00
function setParent () {
if ( isset ( $_POST [ " parent " ])) {
2011-08-07 07:24:39 +00:00
if ( $_POST [ " parent " ] != TINYIB_NEWTHREAD ) {
2010-12-03 10:31:51 +00:00
if ( ! threadExistsByID ( $_POST [ 'parent' ])) {
2020-09-01 03:51:14 +00:00
fancyDie ( __ ( 'Invalid parent thread ID supplied, unable to create post.' ));
2010-12-03 10:31:51 +00:00
}
2014-06-24 19:51:22 +00:00
2010-12-03 10:31:51 +00:00
return $_POST [ " parent " ];
}
}
2014-06-24 19:51:22 +00:00
2011-08-07 07:24:39 +00:00
return TINYIB_NEWTHREAD ;
2010-12-03 10:31:51 +00:00
}
2011-08-07 07:24:39 +00:00
function isRawPost () {
if ( isset ( $_POST [ 'rawpost' ])) {
2021-03-28 18:06:43 +00:00
list ( $loggedin , $isadmin ) = manageCheckLogIn ( false );
return $loggedin ;
2010-12-03 10:31:51 +00:00
}
2014-06-24 19:51:22 +00:00
2010-12-03 10:31:51 +00:00
return false ;
}
function validateFileUpload () {
switch ( $_FILES [ 'file' ][ 'error' ]) {
case UPLOAD_ERR_OK :
break ;
case UPLOAD_ERR_FORM_SIZE :
2020-09-01 03:51:14 +00:00
fancyDie ( sprintf ( __ ( 'That file is larger than %s.' ), TINYIB_MAXKBDESC ));
2010-12-03 10:31:51 +00:00
break ;
case UPLOAD_ERR_INI_SIZE :
2020-09-01 03:51:14 +00:00
fancyDie ( sprintf ( __ ( 'The uploaded file exceeds the upload_max_filesize directive (%s) in php.ini.' ), ini_get ( 'upload_max_filesize' )));
2010-12-03 10:31:51 +00:00
break ;
case UPLOAD_ERR_PARTIAL :
2020-09-01 03:51:14 +00:00
fancyDie ( __ ( 'The uploaded file was only partially uploaded.' ));
2010-12-03 10:31:51 +00:00
break ;
case UPLOAD_ERR_NO_FILE :
2020-09-01 03:51:14 +00:00
fancyDie ( __ ( 'No file was uploaded.' ));
2010-12-03 10:31:51 +00:00
break ;
case UPLOAD_ERR_NO_TMP_DIR :
2020-09-01 03:51:14 +00:00
fancyDie ( __ ( 'Missing a temporary folder.' ));
2010-12-03 10:31:51 +00:00
break ;
case UPLOAD_ERR_CANT_WRITE :
2020-09-01 03:51:14 +00:00
fancyDie ( __ ( 'Failed to write file to disk' ));
2010-12-03 10:31:51 +00:00
break ;
default :
2020-09-01 03:51:14 +00:00
fancyDie ( __ ( 'Unable to save the uploaded file.' ));
2010-12-03 10:31:51 +00:00
}
}
2014-06-24 19:51:22 +00:00
function checkDuplicateFile ( $hex ) {
2010-12-03 10:31:51 +00:00
$hexmatches = postsByHex ( $hex );
if ( count ( $hexmatches ) > 0 ) {
foreach ( $hexmatches as $hexmatch ) {
2020-12-21 06:50:17 +00:00
fancyDie ( sprintf ( __ ( 'Duplicate file uploaded. That file has already been posted <a href="%s">here</a>.' ), 'res/' . (( $hexmatch [ 'parent' ] == TINYIB_NEWTHREAD ) ? $hexmatch [ 'id' ] : $hexmatch [ 'parent' ]) . '.html#' . $hexmatch [ 'id' ]));
2010-12-03 10:31:51 +00:00
}
}
}
2013-05-15 03:42:56 +00:00
function thumbnailDimensions ( $post ) {
if ( $post [ 'parent' ] == TINYIB_NEWTHREAD ) {
$max_width = TINYIB_MAXWOP ;
$max_height = TINYIB_MAXHOP ;
} else {
$max_width = TINYIB_MAXW ;
$max_height = TINYIB_MAXH ;
}
return ( $post [ 'image_width' ] > $max_width || $post [ 'image_height' ] > $max_height ) ? array ( $max_width , $max_height ) : array ( $post [ 'image_width' ], $post [ 'image_height' ]);
2010-12-03 10:31:51 +00:00
}
2015-06-25 07:49:25 +00:00
function createThumbnail ( $file_location , $thumb_location , $new_w , $new_h ) {
if ( TINYIB_THUMBNAIL == 'gd' ) {
$system = explode ( " . " , $thumb_location );
$system = array_reverse ( $system );
if ( preg_match ( " /jpg|jpeg/ " , $system [ 0 ])) {
$src_img = imagecreatefromjpeg ( $file_location );
} else if ( preg_match ( " /png/ " , $system [ 0 ])) {
$src_img = imagecreatefrompng ( $file_location );
} else if ( preg_match ( " /gif/ " , $system [ 0 ])) {
$src_img = imagecreatefromgif ( $file_location );
} else {
return false ;
}
2014-06-24 19:51:22 +00:00
2015-06-25 07:49:25 +00:00
if ( ! $src_img ) {
2020-09-01 03:51:14 +00:00
fancyDie ( __ ( 'Unable to read the uploaded file while creating its thumbnail. A common cause for this is an incorrect extension when the file is actually of a different type.' ));
2015-06-25 07:49:25 +00:00
}
2014-06-24 19:51:22 +00:00
2015-06-25 07:49:25 +00:00
$old_x = imageSX ( $src_img );
$old_y = imageSY ( $src_img );
$percent = ( $old_x > $old_y ) ? ( $new_w / $old_x ) : ( $new_h / $old_y );
$thumb_w = round ( $old_x * $percent );
$thumb_h = round ( $old_y * $percent );
$dst_img = imagecreatetruecolor ( $thumb_w , $thumb_h );
if ( preg_match ( " /png/ " , $system [ 0 ]) && imagepng ( $src_img , $thumb_location )) {
imagealphablending ( $dst_img , false );
imagesavealpha ( $dst_img , true );
$color = imagecolorallocatealpha ( $dst_img , 0 , 0 , 0 , 0 );
imagefilledrectangle ( $dst_img , 0 , 0 , $thumb_w , $thumb_h , $color );
imagecolortransparent ( $dst_img , $color );
imagecopyresampled ( $dst_img , $src_img , 0 , 0 , 0 , 0 , $thumb_w , $thumb_h , $old_x , $old_y );
} else {
fastimagecopyresampled ( $dst_img , $src_img , 0 , 0 , 0 , 0 , $thumb_w , $thumb_h , $old_x , $old_y );
2009-09-20 02:53:15 +00:00
}
2015-06-25 07:49:25 +00:00
if ( preg_match ( " /png/ " , $system [ 0 ])) {
if ( ! imagepng ( $dst_img , $thumb_location )) {
return false ;
}
} else if ( preg_match ( " /jpg|jpeg/ " , $system [ 0 ])) {
if ( ! imagejpeg ( $dst_img , $thumb_location , 70 )) {
return false ;
}
} else if ( preg_match ( " /gif/ " , $system [ 0 ])) {
if ( ! imagegif ( $dst_img , $thumb_location )) {
return false ;
}
2009-09-20 02:53:15 +00:00
}
2015-06-25 07:49:25 +00:00
imagedestroy ( $dst_img );
imagedestroy ( $src_img );
2020-08-19 01:51:32 +00:00
} else { // ImageMagick
2015-06-25 07:49:25 +00:00
$discard = '' ;
2020-08-19 01:51:32 +00:00
$exit_status = 1 ;
exec ( " convert -version " , $discard , $exit_status );
if ( $exit_status != 0 ) {
fancyDie ( 'ImageMagick is not installed, or the convert command is not in the server\'s $PATH.<br>Install ImageMagick, or set TINYIB_THUMBNAIL to \'gd\'.' );
}
2015-06-25 07:49:25 +00:00
$exit_status = 1 ;
exec ( " convert $file_location -auto-orient -thumbnail ' " . $new_w . " x " . $new_h . " ' -coalesce -layers OptimizeFrame -depth 4 -type palettealpha $thumb_location " , $discard , $exit_status );
if ( $exit_status != 0 ) {
2009-09-20 02:53:15 +00:00
return false ;
}
}
2014-08-03 20:43:33 +00:00
2009-09-20 02:53:15 +00:00
return true ;
}
2014-08-03 20:43:33 +00:00
function fastimagecopyresampled ( & $dst_image , & $src_image , $dst_x , $dst_y , $src_x , $src_y , $dst_w , $dst_h , $src_w , $src_h , $quality = 3 ) {
2014-06-24 19:51:22 +00:00
// Author: Tim Eckel - Date: 12/17/04 - Project: FreeRingers.net - Freely distributable.
if ( empty ( $src_image ) || empty ( $dst_image )) {
return false ;
}
2009-09-20 02:53:15 +00:00
if ( $quality <= 1 ) {
2014-06-24 19:51:22 +00:00
$temp = imagecreatetruecolor ( $dst_w + 1 , $dst_h + 1 );
imagecopyresized ( $temp , $src_image , $dst_x , $dst_y , $src_x , $src_y , $dst_w + 1 , $dst_h + 1 , $src_w , $src_h );
imagecopyresized ( $dst_image , $temp , 0 , 0 , 0 , 0 , $dst_w , $dst_h , $dst_w , $dst_h );
imagedestroy ( $temp );
2009-09-20 02:53:15 +00:00
} elseif ( $quality < 5 && (( $dst_w * $quality ) < $src_w || ( $dst_h * $quality ) < $src_h )) {
$tmp_w = $dst_w * $quality ;
$tmp_h = $dst_h * $quality ;
2014-06-24 19:51:22 +00:00
$temp = imagecreatetruecolor ( $tmp_w + 1 , $tmp_h + 1 );
imagecopyresized ( $temp , $src_image , $dst_x * $quality , $dst_y * $quality , $src_x , $src_y , $tmp_w + 1 , $tmp_h + 1 , $src_w , $src_h );
imagecopyresampled ( $dst_image , $temp , 0 , 0 , 0 , 0 , $dst_w , $dst_h , $tmp_w , $tmp_h );
imagedestroy ( $temp );
2009-09-20 02:53:15 +00:00
} else {
2014-06-24 19:51:22 +00:00
imagecopyresampled ( $dst_image , $src_image , $dst_x , $dst_y , $src_x , $src_y , $dst_w , $dst_h , $src_w , $src_h );
2009-09-20 02:53:15 +00:00
}
2014-06-24 19:51:22 +00:00
2009-09-20 02:53:15 +00:00
return true ;
}
2014-07-05 19:27:18 +00:00
function addVideoOverlay ( $thumb_location ) {
2018-10-19 07:07:40 +00:00
if ( ! file_exists ( 'video_overlay.png' )) {
return ;
}
if ( TINYIB_THUMBNAIL == 'gd' ) {
2014-07-05 19:27:18 +00:00
if ( substr ( $thumb_location , - 4 ) == " .jpg " ) {
$thumbnail = imagecreatefromjpeg ( $thumb_location );
} else {
$thumbnail = imagecreatefrompng ( $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' );
if ( substr ( $thumb_location , - 4 ) == " .png " ) {
imagecolortransparent ( $thumbnail , imagecolorallocatealpha ( $thumbnail , 0 , 0 , 0 , 127 ));
imagealphablending ( $thumbnail , true );
imagesavealpha ( $thumbnail , true );
}
imagecopy ( $thumbnail , $overlay_play , ( $width / 2 ) - ( $overlay_width / 2 ), ( $height / 2 ) - ( $overlay_height / 2 ), 0 , 0 , $overlay_width , $overlay_height );
if ( substr ( $thumb_location , - 4 ) == " .jpg " ) {
imagejpeg ( $thumbnail , $thumb_location );
} else {
imagepng ( $thumbnail , $thumb_location );
}
2018-10-19 07:07:40 +00:00
} else { // imagemagick
$discard = '' ;
$exit_status = 1 ;
exec ( " convert $thumb_location video_overlay.png -gravity center -composite -quality 75 $thumb_location " , $discard , $exit_status );
2014-07-05 19:27:18 +00:00
}
}
2011-08-07 07:24:39 +00:00
function strallpos ( $haystack , $needle , $offset = 0 ) {
$result = array ();
2014-06-24 19:51:22 +00:00
for ( $i = $offset ; $i < strlen ( $haystack ); $i ++ ) {
2011-08-07 07:24:39 +00:00
$pos = strpos ( $haystack , $needle , $i );
if ( $pos !== False ) {
$offset = $pos ;
if ( $offset >= $i ) {
$i = $offset ;
$result [] = $offset ;
}
}
}
return $result ;
}
2015-08-07 07:01:18 +00:00
2017-12-14 22:36:57 +00:00
function url_get_contents ( $url ) {
if ( ! function_exists ( 'curl_init' )) {
return file_get_contents ( $url );
}
$ch = curl_init ();
curl_setopt ( $ch , CURLOPT_URL , $url );
curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION , true );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
2020-12-01 01:25:48 +00:00
2017-12-14 22:36:57 +00:00
$output = curl_exec ( $ch );
2020-12-01 01:25:48 +00:00
$responsecode = curl_getinfo ( $ch , CURLINFO_HTTP_CODE );
2017-12-14 22:36:57 +00:00
curl_close ( $ch );
2020-12-01 01:25:48 +00:00
if ( intval ( $responsecode ) != 200 ) {
return '' ;
}
2017-12-14 22:36:57 +00:00
return $output ;
}
2016-09-22 00:47:58 +00:00
function isEmbed ( $file_hex ) {
2016-09-24 03:59:09 +00:00
global $tinyib_embeds ;
return in_array ( $file_hex , array_keys ( $tinyib_embeds ));
2016-09-22 00:47:58 +00:00
}
2015-08-07 07:01:18 +00:00
function getEmbed ( $url ) {
2016-09-24 03:59:09 +00:00
global $tinyib_embeds ;
foreach ( $tinyib_embeds as $service => $service_url ) {
2016-09-22 00:47:58 +00:00
$service_url = str_ireplace ( " TINYIBEMBED " , urlencode ( $url ), $service_url );
2020-12-01 01:25:48 +00:00
$data = url_get_contents ( $service_url );
if ( $data != '' ) {
$result = json_decode ( $data , true );
if ( ! empty ( $result )) {
return array ( $service , $result );
}
2015-08-07 07:01:18 +00:00
}
}
2016-09-22 00:47:58 +00:00
2017-12-14 22:36:57 +00:00
return array ( '' , array ());
2015-08-07 07:01:18 +00:00
}
2017-12-14 22:51:59 +00:00
2020-10-08 18:02:25 +00:00
function attachFile ( $post , $filepath , $filename , $uploaded ) {
global $tinyib_uploads ;
if ( ! is_file ( $filepath ) || ! is_readable ( $filepath )) {
@ unlink ( $filepath );
fancyDie ( __ ( 'File transfer failure. Please retry the submission.' ));
}
$filesize = filesize ( $filepath );
if ( TINYIB_MAXKB > 0 && $filesize > ( TINYIB_MAXKB * 1024 )) {
@ unlink ( $filepath );
fancyDie ( sprintf ( __ ( 'That file is larger than %s.' ), TINYIB_MAXKBDESC ));
}
$post [ 'file_original' ] = trim ( htmlentities ( substr ( $filename , 0 , 50 ), ENT_QUOTES ));
$post [ 'file_hex' ] = md5_file ( $filepath );
$post [ 'file_size' ] = $filesize ;
$post [ 'file_size_formatted' ] = convertBytes ( $post [ 'file_size' ]);
checkDuplicateFile ( $post [ 'file_hex' ]);
$file_mime_split = explode ( ' ' , trim ( mime_content_type ( $filepath )));
if ( count ( $file_mime_split ) > 0 ) {
$file_mime = strtolower ( array_pop ( $file_mime_split ));
} else {
if ( !@ getimagesize ( $filepath )) {
@ unlink ( $filepath );
fancyDie ( __ ( 'Failed to read the MIME type and size of the uploaded file. Please retry the submission.' ));
}
$file_mime = mime_content_type ( $filepath );
}
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 ( $uploaded ) {
if ( ! move_uploaded_file ( $filepath , $file_location )) {
fancyDie ( __ ( 'Could not copy uploaded file.' ));
}
} else {
if ( ! rename ( $filepath , $file_location )) {
@ unlink ( $filepath );
fancyDie ( __ ( 'Could not copy uploaded file.' ));
}
}
if ( filesize ( $file_location ) != $filesize ) {
@ unlink ( $file_location );
fancyDie ( __ ( 'File transfer failure. Please go back and try again.' ));
}
if ( $file_mime == 'audio/webm' || $file_mime == 'video/webm' || $file_mime == 'audio/mp4' || $file_mime == 'video/mp4' ) {
$post [ 'image_width' ] = max ( 0 , intval ( shell_exec ( 'mediainfo --Inform="Video;%Width%" ' . $file_location )));
$post [ 'image_height' ] = max ( 0 , intval ( shell_exec ( 'mediainfo --Inform="Video;%Height%" ' . $file_location )));
if ( $post [ 'image_width' ] > 0 && $post [ 'image_height' ] > 0 ) {
list ( $thumb_maxwidth , $thumb_maxheight ) = thumbnailDimensions ( $post );
$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/' . $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/' . $post [ 'thumb' ]);
fancyDie ( __ ( 'Sorry, your video appears to be corrupt.' ));
}
addVideoOverlay ( 'thumb/' . $post [ 'thumb' ]);
}
$duration = intval ( shell_exec ( 'mediainfo --Inform="General;%Duration%" ' . $file_location ));
if ( $duration > 0 ) {
$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 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' ) {
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 );
if ( ! createThumbnail ( $file_location , 'thumb/' . $post [ 'thumb' ], $thumb_maxwidth , $thumb_maxheight )) {
@ unlink ( $file_location );
fancyDie ( __ ( 'Could not create thumbnail.' ));
}
}
if ( $post [ 'thumb' ] != '' ) {
$thumb_info = getimagesize ( 'thumb/' . $post [ 'thumb' ]);
$post [ 'thumb_width' ] = $thumb_info [ 0 ];
$post [ 'thumb_height' ] = $thumb_info [ 1 ];
}
return $post ;
}
2017-12-14 22:51:59 +00:00
function installedViaGit () {
return is_dir ( '.git' );
}