Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

CGI parse and edit file

by ikkon (Monk)
on Feb 29, 2008 at 02:12 UTC ( #671076=perlquestion: print w/replies, xml ) Need Help??
ikkon has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to make a perl script that will open a CSS file add the CSS script into a textarea for editing then after hitting submit on the form save it back to the CSS file as easy as this should be I keep running into problems getting it to work.
#!/usr/bin/perl BEGIN { use CGI::Carp qw(carpout confess); open(LOG, ">>","Entrance.log") or die("Unable to open mycgi-log: $!\n"); carpout(LOG); } #$SIG{__DIE__}; #$SIG{__WARN__}; use CGI qw(:standard); use strict; my $RoomName = 'Entrance'; my $CSS_Name = qq{$RoomName.css}; if (param('cssScript')) { write_file($CSS_Name, param('cssScript')); } my @CSS_Array = read_file($CSS_Name); my $CSS_script = join("\n", @CSS_Array); print header; print start_html("$RoomName Edit"), h1("$RoomName CSS"), start_form; print textarea(-name=>'cssScript', -default=>$CSS_script, -rows=>50, -columns=>75); print p, submit, end_form, hr; print end_html; sub read_file { my ( $f ) = @_; open F, "< $f" or die "Can't open $f : $!"; my @f = <F>; close F; return wantarray ? @f : \@f; } sub write_file { my ( $f, @data ) = @_; @data = () unless @data; open F, "> $f" or die "Can't open $f : $!"; print F @data; close F; }
Any Ideas what I am doing wrong?

Replies are listed 'Best First'.
Re: CGI parse and edit file
by ww (Archbishop) on Feb 29, 2008 at 02:39 UTC

    Without better explanation of the "problems" it's going to be hard to come up with specific "ideas."

    Except that you should look at your Entrance.log. Mine looks like this:

    [Thu Feb 28 21:36:07 2008] Can't open Entrance.css : No such file or directory at line 47.
      [Thu Feb 28 20:23:55 2008] cgiTest.cgi: Can't open Entrance.css : Permission denied at /hosted/MysticAbyss/site/Entrance/cgiTest.cgi line 57.
      thats the error I recieve I am not sure why I am getting a permission error I set the permission to 755 and initial it seems to open just fine until the writing acures then it bombs.
        You don't have a selinux enabled operating system, by chance, or one with some other additional security services?

        Update: much easier explanation: the files are stored as belonging to your user, not the web server's user id.

        Setting the permission to 755 without changing the owner does not confer write privileges to anyone else.
        Allowing write privileges to anyone else is DANGEROUS to the health of your web server.
        If you must allow a web hosted procedure to modify a CSS file, create and edit a user file (ie: a CSS file stored in the user's directory) and design the application to override the appication defaults with this.
        Many browsers will allow this to be done locally, research the target browser(s).
        Again, do not allow users to modify system files, especially as part of a web application (I usually set permissions to 0555 and then override only as necessary).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://671076]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-10-23 21:02 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (285 votes). Check out past polls.