#include <infodrom.style>
#include <quote.style>
#include "fortunes.inc"

<?
  # Since cookies are sent to the client within the HTTP header we'll
  # have to create one befor any regular output is generated.  This
  # means that we'll have to change our normal processing into this
  # scheme:
  #
  # 1. Decode the cookie if we got one back
  # 2. Parse the fortune files, store line numbers
  # 3. Create the new cookie to be sent to the client
  # 4. Set the new cookie
  # 5. Normal processing from now on

  $fortunepath = "/org/infodrom.org/fortunes";

  # There are three fortune files at the moment
  #
  $dbfiles = array ();
  $dbfiles[] = "infodrom-linux";
  $dbfiles[] = "infodrom";
  $dbfiles[] = "linuxtag";
  $dbfiles[] = "decklin-debian";
  $dbfiles[] = "channel-debian-de";
  $dbfiles[] = "fortunes-debconf";

  # The cookie will look like
  #
  # infodrom-linux.4423+infodrom.5006+linuxtag.2310+decklin-debian.239
  #
  # 1. Decode it if we got one
  #
  $skip = array ();
  if (strlen ($_COOKIE[fortunes])) {
    $foo = explode (" ", $_COOKIE[fortunes]);
    $i=0; while ($i < count ($foo)) {
      $bar = explode (".", $foo[$i]);
      $skip[$bar[0]] = $bar[1];
      $i++;
    }
  }
  if ($skip['decklin-debian'] == 0 && $skip['infodrom'] != 0) {
    $skip['decklin-debian'] = 1500;	# Initial value, the file
  }
  if ($skip['channel-debian-de'] == 0 && $skip['infodrom'] != 0) {
    $skip['channel-debian-de'] = 422;	# Initial value, the file
  }
  if ($skip["fortunes-debconf"] == 0 && $skip['infodrom'] != 0) {
    $skip["fortunes-debconf"] = 193;	# Initial value, the file
  }

  # 2. Parse the fortune files, store line numbers
  #
  $cookie = array ();
  $newline = array ();
  $dbfileindex = 0; while ($dbfileindex < count ($dbfiles)) {
    $fortune = fopen ($fortunepath . "/" . $dbfiles[$dbfileindex], "r");
    $ignore = $skip[$dbfiles[$dbfileindex]];

    $thiscookie = array ();
    $lineno = 0;
    if ($ignore > 0) {
      $skipfirst = 1;
    } else {
      $skipfirst = 0;
    }
    while ($fortunes !== false && !feof ($fortune)) {
      $line = chop (fgets ($fortune, 255));
      if ($lineno >= $ignore) {
	if (!strcmp ($line, "%")) {
	  if ($skipfirst) {
	    $skipfirst = 0;
	  }
	  if (count ($thiscookie) > 0 && strlen ($thiscookie[0])) {
	    $cookie[] = $thiscookie;
	  }
	  $thiscookie = array ();
	} else {
	  if (!$skipfirst && strlen ($line)) {
	    $thiscookie[] = $line . "\n";
	  }
	}
      }
      $lineno++;
    }
    fclose ($fortune);

    if (count ($thiscookie) > 0 && strlen ($thiscookie[0]) > 0) {
      $cookie[] = $thiscookie;
    }

    $newline[$dbfiles[$dbfileindex]] = $lineno-2;
    $dbfileindex++;
  }

  # 3. Create the new cookie to be sent to the client
  #
  $newval = "";
  $dbfileindex = 0; while ($dbfileindex < count ($dbfiles)) {
    if (strlen ($newval)) {
      $newval .= " ";
    }
    $newval .= $dbfiles[$dbfileindex] . "." . $newline[$dbfiles[$dbfileindex]];
    $dbfileindex++;
  }

  # 4. Set the new cookie
  #
  setcookie ("fortunes", $newval,
    time()+(365*24*60*60),	# Valid one year
    "/Infodrom/fortunes",	# Limit to this path...
    $HTTP_HOST,			# ...on this server
    0);				# Use it for http
?>

<page title="Infodrom Fortune Cookies"
      alternates="fortunes.rdf|All~fortunes"
      keywords="fortunes Aphorismen">

<format_cookie>
<?

  # 5. Normal processing from now on
  # 
  # If a cookie was found, display all new fortunes
  # Else display 15 selected randomly
  # 

  if (strlen ($_COOKIE[fortunes])) {
    if (count ($cookie) > 0) {
      echo ("<p>All new fortunes after your last visit are shown below.</p>");
      echo ("<div class=\"fortunes\">\n");
      $i = 0; while ($i < count ($cookie)) {
        $thiscookie = $cookie[$i];
	if ($i > 0)
	  echo ("<hr class=\"simple\">\n");
        echo format_cookie ($thiscookie, "0");
        $i++;
      }
      echo ("</div>\n");
    } else {
      echo ("<p>There haven't been any new fortunes since your last visit.</p>");
      echo ("<p>See you again soon.</p>");
    }
  } else {
    echo ("<p>No cookie found, displaying 15 randomly selected fortunes.</p>");
    echo ("<p>If you accept the cookie presented on this page next time and come back later,");
    echo ("it will display all new fortunes after your last visit.</p>\n");
    srand (time ());
    $i = 0; while ($i < 15) {
      $thiscookie = $cookie[rand (0, count ($cookie))];
      echo ("<hr class=\"simple\">\n");
      echo format_cookie ($thiscookie, "0");
      $i++;
    }
  }
?>

</page>

# Local variables:
# mode: indented-text
# mode: auto-fill
# end:
