The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

860 lines
29KB

  1. <?php
  2. /*
  3. * Copyright (c) 2010-2014 Tinyboard Development Group
  4. */
  5. require 'inc/functions.php';
  6. require 'inc/anti-bot.php';
  7. // Fix for magic quotes
  8. if (get_magic_quotes_gpc()) {
  9. function strip_array($var) {
  10. return is_array($var) ? array_map('strip_array', $var) : stripslashes($var);
  11. }
  12. $_GET = strip_array($_GET);
  13. $_POST = strip_array($_POST);
  14. }
  15. if (isset($_POST['delete'])) {
  16. // Delete
  17. if (!isset($_POST['board'], $_POST['password']))
  18. error($config['error']['bot']);
  19. $password = &$_POST['password'];
  20. if ($password == '')
  21. error($config['error']['invalidpassword']);
  22. $delete = array();
  23. foreach ($_POST as $post => $value) {
  24. if (preg_match('/^delete_(\d+)$/', $post, $m)) {
  25. $delete[] = (int)$m[1];
  26. }
  27. }
  28. checkDNSBL();
  29. // Check if board exists
  30. if (!openBoard($_POST['board']))
  31. error($config['error']['noboard']);
  32. // Check if banned
  33. checkBan($board['uri']);
  34. if (empty($delete))
  35. error($config['error']['nodelete']);
  36. foreach ($delete as &$id) {
  37. $query = prepare(sprintf("SELECT `thread`, `time`,`password` FROM ``posts_%s`` WHERE `id` = :id", $board['uri']));
  38. $query->bindValue(':id', $id, PDO::PARAM_INT);
  39. $query->execute() or error(db_error($query));
  40. if ($post = $query->fetch(PDO::FETCH_ASSOC)) {
  41. if ($password != '' && $post['password'] != $password)
  42. error($config['error']['invalidpassword']);
  43. if ($post['time'] > time() - $config['delete_time']) {
  44. error(sprintf($config['error']['delete_too_soon'], until($post['time'] + $config['delete_time'])));
  45. }
  46. if (isset($_POST['file'])) {
  47. // Delete just the file
  48. deleteFile($id);
  49. } else {
  50. // Delete entire post
  51. deletePost($id);
  52. }
  53. _syslog(LOG_INFO, 'Deleted post: ' .
  54. '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $id) . ($post['thread'] ? '#' . $id : '')
  55. );
  56. }
  57. }
  58. buildIndex();
  59. rebuildThemes('post-delete', $board['uri']);
  60. $is_mod = isset($_POST['mod']) && $_POST['mod'];
  61. $root = $is_mod ? $config['root'] . $config['file_mod'] . '?/' : $config['root'];
  62. if (!isset($_POST['json_response'])) {
  63. header('Location: ' . $root . $board['dir'] . $config['file_index'], true, $config['redirect_http']);
  64. } else {
  65. header('Content-Type: text/json');
  66. echo json_encode(array('success' => true));
  67. }
  68. } elseif (isset($_POST['report'])) {
  69. if (!isset($_POST['board'], $_POST['password'], $_POST['reason']))
  70. error($config['error']['bot']);
  71. $report = array();
  72. foreach ($_POST as $post => $value) {
  73. if (preg_match('/^delete_(\d+)$/', $post, $m)) {
  74. $report[] = (int)$m[1];
  75. }
  76. }
  77. checkDNSBL();
  78. // Check if board exists
  79. if (!openBoard($_POST['board']))
  80. error($config['error']['noboard']);
  81. // Check if banned
  82. checkBan($board['uri']);
  83. if (empty($report))
  84. error($config['error']['noreport']);
  85. if (count($report) > $config['report_limit'])
  86. error($config['error']['toomanyreports']);
  87. $reason = escape_markup_modifiers($_POST['reason']);
  88. markup($reason);
  89. foreach ($report as &$id) {
  90. $query = prepare(sprintf("SELECT `thread` FROM ``posts_%s`` WHERE `id` = :id", $board['uri']));
  91. $query->bindValue(':id', $id, PDO::PARAM_INT);
  92. $query->execute() or error(db_error($query));
  93. $thread = $query->fetchColumn();
  94. if ($config['syslog'])
  95. _syslog(LOG_INFO, 'Reported post: ' .
  96. '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $thread ? $thread : $id) . ($thread ? '#' . $id : '') .
  97. ' for "' . $reason . '"'
  98. );
  99. $query = prepare("INSERT INTO ``reports`` VALUES (NULL, :time, :ip, :board, :post, :reason)");
  100. $query->bindValue(':time', time(), PDO::PARAM_INT);
  101. $query->bindValue(':ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
  102. $query->bindValue(':board', $board['uri'], PDO::PARAM_INT);
  103. $query->bindValue(':post', $id, PDO::PARAM_INT);
  104. $query->bindValue(':reason', $reason, PDO::PARAM_STR);
  105. $query->execute() or error(db_error($query));
  106. }
  107. $is_mod = isset($_POST['mod']) && $_POST['mod'];
  108. $root = $is_mod ? $config['root'] . $config['file_mod'] . '?/' : $config['root'];
  109. if (!isset($_POST['json_response'])) {
  110. header('Location: ' . $root . $board['dir'] . $config['file_index'], true, $config['redirect_http']);
  111. } else {
  112. header('Content-Type: text/json');
  113. echo json_encode(array('success' => true));
  114. }
  115. } elseif (isset($_POST['post'])) {
  116. if (!isset($_POST['body'], $_POST['board']))
  117. error($config['error']['bot']);
  118. $post = array('board' => $_POST['board']);
  119. // Check if board exists
  120. if (!openBoard($post['board']))
  121. error($config['error']['noboard']);
  122. if (!isset($_POST['name']))
  123. $_POST['name'] = $config['anonymous'];
  124. if (!isset($_POST['email']))
  125. $_POST['email'] = '';
  126. if (!isset($_POST['subject']))
  127. $_POST['subject'] = '';
  128. if (!isset($_POST['password']))
  129. $_POST['password'] = '';
  130. if (isset($_POST['thread'])) {
  131. $post['op'] = false;
  132. $post['thread'] = round($_POST['thread']);
  133. } elseif ($config['quick_reply'] && isset($_POST['quick-reply'])) {
  134. $post['op'] = false;
  135. $post['thread'] = round($_POST['quick-reply']);
  136. } else
  137. $post['op'] = true;
  138. if (!(($post['op'] && $_POST['post'] == $config['button_newtopic']) ||
  139. (!$post['op'] && $_POST['post'] == $config['button_reply'])))
  140. error($config['error']['bot']);
  141. // Check the referrer
  142. if ($config['referer_match'] !== false &&
  143. (!isset($_SERVER['HTTP_REFERER']) || !preg_match($config['referer_match'], rawurldecode($_SERVER['HTTP_REFERER']))))
  144. error($config['error']['referer']);
  145. checkDNSBL();
  146. // Check if banned
  147. checkBan($board['uri']);
  148. // Check for CAPTCHA right after opening the board so the "return" link is in there
  149. if ($config['recaptcha']) {
  150. if (!isset($_POST['recaptcha_challenge_field']) || !isset($_POST['recaptcha_response_field']))
  151. error($config['error']['bot']);
  152. // Check what reCAPTCHA has to say...
  153. $resp = recaptcha_check_answer($config['recaptcha_private'],
  154. $_SERVER['REMOTE_ADDR'],
  155. $_POST['recaptcha_challenge_field'],
  156. $_POST['recaptcha_response_field']);
  157. if (!$resp->is_valid) {
  158. error($config['error']['captcha']);
  159. }
  160. }
  161. if ($post['mod'] = isset($_POST['mod']) && $_POST['mod']) {
  162. require 'inc/mod/auth.php';
  163. if (!$mod) {
  164. // Liar. You're not a mod.
  165. error($config['error']['notamod']);
  166. }
  167. $post['sticky'] = $post['op'] && isset($_POST['sticky']);
  168. $post['locked'] = $post['op'] && isset($_POST['lock']);
  169. $post['raw'] = isset($_POST['raw']);
  170. if ($post['sticky'] && !hasPermission($config['mod']['sticky'], $board['uri']))
  171. error($config['error']['noaccess']);
  172. if ($post['locked'] && !hasPermission($config['mod']['lock'], $board['uri']))
  173. error($config['error']['noaccess']);
  174. if ($post['raw'] && !hasPermission($config['mod']['rawhtml'], $board['uri']))
  175. error($config['error']['noaccess']);
  176. }
  177. if (!$post['mod']) {
  178. $post['antispam_hash'] = checkSpam(array($board['uri'], isset($post['thread']) && !($config['quick_reply'] && isset($_POST['quick-reply'])) ? $post['thread'] : ($config['try_smarter'] && isset($_POST['page']) ? 0 - (int)$_POST['page'] : null)));
  179. if ($post['antispam_hash'] === true)
  180. error($config['error']['spam']);
  181. }
  182. if ($config['robot_enable'] && $config['robot_mute']) {
  183. checkMute();
  184. }
  185. //Check if thread exists
  186. if (!$post['op']) {
  187. $query = prepare(sprintf("SELECT `sticky`,`locked`,`sage` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri']));
  188. $query->bindValue(':id', $post['thread'], PDO::PARAM_INT);
  189. $query->execute() or error(db_error());
  190. if (!$thread = $query->fetch(PDO::FETCH_ASSOC)) {
  191. // Non-existant
  192. error($config['error']['nonexistant']);
  193. }
  194. }
  195. // Check for an embed field
  196. if ($config['enable_embedding'] && isset($_POST['embed']) && !empty($_POST['embed'])) {
  197. // yep; validate it
  198. $value = $_POST['embed'];
  199. foreach ($config['embedding'] as &$embed) {
  200. if (preg_match($embed[0], $value)) {
  201. // Valid link
  202. $post['embed'] = $value;
  203. // This is bad, lol.
  204. $post['no_longer_require_an_image_for_op'] = true;
  205. break;
  206. }
  207. }
  208. if (!isset($post['embed'])) {
  209. error($config['error']['invalid_embed']);
  210. }
  211. }
  212. if (!hasPermission($config['mod']['bypass_field_disable'], $board['uri'])) {
  213. if ($config['field_disable_name'])
  214. $_POST['name'] = $config['anonymous']; // "forced anonymous"
  215. if ($config['field_disable_email'])
  216. $_POST['email'] = '';
  217. if ($config['field_disable_password'])
  218. $_POST['password'] = '';
  219. if ($config['field_disable_subject'] || (!$post['op'] && $config['field_disable_reply_subject']))
  220. $_POST['subject'] = '';
  221. }
  222. if ($config['allow_upload_by_url'] && isset($_POST['file_url']) && !empty($_POST['file_url'])) {
  223. $post['file_url'] = $_POST['file_url'];
  224. if (!preg_match('@^https?://@', $post['file_url']))
  225. error($config['error']['invalidimg']);
  226. if (mb_strpos($post['file_url'], '?') !== false)
  227. $url_without_params = mb_substr($post['file_url'], 0, mb_strpos($post['file_url'], '?'));
  228. else
  229. $url_without_params = $post['file_url'];
  230. $post['extension'] = strtolower(mb_substr($url_without_params, mb_strrpos($url_without_params, '.') + 1));
  231. if (!in_array($post['extension'], $config['allowed_ext']) && !in_array($post['extension'], $config['allowed_ext_files']))
  232. error($config['error']['unknownext']);
  233. $post['file_tmp'] = tempnam($config['tmp'], 'url');
  234. function unlink_tmp_file($file) {
  235. @unlink($file);
  236. fatal_error_handler();
  237. }
  238. register_shutdown_function('unlink_tmp_file', $post['file_tmp']);
  239. $fp = fopen($post['file_tmp'], 'w');
  240. $curl = curl_init();
  241. curl_setopt($curl, CURLOPT_URL, $post['file_url']);
  242. curl_setopt($curl, CURLOPT_FAILONERROR, true);
  243. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
  244. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
  245. curl_setopt($curl, CURLOPT_TIMEOUT, $config['upload_by_url_timeout']);
  246. curl_setopt($curl, CURLOPT_USERAGENT, 'Tinyboard');
  247. curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
  248. curl_setopt($curl, CURLOPT_FILE, $fp);
  249. curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
  250. if (curl_exec($curl) === false)
  251. error($config['error']['nomove']);
  252. curl_close($curl);
  253. fclose($fp);
  254. $_FILES['file'] = array(
  255. 'name' => basename($url_without_params),
  256. 'tmp_name' => $post['file_tmp'],
  257. 'error' => 0,
  258. 'size' => filesize($post['file_tmp'])
  259. );
  260. }
  261. // Check for a file
  262. if ($post['op'] && !isset($post['no_longer_require_an_image_for_op'])) {
  263. if (!isset($_FILES['file']['tmp_name']) || $_FILES['file']['tmp_name'] == '' && $config['force_image_op'])
  264. error($config['error']['noimage']);
  265. }
  266. $post['name'] = $_POST['name'] != '' ? $_POST['name'] : $config['anonymous'];
  267. $post['subject'] = $_POST['subject'];
  268. $post['email'] = str_replace(' ', '%20', htmlspecialchars($_POST['email']));
  269. $post['body'] = $_POST['body'];
  270. $post['password'] = $_POST['password'];
  271. $post['has_file'] = !isset($post['embed']) && (($post['op'] && !isset($post['no_longer_require_an_image_for_op']) && $config['force_image_op']) || (isset($_FILES['file']) && $_FILES['file']['tmp_name'] != ''));
  272. if ($post['has_file'])
  273. $post['filename'] = urldecode(get_magic_quotes_gpc() ? stripslashes($_FILES['file']['name']) : $_FILES['file']['name']);
  274. if (!($post['has_file'] || isset($post['embed'])) || (($post['op'] && $config['force_body_op']) || (!$post['op'] && $config['force_body']))) {
  275. $stripped_whitespace = preg_replace('/[\s]/u', '', $post['body']);
  276. if ($stripped_whitespace == '') {
  277. error($config['error']['tooshort_body']);
  278. }
  279. }
  280. if (!$post['op']) {
  281. // Check if thread is locked
  282. // but allow mods to post
  283. if ($thread['locked'] && !hasPermission($config['mod']['postinlocked'], $board['uri']))
  284. error($config['error']['locked']);
  285. $numposts = numPosts($post['thread']);
  286. if ($config['reply_hard_limit'] != 0 && $config['reply_hard_limit'] <= $numposts['replies'])
  287. error($config['error']['reply_hard_limit']);
  288. if ($post['has_file'] && $config['image_hard_limit'] != 0 && $config['image_hard_limit'] <= $numposts['images'])
  289. error($config['error']['image_hard_limit']);
  290. }
  291. if ($post['has_file']) {
  292. $size = $_FILES['file']['size'];
  293. if ($size > $config['max_filesize'])
  294. error(sprintf3($config['error']['filesize'], array(
  295. 'sz' => number_format($size),
  296. 'filesz' => number_format($size),
  297. 'maxsz' => number_format($config['max_filesize'])
  298. )));
  299. }
  300. $post['capcode'] = false;
  301. if ($mod && preg_match('/^((.+) )?## (.+)$/', $post['name'], $matches)) {
  302. $name = $matches[2] != '' ? $matches[2] : $config['anonymous'];
  303. $cap = $matches[3];
  304. if (isset($config['mod']['capcode'][$mod['type']])) {
  305. if ( $config['mod']['capcode'][$mod['type']] === true ||
  306. (is_array($config['mod']['capcode'][$mod['type']]) &&
  307. in_array($cap, $config['mod']['capcode'][$mod['type']])
  308. )) {
  309. $post['capcode'] = utf8tohtml($cap);
  310. $post['name'] = $name;
  311. }
  312. }
  313. }
  314. $trip = generate_tripcode($post['name']);
  315. $post['name'] = $trip[0];
  316. $post['trip'] = isset($trip[1]) ? $trip[1] : '';
  317. $noko = false;
  318. if (strtolower($post['email']) == 'noko') {
  319. $noko = true;
  320. $post['email'] = '';
  321. } elseif (strtolower($post['email']) == 'nonoko'){
  322. $noko = false;
  323. $post['email'] = '';
  324. } else $noko = $config['always_noko'];
  325. if ($post['has_file']) {
  326. $post['extension'] = strtolower(mb_substr($post['filename'], mb_strrpos($post['filename'], '.') + 1));
  327. if (isset($config['filename_func']))
  328. $post['file_id'] = $config['filename_func']($post);
  329. else
  330. $post['file_id'] = time() . substr(microtime(), 2, 3);
  331. $post['file'] = $board['dir'] . $config['dir']['img'] . $post['file_id'] . '.' . $post['extension'];
  332. $post['thumb'] = $board['dir'] . $config['dir']['thumb'] . $post['file_id'] . '.' . ($config['thumb_ext'] ? $config['thumb_ext'] : $post['extension']);
  333. }
  334. if ($config['strip_combining_chars']) {
  335. $post['name'] = strip_combining_chars($post['name']);
  336. $post['email'] = strip_combining_chars($post['email']);
  337. $post['subject'] = strip_combining_chars($post['subject']);
  338. $post['body'] = strip_combining_chars($post['body']);
  339. }
  340. // Check string lengths
  341. if (mb_strlen($post['name']) > 35)
  342. error(sprintf($config['error']['toolong'], 'name'));
  343. if (mb_strlen($post['email']) > 40)
  344. error(sprintf($config['error']['toolong'], 'email'));
  345. if (mb_strlen($post['subject']) > 100)
  346. error(sprintf($config['error']['toolong'], 'subject'));
  347. if (!$mod && mb_strlen($post['body']) > $config['max_body'])
  348. error($config['error']['toolong_body']);
  349. if (mb_strlen($post['password']) > 20)
  350. error(sprintf($config['error']['toolong'], 'password'));
  351. wordfilters($post['body']);
  352. $post['body'] = escape_markup_modifiers($post['body']);
  353. if ($mod && isset($post['raw']) && $post['raw']) {
  354. $post['body'] .= "\n<tinyboard raw html>1</tinyboard>";
  355. }
  356. if ($config['country_flags']) {
  357. require 'inc/lib/geoip/geoip.inc';
  358. $gi=geoip\geoip_open('inc/lib/geoip/GeoIPv6.dat', GEOIP_STANDARD);
  359. function ipv4to6($ip) {
  360. if (strpos($ip, ':') !== false) {
  361. if (strpos($ip, '.') > 0)
  362. $ip = substr($ip, strrpos($ip, ':')+1);
  363. else return $ip; //native ipv6
  364. }
  365. $iparr = array_pad(explode('.', $ip), 4, 0);
  366. $part7 = base_convert(($iparr[0] * 256) + $iparr[1], 10, 16);
  367. $part8 = base_convert(($iparr[2] * 256) + $iparr[3], 10, 16);
  368. return '::ffff:'.$part7.':'.$part8;
  369. }
  370. if ($country_code = geoip\geoip_country_code_by_addr_v6($gi, ipv4to6($_SERVER['REMOTE_ADDR']))) {
  371. if (!in_array(strtolower($country_code), array('eu', 'ap', 'o1', 'a1', 'a2')))
  372. $post['body'] .= "\n<tinyboard flag>".strtolower($country_code)."</tinyboard>".
  373. "\n<tinyboard flag alt>".geoip\geoip_country_name_by_addr_v6($gi, ipv4to6($_SERVER['REMOTE_ADDR']))."</tinyboard>";
  374. }
  375. }
  376. if ($config['user_flag'] && isset($_POST['user_flag']))
  377. if (!empty($_POST['user_flag']) ){
  378. $user_flag = $_POST['user_flag'];
  379. if (!isset($config['user_flags'][$user_flag]))
  380. error('Invalid flag selection!');
  381. $flag_alt = isset($user_flag_alt) ? $user_flag_alt : $config['user_flags'][$user_flag];
  382. $post['body'] .= "\n<tinyboard flag>" . strtolower($user_flag) . "</tinyboard>" .
  383. "\n<tinyboard flag alt>" . $flag_alt . "</tinyboard>";
  384. }
  385. if (mysql_version() >= 50503) {
  386. $post['body_nomarkup'] = $post['body']; // Assume we're using the utf8mb4 charset
  387. } else {
  388. // MySQL's `utf8` charset only supports up to 3-byte symbols
  389. // Remove anything >= 0x010000
  390. $chars = preg_split('//u', $post['body'], -1, PREG_SPLIT_NO_EMPTY);
  391. $post['body_nomarkup'] = '';
  392. foreach ($chars as $char) {
  393. $o = 0;
  394. $ord = ordutf8($char, $o);
  395. if ($ord >= 0x010000)
  396. continue;
  397. $post['body_nomarkup'] .= $char;
  398. }
  399. }
  400. $post['tracked_cites'] = markup($post['body'], true);
  401. if ($post['has_file']) {
  402. if (!in_array($post['extension'], $config['allowed_ext']) && !in_array($post['extension'], $config['allowed_ext_files']))
  403. error($config['error']['unknownext']);
  404. $is_an_image = !in_array($post['extension'], $config['allowed_ext_files']);
  405. // Truncate filename if it is too long
  406. $post['filename'] = mb_substr($post['filename'], 0, $config['max_filename_len']);
  407. $upload = $_FILES['file']['tmp_name'];
  408. if (!is_readable($upload))
  409. error($config['error']['nomove']);
  410. $post['filehash'] = md5_file($upload);
  411. $post['filesize'] = filesize($upload);
  412. }
  413. if (!hasPermission($config['mod']['bypass_filters'], $board['uri'])) {
  414. require_once 'inc/filters.php';
  415. do_filters($post);
  416. }
  417. if ($post['has_file']) {
  418. if ($is_an_image && $config['ie_mime_type_detection'] !== false) {
  419. // Check IE MIME type detection XSS exploit
  420. $buffer = file_get_contents($upload, null, null, null, 255);
  421. if (preg_match($config['ie_mime_type_detection'], $buffer)) {
  422. undoImage($post);
  423. error($config['error']['mime_exploit']);
  424. }
  425. require_once 'inc/image.php';
  426. // find dimensions of an image using GD
  427. if (!$size = @getimagesize($upload)) {
  428. error($config['error']['invalidimg']);
  429. }
  430. if ($size[0] > $config['max_width'] || $size[1] > $config['max_height']) {
  431. error($config['error']['maxsize']);
  432. }
  433. if ($config['convert_auto_orient'] && ($post['extension'] == 'jpg' || $post['extension'] == 'jpeg')) {
  434. // The following code corrects the image orientation.
  435. // Currently only works with the 'convert' option selected but it could easily be expanded to work with the rest if you can be bothered.
  436. if (!($config['redraw_image'] || (($config['strip_exif'] && !$config['use_exiftool']) && ($post['extension'] == 'jpg' || $post['extension'] == 'jpeg')))) {
  437. if (in_array($config['thumb_method'], array('convert', 'convert+gifsicle', 'gm', 'gm+gifsicle'))) {
  438. $exif = @exif_read_data($upload);
  439. $gm = in_array($config['thumb_method'], array('gm', 'gm+gifsicle'));
  440. if (isset($exif['Orientation']) && $exif['Orientation'] != 1) {
  441. if ($config['convert_manual_orient']) {
  442. $error = shell_exec_error(($gm ? 'gm ' : '') . 'convert ' .
  443. escapeshellarg($upload) . ' ' .
  444. ImageConvert::jpeg_exif_orientation(false, $exif) . ' ' .
  445. ($config['strip_exif'] ? '+profile "*"' :
  446. ($config['use_exiftool'] ? '' : '+profile "*"')
  447. ) . ' ' .
  448. escapeshellarg($upload));
  449. if ($config['use_exiftool'] && !$config['strip_exif']) {
  450. if ($exiftool_error = shell_exec_error(
  451. 'exiftool -overwrite_original -q -q -orientation=1 -n ' .
  452. escapeshellarg($upload)))
  453. error('exiftool failed!', null, $exiftool_error);
  454. } else {
  455. // TODO: Find another way to remove the Orientation tag from the EXIF profile
  456. // without needing `exiftool`.
  457. }
  458. } else {
  459. $error = shell_exec_error(($gm ? 'gm ' : '') . 'convert ' .
  460. escapeshellarg($upload) . ' -auto-orient ' . escapeshellarg($upload));
  461. }
  462. if ($error)
  463. error('Could not auto-orient image!', null, $error);
  464. $size = @getimagesize($upload);
  465. if ($config['strip_exif'])
  466. $post['exif_stripped'] = true;
  467. }
  468. }
  469. }
  470. }
  471. // create image object
  472. $image = new Image($upload, $post['extension'], $size);
  473. if ($image->size->width > $config['max_width'] || $image->size->height > $config['max_height']) {
  474. $image->delete();
  475. error($config['error']['maxsize']);
  476. }
  477. $post['width'] = $image->size->width;
  478. $post['height'] = $image->size->height;
  479. if ($config['spoiler_images'] && isset($_POST['spoiler'])) {
  480. $post['thumb'] = 'spoiler';
  481. $size = @getimagesize($config['spoiler_image']);
  482. $post['thumbwidth'] = $size[0];
  483. $post['thumbheight'] = $size[1];
  484. } elseif ($config['minimum_copy_resize'] &&
  485. $image->size->width <= $config['thumb_width'] &&
  486. $image->size->height <= $config['thumb_height'] &&
  487. $post['extension'] == ($config['thumb_ext'] ? $config['thumb_ext'] : $post['extension'])) {
  488. // Copy, because there's nothing to resize
  489. copy($upload, $post['thumb']);
  490. $post['thumbwidth'] = $image->size->width;
  491. $post['thumbheight'] = $image->size->height;
  492. } else {
  493. $thumb = $image->resize(
  494. $config['thumb_ext'] ? $config['thumb_ext'] : $post['extension'],
  495. $post['op'] ? $config['thumb_op_width'] : $config['thumb_width'],
  496. $post['op'] ? $config['thumb_op_height'] : $config['thumb_height']
  497. );
  498. $thumb->to($post['thumb']);
  499. $post['thumbwidth'] = $thumb->width;
  500. $post['thumbheight'] = $thumb->height;
  501. $thumb->_destroy();
  502. }
  503. if ($config['redraw_image'] || (!@$post['exif_stripped'] && $config['strip_exif'] && ($post['extension'] == 'jpg' || $post['extension'] == 'jpeg'))) {
  504. if (!$config['redraw_image'] && $config['use_exiftool']) {
  505. if($error = shell_exec_error('exiftool -overwrite_original -ignoreMinorErrors -q -q -all= ' .
  506. escapeshellarg($upload)))
  507. error('Could not strip EXIF metadata!', null, $error);
  508. } else {
  509. $image->to($post['file']);
  510. $dont_copy_file = true;
  511. }
  512. }
  513. $image->destroy();
  514. } else {
  515. // not an image
  516. //copy($config['file_thumb'], $post['thumb']);
  517. $post['thumb'] = 'file';
  518. $size = @getimagesize(sprintf($config['file_thumb'],
  519. isset($config['file_icons'][$post['extension']]) ?
  520. $config['file_icons'][$post['extension']] : $config['file_icons']['default']));
  521. $post['thumbwidth'] = $size[0];
  522. $post['thumbheight'] = $size[1];
  523. }
  524. if (!isset($dont_copy_file) || !$dont_copy_file) {
  525. if (isset($post['file_tmp'])) {
  526. if (!@rename($upload, $post['file']))
  527. error($config['error']['nomove']);
  528. chmod($post['file'], 0644);
  529. } elseif (!@move_uploaded_file($upload, $post['file']))
  530. error($config['error']['nomove']);
  531. }
  532. }
  533. if ($post['has_file']) {
  534. if ($config['image_reject_repost']) {
  535. if ($p = getPostByHash($post['filehash'])) {
  536. undoImage($post);
  537. error(sprintf($config['error']['fileexists'],
  538. ($post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root']) .
  539. ($board['dir'] . $config['dir']['res'] .
  540. ($p['thread'] ?
  541. $p['thread'] . '.html#' . $p['id']
  542. :
  543. $p['id'] . '.html'
  544. ))
  545. ));
  546. }
  547. } else if (!$post['op'] && $config['image_reject_repost_in_thread']) {
  548. if ($p = getPostByHashInThread($post['filehash'], $post['thread'])) {
  549. undoImage($post);
  550. error(sprintf($config['error']['fileexistsinthread'],
  551. ($post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root']) .
  552. ($board['dir'] . $config['dir']['res'] .
  553. ($p['thread'] ?
  554. $p['thread'] . '.html#' . $p['id']
  555. :
  556. $p['id'] . '.html'
  557. ))
  558. ));
  559. }
  560. }
  561. }
  562. if (!hasPermission($config['mod']['postunoriginal'], $board['uri']) && $config['robot_enable'] && checkRobot($post['body_nomarkup'])) {
  563. undoImage($post);
  564. if ($config['robot_mute']) {
  565. error(sprintf($config['error']['muted'], mute()));
  566. } else {
  567. error($config['error']['unoriginal']);
  568. }
  569. }
  570. // Remove board directories before inserting them into the database.
  571. if ($post['has_file']) {
  572. $post['file_path'] = $post['file'];
  573. $post['thumb_path'] = $post['thumb'];
  574. $post['file'] = mb_substr($post['file'], mb_strlen($board['dir'] . $config['dir']['img']));
  575. if ($is_an_image && $post['thumb'] != 'spoiler')
  576. $post['thumb'] = mb_substr($post['thumb'], mb_strlen($board['dir'] . $config['dir']['thumb']));
  577. }
  578. $post = (object)$post;
  579. if ($error = event('post', $post)) {
  580. undoImage((array)$post);
  581. error($error);
  582. }
  583. $post = (array)$post;
  584. $post['id'] = $id = post($post);
  585. insertFloodPost($post);
  586. if (isset($post['antispam_hash'])) {
  587. incrementSpamHash($post['antispam_hash']);
  588. }
  589. if (isset($post['tracked_cites']) && !empty($post['tracked_cites'])) {
  590. $insert_rows = array();
  591. foreach ($post['tracked_cites'] as $cite) {
  592. $insert_rows[] = '(' .
  593. $pdo->quote($board['uri']) . ', ' . (int)$id . ', ' .
  594. $pdo->quote($cite[0]) . ', ' . (int)$cite[1] . ')';
  595. }
  596. query('INSERT INTO ``cites`` VALUES ' . implode(', ', $insert_rows)) or error(db_error());
  597. }
  598. if (!$post['op'] && strtolower($post['email']) != 'sage' && !$thread['sage'] && ($config['reply_limit'] == 0 || $numposts['replies']+1 < $config['reply_limit'])) {
  599. bumpThread($post['thread']);
  600. }
  601. buildThread($post['op'] ? $id : $post['thread']);
  602. if ($config['try_smarter'] && $post['op'])
  603. $build_pages = range(1, $config['max_pages']);
  604. if ($post['op'])
  605. clean();
  606. event('post-after', $post);
  607. buildIndex();
  608. if (isset($_SERVER['HTTP_REFERER'])) {
  609. // Tell Javascript that we posted successfully
  610. if (isset($_COOKIE[$config['cookies']['js']]))
  611. $js = json_decode($_COOKIE[$config['cookies']['js']]);
  612. else
  613. $js = (object) array();
  614. // Tell it to delete the cached post for referer
  615. $js->{$_SERVER['HTTP_REFERER']} = true;
  616. // Encode and set cookie
  617. setcookie($config['cookies']['js'], json_encode($js), 0, $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, false, false);
  618. }
  619. $root = $post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root'];
  620. if ($noko) {
  621. $redirect = $root . $board['dir'] . $config['dir']['res'] .
  622. sprintf($config['file_page'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : '');
  623. if (!$post['op'] && isset($_SERVER['HTTP_REFERER'])) {
  624. $regex = array(
  625. 'board' => str_replace('%s', '(\w{1,8})', preg_quote($config['board_path'], '/')),
  626. 'page' => str_replace('%d', '(\d+)', preg_quote($config['file_page'], '/')),
  627. 'page50' => str_replace('%d', '(\d+)', preg_quote($config['file_page50'], '/')),
  628. 'res' => preg_quote($config['dir']['res'], '/'),
  629. );
  630. if (preg_match('/\/' . $regex['board'] . $regex['res'] . $regex['page50'] . '([?&].*)?$/', $_SERVER['HTTP_REFERER'])) {
  631. $redirect = $root . $board['dir'] . $config['dir']['res'] .
  632. sprintf($config['file_page50'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : '');
  633. }
  634. }
  635. } else {
  636. $redirect = $root . $board['dir'] . $config['file_index'];
  637. }
  638. if ($config['syslog'])
  639. _syslog(LOG_INFO, 'New post: /' . $board['dir'] . $config['dir']['res'] .
  640. sprintf($config['file_page'], $post['op'] ? $id : $post['thread']) . (!$post['op'] ? '#' . $id : ''));
  641. if (!$post['mod']) header('X-Associated-Content: "' . $redirect . '"');
  642. if ($post['op'])
  643. rebuildThemes('post-thread', $board['uri']);
  644. else
  645. rebuildThemes('post', $board['uri']);
  646. if (!isset($_POST['json_response'])) {
  647. header('Location: ' . $redirect, true, $config['redirect_http']);
  648. } else {
  649. header('Content-Type: text/json; charset=utf-8');
  650. echo json_encode(array(
  651. 'redirect' => $redirect,
  652. 'noko' => $noko,
  653. 'id' => $id
  654. ));
  655. }
  656. } elseif (isset($_POST['appeal'])) {
  657. if (!isset($_POST['ban_id']))
  658. error($config['error']['bot']);
  659. $ban_id = (int)$_POST['ban_id'];
  660. $bans = Bans::find($_SERVER['REMOTE_ADDR']);
  661. foreach ($bans as $_ban) {
  662. if ($_ban['id'] == $ban_id) {
  663. $ban = $_ban;
  664. break;
  665. }
  666. }
  667. if (!isset($ban)) {
  668. error(_("That ban doesn't exist or is not for you."));
  669. }
  670. if ($ban['expires'] && $ban['expires'] - $ban['created'] <= $config['ban_appeals_min_length']) {
  671. error(_("You cannot appeal a ban of this length."));
  672. }
  673. $query = query("SELECT `denied` FROM ``ban_appeals`` WHERE `ban_id` = $ban_id") or error(db_error());
  674. $ban_appeals = $query->fetchAll(PDO::FETCH_COLUMN);
  675. if (count($ban_appeals) >= $config['ban_appeals_max']) {
  676. error(_("You cannot appeal this ban again."));
  677. }
  678. foreach ($ban_appeals as $is_denied) {
  679. if (!$is_denied)
  680. error(_("There is already a pending appeal for this ban."));
  681. }
  682. $query = prepare("INSERT INTO ``ban_appeals`` VALUES (NULL, :ban_id, :time, :message, 0)");
  683. $query->bindValue(':ban_id', $ban_id, PDO::PARAM_INT);
  684. $query->bindValue(':time', time(), PDO::PARAM_INT);
  685. $query->bindValue(':message', $_POST['appeal']);
  686. $query->execute() or error(db_error($query));
  687. displayBan($ban);
  688. } else {
  689. if (!file_exists($config['has_installed'])) {
  690. header('Location: install.php', true, $config['redirect_http']);
  691. } else {
  692. // They opened post.php in their browser manually.
  693. error($config['error']['nopost']);
  694. }
  695. }