PageRenderTime 61ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/functions_posting.php

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

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