Location: PHPKode > projects > raSMP > rasmp/update_rasmp.php
<?php

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

 Name		: raSMP 2.0
 Copyright	: 2002, Adam Alkins
 Website	: http://www.rasmp.com
 email		: hide@address.com

 $Id: update_rasmp.php,v 1.22 2003/03/22 00:53:37 rasadam Exp $: 

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

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

This program 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.

This program is distributed in the hope that it 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.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the 
Free Software Foundation Inc., 59 Temple Place, Suite 330,
Boston, MA  02111-1307  USA

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

/*

raSMP Update file

NOTE: DO NOT RUN THIS FILE IS YOU ARE UPDATING YOUR CVS. ONLY
EXECUTE WITH TAGGED RELEASES. YOU'VE BEEN WARNED!

*/

define('RASMP',true);

define('SCRIPT_PATH','./');
include SCRIPT_PATH.'common/extension.inc';

// Limit PHP error reporting
error_reporting  (E_ERROR | E_WARNING | E_PARSE);

// Turn off magic_quotes_runtime (If on, will cause problems)
set_magic_quotes_runtime(0);


include SCRIPT_PATH.'common/timer_begin.'.FILE_EXT;
include SCRIPT_PATH.'common/db.'.FILE_EXT;
include SCRIPT_PATH.'common/constants.'.FILE_EXT;
include SCRIPT_PATH.'common/version.'.FILE_EXT;

unset($query);
unset($result);
unset($version);

// Because of a mistake in the schemas, fix version number
$query = "UPDATE ".CONFIG_TABLE." SET config_name = 'version' WHERE config_name = '".DB_PREFIX."version'";
db_query($query,'',0);

// get database version
$query = "SELECT config_value as number FROM ".CONFIG_TABLE." WHERE config_name = 'version'";
$result = db_query($query,'',0);

// if the database version could not be found
if(db_numrows($result)==0)
{
	echo '<b>Critical Error: Configuration value missing from the Database</b><br />Update Aborted';
	die;
}
else
{
	$version = db_fetcharray($result);
	
	// if the database version is the same as the file version
	if($version['number'] == RASMP_VERSION)
	{
		echo '<b>Error: raSMP has already been updated</b><br />Please delete this file!';
		die;
	}
}
unset($query);
switch($version['number'])
{
	case '2.0 Alpha':
		switch(DB_TYPE)
		{
			case 'mysql':
				$query[] = "ALTER TABLE ".BAD_LINKS_TABLE." ADD `error_type` TINYINT( 1 ) NOT NULL AFTER `error_id`";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('site_name', 'raSMP')";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('site_webmaster', 'hide@address.com')";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('cookie_time', 86400)";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('cookie_name', 'rasmpsite')";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('cookie_path', '/')";
				
				if($_SERVER['HTTP_HOST']=='localhost')
				{
					$cookie_domain = '';
				}
				else
				{
					$cookie_domain = $_SERVER['HTTP_HOST'];
				}
				
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('cookie_domain', '".$cookie_domain."')";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('rasmp_installed',".time().")";
				$query[] = "ALTER TABLE ".ACCOUNTS_TABLE." ADD `perm_settings` TINYINT(1) NOT NULL default '0' AFTER `perm_polls`";
				$query[] = "INSERT INTO ".ADMIN_TABLE." VALUES ('Site Settings', 'settings.php', 3)";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('online_timeout', 300)";
				$query[] = "CREATE TABLE ".DB_PREFIX."messages (
  message_id bigint(20) NOT NULL auto_increment,
  message_from mediumint(9) NOT NULL default '0',
  message_to mediumint(9) NOT NULL default '0',
  title varchar(255) NOT NULL default '',
  body mediumtext NOT NULL,
  is_read tinyint(1) NOT NULL default '0',
  in_folder mediumint(9) NOT NULL default '0',
  date_sent bigint(20) NOT NULL default '0',
  date_read bigint(20) NOT NULL default '0',
  PRIMARY KEY  (message_id)
) TYPE=MyISAM;";
				$query[] = "CREATE TABLE ".DB_PREFIX."message_folders (
  folder_id mediumint(9) NOT NULL auto_increment,
  folder_name varchar(255) NOT NULL default '',
  folder_creator mediumint(9) NOT NULL default '0',
  PRIMARY KEY  (folder_id)
) TYPE=MyISAM;";
				$query[] = "INSERT INTO ".MFOLDERS_TABLE." VALUES (1, 'Inbox', -1)";
				$query[] = "INSERT INTO ".ADMIN_TABLE." VALUES('Message Centre','messages.php',3)";
				$query[] = "ALTER TABLE ".ACCOUNTS_TABLE." ADD `perm_messages` TINYINT(1) NOT NULL default '0' AFTER `perm_errors`";

				if(!defined("NEWS_TABLE"))
				{
					define('NEWS_TABLE', DB_PREFIX.'news');
				}

				if(!defined("NEWSLETTER_TABLE"))
				{				
					define('NEWSLETTER_TABLE', DB_PREFIX.'newsletter');
				}
				
				$query[] = "DROP TABLE ".NEWS_TABLE.", ".NEWSLETTER_TABLE;
				$query[] = "ALTER TABLE ".ACCOUNTS_TABLE." DROP `perm_news`, DROP `perm_newsletter`, DROP `perm_polls`";
				$query[] = "DELETE FROM ".ADMIN_TABLE." WHERE module_file = 'news.php' OR module_file = 'newsletter.php' OR module_file = 'polls.php'";
				$query[] = "DELETE FROM ".ADMIN_CATS_TABLE." WHERE category_id = 1";
				$query[] = "UPDATE ".ADMIN_CATS_TABLE." SET category_id = category_id - 1";
				$query[] = "UPDATE ".ADMIN_TABLE." SET admin_cat = admin_cat - 1";
				$query[] = "ALTER TABLE ".ACCOUNTS_TABLE." DROP `perm_optimise`";
				$query[] = "DELETE FROM ".ADMIN_TABLE." WHERE module_file = 'optimise.php'";
				$query[] = "ALTER TABLE ".ACCOUNTS_TABLE." ADD `last_login` BIGINT DEFAULT '0' NOT NULL AFTER `user_email`, ADD `this_login` BIGINT DEFAULT '0' NOT NULL AFTER `last_login`";				
				$query[] = "ALTER TABLE ".ACCOUNTS_TABLE." ADD `message_popup` TINYINT(1) DEFAULT '0' NOT NULL AFTER `this_login`";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('site_disabled',0)";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('disabled_message','Our Site has been temporarily taken offline. Please check back later.')";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('page_not_exist','I\'m sorry but the page you requested does not exist.')";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('banned_message','ERROR: Banned from this site')";
				$query[] = "INSERT INTO ".CONFIG_TABLE." VALUES('ip_security',4)";
				$query[] = "ALTER TABLE ".ACCOUNTS_TABLE." ADD `user_notes` MEDIUMTEXT NOT NULL DEFAULT '' AFTER `user_password`";

				if(!defined("BAD_LINKS_TABLE"))
				{				
					define('BAD_LINKS_TABLE', DB_PREFIX.'bad_links');
				}
								
				$query[] = "DROP TABLE ".BAD_LINKS_TABLE;
				$query[] = "ALTER TABLE ".ACCOUNTS_TABLE." DROP `perm_news`";
				break;
		}
		break;
	default:
		echo '<b>Error: raSMP version not recognised</b><br />Update aborted!';
		die;
}

$query[] = "UPDATE ".CONFIG_TABLE." SET config_value = '".RASMP_VERSION."' WHERE config_name = 'version'";

for($i = 0; $i < count($query); $i++)
{
	$result = db_query($query[$i],'',0);
	if(!$result)
	{
		$error_msg[] = db_errormsg();
		$error_no[] = db_errorno();
		$error[] = $i;
	}
}

if(count($error)==0)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>raSMP Update</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<p><strong><font size="6">raSMP Update</font></strong> </p>
<p><strong>.<br />
  .<br />
  .<br />
  .<br />
  .<br />
  .</strong><br />
  <strong>.............................................................................................</strong><br />
  <strong>.</strong> Update from <?php echo $version['number']; ?> to <?php echo RASMP_VERSION; ?> completed successfuly<br />
  <strong>............................................................................................. 
  <br />
  .<br />
  .<br />
  .<br />
  .<br />
  .<br />
  . </strong></p>
<p>&nbsp; </p>
</body>
</html>
<?php
	die;
}
else
{
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>raSMP Update</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<p><strong><font size="6">raSMP Update</font></strong> </p>
<p> <strong>.............................................................................................</strong><br />
  <strong>.</strong> Update from <?php echo $version['number']; ?> to <?php echo RASMP_VERSION; ?> completed with errors<br />
  <strong>............................................................................................. 
  </strong></p>
<?php
	for($i = 0; $i < count($error); $i++)
	{
		echo '
<p>Query: <strong>'.$query[$error[$i]].'</strong><br />
  Error Number: <strong>'.$error_no[$i].'</strong><br />
  Error Message: <strong>'.$error_msg[$i].'</strong></p>';
?>
</body>
</html>
<?php
	}
	die;
}
?>
Return current item: raSMP