Dear all,
sorry, i know this code is far little too long to debug here, but there is
really annoying logical error. If someone debugs this, I really offer warm
virtual handshake.
What this code SHOULD do:
- read new (=updated) licensetext from file $license_path
then
- read and modify recursively all files from $current_dir, replacing old
licensetexts and licenseplacehol der text EITHER with new licentext OR with
licenseplacehol der
In other words, this is meant to make the updating of license text easy,
because you can put text like
<?php /* MY_APP_NAME AUTO-LICENCE-PLACEHOLDER /*>
to your files and make the replace operation with 1 command for all files.
PROBLEMS/WEIRD BEHAVIOURS:
- one extra linefeed seems to appear after last line in some operations. I
have tried to handle this, but still it comes.
- really weird: the code should (for debugging purpose) print the lines of
ORIGINAL file before the operation, yet it seems to print the lines of the
rewritten file. This is really really weird...'
Thanks very mych if you can help!!
=============== =============== ===
<?php
$x = new license_compres sor("TARU_LICEN SE_NOTE","../tarulicense.txt ");
$x->dance(1,"files ");
//$x->dance(0,"files ");
//$x->dance(1,"files ");
//$x->dance(0,"files ");
error_reporting (E_ALL);
class license_compres sor
{
var $version = "License compressor 1.0";
var $linestop ="\r\n";
var $ok = 1;
var $msgs = array();
var $license_filepa th;
var $license = "";
var $id = "";
var $php_begin = "";
var $php_end = "";
var $php_comp = "";
var $php_supp = "";
var $html_begin = "";
var $html_end = "";
var $html_comp = "";
var $html_supp = "";
var $gen_begin = "";
var $gen_end = "";
var $gen_comp = "";
var $gen_supp = "";
var $php_str = "";
var $html_str = "";
var $gen_str = "";
//
=============== =============== =============== =============== =============== =
function license_compres sor($id, $license_filepa th)
{
$this->msgs[] = $this->version;
$this->msgs[] = "============== =============== =============== ";
if(!is_string($ id) || !strlen($id))
{$this->ok = 0; $this->msgs[]= "License dentifier is not a valid
string."; }
else
{ $this->id = $id; $this->msgs[]= "License dentifier =
".htmlspecialch ars($id); }
if(!($this->license = file_get_conten ts($license_fil epath)))
{ $this->ok = 0; $this->msgs[]= "License filepath is not a valid
path."; }
else
{
$this->license_filepa th = $license_filepa th;
$this->msgs[]= "License filepath = ".htmlspecialch ars($license_fi lepath);
}
$this->php_begin = "<?php /* ".$this->id." AUTO-LICENSE-BEGIN";
$this->php_end = $this->id." AUTO-LICENSE-END */ ?>";
$this->php_comp = "<?php /* ".$this->id." AUTO-LICENSE-PLACEHOLDER */
?>";
$this->php_supp = $this->php_begin.$thi s->linestop.$th is->license.
$this->linestop.$th is->php_end;
$this->html_begin = "<!-- ".$this->id." AUTO-LICENSE-BEGIN";
$this->html_end = $this->id." AUTO-LICENSE-END -->";
$this->html_comp = "<!-- ".$this->id." AUTO-LICENSE-PLACEHOLDER -->";
$this->html_supp = $this->html_begin.$th is->linestop.$th is->license.
$this->linestop.$th is->html_end;
$this->gen_begin = "/* ".$this->id." AUTO-LICENSE-BEGIN";
$this->gen_end = $this->id." AUTO-LICENSE-END */";
$this->gen_comp = "/* ".$this->id." AUTO-LICENSE-PLACEHOLDER */";
$this->gen_supp = $this->gen_begin.$thi s->linestop.$th is->license.
$this->linestop.$th is->gen_end;
}
//
=============== =============== =============== =============== =============== =
function dance($suppress , $current_dir)
{
if(!is_dir($cur rent_dir))
{$this->ok = 0; $this->msgs[]= "Current directory path is not a valid
directory path."; }
else
{
$this->msgs[]= "Current directory = ".htmlspecialch ars($current_di r);
}
$this->show_msgs(); $this->msgs = array();
if(!$this->ok) { $this->msgs[]= "Execution terminated.";
$this->show_msgs(); $this->msgs = array(); return;}
$this->msgs[] = $suppress?"Mode = suppress":"Mode = compress";
if($suppress)
{ $this->php_str = $this->php_supp; $this->html_str = $this->html_supp;
$this->gen_str = $this->gen_supp;}
else
{ $this->php_str = $this->php_comp; $this->html_str = $this->html_comp;
$this->gen_str = $this->gen_comp;}
$this->do_recursion($ current_dir);
$this->show_msgs();
$this->msgs = array();
return;
}
//
=============== =============== =============== =============== =============== =
function do_recursion($c urrent_dir)
{
if (($dir_handle = @opendir($curre nt_dir)) !== false)
{
$this->msgs[] = "Opening directory
'".htmlspecialc hars($current_d ir)."'.";
}
else
{
$this->msgs[] = "Opening of directory '".
htmlspecialchar s($current_dir) ."' failed.";
return;
}
$entries = array();
while (false !== ($dir_entry = readdir($dir_ha ndle)))
{
$goodentry =$current_dir."/".$dir_entr y;
if(in_array($go odentry,$entrie s)) continue;
else $entries[]= $goodentry;
if(is_file($goo dentry))
{
$this->msgs[] = "Found file '".$goodentry." '.";
$arr = file($goodentry );
$lines = count($arr);
$this->msgs[]= "Rows in $goodentry: ".$lines;
$new_content ="";
$copyflag = 1;
$expected = false;
$error = 0;
$changes = 0;
foreach($arr as $line_nr =$line)
{
if($error) {continue 2;}
$stop = ($line_nr == $lines-1)?'':$this->linestop;
if($stop==='') $stopnote ="-"; else $stopnote ="r&n";
$line = trim($line);
$this->msgs[]= "Line nr:". ($line_nr+1).
" | Copyflag: $copyflag | Line: ".
htmlspecialchar s($line)." | Stop: $stopnote";
if($line === $this->php_comp && $copyflag)
{ $new_content .= $this->php_str.$sto p; $changes = 1;}
elseif($line === $this->html_comp && $copyflag)
{ $new_content .= $this->html_str.$stop ; $changes = 1;}
elseif($line === $this->gen_comp && $copyflag)
{ $new_content .= $this->gen_str.$sto p; $changes = 1;}
elseif($line === $this->php_begin && $copyflag)
{ $new_content .= $this->php_str.$sto p; $copyflag = 0; $expected =
"PHPEND";
$changes = 1;}
elseif($line === $this->html_begin && $copyflag)
{ $new_content .= $this->html_str.$stop ; $copyflag = 0; $expected =
"HTMLEND";
$changes = 1;}
elseif($line === $this->gen_begin && $copyflag)
{ $new_content .= $this->gen_str.$sto p; $copyflag = 0; $expected =
"GENEND";
$changes = 1;}
elseif($line === $this->php_end)
{
if(!$copyflag && $expected === "PHPEND")
{$copyflag = 1; $expected = false;}
else
{
$this->msgs[]="File '"
.htmlspecialcha rs($goodentry)
."' could not be processed.";
$error=1;
}
}
elseif($line === $this->html_end)
{
if(!$copyflag && $expected === "HTMLEND")
{$copyflag = 1; $expected = false;}
else
{
$this->msgs[]="File '"
.htmlspecialcha rs($goodentry)
."' could not be processed.";
$error=1;
}
}
elseif($line === $this->gen_end)
{
if(!$copyflag && $expected === "GENEND")
{$copyflag = 1; $expected = false;}
else
{
$this->msgs[]="File '"
.htmlspecialcha rs($goodentry)
."' could not be processed.";
$error=1;
}
}
elseif($copyfla g)
{ $new_content .= $arr[$line_nr]; }
}
if(!$changes)
{
$this->msgs[]="No need to rewrite file '".
htmlspecialchar s($goodentry)." '.<br>";
}
elseif
(
(!$error)
&&
($filehandle = fopen($goodentr y, 'w'))
&&
(fwrite($fileha ndle , $new_content))
&&
(fclose($fileha ndle))
)
{
$this->msgs[]="File '".
htmlspecialchar s($goodentry)." ' has been succesfully
rewritten.<br>" ;
}
else
{
$this->msgs[]="There was an error while trying to save file '".
htmspecialchars ($goodentry)."' .<br>";
}
}
elseif(is_dir($ goodentry) && $dir_entry !== "." && $dir_entry !== "..")
{
$this->do_recursion($ goodentry);
}
}
closedir($dir_h andle);
}
//
=============== =============== =============== =============== =============== =
function show_msgs()
{
echo "<p style='font-family: courier'>";
foreach($this->msgs as $msg)
echo "$msg<br>";
echo "</p>";
}
//
=============== =============== =============== =============== =============== =
}