#!/usr/bin/perl #use lib qw(/home/catmindeye/free4med.com/cgi-bin/lib/); ######################################################### # class06-upload.pl - Create a form that can upload images ######################################################### if ($0=~m#^(.*)\\#){ $execDir = "$1"; } # Win/DOS elsif ($0=~m#^(.*)/# ){ $execDir = "$1"; } # Unix else {`pwd` =~ /(.*)/; $execDir = "$1"; } # Unix # Get DB Login Info require "$execDir/info.pl"; # Use CGI Perl module to output any error msgs to browser use CGI::Carp qw(fatalsToBrowser); # Define the name of the script just for reference $scriptName = "flashtodatabase.pl"; $pageTitle = "Welcome to the Forum"; # Obtain any variables submitted either in the URL string # or by an HTML form via the GET or POST method, and # place in the %INPUT_VARS hash array %INPUT_VARS = {}; &input_vars_receive; &input_vars_parse; #Set flvtool2 and ffmpeg variable $ENV{'PATH'} = $ENV{'PATH'} . ':.:/home/catmindeye/lame/bin:/home/catmindeye/lame/include:/home/catmindeye/bin'; $ENV{'LD_LIBRARY_PATH'} = $ENV{'LD_LIBRARY_PATH'} . ':/home/catmindeye/lame/lib'; $ENV{'RUBYLIB'} = $ENV{'RUBYLIB'} . ':/home/catmindeye/lib/ruby'; ######################################################### # Get the necessary Perl modules for session management ######################################################### use CGI; use CGI::Session; use CGI::Session::Driver::file; ######################################################### # Initialize a new session or obtain old one if possible ######################################################### $cgi = new CGI; $session = new CGI::Session("driver:file", $cgi, {Directory=>$globalSessionDirectory}); $session->expire('+30m'); # Session expires after 30 minutes of inactivity $session->flush(); ######################################################### # Put our session ID in a cookie ######################################################### $cookie = $cgi->cookie(CGISESSID => $session->id ); $member_id = $session->param("~userid"); $username = $session->param("~username"); ######################################################### # Define our DB info ######################################################### use DBI(); $dbpath ="dbi:mysql:database=mobile;host=mysql.free4md.com"; ######################################################### # Connect to the database. ######################################################### # This only needs to be done once per script $dbh = DBI->connect($dbpath, "fff", "fff", {'RaiseError' => 1, 'PrintError' => 1}); ######################################################### # Define our widgets and initial values ######################################################### ### New site suhmission widgets ### $textCommentName = "user_name"; # The name of text widget that has comment $textCommentValue = $INPUT_VARS{$textCommentName}; # The value of comment widget $timecodeName = "time_code"; # timecode value $timecodeValue = $INPUT_VARS{$timecodeName}; # timecode value $attachment_output_folder = "www.free4md.com"; $thumbnail = $INPUT_VARS{$media_filename}; $extention = ".jpg"; $outputname = $media_filename.$extention; ### Status variables ### $statusMsg = ""; # Gives response back to user (i.e. "Thank you for your ...") $hasErrors = 0; # Keeps track of whether there are input errors ######################################################### # Assume someone submitted new site ######################################################### # Error Checking $noComment= 0; # Flag that is set if comment widget was blank $noTimecode = 0; # Flag that is set if timecode was blank # Get the values from the widgets and trim whitespace $user_name = &trim_it($INPUT_VARS{$textCommentName}); $time_code = &trim_it($INPUT_VARS{$timecodeName}); # Now check each widget. Title widget is checked first to see if blank if ($user_name eq "") { # If blank, set general error flag, title error flag, and general error message $hasErrors = 1; $noComment = 1; $statusMsg = "There were errors in your site submission."; } # Description widget is checked first to see if blank if ($time_code eq "") { # If blank, set general error flag, desc error flag, and general error message $hasErrors = 1; $noTimecode = 1; $statusMsg = "There were errors in your site submission."; } # If we had no errors, we can put it in the database if ($hasErrors==0) { # This is a good submission, so escape any single quotes in the text values $commentDB = &escape_quotes($user_name); $timecodeDB = &escape_quotes($time_code); # Insert the new site into the database $SqlStatement = "INSERT INTO frame (comment,timecode) "; $SqlStatement .= "VALUES ('$commentDB',$timecodeDB) "; $affected_rows = $dbh->do($SqlStatement); $thumbnail_out = system("/home/catmindeye/ffmpeg/bin/ffmpeg -i $attachment_output_folder$thumbnail -t 0.001 -ss 1 -vframes $timecodeDB -f mjpeg -s 64X48 $attachment_output_folder$outputname"); $SqlStatement = "INSERT INTO video_thumbs (media_filename,thumbnail) "; $SqlStatement = "VALUES ('$media_filename','$thumbnail_out'); $affected_rows = $dbh->do($SqlStatement); # Create general status message $statusMsg = "Thank you for your submission!"; # Reset the text widgets to accept input once again for the next submission $textCommentValue = ""; $timecodeValue = ""; } $thumbnail_out = &url_encode($thumbnail_out); #hasErrors=$hasErrors&statusMsg=$statusMsg^; # Send it to Flash print qq^thumbnail=$thumbnail_out&comment=$commentDB^; ######################################################### # Disconnect from the database. ######################################################### # Always the last thing you do before exiting your script $dbh->disconnect(); exit(0); ######################################################### # Takes data from an HTML Form or URL string ######################################################### sub input_vars_receive { $formData = $ENV{'QUERY_STRING'}; if ($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN, $formData, $ENV{'CONTENT_LENGTH'}); } } ######################################################### # Parses form info ######################################################### sub input_vars_parse { local($name,$value,$pair); local(@pairs) = split(/&/, $formData); # Get parameter names, their values and copy into $INPUT_VARS array foreach $pair (@pairs) { ($name,$value)=split(/=/,$pair); $value=~tr/+/ /s; $value=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; $INPUT_VARS{$name}=$value; } } ######################################################### # Write To Log File ######################################################### sub write_to_log ######################################################### { # Write to log file local($msg) = @_; open INPTR, ">>$logFile"; print INPTR "$msg\n"; close INPTR; } ######################################################### # Trim whitespace ######################################################### sub trim_it { local($msg) = @_; while ($msg=~/^\s/i) { $msg=~s/^\s//i;} while ($msg =~/\s$/) { $msg =~s/\s$//; } return $msg; } ######################################################### # Escape single quotes for DB entry ######################################################### sub escape_quotes { local($msg) = @_; local($delim) = "'"; local($rep) = "''"; $msg =~s/$delim/$rep/eg; return $msg; } ######################################################### # URL-encode data ######################################################### sub url_encode { my($toencode) = @_; $toencode=~s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg; return $toencode; }