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.

929 lines
31KB

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