From 431d138784ea13af9a26a16780d52b3385ab0b21 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 22 Sep 2014 12:00:06 +0000 Subject: moved cgi stuff to rh-bin diff --git a/README b/README index 98fa91f..a11d503 100644 --- a/README +++ b/README @@ -31,7 +31,7 @@ add the following to the virtualhost config: Include /etc/rivendell/apache.conf - + SetHandler perl-script PerlResponseHandler ModPerl::Registry PerlOptions +ParseHeaders diff --git a/authtoken.json b/authtoken.json index c65eb64..94d5042 100755 --- a/authtoken.json +++ b/authtoken.json @@ -2,7 +2,7 @@ use strict; use File::Basename; -use lib dirname( __FILE__ ) . '/lib'; +use lib dirname( __FILE__ ) . '/rh-bin'; use rddb; my $status = 'ERROR'; diff --git a/js/shows.js b/js/shows.js index 9f8170f..6b272bf 100644 --- a/js/shows.js +++ b/js/shows.js @@ -158,7 +158,7 @@ function shows_init() { shows_currentid = sessionStorage.getItem("shows_currentid"); shows_list = []; data = { LOGIN_NAME: auth_username, PASSWORD: auth_token }; - $.post("/listdropboxes.cgi", data, shows_updateList, "xml") + $.post("/rh-bin/listdropboxes.cgi", data, shows_updateList, "xml") } function shows_cleanup() { diff --git a/lib/rddb.pm b/lib/rddb.pm deleted file mode 100755 index 34321d2..0000000 --- a/lib/rddb.pm +++ /dev/null @@ -1,160 +0,0 @@ -#!/usr/bin/perl - -use strict; -use Config::IniFiles; -use DBI; - -package rddb; - - -sub opendb -{ - my $RD_CONF = "/etc/rd.conf"; - my $cfg = Config::IniFiles->new(-file => $RD_CONF) - or return (undef , 'ERROR', "Config File Error: " . join("\n", @Config::IniFiles::errors)); - - my $dbhost = $cfg->val('mySQL', 'Hostname'); - my $dbname = $cfg->val('mySQL', 'Database'); - my $dbuser = $cfg->val('mySQL', 'Loginname'); - my $dbpasswd = $cfg->val('mySQL', 'Password'); - - my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost","$dbuser","$dbpasswd") - or return (undef, 'ERROR', "Database Error: " . $DBI::errstr); - - $dbh->do(qq{SET CHARACTER SET utf8;}) - or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); - - return ($dbh, 'OK', 'success'); -} - -sub closedb -{ - my $dbh = shift; - $dbh->disconnect(); -} - -sub get_token -{ - my ($dbh, $username) = @_; - - my $sql = qq{select PASSWORD from USERS where LOGIN_NAME = ?;}; - my $sth = $dbh->prepare($sql) - or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); - - $sth->execute($username) - or return (undef, 'ERROR', "Database Error: " . $sth->errstr); - - my ($token) = $sth->fetchrow_array; - $sth->finish(); - - unless(defined $token) { - return (undef, 'ERROR', "user '" . $username . "' not known by rivendell") - } - return ($token, 'OK', 'success'); -} - -sub check_token -{ - my ($dbh, $username, $token) = @_; - - my $sql = qq{select PASSWORD from USERS where LOGIN_NAME = ?;}; - my $sth = $dbh->prepare($sql) - or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); - - $sth->execute($username) - or return (undef, 'ERROR', "Database Error: " . $sth->errstr); - - my ($token_result) = $sth->fetchrow_array; - $sth->finish(); - - unless(defined $token_result) { - return (undef, 'ERROR', "user '" . $username . "' not known by rivendell") - } - - if($token_result eq $token) { - return (1, 'OK', 'success'); - } - return (0, 'ERROR', "wrong password"); -} - -sub get_showtitle_and_log -{ - my ($dbh, $showid) = @_; - - my $sql = qq{select TITLE,MACROS from CART where NUMBER = ?;}; - my $sth = $dbh->prepare($sql) - or return (undef, undef, 'ERROR', "Database Error: " . $dbh->errstr); - - $sth->execute($showid) - or return (undef, undef, 'ERROR', "Database Error: " . $sth->errstr); - - my ($title, $macros) = $sth->fetchrow_array; - $sth->finish(); - - unless(defined $title) { - return (undef, undef, 'ERROR', "Show with ID=" . $showid . " not found!") - } - unless(defined $macros) { - return (undef, undef, 'ERROR', "Show with ID=" . $showid . " has no macro!"); - } - - unless($macros =~ /^LL 1 ([^ ]+) 0\!$/) { - return (undef, undef, 'ERROR', "Show with ID=" . $showid . " has invalid macro: '" . $macros . "'"); - } - my $log = $1; - - return ($title, $log, 'OK', 'success'); -} - -sub get_dropboxes -{ - my ($dbh, $username) = @_; - - my $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.TO_CART,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,DROPBOXES.SET_USER_DEFINED,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME=? and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.STATION_NAME=?;}; - - my $sth = $dbh->prepare($sql) - or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); - - $sth->execute($username, 'import-dropbox') # TODO: hardcoded value - or return (undef, 'ERROR', "Database Error: " . $sth->errstr); - - my @allowed_dbs; - while(my ($group, $to_cart, $normlevel, $trimlevel, $params, $lowcart, $highcart, $groupdesc) = $sth->fetchrow_array()) { - my @p = split(';', $params); - - my $entry = {}; - $entry->{'GROUP'} = $group; - $entry->{'GROUPDESC'} = $groupdesc; - $entry->{'GROUPLOWCART'} = $lowcart; - $entry->{'GROUPHIGHCART'} = $highcart; - $entry->{'NORMLEVEL'} = $normlevel; - $entry->{'TRIMLEVEL'} = $trimlevel; - $entry->{'PARAM'} = $params; - if($p[0] eq "S") { - $entry->{'TYPE'} = 'show'; - $entry->{'SHOWID'} = $to_cart; - - my ($title, $log, $status, $errorstring) = get_showtitle_and_log($dbh, $to_cart); - unless (defined $title && defined $log) { - return (undef, $status, $errorstring); - } - $entry->{'SHOWTITLE'} = $title; - $entry->{'SHOWLOG'} = $log; - - $entry->{'SHOWRHYTHM'} = $p[1]; - $entry->{'SHOWDOW'} = int $p[2]; - $entry->{'SHOWDOW'} = 0 unless $entry->{'SHOWDOW'} < 7; - substr($p[3], 2, 0) = ':'; - $entry->{'SHOWSTARTTIME'} = $p[3]; - $entry->{'SHOWLEN'} = int $p[4]; - } - - push @allowed_dbs, $entry; - } - $sth->finish(); - - return @allowed_dbs; -} - - -return 1; diff --git a/listdropboxes.cgi b/listdropboxes.cgi deleted file mode 100755 index 9261e20..0000000 --- a/listdropboxes.cgi +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/perl - -use strict; -use CGI; -use File::Basename; -use lib dirname( __FILE__ ) . '/lib'; -use rddb; - -my $status = 'ERROR'; -my $errorstring = 'unknown'; -my $responsecode = 500; -my @dropboxes = (); - -my $q = CGI->new; -my $username = $q->param('LOGIN_NAME'); -my $token = $q->param('PASSWORD'); - -my $dbh; -($dbh, $status, $errorstring) = rddb::opendb(); -if(defined $dbh) { - my $result; - ($result, $status, $errorstring) = rddb::check_token($dbh, $username, $token); - if($result == 1) { - $responsecode = 200; - @dropboxes = rddb::get_dropboxes($dbh, $username); - unless (defined $dropboxes[0]) { - $responsecode = 500; - $status = $dropboxes[1]; - $errorstring = $dropboxes[2]; - } - } elsif($result == 0) { - $responsecode = 403; - } else { - $responsecode = 500; - } - rddb::closedb($dbh); -} - -print "Content-type: application/xml; charset=UTF-8\n\n"; - -if($responsecode != 200) { - print "\n"; - print " " . $responsecode . "\n"; - print " " . $errorstring . "\n"; - print "\n"; -} else { - print "\n"; - for my $href (@dropboxes) { - print " \n"; - print " " . $href->{'GROUP'} . "\n"; - print " " . $href->{'GROUPDESC'} . "\n"; - print " " . $href->{'GROUPLOWCART'} . "\n"; - print " " . $href->{'GROUPHIGHCART'} . "\n"; - print " " . $href->{'NORMLEVEL'} . "\n"; - print " " . $href->{'TRIMLEVEL'} . "\n"; - print " " . $href->{'PARAM'} . "\n"; - print " " . $href->{'TYPE'} . "\n"; - if($href->{'TYPE'} eq "show") { - print " " . $href->{'SHOWID'} . "\n"; - print " " . $href->{'SHOWTITLE'} . "\n"; - print " " . $href->{'SHOWLOG'} . "\n"; - print " " . $href->{'SHOWRHYTHM'} . "\n"; - print " " . $href->{'SHOWDOW'} . "\n"; - print " " . $href->{'SHOWSTARTTIME'} . "\n"; - print " " . $href->{'SHOWLEN'} . "\n"; - } - print " \n"; - } - print "\n"; -} diff --git a/rh-bin/listdropboxes.cgi b/rh-bin/listdropboxes.cgi new file mode 100755 index 0000000..14ecf11 --- /dev/null +++ b/rh-bin/listdropboxes.cgi @@ -0,0 +1,70 @@ +#!/usr/bin/perl + +use strict; +use CGI; +use File::Basename; +use lib dirname( __FILE__ ); +use rddb; + +my $status = 'ERROR'; +my $errorstring = 'unknown'; +my $responsecode = 500; +my @dropboxes = (); + +my $q = CGI->new; +my $username = $q->param('LOGIN_NAME'); +my $token = $q->param('PASSWORD'); + +my $dbh; +($dbh, $status, $errorstring) = rddb::opendb(); +if(defined $dbh) { + my $result; + ($result, $status, $errorstring) = rddb::check_token($dbh, $username, $token); + if($result == 1) { + $responsecode = 200; + @dropboxes = rddb::get_dropboxes($dbh, $username); + unless (defined $dropboxes[0]) { + $responsecode = 500; + $status = $dropboxes[1]; + $errorstring = $dropboxes[2]; + } + } elsif($result == 0) { + $responsecode = 403; + } else { + $responsecode = 500; + } + rddb::closedb($dbh); +} + +print "Content-type: application/xml; charset=UTF-8\n\n"; + +if($responsecode != 200) { + print "\n"; + print " " . $responsecode . "\n"; + print " " . $errorstring . "\n"; + print "\n"; +} else { + print "\n"; + for my $href (@dropboxes) { + print " \n"; + print " " . $href->{'GROUP'} . "\n"; + print " " . $href->{'GROUPDESC'} . "\n"; + print " " . $href->{'GROUPLOWCART'} . "\n"; + print " " . $href->{'GROUPHIGHCART'} . "\n"; + print " " . $href->{'NORMLEVEL'} . "\n"; + print " " . $href->{'TRIMLEVEL'} . "\n"; + print " " . $href->{'PARAM'} . "\n"; + print " " . $href->{'TYPE'} . "\n"; + if($href->{'TYPE'} eq "show") { + print " " . $href->{'SHOWID'} . "\n"; + print " " . $href->{'SHOWTITLE'} . "\n"; + print " " . $href->{'SHOWLOG'} . "\n"; + print " " . $href->{'SHOWRHYTHM'} . "\n"; + print " " . $href->{'SHOWDOW'} . "\n"; + print " " . $href->{'SHOWSTARTTIME'} . "\n"; + print " " . $href->{'SHOWLEN'} . "\n"; + } + print " \n"; + } + print "\n"; +} diff --git a/rh-bin/rddb.pm b/rh-bin/rddb.pm new file mode 100755 index 0000000..34321d2 --- /dev/null +++ b/rh-bin/rddb.pm @@ -0,0 +1,160 @@ +#!/usr/bin/perl + +use strict; +use Config::IniFiles; +use DBI; + +package rddb; + + +sub opendb +{ + my $RD_CONF = "/etc/rd.conf"; + my $cfg = Config::IniFiles->new(-file => $RD_CONF) + or return (undef , 'ERROR', "Config File Error: " . join("\n", @Config::IniFiles::errors)); + + my $dbhost = $cfg->val('mySQL', 'Hostname'); + my $dbname = $cfg->val('mySQL', 'Database'); + my $dbuser = $cfg->val('mySQL', 'Loginname'); + my $dbpasswd = $cfg->val('mySQL', 'Password'); + + my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost","$dbuser","$dbpasswd") + or return (undef, 'ERROR', "Database Error: " . $DBI::errstr); + + $dbh->do(qq{SET CHARACTER SET utf8;}) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + return ($dbh, 'OK', 'success'); +} + +sub closedb +{ + my $dbh = shift; + $dbh->disconnect(); +} + +sub get_token +{ + my ($dbh, $username) = @_; + + my $sql = qq{select PASSWORD from USERS where LOGIN_NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $sth->execute($username) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + my ($token) = $sth->fetchrow_array; + $sth->finish(); + + unless(defined $token) { + return (undef, 'ERROR', "user '" . $username . "' not known by rivendell") + } + return ($token, 'OK', 'success'); +} + +sub check_token +{ + my ($dbh, $username, $token) = @_; + + my $sql = qq{select PASSWORD from USERS where LOGIN_NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $sth->execute($username) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + my ($token_result) = $sth->fetchrow_array; + $sth->finish(); + + unless(defined $token_result) { + return (undef, 'ERROR', "user '" . $username . "' not known by rivendell") + } + + if($token_result eq $token) { + return (1, 'OK', 'success'); + } + return (0, 'ERROR', "wrong password"); +} + +sub get_showtitle_and_log +{ + my ($dbh, $showid) = @_; + + my $sql = qq{select TITLE,MACROS from CART where NUMBER = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $sth->execute($showid) + or return (undef, undef, 'ERROR', "Database Error: " . $sth->errstr); + + my ($title, $macros) = $sth->fetchrow_array; + $sth->finish(); + + unless(defined $title) { + return (undef, undef, 'ERROR', "Show with ID=" . $showid . " not found!") + } + unless(defined $macros) { + return (undef, undef, 'ERROR', "Show with ID=" . $showid . " has no macro!"); + } + + unless($macros =~ /^LL 1 ([^ ]+) 0\!$/) { + return (undef, undef, 'ERROR', "Show with ID=" . $showid . " has invalid macro: '" . $macros . "'"); + } + my $log = $1; + + return ($title, $log, 'OK', 'success'); +} + +sub get_dropboxes +{ + my ($dbh, $username) = @_; + + my $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.TO_CART,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,DROPBOXES.SET_USER_DEFINED,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME=? and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.STATION_NAME=?;}; + + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $sth->execute($username, 'import-dropbox') # TODO: hardcoded value + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + my @allowed_dbs; + while(my ($group, $to_cart, $normlevel, $trimlevel, $params, $lowcart, $highcart, $groupdesc) = $sth->fetchrow_array()) { + my @p = split(';', $params); + + my $entry = {}; + $entry->{'GROUP'} = $group; + $entry->{'GROUPDESC'} = $groupdesc; + $entry->{'GROUPLOWCART'} = $lowcart; + $entry->{'GROUPHIGHCART'} = $highcart; + $entry->{'NORMLEVEL'} = $normlevel; + $entry->{'TRIMLEVEL'} = $trimlevel; + $entry->{'PARAM'} = $params; + if($p[0] eq "S") { + $entry->{'TYPE'} = 'show'; + $entry->{'SHOWID'} = $to_cart; + + my ($title, $log, $status, $errorstring) = get_showtitle_and_log($dbh, $to_cart); + unless (defined $title && defined $log) { + return (undef, $status, $errorstring); + } + $entry->{'SHOWTITLE'} = $title; + $entry->{'SHOWLOG'} = $log; + + $entry->{'SHOWRHYTHM'} = $p[1]; + $entry->{'SHOWDOW'} = int $p[2]; + $entry->{'SHOWDOW'} = 0 unless $entry->{'SHOWDOW'} < 7; + substr($p[3], 2, 0) = ':'; + $entry->{'SHOWSTARTTIME'} = $p[3]; + $entry->{'SHOWLEN'} = int $p[4]; + } + + push @allowed_dbs, $entry; + } + $sth->finish(); + + return @allowed_dbs; +} + + +return 1; -- cgit v0.10.2