phpBB
Statistics
| Revision:

root / branches / phpBB-3_0_0 / phpBB / includes / functions_posting.php

History | View | Annotate | Download (86.9 kB)

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