"Not too long ago," Jess writes, "I adopted an application that needed 'a rather minor change' to its functionality. Naturally, when I started, the project owner had no idea what file or directory the functionality was in, so he gave me access to the server and sent me off. After wading through a number of oddly named directories trying to find where the site was even located, I finally found the index file I had hoped would set me in the right direction."

"Of course, it didn't. After twenty minutes of jumping from page to page to page, I realized that I'd simply have to grep the entire application: a gig or so of content with tens of thousands of files within hundreds of directories. After nothing turned up, I quickly realized that most of the files had completely meaningless extensions: .html files had lots of PHP, .php4 files had PHP5, and .php files rarely had any PHP.

"After expanding my search, I noticed a curious directory named "google". It was packed with files like this:

cache_results.php        engines_2.php            engines--.php  
googlesearch.inc.php     index.php                ORIGINALengines.php
engines_old.php          engines-.php             g_original.php 
my_search_results.inc    query.php                search_results.inc.php
engines_2007-05-01.php   -engines-.php            engines.php
index.html               search.html              test.php

"Upon closer inspection, I discovered that nearly all of the similarly named files had nearly identical content, and were all referenced throughout the other files in various directories, all which may or may not have been used in the site. Oh, and they all looked like this...

<?php
class GoogleSearch
{ var $url; var $lang;
  var $cookie; var $query;
  var $rpp; var $ra; var $rb;
  var $results; var $referer;
  var $nresults;

  function GoogleSearch()
  { $this->url='http://www.google.com/'; $this->lang='en'; $this->rpp=100;
    $this->ra=0; $this->rb=0; $this->query=''; $this->nresults=0;
    $html=Browser($this->url); $this->cookie=GetCookies($html); }

  function GoogleQuery($q,$n=1)
  { if (!($q==$this->query && $n>=$this->ra && $n<=$this->rb))
    { $u=$this->url.'search?q='.urlencode($q)."&num=".$this->rpp."&hl=".$this->lang."&lr=&safe=off&sa=N";
      $this->ra=(($n-1)-($n-1)%$this->rpp)+1; $this->rb=$this->ra+$this->rpp-1;
      if ($n>$this->rpp) $u.='&start='.($this->ra-1);
      $this->query=$q; $html=Browser($u,'GET','',$this->cookie,$this->referer); $this->referer=$u;
      $this->results=array();
      $p=0; do { $p=strpos($html,'<p class=g>',$p);
                 if ($p!==false)
                 { $r=array();
                   $a=strpos($html,'"',$p+11)+1; $b=strpos($html,'"',$a);
                   $u=substr($html,$a,$b-$a);
                   if (substr($u,0,4)==='/url') $u=substr($u,strpos($u,'&q=')+3);
                   $r['url']=$u;
                   $a=strpos($html,'>',$b)+1; $b=strpos($html,'</a',$a); $p=$b;
                   $r['meta']=strip_tags(substr($html,$a,$b-$a));
                   $a=strpos($html,'<font',$b); $b=strpos($html,'Similar pages',$a);
                   $u=substr($html,$a,$b-$a); $b=strpos($u,'color=#008000>');
                   if ($b!==false)
                    $r['content']=trim(strip_tags(strtr(substr($u,0,$b+14),array("<br>"=>" "))));
                   else $r['content']='';
                   $this->results[]=$r; }
               } while ($p!==false);
    $a=strpos($html,'of about <b>');
    if ($a!==false)
    { $a+=12; $b=strpos($html,'</b',$a);
      $this->nresults=strtr(substr($html,$a,$b-$a),array(','=>'','.'=>'')); }
    else $this->nresults=0; }
    $u=$this->results[$n-$this->ra];
    return $u; }

  function ResultsNumber() { return $this->nresults; }
}
?>

"Yeah, this code has it all: not a single comment, variable names between 1 and 3 letters long, almost no formatting, and a whole bunch of statements packed into a single line. I won't even go into all the actual problems with the code.

"As for what this file does... how it's being used... if it's being used... I still have absolutely no idea. But the good news is, I've learned my lesson about projects needing only 'a rather minor change'.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!