diff options
| author | pepper <peppersclothescult@gmail.com> | 2014-01-12 19:47:18 -0800 |
|---|---|---|
| committer | pepper <peppersclothescult@gmail.com> | 2014-01-12 19:47:18 -0800 |
| commit | e5b7549dbe43872f1cd0d8395186ad8e495720fb (patch) | |
| tree | 356239d88572247479985612942eb1233da0bc12 /cgi-bin/view | |
| parent | cce4f73a1637d54ab7c096815864a2c9c66fda65 (diff) | |
added upload stuff and view code
Diffstat (limited to 'cgi-bin/view')
| -rwxr-xr-x | cgi-bin/view | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/cgi-bin/view b/cgi-bin/view new file mode 100755 index 0000000..526d268 --- /dev/null +++ b/cgi-bin/view @@ -0,0 +1,100 @@ +#!/usr/bin/perl +use DBI; +use Data::Dumper; +use JSON; +use feature qw/switch/; +use CGI; + +my $IN = new CGI; +print $IN->header(); + +our $json = JSON->new->allow_nonref; +my $sql_username = "asdfus"; +my $sql_dbname = "asdfus"; +my $sql_passwd = "gTYgT&M6q"; + + +sub not_specified_error{ +my $param = shift; + print $json->pretty->encode ({ ERROR => sprintf("Function parameter %s not defined", $param ) }); + exit(1); +} + +our $dbh = DBI->connect("DBI:mysql:$sql_dbname", $sql_username, $sql_passwd) + or die "Couldn't connect to database: " . DBI->errstr; +sub mysql_retrieve{ + my ($statement, @args) = @_; + my $sth = $dbh->prepare($statement) + or die "Couldn't prepare statement: " . $dbh->errstr; + $sth->execute(@args) + or die "Couldn't execute statement: " . $sth->errstr; + my @rows = (); + while ($data = $sth->fetchrow_hashref()) { + foreach (keys $data){ + $data->{$_} =~ s/NULL|null//ig; + $data->{$_} = $data->{$_} || ""; + } + push(@rows, $data); + } + if ($sth->rows == 0) { + return $json->pretty->encode ({ ERROR => "No ids matched `$data_id'.\n\n" }); + } + + $sth->finish; + return $json->pretty->encode({ SUCCESS => 1, data => \@rows }); +} + +sub main{ + my $params = {}; + my $query_string = $ENV{'QUERY_STRING'}; + my @pairs = split(/[&;]/, $query_string); + foreach(@pairs){ + my ($key, $value) = split(/=/, $_, 2); + + $params->{$key} = $value; + } + + unless(defined $params->{f}){ + not_specified_error("f"); + } + my $res; + + given($params->{f}){ + when(/info/){ + unless(defined $params->{id}) { not_specified_error("id"); }; + $res = mysql_retrieve('SELECT * FROM shaders WHERE id = ?', $params->{id}); + } + when(/all/){ + $res = mysql_retrieve('select * from shaders order by id'); + } + when(/range/){ + unless (defined $params->{limit}){ not_specified_error("limit") }; + if (defined $params->{last}){ + $res = mysql_retrieve(sprintf(' select * from shaders order by id desc limit %s , %s', $param->{last}, $params->{limit})); + }else{ + $res = mysql_retrieve(sprintf(' select * from shaders order by id desc limit 0 , %s', $params->{limit})); + } + } + when(/history/){ + unless(defined $params->{id}) { not_specified_error("id"); }; + $res = mysql_retrieve('SELECT * FROM shaders WHERE shader_id = ? order by id', $params->{id}); + } + when(/username/){ + unless(defined $params->{username}) { not_specified_error("username") }; + $res = mysql_retrieve('select * from shaders where username = ?', $params->{username}); + } + when(/list_users/){ + $res = mysql_retrieve('select distinct username from shaders;'); + } + when(/originals/){ + $res = mysql_retrieve('select shaders.* from shader_ids cross join shaders on shader_ids.id = shaders.shader_id'); + } + default { + print $json->pretty->encode ({ ERROR => "Function parameter f improperly defined"}); + exit(1); + }; + } + print $res; +} +main(); +exit(0); |
