PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/includes/functions_posting.php

https://github.com/GreyTeardrop/socionicasys-forum
PHP | 2754 lines | 2087 code | 407 blank | 260 comment | 508 complexity | ddef962ee9435a0ea03bef6bfc01abab MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, MPL-2.0-no-copyleft-exception

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. $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
  419. $filedata['post_attach'] = false;
  420. $file->remove();
  421. return $filedata;
  422. }
  423. }
  424. // Create Thumbnail
  425. if ($filedata['thumbnail'])
  426. {
  427. $source = $file->get('destination_file');
  428. $destination = $file->get('destination_path') . '/thumb_' . $file->get('realname');
  429. if (!create_thumbnail($source, $destination, $file->get('mimetype')))
  430. {
  431. $filedata['thumbnail'] = 0;
  432. }
  433. }
  434. return $filedata;
  435. }
  436. /**
  437. * Calculate the needed size for Thumbnail
  438. */
  439. function get_img_size_format($width, $height)
  440. {
  441. global $config;
  442. // Maximum Width the Image can take
  443. $max_width = ($config['img_max_thumb_width']) ? $config['img_max_thumb_width'] : 400;
  444. if ($width > $height)
  445. {
  446. return array(
  447. round($width * ($max_width / $width)),
  448. round($height * ($max_width / $width))
  449. );
  450. }
  451. else
  452. {
  453. return array(
  454. round($width * ($max_width / $height)),
  455. round($height * ($max_width / $height))
  456. );
  457. }
  458. }
  459. /**
  460. * Return supported image types
  461. */
  462. function get_supported_image_types($type = false)
  463. {
  464. if (@extension_loaded('gd'))
  465. {
  466. $format = imagetypes();
  467. $new_type = 0;
  468. if ($type !== false)
  469. {
  470. // Type is one of the IMAGETYPE constants - it is fetched from getimagesize()
  471. // We do not use the constants here, because some were not available in PHP 4.3.x
  472. switch ($type)
  473. {
  474. // GIF
  475. case 1:
  476. $new_type = ($format & IMG_GIF) ? IMG_GIF : false;
  477. break;
  478. // JPG, JPC, JP2
  479. case 2:
  480. case 9:
  481. case 10:
  482. case 11:
  483. case 12:
  484. $new_type = ($format & IMG_JPG) ? IMG_JPG : false;
  485. break;
  486. // PNG
  487. case 3:
  488. $new_type = ($format & IMG_PNG) ? IMG_PNG : false;
  489. break;
  490. // WBMP
  491. case 15:
  492. $new_type = ($format & IMG_WBMP) ? IMG_WBMP : false;
  493. break;
  494. }
  495. }
  496. else
  497. {
  498. $new_type = array();
  499. $go_through_types = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP);
  500. foreach ($go_through_types as $check_type)
  501. {
  502. if ($format & $check_type)
  503. {
  504. $new_type[] = $check_type;
  505. }
  506. }
  507. }
  508. return array(
  509. 'gd' => ($new_type) ? true : false,
  510. 'format' => $new_type,
  511. 'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1
  512. );
  513. }
  514. return array('gd' => false);
  515. }
  516. /**
  517. * Create Thumbnail
  518. */
  519. function create_thumbnail($source, $destination, $mimetype)
  520. {
  521. global $config;
  522. $min_filesize = (int) $config['img_min_thumb_filesize'];
  523. $img_filesize = (file_exists($source)) ? @filesize($source) : false;
  524. if (!$img_filesize || $img_filesize <= $min_filesize)
  525. {
  526. return false;
  527. }
  528. $dimension = @getimagesize($source);
  529. if ($dimension === false)
  530. {
  531. return false;
  532. }
  533. list($width, $height, $type, ) = $dimension;
  534. if (empty($width) || empty($height))
  535. {
  536. return false;
  537. }
  538. list($new_width, $new_height) = get_img_size_format($width, $height);
  539. // Do not create a thumbnail if the resulting width/height is bigger than the original one
  540. if ($new_width >= $width && $new_height >= $height)
  541. {
  542. return false;
  543. }
  544. $used_imagick = false;
  545. // Only use imagemagick if defined and the passthru function not disabled
  546. if ($config['img_imagick'] && function_exists('passthru'))
  547. {
  548. if (substr($config['img_imagick'], -1) !== '/')
  549. {
  550. $config['img_imagick'] .= '/';
  551. }
  552. @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) . '"');
  553. if (file_exists($destination))
  554. {
  555. $used_imagick = true;
  556. }
  557. }
  558. if (!$used_imagick)
  559. {
  560. $type = get_supported_image_types($type);
  561. if ($type['gd'])
  562. {
  563. // If the type is not supported, we are not able to create a thumbnail
  564. if ($type['format'] === false)
  565. {
  566. return false;
  567. }
  568. switch ($type['format'])
  569. {
  570. case IMG_GIF:
  571. $image = @imagecreatefromgif($source);
  572. break;
  573. case IMG_JPG:
  574. @ini_set('gd.jpeg_ignore_warning', 1);
  575. $image = @imagecreatefromjpeg($source);
  576. break;
  577. case IMG_PNG:
  578. $image = @imagecreatefrompng($source);
  579. break;
  580. case IMG_WBMP:
  581. $image = @imagecreatefromwbmp($source);
  582. break;
  583. }
  584. if (empty($image))
  585. {
  586. return false;
  587. }
  588. if ($type['version'] == 1)
  589. {
  590. $new_image = imagecreate($new_width, $new_height);
  591. if ($new_image === false)
  592. {
  593. return false;
  594. }
  595. imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  596. }
  597. else
  598. {
  599. $new_image = imagecreatetruecolor($new_width, $new_height);
  600. if ($new_image === false)
  601. {
  602. return false;
  603. }
  604. // Preserve alpha transparency (png for example)
  605. @imagealphablending($new_image, false);
  606. @imagesavealpha($new_image, true);
  607. imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  608. }
  609. // If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
  610. if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on')
  611. {
  612. @touch($destination);
  613. }
  614. switch ($type['format'])
  615. {
  616. case IMG_GIF:
  617. imagegif($new_image, $destination);
  618. break;
  619. case IMG_JPG:
  620. imagejpeg($new_image, $destination, 90);
  621. break;
  622. case IMG_PNG:
  623. imagepng($new_image, $destination);
  624. break;
  625. case IMG_WBMP:
  626. imagewbmp($new_image, $destination);
  627. break;
  628. }
  629. imagedestroy($new_image);
  630. }
  631. else
  632. {
  633. return false;
  634. }
  635. }
  636. if (!file_exists($destination))
  637. {
  638. return false;
  639. }
  640. phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
  641. return true;
  642. }
  643. /**
  644. * Assign Inline attachments (build option fields)
  645. */
  646. function posting_gen_inline_attachments(&$attachment_data)
  647. {
  648. global $template;
  649. if (sizeof($attachment_data))
  650. {
  651. $s_inline_attachment_options = '';
  652. foreach ($attachment_data as $i => $attachment)
  653. {
  654. $s_inline_attachment_options .= '<option value="' . $i . '">' . utf8_basename($attachment['real_filename']) . '</option>';
  655. }
  656. $template->assign_var('S_INLINE_ATTACHMENT_OPTIONS', $s_inline_attachment_options);
  657. return true;
  658. }
  659. return false;
  660. }
  661. /**
  662. * Generate inline attachment entry
  663. */
  664. function posting_gen_attachment_entry($attachment_data, &$filename_data, $show_attach_box = true)
  665. {
  666. global $template, $config, $phpbb_root_path, $phpEx, $user, $auth;
  667. // Some default template variables
  668. $template->assign_vars(array(
  669. 'S_SHOW_ATTACH_BOX' => $show_attach_box,
  670. 'S_HAS_ATTACHMENTS' => sizeof($attachment_data),
  671. 'FILESIZE' => $config['max_filesize'],
  672. 'FILE_COMMENT' => (isset($filename_data['filecomment'])) ? $filename_data['filecomment'] : '',
  673. ));
  674. if (sizeof($attachment_data))
  675. {
  676. // We display the posted attachments within the desired order.
  677. ($config['display_order']) ? krsort($attachment_data) : ksort($attachment_data);
  678. foreach ($attachment_data as $count => $attach_row)
  679. {
  680. $hidden = '';
  681. $attach_row['real_filename'] = utf8_basename($attach_row['real_filename']);
  682. foreach ($attach_row as $key => $value)
  683. {
  684. $hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
  685. }
  686. $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);
  687. $template->assign_block_vars('attach_row', array(
  688. 'FILENAME' => utf8_basename($attach_row['real_filename']),
  689. 'A_FILENAME' => addslashes(utf8_basename($attach_row['real_filename'])),
  690. 'FILE_COMMENT' => $attach_row['attach_comment'],
  691. 'ATTACH_ID' => $attach_row['attach_id'],
  692. 'S_IS_ORPHAN' => $attach_row['is_orphan'],
  693. 'ASSOC_INDEX' => $count,
  694. 'U_VIEW_ATTACHMENT' => $download_link,
  695. 'S_HIDDEN' => $hidden)
  696. );
  697. }
  698. }
  699. return sizeof($attachment_data);
  700. }
  701. //
  702. // General Post functions
  703. //
  704. /**
  705. * Load Drafts
  706. */
  707. function load_drafts($topic_id = 0, $forum_id = 0, $id = 0, $pm_action = '', $msg_id = 0)
  708. {
  709. global $user, $db, $template, $auth;
  710. global $phpbb_root_path, $phpEx;
  711. $topic_ids = $forum_ids = $draft_rows = array();
  712. // Load those drafts not connected to forums/topics
  713. // If forum_id == 0 AND topic_id == 0 then this is a PM draft
  714. if (!$topic_id && !$forum_id)
  715. {
  716. $sql_and = ' AND d.forum_id = 0 AND d.topic_id = 0';
  717. }
  718. else
  719. {
  720. $sql_and = '';
  721. $sql_and .= ($forum_id) ? ' AND d.forum_id = ' . (int) $forum_id : '';
  722. $sql_and .= ($topic_id) ? ' AND d.topic_id = ' . (int) $topic_id : '';
  723. }
  724. $sql = 'SELECT d.*, f.forum_id, f.forum_name
  725. FROM ' . DRAFTS_TABLE . ' d
  726. LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = d.forum_id)
  727. WHERE d.user_id = ' . $user->data['user_id'] . "
  728. $sql_and
  729. ORDER BY d.save_time DESC";
  730. $result = $db->sql_query($sql);
  731. while ($row = $db->sql_fetchrow($result))
  732. {
  733. if ($row['topic_id'])
  734. {
  735. $topic_ids[] = (int) $row['topic_id'];
  736. }
  737. $draft_rows[] = $row;
  738. }
  739. $db->sql_freeresult($result);
  740. if (!sizeof($draft_rows))
  741. {
  742. return;
  743. }
  744. $topic_rows = array();
  745. if (sizeof($topic_ids))
  746. {
  747. $sql = 'SELECT topic_id, forum_id, topic_title
  748. FROM ' . TOPICS_TABLE . '
  749. WHERE ' . $db->sql_in_set('topic_id', array_unique($topic_ids));
  750. $result = $db->sql_query($sql);
  751. while ($row = $db->sql_fetchrow($result))
  752. {
  753. $topic_rows[$row['topic_id']] = $row;
  754. }
  755. $db->sql_freeresult($result);
  756. }
  757. unset($topic_ids);
  758. $template->assign_var('S_SHOW_DRAFTS', true);
  759. foreach ($draft_rows as $draft)
  760. {
  761. $link_topic = $link_forum = $link_pm = false;
  762. $insert_url = $view_url = $title = '';
  763. if (isset($topic_rows[$draft['topic_id']])
  764. && (
  765. ($topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id']))
  766. ||
  767. (!$topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_getf_global('f_read'))
  768. ))
  769. {
  770. $topic_forum_id = ($topic_rows[$draft['topic_id']]['forum_id']) ? $topic_rows[$draft['topic_id']]['forum_id'] : $forum_id;
  771. $link_topic = true;
  772. $view_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id']);
  773. $title = $topic_rows[$draft['topic_id']]['topic_title'];
  774. $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']);
  775. }
  776. else if ($draft['forum_id'] && $auth->acl_get('f_read', $draft['forum_id']))
  777. {
  778. $link_forum = true;
  779. $view_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $draft['forum_id']);
  780. $title = $draft['forum_name'];
  781. $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $draft['forum_id'] . '&amp;mode=post&amp;d=' . $draft['draft_id']);
  782. }
  783. else
  784. {
  785. // Either display as PM draft if forum_id and topic_id are empty or if access to the forums has been denied afterwards...
  786. $link_pm = true;
  787. $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" : ''));
  788. }
  789. $template->assign_block_vars('draftrow', array(
  790. 'DRAFT_ID' => $draft['draft_id'],
  791. 'DATE' => $user->format_date($draft['save_time']),
  792. 'DRAFT_SUBJECT' => $draft['draft_subject'],
  793. 'TITLE' => $title,
  794. 'U_VIEW' => $view_url,
  795. 'U_INSERT' => $insert_url,
  796. 'S_LINK_PM' => $link_pm,
  797. 'S_LINK_TOPIC' => $link_topic,
  798. 'S_LINK_FORUM' => $link_forum)
  799. );
  800. }
  801. }
  802. /**
  803. * Topic Review
  804. */
  805. function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id = 0, $show_quote_button = true)
  806. {
  807. global $user, $auth, $db, $template, $bbcode, $cache;
  808. global $config, $phpbb_root_path, $phpEx;
  809. // Go ahead and pull all data for this topic
  810. $sql = 'SELECT p.post_id
  811. FROM ' . POSTS_TABLE . ' p' . "
  812. WHERE p.topic_id = $topic_id
  813. " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . '
  814. ' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
  815. ' . (($mode == 'post_review_edit') ? " AND p.post_id = $cur_post_id" : '') . '
  816. ORDER BY p.post_time ';
  817. $sql .= ($mode == 'post_review') ? 'ASC' : 'DESC';
  818. $result = $db->sql_query_limit($sql, $config['posts_per_page']);
  819. $post_list = array();
  820. while ($row = $db->sql_fetchrow($result))
  821. {
  822. $post_list[] = $row['post_id'];
  823. }
  824. $db->sql_freeresult($result);
  825. if (!sizeof($post_list))
  826. {
  827. return false;
  828. }
  829. // Handle 'post_review_edit' like 'post_review' from now on
  830. if ($mode == 'post_review_edit')
  831. {
  832. $mode = 'post_review';
  833. }
  834. $sql = $db->sql_build_query('SELECT', array(
  835. 'SELECT' => 'u.username, u.user_id, u.user_colour, p.*, z.friend, z.foe',
  836. 'FROM' => array(
  837. USERS_TABLE => 'u',
  838. POSTS_TABLE => 'p',
  839. ),
  840. 'LEFT_JOIN' => array(
  841. array(
  842. 'FROM' => array(ZEBRA_TABLE => 'z'),
  843. 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
  844. )
  845. ),
  846. 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
  847. AND u.user_id = p.poster_id'
  848. ));
  849. //-- mod: Prime Trash Bin (Posts) -------------------------------------------//
  850. // When doing topic review, get only posts that aren't mock-deleted.
  851. include ($phpbb_root_path . 'includes/prime_trash_bin_a.' . $phpEx);
  852. if (stifle_topics_enabled())
  853. {
  854. $sql = str_replace('AND u.user_id = p.poster_id', 'AND u.user_id = p.poster_id AND p.post_deleted_time = 0', $sql);
  855. }
  856. //-- end: Prime Trash Bin (Posts) -------------------------------------------//
  857. $result = $db->sql_query($sql);
  858. $bbcode_bitfield = '';
  859. $rowset = array();
  860. $has_attachments = false;
  861. while ($row = $db->sql_fetchrow($result))
  862. {
  863. $rowset[$row['post_id']] = $row;
  864. $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
  865. if ($row['post_attachment'])
  866. {
  867. $has_attachments = true;
  868. }
  869. }
  870. $db->sql_freeresult($result);
  871. // Instantiate BBCode class
  872. if (!isset($bbcode) && $bbcode_bitfield !== '')
  873. {
  874. include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
  875. $bbcode = new bbcode(base64_encode($bbcode_bitfield));
  876. }
  877. // Grab extensions
  878. $extensions = $attachments = array();
  879. if ($has_attachments && $auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
  880. {
  881. $extensions = $cache->obtain_attach_extensions($forum_id);
  882. // Get attachments...
  883. $sql = 'SELECT *
  884. FROM ' . ATTACHMENTS_TABLE . '
  885. WHERE ' . $db->sql_in_set('post_msg_id', $post_list) . '
  886. AND in_message = 0
  887. ORDER BY filetime DESC, post_msg_id ASC';
  888. $result = $db->sql_query($sql);
  889. while ($row = $db->sql_fetchrow($result))
  890. {
  891. $attachments[$row['post_msg_id']][] = $row;
  892. }
  893. $db->sql_freeresult($result);
  894. }
  895. for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
  896. {
  897. // A non-existing rowset only happens if there was no user present for the entered poster_id
  898. // This could be a broken posts table.
  899. if (!isset($rowset[$post_list[$i]]))
  900. {
  901. continue;
  902. }
  903. $row =& $rowset[$post_list[$i]];
  904. $poster_id = $row['user_id'];
  905. $post_subject = $row['post_subject'];
  906. $message = censor_text($row['post_text']);
  907. $decoded_message = false;
  908. if ($show_quote_button && $auth->acl_get('f_reply', $forum_id))
  909. {
  910. $decoded_message = $message;
  911. decode_message($decoded_message, $row['bbcode_uid']);
  912. $decoded_message = bbcode_nl2br($decoded_message);
  913. }
  914. if ($row['bbcode_bitfield'])
  915. {
  916. $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
  917. }
  918. $message = bbcode_nl2br($message);
  919. $message = smiley_text($message, !$row['enable_smilies']);
  920. if (!empty($attachments[$row['post_id']]))
  921. {
  922. $update_count = array();
  923. parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
  924. }
  925. $post_subject = censor_text($post_subject);
  926. $post_anchor = ($mode == 'post_review') ? 'ppr' . $row['post_id'] : 'pr' . $row['post_id'];
  927. $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']}");
  928. $template->assign_block_vars($mode . '_row', array(
  929. 'POST_AUTHOR_FULL' => get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  930. 'POST_AUTHOR_COLOUR' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  931. 'POST_AUTHOR' => get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  932. 'U_POST_AUTHOR' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  933. 'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
  934. 'S_FRIEND' => ($row['friend']) ? true : false,
  935. 'S_IGNORE_POST' => ($row['foe']) ? true : false,
  936. '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>') : '',
  937. 'POST_SUBJECT' => $post_subject,
  938. 'MINI_POST_IMG' => $user->img('icon_post_target', $user->lang['POST']),
  939. 'POST_DATE' => $user->format_date($row['post_time']),
  940. 'MESSAGE' => $message,
  941. 'DECODED_MESSAGE' => $decoded_message,
  942. 'POST_ID' => $row['post_id'],
  943. 'U_MINI_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
  944. '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) : '',
  945. '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'])) : '')
  946. );
  947. //-- mod: Prime Trash Bin (Posts) -------------------------------------------//
  948. // Set up what we're going to display for the deleted message (topic review).
  949. if (!empty($row['post_deleted_time']))
  950. {
  951. include ($phpbb_root_path . 'includes/prime_trash_bin_a.' . $phpEx);
  952. set_stifled_post_template_vars($row, $message, $post_subject, ($blockname = $mode . '_row'));
  953. }
  954. //-- end: Prime Trash Bin (Posts) -------------------------------------------//
  955. // Display not already displayed Attachments for this post, we already parsed them. ;)
  956. if (!empty($attachments[$row['post_id']]))
  957. {
  958. foreach ($attachments[$row['post_id']] as $attachment)
  959. {
  960. $template->assign_block_vars($mode . '_row.attachment', array(
  961. 'DISPLAY_ATTACHMENT' => $attachment)
  962. );
  963. }
  964. }
  965. unset($rowset[$post_list[$i]]);
  966. }
  967. if ($mode == 'topic_review')
  968. {
  969. $template->assign_var('QUOTE_IMG', $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']));
  970. }
  971. return true;
  972. }
  973. /**
  974. * User Notification
  975. */
  976. function user_notification($mode, $subject, $topic_title, $forum_name, $forum_id, $topic_id, $post_id)
  977. {
  978. global $db, $user, $config, $phpbb_root_path, $phpEx, $auth;
  979. $topic_notification = ($mode == 'reply' || $mode == 'quote') ? true : false;
  980. $forum_notification = ($mode == 'post') ? true : false;
  981. if (!$topic_notification && !$forum_notification)
  982. {
  983. trigger_error('NO_MODE');
  984. }
  985. if (($topic_notification && !$config['allow_topic_notify']) || ($forum_notification && !$config['allow_forum_notify']))
  986. {
  987. return;
  988. }
  989. $topic_title = ($topic_notification) ? $topic_title : $subject;
  990. $topic_title = censor_text($topic_title);
  991. // Get banned User ID's
  992. $sql = 'SELECT ban_userid
  993. FROM ' . BANLIST_TABLE . '
  994. WHERE ban_userid <> 0
  995. AND ban_exclude <> 1';
  996. $result = $db->sql_query($sql);
  997. $sql_ignore_users = ANONYMOUS . ', ' . $user->data['user_id'];
  998. while ($row = $db->sql_fetchrow($result))
  999. {
  1000. $sql_ignore_users .= ', ' . (int) $row['ban_userid'];
  1001. }
  1002. $db->sql_freeresult($result);
  1003. $notify_rows = array();
  1004. // -- get forum_userids || topic_userids
  1005. $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
  1006. FROM ' . (($topic_notification) ? TOPICS_WATCH_TABLE : FORUMS_WATCH_TABLE) . ' w, ' . USERS_TABLE . ' u
  1007. WHERE w.' . (($topic_notification) ? 'topic_id' : 'forum_id') . ' = ' . (($topic_notification) ? $topic_id : $forum_id) . "
  1008. AND w.user_id NOT IN ($sql_ignore_users)
  1009. AND w.notify_status = " . NOTIFY_YES . '
  1010. AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
  1011. AND u.user_id = w.user_id';
  1012. $result = $db->sql_query($sql);
  1013. while ($row = $db->sql_fetchrow($result))
  1014. {
  1015. $notify_rows[$row['user_id']] = array(
  1016. 'user_id' => $row['user_id'],
  1017. 'username' => $row['username'],
  1018. 'user_email' => $row['user_email'],
  1019. 'user_jabber' => $row['user_jabber'],
  1020. 'user_lang' => $row['user_lang'],
  1021. 'notify_type' => ($topic_notification) ? 'topic' : 'forum',
  1022. 'template' => ($topic_notification) ? 'topic_notify' : 'newtopic_notify',
  1023. 'method' => $row['user_notify_type'],
  1024. 'allowed' => false
  1025. );
  1026. }
  1027. $db->sql_freeresult($result);
  1028. // forum notification is sent to those not already receiving topic notifications
  1029. if ($topic_notification)
  1030. {
  1031. if (sizeof($notify_rows))
  1032. {
  1033. $sql_ignore_users .= ', ' . implode(', ', array_keys($notify_rows));
  1034. }
  1035. $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
  1036. FROM ' . FORUMS_WATCH_TABLE . ' fw, ' . USERS_TABLE . " u
  1037. WHERE fw.forum_id = $forum_id
  1038. AND fw.user_id NOT IN ($sql_ignore_users)
  1039. AND fw.notify_status = " . NOTIFY_YES . '
  1040. AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
  1041. AND u.user_id = fw.user_id';
  1042. $result = $db->sql_query($sql);
  1043. while ($row = $db->sql_fetchrow($result))
  1044. {
  1045. $notify_rows[$row['user_id']] = array(
  1046. 'user_id' => $row['user_id'],
  1047. 'username' => $row['username'],
  1048. 'user_email' => $row['user_email'],
  1049. 'user_jabber' => $row['user_jabber'],
  1050. 'user_lang' => $row['user_lang'],
  1051. 'notify_type' => 'forum',
  1052. 'template' => 'forum_notify',
  1053. 'method' => $row['user_notify_type'],
  1054. 'allowed' => false
  1055. );
  1056. }
  1057. $db->sql_freeresult($result);
  1058. }
  1059. if (!sizeof($notify_rows))
  1060. {
  1061. return;
  1062. }
  1063. // Make sure users are allowed to read the forum
  1064. foreach ($auth->acl_get_list(array_keys($notify_rows), 'f_read', $forum_id) as $forum_id => $forum_ary)
  1065. {
  1066. foreach ($forum_ary as $auth_option => $user_ary)
  1067. {
  1068. foreach ($user_ary as $user_id)
  1069. {
  1070. $notify_rows[$user_id]['allowed'] = true;
  1071. }
  1072. }
  1073. }
  1074. // Now, we have to do a little step before really sending, we need to distinguish our users a little bit. ;)
  1075. $msg_users = $delete_ids = $update_notification = array();
  1076. foreach ($notify_rows as $user_id => $row)
  1077. {
  1078. if (!$row['allowed'] || !trim($row['user_email']))
  1079. {
  1080. $delete_ids[$row['notify_type']][] = $row['user_id'];
  1081. }
  1082. else
  1083. {
  1084. $msg_users[] = $row;
  1085. $update_notification[$row['notify_type']][] = $row['user_id'];
  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. // www.phpBB-SEO.com SEO TOOLKIT BEGIN
  1114. global $phpbb_seo;
  1115. $phpbb_seo->set_url(htmlspecialchars_decode($forum_name), $forum_id, 'forum');
  1116. $phpbb_seo->prepare_iurl(array('topic_id' => $topic_id, 'topic_title' => htmlspecialchars_decode($topic_title)), 'topic', $phpbb_seo->seo_url['forum'][$forum_id]);
  1117. $messenger->assign_vars(array(
  1118. 'USERNAME' => htmlspecialchars_decode($addr['name']),
  1119. 'TOPIC_TITLE' => htmlspecialchars_decode($topic_title),
  1120. 'FORUM_NAME' => htmlspecialchars_decode($forum_name),
  1121. 'U_FORUM' => !empty($phpbb_seo->seo_opt['url_rewrite']) ? $phpbb_seo->drop_sid(append_sid("{$phpbb_root_path}viewforum.$phpEx?f=$forum_id")) : generate_board_url() . "/viewforum.$phpEx?f=$forum_id",
  1122. 'U_TOPIC' => !empty($phpbb_seo->seo_opt['url_rewrite']) ? $phpbb_seo->drop_sid(append_sid("{$phpbb_root_path}viewtopic.$phpEx?f=$forum_id&amp;t=$topic_id")) : generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id",
  1123. // www.phpBB-SEO.com SEO TOOLKIT END
  1124. 'U_NEWEST_POST' => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id&p=$post_id&e=$post_id",
  1125. 'U_STOP_WATCHING_TOPIC' => generate_board_url() . "/viewtopic.$phpEx?uid={$addr['user_id']}&f=$forum_id&t=$topic_id&unwatch=topic",
  1126. 'U_STOP_WATCHING_FORUM' => generate_board_url() . "/viewforum.$phpEx?uid={$addr['user_id']}&f=$forum_id&unwatch=forum",
  1127. ));
  1128. $messenger->send($addr['method']);
  1129. }
  1130. }
  1131. unset($msg_list_ary);
  1132. $messenger->save_queue();
  1133. }
  1134. // Handle the DB updates
  1135. $db->sql_transaction('begin');
  1136. if (!empty($update_notification['topic']))
  1137. {
  1138. $sql = 'UPDATE ' . TOPICS_WATCH_TABLE . '
  1139. SET notify_status = ' . NOTIFY_NO . "
  1140. WHERE topic_id = $topic_id
  1141. AND " . $db->sql_in_set('user_id', $update_notification['topic']);
  1142. $db->sql_query($sql);
  1143. }
  1144. if (!empty($update_notification['forum']))
  1145. {
  1146. $sql = 'UPDATE ' . FORUMS_WATCH_TABLE . '
  1147. SET notify_status = ' . NOTIFY_NO . "
  1148. WHERE forum_id = $forum_id
  1149. AND " . $db->sql_in_set('user_id', $update_notification['forum']);
  1150. $db->sql_query($sql);
  1151. }
  1152. // Now delete the user_ids not authorised to receive notifications on this topic/forum
  1153. if (!empty($delete_ids['topic']))
  1154. {
  1155. $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . "
  1156. WHERE topic_id = $topic_id
  1157. AND " . $db->sql_in_set('user_id', $delete_ids['topic']);
  1158. $db->sql_query($sql);
  1159. }
  1160. if (!empty($delete_ids['forum']))
  1161. {
  1162. $sql = 'DELETE FROM ' . FORUMS_WATCH_TABLE . "
  1163. WHERE forum_id = $forum_id
  1164. AND " . $db->sql_in_set('user_id', $delete_ids['forum']);
  1165. $db->sql_query($sql);
  1166. }
  1167. $db->sql_transaction('commit');
  1168. }
  1169. //
  1170. // Post handling functions
  1171. //
  1172. /**
  1173. * Delete Post
  1174. */
  1175. function delete_post($forum_id, $topic_id, $post_id, &$data)
  1176. {
  1177. global $db, $user, $auth;
  1178. global $config, $phpEx, $phpbb_root_path;
  1179. // Specify our post mode
  1180. $post_mode = 'delete';
  1181. if (($data['topic_first_post_id'] === $data['topic_last_post_id']) && $data['topic_replies_real'] == 0)
  1182. {
  1183. $post_mode = 'delete_topic';
  1184. }
  1185. else if ($data['topic_first_post_id'] == $post_id)
  1186. {
  1187. $post_mode = 'delete_first_post';
  1188. }
  1189. else if ($data['topic_last_post_id'] == $post_id)
  1190. {
  1191. $post_mode = 'delete_last_post';
  1192. }
  1193. $sql_data = array();
  1194. $next_post_id = false;
  1195. include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  1196. $db->sql_transaction('begin');
  1197. // we must make sure to update forums that contain the shadow'd topic
  1198. if ($post_mode == 'delete_topic')
  1199. {
  1200. $shadow_forum_ids = array();
  1201. $sql = 'SELECT forum_id
  1202. FROM ' . TOPICS_TABLE . '
  1203. WHERE ' . $db->sql_in_set('topic_moved_id', $topic_id);
  1204. $result = $db->sql_query($sql);
  1205. while ($row = $db->sql_fetchrow($result))
  1206. {
  1207. if (!isset($shadow_forum_ids[(int) $row['forum_id']]))
  1208. {
  1209. $shadow_forum_ids[(int) $row['forum_id']] = 1;
  1210. }
  1211. else
  1212. {
  1213. $shadow_forum_ids[(int) $row['forum_id']]++;
  1214. }
  1215. }
  1216. $db->sql_freeresult($result);
  1217. }
  1218. if (!delete_posts('post_id', array($post_id), false, false))
  1219. {
  1220. // Try to delete topic, we may had an previous error causing inconsistency
  1221. if ($post_mode == 'delete_topic')
  1222. {
  1223. delete_topics('topic_id', array($topic_id), false);
  1224. }
  1225. trigger_error('ALREADY_DELETED');
  1226. }
  1227. $db->sql_transaction('commit');
  1228. // Collect the necessary information for updating the tables
  1229. $sql_data[FORUMS_TABLE] = '';
  1230. switch ($post_mode)
  1231. {
  1232. case 'delete_topic':
  1233. foreach ($shadow_forum_ids as $updated_forum => $topic_count)
  1234. {
  1235. // counting is fun! we only have to do sizeof($forum_ids) number of queries,
  1236. // even if the topic is moved back to where its shadow lives (we count how many times it is in a forum)
  1237. $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);
  1238. update_post_information('forum', $updated_forum);
  1239. }
  1240. delete_topics('topic_id', array($topic_id), false);
  1241. if ($data['topic_type'] != POST_GLOBAL)
  1242. {
  1243. $sql_data[FORUMS_TABLE] .= 'forum_topics_real = forum_topics_real - 1';
  1244. $sql_data[FORUMS_TABLE] .= ($data['topic_approved']) ? ', forum_posts = forum_posts - 1, forum_topics = forum_topics - 1' : '';
  1245. }
  1246. $update_sql = update_post_information('forum', $forum_id, true);
  1247. if (sizeof($update_sql))
  1248. {
  1249. $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
  1250. $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
  1251. }
  1252. break;
  1253. case 'delete_first_post':
  1254. $sql = 'SELECT p.post_id, p.poster_id, p.post_time, p.post_username, u.username, u.user_colour
  1255. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
  1256. WHERE p.topic_id = $topic_id
  1257. AND p.poster_id = u.user_id
  1258. ORDER BY p.post_time ASC";
  1259. $result = $db->sql_query_limit($sql, 1);
  1260. $row = $db->sql_fetchrow($result);
  1261. $db->sql_freeresult($result);
  1262. if ($data['topic_type'] != POST_GLOBAL)
  1263. {
  1264. $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
  1265. }
  1266. $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'];
  1267. // 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"
  1268. $sql_data[TOPICS_TABLE] .= ', topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
  1269. $next_post_id = (int) $row['post_id'];
  1270. break;
  1271. case 'delete_last_post':
  1272. if ($data['topic_type'] != POST_GLOBAL)
  1273. {
  1274. $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
  1275. }
  1276. $update_sql = update_post_information('forum', $forum_id, true);
  1277. if (sizeof($update_sql))
  1278. {
  1279. $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
  1280. $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
  1281. }
  1282. $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' : '');
  1283. $update_sql = update_post_information('topic', $topic_id, true);
  1284. if (sizeof($update_sql))
  1285. {
  1286. $sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
  1287. $next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
  1288. }
  1289. else
  1290. {
  1291. $sql = 'SELECT MAX(post_id) as last_post_id
  1292. FROM ' . POSTS_TABLE . "
  1293. WHERE topic_id = $topic_id " .
  1294. ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '');
  1295. $result = $db->sql_query($sql);
  1296. $row = $db->sql_fetchrow($result);
  1297. $db->sql_freeresult($result);
  1298. $next_post_id = (int) $row['last_post_id'];
  1299. }
  1300. break;
  1301. case 'delete':
  1302. $sql = 'SELECT post_id
  1303. FROM ' . POSTS_TABLE . "
  1304. WHERE topic_id = $topic_id " .
  1305. ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '') . '
  1306. AND post_time > ' . $data['post_time'] . '
  1307. ORDER BY post_time ASC';
  1308. $result = $db->sql_query_limit($sql, 1);
  1309. $row = $db->sql_fetchrow($result);
  1310. $db->sql_freeresult($result);
  1311. if ($data['topic_type'] != POST_GLOBAL)
  1312. {
  1313. $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
  1314. }
  1315. $sql_data[TOPICS_TABLE] = 'topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
  1316. $next_post_id = (int) $row['post_id'];
  1317. break;
  1318. }
  1319. if (($post_mode == 'delete') || ($post_mode == 'delete_last_post') || ($post_mode == 'delete_first_post'))
  1320. {
  1321. $sql = 'SELECT 1 AS has_attachments
  1322. FROM ' . ATTACHMENTS_TABLE . '
  1323. WHERE topic_id = ' . $topic_id;
  1324. $result = $db->sql_query_limit($sql, 1);
  1325. $has_attachments = (int) $db->sql_fetchfield('has_attachments');
  1326. $db->sql_freeresult($result);
  1327. if (!$has_attachments)
  1328. {
  1329. $sql_data[TOPICS_TABLE] .= ', topic_attachment = 0';
  1330. }
  1331. }
  1332. // $sql_data[USERS_TABLE] = ($data['post_postcount']) ? 'user_posts = user_posts - 1' : '';
  1333. $db->sql_transaction('begin');
  1334. $where_sql = array(
  1335. FORUMS_TABLE => "forum_id = $forum_id",
  1336. TOPICS_TABLE => "topic_id = $topic_id",
  1337. USERS_TABLE => 'user_id = ' . $data['poster_id']
  1338. );
  1339. foreach ($sql_data as $table => $update_sql)
  1340. {
  1341. if ($update_sql)
  1342. {
  1343. $db->sql_query("UPDATE $table SET $update_sql WHERE " . $where_sql[$table]);
  1344. }
  1345. }
  1346. // Adjust posted info for this user by looking for a post by him/her within this topic...
  1347. if ($post_mode != 'delete_topic' && $config['load_db_track'] && $data['poster_id'] != ANONYMOUS)
  1348. {
  1349. $sql = 'SELECT poster_id
  1350. FROM ' . POSTS_TABLE . '
  1351. WHERE topic_id = ' . $topic_id . '
  1352. AND poster_id = ' . $data['poster_id'];
  1353. $result = $db->sql_query_limit($sql, 1);
  1354. $poster_id = (int) $db->sql_fetchfield('poster_id');
  1355. $db->sql_freeresult($result);
  1356. // The user is not having any more posts within this topic
  1357. if (!$poster_id)
  1358. {
  1359. $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
  1360. WHERE topic_id = ' . $topic_id . '
  1361. AND user_id = ' . $data['poster_id'];
  1362. $db->sql_query($sql);
  1363. }
  1364. }
  1365. $db->sql_transaction('commit');
  1366. if ($data['post_reported'] && ($post_mode != 'delete_topic'))
  1367. {
  1368. sync('topic_reported', 'topic_id', array($topic_id));
  1369. }
  1370. return $next_post_id;
  1371. }
  1372. /**
  1373. * Submit Post
  1374. * @todo Split up and create lightweight, simple API for this.
  1375. */
  1376. function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $update_message = true, $update_search_index = true)
  1377. {
  1378. global $db, $auth, $user, $config, $phpEx, $template, $phpbb_root_path;
  1379. // www.phpBB-SEO.com SEO TOOLKIT BEGIN
  1380. global $phpbb_seo;
  1381. // www.phpBB-SEO.com SEO TOOLKIT END
  1382. // We do not handle erasing posts here
  1383. if ($mode == 'delete')
  1384. {
  1385. return false;
  1386. }
  1387. $current_time = time();
  1388. if ($mode == 'post')
  1389. {
  1390. $post_mode = 'post';
  1391. $update_message = true;
  1392. }
  1393. else if ($mode != 'edit')
  1394. {
  1395. $post_mode = 'reply';
  1396. $update_message = true;
  1397. }
  1398. else if ($mode == 'edit')
  1399. {
  1400. $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'));
  1401. }
  1402. // First of all make sure the subject and topic title are having the correct length.
  1403. // To achieve this without cutting off between special chars we convert to an array and then count the elements.
  1404. $subject = truncate_string($subject);
  1405. $data['topic_title'] = truncate_string($data['topic_title']);
  1406. // Collect some basic information about which tables and which rows to update/insert
  1407. $sql_data = $topic_row = array();
  1408. $poster_id = ($mode == 'edit') ? $data['poster_id'] : (int) $user->data['user_id'];
  1409. // Retrieve some additional information if not present
  1410. if ($mode == 'edit' && (!isset($data['post_approved']) || !isset($data['topic_approved']) || $data['post_approved'] === false || $data['topic_approved'] === false))
  1411. {
  1412. $sql = 'SELECT p.post_approved, t.topic_type, t.topic_replies, t.topic_replies_real, t.topic_approved
  1413. FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
  1414. WHERE t.topic_id = p.topic_id
  1415. AND p.post_id = ' . $data['post_id'];
  1416. $result = $db->sql_query($sql);
  1417. $topic_row = $db->sql_fetchrow($result);
  1418. $db->sql_freeresult($result);
  1419. $data['topic_approved'] = $topic_row['topic_approved'];
  1420. $data['post_approved']

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