#!/usr/bin/perl # # nph-debug.cgi Simple CGI/PHP debug aid # # Instruction: # Using WebFTP of PoWeb, # 0. Make sure that the directory of failing script is set to 710 (or 755), # NOT 777, 775, NOT 644,600,700. # 1. Go into the directory of failing script. # 2. Click on "Create file" button. # 3. Paste this code ("#!/usr/bin/perl" must be the first line). # 4. Save is as "nph-debug.cgi" (It must be all lower case for this). # 5, chmod 700 (755 is OK, too). # 6. Access http://YOURSITE.COM/SUBDIR/nph-debug.cgi # 7. Click on the name of failing script. # 8. See what happens. # Post the result to PowWeb forum if you can't solve it by yourself. # $|=1; print "HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html\n\n"; #open(STDERR,">&ERROR.txt"); open(STDERR,">&STDOUT"); #print "nph-debug.cgi ver. 1.5
\n"; $name = $0; $cginame = $ARGV[0]; $red=""; if( $cginame eq '' ){ print <<"INITIAL_LIST";

Please choose the script to debug

Following checks will be performed:
\tSyntax check, CR/LF line ending check
\tActual execution

Your script may behave differently, and it may corrupt
datafile in rare cases.
Make sure you have full backup.
Also, password protect the directory during debugging.
INITIAL_LIST
 @cgilist = split("\n",`ls -alt *.cgi *.pl *.php *.sh`);
 foreach $t (@cgilist){
   $t =~s/([^ ]+\.(cgi|pl|php|sh))$/$1<\/a>/;
   print "$t\n";
 }
}else{
 open(FD, $cginame) || die("${red}Error: cannot open file.($cginame)");
 $line = ;
 close(FD);
 $crlf = 1 if $line =~s/\r\n//;
 if($crlf){
   open(FD, $cginame) || die("${red}Error: cannot open file.($cginame)");
   print "${red}Wrong line ending 'CR/LF' detected. 
This is usually caused by wrong FTP transfer mode (binary).
Now, trying to correct it.
"; @lines = ; close(FD); rename($cginame, $cginame.".bak"); $allline = join("", map { s/\r\n/\n/; $_; } @lines); open(FD2, ">$cginame") || die("${red}Error: cannot open file.($cginame) for writing"); print FD2 $allline; close(FD2); chmod(0700, $cginame); print "Correction done!
\n"; } if( $line =~/^#!/ ){ $cmd = $line; $cmd =~ s/#!//; chomp($cmd); if( ! -f $cmd ){ print "${red}'$cmd' used in the shebang line not found\n"; }elsif( ! -x $cmd ){ print "${red}'$cmd' used in the shebang line isn't executable\n"; } } $ENV{SCRIPT_FILENAME} =~s/$name/$cginame/; $ENV{SCRIPT_NAME} =~s/$name/$cginame/; $ENV{SCRIPT_URL} =~s/$name/$cginame/; $ENV{SCRIPT_URI} =~s/$name/$cginame/; $ENV{REQUEST_URI} =~s/$name/$cginame/; $ENV{QUERY_STRING} = ""; if( $line =~/perl/){ $syntax = `export -p >/dev/null;perl -c $cginame 2>&1`; }elsif( $line =~/bin\/sh/){ $syntax = `export -p >/dev/null;/bin/sh -s $cginame 2>&1`; # }elsif( $cginame =~/php$/){ # $syntax = `export -p >/dev/null;/usr/local/bin/php -l $cginame 2>&1`; # }elsif( $cginame =~/php5$/){ # $syntax = `export -p >/dev/null;/usr/local/php5/bin/php -l $cginame 2>&1`; } if($syntax && $syntax !~/syntax OK/){ print <<"SYNTAX_END"; ${red}
-----nph-debug------
Syntax Error and/or warning detected
$syntax
------------
SYNTAX_END }else{ if($cginame !~/\.php.?$/ && $line !~/php/){ if( ! -x $cginame){ print "${red}$cginame doens't have the executable permission
\n"; print "Setting the correct permission of 0700

\n"; chmod(0700, $cginame); } print `export -p >/dev/null; ./$cginame 2>&1`; }elsif( $cginame =~/php$/){ print `export -p >/dev/null;/usr/local/bin/php -l -f $cginame 2>&1`; }elsif( $cginame =~/php5$/){ print `export -p >/dev/null;/usr/local/php5/bin/php -l -f $cginame 2>&1`; }else{ print "Unlnown command\n"; } print "
-----nph-debug------\n";
 }
 print `ls -al $cginame`;
 print "\nFirst line: $line";
 print "Line ending seems to be correct (LF)\n" if !$crlf;
}
print "\n------done ver. 1.5------";
exit 0;
1;