-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandgranat.pm
More file actions
6433 lines (5669 loc) · 192 KB
/
Handgranat.pm
File metadata and controls
6433 lines (5669 loc) · 192 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
#!/usr/bin/perl -I /sw/lib/perl5/5.8.6/darwin-thread-multi-2level/
# handgranat version 1.6.5 (pre) $Id: Handgranat.pm,v 1.28 2008/10/22 20:34:25 kers Exp $
# $Date: 2008/10/22 20:34:25 $ $Author: kers $
# hgr is made by popular computer scientists working for the good of the people
# based on oddmuse and
# UseModWiki version 1.0 (September 12, 2003)
# Copyright (C) 2000, 2001, 2002, 2003 Clifford A. Adams <caadams@usemod.com>
# Copyright (C) 2002, 2003 Sunir Shah <sunir@sunir.org>
# Based on the GPLed AtisWiki 0.3 (C) 1998 Markus Denke
# <marcus@ira.uka.de>
# ...which was based on
# the LGPLed CVWiki CVS-patches (C) 1997 Peter Merel
# and The Original WikiWikiWeb (C) Ward Cunningham
# <ward@c2.com> (code reused with permission)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
package Handgranat;
use strict;
use DBI;
use MIME::QuotedPrint;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use Handgranat::Karameller;
use Handgranat::HTML;
use Handgranat::DB ':all';
require Exporter;
# use AutoLoader qw(AUTOLOAD);
our @ISA = qw(Exporter);
local $| = 1; # Do not buffer output (localized for mod_perl)
# Configuration/constant variables:
use vars qw(@RcDays @HtmlPairs @HtmlSingle
$TempDir $LockDir $DataDir $HtmlDir $UserDir $KeepDir $PageDir
$InterFile $RcFile $RcOldFile $IndexFile $FullUrl $SiteName $HomePage
$LogoUrl $RcDefault $IndentLimit $RecentTop $EditAllowed $UseDiff
$UseSubpage $RawHtml $SimpleLinks $NonEnglish $LogoLeft
$KeepDays $HtmlTags $HtmlLinks $UseDiffLog $KeepMajor $KeepAuthor
$FreeUpper $FastGlob $EmbedWiki
$ScriptTZ $BracketText $UseAmPm $UseConfig $UseIndex $UseLookup
$AdminPass $EditPass $UseHeadings $NetworkFile $BracketWiki
$FreeLinks $AdminDelete $FreeLinkPattern $RCName
$ShowEdits $LinkPattern $InterLinkPattern $InterSitePattern
$UrlProtocols $UrlPattern $ImageExtensions $RFCPattern
$FS $FS1 $FS2 $FS3 $CookieName $SiteBase $NotFoundPg
$MaxPost $NewText $NotifyDefault $HttpCharset
$DeletedPage $ReplaceFile @ReplaceableFiles $TableSyntax
$MetaKeywords $NamedAnchors $InterWikiMoniker $SiteDescription $RssLogoUrl
$NumberDates $EarlyRules $LateRules $NewFS $KeepSize $SlashLinks $BGColor
$UpperFirst $AdminBar $RepInterMap $DiffColor1 $DiffColor2 $ConfirmDel
$MaskHosts $LockCrash $HistoryEdit
$FavIcon $RssDays
$StartUID $AuthorFooter $UseUpload $AllUpload
$LimitFileUrl $MaintTrimRc $SearchButton
$EditNameLink $UseMetaWiki @ImageSites $BracketImg $HostUrl $HostName);
# Note: $NotifyDefault is kept because it was a config variable in 0.90
# Other global variables:
use vars qw(%Page %Section %Text %InterSite %SaveUrl %SaveNumUrl $WeekNo
%KeptRevisions %UserCookie %SetCookie %UserData %IndexHash %Translate
%LinkIndex $InterSiteInit $SaveUrlIndex $SaveNumUrlIndex $MainPage
$OpenPageName @KeptList @IndexList $IndexInit $TableMode $GlobalId
$Now $UserID $TimeZoneOffset $ScriptName $BrowseCode $OtherCode
$AnchoredLinkPattern @HeadingNumbers $TableOfContents $QuotedFullUrl
$ConfigError $UploadPattern $Rot13Mode $ConfigFile);
# == Configuration =====================================================
my $VERSION = sprintf( "%d.%02d", q$Revision: 1.28 $ =~ /(\d+)\.(\d+)/ );
my $ID_VER =
sprintf( "%s.%s", q$Id: Handgranat.pm,v 1.28 2008/10/22 20:34:25 kers Exp $ );
# the following should be defined in external $ConfigFile:
use vars qw($dbh $use_database $mysql_host $mysql_db $mysql_user $mysql_pass);
# end of stuff from $ConfigFile
$UseConfig = 1; # 1 = use config file, 0 = do not look for config
$ConfigFile = 'tjo.pl';
# Default configuration (used if UseConfig is 0)
#$FullUrl = "http://handgranat.org/"; # Set if the auto-detected URL is wrong
$HostName = $ENV{'SERVER_NAME'};
if ( $HostName eq '' ) { $HostName = 'handgranat.org'; }
$FullUrl = "http://" . $HostName . "/";
$HostUrl = "http://" . $HostName . "/";
$NotFoundPg = ""; #- Page for not-found links ("" for blank pg)
#- Should be moved to external config and be documented /Kers 080817 (TODO)
$MaxPost = 1024 * 210; #- Maximum 210K posts (about 200K for pages)
$NewText = ""; #- New page text ("" for default message)
#- Should be moved to external config and be documented /Kers 080817 (TODO)
$HttpCharset = "iso-8859-1"; #- Charset for pages, like "iso-8859-2"
$InterWikiMoniker = ''; #- InterWiki moniker for this wiki. (for RSS)
#- Should be moved to external config and be documented /Kers 080817 (TODO)
$SiteDescription = $SiteName; #- Description of this wiki. (for RSS)
#- Should be moved to external config and be documented /Kers 080817 (TODO)
$RssLogoUrl = ''; #- Optional image for RSS feed
#- Should be moved to external config and be documented /Kers 080817 (TODO)
# $EarlyRules and $LateRules are perl code that is evaled and can work with the node content ($text)
# before and after the system has worked it's magic. Sould not be used except in an emergancy.
# $LateRules .= <<EOLR;
# $text =~ s#\(:([^:]*):\)#<b>$1</b>#g;
# $origText .= "<hr>smile-bold done<hr>"; # FIXME: debug
# EOLR
$EarlyRules = ''; #- Local syntax rules for wiki->html (evaled)
$LateRules = ''; #- Local syntax rules for wiki->html (evaled)
$KeepSize = 0; #- If non-zero, maximum size of keep file
$BGColor = ''; #- Background color ('' to disable)
$DiffColor1 = '#ffffaf'; #- Background color of old/deleted text
$DiffColor2 = '#cfffcf'; #- Background color of new/added text
$FavIcon =
$HostUrl . 'ikoner/handikon.png'; #- URL of bookmark/favorites icon, or ''
$RssDays = 7; #- Default number of days in RSS feed
$StartUID = 1001; #- Starting number for user IDs
@ImageSites = qw (); #- Url prefixes of good image sites: ()=all
# ^^ is this one needed ? --Kers 080817
# no, we trust all! welcome goatse! let's remove it
# Major options:
$UseSubpage = 1; #- 1 = use subpages, 0 = do not use subpages
$EditAllowed = 1; #- 1 = editing allowed, 0 = read-only
## RawHtml & HtmlTags should really be removed and replaced with a good filtering system
$RawHtml = 0; #- 1 = allow <HTML> tag, 0 = no raw HTML in pages
$HtmlTags = 0; #- 1 = "unsafe" HTML tags, 0 = only minimal tags
$UseDiff = 1; #- 1 = use diff features, 0 = do not use diff
$FreeLinks = 1; #- 1 = use [[word]] links, 0 = LinkPattern only
$AdminDelete = 1; #- 1 = Admin only deletes, 0 = Editor can delete
$EmbedWiki = 0; # 1 = no headers/footers, 0 = normal wiki pages
$DeletedPage = 'DeletedPage'; # 0 = disable, 'PageName' = tag to delete page
$ReplaceFile = 'ReplaceFile'; # 0 = disable, 'PageName' = indicator tag
@ReplaceableFiles = (); # List of allowed server files to replace
$TableSyntax = 1; # 1 = wiki syntax tables, 0 = no table syntax
$NewFS = 0; # 1 = new multibyte $FS, 0 = old $FS
$UseUpload = 0; # 1 = allow uploads, 0 = no uploads
# Minor options:
$LogoLeft = 0; # 1 = logo on left, 0 = logo on right
$RecentTop = 1; # 1 = recent on top, 0 = recent on bottom
$UseDiffLog = 1; # 1 = save diffs to log, 0 = do not save diffs
$KeepMajor = 1; # 1 = keep major rev, 0 = expire all revisions
$KeepAuthor = 1; # 1 = keep author rev, 0 = expire all revisions
$ShowEdits = 1; # 1 = show minor edits, 0 = hide edits by default
$HtmlLinks = 0; # 1 = allow A HREF links, 0 = no raw HTML links
$SimpleLinks = 0; # 1 = only letters, 0 = allow _ and numbers
$NonEnglish = 1; # 1 = extra link chars, 0 = only A-Za-z chars
$BracketText = 1; # 1 = allow [URL text], 0 = no link descriptions
$UseAmPm = 0; # 1 = use am/pm in times, 0 = use 24-hour times
$UseIndex = 0; # 1 = use index file, 0 = slow/reliable method
$UseHeadings = 1; # 1 = allow = h1 text =, 0 = no header formatting
$NetworkFile = 1; # 1 = allow remote file:, 0 = no file:// links
$BracketWiki = 0; # 1 = [WikiLnk txt] link, 0 = no local descriptions
$UseLookup = 1; # 1 = lookup host names, 0 = skip lookup (IP only)
$FreeUpper = 1; # 1 = force upper case, 0 = do not force case
$FastGlob = 1; # 1 = new faster code, 0 = old compatible code
$MetaKeywords = 0; # 1 = Google-friendly, 0 = search-engine averse
$NamedAnchors = 1; # 0 = no anchors, 1 = enable anchors,
# 2 = enable but suppress display
$SlashLinks = 0; # 1 = use script/ioncat links, 0 = script?ioncat
$UpperFirst = 1; # 1 = free links start uppercase, 0 = no ucfirst
$AdminBar = 1; # 1 = admins see admin links, 0 = no admin bar
$RepInterMap = 0; # 1 = intermap is replacable, 0 = not replacable
$ConfirmDel = 1; # 1 = delete link confirm page, 0 = immediate delete
$MaskHosts = 0; # 1 = mask hosts/IPs, 0 = no masking
$LockCrash = 0; # 1 = crash if lock stuck, 0 = auto clear locks
$HistoryEdit = 0; # 1 = edit links on history page, 0 = no edit links
$NumberDates = 0; # 1 = 2003-6-17 dates, 0 = June 17, 2003 dates
$AuthorFooter = 1; # 1 = show last author in footer, 0 = do not show
$AllUpload = 0; # 1 = anyone can upload, 0 = only editor/admins
$LimitFileUrl = 1; # 1 = limited use of file: URLs, 0 = no limits
$MaintTrimRc = 0; # 1 = maintain ioncat trims RC, 0 = only maintainrc
$SearchButton = 0; # 1 = search button on page, 0 = old behavior
$EditNameLink = 0; # 1 = edit links use name (CSS), 0 = '?' links
$UseMetaWiki = 0; # 1 = add MetaWiki search links, 0 = no MW links
$BracketImg = 1; # 1 = [url url.gif] becomes image link, 0 = no img
#do "/home/handgranat/blendas_huvud/translation.pl";
#do "translation.pl";
$WeekNo = int( ( localtime( time() ) )[7] / 7 ) + 2;
# Names of sites. (The first entry is used for the number link.)
# HTML tag lists, enabled if $HtmlTags is set.
# Scripting is currently possible with these tags,
# so they are *not* particularly "safe".
# Tags that must be in <tag> ... </tag> pairs:
@HtmlPairs = qw(b i u font big small sub sup h1 h2 h3 h4 h5 h6 cite code
em s strike strong tt var div center blockquote ol ul dl table caption);
# Single tags (that do not require a closing /tag)
@HtmlSingle = qw(br p hr li dt dd tr td th);
@HtmlPairs = ( @HtmlPairs, @HtmlSingle ); # All singles can also be pairs
if ($UseConfig) {
# die "kan inte ladda $! ($ConfigFile)" unless do "$ConfigFile";
my $return;
unless ( $return = do $ConfigFile ) {
die "couldn't parse $ConfigFile: $@" if $@;
die "couldn't do $ConfigFile: $!" unless defined $return;
die "couldn't run $ConfigFile" unless $return;
}
}
# == You should not have to change anything below this line. =============
$IndentLimit = 20; # Maximum depth of nested lists
$PageDir = "$DataDir/page"; # Stores page data
$HtmlDir = "$DataDir/html"; # Stores HTML versions
$UserDir = "$DataDir/user"; # Stores user data
$KeepDir = "$DataDir/keep"; # Stores kept (old) page data
$TempDir = "$DataDir/temp"; # Temporary files and locks
$LockDir = "$TempDir/lock"; # DB is locked if this exists
$InterFile = "$DataDir/intermap"; # Interwiki site->url map
$RcFile = "$DataDir/rclog"; # New RecentChanges logfile
$RcOldFile = "$DataDir/oldrclog"; # Old RecentChanges logfile
$IndexFile = "$DataDir/pageidx"; # List of all pages
my $mysql_connected = 0;
if ($RepInterMap) {
push @ReplaceableFiles, $InterFile;
}
#die "kan inte lada $@" unless do "/home/kers/Kod/handgranat/www/tjo.pl";
#die $DataDir;
# The "main" program, called at the end of this script file.
sub DoWikiRequest {
if ( $UseConfig && ( -f $ConfigFile ) ) {
$ConfigError = '';
if ( !do $ConfigFile ) { # Some error occurred
$ConfigError = $@;
if ( $ConfigError eq '' ) {
# Unfortunately, if the last expr returns 0, one will get a false
# error above. To remain compatible with existing installs the
# wiki must not report an error unless there is error text in $@.
# (Errors in "use strict" may not have error text.)
# Uncomment the line below if you want to catch use strict errors.
# $ConfigError = T('Unknown Error (no error text)');
}
}
}
&InitLinkPatterns();
eval $BrowseCode;
&InitRequest() or return;
if ( !&DoBrowseRequest() ) {
eval $OtherCode;
&DoOtherRequest();
}
}
# == Common and cache-browsing code ====================================
sub InitLinkPatterns {
my ( $UpperLetter, $LowerLetter, $AnyLetter, $LpA, $LpB, $QDelim );
# Field separators are used in the URL-style patterns below.
if ($NewFS) {
$FS = "\x1e\xff\xfe\x1e"; # An unlikely sequence for any charset
}
else {
$FS = "\xb3"; # The FS character is a superscript "3"
}
$FS1 = $FS . "1"; # The FS values are used to separate fields
$FS2 = $FS . "2"; # in stored hashtables and other data structures.
$FS3 = $FS . "3"; # The FS character is not allowed in user data.
$UpperLetter = "[A-Z";
$LowerLetter = "[a-z";
$AnyLetter = "[A-Za-z";
if ($NonEnglish) {
$UpperLetter .= "\xc0-\xde";
$LowerLetter .= "\xdf-\xff";
if ($NewFS) {
$AnyLetter .= "\x80-\xff";
}
else {
$AnyLetter .= "\xc0-\xff";
}
}
if ( !$SimpleLinks ) {
$AnyLetter .= "_0-9";
}
$UpperLetter .= "]";
$LowerLetter .= "]";
$AnyLetter .= "]";
# Main link pattern: lowercase between uppercase, then anything
$LpA =
$UpperLetter . "+" . $LowerLetter . "+" . $UpperLetter . $AnyLetter . "*";
# Optional subpage link pattern: uppercase, lowercase, then anything
$LpB = $UpperLetter . "+" . $LowerLetter . "+" . $AnyLetter . "*";
if ($UseSubpage) {
# Loose pattern: If subpage is used, subpage may be simple name
#$LinkPattern = "((?:(?:$LpA)?\\/$LpB)|$LpA)";
# Strict pattern: both sides must be the main LinkPattern
$LinkPattern = "((?:(?:$LpA)?(\\/| - ))?$LpA)";
}
else {
$LinkPattern = "($LpA)";
}
$QDelim = '(?:"")?'; # Optional quote delimiter (not in output)
$AnchoredLinkPattern = $LinkPattern . '#(\\w+)' . $QDelim if $NamedAnchors;
$LinkPattern .= $QDelim;
# Inter-site convention: sites must start with uppercase letter
# (Uppercase letter avoids confusion with URLs)
$InterSitePattern = $UpperLetter . $AnyLetter . "+";
$InterLinkPattern = "((?:$InterSitePattern:[^\\]\\s\"<>$FS]+)$QDelim)";
if ($FreeLinks) {
# Note: the - character must be first in $AnyLetter definition
if ($NonEnglish) {
$AnyLetter = "[-,.()/' _0-9A-Za-z\xc0-\xff]";
}
else {
$AnyLetter = "[-,.()' _0-9A-Za-z]";
}
}
$FreeLinkPattern = "($AnyLetter+)";
$FreeLinkPattern .= $QDelim;
# Url-style links are delimited by one of:
# 1. Whitespace (kept in output)
# 2. Left or right angle-bracket (< or >) (kept in output)
# 3. Right square-bracket (]) (kept in output)
# 4. A single double-quote (") (kept in output)
# 5. A $FS (field separator) character (kept in output)
# 6. A double double-quote ("") (removed from output)
$UrlProtocols = "http|https|ftp|mms|afs|news|nntp|mid|cid|mailto|wais|"
. "prospero|telnet|gopher|xmpp";
$UrlProtocols .= '|file' if ( $NetworkFile || !$LimitFileUrl );
$UrlPattern = "((?:(?:$UrlProtocols):[^\\]\\s\"<>$FS]+)$QDelim)";
$ImageExtensions = "(gif|jpg|png|bmp|jpeg)";
# $ImagePattern = "(http:|https:|ftp:).+\.$ImageExtensions$";
$RFCPattern = "RFC\\s?(\\d+)";
$UploadPattern = "upload:([^\\]\\s\"<>$FS]+)$QDelim";
}
sub GetPageDirectory {
my ($id) = @_;
if ( $id =~ /^([a-zA-Z])/ ) {
return uc($1);
}
return "other";
}
sub T {
my ($text) = @_;
if ( defined( $Translate{$text} ) && ( $Translate{$text} ne '' ) ) {
return $Translate{$text};
}
return $text;
}
sub Ts {
my ( $text, $string ) = @_;
$text = T($text);
$text =~ s/\%s/$string/;
return $text;
}
sub Tss {
my $text = @_[0];
$text = T($text);
$text =~ s/\%([1-9])/$_[$1]/ge;
return $text;
}
# == Normal page-browsing and RecentChanges code =======================
$BrowseCode = ""; # Comment next line to always compile (slower)
#$BrowseCode = <<'#END_OF_BROWSE_CODE';
sub InitRequest {
my @ScriptPath = split( '/', "$ENV{SCRIPT_NAME}" );
$CGI::POST_MAX = $MaxPost;
$CGI::DISABLE_UPLOADS = 1; # no uploads
# Fix some issues with editing UTF8 pages (if charset specified)
if ( $HttpCharset ne '' ) {
$q->charset($HttpCharset);
}
$Now = time; # Reset in case script is persistent
# $ScriptName = pop(@ScriptPath); # Name used in links
# $ScriptName = "http://handgranat.org/";
$ScriptName = "http://" . $HostName . "/";
$IndexInit = 0; # Must be reset for each request
$InterSiteInit = 0;
%InterSite = ();
$MainPage = "."; # For subpages only, the name of the top-level page
$OpenPageName = ""; # Currently open page
&CreateDir($DataDir); # Create directory if it doesn't exist
if ( !-d $DataDir ) {
&ReportError( Ts( 'Could not create %s', $DataDir ) . ": $!" );
return 0;
}
&InitCookie(); # Reads in user data
return 1;
}
sub InitCookie {
%SetCookie = ();
$TimeZoneOffset = 0;
undef $q->{'.cookies'}; # Clear cache if it exists (for SpeedyCGI)
%UserData = (); # Fix for persistent environments.
%UserCookie = $q->cookie($CookieName);
$UserID = $UserCookie{'id'};
$UserID =~ s/\D//g; # Numeric only
if ( $UserID < 200 ) {
$UserID = 111;
}
else {
&LoadUserData($UserID);
}
if ( $UserID > 199 ) {
if ( ( $UserData{'id'} != $UserCookie{'id'} )
|| ( $UserData{'randkey'} != $UserCookie{'randkey'} ) )
{
$UserID = 113;
%UserData = (); # Invalid. Consider warning message.
}
}
if ( $UserData{'tzoffset'} != 0 ) {
$TimeZoneOffset = $UserData{'tzoffset'} * ( 60 * 60 );
}
}
sub avutf {
my $id = shift;
# iebuggen kom hit
$id =~ 's/Ã¥/å/g';
$id =~ 's/ä/ä/g';
$id =~ 's/ö/ö/g';
$id =~ 's/%C3%A5/%E5/g';
$id =~ 's/%C3%A4/%E4/g';
$id =~ 's/%C3%B6/%F6/g';
return $id;
}
sub DoBrowseRequest {
my ( $id, $ioncat, $text );
if ( !$q->param ) { # No parameter
&BrowsePage($HomePage);
return 1;
}
$id = &GetParam( 'keywords', '' );
$id =~ s/\/$//;
if ($id) { # Just script?PageName
$id = avutf($id);
if ( $FreeLinks && ( !-f &GetPageFile($id) ) ) {
$id = &FreeToNormal($id);
}
if ( $id =~ /^[^\/]+\/Blog*\/Atom$/i ) {
&BrowsePage( $id, 4 );
return 1;
}
if ( $id =~ /^[^\/]+\/Allt$/i ) {
# &BrowsePage($id, 4);
print "Content-Type: text/html\n\n"; # Needed for browser failure
my $content = '';
my $mz = $id;
$mz =~ s/\/.*//;
my $num = 10000;
$MainPage = 'Allt om ' . $mz;
&TheHeader( "Allt om " . $mz );
$content .= &printDiaryLinks('10000 Blenda/[^2].*');
my $text;
my $regex = $mz;
my @pages = ( grep( /$regex/, AllPagesList() ) ); #Dödsstraff?
@pages = sort { $b cmp $a } @pages;
@pages = @pages[ 0 .. $num - 1 ] if $#pages >= $num;
if (@pages) {
# byt till scriptlink
for my $id (@pages) {
$text .= '<li>';
$text .= &ScriptLink( $id, $id );
$text .= '</li>';
}
}
&TheContent($text);
print &GetCommonFooter();
return 1;
}
if ( $id =~ /^[^\/]+\/Blog*$/i ) {
&BrowsePage( $id, 3 );
return 1;
}
#($id =~ /^[^\/]+\/[Vv](qnt|zbetba)$/) ||
if ( $id =~ /^(cbfgn|qvss|tnzyn_irefvbare|erqvtren|uvfgbevn)/i ) {
$Rot13Mode = 1;
$id = RotateThirteen($id);
}
if ( $id =~ /^Diff\/.+/i ) {
$id =~ s/^diff\///i;
&BrowsePage( $id, 5 );
return 1;
}
if ( $id =~ /Nytt_konto/i ) {
$UserID = 0;
&DoEditPrefs(); # Also creates new ID
return 1;
}
if ( $id =~ /Inställningar/i ) {
&DoEditPrefs(); # Also creates new ID
return 1;
}
if ( $id =~ /Inloggning/i ) {
&DoEnterLogin();
return 1;
}
if ( $id =~ /^Gamla_Versioner\/\d+\/.+/i ) {
my $number = $id;
$number =~ s/^Gamla_Versioner\///i;
$number =~ s/\/.*//;
$id =~ s/^Gamla_Versioner\/\d+\///i;
&BrowsePage( $id, ( 1000 + $number ) )
; # magic numbers must die!!! fix this shit!!!
return 1;
}
if ( $id =~ /^Senaste\/[^\/]+$/i ) {
$MainPage = $id;
$MainPage =~ s/^Senaste\///i;
# &ReBrowsePage(&getDiaryRecentId(), $id, 0);
&BrowsePage( &getDiaryRecentId() );
return 1;
}
if ( $id =~ /^[^\/]+\/Idag$/si ) {
my $newid = $id;
$newid =~ s/....$//;
$newid = $newid . &CalcIsoDay($Now);
# &ReBrowsePage($newid, $id, 0);
&BrowsePage($newid);
return 1;
}
if ( $id =~ /changelog/i ) {
&DoChangelog();
return 1;
}
if ( $id =~ /IP_Log/ ) {
&IPLog($id);
return 1;
}
# /i gör regex case-insensitive yo!
if ( ( $id =~ /etikett\/.*/i ) or ( $id =~ /karameller\/.*/i ) ) {
&Etikett($id);
return 1;
}
if ( $id =~ /^[^\/]+\/Imorgon$/i ) {
my $newid = $id;
$newid =~ s/.......$//;
$newid = $newid . &CalcIsoDay( ( $Now + 86400 ) );
&ReBrowsePage( $newid, $id, 0 );
return 1;
}
if ( $id =~ /^Posta\/[^\/]+$/i ) {
my $newid = $id;
$newid =~ s/^Posta\///i;
$newid = $newid . '/' . &CalcIsoDay($Now);
&DoEdit( $newid, 0, 0, "", 0 ) if &ValidIdOrDie($id);
return 1;
}
if ( $id =~ /^Redigera\/.+/i ) {
my $newid = $id;
$newid =~ s/^Redigera\///i;
&DoEdit( $newid, 0, 0, "", 0 ) if &ValidIdOrDie($newid);
return 1;
}
if ( $id =~ /^Historia\/.+/i ) {
my $newid = $id;
$newid =~ s/Historia\///i;
&DoHistory($newid) if &ValidIdOrDie($newid);
return 1;
}
if ( ( $NotFoundPg ne '' ) && ( !-f &GetPageFile($id) ) ) {
$id = $NotFoundPg;
}
&BrowsePage( $id, &GetParam( 'raw', 0 ) ) if &ValidIdOrDie($id);
return 1;
}
$ioncat = lc( &GetParam( 'ioncat', '' ) );
$id = &GetParam( 'id', '' );
$id = avutf($id);
if ( $ioncat eq 'browse' ) {
if ( $FreeLinks && ( !-f &GetPageFile($id) ) ) {
$id = &FreeToNormal($id);
}
if ( ( $NotFoundPg ne '' ) && ( !-f &GetPageFile($id) ) ) {
$id = $NotFoundPg;
}
&BrowsePage( $id, &GetParam( 'raw', 0 ) ) if &ValidIdOrDie($id);
return 1;
}
elsif ( $ioncat eq 'rc' ) {
&BrowsePage($RCName);
return 1;
}
elsif ( $ioncat eq 'random' ) {
&DoRandom();
return 1;
}
elsif ( $ioncat eq 'changelog' ) {
&DoChangelog();
return 1;
}
elsif ( $ioncat eq 'history' ) {
&DoHistory($id) if &ValidIdOrDie($id);
return 1;
}
return 0; # Request not handled
}
sub GetHeadlineOne {
my ( $id, $pageText ) = @_;
if ( ( substr( $pageText, 0, 2 ) ) eq '= ' ) {
($id) = ( $pageText =~ /= (.+) =/ );
}
else {
$id =~
s|/| - |; # OK det aer raett fuckat att denna ligger haer. spaghetti!!
}
return &QuoteHtml($id);
}
sub rprint {
if ($Rot13Mode) {
r13print(@_);
}
else {
print(@_);
}
}
sub BrowsePage {
my ( $id, $raw ) = @_;
my ( $fullHtml, $oldId, $allDiff, $showDiff, $openKept );
my ( $revision, $goodRevision, $diffRevision, $newText );
PageExists($id)
|| (PageExists( RotateThirteen($id) )
&&{ $Rot13Mode = 1 }
&&{ $id = RotateThirteen($id) } );
&OpenPage($id);
&OpenDefaultText();
$GlobalId = $id;
$newText = $Text{'text'}; # For differences
$openKept = 0;
$revision = &GetParam( 'revision', '' );
if ( $raw > 1000 ) {
$revision = ( $raw - 1000 );
$raw = 0;
}
$revision =~ s/\D//g; # Remove non-numeric chars
$goodRevision = $revision; # Non-blank only if exists
if ( $revision ne '' ) {
&OpenKeptRevisions('text_default');
$openKept = 1;
if ( !defined( $KeptRevisions{$revision} ) ) {
$goodRevision = '';
}
else {
&OpenKeptRevision($revision);
}
}
# Handle a single-level redirect
$oldId = &GetParam( 'oldid', '' );
if ( ( $oldId eq '' )
&& ( substr( $Text{'text'}, 0, 10 ) eq '#REDIRECT ' ) )
{
$oldId = $id;
if ( ($FreeLinks) && ( $Text{'text'} =~ /\#REDIRECT\s+\[\[.+\]\]/ ) ) {
($id) = ( $Text{'text'} =~ /\#REDIRECT\s+\[\[(.+)\]\]/ );
$id = &FreeToNormal($id);
}
else {
($id) = ( $Text{'text'} =~ /\#REDIRECT\s+(\S+)/ );
}
if ( &ValidId($id) eq '' ) {
# Consider revision in rebrowse?
&ReBrowsePage( $id, $oldId, 0 );
return;
}
else { # Not a valid target, so continue as normal page
$id = $oldId;
$oldId = '';
}
}
$MainPage = $id;
$MainPage =~ s|/.*||; # Only the main page name (remove subpage)
#raw things
if ( $raw == 3 || $raw == 4 ) {
my $num = 20;
my $entries = "";
my @pages =
( grep( /$MainPage\/\d\d\d\d-\d\d-\d\d/, AllPagesList() ) )
; #Dödsstraff?
@pages = sort { $b cmp $a } @pages;
@pages = @pages[ 0 .. $num - 1 ] if $#pages >= $num;
my $lastdate = 0;
my $author = $MainPage; # maybe change to get from username instead
if (@pages) {
for my $pageid (@pages) {
my $entry;
my $entrytitle;
my $slowtext;
OpenPage($pageid);
OpenDefaultText();
$entry = $Text{'text'};
my @entries = split /^----+\s*$/m, $entry;
$entry = "";
my $date = $pageid;
$date =~ s|^[^/]+/||g;
if ( $lastdate == 0 ) { $lastdate = $date }
if ( $raw == 3 ) {
for my $subentry (@entries) {
$subentry =~ s/^= *([^=]*)=\s*$/== $1 ==/gm; # h1 -> h2
my $countcom = () = $subentry =~ /^\:/gm;
my $count = () = $subentry =~ /\(\:/g;
$count += $countcom;
$subentry =~ s/^\:.*$//gm;
$subentry =~ s/\(\:[^)]*\)//g;
$subentry = &WikiToHTML($subentry);
my $er = "";
if ( $count != 0 ) {
if ( $count != 1 ) {
$er = "er";
}
$slowtext = ", med $count kommentar$er om det här";
}
else {
$slowtext = "";
}
$slowtext =
'<li>'
. $date
. '</li><li>'
. &ScriptLink( $pageid, "Permanent länk${slowtext}." )
. '</li><li>'
. &ScriptLink( "Redigera/$pageid", "Kommentera..." )
. '</li>';
$entry =
'<div class="contbox">'
. $subentry
. '<ul class="endlinks">'
. $slowtext
. '</ul></div>'
. $entry;
}
}
if ( $raw == 4 ) {
my $i = -1;
my $mashdate = $date;
$mashdate =~ y/-//;
for my $subentry (@entries) {
$i = $i + 1;
my $updated =
"${date}T00:00:0${i}Z"; # DS DS DS DS DS DS!!!!
my $atomid =
"urn:newsml:" . $HostName . ":$author:$mashdate:$i";
my $atomtitel = $subentry;
if ( $atomtitel =~ /^=+[^=]+=+/m ) { # FIXME opta opta!
#fix me nya rubriksyntaxen
$atomtitel =~ s/[^=]*=+\s*([^=]+) =+.*/$1/s;
}
else {
$atomtitel = "utan titel, $date";
}
$subentry =~ s/^[:=].*$//gm;
$subentry =~ s/^Se även .*$//gm;
$subentry =~ s/\(\:[^)]*\)//g;
$subentry = &WikiToHTML($subentry);
$subentry =
&QuoteHtml($subentry); # NTS also quote all titles etc
$author = &QuoteHtml($author);
$atomtitel = &QuoteHtml($atomtitel);
$entry = <<END3;
<entry>
<title>$atomtitel</title>
<link rel="alternate" type="text/html" href="http://$HostName/$pageid"/>
<updated>$updated</updated>
<id>$atomid</id>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
$subentry
</div>
</content>
</entry>
$entry
END3
}
}
$entries .= $entry;
}
}
my $atom = &GetHttpHeader('text/xml');
$atom .= <<END1;
<?xml version='1.0' encoding='iso-8859-1' ?>
<feed xmlns='http://www.w3.org/2005/Atom'>
<title>$MainPage</title>
<link rel="alternate" type="text/html" href="http://$HostName/$MainPage" />
<updated>${lastdate}T00:00:00Z</updated>
<link rel="self" href="http://$HostName/$MainPage/Blog/Atom" />
<author>
<name>$author</name>
</author>
<id>urn:newsml:$HostName:$author:ds:ds</id>
$entries
</feed>
END1
my $xhtml = &GetHttpHeader('text/html');
$xhtml .= <<END2;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><title>$MainPage</title><script src="/__utm.js" type="text/javascript"></script><script src="/__utm.js" type="text/javascript"></script>
<link rel="stylesheet" href="http://$HostName/wiki.pl?ioncat=browse&id=Handgranat/Css&raw=1" title="blogs" type="text/css" />
<link rel="stylesheet" href="http://$HostName/wiki.pl?ioncat=browse&id=$MainPage/Css&raw=1" title="blogs" type="text/css" />
<link rel="alternate" type="application/atom+xml" title="$MainPage" href="http://$HostName/$MainPage/Blog/Atom" />
</head>
<body>
<div class="header">
<h1>${MainPage}</h1>
</div>
<div class="content">
$entries
</div>
<div class="footer">
</div>
</body></html>
END2
if ( $raw == 4 ) {
print $atom;
}
else {
print $xhtml;
}
return;
}
if ( $raw == 1 || $raw == 2 ) {
if ( $id =~ /CSS$/i ) {
print &GetHttpHeader('text/css');
}
else {
print &GetHttpHeader('text/plain');
}
if ( $raw == 2 ) {
print $Section{'ts'} . " # Do not delete this line when editing!\n";
}
print $Text{'text'};
return;
}
# $fullHtml = &GetHeader($id, &QuoteHtml($id), $oldId);
$fullHtml =
&GetHeader( $id, &GetHeadlineOne( $id, $Text{'text'} ), $oldId );
if ( $revision ne '' ) {
if ( ( $revision eq $Page{'revision'} ) || ( $goodRevision ne '' ) ) {
$fullHtml .=
'<b>' . Ts( 'Showing revision %s', $revision ) . "</b><br>";
}
else {
$fullHtml .= '<b>'
. Ts( 'Revision %s not available', $revision ) . ' ('
. T('showing current revision instead')
. ')</b><br>';
}
}
$allDiff = &GetParam( 'alldiff', 0 );
if ( $allDiff != 0 ) {
$allDiff = &GetParam( 'defaultdiff', 1 );
}
if (
(
( $id eq $RCName ) || ( T($RCName) eq $id ) || ( T($id) eq $RCName )
)
&& &GetParam( 'norcdiff', 1 )
)
{
$allDiff = 0; # Only show if specifically requested
}
$showDiff = &GetParam( 'diff', $allDiff );
if ( $raw == 5 ) {
$showDiff = 1;
$raw = 0;
}
if ( $UseDiff && $showDiff ) {
$diffRevision = $goodRevision;
$diffRevision = &GetParam( 'diffrevision', $diffRevision );
# Eventually try to avoid the following keep-loading if possible?
&OpenKeptRevisions('text_default') if ( !$openKept );
$fullHtml .=
&GetDiffHTML( $showDiff, $id, $diffRevision, $revision, $newText );
$fullHtml .= "<hr class=wikilinediff>\n";
}
$fullHtml .=
"<div class=\"content\"><div class=\"contbox\">"
. &WikiToHTML( $Text{'text'} )
. "</div>";
if ( ( $id eq $RCName ) || ( T($RCName) eq $id ) || ( T($id) eq $RCName ) )
{
rprint $fullHtml;
&DoRc(1);
rprint( &GetFooterText( $id, $goodRevision ) );
return;
}
$fullHtml .= &GetFooterText( $id, $goodRevision );