Sending arguments from PHP to Perl on Linux

60 views Asked by At

I'm trying to run a Perl script on a remote (currently virtual) Linux server using shell_exec(). The Perl file requires 3 arguments, a file name, an encoding and the contents of the file.

It's made, to write specific lines into Perl files made by other people.

The script should open the file with the file name in arg[0] with the encoding in arg[1] and write in it the contents in arg[2].

I managed to get it to work, as in it writes the contents to the files, but since this program writes over files where the encoding is important, I would have to write them over in just the same encoding they were in originally, and since we use accented characters I need to make sure that those work properly as well. How could I do that?

I will add code if needed.

EDIT: Code:

Perl:

#!/usr/bin/perl -w

use warnings;
#use strict;

my $fileName = "";
my $contents = "";
my $encoding = "";

if(@ARGV != 3){
    print "use: documentor_write_file fileName encoding contents";
    exit;
}else{

    $fileName = $ARGV[0];
    $encoding = $ARGV[1];
    $contents = $ARGV[2];

}

sub write_file{

    open(DATA,"> :encoding($encoding)",$fileName);
    $contents =~ s/&/&/g;

    $contents =~ s/$/$/g;

    $contents =~ s/&lt;/</g;

    $contents =~ s/&gt;/>/g;

    $contents =~ s/\\&quot;/\"/g;

    $contents =~ s/&cent;/¢/g;
    $contents =~ s/&pound;/£/g;
    $contents =~ s/&yen;/¥/g;
    $contents =~ s/&euro;/€/g;
    $contents =~ s/&copy;/©/g;
    $contents =~ s/&reg;/®/g;

    print(DATA);

    print DATA $contents;

}

write_file;

close(DATA);
exit;

PHP:

if(isset($_GET['stf'])){
    $ret = "";

    $fn = $_POST['fn'];     //File name
    $enc = $_POST['enc'];       //Encoding
    $cont = $_POST['content'];  //Content

    $cont = str_replace("\"","\\\"", $cont);    //Replaces " with \"
    $cont = pg_escape_string($cont);        //
    $cont = htmlspecialchars(htmlentities($cont));  //
    $cont = str_replace("$", "&dollar;", $cont);    //replaces $ with &dollar;

    $cmd2 = "perl /documentor_write_file.pl $fn $enc " . "\"" . $cont . "\"";
    $ret = shell_exec($cmd2);
    //print($ret . " - " . $cmd2);
}

Example:

File contents: (Perl file)

#!/usr/bin/perl -w

BEGIN{push @INC,"/usr/local/dpdregister/processes/common/";}
eval {require 'dpdregister.conf'; use vars qw ( $dbh ) };

=history
<HIST>
history2 áéűáé
több history
</HIST>
=cut

What it actually writes into the file:

#!/usr/bin/perl -w

BEGIN{push @INC,\&amp;quot;/usr/local/dpdregister/processes/common/\&amp;quot;;}
eval {require ''dpdregister.conf''; use vars qw ( &dollar;dbh ) };

=history
&amp;lt;HIST&amp;gt;
history2 &amp;aacute;&amp;eacute;\x{00c5}\x{00b1}&amp;aacute;&amp;eacute;
t&amp;ouml;bb history

&amp;lt;/HIST&amp;gt;
=cut
1

There are 1 answers

1
Anique On

Would it not be easier to write everything as UTF-8?

It would need a one-time conversion of all these Perl files to UTF-8 (which can also be done in Perl, if needed; just don't forget to set the use utf8; pragma), but after that you would only need to check that what you are sending to the file is actually UTF-8.