<?php

  //CHANGE THIS
  mysql_connect("localhost", "newquotes", "blah");
  mysql_select_db("newquotes");

  if (!isset($channel) || empty($channel)) {
    //CHANGE THIS (default channel to show)
	  $channel = "exeter";
  }

  if (!isset($remote) || empty($remote)) {
    $remote = 0;
  }

  if (!$remote) {
?>
<html>
<head>
  <!-- CHANGE THIS -->
  <title>#<?=$channel?> quotes from NoTopic</title>
  <link rel="stylesheet" type="text/css" href="styles.css">
</head>

<body>
<h1>#<?=$channel?> quotes</h1>
<br>
<form method="get" action="<?=$PHP_SELF;?>">
Filter for: <input type="text" name="filter" maxlength="32" size="32"> <input type="submit" value="go"><input type="hidden" value="search" name="search"><input type="hidden" name="channel" value="<?=$channel?>">
</form>

Sorted with most recent first.
<?php

/*
mysql> show columns from exeter;
+-----------+----------+------+-----+---------+-------+---------------------------------+
| Field     | Type     | Null | Key | Default | Extra | Privileges                      |
+-----------+----------+------+-----+---------+-------+---------------------------------+
| id        | int(11)  | YES  |     | NULL    |       | select,insert,update,references |
| mask      | text     | YES  |     | NULL    |       | select,insert,update,references |
| quote     | text     | YES  |     | NULL    |       | select,insert,update,references |
| timestamp | datetime | YES  |     | NULL    |       | select,insert,update,references |
+-----------+----------+------+-----+---------+-------+---------------------------------+
4 rows in set (0.03 sec)
*/
  }

  $chan = $channel;
  $channel = "#" . $channel;

  if (isset($filter)) {
    $sql = "SELECT * FROM quotes WHERE quote LIKE '%$filter%' AND channel='$channel' ORDER BY timestamp DESC";
  }
  else {
    $sql = "SELECT * FROM quotes WHERE channel='$channel' ORDER BY timestamp DESC";
  }

  if ($remote) {
    $sql .= " LIMIT 20";
  }

  $results = mysql_query($sql);

	if ($results && mysql_num_rows($results)) {
		$count = mysql_num_rows($results);
    if (!$remote) {
		  $quoted = array();
		  $quoter = array();
      echo "<table>";
      echo "<tr><th>#</th><th>Added by</th><th>When</th><th align=\"left\">Quote</th></tr>";
      $class = 0;
    }
    if (!empty($page)) {
      mysql_data_seek($results, $page * 100);
    }
    else {
      $page = 0;
    }
    $counter = 0;
    while (($quote = mysql_fetch_object($results)) && ($counter < 100)) {
      if (!$remote) {
        $counter++;
        echo "<tr class=\"";
        echo ($class == 0) ? "odd" : "even";
        $class = !$class;
        echo "\"><td valign=\"top\"><a name=\"$quote->id\"><a href=\"#$quote->id\"><small>" . $quote->id . "</small></a></td><td class=\"nick\" valign=\"top\" nowrap><small>";
        echo "<span title=\"" . $quote->host . "\">";
        echo $quote->nick;
        echo "</span></small>";
        echo "</td>";
        echo "<td class=\"time\" valign=\"top\" nowrap><small>";
        echo date("H:i j M y", $quote->timestamp);
        echo "</small></td>";

        if (empty($quoter[$quote->nick])) {
          $quoter[$quote->nick] = 1;
        }
        else {
          $quoter[$quote->nick]++;
        }

        $quote_text = htmlentities($quote->quote);
        $quotes = @preg_split("/ \| /", $quote_text);
        $newquote = "";
        foreach ($quotes as $q) {
          $q = trim($q);
          //no timestamps
          $q = preg_replace('/^\[?[0-9:.]+\]?/', '', $q);

          //$q = preg_replace('/^((&lt;|\\[|\\()*.*?)( )(.*?(&gt;|\\]|\\))+:?)/', "\\1&nbsp;\\4", $q);
          
          //hilight nicks
          if (!preg_match("/^\* /", $q)) {
            if (preg_match('/^((&lt;|\\[|\\()*[@%+]?([^\]>\\\)]+?)[@%+]?(&gt;|\\]|\\))+:?)/', $q, $matches)) {
              if (empty($quoted[$matches[3]])) {
                $quoted[$matches[3]] = 1;
              }
              else {
                $quoted[$matches[3]] = $quoted[$matches[3]] + 1;
              }
            }
            $q = preg_replace('/^((&lt;|\\[|\\()*[^\]>\\\)]+?(&gt;|\\]|\\))+:?)/', "<b>\\1</b>", $q);
          }
          else {
            if (preg_match('/^\* [@%+]?(\S+)[@%+]?/', $q, $matches)) {
              if (empty($quoted[$matches[1]])) {
                $quoted[$matches[1]] = 1;
              }
              else {
                $quoted[$matches[1]] = $quoted[$matches[1]] + 1;
              }
            }
            $q = preg_replace('/^\* (\S+)/', "* <b>\\1</b>", $q);
          }
          //$newquote .= preg_replace('/ /', "&nbsp;", $q);
          $newquote .= $q;
          $newquote .= "<br>";
        }      
        $quote_text = preg_replace('/ \| /', "<br>", $quote_text);
        echo "<td class=\"quote\">" . $newquote . "</td>";
        echo "</tr>";
      }
      else {
        //remote
        echo $quote->quote . "\n";
      }
    }
		
    if (!$remote) {
      echo "</table>";
      echo "$count results<br>";
      if ($page > 0) {
        echo "<a href=\"index.php?channel=$chan&filter=$filter&page=" . ($page - 1) . "\">&lt;&lt; Prev page</a>&nbsp;&nbsp";
      }
      if (($counter + ($page * 100)) < $count) {
        echo "<a href=\"index.php";
        echo "?channel=$chan";
        echo "&page=";
        echo ($page + 1);
        echo "&filter=$filter";
        echo "\">Next page &gt;&gt;</a>";
      }

      echo "<br><br>";
      echo "<table width=\"70%\" cellpadding=\"5\" align=\"center\">";
      echo "<tr><td valign=\"top\">";
      arsort($quoted);
      $i = 0;
      echo "<h3>Top 5 Quoted</h3>";
      echo "<small>on this page of results</small><br><br>";
      foreach($quoted as $q => $count) {
        echo "$q was quoted $count times<br>";
        if (++$i > 5) {
          break;
        }
      }
      echo "</td><td valign=\"top\">";
      echo "<h3>Top 5 Quoters</h3>";
      echo "<small>on this page of results</small><br><br>";
      arsort($quoter);
      $i = 0;
      foreach($quoter as $q => $count) {
        echo "$q added $count quotes<br>";
        if (++$i > 5) {
          break;
        }
      }
      echo "</td></tr></table>";
    }
	}
	else echo "oops: no results<br>" . mysql_error();
  
  if ($remote) {
    exit;
  }

  $result = mysql_query("SELECT DISTINCT channel FROM quotes ORDER BY channel");

  echo "<br><br><small>Other channels<br> :: ";
  while ($chan = mysql_fetch_object($result)) {
	  $chanlink = preg_replace("/#(.+)/", "\\1", $chan->channel);
	  echo "<a href=\"$PHP_SELF?channel=$chanlink\">$chan->channel</a> :: ";
  }

?>
</body>
</html>