archives
Code only a mother could love.
Submitted by phrax on Mon, 2004-12-06 14:28. Wonky CodeThe title is actually misleading. If I wrote this and my mum was a programmer she would probably beat me stupid. Luckily she isn't a programmer, I don't live at home and I didn't write this.
Props to Au5lander for sending this in with the title, "sometimes you just have to start over". In this case I can't disagree! This code makes it as a WTF because it is just so ugly! You have to concentrate just read it!
Other WTFs::
- Spelling Mistakes.
 - Improper use of global variables in functions.
 - WTF...
<?php
while(strstr($search," ")!=FALSE)
$search=str_replace(" "," ",$search);
while(strstr($search," ")!=FALSE)
$search=str_replace(" "," ",$search);
while(strstr($search," ")!=FALSE)
$search=str_replace(" "," ",$search);
while(strstr($search," ")!=FALSE)
$search=str_replace(" "," ",$search);
?> - Minor WTF. Returning empty string on error, array on success: 
<?php
function get_dir($s){
global $dir,$url,$maxf;
$dp=opendir($s);
if($dp==FALSE)
return "";
for($nfiles=0;$tmp=readdir($dp);$nfiles++)
{
if($tmp)
$files[$nfiles]=$tmp;
else
break;
}
closedir($dp);
return $files;
}
?> 
There are piles of other little WTF's. I'll let you find the others.
<?php
function getcurl($file,$n){
global $dir,$url,$maxf,$originaldir;
$cwd = $url;
if(isset($ret))
unset($ret);
if($n)
{
    if(strstr(substr($url,0,7),"http://")==FALSE)
        $httpurl=false;
    else
        $httpurl=true;
    if($originaldir)
        $ret=$cwd."/".$originaldir."/".$file;
    else
        $ret=$cwd."/".$dir."/".$file;
}
else
    $ret=$cwd;
$ret=str_replace("\\","/",$ret);
$ret=str_replace("\\\\","/",$ret);
$ret=str_replace("////","/",$ret);
$ret=str_replace("///","/",$ret);
$ret=str_replace("//","/",$ret);
if($httpurl)
    $ret="http://".substr($ret,6,strlen($ret)-6);
return $ret;
}
function get_dir($s){
global $dir,$url,$maxf;
$dp=opendir($s);
if($dp==FALSE)
    return "";
for($nfiles=0;$tmp=readdir($dp);$nfiles++)
{
    if($tmp)
        $files[$nfiles]=$tmp;
    else
        break;
}
closedir($dp);
return $files;
}
function gfinf(){
global $dir,$url,$maxf,$originaldir;
if(isset($ret))
    unset($ret);
settype($ret,"string");
$cwd=getcwd()."/";
if(!is_dir($dir))
{
    $originaldir=$dir;
    $dir=$cwd.$dir;
}
$dir=str_replace("\\","/",$dir);
$dir=str_replace("//","/",$dir);
$dir=str_replace("\\\\","/",$dir);
$dir=str_replace("///","/",$dir);
if(defined("PHP5")&&PHP5)
    $files=scandir($dir);
else
    $files=get_dir($dir);
$nfiles=count($files);
if(isset($search))
    unset($search);
$search=$_GET["search"];
$search=str_replace("/"," ",$search);
$search=str_replace("\\"," ",$search);
$search=str_replace("-"," ",$search);
$search=str_replace("."," ",$search);
$search=str_replace(","," ",$search);
while(strstr($search,"     ")!=FALSE)
    $search=str_replace("     "," ",$search);
while(strstr($search,"    ")!=FALSE)
    $search=str_replace("    "," ",$search);
while(strstr($search,"   ")!=FALSE)
    $search=str_replace("   "," ",$search);
while(strstr($search,"  ")!=FALSE)
    $search=str_replace("  "," ",$search);
$tmo=$search;
if(isset($search))
    unset($search);
$search=explode(" ",$tmo);
$nsearch=count($search);
settype($ntt,"integer");
for($n=0;isset($files[$n])&&$files[$n];$n++)
{
    if(is_dir($dir.$files[$n]))
    {
        if(defined("PHP5")&&PHP5)
            $ss=scandir($dir.$files[$n]);
        else
            $ss=get_dir($dir.$files[$n]);
        $nss=count($ss);
        if($nss>2)
        {
            for($nn=0;$nn<$nss;$nn++)
            {
                if(!is_dir($dir.$files[$n].$ss[$nn]))
                {
                    $tt[$ntt]=$ss[$nn];
                    $ntt++;
                }
            }
        }
    }
}
if(isset($temp))
    unset($temp);
$temp[1]=implode("/¨*'*¨/",$files);
$temp[2]=implode("/¨*'*¨/",$tt);
if(isset($files))
    unset($files);
if(isset($tt))
    unset($tt);
$temp[3]=$temp[1].$temp[2];
$files=explode("/¨*'*¨/",$temp[3]);
$nfiles=count($files);
if(isset($temp))
    unset($temp);
$nn=$nfiles;
if(isset($files[$nn]))
    $nn++;
for($n=0;$n<$nfiles;$n++)
{
    if(isset($files[$n]))
    {
        if(!is_dir($dir.$files[$n])&&!is_dir($files[$n]))
        {
            if($files[$n]!=".."&&$files[$n]!=".")
            {
                $t[$nn]=$files[$n];
                $nn++;
            }
        }
    }
}
if(isset($files))
    unset($files);
if(isset($tt))
    unset($tt);
settype($files,"array");
$tt=implode("/¨*'*¨/",$t);
if(isset($t))
    unset($t);
$files=explode("/¨*'*¨/",$tt);
if(isset($tt))
    unset($tt);
$nfiles=count($files);
if(isset($files[$nn]))
    $nn++;
for($n=0;$n<$nfiles&&$n<=$maxf;$n++)
{
    for($nn=0;$nn<$nsearch;$nn++)
    {
        if(strstr($files[$n],$search[$nn])!=FALSE)
        {
            if(isset($putfile))
                unset($putfile);
            $putfile=substr($dir.$files[$n],strlen($dir));
            $o="filename=$putfile&sha1=".sha1_file($dir.$files[$n]).
               "&md5=".md5_file($dir.$files[$n]).
               "&size=".filesize($dir.$files[$n]).
               "&url=".getcurl($putfile,1);
            if(!isset($nfirst)||!$nfirst)
            {
                $nfirst=1;
                $ret=$o;
            }
            else
            $ret.="\n".$o;
        }
    }
}
return $ret;
}
//Begining
require_once("config.php");
if(!isset($_GET["source"])||!$_GET["source"]||!isset($_GET["search"])||!$_GET["search"])
{
    exit;
}
if(strlen($_GET["search"])<$searchlen[1])
    die("ERROR:MinimumSearchLenght:".$searchlen[1]);
if($searchlen[2]!=0)
{
    if(strlen($_GET["search"])>$searchlen[2])
        die("ERROR:MaximumSearchLenght:".$searchlen[2]);
}
$ret=gfinf();
if($ret)
    die($ret);
exit;
?>

