1 package Sesse::pr0n::WebDAV;
5 use Sesse::pr0n::Common qw(error dberror);
11 my $dbh = Sesse::pr0n::Common::get_dbh();
13 $r->headers_out->{'DAV'} = "1,2";
15 # We only handle depth=0, depth=1 (cf. the RFC)
16 my $depth = $r->headers_in->{'depth'};
17 $depth = 0 if (!defined($depth));
18 if (defined($depth) && $depth ne "0" && $depth ne "1") {
19 $r->content_type('text/plain; charset="utf-8"');
21 $r->print("Invalid depth setting");
22 return Apache2::Const::OK;
25 my ($user,$takenby) = Sesse::pr0n::Common::check_access($r);
26 if (!defined($user)) {
27 return Apache2::Const::OK;
30 # Just "ping, are you alive and do you speak WebDAV"
31 if ($r->method eq "OPTIONS") {
32 $r->content_type('text/plain; charset="utf-8"');
34 $r->headers_out->{'allow'} = 'OPTIONS,PUT';
35 $r->headers_out->{'ms-author-via'} = 'DAV';
36 return Apache2::Const::OK;
39 # Directory listings et al
40 if ($r->method eq "PROPFIND") {
41 # We ignore the body, but we _must_ consume it fully before
42 # we output anything, or Squid will get seriously confused
43 $r->discard_request_body;
45 $r->content_type('text/xml; charset="utf-8"');
48 if ($r->uri =~ m#^/webdav/?$#) {
49 $r->headers_out->{'content-location'} = "/webdav/";
53 <?xml version="1.0" encoding="utf-8"?>
54 <multistatus xmlns="DAV:">
59 <resourcetype><collection/></resourcetype>
60 <getcontenttype>text/xml</getcontenttype>
62 <status>HTTP/1.1 200 OK</status>
67 # Optionally list the upload/ dir
71 <href>/webdav/upload/</href>
74 <resourcetype><collection/></resourcetype>
75 <getcontenttype>text/xml</getcontenttype>
77 <status>HTTP/1.1 200 OK</status>
82 $r->print("</multistatus>\n");
83 } elsif ($r->uri =~ m#^/webdav/upload/?$#) {
84 $r->headers_out->{'content-location'} = "/webdav/upload/";
86 # Upload root directory
88 <?xml version="1.0" encoding="utf-8"?>
89 <multistatus xmlns="DAV:">
91 <href>/webdav/upload/</href>
94 <resourcetype><collection/></resourcetype>
95 <getcontenttype>text/xml</getcontenttype>
97 <status>HTTP/1.1 200 OK</status>
102 # Optionally list all events
104 my $q = $dbh->prepare('SELECT * FROM events WHERE vhost=?') or
105 dberror($r, "Couldn't list events");
106 $q->execute($r->get_server_name) or
107 dberror($r, "Couldn't get events");
109 while (my $ref = $q->fetchrow_hashref()) {
110 my $id = $ref->{'id'};
111 my $name = $ref->{'name'};
113 $name =~ s/&/\&/g; # hack :-)
116 <href>/webdav/upload/$id/</href>
119 <resourcetype><collection/></resourcetype>
120 <getcontenttype>text/xml</getcontenttype>
121 <displayname>$name</displayname>
123 <status>HTTP/1.1 200 OK</status>
131 $r->print("</multistatus>\n");
132 } elsif ($r->uri =~ m#^/webdav/upload/([a-zA-Z0-9-]+)/?$#) {
135 $r->headers_out->{'content-location'} = "/webdav/upload/$event/";
137 # Check that we do indeed exist
138 my $ref = $dbh->selectrow_hashref('SELECT count(*) AS numev FROM events WHERE id=?',
140 if ($ref->{'numev'} != 1) {
142 $r->content_type('text/plain; charset=utf-8');
143 $r->print("Couldn't find event in database");
144 return Apache2::Const::OK;
147 # OK, list the directory
149 <?xml version="1.0" encoding="utf-8"?>
150 <multistatus xmlns="DAV:">
152 <href>/webdav/upload/$event/</href>
155 <resourcetype><collection/></resourcetype>
156 <getcontenttype>text/xml</getcontenttype>
158 <status>HTTP/1.1 200 OK</status>
163 # List all the files within too, of course :-)
165 my $q = $dbh->prepare('SELECT * FROM images WHERE event=?') or
166 dberror($r, "Couldn't list images");
167 $q->execute($event) or
168 dberror($r, "Couldn't get events");
170 while (my $ref = $q->fetchrow_hashref()) {
171 my $id = $ref->{'id'};
172 my $filename = $ref->{'filename'};
173 my $fname = Sesse::pr0n::Common::get_disk_location($r, $id);
174 my (undef, undef, undef, undef, undef, undef, undef, $size, undef, $mtime) = stat($fname)
176 $mtime = POSIX::strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime($mtime));
177 my $mime_type = Sesse::pr0n::Common::get_mimetype_from_filename($filename);
181 <href>/webdav/upload/$event/$filename</href>
185 <getcontenttype>$mime_type</getcontenttype>
186 <getcontentlength>$size</getcontentlength>
187 <getlastmodified>$mtime</getlastmodified>
189 <status>HTTP/1.1 200 OK</status>
196 # And the magical autorename folder
199 <href>/webdav/upload/$event/autorename/</href>
202 <resourcetype><collection/></resourcetype>
203 <getcontenttype>text/xml</getcontenttype>
205 <status>HTTP/1.1 200 OK</status>
211 $r->print("</multistatus>\n");
213 return Apache2::Const::OK;
214 } elsif ($r->uri =~ m#^/webdav/upload/([a-zA-Z0-9-]+)/autorename/?$#) {
215 # The autorename folder is always empty
218 $r->headers_out->{'content-location'} = "/webdav/upload/$event/autorename/";
220 # Check that we do indeed exist
221 my $ref = $dbh->selectrow_hashref('SELECT count(*) AS numev FROM events WHERE id=?',
223 if ($ref->{'numev'} != 1) {
225 $r->content_type('text/plain; charset=utf-8');
226 $r->print("Couldn't find event in database");
227 return Apache2::Const::OK;
230 # OK, list the (empty) directory
232 <?xml version="1.0" encoding="utf-8"?>
233 <multistatus xmlns="DAV:">
235 <href>/webdav/upload/$event/autorename/</href>
238 <resourcetype><collection/></resourcetype>
239 <getcontenttype>text/xml</getcontenttype>
241 <status>HTTP/1.1 200 OK</status>
247 return Apache2::Const::OK;
248 } elsif ($r->uri =~ m#^/webdav/upload/([a-zA-Z0-9-]+)/([a-zA-Z0-9._-]+)$#) {
250 my ($event, $filename) = ($1, $2);
251 my ($fname, $size, $mtime);
253 # check if we have a pending fake file for this
254 my $ref = $dbh->selectrow_hashref('SELECT count(*) AS numfiles FROM fake_files WHERE event=? AND filename=? AND expires_at > now()',
255 undef, $event, $filename);
256 if ($ref->{'numfiles'} == 1) {
257 $fname = "/dev/null";
261 ($fname, $size, $mtime) = Sesse::pr0n::Common::stat_image($r, $event, $filename);
264 if (!defined($fname)) {
266 $r->content_type('text/plain; charset=utf-8');
267 $r->print("Couldn't find file");
268 return Apache2::Const::OK;
270 my $mime_type = Sesse::pr0n::Common::get_mimetype_from_filename($filename);
272 $mtime = POSIX::strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime($mtime));
274 <?xml version="1.0" encoding="utf-8"?>
275 <multistatus xmlns="DAV:">
277 <href>/webdav/upload/$event/$filename</href>
281 <getcontenttype>$mime_type</getcontenttype>
282 <getcontentlength>$size</getcontentlength>
283 <getlastmodified>$mtime</getlastmodified>
285 <status>HTTP/1.1 200 OK</status>
290 return Apache2::Const::OK;
291 } elsif ($r->uri =~ m#^/webdav/upload/([a-zA-Z0-9-]+)/autorename/(.{1,250})$#) {
292 # stat a single file in autorename
293 my ($event, $filename) = ($1, $2);
294 my ($fname, $size, $mtime);
296 # check if we have a pending fake file for this
297 my $ref = $dbh->selectrow_hashref('SELECT count(*) AS numfiles FROM fake_files WHERE event=? AND filename=? AND expires_at > now()',
298 undef, $event, $filename);
299 if ($ref->{'numfiles'} == 1) {
300 $fname = "/dev/null";
304 # check if we have a "shadow file" for this
305 my $ref = $dbh->selectrow_hashref('SELECT id FROM shadow_files WHERE event=? AND filename=? AND expires_at > now()',
306 undef, $event, $filename);
308 ($fname, $size, $mtime) = Sesse::pr0n::Common::stat_image_from_id($r, $ref->{'id'});
312 if (!defined($fname)) {
314 $r->content_type('text/plain; charset=utf-8');
315 $r->print("Couldn't find file");
316 return Apache2::Const::OK;
318 my $mime_type = Sesse::pr0n::Common::get_mimetype_from_filename($filename);
320 $mtime = POSIX::strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime($mtime));
322 <?xml version="1.0" encoding="utf-8"?>
323 <multistatus xmlns="DAV:">
325 <href>/webdav/upload/$event/autorename/$filename</href>
329 <getcontenttype>$mime_type</getcontenttype>
330 <getcontentlength>$size</getcontentlength>
331 <getlastmodified>$mtime</getlastmodified>
333 <status>HTTP/1.1 200 OK</status>
340 $r->content_type('text/plain; charset=utf-8');
341 $r->print("Couldn't find file");
343 return Apache2::Const::OK;
346 if ($r->method eq "HEAD" or $r->method eq "GET") {
347 if ($r->uri !~ m#^/webdav/upload/([a-zA-Z0-9-]+)/(autorename/)?(.{1,250})$#) {
349 $r->content_type('text/xml; charset=utf-8');
350 $r->print("<?xml version=\"1.0\"?>\n<p>Couldn't find file</p>");
351 return Apache2::Const::OK;
354 my ($event, $autorename, $filename) = ($1, $2, $3);
356 # Check if this file really exists
357 my ($fname, $size, $mtime);
359 # check if we have a pending fake file for this
360 my $ref = $dbh->selectrow_hashref('SELECT count(*) AS numfiles FROM fake_files WHERE event=? AND filename=? AND expires_at > now()',
361 undef, $event, $filename);
362 if ($ref->{'numfiles'} == 1) {
363 $fname = "/dev/null";
367 # check if we have a "shadow file" for this
368 if (defined($autorename) && $autorename eq "autorename/") {
369 my $ref = $dbh->selectrow_hashref('SELECT id FROM shadow_files WHERE event=? AND filename=? AND expires_at > now()',
370 undef, $event, $filename);
372 ($fname, $size, $mtime) = Sesse::pr0n::Common::stat_image_from_id($r, $ref->{'id'});
374 } elsif (!defined($fname)) {
375 ($fname, $size, $mtime) = Sesse::pr0n::Common::stat_image($r, $event, $filename);
379 if (!defined($fname)) {
381 $r->content_type('text/plain; charset=utf-8');
382 $r->print("Couldn't find file");
383 return Apache2::Const::OK;
387 $r->set_content_length($size);
388 $r->set_last_modified($mtime);
390 if ($r->method eq "GET") {
391 $r->sendfile($fname);
393 return Apache2::Const::OK;
396 if ($r->method eq "PUT") {
397 if ($r->uri !~ m#^/webdav/upload/([a-zA-Z0-9-]+)/(autorename/)?(.{1,250})$#) {
399 $r->content_type('text/plain; charset=utf-8');
400 $r->print("No access");
401 return Apache2::Const::OK;
404 my ($event, $autorename, $filename) = ($1, $2, $3);
405 my $size = $r->headers_in->{'content-length'};
406 my $orig_filename = $filename;
408 # Remove evil characters
409 if ($filename =~ /[^a-zA-Z0-9._-]/) {
410 if (defined($autorename) && $autorename eq "autorename/") {
411 $filename =~ tr/a-zA-Z0-9.-/_/c;
414 $r->content_type('text/plain; charset=utf-8');
415 $r->print("Illegal characters in filename");
416 return Apache2::Const::OK;
421 # gnome-vfs and mac os x love to make zero-byte files,
424 if ($r->headers_in->{'content-length'} == 0) {
425 $dbh->do('DELETE FROM fake_files WHERE expires_at <= now() OR (event=? AND filename=?);',
426 undef, $event, $filename)
427 or dberror($r, "Couldn't prune fake_files");
428 $dbh->do('INSERT INTO fake_files (event,filename,expires_at) VALUES (?,?,now() + interval \'30 seconds\');',
429 undef, $event, $filename)
430 or dberror($r, "Couldn't add file");
431 $r->content_type('text/plain; charset="utf-8"');
434 $r->log->info("Fake upload of $event/$filename");
435 return Apache2::Const::OK;
439 my $ref = $dbh->selectrow_hashref("SELECT NEXTVAL('imageid_seq') AS id;");
440 my $newid = $ref->{'id'};
441 if (!defined($newid)) {
442 dberror($r, "Couldn't get new ID");
445 # Autorename if we need to
446 if (defined($autorename) && $autorename eq "autorename/") {
447 my $ref = $dbh->selectrow_hashref("SELECT COUNT(*) AS numfiles FROM images WHERE event=? AND filename=?",
448 undef, $event, $filename)
449 or dberror($r, "Couldn't check for existing files");
450 if ($ref->{'numfiles'} > 0) {
451 $r->log->info("Renaming $filename to $newid.jpeg");
452 $filename = "$newid.jpeg";
457 # Enable transactions and error raising temporarily
458 local $dbh->{AutoCommit} = 0;
459 local $dbh->{RaiseError} = 1;
462 # Try to insert this new file
464 $dbh->do('DELETE FROM fake_files WHERE event=? AND filename=?;',
465 undef, $event, $filename);
467 $dbh->do('INSERT INTO images (id,event,uploadedby,takenby,filename) VALUES (?,?,?,?,?);',
468 undef, $newid, $event, $user, $takenby, $filename);
470 # Now save the file to disk
471 $fname = Sesse::pr0n::Common::get_disk_location($r, $newid);
472 open NEWFILE, ">$fname"
476 my $content_length = $r->headers_in->{'content-length'};
477 if ($r->read($buf, $content_length)) {
478 print NEWFILE $buf or die "write($fname): $!";
481 close NEWFILE or die "close($fname): $!";
483 # Orient stuff correctly
484 system("/usr/bin/exifautotran", $fname) == 0
485 or die "/usr/bin/exifautotran: $!";
487 # Make cache while we're at it.
488 # Don't do it for the resource forks Mac OS X loves to upload :-(
489 if ($filename !~ /^\._/) {
490 Sesse::pr0n::Common::ensure_cached($r, $filename, $newid, -1, -1, 1, 80, 64, 320, 256, -1, -1);
493 # OK, we got this far, commit
496 $r->log->notice("Successfully wrote $event/$filename to $fname");
499 # Some error occurred, rollback and bomb out
501 dberror($r, "Transaction aborted because $@");
506 # Insert a `shadow file' we can stat the next 30 secs
507 if (defined($autorename) && $autorename eq "autorename/") {
508 $dbh->do('DELETE FROM shadow_files WHERE expires_at <= now() OR (event=? AND filename=?);',
509 undef, $event, $filename)
510 or dberror($r, "Couldn't prune shadow_files");
511 $dbh->do('INSERT INTO shadow_files (event,filename,id,expires_at) VALUES (?,?,?,now() + interval \'30 seconds\');',
512 undef, $event, $orig_filename, $newid)
513 or dberror($r, "Couldn't add shadow file");
514 $r->log->info("Added shadow entry for $event/$filename");
517 $r->content_type('text/plain; charset="utf-8"');
521 return Apache2::Const::OK;
524 # Yes, we fake locks. :-)
525 if ($r->method eq "LOCK") {
526 if ($r->uri !~ m#^/webdav/upload/([a-zA-Z0-9-]+)/(autorename/)?([a-zA-Z0-9._-]+)$#) {
528 $r->content_type('text/plain; charset=utf-8');
529 $r->print("No access");
530 return Apache2::Const::OK;
533 my ($event, $autorename, $filename) = ($1, $2, $3);
534 my $sha1 = Digest::SHA1::sha1_base64("/$event/$autorename/$filename");
537 $r->content_type('text/xml; charset=utf-8');
540 <?xml version="1.0" encoding="utf-8"?>
544 <locktype><write/></locktype>
545 <lockscope><exclusive/></lockscope>
548 <href>/webdav/upload/$event/$autorename$filename</href>
550 <timeout>Second-3600</timeout>
552 <href>opaquelocktoken:$sha1</href>
558 return Apache2::Const::OK;
561 if ($r->method eq "UNLOCK") {
562 $r->content_type('text/plain; charset="utf-8"');
566 return Apache2::Const::OK;
569 if ($r->method eq "DELETE") {
570 if ($r->uri !~ m#^/webdav/upload/([a-zA-Z0-9-]+)/(autorename/)?(\._[a-zA-Z0-9._-]+)$#) {
572 $r->content_type('text/plain; charset=utf-8');
573 $r->print("No access");
574 return Apache2::Const::OK;
577 my ($event, $autorename, $filename) = ($1, $2, $3);
578 $dbh->do('DELETE FROM images WHERE event=? AND filename=?;',
579 undef, $event, $filename)
580 or dberror($r, "Couldn't remove file");
584 $r->log->info("deleted $event/$filename");
586 return Apache2::Const::OK;
589 if ($r->method eq "MOVE" or
590 $r->method eq "MKCOL" or
591 $r->method eq "RMCOL" or
592 $r->method eq "RENAME" or
593 $r->method eq "COPY") {
594 $r->content_type('text/plain; charset="utf-8"');
596 $r->print("Sorry, you do not have access to that feature.");
597 return Apache2::Const::OK;
600 $r->content_type('text/plain; charset=utf-8');
601 $r->log->error("unknown method " . $r->method);
603 $r->print("Unknown method");
605 return Apache2::Const::OK;