· 8 years ago · May 29, 2018, 06:02 AM
1<?php
2/***********************************************************************
3
4 Copyright (C) 2008 FluxBB.org
5
6 Based on code copyright (C) 2002-2008 PunBB.org
7
8 This file is part of FluxBB.
9
10 FluxBB is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published
12 by the Free Software Foundation; either version 2 of the License,
13 or (at your option) any later version.
14
15 FluxBB is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 MA 02111-1307 USA
24
25************************************************************************/
26
27
28//
29// Return all code blocks that hook into $hook_id
30//
31function get_hook($hook_id)
32{
33 global $forum_hooks;
34
35 return !defined('FORUM_DISABLE_HOOKS') && isset($forum_hooks[$hook_id]) ? implode("\n", $forum_hooks[$hook_id]) : false;
36}
37
38
39//
40// Authenticates the provided username and password against the user database
41// $user can be either a user ID (integer) or a username (string)
42// $password can be either a plaintext password or a password hash including salt ($password_is_hash must be set accordingly)
43//
44function authenticate_user($user, $password, $password_is_hash = false)
45{
46 global $forum_db, $forum_user;
47
48 ($hook = get_hook('fn_authenticate_user_start')) ? eval($hook) : null;
49
50 // Check if there's a user matching $user and $password
51 $query = array(
52 'SELECT' => 'u.*, g.*, o.logged, o.idle, o.csrf_token, o.prev_url',
53 'FROM' => 'users AS u',
54 'JOINS' => array(
55 array(
56 'INNER JOIN' => 'groups AS g',
57 'ON' => 'g.g_id=u.group_id'
58 ),
59 array(
60 'LEFT JOIN' => 'online AS o',
61 'ON' => 'o.user_id=u.id'
62 )
63 )
64 );
65
66 // Are we looking for a user ID or a username?
67 $query['WHERE'] = is_int($user) ? 'u.id='.intval($user) : 'u.username=\''.$forum_db->escape($user).'\'';
68
69 ($hook = get_hook('fn_qr_get_user')) ? eval($hook) : null;
70 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
71 $forum_user = $forum_db->fetch_assoc($result);
72
73 if (!isset($forum_user['id']) ||
74 ($password_is_hash && $password != $forum_user['password']) ||
75 (!$password_is_hash && sha1($forum_user['salt'].sha1($password)) != $forum_user['password']))
76 set_default_user();
77
78 ($hook = get_hook('fn_authenticate_user_end')) ? eval($hook) : null;
79}
80
81
82//
83// Attempt to login with the user ID and password hash from the cookie
84//
85function cookie_login(&$forum_user)
86{
87 global $forum_db, $db_type, $forum_config, $cookie_name, $cookie_path, $cookie_domain, $cookie_secure, $forum_time_formats, $forum_date_formats;
88
89 ($hook = get_hook('fn_cookie_login_start')) ? eval($hook) : null;
90
91 $now = time();
92 $expire = $now + 31536000; // The cookie expires after a year
93
94 // We assume it's a guest
95 $cookie = array('user_id' => 1, 'password_hash' => 'Guest');
96
97 // If a cookie is set, we get the user_id and password hash from it
98 if (isset($_COOKIE[$cookie_name]))
99 @list($cookie['user_id'], $cookie['password_hash']) = @explode('|', base64_decode($_COOKIE[$cookie_name]));
100
101 ($hook = get_hook('fn_cookie_login_fetch_cookie')) ? eval($hook) : null;
102
103 if (intval($cookie['user_id']) > 1)
104 {
105 authenticate_user(intval($cookie['user_id']), $cookie['password_hash'], true);
106
107 // If we got back the default user, the login failed
108 if ($forum_user['id'] == '1')
109 {
110 forum_setcookie($cookie_name, base64_encode('1|'.random_key(8, true)), $expire);
111 return;
112 }
113
114 // Set a default language if the user selected language no longer exists
115 if (!file_exists(FORUM_ROOT.'lang/'.$forum_user['language'].'/common.php'))
116 $forum_user['language'] = $forum_config['o_default_lang'];
117
118 // Set a default style if the user selected style no longer exists
119 if (!file_exists(FORUM_ROOT.'style/'.$forum_user['style'].'/'.$forum_user['style'].'.php'))
120 $forum_user['style'] = $forum_config['o_default_style'];
121
122 if (!$forum_user['disp_topics'])
123 $forum_user['disp_topics'] = $forum_config['o_disp_topics_default'];
124 if (!$forum_user['disp_posts'])
125 $forum_user['disp_posts'] = $forum_config['o_disp_posts_default'];
126
127 if ($forum_user['save_pass'] == '0')
128 $expire = 0;
129
130 // Check user has a valid date and time format
131 if (!isset($forum_time_formats[$forum_user['time_format']]))
132 $forum_user['time_format'] = 0;
133 if (!isset($forum_date_formats[$forum_user['date_format']]))
134 $forum_user['date_format'] = 0;
135
136 // Define this if you want this visit to affect the online list and the users last visit data
137 if (!defined('FORUM_QUIET_VISIT'))
138 {
139 // Update the online list
140 if (!$forum_user['logged'])
141 {
142 $forum_user['logged'] = $now;
143 $forum_user['csrf_token'] = random_key(40, false, true);
144 $forum_user['prev_url'] = get_current_url(255);
145
146 // REPLACE INTO avoids a user having two rows in the online table
147 $query = array(
148 'REPLACE' => 'user_id, ident, logged, csrf_token',
149 'INTO' => 'online',
150 'VALUES' => $forum_user['id'].', \''.$forum_db->escape($forum_user['username']).'\', '.$forum_user['logged'].', \''.$forum_user['csrf_token'].'\'',
151 'UNIQUE' => 'user_id='.$forum_user['id']
152 );
153
154 if ($forum_user['prev_url'] != null)
155 {
156 $query['REPLACE'] .= ', prev_url';
157 $query['VALUES'] .= ', \''.$forum_db->escape($forum_user['prev_url']).'\'';
158 }
159
160 ($hook = get_hook('fn_qr_add_online_user')) ? eval($hook) : null;
161 $forum_db->query_build($query) or error(__FILE__, __LINE__);
162
163 // Reset tracked topics
164 set_tracked_topics(null);
165 }
166 else
167 {
168 // Special case: We've timed out, but no other user has browsed the forums since we timed out
169 if ($forum_user['logged'] < ($now-$forum_config['o_timeout_visit']))
170 {
171 $query = array(
172 'UPDATE' => 'users',
173 'SET' => 'last_visit='.$forum_user['logged'],
174 'WHERE' => 'id='.$forum_user['id']
175 );
176
177 ($hook = get_hook('fn_qr_update_user_visit')) ? eval($hook) : null;
178 $forum_db->query_build($query) or error(__FILE__, __LINE__);
179
180 $forum_user['last_visit'] = $forum_user['logged'];
181 }
182
183 $forum_user['prev_url'] = get_current_url(255);
184
185 // Now update the logged time and save the current URL in the online list
186 $query = array(
187 'UPDATE' => 'online',
188 'SET' => 'logged='.$now,
189 'WHERE' => 'user_id='.$forum_user['id']
190 );
191
192 if ($forum_user['prev_url'] != null)
193 $query['SET'] .= ', prev_url=\''.$forum_db->escape($forum_user['prev_url']).'\'';
194
195 if ($forum_user['idle'] == '1')
196 $query['SET'] .= ', idle=0';
197
198 ($hook = get_hook('fn_qr_update_online_user')) ? eval($hook) : null;
199 $forum_db->query_build($query) or error(__FILE__, __LINE__);
200
201 // Update tracked topics with the current expire time
202 if (isset($_COOKIE[$cookie_name.'_track']))
203 forum_setcookie($cookie_name.'_track', $_COOKIE[$cookie_name.'_track'], time() + $forum_config['o_timeout_visit']);
204 }
205 }
206
207 $forum_user['is_guest'] = false;
208 $forum_user['is_admmod'] = $forum_user['g_id'] == FORUM_ADMIN || $forum_user['g_moderator'] == '1';
209 }
210 else
211 set_default_user();
212
213 ($hook = get_hook('fn_cookie_login_end')) ? eval($hook) : null;
214}
215
216
217//
218// Fill $forum_user with default values (for guests)
219//
220function set_default_user()
221{
222 global $forum_db, $db_type, $forum_user, $forum_config;
223
224 ($hook = get_hook('fn_set_default_user_start')) ? eval($hook) : null;
225
226 $remote_addr = get_remote_address();
227
228 // Fetch guest user
229 $query = array(
230 'SELECT' => 'u.*, g.*, o.logged, o.csrf_token, o.prev_url',
231 'FROM' => 'users AS u',
232 'JOINS' => array(
233 array(
234 'INNER JOIN' => 'groups AS g',
235 'ON' => 'g.g_id=u.group_id'
236 ),
237 array(
238 'LEFT JOIN' => 'online AS o',
239 'ON' => 'o.ident=\''.$remote_addr.'\''
240 )
241 ),
242 'WHERE' => 'u.id=1'
243 );
244
245 ($hook = get_hook('fn_qr_get_default_user')) ? eval($hook) : null;
246 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
247 if (!$forum_db->num_rows($result))
248 exit('Unable to fetch guest information. The table \''.$forum_db->prefix.'users\' must contain an entry with id = 1 that represents anonymous users.');
249
250 $forum_user = $forum_db->fetch_assoc($result);
251
252 // Update online list
253 if (!$forum_user['logged'])
254 {
255 $forum_user['logged'] = time();
256 $forum_user['csrf_token'] = random_key(40, false, true);
257 $forum_user['prev_url'] = get_current_url(255);
258
259 // REPLACE INTO avoids a user having two rows in the online table
260 $query = array(
261 'REPLACE' => 'user_id, ident, logged, csrf_token',
262 'INTO' => 'online',
263 'VALUES' => '1, \''.$forum_db->escape($remote_addr).'\', '.$forum_user['logged'].', \''.$forum_user['csrf_token'].'\'',
264 'UNIQUE' => 'user_id=1 AND ident=\''.$forum_db->escape($remote_addr).'\''
265 );
266
267 if ($forum_user['prev_url'] != null)
268 {
269 $query['REPLACE'] .= ', prev_url';
270 $query['VALUES'] .= ', \''.$forum_db->escape($forum_user['prev_url']).'\'';
271 }
272
273 ($hook = get_hook('fn_qr_add_online_guest_user')) ? eval($hook) : null;
274 $forum_db->query_build($query) or error(__FILE__, __LINE__);
275 }
276 else
277 {
278 $forum_user['prev_url'] = get_current_url(255);
279
280 $query = array(
281 'UPDATE' => 'online',
282 'SET' => 'logged='.time(),
283 'WHERE' => 'ident=\''.$forum_db->escape($remote_addr).'\''
284 );
285
286 if ($forum_user['prev_url'] != null)
287 $query['SET'] .= ', prev_url=\''.$forum_db->escape($forum_user['prev_url']).'\'';
288
289 ($hook = get_hook('fn_qr_update_online_guest_user')) ? eval($hook) : null;
290 $forum_db->query_build($query) or error(__FILE__, __LINE__);
291 }
292
293 $forum_user['disp_topics'] = $forum_config['o_disp_topics_default'];
294 $forum_user['disp_posts'] = $forum_config['o_disp_posts_default'];
295 $forum_user['timezone'] = $forum_config['o_default_timezone'];
296 $forum_user['language'] = $forum_config['o_default_lang'];
297 $forum_user['style'] = $forum_config['o_default_style'];
298 $forum_user['is_guest'] = true;
299 $forum_user['is_admmod'] = false;
300}
301
302
303//
304// Set a cookie, FluxBB style!
305//
306function forum_setcookie($name, $value, $expire)
307{
308 global $cookie_path, $cookie_domain, $cookie_secure;
309
310 ($hook = get_hook('fn_forum_setcookie_start')) ? eval($hook) : null;
311
312 // Enable sending of a P3P header by removing // from the following line (try this if login is failing in IE6)
313// @header('P3P: CP="CUR ADM"');
314
315 if (version_compare(PHP_VERSION, '5.2.0', '>='))
316 setcookie($name, $value, $expire, $cookie_path, $cookie_domain, $cookie_secure, true);
317 else
318 setcookie($name, $value, $expire, $cookie_path.'; HttpOnly', $cookie_domain, $cookie_secure);
319}
320
321
322//
323// Check whether the connecting user is banned (and delete any expired bans while we're at it)
324//
325function check_bans()
326{
327 global $forum_db, $forum_config, $lang_common, $forum_user, $forum_bans;
328
329 ($hook = get_hook('fn_check_bans_start')) ? eval($hook) : null;
330
331 // Admins aren't affected
332 if (defined('FORUM_ADMIN') && $forum_user['g_id'] == FORUM_ADMIN || !$forum_bans)
333 return;
334
335 // Add a dot or a colon (depending on IPv4/IPv6) at the end of the IP address to prevent banned address
336 // 192.168.0.5 from matching e.g. 192.168.0.50
337 $user_ip = get_remote_address();
338 $user_ip .= (strpos($user_ip, '.') !== false) ? '.' : ':';
339
340 $bans_altered = false;
341
342 foreach ($forum_bans as $cur_ban)
343 {
344 // Has this ban expired?
345 if ($cur_ban['expire'] != '' && $cur_ban['expire'] <= time())
346 {
347 $query = array(
348 'DELETE' => 'bans',
349 'WHERE' => 'id='.$cur_ban['id']
350 );
351
352 ($hook = get_hook('fn_qr_delete_expired_ban')) ? eval($hook) : null;
353 $forum_db->query_build($query) or error(__FILE__, __LINE__);
354
355 $bans_altered = true;
356 continue;
357 }
358
359 if ($cur_ban['username'] != '' && strtolower($forum_user['username']) == strtolower($cur_ban['username']))
360 {
361 $query = array(
362 'DELETE' => 'online',
363 'WHERE' => 'ident=\''.$forum_db->escape($forum_user['username']).'\''
364 );
365
366 ($hook = get_hook('fn_qr_delete_online_user')) ? eval($hook) : null;
367 $forum_db->query_build($query) or error(__FILE__, __LINE__);
368
369 message($lang_common['Ban message'].(($cur_ban['expire'] != '') ? ' '.sprintf($lang_common['Ban message 2'], strtolower(format_time($cur_ban['expire'], true))) : '').(($cur_ban['message'] != '') ? ' '.$lang_common['Ban message 3'].'</p><p><strong>'.forum_htmlencode($cur_ban['message']).'</strong></p>' : '</p>').'<p>'.sprintf($lang_common['Ban message 4'], '<a href="mailto:'.$forum_config['o_admin_email'].'">'.$forum_config['o_admin_email'].'</a>'));
370 }
371
372 if ($cur_ban['ip'] != '')
373 {
374 $cur_ban_ips = explode(' ', $cur_ban['ip']);
375
376 $num_ips = count($cur_ban_ips);
377 for ($i = 0; $i < $num_ips; ++$i)
378 {
379 // Both the ban and the IP match IPv4
380 if (strpos($cur_ban_ips[$i], '.') !== false && strpos($user_ip, '.') !== false)
381 $cur_ban_ips[$i] = $cur_ban_ips[$i].'.';
382 // Both the ban and the IP match IPv6
383 else if (strpos($cur_ban_ips[$i], ':') !== false && strpos($user_ip, ':') !== false)
384 $cur_ban_ips[$i] = $cur_ban_ips[$i].':';
385 // The ban and the IP are not using the same system
386 else
387 continue;
388
389 if (substr($user_ip, 0, strlen($cur_ban_ips[$i])) == $cur_ban_ips[$i])
390 {
391 $query = array(
392 'DELETE' => 'online',
393 'WHERE' => 'ident=\''.$forum_db->escape($forum_user['username']).'\''
394 );
395
396 ($hook = get_hook('fn_qr_delete_online_user2')) ? eval($hook) : null;
397 $forum_db->query_build($query) or error(__FILE__, __LINE__);
398
399 message($lang_common['Ban message'].(($cur_ban['expire'] != '') ? ' '.sprintf($lang_common['Ban message 2'], strtolower(format_time($cur_ban['expire'], true))) : '').(($cur_ban['message'] != '') ? ' '.$lang_common['Ban message 3'].'</p><p><strong>'.forum_htmlencode($cur_ban['message']).'</strong></p>' : '</p>').'<p>'.sprintf($lang_common['Ban message 4'], '<a href="mailto:'.$forum_config['o_admin_email'].'">'.$forum_config['o_admin_email'].'</a>'));
400 }
401 }
402 }
403 }
404
405 // If we removed any expired bans during our run-through, we need to regenerate the bans cache
406 if ($bans_altered)
407 {
408 require_once FORUM_ROOT.'include/cache.php';
409 generate_bans_cache();
410 }
411}
412
413
414//
415// Update "Users online"
416//
417function update_users_online()
418{
419 global $forum_db, $forum_config, $forum_user;
420
421 $now = time();
422
423 ($hook = get_hook('fn_update_users_online_start')) ? eval($hook) : null;
424
425 // Fetch all online list entries that are older than "o_timeout_online"
426 $query = array(
427 'SELECT' => 'o.*',
428 'FROM' => 'online AS o',
429 'WHERE' => 'o.logged<'.($now-$forum_config['o_timeout_online'])
430 );
431
432 ($hook = get_hook('fn_qr_get_old_online_users')) ? eval($hook) : null;
433 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
434 while ($cur_user = $forum_db->fetch_assoc($result))
435 {
436 // If the entry is a guest, delete it
437 if ($cur_user['user_id'] == '1')
438 {
439 $query = array(
440 'DELETE' => 'online',
441 'WHERE' => 'ident=\''.$forum_db->escape($cur_user['ident']).'\''
442 );
443
444 ($hook = get_hook('fn_qr_delete_online_guest_user')) ? eval($hook) : null;
445 $forum_db->query_build($query) or error(__FILE__, __LINE__);
446 }
447 else
448 {
449 // If the entry is older than "o_timeout_visit", update last_visit for the user in question, then delete him/her from the online list
450 if ($cur_user['logged'] < ($now-$forum_config['o_timeout_visit']))
451 {
452 $query = array(
453 'UPDATE' => 'users',
454 'SET' => 'last_visit='.$cur_user['logged'],
455 'WHERE' => 'id='.$cur_user['user_id']
456 );
457
458 ($hook = get_hook('fn_qr_update_user_visit2')) ? eval($hook) : null;
459 $forum_db->query_build($query) or error(__FILE__, __LINE__);
460
461 $query = array(
462 'DELETE' => 'online',
463 'WHERE' => 'user_id='.$cur_user['user_id']
464 );
465
466 ($hook = get_hook('fn_qr_delete_online_user3')) ? eval($hook) : null;
467 $forum_db->query_build($query) or error(__FILE__, __LINE__);
468 }
469 else if ($cur_user['idle'] == '0')
470 {
471 $query = array(
472 'UPDATE' => 'online',
473 'SET' => 'idle=1',
474 'WHERE' => 'user_id='.$cur_user['user_id']
475 );
476
477 ($hook = get_hook('fn_qr_update_online_user2')) ? eval($hook) : null;
478 $forum_db->query_build($query) or error(__FILE__, __LINE__);
479 }
480 }
481 }
482
483 ($hook = get_hook('fn_update_users_online_end')) ? eval($hook) : null;
484}
485
486
487//
488// Generate the "navigator" that appears at the top of every page
489//
490function generate_navlinks()
491{
492 global $forum_config, $lang_common, $forum_url, $forum_user;
493
494 // Index should always be displayed
495 $links['index'] = '<li id="navindex"'.((FORUM_PAGE == 'index') ? ' class="isactive"' : '').'><a href="'.forum_link($forum_url['index']).'"><span>'.$lang_common['Index'].'</span></a></li>';
496
497 if ($forum_user['g_read_board'] == '1' && $forum_user['g_view_users'] == '1')
498 $links['userlist'] = '<li id="navuserlist"'.((FORUM_PAGE == 'userlist') ? ' class="isactive"' : '').'><a href="'.forum_link($forum_url['users']).'"><span>'.$lang_common['User list'].'</span></a></li>';
499
500 if ($forum_config['o_rules'] == '1' && (!$forum_user['is_guest'] || $forum_user['g_read_board'] == '1' || $forum_config['o_regs_allow'] == '1'))
501 $links['rules'] = '<li id="navrules"'.((FORUM_PAGE == 'rules') ? ' class="isactive"' : '').'><a href="'.forum_link($forum_url['rules']).'"><span>'.$lang_common['Rules'].'</span></a></li>';
502
503 if ($forum_user['is_guest'])
504 {
505 if ($forum_user['g_read_board'] == '1' && $forum_user['g_search'] == '1')
506 $links['search'] = '<li id="navsearch"'.((FORUM_PAGE == 'search') ? ' class="isactive"' : '').'><a href="'.forum_link($forum_url['search']).'"><span>'.$lang_common['Search'].'</span></a></li>';
507
508 $links['register'] = '<li id="navregister"'.((FORUM_PAGE == 'register') ? ' class="isactive"' : '').'><a href="'.forum_link($forum_url['register']).'"><span>'.$lang_common['Register'].'</span></a></li>';
509 $links['login'] = '<li id="navlogin"'.((FORUM_PAGE == 'login') ? ' class="isactive"' : '').'><a href="'.forum_link($forum_url['login']).'"><span>'.$lang_common['Login'].'</span></a></li>';
510 }
511 else
512 {
513 if (!$forum_user['is_admmod'])
514 {
515 if ($forum_user['g_read_board'] == '1' && $forum_user['g_search'] == '1')
516 $links['search'] = '<li id="navsearch"'.((FORUM_PAGE == 'search') ? ' class="isactive"' : '').'><a href="'.forum_link($forum_url['search']).'"><span>'.$lang_common['Search'].'</span></a></li>';
517
518 $links['profile'] = '<li id="navprofile"'.((substr(FORUM_PAGE, 0, 7) == 'profile') ? ' class="isactive"' : '').'><a href="'.forum_link($forum_url['user'], $forum_user['id']).'"><span>'.$lang_common['Profile'].'</span></a></li>';
519 }
520 else
521 {
522 $links['search'] = '<li id="navsearch"'.((FORUM_PAGE == 'search') ? ' class="isactive"' : '').'><a href="'.forum_link($forum_url['search']).'"><span>'.$lang_common['Search'].'</span></a></li>';
523 $links['profile'] = '<li id="navprofile"'.((FORUM_PAGE == 'editprofile' || FORUM_PAGE == 'viewprofile') ? ' class="isactive"' : '').'><a href="'.forum_link($forum_url['user'], $forum_user['id']).'"><span>'.$lang_common['Profile'].'</span></a></li>';
524 $links['admin'] = '<li id="navadmin"'.((substr(FORUM_PAGE, 0, 5) == 'admin') ? ' class="isactive"' : '').'><a href="'.forum_link($forum_url['admin_index']).'"><span>'.$lang_common['Admin'].'</span></a></li>';
525 }
526
527 $links['logout'] = '<li id="navlogout"><a href="'.forum_link($forum_url['logout'], array($forum_user['id'], generate_form_token('logout'.$forum_user['id']))).'"><span>'.$lang_common['Logout'].'</span></a></li>';
528 }
529
530 // Are there any additional navlinks we should insert into the array before imploding it?
531 if ($forum_config['o_additional_navlinks'] != '')
532 {
533 if (preg_match_all('#([0-9]+)\s*=\s*(.*?)\n#s', $forum_config['o_additional_navlinks']."\n", $extra_links))
534 {
535 // Insert any additional links into the $links array (at the correct index)
536 $num_links = count($extra_links[1]);
537 for ($i = 0; $i < $num_links; ++$i)
538 array_insert($links, (int)$extra_links[1][$i], '<li id="navextra'.($i + 1).'">'.$extra_links[2][$i].'</li>');
539 }
540 }
541
542 ($hook = get_hook('fn_generate_navlinks_end')) ? eval($hook) : null;
543
544 return implode("\n\t\t", $links);
545}
546
547
548//
549// Display the profile navigation menu
550//
551function generate_profile_menu()
552{
553 global $lang_profile, $forum_url, $forum_config, $forum_user, $id;
554
555 // Setup links for profile menu
556 $profilenav_links = array(
557 ''.((FORUM_PAGE == 'profile-about') ? '' : '').'<a href="'.forum_link($forum_url['profile_about'], $id).'"><span>'.$lang_profile['Section about'].'</span></a>',
558 ''.((FORUM_PAGE == 'profile-identity') ? '' : '').'<a href="'.forum_link($forum_url['profile_identity'], $id).'"><span>'.$lang_profile['Section identity'].'</span></a>',
559 ''.((FORUM_PAGE == 'profile-settings') ? '' : '').'<a href="'.forum_link($forum_url['profile_settings'], $id).'"><span>'.$lang_profile['Section settings'].'</span></a>',
560 );
561
562 if ($forum_config['o_signatures'] == '1')
563 $profilenav_links['signature'] = ''.((FORUM_PAGE == 'profile-signature') ? '' : '').'<a href="'.forum_link($forum_url['profile_signature'], $id).'"><span>'.$lang_profile['Section signature'].'</span></a></li>';
564
565 if ($forum_config['o_avatars'] == '1')
566 $profilenav_links['avatar'] = ''.((FORUM_PAGE == 'profile-avatar') ? '' : '').'<a href="'.forum_link($forum_url['profile_avatar'], $id).'"><span>'.$lang_profile['Section avatar'].'</span></a>';
567
568 if ($forum_user['g_id'] == FORUM_ADMIN || ($forum_user['g_moderator'] == '1' && $forum_user['g_mod_ban_users'] == '1'))
569 $profilenav_links['admin'] = ''.((FORUM_PAGE == 'profile-admin') ? '' : '').'<a href="'.forum_link($forum_url['profile_admin'], $id).'"><span>'.$lang_profile['Section admin'].'</span></a>';
570
571 ($hook = get_hook('fn_generate_profile_menu_end')) ? eval($hook) : null;
572
573?>
574 <div class="ProfileOptions">
575 <?php echo implode("\n\t\t\t", $profilenav_links)."\n"; ?>
576 </div>
577<?php
578
579}
580
581
582//
583// Generate breadcrumb navigation
584//
585function generate_crumbs($reverse)
586{
587 global $lang_common, $forum_url, $forum_config, $forum_page;
588
589 ($hook = get_hook('fn_generate_crumbs_start')) ? eval($hook) : null;
590
591 if (empty($forum_page['crumbs']))
592 $forum_page['crumbs'][0] = $forum_config['o_board_title'];
593
594 $crumbs = '';
595 $num_crumbs = count($forum_page['crumbs']);
596
597 if ($reverse)
598 {
599 for ($i = ($num_crumbs - 1); $i >= 0; --$i)
600 $crumbs .= (is_array($forum_page['crumbs'][$i]) ? forum_htmlencode($forum_page['crumbs'][$i][0]) : forum_htmlencode($forum_page['crumbs'][$i])).((isset($forum_page['page']) && $i == ($num_crumbs - 1)) ? ' ('.$lang_common['Page'].' '.$forum_page['page'].')' : '').($i > 0 ? $lang_common['Title separator'] : '');
601 }
602 else
603 {
604 for ($i = 0; $i < $num_crumbs; ++$i)
605 {
606 if ($i < ($num_crumbs - 1))
607 $crumbs .= '<span class="crumb'.(($i == 0) ? ' crumbfirst' : '').'">'.(($i >= 1) ? '<span>'.$lang_common['Crumb separator'].'</span>' : '').(is_array($forum_page['crumbs'][$i]) ? '<a href="'.$forum_page['crumbs'][$i][1].'">'.forum_htmlencode($forum_page['crumbs'][$i][0]).'</a>' : forum_htmlencode($forum_page['crumbs'][$i])).'</span> ';
608 else
609 $crumbs .= '<span class="crumb crumblast'.(($i == 0) ? ' crumbfirst' : '').'">'.(($i >= 1) ? '<span>'.$lang_common['Crumb separator'].'</span>' : '').(is_array($forum_page['crumbs'][$i]) ? '<a href="'.$forum_page['crumbs'][$i][1].'">'.forum_htmlencode($forum_page['crumbs'][$i][0]).'</a>' : forum_htmlencode($forum_page['crumbs'][$i])).'</span> ';
610 }
611 }
612
613 ($hook = get_hook('fn_generate_crumbs_end')) ? eval($hook) : null;
614
615 return $crumbs;
616}
617
618
619//
620// Save array of tracked topics in cookie
621//
622function set_tracked_topics($tracked_topics)
623{
624 global $cookie_name, $cookie_path, $cookie_domain, $cookie_secure, $forum_config;
625
626 ($hook = get_hook('fn_set_tracked_topics_start')) ? eval($hook) : null;
627
628 $cookie_data = '';
629 if (!empty($tracked_topics))
630 {
631 // Sort the arrays (latest read first)
632 arsort($tracked_topics['topics'], SORT_NUMERIC);
633 arsort($tracked_topics['forums'], SORT_NUMERIC);
634
635 // Homebrew serialization (to avoid having to run unserialize() on cookie data)
636 foreach ($tracked_topics['topics'] as $id => $timestamp)
637 $cookie_data .= 't'.$id.'='.$timestamp.';';
638 foreach ($tracked_topics['forums'] as $id => $timestamp)
639 $cookie_data .= 'f'.$id.'='.$timestamp.';';
640
641 // Enforce a 4048 byte size limit (4096 minus some space for the cookie name)
642 if (strlen($cookie_data) > 4048)
643 {
644 $cookie_data = substr($cookie_data, 0, 4048);
645 $cookie_data = substr($cookie_data, 0, strrpos($cookie_data, ';')).';';
646 }
647 }
648
649 forum_setcookie($cookie_name.'_track', $cookie_data, time() + $forum_config['o_timeout_visit']);
650 $_COOKIE[$cookie_name.'_track'] = $cookie_data; // Set it directly in $_COOKIE as well
651}
652
653
654//
655// Extract array of tracked topics from cookie
656//
657function get_tracked_topics()
658{
659 global $cookie_name;
660
661 $cookie_data = isset($_COOKIE[$cookie_name.'_track']) ? $_COOKIE[$cookie_name.'_track'] : false;
662 if (!$cookie_data)
663 return array('topics' => array(), 'forums' => array());
664
665 if (strlen($cookie_data) > 4048)
666 return array('topics' => array(), 'forums' => array());
667
668 // Unserialize data from cookie
669 $tracked_topics = array('topics' => array(), 'forums' => array());
670 $temp = explode(';', $cookie_data);
671 foreach ($temp as $t)
672 {
673 $type = substr($t, 0, 1) == 'f' ? 'forums' : 'topics';
674 $id = intval(substr($t, 1));
675 $timestamp = intval(@substr($t, strpos($t, '=') + 1));
676 if ($id > 0 && $timestamp > 0)
677 $tracked_topics[$type][$id] = $timestamp;
678 }
679
680 ($hook = get_hook('fn_get_tracked_topics_end')) ? eval($hook) : null;
681
682 return $tracked_topics;
683}
684
685
686//
687// Update posts, topics, last_post, last_post_id and last_poster for a forum
688//
689function sync_forum($forum_id)
690{
691 global $forum_db;
692
693 ($hook = get_hook('fn_sync_forum_start')) ? eval($hook) : null;
694
695 // Get topic and post count for forum
696 $query = array(
697 'SELECT' => 'COUNT(t.id), SUM(t.num_replies)',
698 'FROM' => 'topics AS t',
699 'WHERE' => 't.forum_id='.$forum_id
700 );
701
702 ($hook = get_hook('fn_qr_get_forum_stats')) ? eval($hook) : null;
703 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
704 list($num_topics, $num_posts) = $forum_db->fetch_row($result);
705
706 $num_posts = $num_posts + $num_topics; // $num_posts is only the sum of all replies (we have to add the topic posts)
707
708 // Get last_post, last_post_id and last_poster for forum (if any)
709 $query = array(
710 'SELECT' => 't.last_post, t.last_post_id, t.last_poster',
711 'FROM' => 'topics AS t',
712 'WHERE' => 't.forum_id='.$forum_id.' AND t.moved_to is NULL',
713 'ORDER BY' => 't.last_post DESC',
714 'LIMIT' => '1'
715 );
716
717 ($hook = get_hook('fn_qr_get_forum_last_post_data')) ? eval($hook) : null;
718 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
719 if ($forum_db->num_rows($result))
720 {
721 list($last_post, $last_post_id, $last_poster) = $forum_db->fetch_row($result);
722 $last_poster = '\''.$forum_db->escape($last_poster).'\'';
723 }
724 else
725 $last_post = $last_post_id = $last_poster = 'NULL';
726
727 // Now update the forum
728 $query = array(
729 'UPDATE' => 'forums',
730 'SET' => 'num_topics='.$num_topics.', num_posts='.$num_posts.', last_post='.$last_post.', last_post_id='.$last_post_id.', last_poster='.$last_poster,
731 'WHERE' => 'id='.$forum_id
732 );
733
734 ($hook = get_hook('fn_qr_update_forum')) ? eval($hook) : null;
735 $forum_db->query_build($query) or error(__FILE__, __LINE__);
736}
737
738
739//
740// Update replies, last_post, last_post_id and last_poster for a topic
741//
742function sync_topic($topic_id)
743{
744 global $forum_db;
745
746 ($hook = get_hook('fn_sync_topic_start')) ? eval($hook) : null;
747
748 // Count number of replies in the topic
749 $query = array(
750 'SELECT' => 'COUNT(p.id)',
751 'FROM' => 'posts AS p',
752 'WHERE' => 'p.topic_id='.$topic_id
753 );
754
755 ($hook = get_hook('fn_qr_get_topic_reply_count')) ? eval($hook) : null;
756 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
757 $num_replies = $forum_db->result($result, 0) - 1;
758
759 // Get last_post, last_post_id and last_poster
760 $query = array(
761 'SELECT' => 'p.posted, p.id, p.poster',
762 'FROM' => 'posts AS p',
763 'WHERE' => 'p.topic_id='.$topic_id,
764 'ORDER BY' => 'p.id DESC',
765 'LIMIT' => '1'
766 );
767
768 ($hook = get_hook('fn_qr_get_topic_last_post_data')) ? eval($hook) : null;
769 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
770 list($last_post, $last_post_id, $last_poster) = $forum_db->fetch_row($result);
771
772 // Now update the topic
773 $query = array(
774 'UPDATE' => 'topics',
775 'SET' => 'num_replies='.$num_replies.', last_post='.$last_post.', last_post_id='.$last_post_id.', last_poster=\''.$forum_db->escape($last_poster).'\'',
776 'WHERE' => 'id='.$topic_id
777 );
778
779 ($hook = get_hook('fn_qr_update_topic')) ? eval($hook) : null;
780 $forum_db->query_build($query) or error(__FILE__, __LINE__);
781}
782
783
784//
785// Verifies that the provided username is OK for insertion into the database
786//
787function validate_username($username, $exclude_id = null)
788{
789 global $lang_common, $lang_register, $lang_profile, $forum_config;
790
791 $errors = array();
792
793 ($hook = get_hook('fn_validate_username_start')) ? eval($hook) : null;
794
795 // Convert multiple whitespace characters into one (to prevent people from registering with indistinguishable usernames)
796 $username = preg_replace('#\s+#s', ' ', $username);
797
798 // Validate username
799 if (forum_strlen($username) < 2)
800 $errors[] = $lang_profile['Username too short'];
801 else if (forum_strlen($username) > 25)
802 $errors[] = $lang_profile['Username too long'];
803 else if (strtolower($username) == 'guest' || strtolower($username) == strtolower($lang_common['Guest']))
804 $errors[] = $lang_profile['Username guest'];
805 else if (preg_match('/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $username))
806 $errors[] = $lang_profile['Username IP'];
807 else if ((strpos($username, '[') !== false || strpos($username, ']') !== false) && strpos($username, '\'') !== false && strpos($username, '"') !== false)
808 $errors[] = $lang_profile['Username reserved chars'];
809 else if (preg_match('#\[b\]|\[/b\]|\[u\]|\[/u\]|\[i\]|\[/i\]|\[color|\[/color\]|\[quote\]|\[quote=|\[/quote\]|\[code\]|\[/code\]|\[img\]|\[/img\]|\[url|\[/url\]|\[email|\[/email\]#i', $username))
810 $errors[] = $lang_profile['Username BBCode'];
811
812 // Check username for any censored words
813 if ($forum_config['o_censoring'] == '1' && censor_words($username) != $username)
814 $errors[] = $lang_profile['Username censor'];
815
816 // Check for username dupe
817 $dupe = check_username_dupe($username, $exclude_id);
818 if ($dupe !== false)
819 $errors[] = sprintf($lang_profile['Username dupe'], forum_htmlencode($dupe));
820
821 return $errors;
822}
823
824
825//
826// Adds a new user. The username must be passed through validate_username() first.
827//
828function add_user($user_info, &$new_uid)
829{
830 global $forum_db, $base_url, $lang_common, $forum_config, $forum_user, $forum_url;
831
832 ($hook = get_hook('fn_add_user_start')) ? eval($hook) : null;
833
834 // Add the user
835 $query = array(
836 'INSERT' => 'username, group_id, password, email, email_setting, save_pass, timezone, dst, language, style, registered, registration_ip, last_visit, salt, activate_key',
837 'INTO' => 'users',
838 'VALUES' => '\''.$forum_db->escape($user_info['username']).'\', '.$user_info['group_id'].', \''.$forum_db->escape($user_info['password_hash']).'\', \''.$forum_db->escape($user_info['email']).'\', '.$user_info['email_setting'].', '.$user_info['save_pass'].', '.floatval($user_info['timezone']).', '.$user_info['dst'].', \''.$forum_db->escape($user_info['language']).'\', \''.$forum_db->escape($user_info['style']).'\', '.$user_info['registered'].', \''.$forum_db->escape($user_info['registration_ip']).'\', '.$user_info['registered'].', \''.$forum_db->escape($user_info['salt']).'\', '.$user_info['activate_key'].''
839 );
840
841 ($hook = get_hook('fn_qr_add_user')) ? eval($hook) : null;
842 $forum_db->query_build($query) or error(__FILE__, __LINE__);
843 $new_uid = $forum_db->insert_id();
844
845 // Must the user verify the registration?
846 if ($user_info['require_verification'])
847 {
848 // Load the "welcome" template
849 $mail_tpl = trim(file_get_contents(FORUM_ROOT.'lang/'.$forum_user['language'].'/mail_templates/welcome.tpl'));
850
851 // The first row contains the subject
852 $first_crlf = strpos($mail_tpl, "\n");
853 $mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));
854 $mail_message = trim(substr($mail_tpl, $first_crlf));
855
856 $mail_subject = str_replace('<board_title>', $forum_config['o_board_title'], $mail_subject);
857 $mail_message = str_replace('<base_url>', $base_url.'/', $mail_message);
858 $mail_message = str_replace('<username>', $user_info['username'], $mail_message);
859 $mail_message = str_replace('<activation_url>', str_replace('&', '&', forum_link($forum_url['change_password_key'], array($new_uid, substr($user_info['activate_key'], 1, -1)))), $mail_message);
860 $mail_message = str_replace('<board_mailer>', sprintf($lang_common['Forum mailer'], $forum_config['o_board_title']), $mail_message);
861
862 ($hook = get_hook('fn_add_user_send_verification')) ? eval($hook) : null;
863
864 forum_mail($user_info['email'], $mail_subject, $mail_message);
865 }
866
867 // Should we alert people on the admin mailing list that a new user has registered?
868 if ($user_info['notify_admins'] && $forum_config['o_mailing_list'] != '')
869 {
870 $mail_subject = 'Alert - New registration';
871 $mail_message = 'User \''.$user_info['username'].'\' registered in the forums at '.$base_url.'/'."\n\n".'User profile: '.forum_link($forum_url['user'], $new_uid)."\n\n".'-- '."\n".'Forum Mailer'."\n".'(Do not reply to this message)';
872
873 forum_mail($forum_config['o_mailing_list'], $mail_subject, $mail_message);
874 }
875
876 ($hook = get_hook('fn_add_user_end')) ? eval($hook) : null;
877}
878
879
880//
881// Delete a user and all information associated with it
882//
883function delete_user($user_id)
884{
885 global $forum_db, $db_type, $forum_config;
886
887 ($hook = get_hook('fn_delete_user_start')) ? eval($hook) : null;
888
889 // First we need to get some data on the user
890 $query = array(
891 'SELECT' => 'u.username, u.group_id, g.g_moderator',
892 'FROM' => 'users AS u',
893 'JOINS' => array(
894 array(
895 'INNER JOIN' => 'groups AS g',
896 'ON' => 'g.g_id=u.group_id'
897 )
898 ),
899 'WHERE' => 'u.id='.$user_id
900 );
901
902 ($hook = get_hook('fn_qr_get_user_data')) ? eval($hook) : null;
903 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
904 $user = $forum_db->fetch_assoc($result);
905
906 // Delete any subscriptions
907 $query = array(
908 'DELETE' => 'subscriptions',
909 'WHERE' => 'user_id='.$user_id
910 );
911
912 ($hook = get_hook('fn_qr_delete_subscriptions')) ? eval($hook) : null;
913 $forum_db->query_build($query) or error(__FILE__, __LINE__);
914
915 // Remove him/her from the online list (if they happen to be logged in)
916 $query = array(
917 'DELETE' => 'online',
918 'WHERE' => 'user_id='.$user_id
919 );
920
921 ($hook = get_hook('fn_qr_delete_user_delete_online')) ? eval($hook) : null;
922 $forum_db->query_build($query) or error(__FILE__, __LINE__);
923
924 // Should we delete all posts made by this user?
925 if (isset($_POST['delete_posts']))
926 {
927 @set_time_limit(0);
928
929 // Find all posts made by this user
930 $query = array(
931 'SELECT' => 'p.id, p.topic_id, t.forum_id, t.first_post_id',
932 'FROM' => 'posts AS p',
933 'JOINS' => array(
934 array(
935 'INNER JOIN' => 'topics AS t',
936 'ON' => 't.id=p.topic_id'
937 )
938 ),
939 'WHERE' => 'p.poster_id='.$user_id
940 );
941
942 ($hook = get_hook('fn_qr_get_user_posts')) ? eval($hook) : null;
943 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
944 while ($cur_post = $forum_db->fetch_assoc($result))
945 {
946 if ($cur_post['first_post_id'] == $cur_post['id'])
947 delete_topic($cur_post['topic_id'], $cur_post['forum_id']);
948 else
949 delete_post($cur_post['id'], $cur_post['topic_id'], $cur_post['forum_id']);
950 }
951 }
952 else
953 {
954 // Set all his/her posts to guest
955 $query = array(
956 'UPDATE' => 'posts',
957 'SET' => 'poster_id=1',
958 'WHERE' => 'poster_id='.$user_id
959 );
960
961 ($hook = get_hook('fn_qr_reset_user_posts')) ? eval($hook) : null;
962 $forum_db->query_build($query) or error(__FILE__, __LINE__);
963 }
964
965 // Delete the user
966 $query = array(
967 'DELETE' => 'users',
968 'WHERE' => 'id='.$user_id
969 );
970
971 ($hook = get_hook('fn_qr_delete_user')) ? eval($hook) : null;
972 $forum_db->query_build($query) or error(__FILE__, __LINE__);
973
974 // Delete user avatar
975 if (file_exists($forum_config['o_avatars_dir'].'/'.$user_id.'.gif'))
976 @unlink($forum_config['o_avatars_dir'].'/'.$user_id.'.gif');
977 if (file_exists($forum_config['o_avatars_dir'].'/'.$user_id.'.jpg'))
978 @unlink($forum_config['o_avatars_dir'].'/'.$user_id.'.jpg');
979 if (file_exists($forum_config['o_avatars_dir'].'/'.$user_id.'.png'))
980 @unlink($forum_config['o_avatars_dir'].'/'.$user_id.'.png');
981
982 // If the user is a moderator or an administrator, we remove him/her from the moderator list in all forums
983 // and regenerate the bans cache (in case he/she created any bans)
984 if ($user['group_id'] == FORUM_ADMIN || $user['g_moderator'] == '1')
985 {
986 clean_forum_moderators();
987
988 // Regenerate the bans cache
989 require_once FORUM_ROOT.'include/cache.php';
990 generate_bans_cache();
991 }
992
993 ($hook = get_hook('fn_delete_user_end')) ? eval($hook) : null;
994}
995
996
997//
998// Iterates through all forum moderator lists and removes any erroneous entries
999//
1000function clean_forum_moderators()
1001{
1002 global $forum_db;
1003
1004 ($hook = get_hook('fn_clean_forum_moderators_start')) ? eval($hook) : null;
1005
1006 // Get a list of forums and their respective lists of moderators
1007 $query = array(
1008 'SELECT' => 'f.id, f.moderators',
1009 'FROM' => 'forums AS f',
1010 'WHERE' => 'f.moderators IS NOT NULL'
1011 );
1012
1013 ($hook = get_hook('fn_qr_get_forum_moderators')) ? eval($hook) : null;
1014 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
1015
1016 while ($cur_forum = $forum_db->fetch_assoc($result))
1017 {
1018 $cur_moderators = unserialize($cur_forum['moderators']);
1019 $new_moderators = $cur_moderators;
1020
1021 // Iterate through each user in the list and check if he/she is in a moderator or admin group
1022 foreach ($cur_moderators as $username => $user_id)
1023 {
1024 $query = array(
1025 'SELECT' => '1',
1026 'FROM' => 'users AS u',
1027 'JOINS' => array(
1028 array(
1029 'INNER JOIN' => 'groups AS g',
1030 'ON' => 'g.g_id=u.group_id'
1031 )
1032 ),
1033 'WHERE' => '(g.g_moderator=1 OR u.group_id=1) AND u.id='.$user_id
1034 );
1035
1036 ($hook = get_hook('fn_qr_check_user_in_moderator_group')) ? eval($hook) : null;
1037 $result2 = $forum_db->query_build($query) or error(__FILE__, __LINE__);
1038
1039 if (!$forum_db->num_rows($result2)) // If the user isn't in a moderator or admin group, remove him/her from the list
1040 unset($new_moderators[$username]);
1041 }
1042
1043 // If we changed anything, update the forum
1044 if ($cur_moderators != $new_moderators)
1045 {
1046 $new_moderators = (!empty($new_moderators)) ? '\''.$forum_db->escape(serialize($new_moderators)).'\'' : 'NULL';
1047
1048 $query = array(
1049 'UPDATE' => 'forums',
1050 'SET' => 'moderators='.$new_moderators,
1051 'WHERE' => 'id='.$cur_forum['id']
1052 );
1053
1054 ($hook = get_hook('fn_qr_set_forum_moderators')) ? eval($hook) : null;
1055 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1056 }
1057 }
1058
1059 ($hook = get_hook('fn_clean_forum_moderators_end')) ? eval($hook) : null;
1060}
1061
1062
1063//
1064// Locate and delete any orphaned redirect topics
1065//
1066function delete_orphans()
1067{
1068 global $forum_db;
1069
1070 ($hook = get_hook('fn_delete_orphans_start')) ? eval($hook) : null;
1071
1072 // Locate any orphaned redirect topics
1073 $query = array(
1074 'SELECT' => 't1.id',
1075 'FROM' => 'topics AS t1',
1076 'JOINS' => array(
1077 array(
1078 'LEFT JOIN' => 'topics AS t2',
1079 'ON' => 't1.moved_to=t2.id'
1080 )
1081 ),
1082 'WHERE' => 't2.id IS NULL AND t1.moved_to IS NOT NULL'
1083 );
1084
1085 ($hook = get_hook('fn_qr_get_orphans')) ? eval($hook) : null;
1086 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
1087 $num_orphans = $forum_db->num_rows($result);
1088
1089 if ($num_orphans)
1090 {
1091 for ($i = 0; $i < $num_orphans; ++$i)
1092 $orphans[] = $forum_db->result($result, $i);
1093
1094 // Delete the orphan
1095 $query = array(
1096 'DELETE' => 'topics',
1097 'WHERE' => 'id IN('.implode(',', $orphans).')'
1098 );
1099
1100 ($hook = get_hook('fn_qr_delete_orphan')) ? eval($hook) : null;
1101 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1102 }
1103}
1104
1105
1106//
1107// Delete a topic and all of it's posts
1108//
1109function delete_topic($topic_id, $forum_id)
1110{
1111 global $forum_db, $db_type;
1112
1113 ($hook = get_hook('fn_delete_topic_start')) ? eval($hook) : null;
1114
1115 // Delete the topic and any redirect topics
1116 $query = array(
1117 'DELETE' => 'topics',
1118 'WHERE' => 'id='.$topic_id.' OR moved_to='.$topic_id
1119 );
1120
1121 ($hook = get_hook('fn_qr_delete_topic')) ? eval($hook) : null;
1122 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1123
1124 // Create a list of the post ID's in this topic
1125 $post_ids = '';
1126 $query = array(
1127 'SELECT' => 'p.id',
1128 'FROM' => 'posts AS p',
1129 'WHERE' => 'p.topic_id='.$topic_id
1130 );
1131
1132 ($hook = get_hook('fn_qr_get_posts_to_delete')) ? eval($hook) : null;
1133 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
1134 while ($row = $forum_db->fetch_row($result))
1135 $post_ids .= ($post_ids != '') ? ','.$row[0] : $row[0];
1136
1137 // Make sure we have a list of post ID's
1138 if ($post_ids != '')
1139 {
1140 // Delete posts in topic
1141 $query = array(
1142 'DELETE' => 'posts',
1143 'WHERE' => 'topic_id='.$topic_id
1144 );
1145
1146 ($hook = get_hook('fn_qr_delete_topic_posts')) ? eval($hook) : null;
1147 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1148
1149 require FORUM_ROOT.'include/search_idx.php';
1150 strip_search_index($post_ids);
1151 }
1152
1153 // Delete any subscriptions for this topic
1154 $query = array(
1155 'DELETE' => 'subscriptions',
1156 'WHERE' => 'topic_id='.$topic_id
1157 );
1158
1159 ($hook = get_hook('fn_qr_delete_topic_subscriptions')) ? eval($hook) : null;
1160 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1161
1162 sync_forum($forum_id);
1163
1164 ($hook = get_hook('fn_delete_topic_end')) ? eval($hook) : null;
1165}
1166
1167
1168//
1169// Delete a single post
1170//
1171function delete_post($post_id, $topic_id, $forum_id)
1172{
1173 global $forum_db, $db_type;
1174
1175 ($hook = get_hook('fn_delete_post_start')) ? eval($hook) : null;
1176
1177 $query = array(
1178 'SELECT' => 'p.id, p.poster, p.posted',
1179 'FROM' => 'posts AS p',
1180 'WHERE' => 'p.topic_id='.$topic_id,
1181 'ORDER BY' => 'p.id DESC',
1182 'LIMIT' => '2'
1183 );
1184
1185 ($hook = get_hook('fn_qr_get_topic_lastposts_info')) ? eval($hook) : null;
1186 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
1187 list($last_id, ,) = $forum_db->fetch_row($result);
1188 list($second_last_id, $second_poster, $second_posted) = $forum_db->fetch_row($result);
1189
1190 // Delete the post
1191 $query = array(
1192 'DELETE' => 'posts',
1193 'WHERE' => 'id='.$post_id
1194 );
1195
1196 ($hook = get_hook('fn_qr_delete_post')) ? eval($hook) : null;
1197 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1198
1199 require FORUM_ROOT.'include/search_idx.php';
1200 strip_search_index($post_id);
1201
1202 // Count number of replies in the topic
1203 $query = array(
1204 'SELECT' => 'COUNT(p.id)',
1205 'FROM' => 'posts AS p',
1206 'WHERE' => 'p.topic_id='.$topic_id
1207 );
1208
1209 ($hook = get_hook('fn_qr_get_topic_reply_count2')) ? eval($hook) : null;
1210 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
1211 $num_replies = $forum_db->result($result, 0) - 1;
1212
1213 // Update the topic now that a post has been deleted
1214 $query = array(
1215 'UPDATE' => 'topics',
1216 'SET' => 'num_replies='.$num_replies,
1217 'WHERE' => 'id='.$topic_id
1218 );
1219
1220 // If we deleted the most recent post, we need to sync up last post data as wel
1221 if ($last_id == $post_id)
1222 $query['SET'] .= ', last_post='.$second_posted.', last_post_id='.$second_last_id.', last_poster=\''.$forum_db->escape($second_poster).'\'';
1223
1224 ($hook = get_hook('fn_qr_update_topic2')) ? eval($hook) : null;
1225 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1226
1227 sync_forum($forum_id);
1228
1229 ($hook = get_hook('fn_delete_post_end')) ? eval($hook) : null;
1230}
1231
1232
1233//
1234// Creates a new topic with its first post
1235//
1236function add_topic($post_info, &$new_tid, &$new_pid)
1237{
1238 global $forum_db, $db_type, $forum_config, $lang_common;
1239
1240 ($hook = get_hook('fn_add_topic_start')) ? eval($hook) : null;
1241
1242 // Add the topic
1243 $query = array(
1244 'INSERT' => 'poster, subject, posted, last_post, last_poster, forum_id',
1245 'INTO' => 'topics',
1246 'VALUES' => '\''.$forum_db->escape($post_info['poster']).'\', \''.$forum_db->escape($post_info['subject']).'\', '.$post_info['posted'].', '.$post_info['posted'].', \''.$forum_db->escape($post_info['poster']).'\', '.$post_info['forum_id']
1247 );
1248
1249 ($hook = get_hook('fn_qr_add_topic')) ? eval($hook) : null;
1250 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1251 $new_tid = $forum_db->insert_id();
1252
1253 // To subscribe or not to subscribe, that ...
1254 if (!$post_info['is_guest'] && $post_info['subscribe'])
1255 {
1256 $query = array(
1257 'INSERT' => 'user_id, topic_id',
1258 'INTO' => 'subscriptions',
1259 'VALUES' => $post_info['poster_id'].' ,'.$new_tid
1260 );
1261
1262 ($hook = get_hook('fn_qr_add_subscription')) ? eval($hook) : null;
1263 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1264 }
1265
1266 // Create the post ("topic post")
1267 $query = array(
1268 'INSERT' => 'poster, poster_id, poster_ip, message, hide_smilies, posted, topic_id',
1269 'INTO' => 'posts',
1270 'VALUES' => '\''.$forum_db->escape($post_info['poster']).'\', '.$post_info['poster_id'].', \''.get_remote_address().'\', \''.$forum_db->escape($post_info['message']).'\', '.$post_info['hide_smilies'].', '.$post_info['posted'].', '.$new_tid
1271 );
1272
1273 // If it's a guest post, there might be an e-mail address we need to include
1274 if ($post_info['is_guest'] && $post_info['poster_email'] != null)
1275 {
1276 $query['INSERT'] .= ', poster_email';
1277 $query['VALUES'] .= ', \''.$post_info['poster_email'].'\'';
1278 }
1279
1280 ($hook = get_hook('fn_qr_add_topic_post')) ? eval($hook) : null;
1281 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1282 $new_pid = $forum_db->insert_id();
1283
1284 // Update the topic with last_post_id and first_post_id
1285 $query = array(
1286 'UPDATE' => 'topics',
1287 'SET' => 'last_post_id='.$new_pid.', first_post_id='.$new_pid,
1288 'WHERE' => 'id='.$new_tid
1289 );
1290
1291 ($hook = get_hook('fn_qr_update_topic3')) ? eval($hook) : null;
1292 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1293
1294 require FORUM_ROOT.'include/search_idx.php';
1295 update_search_index('post', $new_pid, $post_info['message'], $post_info['subject']);
1296
1297
1298 sync_forum($post_info['forum_id']);
1299
1300 ($hook = get_hook('fn_add_topic_end')) ? eval($hook) : null;
1301}
1302
1303
1304//
1305// Creates a new post
1306//
1307function add_post($post_info, &$new_pid)
1308{
1309 global $forum_db, $db_type, $forum_config, $lang_common;
1310
1311 ($hook = get_hook('fn_add_post_start')) ? eval($hook) : null;
1312
1313 // Add the post
1314 $query = array(
1315 'INSERT' => 'poster, poster_id, poster_ip, message, hide_smilies, posted, topic_id',
1316 'INTO' => 'posts',
1317 'VALUES' => '\''.$forum_db->escape($post_info['poster']).'\', '.$post_info['poster_id'].', \''.get_remote_address().'\', \''.$forum_db->escape($post_info['message']).'\', '.$post_info['hide_smilies'].', '.$post_info['posted'].', '.$post_info['topic_id']
1318 );
1319
1320 // If it's a guest post, there might be an e-mail address we need to include
1321 if ($post_info['is_guest'] && $post_info['poster_email'] != null)
1322 {
1323 $query['INSERT'] .= ', poster_email';
1324 $query['VALUES'] .= ', \''.$post_info['poster_email'].'\'';
1325 }
1326
1327 ($hook = get_hook('fn_qr_add_post')) ? eval($hook) : null;
1328 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1329 $new_pid = $forum_db->insert_id();
1330
1331 if (!$post_info['is_guest'])
1332 {
1333 // Subscribe or unsubscribe?
1334 if ($post_info['subscr_action'] == 1)
1335 {
1336 $query = array(
1337 'INSERT' => 'user_id, topic_id',
1338 'INTO' => 'subscriptions',
1339 'VALUES' => $post_info['poster_id'].' ,'.$post_info['topic_id']
1340 );
1341
1342 ($hook = get_hook('fn_qr_add_subscription2')) ? eval($hook) : null;
1343 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1344 }
1345 else if ($post_info['subscr_action'] == 2)
1346 {
1347 $query = array(
1348 'DELETE' => 'subscriptions',
1349 'WHERE' => 'topic_id='.$post_info['topic_id'].' AND user_id='.$post_info['poster_id']
1350 );
1351
1352 ($hook = get_hook('fn_qr_delete_subscription')) ? eval($hook) : null;
1353 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1354 }
1355 }
1356
1357 // Count number of replies in the topic
1358 $query = array(
1359 'SELECT' => 'COUNT(p.id)',
1360 'FROM' => 'posts AS p',
1361 'WHERE' => 'p.topic_id='.$post_info['topic_id']
1362 );
1363
1364 ($hook = get_hook('fn_qr_get_topic_reply_count3')) ? eval($hook) : null;
1365 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
1366 $num_replies = $forum_db->result($result, 0) - 1;
1367
1368 // Update topic
1369 $query = array(
1370 'UPDATE' => 'topics',
1371 'SET' => 'num_replies='.$num_replies.', last_post='.$post_info['posted'].', last_post_id='.$new_pid.', last_poster=\''.$forum_db->escape($post_info['poster']).'\'',
1372 'WHERE' => 'id='.$post_info['topic_id']
1373 );
1374
1375 ($hook = get_hook('fn_qr_update_topic4')) ? eval($hook) : null;
1376 $forum_db->query_build($query) or error(__FILE__, __LINE__);
1377
1378 sync_forum($post_info['forum_id']);
1379
1380 require FORUM_ROOT.'include/search_idx.php';
1381 update_search_index('post', $new_pid, $post_info['message']);
1382
1383 send_subscriptions($post_info, $new_pid);
1384
1385 ($hook = get_hook('fn_add_post_end')) ? eval($hook) : null;
1386}
1387
1388
1389//
1390// Send out subscription emails
1391//
1392function send_subscriptions($post_info, $new_pid)
1393{
1394 global $forum_config, $forum_db, $forum_url, $lang_common;
1395
1396 ($hook = get_hook('fn_send_subscriptions_start')) ? eval($hook) : null;
1397
1398 if ($forum_config['o_subscriptions'] != '1')
1399 return;
1400
1401 // Get the post time for the previous post in this topic
1402 $query = array(
1403 'SELECT' => 'p.posted',
1404 'FROM' => 'posts AS p',
1405 'WHERE' => 'p.topic_id='.$post_info['topic_id'],
1406 'ORDER BY' => 'p.id DESC',
1407 'LIMIT' => '1, 1'
1408 );
1409
1410 ($hook = get_hook('fn_qr_get_previous_post_time')) ? eval($hook) : null;
1411 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
1412 $previous_post_time = $forum_db->result($result);
1413
1414 // Get any subscribed users that should be notified (banned users are excluded)
1415 $query = array(
1416 'SELECT' => 'u.id, u.email, u.notify_with_post, u.language',
1417 'FROM' => 'users AS u',
1418 'JOINS' => array(
1419 array(
1420 'INNER JOIN' => 'subscriptions AS s',
1421 'ON' => 'u.id=s.user_id'
1422 ),
1423 array(
1424 'LEFT JOIN' => 'forum_perms AS fp',
1425 'ON' => '(fp.forum_id='.$post_info['forum_id'].' AND fp.group_id=u.group_id)'
1426 ),
1427 array(
1428 'LEFT JOIN' => 'online AS o',
1429 'ON' => 'u.id=o.user_id'
1430 ),
1431 array(
1432 'LEFT JOIN' => 'bans AS b',
1433 'ON' => 'u.username=b.username'
1434 ),
1435 ),
1436 'WHERE' => 'b.username IS NULL AND COALESCE(o.logged, u.last_visit)>'.$previous_post_time.' AND (fp.read_forum IS NULL OR fp.read_forum=1) AND s.topic_id='.$post_info['topic_id'].' AND u.id!='.$post_info['poster_id']
1437 );
1438
1439 ($hook = get_hook('fn_qr_get_users_to_notify')) ? eval($hook) : null;
1440 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
1441
1442 if ($forum_db->num_rows($result))
1443 {
1444 require_once FORUM_ROOT.'include/email.php';
1445
1446 $notification_emails = array();
1447
1448 // Loop through subscribed users and send e-mails
1449 while ($cur_subscriber = $forum_db->fetch_assoc($result))
1450 {
1451 // Is the subscription e-mail for $cur_subscriber['language'] cached or not?
1452 if (!isset($notification_emails[$cur_subscriber['language']]))
1453 {
1454 if (file_exists(FORUM_ROOT.'lang/'.$cur_subscriber['language'].'/mail_templates/new_reply.tpl'))
1455 {
1456 // Load the "new reply" template
1457 $mail_tpl = trim(file_get_contents(FORUM_ROOT.'lang/'.$cur_subscriber['language'].'/mail_templates/new_reply.tpl'));
1458
1459 // Load the "new reply full" template (with post included)
1460 $mail_tpl_full = trim(file_get_contents(FORUM_ROOT.'lang/'.$cur_subscriber['language'].'/mail_templates/new_reply_full.tpl'));
1461
1462 // The first row contains the subject (it also starts with "Subject:")
1463 $first_crlf = strpos($mail_tpl, "\n");
1464 $mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));
1465 $mail_message = trim(substr($mail_tpl, $first_crlf));
1466
1467 $first_crlf = strpos($mail_tpl_full, "\n");
1468 $mail_subject_full = trim(substr($mail_tpl_full, 8, $first_crlf-8));
1469 $mail_message_full = trim(substr($mail_tpl_full, $first_crlf));
1470
1471 $mail_subject = str_replace('<topic_subject>', '\''.$post_info['subject'].'\'', $mail_subject);
1472 $mail_message = str_replace('<topic_subject>', '\''.$post_info['subject'].'\'', $mail_message);
1473 $mail_message = str_replace('<replier>', $post_info['poster'], $mail_message);
1474 $mail_message = str_replace('<post_url>', forum_link($forum_url['post'], $new_pid), $mail_message);
1475 $mail_message = str_replace('<unsubscribe_url>', forum_link($forum_url['unsubscribe'], array($post_info['topic_id'], generate_form_token('unsubscribe'.$post_info['topic_id'].$cur_subscriber['id']))), $mail_message);
1476 $mail_message = str_replace('<board_mailer>', sprintf($lang_common['Forum mailer'], $forum_config['o_board_title']), $mail_message);
1477
1478 $mail_subject_full = str_replace('<topic_subject>', '\''.$post_info['subject'].'\'', $mail_subject_full);
1479 $mail_message_full = str_replace('<topic_subject>', '\''.$post_info['subject'].'\'', $mail_message_full);
1480 $mail_message_full = str_replace('<replier>', $post_info['poster'], $mail_message_full);
1481 $mail_message_full = str_replace('<message>', $post_info['message'], $mail_message_full);
1482 $mail_message_full = str_replace('<post_url>', forum_link($forum_url['post'], $new_pid), $mail_message_full);
1483 $mail_message_full = str_replace('<unsubscribe_url>', forum_link($forum_url['unsubscribe'], array($post_info['topic_id'], generate_form_token('unsubscribe'.$post_info['topic_id'].$cur_subscriber['id']))), $mail_message_full);
1484 $mail_message_full = str_replace('<board_mailer>', sprintf($lang_common['Forum mailer'], $forum_config['o_board_title']), $mail_message_full);
1485
1486 $notification_emails[$cur_subscriber['language']][0] = $mail_subject;
1487 $notification_emails[$cur_subscriber['language']][1] = $mail_message;
1488 $notification_emails[$cur_subscriber['language']][2] = $mail_subject_full;
1489 $notification_emails[$cur_subscriber['language']][3] = $mail_message_full;
1490
1491 $mail_subject = $mail_message = $mail_subject_full = $mail_message_full = null;
1492 }
1493 }
1494
1495 // We have to double check here because the templates could be missing
1496 if (isset($notification_emails[$cur_subscriber['language']]))
1497 {
1498 // Make sure the e-mail address format is valid before sending
1499 if (is_valid_email($cur_subscriber['email']))
1500 {
1501 if ($cur_subscriber['notify_with_post'] == '0')
1502 forum_mail($cur_subscriber['email'], $notification_emails[$cur_subscriber['language']][0], $notification_emails[$cur_subscriber['language']][1]);
1503 else
1504 forum_mail($cur_subscriber['email'], $notification_emails[$cur_subscriber['language']][2], $notification_emails[$cur_subscriber['language']][3]);
1505 }
1506 }
1507 }
1508 }
1509
1510 ($hook = get_hook('fn_send_subscriptions_end')) ? eval($hook) : null;
1511}
1512
1513
1514//
1515// Make a string safe to use in a URL
1516//
1517function sef_friendly($str)
1518{
1519 ($hook = get_hook('fn_sef_friendly_start')) ? eval($hook) : null;
1520
1521 $str = strtolower(utf8_decode($str));
1522 $str = strtr($str,
1523 "\xc0\xc1\xc2\xc3\xc4\xc5\xe0\xe1\xe2\xe3\xe4\xe5\xd2\xd3\xd4\xd5\xd6\xd8\xf2\xf3\xf4\xf5\xf6\xf8\xc8\xc9\xca\xcb\xe8\xe9\xea\xeb\xc7\xe7\xcc\xcd\xce\xcf\xec\xed\xee\xef\xd9\xda\xdb\xdc\xf9\xfa\xfb\xfc\xff\xd1\xf1",
1524 'aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn'
1525 );
1526 $str = preg_replace(array('/[^a-z0-9\s]/', '/[\s]+/'), array('', '-'), $str);
1527
1528 return $str != '-' ? trim($str, '-') : '';
1529}
1530
1531
1532//
1533// Replace censored words in $text
1534//
1535function censor_words($text)
1536{
1537 global $forum_db;
1538 static $search_for, $replace_with;
1539
1540 ($hook = get_hook('fn_censor_words_start')) ? eval($hook) : null;
1541
1542 // If not already loaded in a previous call, load the cached censors
1543 if (!defined('FORUM_CENSORS_LOADED'))
1544 {
1545 if (file_exists(FORUM_CACHE_DIR.'cache_censors.php'))
1546 include FORUM_CACHE_DIR.'cache_censors.php';
1547
1548 if (!defined('FORUM_CENSORS_LOADED'))
1549 {
1550 require_once FORUM_ROOT.'include/cache.php';
1551 generate_censors_cache();
1552 require FORUM_CACHE_DIR.'cache_censors.php';
1553 }
1554
1555 $search_for = array();
1556 $replace_with = array();
1557
1558 foreach ($forum_censors as $censor_key => $cur_word)
1559 {
1560 $search_for[$censor_key] = '/\b('.str_replace('\*', '\w*?', preg_quote($cur_word['search_for'], '/')).')\b/iu';
1561 $replace_with[$censor_key] = $cur_word['replace_with'];
1562
1563 ($hook = get_hook('fn_censor_words_setup_regex')) ? eval($hook) : null;
1564 }
1565 }
1566
1567 if (!empty($search_for))
1568 $text = substr(preg_replace($search_for, $replace_with, ' '.$text.' '), 1, -1);
1569
1570 return $text;
1571}
1572
1573
1574//
1575// Check if a username is occupied
1576//
1577function check_username_dupe($username, $exclude_id = null)
1578{
1579 global $forum_db;
1580
1581 ($hook = get_hook('fn_check_username_dupe_start')) ? eval($hook) : null;
1582
1583 $query = array(
1584 'SELECT' => 'u.username',
1585 'FROM' => 'users AS u',
1586 'WHERE' => '(UPPER(username)=UPPER(\''.$forum_db->escape($username).'\') OR UPPER(username)=UPPER(\''.$forum_db->escape(preg_replace('/[^\w]/u', '', $username)).'\')) AND id>1'
1587 );
1588
1589 if ($exclude_id)
1590 $query['WHERE'] .= ' AND id!='.$exclude_id;
1591
1592 ($hook = get_hook('fn_qr_check_username_dupe')) ? eval($hook) : null;
1593 $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
1594
1595 return $forum_db->num_rows($result) ? $forum_db->result($result) : false;
1596}
1597
1598
1599//
1600// Determines the correct title for $user
1601// $user must contain the elements 'username', 'title', 'posts', 'g_id' and 'g_user_title'
1602//
1603function get_title($user)
1604{
1605 global $forum_db, $forum_config, $forum_bans, $lang_common;
1606 static $ban_list, $forum_ranks;
1607
1608 ($hook = get_hook('fn_get_title_start')) ? eval($hook) : null;
1609
1610 // If not already built in a previous call, build an array of lowercase banned usernames
1611 if (empty($ban_list))
1612 {
1613 $ban_list = array();
1614
1615 foreach ($forum_bans as $cur_ban)
1616 $ban_list[] = strtolower($cur_ban['username']);
1617 }
1618
1619 // If not already loaded in a previous call, load the cached ranks
1620 if ($forum_config['o_ranks'] == '1' && !defined('FORUM_RANKS_LOADED'))
1621 {
1622 if (file_exists(FORUM_CACHE_DIR.'cache_ranks.php'))
1623 include FORUM_CACHE_DIR.'cache_ranks.php';
1624
1625 if (!defined('FORUM_RANKS_LOADED'))
1626 {
1627 require_once FORUM_ROOT.'include/cache.php';
1628 generate_ranks_cache();
1629 require FORUM_CACHE_DIR.'cache_ranks.php';
1630 }
1631 }
1632
1633 // If the user has a custom title
1634 if ($user['title'] != '')
1635 $user_title = forum_htmlencode($forum_config['o_censoring'] == '1' ? censor_words($user['title']) : $user['title']);
1636 // If the user is banned
1637 else if (in_array(strtolower($user['username']), $ban_list))
1638 $user_title = $lang_common['Banned'];
1639 // If the user group has a default user title
1640 else if ($user['g_user_title'] != '')
1641 $user_title = forum_htmlencode($user['g_user_title']);
1642 // If the user is a guest
1643 else if ($user['g_id'] == FORUM_GUEST)
1644 $user_title = $lang_common['Guest'];
1645 else
1646 {
1647 // Are there any ranks?
1648 if ($forum_config['o_ranks'] == '1' && !empty($forum_ranks))
1649 {
1650 @reset($forum_ranks);
1651 while (list(, $cur_rank) = @each($forum_ranks))
1652 {
1653 if (intval($user['num_posts']) >= $cur_rank['min_posts'])
1654 $user_title = forum_htmlencode($cur_rank['rank']);
1655 }
1656 }
1657
1658 // If the user didn't "reach" any rank (or if ranks are disabled), we assign the default
1659 if (!isset($user_title))
1660 $user_title = $lang_common['Member'];
1661 }
1662
1663 ($hook = get_hook('fn_get_title_end')) ? eval($hook) : null;
1664
1665 return $user_title;
1666}
1667
1668
1669//
1670// Generate a string with numbered links (for multipage scripts)
1671//
1672function paginate($num_pages, $cur_page, $link, $separator, $args = null)
1673{
1674 global $forum_url, $lang_common;
1675
1676 $pages = array();
1677 $link_to_all = false;
1678
1679 ($hook = get_hook('fn_paginate_start')) ? eval($hook) : null;
1680
1681 // If $cur_page == -1, we link to all pages (used in viewforum.php)
1682 if ($cur_page == -1)
1683 {
1684 $cur_page = 1;
1685 $link_to_all = true;
1686 }
1687
1688 if ($num_pages <= 1)
1689 $pages = array('<li class="Current">1</li>');
1690 else
1691 {
1692 // Add a previous page link
1693 if ($num_pages > 1 && $cur_page > 1)
1694 $pages[] = '<li><a'.(empty($pages) ? ' class="item1"' : '').' href="'.forum_sublink($link, $forum_url['page'], ($cur_page - 1), $args).'">'.$lang_common['Previous'].'</a></li>';
1695
1696 if ($cur_page > 3)
1697 {
1698 $pages[] = '<li><a'.(empty($pages) ? ' class="item1"' : '').' href="'.forum_sublink($link, $forum_url['page'], 1, $args).'">1</a></li>';
1699
1700 if ($cur_page > 5)
1701 $pages[] = '<li class="Dot">…</li>';
1702 }
1703
1704 // Don't ask me how the following works. It just does, OK? :-)
1705 for ($current = ($cur_page == 5) ? $cur_page - 3 : $cur_page - 2, $stop = ($cur_page + 4 == $num_pages) ? $cur_page + 4 : $cur_page + 3; $current < $stop; ++$current)
1706 {
1707 if ($current < 1 || $current > $num_pages)
1708 continue;
1709 else if ($current != $cur_page || $link_to_all)
1710 $pages[] = '<li><a'.(empty($pages) ? ' class="Current" ' : '').' href="'.forum_sublink($link, $forum_url['page'], $current, $args).'">'.$current.'</a></li>';
1711 else
1712 $pages[] = '<li'.(empty($pages) ? ' class="Current"' : '').'>'.$current.'</li>';
1713 }
1714
1715 if ($cur_page <= ($num_pages-3))
1716 {
1717 if ($cur_page != ($num_pages-3) && $cur_page != ($num_pages-4))
1718 $pages[] = '<li class="Dot">…</li>';
1719
1720 $pages[] = '<li><a'.(empty($pages) ? ' class="item1" ' : '').' href="'.forum_sublink($link, $forum_url['page'], $num_pages, $args).'">'.$num_pages.'</a></li>';
1721 }
1722
1723 // Add a next page link
1724 if ($num_pages > 1 && !$link_to_all && $cur_page < $num_pages)
1725 $pages[] = '<li><a'.(empty($pages) ? ' class="item1" ' : '').' href="'.forum_sublink($link, $forum_url['page'], ($cur_page + 1), $args).'">'.$lang_common['Next'].'</a></li>';
1726 }
1727
1728 ($hook = get_hook('fn_paginate_end')) ? eval($hook) : null;
1729
1730 return implode($separator, $pages);
1731}
1732
1733
1734//
1735// Clean version string from trailing '.0's
1736//
1737function clean_version($version)
1738{
1739 return preg_replace('/(\.0)+(?!\.)|(\.0+$)/', '$2', $version);
1740}
1741
1742
1743//
1744// Display a message
1745//
1746function message($message, $link = '')
1747{
1748 global $forum_db, $forum_url, $lang_common, $forum_config, $base_url, $forum_start, $tpl_main, $forum_user, $forum_page, $forum_updates;
1749
1750 ($hook = get_hook('fn_message_start')) ? eval($hook) : null;
1751
1752 if (!defined('FORUM_HEADER'))
1753 {
1754 // Setup breadcrumbs
1755 $forum_page['crumbs'] = array(
1756 array($forum_config['o_board_title'], forum_link($forum_url['index'])),
1757 $lang_common['Info']
1758 );
1759
1760 define('FORUM_PAGE', 'message');
1761 require FORUM_ROOT.'header.php';
1762
1763 // START SUBST - <!-- forum_main -->
1764 ob_start();
1765 }
1766
1767?>
1768 <div class="LostNoise"><?php echo $message ?><?php if ($link != '') echo ' <span>'.$link.'</span>' ?></div>
1769
1770<?php
1771
1772 $tpl_temp = trim(ob_get_contents());
1773 $tpl_main = str_replace('<!-- forum_main -->', $tpl_temp, $tpl_main);
1774 ob_end_clean();
1775 // END SUBST - <!-- forum_main -->
1776
1777 require FORUM_ROOT.'footer.php';
1778}
1779
1780
1781//
1782// Display a form that the user can use to confirm that they want to undertake an action.
1783// Used when the CSRF token from the request does not match the token stored in the database.
1784//
1785function csrf_confirm_form()
1786{
1787 global $forum_db, $forum_url, $lang_common, $forum_config, $base_url, $forum_start, $tpl_main, $forum_user, $forum_page, $forum_updates;
1788
1789 // If we've disabled the CSRF check for this page, we have nothing to do here.
1790 if (defined('FORUM_DISABLE_CSRF_CONFIRM'))
1791 return;
1792
1793 // User pressed the cancel button
1794 if (isset($_POST['confirm_cancel']))
1795 redirect(forum_htmlencode($_POST['prev_url']), $lang_common['Cancel redirect']);
1796
1797 //
1798 // A helper function for csrf_confirm_form. It takes a multi-dimensional array and returns it as a
1799 // single-dimensional array suitable for use in hidden fields.
1800 //
1801 function _csrf_confirm_form($key, $values)
1802 {
1803 $fields = array();
1804
1805 if (is_array($values))
1806 {
1807 foreach ($values as $cur_key => $cur_values)
1808 $fields = array_merge($fields, _csrf_confirm_form($key.'['.$cur_key.']', $cur_values));
1809
1810 return $fields;
1811 }
1812 else
1813 $fields[$key] = $values;
1814
1815 return $fields;
1816 }
1817
1818 ($hook = get_hook('fn_csrf_confirm_form_start')) ? eval($hook) : null;
1819
1820 // Setup breadcrumbs
1821 $forum_page['crumbs'] = array(
1822 array($forum_config['o_board_title'], forum_link($forum_url['index'])),
1823 $lang_common['Confirm action']
1824 );
1825
1826 $forum_page['form_action'] = get_current_url();
1827
1828 $forum_page['hidden_fields'] = array(
1829 '<input type="hidden" name="csrf_token" value="'.generate_form_token($forum_page['form_action']).'" />',
1830 '<input type="hidden" name="prev_url" value="'.forum_htmlencode($forum_user['prev_url']).'" />'
1831 );
1832
1833 foreach ($_POST as $submitted_key => $submitted_val)
1834 {
1835 if ($submitted_key != 'csrf_token' && $submitted_key != 'prev_url')
1836 {
1837 $hidden_fields = _csrf_confirm_form($submitted_key, $submitted_val);
1838 foreach ($hidden_fields as $field_key => $field_val)
1839 $forum_page['hidden_fields'][$field_key] = '<input type="hidden" name="'.forum_htmlencode($field_key).'" value="'.forum_htmlencode($field_val).'" />';
1840 }
1841 }
1842
1843 define('FORUM_PAGE', 'dialogue');
1844 require FORUM_ROOT.'header.php';
1845
1846 // START SUBST - <!-- forum_main -->
1847 ob_start();
1848
1849 ($hook = get_hook('fn_csrf_confirm_form_pre_header_load')) ? eval($hook) : null;
1850
1851?>
1852<div id="brd-main" class="main">
1853
1854 <h1><span><?php echo end($forum_page['crumbs']) ?></span></h1>
1855
1856 <div class="main-head">
1857 <h2><span><?php echo $lang_common['Confirm action head'] ?></span></h2>
1858 </div>
1859 <div class="main-content frm">
1860 <div class="frm-info">
1861 <p><?php echo $lang_common['CSRF token mismatch'] ?></p>
1862 </div>
1863 <form class="frm-form" method="post" accept-charset="utf-8" action="<?php echo $forum_page['form_action'] ?>">