DokuWiki

It's better when it's simple

User Tools

Site Tools


tips:findbadphp.php

findbadphp.php

Use this script to find corrupted PHP files. Place it in your wiki root and call it from your browser.

findbad.php
<?php
/**
 * This script will search all PHP files in the same directory
 * and below and will check for trailing or leading whitespaces
 * and UTF-8 BOMs
 */
 
header("Content-Type: text/plain");
 
$here=dirname(__FILE__);
$exceptions=Array(
    $here.'/conf/users.auth.php',
    $here.'/conf/acl.auth.php',
    $here.'/lib/images/fileicons/index.php',
    $here.'/lib/images/smileys/index.php',
    $here.'/lib/images/fileicons/svg/index.php',
    $here.'/bin/striplangs.php',
    $here.'/bin/gittool.php',
    $here.'/bin/dwpage.php',
    $here.'/bin/plugin.php',
    $here.'/bin/indexer.php',
    $here.'/bin/render.php',
    $here.'/bin/wantedpages.php',
);
 
echo "starting...\n";
flush();
traverse($here);
echo "finished...\n";
 
function traverse($dir){
    global $exceptions;
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            //skip hidden files and upper dirs
            if(preg_match('/^[\._]/',$file)) continue;
            if(is_dir($dir.'/'.$file)){
                traverse($dir.'/'.$file);
                continue;
            }
            flush();
 
            if(!preg_match('/\.php$/',$file)) continue;
            if(in_array("$dir/$file", $exceptions)) continue;
            $check = @file_get_contents("$dir/$file",0,null,0,3);
            if(!$check){
                echo "$dir/$file is not readable.\n";
                continue;
            }
 
            if($check == "\xef\xbb\xbf"){
                echo "$dir/$file has UTF-8 BOM at start.\n";
                continue;
            }
 
            if($check != '<?p'){
                echo "$dir/$file doesn't start with <?php\n";
                continue;
            }
 
            $check = file_get_contents("$dir/$file",0,null,
                                filesize("$dir/$file")-5,5);
            $pos = strpos($check, '?>');
            if($pos !== false && !(substr($check,-2) == '?>' || substr($check,-3) == "?>\n") ){
                echo "$dir/$file has trailing chars after closing ?>\n";
                continue;
            }
       }
       closedir($dh);
   }
}
tips/findbadphp.php.txt · Last modified: 2022-01-26 05:56 by schplurtz

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki