-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.pl
executable file
·140 lines (124 loc) · 3.75 KB
/
index.pl
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
#!/usr/bin/perl -w -I.
use strict;
use Data::Dumper;
use HTML::Template;
use URI::Escape;
use JSON;
use RSSTootalizer::User;
our %FORM;
our $CURRENTUSER;
our $config = "";
open CONFIG, "rsstootalizer.conf.json" or die "Cannot open rsstootalizer.conf.json";
{
local $/ = undef;
$config = <CONFIG>;
}
close CONFIG;
$config = decode_json($config);
sub Error {{{
my $errorheadline = shift;
my $errormessage = shift;
$errormessage .= "\nStack Trace:\n";
my $i=0;
while ((my @call_details = (caller($i++))) ){
$errormessage .= $call_details[1].":".$call_details[2]." in function ".$call_details[3]."\n";
}
my $output;
if ($FORM{"mode"} eq "JSON"){
$output = HTML::Template->new(filename => "error.json", path => "static/templates", die_on_bad_params=>0);
print "Content-Type: text/plain;charset=utf8\n\n";
$errormessage =~ s/\n/\\n/g;
} else {
$output = HTML::Template->new(filename => "Error.html", path => "static/templates", die_on_bad_params=>0);
print "Content-Type: text/html;charset=utf8\n\n";
}
$output->param(status => $errorheadline, msg => $errormessage);
print $output->output();
exit(1);
}}}
sub populateAddToFORM {{{
my $key = shift;
my $value = shift;
return unless defined($value);
$key =~ s/\+/ /g;
$key = uri_unescape($key);
$key =~ s/\[\]$//;
$value =~ s/\+/ /g;
$value = uri_unescape($value);
if (exists($FORM{$key}) && $key ne "mode"){
if (ref($FORM{$key}) ne 'ARRAY'){
my $x = $FORM{$key};
delete $FORM{$key};
@{$FORM{$key}} = ($x);
}
push @{$FORM{$key}}, $value;
} else {
$FORM{$key} = $value;
}
}}}
sub populateGetFields {{{
my $tmpStr = "";
if (defined($ENV{'QUERY_STRING'})){
$tmpStr = "".$ENV{"QUERY_STRING"};
}
my @parts = split(/\&/, $tmpStr);
foreach my $part (@parts) {
my ($key, $value) = split(/\=/, $part);
&populateAddToFORM($key, $value);
}
}}}
sub populatePostFields {{{
return unless (exists($ENV{"CONTENT_LENGTH"}));
my $tmpStr;
read(STDIN, $tmpStr, $ENV{"CONTENT_LENGTH"});
my @parts = split( /\&/, $tmpStr );
foreach my $part (@parts) {
my ($key, $value) = split( /\=/, $part );
&populateAddToFORM($key, $value);
}
}}}
sub populateCookieFields {{{
my $tmpStr = "";
if (defined($ENV{'HTTP_COOKIE'})){
$tmpStr = "".$ENV{"HTTP_COOKIE"};
}
my @parts = split(/;/, $tmpStr);
foreach my $part (@parts) {
my ($key, $value) = split(/\=/, $part);
$key =~ s/^ //;
&populateAddToFORM($key, $value);
}
}}}
sub CheckCredentials {
$CURRENTUSER = RSSTootalizer::User->authenticate();
if ($CURRENTUSER){
return 1;
}
return 0;
}
$FORM{"mode"} = "Login";
&populateGetFields();
&populatePostFields();
&populateCookieFields();
# Force Unicode output
binmode STDERR, ":utf8";
binmode STDOUT, ":utf8";
my $object;
# TODO: This is a very bad solution but not as bad as an uncontrolled eval...
# The @main::modules array holds a list of all permissible values of the $main::FORM{"mode"} variable.
# If the value is not in this array, the request is not processed and an error is displayed.
my @modules = ("Login", "Logout", "OAuthLogin", "Dashboard", "Callback", "EditFeed", "About");
if (! grep {$_ eq $FORM{mode}} @modules) {
Error("Validation Error", "$FORM{mode} is not a valid module");
}
my $x = "RSSTootalizer::Website::$FORM{mode}";
eval "use $x; 1" || Error("Parse Error", "Could not include $x: $@");
eval { $object=$x->new(); } || Error("Functional Error", "This function is not implemented yet ('".$FORM{mode}."').");
if ($object->requires_authentication()) { # Mode requires user to be logged in?
unless (CheckCredentials()) {
$x = "RSSTootalizer::Website::Login";
eval "use $x; 1" || Error("Parse Error", "Could not include $x: $@");
eval { $object=$x->new(); } || Error("Functional Error", "This function is not implemented yet ('".$FORM{mode}."').");
}
}
$object->render();