<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <title>Thingamablog Blog Search</title>
    <link rel="stylesheet" href="styles-site.css" type="text/css" />
    <link rel="icon" type="image/x-icon" href="favicon.ico" />    
  </head>

  <body>
    <div id="banner">
    <h2>Search Thingamablog Blog</h2>
    </div>
    <p></p>
    <table border="3" align="center">
    <tr><td align="center">
    <form action="tamb_search.php" method="post">
    Search For: <input type="text" name="pattern" /><br><br>
    <input type="submit" value="Search Thingamablog Blog"/>
    </form>
    </tr></td>
  </table>
    <div align="center">
  <p>
    <a href="http://www.thekimerers.com/brian/blogs/TAMB/index.php">Back to the Blog</a>
  </p>
    </div>

    <div class="blogbody">
  <p>This search looks only in the entries of the Thingamablog Blog.
If you want to search the internet, you should use Google or Bing or
Yahoo! or whatever search engine you like.</p>
  <p>Enter a search string and click on the button. The search is not
case sensitive.</p>
    </div>

    <p></p>
    <div class="title">
    <p>Results of your search.</p>
    </div>

    <div class="blogbody">

     <?php
  /*
   * This search function depends on a specific directory
   * structure to exist. It only works on the structure for
   * a Thingamablog blog. It searches only in the individual
   * entries of the blog to avoid multiple hits on the same
   * entry in different contexts.
   *
   * The structure of the directories is
   * archives
   *   -  <year>
   *     -  <month>
   *       .  entries.php
   */
    // Get the string to search for from the form that posted this file
  $searchString = $_POST['pattern'];
    // Set the name of the archive directory
    $archiveDirName = 'archives';
  // The path to the year directory
  $pathToYearDir;
  // The path to the month directory
  $pathToMonthDir;

    // open the archive directory
    if ($archiveDirHandle = opendir($archiveDirName)) 
    {
        // Loop through all of the files in the archive directory
        while (false !== ($yearDirName = readdir($archiveDirHandle)))
    {
            // The files in the archives directory
            // should be year directories, but check.
      // Skip over the parent directories
      if($yearDirName == ".") continue;
      if($yearDirName == "..") continue;

      $pathToYearDir = $archiveDirName . "/" . $yearDirName;
      // Check to see if this is a directory.
            if(is_dir($pathToYearDir))
      {
        // We have a directory which is not a parent directory.
        // Assume it is a year directory.
                // Open the year directory
        $yearDirNameHandle = opendir($pathToYearDir);
                // All of the files in the year directory
                // should be month directories, but check
                while(false !== ($monthDirName = readdir($yearDirNameHandle)))
        {
          // Skip over the parent directories
          if($monthDirName == ".") continue;
          if($monthDirName == "..") continue;
          // Check to see if this is a directory.
          $pathToMonthDir = $archiveDirName . "/" . $yearDirName . "/" . $monthDirName;
                    if(is_dir($pathToMonthDir))
                    {
                        // The files in the month directory that we are interested in
                        // are all .php files since those are the 
                        // entries in the blog
                        $monthDirNameHandle = opendir($pathToMonthDir);
                        while(false !== ($entry = readdir($monthDirNameHandle)))
            {
              // Skip over the parent directories
              if($entry == ".") continue;
              if($entry == "..") continue;
              // Look only for the proper file types
                            $suffix = substr($entry, -4);
                            if(($suffix == '.php') || ($suffix == 'html'))
              {
                // Read in the file into one string
                $entryString = file_get_contents($pathToMonthDir . "/" . $entry);
                // Remove the tags. We aren't interested in searching tags
                $tagPattern = '/<.*>/';
                $replaceString = ' ';
                $tagFreeEntryText = preg_replace($tagPattern, $replaceString, $entryString);
                // Look for the pattern, case insensitive in the tag free string
                $found = stripos($tagFreeEntryText, $searchString);
                // If the pattern is found, print out a link to the page.
                if($found != false)
                {
                  $startPoint = $found - 100;
                  if($startPoint < 0)
                  {
                    $startPoint = 0;
                  }
                  $endPoint = $found + 100;
                  if($endPoint >= strlen($tagFreeEntryText))
                  {
                    $endPoint = strlen($tagFreeEntryText) - 1;
                  }
                  $contextString = substr($tagFreeEntryText, $startPoint, $endPoint - $startPoint);
                  echo "<p>\n";
                  echo $contextString . "<br>\n";
                  // Print a link to the path so that the web page 
                  // will have a fully qualified URL to the selected
                  // pages.
                  $urlToEntry = $pathToMonthDir . "/" . $entry;
                  echo "<a href=\"";
                  echo $urlToEntry;
                  echo "\" >\n";
                  echo $urlToEntry;
                  echo "</a>\n";
                  echo "</p>\n";
                }
                            }
                        }
            closedir($monthDirNameHandle);
                    }
 
                }
        closedir($yearDirNameHandle);
            }
        }
    }

    closedir($archiveDirHandle);
?>

    </div>
    </body>

</html>