| 1 | |
|---|
| 2 | |
|---|
| 3 | use warnings; |
|---|
| 4 | use strict; |
|---|
| 5 | |
|---|
| 6 | use CGI; |
|---|
| 7 | use Config::Tiny; |
|---|
| 8 | use OpenGuides::Template; |
|---|
| 9 | use OpenGuides::Utils; |
|---|
| 10 | use URI::Escape; |
|---|
| 11 | |
|---|
| 12 | my @badchars = qw( & ? ); |
|---|
| 13 | push @badchars, '#'; |
|---|
| 14 | |
|---|
| 15 | my $q = CGI->new; |
|---|
| 16 | my $config = Config::Tiny->read('wiki.conf'); |
|---|
| 17 | my $wiki = OpenGuides::Utils->make_wiki_object( config => $config ); |
|---|
| 18 | |
|---|
| 19 | my $pagename = $q->param("pagename") || ""; |
|---|
| 20 | $pagename =~ s/^\s*//; |
|---|
| 21 | $pagename =~ s/\s*$//; |
|---|
| 22 | |
|---|
| 23 | my $action = $q->param("action") || ""; |
|---|
| 24 | |
|---|
| 25 | if ( $action eq "makepage" ) { |
|---|
| 26 | make_page(); |
|---|
| 27 | } else { |
|---|
| 28 | show_form(); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | exit 0; |
|---|
| 32 | |
|---|
| 33 | sub show_form { |
|---|
| 34 | print OpenGuides::Template->output( wiki => $wiki, |
|---|
| 35 | config => $config, |
|---|
| 36 | template => "newpage.tt", |
|---|
| 37 | vars => { |
|---|
| 38 | not_editable => 1, |
|---|
| 39 | not_deletable => 1, |
|---|
| 40 | deter_robots => 1, |
|---|
| 41 | disallowed_chars => \@badchars, |
|---|
| 42 | pagename => $pagename } |
|---|
| 43 | ); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | sub make_page { |
|---|
| 47 | |
|---|
| 48 | unless ( $pagename ) { |
|---|
| 49 | print OpenGuides::Template->output( |
|---|
| 50 | wiki => $wiki, |
|---|
| 51 | config => $config, |
|---|
| 52 | template => "error.tt", |
|---|
| 53 | vars => { not_editable => 1, |
|---|
| 54 | not_deletable => 1, |
|---|
| 55 | deter_robots => 1, |
|---|
| 56 | message => "Please enter a page name!", |
|---|
| 57 | return_url => "newpage.cgi" } ); |
|---|
| 58 | exit 0; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | my %badhash = map { $_ => 1 } @badchars; |
|---|
| 63 | my @naughty; |
|---|
| 64 | foreach my $i ( 0 .. (length $pagename) - 1 ) { |
|---|
| 65 | my $char = substr( $pagename, $i, 1 ); |
|---|
| 66 | push @naughty, $char if $badhash{$char}; |
|---|
| 67 | } |
|---|
| 68 | if ( scalar @naughty ) { |
|---|
| 69 | my $message = "Page name $pagename contains disallowed characters"; |
|---|
| 70 | print OpenGuides::Template->output( |
|---|
| 71 | wiki => $wiki, |
|---|
| 72 | config => $config, |
|---|
| 73 | template => "error.tt", |
|---|
| 74 | vars => { |
|---|
| 75 | pagename => $pagename, |
|---|
| 76 | not_editable => 1, |
|---|
| 77 | not_deletable => 1, |
|---|
| 78 | deter_robots => 1, |
|---|
| 79 | message => $message, |
|---|
| 80 | return_url => "newpage.cgi?pagename=" . uri_escape($pagename) |
|---|
| 81 | } |
|---|
| 82 | ); |
|---|
| 83 | exit 0; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | my $node_param = $wiki->formatter->node_name_to_node_param($pagename); |
|---|
| 88 | print "Location: ".$config->{_}->{script_url}.$config->{_}->{script_name}."?action=edit;id=$node_param\n\n"; |
|---|
| 89 | exit 0; |
|---|
| 90 | } |
|---|
| 91 | |
|---|