#!/usr/bin/perl
use strict;
use warnings;
use Config::Simple;
use CGI::Simple;
use CGI::Session;
use DBI;
tie my %Config, "Config::Simple", '/etc/mat.conf';
my $q = new CGI::Simple;
my $id = $q->param('id');
my $action = $q->param('action');
my $storage = $q->param('storage');
my $password = $q->param('password');
my $session = CGI::Session->new(undef, undef, {Directory =>
$Config{'session_directory'}});
sub misconfigured
{
print "Content-Type: text/plain; charset=utf-8\n\r\n\r";
print "This system is misconfigured.\n";
exit 1;
}
sub invalid_input()
{
print "Content-Type: text/plain; charset=utf-8\n\r\n\r";
print "Invalid input.\n";
exit 1;
}
sub unauthorized()
{
print "Content-Type: text/plain; charset=utf-8\n\r\n\r";
print "Not authorized!\n";
exit 1;
}
sub send_cookie()
{
print $session->header(-type => 'text/html', -charset => 'utf8');
}
sub cmd_view($)
{
my ( $id ) = @_;
my $db = DBI->connect($Config{'database'}, "", "",
{HandleError => \&misconfigured, AutoCommit => 1});
my $portion = $db->selectrow_hashref("SELECT name, storage, uri, amount, ".
"energy FROM recipes AS r JOIN inventory AS i ON i.recipe_id=r.id ".
"WHERE i.id=".$id.";");
print $id, "
", $portion->{name}, "
\n";
print $portion->{storage}, "
\n";
print $portion->{amount} ? $portion->{amount}." g
" : "", "
\n";
print $portion->{energy} ? $portion->{energy}." kJ
" : "", "
\n";
if(substr($portion->{uri}, 0, 4) eq "http") {
print ''.$portion->{uri}.'', "
\n";
} else {
print $portion->{uri}, "
\n";
}
if ($session->param('authenticated') eq "yes") {
print "\n";
for my $store ( split(" ", $Config{'relocate_stores'}) ) {
print " ".
"$store \n";
}
print "\n";
}
}
sub cmd_relocate($$)
{
my ( $id, $storage ) = @_;
my $db = DBI->connect($Config{'database'}, "", "",
{HandleError => \&misconfigured, AutoCommit => 1});
$db->do('UPDATE inventory SET storage="'.$storage.'" WHERE id='.$id);
cmd_view($id);
}
sub cmd_login($)
{
my ( $password ) = @_;
if ( $password and ($password eq $Config{'password'} )) {
$session->param('authenticated', "yes");
print "welcome.";
} else {
print "