-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimgboard.php
10402 lines (8338 loc) · 279 KB
/
imgboard.php
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
require_once 'lib/util.php';
/*
if( isset( $_REQUEST["profile"] ) ) {
xhprof_enable( XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY );
register_shutdown_function( "xhprof_save" );
}
if ( isset($_REQUEST["sqlprofile"] )) {
$mysql_query_log = YES;
}
*/
require_once "yotsuba_config.php";
require_once( "lib/ads.php" );
if (TEST_BOARD) {
require_once 'lib/admin-test.php';
require_once( "lib/postfilter-test.php" );
require_once 'lib/captcha-test.php';
require_once 'lib/auth-test.php';
require_once 'lib/geoip2-test.php';
require_once 'lib/userpwd-test.php';
}
else {
require_once 'lib/admin.php';
require_once( "lib/postfilter.php" );
require_once 'lib/captcha.php';
require_once 'lib/auth.php';
require_once 'lib/geoip2.php';
require_once 'lib/userpwd.php';
}
require_once 'lib/phash.php';
if (ENABLE_PAINTERJS) {
if (TEST_BOARD) {
require_once('lib/oekaki-test.php');
}
else {
require_once('lib/oekaki.php');
}
}
// HTML rendering unique to imgboard and upload board
if (UPLOAD_BOARD) {
require_once "views/upboard.php";
} else if (TEST_BOARD) {
require_once "views/imgboard-test.php";
} else {
require_once "views/imgboard.php";
}
if( !function_exists( 'generate_catalog' ) ) {
if (TEST_BOARD) {
require_once 'catalog-test.php';
} else {
require_once 'catalog.php';
}
}
if( ENABLE_JSON ) {
$in_imgboard = 1;
if (TEST_BOARD) {
require_once 'json-test.php';
} else {
require_once 'json.php';
}
}
if (!defined('SQLLOGMOD')) {
define( 'SQLLOGBAN', 'banned_users' ); //Table (NOT DATABASE) used for holding banned users
define( 'SQLLOGMOD', 'mod_users' ); //Table (NOT DATABASE) used for holding mod users
}
define( 'SQLLOGDEL', 'del_log' ); //Table (NOT DATABASE) used for holding deletion log
auth_user();
if (LOCKDOWN) {
if (($_POST['mode'] == 'usrdel' || $_POST['mode'] == 'arcdel' || $_GET['mode'] == 'latest') && has_level('janitor')) {
// Allow janitors to delete posts and update mod tools
}
else if (has_level('manager') || has_flag('developer')) {
// Allow the manager to do anything
}
else {
die('<span style="color: red;" id="errmsg">' . LOCKDOWN_MSG . '</span>');
}
}
if (TEST_BOARD && (!has_level() || !has_flag('developer'))) {
die('');
}
// test
if( TEST_BOARD || has_flag('developer') ) {
ini_set( 'display_errors', 1 );
//error_reporting(E_ALL & ~E_NOTICE);
}
extract( $_POST, EXTR_SKIP );
extract( $_GET, EXTR_SKIP );
extract( $_COOKIE, EXTR_SKIP );
if (isset( $_COOKIE['4chan_pass']) && $_COOKIE['4chan_pass']) {
$pwdc = $_COOKIE['4chan_pass'];
}
else {
$pwdc = null;
}
// FIXME whitelist
unset( $dest );
unset( $log );
unset( $update_avg_secs );
if( $argv[1] ) $mode = $argv[1];
$id = intval( $id );
if( $_SERVER["REQUEST_METHOD"] == "POST" ) {
// bust cache
header( 'Cache-Control: private, no-cache, must-revalidate' );
header( 'Expires: -1' );
header( 'Vary: *' );
}
if( array_key_exists( 'upfile', $_FILES ) ) {
$upfile_name = $_FILES["upfile"]["name"];
$upfile = $_FILES["upfile"]["tmp_name"];
} else {
$upfile_name = $upfile = '';
}
$fwritetimer = 0.0;
ignore_user_abort( true );
$word_filters_enabled = false;
if (WORD_FILT) {
$word_filt_root = '/www/global/yotsuba/wordfilters/';
if (file_exists($word_filt_root . BOARD_DIR . '.php')) {
include_once($word_filt_root . BOARD_DIR . '.php');
$word_filters_enabled = true;
}
else if (file_exists($word_filt_root . 'global.php')) {
include_once($word_filt_root . 'global.php');
$word_filters_enabled = true;
}
}
if( JANITOR_BOARD == 1 && !has_level( 'janitor' ) ) {
die( '' );
}
if( JANITOR_BOARD == 1 )
include_once 'plugins/broomcloset.php';
// QENHANCE
if( META_BOARD ) {
include_once 'plugins/enhance_q.php';
}
$mysql_connect_opts = 0;
mysql_board_connect(BOARD_DIR);
$board_flags_array = null;
if (ENABLE_BOARD_FLAGS) {
$_flags_type = (defined('BOARD_FLAGS_TYPE') && BOARD_FLAGS_TYPE) ? BOARD_FLAGS_TYPE : BOARD_DIR;
$_board_flags_path = '/www/global/yotsuba/lib/board_flags_' . $_flags_type . '.php';
if (file_exists($_board_flags_path)) {
include_once($_board_flags_path);
$board_flags_array = get_board_flags_array();
}
}
$thread_unique_ips = 0;
$index_rbl = PAGE_MAX;
$index_last_thread = 0;
$index_last_post = 0;
if (JANITOR_BOARD && PAGE_MAX == 0) {
$index_rbl = ceil(LOG_MAX / DEF_PAGES);
}
$valid_boards = "3|aco|adv|an|biz|diy|fa|fit|gd|gif|int|lit|hc|hr|a|b|ck|co|cm|c|d|e|f|g|h|i|k|lgbt|m|n|o|out|p|r|s|t|u|vp|vg|vr|v|w|x|y|wg|ic|cgl|hm|mlp|mu|pol|po|r9k|s4s|sci|soc|tg|tv|toy|trv|jp|sp|wsg|qa|qst|his|trash|news|wsr|vip|bant|vrpg|vmg|vst|vt|vm|pw|xs";
$boards_matching_arr = array();
$captcha_bypass = null;
$rangeban_bypass = false;
$passid = '';
// FIXME, this should be put somewhere else.
function is_local() {
$longip = ip2long( $_SERVER[ 'REMOTE_ADDR' ] );
return !$longip || cidrtest( $longip, "10.0.0.0/24" ) || cidrtest( $longip, "204.152.204.0/24" ) || cidrtest( $longip, "127.0.0.0/24" );
}
/**
* Abbreviates posts on index pages.
* Truncate $str to $max_lines lines and return $str and $abbr
* where $abbr = whether or not $str was actually truncated.
* Expects well-formed HTML.
*/
function abbreviate($str, $max_lines = 20) {
$lines = explode('<br>', $str);
if (count($lines) > $max_lines) {
$abbr = 1;
$lines = array_slice($lines, 0, $max_lines);
$str = implode('<br>', $lines );
$unpaired_tags = array(
'img' => true,
'br' => true,
'input' => true,
'hr' => true,
'param' => true
);
preg_match_all('/<\/([^>]+)>/', $str, $closed_tags, PREG_SET_ORDER);
$closed_count = count($closed_tags);
$closed_map = array();
foreach ($closed_tags as $m) {
if (!isset($closed_map[$m[1]])) {
$closed_map[$m[1]] = 1;
}
else {
$closed_map[$m[1]] += 1;
}
}
preg_match_all('/<([a-z0-9]+)(?: |>)/', $str, $open_tags, PREG_SET_ORDER);
$open_count = count($open_tags);
for ($i = 0; $i < $open_count; ++$i) {
$tag = $open_tags[$i][1];
if (isset($unpaired_tags[$tag])) {
continue;
}
if (!isset($closed_map[$tag])) {
$str .= "</$tag>";
}
else if ($closed_map[$tag] > 0) {
$closed_map[$tag] -= 1;
}
else if ($closed_map[$tag] <= 0) {
$str .= "</$tag>";
}
}
}
else {
$abbr = 0;
}
return array($str, $abbr);
}
/**
* Currently only used on /archive
* strips html tags and replaces sjis art with [SJIS] placeholders
*/
function truncate_comment($str, $length, $keep_spoilers = false) {
// remove sjis
if (SJIS_TAGS && strpos($str, '<span class="sjis"') !== false) {
$str = preg_replace('/<span class="sjis".+?<\/span>/', '[SJIS]', $str);
}
$len = mb_strlen($str);
if ($len <= $length) {
return $str;
}
if (!$keep_spoilers) {
$str = strip_tags($str);
}
else {
$str = strip_tags($str, '<s>');
}
if ($len <= $length) {
return $str;
}
$str = mb_substr($str, 0, $length);
// remove truncated html entities
$str = preg_replace('/&[^;]*$/', '', $str);
if ($keep_spoilers) {
$str = preg_replace('/<[^>]*$/', '', $str);
$oc = substr_count($str, '<s>');
if ($oc) {
$cc = substr_count($str, '</s>');
$dc = $oc - $cc;
if ($dc > 0) {
$str .= str_repeat('</s>', $dc);
}
}
}
$str .= '…';
return $str;
}
function paranoid_rename( $src, $dest )
{
$across_devices = false; //keep around for future use
$u = false;
if( $across_devices ) {
// rename to dest dir, then over dest
$dsrc = dirname( $dest ) . "/" . basename( $src );
if( !@rename( $src, $dsrc ) ) $u = $src;
else if( !@rename( $dsrc, $dest ) ) $u = $dsrc;
} else {
if( !@rename( $src, $dest ) ) $u = $src;
}
if( $u )
unlink( $u );
}
function rename_across_device( $src, $dest )
{
// FIXME: copy() does a chmod but we don't need that
copy($src, $dest);
unlink($src);
}
function getmypid_cached()
{
static $pid = -1;
if ($pid === -1) $pid = getmypid();
return $pid;
}
// print $contents to $filename by using a temporary file and renaming it
// may destroy $contents in the process
// (makes *.html and *.gz if USE_GZIP is on)
function print_page( $filename, &$contents, $force_nogzip = 0, $trim_whitespace = 1 )
{
global $fwritetimer;
$timestarted = microtime( true );
if( NEW_HTML == 1 && $trim_whitespace ) {
$contents = str_replace( array("\r\n", "\n", "\t"), array('', '', ''), $contents );
}
$gzip = ( USE_GZIP == 1 && !$force_nogzip );
if( $gzip ) {
$tempname = dirname( $filename )."/gztmp".getmypid_cached();
// FIXME: number of syscalls done by gzwrite is not optimal (it does a small one then 4KB writes after)
// for small files (how small?) do gzencode() and file_put_contents() instead.
$fp = gzopen($tempname, "wb9");
if( $fp === false ) return;
gzwrite($fp, $contents);
gzclose($fp);
// chmod( $tempname, 0664 ); //it was created 0600
paranoid_rename( $tempname, $filename . ".gz" );
} else {
$tempname = dirname( $filename )."/tmp".getmypid_cached();
if( file_put_contents( $tempname, $contents ) === false ) return;
// chmod( $tempname, 0664 ); //it was created 0600
paranoid_rename( $tempname, $filename );
}
$fwritetimer += ( microtime( true ) - $timestarted );
}
function file_get_contents_cached( $filename )
{
static $cache = array();
if( isset( $cache[$filename] ) )
return $cache[$filename];
$cache[$filename] = @file_get_contents( $filename );
return $cache[$filename];
}
function file_array_cached( $filename )
{
static $cache = array();
if( isset( $cache[$filename] ) )
return $cache[$filename];
$cache[$filename] = @file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
return $cache[$filename];
}
function get_blotter() {
if (!SHOW_BLOTTER) {
return '';
}
$msg_limit = 3;
$blotter = <<<HTML
<table id="blotter" class="desktop"><thead><tr><td colspan="2"><hr class="aboveMidAd"></td></tr></thead><tbody id="blotter-msgs">
HTML;
$query = <<<SQL
SELECT sql_cache `date`, content
FROM blotter_messages
ORDER BY id DESC LIMIT $msg_limit
SQL;
$res = mysql_global_call($query);
$mtime = 0;
if ($res && mysql_num_rows($res) > 0) {
while ($row = mysql_fetch_assoc($res)) {
if ($mtime === 0) {
$mtime = $row['date'];
}
$blotter .= '<tr><td data-utc="' .
$row['date'] . '" class="blotter-date">' .
date('m/d/y', $row['date']) . '</td><td class="blotter-content">' .
$row['content'] . '</td></tr>';
}
}
else {
return '';
}
$blotter .= '</tbody><tfoot><tr><td colspan="2">[<a data-utc="' . $mtime
. '" id="toggleBlotter" href="#">Hide</a>]<span> [<a href="//www.'
. L::d(BOARD_DIR)
. '/blotter" target="_blank">Show All</a>]</span></td></tr></tfoot></table>';
return $blotter;
}
function blotter_contents()
{
static $cache;
if( isset( $cache ) ) return $cache;
$ret = "";
$topN = 4; //how many lines to print
$bl_lines = file( BLOTTER_PATH );
$bl_top = array_slice( $bl_lines, 0, $topN );
$date = "";
foreach( $bl_top as $line ) {
if( !$date ) {
$lineparts = explode( ' - ', $line );
if( strpos( $lineparts[0], '<font' ) !== false ) {
$dateparts = explode( '>', $lineparts[0] );
$date = $dateparts[1];
$date = "<li><font color=\"red\">Blotter updated: $date</font>";
} else {
$date = $lineparts[0];
$date = "<li>Blotter updated: $date";
}
}
$line = trim( $line );
$line = str_replace( "\\", "\\\\", $line );
$line = str_replace( "'", "\'", $line );
$ret .= "'<li>$line'+\n";
}
$ret .= "''";
$cache = array($date, $ret);
return array($date, $ret);
}
function find_match_and_prefix( $regex, $str, $off, &$match )
{
if( !preg_match( $regex, $str, $m, PREG_OFFSET_CAPTURE, $off ) ) return false;
$moff = $m[0][1];
$match = array(substr( $str, $off, $moff - $off ), $m[0][0]);
return true;
}
// skip_on_spoilers will stop parsing and return the unmodified comment
// if spoiler tags are found inside the string to wrap.
// This is to avoid sjis.spoiler tags mixing mostly.
function parse_bbcode_one( $com, $tn, $st, $et, $nest_limit = 2, $skip_on_spoilers = false )
{
if( !find_match_and_prefix( "/\[$tn\]/", $com, 0, $m ) ) return $com;
$bracket_tn = "[$tn]";
$bl = strlen( $bracket_tn );
$el = $bl + 1;
$ret = $m[0] . $st;
$lev = 1;
$off = strlen( $m[0] ) + $bl;
while( 1 ) {
if (!find_match_and_prefix( "@\[/?$tn\]@", $com, $off, $m)) break;
list( $txt, $tag ) = $m;
if (!$skip_on_spoilers || $tag === $bracket_tn) {
$ret .= $txt;
}
else if (preg_match('/\[\/?spoiler\]/', $txt)) {
return $com;
}
$off += strlen( $txt ) + strlen( $tag );
if( $tag == $bracket_tn ) {
if( $lev < $nest_limit )
$ret .= $st;
$lev++;
} else if( $lev ) {
if( $lev <= $nest_limit )
$ret .= $et;
$lev--;
}
}
$tail = substr($com, $off, strlen($com) - $off);
$ret .= $tail;
$lev = min( $lev, $nest_limit );
if ($lev > 0) {
if ($skip_on_spoilers && preg_match('/\[\/?spoiler\]/', $tail)) {
return $com;
}
$ret .= str_repeat( $et, $lev );
}
return $ret;
}
function spoiler_parse( $com )
{
return parse_bbcode_one( $com, 'spoiler', '<s>', '</s>' );
}
function jsmath_parse( $com )
{
$com = parse_bbcode_one( $com, "math", '<span class="math">', '</span>' );
$com = parse_bbcode_one( $com, "eqn", '<div class="math">', '</div>' );
return $com;
}
/* BBCode for bold, italic, and r/g/b color tags */
function parse_op_markup($com) {
$com = parse_bbcode_one($com, 'b', '<span class="mu-s">', '</span>', 1);
$com = parse_bbcode_one($com, 'i', '<span class="mu-i">', '</span>', 1);
$com = parse_bbcode_one($com, 'red', '<span class="mu-r">', '</span>', 1);
$com = parse_bbcode_one($com, 'green', '<span class="mu-g">', '</span>', 1);
$com = parse_bbcode_one($com, 'blue', '<span class="mu-b">', '</span>', 1);
return $com;
}
function code_parse( $com )
{
return parse_bbcode_one( $com, 'code', '<pre class="prettyprint">', '</pre>' );
}
function sjis_parse($com) {
$skip_on_spoilers = strpos($com, '[spoiler]') !== false;
return parse_bbcode_one($com, 'sjis', '<span class="sjis">', '</span>', 1, $skip_on_spoilers);
}
// convenience function for wordfilters.
// text must be html escaped
function random_color( $str, $background = 1, $foreground = 1 )
{
$style = "";
if( $background ) {
$r = rand( 0, 255 );
$g = rand( 0, 255 );
$b = rand( 0, 255 );
$style = $style . "background: #" . sprintf( "%02x%02x%02x", $r, $g, $b ) . "; ";
}
if( $foreground ) {
$r = rand( 0, 255 );
$g = rand( 0, 255 );
$b = rand( 0, 255 );
$style = $style . "color: #" . sprintf( "%02x%02x%02x", $r, $g, $b ) . "; ";
}
if( $style ) {
return "<span style=\"$style\">$str</span>";
}
return $str;
}
function append_ban( $board, $ip )
{
$cmd = "nohup /usr/local/bin/suid_run_global bin/appendban $board $ip >/dev/null 2>&1 &";
exec( $cmd );
}
// check whether the current user can perform $action (on $no, for some actions)
// board-level access is cached in $valid_cache.
// FIXME move to lib/admin.php
function valid( $action = 'moderator', $no = 0 )
{
static $valid_cache, $can_post_html; // the access level of the user
$access_level = array('none' => 0, 'janitor' => 1, 'janitor_this_board' => 2, 'moderator' => 5, 'manager' => 10, 'admin' => 20);
if( !isset( $valid_cache ) ) {
$valid_cache = $access_level['none'];
if( isset( $_COOKIE['4chan_auser'] ) && isset( $_COOKIE['apass'] ) ) {
$user = mysql_real_escape_string( $_COOKIE['4chan_auser'] );
$pass = $_COOKIE['apass'];
}
if( $user && $pass ) {
$result = mysql_global_call( "SELECT allow,deny,password_expired,username,password FROM " . SQLLOGMOD . " WHERE username='$user' LIMIT 1" );
list( $allow, $deny, $expired, $username, $password ) = mysql_fetch_row( $result );
mysql_free_result( $result );
$admin_salt = file_get_contents('/www/keys/2014_admin.salt');
if (!$admin_salt) {
die('Internal Server Error (s0)');
}
$hashed_admin_password = hash('sha256', $username . $password . $admin_salt);
if ($hashed_admin_password !== $pass) {
return false;
}
if( $expired ) {
error( 'Your password has expired; check IRC for instructions on changing it.' );
}
if( $allow ) {
$allows = explode( ',', $allow );
$seen_janitor_token = false;
// each token can increase the access level,
// except that we only know that they're a moderator or a janitor for another board
// AFTER we read all the tokens
$cphtml = false;
foreach( $allows as $token ) {
if( $token == 'janitor' ) {
$seen_janitor_token = true;
} else if( $token == 'manager' && $valid_cache < $access_level['manager'] ) {
$valid_cache = $access_level['manager'];
} else if( $token == 'admin' && $valid_cache < $access_level['admin'] ) {
$valid_cache = $access_level['admin'];
} else if( ( $token == BOARD_DIR || $token == 'all' ) && $valid_cache < $access_level['janitor_this_board'] ) {
$valid_cache = $access_level['janitor_this_board']; // or could be moderator, will be increased in next step
} elseif( $token == 'html' ) {
$cphtml = true;
}
}
$can_post_html = $cphtml;
// now we can set moderator or janitor status
if( !$seen_janitor_token ) {
if( $valid_cache < $access_level['moderator'] )
$valid_cache = $access_level['moderator'];
} else {
if( $valid_cache < $access_level['janitor'] )
$valid_cache = $access_level['janitor'];
}
if( $deny ) {
$denies = explode( ',', $deny );
if( in_array( BOARD_DIR, $denies ) ) {
$valid_cache = $access_level['none'];
}
}
}
}
}
{
// local rpc can do anything
$longip = ip2long( $_SERVER['REMOTE_ADDR'] );
if( !$longip || cidrtest( $longip, "10.0.0.0/24" ) ||
cidrtest( $longip, "204.152.204.0/24" ) || cidrtest( $longip, "127.0.0.0/24" )
)
return YES;
}
switch( $action ) {
case 'moderator':
return $valid_cache >= $access_level['moderator'];
case 'textonly':
return $valid_cache >= $access_level['moderator'];
case 'htmlnopw':
return $valid_cache >= $access_level['manager'];
case 'htmlpost':
return $can_post_html;
case 'janitor_board':
return $valid_cache >= $access_level['janitor'];
case 'delete':
if( $valid_cache >= $access_level['janitor_this_board'] ) {
return true;
} // if they're a janitor on another board, check for illegal post unlock
else if( $valid_cache >= $access_level['janitor'] ) {
$query = mysql_global_do( "SELECT COUNT(*) from reports WHERE board='" . BOARD_DIR . "' AND no=$no AND cat=2" );
$illegal_count = mysql_result( $query, 0, 0 );
mysql_free_result( $query );
return $illegal_count >= 3;
}
case 'reportflood':
return $valid_cache >= $access_level['janitor'];
case 'floodbypass':
return $valid_cache >= $access_level['janitor'];
case 'rebuild':
return $valid_cache >= $access_level['janitor'];
case 'admin':
return $valid_cache >= $access_level['admin'];
default: // unsupported action
return false;
}
}
function iplog_add( $board, $no, $ip, $time, $is_thread, $tim, $had_image )
{
mysql_global_call( "INSERT INTO user_actions (board,postno,ip,time,uploaded,action,had_image) VALUES ('%s',%d,%d,from_unixtime(%d),%d,'%s',%d)",$board, $no, ip2long($ip), $time, $tim, $is_thread ? "new_thread" : "new_reply", $had_image);
}
function clean_log_bool( &$row )
{
static $bool_cols = array('sticky', 'permasage', 'closed', 'filedeleted', 'permaage', 'undead', 'archived');
foreach ($bool_cols as $col) {
if (!isset($row[$col])) continue; // FIXME split this function up to avoid this test
$c = &$row[$col];
settype($c, "bool");
// NOTES: $c = $c ? TRUE : FALSE allocates new bools each time
// $c = $c ? &$itrue : &$ifalse causes them to be converted to strings(!)
// Put this back to TRUE : FALSE instead of a settype call sometime so we can look at the php bytecodes
}
}
function clean_log_int( &$row )
{
static $int_cols = array('no', 'w', 'h', 'tn_w', 'tn_h', 'last_modified', 'time', 'fsize', 'resto');
// turn columns into int (does this help?)
foreach( $int_cols as $col ) {
if (!isset($row[$col])) continue;
$c = &$row[$col];
settype($c, "int");
}
}
function clean_log_delreply( &$row )
{
if (!$row['resto'])
return;
static $del_cols = array('sticky', 'permasage', 'closed', 'last_modified', 'root', 'undead', 'permaage');
// delete fields not used for replies
foreach( $del_cols as $col ) {
unset($row[$col]);
}
}
function clean_log_intern( &$row )
{
static $intern_cols = array('name', 'ext', 'capcode', 'country');
// Intern repeated strings that are usually the same value.
// In PHP 6 this... doesn't seem to do anything? Let's try again in 7.
static $log_intern;
if( !isset( $log_intern ) ) {
$log_intern = array();
foreach( $intern_cols as $col )
$log_intern[$col] = array();
}
foreach( $intern_cols as $col ) {
$intern_array = &$log_intern[$col];
$c = &$row[$col];
$v = $c;
if( !isset($intern_array[$v]) )
$intern_array[$v] = $v;
$c = &$intern_array[$c];
}
}
function clean_log_row( &$row )
{
//static $rn = 0;
clean_log_delreply( $row );
clean_log_bool( $row );
clean_log_int( $row );
//clean_log_intern( $row );
//if (++$rn == 100) {
// debug_zval_dump($row);
//}
}
function log_bad_cache_entry($no)
{
global $log_cache_level;
global $log;
internal_error_log("logcache", "missing children for OP no $no, cache level $log_cache_level, cache contents ".count($log));
}
function invalidate_log($thread)
{
global $log_cache_level;
global $log;
if (isset($log[$thread])) {
if ($log[$thread]['resto']) {
die(S_ASSERT);
}
unset($log[$thread]);
}
if ($log_cache_level==2)
$log_cache_level = 1;
}
// build a structure out of all the posts in the database.
// this lets us replace a LOT of queries with a simple array access.
// it only builds the first time it was called.
// rather than calling log_cache(1) to rebuild everything,
// you should just manipulate the structure directly.
// $thread may be any postno in a thread
// without a thread, $archive_mode fetches all live threads if 0 and all archived threads if 1
function log_cache($invalidate = 0, $thread = 0, $archive_mode = 0) {
global $log_cache_level;
global $log, $ipcount, $mysql_unbuffered_reads;
if (!isset($log) || $invalidate) {
$log = array();
$log_cache_level = 0;
}
// Optimisation for index rebuilding when REPLIES_SHOWN is 0.
// No need to fetch the entire board in this case.
$optimised_indexes = !$thread && !$archive_mode && !REPLIES_SHOWN && IS_REBUILDD;
// Handle cache
// 1 = Live OPs are cached, 2 = Some threads are cached, 3 = Whole live board is cached
if ($optimised_indexes) {
$nlog_cache_level = 1;
}
else {
$nlog_cache_level = $thread ? 2 : 3;
}
// Whole board is cached, nothing to do.
if ($log_cache_level == 3 && $archive_mode === 0) {
return;
}
// Thread is cached, nothing to do.
if ($log_cache_level == 2 && isset($log[$thread])) {
return;
}
// Live OPs are cached, nothing to do.
if ($log_cache_level == 1 && $optimised_indexes) {
return;
}
if ($nlog_cache_level > $log_cache_level) {
$log_cache_level = $nlog_cache_level;
}
$ips = array();
mysql_board_call( "SET read_buffer_size=1048576" );
$mysql_unbuffered_reads = 1;
$query_archived = false;
if ($thread) {
if ($archive_mode === 0) {
$where = " WHERE archived = 0 AND (resto = $thread OR no = $thread)";
}
else if ($archive_mode === 1) {
$where = " WHERE archived = 1 AND (resto = $thread OR no = $thread)";
}
else {
$query_archived = true;
$where = " WHERE (archived = 0 AND resto = $thread) OR (archived = 1 AND resto = $thread) OR no = $thread";
}
}
else {
if ($archive_mode === 1) {
$query_archived = true;
$where = ' WHERE archived = 1';
}
else if ($optimised_indexes) {
$where = ' WHERE archived = 0 AND resto = 0';
$_thread_ids = array();
}
else {
$where = ' WHERE archived = 0';
}
}
$fields = "no,sticky,permasage,closed,now,name,sub,com,host,pwd,filename,ext,w,h,tn_w,tn_h,tim,time,md5,fsize,last_modified,root,resto,filedeleted,id,capcode,country,undead,permaage,since4pass";
if ($query_archived) {
$fields .= ",archived";
}
if (MOBILE_IMG_RESIZE) {
$fields .= ",m_img";
}
if (ENABLE_BOARD_FLAGS) {
$fields .= ",board_flag";
}
$sql_cache = "sql_no_cache";
$query = mysql_board_call( "SELECT $sql_cache $fields FROM `" . SQLLOG . "`" . $where );
$offset = 0;
while( $row = mysql_fetch_assoc( $query ) ) {
if (!$query_archived) $row['archived'] = $archive_mode;
clean_log_row( $row );
$row_no = $row['no'];
$row_resto = $row['resto'];
// IF mysql returns rows in order by default then replies come after OP
// so if OP doesn't exist, this post is orphaned and should be skipped
// TODO: let's not skip for now, it seems more likely to cause bugs than anything
//if ($fetching_whole_threads && $row_resto && !isset($log[$row_resto])) continue;
if ($optimised_indexes) {
$_thread_ids[] = (int)$row_no;
}
$ips[$row['host']] = TRUE;
if (!$row_resto) {
$row['children'] = array();
$row['imgreplycount'] = 0;
$row['replycount'] = 0;
}
else {
if (isset($log[$row_resto])) {
$log[$row_resto]['children'][$row_no] = TRUE;
if ($row['fsize'] && !$row['filedeleted']) {
$log[$row_resto]['imgreplycount']++;
}
$log[$row_resto]['replycount']++;
}
}
$log[$row_no] = $row;
}
$query = null;
$nrows = count($log);
//if (BOARD_DIR=="b" && !$thread) quick_log_to("/www/perhost/logcaches.log", "inefficient all-board log_cache run t=$thread r=$nrows inv=$invalidate lev=$log_cache_level", true);
// if (!STATIC_REBUILD && $thread) quick_log_to("/www/perhost/logcaches.log", "inefficient? one-thread log_cache run t=$thread r=$nrows inv=$invalidate lev=$log_cache_level", true);
$is_single_thread = $thread && isset($log[$thread]['children']);
$ipcount = count( $ips );
unset($ips);
$mysql_unbuffered_reads = 0;
mysql_board_call( "SET read_buffer_size=131072" );
if (!$thread) {
if ($optimised_indexes) {
if (empty($_thread_ids)) {
return;
}