<?php

// STD Page Generation System[tm] v0.3
// Copyright (C) 2004-2005, Cockos Incorporated
// License: public domain

// to use this code to make new pages, do:
//   include("std.php");
//   standardheaders("MyAppName"); // the parameter can be "" for the main page
//   displayfile("index.txt");
//   standardfooters();
//
//   index.txt is the content, and if the line begins with ^ it is centered.
//   by default the minimum page size is 80x25. you can tweak below, too.

//   Cockos STD homepage, http://cockos.com/std/

//   v0.4: supports sane mode (plain HTML generation)
//   v0.3: support optional border and random-bg-text (nullsoft BEEFlike)
//   v0.2: autowrap support for long lines. specify your width/min height here.
//         (inspired by John Caruso's version)
//   v0.1: first version

$company_name "Cockos Incorporated";
if (!isset(
$width) || $width 20 || $width 200$width=80;
if (!isset(
$minheight) || $minheight || $minheight 300$minheight=25;
if (!isset(
$sanemode)) $sanemode=0;
if (!isset(
$border)) $border=1;
if (!isset(
$randombg)) $randombg=0;
if (!isset(
$bgcol_r)) $bgcol_r=0.3// color, at brightest, of random bg chars
if (!isset($bgcol_g)) $bgcol_g=0.2;
if (!isset(
$bgcol_b)) $bgcol_b=0.3;







function 
padoutstring($x$indent$outlen)
{
  global 
$randombg,$bgcol_r,$bgcol_g,$bgcol_b;
  global 
$border$sanemode;
  
$buf="";
  
$mylen=strlen(strip_tags($x));
  if (!
$sanemode && $indent 0$indent=(($outlen-$mylen)/2)|0;

  if (
$sanemode)
  {
    if (
$indent 0$buf "<center>";
    else for (
$pos 1$pos <= $indent$pos ++) $buf.=" ";

    
$buf .= $x;

    if (
$indent 0$buf .= "</centeR>";
  }
  else
  {
    for (
$pos 0$pos <= $indent$pos ++) $buf.=" ";
    
$buf .= $x;
    if (!
$sanemode) for ($pos $indent $mylen$pos <= $outlen$pos++) $buf.=" ";
  }

  if (
$sanemode)
  {
    
$nb=ltrim($buf);
    
$nc=strlen($buf)-strlen($nb);
    
$buf="";
    for (
$x 0$x $nc$x ++) $buf .="&nbsp;&nbsp;";
    
$buf .= $nb;
  }
  else if (
$randombg// convert left and right whitespace to random chars, darkened
  
{
    
$nb=ltrim($buf);
    
$nc=strlen($buf)-strlen($nb);
    
$nb=rtrim($nb);
    
$nc2=strlen($buf)-$nc-strlen($nb);
    
$buf="";
    for (
$x=0;$x<$nc+$nc2$x++) 
    {
      if (
$x == $nc$buf .= $nb;
      if (!(
$x 1) || $x == $nc)
      {
        
$t=rand()%255;
        
$colstr=sprintf("%02X%02X%02X",$t*$bgcol_r,$t*$bgcol_g,$t*$bgcol_b);
      }
      
$buf.=sprintf("<font color=\"#$colstr\">%1X</font>",$t%16);
      
$t/=16;
    }
  }

  if (!
$sanemode && $border$buf =  "|" $buf "|";
  if (
$sanemode$buf .= "<BR>";
  return 
$buf "\n";
}

function 
displayfile($filename)
{
  global 
$width$border$sanemode;
  global 
$minheight;
  
  
$xdim=$width;
  
$ydim=0;
  if (!
$sanemode && $border)
  {
  echo 
"/";
  for (
$x 1$x $xdim+3-1$x++) echo "-";
  echo 
"\\\n";
  }

  
$fp fopen($filename,"r");
  if (
$fp)
  {
    while ((
$x=fgets($fp,1024)))
    {
      
$x=rtrim($x);
      if (
substr($x,0,1) == "^") { $indents[$ydim]=-1$x=substr($x,1); }
      
$l=strlen(strip_tags($x));
      
$lines[$ydim++]=$x;
    }
    
fclose($fp);
  }

  
$ypos=0;
  
$yspos=0;
  while (
$yspos $ydim)
  {
    if (
$indents[$yspos] == ''$ind=0;
    else 
$ind=$indents[$yspos];

    
$sl=$lines[$yspos++];

    do 
    {
      if (!
$sanemode && strlen(strip_tags($sl)) >= $xdim)
      {
        
$newl=strlen($sl);
        do
        {
          
$newsub=substr($sl,0,--$newl);
        }
        while (
strlen(strip_tags($newsub)) >= $xdim);
 
        do
        {
          
$tmp=substr($sl,--$newl,1);
        }
        while (
$newl && $tmp != "\t" && $tmp != " ");
          
        echo 
padoutstring(substr($sl,0,$newl),$ind,$xdim);
        
        do
        {
          
$tmp=substr($sl,++$newl,1);
        }
        while (
$tmp == "\t" || $tmp == " ");

        
$sl=substr($sl,$newl);
      }
      else
      {
        echo 
padoutstring($sl,$ind,$xdim);
        
$sl="";
      }
      
$ypos++;
    } while (
$sl != "");
  }
  if (!
$sanemode) while ($ypos $minheight
  {
    echo 
padoutstring("",0,$xdim);
    
$ypos++;
  }

  if (!
$sanemode && $border)
  {
  echo 
"\\";
  for (
$x 1$x $xdim+3-1$x++) echo "-";
  echo 
"/\n";
  }
}

function 
standardheaders($title)
{
  global 
$company_name$sanemode;
  echo 
"<HTML><HEAD><TITLE>";
  if (
$title != "") echo "$title | ";
  echo 
"$company_name</TITLE>";
  echo 
"</HEAD><BODY BGCOLOR=\"#000000\" TEXT=\"#8f8f8f\" LINK=\"#8f8f8f\" VLINK=\"#8f8f8f\" ALINK=\"#8f8f8f\">";
  if (!
$sanemode) echo "<TABLE width=100% height=100% valign=center><TR><TD align=center><CENTER><PRE>";
  
}

function 
standardfooters()
{
  global 
$sanemode;
  if (!
$sanemode) echo "</pre></TD></TR></TABLE>";
  echo 
"</BODY></HTML>";
}

?>