Bug 24879: Add check_cookie_auth when missing
[koha.git] / cataloguing / value_builder / upload.pl
1 #!/usr/bin/perl
2
3 # Converted to new plugin style (Bug 6874/See also 13437)
4
5 # This file is part of Koha.
6 #
7 # Copyright (C) 2015 Rijksmuseum
8 # Copyright (C) 2011-2012 BibLibre
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use Modern::Perl;
24
25 # This plugin does not use the plugin launcher. It refers to tools/upload.pl.
26 # That script and template support using it as a plugin.
27
28 # If the plugin is called with the pattern [id=some_hashvalue] in the
29 # corresponding field, it starts the upload script as a search, providing
30 # the possibility to delete the uploaded file. If the field is empty, you
31 # can upload a new file.
32
33 use CGI qw ( -utf8 );
34 use C4::Auth qw( check_cookie_auth );
35 my $input = CGI->new;
36 my ($auth_status) =
37     check_cookie_auth( $input->cookie('CGISESSID'), { catalogue => 1 } );
38 if ( $auth_status ne "ok" ) {
39     print $input->header( -type => 'text/plain', -status => '403 Forbidden' );
40     exit 0;
41 }
42
43 my $builder = sub {
44     my ( $params ) = @_;
45     return <<"SCRIPT";
46 <script>
47         function Click$params->{id}(event) {
48             event.preventDefault();
49             var index = event.data.id;
50             var str = document.getElementById(index).value;
51             var myurl, term;
52             if( str && str.match(/id=([0-9a-f]+)/) ) {
53                 term = RegExp.\$1;
54                 myurl = '../tools/upload.pl?op=search&index='+index+'&term='+term+'&plugin=1';
55             } else {
56                 myurl = '../tools/upload.pl?op=new&index='+index+'&plugin=1';
57             }
58             window.open( myurl, 'tag_editor', 'width=800,height=400,toolbar=false,scrollbars=yes' );
59         }
60 </script>
61 SCRIPT
62 };
63
64 return { builder => $builder };