PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/functions_posting.php

https://bitbucket.org/jablonski/yebood
PHP | 2731 lines | 2069 code | 408 blank | 254 comment | 501 complexity | df0dc446bbd7aa8585891850ee5e4779 MD5 | raw file
Possible License(s): AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. /**
  18. * Fill smiley templates (or just the variables) with smilies, either in a window or inline
  19. */
  20. function generate_smilies($mode, $forum_id)
  21. {
  22. global $auth, $db, $user, $config, $template;
  23. global $phpEx, $phpbb_root_path;
  24. $start = request_var('start', 0);
  25. if ($mode == 'window')
  26. {
  27. if ($forum_id)
  28. {
  29. $sql = 'SELECT forum_style
  30. FROM ' . FORUMS_TABLE . "
  31. WHERE forum_id = $forum_id";
  32. $result = $db->sql_query_limit($sql, 1);
  33. $row = $db->sql_fetchrow($result);
  34. $db->sql_freeresult($result);
  35. $user->setup('posting', (int) $row['forum_style']);
  36. }
  37. else
  38. {
  39. $user->setup('posting');
  40. }
  41. page_header($user->lang['SMILIES']);
  42. $sql = 'SELECT COUNT(smiley_id) AS item_count
  43. FROM ' . SMILIES_TABLE . '
  44. GROUP BY smiley_url';
  45. $result = $db->sql_query($sql, 3600);
  46. $smiley_count = 0;
  47. while ($row = $db->sql_fetchrow($result))
  48. {
  49. ++$smiley_count;
  50. }
  51. $db->sql_freeresult($result);
  52. $template->set_filenames(array(
  53. 'body' => 'posting_smilies.html')
  54. );
  55. $template->assign_var('PAGINATION',
  56. generate_pagination(append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&amp;f=' . $forum_id),
  57. $smiley_count, $config['smilies_per_page'], $start, true)
  58. );
  59. }
  60. $display_link = false;
  61. if ($mode == 'inline')
  62. {
  63. $sql = 'SELECT smiley_id
  64. FROM ' . SMILIES_TABLE . '
  65. WHERE display_on_posting = 0';
  66. $result = $db->sql_query_limit($sql, 1, 0, 3600);
  67. if ($row = $db->sql_fetchrow($result))
  68. {
  69. $display_link = true;
  70. }
  71. $db->sql_freeresult($result);
  72. }
  73. if ($mode == 'window')
  74. {
  75. $sql = 'SELECT smiley_url, MIN(emotion) as emotion, MIN(code) AS code, smiley_width, smiley_height, MIN(smiley_order) AS min_smiley_order
  76. FROM ' . SMILIES_TABLE . '
  77. GROUP BY smiley_url, smiley_width, smiley_height
  78. ORDER BY min_smiley_order';
  79. $result = $db->sql_query_limit($sql, $config['smilies_per_page'], $start, 3600);
  80. }
  81. else
  82. {
  83. $sql = 'SELECT *
  84. FROM ' . SMILIES_TABLE . '
  85. WHERE display_on_posting = 1
  86. ORDER BY smiley_order';
  87. $result = $db->sql_query($sql, 3600);
  88. }
  89. $smilies = array();
  90. while ($row = $db->sql_fetchrow($result))
  91. {
  92. if (empty($smilies[$row['smiley_url']]))
  93. {
  94. $smilies[$row['smiley_url']] = $row;
  95. }
  96. }
  97. $db->sql_freeresult($result);
  98. if (sizeof($smilies))
  99. {
  100. $root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
  101. foreach ($smilies as $row)
  102. {
  103. $template->assign_block_vars('smiley', array(
  104. 'SMILEY_CODE' => $row['code'],
  105. 'A_SMILEY_CODE' => addslashes($row['code']),
  106. 'SMILEY_IMG' => $root_path . $config['smilies_path'] . '/' . $row['smiley_url'],
  107. 'SMILEY_WIDTH' => $row['smiley_width'],
  108. 'SMILEY_HEIGHT' => $row['smiley_height'],
  109. 'SMILEY_DESC' => $row['emotion'])
  110. );
  111. }
  112. }
  113. if ($mode == 'inline' && $display_link)
  114. {
  115. $template->assign_vars(array(
  116. 'S_SHOW_SMILEY_LINK' => true,
  117. 'U_MORE_SMILIES' => append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&amp;f=' . $forum_id))
  118. );
  119. }
  120. if ($mode == 'window')
  121. {
  122. page_footer();
  123. }
  124. }
  125. /**
  126. * Update last post information
  127. * Should be used instead of sync() if only the last post information are out of sync... faster
  128. *
  129. * @param string $type Can be forum|topic
  130. * @param mixed $ids topic/forum ids
  131. * @param bool $return_update_sql true: SQL query shall be returned, false: execute SQL
  132. */
  133. function update_post_information($type, $ids, $return_update_sql = false)
  134. {
  135. global $db;
  136. if (empty($ids))
  137. {
  138. return;
  139. }
  140. if (!is_array($ids))
  141. {
  142. $ids = array($ids);
  143. }
  144. $update_sql = $empty_forums = $not_empty_forums = array();
  145. if ($type != 'topic')
  146. {
  147. $topic_join = ', ' . TOPICS_TABLE . ' t';
  148. $topic_condition = 'AND t.topic_id = p.topic_id AND t.topic_approved = 1';
  149. }
  150. else
  151. {
  152. $topic_join = '';
  153. $topic_condition = '';
  154. }
  155. if (sizeof($ids) == 1)
  156. {
  157. $sql = 'SELECT MAX(p.post_id) as last_post_id
  158. FROM ' . POSTS_TABLE . " p $topic_join
  159. WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
  160. $topic_condition
  161. AND p.post_approved = 1";
  162. }
  163. else
  164. {
  165. $sql = 'SELECT p.' . $type . '_id, MAX(p.post_id) as last_post_id
  166. FROM ' . POSTS_TABLE . " p $topic_join
  167. WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
  168. $topic_condition
  169. AND p.post_approved = 1
  170. GROUP BY p.{$type}_id";
  171. }
  172. $result = $db->sql_query($sql);
  173. $last_post_ids = array();
  174. while ($row = $db->sql_fetchrow($result))
  175. {
  176. if (sizeof($ids) == 1)
  177. {
  178. $row[$type . '_id'] = $ids[0];
  179. }
  180. if ($type == 'forum')
  181. {
  182. $not_empty_forums[] = $row['forum_id'];
  183. if (empty($row['last_post_id']))
  184. {
  185. $empty_forums[] = $row['forum_id'];
  186. }
  187. }
  188. $last_post_ids[] = $row['last_post_id'];
  189. }
  190. $db->sql_freeresult($result);
  191. if ($type == 'forum')
  192. {
  193. $empty_forums = array_merge($empty_forums, array_diff($ids, $not_empty_forums));
  194. foreach ($empty_forums as $void => $forum_id)
  195. {
  196. $update_sql[$forum_id][] = 'forum_last_post_id = 0';
  197. $update_sql[$forum_id][] = "forum_last_post_subject = ''";
  198. $update_sql[$forum_id][] = 'forum_last_post_time = 0';
  199. $update_sql[$forum_id][] = 'forum_last_poster_id = 0';
  200. $update_sql[$forum_id][] = "forum_last_poster_name = ''";
  201. $update_sql[$forum_id][] = "forum_last_poster_colour = ''";
  202. }
  203. }
  204. if (sizeof($last_post_ids))
  205. {
  206. $sql = 'SELECT p.' . $type . '_id, p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
  207. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  208. WHERE p.poster_id = u.user_id
  209. AND ' . $db->sql_in_set('p.post_id', $last_post_ids);
  210. $result = $db->sql_query($sql);
  211. while ($row = $db->sql_fetchrow($result))
  212. {
  213. $update_sql[$row["{$type}_id"]][] = $type . '_last_post_id = ' . (int) $row['post_id'];
  214. $update_sql[$row["{$type}_id"]][] = "{$type}_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
  215. $update_sql[$row["{$type}_id"]][] = $type . '_last_post_time = ' . (int) $row['post_time'];
  216. $update_sql[$row["{$type}_id"]][] = $type . '_last_poster_id = ' . (int) $row['poster_id'];
  217. $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
  218. $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "'";
  219. }
  220. $db->sql_freeresult($result);
  221. }
  222. unset($empty_forums, $ids, $last_post_ids);
  223. if ($return_update_sql || !sizeof($update_sql))
  224. {
  225. return $update_sql;
  226. }
  227. $table = ($type == 'forum') ? FORUMS_TABLE : TOPICS_TABLE;
  228. foreach ($update_sql as $update_id => $update_sql_ary)
  229. {
  230. $sql = "UPDATE $table
  231. SET " . implode(', ', $update_sql_ary) . "
  232. WHERE {$type}_id = $update_id";
  233. $db->sql_query($sql);
  234. }
  235. return;
  236. }
  237. /**
  238. * Generate Topic Icons for display
  239. */
  240. function posting_gen_topic_icons($mode, $icon_id)
  241. {
  242. global $phpbb_root_path, $config, $template, $cache;
  243. // Grab icons
  244. $icons = $cache->obtain_icons();
  245. if (!$icon_id)
  246. {
  247. $template->assign_var('S_NO_ICON_CHECKED', ' checked="checked"');
  248. }
  249. if (sizeof($icons))
  250. {
  251. foreach ($icons as $id => $data)
  252. {
  253. if ($data['display'])
  254. {
  255. $template->assign_block_vars('topic_icon', array(
  256. 'ICON_ID' => $id,
  257. 'ICON_IMG' => $phpbb_root_path . $config['icons_path'] . '/' . $data['img'],
  258. 'ICON_WIDTH' => $data['width'],
  259. 'ICON_HEIGHT' => $data['height'],
  260. 'S_CHECKED' => ($id == $icon_id) ? true : false,
  261. 'S_ICON_CHECKED' => ($id == $icon_id) ? ' checked="checked"' : '')
  262. );
  263. }
  264. }
  265. return true;
  266. }
  267. return false;
  268. }
  269. /**
  270. * Build topic types able to be selected
  271. */
  272. function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL)
  273. {
  274. global $auth, $user, $template, $topic_type;
  275. $toggle = false;
  276. $topic_types = array(
  277. 'sticky' => array('const' => POST_STICKY, 'lang' => 'POST_STICKY'),
  278. 'announce' => array('const' => POST_ANNOUNCE, 'lang' => 'POST_ANNOUNCEMENT'),
  279. 'global' => array('const' => POST_GLOBAL, 'lang' => 'POST_GLOBAL')
  280. );
  281. $topic_type_array = array();
  282. foreach ($topic_types as $auth_key => $topic_value)
  283. {
  284. // We do not have a special post global announcement permission
  285. $auth_key = ($auth_key == 'global') ? 'announce' : $auth_key;
  286. if ($auth->acl_get('f_' . $auth_key, $forum_id))
  287. {
  288. $toggle = true;
  289. $topic_type_array[] = array(
  290. 'VALUE' => $topic_value['const'],
  291. 'S_CHECKED' => ($cur_topic_type == $topic_value['const'] || ($forum_id == 0 && $topic_value['const'] == POST_GLOBAL)) ? ' checked="checked"' : '',
  292. 'L_TOPIC_TYPE' => $user->lang[$topic_value['lang']]
  293. );
  294. }
  295. }
  296. if ($toggle)
  297. {
  298. $topic_type_array = array_merge(array(0 => array(
  299. 'VALUE' => POST_NORMAL,
  300. 'S_CHECKED' => ($cur_topic_type == POST_NORMAL) ? ' checked="checked"' : '',
  301. 'L_TOPIC_TYPE' => $user->lang['POST_NORMAL'])),
  302. $topic_type_array
  303. );
  304. foreach ($topic_type_array as $array)
  305. {
  306. $template->assign_block_vars('topic_type', $array);
  307. }
  308. $template->assign_vars(array(
  309. 'S_TOPIC_TYPE_STICKY' => ($auth->acl_get('f_sticky', $forum_id)),
  310. 'S_TOPIC_TYPE_ANNOUNCE' => ($auth->acl_get('f_announce', $forum_id)))
  311. );
  312. }
  313. return $toggle;
  314. }
  315. //
  316. // Attachment related functions
  317. //
  318. /**
  319. * Upload Attachment - filedata is generated here
  320. * Uses upload class
  321. */
  322. function upload_attachment($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false, $local_filedata = false)
  323. {
  324. global $auth, $user, $config, $db, $cache;
  325. global $phpbb_root_path, $phpEx;
  326. $filedata = array(
  327. 'error' => array()
  328. );
  329. include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
  330. $upload = new fileupload();
  331. if ($config['check_attachment_content'] && isset($config['mime_triggers']))
  332. {
  333. $upload->set_disallowed_content(explode('|', $config['mime_triggers']));
  334. }
  335. if (!$local)
  336. {
  337. $filedata['post_attach'] = ($upload->is_valid($form_name)) ? true : false;
  338. }
  339. else
  340. {
  341. $filedata['post_attach'] = true;
  342. }
  343. if (!$filedata['post_attach'])
  344. {
  345. $filedata['error'][] = $user->lang['NO_UPLOAD_FORM_FOUND'];
  346. return $filedata;
  347. }
  348. $extensions = $cache->obtain_attach_extensions((($is_message) ? false : (int) $forum_id));
  349. $upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
  350. $file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->form_upload($form_name);
  351. if ($file->init_error)
  352. {
  353. $filedata['post_attach'] = false;
  354. return $filedata;
  355. }
  356. $cat_id = (isset($extensions[$file->get('extension')]['display_cat'])) ? $extensions[$file->get('extension')]['display_cat'] : ATTACHMENT_CATEGORY_NONE;
  357. // Make sure the image category only holds valid images...
  358. if ($cat_id == ATTACHMENT_CATEGORY_IMAGE && !$file->is_image())
  359. {
  360. $file->remove();
  361. // If this error occurs a user tried to exploit an IE Bug by renaming extensions
  362. // Since the image category is displaying content inline we need to catch this.
  363. trigger_error($user->lang['ATTACHED_IMAGE_NOT_IMAGE']);
  364. }
  365. // Do we have to create a thumbnail?
  366. $filedata['thumbnail'] = ($cat_id == ATTACHMENT_CATEGORY_IMAGE && $config['img_create_thumbnail']) ? 1 : 0;
  367. // Check Image Size, if it is an image
  368. if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id) && $cat_id == ATTACHMENT_CATEGORY_IMAGE)
  369. {
  370. $file->upload->set_allowed_dimensions(0, 0, $config['img_max_width'], $config['img_max_height']);
  371. }
  372. // Admins and mods are allowed to exceed the allowed filesize
  373. if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id))
  374. {
  375. if (!empty($extensions[$file->get('extension')]['max_filesize']))
  376. {
  377. $allowed_filesize = $extensions[$file->get('extension')]['max_filesize'];
  378. }
  379. else
  380. {
  381. $allowed_filesize = ($is_message) ? $config['max_filesize_pm'] : $config['max_filesize'];
  382. }
  383. $file->upload->set_max_filesize($allowed_filesize);
  384. }
  385. $file->clean_filename('unique', $user->data['user_id'] . '_');
  386. // Are we uploading an image *and* this image being within the image category? Only then perform additional image checks.
  387. $no_image = ($cat_id == ATTACHMENT_CATEGORY_IMAGE) ? false : true;
  388. $file->move_file($config['upload_path'], false, $no_image);
  389. if (sizeof($file->error))
  390. {
  391. $file->remove();
  392. $filedata['error'] = array_merge($filedata['error'], $file->error);
  393. $filedata['post_attach'] = false;
  394. return $filedata;
  395. }
  396. $filedata['filesize'] = $file->get('filesize');
  397. $filedata['mimetype'] = $file->get('mimetype');
  398. $filedata['extension'] = $file->get('extension');
  399. $filedata['physical_filename'] = $file->get('realname');
  400. $filedata['real_filename'] = $file->get('uploadname');
  401. $filedata['filetime'] = time();
  402. // Check our complete quota
  403. if ($config['attachment_quota'])
  404. {
  405. if ($config['upload_dir_size'] + $file->get('filesize') > $config['attachment_quota'])
  406. {
  407. $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
  408. $filedata['post_attach'] = false;
  409. $file->remove();
  410. return $filedata;
  411. }
  412. }
  413. // Check free disk space
  414. if ($free_space = @disk_free_space($phpbb_root_path . $config['upload_path']))
  415. {
  416. if ($free_space <= $file->get('filesize'))
  417. {
  418. if ($auth->acl_get('a_'))
  419. {
  420. $filedata['error'][] = $user->lang['ATTACH_DISK_FULL'];
  421. }
  422. else
  423. {
  424. $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
  425. }
  426. $filedata['post_attach'] = false;
  427. $file->remove();
  428. return $filedata;
  429. }
  430. }
  431. // Create Thumbnail
  432. if ($filedata['thumbnail'])
  433. {
  434. $source = $file->get('destination_file');
  435. $destination = $file->get('destination_path') . '/thumb_' . $file->get('realname');
  436. if (!create_thumbnail($source, $destination, $file->get('mimetype')))
  437. {
  438. $filedata['thumbnail'] = 0;
  439. }
  440. }
  441. return $filedata;
  442. }
  443. /**
  444. * Calculate the needed size for Thumbnail
  445. */
  446. function get_img_size_format($width, $height)
  447. {
  448. global $config;
  449. // Maximum Width the Image can take
  450. $max_width = ($config['img_max_thumb_width']) ? $config['img_max_thumb_width'] : 400;
  451. if ($width > $height)
  452. {
  453. return array(
  454. round($width * ($max_width / $width)),
  455. round($height * ($max_width / $width))
  456. );
  457. }
  458. else
  459. {
  460. return array(
  461. round($width * ($max_width / $height)),
  462. round($height * ($max_width / $height))
  463. );
  464. }
  465. }
  466. /**
  467. * Return supported image types
  468. */
  469. function get_supported_image_types($type = false)
  470. {
  471. if (@extension_loaded('gd'))
  472. {
  473. $format = imagetypes();
  474. $new_type = 0;
  475. if ($type !== false)
  476. {
  477. // Type is one of the IMAGETYPE constants - it is fetched from getimagesize()
  478. // We do not use the constants here, because some were not available in PHP 4.3.x
  479. switch ($type)
  480. {
  481. // GIF
  482. case 1:
  483. $new_type = ($format & IMG_GIF) ? IMG_GIF : false;
  484. break;
  485. // JPG, JPC, JP2
  486. case 2:
  487. case 9:
  488. case 10:
  489. case 11:
  490. case 12:
  491. $new_type = ($format & IMG_JPG) ? IMG_JPG : false;
  492. break;
  493. // PNG
  494. case 3:
  495. $new_type = ($format & IMG_PNG) ? IMG_PNG : false;
  496. break;
  497. // WBMP
  498. case 15:
  499. $new_type = ($format & IMG_WBMP) ? IMG_WBMP : false;
  500. break;
  501. }
  502. }
  503. else
  504. {
  505. $new_type = array();
  506. $go_through_types = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP);
  507. foreach ($go_through_types as $check_type)
  508. {
  509. if ($format & $check_type)
  510. {
  511. $new_type[] = $check_type;
  512. }
  513. }
  514. }
  515. return array(
  516. 'gd' => ($new_type) ? true : false,
  517. 'format' => $new_type,
  518. 'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1
  519. );
  520. }
  521. return array('gd' => false);
  522. }
  523. /**
  524. * Create Thumbnail
  525. */
  526. function create_thumbnail($source, $destination, $mimetype)
  527. {
  528. global $config;
  529. $min_filesize = (int) $config['img_min_thumb_filesize'];
  530. $img_filesize = (file_exists($source)) ? @filesize($source) : false;
  531. if (!$img_filesize || $img_filesize <= $min_filesize)
  532. {
  533. return false;
  534. }
  535. $dimension = @getimagesize($source);
  536. if ($dimension === false)
  537. {
  538. return false;
  539. }
  540. list($width, $height, $type, ) = $dimension;
  541. if (empty($width) || empty($height))
  542. {
  543. return false;
  544. }
  545. list($new_width, $new_height) = get_img_size_format($width, $height);
  546. // Do not create a thumbnail if the resulting width/height is bigger than the original one
  547. if ($new_width >= $width && $new_height >= $height)
  548. {
  549. return false;
  550. }
  551. $used_imagick = false;
  552. // Only use imagemagick if defined and the passthru function not disabled
  553. if ($config['img_imagick'] && function_exists('passthru'))
  554. {
  555. if (substr($config['img_imagick'], -1) !== '/')
  556. {
  557. $config['img_imagick'] .= '/';
  558. }
  559. @passthru(escapeshellcmd($config['img_imagick']) . 'convert' . ((defined('PHP_OS') && preg_match('#^win#i', PHP_OS)) ? '.exe' : '') . ' -quality 85 -geometry ' . $new_width . 'x' . $new_height . ' "' . str_replace('\\', '/', $source) . '" "' . str_replace('\\', '/', $destination) . '"');
  560. if (file_exists($destination))
  561. {
  562. $used_imagick = true;
  563. }
  564. }
  565. if (!$used_imagick)
  566. {
  567. $type = get_supported_image_types($type);
  568. if ($type['gd'])
  569. {
  570. // If the type is not supported, we are not able to create a thumbnail
  571. if ($type['format'] === false)
  572. {
  573. return false;
  574. }
  575. switch ($type['format'])
  576. {
  577. case IMG_GIF:
  578. $image = @imagecreatefromgif($source);
  579. break;
  580. case IMG_JPG:
  581. @ini_set('gd.jpeg_ignore_warning', 1);
  582. $image = @imagecreatefromjpeg($source);
  583. break;
  584. case IMG_PNG:
  585. $image = @imagecreatefrompng($source);
  586. break;
  587. case IMG_WBMP:
  588. $image = @imagecreatefromwbmp($source);
  589. break;
  590. }
  591. if (empty($image))
  592. {
  593. return false;
  594. }
  595. if ($type['version'] == 1)
  596. {
  597. $new_image = imagecreate($new_width, $new_height);
  598. if ($new_image === false)
  599. {
  600. return false;
  601. }
  602. imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  603. }
  604. else
  605. {
  606. $new_image = imagecreatetruecolor($new_width, $new_height);
  607. if ($new_image === false)
  608. {
  609. return false;
  610. }
  611. // Preserve alpha transparency (png for example)
  612. @imagealphablending($new_image, false);
  613. @imagesavealpha($new_image, true);
  614. imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  615. }
  616. // If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
  617. if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on')
  618. {
  619. @touch($destination);
  620. }
  621. switch ($type['format'])
  622. {
  623. case IMG_GIF:
  624. imagegif($new_image, $destination);
  625. break;
  626. case IMG_JPG:
  627. imagejpeg($new_image, $destination, 90);
  628. break;
  629. case IMG_PNG:
  630. imagepng($new_image, $destination);
  631. break;
  632. case IMG_WBMP:
  633. imagewbmp($new_image, $destination);
  634. break;
  635. }
  636. imagedestroy($new_image);
  637. }
  638. else
  639. {
  640. return false;
  641. }
  642. }
  643. if (!file_exists($destination))
  644. {
  645. return false;
  646. }
  647. phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
  648. return true;
  649. }
  650. /**
  651. * Assign Inline attachments (build option fields)
  652. */
  653. function posting_gen_inline_attachments(&$attachment_data)
  654. {
  655. global $template;
  656. if (sizeof($attachment_data))
  657. {
  658. $s_inline_attachment_options = '';
  659. foreach ($attachment_data as $i => $attachment)
  660. {
  661. $s_inline_attachment_options .= '<option value="' . $i . '">' . utf8_basename($attachment['real_filename']) . '</option>';
  662. }
  663. $template->assign_var('S_INLINE_ATTACHMENT_OPTIONS', $s_inline_attachment_options);
  664. return true;
  665. }
  666. return false;
  667. }
  668. /**
  669. * Generate inline attachment entry
  670. */
  671. function posting_gen_attachment_entry($attachment_data, &$filename_data, $show_attach_box = true)
  672. {
  673. global $template, $config, $phpbb_root_path, $phpEx, $user, $auth;
  674. // Some default template variables
  675. $template->assign_vars(array(
  676. 'S_SHOW_ATTACH_BOX' => $show_attach_box,
  677. 'S_HAS_ATTACHMENTS' => sizeof($attachment_data),
  678. 'FILESIZE' => $config['max_filesize'],
  679. 'FILE_COMMENT' => (isset($filename_data['filecomment'])) ? $filename_data['filecomment'] : '',
  680. ));
  681. if (sizeof($attachment_data))
  682. {
  683. // We display the posted attachments within the desired order.
  684. ($config['display_order']) ? krsort($attachment_data) : ksort($attachment_data);
  685. foreach ($attachment_data as $count => $attach_row)
  686. {
  687. $hidden = '';
  688. $attach_row['real_filename'] = utf8_basename($attach_row['real_filename']);
  689. foreach ($attach_row as $key => $value)
  690. {
  691. $hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
  692. }
  693. $download_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'mode=view&amp;id=' . (int) $attach_row['attach_id'], true, ($attach_row['is_orphan']) ? $user->session_id : false);
  694. $template->assign_block_vars('attach_row', array(
  695. 'FILENAME' => utf8_basename($attach_row['real_filename']),
  696. 'A_FILENAME' => addslashes(utf8_basename($attach_row['real_filename'])),
  697. 'FILE_COMMENT' => $attach_row['attach_comment'],
  698. 'ATTACH_ID' => $attach_row['attach_id'],
  699. 'S_IS_ORPHAN' => $attach_row['is_orphan'],
  700. 'ASSOC_INDEX' => $count,
  701. 'U_VIEW_ATTACHMENT' => $download_link,
  702. 'S_HIDDEN' => $hidden)
  703. );
  704. }
  705. }
  706. return sizeof($attachment_data);
  707. }
  708. //
  709. // General Post functions
  710. //
  711. /**
  712. * Load Drafts
  713. */
  714. function load_drafts($topic_id = 0, $forum_id = 0, $id = 0, $pm_action = '', $msg_id = 0)
  715. {
  716. global $user, $db, $template, $auth;
  717. global $phpbb_root_path, $phpEx;
  718. $topic_ids = $forum_ids = $draft_rows = array();
  719. // Load those drafts not connected to forums/topics
  720. // If forum_id == 0 AND topic_id == 0 then this is a PM draft
  721. if (!$topic_id && !$forum_id)
  722. {
  723. $sql_and = ' AND d.forum_id = 0 AND d.topic_id = 0';
  724. }
  725. else
  726. {
  727. $sql_and = '';
  728. $sql_and .= ($forum_id) ? ' AND d.forum_id = ' . (int) $forum_id : '';
  729. $sql_and .= ($topic_id) ? ' AND d.topic_id = ' . (int) $topic_id : '';
  730. }
  731. $sql = 'SELECT d.*, f.forum_id, f.forum_name
  732. FROM ' . DRAFTS_TABLE . ' d
  733. LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = d.forum_id)
  734. WHERE d.user_id = ' . $user->data['user_id'] . "
  735. $sql_and
  736. ORDER BY d.save_time DESC";
  737. $result = $db->sql_query($sql);
  738. while ($row = $db->sql_fetchrow($result))
  739. {
  740. if ($row['topic_id'])
  741. {
  742. $topic_ids[] = (int) $row['topic_id'];
  743. }
  744. $draft_rows[] = $row;
  745. }
  746. $db->sql_freeresult($result);
  747. if (!sizeof($draft_rows))
  748. {
  749. return;
  750. }
  751. $topic_rows = array();
  752. if (sizeof($topic_ids))
  753. {
  754. $sql = 'SELECT topic_id, forum_id, topic_title
  755. FROM ' . TOPICS_TABLE . '
  756. WHERE ' . $db->sql_in_set('topic_id', array_unique($topic_ids));
  757. $result = $db->sql_query($sql);
  758. while ($row = $db->sql_fetchrow($result))
  759. {
  760. $topic_rows[$row['topic_id']] = $row;
  761. }
  762. $db->sql_freeresult($result);
  763. }
  764. unset($topic_ids);
  765. $template->assign_var('S_SHOW_DRAFTS', true);
  766. foreach ($draft_rows as $draft)
  767. {
  768. $link_topic = $link_forum = $link_pm = false;
  769. $insert_url = $view_url = $title = '';
  770. if (isset($topic_rows[$draft['topic_id']])
  771. && (
  772. ($topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id']))
  773. ||
  774. (!$topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_getf_global('f_read'))
  775. ))
  776. {
  777. $topic_forum_id = ($topic_rows[$draft['topic_id']]['forum_id']) ? $topic_rows[$draft['topic_id']]['forum_id'] : $forum_id;
  778. $link_topic = true;
  779. $view_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id']);
  780. $title = $topic_rows[$draft['topic_id']]['topic_title'];
  781. $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id'] . '&amp;mode=reply&amp;d=' . $draft['draft_id']);
  782. }
  783. else if ($draft['forum_id'] && $auth->acl_get('f_read', $draft['forum_id']))
  784. {
  785. $link_forum = true;
  786. $view_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $draft['forum_id']);
  787. $title = $draft['forum_name'];
  788. $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $draft['forum_id'] . '&amp;mode=post&amp;d=' . $draft['draft_id']);
  789. }
  790. else
  791. {
  792. // Either display as PM draft if forum_id and topic_id are empty or if access to the forums has been denied afterwards...
  793. $link_pm = true;
  794. $insert_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&amp;mode=compose&amp;d={$draft['draft_id']}" . (($pm_action) ? "&amp;action=$pm_action" : '') . (($msg_id) ? "&amp;p=$msg_id" : ''));
  795. }
  796. $template->assign_block_vars('draftrow', array(
  797. 'DRAFT_ID' => $draft['draft_id'],
  798. 'DATE' => $user->format_date($draft['save_time']),
  799. 'DRAFT_SUBJECT' => $draft['draft_subject'],
  800. 'TITLE' => $title,
  801. 'U_VIEW' => $view_url,
  802. 'U_INSERT' => $insert_url,
  803. 'S_LINK_PM' => $link_pm,
  804. 'S_LINK_TOPIC' => $link_topic,
  805. 'S_LINK_FORUM' => $link_forum)
  806. );
  807. }
  808. }
  809. /**
  810. * Topic Review
  811. */
  812. function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id = 0, $show_quote_button = true)
  813. {
  814. global $user, $auth, $db, $template, $bbcode, $cache;
  815. global $config, $phpbb_root_path, $phpEx;
  816. // Go ahead and pull all data for this topic
  817. $sql = 'SELECT p.post_id
  818. FROM ' . POSTS_TABLE . ' p' . "
  819. WHERE p.topic_id = $topic_id
  820. " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . '
  821. ' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
  822. ' . (($mode == 'post_review_edit') ? " AND p.post_id = $cur_post_id" : '') . '
  823. ORDER BY p.post_time ';
  824. $sql .= ($mode == 'post_review') ? 'ASC' : 'DESC';
  825. $result = $db->sql_query_limit($sql, $config['posts_per_page']);
  826. $post_list = array();
  827. while ($row = $db->sql_fetchrow($result))
  828. {
  829. $post_list[] = $row['post_id'];
  830. }
  831. $db->sql_freeresult($result);
  832. if (!sizeof($post_list))
  833. {
  834. return false;
  835. }
  836. // Handle 'post_review_edit' like 'post_review' from now on
  837. if ($mode == 'post_review_edit')
  838. {
  839. $mode = 'post_review';
  840. }
  841. $sql = $db->sql_build_query('SELECT', array(
  842. 'SELECT' => 'u.username, u.user_id, u.user_colour, p.*, z.friend, z.foe',
  843. 'FROM' => array(
  844. USERS_TABLE => 'u',
  845. POSTS_TABLE => 'p',
  846. ),
  847. 'LEFT_JOIN' => array(
  848. array(
  849. 'FROM' => array(ZEBRA_TABLE => 'z'),
  850. 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
  851. )
  852. ),
  853. 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
  854. AND u.user_id = p.poster_id'
  855. ));
  856. $result = $db->sql_query($sql);
  857. $bbcode_bitfield = '';
  858. $rowset = array();
  859. $has_attachments = false;
  860. while ($row = $db->sql_fetchrow($result))
  861. {
  862. $rowset[$row['post_id']] = $row;
  863. $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
  864. if ($row['post_attachment'])
  865. {
  866. $has_attachments = true;
  867. }
  868. }
  869. $db->sql_freeresult($result);
  870. // Instantiate BBCode class
  871. if (!isset($bbcode) && $bbcode_bitfield !== '')
  872. {
  873. include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
  874. $bbcode = new bbcode(base64_encode($bbcode_bitfield));
  875. }
  876. // Grab extensions
  877. $extensions = $attachments = array();
  878. if ($has_attachments && $auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
  879. {
  880. $extensions = $cache->obtain_attach_extensions($forum_id);
  881. // Get attachments...
  882. $sql = 'SELECT *
  883. FROM ' . ATTACHMENTS_TABLE . '
  884. WHERE ' . $db->sql_in_set('post_msg_id', $post_list) . '
  885. AND in_message = 0
  886. ORDER BY filetime DESC, post_msg_id ASC';
  887. $result = $db->sql_query($sql);
  888. while ($row = $db->sql_fetchrow($result))
  889. {
  890. $attachments[$row['post_msg_id']][] = $row;
  891. }
  892. $db->sql_freeresult($result);
  893. }
  894. for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
  895. {
  896. // A non-existing rowset only happens if there was no user present for the entered poster_id
  897. // This could be a broken posts table.
  898. if (!isset($rowset[$post_list[$i]]))
  899. {
  900. continue;
  901. }
  902. $row =& $rowset[$post_list[$i]];
  903. $poster_id = $row['user_id'];
  904. $post_subject = $row['post_subject'];
  905. $message = censor_text($row['post_text']);
  906. $decoded_message = false;
  907. if ($show_quote_button && $auth->acl_get('f_reply', $forum_id))
  908. {
  909. $decoded_message = $message;
  910. decode_message($decoded_message, $row['bbcode_uid']);
  911. $decoded_message = bbcode_nl2br($decoded_message);
  912. }
  913. if ($row['bbcode_bitfield'])
  914. {
  915. $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
  916. }
  917. $message = bbcode_nl2br($message);
  918. $message = smiley_text($message, !$row['enable_smilies']);
  919. if (!empty($attachments[$row['post_id']]))
  920. {
  921. $update_count = array();
  922. parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
  923. }
  924. $post_subject = censor_text($post_subject);
  925. $post_anchor = ($mode == 'post_review') ? 'ppr' . $row['post_id'] : 'pr' . $row['post_id'];
  926. $u_show_post = append_sid($phpbb_root_path . 'viewtopic.' . $phpEx, "f=$forum_id&amp;t=$topic_id&amp;p={$row['post_id']}&amp;view=show#p{$row['post_id']}");
  927. $template->assign_block_vars($mode . '_row', array(
  928. 'POST_AUTHOR_FULL' => get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  929. 'POST_AUTHOR_COLOUR' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  930. 'POST_AUTHOR' => get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  931. 'U_POST_AUTHOR' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  932. 'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
  933. 'S_FRIEND' => ($row['friend']) ? true : false,
  934. 'S_IGNORE_POST' => ($row['foe']) ? true : false,
  935. 'L_IGNORE_POST' => ($row['foe']) ? sprintf($user->lang['POST_BY_FOE'], get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']), "<a href=\"{$u_show_post}\" onclick=\"dE('{$post_anchor}', 1); return false;\">", '</a>') : '',
  936. 'POST_SUBJECT' => $post_subject,
  937. 'MINI_POST_IMG' => $user->img('icon_post_target', $user->lang['POST']),
  938. 'POST_DATE' => $user->format_date($row['post_time']),
  939. 'MESSAGE' => $message,
  940. 'DECODED_MESSAGE' => $decoded_message,
  941. 'POST_ID' => $row['post_id'],
  942. 'U_MINI_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
  943. 'U_MCP_DETAILS' => ($auth->acl_get('m_info', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=post_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
  944. 'POSTER_QUOTE' => ($show_quote_button && $auth->acl_get('f_reply', $forum_id)) ? addslashes(get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username'])) : '')
  945. );
  946. // Display not already displayed Attachments for this post, we already parsed them. ;)
  947. if (!empty($attachments[$row['post_id']]))
  948. {
  949. foreach ($attachments[$row['post_id']] as $attachment)
  950. {
  951. $template->assign_block_vars($mode . '_row.attachment', array(
  952. 'DISPLAY_ATTACHMENT' => $attachment)
  953. );
  954. }
  955. }
  956. unset($rowset[$post_list[$i]]);
  957. }
  958. if ($mode == 'topic_review')
  959. {
  960. $template->assign_var('QUOTE_IMG', $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']));
  961. }
  962. return true;
  963. }
  964. /**
  965. * User Notification
  966. */
  967. function user_notification($mode, $subject, $topic_title, $forum_name, $forum_id, $topic_id, $post_id)
  968. {
  969. global $db, $user, $config, $phpbb_root_path, $phpEx, $auth;
  970. $topic_notification = ($mode == 'reply' || $mode == 'quote') ? true : false;
  971. $forum_notification = ($mode == 'post') ? true : false;
  972. if (!$topic_notification && !$forum_notification)
  973. {
  974. trigger_error('NO_MODE');
  975. }
  976. if (($topic_notification && !$config['allow_topic_notify']) || ($forum_notification && !$config['allow_forum_notify']))
  977. {
  978. return;
  979. }
  980. $topic_title = ($topic_notification) ? $topic_title : $subject;
  981. $topic_title = censor_text($topic_title);
  982. // Exclude guests, current user and banned users from notifications
  983. if (!function_exists('phpbb_get_banned_user_ids'))
  984. {
  985. include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  986. }
  987. $sql_ignore_users = phpbb_get_banned_user_ids();
  988. $sql_ignore_users[ANONYMOUS] = ANONYMOUS;
  989. $sql_ignore_users[$user->data['user_id']] = $user->data['user_id'];
  990. $notify_rows = array();
  991. // -- get forum_userids || topic_userids
  992. $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
  993. FROM ' . (($topic_notification) ? TOPICS_WATCH_TABLE : FORUMS_WATCH_TABLE) . ' w, ' . USERS_TABLE . ' u
  994. WHERE w.' . (($topic_notification) ? 'topic_id' : 'forum_id') . ' = ' . (($topic_notification) ? $topic_id : $forum_id) . '
  995. AND ' . $db->sql_in_set('w.user_id', $sql_ignore_users, true) . '
  996. AND w.notify_status = ' . NOTIFY_YES . '
  997. AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
  998. AND u.user_id = w.user_id';
  999. $result = $db->sql_query($sql);
  1000. while ($row = $db->sql_fetchrow($result))
  1001. {
  1002. $notify_user_id = (int) $row['user_id'];
  1003. $notify_rows[$notify_user_id] = array(
  1004. 'user_id' => $notify_user_id,
  1005. 'username' => $row['username'],
  1006. 'user_email' => $row['user_email'],
  1007. 'user_jabber' => $row['user_jabber'],
  1008. 'user_lang' => $row['user_lang'],
  1009. 'notify_type' => ($topic_notification) ? 'topic' : 'forum',
  1010. 'template' => ($topic_notification) ? 'topic_notify' : 'newtopic_notify',
  1011. 'method' => $row['user_notify_type'],
  1012. 'allowed' => false
  1013. );
  1014. // Add users who have been already notified to ignore list
  1015. $sql_ignore_users[$notify_user_id] = $notify_user_id;
  1016. }
  1017. $db->sql_freeresult($result);
  1018. // forum notification is sent to those not already receiving topic notifications
  1019. if ($topic_notification)
  1020. {
  1021. $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
  1022. FROM ' . FORUMS_WATCH_TABLE . ' fw, ' . USERS_TABLE . " u
  1023. WHERE fw.forum_id = $forum_id
  1024. AND " . $db->sql_in_set('fw.user_id', $sql_ignore_users, true) . '
  1025. AND fw.notify_status = ' . NOTIFY_YES . '
  1026. AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
  1027. AND u.user_id = fw.user_id';
  1028. $result = $db->sql_query($sql);
  1029. while ($row = $db->sql_fetchrow($result))
  1030. {
  1031. $notify_user_id = (int) $row['user_id'];
  1032. $notify_rows[$notify_user_id] = array(
  1033. 'user_id' => $notify_user_id,
  1034. 'username' => $row['username'],
  1035. 'user_email' => $row['user_email'],
  1036. 'user_jabber' => $row['user_jabber'],
  1037. 'user_lang' => $row['user_lang'],
  1038. 'notify_type' => 'forum',
  1039. 'template' => 'forum_notify',
  1040. 'method' => $row['user_notify_type'],
  1041. 'allowed' => false
  1042. );
  1043. }
  1044. $db->sql_freeresult($result);
  1045. }
  1046. if (!sizeof($notify_rows))
  1047. {
  1048. return;
  1049. }
  1050. // Make sure users are allowed to read the forum
  1051. foreach ($auth->acl_get_list(array_keys($notify_rows), 'f_read', $forum_id) as $forum_id => $forum_ary)
  1052. {
  1053. foreach ($forum_ary as $auth_option => $user_ary)
  1054. {
  1055. foreach ($user_ary as $user_id)
  1056. {
  1057. $notify_rows[$user_id]['allowed'] = true;
  1058. }
  1059. }
  1060. }
  1061. // Now, we have to do a little step before really sending, we need to distinguish our users a little bit. ;)
  1062. $msg_users = $delete_ids = $update_notification = array();
  1063. foreach ($notify_rows as $user_id => $row)
  1064. {
  1065. if (!$row['allowed'] || !trim($row['user_email']))
  1066. {
  1067. $delete_ids[$row['notify_type']][] = $row['user_id'];
  1068. }
  1069. else
  1070. {
  1071. $msg_users[] = $row;
  1072. $update_notification[$row['notify_type']][] = $row['user_id'];
  1073. /*
  1074. * We also update the forums watch table for this user when we are
  1075. * sending out a topic notification to prevent sending out another
  1076. * notification in case this user is also subscribed to the forum
  1077. * this topic was posted in.
  1078. * Since an UPDATE query is used, this has no effect on users only
  1079. * subscribed to the topic (i.e. no row is created) and should not
  1080. * be a performance issue.
  1081. */
  1082. if ($row['notify_type'] === 'topic')
  1083. {
  1084. $update_notification['forum'][] = $row['user_id'];
  1085. }
  1086. }
  1087. }
  1088. unset($notify_rows);
  1089. // Now, we are able to really send out notifications
  1090. if (sizeof($msg_users))
  1091. {
  1092. include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
  1093. $messenger = new messenger();
  1094. $msg_list_ary = array();
  1095. foreach ($msg_users as $row)
  1096. {
  1097. $pos = (!isset($msg_list_ary[$row['template']])) ? 0 : sizeof($msg_list_ary[$row['template']]);
  1098. $msg_list_ary[$row['template']][$pos]['method'] = $row['method'];
  1099. $msg_list_ary[$row['template']][$pos]['email'] = $row['user_email'];
  1100. $msg_list_ary[$row['template']][$pos]['jabber'] = $row['user_jabber'];
  1101. $msg_list_ary[$row['template']][$pos]['name'] = $row['username'];
  1102. $msg_list_ary[$row['template']][$pos]['lang'] = $row['user_lang'];
  1103. $msg_list_ary[$row['template']][$pos]['user_id']= $row['user_id'];
  1104. }
  1105. unset($msg_users);
  1106. foreach ($msg_list_ary as $email_template => $email_list)
  1107. {
  1108. foreach ($email_list as $addr)
  1109. {
  1110. $messenger->template($email_template, $addr['lang']);
  1111. $messenger->to($addr['email'], $addr['name']);
  1112. $messenger->im($addr['jabber'], $addr['name']);
  1113. $messenger->assign_vars(array(
  1114. 'USERNAME' => htmlspecialchars_decode($addr['name']),
  1115. 'TOPIC_TITLE' => htmlspecialchars_decode($topic_title),
  1116. 'FORUM_NAME' => htmlspecialchars_decode($forum_name),
  1117. 'U_FORUM' => generate_board_url() . "/viewforum.$phpEx?f=$forum_id",
  1118. 'U_TOPIC' => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id",
  1119. 'U_NEWEST_POST' => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id&p=$post_id&e=$post_id",
  1120. 'U_STOP_WATCHING_TOPIC' => generate_board_url() . "/viewtopic.$phpEx?uid={$addr['user_id']}&f=$forum_id&t=$topic_id&unwatch=topic",
  1121. 'U_STOP_WATCHING_FORUM' => generate_board_url() . "/viewforum.$phpEx?uid={$addr['user_id']}&f=$forum_id&unwatch=forum",
  1122. ));
  1123. $messenger->send($addr['method']);
  1124. }
  1125. }
  1126. unset($msg_list_ary);
  1127. $messenger->save_queue();
  1128. }
  1129. // Handle the DB updates
  1130. $db->sql_transaction('begin');
  1131. if (!empty($update_notification['topic']))
  1132. {
  1133. $sql = 'UPDATE ' . TOPICS_WATCH_TABLE . '
  1134. SET notify_status = ' . NOTIFY_NO . "
  1135. WHERE topic_id = $topic_id
  1136. AND " . $db->sql_in_set('user_id', $update_notification['topic']);
  1137. $db->sql_query($sql);
  1138. }
  1139. if (!empty($update_notification['forum']))
  1140. {
  1141. $sql = 'UPDATE ' . FORUMS_WATCH_TABLE . '
  1142. SET notify_status = ' . NOTIFY_NO . "
  1143. WHERE forum_id = $forum_id
  1144. AND " . $db->sql_in_set('user_id', $update_notification['forum']);
  1145. $db->sql_query($sql);
  1146. }
  1147. // Now delete the user_ids not authorised to receive notifications on this topic/forum
  1148. if (!empty($delete_ids['topic']))
  1149. {
  1150. $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . "
  1151. WHERE topic_id = $topic_id
  1152. AND " . $db->sql_in_set('user_id', $delete_ids['topic']);
  1153. $db->sql_query($sql);
  1154. }
  1155. if (!empty($delete_ids['forum']))
  1156. {
  1157. $sql = 'DELETE FROM ' . FORUMS_WATCH_TABLE . "
  1158. WHERE forum_id = $forum_id
  1159. AND " . $db->sql_in_set('user_id', $delete_ids['forum']);
  1160. $db->sql_query($sql);
  1161. }
  1162. $db->sql_transaction('commit');
  1163. }
  1164. //
  1165. // Post handling functions
  1166. //
  1167. /**
  1168. * Delete Post
  1169. */
  1170. function delete_post($forum_id, $topic_id, $post_id, &$data)
  1171. {
  1172. global $db, $user, $auth;
  1173. global $config, $phpEx, $phpbb_root_path;
  1174. // Specify our post mode
  1175. $post_mode = 'delete';
  1176. if (($data['topic_first_post_id'] === $data['topic_last_post_id']) && $data['topic_replies_real'] == 0)
  1177. {
  1178. $post_mode = 'delete_topic';
  1179. }
  1180. else if ($data['topic_first_post_id'] == $post_id)
  1181. {
  1182. $post_mode = 'delete_first_post';
  1183. }
  1184. else if ($data['topic_last_post_id'] == $post_id)
  1185. {
  1186. $post_mode = 'delete_last_post';
  1187. }
  1188. $sql_data = array();
  1189. $next_post_id = false;
  1190. include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  1191. $db->sql_transaction('begin');
  1192. // we must make sure to update forums that contain the shadow'd topic
  1193. if ($post_mode == 'delete_topic')
  1194. {
  1195. $shadow_forum_ids = array();
  1196. $sql = 'SELECT forum_id
  1197. FROM ' . TOPICS_TABLE . '
  1198. WHERE ' . $db->sql_in_set('topic_moved_id', $topic_id);
  1199. $result = $db->sql_query($sql);
  1200. while ($row = $db->sql_fetchrow($result))
  1201. {
  1202. if (!isset($shadow_forum_ids[(int) $row['forum_id']]))
  1203. {
  1204. $shadow_forum_ids[(int) $row['forum_id']] = 1;
  1205. }
  1206. else
  1207. {
  1208. $shadow_forum_ids[(int) $row['forum_id']]++;
  1209. }
  1210. }
  1211. $db->sql_freeresult($result);
  1212. }
  1213. if (!delete_posts('post_id', array($post_id), false, false))
  1214. {
  1215. // Try to delete topic, we may had an previous error causing inconsistency
  1216. if ($post_mode == 'delete_topic')
  1217. {
  1218. delete_topics('topic_id', array($topic_id), false);
  1219. }
  1220. trigger_error('ALREADY_DELETED');
  1221. }
  1222. $db->sql_transaction('commit');
  1223. // Collect the necessary information for updating the tables
  1224. $sql_data[FORUMS_TABLE] = '';
  1225. switch ($post_mode)
  1226. {
  1227. case 'delete_topic':
  1228. foreach ($shadow_forum_ids as $updated_forum => $topic_count)
  1229. {
  1230. // counting is fun! we only have to do sizeof($forum_ids) number of queries,
  1231. // even if the topic is moved back to where its shadow lives (we count how many times it is in a forum)
  1232. $db->sql_query('UPDATE ' . FORUMS_TABLE . ' SET forum_topics_real = forum_topics_real - ' . $topic_count . ', forum_topics = forum_topics - ' . $topic_count . ' WHERE forum_id = ' . $updated_forum);
  1233. update_post_information('forum', $updated_forum);
  1234. }
  1235. delete_topics('topic_id', array($topic_id), false);
  1236. if ($data['topic_type'] != POST_GLOBAL)
  1237. {
  1238. $sql_data[FORUMS_TABLE] .= 'forum_topics_real = forum_topics_real - 1';
  1239. $sql_data[FORUMS_TABLE] .= ($data['topic_approved']) ? ', forum_posts = forum_posts - 1, forum_topics = forum_topics - 1' : '';
  1240. }
  1241. $update_sql = update_post_information('forum', $forum_id, true);
  1242. if (sizeof($update_sql))
  1243. {
  1244. $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
  1245. $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
  1246. }
  1247. break;
  1248. case 'delete_first_post':
  1249. $sql = 'SELECT p.post_id, p.poster_id, p.post_time, p.post_username, u.username, u.user_colour
  1250. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
  1251. WHERE p.topic_id = $topic_id
  1252. AND p.poster_id = u.user_id
  1253. ORDER BY p.post_time ASC";
  1254. $result = $db->sql_query_limit($sql, 1);
  1255. $row = $db->sql_fetchrow($result);
  1256. $db->sql_freeresult($result);
  1257. if ($data['topic_type'] != POST_GLOBAL)
  1258. {
  1259. $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
  1260. }
  1261. $sql_data[TOPICS_TABLE] = 'topic_poster = ' . intval($row['poster_id']) . ', topic_first_post_id = ' . intval($row['post_id']) . ", topic_first_poster_colour = '" . $db->sql_escape($row['user_colour']) . "', topic_first_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "', topic_time = " . (int) $row['post_time'];
  1262. // Decrementing topic_replies here is fine because this case only happens if there is more than one post within the topic - basically removing one "reply"
  1263. $sql_data[TOPICS_TABLE] .= ', topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
  1264. $next_post_id = (int) $row['post_id'];
  1265. break;
  1266. case 'delete_last_post':
  1267. if ($data['topic_type'] != POST_GLOBAL)
  1268. {
  1269. $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
  1270. }
  1271. $update_sql = update_post_information('forum', $forum_id, true);
  1272. if (sizeof($update_sql))
  1273. {
  1274. $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
  1275. $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
  1276. }
  1277. $sql_data[TOPICS_TABLE] = 'topic_bumped = 0, topic_bumper = 0, topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
  1278. $update_sql = update_post_information('topic', $topic_id, true);
  1279. if (sizeof($update_sql))
  1280. {
  1281. $sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
  1282. $next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
  1283. }
  1284. else
  1285. {
  1286. $sql = 'SELECT MAX(post_id) as last_post_id
  1287. FROM ' . POSTS_TABLE . "
  1288. WHERE topic_id = $topic_id " .
  1289. ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '');
  1290. $result = $db->sql_query($sql);
  1291. $row = $db->sql_fetchrow($result);
  1292. $db->sql_freeresult($result);
  1293. $next_post_id = (int) $row['last_post_id'];
  1294. }
  1295. break;
  1296. case 'delete':
  1297. $sql = 'SELECT post_id
  1298. FROM ' . POSTS_TABLE . "
  1299. WHERE topic_id = $topic_id " .
  1300. ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '') . '
  1301. AND post_time > ' . $data['post_time'] . '
  1302. ORDER BY post_time ASC';
  1303. $result = $db->sql_query_limit($sql, 1);
  1304. $row = $db->sql_fetchrow($result);
  1305. $db->sql_freeresult($result);
  1306. if ($data['topic_type'] != POST_GLOBAL)
  1307. {
  1308. $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
  1309. }
  1310. $sql_data[TOPICS_TABLE] = 'topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
  1311. $next_post_id = (int) $row['post_id'];
  1312. break;
  1313. }
  1314. if (($post_mode == 'delete') || ($post_mode == 'delete_last_post') || ($post_mode == 'delete_first_post'))
  1315. {
  1316. $sql = 'SELECT 1 AS has_attachments
  1317. FROM ' . ATTACHMENTS_TABLE . '
  1318. WHERE topic_id = ' . $topic_id;
  1319. $result = $db->sql_query_limit($sql, 1);
  1320. $has_attachments = (int) $db->sql_fetchfield('has_attachments');
  1321. $db->sql_freeresult($result);
  1322. if (!$has_attachments)
  1323. {
  1324. $sql_data[TOPICS_TABLE] .= ', topic_attachment = 0';
  1325. }
  1326. }
  1327. // $sql_data[USERS_TABLE] = ($data['post_postcount']) ? 'user_posts = user_posts - 1' : '';
  1328. $db->sql_transaction('begin');
  1329. $where_sql = array(
  1330. FORUMS_TABLE => "forum_id = $forum_id",
  1331. TOPICS_TABLE => "topic_id = $topic_id",
  1332. USERS_TABLE => 'user_id = ' . $data['poster_id']
  1333. );
  1334. foreach ($sql_data as $table => $update_sql)
  1335. {
  1336. if ($update_sql)
  1337. {
  1338. $db->sql_query("UPDATE $table SET $update_sql WHERE " . $where_sql[$table]);
  1339. }
  1340. }
  1341. // Adjust posted info for this user by looking for a post by him/her within this topic...
  1342. if ($post_mode != 'delete_topic' && $config['load_db_track'] && $data['poster_id'] != ANONYMOUS)
  1343. {
  1344. $sql = 'SELECT poster_id
  1345. FROM ' . POSTS_TABLE . '
  1346. WHERE topic_id = ' . $topic_id . '
  1347. AND poster_id = ' . $data['poster_id'];
  1348. $result = $db->sql_query_limit($sql, 1);
  1349. $poster_id = (int) $db->sql_fetchfield('poster_id');
  1350. $db->sql_freeresult($result);
  1351. // The user is not having any more posts within this topic
  1352. if (!$poster_id)
  1353. {
  1354. $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
  1355. WHERE topic_id = ' . $topic_id . '
  1356. AND user_id = ' . $data['poster_id'];
  1357. $db->sql_query($sql);
  1358. }
  1359. }
  1360. $db->sql_transaction('commit');
  1361. if ($data['post_reported'] && ($post_mode != 'delete_topic'))
  1362. {
  1363. sync('topic_reported', 'topic_id', array($topic_id));
  1364. }
  1365. return $next_post_id;
  1366. }
  1367. /**
  1368. * Submit Post
  1369. * @todo Split up and create lightweight, simple API for this.
  1370. */
  1371. function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $update_message = true, $update_search_index = true)
  1372. {
  1373. global $db, $auth, $user, $config, $phpEx, $template, $phpbb_root_path;
  1374. // We do not handle erasing posts here
  1375. if ($mode == 'delete')
  1376. {
  1377. return false;
  1378. }
  1379. $current_time = time();
  1380. if ($mode == 'post')
  1381. {
  1382. $post_mode = 'post';
  1383. $update_message = true;
  1384. }
  1385. else if ($mode != 'edit')
  1386. {
  1387. $post_mode = 'reply';
  1388. $update_message = true;
  1389. }
  1390. else if ($mode == 'edit')
  1391. {
  1392. $post_mode = ($data['topic_replies_real'] == 0) ? 'edit_topic' : (($data['topic_first_post_id'] == $data['post_id']) ? 'edit_first_post' : (($data['topic_last_post_id'] == $data['post_id']) ? 'edit_last_post' : 'edit'));
  1393. }
  1394. // First of all make sure the subject and topic title are having the correct length.
  1395. // To achieve this without cutting off between special chars we convert to an array and then count the elements.
  1396. $subject = truncate_string($subject);
  1397. $data['topic_title'] = truncate_string($data['topic_title']);
  1398. // Collect some basic information about which tables and which rows to update/insert
  1399. $sql_data = $topic_row = array();
  1400. $poster_id = ($mode == 'edit') ? $data['poster_id'] : (int) $user->data['user_id'];
  1401. // Retrieve some additional information if not present
  1402. if ($mode == 'edit' && (!isset($data['post_approved']) || !isset($data['topic_approved']) || $data['post_approved'] === false || $data['topic_approved'] === false))
  1403. {
  1404. $sql = 'SELECT p.post_approved, t.topic_type, t.topic_replies, t.topic_replies_real, t.topic_approved
  1405. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
  1406. WHERE t.topic_id = p.topic_id
  1407. AND p.post_id = ' . $data['post_id'];
  1408. $result = $db->sql_query($sql);
  1409. $topic_row = $db->sql_fetchrow($result);
  1410. $db->sql_freeresult($result);
  1411. $data['topic_approved'] = $topic_row['topic_approved'];
  1412. $data['post_approved'] = $topic_row['post_approved'];
  1413. }
  1414. // This variable indicates if the user is able to post or put into the queue - it is used later for all code decisions regarding approval
  1415. // The variable name should be $post_approved, because it indicates if the post is approved or not
  1416. $post_approval = 1;
  1417. // Check the permissions for post approval. Moderators are not affected.
  1418. if (!$auth->acl_get('f_noapprove', $data['forum_id']) && !$auth->acl_get('m_approve', $data['forum_id']))
  1419. {
  1420. // Post not approved, but in queue
  1421. $post_approval = 0;
  1422. }
  1423. // Mods are able to force approved/unapproved posts. True means the post is approved, false the post is unapproved
  1424. if (isset($data['force_approved_state']))
  1425. {
  1426. $post_approval = ($data['force_approved_state']) ? 1 : 0;
  1427. }
  1428. // Start the transaction here
  1429. $db->sql_transaction('begin');
  1430. // Collect Information
  1431. switch (

Large files files are truncated, but you can click here to view the full file