3 # Copyright 2007 Liblime Ltd
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 $debug = $ENV{DEBUG} || 0;
35 my $type = $input->param('type') || '';
36 my $referer = $input->param('referer') || '';
37 my $oldreferer = $referer;
38 my $help = $input->param('help') || '';
39 # strip any DOS-newlines that TinyMCE may have sneaked in
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45 template_name => "help/edithelp.tt",
55 reserveforothers => 1,
65 sub _get_filepath ($;$) {
67 $referer =~ /koha\/(.*)\.pl/;
69 $file =~ s/[^0-9a-zA-Z_\-\/]*//g;
70 my $from = "help/$file.tt";
71 my $htdocs = C4::Context->config('intrahtdocs');
72 my ($theme, $lang, $availablethemes) = C4::Templates::themelanguage( $htdocs, $from, "intranet", $input );
73 $debug and print STDERR "help filepath: $htdocs/$theme/$lang/modules/$from";
74 return "$htdocs/$theme/$lang/modules/$from";
77 $type = 'create' if $type eq 'addnew';
78 if ( $type eq 'create' || $type eq 'save' ) {
79 my $file = _get_filepath($referer);
80 open my $fh, ">", $file;
82 # file is open write to it
84 " [% INCLUDE 'help-top.inc' %]\n",
85 $type eq 'create' ? "<div class=\"main\">\n$help\n</div>" : $help,
86 "\n[% INCLUDE 'help-bottom.inc' %]\n";
88 print $input->redirect("/cgi-bin/koha/help.pl?url=$oldreferer");
91 $error = "Cannot write file: '$file'";
94 elsif ( $type eq 'modify' ) {
95 # open file load data, kill include calls, pass data to the template
96 my $file = _get_filepath($referer, 1); # 2nd argument triggers themelanguage call
98 $error = "Cannot read file: '$file'.";
100 (-w $file) or $error =
101 "WARNING: You will not be able to save, because your webserver cannot write to '$file'. Contact your admin about help file permissions.";
102 open (my $fh, '<', $file) or die "Cannot read file '$file'"; # unlikely death, since we just checked
105 $help .= /\[% INCLUDE .* %\](.*)$/ ? $1 : $_;
108 $template->param( 'help' => $help );
114 'referer' => $referer,
117 ($error) and $template->param('error' => $error);
118 output_html_with_http_headers $input, "", $template->output;