The version of vichan running on lainchan.org
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

930 строки
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'] && !$config['allow_no_country']) || ($config['country_flags'] && $config['allow_no_country'] && !isset($_POST['no_country']))) {
  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. $md5cmd = $config['bsd_md5'] ? 'md5 -r' : 'md5sum';
  453. if ($output = shell_exec_error("cat $filenames | $md5cmd")) {
  454. $explodedvar = explode(' ', $output);
  455. $hash = $explodedvar[0];
  456. $post['filehash'] = $hash;
  457. } elseif ($config['max_images'] === 1) {
  458. $post['filehash'] = md5_file($upload);
  459. } else {
  460. $str_to_hash = '';
  461. foreach (explode(' ', $filenames) as $i => $f) {
  462. $str_to_hash .= file_get_contents($f);
  463. }
  464. $post['filehash'] = md5($str_to_hash);
  465. }
  466. }
  467. if (!hasPermission($config['mod']['bypass_filters'], $board['uri'])) {
  468. require_once 'inc/filters.php';
  469. do_filters($post);
  470. }
  471. if ($post['has_file']) {
  472. foreach ($post['files'] as $key => &$file) {
  473. if ($file['is_an_image'] && $config['ie_mime_type_detection'] !== false) {
  474. // Check IE MIME type detection XSS exploit
  475. $buffer = file_get_contents($upload, null, null, null, 255);
  476. if (preg_match($config['ie_mime_type_detection'], $buffer)) {
  477. undoImage($post);
  478. error($config['error']['mime_exploit']);
  479. }
  480. require_once 'inc/image.php';
  481. // find dimensions of an image using GD
  482. if (!$size = @getimagesize($file['tmp_name'])) {
  483. error($config['error']['invalidimg']);
  484. }
  485. if ($size[0] > $config['max_width'] || $size[1] > $config['max_height']) {
  486. error($config['error']['maxsize']);
  487. }
  488. if ($config['convert_auto_orient'] && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg')) {
  489. // The following code corrects the image orientation.
  490. // Currently only works with the 'convert' option selected but it could easily be expanded to work with the rest if you can be bothered.
  491. if (!($config['redraw_image'] || (($config['strip_exif'] && !$config['use_exiftool']) && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg')))) {
  492. if (in_array($config['thumb_method'], array('convert', 'convert+gifsicle', 'gm', 'gm+gifsicle'))) {
  493. $exif = @exif_read_data($file['tmp_name']);
  494. $gm = in_array($config['thumb_method'], array('gm', 'gm+gifsicle'));
  495. if (isset($exif['Orientation']) && $exif['Orientation'] != 1) {
  496. if ($config['convert_manual_orient']) {
  497. $error = shell_exec_error(($gm ? 'gm ' : '') . 'convert ' .
  498. escapeshellarg($file['tmp_name']) . ' ' .
  499. ImageConvert::jpeg_exif_orientation(false, $exif) . ' ' .
  500. ($config['strip_exif'] ? '+profile "*"' :
  501. ($config['use_exiftool'] ? '' : '+profile "*"')
  502. ) . ' ' .
  503. escapeshellarg($file['tmp_name']));
  504. if ($config['use_exiftool'] && !$config['strip_exif']) {
  505. if ($exiftool_error = shell_exec_error(
  506. 'exiftool -overwrite_original -q -q -orientation=1 -n ' .
  507. escapeshellarg($file['tmp_name'])))
  508. error(_('exiftool failed!'), null, $exiftool_error);
  509. } else {
  510. // TODO: Find another way to remove the Orientation tag from the EXIF profile
  511. // without needing `exiftool`.
  512. }
  513. } else {
  514. $error = shell_exec_error(($gm ? 'gm ' : '') . 'convert ' .
  515. escapeshellarg($file['tmp_name']) . ' -auto-orient ' . escapeshellarg($upload));
  516. }
  517. if ($error)
  518. error(_('Could not auto-orient image!'), null, $error);
  519. $size = @getimagesize($file['tmp_name']);
  520. if ($config['strip_exif'])
  521. $file['exif_stripped'] = true;
  522. }
  523. }
  524. }
  525. }
  526. // create image object
  527. $image = new Image($file['tmp_name'], $file['extension'], $size);
  528. if ($image->size->width > $config['max_width'] || $image->size->height > $config['max_height']) {
  529. $image->delete();
  530. error($config['error']['maxsize']);
  531. }
  532. $file['width'] = $image->size->width;
  533. $file['height'] = $image->size->height;
  534. if ($config['spoiler_images'] && isset($_POST['spoiler'])) {
  535. $file['thumb'] = 'spoiler';
  536. $size = @getimagesize($config['spoiler_image']);
  537. $file['thumbwidth'] = $size[0];
  538. $file['thumbheight'] = $size[1];
  539. } elseif ($config['minimum_copy_resize'] &&
  540. $image->size->width <= $config['thumb_width'] &&
  541. $image->size->height <= $config['thumb_height'] &&
  542. $file['extension'] == ($config['thumb_ext'] ? $config['thumb_ext'] : $file['extension'])) {
  543. // Copy, because there's nothing to resize
  544. copy($file['tmp_name'], $file['thumb']);
  545. $file['thumbwidth'] = $image->size->width;
  546. $file['thumbheight'] = $image->size->height;
  547. } else {
  548. $thumb = $image->resize(
  549. $config['thumb_ext'] ? $config['thumb_ext'] : $file['extension'],
  550. $post['op'] ? $config['thumb_op_width'] : $config['thumb_width'],
  551. $post['op'] ? $config['thumb_op_height'] : $config['thumb_height']
  552. );
  553. $thumb->to($file['thumb']);
  554. $file['thumbwidth'] = $thumb->width;
  555. $file['thumbheight'] = $thumb->height;
  556. $thumb->_destroy();
  557. }
  558. if ($config['redraw_image'] || (!@$file['exif_stripped'] && $config['strip_exif'] && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg'))) {
  559. if (!$config['redraw_image'] && $config['use_exiftool']) {
  560. if($error = shell_exec_error('exiftool -overwrite_original -ignoreMinorErrors -q -q -all= ' .
  561. escapeshellarg($file['tmp_name'])))
  562. error(_('Could not strip EXIF metadata!'), null, $error);
  563. } else {
  564. $image->to($file['file']);
  565. $dont_copy_file = true;
  566. }
  567. }
  568. $image->destroy();
  569. } else {
  570. // not an image
  571. //copy($config['file_thumb'], $post['thumb']);
  572. $file['thumb'] = 'file';
  573. $size = @getimagesize(sprintf($config['file_thumb'],
  574. isset($config['file_icons'][$file['extension']]) ?
  575. $config['file_icons'][$file['extension']] : $config['file_icons']['default']));
  576. $file['thumbwidth'] = $size[0];
  577. $file['thumbheight'] = $size[1];
  578. }
  579. if (!isset($dont_copy_file) || !$dont_copy_file) {
  580. if (isset($file['file_tmp'])) {
  581. if (!@rename($file['tmp_name'], $file['file']))
  582. error($config['error']['nomove']);
  583. chmod($file['file'], 0644);
  584. } elseif (!@move_uploaded_file($file['tmp_name'], $file['file']))
  585. error($config['error']['nomove']);
  586. }
  587. }
  588. if ($config['image_reject_repost']) {
  589. if ($p = getPostByHash($post['filehash'])) {
  590. undoImage($post);
  591. error(sprintf($config['error']['fileexists'],
  592. ($post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root']) .
  593. ($board['dir'] . $config['dir']['res'] .
  594. ($p['thread'] ?
  595. $p['thread'] . '.html#' . $p['id']
  596. :
  597. $p['id'] . '.html'
  598. ))
  599. ));
  600. }
  601. } else if (!$post['op'] && $config['image_reject_repost_in_thread']) {
  602. if ($p = getPostByHashInThread($post['filehash'], $post['thread'])) {
  603. undoImage($post);
  604. error(sprintf($config['error']['fileexistsinthread'],
  605. ($post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root']) .
  606. ($board['dir'] . $config['dir']['res'] .
  607. ($p['thread'] ?
  608. $p['thread'] . '.html#' . $p['id']
  609. :
  610. $p['id'] . '.html'
  611. ))
  612. ));
  613. }
  614. }
  615. }
  616. if (!hasPermission($config['mod']['postunoriginal'], $board['uri']) && $config['robot_enable'] && checkRobot($post['body_nomarkup'])) {
  617. undoImage($post);
  618. if ($config['robot_mute']) {
  619. error(sprintf($config['error']['muted'], mute()));
  620. } else {
  621. error($config['error']['unoriginal']);
  622. }
  623. }
  624. // Remove board directories before inserting them into the database.
  625. if ($post['has_file']) {
  626. foreach ($post['files'] as $key => &$file) {
  627. $file['file_path'] = $file['file'];
  628. $file['thumb_path'] = $file['thumb'];
  629. $file['file'] = mb_substr($file['file'], mb_strlen($board['dir'] . $config['dir']['img']));
  630. if ($file['is_an_image'] && $file['thumb'] != 'spoiler')
  631. $file['thumb'] = mb_substr($file['thumb'], mb_strlen($board['dir'] . $config['dir']['thumb']));
  632. }
  633. }
  634. $post = (object)$post;
  635. $post->files = array_map(function($a) { return (object)$a; }, $post->files);
  636. $error = event('post', $post);
  637. $post->files = array_map(function($a) { return (array)$a; }, $post->files);
  638. if ($error) {
  639. undoImage((array)$post);
  640. error($error);
  641. }
  642. $post = (array)$post;
  643. if ($post['files'])
  644. $post['files'] = $post['files'];
  645. $post['num_files'] = sizeof($post['files']);
  646. $post['id'] = $id = post($post);
  647. insertFloodPost($post);
  648. if (isset($post['antispam_hash'])) {
  649. incrementSpamHash($post['antispam_hash']);
  650. }
  651. if (isset($post['tracked_cites']) && !empty($post['tracked_cites'])) {
  652. $insert_rows = array();
  653. foreach ($post['tracked_cites'] as $cite) {
  654. $insert_rows[] = '(' .
  655. $pdo->quote($board['uri']) . ', ' . (int)$id . ', ' .
  656. $pdo->quote($cite[0]) . ', ' . (int)$cite[1] . ')';
  657. }
  658. query('INSERT INTO ``cites`` VALUES ' . implode(', ', $insert_rows)) or error(db_error());
  659. }
  660. if (!$post['op'] && strtolower($post['email']) != 'sage' && !$thread['sage'] && ($config['reply_limit'] == 0 || $numposts['replies']+1 < $config['reply_limit'])) {
  661. bumpThread($post['thread']);
  662. }
  663. buildThread($post['op'] ? $id : $post['thread']);
  664. if ($config['try_smarter'] && $post['op'])
  665. $build_pages = range(1, $config['max_pages']);
  666. if ($post['op'])
  667. clean();
  668. event('post-after', $post);
  669. buildIndex();
  670. if (isset($_SERVER['HTTP_REFERER'])) {
  671. // Tell Javascript that we posted successfully
  672. if (isset($_COOKIE[$config['cookies']['js']]))
  673. $js = json_decode($_COOKIE[$config['cookies']['js']]);
  674. else
  675. $js = (object) array();
  676. // Tell it to delete the cached post for referer
  677. $js->{$_SERVER['HTTP_REFERER']} = true;
  678. // Encode and set cookie
  679. setcookie($config['cookies']['js'], json_encode($js), 0, $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, false, false);
  680. }
  681. $root = $post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root'];
  682. if ($noko) {
  683. $redirect = $root . $board['dir'] . $config['dir']['res'] .
  684. sprintf($config['file_page'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : '');
  685. if (!$post['op'] && isset($_SERVER['HTTP_REFERER'])) {
  686. $regex = array(
  687. 'board' => str_replace('%s', '(\w{1,8})', preg_quote($config['board_path'], '/')),
  688. 'page' => str_replace('%d', '(\d+)', preg_quote($config['file_page'], '/')),
  689. 'page50' => str_replace('%d', '(\d+)', preg_quote($config['file_page50'], '/')),
  690. 'res' => preg_quote($config['dir']['res'], '/'),
  691. );
  692. if (preg_match('/\/' . $regex['board'] . $regex['res'] . $regex['page50'] . '([?&].*)?$/', $_SERVER['HTTP_REFERER'])) {
  693. $redirect = $root . $board['dir'] . $config['dir']['res'] .
  694. sprintf($config['file_page50'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : '');
  695. }
  696. }
  697. } else {
  698. $redirect = $root . $board['dir'] . $config['file_index'];
  699. }
  700. if ($config['syslog'])
  701. _syslog(LOG_INFO, 'New post: /' . $board['dir'] . $config['dir']['res'] .
  702. sprintf($config['file_page'], $post['op'] ? $id : $post['thread']) . (!$post['op'] ? '#' . $id : ''));
  703. if (!$post['mod']) header('X-Associated-Content: "' . $redirect . '"');
  704. if ($post['op'])
  705. rebuildThemes('post-thread', $board['uri']);
  706. else
  707. rebuildThemes('post', $board['uri']);
  708. if (!isset($_POST['json_response'])) {
  709. header('Location: ' . $redirect, true, $config['redirect_http']);
  710. } else {
  711. header('Content-Type: text/json; charset=utf-8');
  712. echo json_encode(array(
  713. 'redirect' => $redirect,
  714. 'noko' => $noko,
  715. 'id' => $id
  716. ));
  717. }
  718. } elseif (isset($_POST['appeal'])) {
  719. if (!isset($_POST['ban_id']))
  720. error($config['error']['bot']);
  721. $ban_id = (int)$_POST['ban_id'];
  722. $bans = Bans::find($_SERVER['REMOTE_ADDR']);
  723. foreach ($bans as $_ban) {
  724. if ($_ban['id'] == $ban_id) {
  725. $ban = $_ban;
  726. break;
  727. }
  728. }
  729. if (!isset($ban)) {
  730. error(_("That ban doesn't exist or is not for you."));
  731. }
  732. if ($ban['expires'] && $ban['expires'] - $ban['created'] <= $config['ban_appeals_min_length']) {
  733. error(_("You cannot appeal a ban of this length."));
  734. }
  735. $query = query("SELECT `denied` FROM ``ban_appeals`` WHERE `ban_id` = $ban_id") or error(db_error());
  736. $ban_appeals = $query->fetchAll(PDO::FETCH_COLUMN);
  737. if (count($ban_appeals) >= $config['ban_appeals_max']) {
  738. error(_("You cannot appeal this ban again."));
  739. }
  740. foreach ($ban_appeals as $is_denied) {
  741. if (!$is_denied)
  742. error(_("There is already a pending appeal for this ban."));
  743. }
  744. $query = prepare("INSERT INTO ``ban_appeals`` VALUES (NULL, :ban_id, :time, :message, 0)");
  745. $query->bindValue(':ban_id', $ban_id, PDO::PARAM_INT);
  746. $query->bindValue(':time', time(), PDO::PARAM_INT);
  747. $query->bindValue(':message', $_POST['appeal']);
  748. $query->execute() or error(db_error($query));
  749. displayBan($ban);
  750. } else {
  751. if (!file_exists($config['has_installed'])) {
  752. header('Location: install.php', true, $config['redirect_http']);
  753. } else {
  754. // They opened post.php in their browser manually.
  755. error($config['error']['nopost']);
  756. }
  757. }