#!/usr/bin/perl -wT # # get-list: A script to return a requested list # # 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 XML::DOM; use Fcntl ':flock'; # import LOCK_* constants my $query = CGI->new(); my $id = $query->param('id'); my $key = $query->param('key'); my $command = $query->param('command'); my $position = $query->param('position'); my $value = $query->param('value'); if (defined($id) and defined($key) and defined($command) and defined($value) 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 (-f $filename) { eval { my $parser = new XML::DOM::Parser; my $document = $parser->parsefile($filename); if ($command eq 'insert') { if (defined($position) and $position =~ m/^[0-9]+$/os) { my $refChild = $document->getDocumentElement->getChildNodes->[$position]; my $newChild = $document->createElement('value'); $newChild->appendChild($document->createTextNode($value)); $document->getDocumentElement->insertBefore($newChild, $refChild); } else { print "Status: 400 Bad Request\nContent-Type: text/plain\n\nInvalid or missing position.\n"; return; } } elsif ($command eq 'delete') { foreach my $oldChild ($document->getDocumentElement->getChildNodes) { if ($oldChild->hasChildNodes and $oldChild->getFirstChild->nodeValue) { $document->getDocumentElement->removeChild($oldChild); last; } } } elsif ($command eq 'append') { my $newChild = $document->createElement('value'); $newChild->appendChild($document->createTextNode($value)); $document->getDocumentElement->appendChild($newChild); } else { print "Status: 400 Bad Request\nContent-Type: text/plain\n\nInvalid command.\n"; return; } $document->printToFile($filename); $document->dispose; print "Status: 204 No Content\n"; }; die $@ if $@; # the eval is just to allow us to use return, not to catch exceptions } else { print "Status: 403 Forbidden\nContent-Type: text/plain\n\nInvalid id or key.\n"; } close($lock); } else { print <