/
WKCDataFiles.pm
2925 lines (2302 loc) · 95.2 KB
/
WKCDataFiles.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
#
# WKCDataFiles.pl -- Access disk data files
#
# (c) Copyright 2006 Software Garden, Inc.
# All Rights Reserved.
# Subject to Software License included with WKC.pm
#
package WKCDataFiles;
use strict;
use CGI qw(:standard);
use utf8;
use Time::Local;
use Net::FTP;
use WKC;
use WKCSheet; # For encode/decode from save, log stuff
use WKCStrings; # For WKCmonthnames
#
# Export symbols
#
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(get_siteinfo save_siteinfo update_siteinfo get_hostinfo save_hostinfo
get_userinfo update_userinfo get_userinfofileinfo
get_templateinfo get_template get_templatedirectory
check_site_exists delete_site
create_new_page save_page load_page load_page_from_array
edit_published_page edit_backup_page publish_page
delete_page abandon_page_edit rename_existing_page
rename_specific_file delete_specific_file download_specific_file
get_page_backup_list delete_old_backups
get_page_edit_path get_page_published_datafile_path
get_ensured_page_published_datafile_path get_ensured_page_edit_path
);
our $VERSION = '1.0.0';
#
# Locals
#
#
# %siteinfo - Information about the pages on a web site
#
# Contents:
#
# $siteinfo{files}
# {$pagename} - One for each page being tracked
# {filename} - name of file
# {fullnamepublished} - Title (in published file)
# {dtmpublished} - date/time last modified for published version (string from dir listing)
# {size} - size in bytes of published file
# {pubstatus} - 0 if unpublished, 1 otherwise
# {authors} - if null, nobody editing
# {$author} - One for each copy open for edit (usually zero or one)
# {fullnameedit} - Title (in edited file)
# {dtmedit} - date/time last modified for edit version (string)
# {editstatus} - "", "modified", 1 (editing, unchanged from published), "remote" (version on FTP site)
# $siteinfo{ftpdatetime} - Last time reconciled by FTP
# $siteinfo{updates} - used by update_siteinfo to let caller know if there are changes to the saved version
#
my %siteinfo;
my @siteinfofilesfields = qw(filename fullnamepublished dtmpublished size pubstatus);
my @siteinfoauthorsfields = qw(fullnameedit dtmedit editstatus);
my %siteinfoauthordata = ('fullnameedit' => 1, 'dtmedit' => 1, 'editstatus' => 1);
#
# %hostinfo - Information about accessing hosts and the sites on them
#
# Contents:
#
# $hostinfo{hosts}
# {$hostname} - One for each host listed
# {longname} - A more descriptive name (optional)
# {url} - How to access it by ftp (e.g., ftp.domainname.com) or blank if local
# {loginname} - Name to use in logging in by ftp (only if not local)
# {loginpassword} - Password to use when logging in (only if not local)
# {wkcpath} - Path to the directory containing the wkcdata directory (only if not local)
# $hostinfo{sites}
# {$sitename} - One for each site listed
# {longname} - A more descriptive name (optional)
# {host} - Name of host where this site resides
# {nameonhost} - Name of site used on host
# {authoronhost} - Author name to use on host
# {authorfromlogin} - If yes, if requiring login use logged in name as author
# {htmlpath} - Path to directory
# {htmlurl} - URL to directory with HTML
# {editurl} - URL to invoke editing a path
# {servicesurl} - URL to invoke runtime recalc (Not used: replaced after 0.91 with just editurl)
# {checked} - True if checked existence on host
# {backupmindays} - minimum number of days to keep backups no matter what the max files
# {backupmaxfiles} - maximum number of backup files for each page, delete oldest (archive files are not deleted)
# if absent or <0, keep all
# {publishrss} - yes/no, if yes publishes both page and site RSS files (RSS 2.0)
# {rssmaxsiteitems} - Maximum of items to list on site feed (default is 10)
# {rssmaxpageitems} - Maximum of items to list on page feed (default is 10)
# {rsstitle} - Title of site RSS feed, start of title for each page
# {rsslink} - URL of HTML website corresponding to this feed
# {rssdescription} - Phrase or sentence describing site feed
# {rsschannelxml} - Additional XML for all feeds
# $hostinfo{requirelogin} - yes/no, if yes, there is a user directory with files
# $hostinfo{lastsaved} - date/time last saved for display purposes
# $hostinfo{version} - 1 (if missing or null, original format from 0.1 Alpha)
#
my %hostinfo;
my @hostinfohostfields = qw(longname url loginname loginpassword wkcpath);
my @hostinfositefields = qw(longname host nameonhost authoronhost authorfromlogin htmlpath htmlurl editurl
backupmindays backupmaxfiles checked
publishrss rssmaxsiteitems rssmaxpageitems rsstitle rsslink rssdescription rsschannelxml
);
my %restricted_sitevalues = ("host" => 1, "nameonhost" => 1, "authoronhost" => 1); # restricted to a-zA-Z0-0\- charset
#
# %userinfo - Information about the registered users
#
# Contents:
#
# $userinfo{$username} - One for each user (must be lower case, no special chars, etc.) for filename
# {displayname} - A longer form of the name
# {password} - An MD5 hash of the password
# {admin} - yes/no if admin permissions
# {allsites} - yes if r/w access to all sites, ignoring {sites} value
# {allreadsites} - yes if read access to all sites, ignoring {readsites} value
# {sites} - comma separated list of sites this user has r/w access to (saved in file separately)
# {readsites} - comma separated list of sites this user has read access to (may be in sites and not readsites)
# {lastsaved} - date/time last saved
# $userinfo{HOSTrequirelogin} - copy of host's requirelogin value (set and used elsewhere)
#
my %userinfo;
# Return something
1;
# # # # # # #
#
# $ok = ensure_wkcdata(\%params)
#
# Makes sure we have a directory structure for local data
#
sub ensure_wkcdata {
my $params = shift @_;
return 1 if $params->{ensuredwkcdata};
my $ok = -e "$params->{localwkcpath}/wkcdata";
mkdir "$params->{localwkcpath}/wkcdata" if !$ok;
$params->{ensuredwkcdata} = 1;
return $ok;
}
# # # # # # #
#
# $whichsite = get_whichsite(\%params)
#
# Returns the sitename for the current parameters
#
sub get_whichsite {
my $params = shift @_;
my $whichsite;
$whichsite = $params->{sitename};
$whichsite =~ s/[^a-z0-9\-]//g;
$whichsite ||= "sitename error";
return $whichsite;
}
# # # # # # #
#
# $siteinfo = get_siteinfo(\%params)
#
# Returns a pointer to %siteinfo, filled in, for the current site
#
sub get_siteinfo {
my $params = shift @_;
%siteinfo = ();
my @filelines;
if (!$params->{sitename}) {
return \%siteinfo; # empty
}
ensure_wkcdata($params);
my $whichsite = get_whichsite($params);
open (DATAFILEIN, "$params->{localwkcpath}/wkcdata/sites/$whichsite/siteinfo.txt");
@filelines = <DATAFILEIN>;
close DATAFILEIN;
my $ok = parse_siteinfo_data(\@filelines, \%siteinfo);
#!!! Check ok!
return \%siteinfo;
}
# # # # # # #
#
# $ok = save_siteinfo(\%params, \%siteinfo)
#
# Saves the siteinfo as a file
#
sub save_siteinfo {
my ($params, $siteinfo) = @_;
if (!$params->{sitename}) {
return 0; # failure
}
my $ok;
my $outstr;
$outstr .= "filetype:wkcsiteinfo:version:1\n"; # output file type and version
my $ftpdatetime = encode_for_save($siteinfo->{ftpdatetime});
$outstr .= "ftpdatetime:$ftpdatetime\n";
foreach my $name (sort keys %{$siteinfo->{files}}) { # Output each name
my $fileinfo = $siteinfo->{files}->{$name};
my $string = encode_fields(\@siteinfofilesfields, $fileinfo); # output regular fields
foreach my $author (sort keys %{$fileinfo->{authors}}) { # and each author's fields working on that name
my $str = encode_fields(\@siteinfoauthorsfields, $fileinfo->{authors}->{$author});
$author = encode_for_save($author);
$string .= ":author:$author$str";
}
$outstr .= "file:$name$string\n"
}
my $whichsite = get_whichsite($params);
#print "Writing out siteinfo.\n";
open (DATAFILEIN, "> $params->{localwkcpath}/wkcdata/sites/$whichsite/siteinfo.txt");
print DATAFILEIN $outstr;
close DATAFILEIN;
return $ok;
}
# # # # # # #
#
# $error = parse_siteinfo_data(\@lines, \%siteinfo)
#
# Returns "" if OK, otherwise error string.
# Fills in %siteinfo.
#
# File Format is:
#
# filetype:wkcsiteinfo:version:1
# file:name
# :filename:filename
# :fullnamepublished:full name
# :dtmpublished:dt modified
# :size:size
# :pubstatus:pubstatus
# :author:name - remembered for the following three until next author
# :fullnameedit:full name
# :dtmedit:dt modified
# :editstatus:editstatus
# ftpdatetime:datetime
#
sub parse_siteinfo_data {
my ($lines, $siteinfo) = @_;
my ($rest, $linetype, $name, $type, $type2, $rest, $value, $filetype, $fileversion);
$siteinfo->{files} ||= {};
my $filevalues = $siteinfo->{files};
foreach my $line (@$lines) {
chomp $line;
$line =~ s/\r//g;
$line =~ s/^\x{EF}\x{BB}\x{BF}//; # remove UTF-8 Byte Order Mark if present
($linetype, $rest) = split(/:/, $line, 2);
if ($linetype eq "filetype") {
($filetype, $name, $fileversion, $rest) = split(/:/, $rest, 4);
}
elsif ($linetype eq "file") {
($name, $type, $rest) = split(/:/, $rest, 3);
$filevalues->{$name} ||= {};
my $author;
while ($type) {
($value, $type2, $rest) = split(/:/, $rest, 3);
if ($type eq "author") { # remember author
$author = decode_from_save($value);
$filevalues->{$name}->{authors} ||= {};
$filevalues->{$name}->{authors}->{$author} = {};
}
elsif ($siteinfoauthordata{$type}) { # a value that is in the author sub-hash
# $params->{debugmessage} = "Missing author in siteinfo for page $name\n" unless $author;
$filevalues->{$name}->{authors}->{$author}->{$type} = decode_from_save($value);
}
else {
$filevalues->{$name}->{$type} = decode_from_save($value);
}
$type = $type2;
}
}
elsif ($linetype eq "ftpdatetime") {
$siteinfo->{ftpdatetime} = decode_from_save($rest);
}
if ($filetype ne "wkcsiteinfo" || $fileversion ne "1") { # first line must be filetype so this will pass
$siteinfo->{files} = {}; # bad version - wipe out so we'll load it from the directories
$siteinfo->{updates} = 1;
# $params->{debugmessage} = "Old version siteinfo - will be updated automatically.\n";
return "";
}
}
$siteinfo->{updates} = 0; # Initialize to "no changes to saved file"
return "";
}
# # # # # # #
#
# $ok = update_siteinfo(\%params, $hostsinfo, \%siteinfo)
#
# Looks at the web site and local directory to update the siteinfo
#
sub update_siteinfo {
my ($params, $hostsinfo, $siteinfo) = @_;
if (!$params->{sitename}) {
return 0; # failure
}
my ($name, $fileinfo, $authorinfo, $mtime, @tv, $mtimestr, $fsize);
$siteinfo->{files} ||= {};
foreach $name (keys %{$siteinfo->{files}}) {
$siteinfo->{files}->{$name} ||= {};
$siteinfo->{files}->{$name}->{relevant} = 0;
foreach my $author (keys %{$siteinfo->{files}->{$name}->{authors}}) {
$siteinfo->{files}->{$name}->{authors}->{$author} ||= {};
$siteinfo->{files}->{$name}->{authors}->{$author}->{relevant} = 0;
}
}
my $whichsite = get_whichsite($params);
#print "Listing: $params->{localwkcpath}/wkcdata/sites/$whichsite/*.edit.*.txt\n";
my @localfiles = glob("$params->{localwkcpath}/wkcdata/sites/$whichsite/*.edit.*.txt");
my $sitedata = $hostsinfo->{sites}->{$params->{sitename}};
my $hostdata = $hostsinfo->{hosts}->{$sitedata->{host}};
foreach my $lfile (@localfiles) {
#print "Local file: $lfile\n";
$name = lc $lfile;
$name =~ s/^.*\/(.+?)\.edit\.(.*?)\.txt$/$1/;
$siteinfo->{files}->{$name} ||= {};
$fileinfo = $siteinfo->{files}->{$name};
$fileinfo->{relevant} = 1;
$fileinfo->{filename} = "$name.html";
$fileinfo->{authors} ||= {};
$fileinfo->{authors}->{$2} ||= {};
$authorinfo = $fileinfo->{authors}->{$2};
$authorinfo->{relevant} = 1;
$mtime = (stat($lfile))[9];
@tv = localtime($mtime);
$mtimestr = sprintf("%s %d, %04d %02d:%02d:%02d", $WKCmonthnames[$tv[4]], $tv[3], $tv[5]+1900,
$tv[2], $tv[1], $tv[0]);
if ($authorinfo->{dtmedit} ne $mtimestr) { # Modified since last checked
$authorinfo->{dtmedit} = $mtimestr; # Update time
$siteinfo->{updates} += 1;
my (@pubheaderlines, %pubheaderdata);
my $loaderr = load_page($lfile, \@pubheaderlines, 0); # Get header info
my $pareseok = parse_header_save(\@pubheaderlines, \%pubheaderdata); # Get data from header
$authorinfo->{fullnameedit} = $pubheaderdata{fullname};
$authorinfo->{editstatus} = "modified" if $pubheaderdata{lastmodified}; # if there is a time, it was modified
}
$authorinfo->{editstatus} ||= 1;
}
if ($hostdata->{url}) { # remote publishing
my $ftp;
if (!$siteinfo->{ftpdatetime}) {
$ftp = Net::FTP->new($hostdata->{url}, Debug => 0, Passive => 1, Timeout => 30);
}
else {
foreach $name (keys %{$siteinfo->{files}}) { # Mark remote published ones relevant
if ($siteinfo->{files}->{$name}->{pubstatus}
|| $siteinfo->{files}->{$name}->{authors}) {
$siteinfo->{files}->{$name}->{relevant} = 1;
foreach my $author (keys %{$siteinfo->{files}->{$name}->{authors}}) { # All authors, too
$siteinfo->{files}->{$name}->{authors}->{$author}->{relevant} = 1;
}
}
}
}
if ($ftp) {
my $ok = $ftp->login($hostdata->{loginname}, $hostdata->{loginpassword});
$ok = $ftp->cwd("$hostdata->{wkcpath}/wkcdata/sites/$sitedata->{nameonhost}") if $ok;
my @dir;
@dir = $ftp->dir if $ok;
my $filenumber;
my $filenames;
my (@fsize, @fmon, @fday, @fname, @fhour, @fmin, @fyear, @ftime);
my @timevalues = localtime;
my $year = $timevalues[5];
foreach my $line (@dir) { # go through once to find values to sort
#print "ls: $line\n";
my ($access, $links, $owner, $group, $s, $m, $d, $timeyr, $n) = split(" ", $line, 9);
next unless $access =~ /^-/;
$filenumber++;
($fsize[$filenumber], $fmon[$filenumber], $fday[$filenumber], $fname[$filenumber]) = ($s, ucfirst lc $m, $d, $n);
if ($timeyr =~ /:/) {
($fhour[$filenumber], $fmin[$filenumber]) = split(/:/, $timeyr);
$fyear[$filenumber] = $year + 1900;
}
else {
($fyear[$filenumber], $fhour[$filenumber], $fmin[$filenumber]) = ($timeyr, 0, 0);
}
}
for (my $j = 1; $j <= $filenumber; $j++) {
$name = lc $fname[$j];
if ($name =~ /^(.+?)\.published\.txt$/) {
$name = $1;
$siteinfo->{files}->{$name} ||= {};
$fileinfo = $siteinfo->{files}->{$name};
$fileinfo->{filename} = "$name.html";
my $dtmpub = sprintf("%s %d, %04d %02d:%02d", $fmon[$j], $fday[$j], $fyear[$j], $fhour[$j] ,$fmin[$j]);
if ($dtmpub ne $fileinfo->{dtmpublished} || $fsize[$j] != $fileinfo->{size}) {
my $pubpath = get_page_published_datafile_path($params, $hostsinfo, $whichsite, $name);
$ok = $ftp->get($fname[$j], $pubpath); # Download published file
my (@pubheaderlines, %pubheaderdata);
my $loaderr = load_page($pubpath, \@pubheaderlines, 0); # Get header info
my $pareseok = parse_header_save(\@pubheaderlines, \%pubheaderdata); # Get data from header
$fileinfo->{fullnamepublished} = $pubheaderdata{fullname}; # Save long name
$fileinfo->{dtmpublished} = $dtmpub; # Update dtm
$fileinfo->{size} = $fsize[$j]; # and size
}
$fileinfo->{pubstatus} = 1;
$fileinfo->{relevant} = 1;
}
elsif ($name =~ /^(.+?)\.edit\.(.*?)\.txt$/) { # see if anybody is editing
$name = $1;
$siteinfo->{files}->{$name} ||= {};
$fileinfo = $siteinfo->{files}->{$name};
$fileinfo->{filename} = "$name.html";
$fileinfo->{authors} ||= {};
$fileinfo->{authors}->{$2} ||= {};
$authorinfo = $fileinfo->{authors}->{$2};
$authorinfo->{relevant} = 1;
$authorinfo->{editstatus} ||= "remote"; # set to remote if no local version
$fileinfo->{relevant} = 1;
}
}
$ftp->quit;
if (!$ok) {
my $msgsc = special_chars($ftp->message);
$params->{debugmessage} = qq!$WKCStrings{datafilesunableftperrorstatus}: $msgsc.\n!;
}
else {
@tv = localtime;
$siteinfo->{ftpdatetime} = sprintf("%s %d, %04d %02d:%02d:%02d", $WKCmonthnames[$tv[4]], $tv[3], $tv[5]+1900,
$tv[2], $tv[1], $tv[0]); # remember when we last loaded
$siteinfo->{updates} += 1;
}
}
}
else { # Local publishing
@localfiles = glob("$params->{localwkcpath}/wkcdata/sites/$whichsite/*.published.txt");
foreach my $lfile (@localfiles) {
$name = lc $lfile;
if ($name =~ /^.*\/(.+?)\.published\.txt$/) {
$name = $1;
$siteinfo->{files}->{$name} ||= {};
$fileinfo = $siteinfo->{files}->{$name};
$fileinfo->{relevant} = 1;
$fileinfo->{filename} = "$name.html";
$mtime = (stat($lfile))[9];
@tv = localtime($mtime);
$mtimestr = sprintf("%s %d, %04d %02d:%02d:%02d", $WKCmonthnames[$tv[4]], $tv[3], $tv[5]+1900,
$tv[2], $tv[1], $tv[0]);
$fsize = (stat(_))[7];
if ($fileinfo->{dtmpublished} ne $mtimestr || $fileinfo->{size} != $fsize) {
my (@pubheaderlines, %pubheaderdata);
my $loaderr = load_page($lfile, \@pubheaderlines, 0); # Get header info
my $pareseok = parse_header_save(\@pubheaderlines, \%pubheaderdata); # Get data from header
$fileinfo->{fullnamepublished} = $pubheaderdata{fullname};
$fileinfo->{dtmpublished} = $mtimestr;
$fileinfo->{size} = $fsize;
$siteinfo->{updates} += 1;
}
$fileinfo->{pubstatus} = 1;
}
}
@tv = localtime;
$siteinfo->{ftpdatetime} = sprintf("%s %d, %04d %02d:%02d:%02d", $WKCmonthnames[$tv[4]], $tv[3], $tv[5]+1900,
$tv[2], $tv[1], $tv[0]); # show now as last loaded
}
foreach $name (keys %{$siteinfo->{files}}) { # Go through all
#print "Name: $name\n";
$fileinfo = $siteinfo->{files}->{$name};
if ($fileinfo->{relevant}) { # Still around
foreach my $author (keys %{$siteinfo->{files}->{$name}->{authors}}) {
next if $siteinfo->{files}->{$name}->{authors}->{$author}->{relevant}; # author still has it open
delete $siteinfo->{files}->{$name}->{authors}->{$author}; # remove author no longer seen editing this page
}
next; # look at next page
}
delete $siteinfo->{files}->{$name}; # Remove ones we don't see anymore
$siteinfo->{updates} += 1;
}
return 1;
}
# # # # # # #
#
# $hostinfo = get_hostinfo(\%params)
#
# Returns a pointer to %hostinfo, filled in
#
sub get_hostinfo {
my $params = shift @_;
%hostinfo = ();
my @filelines;
ensure_wkcdata($params);
open (DATAFILEIN, "$params->{localwkcpath}/wkcdata/hostinfo.txt");
@filelines = <DATAFILEIN>;
close DATAFILEIN;
my $errtext = parse_hostinfo_data(\@filelines, \%hostinfo);
if ($errtext) {
$params->{debugmessage} = "$errtext\n";
}
if ($hostinfo{version} ne 1 && (scalar @filelines) > 0) {
$params->{debugmessage} = "Hostinfo version not 1 - resetting site checked flags.\n";
foreach my $hostname (sort keys %{$hostinfo{sites}}) {
$hostinfo{sites}->{$hostname}->{checked} = 0;
}
my $ok = save_hostinfo($params, \%hostinfo);
}
return \%hostinfo;
}
# # # # # # #
#
# $ok = save_hostinfo(\%params, \%hostinfo)
#
# Saves the hostinfo as a file
#
sub save_hostinfo {
my ($params, $hostinfo) = @_;
my $ok;
my $outstr;
$outstr .= "filetype:wkchostinfo:version:1\n"; # output file type and version
my $clocktime = encode_for_save(scalar localtime);
$outstr .= "lastsaved:$clocktime\n";
if ($hostinfo->{requirelogin}) {
$outstr .= "requirelogin:$hostinfo->{requirelogin}\n";
}
foreach my $name (sort keys %{$hostinfo->{hosts}}) { # Output each host name
my $hostvals = $hostinfo->{hosts}->{$name};
my $string = encode_fields(\@hostinfohostfields, $hostvals);
$outstr .= "host:$name$string\n";
}
foreach my $name (sort keys %{$hostinfo->{sites}}) { # Output each site name
my $sitevals = $hostinfo->{sites}->{$name};
my $string = encode_fields(\@hostinfositefields, $sitevals);
$outstr .= "site:$name$string\n";
}
open (DATAFILEIN, "> $params->{localwkcpath}/wkcdata/hostinfo.txt");
print DATAFILEIN $outstr;
close DATAFILEIN;
# $params->{debugmessage} = "Saving hostinfo\n";
return $ok;
}
# # # # # # #
#
# $string = encode_fields(\@fieldnames, \%info)
#
# Returns ":fieldname1:fieldvalue1:fieldname2:fieldvalue2..."
#
sub encode_fields {
my ($fieldnames, $info) = @_;
my $str;
foreach my $val (@$fieldnames) {
my $valstr = encode_for_save($info->{$val});
$str .= ":$val:$valstr";
}
return $str;
}
# # # # # # #
#
# $error = parse_hostinfo_data(\@lines, \%hostinfo)
#
# Returns "" if OK, otherwise error string.
# Fills in %hostinfo.
#
# File Format is:
#
# filetype:wkchostinfo:version:1
# host:name
# :hostfieldname:fieldvalue
# site:name
# :sitefieldname:fieldvalue
#
sub parse_hostinfo_data {
my ($lines, $hostinfo) = @_;
my ($rest, $linetype, $name, $type, $type2, $rest, $value, $filetype, $fileversion);
$hostinfo->{hosts} ||= {};
$hostinfo->{sites} ||= {};
my $hostsvalues = $hostinfo->{hosts};
my $sitesvalues = $hostinfo->{sites};
foreach my $line (@$lines) {
chomp $line;
$line =~ s/\r//g;
$line =~ s/^\x{EF}\x{BB}\x{BF}//; # remove UTF-8 Byte Order Mark if present
($linetype, $rest) = split(/:/, $line, 2);
if ($linetype eq "filetype") {
($filetype, $name, $fileversion, $rest) = split(/:/, $rest, 4);
if ($filetype ne "wkchostinfo") { # if present, must be this
return "Incorrect filetype: $filetype";
}
$hostinfo->{version} = $fileversion; # remember this
}
elsif ($linetype eq "host") {
($name, $type, $rest) = split(/:/, $rest, 3);
$name =~ s/[^a-z0-9\-]//g; # just in case corrupted file
$hostsvalues->{$name} ||= {};
while ($type) {
($value, $type2, $rest) = split(/:/, $rest, 3);
$hostsvalues->{$name}->{$type} = decode_from_save($value);
$type = $type2;
}
}
elsif ($linetype eq "site") {
($name, $type, $rest) = split(/:/, $rest, 3);
$name =~ s/[^a-z0-9\-]//g; # just in case corrupted file
$sitesvalues->{$name} ||= {};
while ($type) {
($value, $type2, $rest) = split(/:/, $rest, 3);
$sitesvalues->{$name}->{$type} = decode_from_save($value);
$sitesvalues->{$name}->{$type} =~ s/[^a-z0-9\-]//g if $restricted_sitevalues{$type};
$type = $type2;
}
}
elsif ($linetype eq "requirelogin") {
($value, $rest) = split(/:/, $rest, 2);
$hostinfo->{requirelogin} = decode_from_save($value);
}
elsif ($linetype eq "lastsaved") {
$hostinfo->{lastsaved} = decode_from_save($rest);
}
}
}
# # # # # # #
#
# $userinfo = get_userinfo(\%params)
#
# Retrieves the userinfo data and returns a reference to %userinfo
#
sub get_userinfo {
my $params = shift @_;
%userinfo = ();
mkdir "$params->{localwkcpath}/wkcdata/users"; # ensure directory exists
my @localfiles = glob("$params->{localwkcpath}/wkcdata/users/*.txt"); # get a list of all defined users
foreach my $lfile (@localfiles) {
my $name = lc $lfile;
$name =~ s/^.*\/(.+?)\.txt$/$1/;
next unless $name;
get_userinfofileinfo($params, \%userinfo, $name);
}
return \%userinfo;
}
# # # # # # #
#
# $errortext = get_userinfofileinfo(\%params, \%userinfo, $name)
#
# Reads in data from user file and fills in $userinfo->{$name}.
# Returns $errortext if user not found.
#
sub get_userinfofileinfo {
my ($params, $userinfo, $name) = @_;
my @filelines;
my $ok = open (DATAFILEIN, "$params->{localwkcpath}/wkcdata/users/$name.txt");
if (!$ok) {
return "$WKCStrings{datafilesnotfound}";
}
@filelines = <DATAFILEIN>;
close DATAFILEIN;
$userinfo->{$name} ||= {}; # make sure there is a hash for the data
foreach my $line (@filelines) {
chomp $line;
$line =~ s/\r//g;
$line =~ s/^\x{EF}\x{BB}\x{BF}//; # remove UTF-8 Byte Order Mark if present
my ($linetype, $rest) = split(/:/, $line, 2);
if ($linetype eq "site") {
my $sitename = decode_from_save($rest);
if ($userinfo->{$name}->{sites}) {
$userinfo->{$name}->{sites} .= ",$sitename";
}
else {
$userinfo->{$name}->{sites} = $sitename;
}
}
elsif ($linetype eq "readsite") {
my $sitename = decode_from_save($rest);
if ($userinfo->{$name}->{readsites}) {
$userinfo->{$name}->{readsites} .= ",$sitename";
}
else {
$userinfo->{$name}->{readsites} = $sitename;
}
}
else {
$userinfo->{$name}->{$linetype} = decode_from_save($rest);
}
}
return;
}
# # # # # # #
#
# $errtext = update_userinfo(\%params, \%userinfo, $username)
#
# Updates the userinfo data about the specified user.
# If $userinfo->{username}->{delete}, the user is deleted from the system.
#
sub update_userinfo {
my ($params, $userinfo, $username) = @_;
my ($outstr, $str);
if ($userinfo->{$username}->{delete}) { # delete user file
unlink "$params->{localwkcpath}/wkcdata/users/$username.txt";
return;
}
my $clocktime = encode_for_save(scalar localtime);
$outstr .= "lastsaved:$clocktime\n";
$str = encode_for_save($userinfo->{$username}->{displayname});
$outstr .= "displayname:$str\n";
$str = encode_for_save($userinfo->{$username}->{password});
$outstr .= "password:$str\n";
$str = encode_for_save($userinfo->{$username}->{admin});
$outstr .= "admin:$str\n";
$str = encode_for_save($userinfo->{$username}->{allsites});
$outstr .= "allsites:$str\n" if $str;
$str = encode_for_save($userinfo->{$username}->{allreadsites});
$outstr .= "allreadsites:$str\n" if $str;
foreach my $sitename (split (/,/, $userinfo->{$username}->{sites})) { # Output each site name
$str = encode_for_save($sitename);
$outstr .= "site:$str\n";
}
foreach my $sitename (split (/,/, $userinfo->{$username}->{readsites})) { # Output each read site name
$str = encode_for_save($sitename);
$outstr .= "readsite:$str\n";
}
open (DATAFILEIN, "> $params->{localwkcpath}/wkcdata/users/$username.txt");
print DATAFILEIN $outstr;
close DATAFILEIN;
return;
}
# # # # # # #
#
# get_templateinfo(\%params, $type, \@templateinfo)
#
# Retrieves the templateinfo data for specified templates in all locations available for the current site.
# Type is something like "htmltemplate", "pagetemplate", etc.
#
sub get_templateinfo {
my ($params, $type, $templateinfo) = @_;
splice @$templateinfo; # erase whatever is there
fill_templateinfo($params, "system", $type, $templateinfo);
fill_templateinfo($params, "shared", $type, $templateinfo);
fill_templateinfo($params, "site", $type, $templateinfo);
return;
}
# # # # # # #
#
# $directory = get_templatedirectory(\%params, $where)
#
# Returns a directory path to the "system", "shared", and "site" template directories.
#
sub get_templatedirectory {
my ($params, $where) = @_;
my $sitename = $params->{sitename};
$sitename =~ s/[^a-z0-9\-]//g;
if ($where eq "system") {
return "$params->{localwkcpath}";
}
elsif ($where eq "shared") {
return "$params->{localwkcpath}/wkcdata/templates";
}
elsif ($where eq "site") {
return "$params->{localwkcpath}/wkcdata/sites/$sitename/templates";
}
return $WKCStrings{"datafilesunknownlocation"}; # hopefully this isn't a dir name
}
# # # # # # #
#
# fill_templateinfo($params, $location, $type, \@templateinfo)
#
# Fills in the template_info for $type files in $location type of directory
#
sub fill_templateinfo {
my ($params, $location, $type, $templateinfo) = @_;
my $directory = get_templatedirectory($params, $location);
my $ok = -e "$directory";
if (!$ok) {
return; # if no dir, then no files to list
}
my @localfiles = glob("$directory/*.$type.txt"); # get a list of all templates
my @names;
foreach my $lfile (@localfiles) {
my $name = lc $lfile;
$name =~ s/^.*\/(.+?)\.$type\.txt$/$1/;
push @names, $name if $name;
}
foreach my $name (sort @names) {
my $templatename = "$location:$name"; # create a template name
my $tcontents = get_template($params, $type, $templatename);
$tcontents =~ m/{{templatedescriptionline}}(.+?)\n/;
push @$templateinfo, {name => $templatename, longname => $1};
}
return;
}
# # # # # # #
#
# $contents = get_template(\%params, $type, $templatename)
#
# Returns the contents of $templatename, or null.
# $templatename is in the form of "location:name".
#
sub get_template {
my ($params, $type, $templatename) = @_;
my ($location, $name) = split(/:/, $templatename, 2);
$location =~ s/[^a-z0-9\-]//g;
$name =~ s/[^a-z0-9\-]//g;
my $directory = get_templatedirectory($params, $location);
my $ok = open (DATAFILEIN, "$directory/$name.$type.txt");
if (!$ok) {
return;
}
my $content;
while (my $line = <DATAFILEIN>) {
$content .= $line;
}
close DATAFILEIN;
return $content;
}
# # # # # # #
#
# $str = check_site_exists(\%params, $hostinfo, $sitename)
#
# Makes sure that site info points to a real site and creates the
# directory structure there if necessary
#
# Remembers successful checks in hostinfo so no need to do each time