#!/usr/bin/perl -wT # # create-list: A script to create a list for use with the other scripts # # Copyright (c) 2004 by Ian Hickson # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA use strict; # be anal about stuff use CGI; # oh yeah, this is a CGI script use Digest::MD5; # allow us to safely encode user-given data use POSIX; # for strftime use Fcntl ':flock'; # import LOCK_* constants my $query = CGI->new(); my $id = $query->param('id'); my $key = $query->param('key'); my $note = $query->param('note'); if (defined($id) and defined($key) and defined($note) and $query->request_method eq 'POST') { my $md5 = Digest::MD5->new; $md5->add($id); $md5->add('.'); $md5->add($key); my $filename = '.data/'.($md5->hexdigest); open(my $lock, '+<', '.lock') or die $!; flock($lock, LOCK_EX); if (not -f $filename) { my $eid = escape($id); my $ekey = escape($key); my $enote = escape($note); my $edate = escape(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime time)); my $eip = escape($ENV{REMOTE_ADDR}); eval { open(my $file, '>', $filename) or die $!; print $file "" or die $!; close $file or die $!; }; if ($@) { print "Status: 500 Internal Error\nContent-Type: text/plain\n\nFailed to create list.\n$@\n"; } else { my $uri = $ENV{SCRIPT_URI}; $uri =~ s/create-list$/get-list/os; print "Status: 201 Created\nLocation: $uri?id=$id&key=$key\nContent-Type: text/plain\n\nCreated. Use the get-list and create-list scripts to access your list, e.g.:\n $uri?id=$id&key=$key\n"; } } else { print "Status: 409 Conflict\nContent-Type: text/plain\n\nA list with that id and key already exists.\n"; } close($lock); } else { $id = defined $id ? " (you provided '$id')" : ''; $key = defined $id ? " (you provided '$key')" : ''; $note = defined $id ? "\n (you provided '$note')" : ''; print </>/gos; return $_; }