Un pequeño script php para cuando el cliente ftp te hace de las suyas

Recientemente me he encontrado que mi cliente ftp hace la conversión ASCII como le da la real gana. Y cuando desde windows subes ficheros por ftp a un servidor *nix, todos sabemos que es un problema muy serio. Yo me encontré que me había llenado los ficheros php con espacios en blanco al final, luego no iba ninguna función del tipo header()

Buscando por internet me encontré con un script que miraba si existían esos ficheros. Yo lo he modificado, añadido alguna funcionalidad extra y cambiado para que no solo avise sino que también lo arregle.

El funcionamiento es:

  • www.tusite/script.php?clear=0 Si solo quieres ver si hay ficheros con ese problema.
  • www.tusite/script.php?clear=1 Si lo quieres arreglar.

El código a continuación.

[php]
<?php
/***********************
*@author: Francisco Soto
*@author: Ritesh Agrawal
*@description: Identifies and fix php files that contain leading or trailing spaces before or after PHP opening or closings tags
*@version: 1.1
*@date: Sep 26, 2010
* – html based output
***********************/
//Set Source Path
$sourcepath = “.”;
//Set deep
$deep = 6;

//Regex Express to test leading and trailing spaces
define(“PRE”, “#^[\n\r|\n\r|\n|\r|\s]+<\?php#”);
define(“POST”, “#\?>[\n\r|\n\r|\n|\r|\s]+$#”);

define(“PHP”, “#[\.php|\.PHP|\.ctp|\.CTP]$#”);

//Clear the file Status Cache
clearstatcache();
//============ Code borrowed from php.net ===============
// Replace \ by / and remove the final / if any
$root = ereg_replace( “/$”, “”, ereg_replace( “[\\]“, “/”, $sourcepath ));
// Touch all the files from the $root directory
if( false === m_walk_dir( $root, “check”, true, $deep )) {
echo “‘{$root}’ is not a valid directory
“;
}
// Walk a directory recursively, and apply a callback on each file added a deep variable
function m_walk_dir( $root, $callback, $recursive = true, $deep ) {
$dh = @opendir( $root );
if ($deep==0)
return false;
if( false === $dh ) {
return false;
}
while( $file = readdir( $dh )) {
if( “.” == $file || “..” == $file ){
continue;
}
call_user_func( $callback, “{$root}/{$file}” );
if( false !== $recursive && is_dir( “{$root}/{$file}” )) {
m_walk_dir( “{$root}/{$file}”, $callback, $recursive,$deep-1 );
}
}
closedir( $dh );
return true;
}
//============== end ======================
//If file, checks whether there is any leading spaces before opening PHP tag or
// trailing spaces after closing PHP tag if variable clear is set to 1 made a trim of file contents.
function check( $path ) {

if( !is_dir( $path ) && preg_match(PHP, $path)) {
$fh = file_get_contents($path);
if ($_GET['clear']==1)
{
$f=trim(file_get_contents($path));
$fp = fopen($path, ‘w’);
fwrite($fp, $f);
fclose($fp);
}
else {
if(preg_match(PRE, $fh))
echo $path . ” — contains leading spaces
“;
if(preg_match(POST, $fh))
echo $path . ” — contains trailing spaces
“;
}
}
}
?>[/php]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>