<?php

  $fd = fopen("/home/dumont/public_html/clickolinko/db", "r");
  $times = array();
  $urls = array();
  $titles = array();
  $first_time = 0;

  # added these initial definitions to avoid "variable undefined" warnings
  # --pedro 20130131
  $q = null;
  $tz = "";
  $showtime = "";
  $limit = "";
  $search = "";
  $url = "";
  $oldtz = "";

  # added these isset wrappers to avoid "variable undefined" (or similar)
  # warnings --pedro 20130131
  if (isset($_REQUEST['tz'])) {
	 $tz = $_REQUEST['tz'];
  }
  if (isset($_REQUEST['showtime'])) {
	  $showtime = $_REQUEST['showtime'];
  }
  if (isset($_REQUEST['limit'])) {
	  $limit = $_REQUEST['limit'];
  }
  if (isset($_REQUEST['search'])) {
	  $search = $_REQUEST['search'];
  }
  if (isset($_REQUEST['q'])) {
	  $q = $_REQUEST['q'];
  }
  if (strlen($q) > 0 && strlen($q) < 10) {
    $q = hexdec($q);
  }
  while (!feof($fd)) {
    $str = fgets($fd, 4096);
    $splits = array_pad(explode(" ", $str), 3, null);
	#print("string was: '$str'\n");
	#print("splits is: '0:$splits[0] 1:$splits[1] 2:$splits[2]'\n");
    $time = explode(".", $splits[0]);
    array_push($times, $time[0]);
    if (!$first_time) {
        $first_time = $time[0];
    }
    array_push($urls, str_replace("\n", "", $splits[1]));
    if ($splits[2] != null) {
       array_push($titles, str_replace("\n", "", $splits[2]));
    } else {
      array_push($titles, str_replace("\n", "", $splits[1]));
    }
    if ($q != null) {
	    if ($splits[0] == $q) {
	       $url = $splits[1];
	       break;
	    }
    }
  }

  if ($url != "") {
    header("Location: $url");
    print("<html><head><title>Redirecting to $url</title></head><body><h1>Redirect</h1>\n");
    print("Redirecting to <a rel='nofollow' href='$url'>$url</a></body></html>");
  } else {
?><html><head><title>URLs seen by clickolinko</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="alternate" type="application/rss+xml" title="RSS feed" href="http://flynn.zork.net/~sneakums/clickolinko.xml">
<style type='text/css'>
body { font-family: sans-serif; }
hr { border: none; height: 1px; background-color: black; }
form { margin: 0pt; }
</style></head><body>
<table width="100%" border="0"><tbody><tr>
<td><b><big><big>clickolinko</big></big></b><br><i><small>the automatic web log</small></i></td>
<td align="right" valign="bottom">
<form method="get" action="">URL search: <?php
    if ($tz != "") {
        printf("<input name='tz' type='hidden' value='%s'>\n", htmlspecialchars($tz));
    }
    if ($showtime != "") {
        printf("<input name='showtime' type='hidden' value='%s'>\n", htmlspecialchars($showtime));
    }
    if ($limit != "") {
        printf("<input name='limit' type='hidden' value='%s'>\n", htmlspecialchars($limit));
    }
    printf("<input name='search' type='text' value='%s'>\n", htmlspecialchars($search));
?><input type='submit' value='Go'></form>
</td></tr></tbody></table>
<hr>
<?php
    if ($tz != "") {
       $oldtz = getenv("TZ");
       putenv("TZ=$tz");
       strftime("%Y%b%d&nbsp;%H:%M:%S");
    }
    if (intval($limit) > 0) {
      $urls_left = $limit;
    } else {
      $urls_left = min(35, count($urls) - 1);
    }
    while ($urls && ($urls_left > 0)) {
      $url = array_pop($urls);
      $title = array_pop($titles);
      $time = array_pop($times);
      if ($url != "" && (substr($url, -4) != ".exe") && ($search == "" || ($search != "" && stristr($url, $search)))) {
	 $purl = $url;
	 $purl = preg_replace("/(http:\/\/[^\/]+\/).*/", '$1', $purl, 1);
         $hurl = htmlspecialchars($url);
         $uurl = str_replace("'", "%27", $url);
         $htitle = htmlspecialchars($title);
         $utitle = str_replace("'", "%27", $title);
         if ($showtime == "yes") {
            $timef = strftime("%Y%b%d&nbsp;%H:%M:%S", $time);
            print("<tt>$timef</tt>&nbsp;");
         }
	 print("<a rel='nofollow' href='$purl'>*</a>&nbsp;<a rel='nofollow' href='$uurl'>$hurl</a>");
         if ($title != $url) {
            print(" <tt>&lt;--</tt> ");
	    print (str_replace ("%20", " ", $htitle));
         }
         print("<br>\n");
         --$urls_left;
	 if ($urls_left % 5) {
	     flush();
         }
      }
    }
    if ($oldtz != "") {
       putenv("TZ=$oldtz");
       strftime("%Y%b%d&nbsp;%H:%M:%S");
    }
	if (0) { # disable for now, no idea how to fix it -sneakums
?>
<hr>
<?php
printf("<small><i>Log begins %s.\n", strftime("%c %Z", $first_time));

# get todays date
$today = getdate();
# get a unix date for the most recent midnight
$last_unix_midnight = mktime(00,00,00,$today[mon],$today[mday], $today[year]);

# open hitcount_db
$hitcount_db = fopen("/home/dumont/public_html/clickolinko/hitcount_db", "r");
$hits = array(); # hit database to be created from file
while (!feof($hitcount_db)) {
   $myline = fgets($hitcount_db, 65);
   if ($myline){
   	$splitline = explode(":", chop($myline));
     	$hits[$splitline[0]] = $splitline[1];
   }

}
fclose($hitcount_db);

# increment total and today's count since midnight
$hits[$last_unix_midnight] = $hits[$last_unix_midnight] +1;
$hits["total"] = $hits["total"] + 1;

# get oldest date for "x hits since $oldest" line
$oldest = $last_unix_midnight; # start with this morning
$arraykeys = array_keys($hits); # array of keys
foreach($arraykeys as $contender) {
	if ($contender != "total" && $contender < $oldest) {
		$oldest = $contender;
	}
}
$oldest = strftime("%c, %Z", $oldest); # ASCIIfy timestamp

# output
print ("<br>" . $hits[$last_unix_midnight] . " hits today; " . $hits['total'] ." total hits since $oldest.</i></small>");

# open hitcount db for writing:
$hitcount_db = fopen("/home/dumont/public_html/clickolinko/hitcount_db", "w");
foreach ($arraykeys as $key) { # recompile database and fwrite out
  if ($key) {
	  $myline = $key . ":" . $hits[$key];
	  fwrite($hitcount_db, "$myline\n");
  }
}
fclose($hitcount_db); # done -- 2005.09.19 PAHP 
	}
?>
<hr>
<table width='100%'><tr>
<td width='110'><a href="http://tastytronic.net/~dumont/"><img src='dumonthead.png' border='0' alt='dumont, master of the io tower'></a></td>
<td>
<small>clickolinko was created by Neale Pickett, who was too modest to credit himself.<br>
Sean Neakums, who butchered clickolinko after installing it on flynn, does not suffer from 
that problem.<br>
Props to <a title="You love us." href='http://slashnet.org/channels/tron/'>the usual gang of idiots</a> for ideas, suggestions, testing and shouting.
</small></td>
<td>
<img src='powered-by-zap.png' align='right'>
</td></tr></table>
</body></html>
<?php
  }
?> 
