From fdf32609f44dc8eb6ae291864f0d7c9e0379782f Mon Sep 17 00:00:00 2001
From: pepper <peppersclothescult@gmail.com>
Date: Mon, 20 Jan 2014 01:20:54 -0800
Subject: added cgi-bin proxy, sql stuff

---
 bin/sql_build.sql | 8 ++++++++
 1 file changed, 8 insertions(+)
 create mode 100644 bin/sql_build.sql

(limited to 'bin/sql_build.sql')

diff --git a/bin/sql_build.sql b/bin/sql_build.sql
new file mode 100644
index 0000000..38f39fd
--- /dev/null
+++ b/bin/sql_build.sql
@@ -0,0 +1,8 @@
+create database IF NOT EXISTS asdfus;
+grant all privileges on asdfus.* to 'asdfus'@'localhost' identified by 'gTYgT&M6q';
+flush privileges;
+use asdfus;
+select * from INFORMATION_SCHEMA.TABLES where table_name = "shaders" and table_schema = "asdfus" ;
+create table IF NOT EXISTS shaders (id int(11) AUTO_INCREMENT NOT NULL, PRIMARY KEY(id), script blob, image_url blob, thumbnail_url varchar(50), username blob, time bigint(20) NOT NULL, shader_id int(11));
+
+create table IF NOT EXISTS shader_ids (id int(11) AUTO_INCREMENT NOT NULL, PRIMARY KEY(id), username varchar(40));
-- 
cgit v1.2.3-70-g09d2


From b7f25faab88480f14adfe855192291867b2d7b05 Mon Sep 17 00:00:00 2001
From: pepper <peppersclothescult@gmail.com>
Date: Tue, 21 Jan 2014 01:01:19 -0800
Subject: changed sql username to varchar

---
 bin/sql_build.sql | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

(limited to 'bin/sql_build.sql')

diff --git a/bin/sql_build.sql b/bin/sql_build.sql
index 38f39fd..cab13c0 100644
--- a/bin/sql_build.sql
+++ b/bin/sql_build.sql
@@ -2,7 +2,7 @@ create database IF NOT EXISTS asdfus;
 grant all privileges on asdfus.* to 'asdfus'@'localhost' identified by 'gTYgT&M6q';
 flush privileges;
 use asdfus;
-select * from INFORMATION_SCHEMA.TABLES where table_name = "shaders" and table_schema = "asdfus" ;
-create table IF NOT EXISTS shaders (id int(11) AUTO_INCREMENT NOT NULL, PRIMARY KEY(id), script blob, image_url blob, thumbnail_url varchar(50), username blob, time bigint(20) NOT NULL, shader_id int(11));
+create table IF NOT EXISTS shaders (id int(11) AUTO_INCREMENT NOT NULL, PRIMARY KEY(id), script blob, image_url blob, thumbnail_url varchar(50), username varchar(30), time bigint(20) NOT NULL, shader_id int(11));
 
 create table IF NOT EXISTS shader_ids (id int(11) AUTO_INCREMENT NOT NULL, PRIMARY KEY(id), username varchar(40));
+alter table shaders modify username varchar(30);
-- 
cgit v1.2.3-70-g09d2


From c1e6c0c182102e5d61a8ac26b403b42c5fd40591 Mon Sep 17 00:00:00 2001
From: pepper <peppersclothescult@gmail.com>
Date: Wed, 29 Jan 2014 10:24:15 -0800
Subject: fixed upload, view bug, added sql statements

---
 bin/sql_build.sql |   9 +++
 cgi-bin/upload    | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 cgi-bin/view      |  53 ++++++++-------
 3 files changed, 234 insertions(+), 26 deletions(-)
 create mode 100755 cgi-bin/upload

(limited to 'bin/sql_build.sql')

diff --git a/bin/sql_build.sql b/bin/sql_build.sql
index cab13c0..8badd66 100644
--- a/bin/sql_build.sql
+++ b/bin/sql_build.sql
@@ -6,3 +6,12 @@ create table IF NOT EXISTS shaders (id int(11) AUTO_INCREMENT NOT NULL, PRIMARY
 
 create table IF NOT EXISTS shader_ids (id int(11) AUTO_INCREMENT NOT NULL, PRIMARY KEY(id), username varchar(40));
 alter table shaders modify username varchar(30);
+
+IF EXISTS (
+  SELECT * 
+  FROM   sys.columns 
+  WHERE name = 'shaders'
+)
+begin
+  alter table shaders add name varchar(40);
+end;
diff --git a/cgi-bin/upload b/cgi-bin/upload
new file mode 100755
index 0000000..4e4ffbd
--- /dev/null
+++ b/cgi-bin/upload
@@ -0,0 +1,198 @@
+#!/usr/bin/perl
+
+use strict; 
+use CGI::Carp qw(fatalsToBrowser);
+
+use DBI;
+use Digest::MD5;
+use File::MMagic;
+use CGI;
+use File::Slurp;
+use Data::Dumper;
+use Amazon::S3;
+use Digest::SHA1;
+use JSON
+#set the temporary directory here
+my $upload_dir = '/tmp/';
+#maximum filesize goes here
+my $maxFileSize = 2.2 * 1024 * 1024;
+
+#aws credentials here
+my $aws_access_key_id     = "AKIAIR53VPBXKJMXZIBA";
+my $aws_secret_access_key = "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";
+
+#SQL CONNECTIONS
+my $sql_username = "asdfus";
+my $sql_dbname = "asdfus";         
+my $sql_passwd = "gTYgT&M6q";
+my $db_tag = "shader";
+
+my $time_now = time();
+
+
+my $IN = new CGI;
+my $resp = {};  
+
+sub get_postdata{
+    return $IN->param('POSTDATA');
+}
+
+#qq file is the default name for the file parameter when using qq upload. change if necessary
+sub get_filedata{
+    my $file = $IN->upload('qqfile');
+    if ($file){
+      return read_file( $file, binmode => ':raw' ) ;
+    }else{
+      return
+    }
+}
+
+
+sub get_filetype{
+  my $filedata = shift;
+  my $mm = new File::MMagic;
+  my $mtype = $mm->checktype_contents($filedata);
+  if ($mtype =~ m@^image/(gif|png|jpeg)@gi){
+    return $1;
+  }else{
+    return undef; 
+  }
+}
+
+
+sub make_photoblaster_directory{
+  return substr(Digest::SHA1->sha1_hex($time_now), 0, 2);
+}    
+sub make_photoblaster_filename{
+  my $filename = $IN->param("filename") || sprintf("%s_%s", "im", $time_now);
+  $filename =~ s/[^-a-zA-Z0-9]/-/g;
+  $filename =~ s/-+/-/g;
+  $filename =~ s/\....$//;
+  $filename =~ s/^-//g;
+  return $filename;
+}
+sub make_photoblaster_url{
+  my $photoblaster_directory = shift;
+  my $filename = shift;
+  return sprintf("http://i.asdf.us/im/%s/%s", $photoblaster_directory, $filename);
+}
+sub add_to_photoblaster_db{
+  my $pb_dir = shift;
+  my $filename = shift;
+  our $dbh = DBI->connect("DBI:mysql:$sql_dbname", $sql_username, $sql_passwd);
+  unless($dbh){  return undef && print STDERR "Could not connect to database: $DBI::errstr"};
+  my $execute = $dbh->do(
+    "INSERT INTO im_cmd ".
+    "(date, name, dir, newfile, tag, remote_addr)".
+    "VALUES(?,?,?,?,?,?);",
+    undef,
+    $time_now,
+    $IN->param("username") || "",
+    $pb_dir,
+    $filename,
+    $db_tag,
+    $ENV{REMOTE_ADDR} || '127.0.0.1'
+  );
+  return $execute;
+}
+
+sub write_to_file{
+  my $filepath = shift;
+  my $filedata = shift;
+  write_file( $filepath, {binmode => ':raw'}, $filedata ) or return undef;
+  return 1;
+}
+
+sub test_gif_size{
+  my $filepath = shift;
+  my $check_size = -s "$filepath";
+  print STDERR qq|Main filesize: $check_size  Max Filesize: $maxFileSize nn|;
+  return $check_size;
+}
+
+sub optimize_gif{
+  my $filepath = shift;
+  my $tmpname = $filepath.".tmp";
+  #because system gives the unix exit code
+  not system("gifsicle -O2 --colors=256 $filepath > $tmpname") or return undef;
+  not system("mv -f $tmpname $filepath") or return undef;
+  return 1;
+}
+
+sub upload_to_AmazonS3{  
+  my $keyname = shift;
+  my $value = shift;
+  my $s3 = Amazon::S3->new(
+      {   aws_access_key_id     => $aws_access_key_id,
+          aws_secret_access_key => $aws_secret_access_key
+      }
+  );
+  my $bucket = $s3->bucket("i.asdf.us");
+  $bucket->add_key(
+      $keyname, $value,
+      {   content_type        => 'image/gif',
+      }
+  );
+  return $bucket;
+}
+
+sub delete_file{
+  my $filepath = shift;
+  unlink($filepath); 
+}
+sub error{
+  my $message = shift;
+
+  my $resp = { success => "false", error => $message };
+  print JSON->new->encode($resp);
+  exit 1;
+}
+
+sub main{
+  print $IN->header();
+  
+  my $filedata = get_postdata() || get_filedata() || read_file("/tmp/tt.gif", binmode=>":raw");
+  unless($filedata){ error("NO DATA RECIEVED") };
+  my $filetype = get_filetype($filedata);
+  unless($filetype){ error("BAD FILE TYPE")};
+
+  my $photoblaster_directory = make_photoblaster_directory();
+  my $photoblaster_filename = sprintf("%s.%s", make_photoblaster_filename(), $filetype);
+
+  my $filepath = $main::upload_dir . $photoblaster_filename;
+
+  if ($filetype =~ /gif$/i){
+    unless(write_to_file($filepath, $filedata)){
+      error("UNABLE TO WRITE TO FILE");
+    }
+    unless (optimize_gif($filepath)){
+      error("UNABLE TO OPTIMIZE GIF");
+    }
+    my $filedata = read_file($filepath, binmode=>":raw");
+  }
+
+  my $filesize = length($filedata);
+  if ($filesize < 1) {
+    error( "EMPTY FILE"); 
+  } elsif ($filesize > $maxFileSize) {
+    error("FILE TOO LARGE");
+  }
+
+  unless(upload_to_AmazonS3(sprintf("im/%s/%s", $photoblaster_directory, $photoblaster_filename), $filedata)){
+    error("UNABLE TO UPLOAD TO AMAZONS3");
+  };
+  delete_file($filepath);
+  my $photoblaster_url = make_photoblaster_url($photoblaster_directory, $photoblaster_filename);
+  unless(add_to_photoblaster_db($photoblaster_directory, $photoblaster_filename)){
+    print STDERR "problem adding value to db";
+  }
+  my $resp = {
+    success => "true", 
+    url => $photoblaster_url, 
+    filesize =>  $filesize 
+  };
+  print JSON->new->encode($resp);
+}
+
+main();
+
diff --git a/cgi-bin/view b/cgi-bin/view
index 2e9a540..6bb55b4 100755
--- a/cgi-bin/view
+++ b/cgi-bin/view
@@ -6,7 +6,7 @@ use feature qw/switch/;
 use CGI;
 
 my $IN = new CGI;
-print $IN->header();
+print $IN->header(-type  =>  'application/json');
 
 our $json =  JSON->new->allow_nonref;
 my $sql_username = "asdfus";
@@ -24,7 +24,7 @@ sub response{
   my $res = $json->pretty->encode(shift);
   my $callback = shift;
   if (defined $callback){
-    print $callback + "(" + $res + ")";
+    print $callback . "(" . $res . ")";
   }
   else {
     print $res;
@@ -72,40 +72,41 @@ sub main{
   }
   my $res;
   
-  given($params->{f}){
-    when(/info/){ 
+  for($params->{f}){
+    /info/ && do {
       unless(defined $params->{id}) { not_specified_error("id"); };
       $res = mysql_retrieve('SELECT * FROM shaders WHERE id = ?', $params->{id});
-    }
-    when(/all/){ 
+      last;
+    };
+    /all/ && do { 
       $res =  mysql_retrieve('select * from shaders order by id'); 
-    }
-    when(/range/){
+      last;
+    };
+    /range/ && do {
       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/){ 
+      $res = mysql_retrieve(' select * from shaders order by id desc limit ? , ?', ($params->{last} || 0), $params->{limit});
+      last;
+    };
+    /history/ && do{ 
       unless(defined $params->{id}) { not_specified_error("id"); };
       $res = mysql_retrieve('SELECT * FROM shaders WHERE shader_id = ? order by id', $params->{id});
-    }
-    when(/username/){
+      last;
+    };
+    /username/ && do{
       unless(defined $params->{username}) { not_specified_error("username") };
       $res = mysql_retrieve('select * from shaders where username = ?', $params->{username}); 
-    }
-    when(/list_users/){
+      last;
+    };
+    /list_users/ && do{
       $res = mysql_retrieve('select distinct username from shaders;');
-    }
-    when(/originals/){
+      last;
+    };
+    /originals/ && do{
       $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);
-    }; 
+      last; 
+    };
+    response({ ERROR => "Function parameter f improperly defined"}, $callback);
+    exit(1);
   }
   response($res, $callback);
 }
-- 
cgit v1.2.3-70-g09d2

