<?php
/**
 * Squish :: A blind redirection Web service based on the TightURL library.
 * Squish is the reference implementation front-end to a TightURL service.
 * You can create your own TightURL service PHP front-end by using Squish as
 * a model, or customize the Squish templates to suit your needs.  Squish was
 * designed however, to allow administrators to customize many aspects of the
 * browser output by setting configuration variables.  Many users of Squish
 * have apparently not felt the need to modify the templates or create their
 * own.
 * Squish powers the public installation of TightURL at http://tighturl.com
 *
 * Copyright (c) 2004-2008, Ron Guerin <ron@vnetworx.net>
 *
 * This file implements the Web front-end of a blind redirection service
 * named Squish.  Squish is part of the software package named TightURL.
 * TightURL is Free Software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Squish/TightURL are distributed in the hope that they will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * If you are not able to view the LICENSE, which should
 * always be possible within a valid and working TightURL release,
 * please write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * to get a copy of the GNU General Public License or to report a
 * possible license violation.
 *
 * @package TightURL
 * @author Ron Guerin <ron@vnetworx.net>
 * @license http://www.fsf.org/licenses/gpl.html GNU Public License
 * @copyright Copyright &copy; 2004-2008 Ron Guerin
 * @filesource
 * @link http://tighturl.com TightURL
 * @version 0.2.0
 * 
 */
define("VERSION""0.2.0dev");
define("REQUIRED_PHP_VERSION""4.3.0");

// System defaults,  DO NOT EDIT THIS FILE
// Edit squish.config.inc.php instead!

global $copyright$conn$db$svcname;

$dbhost "localhost";
$dbuser "dbuser";
$dbpass "dbpass";

// TightURL variables
$dbname "tighturl";
$dbtable "urls";
$rewriting=0//0=off (Basic mode), 1=404-Method, 2=mod_rerwrite
//$FOFMethod=FALSE; //0=Full URL path or mod_rewrite, 1=404-Method compressed URLs
$redirparam "i";
$viewparam "v";
$cookieparam "p";

// URIBL variables
$uribl = array("multi.surbl.org""black.uribl.com");
$uriblurl = array("www.surbl.org""www.uribl.com");

// Text strings and style variables
$svcname "SquishURL";
$verbtext "Squish";
$pasttext "Squished";
$tagline "Squish long URLs to make short ones";
$headcolor "#006600";
$tablecolor "#00CC99";
$copystart date("Y");
$copyrightholder "SquishURL Enterprises";
$nover false;
$squisher true;
$previews true;
$complaints true;
$squish true;
$preview false;
$complain false;
$uribls "";

$selfsite  $_SERVER['HTTP_HOST'];
$aliasurl "alias.tighturl.com";
$aliasrewriting=1//0=off (Basic mode), 1=404-Method, 2=mod_rerwrite

$BB2 true;
$BBstats true;
$BBstrict false;
$BBverbose true;
$BBLogging true;
$bb2_settings_defaults "";
$resolve true;

// Reserved URLs
$ReservedURL = array("x""rest""xmlrpc""soap""xml""atom""rss""blog",
                     
"faq""help""about""api""code""source""docs",
                     
"cvs""arch""url""admin""setup""svn""project",
                     
"abuse""cgi-sys""disabled""preview""previewon""previewoff",
                     
"view""viewon""viewoff""copying""hg""git""decimal""license");

// Merge the two reserved URI arrays together
//$ReservedURIs = array_merge($SysReservedURI, $ReservedURI);

// You REALLY don't want to edit below here unless you know what you're doing.

// *************************************************************************

(require_once("gettext.php")) or die_HTML($svcname"Error: Cannot locate" " gettext.php.");

if (
version_compare(phpversion(), REQUIRED_PHP_VERSION)<0) {
  
die_HTML($svcname"Error: Squish " VERSION " needs PHP >= ".REQUIRED_PHP_VERSION." (you are using ".phpversion().")");
}

if (! 
extension_loaded('mysql')) {
  
die_HTML($svcname"Error: Squish " VERSION " needs the PHP MySQL module and it is not loaded.");
  exit;
}

if (! 
extension_loaded('pcre')) {
  
die_HTML($svcname"Error: Squish " VERSION " needs the PHP PCRE module and it is not loaded.");
  exit;
}

/**
 * Add back get_magic_quotes_gpc() if this is PHP6 or later
 * This is to maintain compatibility with PHP5 where we must check to see if
 * "magic quotes" are on, and undo their effects if so.
 */
if (! function_exists('get_magic_quotes_gpc')) {
  function 
get_magic_quotes_gpc()
  { return 
0; }
}

$validschemes "(http|https|ftp|sftp)";
$forbid "\.(cmd|bat|exe|scr|pif|vbs|js|pif|msi|cdr)";

// Figure out correct self
if (strncmp($_SERVER['PHP_SELF'], $_SERVER['REQUEST_URI'], strlen($_SERVER['PHP_SELF'])) != 0) {
  if (
preg_match("|(.*)/.*$|",$_SERVER['PHP_SELF'],$matches)) $self $matches[1];
  if (! 
preg_match("|.*/$|"$self)) $self .= "/";
}
else {
  
$self $_SERVER['PHP_SELF'];  // We need the script name
  
if (! preg_match("|.*/$|"$self)) $self .= "/";
}

// ****** !All overridable configuration variables must go above this line! ******

// Status: 0=Ok, 1=White, 2=Grey, 3=Black, 4=Policy, 5=Complaints, 6=Killbot
// BUG: check and installer for 'BadBehavior

(require_once("tighturl.lib.inc.php")) or die_HTML($svcname"Error: Cannot locate tighturl.lib.inc.php.");
if (
file_exists("squish.config.inc.php")) require_once("squish.config.inc.php");

// Figure out our copyright string
$thisyear date("Y");
$copyright $copystart;
if (
$copystart != $thisyear$copyright .= "-" $thisyear;
for (
$i=0$i<count($uribl); $i++) {
  if (
$i 0$uribls .= ", ";
  
$uribls .= "<a href='http://" $uriblurl[$i] . "'>" $uribl[$i] . "</a>";
}

$remote $_SERVER['REMOTE_ADDR'];

if (! 
$rewriting$parm "?".$redirparam."=";  // We need the parameter tag

// Connect to MySQL, open database.
$conn = @mysql_connect($dbhost$dbuser$dbpass) or die_HTML($svcname"Error: Cannot connect to database.");
$db mysql_select_db($dbname$conn) or die_HTML($svcname"Error: Cannot select database. "mysql_error());

// When in doubt, turn Bad Behavior on, set it to FALSE in the config to turn it off.
if (! isset($BB2)) $BB2 true;

// If user has not turned off Bad Behavior in the config, use BB2 (highly recommended)
if ($BB2 && file_exists("bad-behavior/bad-behavior-tighturl.php")) require_once("bad-behavior/bad-behavior-tighturl.php");
   else 
$BB2 FALSE;

// Figure out what kind of request this is and service it.
  
// Check for preview cookie
if (isset($_COOKIE["preview"]) && $_COOKIE["preview"]) {
  
$previews true;
}
else {
  
$previews false;
}

// Previews:
//   $SVCNAME: $COOKIESTATESVCNAME  ALIASNAME: $COOKIESTATEALIASNAME

//if (! setcookie("preview", true, time()+(60*60*24*365*20)) ) echo "bleh!\n";

// This is klugey.  Clean up later.
//request[url]: http://www.nano-editor.org/dist/v2.1/nano.html
//request[save]: y
//server[query_string]: save=y&url=http%3A%2F%2Fwww.nano-editor.org%2Fdist%2Fv2.1%2Fnano.html&tighturlaction=Squish+URL&previewurl=&complainturl=
//server[request_uri]: /testing/?save=y&url=http%3A%2F%2Fwww.nano-editor.org%2Fdist%2Fv2.1%2Fnano.html&tighturlaction=Squish+URL&previewurl=&complainturl=

$debug=TRUE;
$debug=FALSE;
if (
$debug) {
  if (isset(
$_REQUEST['url'])) echo "request[url]: ".$_REQUEST['url']."<br />\n";
  if (isset(
$_REQUEST['save'])) echo "request[save]: ".$_REQUEST['save']."<br />\n";
  if (isset(
$_SERVER['QUERY_STRING'])) echo "server[query_string]: ".$_SERVER['QUERY_STRING']."<br />\n";
  if (isset(
$_SERVER['REQUEST_URI'])) echo "server[request_uri]: ".$_SERVER['REQUEST_URI']."<br />\n";
  if (isset(
$_REQUEST['i'])) echo "request[i]: ".$_REQUEST['i']."<br />\n";
  if (isset(
$_REQUEST['v'])) echo "request[v]: ".$_REQUEST['v']."<br />\n";
  if (isset(
$_REQUEST['c'])) echo "request[c]: ".$_REQUEST['c']."<br />\n";
  if (isset(
$_SERVER['QUERY_STRING'])) echo "query_string: ".$_SERVER['QUERY_STRING']."<br />\n"//query_string: i=da42
  
if (isset($_SERVER['SCRIPT_NAME'])) echo "script_name: ".$_SERVER['SCRIPT_NAME']."<br />\n"//script_name: /testing/index.php
  
echo "selfsite: $selfsite self: $self</br>\n"//  selfsite: tighturl.com self: /testing/
}

if ((isset(
$_REQUEST['save']) && $_REQUEST['save'] == 'y')
  && (isset(
$_REQUEST['url']) && ! empty($_REQUEST['url']) && trim($_REQUEST['url']) != ""
  
&& (preg_match("|^.*url=(.*)&.*|U"$_SERVER['QUERY_STRING'], $matches)))) {
  
$encodedurl trim($matches[1]);
  if (
preg_match("|^(.*)&tighturlaction.*$|"$encodedurl$matches)) $encodedurl $matches[1];    
  
$decodedurl trim(urldecode($encodedurl));  
  
save_URL($decodedurl);
}
elseif (isset(
$_REQUEST[$redirparam]) && ! empty($_REQUEST[$redirparam])) {
  if (
$debug) echo "redirparam hi! ".$_REQUEST[$redirparam]."<br />\n";
  
lookup_ID($_REQUEST[$redirparam]);
}
//elseif ( (isset($_COOKIE["preview"]) && $_COOKIE["preview"]) || (isset($_REQUEST[$viewparam]) && !empty($_REQUEST[$viewparam])) ) {
elseif (isset($_REQUEST[$viewparam]) && ! empty($_REQUEST[$viewparam])) {
  if (
$debug) echo "view hi!<br />\n";
  
lookup_ID("view/" $_REQUEST[$viewparam]);
}
elseif (isset(
$_REQUEST[$cookieparam]) && ! empty($_REQUEST[$cookieparam])) {
  if (
$debug) echo "cookie hi!<br />\n";
  if (
strtolower($_REQUEST[$cookieparam]) != "on") {
    
$previews false;
    
$_REQUEST[$cookieparam] = "off";
    
// Delete the preview cookie
    
setcookie ("preview"""time() - 3600);
  }
  else {
    
$_REQUEST[$cookieparam] = "on";
    
$previews true;
    
// Set the preview cookie to be valid for 20 years
    
setcookie("preview"truetime()+(60*60*24*365*20));
  }

  
// Sanitize $_REQUEST[$cookieparam]
  
if ($debug) echo "cookie: $_REQUEST[$cookieparam]<br />\n";  

  
lookup_ID("previews/" $_REQUEST[$cookieparam]);
}
elseif (
$_SERVER['REQUEST_URI'] == $self) {
  if (
$debug) echo "main hi!<br />\n";
  
display_HTML("""main");
}
//elseif ($rewriting && preg_match("/^\/+(.+)\/*(.*)\/*$/", $_SERVER['REQUEST_URI'], $matches)) {
elseif ($rewriting && preg_match("/^\/+(.+)\/(.*)\/*$/"$_SERVER['REQUEST_URI'], $matches)) {
  if (
$debug) echo "lookup hi! 1:".$matches[1]." 2:".$matches[2]."<br />\n";
  
lookup_ID($matches[2]);
}
//elseif ($rewriting && $_SERVER['REQUEST_URI'] != "/") {
else {
  if (
$debug) echo "invalid hi!<br />\n";
  
display_HTML("""""Error: Couldn't find a valid " $svcname " URI.");
}
exit;

// *************************************************************************

/**
 * Save the given URL in the database if unique or find the existing ID for given URL.
 *
 * The ID returned will be a sexatrigesimal (Base-36) number.
 *
 * Saves the URL in the database, converts the decimal ID value returned by the database to
 * a sexatrigesimal value, and displays the generated TightURL.
 */
function save_URL($decodedurl) {
/*
 *
 */

  
global $dbtable$svcname$rewriting$pasttext$self$selfsite$remote$resolve$sitenames;

//  echo "selfsite: $selfsite self: $self\n";
//  selfsite: tighturl.com self: /testing/
  
  
$tighturl TightURL_SaveURL($decodedurl$sitenames$self, &$errcode, &$shortestname);
  switch (
$errcode) {
  case 
0// Success

//    $sexatrigesimal = base_convert($decimal, 10, 36);
//    $address = "http://" . $selfsite . $self;
//    No parameter tags with rewrite!
//    if (! $rewriting ) $address .= "?i=";  // We need the parameter tag
//    $address .= $sexatrigesimal; // Append the Base-36 ID to the URL
    
$address "http://" $selectedname $self;
    
display_HTML("""save"""$decodedurl$selectedname);
    return;
    break;
  case 
2// Invalid URL
    
display_HTML("""""Error: That URL (".htmlspecialchars($decodedurl).") is not valid."$decodedurl""$decodedurl);
    return;
    break;
  case 
3// Can't shorten
    
display_HTML("""""Fail: That URL cannot be shortened by $svcname. Sorry!"$decodedurl""$decodedurl);
    return;
    break;
  case 
4// Points to ourself
    // Build $ourself here from $namelist and $self
    
display_HTML("""""Error: A ".$svcname." URL cannot point to another URL within ".$ourself." ."$decodedurl""$decodedurl);
    return;
    break;
  case 
5// Redirector URL
    
display_HTML("""""Error: ".$svcname." is not a URL obfuscation service, and does not accept redirection links."$decodedurl""$decodedurl);
    return;
    break;
  case 
6// Forbidden Type
    
display_HTML("""""Error: Executable URIs are not accepted here due to phishing/malware abuse."$decodedurl""$decodedurl);
    return;
    break;
  case 
7// Policy Violation
    
display_HTML("""""Error: Submitted URL violates ".$svcname." policy/terms of service, it will not be accepted."$decodedurl""$decodedurl);
    return;
    break;
  case 
8// Blacklisted
    
display_HTML("HTTP/1.0 403 Forbidden""""Error: Submitted URL (" $decodedurl ") is listed in " $lists ". You may not create a " $svcname " link for it.");
    return;
    break;
  case 
9// Does not exist/doesn't resolve
    
display_HTML("""""Error: Submitted URL does not exist on the public Internet."$decodedurl""$decodedurl);
    return;
    break;
  default:
    
display_HTML("""""Error: $svcname database or system error.");
    return;
    break;
  }
}

/**
 * Looks up given ID in the database and redirects, displays template, or
 * displays error page. Expects the ID to be a sexatrigesimal (Base-36) number,
 * which is the format used by TightURLs.
 *
 * We convert the ID to decimal before looking it up in the database, as the
 * ID field is a MySQL autoincrement decimal value.
 */
function lookup_ID($sexatrigesimal) {
  global 
$conn$dbtable$svcname$rewriting;
  global 
$debug;

  
$status false$urldata false;
  if (
$debug) echo "sexatrigesimal: $sexatrigesimal<br />\n";

  if (
strpos($sexatrigesimal"/") !== false) {
    list(
$template$urldata) = explode("/"$sexatrigesimal);
    
$status = -1;
  }
  else {
    
$url TightURL_LookupID($conn$dbtable$sexatrigesimal$statustrue);
  }
  if (
$debug) echo "explode template: $template urldata: $urldata<br />\n";

  switch (
$status) {
  case 
0:
  case 
1:
    
// Check for preview cookie
    
if (isset($_COOKIE["preview"]) && $_COOKIE["preview"]) {
      
// do something preview true
      // TODO: pass along referrer here 
      
lookup_ID("view/" $sexatrigesimal);
    }
    else {
      
// do something preview false
      
if ($debug) {
        echo 
"HTTP/1.0 301 Moved Permanently<br />\n";
        echo 
"Location: $url<br />\n";
      }
      else {
      
header("HTTP/1.0 301 Moved Permanently");
      
header("Location: $url");
      }
    }
    break;
  case 
5:
    
display_HTML("HTTP/1.0 403 Forbidden""complaints");
    return;
    break;
  case 
4:
    
display_HTML("HTTP/1.0 403 Forbidden""policy");
    return;
    break;
  case 
3:
    
display_HTML("HTTP/1.0 403 Forbidden""blacklist");
    return;
    break;
  case 
2:
    
display_HTML("""greylist"""$url);
    return;
    break;
  case -
1:
    
// This is a template or an API
    
$template strtolower($sexatrigesimal);
    if (
preg_match("|(.*?)/(.*)|"$sexatrigesimal$matches)) {
      
$template $matches[1];
      
$urldata $matches[2];
      
//$urldata = substr($sexatrigesimal, strrpos($sexatrigesimal, "/") +1);
      
if ($debug) echo "template: $template rewriting: $rewriting urldata: $urldata matches2: $matches[2]<br />\n";
//      switch ($rewriting) { //0=off (Basic mode), 1=404-Method, 2=mod_rerwrite
//      case 0:
//      case 2:
//        // FIX: test this with mod_rewrite
//        $urldata = $_SERVER['t']; // this seems to make no sense with case 0
//        break;
//      case 1:
//        $urldata = $matches[2];
//        break;
//      }
    
}
    if (
$debug) echo "template: $template urldata: $urldata<br />\n";
    switch (
$template) {
    case 
"rest":
      
api_REST();
      return;
      break 
2;
    case 
"xmlrpc":
      
api_XMLRPC();
      return;
      break 
2;
    case 
"soap":
      
api_SOAP();
      return;
      break 
2;
    case 
"previews":
      
display_HTML("""previews"""""$urldata);
      return;
      break 
2;
//    case "viewoff":
//    case "previewoff":
//      display_HTML("", "previews", "", "", false);
//      return;
//      break 2;
    
default:
      if (
$debug) echo "*default*<br />\n";
      
display_HTML(""$template""""$urldata);
      return;
      break 
2;
     }
  default:
    
display_HTML("HTTP/1.0 404 Not Found""""Error: That " $svcname " ID is not in the database.");
    return;
    break;
  }
}

function 
api_REST() {
  
die_HTML($svcname"Error: REST API not implemented yet.""HTTP/1.0 501 Not Implemented");
}

function 
api_XMLRPC() {
  
die_HTML($svcname"Error: XML-RPC API not implemented yet.""HTTP/1.0 501 Not Implemented");
}

function 
api_SOAP() {
  
die_HTML($svcname"Error: SOAP API not implemented yet.""HTTP/1.0 501 Not Implemented");
}

/**
 * Display HTML page using template and template variables.
 *
 * Reads in the main system template file (squish.tmpl) into $html .
 *
 * $code
 *   HTTP 1.0 status code and message.
 *
 * $template
 *   Checks for the existence of a subtemplate named squish.$template.tmpl
 *   and replaces template variable $HTML in the main template squish.tmpl
 *   with the contents of squish.$template.tmpl if any.
 *
 *   Then any remaining $HTML from the only or inner template is replaced by $content,
 *   along with $url, $tighturl, and $input.  A variety of other replacements are
 *   made using various global variables.
 *
 * $content
 *   HTML content to be replace template variable $HTML
 *
 * $url
 *   URL submitted to TightURL
 * 
 * $tighturl
 *   TightURL generated for $url
 * 
 * $input
 *   When submitted URL does not validate it is passed back as $input
 * 
 * Template variables are words in all capital letters that start with a
 * $ symbol, such as $TEMPLATEVARIABLE.  Squish now supports at least
 * 20 template variables.  At runtime, these template variables are replaced
 * by program variables.
 * - $HTML : HTML passed into the function as $input by the program or an inner template 
 * - $PARM : Parameter tag when not using 404-Method
 * - $SQUISHVER : Running version of Squish
 * - $TIGHTURLVER : Running version of TightURL library
 * - $URL : URL submitted to TightURL
 * - $URLLEN : Length of the submitted URL
 * - $TIGHTURL : TightURL generated for the submitted URL
 * - $TIGHTURLLEN : Length of generated TightURL
 * - $DIFF : Difference in length between submitted and TightURLs
 * - $PREVIEW : Resolved URL
 * - $PREVIEWTEXT : Message related to resolved URL in $PREVIEW
 * - $INPUT : Bad input URL being passed back to output form
 * - $SVCNAME : Name of the TightURL service
 * - $HEADCOLOR : Color of the H1 Header tag
 * - $TABLECOLOR : Color of the table containing URL input field
 * - $TAGLINE : Tagline of the Squish service
 * - $CPASTTEXT : Capitalized past-tense word for tightening URLs
 * - $PASTTEXT : Non-Capitalized past-tense word for tightening URLs
 * - $CVERBTEXT : Capitalized action word for tightening URLs
 * - $VERBTEXT : Non-Capitalized action word for tightening URLs
 * - $COPYRIGHT : Copyright duration string generated from $copystart global variable,
 *                will be current 4-digit year if $copystart not defined.
 * - $COPYRIGHTHOLDER : Name of copyright holder
 * - $URLBLS : HTML string of URIBLs TightURL is checking
 * - $HOST : Hostname Squish is running on
 * - $ALIAS : Alias hostname Squish is running on
 * - $SELF : Name Squish is invoked as
 * - $BBSTATS : Bad Behavior stats
 * - <PREVIEW></PREVIEW> : Section containing a TightURL preview input form
 * - <COMPLAIN></COMPLAIN> : Section containing a TightURL complaint input form
 * - <VIEW></VIEW> : Section containing TightURL preview output
 */
function display_HTML ($code$template$content=""$url=""$tighturl=""$input="") {
  global 
$svcname$verbtext$pasttext$tagline$uribls$parm$nover$conn$dbtable,
    
$headcolor$tablecolor$copyright$copyrightholder$self$aliasurl$complain
    
$complaints$squish$squisher$previews$preview$BB2$debug;
  
  
$removeview false;

  if (
$code=""$code "HTTP/1.0 200 OK";
  if (
preg_match("/\/$/"$template)) $template rtrim($template,"/");

  if (
file_exists("squish.tmpl")) {
    
$html file_get_contents("squish.tmpl");
    if ((
$template != "") && file_exists("squish." $template ".tmpl")) {
      
$templatecontents file_get_contents("squish." $template ".tmpl");
//      if ($template == "previews") {
//        if ($url) {
//          // Set the preview cookie to be valid for 20 years
//          setcookie("preview", true, time()+(60*60*24*365*20));
//        }
//        else {
//          // Delete the preview cookie
//          setcookie ("preview", "", time() - 3600);
//        }
//      }
      
if ($template == "view" || $template == "preview") {
        
$url TightURL_LookupID($conn$dbtable$tighturl$status);
        if (
$debug) {
          echo 
"conn: $conn  dbtable: $dbtable tighturl: $tighturl status: $status<br />\n";
        }
        switch (
$status) {
        case 
0:
        case 
1:
          
$preview "<a href=\"$url\">".sanitize_html_string($url)."</a>";
          
$previewtxt "";
          break;
        case 
5:
          
$preview sanitize_html_string($url);
          
$previewtxt "The URL below has been disabled due to complaints from our users.<br /><br />";
          break;
        case 
4:
          
$preview sanitize_html_string($url);
          
$previewtxt "The URL below has been disabled due to violations of our Terms of Service.<br /><br />";
          break;
        case 
3:
          
$preview sanitize_html_string($url);
          
$previewtxt "The URL below has been disabled due to blacklisting on "$uribls "<br /><br />";
          break;
        case 
2:
          
$preview sanitize_html_string($url);
          
$previewtxt "<large>The URL below has generated many user complaints and should be treated with suspicion."$uribls "</large><br /><br />";
          break;
        case -
255;
          
display_HTML("HTTP/1.0 404 Not Found""""Error: That " $svcname " ID is not in our database.");
          return;
        default:
          
display_HTML("HTTP/1.0 500 Server Error""""Error: There has been an error accessing the " $svcname " database.");
          return;        
          
//preg_match("|(.*)<VIEW>.*</VIEW>(.*)|is", $templatecontents, $matches);
          //$templatecontents = $matches[1] . $matches[2];
          //$preview = "";
          //$previewtxt = "Error: That " . $svcname . " ID is not in our database.<br /><br />";
          //break;
        
}
        
$templatecontents preg_replace("/\\\$PREVIEWTXT/"$previewtxt$templatecontents);
        
$templatecontents preg_replace("/\\\$PREVIEW/"$preview$templatecontents);
        if (
$status >= ) {
          
preg_match("|(.*)<VIEW>(.*)</VIEW>(.*)|is"$templatecontents$matches);
          
$templatecontents $matches[1] . $matches[2] . $matches[3];
        } 
      }
      
$html preg_replace("/\\\$HTML/"$templatecontents$html);
    }
    elseif (
$template != "") {
      
die_HTML($svcname"Error: Template file squish." $template ".tmpl cannot be found.");
    }

    if ((! 
$squish) && (! $preview) && (! $complain)) {
      
// Don't display the standard box of choices at all
      
if (preg_match("|(.*)<TIGHTURL>(.*)</TIGHTURL>(.*)|is"$html$matches))
        
$html $matches[1] . $matches[3];
    }
    if (
$squisher && $squish) {
      
// Display the squish section
      
if (preg_match("|(.*)<SQUISH>(.*)</SQUISH>(.*)|is"$html$matches))
        
$html $matches[1] . $matches[2] . $matches[3];
    }
    else {
      
// Remove the squish section
      
if (preg_match("|(.*)<SQUISH>(.*)</SQUISH>(.*)|is"$html$matches))
        
$html $matches[1] . $matches[3];
    }
    if (
$previews && $preview) {
      
// Display the preview section
      
if (preg_match("|(.*)<PREVIEW>(.*)</PREVIEW>(.*)|is"$html$matches)) {
        
// This hopefully only matches when it should.  Language substitute this if you don't use English templates.
        
if ((! $squish) || (! $squisher)) $matches[2] = preg_replace("|Or enter|s""Enter"$matches[2]);
        
$html $matches[1] . $matches[2] . $matches[3];
      }
    }
    else {
      
// Remove the preview section
      
if (preg_match("|(.*)<PREVIEW>(.*)</PREVIEW>(.*)|is"$html$matches))
        
$html $matches[1] . $matches[3];
    }
    if (
$complaints && $complain) {
      if (
$debug) echo "complaints: ".$complaints." complain: ".$complain."\n";
      
// Display the complaint/abuse section
      
if (preg_match("|(.*)<COMPLAIN>(.*)</COMPLAIN>(.*)|is"$html$matches)) {
        
// This hopefully only matches when it should.  Language substitute this if you don't use English templates.
        
if (((! $squish) || (! $squisher)) && ((! $preview) || (! $previews))) $matches[2] = preg_replace("|Or report|s""Report"$matches[2]);
        
$html $matches[1] . $matches[2] . $matches[3];
      }
       
$html $matches[1] . $matches[2] . $matches[3];
    }
    else {
      
// Do not display the complaint/abuse section
      
if (preg_match("|(.*)<COMPLAIN>(.*)</COMPLAIN>(.*)|is"$html$matches))
       
$html $matches[1] . $matches[3];
    }
    
    if (
substr($content06) == "Error:") {
      
$content preg_replace("/Error:/""<big><font color='red'>Error:"$content)."</font></big>";
    }
    if (
$content$content .= "<br />\n";
    
// Always replace longer similar tokens before shorter ones. Things won't work the
    // way you expect if you replace $URL first, and then replace $URLMORELETTERS.
    
$html preg_replace("/\\\$HTML/"$content$html);
    
$html preg_replace("/\\\$PARM/"$parm$html);
    
$html preg_replace("/\\\$URLLEN/"strlen($url), $html);
    
$html preg_replace("/\\\$URL/"htmlspecialchars($url), $html);
    
$html preg_replace("/\\\$INPUT/"$input$html);
    if (isset(
$nover) && $nover) {
      
$html preg_replace("/\\\$SQUISHVER/"""$html);
      
$html preg_replace("/\\\$TIGHTURLVER/"""$html);
    }
    else {
      
$html preg_replace("/\\\$SQUISHVER/"VERSION$html);
      
$html preg_replace("/\\\$TIGHTURLVER/"TLVERSION$html);
    }
    
$html preg_replace("/\\\$TIGHTURLLEN/"strlen($tighturl), $html);
    
$html preg_replace("/\\\$TIGHTURL/"$tighturl$html);
    
$html preg_replace("/\\\$DIFF/"strlen($url)-strlen($tighturl), $html);
    
$html preg_replace("/\\\$SVCNAME/"$svcname$html);
    
$html preg_replace("/\\\$HEADCOLOR/"$headcolor$html);
    
$html preg_replace("/\\\$TABLECOLOR/"$tablecolor$html);
    
$html preg_replace("/\\\$TAGLINE/"$tagline$html);
    
$html preg_replace("/\\\$CPASTTEXT/"$pasttext$html);
    
$html preg_replace("/\\\$PASTTEXT/"strtolower($pasttext), $html);
    
$html preg_replace("/\\\$VERBTEXT/"strtolower($verbtext), $html);
    
$html preg_replace("/\\\$CVERBTEXT/"$verbtext$html);
    
$html preg_replace("/\\\$COPYRIGHTHOLDER/"$copyrightholder$html);
    
$html preg_replace("/\\\$COPYRIGHT/"$copyright$html);
    
$html preg_replace("/\\\$URIBLS/"$uribls$html);
    
$html preg_replace("/\\\$HOST/"$_SERVER['HTTP_HOST'], $html);
    
$html preg_replace("/\\\$ALIASURL/"$aliasurl$html);
    
$html preg_replace("/\\\$SELF/"$self$html);
    
$html preg_replace("/\\\$__/""$"$html); // Template Variables shown as text instead of substituted
    
if (preg_match("|<title>(.*)</title>|is"$html$matches)) 
      
$html preg_replace("|<title>(.*)</title>|is""<title>" strip_tags($matches[1]) . "</title>"$html);
    if (
$BB2 && function_exists('bb2_insert_stats')) {
      
$html preg_replace("/\\\$BBSTATS/",  bb2_insert_stats(), $html);
      
$bb2code bb2_insert_head();
      if (
preg_match("|<head>(.*)</head>|is"$html$matches))
        
$html preg_replace("|<head>(.*)</head>|is""<head>\n" $bb2code $matches[1] . "</head>"$html);
    }             
    else {
      
$html preg_replace("/\\\$BBSTATS/"""$html);           
    }
    if (
$template == "previews") {
      if (
$previews) {
        
$cookie "On";
        
$cookieopposite "Off";
      }
      else {
        
$cookie "Off";
        
$cookieopposite "On";
      }
      
$html preg_replace("/\\\$PREVIEWSOPPOSITE/"strtoupper($cookieopposite), $html);
      
$html preg_replace("/\\\$PREVIEWTOGGLE/"$self "?p=" strtolower($cookieopposite), $html);
      
$html preg_replace("/\\\$PREVIEWON/"$self "?p=on"$html);
      
$html preg_replace("/\\\$PREVIEWOFF/"$self "?p=off"$html);
      
$html preg_replace("/\\\$PREVIEWALIASTOGGLE/"$aliasurl "?c=" strtolower($cookieopposite), $html);
      
$html preg_replace("/\\\$PREVIEWALIASON/"$aliasurl "?p=on"$html);
      
$html preg_replace("/\\\$PREVIEWALIASOFF/"$aliasurl "?p=off"$html);
      
$html preg_replace("/\\\$PREVIEWS/"strtoupper($cookie), $html);
    }
    
header($code);
    echo 
$html;
  }
  else {
    
die_HTML($svcname"Error: <big><font color='red'>Error: Squish/TightURL Redirection service (" $svcname ") site template not found.</font></big>");
  }
}

/**
 * Die in an HTML-friendly way, without the benefit of a template.
 * Use display_HTML to "die" using the Squish/TightURL site template.
 */
function die_HTML($svcname$errmsg$code="HTTP/1.0 500 Internal Server Error") {
  
header($code);
  echo 
"<html>\n  <head>\n    <title>" strip_tags($svcname) . "</title>\n  </head>\n  <body>\n";
  echo 
"   " $errmsg "<br />\n";
  echo 
"  </body>\n</html>";
  die;
}


/**
 * Clone the user's Squish preferences into any alias domain.
 * Do redirection acrobatics thus:
 * Call cookie-setting page in current domain.  Set cookie there.
 * Redirect without showing output to alias domain.  Set cookie there.
 * Redirect without showing output to original page.  Show current settings.
 */
function Cookie_Cloner($cookie$value$current$alias) {
  
// Set cookie for this domain
  // Call the alias site to set cookie there, pass along referrer
  
}

// ***************** Non-Squish-specific functions *********************

/**
 * sanitize a string for MySQL input
 */
function sanitize_sql_string($string) {
  if (
get_magic_quotes_gpc()) $string stripslashes($string);
  return 
mysql_escape_string($string);
}

/**
 * sanitize a string for HTML output
 */
function sanitize_html_string($string) {
  if (
get_magic_quotes_gpc()) $string stripslashes($string);
  
$string htmlentities(strip_tags($string));
  return (
$string);
}

?>