![]() |
| |||
| 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 licenseplaceholder text EITHER with new licentext OR with licenseplaceholder 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_compressor("TARU_LICENSE_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_compressor { var $version = "License compressor 1.0"; var $linestop ="\r\n"; var $ok = 1; var $msgs = array(); var $license_filepath; 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_compressor($id, $license_filepath) { $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 = ".htmlspecialchars($id); } if(!($this->license = file_get_contents($license_filepath))) { $this->ok = 0; $this->msgs[]= "License filepath is not a valid path."; } else { $this->license_filepath = $license_filepath; $this->msgs[]= "License filepath = ".htmlspecialchars($license_filepath); } $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.$this->linestop.$this->license. $this->linestop.$this->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.$this->linestop.$this->license. $this->linestop.$this->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.$this->linestop.$this->license. $this->linestop.$this->gen_end; } // ================================================== ========================== function dance($suppress, $current_dir) { if(!is_dir($current_dir)) {$this->ok = 0; $this->msgs[]= "Current directory path is not a valid directory path."; } else { $this->msgs[]= "Current directory = ".htmlspecialchars($current_dir); } $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($current_dir) { if (($dir_handle = @opendir($current_dir)) !== false) { $this->msgs[] = "Opening directory '".htmlspecialchars($current_dir)."'."; } else { $this->msgs[] = "Opening of directory '". htmlspecialchars($current_dir)."' failed."; return; } $entries = array(); while (false !== ($dir_entry = readdir($dir_handle))) { $goodentry =$current_dir."/".$dir_entry; if(in_array($goodentry,$entries)) continue; else $entries[]= $goodentry; if(is_file($goodentry)) { $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: ". htmlspecialchars($line)." | Stop: $stopnote"; if($line === $this->php_comp && $copyflag) { $new_content .= $this->php_str.$stop; $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.$stop; $changes = 1;} elseif($line === $this->php_begin && $copyflag) { $new_content .= $this->php_str.$stop; $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.$stop; $copyflag = 0; $expected = "GENEND"; $changes = 1;} elseif($line === $this->php_end) { if(!$copyflag && $expected === "PHPEND") {$copyflag = 1; $expected = false;} else { $this->msgs[]="File '" .htmlspecialchars($goodentry) ."' could not be processed."; $error=1; } } elseif($line === $this->html_end) { if(!$copyflag && $expected === "HTMLEND") {$copyflag = 1; $expected = false;} else { $this->msgs[]="File '" .htmlspecialchars($goodentry) ."' could not be processed."; $error=1; } } elseif($line === $this->gen_end) { if(!$copyflag && $expected === "GENEND") {$copyflag = 1; $expected = false;} else { $this->msgs[]="File '" .htmlspecialchars($goodentry) ."' could not be processed."; $error=1; } } elseif($copyflag) { $new_content .= $arr[$line_nr]; } } if(!$changes) { $this->msgs[]="No need to rewrite file '". htmlspecialchars($goodentry)."'.<br>"; } elseif ( (!$error) && ($filehandle = fopen($goodentry, 'w')) && (fwrite($filehandle , $new_content)) && (fclose($filehandle)) ) { $this->msgs[]="File '". htmlspecialchars($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_handle); } // ================================================== ========================== function show_msgs() { echo "<p style='font-family: courier'>"; foreach($this->msgs as $msg) echo "$msg<br>"; echo "</p>"; } // ================================================== ========================== } |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |