/
MySQL.pm
5685 lines (4835 loc) · 163 KB
/
MySQL.pm
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
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2002 by Open Source Development Network. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id$
package Slash::DB::MySQL;
use strict;
use Digest::MD5 'md5_hex';
use HTML::Entities;
use Time::HiRes;
use Date::Format qw(time2str);
use Slash::Utility;
use Storable qw(thaw freeze);
use URI ();
use vars qw($VERSION);
use base 'Slash::DB';
use base 'Slash::DB::Utility';
# for palmlog
use MIME::Base64;
($VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
# Fry: How can I live my life if I can't tell good from evil?
# For the getDecriptions() method
my %descriptions = (
'sortcodes'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='sortcodes'") },
'generic'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='$_[2]'") },
'statuscodes'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='statuscodes'") },
'months'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='months'") },
'years'
=> sub { $_[0]->sqlSelectMany('name,name', 'code_param', "type='years'") },
'blocktype'
=> sub { $_[0]->sqlSelectMany('name,name', 'code_param', "type='blocktype'") },
'tzcodes'
=> sub { $_[0]->sqlSelectMany('tz,off_set', 'tzcodes') },
'tzdescription'
=> sub { $_[0]->sqlSelectMany('tz,description', 'tzcodes') },
'dateformats'
=> sub { $_[0]->sqlSelectMany('id,description', 'dateformats') },
'datecodes'
=> sub { $_[0]->sqlSelectMany('id,format', 'dateformats') },
'discussiontypes'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='discussiontypes'") },
'commentmodes'
=> sub { $_[0]->sqlSelectMany('mode,name', 'commentmodes') },
'threshcodes'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='threshcodes'") },
'threshcode_values'
=> sub { $_[0]->sqlSelectMany('code,code', 'code_param', "type='threshcodes'") },
'postmodes'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='postmodes'") },
'isolatemodes'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='isolatemodes'") },
'issuemodes'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='issuemodes'") },
'vars'
=> sub { $_[0]->sqlSelectMany('name,name', 'vars') },
'topics'
=> sub { $_[0]->sqlSelectMany('tid,alttext', 'topics') },
'topics_all'
=> sub { $_[0]->sqlSelectMany('tid,alttext', 'topics') },
'topics_section'
=> sub { $_[0]->sqlSelectMany('topics.tid,topics.alttext', 'topics, section_topics', "section='$_[2]' AND section_topics.tid=topics.tid") },
'maillist'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='maillist'") },
'session_login'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='session_login'") },
'sortorder'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='sortorder'") },
'displaycodes'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='displaycodes'") },
'commentcodes'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='commentcodes'") },
'sections'
=> sub { $_[0]->sqlSelectMany('section,title', 'sections', 'isolate=0', 'order by title') },
'sections-all'
=> sub { $_[0]->sqlSelectMany('section,title', 'sections', '', 'ORDER BY title') },
'static_block'
=> sub { $_[0]->sqlSelectMany('bid,bid', 'blocks', "$_[2] >= seclev AND type != 'portald'") },
'portald_block'
=> sub { $_[0]->sqlSelectMany('bid,bid', 'blocks', "$_[2] >= seclev AND type = 'portald'") },
'color_block'
=> sub { $_[0]->sqlSelectMany('bid,bid', 'blocks', "type = 'color'") },
'authors'
=> sub { $_[0]->sqlSelectMany('uid,nickname', 'authors_cache', "author = 1") },
'all-authors'
=> sub { $_[0]->sqlSelectMany('uid,nickname', 'authors_cache') },
'admins'
=> sub { $_[0]->sqlSelectMany('uid,nickname', 'users', 'seclev >= 100') },
'users'
=> sub { $_[0]->sqlSelectMany('uid,nickname', 'users') },
'templates'
=> sub { $_[0]->sqlSelectMany('tpid,name', 'templates') },
'templatesbypage'
=> sub { $_[0]->sqlSelectMany('tpid,name', 'templates', "page = '$_[2]'") },
'templatesbysection'
=> sub { $_[0]->sqlSelectMany('tpid,name', 'templates', "section = '$_[2]'") },
'keywords'
=> sub { $_[0]->sqlSelectMany('id,CONCAT(keyword, " - ", name)', 'related_links') },
'pages'
=> sub { $_[0]->sqlSelectMany('distinct page,page', 'templates') },
'templatesections'
=> sub { $_[0]->sqlSelectMany('distinct section, section', 'templates') },
'sectionblocks'
=> sub { $_[0]->sqlSelectMany('bid,title', 'blocks', 'portal=1') },
'plugins'
=> sub { $_[0]->sqlSelectMany('value,description', 'site_info', "name='plugin'") },
'site_info'
=> sub { $_[0]->sqlSelectMany('name,value', 'site_info', "name != 'plugin'") },
'topic-sections'
=> sub { $_[0]->sqlSelectMany('section,1', 'section_topics', "tid = '$_[2]'") },
'forms'
=> sub { $_[0]->sqlSelectMany('value,value', 'site_info', "name = 'form'") },
'journal_discuss'
=> sub { $_[0]->sqlSelectMany('code,name', 'code_param', "type='journal_discuss'") },
);
########################################################
sub _whereFormkey {
my($self) = @_;
my $ipid = getCurrentUser('ipid');
my $uid = getCurrentUser('uid');
my $where;
# anonymous user without cookie, check host, not ipid
if (isAnon($uid)) {
$where = "ipid = '$ipid'";
} else {
$where = "uid = '$uid'";
}
return $where;
};
########################################################
# Notes:
# formAbuse, use defaults as ENV, be able to override
# (pudge idea).
# description method cleanup. (done)
# fetchall_rowref vs fetch the hashses and push'ing
# them into an array (good arguments for both)
# break up these methods into multiple classes and
# use the dB classes to override methods (this
# could end up being very slow though since the march
# is kinda slow...).
# the getAuthorEdit() methods need to be refined
########################################################
########################################################
sub init {
my($self) = @_;
# These are here to remind us of what exists
$self->{_storyBank} = {};
$self->{_codeBank} = {};
$self->{_sectionBank} = {};
$self->{_boxes} = {};
$self->{_sectionBoxes} = {};
}
#######################################################
# Wrapper to get the latest ID from the database
sub getLastInsertId {
my($self, $table, $col) = @_;
my($answer) = $self->sqlSelect('LAST_INSERT_ID()');
return $answer;
}
########################################################
# Yes, this is ugly, and we can ditch it in about 6 months
# Turn off autocommit here
sub sqlTransactionStart {
my($self, $arg) = @_;
$self->sqlDo($arg);
}
########################################################
# Put commit here
sub sqlTransactionFinish {
my($self) = @_;
$self->sqlDo("UNLOCK TABLES");
}
########################################################
# In another DB put rollback here
sub sqlTransactionCancel {
my($self) = @_;
$self->sqlDo("UNLOCK TABLES");
}
########################################################
# Bad need of rewriting....
sub createComment {
my($self, $comment) = @_;
my $comment_text = $comment->{comment};
delete $comment->{comment};
$comment->{signature} = md5_hex($comment_text);
$comment->{-date} = 'now()';
my $cid;
if ($self->sqlInsert('comments', $comment)) {
$cid = $self->getLastInsertId();
} else {
errorLog("$DBI::errstr");
return -1;
}
$self->sqlInsert('comment_text', {
cid => $cid,
comment => $comment_text,
});
# should this be conditional on the others happening?
# is there some sort of way to doublecheck that this value
# is correct? -- pudge
# This is fine as is; if the insert failed, we've already
# returned out of this method. - Jamie
$self->sqlUpdate(
"discussions",
{ -commentcount => 'commentcount+1' },
"id=$comment->{sid}",
);
return $cid;
}
########################################################
sub setModeratorLog {
my($self, $comment, $uid, $val, $reason, $active) = @_;
$active = 1 unless defined $active;
$self->sqlInsert("moderatorlog", {
uid => $uid,
val => $val,
sid => $comment->{sid},
cid => $comment->{cid},
cuid => $comment->{uid},
reason => $reason,
-ts => 'now()',
active => $active,
});
}
########################################################
#this is broke right now -Brian
#
# Work, dammit! - Cliff
sub getMetamodComments {
my($self, $user, $num_comments) = @_;
#require Benchmark;
#my $t0 = new Benchmark;
# We first check to see if we have any moderator records that need
# processing at the current count level If not, we then increment the
# count level and use that.
#
# If the vars are cached, might we have a race condition here?
my $thresh = $self->getVar('m2_consensus', 'value');
my $previousM2s = [
map { $_ = $_->[0] }
@{$self->sqlSelectAll('mmid', 'metamodlog', "uid=$user->{uid}")}
];
my $M2mods = [];
$self->sqlTransactionStart('LOCK TABLES moderatorlog READ, vars WRITE');
my $modpos = $user->{lastmmid} ||
$self->getVar('m2_modlog_pos', 'value');
my $timesthru = $self->getVar('m2_modlog_cycles', 'value');
my($minMod, $maxMod) =
$self->sqlSelect('min(id), max(id)', 'moderatorlog');
$minMod--;
my($count, $num) = (0, $num_comments);
while ($num && $count < 2) {
my @excluded;
@excluded = map { $_ = $_->{id} } (@excluded = @{$M2mods})
if @{$M2mods};
push @excluded, @{$previousM2s} if scalar @{$previousM2s};
$modpos = $minMod if $modpos < $minMod ||
$maxMod - $modpos < $num_comments;
my $cond = "moderatorlog.uid != $user->{uid}
AND moderatorlog.cuid != $user->{uid}
AND moderatorlog.reason < 8
AND moderatorlog.id > $modpos
AND moderatorlog.m2count < $thresh";
{
local $" = ',';
$cond .= " AND id NOT IN (@excluded)"
if scalar @excluded;
}
my $result = $self->sqlSelectAllHashrefArray(
'id, cid as mcid, reason as modreason',
'moderatorlog',
$cond,
"ORDER BY id LIMIT $num"
);
if ($result) {
push @{$M2mods}, @{$result};
$num -= scalar @{$result};
}
# We only do this the first time thru.
if ($num && ! $count) {
$self->setVar('m2_modlog_cycles', $timesthru + 1);
$modpos = $minMod;
}
$count++;
}
# Only write position change if it changes for the user.
$self->setVar('m2_modlog_pos', $M2mods->[-1]{id})
if @{$M2mods} && !$user->{lastmmid};
$self->sqlTransactionFinish();
# Note in error log if we've picked up less than the requested number
# of comments.
if ($num) {
my @list = @{$M2mods};
$_ = $_->{id} for @list;
local $" = ', ';
errorLog(<<EOT);
M2 - Gave U#$user->{uid} less than $num_comments comments: [@list]
EOT
}
# Update user if necessary. Users are STUCK at the same moderations
# for M2 until they submit the form (or those comments fall out of
# the moderatorlog.
$self->setUser($user->{uid}, { lastmmid => $modpos })
if !$user->{lastmmid};
my(@comments);
push @comments, $_->{mcid} for @{$M2mods};
# Retrieve the remaining data.
my $comments;
{
local $" = ',';
$comments = $self->sqlSelectAllHashref(
'cid',
'comments.cid, comments.sid as sid, date,
subject, discussions.sid as discussions_sid,
comment,comments.uid,pid, reason, sig, title, nickname',
'comments, comment_text, discussions, users',
"comments.cid in (@comments)
AND comments.cid=comment_text.cid
AND comments.uid=users.uid
AND discussions.id=comments.sid"
) if @comments;
}
for my $m2Mod (@{$M2mods}) {
while (my($key, $val) = each %{$comments->{$m2Mod->{mcid}}}) {
$m2Mod->{$key} = $val;
# Anonymize comment identity a bit for fairness.
$m2Mod->{$key} = '-' if $key eq 'nickname';
$m2Mod->{$key} = getCurrentStatic(
'anonymous_coward_uid'
) if $key eq 'uid';
$m2Mod->{$key} = '0' if $key eq 'points';
# No longer anonymizing sig.
#$m2Mod->{$key} = '' if $key eq 'sig';
}
# We also need to provide the url, but the question is,
# where to link to?
if ($m2Mod->{discussions_sid}) {
# This is a comment posted to a story discussion, so
# we can link straight to the story, providing even
# more context for this comment.
$m2Mod->{url} = getCurrentStatic('rootdir')
. "/article.pl?sid=$m2Mod->{discussions_sid}";
} else {
# This is a comment posted to a discussion that isn't
# a story. It could be attached to a poll, a journal
# entry, or nothing at all (user-created discussion).
# Whatever the case, we can't trust the url field, so
# we should just link to the discussion itself.
$m2Mod->{url} = getCurrentStatic('rootdir')
. "/comments.pl?sid=$m2Mod->{sid}";
}
$m2Mod->{no_moderation} = 1;
delete $m2Mod->{mcid};
}
#my $t1 = new Benchmark;
#printf STDERR "M2 Time: %s\n",
# Benchmark::timestr(Benchmark::timediff($t1, $t0), 'noc');
# format in the template instead
# formatDate($M2mods);
return $M2mods
}
########################################################
# ok, I was tired of trying to mould getDescriptions into
# taking more args.
sub getTemplateList {
my($self, $section, $page) = @_;
my $templatelist = {};
my $where = "seclev <= " . getCurrentUser('seclev');
$where .= " AND section = '$section'" if $section;
$where .= " AND page = '$page'" if $page;
my $templates = $self->sqlSelectMany('tpid,name', 'templates', $where);
while (my($tpid, $name) = $templates->fetchrow) {
$templatelist->{$tpid} = $name;
}
return $templatelist;
}
########################################################
sub getModeratorCommentLog {
my($self, $asc_desc, $limit, $type, $value) = @_;
$asc_desc ||= 'ASC';
$asc_desc = uc $asc_desc;
$asc_desc = 'ASC' if $asc_desc ne 'DESC';
if ($limit and $limit =~ /^(\d+)$/) {
$limit = "LIMIT $1";
} else {
$limit = "";
}
my $vq = $self->sqlQuote($value);
my $where_clause = "";
if ($type eq 'uid') { $where_clause = "moderatorlog.uid=$vq AND comments.uid=users.uid" }
elsif ($type eq 'cid') { $where_clause = "moderatorlog.cid=$vq AND moderatorlog.uid=users.uid" }
elsif ($type eq 'cuid') { $where_clause = "moderatorlog.cuid=$vq AND moderatorlog.uid=users.uid" }
elsif ($type eq 'subnetid') { $where_clause = "comments.subnetid=$vq AND moderatorlog.uid=users.uid" }
elsif ($type eq 'ipid') { $where_clause = "comments.ipid=$vq AND moderatorlog.uid=users.uid" }
return [ ] unless $where_clause;
my $comments = $self->sqlSelectMany("comments.sid AS sid,
comments.cid AS cid,
comments.points AS score,
users.uid AS uid,
users.nickname AS nickname,
moderatorlog.val AS val,
moderatorlog.reason AS reason,
moderatorlog.ts AS ts,
moderatorlog.active AS active",
"moderatorlog, users, comments",
"$where_clause
AND moderatorlog.cid=comments.cid",
"ORDER BY ts $asc_desc",
$limit
);
my(@comments, $comment);
push @comments, $comment while ($comment = $comments->fetchrow_hashref);
return \@comments;
}
########################################################
sub getModeratorLogID {
my($self, $cid, $uid) = @_;
# We no longer need the SID as CID is now unique.
my($mid) = $self->sqlSelect(
"id", "moderatorlog", "uid=$uid and cid=$cid"
);
return $mid;
}
########################################################
sub undoModeration {
my($self, $uid, $sid) = @_;
my $constants = getCurrentStatic();
# SID here really refers to discussions.id, NOT stories.sid
my $cursor = $self->sqlSelectMany("cid,val,active,cuid",
"moderatorlog",
"moderatorlog.uid=$uid and moderatorlog.sid=$sid"
);
my $min_score = $constants->{comment_minscore};
my $max_score = $constants->{comment_maxscore};
my $min_karma = $constants->{minkarma};
my $max_karma = $constants->{maxkarma};
my @removed;
while (my($cid, $val, $active, $cuid) = $cursor->fetchrow){
# Make the logic a little plainer
my $adjust = -$val;
$adjust =~ s/^([^+-])/+$1/;
# We undo moderation even for inactive records (but silently for
# inactive ones...). Leave them in the table but inactive, so
# they are still eligible to be metamodded.
$self->sqlUpdate("moderatorlog",
{ active => 0 },
"cid=$cid and uid=$uid"
);
# If moderation wasn't actually performed, we skip ahead one.
next if ! $active;
# Make sure the comment's score falls within the proper boundaries
my $scorelogic = $adjust < 0
? "points > $min_score"
: "points < $max_score";
$self->sqlUpdate(
"comments",
{ -points => "points $adjust" },
"cid=$cid AND $scorelogic"
);
# Restore modded user's karma, again within the proper boundaries
$scorelogic = $adjust < 0
? "karma > $min_karma"
: "karma < $max_karma";
$self->sqlUpdate(
"users_info",
{ -karma => "karma $adjust" },
"uid=$cuid AND $scorelogic"
);
push(@removed, $cid);
}
return \@removed;
}
########################################################
sub deleteSectionTopicsByTopic {
my($self, $tid) = @_;
$self->sqlDo("DELETE FROM section_topics WHERE tid=$tid");
}
########################################################
sub deleteRelatedLink {
my($self, $id) = @_;
$self->sqlDo("DELETE FROM related_links WHERE id=$id");
}
########################################################
sub createSectionTopic {
my($self, $section, $tid) = @_;
$self->sqlDo("INSERT INTO section_topics (section, tid) VALUES ('$section',$tid)");
}
########################################################
sub getSectionExtras {
my($self, $section) = @_;
return unless $section;
my $answer = $self->sqlSelectAll(
'name,value',
'section_extras',
'section=' . $self->sqlQuote($section)
);
return $answer;
}
########################################################
sub setSectionExtras {
my($self, $section, $extras) = @_;
return unless $section;
$self->sqlDo('DELETE FROM section_extras
WHERE section=' . $self->sqlQuote($section)
);
for (@{$extras}) {
$self->sqlInsert('section_extras', {
section => $section,
name => $_->[0],
value => $_->[1],
});
}
}
########################################################
sub getContentFilters {
my($self, $formname, $field) = @_;
my $field_string = $field ne '' ? " AND field = '$field'" : " AND field != ''";
my $filters = $self->sqlSelectAll("*", "content_filters",
"regex != '' $field_string and form = '$formname'");
return $filters;
}
########################################################
sub createPollVoter {
my($self, $qid, $aid) = @_;
my $qid_quoted = $self->sqlQuote($qid);
my $aid_quoted = $self->sqlQuote($aid);
$self->sqlInsert("pollvoters", {
qid => $qid,
id => md5_hex($ENV{REMOTE_ADDR} . $ENV{HTTP_X_FORWARDED_FOR}),
-'time' => 'now()',
uid => $ENV{SLASH_USER}
});
$self->sqlDo("UPDATE pollquestions SET voters=voters+1
WHERE qid=$qid_quoted");
$self->sqlDo("UPDATE pollanswers SET votes=votes+1
WHERE qid=$qid_quoted AND aid=$aid_quoted");
}
########################################################
sub createSubmission {
my($self, $submission) = @_;
return unless $submission;
$submission->{ipid} = getCurrentUser('ipid');
$submission->{subnetid} = getCurrentUser('subnetid');
$submission->{email} ||= '';
my($sec, $min, $hour, $mday, $mon, $year) = localtime;
my $subid = "$hour$min$sec.$mon$mday$year";
$submission->{'-time'} = 'now()';
$submission->{'subid'} = $subid;
$self->sqlInsert('submissions', $submission);
return $subid;
}
#################################################################
sub getStoryDiscussions {
my($self, $section, $limit, $start) = @_;
$limit ||= 50; # Sanity check in case var is gone
$start ||= 0; # Sanity check in case var is gone
my $tables = "discussions, stories",
my $where = "displaystatus != -1 AND discussions.sid=stories.sid AND time <= NOW() AND writestatus != 'delete' AND writestatus != 'archived'";
if ($section) {
$where .= " AND discussions.section = '$section'"
} else {
$tables .= ", sections";
$where .= " AND sections.section = discussions.section AND sections.isolate != 1 ";
}
my $discussion = $self->sqlSelectAll("discussions.sid, discussions.title, discussions.url",
$tables,
$where,
"ORDER BY time DESC LIMIT $start, $limit"
);
return $discussion;
}
#################################################################
# Less then 2, ince 2 would be a read only discussion
sub getDiscussions {
my($self, $section, $limit, $start) = @_;
$limit ||= 50; # Sanity check in case var is gone
$start ||= 0; # Sanity check in case var is gone
my $tables = "discussions";
my $where = "type != 'archived' AND ts <= now()";
if ($section) {
$where .= " AND discussions.section = '$section'"
} else {
$tables .= ", sections";
$where .= " AND sections.section = discussions.section AND sections.isolate != 1 ";
}
my $discussion = $self->sqlSelectAll("discussions.id, discussions.title, discussions.url",
$tables,
$where,
"ORDER BY ts DESC LIMIT $start, $limit"
);
return $discussion;
}
#################################################################
# Less then 2, ince 2 would be a read only discussion
sub getDiscussionsByCreator {
my($self, $section, $uid, $limit, $start) = @_;
return unless $uid;
$limit ||= 50; # Sanity check in case var is gone
$start ||= 0; # Sanity check in case var is gone
my $tables = "discussions";
my $where = "type != 'archived' AND ts <= now() AND uid = $uid";
if ($section) {
$where .= " AND discussions.section = '$section'"
} else {
$tables .= ", sections";
$where .= " AND sections.section = discussions.section AND sections.isolate != 1 ";
}
my $discussion = $self->sqlSelectAll("id, title, url",
$tables,
$where,
"ORDER BY ts DESC LIMIT $start, $limit"
);
return $discussion;
}
#################################################################
sub getDiscussionsUserCreated {
my($self, $section, $limit, $start, $all) = @_;
$limit ||= 50; # Sanity check in case var is gone
$start ||= 0; # Sanity check in case var is gone
my $tables = "discussions, users";
my $where = "type = 'recycle' AND ts <= now() AND users.uid = discussions.uid";
$where .= " AND section = '$section'"
if $section;
$where .= " AND commentcount > 0"
unless $all;
if ($section) {
$where .= " AND discussions.section = '$section'"
} else {
$tables .= ", sections";
$where .= " AND sections.section = discussions.section AND sections.isolate != 1 ";
}
my $discussion = $self->sqlSelectAll("discussions.id, discussions.title, discussions.ts, users.nickname",
$tables,
$where,
"ORDER BY ts DESC LIMIT $start, $limit"
);
return $discussion;
}
########################################################
# Handles admin logins (checks the sessions table for a cookie that
# matches). Called during authentication
sub getSessionInstance {
my($self, $uid, $session_in) = @_;
my $admin_timeout = getCurrentStatic('admin_timeout');
my $session_out = '';
if ($session_in) {
# CHANGE DATE_ FUNCTION
$self->sqlDo("DELETE from sessions WHERE now() > DATE_ADD(lasttime, INTERVAL $admin_timeout MINUTE)");
my $session_in_q = $self->sqlQuote($session_in);
my($uid) = $self->sqlSelect(
'uid',
'sessions',
"session=$session_in_q"
);
if ($uid) {
$self->sqlDo("DELETE from sessions WHERE uid = '$uid' AND " .
"session != $session_in_q"
);
$self->sqlUpdate('sessions', {-lasttime => 'now()'},
"session = $session_in_q"
);
$session_out = $session_in;
}
}
if (!$session_out) {
my($title, $last_sid, $last_subid) = $self->sqlSelect(
'lasttitle, last_sid, last_subid',
'sessions',
"uid=$uid"
);
$_ ||= '' for ($title, $last_sid, $last_subid);
# Why not have sessions have UID be unique and use
# sqlReplace() here? Minor quibble, just curious.
# - Cliff
$self->sqlDo("DELETE FROM sessions WHERE uid=$uid");
$self->sqlInsert('sessions', { -uid => $uid,
-logintime => 'now()',
-lasttime => 'now()',
lasttitle => $title,
last_sid => $last_sid,
last_subid => $last_subid
});
$session_out = $self->getLastInsertId('sessions', 'session');
}
return $session_out;
}
########################################################
sub setContentFilter {
my($self, $formname) = @_;
my $form = getCurrentForm();
$formname ||= $form->{formname};
$self->sqlUpdate("content_filters", {
regex => $form->{regex},
form => $formname,
modifier => $form->{modifier},
field => $form->{field},
ratio => $form->{ratio},
minimum_match => $form->{minimum_match},
minimum_length => $form->{minimum_length},
err_message => $form->{err_message},
}, "filter_id=$form->{filter_id}"
);
}
########################################################
# Only Slashdot uses this method
# not for long... - Cliff
sub setSectionExtra {
my($self, $full, $story) = @_;
if ($full && $self->sqlTableExists($story->{section}) && $story->{section}) {
my $extra = $self->sqlSelectHashref('*', $story->{section}, "sid='$story->{sid}'");
for (keys %$extra) {
$story->{$_} = $extra->{$_};
}
}
}
########################################################
# This creates an entry in the accesslog
sub createAccessLog {
my($self, $op, $dat) = @_;
my $constants = getCurrentStatic();
my $form = getCurrentForm();
my $r = Apache->request;
my $hostip = $r->connection->remote_ip;
my $uid;
if ($ENV{SLASH_USER}) {
$uid = $ENV{SLASH_USER};
} else {
$uid = $constants->{anonymous_coward_uid};
}
my $section = $form->{section} ? $form->{section} : 'index';
my $ipid = getCurrentUser('ipid') || md5_hex($hostip);
$hostip =~ s/^(\d+\.\d+\.\d+)\.\d+$/$1.0/;
$hostip = md5_hex($hostip);
my $subnetid = getCurrentUser('subnetid') || $hostip;
if ($dat =~ /.*(\d{2}\/\d{2}\/\d{2}\/\d{4,7}).*/) {
$dat = $1;
# $self->sqlUpdate('stories', { -hits => 'hits+1' },
# 'sid=' . $self->sqlQuote($dat)
# );
}
$self->sqlInsert('accesslog', {
host_addr => $ipid,
subnetid => $subnetid,
dat => $dat,
uid => $uid,
section => $section,
op => $op,
-ts => 'NOW()',
query_string => $ENV{QUERY_STRING} || '0',
user_agent => $ENV{HTTP_USER_AGENT} || '0',
}, 1);
=pod
CREATE TABLE palmlog (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
ts DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL,
HTTP_USER_AGENT VARCHAR(254) DEFAULT '' NOT NULL,
REQUEST_URI VARCHAR(254) DEFAULT '' NOT NULL,
REMOTE_ADDR VARCHAR(15) DEFAULT '' NOT NULL,
HTTP_X_FORWARDED_FOR VARCHAR(15) DEFAULT '' NOT NULL,
HTTP_VIA VARCHAR(254) DEFAULT '' NOT NULL,
HTTP_FORWARDED VARCHAR(254) DEFAULT '' NOT NULL,
HTTP_X_AVANTGO_VERSION VARCHAR(10) DEFAULT '' NOT NULL,
HTTP_X_AVANTGO_DEVICEOS VARCHAR(254) DEFAULT '' NOT NULL,
HTTP_X_AVANTGO_DEVICEID VARCHAR(254) DEFAULT '' NOT NULL,
HTTP_X_AVANTGO_USERID VARCHAR(254) DEFAULT '' NOT NULL,
HTTP_X_AVANTGO_CLIENTLANGUAGE VARCHAR(10) DEFAULT '' NOT NULL,
HTTP_X_AVANTGO_SCREENSIZE VARCHAR(15) DEFAULT '' NOT NULL,
HTTP_X_AVANTGO_COLORDEPTH VARCHAR(10) DEFAULT '' NOT NULL,
INDEX ts (ts),
INDEX REMOTE_ADDR (REMOTE_ADDR),
INDEX HTTP_X_AVANTGO_DEVICEID (HTTP_X_AVANTGO_DEVICEID),
INDEX HTTP_X_AVANTGO_USERID (HTTP_X_AVANTGO_USERID),
INDEX HTTP_VIA (HTTP_VIA),
INDEX HTTP_X_FORWARDED_FOR (HTTP_X_FORWARDED_FOR),
INDEX HTTP_FORWARDED (HTTP_FORWARDED),
PRIMARY KEY (id)
) TYPE = myisam;
=cut
# this is temporary -- pudge
if ($constants->{palmlog} && $op eq 'palm') {
my @direct = qw(
HTTP_USER_AGENT
REQUEST_URI
REMOTE_ADDR
HTTP_VIA
HTTP_X_FORWARDED_FOR
HTTP_FORWARDED
HTTP_X_AVANTGO_VERSION
);
my @base64 = qw(
HTTP_X_AVANTGO_DEVICEOS
HTTP_X_AVANTGO_DEVICEID
HTTP_X_AVANTGO_USERID
HTTP_X_AVANTGO_CLIENTLANGUAGE
HTTP_X_AVANTGO_SCREENSIZE
HTTP_X_AVANTGO_COLORDEPTH
);
my %palmlog = (
-ts => 'NOW()',
);
@palmlog{@direct} = map { defined $_ ? $_ : '' } @ENV{@direct};
@palmlog{@base64} = map { decode_base64($_) } @ENV{@base64};
$self->sqlInsert('palmlog', \%palmlog, 1);
}
}
########################################################
# pass in additional optional descriptions
sub getDescriptions {
my($self, $codetype, $optional, $flag, $altdescs) = @_;
return unless $codetype;
my $codeBank_hash_ref = {};