<?php

?>

ASCII Scanner
(c) Schien Dong, Antradar Software Inc. 2018

<?php

$req=2;
$self=strtolower($argv[0]);
$exemode=1;

if (strpos($self,'.php')!==false) {$req=2;$exemode=0;}

if (count($argv)!=$req){
?>
Syntax: <?php echo $exemode?'asciiscan':'php asciiscan.php'?> [init_directory]

<?php	
	die();
}

$maindir=trim($argv[$req-1]);

asciiscan($maindir);


function asciiscan($dir){
	
	if (!is_dir($dir)) {
?>
Error: <?php echo $dir;?> is not a valid directory.

<?php		
		die();
	}
		
	if (!$dh=opendir($dir)){
?>
Error: cannot open directory <?php echo $dir;?>

<?php		
	}
	
	//echo "Entering $dir\r\n";
	
	while (($file = readdir($dh)) !== false) {
		$parts=explode('.',$file);
		$ext=$parts[count($parts)-1];
		
		$type=filetype($dir.'/'.$file);
		
		if ($type=='dir'){
			if ($file!='.'&&$file!='..'&&$file!='.svn'&&$file!='.git') asciiscan($dir.'/'.$file);		
		} else {
			if ($ext=='php'||$ext=='js'){
				analyse($dir.'/'.$file);
			}
		}
		
	}//while
	closedir($dh);    	
		
}//func asciiscan

function analyse($fn){
	
	$buf=array();
	
	$f=fopen($fn,'rb');
	$buf=fread($f,3);
	fclose($f);
	
	$bad=0;
	for ($i=0;$i<3;$i++){
		$c=$buf[$i];
		if (ord($c)>126) $bad=1;
	}

	if ($bad) echo "$fn\r\n";
}



