Location: PHPKode > projects > TightURL > tighturl-0.1.3.3/tighturl-install.php
<?php 
// Very crude installer thrown together so there can be a
// new TightURL 0.3.0 release.  It is ugly but will do the job.

if (isset($_REQUEST['Action']) && $_REQUEST['Action'])
{ 

// Install TightURL by setting up the database.  This will create tables if needed.
// It will also upgrade previous versions of the database to be 0.3.0 compliant

  (require_once("tighturl.config.inc.php")) or die_HTML("Error: Cannot locate tighturl.config.inc.php.");

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

  $new = mysql_query("CREATE TABLE `".$dbtable."` (  `id` int(11) unsigned NOT NULL auto_increment,  `status` tinyint(3) unsigned NOT NULL,  `hits` int(11) NOT NULL default '0',  `lasthit` datetime NOT NULL default '0000-00-00 00:00:00',  `url` text NOT NULL,  `adddate` datetime NOT NULL default '0000-00-00 00:00:00',  `addip` text NOT NULL,  `lastcheck` datetime NOT NULL default '0000-00-00 00:00:00',  `checkcount` int(10) unsigned NOT NULL,  `complaints` int(10) unsigned NOT NULL,  `lastcomplaint` datetime NOT NULL default '0000-00-00 00:00:00',  PRIMARY KEY  (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
  mysql_query("CREATE TABLE IF NOT EXISTS `".$dbtable."_bb2` (  `id` int(11) NOT NULL auto_increment,  `ip` text NOT NULL,  `date` datetime NOT NULL default '0000-00-00 00:00:00',  `request_method` text NOT NULL,  `request_uri` text NOT NULL,  `server_protocol` text NOT NULL,  `http_headers` text NOT NULL,  `user_agent` text NOT NULL,  `request_entity` text NOT NULL,  `key` text NOT NULL,  PRIMARY KEY  (`id`),  KEY `ip` (`ip`(15)),  KEY `user_agent` (`user_agent`(10))) ENGINE=MyISAM DEFAULT CHARSET=latin1;");

  if (! $new) {
    $fields = mysql_list_fields('$dbname', '$dbtable');
    $result = mysql_query("SHOW COLUMNS FROM $dbtable");

    $columnnames = array();
    while ($row = mysql_fetch_array($result)) {
      $columnnames[]=$row[0];
    }

    $hits = FALSE; $lasthit = FALSE;
    if (in_array('hits', $columnnames)) $hits = TRUE;
    if (in_array('lasthit', $columnnames)) $lasthit = TRUE;
    if (in_array('status', $columnnames)) $status = TRUE;

    if (! $status) {  // If there's a status field, then this DB has already been upgraded, do nothing.
      $maker = "ALTER TABLE `tighturl` DROP PRIMARY KEY, CHANGE `id` `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT FIRST, ADD `status` tinyint(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `id`";
      if ($hits) {
        $maker .= ", CHANGE `hits` `hits` int(11) NOT NULL DEFAULT '0' AFTER `status`";
      }
      else {
        $maker .= ", ADD `hits` int(11) NOT NULL DEFAULT '0' AFTER `status`";
      }
      if ($lasthit) {
        $maker .= ", CHANGE `lasthit` `lasthit` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `hits`";
      }
      else {
        $maker .= ", ADD `lasthit` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `hits`";
      }
      $maker .= ", CHANGE `url` `url` text NOT NULL AFTER `lasthit`"
       . ", ADD `adddate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `url`"
       . ", ADD `addip` text NOT NULL AFTER `adddate`"
       . ", ADD `checkcount` int(10) NOT NULL DEFAULT '0' AFTER `addip`"
       . ", ADD `lastcheck` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `checkcount`"
       . ", ADD `complaints` int(10) NOT NULL DEFAULT '0' AFTER `lastcheck`"
       . ", ADD `lastcomplaint` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `complaints`"
       . ", ADD PRIMARY KEY (`id`), TYPE=MyISAM;";

      mysql_query($maker) or die_HTML("Error: Database error. ". mysql_error());
    }
  }

?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
   <title>TightURL Installer</title>
</head>
<body>

<h1>Finished.</h1>

<p>Your TightURL database has been installed or upgraded as appropriate.</p>

<p><font color="red">You must delete (or at least rename) tighturl-install.php in order to run TightURL.</font></p>

</body>
</html>

<?php
exit; 

} else { 
// start else clause 

?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="hide@address.com (Johnie Ingram)">
   <META NAME="Description" CONTENT="The initial installation of Debian/GNU Apache.">
   <TITLE>TightURL Install/Upgrade</TITLE>
</head>
<body>
<h1>Welcome to the TightURL Installer and Upgrader</h1>

<P>This script will either set up a newly created TightURL database, or upgrade an existing TightURL database
to work with the version of TightURL you just installed.  This process will happen automatically once you click
the "install/upgrade now!" button below.</P>

<P>Before starting, please make sure you've completed the following steps below:<br />
<ol>
  <li>BACK UP your TightURL database!!!!</li>
  <li>Make sure <FONT FACE="FIXED"><CODE>tighturl.config.inc.php</CODE></FONT> has the proper settings for
      <FONT FACE="FIXED"><CODE>$dbhost</CODE></FONT>, <FONT FACE="FIXED"><CODE>$dbname</CODE></FONT>, <FONT FACE="FIXED"><CODE>$dbtable</CODE></FONT>,
      <FONT FACE="FIXED"><CODE>$dbuser</CODE></FONT>, and <FONT FACE="FIXED"><CODE>$dbpass</CODE></FONT>.</li>
</ol>

<form action="tighturl-install.php" method="post">
  <table border="0">
    <tr>
      <td width="100%">
        <p align="center"> 
          <input type="submit" name="Action" value="Install/Upgrade Now!"> 
        </p>
      </td>
    </tr>
  </table>
</form>
</P>

<P><HR NOSHADE></P>
</BODY>
</HTML>
<?php 
} // end else clause 

function die_HTML($errmsg) {

  echo "<html>\n  <head>\n    <title>TightURL Installer/Upgrader</title>\n  </head>\n  <body>\n";
  echo "   " . $errmsg . "<br />\n";
  echo "  </body>\n</html>";
  die;
}

?>
Return current item: TightURL