diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-11-22 23:19:36 -0500 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-11-22 23:19:36 -0500 |
| commit | 070b5e6d9d3c7e23c06f0ae0b75026923529c24d (patch) | |
| tree | 89c430dd17ca54ff60f885ecd99cf6cfa0f0b7ff /cgi-bin | |
| parent | 04537ed34d443d0610b77420d1dbef64bc05fbfa (diff) | |
| parent | 1631cdf643283fc71bc9d70b5dcbce03ab9c2386 (diff) | |
Merge branch 'master' of lmno:dither
Diffstat (limited to 'cgi-bin')
| -rw-r--r-- | cgi-bin/js_check/__init__.pyc | bin | 156 -> 190 bytes | |||
| -rw-r--r-- | cgi-bin/js_check/jsparser.pyc | bin | 27289 -> 28605 bytes | |||
| -rw-r--r-- | cgi-bin/js_check/validate.pyc | bin | 2770 -> 2756 bytes | |||
| -rwxr-xr-x | cgi-bin/proxy | 26 | ||||
| -rwxr-xr-x | cgi-bin/save | 13 | ||||
| -rwxr-xr-x | cgi-bin/test.cgi | 13 | ||||
| -rw-r--r-- | cgi-bin/thumbnail_upload | 4 | ||||
| -rwxr-xr-x | cgi-bin/upload | 201 | ||||
| -rwxr-xr-x | cgi-bin/view | 51 |
9 files changed, 276 insertions, 32 deletions
diff --git a/cgi-bin/js_check/__init__.pyc b/cgi-bin/js_check/__init__.pyc Binary files differindex 5d23962..e3d1cac 100644 --- a/cgi-bin/js_check/__init__.pyc +++ b/cgi-bin/js_check/__init__.pyc diff --git a/cgi-bin/js_check/jsparser.pyc b/cgi-bin/js_check/jsparser.pyc Binary files differindex 42b15b8..5ce5c81 100644 --- a/cgi-bin/js_check/jsparser.pyc +++ b/cgi-bin/js_check/jsparser.pyc diff --git a/cgi-bin/js_check/validate.pyc b/cgi-bin/js_check/validate.pyc Binary files differindex 0905b8f..a3c7071 100644 --- a/cgi-bin/js_check/validate.pyc +++ b/cgi-bin/js_check/validate.pyc diff --git a/cgi-bin/proxy b/cgi-bin/proxy new file mode 100755 index 0000000..9c2ca3d --- /dev/null +++ b/cgi-bin/proxy @@ -0,0 +1,26 @@ +#!/usr/bin/python + +from os import environ as env +import urllib.request +import sys + +def error(): + print("Content-type: text/html") + print() + print("HELLO!") + +def proxy(url,ext): + req = urllib.request.urlopen(url) + print("Content-type: image/" + ext) + print() + sys.stdout.flush() + sys.stdout.buffer.write( req.read() ) + +path = env['QUERY_STRING'] +ext = path[-3:].lower() + +if path[0:4] == "http" and ext in ("gif","jpg","png","peg"): + proxy(path,ext) +else: + error() + diff --git a/cgi-bin/save b/cgi-bin/save index 4c87493..2bfb8c2 100755 --- a/cgi-bin/save +++ b/cgi-bin/save @@ -53,7 +53,7 @@ def strip_querystring(url): p = re.compile('^[^\?]*') return p.findall(url)[0] -def store_shader_in_db(script, shader_id=False, image_url="",username="",name=""): +def store_shader_in_db(script, shader_id=False, image_url="",username="",name="",width="0",height="0"): conn = MySQLdb.connect(host= "127.0.0.1", user="asdfus", @@ -66,8 +66,8 @@ def store_shader_in_db(script, shader_id=False, image_url="",username="",name="" shader_id = conn.insert_id() conn.commit() - x.execute("""INSERT INTO shaders (script, image_url, username, name, remote_addr, time, shader_id) values (%s,%s,%s,%s,%s,%s,%s)""" , - (script, image_url, username, name, REMOTE_IP, NOW, shader_id) + x.execute("""INSERT INTO shaders (script, image_url, username, name, remote_addr, time, shader_id, width, height) values (%s,%s,%s,%s,%s,%s,%s,%s,%s)""" , + (script, image_url, username, name, REMOTE_IP, NOW, shader_id, width, height) ); last_id = conn.insert_id() conn.commit() @@ -97,10 +97,13 @@ def main(): error("Cannot save this shader:%s" % test_failure) shader_version_id = store_shader_in_db( - form['script'].value, form['shader_id'].value, + form['script'].value, + form['shader_id'].value, image_url, form['username'].value, - form['name'].value); + form['name'].value, + form['width'].value, + form['height'].value); if not shader_version_id: error('DB Error') else: diff --git a/cgi-bin/test.cgi b/cgi-bin/test.cgi new file mode 100755 index 0000000..e33ee3e --- /dev/null +++ b/cgi-bin/test.cgi @@ -0,0 +1,13 @@ +#!/usr/bin/perl + print "Content-type: text/html\n\n"; + print <<HTML; + <html> +<head> + <title>A Simple Perl CGI</title> + </head> + <body> + <h1>A Simple Perl CGI</h1> + <p>Hello World</p> + </body> +HTML + exit; diff --git a/cgi-bin/thumbnail_upload b/cgi-bin/thumbnail_upload index 8ee9866..ab12c66 100644 --- a/cgi-bin/thumbnail_upload +++ b/cgi-bin/thumbnail_upload @@ -145,7 +145,7 @@ sub upload_to_AmazonS3{ sub error{ my $message = shift; - my $resp = { success => "false", "error" => $message }; + my $resp = { success => JSON::false, "error" => $message }; print JSON->new->encode($resp); exit 1; } @@ -174,7 +174,7 @@ sub main{ error( "problem adding value to db"); } my $resp = { - success => "true", + success => JSON::true, url => $thumbnail_url, filesize => $filesize }; diff --git a/cgi-bin/upload b/cgi-bin/upload new file mode 100755 index 0000000..4f4012c --- /dev/null +++ b/cgi-bin/upload @@ -0,0 +1,201 @@ +#!/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 $upload_dir = "/tmp/"; + my $filepath = $upload_dir . $photoblaster_filename; + + $db_tag = $IN->param("tag") || $db_tag; + + 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 f7435f3..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"; @@ -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); } |
