bug 14504: (QA followup) fixing DelItemCheck arguments
[koha.git] / edithelp.pl
1 #!/usr/bin/perl
2
3 # Copyright 2007 Liblime Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use C4::Output;
22 use C4::Templates;
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25
26 use vars qw($debug);
27
28 BEGIN {
29         $debug = $ENV{DEBUG} || 0;
30 }
31
32 our $input = new CGI;
33
34 my $type    = $input->param('type') || '';
35 my $referer = $input->param('referer') || '';
36 my $oldreferer = $referer;
37 my $help    = $input->param('help') || '';
38 # strip any DOS-newlines that TinyMCE may have sneaked in
39 $help =~ s/\r//g;
40 my $error;
41
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {
44         template_name   => "help/edithelp.tt",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => {
49             catalogue        => 1,
50             circulate        => 1,
51             parameters       => 1,
52             borrowers        => 1,
53             permissions      => 1,
54             reserveforothers => 1,
55             reserveforself   => 1,
56             editcatalogue    => 1,
57             updatecharges    => 1,
58         },
59         debug => 1,
60     }
61 );
62
63 sub _get_filepath ($;$) {
64     my $referer = shift;
65     $referer =~ /koha\/(.*)\.pl/;
66     my $file = $1;
67     $file =~ s/[^0-9a-zA-Z_\-\/]*//g;
68     my $from = "help/$file.tt";
69     my $htdocs = C4::Context->config('intrahtdocs');
70     my ($theme, $lang, $availablethemes) = C4::Templates::themelanguage( $htdocs, $from, "intranet", $input );
71         $debug and print STDERR "help filepath: $htdocs/$theme/$lang/modules/$from";
72         return "$htdocs/$theme/$lang/modules/$from";
73 }
74
75 $type = 'create' if $type eq 'addnew';
76 if ( $type eq 'create' || $type eq 'save' ) {
77         my $file = _get_filepath($referer);
78     open my $fh, ">:encoding(utf-8)", $file;
79     if ( $fh ) {
80         # file is open write to it
81         print $fh
82             " [% INCLUDE 'help-top.inc' %]\n",
83                     $type eq 'create' ? "<div class=\"main\">\n$help\n</div>" : $help,
84             "\n[% INCLUDE 'help-bottom.inc' %]\n";
85         close $fh;
86                 print $input->redirect("/cgi-bin/koha/help.pl?url=$oldreferer");
87     }
88     else {
89         $error = "Cannot write file: '$file'";
90     }
91 }
92 elsif ( $type eq 'modify' ) {
93     # open file load data, kill include calls, pass data to the template
94         my $file = _get_filepath($referer, 1);  # 2nd argument triggers themelanguage call
95         if (! -r $file) {
96                 $error = "Cannot read file: '$file'.";
97         } else {
98                 (-w $file) or $error = 
99                         "WARNING: You will not be able to save, because your webserver cannot write to '$file'. Contact your admin about help file permissions.";
100         open (my $fh, '<:encoding(utf-8)', $file) or die "Cannot read file '$file'";    # unlikely death, since we just checked
101         my $help = '';
102         while ( <$fh> ) {
103             $help .= /\[% INCLUDE .* %\](.*)$/ ? $1 : $_;
104         }
105         close $fh;
106         $template->param( 'help' => $help );
107                 $type = 'save';
108         }
109 }
110
111 $template->param(
112     'referer' => $referer,
113     'type'    => $type,
114 );
115 ($error) and $template->param('error' => $error);
116 output_html_with_http_headers $input, "", $template->output;