View | Details | Raw Unified | Return to bug 6874
Collapse All | Expand All

(-)a/C4/UploadedFiles.pm (-22 / +87 lines)
Lines 183-226 sub UploadFile { Link Here
183
    return;
183
    return;
184
}
184
}
185
185
186
=head2 DanglingEntry
187
188
    C4::UploadedFiles::DanglingEntry($id,$isfileuploadurl);
189
190
Determine if a entry is dangling.
191
192
Returns: 2 == no db entry
193
         1 == no file
194
         0 == both a file and db entry.
195
        -1 == N/A (undef id / non-file-upload URL)
196
197
=cut
198
199
sub DanglingEntry {
200
    my ($id,$isfileuploadurl) = @_;
201
    my $retval;
202
203
    if (defined($id)) {
204
        my $file = GetUploadedFile($id);
205
        if($file) {
206
            my $file_path = $file->{filepath};
207
            my $file_deleted = 0;
208
            unless( -f $file_path ) {
209
                $retval = 1;
210
            } else {
211
                $retval = 0;
212
            }
213
        }
214
        else {
215
            if ( $isfileuploadurl ) {
216
                $retval = 2;
217
            }
218
            else {
219
                $retval = -1;
220
            }
221
        }
222
    }
223
    else {
224
        $retval = -1;
225
    }
226
    return $retval;
227
}
228
186
=head2 DelUploadedFile
229
=head2 DelUploadedFile
187
230
188
    C4::UploadedFiles::DelUploadedFile($id);
231
    C4::UploadedFiles::DelUploadedFile($id);
189
232
190
Remove a previously uploaded file, given its id.
233
Remove a previously uploaded file, given its id.
191
234
192
Returns a false value if an error occurs.
235
Returns: 1 == file deleted
236
         0 == file not deleted
237
         -1== no file to delete / no meaninful id passed
193
238
194
=cut
239
=cut
195
240
196
sub DelUploadedFile {
241
sub DelUploadedFile {
197
    my ($id) = @_;
242
    my ($id) = @_;
198
243
    my $retval;
199
    my $file = GetUploadedFile($id);
244
200
    if($file) {
245
    if ($id) {
201
        my $file_path = $file->{filepath};
246
        my $file = GetUploadedFile($id);
202
        my $file_deleted = 0;
247
        if($file) {
203
        unless( -f $file_path ) {
248
            my $file_path = $file->{filepath};
204
            warn "Id $file->{id} is in database but not in filesystem, removing id from database";
249
            my $file_deleted = 0;
205
            $file_deleted = 1;
250
            unless( -f $file_path ) {
206
        } else {
251
                warn "Id $file->{id} is in database but not in filesystem, removing id from database";
207
            if(unlink $file_path) {
208
                $file_deleted = 1;
252
                $file_deleted = 1;
253
            } else {
254
                if(unlink $file_path) {
255
                    $file_deleted = 1;
256
                }
209
            }
257
            }
210
        }
211
258
212
        unless($file_deleted) {
259
            unless($file_deleted) {
213
            warn "File $file_path cannot be deleted: $!";
260
                warn "File $file_path cannot be deleted: $!";
214
        }
261
            }
215
262
216
        my $dbh = C4::Context->dbh;
263
            my $dbh = C4::Context->dbh;
217
        my $query = qq{
264
            my $query = qq{
218
            DELETE FROM uploaded_files
265
                DELETE FROM uploaded_files
219
            WHERE id = ?
266
                WHERE id = ?
220
        };
267
            };
221
        my $sth = $dbh->prepare($query);
268
            my $sth = $dbh->prepare($query);
222
        return $sth->execute($id);
269
            my $numrows = $sth->execute($id);
270
            # if either a DB entry or file was deleted,
271
            # then clearly we have a deletion.
272
            if ($numrows>0 || $file_deleted==1) {
273
                $retval = 1;
274
            }
275
            else {
276
                $retval = 0;
277
            }
278
        }
279
        else {
280
            warn "There was no file for id=($id)";
281
            $retval = -1;
282
        }
283
    }
284
    else {
285
        warn "DelUploadFile called with no id.";
286
        $retval = -1;
223
    }
287
    }
288
    return $retval;
224
}
289
}
225
290
226
1;
291
1;
(-)a/cataloguing/value_builder/upload.pl (-10 / +32 lines)
Lines 46-55 sub plugin_javascript { Link Here
46
46
47
        function Clic$function_name(index) {
47
        function Clic$function_name(index) {
48
            var id = document.getElementById(index).value;
48
            var id = document.getElementById(index).value;
49
            var IsFileUploadUrl=0;
50
            if (id.match(/opac-retrieve-file/)) {
51
                IsFileUploadUrl=1;
52
            }
49
            if(id.match(/id=([0-9a-f]+)/)){
53
            if(id.match(/id=([0-9a-f]+)/)){
50
                id = RegExp.\$1;
54
                id = RegExp.\$1;
51
            }
55
            }
52
            window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=upload.pl&index=\"+index+\"&id=\"+id, 'upload', 'width=600,height=400,toolbar=false,scrollbars=no');
56
            var newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=upload.pl&index=\"+index+\"&id=\"+id+\"&from_popup=0\"+\"&IsFileUploadUrl=\"+IsFileUploadUrl, 'upload', 'width=600,height=400,toolbar=false,scrollbars=no');
57
            newin.focus();
53
58
54
        }
59
        }
55
    </script>
60
    </script>
Lines 64-73 sub plugin { Link Here
64
    my $id = $input->param('id');
69
    my $id = $input->param('id');
65
    my $delete = $input->param('delete');
70
    my $delete = $input->param('delete');
66
    my $uploaded_file = $input->param('uploaded_file');
71
    my $uploaded_file = $input->param('uploaded_file');
67
72
    my $from_popup = $input->param('from_popup');
68
    my $template_name = ($id || $delete)
73
    my $isfileuploadurl = $input->param('IsFileUploadUrl');
69
                    ? "upload_delete_file.tt"
74
    my $dangling = C4::UploadedFiles::DanglingEntry($id,$isfileuploadurl);
70
                    : "upload.tt";
75
    my $template_name;
76
    if ($delete || ($id && ($dangling==0 || $dangling==1))) {
77
        $template_name = "upload_delete_file.tt";
78
    }
79
    else {
80
        $template_name = "upload.tt";
81
    }
71
82
72
    my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
83
    my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73
        {   template_name   => "cataloguing/value_builder/$template_name",
84
        {   template_name   => "cataloguing/value_builder/$template_name",
Lines 79-84 sub plugin { Link Here
79
        }
90
        }
80
    );
91
    );
81
92
93
    if ($dangling==2) {
94
        $template->param( dangling => 1 );
95
    }
96
82
    # Dealing with the uploaded file
97
    # Dealing with the uploaded file
83
    my $dir = $input->param('dir');
98
    my $dir = $input->param('dir');
84
    if ($uploaded_file and $dir) {
99
    if ($uploaded_file and $dir) {
Lines 97-110 sub plugin { Link Here
97
        } else {
112
        } else {
98
            $template->param(error => 1);
113
            $template->param(error => 1);
99
        }
114
        }
100
    } elsif ($delete || $id) {
115
    } elsif ($delete || ($id && ($dangling==0 || $dangling==1))) {
101
        # If there's already a file uploaded for this field,
116
        # If there's already a file uploaded for this field,
102
        # We handle its deletion
117
        # We handle its deletion
103
        if ($delete) {
118
        if ($delete) {
104
            if(C4::UploadedFiles::DelUploadedFile($id)) {;
119
            if(C4::UploadedFiles::DelUploadedFile($id)==0) {;
105
                $template->param(success => 1);
106
            } else {
107
                $template->param(error => 1);
120
                $template->param(error => 1);
121
            } else {
122
                $template->param(success => 1);
108
            }
123
            }
109
        }
124
        }
110
    } else {
125
    } else {
Lines 129-142 sub plugin { Link Here
129
            $template->param( error_upload_path_not_configured => 1 );
144
            $template->param( error_upload_path_not_configured => 1 );
130
        }
145
        }
131
146
147
        if (!$uploaded_file && !$dir && $from_popup) {
148
            $template->param(error_nothing_selected => 1);
149
        }
150
        elsif (!$uploaded_file && $dir) {
151
            $template->param(error_no_file_selected => 1);
152
        }
132
        if ($uploaded_file and not $dir) {
153
        if ($uploaded_file and not $dir) {
133
            $template->param(error_no_dir_selected => 1);
154
            $template->param(error_no_dir_selected => 1);
134
        }
155
        }
156
135
    }
157
    }
136
158
137
    $template->param(
159
    $template->param(
138
        index => $index,
160
        index => $index,
139
        id => $id
161
        id => $id,
140
    );
162
    );
141
163
142
    output_html_with_http_headers $input, $cookie, $template->output;
164
    output_html_with_http_headers $input, $cookie, $template->output;
(-)a/installer/data/mysql/kohastructure.sql (-1 / +1 lines)
Lines 3351-3357 CREATE TABLE IF NOT EXISTS `borrower_modifications` ( Link Here
3351
-- Table structure for table uploaded_files
3351
-- Table structure for table uploaded_files
3352
--
3352
--
3353
3353
3354
DROP TABLE IF EXISTS uploaded_files
3354
DROP TABLE IF EXISTS uploaded_files;
3355
CREATE TABLE uploaded_files (
3355
CREATE TABLE uploaded_files (
3356
    id CHAR(40) NOT NULL PRIMARY KEY,
3356
    id CHAR(40) NOT NULL PRIMARY KEY,
3357
    filename TEXT NOT NULL,
3357
    filename TEXT NOT NULL,
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/value_builder/upload.tt (-15 / +40 lines)
Lines 7-24 Link Here
7
    <script type="text/javascript" src="[% interface %]/lib/jquery/jquery.js"></script>
7
    <script type="text/javascript" src="[% interface %]/lib/jquery/jquery.js"></script>
8
    <link rel="stylesheet" type="text/css" href="[% themelang %]/css/staff-global.css" />
8
    <link rel="stylesheet" type="text/css" href="[% themelang %]/css/staff-global.css" />
9
    <script type="text/javascript">
9
    <script type="text/javascript">
10
      function _(s) { return s; }
10
      function ValidateForm() {
11
      $(document).ready(function() {
11
        var filename = document.forms["UploadForm"]["uploaded_file"].value;
12
        var selected = 0;
13
        var value;
12
        $('form').each(function() {
14
        $('form').each(function() {
13
          $(this).submit(function() {
15
          value = $(this).find('input[type="radio"][name="dir"]:checked').val();
14
            var value = $(this).find('input[type="radio"][name="dir"]:checked').val();
16
          if (value) {
15
            if (!value) {
17
            selected = 1;
16
              alert(_("Please select the destination of file"));
18
          }
17
              return false;
19
        });
18
            }
20
        if (!filename && !selected) {
19
          });
21
          alert("Please select a file and its destination.");
20
        })
22
          return false;
21
      });
23
        }
24
        else if (!filename) {
25
          alert("Please select a file to upload.");
26
          return false;
27
        }
28
        else if (!selected) {
29
          alert("Please select a file destination.");
30
          return false;
31
        }
32
        else {
33
          return true;
34
        }
35
      }
22
    </script>
36
    </script>
23
37
24
</head>
38
</head>
Lines 73-89 Link Here
73
          <p>Configuration variable 'upload_path' is not configured.</p>
87
          <p>Configuration variable 'upload_path' is not configured.</p>
74
          <p>Please configure it in your koha-conf.xml</p>
88
          <p>Please configure it in your koha-conf.xml</p>
75
        [% ELSE %]
89
        [% ELSE %]
90
          [% IF (error_nothing_selected) %]
91
              <p class="error">Error: You have to choose the file to upload and select where to upload the file.</p>
92
          [% END %]
93
          [% IF (error_no_file_selected) %]
94
              <p class="error">Error: You have to choose the file to upload.</p>
95
          [% END %]
76
          [% IF (error_no_dir_selected) %]
96
          [% IF (error_no_dir_selected) %]
77
              <p class="error">Error: You have to select the destination of uploaded file.<p>
97
              <p class="error">Error: You have to select where to upload the file.</p>
98
          [% END %]
99
          [% IF (dangling) %]
100
              <p class="error">Error: The URL has no file to retrieve.</p>
78
          [% END %]
101
          [% END %]
79
          <h2>Please select the file to upload : </h2>
102
          <h2>Please select the file to upload : </h2>
80
          <form method="post" enctype="multipart/form-data" action="/cgi-bin/koha/cataloguing/plugin_launcher.pl">
103
          <form name="UploadForm" method="post" enctype="multipart/form-data" action="/cgi-bin/koha/cataloguing/plugin_launcher.pl" onsubmit="return ValidateForm()">
81
              [% filefield %]
104
              [% filefield %]
82
              <h3>Choose where to upload file</h3>
105
              <h3>Choose where to upload the file</h3>
83
              [% INCLUDE list_dirs dirs = dirs_tree %]
106
              [% INCLUDE list_dirs dirs = dirs_tree %]
107
              <input type="hidden" name="from_popup" value="1" />
84
              <input type="hidden" name="plugin_name" value="upload.pl" />
108
              <input type="hidden" name="plugin_name" value="upload.pl" />
85
              <input type="hidden" name="index" value="[% index %]" />
109
              <input type="hidden" name="index" value="[% index %]" />
86
              <input type="submit">
110
              <input type="submit" value="Upload file">
111
              <input type="button" value="Cancel" onclick="window.close();" />
87
          </form>
112
          </form>
88
        [% END %]
113
        [% END %]
89
    [% END %]
114
    [% END %]
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/value_builder/upload_delete_file.tt (-1 / +1 lines)
Lines 4-10 Link Here
4
<head>
4
<head>
5
    <title>Upload plugin</title>
5
    <title>Upload plugin</title>
6
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
    <script type="text/javascript" src="[% themelang %]/lib/jquery/jquery.js"></script>
7
    <script type="text/javascript" src="[% interface %]/lib/jquery/jquery.js"></script>
8
    <link rel="stylesheet" type="text/css" href="[% themelang %]/css/staff-global.css" />
8
    <link rel="stylesheet" type="text/css" href="[% themelang %]/css/staff-global.css" />
9
    <script type="text/javascript">
9
    <script type="text/javascript">
10
        //<![CDATA[
10
        //<![CDATA[
(-)a/t/db_dependent/UploadedFiles.t (-3 / +12 lines)
Lines 3-9 Link Here
3
use Modern::Perl;
3
use Modern::Perl;
4
use File::Temp qw/ tempdir /;
4
use File::Temp qw/ tempdir /;
5
use Test::CGI::Multipart;
5
use Test::CGI::Multipart;
6
use Test::More tests => 11;
6
use Test::More tests => 15;
7
7
8
use t::lib::Mocks;
8
use t::lib::Mocks;
9
9
Lines 35-41 foreach my $key (qw(id filename filepath dir)) { Link Here
35
35
36
ok(-e $file->{filepath}, "File $file->{filepath} exists");
36
ok(-e $file->{filepath}, "File $file->{filepath} exists");
37
37
38
ok(C4::UploadedFiles::DelUploadedFile($id), "DelUploadedFile($id) returned true");
38
ok(C4::UploadedFiles::DanglingEntry()==-1, "DanglingEntry() returned -1 as expected.");
39
ok(C4::UploadedFiles::DanglingEntry($id)==0, "DanglingEntry($id) returned 0 as expected.");
40
unlink ($file->{filepath});
41
ok(C4::UploadedFiles::DanglingEntry($id)==1, "DanglingEntry($id) returned 1 as expected.");
42
43
open my $fh,">",($file->{filepath});
44
print $fh "";
45
close $fh;
46
47
ok(C4::UploadedFiles::DelUploadedFile($id)==1, "DelUploadedFile($id) returned 1.");
48
ok(C4::UploadedFiles::DelUploadedFile($id)==-1, "DelUploadedFile($id) returned -1 as expected.");
39
ok(! -e $file->{filepath}, "File $file->{filepath} does not exist anymore");
49
ok(! -e $file->{filepath}, "File $file->{filepath} does not exist anymore");
40
50
41
is(C4::UploadedFiles::UploadFile($testfilename, '../', $testfile_fh->handle), undef, 'UploadFile with $dir containing ".." return undef');
51
is(C4::UploadedFiles::UploadFile($testfilename, '../', $testfile_fh->handle), undef, 'UploadFile with $dir containing ".." return undef');
42
- 

Return to bug 6874