summaryrefslogtreecommitdiff
path: root/cgi-bin/view
blob: e4f4f2dbd41609150f4af5872bf233b30c7b820c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/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;
  my $callback = shift;
  response({ ERROR => sprintf("Function parameter %s not defined", $param ) }, $callback);
  exit(1);
}
sub response{
  my $res = $json->pretty->encode(shift);
  my $callback = shift;
  if (defined $callback){
    print $callback + "(" + $res + ")";
  }
  else {
    print $res;
  }
}

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 { ERROR => "No ids matched `$data_id'.\n\n" };
  }
  
  $sth->finish;
  return { 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;
  }
  my $callback = $params->{'callback'} || undef

  unless(defined $params->{f}){
    not_specified_error("f", $callback);
  }
  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 {
      response({ ERROR => "Function parameter f improperly defined"}, $callback);
      exit(1);
    }; 
  }
  response($res, $callback);
}
main();
exit(0);