<?php

// DO NOT EDIT THIS FILE

function yah ()
{
  global $file;
  global $i;
  global $scriptName;

  $begNav = "<!-- E4:START:NAV_BAR -->";
  $endNav = "<!-- E4:END:NAV_BAR -->";

  $found = 0;
  while (!$found)
  {
     if (strpos("_".$file[$i],$begNav))
     {
       $found = 1;
     }
     $i++;
  }

  $found = 0;
  $j = 0;
  while (!$found)
  {
     if (strpos("_".$file[$i],$endNav))
     {
       $found = 1;
     }
     else
     {
       $youarestring = $youarestring.$file[$i];
     }
     $i++;
     $j++;
  }

  $youarebits = explode ("<", $youarestring);
  for ($k = 0 ; $k <  sizeof ($youarebits) ; $k++)
  {
    unset ($yregs);
    eregi("a href=\"(.*)\">(.*)", $youarebits[$k], $yregs);
    if (sizeof ($yregs) > 1)
    {
      if (trim($yregs[2]) == "Netscape Search")
      {
        // do nowt
      }
      else if (trim($yregs[2]) == "Home")
      {
         $youarelink[] = $scriptName;
         $youarecat[] = $yregs[2];
      }
      else
      {
         $youarelink[] = $scriptName."?dir=".$yregs[1];
         $youarecat[] = $yregs[2];
      }
    }
  }
  return array ($youarelink, $youarecat);
}

function title ()
{
  global $file;
  global $i;

  $begTitle = "<!-- E5:START:TITLE -->";
  $endTitle = "<!-- E5:END:TITLE -->";
  $titleRegex = "<!-- E5:START:TITLE -->(.*)<!-- E5:END:TITLE -->";

  $found = 0;
  $j = 0;
  while (!$found)
  {
     if (strpos("_".$file[$i],$begTitle))
     {
       $found = 1;
       //$heading[$j] = $file[$i];
       $titlestring = $titlestring.$file[$i];
       $j++;
     }
     $i++;
  }
  $found = 0;
  while (!$found)
  {
     if (strpos("_".$file[$i],$endTitle))
     {
       $found = 1;
       $titlestring = $titlestring.$file[$i];
     }
     else
     {
       $titlestring = $titlestring.$file[$i];
     }
     $i++;
     $j++;
  }

  eregi ($titleRegex, $titlestring, $tregs);
  $title = $tregs[1];
  return $title;
}

function getContents()
{
  global $file;
  global $i;
  global $scriptName;

  $begCon = "<Table Border=0 Cellpadding=0 Cellspacing=0 Width=600>";
  $endCon = "</table>";
  $seeAlso = "See also";
  $edIdent = "profile";
  $edRegex = "<a href=\"(.*)\">(.*)</a>(.*)";
  $catRegex = "<a href=\"(.*)\">.*<b>(.*)</b></a>(.*)";
  $siteRegex = "<a href=\"(.*)\">(.*)</a>(.*)";
  $edchoice = "ed_choice.gif";
  $coolRegex1 = "<img .*>(.*)";
  $coolRegex2 = "<img .*>(.*)</ul>.*";
  $begAlpha = "\[";
  $endAlpha = "\]";
  $alphaRegex = "<a href=\"(.*)\"><b>(.*)</b></a>";


  $m = 0;
  $j = 0;
  do
  {
     //find "["
     //which starts the alphabet bar
    $found = 0;
    while ((!$found) && ($i <= sizeof($file)))
    {
       if (eregi($begAlpha,$file[$i]))
       {
         $al[$j] = $file[$i];
         $found = 1;
         //echo "open bracket found";
       }
       $i++;
    }
  } while ((!$found) && ($i <= sizeof($file)));
  if ($found)
  {
       // grab everything from here til the next "]"
     do
     {
      $found = 0;
       $j++;
       while ((!$found) && ($i <= sizeof($file)))
       {
          $al[$j] = $file[$i];
          if (eregi($endAlpha,$file[$i]))
          {
            $found = 1;
            //echo "end bracket found";
          }
          $i++;
          $j++;
       }
       $j++;
       $m++;
     }
     while ( (!$found) && ($i <= sizeof($file)) );
  }

  for ($i = 0 ; $i < sizeof ($al) ; $i++)
  {
    $lineal = eregi_replace("href=\"/", "href=\"".$scriptName."?dir=", $al[$i]);
    unset($regsal);
    eregi($alphaRegex,$lineal, $regsal);
    if (sizeof ($regsal) > 1)
    {
       $allink[] = $regsal[1];
       $alcategory[] = $regsal[2];
       //echo ("Alpha link: ".$regsal[1]." to ".$regsal[2]);
    }
  }
  //echo sizeof ($alcategory)." alpha links found";

  $m = 0;
  $j = 0;
  $i = 0;
  do
  {
    // find "<Table Border=0 Cellpadding=0 Cellspacing=0 Width=600>"
    // which starts the categories tables
    $found = 0;
    while ((!$found) && ($i <= sizeof($file)))
    {
       if (eregi($begCon,$file[$i]))
       {
         //echo "top of table found";
         $line[$j] = $file[$i];
         $found = 1;
       }
       $i++;
    }
    // grab everything from here til the next "</table>"
    $found = 0;
    $j++;
    $seealso = 0;
    while ((!$found) && ($i <= sizeof($file)))
    {
       //echo "line grabbed";
       $line[$j] = $file[$i];
       if ($seealso)
       {
         $see[$j] = 1;
       }
       else
       {
         $see[$j] = 0;
       }
       if (eregi($seeAlso,$file[$i]))
       {
         $seealso = 1;
       }
       if (eregi($endCon,$file[$i]))
       {
         //echo "end found";
         $found = 1;
         $seealso = 0;
         $lastline = $i;
       }
       $i++;
       $j++;
    }
    $j++;
    $m++;
  }
  while ($i <= sizeof($file));

  //$i = $lastline;
  $i = 0;
  do
  {
     if (eregi($edIdent,$file[$i]))
     {
       $eds = $eds.$file[$i];
       $i++; // netscape now split eds links across 2 lines
       $eds = $eds.$file[$i].";"; //semi colon added as netscape on't have a delimiter between eds
     }

     $i++;
  }
  while ($i <= sizeof($file));

  $edbits = explode (";",$eds);
  //echo "editors: ".htmlspecialchars($eds)." end eds";
  for ($i = 0 ; $i < sizeof ($edbits) -1 ; $i++) // last one will be a dummy
  {
     eregi($edRegex,$edbits[$i], $regs3);
     $edlink[] = "http://dmoz.org".$regs3[1];
     $edname[] = $regs3[2];
  }

  $linea = implode ("", $line);
  $line = explode ("<li>", $linea);

  // now print the stuff to see what we've got
  $issee = 0;
  // disregard the first line
  for ($i = 1 ; $i < sizeof ($line) ; $i++)
  {
    flush ();
    $line2 = eregi_replace("href=\"/", "href=\"".$scriptName."?dir=", $line[$i]);
   // echo "<hr>processing line<br>".htmlspecialchars($line[$i])."<hr>";
    unset($regs);
    eregi($catRegex,$line2, $regs);
    if (sizeof ($regs) > 1)
    {
        //echo "match found<br>";
        if (eregi ($edchoice, $regs[3]))
        {
          //echo "cool link<br>";
          $coollinkref[] = $regs[1];
          $coollinkname[] = $regs[2];
          unset ($coolregs);
          //echo "<hr>".htmlspecialchars($regs[3])."<hr>";
          eregi($coolRegex2, $regs[3], $coolregs);
          if (sizeof ($coolregs) > 1)
          {
           // echo "second version";
            $cooldescription[] = $coolregs[1];
          }
          else
          {
           // echo "trying first version";
            unset ($coolregs2);
            eregi($coolRegex1, $regs[3], $coolregs2);
            if (sizeof ($coolregs2) > 1)
            {
              $cooldescription[] = $coolregs2[1];
            }
            else
            {
             //  echo "empty description";
               $cooldescription[] = "";
            }
          }
          $issee = 0;
        }
        else if (!$issee)
        {
         //  echo "category link<br>";
           $link[] = $regs[1];
           $category[] = $regs[2];;
        }
        else
        {
         // echo "related link<br>";
          $rellink[] = $regs[1];
          $relcategory[] = $regs[2];
        }
        if ( (!$issee) && (eregi($seeAlso,$line2)))
        {
          $issee = 1;
        }
    }
    else
    {
      // echo "checking for site...";
       unset($regs2);
       eregi($siteRegex,$line2, $regs2);
       if (sizeof ($regs2) > 1)
       {
        //   echo "site link<br>";
           $linkref[] = $regs2[1];
           $linkname[] = $regs2[2];
           $description[] = $regs2[3];
       }
    }
  }
  return array ($link, $category, $rellink, $relcategory, $linkref, $linkname, $description, $edlink, $edname, $coollinkref, $coollinkname, $cooldescription, $allink, $alcategory);
}

function outputTemplates ()
{
   global $title;       // page title
   global $youarelink;  // links up through the directory hierarchy
   global $youarecat;   // categories up through the directory hierarchy
   global $link;        // links to sub categories in the directory
   global $category;    // names of sub categories in the directory
   global $rellink;     // links to related categories in the directory
   global $relcategory; // names of related categories in the directory
   global $linkref;     // urls of external sites
   global $linkname;    // names of external sites
   global $description; // description of external sites
   global $coollinkref; // urls of external sites tagged as cool
   global $coollinkname;// names of external sites tagged as cool
   global $cooldescription; // description of external sites tagged as cool
   global $edlink;      // link to editor profile page
   global $edname;      // name of editor
   global $templateDir; // directory containing the templates
   global $allink;        // links to a-z sub categories in the directory
   global $alcategory;    // names of a-z sub categories in the directory

   $tpl = new FastTemplate($templateDir);

   $tpl->define(
   	array(
   		main    => "main.tpl",
   		cat     => "cats.tpl",
     alphacat => "alphacats.tpl",
     rel     => "relcats.tpl",
     link     => "links.tpl",
     coollink => "coollinks.tpl",
     youare     => "youarehere.tpl",
     editor     => "editors.tpl"
   	)
   );

   $tpl->define_dynamic("catrow", "cat");
   $tpl->define_dynamic("alpharow", "alphacat");
   $tpl->define_dynamic("relrow", "rel");
   $tpl->define_dynamic("linkrow", "link");
   $tpl->define_dynamic("coollinkrow", "coollink");
   $tpl->define_dynamic("youarerow", "youare");
   $tpl->define_dynamic("editorrow", "editor");

   $become = becomeAnEditor();
   $tpl->assign(array(TITLE => $title,
                      SEARCHBOX => getSearchBox (),
                      BECOMEANEDITOR => $become));

   if (sizeof($youarecat) == 0)
   {
      $tpl->assign(array(YOUAREHERE => ""
                      ));
   }
   else
   {
      for ($i = 0 ; $i < sizeof($youarecat) ; $i++)
      {
      	$tpl->assign(
      		array(
      			YOUARELINK		=>	$youarelink[$i],
      			YOUARECAT	=>	$youarecat[$i]
      		)
      	);

      	$tpl->parse(YOUAREHERE,".youarerow");
      }
      $tpl->parse(YOUAREHERE, array ("youare"));
   }

   if (sizeof($edname) == 0)
   {
      $tpl->assign(array(EDITORS => " "
                      ));
   }
   else
   {
      for ($i = 0 ; $i < sizeof($edname) ; $i++)
      {
      	$tpl->assign(
      		array(
      			EDITORLINK		=>	$edlink[$i],
      			EDITOR	=>	$edname[$i]
      		)
      	);

      	$tpl->parse(EDITORS,".editorrow");
      }
      $tpl->parse(EDITORS, array ("editor"));
   }

   if (sizeof($category) == 0)
   {
      $tpl->assign(array(CATEGORYROWS => " "
                      ));
   }
   else
   {
      for ($i = 0 ; $i < sizeof($category) ; $i++)
      {
      	$tpl->assign(
      		array(
      			LINK		=>	$link[$i],
      			CATEGORY	=>	$category[$i]
      		)
      	);

      	$tpl->parse(CATEGORYROWS,".catrow");
      }
      $tpl->parse(CATEGORYROWS, array ("cat"));
   }

   if (sizeof($alcategory) == 0)
   {
      $tpl->assign(array(ALPHAROWS => " "
                      ));
   }
   else
   {
      for ($i = 0 ; $i < sizeof($alcategory) ; $i++)
      {
      	$tpl->assign(
      		array(
      			ALPHALINK		=>	$allink[$i],
      			ALPHACATEGORY	=>	$alcategory[$i]
      		)
      	);

      	$tpl->parse(ALPHAROWS,".alpharow");
      }
      $tpl->parse(ALPHAROWS, array ("alphacat"));
   }

   if (sizeof($relcategory) == 0)
   {
      $tpl->assign(array(RELATEDROWS => " "
                      ));
   }
   else
   {
      for ($i = 0 ; $i < sizeof($relcategory) ; $i++)
      {
      	$tpl->assign(
      		array(
      			RELLINK		=>	$rellink[$i],
      			RELCATEGORY	=>	$relcategory[$i]
      		)
      	);

      	$tpl->parse(RELATEDROWS,".relrow");
      }
      $tpl->parse(RELATEDROWS, array ("rel"));
   }

   if (sizeof($linkname) == 0)
   {
      $tpl->assign(array(LINKROWS => " "
                      ));
   }
   else
   {
      for ($i = 0 ; $i < sizeof($linkname) ; $i++)
      {
      	$tpl->assign(
      		array(
      			LINKREF		=>	$linkref[$i],
      			LINKNAME	=>	$linkname[$i],
         DESCRIPTION => $description[$i]
      		)
      	);

      	$tpl->parse(LINKROWS,".linkrow");
      }

      $tpl->parse(LINKROWS, array ("link"));
   }

   if (sizeof($coollinkname) == 0)
   {
      $tpl->assign(array(COOLLINKROWS => " "
                      ));
   }
   else
   {
      for ($i = 0 ; $i < sizeof($coollinkname) ; $i++)
      {
      	$tpl->assign(
      		array(
      			COOLLINKREF		=>	$coollinkref[$i],
      			COOLLINKNAME	=>	$coollinkname[$i],
         COOLDESCRIPTION => $cooldescription[$i]
      		)
      	);

      	$tpl->parse(COOLLINKROWS,".coollinkrow");
      }

      $tpl->parse(COOLLINKROWS, array ("coollink"));
   }


   $tpl->parse(MAIN, array ("main"));

   $tpl->FastPrint();
}
?>