<?php
require_once 'functions.php'; 
$dbh = DBConnect();

PrintHeader();

$show = request_value('show');
$fid = request_value('fid');
$k = request_value('k');
$v = (int)request_value('v');

$query = 'select * from firsts';
$title = '<h3>A kinda sorta list of authors</h3>';

switch ($show) {
  case 'list':
    $wheres = array();
    switch ($k) {
      case 'fid':
        if ($v) {
          $wheres[] = sprintf(' fid=%d ', $v);
        }
        break;
      case 'aid':
        if ($v) {
          $wheres[] = sprintf(' topic_id in (select topic_id from authorposts where aid = %d) ', $v);
        }
        break;
      case 'gid':
        if ($v) {
          $wheres[] = sprintf(' topic_id in (select topic_id from ghostposts where gid = %d) ', $v);
        }
        break;
    }
    if ($wheres) {
      print "<h3>Posts that may or may not have been written by the same person or persons. Maybe.</h3>\n";
      print "<ul>\n";
      $query = 'select * from topics where (' . implode(') or (', $wheres) . ') order by topic_id';
      if ($result = $dbh->query($query)) {
        while ($row = $result->fetchObject()) {
          foreach ($row as $k => $v) {
            $row->$k = KillTags(stripslashes($v));
          }
          print <<<EOT
<li><a href="display_topic.php?topic_id={$row->topic_id}">{$row->name}</a> by <a href="authorish.php?show=who&fid={$row->fid}">{$row->author}</a> on {$row->created}
EOT;
        }
      }
      print "</ul>\n";
      break;
    }
    // fall through
  case 'who':
    if ($fid) {
      $query .= sprintf(' where fid=%d ', $fid);
      $title = '<h3>Posts that may or may not be by the same person or persons. Could be. Not impossible.</h3>';
    }
    // fall through
  default:
    print <<<EOT
{$title}
<table cellspacing="0" cellpadding="3" border="1">
 <tr>
  <td valign="top" align="left">
   <b>An Authorish Name</b>
  </td>
  <td valign="top" align="left">
   <b>Posting As<br/><span style="font-size:smaller;">(Probably)</span></b>
  </td>
  <td valign="top" align="left">
   <b>Ghost Posts From Those Hosts</b>
  </td>
 </tr>

EOT;
    if ($result = $dbh->query($query)) {
      $asth = do_prep('select * from authors where fid = :fid');
      $gsth = do_prep('select * from ghosts where fid = :fid');
      while ($row = $result->fetchObject()) {
        print <<<EOT
 <tr>
  <td valign="top" align="left">
<a href="authorish.php?show=list&k=fid&v={$row->fid}">{$row->author}</a> ({$row->tick} posts)
  </td>
  <td valign="top" align="left">

EOT;
        do_exec($asth, array(':fid' => $row->fid));
        while ($arow = $asth->fetchObject()) {
          print <<<EOT
<a href="authorish.php?show=list&k=aid&v={$arow->aid}">{$arow->author}</a> ({$arow->tick} posts)
<br/>

EOT;
        }
        print <<<EOT
   &nbsp;
  </td>
  <td valign="top" align="left">

EOT;
        do_exec($gsth, array(':fid' => $row->fid));
        while ($grow = $gsth->fetchObject()) {
          print <<<EOT
<a href="authorish.php?show=list&k=gid&v={$grow->gid}">{$grow->author}</a> ({$grow->tick} posts)
<br/>

EOT;
        }
        print <<<EOT
   &nbsp;
  </td>
 </tr>

EOT;
      }
    }
    print <<<EOT
</table>

EOT;
    break;
}

PrintFooter();
