source: trunk/forum/includes/functions_posting.php

Last change on this file was 702, checked in by george, 15 years ago
  • Upraveno: Aktualizace fóra.
File size: 83.2 KB
Line 
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/**
12* @ignore
13*/
14if (!defined('IN_PHPBB'))
15{
16 exit;
17}
18
19/**
20* Fill smiley templates (or just the variables) with smilies, either in a window or inline
21*/
22function generate_smilies($mode, $forum_id)
23{
24 global $auth, $db, $user, $config, $template;
25 global $phpEx, $phpbb_root_path;
26
27 $start = request_var('start', 0);
28
29 if ($mode == 'window')
30 {
31 if ($forum_id)
32 {
33 $sql = 'SELECT forum_style
34 FROM ' . FORUMS_TABLE . "
35 WHERE forum_id = $forum_id";
36 $result = $db->sql_query_limit($sql, 1);
37 $row = $db->sql_fetchrow($result);
38 $db->sql_freeresult($result);
39
40 $user->setup('posting', (int) $row['forum_style']);
41 }
42 else
43 {
44 $user->setup('posting');
45 }
46
47 page_header($user->lang['SMILIES']);
48
49 $sql = 'SELECT COUNT(smiley_id) AS item_count
50 FROM ' . SMILIES_TABLE . '
51 GROUP BY smiley_url';
52 $result = $db->sql_query($sql, 3600);
53
54 $smiley_count = 0;
55 while ($row = $db->sql_fetchrow($result))
56 {
57 ++$smiley_count;
58 }
59 $db->sql_freeresult($result);
60
61 $template->set_filenames(array(
62 'body' => 'posting_smilies.html')
63 );
64
65 $template->assign_var('PAGINATION',
66 generate_pagination(append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&amp;f=' . $forum_id),
67 $smiley_count, $config['smilies_per_page'], $start, true)
68 );
69 }
70
71 $display_link = false;
72 if ($mode == 'inline')
73 {
74 $sql = 'SELECT smiley_id
75 FROM ' . SMILIES_TABLE . '
76 WHERE display_on_posting = 0';
77 $result = $db->sql_query_limit($sql, 1, 0, 3600);
78
79 if ($row = $db->sql_fetchrow($result))
80 {
81 $display_link = true;
82 }
83 $db->sql_freeresult($result);
84 }
85
86 if ($mode == 'window')
87 {
88 $sql = 'SELECT smiley_url, MIN(emotion) as emotion, MIN(code) AS code, smiley_width, smiley_height
89 FROM ' . SMILIES_TABLE . '
90 GROUP BY smiley_url, smiley_width, smiley_height
91 ORDER BY MIN(smiley_order)';
92 $result = $db->sql_query_limit($sql, $config['smilies_per_page'], $start, 3600);
93 }
94 else
95 {
96 $sql = 'SELECT *
97 FROM ' . SMILIES_TABLE . '
98 WHERE display_on_posting = 1
99 ORDER BY smiley_order';
100 $result = $db->sql_query($sql, 3600);
101 }
102
103 $smilies = array();
104 while ($row = $db->sql_fetchrow($result))
105 {
106 if (empty($smilies[$row['smiley_url']]))
107 {
108 $smilies[$row['smiley_url']] = $row;
109 }
110 }
111 $db->sql_freeresult($result);
112
113 if (sizeof($smilies))
114 {
115 $root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
116
117 foreach ($smilies as $row)
118 {
119 $template->assign_block_vars('smiley', array(
120 'SMILEY_CODE' => $row['code'],
121 'A_SMILEY_CODE' => addslashes($row['code']),
122 'SMILEY_IMG' => $root_path . $config['smilies_path'] . '/' . $row['smiley_url'],
123 'SMILEY_WIDTH' => $row['smiley_width'],
124 'SMILEY_HEIGHT' => $row['smiley_height'],
125 'SMILEY_DESC' => $row['emotion'])
126 );
127 }
128 }
129
130 if ($mode == 'inline' && $display_link)
131 {
132 $template->assign_vars(array(
133 'S_SHOW_SMILEY_LINK' => true,
134 'U_MORE_SMILIES' => append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&amp;f=' . $forum_id))
135 );
136 }
137
138 if ($mode == 'window')
139 {
140 page_footer();
141 }
142}
143
144/**
145* Update last post information
146* Should be used instead of sync() if only the last post information are out of sync... faster
147*
148* @param string $type Can be forum|topic
149* @param mixed $ids topic/forum ids
150* @param bool $return_update_sql true: SQL query shall be returned, false: execute SQL
151*/
152function update_post_information($type, $ids, $return_update_sql = false)
153{
154 global $db;
155
156 if (empty($ids))
157 {
158 return;
159 }
160 if (!is_array($ids))
161 {
162 $ids = array($ids);
163 }
164
165
166 $update_sql = $empty_forums = $not_empty_forums = array();
167
168 if ($type != 'topic')
169 {
170 $topic_join = ', ' . TOPICS_TABLE . ' t';
171 $topic_condition = 'AND t.topic_id = p.topic_id AND t.topic_approved = 1';
172 }
173 else
174 {
175 $topic_join = '';
176 $topic_condition = '';
177 }
178
179 if (sizeof($ids) == 1)
180 {
181 $sql = 'SELECT MAX(p.post_id) as last_post_id
182 FROM ' . POSTS_TABLE . " p $topic_join
183 WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
184 $topic_condition
185 AND p.post_approved = 1";
186 }
187 else
188 {
189 $sql = 'SELECT p.' . $type . '_id, MAX(p.post_id) as last_post_id
190 FROM ' . POSTS_TABLE . " p $topic_join
191 WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
192 $topic_condition
193 AND p.post_approved = 1
194 GROUP BY p.{$type}_id";
195 }
196 $result = $db->sql_query($sql);
197
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
206 if ($type == 'forum')
207 {
208 $not_empty_forums[] = $row['forum_id'];
209
210 if (empty($row['last_post_id']))
211 {
212 $empty_forums[] = $row['forum_id'];
213 }
214 }
215
216 $last_post_ids[] = $row['last_post_id'];
217 }
218 $db->sql_freeresult($result);
219
220 if ($type == 'forum')
221 {
222 $empty_forums = array_merge($empty_forums, array_diff($ids, $not_empty_forums));
223
224 foreach ($empty_forums as $void => $forum_id)
225 {
226 $update_sql[$forum_id][] = 'forum_last_post_id = 0';
227 $update_sql[$forum_id][] = "forum_last_post_subject = ''";
228 $update_sql[$forum_id][] = 'forum_last_post_time = 0';
229 $update_sql[$forum_id][] = 'forum_last_poster_id = 0';
230 $update_sql[$forum_id][] = "forum_last_poster_name = ''";
231 $update_sql[$forum_id][] = "forum_last_poster_colour = ''";
232 }
233 }
234
235 if (sizeof($last_post_ids))
236 {
237 $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
238 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
239 WHERE p.poster_id = u.user_id
240 AND ' . $db->sql_in_set('p.post_id', $last_post_ids);
241 $result = $db->sql_query($sql);
242
243 while ($row = $db->sql_fetchrow($result))
244 {
245 $update_sql[$row["{$type}_id"]][] = $type . '_last_post_id = ' . (int) $row['post_id'];
246 $update_sql[$row["{$type}_id"]][] = "{$type}_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
247 $update_sql[$row["{$type}_id"]][] = $type . '_last_post_time = ' . (int) $row['post_time'];
248 $update_sql[$row["{$type}_id"]][] = $type . '_last_poster_id = ' . (int) $row['poster_id'];
249 $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
250 $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "'";
251 }
252 $db->sql_freeresult($result);
253 }
254 unset($empty_forums, $ids, $last_post_ids);
255
256 if ($return_update_sql || !sizeof($update_sql))
257 {
258 return $update_sql;
259 }
260
261 $table = ($type == 'forum') ? FORUMS_TABLE : TOPICS_TABLE;
262
263 foreach ($update_sql as $update_id => $update_sql_ary)
264 {
265 $sql = "UPDATE $table
266 SET " . implode(', ', $update_sql_ary) . "
267 WHERE {$type}_id = $update_id";
268 $db->sql_query($sql);
269 }
270
271 return;
272}
273
274/**
275* Generate Topic Icons for display
276*/
277function posting_gen_topic_icons($mode, $icon_id)
278{
279 global $phpbb_root_path, $config, $template, $cache;
280
281 // Grab icons
282 $icons = $cache->obtain_icons();
283
284 if (!$icon_id)
285 {
286 $template->assign_var('S_NO_ICON_CHECKED', ' checked="checked"');
287 }
288
289 if (sizeof($icons))
290 {
291 foreach ($icons as $id => $data)
292 {
293 if ($data['display'])
294 {
295 $template->assign_block_vars('topic_icon', array(
296 'ICON_ID' => $id,
297 'ICON_IMG' => $phpbb_root_path . $config['icons_path'] . '/' . $data['img'],
298 'ICON_WIDTH' => $data['width'],
299 'ICON_HEIGHT' => $data['height'],
300
301 'S_CHECKED' => ($id == $icon_id) ? true : false,
302 'S_ICON_CHECKED' => ($id == $icon_id) ? ' checked="checked"' : '')
303 );
304 }
305 }
306
307 return true;
308 }
309
310 return false;
311}
312
313/**
314* Build topic types able to be selected
315*/
316function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL)
317{
318 global $auth, $user, $template, $topic_type;
319
320 $toggle = false;
321
322 $topic_types = array(
323 'sticky' => array('const' => POST_STICKY, 'lang' => 'POST_STICKY'),
324 'announce' => array('const' => POST_ANNOUNCE, 'lang' => 'POST_ANNOUNCEMENT'),
325 'global' => array('const' => POST_GLOBAL, 'lang' => 'POST_GLOBAL')
326 );
327
328 $topic_type_array = array();
329
330 foreach ($topic_types as $auth_key => $topic_value)
331 {
332 // We do not have a special post global announcement permission
333 $auth_key = ($auth_key == 'global') ? 'announce' : $auth_key;
334
335 if ($auth->acl_get('f_' . $auth_key, $forum_id))
336 {
337 $toggle = true;
338
339 $topic_type_array[] = array(
340 'VALUE' => $topic_value['const'],
341 'S_CHECKED' => ($cur_topic_type == $topic_value['const'] || ($forum_id == 0 && $topic_value['const'] == POST_GLOBAL)) ? ' checked="checked"' : '',
342 'L_TOPIC_TYPE' => $user->lang[$topic_value['lang']]
343 );
344 }
345 }
346
347 if ($toggle)
348 {
349 $topic_type_array = array_merge(array(0 => array(
350 'VALUE' => POST_NORMAL,
351 'S_CHECKED' => ($topic_type == POST_NORMAL) ? ' checked="checked"' : '',
352 'L_TOPIC_TYPE' => $user->lang['POST_NORMAL'])),
353
354 $topic_type_array
355 );
356
357 foreach ($topic_type_array as $array)
358 {
359 $template->assign_block_vars('topic_type', $array);
360 }
361
362 $template->assign_vars(array(
363 'S_TOPIC_TYPE_STICKY' => ($auth->acl_get('f_sticky', $forum_id)),
364 'S_TOPIC_TYPE_ANNOUNCE' => ($auth->acl_get('f_announce', $forum_id)))
365 );
366 }
367
368 return $toggle;
369}
370
371//
372// Attachment related functions
373//
374
375/**
376* Upload Attachment - filedata is generated here
377* Uses upload class
378*/
379function upload_attachment($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false, $local_filedata = false)
380{
381 global $auth, $user, $config, $db, $cache;
382 global $phpbb_root_path, $phpEx;
383
384 $filedata = array(
385 'error' => array()
386 );
387
388 include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
389 $upload = new fileupload();
390
391 if ($config['check_attachment_content'])
392 {
393 $upload->set_disallowed_content(explode('|', $config['mime_triggers']));
394 }
395
396 if (!$local)
397 {
398 $filedata['post_attach'] = ($upload->is_valid($form_name)) ? true : false;
399 }
400 else
401 {
402 $filedata['post_attach'] = true;
403 }
404
405 if (!$filedata['post_attach'])
406 {
407 $filedata['error'][] = $user->lang['NO_UPLOAD_FORM_FOUND'];
408 return $filedata;
409 }
410
411 $extensions = $cache->obtain_attach_extensions((($is_message) ? false : (int) $forum_id));
412 $upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
413
414 $file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->form_upload($form_name);
415
416 if ($file->init_error)
417 {
418 $filedata['post_attach'] = false;
419 return $filedata;
420 }
421
422 $cat_id = (isset($extensions[$file->get('extension')]['display_cat'])) ? $extensions[$file->get('extension')]['display_cat'] : ATTACHMENT_CATEGORY_NONE;
423
424 // Make sure the image category only holds valid images...
425 if ($cat_id == ATTACHMENT_CATEGORY_IMAGE && !$file->is_image())
426 {
427 $file->remove();
428
429 // If this error occurs a user tried to exploit an IE Bug by renaming extensions
430 // Since the image category is displaying content inline we need to catch this.
431 trigger_error($user->lang['ATTACHED_IMAGE_NOT_IMAGE']);
432 }
433
434 // Do we have to create a thumbnail?
435 $filedata['thumbnail'] = ($cat_id == ATTACHMENT_CATEGORY_IMAGE && $config['img_create_thumbnail']) ? 1 : 0;
436
437 // Check Image Size, if it is an image
438 if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id) && $cat_id == ATTACHMENT_CATEGORY_IMAGE)
439 {
440 $file->upload->set_allowed_dimensions(0, 0, $config['img_max_width'], $config['img_max_height']);
441 }
442
443 // Admins and mods are allowed to exceed the allowed filesize
444 if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id))
445 {
446 if (!empty($extensions[$file->get('extension')]['max_filesize']))
447 {
448 $allowed_filesize = $extensions[$file->get('extension')]['max_filesize'];
449 }
450 else
451 {
452 $allowed_filesize = ($is_message) ? $config['max_filesize_pm'] : $config['max_filesize'];
453 }
454
455 $file->upload->set_max_filesize($allowed_filesize);
456 }
457
458 $file->clean_filename('unique', $user->data['user_id'] . '_');
459
460 // Are we uploading an image *and* this image being within the image category? Only then perform additional image checks.
461 $no_image = ($cat_id == ATTACHMENT_CATEGORY_IMAGE) ? false : true;
462
463 $file->move_file($config['upload_path'], false, $no_image);
464
465 if (sizeof($file->error))
466 {
467 $file->remove();
468 $filedata['error'] = array_merge($filedata['error'], $file->error);
469 $filedata['post_attach'] = false;
470
471 return $filedata;
472 }
473
474 $filedata['filesize'] = $file->get('filesize');
475 $filedata['mimetype'] = $file->get('mimetype');
476 $filedata['extension'] = $file->get('extension');
477 $filedata['physical_filename'] = $file->get('realname');
478 $filedata['real_filename'] = $file->get('uploadname');
479 $filedata['filetime'] = time();
480
481 // Check our complete quota
482 if ($config['attachment_quota'])
483 {
484 if ($config['upload_dir_size'] + $file->get('filesize') > $config['attachment_quota'])
485 {
486 $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
487 $filedata['post_attach'] = false;
488
489 $file->remove();
490
491 return $filedata;
492 }
493 }
494
495 // Check free disk space
496 if ($free_space = @disk_free_space($phpbb_root_path . $config['upload_path']))
497 {
498 if ($free_space <= $file->get('filesize'))
499 {
500 $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
501 $filedata['post_attach'] = false;
502
503 $file->remove();
504
505 return $filedata;
506 }
507 }
508
509 // Create Thumbnail
510 if ($filedata['thumbnail'])
511 {
512 $source = $file->get('destination_file');
513 $destination = $file->get('destination_path') . '/thumb_' . $file->get('realname');
514
515 if (!create_thumbnail($source, $destination, $file->get('mimetype')))
516 {
517 $filedata['thumbnail'] = 0;
518 }
519 }
520
521 return $filedata;
522}
523
524/**
525* Calculate the needed size for Thumbnail
526*/
527function get_img_size_format($width, $height)
528{
529 global $config;
530
531 // Maximum Width the Image can take
532 $max_width = ($config['img_max_thumb_width']) ? $config['img_max_thumb_width'] : 400;
533
534 if ($width > $height)
535 {
536 return array(
537 round($width * ($max_width / $width)),
538 round($height * ($max_width / $width))
539 );
540 }
541 else
542 {
543 return array(
544 round($width * ($max_width / $height)),
545 round($height * ($max_width / $height))
546 );
547 }
548}
549
550/**
551* Return supported image types
552*/
553function get_supported_image_types($type = false)
554{
555 if (@extension_loaded('gd'))
556 {
557 $format = imagetypes();
558 $new_type = 0;
559
560 if ($type !== false)
561 {
562 // Type is one of the IMAGETYPE constants - it is fetched from getimagesize()
563 // We do not use the constants here, because some were not available in PHP 4.3.x
564 switch ($type)
565 {
566 // GIF
567 case 1:
568 $new_type = ($format & IMG_GIF) ? IMG_GIF : false;
569 break;
570
571 // JPG, JPC, JP2
572 case 2:
573 case 9:
574 case 10:
575 case 11:
576 case 12:
577 $new_type = ($format & IMG_JPG) ? IMG_JPG : false;
578 break;
579
580 // PNG
581 case 3:
582 $new_type = ($format & IMG_PNG) ? IMG_PNG : false;
583 break;
584
585 // WBMP
586 case 15:
587 $new_type = ($format & IMG_WBMP) ? IMG_WBMP : false;
588 break;
589 }
590 }
591 else
592 {
593 $new_type = array();
594 $go_through_types = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP);
595
596 foreach ($go_through_types as $check_type)
597 {
598 if ($format & $check_type)
599 {
600 $new_type[] = $check_type;
601 }
602 }
603 }
604
605 return array(
606 'gd' => ($new_type) ? true : false,
607 'format' => $new_type,
608 'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1
609 );
610 }
611
612 return array('gd' => false);
613}
614
615/**
616* Create Thumbnail
617*/
618function create_thumbnail($source, $destination, $mimetype)
619{
620 global $config;
621
622 $min_filesize = (int) $config['img_min_thumb_filesize'];
623 $img_filesize = (file_exists($source)) ? @filesize($source) : false;
624
625 if (!$img_filesize || $img_filesize <= $min_filesize)
626 {
627 return false;
628 }
629
630 $dimension = @getimagesize($source);
631
632 if ($dimension === false)
633 {
634 return false;
635 }
636
637 list($width, $height, $type, ) = $dimension;
638
639 if (empty($width) || empty($height))
640 {
641 return false;
642 }
643
644 list($new_width, $new_height) = get_img_size_format($width, $height);
645
646 // Do not create a thumbnail if the resulting width/height is bigger than the original one
647 if ($new_width >= $width && $new_height >= $height)
648 {
649 return false;
650 }
651
652 $used_imagick = false;
653
654 // Only use imagemagick if defined and the passthru function not disabled
655 if ($config['img_imagick'] && function_exists('passthru'))
656 {
657 if (substr($config['img_imagick'], -1) !== '/')
658 {
659 $config['img_imagick'] .= '/';
660 }
661
662 @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) . '"');
663
664 if (file_exists($destination))
665 {
666 $used_imagick = true;
667 }
668 }
669
670 if (!$used_imagick)
671 {
672 $type = get_supported_image_types($type);
673
674 if ($type['gd'])
675 {
676 // If the type is not supported, we are not able to create a thumbnail
677 if ($type['format'] === false)
678 {
679 return false;
680 }
681
682 switch ($type['format'])
683 {
684 case IMG_GIF:
685 $image = @imagecreatefromgif($source);
686 break;
687
688 case IMG_JPG:
689 @ini_set('gd.jpeg_ignore_warning', 1);
690 $image = @imagecreatefromjpeg($source);
691 break;
692
693 case IMG_PNG:
694 $image = @imagecreatefrompng($source);
695 break;
696
697 case IMG_WBMP:
698 $image = @imagecreatefromwbmp($source);
699 break;
700 }
701
702 if (empty($image))
703 {
704 return false;
705 }
706
707 if ($type['version'] == 1)
708 {
709 $new_image = imagecreate($new_width, $new_height);
710
711 if ($new_image === false)
712 {
713 return false;
714 }
715
716 imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
717 }
718 else
719 {
720 $new_image = imagecreatetruecolor($new_width, $new_height);
721
722 if ($new_image === false)
723 {
724 return false;
725 }
726
727 // Preserve alpha transparency (png for example)
728 @imagealphablending($new_image, false);
729 @imagesavealpha($new_image, true);
730
731 imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
732 }
733
734 // If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
735 if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on')
736 {
737 @touch($destination);
738 }
739
740 switch ($type['format'])
741 {
742 case IMG_GIF:
743 imagegif($new_image, $destination);
744 break;
745
746 case IMG_JPG:
747 imagejpeg($new_image, $destination, 90);
748 break;
749
750 case IMG_PNG:
751 imagepng($new_image, $destination);
752 break;
753
754 case IMG_WBMP:
755 imagewbmp($new_image, $destination);
756 break;
757 }
758
759 imagedestroy($new_image);
760 }
761 else
762 {
763 return false;
764 }
765 }
766
767 if (!file_exists($destination))
768 {
769 return false;
770 }
771
772 phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
773
774 return true;
775}
776
777/**
778* Assign Inline attachments (build option fields)
779*/
780function posting_gen_inline_attachments(&$attachment_data)
781{
782 global $template;
783
784 if (sizeof($attachment_data))
785 {
786 $s_inline_attachment_options = '';
787
788 foreach ($attachment_data as $i => $attachment)
789 {
790 $s_inline_attachment_options .= '<option value="' . $i . '">' . utf8_basename($attachment['real_filename']) . '</option>';
791 }
792
793 $template->assign_var('S_INLINE_ATTACHMENT_OPTIONS', $s_inline_attachment_options);
794
795 return true;
796 }
797
798 return false;
799}
800
801/**
802* Generate inline attachment entry
803*/
804function posting_gen_attachment_entry($attachment_data, &$filename_data, $show_attach_box = true)
805{
806 global $template, $config, $phpbb_root_path, $phpEx, $user, $auth;
807
808 // Some default template variables
809 $template->assign_vars(array(
810 'S_SHOW_ATTACH_BOX' => $show_attach_box,
811 'S_HAS_ATTACHMENTS' => sizeof($attachment_data),
812 'FILESIZE' => $config['max_filesize'],
813 'FILE_COMMENT' => (isset($filename_data['filecomment'])) ? $filename_data['filecomment'] : '',
814 ));
815
816 if (sizeof($attachment_data))
817 {
818 // We display the posted attachments within the desired order.
819 ($config['display_order']) ? krsort($attachment_data) : ksort($attachment_data);
820
821 foreach ($attachment_data as $count => $attach_row)
822 {
823 $hidden = '';
824 $attach_row['real_filename'] = utf8_basename($attach_row['real_filename']);
825
826 foreach ($attach_row as $key => $value)
827 {
828 $hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
829 }
830
831 $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);
832
833 $template->assign_block_vars('attach_row', array(
834 'FILENAME' => utf8_basename($attach_row['real_filename']),
835 'A_FILENAME' => addslashes(utf8_basename($attach_row['real_filename'])),
836 'FILE_COMMENT' => $attach_row['attach_comment'],
837 'ATTACH_ID' => $attach_row['attach_id'],
838 'S_IS_ORPHAN' => $attach_row['is_orphan'],
839 'ASSOC_INDEX' => $count,
840
841 'U_VIEW_ATTACHMENT' => $download_link,
842 'S_HIDDEN' => $hidden)
843 );
844 }
845 }
846
847 return sizeof($attachment_data);
848}
849
850//
851// General Post functions
852//
853
854/**
855* Load Drafts
856*/
857function load_drafts($topic_id = 0, $forum_id = 0, $id = 0, $pm_action = '', $msg_id = 0)
858{
859 global $user, $db, $template, $auth;
860 global $phpbb_root_path, $phpEx;
861
862 $topic_ids = $forum_ids = $draft_rows = array();
863
864 // Load those drafts not connected to forums/topics
865 // If forum_id == 0 AND topic_id == 0 then this is a PM draft
866 if (!$topic_id && !$forum_id)
867 {
868 $sql_and = ' AND d.forum_id = 0 AND d.topic_id = 0';
869 }
870 else
871 {
872 $sql_and = '';
873 $sql_and .= ($forum_id) ? ' AND d.forum_id = ' . (int) $forum_id : '';
874 $sql_and .= ($topic_id) ? ' AND d.topic_id = ' . (int) $topic_id : '';
875 }
876
877 $sql = 'SELECT d.*, f.forum_id, f.forum_name
878 FROM ' . DRAFTS_TABLE . ' d
879 LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = d.forum_id)
880 WHERE d.user_id = ' . $user->data['user_id'] . "
881 $sql_and
882 ORDER BY d.save_time DESC";
883 $result = $db->sql_query($sql);
884
885 while ($row = $db->sql_fetchrow($result))
886 {
887 if ($row['topic_id'])
888 {
889 $topic_ids[] = (int) $row['topic_id'];
890 }
891 $draft_rows[] = $row;
892 }
893 $db->sql_freeresult($result);
894
895 if (!sizeof($draft_rows))
896 {
897 return;
898 }
899
900 $topic_rows = array();
901 if (sizeof($topic_ids))
902 {
903 $sql = 'SELECT topic_id, forum_id, topic_title
904 FROM ' . TOPICS_TABLE . '
905 WHERE ' . $db->sql_in_set('topic_id', array_unique($topic_ids));
906 $result = $db->sql_query($sql);
907
908 while ($row = $db->sql_fetchrow($result))
909 {
910 $topic_rows[$row['topic_id']] = $row;
911 }
912 $db->sql_freeresult($result);
913 }
914 unset($topic_ids);
915
916 $template->assign_var('S_SHOW_DRAFTS', true);
917
918 foreach ($draft_rows as $draft)
919 {
920 $link_topic = $link_forum = $link_pm = false;
921 $insert_url = $view_url = $title = '';
922
923 if (isset($topic_rows[$draft['topic_id']])
924 && (
925 ($topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id']))
926 ||
927 (!$topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_getf_global('f_read'))
928 ))
929 {
930 $topic_forum_id = ($topic_rows[$draft['topic_id']]['forum_id']) ? $topic_rows[$draft['topic_id']]['forum_id'] : $forum_id;
931
932 $link_topic = true;
933 $view_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id']);
934 $title = $topic_rows[$draft['topic_id']]['topic_title'];
935
936 $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']);
937 }
938 else if ($draft['forum_id'] && $auth->acl_get('f_read', $draft['forum_id']))
939 {
940 $link_forum = true;
941 $view_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $draft['forum_id']);
942 $title = $draft['forum_name'];
943
944 $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $draft['forum_id'] . '&amp;mode=post&amp;d=' . $draft['draft_id']);
945 }
946 else
947 {
948 // Either display as PM draft if forum_id and topic_id are empty or if access to the forums has been denied afterwards...
949 $link_pm = true;
950 $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" : ''));
951 }
952
953 $template->assign_block_vars('draftrow', array(
954 'DRAFT_ID' => $draft['draft_id'],
955 'DATE' => $user->format_date($draft['save_time']),
956 'DRAFT_SUBJECT' => $draft['draft_subject'],
957
958 'TITLE' => $title,
959 'U_VIEW' => $view_url,
960 'U_INSERT' => $insert_url,
961
962 'S_LINK_PM' => $link_pm,
963 'S_LINK_TOPIC' => $link_topic,
964 'S_LINK_FORUM' => $link_forum)
965 );
966 }
967}
968
969/**
970* Topic Review
971*/
972function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id = 0, $show_quote_button = true)
973{
974 global $user, $auth, $db, $template, $bbcode, $cache;
975 global $config, $phpbb_root_path, $phpEx;
976
977 // Go ahead and pull all data for this topic
978 $sql = 'SELECT p.post_id
979 FROM ' . POSTS_TABLE . ' p' . "
980 WHERE p.topic_id = $topic_id
981 " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . '
982 ' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
983 ' . (($mode == 'post_review_edit') ? " AND p.post_id = $cur_post_id" : '') . '
984 ORDER BY p.post_time ';
985 $sql .= ($mode == 'post_review') ? 'ASC' : 'DESC';
986 $result = $db->sql_query_limit($sql, $config['posts_per_page']);
987
988 $post_list = array();
989
990 while ($row = $db->sql_fetchrow($result))
991 {
992 $post_list[] = $row['post_id'];
993 }
994
995 $db->sql_freeresult($result);
996
997 if (!sizeof($post_list))
998 {
999 return false;
1000 }
1001
1002 // Handle 'post_review_edit' like 'post_review' from now on
1003 if ($mode == 'post_review_edit')
1004 {
1005 $mode = 'post_review';
1006 }
1007
1008 $sql = $db->sql_build_query('SELECT', array(
1009 'SELECT' => 'u.username, u.user_id, u.user_colour, p.*, z.friend, z.foe',
1010
1011 'FROM' => array(
1012 USERS_TABLE => 'u',
1013 POSTS_TABLE => 'p',
1014 ),
1015
1016 'LEFT_JOIN' => array(
1017 array(
1018 'FROM' => array(ZEBRA_TABLE => 'z'),
1019 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
1020 )
1021 ),
1022
1023 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
1024 AND u.user_id = p.poster_id'
1025 ));
1026
1027 $result = $db->sql_query($sql);
1028
1029 $bbcode_bitfield = '';
1030 $rowset = array();
1031 $has_attachments = false;
1032 while ($row = $db->sql_fetchrow($result))
1033 {
1034 $rowset[$row['post_id']] = $row;
1035 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
1036
1037 if ($row['post_attachment'])
1038 {
1039 $has_attachments = true;
1040 }
1041 }
1042 $db->sql_freeresult($result);
1043
1044 // Instantiate BBCode class
1045 if (!isset($bbcode) && $bbcode_bitfield !== '')
1046 {
1047 include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
1048 $bbcode = new bbcode(base64_encode($bbcode_bitfield));
1049 }
1050
1051 // Grab extensions
1052 $extensions = $attachments = array();
1053 if ($has_attachments && $auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
1054 {
1055 $extensions = $cache->obtain_attach_extensions($forum_id);
1056
1057 // Get attachments...
1058 $sql = 'SELECT *
1059 FROM ' . ATTACHMENTS_TABLE . '
1060 WHERE ' . $db->sql_in_set('post_msg_id', $post_list) . '
1061 AND in_message = 0
1062 ORDER BY filetime DESC, post_msg_id ASC';
1063 $result = $db->sql_query($sql);
1064
1065 while ($row = $db->sql_fetchrow($result))
1066 {
1067 $attachments[$row['post_msg_id']][] = $row;
1068 }
1069 $db->sql_freeresult($result);
1070 }
1071
1072 for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
1073 {
1074 // A non-existing rowset only happens if there was no user present for the entered poster_id
1075 // This could be a broken posts table.
1076 if (!isset($rowset[$post_list[$i]]))
1077 {
1078 continue;
1079 }
1080
1081 $row =& $rowset[$post_list[$i]];
1082
1083 $poster_id = $row['user_id'];
1084 $post_subject = $row['post_subject'];
1085 $message = censor_text($row['post_text']);
1086
1087 $decoded_message = false;
1088
1089 if ($show_quote_button && $auth->acl_get('f_reply', $forum_id))
1090 {
1091 $decoded_message = $message;
1092 decode_message($decoded_message, $row['bbcode_uid']);
1093
1094 $decoded_message = bbcode_nl2br($decoded_message);
1095 }
1096
1097 if ($row['bbcode_bitfield'])
1098 {
1099 $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
1100 }
1101
1102 $message = bbcode_nl2br($message);
1103 $message = smiley_text($message, !$row['enable_smilies']);
1104
1105 if (!empty($attachments[$row['post_id']]))
1106 {
1107 $update_count = array();
1108 parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
1109 }
1110
1111 $post_subject = censor_text($post_subject);
1112
1113 $post_anchor = ($mode == 'post_review') ? 'ppr' . $row['post_id'] : 'pr' . $row['post_id'];
1114 $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']}");
1115
1116 $template->assign_block_vars($mode . '_row', array(
1117 'POST_AUTHOR_FULL' => get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1118 'POST_AUTHOR_COLOUR' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1119 'POST_AUTHOR' => get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1120 'U_POST_AUTHOR' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1121
1122 'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
1123 'S_FRIEND' => ($row['friend']) ? true : false,
1124 'S_IGNORE_POST' => ($row['foe']) ? true : false,
1125 '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>') : '',
1126
1127 'POST_SUBJECT' => $post_subject,
1128 'MINI_POST_IMG' => $user->img('icon_post_target', $user->lang['POST']),
1129 'POST_DATE' => $user->format_date($row['post_time']),
1130 'MESSAGE' => $message,
1131 'DECODED_MESSAGE' => $decoded_message,
1132 'POST_ID' => $row['post_id'],
1133 'U_MINI_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
1134 '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) : '',
1135 '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'])) : '')
1136 );
1137
1138 // Display not already displayed Attachments for this post, we already parsed them. ;)
1139 if (!empty($attachments[$row['post_id']]))
1140 {
1141 foreach ($attachments[$row['post_id']] as $attachment)
1142 {
1143 $template->assign_block_vars($mode . '_row.attachment', array(
1144 'DISPLAY_ATTACHMENT' => $attachment)
1145 );
1146 }
1147 }
1148
1149 unset($rowset[$i]);
1150 }
1151
1152 if ($mode == 'topic_review')
1153 {
1154 $template->assign_var('QUOTE_IMG', $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']));
1155 }
1156
1157 return true;
1158}
1159
1160/**
1161* User Notification
1162*/
1163function user_notification($mode, $subject, $topic_title, $forum_name, $forum_id, $topic_id, $post_id)
1164{
1165 global $db, $user, $config, $phpbb_root_path, $phpEx, $auth;
1166
1167 $topic_notification = ($mode == 'reply' || $mode == 'quote') ? true : false;
1168 $forum_notification = ($mode == 'post') ? true : false;
1169
1170 if (!$topic_notification && !$forum_notification)
1171 {
1172 trigger_error('NO_MODE');
1173 }
1174
1175 if (($topic_notification && !$config['allow_topic_notify']) || ($forum_notification && !$config['allow_forum_notify']))
1176 {
1177 return;
1178 }
1179
1180 $topic_title = ($topic_notification) ? $topic_title : $subject;
1181 $topic_title = censor_text($topic_title);
1182
1183 // Get banned User ID's
1184 $sql = 'SELECT ban_userid
1185 FROM ' . BANLIST_TABLE . '
1186 WHERE ban_userid <> 0
1187 AND ban_exclude <> 1';
1188 $result = $db->sql_query($sql);
1189
1190 $sql_ignore_users = ANONYMOUS . ', ' . $user->data['user_id'];
1191 while ($row = $db->sql_fetchrow($result))
1192 {
1193 $sql_ignore_users .= ', ' . (int) $row['ban_userid'];
1194 }
1195 $db->sql_freeresult($result);
1196
1197 $notify_rows = array();
1198
1199 // -- get forum_userids || topic_userids
1200 $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
1201 FROM ' . (($topic_notification) ? TOPICS_WATCH_TABLE : FORUMS_WATCH_TABLE) . ' w, ' . USERS_TABLE . ' u
1202 WHERE w.' . (($topic_notification) ? 'topic_id' : 'forum_id') . ' = ' . (($topic_notification) ? $topic_id : $forum_id) . "
1203 AND w.user_id NOT IN ($sql_ignore_users)
1204 AND w.notify_status = 0
1205 AND u.user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')
1206 AND u.user_id = w.user_id';
1207 $result = $db->sql_query($sql);
1208
1209 while ($row = $db->sql_fetchrow($result))
1210 {
1211 $notify_rows[$row['user_id']] = array(
1212 'user_id' => $row['user_id'],
1213 'username' => $row['username'],
1214 'user_email' => $row['user_email'],
1215 'user_jabber' => $row['user_jabber'],
1216 'user_lang' => $row['user_lang'],
1217 'notify_type' => ($topic_notification) ? 'topic' : 'forum',
1218 'template' => ($topic_notification) ? 'topic_notify' : 'newtopic_notify',
1219 'method' => $row['user_notify_type'],
1220 'allowed' => false
1221 );
1222 }
1223 $db->sql_freeresult($result);
1224
1225 // forum notification is sent to those not already receiving topic notifications
1226 if ($topic_notification)
1227 {
1228 if (sizeof($notify_rows))
1229 {
1230 $sql_ignore_users .= ', ' . implode(', ', array_keys($notify_rows));
1231 }
1232
1233 $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
1234 FROM ' . FORUMS_WATCH_TABLE . ' fw, ' . USERS_TABLE . " u
1235 WHERE fw.forum_id = $forum_id
1236 AND fw.user_id NOT IN ($sql_ignore_users)
1237 AND fw.notify_status = 0
1238 AND u.user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')
1239 AND u.user_id = fw.user_id';
1240 $result = $db->sql_query($sql);
1241
1242 while ($row = $db->sql_fetchrow($result))
1243 {
1244 $notify_rows[$row['user_id']] = array(
1245 'user_id' => $row['user_id'],
1246 'username' => $row['username'],
1247 'user_email' => $row['user_email'],
1248 'user_jabber' => $row['user_jabber'],
1249 'user_lang' => $row['user_lang'],
1250 'notify_type' => 'forum',
1251 'template' => 'forum_notify',
1252 'method' => $row['user_notify_type'],
1253 'allowed' => false
1254 );
1255 }
1256 $db->sql_freeresult($result);
1257 }
1258
1259 if (!sizeof($notify_rows))
1260 {
1261 return;
1262 }
1263
1264 // Make sure users are allowed to read the forum
1265 foreach ($auth->acl_get_list(array_keys($notify_rows), 'f_read', $forum_id) as $forum_id => $forum_ary)
1266 {
1267 foreach ($forum_ary as $auth_option => $user_ary)
1268 {
1269 foreach ($user_ary as $user_id)
1270 {
1271 $notify_rows[$user_id]['allowed'] = true;
1272 }
1273 }
1274 }
1275
1276
1277 // Now, we have to do a little step before really sending, we need to distinguish our users a little bit. ;)
1278 $msg_users = $delete_ids = $update_notification = array();
1279 foreach ($notify_rows as $user_id => $row)
1280 {
1281 if (!$row['allowed'] || !trim($row['user_email']))
1282 {
1283 $delete_ids[$row['notify_type']][] = $row['user_id'];
1284 }
1285 else
1286 {
1287 $msg_users[] = $row;
1288 $update_notification[$row['notify_type']][] = $row['user_id'];
1289 }
1290 }
1291 unset($notify_rows);
1292
1293 // Now, we are able to really send out notifications
1294 if (sizeof($msg_users))
1295 {
1296 include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
1297 $messenger = new messenger();
1298
1299 $msg_list_ary = array();
1300 foreach ($msg_users as $row)
1301 {
1302 $pos = (!isset($msg_list_ary[$row['template']])) ? 0 : sizeof($msg_list_ary[$row['template']]);
1303
1304 $msg_list_ary[$row['template']][$pos]['method'] = $row['method'];
1305 $msg_list_ary[$row['template']][$pos]['email'] = $row['user_email'];
1306 $msg_list_ary[$row['template']][$pos]['jabber'] = $row['user_jabber'];
1307 $msg_list_ary[$row['template']][$pos]['name'] = $row['username'];
1308 $msg_list_ary[$row['template']][$pos]['lang'] = $row['user_lang'];
1309 $msg_list_ary[$row['template']][$pos]['user_id']= $row['user_id'];
1310 }
1311 unset($msg_users);
1312
1313 foreach ($msg_list_ary as $email_template => $email_list)
1314 {
1315 foreach ($email_list as $addr)
1316 {
1317 $messenger->template($email_template, $addr['lang']);
1318
1319 $messenger->to($addr['email'], $addr['name']);
1320 $messenger->im($addr['jabber'], $addr['name']);
1321
1322 $messenger->assign_vars(array(
1323 'USERNAME' => htmlspecialchars_decode($addr['name']),
1324 'TOPIC_TITLE' => htmlspecialchars_decode($topic_title),
1325 'FORUM_NAME' => htmlspecialchars_decode($forum_name),
1326
1327 'U_FORUM' => generate_board_url() . "/viewforum.$phpEx?f=$forum_id",
1328 'U_TOPIC' => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id",
1329 'U_NEWEST_POST' => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id&p=$post_id&e=$post_id",
1330 'U_STOP_WATCHING_TOPIC' => generate_board_url() . "/viewtopic.$phpEx?uid={$addr['user_id']}&f=$forum_id&t=$topic_id&unwatch=topic",
1331 'U_STOP_WATCHING_FORUM' => generate_board_url() . "/viewforum.$phpEx?uid={$addr['user_id']}&f=$forum_id&unwatch=forum",
1332 ));
1333
1334 $messenger->send($addr['method']);
1335 }
1336 }
1337 unset($msg_list_ary);
1338
1339 $messenger->save_queue();
1340 }
1341
1342 // Handle the DB updates
1343 $db->sql_transaction('begin');
1344
1345 if (!empty($update_notification['topic']))
1346 {
1347 $sql = 'UPDATE ' . TOPICS_WATCH_TABLE . "
1348 SET notify_status = 1
1349 WHERE topic_id = $topic_id
1350 AND " . $db->sql_in_set('user_id', $update_notification['topic']);
1351 $db->sql_query($sql);
1352 }
1353
1354 if (!empty($update_notification['forum']))
1355 {
1356 $sql = 'UPDATE ' . FORUMS_WATCH_TABLE . "
1357 SET notify_status = 1
1358 WHERE forum_id = $forum_id
1359 AND " . $db->sql_in_set('user_id', $update_notification['forum']);
1360 $db->sql_query($sql);
1361 }
1362
1363 // Now delete the user_ids not authorised to receive notifications on this topic/forum
1364 if (!empty($delete_ids['topic']))
1365 {
1366 $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . "
1367 WHERE topic_id = $topic_id
1368 AND " . $db->sql_in_set('user_id', $delete_ids['topic']);
1369 $db->sql_query($sql);
1370 }
1371
1372 if (!empty($delete_ids['forum']))
1373 {
1374 $sql = 'DELETE FROM ' . FORUMS_WATCH_TABLE . "
1375 WHERE forum_id = $forum_id
1376 AND " . $db->sql_in_set('user_id', $delete_ids['forum']);
1377 $db->sql_query($sql);
1378 }
1379
1380 $db->sql_transaction('commit');
1381}
1382
1383//
1384// Post handling functions
1385//
1386
1387/**
1388* Delete Post
1389*/
1390function delete_post($forum_id, $topic_id, $post_id, &$data)
1391{
1392 global $db, $user, $auth;
1393 global $config, $phpEx, $phpbb_root_path;
1394
1395 // Specify our post mode
1396 $post_mode = 'delete';
1397 if (($data['topic_first_post_id'] === $data['topic_last_post_id']) && $data['topic_replies_real'] == 0)
1398 {
1399 $post_mode = 'delete_topic';
1400 }
1401 else if ($data['topic_first_post_id'] == $post_id)
1402 {
1403 $post_mode = 'delete_first_post';
1404 }
1405 else if ($data['topic_last_post_id'] == $post_id)
1406 {
1407 $post_mode = 'delete_last_post';
1408 }
1409 $sql_data = array();
1410 $next_post_id = false;
1411
1412 include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
1413
1414 $db->sql_transaction('begin');
1415
1416 // we must make sure to update forums that contain the shadow'd topic
1417 if ($post_mode == 'delete_topic')
1418 {
1419 $shadow_forum_ids = array();
1420
1421 $sql = 'SELECT forum_id
1422 FROM ' . TOPICS_TABLE . '
1423 WHERE ' . $db->sql_in_set('topic_moved_id', $topic_id);
1424 $result = $db->sql_query($sql);
1425 while ($row = $db->sql_fetchrow($result))
1426 {
1427 if (!isset($shadow_forum_ids[(int) $row['forum_id']]))
1428 {
1429 $shadow_forum_ids[(int) $row['forum_id']] = 1;
1430 }
1431 else
1432 {
1433 $shadow_forum_ids[(int) $row['forum_id']]++;
1434 }
1435 }
1436 $db->sql_freeresult($result);
1437 }
1438
1439 if (!delete_posts('post_id', array($post_id), false, false))
1440 {
1441 // Try to delete topic, we may had an previous error causing inconsistency
1442 if ($post_mode == 'delete_topic')
1443 {
1444 delete_topics('topic_id', array($topic_id), false);
1445 }
1446 trigger_error('ALREADY_DELETED');
1447 }
1448
1449 $db->sql_transaction('commit');
1450
1451 // Collect the necessary information for updating the tables
1452 $sql_data[FORUMS_TABLE] = '';
1453 switch ($post_mode)
1454 {
1455 case 'delete_topic':
1456
1457 foreach ($shadow_forum_ids as $updated_forum => $topic_count)
1458 {
1459 // counting is fun! we only have to do sizeof($forum_ids) number of queries,
1460 // even if the topic is moved back to where its shadow lives (we count how many times it is in a forum)
1461 $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);
1462 update_post_information('forum', $updated_forum);
1463 }
1464
1465 delete_topics('topic_id', array($topic_id), false);
1466
1467 if ($data['topic_type'] != POST_GLOBAL)
1468 {
1469 $sql_data[FORUMS_TABLE] .= 'forum_topics_real = forum_topics_real - 1';
1470 $sql_data[FORUMS_TABLE] .= ($data['topic_approved']) ? ', forum_posts = forum_posts - 1, forum_topics = forum_topics - 1' : '';
1471 }
1472
1473 $update_sql = update_post_information('forum', $forum_id, true);
1474 if (sizeof($update_sql))
1475 {
1476 $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
1477 $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
1478 }
1479 break;
1480
1481 case 'delete_first_post':
1482 $sql = 'SELECT p.post_id, p.poster_id, p.post_username, u.username, u.user_colour
1483 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
1484 WHERE p.topic_id = $topic_id
1485 AND p.poster_id = u.user_id
1486 ORDER BY p.post_time ASC";
1487 $result = $db->sql_query_limit($sql, 1);
1488 $row = $db->sql_fetchrow($result);
1489 $db->sql_freeresult($result);
1490
1491 if ($data['topic_type'] != POST_GLOBAL)
1492 {
1493 $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1494 }
1495
1496 $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'])) . "'";
1497
1498 // 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"
1499 $sql_data[TOPICS_TABLE] .= ', topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
1500
1501 $next_post_id = (int) $row['post_id'];
1502 break;
1503
1504 case 'delete_last_post':
1505 if ($data['topic_type'] != POST_GLOBAL)
1506 {
1507 $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1508 }
1509
1510 $update_sql = update_post_information('forum', $forum_id, true);
1511 if (sizeof($update_sql))
1512 {
1513 $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
1514 $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
1515 }
1516
1517 $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' : '');
1518
1519 $update_sql = update_post_information('topic', $topic_id, true);
1520 if (sizeof($update_sql))
1521 {
1522 $sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
1523 $next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
1524 }
1525 else
1526 {
1527 $sql = 'SELECT MAX(post_id) as last_post_id
1528 FROM ' . POSTS_TABLE . "
1529 WHERE topic_id = $topic_id " .
1530 ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '');
1531 $result = $db->sql_query($sql);
1532 $row = $db->sql_fetchrow($result);
1533 $db->sql_freeresult($result);
1534
1535 $next_post_id = (int) $row['last_post_id'];
1536 }
1537 break;
1538
1539 case 'delete':
1540 $sql = 'SELECT post_id
1541 FROM ' . POSTS_TABLE . "
1542 WHERE topic_id = $topic_id " .
1543 ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '') . '
1544 AND post_time > ' . $data['post_time'] . '
1545 ORDER BY post_time ASC';
1546 $result = $db->sql_query_limit($sql, 1);
1547 $row = $db->sql_fetchrow($result);
1548 $db->sql_freeresult($result);
1549
1550 if ($data['topic_type'] != POST_GLOBAL)
1551 {
1552 $sql_data[FORUMS_TABLE] = ($data['post_approved']) ? 'forum_posts = forum_posts - 1' : '';
1553 }
1554
1555 $sql_data[TOPICS_TABLE] = 'topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
1556 $next_post_id = (int) $row['post_id'];
1557 break;
1558 }
1559
1560 if (($post_mode == 'delete') || ($post_mode == 'delete_last_post') || ($post_mode == 'delete_first_post'))
1561 {
1562 $sql = 'SELECT 1 AS has_attachments
1563 FROM ' . ATTACHMENTS_TABLE . '
1564 WHERE topic_id = ' . $topic_id;
1565 $result = $db->sql_query_limit($sql, 1);
1566 $has_attachments = (int) $db->sql_fetchfield('has_attachments');
1567 $db->sql_freeresult($result);
1568
1569 if (!$has_attachments)
1570 {
1571 $sql_data[TOPICS_TABLE] .= ', topic_attachment = 0';
1572 }
1573 }
1574
1575// $sql_data[USERS_TABLE] = ($data['post_postcount']) ? 'user_posts = user_posts - 1' : '';
1576
1577 $db->sql_transaction('begin');
1578
1579 $where_sql = array(
1580 FORUMS_TABLE => "forum_id = $forum_id",
1581 TOPICS_TABLE => "topic_id = $topic_id",
1582 USERS_TABLE => 'user_id = ' . $data['poster_id']
1583 );
1584
1585 foreach ($sql_data as $table => $update_sql)
1586 {
1587 if ($update_sql)
1588 {
1589 $db->sql_query("UPDATE $table SET $update_sql WHERE " . $where_sql[$table]);
1590 }
1591 }
1592
1593 // Adjust posted info for this user by looking for a post by him/her within this topic...
1594 if ($post_mode != 'delete_topic' && $config['load_db_track'] && $data['poster_id'] != ANONYMOUS)
1595 {
1596 $sql = 'SELECT poster_id
1597 FROM ' . POSTS_TABLE . '
1598 WHERE topic_id = ' . $topic_id . '
1599 AND poster_id = ' . $data['poster_id'];
1600 $result = $db->sql_query_limit($sql, 1);
1601 $poster_id = (int) $db->sql_fetchfield('poster_id');
1602 $db->sql_freeresult($result);
1603
1604 // The user is not having any more posts within this topic
1605 if (!$poster_id)
1606 {
1607 $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
1608 WHERE topic_id = ' . $topic_id . '
1609 AND user_id = ' . $data['poster_id'];
1610 $db->sql_query($sql);
1611 }
1612 }
1613
1614 $db->sql_transaction('commit');
1615
1616 if ($data['post_reported'] && ($post_mode != 'delete_topic'))
1617 {
1618 sync('topic_reported', 'topic_id', array($topic_id));
1619 }
1620
1621 return $next_post_id;
1622}
1623
1624/**
1625* Submit Post
1626* @todo Split up and create lightweight, simple API for this.
1627*/
1628function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $update_message = true, $update_search_index = true)
1629{
1630 global $db, $auth, $user, $config, $phpEx, $template, $phpbb_root_path;
1631
1632 // We do not handle erasing posts here
1633 if ($mode == 'delete')
1634 {
1635 return false;
1636 }
1637
1638 $current_time = time();
1639
1640 if ($mode == 'post')
1641 {
1642 $post_mode = 'post';
1643 $update_message = true;
1644 }
1645 else if ($mode != 'edit')
1646 {
1647 $post_mode = 'reply';
1648 $update_message = true;
1649 }
1650 else if ($mode == 'edit')
1651 {
1652 $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'));
1653 }
1654
1655 // First of all make sure the subject and topic title are having the correct length.
1656 // To achieve this without cutting off between special chars we convert to an array and then count the elements.
1657 $subject = truncate_string($subject);
1658 $data['topic_title'] = truncate_string($data['topic_title']);
1659
1660 // Collect some basic information about which tables and which rows to update/insert
1661 $sql_data = $topic_row = array();
1662 $poster_id = ($mode == 'edit') ? $data['poster_id'] : (int) $user->data['user_id'];
1663
1664 // Retrieve some additional information if not present
1665 if ($mode == 'edit' && (!isset($data['post_approved']) || !isset($data['topic_approved']) || $data['post_approved'] === false || $data['topic_approved'] === false))
1666 {
1667 $sql = 'SELECT p.post_approved, t.topic_type, t.topic_replies, t.topic_replies_real, t.topic_approved
1668 FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
1669 WHERE t.topic_id = p.topic_id
1670 AND p.post_id = ' . $data['post_id'];
1671 $result = $db->sql_query($sql);
1672 $topic_row = $db->sql_fetchrow($result);
1673 $db->sql_freeresult($result);
1674
1675 $data['topic_approved'] = $topic_row['topic_approved'];
1676 $data['post_approved'] = $topic_row['post_approved'];
1677 }
1678
1679 // This variable indicates if the user is able to post or put into the queue - it is used later for all code decisions regarding approval
1680 // The variable name should be $post_approved, because it indicates if the post is approved or not
1681 $post_approval = 1;
1682
1683 // Check the permissions for post approval. Moderators are not affected.
1684 if (!$auth->acl_get('f_noapprove', $data['forum_id']) && !$auth->acl_get('m_approve', $data['forum_id']))
1685 {
1686 // Post not approved, but in queue
1687 $post_approval = 0;
1688 }
1689
1690 // Mods are able to force approved/unapproved posts. True means the post is approved, false the post is unapproved
1691 if (isset($data['force_approved_state']))
1692 {
1693 $post_approval = ($data['force_approved_state']) ? 1 : 0;
1694 }
1695
1696 // Start the transaction here
1697 $db->sql_transaction('begin');
1698
1699 // Collect Information
1700 switch ($post_mode)
1701 {
1702 case 'post':
1703 case 'reply':
1704 $sql_data[POSTS_TABLE]['sql'] = array(
1705 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1706 'poster_id' => (int) $user->data['user_id'],
1707 'icon_id' => $data['icon_id'],
1708 'poster_ip' => $user->ip,
1709 'post_time' => $current_time,
1710 'post_approved' => $post_approval,
1711 'enable_bbcode' => $data['enable_bbcode'],
1712 'enable_smilies' => $data['enable_smilies'],
1713 'enable_magic_url' => $data['enable_urls'],
1714 'enable_sig' => $data['enable_sig'],
1715 'post_username' => (!$user->data['is_registered']) ? $username : '',
1716 'post_subject' => $subject,
1717 'post_text' => $data['message'],
1718 'post_checksum' => $data['message_md5'],
1719 'post_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1720 'bbcode_bitfield' => $data['bbcode_bitfield'],
1721 'bbcode_uid' => $data['bbcode_uid'],
1722 'post_postcount' => ($auth->acl_get('f_postcount', $data['forum_id'])) ? 1 : 0,
1723 'post_edit_locked' => $data['post_edit_locked']
1724 );
1725 break;
1726
1727 case 'edit_first_post':
1728 case 'edit':
1729
1730 case 'edit_last_post':
1731 case 'edit_topic':
1732
1733 // If edit reason is given always display edit info
1734
1735 // If editing last post then display no edit info
1736 // If m_edit permission then display no edit info
1737 // If normal edit display edit info
1738
1739 // Display edit info if edit reason given or user is editing his post, which is not the last within the topic.
1740 if ($data['post_edit_reason'] || (!$auth->acl_get('m_edit', $data['forum_id']) && ($post_mode == 'edit' || $post_mode == 'edit_first_post')))
1741 {
1742 $data['post_edit_reason'] = truncate_string($data['post_edit_reason'], 255, 255, false);
1743
1744 $sql_data[POSTS_TABLE]['sql'] = array(
1745 'post_edit_time' => $current_time,
1746 'post_edit_reason' => $data['post_edit_reason'],
1747 'post_edit_user' => (int) $data['post_edit_user'],
1748 );
1749
1750 $sql_data[POSTS_TABLE]['stat'][] = 'post_edit_count = post_edit_count + 1';
1751 }
1752 else if (!$data['post_edit_reason'] && $mode == 'edit' && $auth->acl_get('m_edit', $data['forum_id']))
1753 {
1754 $sql_data[POSTS_TABLE]['sql'] = array(
1755 'post_edit_reason' => '',
1756 );
1757 }
1758
1759 // If the person editing this post is different to the one having posted then we will add a log entry stating the edit
1760 // Could be simplified by only adding to the log if the edit is not tracked - but this may confuse admins/mods
1761 if ($user->data['user_id'] != $poster_id)
1762 {
1763 $log_subject = ($subject) ? $subject : $data['topic_title'];
1764 add_log('mod', $data['forum_id'], $data['topic_id'], 'LOG_POST_EDITED', $log_subject, (!empty($username)) ? $username : $user->lang['GUEST']);
1765 }
1766
1767 if (!isset($sql_data[POSTS_TABLE]['sql']))
1768 {
1769 $sql_data[POSTS_TABLE]['sql'] = array();
1770 }
1771
1772 $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1773 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1774 'poster_id' => $data['poster_id'],
1775 'icon_id' => $data['icon_id'],
1776 'post_approved' => (!$post_approval) ? 0 : $data['post_approved'],
1777 'enable_bbcode' => $data['enable_bbcode'],
1778 'enable_smilies' => $data['enable_smilies'],
1779 'enable_magic_url' => $data['enable_urls'],
1780 'enable_sig' => $data['enable_sig'],
1781 'post_username' => ($username && $data['poster_id'] == ANONYMOUS) ? $username : '',
1782 'post_subject' => $subject,
1783 'post_checksum' => $data['message_md5'],
1784 'post_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1785 'bbcode_bitfield' => $data['bbcode_bitfield'],
1786 'bbcode_uid' => $data['bbcode_uid'],
1787 'post_edit_locked' => $data['post_edit_locked'])
1788 );
1789
1790 if ($update_message)
1791 {
1792 $sql_data[POSTS_TABLE]['sql']['post_text'] = $data['message'];
1793 }
1794
1795 break;
1796 }
1797
1798 $post_approved = $sql_data[POSTS_TABLE]['sql']['post_approved'];
1799 $topic_row = array();
1800
1801 // And the topic ladies and gentlemen
1802 switch ($post_mode)
1803 {
1804 case 'post':
1805 $sql_data[TOPICS_TABLE]['sql'] = array(
1806 'topic_poster' => (int) $user->data['user_id'],
1807 'topic_time' => $current_time,
1808 'topic_last_view_time' => $current_time,
1809 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1810 'icon_id' => $data['icon_id'],
1811 'topic_approved' => $post_approval,
1812 'topic_title' => $subject,
1813 'topic_first_poster_name' => (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
1814 'topic_first_poster_colour' => $user->data['user_colour'],
1815 'topic_type' => $topic_type,
1816 'topic_time_limit' => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
1817 'topic_attachment' => (!empty($data['attachment_data'])) ? 1 : 0,
1818 );
1819
1820 if (isset($poll['poll_options']) && !empty($poll['poll_options']))
1821 {
1822 $poll_start = ($poll['poll_start']) ? $poll['poll_start'] : $current_time;
1823 $poll_length = $poll['poll_length'] * 86400;
1824 if ($poll_length < 0)
1825 {
1826 $poll_start = $poll_start + $poll_length;
1827 if ($poll_start < 0)
1828 {
1829 $poll_start = 0;
1830 }
1831 $poll_length = 1;
1832 }
1833
1834 $sql_data[TOPICS_TABLE]['sql'] = array_merge($sql_data[TOPICS_TABLE]['sql'], array(
1835 'poll_title' => $poll['poll_title'],
1836 'poll_start' => $poll_start,
1837 'poll_max_options' => $poll['poll_max_options'],
1838 'poll_length' => $poll_length,
1839 'poll_vote_change' => $poll['poll_vote_change'])
1840 );
1841 }
1842
1843 $sql_data[USERS_TABLE]['stat'][] = "user_lastpost_time = $current_time" . (($auth->acl_get('f_postcount', $data['forum_id']) && $post_approval) ? ', user_posts = user_posts + 1' : '');
1844
1845 if ($topic_type != POST_GLOBAL)
1846 {
1847 if ($post_approval)
1848 {
1849 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
1850 }
1851 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($post_approval) ? ', forum_topics = forum_topics + 1' : '');
1852 }
1853 break;
1854
1855 case 'reply':
1856 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_view_time = ' . $current_time . ',
1857 topic_replies_real = topic_replies_real + 1,
1858 topic_bumped = 0,
1859 topic_bumper = 0' .
1860 (($post_approval) ? ', topic_replies = topic_replies + 1' : '') .
1861 ((!empty($data['attachment_data']) || (isset($data['topic_attachment']) && $data['topic_attachment'])) ? ', topic_attachment = 1' : '');
1862
1863 $sql_data[USERS_TABLE]['stat'][] = "user_lastpost_time = $current_time" . (($auth->acl_get('f_postcount', $data['forum_id']) && $post_approval) ? ', user_posts = user_posts + 1' : '');
1864
1865 if ($post_approval && $topic_type != POST_GLOBAL)
1866 {
1867 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
1868 }
1869 break;
1870
1871 case 'edit_topic':
1872 case 'edit_first_post':
1873 if (isset($poll['poll_options']) && !empty($poll['poll_options']))
1874 {
1875 $poll_start = ($poll['poll_start']) ? $poll['poll_start'] : $current_time;
1876 $poll_length = $poll['poll_length'] * 86400;
1877 if ($poll_length < 0)
1878 {
1879 $poll_start = $poll_start + $poll_length;
1880 if ($poll_start < 0)
1881 {
1882 $poll_start = 0;
1883 }
1884 $poll_length = 1;
1885 }
1886 }
1887
1888 $sql_data[TOPICS_TABLE]['sql'] = array(
1889 'forum_id' => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1890 'icon_id' => $data['icon_id'],
1891 'topic_approved' => (!$post_approval) ? 0 : $data['topic_approved'],
1892 'topic_title' => $subject,
1893 'topic_first_poster_name' => $username,
1894 'topic_type' => $topic_type,
1895 'topic_time_limit' => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
1896 'poll_title' => (isset($poll['poll_options'])) ? $poll['poll_title'] : '',
1897 'poll_start' => (isset($poll['poll_options'])) ? $poll_start : 0,
1898 'poll_max_options' => (isset($poll['poll_options'])) ? $poll['poll_max_options'] : 1,
1899 'poll_length' => (isset($poll['poll_options'])) ? $poll_length : 0,
1900 'poll_vote_change' => (isset($poll['poll_vote_change'])) ? $poll['poll_vote_change'] : 0,
1901 'topic_last_view_time' => $current_time,
1902
1903 'topic_attachment' => (!empty($data['attachment_data'])) ? 1 : (isset($data['topic_attachment']) ? $data['topic_attachment'] : 0),
1904 );
1905
1906 // Correctly set back the topic replies and forum posts... only if the topic was approved before and now gets disapproved
1907 if (!$post_approval && $data['topic_approved'])
1908 {
1909 // Do we need to grab some topic informations?
1910 if (!sizeof($topic_row))
1911 {
1912 $sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved
1913 FROM ' . TOPICS_TABLE . '
1914 WHERE topic_id = ' . $data['topic_id'];
1915 $result = $db->sql_query($sql);
1916 $topic_row = $db->sql_fetchrow($result);
1917 $db->sql_freeresult($result);
1918 }
1919
1920 // If this is the only post remaining we do not need to decrement topic_replies.
1921 // Also do not decrement if first post - then the topic_replies will not be adjusted if approving the topic again.
1922
1923 // If this is an edited topic or the first post the topic gets completely disapproved later on...
1924 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics = forum_topics - 1';
1925 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1);
1926
1927 set_config_count('num_topics', -1, true);
1928 set_config_count('num_posts', ($topic_row['topic_replies'] + 1) * (-1), true);
1929
1930 // Only decrement this post, since this is the one non-approved now
1931 if ($auth->acl_get('f_postcount', $data['forum_id']))
1932 {
1933 $sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
1934 }
1935 }
1936
1937 break;
1938
1939 case 'edit':
1940 case 'edit_last_post':
1941
1942 // Correctly set back the topic replies and forum posts... but only if the post was approved before.
1943 if (!$post_approval && $data['post_approved'])
1944 {
1945 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_replies = topic_replies - 1, topic_last_view_time = ' . $current_time;
1946 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - 1';
1947
1948 set_config_count('num_posts', -1, true);
1949
1950 if ($auth->acl_get('f_postcount', $data['forum_id']))
1951 {
1952 $sql_data[USERS_TABLE]['stat'][] = 'user_posts = user_posts - 1';
1953 }
1954 }
1955
1956 break;
1957 }
1958
1959 // Submit new topic
1960 if ($post_mode == 'post')
1961 {
1962 $sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' .
1963 $db->sql_build_array('INSERT', $sql_data[TOPICS_TABLE]['sql']);
1964 $db->sql_query($sql);
1965
1966 $data['topic_id'] = $db->sql_nextid();
1967
1968 $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1969 'topic_id' => $data['topic_id'])
1970 );
1971 unset($sql_data[TOPICS_TABLE]['sql']);
1972 }
1973
1974 // Submit new post
1975 if ($post_mode == 'post' || $post_mode == 'reply')
1976 {
1977 if ($post_mode == 'reply')
1978 {
1979 $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1980 'topic_id' => $data['topic_id'])
1981 );
1982 }
1983
1984 $sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data[POSTS_TABLE]['sql']);
1985 $db->sql_query($sql);
1986 $data['post_id'] = $db->sql_nextid();
1987
1988 if ($post_mode == 'post')
1989 {
1990 $sql_data[TOPICS_TABLE]['sql'] = array(
1991 'topic_first_post_id' => $data['post_id'],
1992 'topic_last_post_id' => $data['post_id'],
1993 'topic_last_post_time' => $current_time,
1994 'topic_last_poster_id' => (int) $user->data['user_id'],
1995 'topic_last_poster_name' => (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
1996 'topic_last_poster_colour' => $user->data['user_colour'],
1997 'topic_last_post_subject' => (string) $subject,
1998 );
1999 }
2000
2001 unset($sql_data[POSTS_TABLE]['sql']);
2002 }
2003
2004 $make_global = false;
2005
2006 // Are we globalising or unglobalising?
2007 if ($post_mode == 'edit_first_post' || $post_mode == 'edit_topic')
2008 {
2009 if (!sizeof($topic_row))
2010 {
2011 $sql = 'SELECT topic_type, topic_replies, topic_replies_real, topic_approved, topic_last_post_id
2012 FROM ' . TOPICS_TABLE . '
2013 WHERE topic_id = ' . $data['topic_id'];
2014 $result = $db->sql_query($sql);
2015 $topic_row = $db->sql_fetchrow($result);
2016 $db->sql_freeresult($result);
2017 }
2018
2019 // globalise/unglobalise?
2020 if (($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL) || ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL))
2021 {
2022 if (!empty($sql_data[FORUMS_TABLE]['stat']) && implode('', $sql_data[FORUMS_TABLE]['stat']))
2023 {
2024 $db->sql_query('UPDATE ' . FORUMS_TABLE . ' SET ' . implode(', ', $sql_data[FORUMS_TABLE]['stat']) . ' WHERE forum_id = ' . $data['forum_id']);
2025 }
2026
2027 $make_global = true;
2028 $sql_data[FORUMS_TABLE]['stat'] = array();
2029 }
2030
2031 // globalise
2032 if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL)
2033 {
2034 // Decrement topic/post count
2035 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies_real'] + 1);
2036 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real - 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics - 1' : '');
2037
2038 // Update forum_ids for all posts
2039 $sql = 'UPDATE ' . POSTS_TABLE . '
2040 SET forum_id = 0
2041 WHERE topic_id = ' . $data['topic_id'];
2042 $db->sql_query($sql);
2043 }
2044 // unglobalise
2045 else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL)
2046 {
2047 // Increment topic/post count
2048 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + ' . ($topic_row['topic_replies_real'] + 1);
2049 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($topic_row['topic_approved']) ? ', forum_topics = forum_topics + 1' : '');
2050
2051 // Update forum_ids for all posts
2052 $sql = 'UPDATE ' . POSTS_TABLE . '
2053 SET forum_id = ' . $data['forum_id'] . '
2054 WHERE topic_id = ' . $data['topic_id'];
2055 $db->sql_query($sql);
2056 }
2057 }
2058
2059 // Update the topics table
2060 if (isset($sql_data[TOPICS_TABLE]['sql']))
2061 {
2062 $sql = 'UPDATE ' . TOPICS_TABLE . '
2063 SET ' . $db->sql_build_array('UPDATE', $sql_data[TOPICS_TABLE]['sql']) . '
2064 WHERE topic_id = ' . $data['topic_id'];
2065 $db->sql_query($sql);
2066 }
2067
2068 // Update the posts table
2069 if (isset($sql_data[POSTS_TABLE]['sql']))
2070 {
2071 $sql = 'UPDATE ' . POSTS_TABLE . '
2072 SET ' . $db->sql_build_array('UPDATE', $sql_data[POSTS_TABLE]['sql']) . '
2073 WHERE post_id = ' . $data['post_id'];
2074 $db->sql_query($sql);
2075 }
2076
2077 // Update Poll Tables
2078 if (isset($poll['poll_options']) && !empty($poll['poll_options']))
2079 {
2080 $cur_poll_options = array();
2081
2082 if ($poll['poll_start'] && $mode == 'edit')
2083 {
2084 $sql = 'SELECT *
2085 FROM ' . POLL_OPTIONS_TABLE . '
2086 WHERE topic_id = ' . $data['topic_id'] . '
2087 ORDER BY poll_option_id';
2088 $result = $db->sql_query($sql);
2089
2090 $cur_poll_options = array();
2091 while ($row = $db->sql_fetchrow($result))
2092 {
2093 $cur_poll_options[] = $row;
2094 }
2095 $db->sql_freeresult($result);
2096 }
2097
2098 $sql_insert_ary = array();
2099
2100 for ($i = 0, $size = sizeof($poll['poll_options']); $i < $size; $i++)
2101 {
2102 if (strlen(trim($poll['poll_options'][$i])))
2103 {
2104 if (empty($cur_poll_options[$i]))
2105 {
2106 // If we add options we need to put them to the end to be able to preserve votes...
2107 $sql_insert_ary[] = array(
2108 'poll_option_id' => (int) sizeof($cur_poll_options) + 1 + sizeof($sql_insert_ary),
2109 'topic_id' => (int) $data['topic_id'],
2110 'poll_option_text' => (string) $poll['poll_options'][$i]
2111 );
2112 }
2113 else if ($poll['poll_options'][$i] != $cur_poll_options[$i])
2114 {
2115 $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . "
2116 SET poll_option_text = '" . $db->sql_escape($poll['poll_options'][$i]) . "'
2117 WHERE poll_option_id = " . $cur_poll_options[$i]['poll_option_id'] . '
2118 AND topic_id = ' . $data['topic_id'];
2119 $db->sql_query($sql);
2120 }
2121 }
2122 }
2123
2124 $db->sql_multi_insert(POLL_OPTIONS_TABLE, $sql_insert_ary);
2125
2126 if (sizeof($poll['poll_options']) < sizeof($cur_poll_options))
2127 {
2128 $sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . '
2129 WHERE poll_option_id > ' . sizeof($poll['poll_options']) . '
2130 AND topic_id = ' . $data['topic_id'];
2131 $db->sql_query($sql);
2132 }
2133
2134 // If edited, we would need to reset votes (since options can be re-ordered above, you can't be sure if the change is for changing the text or adding an option
2135 if ($mode == 'edit' && sizeof($poll['poll_options']) != sizeof($cur_poll_options))
2136 {
2137 $db->sql_query('DELETE FROM ' . POLL_VOTES_TABLE . ' WHERE topic_id = ' . $data['topic_id']);
2138 $db->sql_query('UPDATE ' . POLL_OPTIONS_TABLE . ' SET poll_option_total = 0 WHERE topic_id = ' . $data['topic_id']);
2139 }
2140 }
2141
2142 // Submit Attachments
2143 if (!empty($data['attachment_data']) && $data['post_id'] && in_array($mode, array('post', 'reply', 'quote', 'edit')))
2144 {
2145 $space_taken = $files_added = 0;
2146 $orphan_rows = array();
2147
2148 foreach ($data['attachment_data'] as $pos => $attach_row)
2149 {
2150 $orphan_rows[(int) $attach_row['attach_id']] = array();
2151 }
2152
2153 if (sizeof($orphan_rows))
2154 {
2155 $sql = 'SELECT attach_id, filesize, physical_filename
2156 FROM ' . ATTACHMENTS_TABLE . '
2157 WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan_rows)) . '
2158 AND is_orphan = 1
2159 AND poster_id = ' . $user->data['user_id'];
2160 $result = $db->sql_query($sql);
2161
2162 $orphan_rows = array();
2163 while ($row = $db->sql_fetchrow($result))
2164 {
2165 $orphan_rows[$row['attach_id']] = $row;
2166 }
2167 $db->sql_freeresult($result);
2168 }
2169
2170 foreach ($data['attachment_data'] as $pos => $attach_row)
2171 {
2172 if ($attach_row['is_orphan'] && !isset($orphan_rows[$attach_row['attach_id']]))
2173 {
2174 continue;
2175 }
2176
2177 if (!$attach_row['is_orphan'])
2178 {
2179 // update entry in db if attachment already stored in db and filespace
2180 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
2181 SET attach_comment = '" . $db->sql_escape($attach_row['attach_comment']) . "'
2182 WHERE attach_id = " . (int) $attach_row['attach_id'] . '
2183 AND is_orphan = 0';
2184 $db->sql_query($sql);
2185 }
2186 else
2187 {
2188 // insert attachment into db
2189 if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . utf8_basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
2190 {
2191 continue;
2192 }
2193
2194 $space_taken += $orphan_rows[$attach_row['attach_id']]['filesize'];
2195 $files_added++;
2196
2197 $attach_sql = array(
2198 'post_msg_id' => $data['post_id'],
2199 'topic_id' => $data['topic_id'],
2200 'is_orphan' => 0,
2201 'poster_id' => $poster_id,
2202 'attach_comment' => $attach_row['attach_comment'],
2203 );
2204
2205 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $attach_sql) . '
2206 WHERE attach_id = ' . $attach_row['attach_id'] . '
2207 AND is_orphan = 1
2208 AND poster_id = ' . $user->data['user_id'];
2209 $db->sql_query($sql);
2210 }
2211 }
2212
2213 if ($space_taken && $files_added)
2214 {
2215 set_config_count('upload_dir_size', $space_taken, true);
2216 set_config_count('num_files', $files_added, true);
2217 }
2218 }
2219
2220 // we need to update the last forum information
2221 // only applicable if the topic is not global and it is approved
2222 // we also check to make sure we are not dealing with globaling the latest topic (pretty rare but still needs to be checked)
2223 if ($topic_type != POST_GLOBAL && !$make_global && ($post_approved || !$data['post_approved']))
2224 {
2225 // the last post makes us update the forum table. This can happen if...
2226 // We make a new topic
2227 // We reply to a topic
2228 // We edit the last post in a topic and this post is the latest in the forum (maybe)
2229 // We edit the only post in the topic
2230 // We edit the first post in the topic and all the other posts are not approved
2231 if (($post_mode == 'post' || $post_mode == 'reply') && $post_approved)
2232 {
2233 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . $data['post_id'];
2234 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($subject) . "'";
2235 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . $current_time;
2236 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $user->data['user_id'];
2237 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape((!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : '')) . "'";
2238 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($user->data['user_colour']) . "'";
2239 }
2240 else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
2241 {
2242 // this does not _necessarily_ mean that we must update the info again,
2243 // it just means that we might have to
2244 $sql = 'SELECT forum_last_post_id, forum_last_post_subject
2245 FROM ' . FORUMS_TABLE . '
2246 WHERE forum_id = ' . (int) $data['forum_id'];
2247 $result = $db->sql_query($sql);
2248 $row = $db->sql_fetchrow($result);
2249 $db->sql_freeresult($result);
2250
2251 // this post is the latest post in the forum, better update
2252 if ($row['forum_last_post_id'] == $data['post_id'])
2253 {
2254 // If post approved and subject changed, or poster is anonymous, we need to update the forum_last* rows
2255 if ($post_approved && ($row['forum_last_post_subject'] !== $subject || $data['poster_id'] == ANONYMOUS))
2256 {
2257 // the post's subject changed
2258 if ($row['forum_last_post_subject'] !== $subject)
2259 {
2260 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_subject = \'' . $db->sql_escape($subject) . '\'';
2261 }
2262
2263 // Update the user name if poster is anonymous... just in case an admin changed it
2264 if ($data['poster_id'] == ANONYMOUS)
2265 {
2266 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape($username) . "'";
2267 }
2268 }
2269 else if ($data['post_approved'] !== $post_approved)
2270 {
2271 // we need a fresh change of socks, everything has become invalidated
2272 $sql = 'SELECT MAX(topic_last_post_id) as last_post_id
2273 FROM ' . TOPICS_TABLE . '
2274 WHERE forum_id = ' . (int) $data['forum_id'] . '
2275 AND topic_approved = 1';
2276 $result = $db->sql_query($sql);
2277 $row = $db->sql_fetchrow($result);
2278 $db->sql_freeresult($result);
2279
2280 // any posts left in this forum?
2281 if (!empty($row['last_post_id']))
2282 {
2283 $sql = 'SELECT p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
2284 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2285 WHERE p.poster_id = u.user_id
2286 AND p.post_id = ' . (int) $row['last_post_id'];
2287 $result = $db->sql_query($sql);
2288 $row = $db->sql_fetchrow($result);
2289 $db->sql_freeresult($result);
2290
2291 // salvation, a post is found! jam it into the forums table
2292 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
2293 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2294 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
2295 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
2296 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2297 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2298 }
2299 else
2300 {
2301 // just our luck, the last topic in the forum has just been turned unapproved...
2302 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
2303 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
2304 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
2305 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
2306 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
2307 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
2308 }
2309 }
2310 }
2311 }
2312 }
2313 else if ($make_global)
2314 {
2315 // somebody decided to be a party pooper, we must recalculate the whole shebang (maybe)
2316 $sql = 'SELECT forum_last_post_id
2317 FROM ' . FORUMS_TABLE . '
2318 WHERE forum_id = ' . (int) $data['forum_id'];
2319 $result = $db->sql_query($sql);
2320 $forum_row = $db->sql_fetchrow($result);
2321 $db->sql_freeresult($result);
2322
2323 // we made a topic global, go get new data
2324 if ($topic_row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL && $forum_row['forum_last_post_id'] == $topic_row['topic_last_post_id'])
2325 {
2326 // we need a fresh change of socks, everything has become invalidated
2327 $sql = 'SELECT MAX(topic_last_post_id) as last_post_id
2328 FROM ' . TOPICS_TABLE . '
2329 WHERE forum_id = ' . (int) $data['forum_id'] . '
2330 AND topic_approved = 1';
2331 $result = $db->sql_query($sql);
2332 $row = $db->sql_fetchrow($result);
2333 $db->sql_freeresult($result);
2334
2335 // any posts left in this forum?
2336 if (!empty($row['last_post_id']))
2337 {
2338 $sql = 'SELECT p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
2339 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2340 WHERE p.poster_id = u.user_id
2341 AND p.post_id = ' . (int) $row['last_post_id'];
2342 $result = $db->sql_query($sql);
2343 $row = $db->sql_fetchrow($result);
2344 $db->sql_freeresult($result);
2345
2346 // salvation, a post is found! jam it into the forums table
2347 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
2348 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2349 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
2350 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
2351 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2352 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2353 }
2354 else
2355 {
2356 // just our luck, the last topic in the forum has just been globalized...
2357 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = 0';
2358 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = ''";
2359 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = 0';
2360 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = 0';
2361 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = ''";
2362 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = ''";
2363 }
2364 }
2365 else if ($topic_row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL && $forum_row['forum_last_post_id'] < $topic_row['topic_last_post_id'])
2366 {
2367 // this post has a higher id, it is newer
2368 $sql = 'SELECT p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
2369 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2370 WHERE p.poster_id = u.user_id
2371 AND p.post_id = ' . (int) $topic_row['topic_last_post_id'];
2372 $result = $db->sql_query($sql);
2373 $row = $db->sql_fetchrow($result);
2374 $db->sql_freeresult($result);
2375
2376 // salvation, a post is found! jam it into the forums table
2377 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . (int) $row['post_id'];
2378 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2379 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . (int) $row['post_time'];
2380 $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $row['poster_id'];
2381 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2382 $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2383 }
2384 }
2385
2386 // topic sync time!
2387 // simply, we update if it is a reply or the last post is edited
2388 if ($post_approved)
2389 {
2390 // reply requires the whole thing
2391 if ($post_mode == 'reply')
2392 {
2393 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $data['post_id'];
2394 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $user->data['user_id'];
2395 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape((!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : '')) . "'";
2396 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . (($user->data['user_id'] != ANONYMOUS) ? $db->sql_escape($user->data['user_colour']) : '') . "'";
2397 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
2398 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $current_time;
2399 }
2400 else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies']))
2401 {
2402 // only the subject can be changed from edit
2403 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
2404
2405 // Maybe not only the subject, but also changing anonymous usernames. ;)
2406 if ($data['poster_id'] == ANONYMOUS)
2407 {
2408 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape($username) . "'";
2409 }
2410 }
2411 }
2412 else if (!$data['post_approved'] && ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || ($post_mode == 'edit_first_post' && !$data['topic_replies'])))
2413 {
2414 // like having the rug pulled from under us
2415 $sql = 'SELECT MAX(post_id) as last_post_id
2416 FROM ' . POSTS_TABLE . '
2417 WHERE topic_id = ' . (int) $data['topic_id'] . '
2418 AND post_approved = 1';
2419 $result = $db->sql_query($sql);
2420 $row = $db->sql_fetchrow($result);
2421 $db->sql_freeresult($result);
2422
2423 // any posts left in this forum?
2424 if (!empty($row['last_post_id']))
2425 {
2426 $sql = 'SELECT p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
2427 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
2428 WHERE p.poster_id = u.user_id
2429 AND p.post_id = ' . (int) $row['last_post_id'];
2430 $result = $db->sql_query($sql);
2431 $row = $db->sql_fetchrow($result);
2432 $db->sql_freeresult($result);
2433
2434 // salvation, a post is found! jam it into the topics table
2435 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_id = ' . (int) $row['post_id'];
2436 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
2437 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_post_time = ' . (int) $row['post_time'];
2438 $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_poster_id = ' . (int) $row['poster_id'];
2439 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape(($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username']) . "'";
2440 $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
2441 }
2442 }
2443
2444 // Update total post count, do not consider moderated posts/topics
2445 if ($post_approval)
2446 {
2447 if ($post_mode == 'post')
2448 {
2449 set_config_count('num_topics', 1, true);
2450 set_config_count('num_posts', 1, true);
2451 }
2452
2453 if ($post_mode == 'reply')
2454 {
2455 set_config_count('num_posts', 1, true);
2456 }
2457 }
2458
2459 // Update forum stats
2460 $where_sql = array(POSTS_TABLE => 'post_id = ' . $data['post_id'], TOPICS_TABLE => 'topic_id = ' . $data['topic_id'], FORUMS_TABLE => 'forum_id = ' . $data['forum_id'], USERS_TABLE => 'user_id = ' . $poster_id);
2461
2462 foreach ($sql_data as $table => $update_ary)
2463 {
2464 if (isset($update_ary['stat']) && implode('', $update_ary['stat']))
2465 {
2466 $sql = "UPDATE $table SET " . implode(', ', $update_ary['stat']) . ' WHERE ' . $where_sql[$table];
2467 $db->sql_query($sql);
2468 }
2469 }
2470
2471 // Delete topic shadows (if any exist). We do not need a shadow topic for an global announcement
2472 if ($make_global)
2473 {
2474 $sql = 'DELETE FROM ' . TOPICS_TABLE . '
2475 WHERE topic_moved_id = ' . $data['topic_id'];
2476 $db->sql_query($sql);
2477 }
2478
2479 // Committing the transaction before updating search index
2480 $db->sql_transaction('commit');
2481
2482 // Delete draft if post was loaded...
2483 $draft_id = request_var('draft_loaded', 0);
2484 if ($draft_id)
2485 {
2486 $sql = 'DELETE FROM ' . DRAFTS_TABLE . "
2487 WHERE draft_id = $draft_id
2488 AND user_id = {$user->data['user_id']}";
2489 $db->sql_query($sql);
2490 }
2491
2492 // Index message contents
2493 if ($update_search_index && $data['enable_indexing'])
2494 {
2495 // Select the search method and do some additional checks to ensure it can actually be utilised
2496 $search_type = basename($config['search_type']);
2497
2498 if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
2499 {
2500 trigger_error('NO_SUCH_SEARCH_MODULE');
2501 }
2502
2503 if (!class_exists($search_type))
2504 {
2505 include("{$phpbb_root_path}includes/search/$search_type.$phpEx");
2506 }
2507
2508 $error = false;
2509 $search = new $search_type($error);
2510
2511 if ($error)
2512 {
2513 trigger_error($error);
2514 }
2515
2516 $search->index($mode, $data['post_id'], $data['message'], $subject, $poster_id, ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']);
2517 }
2518
2519 // Topic Notification, do not change if moderator is changing other users posts...
2520 if ($user->data['user_id'] == $poster_id)
2521 {
2522 if (!$data['notify_set'] && $data['notify'])
2523 {
2524 $sql = 'INSERT INTO ' . TOPICS_WATCH_TABLE . ' (user_id, topic_id)
2525 VALUES (' . $user->data['user_id'] . ', ' . $data['topic_id'] . ')';
2526 $db->sql_query($sql);
2527 }
2528 else if (($config['email_enable'] || $config['jab_enable']) && $data['notify_set'] && !$data['notify'])
2529 {
2530 $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . '
2531 WHERE user_id = ' . $user->data['user_id'] . '
2532 AND topic_id = ' . $data['topic_id'];
2533 $db->sql_query($sql);
2534 }
2535 }
2536
2537 if ($mode == 'post' || $mode == 'reply' || $mode == 'quote')
2538 {
2539 // Mark this topic as posted to
2540 markread('post', $data['forum_id'], $data['topic_id'], $data['post_time']);
2541 }
2542
2543 // Mark this topic as read
2544 // We do not use post_time here, this is intended (post_time can have a date in the past if editing a message)
2545 markread('topic', (($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']), $data['topic_id'], time());
2546
2547 //
2548 if ($config['load_db_lastread'] && $user->data['is_registered'])
2549 {
2550 $sql = 'SELECT mark_time
2551 FROM ' . FORUMS_TRACK_TABLE . '
2552 WHERE user_id = ' . $user->data['user_id'] . '
2553 AND forum_id = ' . (($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']);
2554 $result = $db->sql_query($sql);
2555 $f_mark_time = (int) $db->sql_fetchfield('mark_time');
2556 $db->sql_freeresult($result);
2557 }
2558 else if ($config['load_anon_lastread'] || $user->data['is_registered'])
2559 {
2560 $f_mark_time = false;
2561 }
2562
2563 if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
2564 {
2565 // Update forum info
2566 if ($topic_type == POST_GLOBAL)
2567 {
2568 $sql = 'SELECT MAX(topic_last_post_time) as forum_last_post_time
2569 FROM ' . TOPICS_TABLE . '
2570 WHERE forum_id = 0';
2571 }
2572 else
2573 {
2574 $sql = 'SELECT forum_last_post_time
2575 FROM ' . FORUMS_TABLE . '
2576 WHERE forum_id = ' . $data['forum_id'];
2577 }
2578 $result = $db->sql_query($sql);
2579 $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
2580 $db->sql_freeresult($result);
2581
2582 update_forum_tracking_info((($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']), $forum_last_post_time, $f_mark_time, false);
2583 }
2584
2585 // Send Notifications
2586 if ($mode != 'edit' && $mode != 'delete' && $post_approval)
2587 {
2588 user_notification($mode, $subject, $data['topic_title'], $data['forum_name'], $data['forum_id'], $data['topic_id'], $data['post_id']);
2589 }
2590
2591 $params = $add_anchor = '';
2592
2593 if ($post_approval)
2594 {
2595 $params .= '&amp;t=' . $data['topic_id'];
2596
2597 if ($mode != 'post')
2598 {
2599 $params .= '&amp;p=' . $data['post_id'];
2600 $add_anchor = '#p' . $data['post_id'];
2601 }
2602 }
2603 else if ($mode != 'post' && $post_mode != 'edit_first_post' && $post_mode != 'edit_topic')
2604 {
2605 $params .= '&amp;t=' . $data['topic_id'];
2606 }
2607
2608 $url = (!$params) ? "{$phpbb_root_path}viewforum.$phpEx" : "{$phpbb_root_path}viewtopic.$phpEx";
2609 $url = append_sid($url, 'f=' . $data['forum_id'] . $params) . $add_anchor;
2610
2611 return $url;
2612}
2613
2614?>
Note: See TracBrowser for help on using the repository browser.