Skip navigation.
Home

Code only a mother could love.

Wonky Code

The 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::

  1. Spelling Mistakes.
  2. Improper use of global variables in functions.
  3. 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);
    ?>
  4. 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;
?>

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I took some code out of this

I took some code out of this WTF and added some demo values. Guess how well the original code works ...
<? $temp = array(); $files=array(1,2,3); $tt=array(4,5,6);
// --- WTF CODE BEGIN ---
$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]);
// --- WTF CODE END ---
print_r($files);
Output:
Array
(
    [0] => 1
    [1] => 2
    [2] => 34
    [3] => 5
    [4] => 6
)
Wow, I'm impressed. I guess I will use this instead of array_merge() now, it makes so much more sense.

I forgot to mention that he m

I forgot to mention that he must be totally unaware of preg_replace, which would have saved him lots of lines.
Example from the WTF:
$dir=str_replace("\\","/",$dir);
$dir=str_replace("//","/",$dir);
$dir=str_replace("\\\\","/",$dir);
$dir=str_replace("///","/",$dir);
Much better solution:
$dir = preg_replace("#[/\\]+#", "/", $dir);

WTF! i submitted this code

WTF! i submitted this code!!! (not saying its mine, just that *I* submitted it). some poor sap in #php the other day was asking for help and pasted this to pastebin.com. I really couldn't figure out WTF he was trying to do here and figured it was perfect for thephpwtf.com. Its even got the same title I gave it. phrax stole my WTF!!!!

Whoops, sorry Au5lander. I

Whoops, sorry Au5lander. I forgot to give you the props when I posted it. My bad. Fixed!

Heh. No Problem. great si

Heh. No Problem. great site btw.

My eyes are hurting!What is

My eyes are hurting!

What is this code supposed to actually DO? It's something to do with files but other than that?

It's too early in the morning for this..

I especially like to exit w

I especially like to exit without giving any hint what happened

if(!isset($_GET["source"])||!$_GET["source"]||!isset($_GET["search"])||!$_GET["search"])
{
exit;
}

str_replace also accept array

str_replace also accept array...

Post new comment




*

  • Web and e-mail addresses are automatically converted into links.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <p> <br /> <br>