forked from featherbb/featherbb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
1857 lines (1514 loc) · 58.7 KB
/
Copy pathfunctions.php
File metadata and controls
1857 lines (1514 loc) · 58.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Copyright (C) 2015 FeatherBB
* based on code by (C) 2008-2012 FluxBB
* and Rickard Andersson (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
//
// Return current timestamp (with microseconds) as a float
//
function get_microtime()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
//
// Cookie stuff!
//
function check_cookie()
{
global $cookie_name, $cookie_seed;
// Get Slim current session
$feather = \Slim\Slim::getInstance();
$now = time();
// Get FeatherBB cookie
$cookie_raw = $feather->getCookie($cookie_name);
// Check if cookie exists and is valid (getCookie method returns false if the data has been tampered locally so it can't decrypt the cookie);
if (isset($cookie_raw)) {
$cookie = json_decode($cookie_raw, true);
$checksum = hash_hmac('sha1', $cookie['user_id'].$cookie['expires'], $cookie_seed . '_checksum');
// If cookie has a non-guest user, hasn't expired and is legit
if ($cookie['user_id'] > 1 && $cookie['expires'] > $now && $checksum == $cookie['checksum']) {
// Get user info from db
$select_check_cookie = array('u.*', 'g.*', 'o.logged', 'o.idle');
$where_check_cookie = array('u.id' => intval($cookie['user_id']));
$result = \DB::for_table('users')
->table_alias('u')
->select_many($select_check_cookie)
->inner_join('groups', array('u.group_id', '=', 'g.g_id'), 'g')
->left_outer_join('online', array('o.user_id', '=', 'u.id'), 'o')
->where($where_check_cookie)
->find_result_set();
foreach ($result as $feather->user);
// Another security check, to prevent identity fraud by changing the user id in the cookie) (might be useless considering the strength of encryption)
if (isset($feather->user->id) && hash_hmac('sha1', $feather->user->password, $cookie_seed.'_password_hash') === $cookie['password_hash']) {
$expires = ($cookie['expires'] > $now + $feather->config['o_timeout_visit']) ? $now + 1209600 : $now + $feather->config['o_timeout_visit'];
$feather->user->is_guest = false;
$feather->user->is_admmod = $feather->user->g_id == FEATHER_ADMIN || $feather->user->g_moderator == '1';
feather_setcookie($feather->user->id, $feather->user->password, $expires);
set_preferences();
return true;
}
}
}
// If there is no cookie, or cookie is guest or expired, let's reconnect.
$expires = $now + 31536000; // The cookie expires after a year
feather_setcookie(1, feather_hash(uniqid(rand(), true)), $expires);
return set_default_user();
}
//
// Set preferences
//
function set_preferences()
{
global $db_type, $cookie_name;
// Get Slim current session
$feather = \Slim\Slim::getInstance();
$now = time();
// Set a default language if the user selected language no longer exists
if (!file_exists(FEATHER_ROOT.'lang/'.$feather->user->language)) {
$feather->user->language = $feather->config['o_default_lang'];
}
// Set a default style if the user selected style no longer exists
if (!file_exists(FEATHER_ROOT.'style/'.$feather->user->style.'.css')) {
$feather->user->style = $feather->config['o_default_style'];
}
if (!$feather->user->disp_topics) {
$feather->user->disp_topics = $feather->config['o_disp_topics_default'];
}
if (!$feather->user->disp_posts) {
$feather->user->disp_posts = $feather->config['o_disp_posts_default'];
}
// Define this if you want this visit to affect the online list and the users last visit data
if (!defined('FEATHER_QUIET_VISIT')) {
// Update the online list
if (!$feather->user->logged) {
$feather->user->logged = $now;
// With MySQL/MySQLi/SQLite, REPLACE INTO avoids a user having two rows in the online table
switch ($db_type) {
case 'mysql':
case 'mysqli':
case 'mysql_innodb':
case 'mysqli_innodb':
case 'sqlite':
case 'sqlite3':
\DB::for_table('online')->raw_execute('REPLACE INTO '.$feather->prefix.'online (user_id, ident, logged) VALUES(:user_id, :ident, :logged)', array(':user_id' => $feather->user->id, ':ident' => $feather->user->username, ':logged' => $feather->user->logged));
break;
default:
\DB::for_table('online')->raw_execute('INSERT INTO '.$feather->prefix.'online (user_id, ident, logged) SELECT :user_id, :ident, :logged WHERE NOT EXISTS (SELECT 1 FROM '.$feather->prefix.'online WHERE user_id=:user_id)', array(':user_id' => $feather->user->id, ':ident' => $feather->user->username, ':logged' => $feather->user->logged));
break;
}
// Reset tracked topics
set_tracked_topics(null);
} else {
// Special case: We've timed out, but no other user has browsed the forums since we timed out
if ($feather->user->logged < ($now-$feather->config['o_timeout_visit'])) {
\DB::for_table('users')->where('id', $feather->user->id)
->find_one()
->set('last_visit', $feather->user->logged)
->save();
$feather->user->last_visit = $feather->user->logged;
}
$idle_sql = ($feather->user->idle == '1') ? ', idle=0' : '';
\DB::for_table('online')->raw_execute('UPDATE '.$feather->prefix.'online SET logged='.$now.$idle_sql.' WHERE user_id=:user_id', array(':user_id' => $feather->user->id));
// Update tracked topics with the current expire time
$cookie_tracked_topics = $feather->getCookie($cookie_name.'_track');
if (isset($cookie_tracked_topics)) {
set_tracked_topics(json_decode($cookie_tracked_topics, true));
}
}
} else {
if (!$feather->user->logged) {
$feather->user->logged = $feather->user->last_visit;
}
}
}
//
// Try to determine the current URL
//
function get_current_url($max_length = 0)
{
$protocol = get_current_protocol();
$port = (isset($_SERVER['SERVER_PORT']) && (($_SERVER['SERVER_PORT'] != '80' && $protocol == 'http') || ($_SERVER['SERVER_PORT'] != '443' && $protocol == 'https')) && strpos($_SERVER['HTTP_HOST'], ':') === false) ? ':'.$_SERVER['SERVER_PORT'] : '';
$url = urldecode($protocol.'://'.$_SERVER['HTTP_HOST'].$port.$_SERVER['REQUEST_URI']);
if (strlen($url) <= $max_length || $max_length == 0) {
return $url;
}
// We can't find a short enough url
return null;
}
//
// Fetch the current protocol in use - http or https
//
function get_current_protocol()
{
$protocol = 'http';
// Check if the server is claiming to using HTTPS
if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') {
$protocol = 'https';
}
// If we are behind a reverse proxy try to decide which protocol it is using
if (defined('FORUM_BEHIND_REVERSE_PROXY')) {
// Check if we are behind a Microsoft based reverse proxy
if (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) != 'off') {
$protocol = 'https';
}
// Check if we're behind a "proper" reverse proxy, and what protocol it's using
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
$protocol = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']);
}
}
return $protocol;
}
//
// Fetch the base_url, optionally support HTTPS and HTTP
//
function get_base_url($support_https = false)
{
global $feather_config;
static $base_url;
if (!$support_https) {
return $feather_config['o_base_url'];
}
if (!isset($base_url)) {
// Make sure we are using the correct protocol
$base_url = str_replace(array('http://', 'https://'), get_current_protocol().'://', $feather_config['o_base_url']);
}
return $base_url;
}
//
// Fetch admin IDs
//
function get_admin_ids()
{
if (file_exists(FORUM_CACHE_DIR.'cache_admins.php')) {
include FORUM_CACHE_DIR.'cache_admins.php';
}
if (!defined('FEATHER_ADMINS_LOADED')) {
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED')) {
require FEATHER_ROOT.'include/cache.php';
}
generate_admins_cache();
require FORUM_CACHE_DIR.'cache_admins.php';
}
return $feather_admins;
}
//
// Fill $feather->user with default values (for guests)
//
function set_default_user()
{
global $db_type, $feather_config;
// Get Slim current session
$feather = \Slim\Slim::getInstance();
$remote_addr = get_remote_address();
// Fetch guest user
$select_set_default_user = array('u.*', 'g.*', 'o.logged', 'o.last_post', 'o.last_search');
$where_set_default_user = array('u.id' => '1');
$result = \DB::for_table('users')
->table_alias('u')
->select_many($select_set_default_user)
->inner_join('groups', array('u.group_id', '=', 'g.g_id'), 'g')
->left_outer_join('online', array('o.ident', '=', $remote_addr), 'o', true)
->where($where_set_default_user)
->find_result_set();
if (!$result) {
exit('Unable to fetch guest information. Your database must contain both a guest user and a guest user group.');
}
foreach ($result as $feather->user);
// Update online list
if (!$feather->user->logged) {
$feather->user->logged = time();
// With MySQL/MySQLi/SQLite, REPLACE INTO avoids a user having two rows in the online table
switch ($db_type) {
case 'mysql':
case 'mysqli':
case 'mysql_innodb':
case 'mysqli_innodb':
case 'sqlite':
case 'sqlite3':
\DB::for_table('online')->raw_execute('REPLACE INTO '.$feather->prefix.'online (user_id, ident, logged) VALUES(1, :ident, :logged)', array(':ident' => $remote_addr, ':logged' => $feather->user->logged));
break;
default:
\DB::for_table('online')->raw_execute('INSERT INTO '.$feather->prefix.'online (user_id, ident, logged) SELECT 1, :ident, :logged WHERE NOT EXISTS (SELECT 1 FROM '.$feather->prefix.'online WHERE ident=:ident)', array(':ident' => $remote_addr, ':logged' => $feather->user->logged));
break;
}
} else {
\DB::for_table('online')->where('ident', $remote_addr)
->update_many('logged', time());
}
$feather->user->disp_topics = $feather_config['o_disp_topics_default'];
$feather->user->disp_posts = $feather_config['o_disp_posts_default'];
$feather->user->timezone = $feather_config['o_default_timezone'];
$feather->user->dst = $feather_config['o_default_dst'];
$feather->user->language = $feather_config['o_default_lang'];
$feather->user->style = $feather_config['o_default_style'];
$feather->user->is_guest = true;
$feather->user->is_admmod = false;
}
//
// Wrapper for Slim setCookie method
//
function feather_setcookie($user_id, $password, $expires)
{
global $cookie_name, $cookie_seed;
// Get Slim current session
$feather = \Slim\Slim::getInstance();
$cookie_data = array('user_id' => $user_id,
'password_hash' => hash_hmac('sha1', $password, $cookie_seed.'_password_hash'),
'expires' => $expires,
'checksum' => hash_hmac('sha1', $user_id.$expires, $cookie_seed.'_checksum'));
$feather->setCookie($cookie_name, json_encode($cookie_data), $expires);
}
//
// Check whether the connecting user is banned (and delete any expired bans while we're at it)
//
function check_bans()
{
global $feather_config, $lang_common, $feather_bans;
// Get Slim current session
$feather = \Slim\Slim::getInstance();
// Admins and moderators aren't affected
if ($feather->user->is_admmod || !$feather_bans) {
return;
}
// Add a dot or a colon (depending on IPv4/IPv6) at the end of the IP address to prevent banned address
// 192.168.0.5 from matching e.g. 192.168.0.50
$user_ip = get_remote_address();
$user_ip .= (strpos($user_ip, '.') !== false) ? '.' : ':';
$bans_altered = false;
$is_banned = false;
foreach ($feather_bans as $cur_ban) {
// Has this ban expired?
if ($cur_ban['expire'] != '' && $cur_ban['expire'] <= time()) {
\DB::for_table('bans')->where('id', $cur_ban['id'])
->delete_many();
$bans_altered = true;
continue;
}
if ($cur_ban['username'] != '' && utf8_strtolower($feather->user->username) == utf8_strtolower($cur_ban['username'])) {
$is_banned = true;
}
if ($cur_ban['ip'] != '') {
$cur_ban_ips = explode(' ', $cur_ban['ip']);
$num_ips = count($cur_ban_ips);
for ($i = 0; $i < $num_ips; ++$i) {
// Add the proper ending to the ban
if (strpos($user_ip, '.') !== false) {
$cur_ban_ips[$i] = $cur_ban_ips[$i].'.';
} else {
$cur_ban_ips[$i] = $cur_ban_ips[$i].':';
}
if (substr($user_ip, 0, strlen($cur_ban_ips[$i])) == $cur_ban_ips[$i]) {
$is_banned = true;
break;
}
}
}
if ($is_banned) {
\DB::for_table('online')->where('ident', $feather->user->username)
->delete_many();
message($lang_common['Ban message'].' '.(($cur_ban['expire'] != '') ? $lang_common['Ban message 2'].' '.strtolower(format_time($cur_ban['expire'], true)).'. ' : '').(($cur_ban['message'] != '') ? $lang_common['Ban message 3'].'<br /><br /><strong>'.feather_escape($cur_ban['message']).'</strong><br /><br />' : '<br /><br />').$lang_common['Ban message 4'].' <a href="mailto:'.feather_escape($feather_config['o_admin_email']).'">'.feather_escape($feather_config['o_admin_email']).'</a>.', true, true, true);
}
}
// If we removed any expired bans during our run-through, we need to regenerate the bans cache
if ($bans_altered) {
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED')) {
require FEATHER_ROOT.'include/cache.php';
}
generate_bans_cache();
}
}
//
// Check username
//
function check_username($username, $errors, $exclude_id = null)
{
global $feather, $feather_config, $errors, $lang_prof_reg, $lang_register, $lang_common, $feather_bans;
// Include UTF-8 function
require_once FEATHER_ROOT.'include/utf8/strcasecmp.php';
// Convert multiple whitespace characters into one (to prevent people from registering with indistinguishable usernames)
$username = preg_replace('%\s+%s', ' ', $username);
// Validate username
if (feather_strlen($username) < 2) {
$errors[] = $lang_prof_reg['Username too short'];
} elseif (feather_strlen($username) > 25) { // This usually doesn't happen since the form element only accepts 25 characters
$errors[] = $lang_prof_reg['Username too long'];
} elseif (!strcasecmp($username, 'Guest') || !utf8_strcasecmp($username, $lang_common['Guest'])) {
$errors[] = $lang_prof_reg['Username guest'];
} elseif (preg_match('%[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}%', $username) || preg_match('%((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))%', $username)) {
$errors[] = $lang_prof_reg['Username IP'];
} elseif ((strpos($username, '[') !== false || strpos($username, ']') !== false) && strpos($username, '\'') !== false && strpos($username, '"') !== false) {
$errors[] = $lang_prof_reg['Username reserved chars'];
} elseif (preg_match('%(?:\[/?(?:b|u|s|ins|del|em|i|h|colou?r|quote|code|img|url|email|list|\*|topic|post|forum|user)\]|\[(?:img|url|quote|list)=)%i', $username)) {
$errors[] = $lang_prof_reg['Username BBCode'];
}
// Check username for any censored words
if ($feather_config['o_censoring'] == '1' && censor_words($username) != $username) {
$errors[] = $lang_register['Username censor'];
}
// Check that the username (or a too similar username) is not already registered
$query = (!is_null($exclude_id)) ? ' AND id!='.$exclude_id : '';
$result = \DB::for_table('online')->raw_query('SELECT username FROM '.$feather->prefix.'users WHERE (UPPER(username)=UPPER(:username1) OR UPPER(username)=UPPER(:username2)) AND id>1'.$query, array(':username1' => $username, ':username2' => ucp_preg_replace('%[^\p{L}\p{N}]%u', '', $username)))->find_one();
if ($result) {
$busy = $result['username'];
$errors[] = $lang_register['Username dupe 1'].' '.feather_escape($busy).'. '.$lang_register['Username dupe 2'];
}
// Check username for any banned usernames
foreach ($feather_bans as $cur_ban) {
if ($cur_ban['username'] != '' && utf8_strtolower($username) == utf8_strtolower($cur_ban['username'])) {
$errors[] = $lang_prof_reg['Banned username'];
break;
}
}
return $errors;
}
//
// Update "Users online"
//
function update_users_online()
{
global $feather, $feather_config;
$now = time();
// Fetch all online list entries that are older than "o_timeout_online"
$select_update_users_online = array('user_id', 'ident', 'logged', 'idle');
$result = \DB::for_table('online')->select_many($select_update_users_online)
->where_lt('logged', $now-$feather_config['o_timeout_online'])
->find_many();
foreach ($result as $cur_user) {
// If the entry is a guest, delete it
if ($cur_user['user_id'] == '1') {
\DB::for_table('online')->where('ident', $cur_user['ident'])
->delete_many();
} else {
// 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
if ($cur_user['logged'] < ($now-$feather_config['o_timeout_visit'])) {
\DB::for_table('users')->where('id', $cur_user['user_id'])
->find_one()
->set('last_visit', $cur_user['logged'])
->save();
\DB::for_table('online')->where('user_id', $cur_user['user_id'])
->delete_many();
} elseif ($cur_user['idle'] == '0') {
\DB::for_table('online')->where('user_id', $cur_user['user_id'])
->update_many('idle', 1);
}
}
}
}
//
// Outputs markup to display a user's avatar
//
function generate_avatar_markup($user_id)
{
global $feather_config;
$filetypes = array('jpg', 'gif', 'png');
$avatar_markup = '';
foreach ($filetypes as $cur_type) {
$path = $feather_config['o_avatars_dir'].'/'.$user_id.'.'.$cur_type;
if (file_exists(FEATHER_ROOT.$path) && $img_size = getimagesize(FEATHER_ROOT.$path)) {
$avatar_markup = '<img src="'.feather_escape(get_base_url(true).'/'.$path.'?m='.filemtime(FEATHER_ROOT.$path)).'" '.$img_size[3].' alt="" />';
break;
}
}
return $avatar_markup;
}
//
// Generate browser's title
//
function generate_page_title($page_title, $p = null)
{
global $lang_common;
if (!is_array($page_title)) {
$page_title = array($page_title);
}
$page_title = array_reverse($page_title);
if ($p > 1) {
$page_title[0] .= ' ('.sprintf($lang_common['Page'], forum_number_format($p)).')';
}
$crumbs = implode($lang_common['Title separator'], $page_title);
return $crumbs;
}
//
// Save array of tracked topics in cookie
//
function set_tracked_topics($tracked_topics = null)
{
global $cookie_name;
// Get Slim current session
$feather = \Slim\Slim::getInstance();
if (!empty($tracked_topics)) {
// Sort the arrays (latest read first)
arsort($tracked_topics['topics'], SORT_NUMERIC);
arsort($tracked_topics['forums'], SORT_NUMERIC);
} else {
$tracked_topics = array('topics' => array(), 'forums' => array());
}
return $feather->setCookie($cookie_name . '_track', json_encode($tracked_topics), time() + $feather->config['o_timeout_visit']);
}
//
// Extract array of tracked topics from cookie
//
function get_tracked_topics()
{
global $cookie_name;
// Get Slim current session
$feather = \Slim\Slim::getInstance();
$cookie_raw = $feather->getCookie($cookie_name.'_track');
if (isset($cookie_raw)) {
$cookie_data = json_decode($cookie_raw, true);
return $cookie_data;
}
return array('topics' => array(), 'forums' => array());
}
//
// Update posts, topics, last_post, last_post_id and last_poster for a forum
//
function update_forum($forum_id)
{
// Get Slim current session
$feather = \Slim\Slim::getInstance();
$stats_query = \DB::for_table('topics')
->where('forum_id', $forum_id)
->select_expr('COUNT(id)', 'total_topics')
->select_expr('SUM(num_replies)', 'total_replies')
->find_one();
$num_topics = intval($stats_query['total_topics']);
$num_replies = intval($stats_query['total_replies']);
$num_posts = $num_replies + $num_topics; // $num_posts is only the sum of all replies (we have to add the topic posts)
$select_update_forum = array('last_post', 'last_post_id', 'last_poster');
$result = \DB::for_table('topics')->select_many($select_update_forum)
->where('forum_id', $forum_id)
->where_null('moved_to')
->order_by_desc('last_post')
->find_one();
if ($result) {
// There are topics in the forum
$insert_update_forum = array(
'num_topics' => $num_topics,
'num_posts' => $num_posts,
'last_post' => $result['last_post'],
'last_post_id' => $result['last_post_id'],
'last_poster' => $result['last_poster'],
);
} else {
// There are no topics
$insert_update_forum = array(
'num_topics' => $num_topics,
'num_posts' => $num_posts,
'last_post' => 'NULL',
'last_post_id' => 'NULL',
'last_poster' => 'NULL',
);
}
\DB::for_table('forums')
->where('id', $forum_id)
->find_one()
->set($insert_update_forum)
->save();
}
//
// Deletes any avatars owned by the specified user ID
//
function delete_avatar($user_id)
{
global $feather_config;
$filetypes = array('jpg', 'gif', 'png');
// Delete user avatar
foreach ($filetypes as $cur_type) {
if (file_exists(FEATHER_ROOT.$feather_config['o_avatars_dir'].'/'.$user_id.'.'.$cur_type)) {
@unlink(FEATHER_ROOT.$feather_config['o_avatars_dir'].'/'.$user_id.'.'.$cur_type);
}
}
}
//
// Delete a topic and all of its posts
//
function delete_topic($topic_id)
{
// Get Slim current session
$feather = \Slim\Slim::getInstance();
// Delete the topic and any redirect topics
$where_delete_topic = array(
array('id' => $topic_id),
array('moved_to' => $topic_id)
);
\DB::for_table('topics')
->where_any_is($where_delete_topic)
->delete_many();
// Delete posts in topic
\DB::for_table('posts')
->where('topic_id', $topic_id)
->delete_many();
// Delete any subscriptions for this topic
\DB::for_table('topic_subscriptions')
->where('topic_id', $topic_id)
->delete_many();
}
//
// Delete a single post
//
function delete_post($post_id, $topic_id)
{
// Get Slim current session
$feather = \Slim\Slim::getInstance();
$result = \DB::for_table('posts')
->select_many('id', 'poster', 'posted')
->where('topic_id', $topic_id)
->order_by_desc('id')
->limit(2)
->find_many();
$i = 0;
foreach ($result as $cur_result) {
if ($i == 0) {
$last_id = $cur_result['id'];
}
else {
$second_last_id = $cur_result['id'];
$second_poster = $cur_result['poster'];
$second_posted = $cur_result['posted'];
}
++$i;
}
// Delete the post
\DB::for_table('posts')
->where('id', $post_id)
->find_one()
->delete();
strip_search_index($post_id);
// Count number of replies in the topic
$num_replies = \DB::for_table('posts')->where('topic_id', $topic_id)->count() - 1;
// If the message we deleted is the most recent in the topic (at the end of the topic)
if ($last_id == $post_id) {
// If there is a $second_last_id there is more than 1 reply to the topic
if (isset($second_last_id)) {
$update_topic = array(
'last_post' => $second_posted,
'last_post_id' => $second_last_id,
'last_poster' => $second_poster,
'num_replies' => $num_replies,
);
\DB::for_table('topics')
->where('id', $topic_id)
->find_one()
->set($update_topic)
->save();
} else {
// We deleted the only reply, so now last_post/last_post_id/last_poster is posted/id/poster from the topic itself
\DB::for_table('topics')
->where('id', $topic_id)
->find_one()
->set_expr('last_post', 'posted')
->set_expr('last_post_id', 'id')
->set_expr('last_poster', 'poster')
->set('num_replies', $num_replies)
->save();
}
} else {
// Otherwise we just decrement the reply counter
\DB::for_table('topics')
->where('id', $topic_id)
->find_one()
->set('num_replies', $num_replies)
->save();
}
}
//
// Replace censored words in $text
//
function censor_words($text)
{
static $search_for, $replace_with;
// If not already built in a previous call, build an array of censor words and their replacement text
if (!isset($search_for)) {
if (file_exists(FORUM_CACHE_DIR.'cache_censoring.php')) {
include FORUM_CACHE_DIR.'cache_censoring.php';
}
if (!defined('FEATHER_CENSOR_LOADED')) {
if (!defined('FORUM_CACHE_FUNCTIONS_LOADED')) {
require FEATHER_ROOT.'include/cache.php';
}
generate_censoring_cache();
require FORUM_CACHE_DIR.'cache_censoring.php';
}
}
if (!empty($search_for)) {
$text = substr(ucp_preg_replace($search_for, $replace_with, ' '.$text.' '), 1, -1);
}
return $text;
}
//
// Determines the correct title for $user
// $user must contain the elements 'username', 'title', 'posts', 'g_id' and 'g_user_title'
//
function get_title($user)
{
global $feather_bans, $lang_common;
static $ban_list;
// If not already built in a previous call, build an array of lowercase banned usernames
if (empty($ban_list)) {
$ban_list = array();
foreach ($feather_bans as $cur_ban) {
$ban_list[] = utf8_strtolower($cur_ban['username']);
}
}
// If the user has a custom title
if ($user['title'] != '') {
$user_title = feather_escape($user['title']);
}
// If the user is banned
elseif (in_array(utf8_strtolower($user['username']), $ban_list)) {
$user_title = $lang_common['Banned'];
}
// If the user group has a default user title
elseif ($user['g_user_title'] != '') {
$user_title = feather_escape($user['g_user_title']);
}
// If the user is a guest
elseif ($user['g_id'] == FEATHER_GUEST) {
$user_title = $lang_common['Guest'];
}
// If nothing else helps, we assign the default
else {
$user_title = $lang_common['Member'];
}
return $user_title;
}
//
// Generate a string with numbered links (for multipage scripts)
//
function paginate($num_pages, $cur_page, $link, $args = null)
{
global $lang_common;
$pages = array();
$link_to_all = false;
// If $cur_page == -1, we link to all pages (used in viewforum.php)
if ($cur_page == -1) {
$cur_page = 1;
$link_to_all = true;
}
if ($num_pages <= 1) {
$pages = array('<strong class="item1">1</strong>');
} else {
// Add a previous page link
if ($num_pages > 1 && $cur_page > 1) {
$pages[] = '<a rel="prev"'.(empty($pages) ? ' class="item1"' : '').' href="'.get_sublink($link, 'page/$1', ($cur_page - 1), $args).'">'.$lang_common['Previous'].'</a>';
}
if ($cur_page > 3) {
$pages[] = '<a'.(empty($pages) ? ' class="item1"' : '').' href="'.$link.'">1</a>';
if ($cur_page > 5) {
$pages[] = '<span class="spacer">'.$lang_common['Spacer'].'</span>';
}
}
// Don't ask me how the following works. It just does, OK? :-)
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) {
if ($current < 1 || $current > $num_pages) {
continue;
} elseif ($current != $cur_page || $link_to_all) {
$pages[] = '<a'.(empty($pages) ? ' class="item1"' : '').' href="'.str_replace('#', '', get_sublink($link, 'page/$1', $current, $args)).'">'.forum_number_format($current).'</a>';
} else {
$pages[] = '<strong'.(empty($pages) ? ' class="item1"' : '').'>'.forum_number_format($current).'</strong>';
}
}
if ($cur_page <= ($num_pages-3)) {
if ($cur_page != ($num_pages-3) && $cur_page != ($num_pages-4)) {
$pages[] = '<span class="spacer">'.$lang_common['Spacer'].'</span>';
}
$pages[] = '<a'.(empty($pages) ? ' class="item1"' : '').' href="'.get_sublink($link, 'page/$1', $num_pages, $args).'">'.forum_number_format($num_pages).'</a>';
}
// Add a next page link
if ($num_pages > 1 && !$link_to_all && $cur_page < $num_pages) {
$pages[] = '<a rel="next"'.(empty($pages) ? ' class="item1"' : '').' href="'.get_sublink($link, 'page/$1', ($cur_page + 1), $args).'">'.$lang_common['Next'].'</a>';
}
}
return implode(' ', $pages);
}
//
// Generate a string with numbered links (for multipage scripts)
// Old FluxBB-style function for search page
//
function paginate_old($num_pages, $cur_page, $link)
{
global $lang_common;
$pages = array();
$link_to_all = false;
// If $cur_page == -1, we link to all pages (used in viewforum.php)
if ($cur_page == -1) {
$cur_page = 1;
$link_to_all = true;
}
if ($num_pages <= 1) {
$pages = array('<strong class="item1">1</strong>');
} else {
// Add a previous page link
if ($num_pages > 1 && $cur_page > 1) {
$pages[] = '<a rel="prev"'.(empty($pages) ? ' class="item1"' : '').' href="'.$link.($cur_page == 2 ? '' : '&p='.($cur_page - 1)).'">'.$lang_common['Previous'].'</a>';
}
if ($cur_page > 3) {
$pages[] = '<a'.(empty($pages) ? ' class="item1"' : '').' href="'.$link.'">1</a>';
if ($cur_page > 5) {
$pages[] = '<span class="spacer">'.$lang_common['Spacer'].'</span>';
}
}
// Don't ask me how the following works. It just does, OK? :-)
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) {
if ($current < 1 || $current > $num_pages) {
continue;
} elseif ($current != $cur_page || $link_to_all) {
$pages[] = '<a'.(empty($pages) ? ' class="item1"' : '').' href="'.$link.($current == 1 ? '' : '&p='.$current).'">'.forum_number_format($current).'</a>';
} else {
$pages[] = '<strong'.(empty($pages) ? ' class="item1"' : '').'>'.forum_number_format($current).'</strong>';
}
}
if ($cur_page <= ($num_pages-3)) {
if ($cur_page != ($num_pages-3) && $cur_page != ($num_pages-4)) {
$pages[] = '<span class="spacer">'.$lang_common['Spacer'].'</span>';
}
$pages[] = '<a'.(empty($pages) ? ' class="item1"' : '').' href="'.$link.'&p='.$num_pages.'">'.forum_number_format($num_pages).'</a>';
}
// Add a next page link
if ($num_pages > 1 && !$link_to_all && $cur_page < $num_pages) {
$pages[] = '<a rel="next"'.(empty($pages) ? ' class="item1"' : '').' href="'.$link.'&p='.($cur_page +1).'">'.$lang_common['Next'].'</a>';
}
}
return implode(' ', $pages);
}
//
// Display a message
//
function message($msg, $http_status = null, $no_back_link = false, $dontStop = false)
{
global $lang_common;
// Did we receive a custom header?
if (!is_null($http_status)) {
header('HTTP/1.1 ' . $http_status);
}
// Get Slim current session
$feather = \Slim\Slim::getInstance();
$http_status = (int) $http_status;
if ($http_status > 0) {
$feather->response->setStatus($http_status);
}
// Overwrite existing body
$feather->response->setBody('');
if (!defined('FEATHER_HEADER')) {
$page_title = array(feather_escape($feather->config['o_board_title']), $lang_common['Info']);
if (!defined('FEATHER_ACTIVE_PAGE')) {
define('FEATHER_ACTIVE_PAGE', 'index');
}
require_once FEATHER_ROOT.'controller/header.php';
$feather->config('templates.path', get_path_view());
$header = new \controller\header();
$header->setTitle($page_title)->display();
}
$feather->render('message.php', array(
'lang_common' => $lang_common,
'message' => $msg,