You are not logged in.

#51 06 Nov 2007 12:31 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

Well, after understanding that a lot of people don't know (as I did) that there are different line endings, and they edit files in random editors, I decided to try to find a way to convert all files in a directory (or a single file) to UNIX line endings, I looked into doing this by PHP, but couldnt find a way (do you know if it is possible by PHP?).

After some googling and reading, I found a line saying that PERL could do it, and found a script looking like this:

Code:

#!/usr/bin/perl -w
use strict;
use Getopt::Std;
use File::Find;



# get the options:
my %opts;
getopts('f:n:h', \%opts) || usage();
usage() if (!$opts{'n'} || $opts{'h'});

# if no files were specified, we'll convert everything in the current 
+directory:
push(@ARGV, '.') unless @ARGV;

my $newline = $opts{'n'};
usage() if ($newline =~ /[^CRLF]/i);

$newline =~ s/CR/\015/i;
$newline =~ s/C/\015/i;
$newline =~ s/R/\015/i;
$newline =~ s/LF/\012/i;
$newline =~ s/L/\012/i;
$newline =~ s/F/\012/i;

foreach my $filename (@ARGV)
{
    # traverse the directory tree and look at each file:
    find(sub { convertNewlines() }, $filename);
}





sub convertNewlines
{
    my $filename = $_;
    
    # don't mess with it unless it's a text file:
    return unless (-T $filename);

    open(FILE, "< $filename")
        or die "Couldn't open file ($filename) for reading: $!";

    my $converted_text;
    my $line_endings_converted = 0;
    while (my $line = <FILE>)
    {
        $line_endings_converted +=
            ($line =~ s/(?:\015\012|\015|\012)/$newline/g);
        $converted_text .= $line;
    }

    # now save it, and binmode it so no additional conversion is done 
+to
    # the line endings:
    open(FILE, "> $filename")
        or die "Couldn't open file ($filename) for writing: $!";
    binmode FILE;
    print FILE $converted_text;
    close FILE;

    print "Converted $line_endings_converted newlines in \"$filename\"
+ " .
          "to $opts{'n'}.\n";
}





sub usage
{
    print <<'END_OF_USAGE';
This script can be used to convert the line endings in files to Unix, 
+Windows,
or MacOS line endings.

Usage:
 $ newlines -n NEWLINE [FILENAMES...]

Arguments:
    -n   The newline sequence that the line endings in the files you
         specified should be converted to. Either "CR" or "R" for carr
+iage
         return, "LF" or "L" for linefeed, or "CRLF" for carriage
         return/linefeed.
Flags:
    -h   Displays this message.
END_OF_USAGE

    exit;
}

With this as usage example:

For example, this:
newlines -n CRLF foo.txt bar.txt foo_bar.txt stuff.* ./more_text_files

converts the line endings in "foo.txt", "bar.txt", "foo_bar.txt", the text files in the current directory named "stuff" with any extension, and all of the text files in "./more_text_files" to CRLF (\015\012).


But can I run PERL through a PHP file? So that I per example included the function file for the PERL, then ran the newlines script through the PHP file. And can a PERL file be placed anywhere on a website, or must it be in the CGI folder? (This is not CGI I'm pretty sure)


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#52 07 Nov 2007 6:58 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

you can if perl is installed (using exec)...  you can do the same thing with php though.

Offline

 

#53 07 Nov 2007 10:56 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

Read about a PHP function called shell_exec, would it work if I wrote something like this:

<?php
$command1 = 'dos2unix -k -o a.txt';
$output1 = shell_exec($command1);
?>

Theoretically speaking? Seeing as explaining where to put a perl file is just as hard as explanining how to transfer files through binary mode.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#54 07 Nov 2007 7:28 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

yes if you need the output of the script (what they echo/print), otherwise exec does the same thing.

whoever runs the script will have to have perl installed on the server (most do I'd venture to guess).

Offline

 

#55 17 Jan 2008 12:32 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

A more unexpected error came along; though I've never had the error (I've been running up to date systems), the script will not work for anyone running php 4 or lower, as the function file_put_contents() requires php 5. Any suggestions on alternative for file_put_contents() that will not require me to make the whole thing again? Can I use Fopen() etc to open the file, read it and then do the replace command on it?


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#56 17 Jan 2008 4:48 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

check to see if the function exists, then include your own implementation if it doesn't

you can add something like this to your script:

Code:

if(!function_exists("file_put_contents")) {

    function file_put_contents($filename, $data = false, int $flags = false, $context = false) {
        // add your own file_put_contents here, the function signature doesn't have to match php5's though
    }

}

Offline

 

#57 21 Jan 2008 9:59 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

Hmmm, now nothing comes up at all, but I cant find no bracket errors, and the HTML should parse before the PHP when the PHP is not active, right?

Currently, the code looks like this:

Code:

<?php
$installterms = $_POST["terms"];
$installmenumod = $_POST["menumod"];
$installdisables = $_POST["seconds"];
$installurls = $_POST["url"];

// Determine status of Terms and Condition selection
if ($installterms == "yes") {
  echo 'You have agreed to the <b>Terms and Conditions</b>, continuing with installation';
} if ($installterms == "no") {
  echo 'You did not agree to the <b>Terms and Conditions</b>, aborting installation';
  exit;
}

// Determine status of PHP Menu Mod selection
if ($installmenumod == "yes") {
  echo 'You chose to install the <b>PHP Menu Mod</b> system, installing...';
  installMenuMod();
} else {
  echo '';
}
// Determine status of Disable-Spambots selection
if ($installdisables == "yes") {
  echo 'You chose to install the <b>Disable-Spambots</b> system, installing...';
  installDisableMod();
} else {
  echo '';
}
// Determine status of Only-active-users-can-post-URLs selection
if ($installurls == "yes") {
  echo 'You chose to install the <b>Only-active-users-can-post-URLs</b> system, installing...';
  installURLMod();
} else {
  echo '';
}
echo '<br>';
echo '<br>';

// Start function compensation
if(!function_exists("file_put_contents")) {
    function file_put_contents($filename, $data = false, int $flags = false, $context = false) {
    
function installMenuMod()
  {
CONTENTS WAS HERE
  }
// End function compensation
}
}

// Start function compensation
if(!function_exists("file_put_contents")) {
    function file_put_contents($filename, $data = false, int $flags = false, $context = false) {
  
function installDisableMod()
  {
CONTENTS WAS HERE
  }
// End function compensation
}
}

// Start function compensation
if(!function_exists("file_put_contents")) {
    function file_put_contents($filename, $data = false, int $flags = false, $context = false) {

function installURLMod()
  {
CONTENTS WAS HERE
  }
// End function compensation
}
}
?>
</body>
</html>

(yes, it's awfully long, so it wouldnt fit in a post; basically, I put the if(!function_exists("file_put_contents")) {
    function file_put_contents($filename, $data = false, int $flags = false, $context = false) {
infront of the functions and a pair of ending braces behind.)

Each mod installation has it's own function, so by choosing the option in the installer, it will install the mods. But I can't understand why it won't come up now, could the function exist check have any impact on it?


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#58 21 Jan 2008 4:43 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

hehe, sorry, you need to add that bit of code somewhere that it can be used globally (like put it in a compat.php file, and require it at the top of each script).

inside the file_put_contents, you need to add the file_put_contents implementation, which would involve fopen'ing your file, and fwrite'ing the contents to the file, the fclose'ing the file and returning.

what that code does is if there is no file_put_contents in the version of PHP that they are using, your calls to file_put_contents will use the one that you include.  it would look something like this:

Code:

if(!function_exists("file_put_contents")) {
    function file_put_contents($filename, $data = false, int $flags = false, $context = false) {
        $mode = $flags && ($flags == FILE_APPEND || strtoupper($flags) == 'FILE_APPEND') ? 'a' : 'w';
        $fh = @fopen($filename, $mode);
        if ($fh === false) {
            return 0;
        } else {
            if (is_array($data)) $data = implode($data);
            $written = fwrite($fh, $data);
            fclose($fh);
            return $written;
        }
    }
}

Offline

 

#59 23 Jan 2008 12:35 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

So, I just put it at the top above my current script? I don't need to rename my illogical variables to make it work, do I? Like this, I dont't have the same variable names used in this:

Code:

function file_put_contents($filename, $data = false, int $flags = false, $context = false)

I use a ridiculous bucket of handwritten $name_one, $name_two, $name_three style of variables (I could not find any other way that did not ruin other important parts of code), as I went through all the files by hand to make it work, and made rewrites for every file. The file that installs it is over 80 KB right now, hate to have to rewrite all of it.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#60 23 Jan 2008 1:06 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

no. let me explain a little better.

in PHP 5 there is a function named file_put_contents, in PHP 4 there is not.  so, the only time you will have problems is when you try to invoke file_get_contents when there isn't one.

PHP allows you to check to see if a function exists or not.  This is sort of like being able to check to see if a file exists before opening it.

if the function does not exist (aka PHP 4), you can define a function by that name, that acts like the one in PHP 5.



when you wrap a function with an if statement, that function is only defined if the if statement is true.

since our if statement checks to see if there is a function named file_put_contents, and if there's not, defines a function named file_put_contents, we make sure that nomatter what, there will be a function named that.

in PHP 5, your original (unaltered) code will use PHP 5's implementation of file_put_contents.

in PHP 4, your original (unaltered) code will use YOUR implementation of file_put_contents.  and this is because function_exists will return false, the ! will make the statement if( false == false ) which evaluates to true, thereby declaring a function named file_put_contents, and your original code will still work.


you don't need to change anything in your original script in order to make this work...  simply add the function from my post above, or add it to a seperate file, and reference it with a require("file_put_contents.php") on each script that uses that function.

Offline

 

#61 23 Jan 2008 1:37 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

I see, thats great then. Just need to find out where I am missing a curly brace (damn thing doesn't even show the HTML right now, and I'm not sure where I deleted something). Does Zend Studio by any chance have a PHP validator that checks for such "small" errors?

Last edited by Butcher (23 Jan 2008 1:59 pm)


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#62 23 Jan 2008 5:27 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

yep, it sure does.

Offline

 

#63 24 Jan 2008 7:28 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

Ran the analyzer and the debug, analyser gave me a lot of crap about how my variables are set up, but clearly he missed that they append. The debug said this though:

Parsing Error: menumod.php line 146 - syntax error, unexpected T_STRING, expecting '&' or T_VARIABLE or T_CONST

and line 146 looks like:

Code:

function file_put_contents($filename, $data = false, int $flags = false, $context = false) {

But I cannot see how that line would make the html disappear, that usually only happens when I miss a curly bracket, and he didn't report that having happened... Any idea what could be going wrong?


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#64 24 Jan 2008 7:47 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

post the code before and after that line. 

keep in mind that the analyzer is going to find where the error is as it would be parsing the php code, and that isn't always where the mistake begins, but where it becomes a problem to the parser.

Offline

 

#65 24 Jan 2008 12:05 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

Line 144 to 158:

Code:

// Check if the "file_put_contents" function exists and works, if not, it compensates
if(!function_exists("file_put_contents")) {
    function file_put_contents($filename, $data = false, int $flags = false, $context = false) {
        $mode = $flags && ($flags == FILE_APPEND || strtoupper($flags) == 'FILE_APPEND') ? 'a' : 'w';
        $fh = @fopen($filename, $mode);
        if ($fh === false) {
            return 0;
        } else {
            if (is_array($data)) $data = implode($data);
            $written = fwrite($fh, $data);
            fclose($fh);
            return $written;
        }
    }
}

Though I think I may have gone wrong with the

Code:

// Start function compensation
if(!function_exists("file_put_contents")) {
    function file_put_contents($filename, $data = false, int $flags = false, $context = false) {

Cause I use them before each function as I said, like so:

Code:

// Start function compensation
if(!function_exists("file_put_contents")) {
    function file_put_contents($filename, $data = false, int $flags = false, $context = false) {
    
function installMenuMod()
  {
CONTENTS WAS HERE
  }
// End function compensation
}
}

But haven't got a error on that yet.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#66 24 Jan 2008 12:17 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

you only need the if(function_exists(..)) { ... } once. 

either include this function in some other php script, and simply add one require(...) at the beginning of each of your mod scripts,

or include the code once at the top of each page.

it looks like you've included it before each function in your scripts, which you don't need to do.

Offline

 

#67 24 Jan 2008 12:32 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

Ran analyser and debug again, apart from some variables that are being appended (and he claiming that the $inc_filehandle = fopen($inc_menufile, 'w') or die("can't create file"); "expression result is never used") I only get a debug error on the int in:

Code:

function file_put_contents($filename, $data = false, int $flags = false, $context = false)

he is expecting it to be a variable or a constant or a &, and probably related: the comma after $flags = false, should also supposedly be a variable or a constant. Though I think he is on a acid trip, and if I had a loose curly brace, he would tell me from the analyzer and debug, wouldn't he? Cause I still can't get the html to parse when opening the file.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#68 24 Jan 2008 4:24 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

haha, LMAO, sorry about that... crossing technologies there.

completely remove "int" from the function signature.  PHP variables are not typed.

Offline

 

#69 25 Jan 2008 8:58 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

Hehe, works now though big_smile

Is there any way to check if a str_replace worked or not? Like other than manually opening the file to check?

And can I run the "$ dos2unix myfile.txt" command by using something like this:

Code:

<?php
$file = "radio.php";
shell_exec(dos2unix $file);
?>

Last edited by Butcher (25 Jan 2008 9:01 am)


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#70 25 Jan 2008 10:32 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

you could do:

Code:

$orig = file_get_contents($file);
shell_exec("dos2unix $file");
$alt = file_get_contents($file);
if($orig === $alt)  {
    // unaltered
} else {
    // altered
}

Offline

 

#71 25 Jan 2008 10:50 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

What would be the consequense of running the dos2unix on a already unix file? As you know, I have a weird script setup (so much for handwritten), and would like to place this under each filename variable, so it changes it before it does the replace.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#72 25 Jan 2008 2:54 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

don't know.  I'm not all that familiar w/ the app. I'd assume that it wouldn't do anything with the file if it were already in unix format. I'd imagine you'd want to check PHP_OS to see if its Linux, Unix, or Windows before running the shell command too.

Offline

 

#73 25 Jan 2008 3:05 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

Yeah, thats right, I want the Unix (LF) line endings, but isn't Unix and Linux the same (both use LF)? Want to run this command because a lot of people are not aware of the line ending difference (as I was), and edit files using windows editors, saving with CR + LF instead of the phpBB standard LF.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#74 25 Jan 2008 4:52 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Automated File Changes

yes, the only case you want to handle is if the PHP_OS is windows...  reason being is that a windows server isn't going to have the dos2unix executable.

Offline

 

#75 26 Jan 2008 3:33 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Automated File Changes

What if PHP is running through for example XAMPP installed on a windows machine? Where would dos2unix be kept? XAMPP has apache, php 4/5, mysql, perl and other stuff, but I never thourougly looked for dos2unix, is the file itself called dos2unix, or is it within another executable?


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 



© 2003 - 2024 NullFX
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License