summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcos <cos>2015-01-10 22:13:05 +0100
committercos <cos>2015-01-10 22:13:05 +0100
commit56cc3091ed3074c6deca5345e5ce775c6077bf13 (patch)
treeedb5285e5923586314788a3e1af3f02441e32b52
parentf4ebbc7557f3d97ef5fd25c9a7052532e33a7470 (diff)
parent7ac05dbbea8543e4b1e1ac3f46779b7ad5191a81 (diff)
downloadmat-56cc3091ed3074c6deca5345e5ce775c6077bf13.zip
Merge branch 'feature/web-relocate'
-rw-r--r--Makefile2
-rwxr-xr-xcgi-bin/mat.cgi86
2 files changed, 77 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index a759288..8acae4d 100644
--- a/Makefile
+++ b/Makefile
@@ -19,3 +19,5 @@ all:
install: $(INSTALLDIRS)
install -d $(DESTDIR)/usr/bin/
install mat $(DESTDIR)/usr/bin/
+ install -d $(DESTDIR)/usr/lib/netizen-mat/
+ install authenticate_session $(DESTDIR)/usr/lib/netizen-mat/
diff --git a/cgi-bin/mat.cgi b/cgi-bin/mat.cgi
index 7b31e87..793671a 100755
--- a/cgi-bin/mat.cgi
+++ b/cgi-bin/mat.cgi
@@ -5,6 +5,7 @@ use warnings;
use Config::Simple;
use CGI::Simple;
+use CGI::Session;
use DBI;
tie my %Config, "Config::Simple", '/etc/mat.conf';
@@ -12,6 +13,9 @@ 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 $session = CGI::Session->new(undef, undef, {Directory =>
+ $Config{'session_directory'}});
sub misconfigured
{
@@ -27,20 +31,80 @@ sub invalid_input()
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 $recipe_row = $db->selectrow_arrayref("SELECT name, storage, uri FROM ".
+ "recipes AS r JOIN inventory AS i ON i.recipe_id=r.id WHERE i.id=".$id.
+ ";");
+
+ print $id, "<br />", $$recipe_row[0], "<br />", $$recipe_row[1], "<br />";
+ if(substr($$recipe_row[2], 0, 4) eq "http") {
+ print '<a href="'.$$recipe_row[2].'">'.$$recipe_row[2].'</a>', "<br />";
+ } else {
+ print $$recipe_row[2], "<br />";
+ }
+ if ($session->param('authenticated') eq "yes") {
+ print "<font size=20>\n";
+ for my $store ( split(" ", $Config{'relocate_stores'}) ) {
+ print " <a href=".$Config{'label_id_prefix'}."$id/relocate/$store>".
+ "$store</a> \n";
+ }
+ print "</font>";
+ }
+}
+
+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);
+}
+
### MAIN PROGRAM ##############################################################
misconfigured unless ($Config{'database'});
+misconfigured unless $session;
invalid_input unless ($id and $action);
-invalid_input unless (($id =~ m/^[0-9]+$/) and ($action =~ m/^view$/));
-
-my $db = DBI->connect($Config{'database'}, "", "",
- {HandleError => \&misconfigured, AutoCommit => 1});
+invalid_input unless (($id =~ m/^[0-9]+$/) and ($action =~ m/^view|relocate$/) and
+ ($storage =~ /^[a-z0-9]*$/));
+send_cookie;
-my $recipe_row = $db->selectrow_arrayref("SELECT name, storage, uri FROM ".
- "recipes AS r JOIN inventory AS i ON i.recipe_id=r.id WHERE i.id=".$id.
- ";");
-
-print "Content-Type: text/plain; charset=utf-8\n\r\n\r";
-print $id, "\n", $$recipe_row[0], "\n", $$recipe_row[1], "\n",
- $$recipe_row[2], "\n";
+for ($action) {
+ if (/^view$/) {
+ cmd_view($id);
+ }
+ elsif (/^relocate$/) {
+ if ($session->param('authenticated') eq "yes") {
+ $session->expire("5y");
+ cmd_relocate($id, $storage);
+ } else {
+ unauthorized();
+ }
+ }
+ else {
+ invalid_input;
+ }
+}