Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Quick 'n very dirty flash-variables updatescript

by teabag (Pilgrim)
on Apr 09, 2003 at 12:06 UTC ( [id://249200]=CUFP: print w/replies, xml ) Need Help??

This crude Perl script creates (Macromedia) Flash-style variables and writes them to an text file, which you can load into your flash site.
It allows you to make textupdates and save the updated text from within your flash webpage.

I know I should have used strict and warnings. Sorry!

Also a very very simple and unsafe security by fixed ip-numbercheck, probably not really safe so I suggest you build a better security option yourself ;)

#!/usr/bin/perl # # flashupdate.pl # by teabag <tony@lvp-site.nl> last rev. 09/04/03 # This Perl script create Flash variables and write them # to an text file, which you can load into your flash site. ################################################################# # Instructions # 1. Define the 3 variables # 2. Upload update.pl script to your /cgi-bin or (cgi)folder # using ASCII mode and CHMOD 755 # 3. Upload an empty (.txt) file named to the path (../data)you speci +fy # in $full_path # 4. Run and RESET/DELETE the script by calling it from an HTML form +of from a # Flash-generated form (using Get URL, with option Variables: Send + using POST) ################################################################# # Define these variables! # relative path form script to dir where datafile is located # no trailing slash! # added just a simple fixed ip-numbercheck $myipnumber = "123.45.67.890"; $full_path = "/htdocs/data"; $data_file = "update.txt"; checkip(); @days = ('Sunday','Monday','Tuesday','Wednesday', 'Thursday','Friday','Saturday'); @months = ('january','february','march','april','may','june','july +', 'august','september','october','november','december'); + ($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6]; $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec); $year += 1900; $date_for_logfile = "$mday $months[$mon] $year, $time"; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name,$value)=split(/=/,$pair); $name =~tr/+//; $name =~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; $value =~tr/+//; $value =~ s/~!/ ~!/g; $value =~ s/\+/ /g; $value =~ s/<([^>])*>//g; $value =~ s/(\r)+/\-\-/g; $value =~ s/\n+//g; $value=~s/\<SCRIPT//gi; $value=~s/\<\/SCRIPT\>//gi; $value=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; #substitute flash special characters $value =~ s/\%/%25/g; # filters and fixes '%' $value =~ s/\+/%2b/g; # filters and fixes '+' $value =~ s/\=/%3D/g; # filters and fixes '=' $value =~ s/\&/%26/g; # filters and fixes '&' $value =~ s/\$/%24/g; # filters and fixes '$' $value =~ s/\{/%7B/g; # filters and fixes '{' $value =~ s/\}/%7D/g; # filters and fixes '}' # this line below strips linebreaks, delete comment tag if you get pro +blems # with the layout! The linebreaks will then work properly, in the layo +ut # $value =~ s/\n/ /g; #filters and deletes the lin +ebreaks $FORM{$name} = $value; push (@formdata,$name); push (@formdata,$value); } %formdata=@formdata; %formdata; foreach $key (keys(%formdata)){ push (@fieldnames,$key); } open (RESULTS,">$full_path/$data_file") || die "Couldn't open file +: $!\n"; foreach $fieldname (@fieldnames) { print RESULTS "$fieldname=$formdata{$fieldname}&" } print RESULTS "updated=$date_for_logfile"; close (RESULTS); # Print Results to your browser print "Content-type:text/html\n\n"; print <<flash_vars_top; <html> <head> <title>Results</title> </head> <body> <center> <b>Your Flash variables created $date_for_logfile :</b><br> flash_vars_top # for each variable name (field name in your form), # print its name & value pair to the web page foreach $fieldname (@fieldnames) { print "<b>$fieldname</b> - $formdata{$fieldname}<br>\n"; } print "</center>\n</body>\n</html>\n"; exit; sub checkip{ if ($ENV{REMOTE_ADDR} eq $myipnumber){ } else { print "Content-type: text/html\n\n"; print "<html><head><title>Error!</title></head><body bgcolor=white><p> +</p><p><center> <font face=\"Verdana, Arial\" color=red size=\"3\"><b>Invalid ip-numbe +r</b> </font></center></p></body></html>"; exit; } }

Replies are listed 'Best First'.
•Re: Quick 'n very dirty flash-variables updatescript
by merlyn (Sage) on Apr 09, 2003 at 15:19 UTC
    Ouch! You need to learn about CGI.pm. That would have reduced your program to about half its size, and eliminated the need for the 30 lines of cargo-culted ancient code.

    Setting up your %formdata hash would have been as simple as:

    use CGI qw(param); for (param()) { $formdata{$_} = param($_); }

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Let's play golf ;). Mine:

      use CGI ':cgi-lib'; my %formdata = Vars;


      If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

(jeffa) Re: Quick 'n very dirty flash-variables updatescript
by jeffa (Bishop) on Apr 09, 2003 at 16:21 UTC

    A couple more pointers for you:

    First, use POSIX::strftime to format your time stamp:
    use POSIX; my $date_for_logfile = strftime( "%e %B %C%y, %T", localtime);
    If you still insist that the month is all lower case, then you could always use lc.

    $date_for_logfile is rediculously long, how about just $log_date instead? or $ts for timestamp? By the way, did you know that you can get a very nice generic timestamp by simply using:

    my $ts = scalar localtime;
    Unless i need a very specific format for my time stamp, that is exactly what i use. POSIX::strftime() takes care of the rest. ;)


    Second item is your check_ip sub. One important lesson i have learned over the years is that unless the sub contains the word print, it should not print. Also, don't use global variables like that, not when you can pass them in:
    my $ip = '127.0.0.1'; print ip_error() unless check_ip($ENV{REMOTE_ADDR},$ip);
    Now, check_ip will look like:
    sub check_ip { my ($ip1,$ip2) = @_; return $ip1 eq $ip2; }
    and our new sub, ip_error() will return (not print) the HTML to be used for our error. I will use CGI.pm to output the HTML, you should too:
    sub ip_error { return header, start_html( -title => 'Error', -style => { -code => q|body { font-family: "Verdana, Arial"; text-alig +n: center; color: red }|, }, ), b('Invalid ip-number'), ; }
    Of course, now check_ip seems a bit overkill, so maybe we could instead use:
    print ip_error() unless $ENV{REMOTE_ADDR} eq $ip;
    I think there is nothing wrong with hacking out some code, but before i share it with others, i refactor out the nonesense.

    Hope this helps, on the whole your script is nice, but you really should have cleaned it up a bit before you posted. As well as using strict and warnings, good indentation is a must, and until you develop your own style of 'proper' indentation, run your code through perltidy first. Here is a clue that you are not indenting properly: when you see two ending braces lined up with each other on two or more consecutive lines:

    } }
    Happy coding to you! ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Quick 'n cleaner flash-variables updatescript
by Ovid (Cardinal) on Apr 09, 2003 at 23:36 UTC

    Ignoring the security concern about the IP address, here's a somewhat cleaner version of your code. With Perltidy and declaring variables, this only took a few minutes.

    #!/usr/bin/perl -w use strict; use CGI qw(:standard); use HTML::Entities; my $myipnumber = "123.45.67.890"; my $full_path = "/htdocs/data"; my $data_file = "update.txt"; checkip(); my @days = ( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ); my @months = ( 'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december +' ); my ($sec, $min, $hour, $mday, $mon, $year, $wday) =(localtime(time))[0 +..6]; my $time = sprintf( "%02d:%02d:%02d", $hour, $min, $sec ); $year += 1900; my $date_for_logfile = "$mday $months[$mon] $year, $time"; my %formdata = map { $_ => param($_) } param(); my @fieldsnames = keys %formdata; open( RESULTS, ">$full_path/$data_file" ) || die "Couldn't open file: +$!\n"; my $results; while (my ($key,$value) = each %formdata) { print RESULTS "$key=$value&"; $results .= b($key)." - $value". br(); } print RESULTS "updated=$date_for_logfile"; close(RESULTS); encode_entities($results); print header(), start_html(-title => 'Results'), CGI::center( b( 'Your Flash variables created $date_for_logfile :' ), br(), $results ), end_html; exit; sub checkip { unless ( $ENV{REMOTE_ADDR} eq $myipnumber ) { print header(), start_html( -title => "Error!", -bgcolor => "white" ), CGI::center( font( { -face => "Verdana, Arial", -color => "red", -size => "3" }, b("Invalid ip-number") ) ), end_html; exit; } }

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

Re: Quick 'n very dirty flash-variables updatescript
by teabag (Pilgrim) on Apr 10, 2003 at 15:30 UTC
    Thanks for all your input.
    I checked out the perldocs of CGI.pm and I found out I've been doing a lot of extra work. I should probably check out other people's code more often.

    About that time format, it's translated from dutch and I normally don't use capitals in the days.

    I'll pay more attention to the formatting, use perltidy, be strict and pray to the almighty camel I will become a better monk in the near future ;)

    teabag
    chmod 666 *.* 'cause everyone should have the right to read and write!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://249200]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-19 13:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found