-
Notifications
You must be signed in to change notification settings - Fork 1
/
prsslist.pm
executable file
·3298 lines (2859 loc) · 125 KB
/
prsslist.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
#!/usr/bin/perl
#
# Original software (c) Copyright 2004, 2005 Software Garden, Inc. All Rights Reserved.
# pRSSlist Copyright (c) 2009, lantrix, TechDebug.com
# Subject to Software License at the end of this file
#
#
# prsslist RSS Generator Program
#
# This is the main software for running the program.
# It is called by prsslist.pl, prsslistcgi.pl.
#
# Use the interface program appropriate to your needs.
#
use strict;
use CGI qw(:standard);
use Time::Local;
use Net::FTP;
use utf8;
use LWP::UserAgent;
#
# Export symbols
#
package pRSSlist;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw($programname %config_values process_request);
our $VERSION = '1.3.1';
# # # # # # # # # #
#
# HTML Constants & Templates
#
# # # # # # # # # #
our $programname = "pRSSlist Program 2.0";
my @itemtags = qw(itemtitle itemlink itemdesc itempubdate itemguid itemguidispermalink itemdeschtml
itemenclosureurl itemenclosurelength itemenclosuretype itemaddlxml); # item field names
my @feedtags = qw(rsstitle rsslink rssdesc); # feed field names
my @publishfields = qw(ftpurl ftpfilename ftpdirectory ftpuser ftppassword publishfile
maxpublishitems mintimepublishitems publishsequence
htmlversion rssfileurl htmlabove htmlitem htmlbelow htmlallitems
publishhtmlfile ftphtmlfilename ftphtmldirectory
backupftpfilename backupftpdirectory backuplocalfilename
backuptyperadio backuppasswords); # information for publishing
my %mintimemapping = ('000' => "None", '024' => "1 day", '048' => "2 days", '072' => "3 days", '168' => "1 week", '336' => "2 weeks"); # hours to string
my @optionfields = qw(optionsmaxsaved maxlistitems templatetitle templatelink templatedesc templateenclosuretype
templatedeschtml templatepubdate templatepubdateusecurrent templateguid templateguidispermalink templateguidradio templateaddlxml
displaytitle displaylink displaypubdate displaydesc displayguid displayenclosure
desclines channeladdlxml rsstagaddltext); # other feed options
my %templatefields = ('templatetitle' => 'itemtitle', 'templatelink' => 'itemlink', 'templatedesc' => 'itemdesc',
'templateenclosuretype' => 'itemenclosuretype',
'templatedeschtml' => 'itemdeschtml', 'templatepubdate' => 'itempubdate',
'templateguid' => 'itemguid', 'templateguidispermalink' => 'itemguidispermalink',
'templateaddlxml' => 'itemaddlxml'); # template to item mapping
my @browsefields = qw(browseftpurl browseftpdirectory browseftpuser browseftppassword browseurlprefix); # information for browsing
my @tablist = qw(Feed Items Publish Options Quit);
my %monthnames = ('Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3, 'May' => 4, 'Jun' => 5, 'Jul' => 6,
'Aug' => 7, 'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11);
my %daynames = ('Sun' => 1, 'Mon' => 2, 'Tue' => 3, 'Wed' => 4, 'Thu' => 5, 'Fri' => 6, 'Sat' => 7);
my $STYLE_STRING = <<"EOF";
body {
background-color:#CCFFFF;
}
body, td, input, textarea, select {
font-size:small;
font-family:verdana,helvetica,sans-serif;
}
td {
vertical-align: top;
}
form {
margin:0px;
padding:0px;
}
.sectiondark {
border: 1px solid #99CC99;
padding: 8px;
}
.sectionplain {
padding: 10px;
}
.title {
color: #006600;
font-weight:bold;
margin: 0em 0px 2pt 0px;
padding: 0px 0px 0px 0px;
}
.title2 {
border-top: 1px solid #006600;
color: #006600;
font-weight:bold;
margin: .5em 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
.pagetitle {
color: #006600;
font-weight:bold;
margin: 0em 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
.pagefeedinfo {
color: black;
font-size: smaller;
margin: 1pt 0px 2pt 0px;
padding: 0px 0px 0px 0px;
}
.desc {
font-size:smaller;
padding: 0px 0px .75em 0px;
}
.tabtable {font-size:small;}
.tab {
border-bottom: 1px solid black;
padding-bottom: 4px;
}
.tab input {
background-color:#CCCC99;
color:black;
}
.tabselected {
border-left: 1px solid black;
border-right: 1px solid black;
border-top: 1px solid black;
background-color:#DDFFDD;
color:black;
padding: 1px 14px 0px 14px;
text-align: center;
font-weight: bold;
}
.tab1 {
border-bottom: 1px solid black;
}
.tab2 {
background-color:#DDFFDD;
}
.tab2left {
border-left: 1px solid black;
background-color:#DDFFDD;
padding-left:20px;
}
.tab2right {
border-right: 1px solid black;
background-color:#DDFFDD;
padding-left:20px;
}
.ttbody {
background-color:#DDFFDD;
border-right: 1px solid black;
border-left: 1px solid black;
border-bottom: 1px solid black;
padding: 0px 10px 4px 10px;
}
.head {
border: 1px dashed #006600;
padding: 6px;
color: #006600;
font-weight: bold;
margin: 1em 0px .5em 0px;
}
.footer {
border: 1px dashed #006600;
padding: 6px;
color: #006600;
font-size: smaller;
margin: 1em 0px .5em 0px;
}
input.small {
font-size: smaller;
}
.itemheader {
color: #006600;
font-weight:bold;
margin: .5em 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
.itemselected {
color: #990000;
font-weight:bold;
margin: .5em 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
.selectedexample {
color: #990000;
}
.itemtitle {
border-left: 1px solid #99CC99;
padding-left: 8px;
margin: 4px 0px 0px 24px;
font-weight: bold;
}
.itemlink {
border-left: 1px solid #99CC99;
padding: 0px 0px 0px 8px;
margin-left: 24px;
font-size: smaller;
}
.itemdesc {
border-left: 1px dotted #99CC99;
padding: 8px 0px 8px 8px;
margin-left: 24px;
font-size: smaller;
}
.itemmisc {
border-left: 1px solid #99CC99;
padding: 8px 0px 8px 8px;
margin-left: 24px;
font-size: smaller;
}
.itembuttons {
float:right;
clear:right;
padding:5px 0px 0px 30px;
border-top:1px solid #99CC99;
margin:.5em 0px 0px 0px;
}
.warning {
color: red;
}
.smallprompt {
font-size: smaller;
}
.tdsmall {
font-size: xx-small;
}
.itemmisc td {
font-size: xx-small;
}
.browsefilename {
font-size: smaller;
font-weight: bold;
background-color: white;
padding-left: 4pt;
}
.browsefilesize {
font-size: smaller;
padding-left: 4pt;
}
.browsefiledate {
font-size: smaller;
padding-left: 4pt;
}
.browsecolumnhead {
background-color: #99CC99;
color: white;
font-size: smaller;
font-weight: bold;
padding: 4pt 0 4pt 4pt;
}
EOF
my $defaulthtmlabove = <<"EOF";
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<title>Contents of RSS feed for: {{rsstitle}}</title>
<style type="text/css"><!--
body {font-family:verdana,helvetica,sans-serif;}
.above {font-size:smaller;background-color:#CCCCCC;padding:1em;margin-bottom:1em;}
.rsstitle {font-size:larger;font-weight:bold;}
.rssdesc {font-style:italic;margin-top:.5em;}
.rsspubdate {font-style:italic;}
.item {font-size:smaller;padding:0px 1em .5em 1em;}
.itemtitle {font-weight:bold;margin-bottom:1em;}
.itemdesc {margin:0px 0px 1em 2em;font-size:smaller;}
.itempubdate {font-style:italic;margin:0px 0px .5em 2em;font-size:smaller;}
.below {font-size:smaller;border-top:1px solid black;padding:.25em 1em 0px 1em;margin-top:.5em;}
.note {font-size:smaller;margin-top:1em;}
-->
</style>
</head>
<body>
<div class="above">
<b>Contents of RSS feed for:</b><br><br>
<div class="rsstitle"><a href="{{rsslink}}">{{rsstitle}}</a></div>
<div class="rssdesc">{{rssdesc}}</div>
</div>
EOF
my $defaulthtmlitem = <<"EOF";
<div class="item">
<div class="itemtitle"><a href="{{itemlink}}">{{itemtitle}}</a></div>
<div class="itemdesc">{{itemdesc}}</div>
<div class="itempubdate">Published: {{itempubdate}}</div>
</div>
EOF
my $defaulthtmlbelow = <<"EOF";
<div class="below">
<div class="rsspubdate">Updated: {{rsspubdate}}</div>
<div class="note">
The URL to provide to an RSS aggregator when subscribing to this feed: <a href="{{rssfileurlraw}}">{{rssfileurl}}</a><br>
(For more information about RSS see: <a href="http://rss.softwaregarden.com/aboutrss.html">What is RSS?</a>.)
</div>
</div>
</body>
</html>
EOF
my $datafiledefault = "listdata"; # default filename prefix to hold data and settings
my $socketdefault = 6555; # default socket to listen to for browser
my $securitycode = "not set!"; # Requests must have a parameter that matches this
my $placeholderpw = "s a m e "; # Used by Save Publish Info and edit FTP password
my $defaultmaxlistitems = 10; # default number of items listed per page
#
# Define configuration information from command line
#
our %config_values; # most configuration values
$config_values{datafile} = $datafiledefault;
$config_values{socket} = $socketdefault;
#
# Main Data
#
my %datavalues;
# # # # # # # # # #
# Subroutines
# # # # # # # # # #
# # # # # # # # # #
# process_request($querystring, $using_cgi)
#
# Responds to browser request and does all the work
# Returns $response, a string with the HTML response
# The $querystring is the raw query from the browser.
# Unless $using_cgi is present and true, a "security"
# parameter is used in each request to guard against
# URLs on web sites that guess we are running locally.
#
sub process_request {
my ($querystring, $using_cgi) = @_;
my $response;
# Remember when we started
my $start_clock_time = scalar localtime;
my $start_program_time = times();
# Get GMT time string
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime;
my $dayname = qw(Sun Mon Tue Wed Thu Fri Sat)[$wday];
my $monname = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$mon];
my $dtstring = sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT", $dayname, $mday, $monname, $year+1900,
$hour, $min, $sec);
# Output start of HTML, including style information
$response .= <<"EOF";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<title>$programname</title>
<style type="text/css"><!--
$STYLE_STRING
-->
</style>
<script>
<!--
var setf = function() {1;}
// -->
</script>
</head>
<body onload="setf()">
EOF
# Get CGI object to get parameters:
$querystring ||= ""; # make sure has something
my $q = new CGI($querystring);
my %params = $q->Vars;
if ($params{newtab} eq 'Quit') {
return;
}
#
# Check security code
#
# If not the same as ours, set it and wipe out parameters.
# This is to prevent malicious web sites from linking
# to a "known" local address and affect this app.
#
$securitycode = "" if $using_cgi; # Don't use if running through a web server
if ($params{securitycode} ne $securitycode) {
%params = ();
$querystring = "";
$securitycode = sprintf("%.14f", rand); # assign a random security code
}
# No parameters starts on "Feed" tab
if ($querystring eq "") {
$params{newtab} = "Feed";
}
#
# Delete feed if requested
#
if ($params{deletefeed}) {
my $fn = $params{deletefeednamelist};
$fn =~ s/(\W|_)//g;
unlink "$config_values{datafile}.feed.$fn.txt";
$params{feedname} = "";
}
#
# Default feed is first file
#
if (!$params{feedname}) {
my $globstr = $config_values{datafile} . ".feed.*.txt";
$globstr =~ s/ /?/g; # escape spaces (bsd_glob not available)
my $fn = (glob ($globstr))[0];
$fn =~ m/feed\.(.+?)\.txt$/;
$params{feedname} = $1;
}
$config_values{feedname} = $params{feedname};
#
# Check to see if feedname selected -- then do operation
#
foreach my $p (keys %params) { # go through all the parameters
# Select feed
if ($p =~ /^selectfeed(\w+)/) {
$config_values{feedname} = $1;
$params{feedname} = $config_values{feedname};
}
# Select and list feed
if ($p =~ /^listfeed(\w+)/) {
$config_values{feedname} = $1;
$params{feedname} = $config_values{feedname};
$params{newtab} = "Items";
$params{itemmode} = "";
}
# Select and add item to feed
if ($p =~ /^addfeed(\w+)/) {
$config_values{feedname} = $1;
$params{feedname} = $config_values{feedname};
$params{newtab} = "Items";
$params{itemmode} = "add";
}
}
if ($params{choosefeed}) {
$config_values{feedname} = $params{feednamelist};
$params{feedname} = $config_values{feedname};
}
$config_values{feedname} =~ s/(\W|_)//g; # make sure only alphanumerics
# If Add & Publish, then switch tabs and still do add
if ($params{addonepublish}) {
$params{addone} = "Add";
$params{newtab} = "Publish";
}
#
# Get current tab and switch if necessary
#
my $currenttab = $params{newtab} || $params{currenttab};
#
# Display tabs and other top stuff
#
$response .= <<"EOF";
<b>$programname</b>
<br><br>
<form name="f0" action="" method="POST">
<input type="hidden" name="securitycode" value="$securitycode">
<input type="hidden" name="currenttab" value="$currenttab">
<table cellpadding="0" cellspacing="0" width="600">
<tr>
EOF
foreach my $tab (@tablist) {
if ($currenttab eq $tab) {
$response .= <<"EOF";
<td class="tab1"> </td>
<td class="tabselected">$tab</td>
EOF
}
else {
$response .= <<"EOF";
<td class="tab1"> </td>
<td class="tab"><input type="submit" name="newtab" value="$tab"></td>
EOF
}
}
$response .= <<"EOF";
<td class="tab1" width="100%"> </td>
</tr>
<tr>
<td class="tab2left" width="1"> </td>
EOF
my $ncols = @tablist;
$ncols = $ncols * 2 + 1;
for (my $i=0; $i<$ncols-2; $i++) {
$response .= <<"EOF";
<td class="tab2"> </td>
EOF
}
$response .= <<"EOF";
<td class="tab2right"> </td>
</tr>
</table>
EOF
#
# Read in datafile (if feedname defined)
#
my %datavalues;
if ($params{feedname}) {
open (DATAFILEIN, "$config_values{datafile}.feed.$config_values{feedname}.txt");
while (<DATAFILEIN>) {
chomp;
s/\r//g; # remove CRs in case moved data from Windows
s/^\x{EF}\x{BB}\x{BF}//; # remove UTF-8 Byte Order Mark if present
my ($valname, $val) = split(/=/, $_, 2);
$datavalues{$valname} = $val;
# retrieve CR and LF's
my %tc = ("\\" => "\\", "n" => "\n", "r" => "\r");
$datavalues{$valname} =~ s/\\(\\|n|r)/$tc{$1}/eg;
}
close DATAFILEIN;
}
my $changed; # if non-zero, then a change was posted
#
# *** Update an edited item
#
if ($params{saveitem}) {
foreach my $vn (@itemtags) {
$datavalues{"$vn$params{edititemnum}"} = $params{"edit$vn"};
}
$datavalues{"itempubdate$params{edititemnum}"} = $dtstring if $params{edititempubdateusecurrent};
if (!$params{edititemguid}) { # do the automatic guid stuff
if ($params{edititemguidauto} eq 'auto') {
$datavalues{"itemguid$params{edititemnum}"} = sprintf("%s-%04d-%02d-%02d-%02d-%02d-%02d",
$config_values{feedname}, $year+1900, $mon+1, $mday, $hour, $min, $sec); # add one to zero-based month
}
elsif ($params{edititemguidauto} eq 'link') {
$datavalues{"itemguid$params{edititemnum}"} = $datavalues{"itemlink$params{edititemnum}"};
}
}
$changed++;
}
#
# *** Cancel feed edit mode without saving
#
if ($params{cancelfeededit}) {
# Nothing to do
}
#
# *** Save edited feed info
#
if ($params{savefeededit}) {
foreach my $vn (@feedtags) {
$datavalues{$vn} = $params{"feededit$vn"};
}
$changed++; # assume all changed -- doesn't check for unchanged values
}
#
# *** Save publish info
#
if ($params{savepublish}) {
# The saved password is not used as an initial value for simple security reasons.
# If blank, the initial value is blank; if not, the initial value is $placeholderpw.
# Yes -- this means you can't set that as a password except from blank, but it's unlikely a problem.
$params{editftppassword} = $datavalues{ftppassword} if ($datavalues{ftppassword} && $params{editftppassword} eq $placeholderpw);
foreach my $vn (@publishfields) {
$datavalues{$vn} = $params{"edit$vn"};
}
$datavalues{htmlabove} = $defaulthtmlabove if $params{setdefaultabove};
$datavalues{htmlitem} = $defaulthtmlitem if $params{setdefaultitem};
$datavalues{htmlbelow} = $defaulthtmlbelow if $params{setdefaultbelow};
$changed++; # assume all changed -- doesn't check for unchanged values
}
#
# *** Save options
#
if ($params{saveoptions}) {
foreach my $vn (@optionfields) {
$datavalues{$vn} = $params{"edit$vn"} || "";
}
$datavalues{maxlistitems} = 0 if $datavalues{maxlistitems} < 0; # make sure in range
$changed++; # assume all changed -- doesn't check for unchanged values
}
#
# *** Change browse values
#
if ($params{changebrowse}) {
$params{editbrowseftppassword} = $datavalues{browseftppassword}
if ($datavalues{browseftppassword} && $params{editbrowseftppassword} eq $placeholderpw);
foreach my $vn (@browsefields) {
$datavalues{$vn} = $params{"edit$vn"} || "";
}
$params{itemmode} = "enclosure";
$changed++; # assume all changed -- doesn't check for unchanged values
}
#
# *** Create a new item
#
if ($params{addone}) {
$datavalues{numitems}--
if ((0+$datavalues{optionsmaxsaved}) && $datavalues{numitems} >= $datavalues{optionsmaxsaved});
for (my $i=$datavalues{numitems}; $i > 0 ; $i--) {
foreach my $vn (@itemtags) {
$datavalues{$vn . ($i+1)} = $datavalues{$vn . $i};
}
}
$datavalues{numitems} += 1;
foreach my $vn (@itemtags) {
$datavalues{$vn . '1'} = $params{"new$vn"};
}
$datavalues{itempubdate1} = $dtstring if $params{newitempubdateusecurrent};
if (!$params{newitemguid}) { # do the automatic guid stuff
if ($params{newitemguidauto} eq 'auto') {
$datavalues{itemguid1} = sprintf("%s-%04d-%02d-%02d-%02d-%02d-%02d",
$config_values{feedname}, $year+1900, $mon+1, $mday, $hour, $min, $sec); # add one to zero-based month
}
elsif ($params{newitemguidauto} eq 'link') {
$datavalues{itemguid1} = $datavalues{itemlink1};
}
}
delete $params{listitemnum};
$changed++;
}
#
# *** Get enclosure info
#
if ($params{enclosureinfo}) {
$params{itemmode} = "enclosureinfo";
}
#
# *** Browse enclosures
#
if ($params{browseenclosure}) {
$params{itemmode} = "enclosure";
}
#
# *** Create a new feed file if requested
#
if ($params{createfeed} ||
($params{newfeedname} && !$params{editfeedinfo} &&
!$params{deletefeed} && ($currenttab eq "Feed"))) {
$config_values{feedname} = $params{newfeedname};
$config_values{feedname} =~ s/(\W|_)//g; # make sure only alphanumerics
$changed++;
%datavalues = ();
$params{editfeedinfo} = "Edit"; # Since it's empty, start in edit mode
}
#
# *** Pass along feed name, in form 0 and form 1 (two forms to help with making Enter
# *** "press" the logical button)
#
$response .= <<"EOF";
<input type="hidden" name="feedname" value="$config_values{feedname}">
</form>
<table cellpadding="0" cellspacing="0" width="600">
<tr>
<td class="ttbody">
EOF
#
# *** Look for per-item operations: Edit, Delete, Move Up, Move Down
# These have the item number coded in
#
foreach my $p (keys %params) { # go through all the parameters
# *** Edit an item
if ($p =~ /^itemedit(\d+)/) {
$params{edititemnum} = $1;
foreach my $vn (@itemtags) { # Copy each value into edit params
$params{"edit$vn"} = $datavalues{"$vn$1"};
}
$params{itemmode} = "edit"; # set mode
}
# *** Delete an item
if ($p =~ /^itemdelete(\d+)/) {
for (my $i=$1; $i < $datavalues{numitems}; $i++) {
foreach my $vn (@itemtags) { # copy each one after it down one
$datavalues{$vn . $i} = $datavalues{$vn . ($i + 1)};
}
}
foreach my $vn (@itemtags) { # Erase last one
delete $datavalues{$vn . $datavalues{numitems}};
}
if ($1 eq $datavalues{numitems} && $1 <= $params{listitemnum}) { # make sure listed page has the moved item
$params{listitemnum} -= $datavalues{maxlistitems};
$params{listitemnum} = 1 if $params{listitemnum} < 1;
}
$datavalues{numitems} -= 1;
$changed++;
}
# *** Move an item up (to a lower number)
if ($p =~ /^itemup(\d+)/) {
if ($1 > 1) {
foreach my $vn (@itemtags) { # Switch the two
my $temp = $datavalues{$vn . ($1 - 1)};
$datavalues{$vn . ($1 - 1)} = $datavalues{$vn . $1};
$datavalues{$vn . $1} = $temp;
}
if ($1 <= $params{listitemnum}) { # make sure listed page has the moved item
$params{listitemnum} -= $datavalues{maxlistitems};
$params{listitemnum} = 1 if $params{listitemnum} < 1;
}
}
$changed++;
}
# *** Move an item down (to a higher number)
if ($p =~ /^itemdown(\d+)/) {
if ($1 < $datavalues{numitems}) {
foreach my $vn (@itemtags) { # Switch the two
my $temp = $datavalues{$vn . ($1 + 1)};
$datavalues{$vn . ($1 + 1)} = $datavalues{$vn . $1};
$datavalues{$vn . $1} = $temp;
}
if ($1 + 1 >= $params{listitemnum} + $datavalues{maxlistitems}) { # make sure listed page has the moved item
$params{listitemnum} ||= 1;
$params{listitemnum} += $datavalues{maxlistitems};
}
}
$changed++;
}
# *** List a section of items
if ($p =~ /^listitem(\d+)/) {
$params{listitemnum} = $1;
}
# *** Continue from browse
if ($p =~ /^browsecontinue(\D+)(\d+)(\D*)/) {
$params{itemmode} = $1;
if ($2 > 0) { # file selected
my $prefix = $1 eq "edit" ? "edit" : "new";
my @files = split(/\|/, $params{browsefilenames});
my ($name, $len) = split(/@/, @files[$2-1]);
if ($3 eq "i") { # continue from successful info
$params{$prefix . "itemenclosuretype"} = $name;
$params{$prefix . "itemenclosurelength"} = $len;
}
else { # normal successful browse
$params{$prefix . "itemenclosureurl"} = "$datavalues{browseurlprefix}$name";
$params{$prefix . "itemenclosurelength"} = $len;
if (!$datavalues{templateenclosuretype}) { # if type doesn't have a default, try to get from server
my $ua = LWP::UserAgent->new; # try to get
$ua->agent($programname);
my $req = HTTP::Request->new(HEAD => $params{$prefix . "itemenclosureurl"});
$req->header('Accept' => '*/*');
my $res = $ua->request($req);
$params{$prefix . "itemenclosuretype"} = $res->content_type if $res->is_success;
}
}
}
}
}
#
# *** Load settings from a URL
#
my $loadfeedsettingsstr;
if ($params{loadfeedsettings}) {
my $urlsc = special_chars($params{loadfeedsettingsurl});
$loadfeedsettingsstr = <<"EOF";
<div class="sectionplain">
<div class="itemheader">LOADING FROM URL:</div>
<div class="itemdesc">$urlsc:</div>
EOF
my $ua = LWP::UserAgent->new; # try to get
$ua->agent($programname . "Settings Load");
my $req = HTTP::Request->new(GET => $params{loadfeedsettingsurl});
$req->header('Accept' => '*/*');
my $res = $ua->request($req);
if ($res->is_success) {
$loadfeedsettingsstr .= <<"EOF";
<div class="itemmisc">
<table cellpadding="0" cellspacing="0">
EOF
my @lines = split(/\n/, $res->content);
my %tc = ("\\" => "\\", "n" => "\n", "r" => "\r");
my %loadablevalues;
foreach my $of (@optionfields) { # all options are OK to load
$loadablevalues{$of} = 1;
}
my @otherloadablefields = qw(maxpublishitems mintimepublishitems publishsequence
htmlversion htmlabove htmlitem htmlbelow htmlallitems);
foreach my $of (@otherloadablefields) { # only these other fields are allowed
$loadablevalues{$of} = 1;
}
foreach my $line (@lines) {
$line =~ s/\r//g;
$line =~ s/^\x{EF}\x{BB}\x{BF}//;
my ($valname, $val) = split(/=/, $line, 2);
my $valnamesc = special_chars($valname);
$val =~ s/\\(\\|n|r)/$tc{$1}/eg;
my $valsc = special_chars($val);
$valsc =~ s/\n/<br>/g;
if ($loadablevalues{$valname}) {
$loadfeedsettingsstr .= "<tr><td><b>$valnamesc: </b></td><td>$valsc</td></tr>";
$datavalues{$valname} = $val; # overwrite with new value
$changed++;
}
else {
next unless $valname; # ignore blank lines
if ($valname =~ /^#/) { # comments start with "#", if has an "=" right after, then displayed
$loadfeedsettingsstr .= "<tr><td>comment: </td><td>$valsc</td></tr>" if ($valname eq "#");
}
else {
$loadfeedsettingsstr .= "<tr><td><b>$valnamesc: </b></td><td><i>skipped</i></td></tr>";
}
}
}
$loadfeedsettingsstr .= <<"EOF";
</table>
</div>
<div class="itemdesc">Loading complete.</div>
EOF
}
else {
my $emsg = special_chars($res->status_line);
$loadfeedsettingsstr .= <<"EOF";
<div class="itemdesc"><span class="warning"><b>Unable to load:</b></span><br>$emsg</div>
EOF
}
if (!$params{feedname}) {
$loadfeedsettingsstr .= <<"EOF";
<span class="warning"><b>Feed not set.</b></span>
EOF
}
$loadfeedsettingsstr .= <<"EOF";
</div>
EOF
}
#
# *** Write out data file if changed
#
if ($changed && $params{feedname}) {
open (DATAFILEOUT, "> $config_values{datafile}.feed.$config_values{feedname}.txt");
foreach my $vn (sort keys %datavalues) {
# escape CR and LF
my $val = $datavalues{$vn};
$val =~ s/\\/\\\\/g;
$val =~ s/\n/\\n/g;
$val =~ s/\r/\\r/g;
print DATAFILEOUT "$vn=$val\n";
}
close DATAFILEOUT;
}
#
# ******
# *
# * Output appropriate material for the tab being displayed
# *
# ******
#
my %htmlvalue; # hash to hold values as they will be inserted into the HTML
#
# Feed
#
if ($currenttab eq "Feed") {
my $fnm = $config_values{feedname} || "*** None ***";
my $rsstitlesc = special_chars($datavalues{rsstitle});
$rsstitlesc = " [$rsstitlesc]" if $rsstitlesc;
$response .= <<"EOF";
<div class="sectiondark">
<div class="pagetitle">SELECT FEED:</div>
<div class="pagefeedinfo">$fnm$rsstitlesc</div>
</div>
<div class="sectionplain">
EOF
# Go through file names and get list of feeds
my $feedlist; # list for selecting
my $feedlist2; # list for deleting
my $globstr = $config_values{datafile} . ".feed.*.txt";
$globstr =~ s/ /?/g; # escape spaces (bsd_glob not available)
# Note that "a c" and "abc" will both be found!!!
my $count;
foreach my $fname (glob ($globstr)) {
$fname =~ m/feed\.(.+?)\.txt$/;
my $sel;
$count++;