Location: PHPKode > projects > PHPDug > install/upgrade_0.6.php
<?php
require_once('../includes/config.php');

function install_error($error)
{
	if(!isset($_GET['ignore_errors']))
	{
		exit($error);
	}
}

$install_version = '2.0.0';

// Check to see if the script is already installed
if(isset($config['installed']))
{
	if($config['version'] == $install_version)
	{
		// Exit the script
		exit('PHPDug is already installed.');
	}
}

if($config['version'] < 0.6)
{
	exit('Upgrading is only available as of v0.6, you are running v'.$config['version']);
}

if(!isset($_GET['install']))
{
	echo 'Before you run an upgrade it is recomended that you backup your PHPDug database<br><Br>Are you sure you want to upgrade your PHPDug installation from '.$config['version'].' to '.$install_version.'?<br><br><a href="upgrade_0.6.php?install=1">Yes do it</a>';
}
else
{
	ignore_user_abort(1);

	echo '<pre>';

	// Try to connect to the databse
	echo "Connecting to database.... \t";
    $db_connection = @mysql_connect ($config['db']['host'], $config['db']['user'], $config['db']['pass']) OR install_error('ERROR ('.mysql_error().')');
    $db_select = @mysql_select_db ($config['db']['name']) OR install_error('ERROR ('.mysql_error().')');
	echo "success<br>";
	
	//	Check that the tables exist
	echo "Checking tables...  \t\t";
	$table_info = @mysql_num_fields(mysql_query("SELECT * FROM ".$config['db']['pre']."stories LIMIT 1")) or install_error('ERROR ('.mysql_error().')');
	echo "success<br>";
	
	echo "Updating stories table.. \t";
	mysql_query("ALTER TABLE `".$config['db']['pre']."stories` ADD `story_prom` ENUM( '0', '1' ) NOT NULL ,ADD `story_prom_date` INT( 11 ) UNSIGNED NOT NULL ;") or install_error('ERROR ('.mysql_error().')');
	echo "success<br>";
	
	echo "Updating fulltext index.. \t";
	mysql_query("ALTER TABLE `".$config['db']['pre']."stories` ADD FULLTEXT (`story_title` ,`story_desc`)") or install_error('ERROR ('.mysql_error().')');
	echo "success<br>";
	
	echo "Changing admin menus.. \t";
	mysql_query("DROP TABLE `".$config['db']['pre']."amenu`");
	mysql_query("CREATE TABLE `".$config['db']['pre']."amenu` (  `menu_id` smallint(3) unsigned NOT NULL auto_increment,  `sort_id` smallint(3) unsigned NOT NULL default '0',  `parent_id` smallint(3) unsigned NOT NULL default '0',  `menu_title` varchar(40) NOT NULL default '',  `menu_icon` varchar(100) NOT NULL default '',  `menu_url` varchar(255) NOT NULL default '',  `menu_target` varchar(10) NOT NULL default '',  `menu_desc` varchar(100) NOT NULL default '',  PRIMARY KEY  (`menu_id`)) TYPE=MyISAM;");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(38, 0, 0, 'CONFIGURATION', '', '', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(39, 15, 38, 'SITEDET', '<img src=\"images/icons/smicon_config.gif\">', 'site_details.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(40, 10, 97, 'EDITDET', '<img src=\"images/icons/smicon_config.gif\">', 'database.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(46, 10, 38, 'DIGGCOM', '<img src=\"images/icons/smicon_digglogo.gif\">', 'usedigg.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(41, 20, 0, 'USERS', '', '', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(42, 20, 41, 'SEARCHUSER', '<img src=\"images/icons/smicon_search.gif\">', 'search_users.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(43, 10, 0, 'CATEGORIES', '', '', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(44, 0, 43, 'ADDCAT', '<img src=\"images/icons/smicon_addrule.gif\">', 'category_add.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(45, 2, 43, 'EDITCAT', '<img src=\"images/icons/smicon_editrule.gif\">', 'category_view.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(47, 30, 0, 'STORIES', '', '', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(48, 5, 47, 'SEARCHSTOR', '<img src=\"images/icons/smicon_search.gif\">', 'search_stories.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(83, 40, 0, 'ADMINS', '', '', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(84, 0, 83, 'ADDADMIN', '<img src=\"images/icons/smicon_addrule.gif\">', 'admin_add.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(85, 0, 83, 'EDITADMIN', '<img src=\"images/icons/smicon_editrule.gif\">', 'admin_view.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(86, 50, 0, 'LOGOUT', '', 'logout.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(87, 35, 0, 'CONTENT', '', '', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(88, 10, 87, 'ADDCONTENT', '<img src=\"images/icons/smicon_addrule.gif\">', 'content_add.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(89, 20, 87, 'EDITCONTENT', '<img src=\"images/icons/smicon_editrule.gif\">', 'content_edit.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(90, 20, 38, 'TEMPLATES', '<img src=\"images/icons/smicon_template.gif\">', 'template_settings.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(91, 12, 38, 'EMAILSET', '<img src=\"images/icons/smicon_config.gif\">', 'email.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(92, 14, 38, 'RATEMODE', '<img src=\"images/icons/smicon_config.gif\">', 'mode.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(93, 10, 47, 'VIEWALL', '<img src=\"images/icons/smicon_search.gif\">', 'view_stories.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(94, 30, 41, 'VIEWALL', '<img src=\"images/icons/smicon_search.gif\">', 'search_users2.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(95, 0, 47, 'COMMENTS', '', '', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(96, 10, 95, 'VIEWALL', '<img src=\"images/icons/smicon_search.gif\">', 'view_comments.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(97, 0, 38, 'DATABASE', '', '', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(98, 0, 95, 'SEARCHCOM', '<img src=\"images/icons/smicon_search.gif\">', 'search_comments.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(100, 15, 38, 'SCRIPTHEALTH', '<img src=\"images/icons/smicon_manage.gif\">', 'health.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(101, 0, 47, 'RSSFEEDS', '', '', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(102, 10, 101, 'ADDFEED', '<img src=\"images/icons/smicon_addrule.gif\">', 'feeds_add.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(103, 20, 101, 'EDITFEED', ' 	<img src=\"images/icons/smicon_editrule.gif\">', 'feeds_view.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(104, 90, 38, 'LOGVIEW', '<img src=\"images/icons/smicon_edittemplate.gif\">', 'log_viewer.php', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(105, 0, 41, 'USERGROUPS', '', '', '', '');");
	mysql_query("INSERT INTO `".$config['db']['pre']."amenu` VALUES(106, 0, 105, 'EDITGROUPS', ' 	<img src=\"images/icons/smicon_editrule.gif\">', 'usergroups_view.php', '', '');");
	echo "success<Br>";
	
	echo "Adding users column.. \t\t";
	mysql_query("ALTER TABLE `".$config['db']['pre']."users` ADD `commentst` MEDIUMINT( 4 ) DEFAULT '-4' NOT NULL ;") or install_error('ERROR ('.mysql_error().')');
	echo "success<br>";
	
	echo "Adding cats column.. \t\t";
	mysql_query("ALTER TABLE `".$config['db']['pre']."cats` ADD `cat_subs` INT( 11 ) UNSIGNED NOT NULL ;") or install_error('ERROR ('.mysql_error().')');
	echo "success<br>";
	
	echo "Adding comment digs table.. \t\t";
	mysql_query("CREATE TABLE `".$config['db']['pre']."cdigs` (  `comment_id` int(11) unsigned NOT NULL default '0',  `user_id` int(11) unsigned NOT NULL default '0',  `story_id` int(11) unsigned NOT NULL default '0',  `time` int(11) unsigned NOT NULL default '0',  `dig` tinyint(1) NOT NULL default '0',  PRIMARY KEY  (`comment_id`,`user_id`),  KEY `user_id` (`user_id`,`story_id`)) TYPE=MyISAM;");
	echo "success<br>";
	
	echo "Adding comment digs column.. \t\t";
	mysql_query("ALTER TABLE `".$config['db']['pre']."comm` ADD `comment_digs` MEDIUMINT( 8 ) DEFAULT '0' NOT NULL ;") or install_error('ERROR ('.mysql_error().')');
	echo "success<br>";
	
	echo "Modify story title field.. \t";
	mysql_query("ALTER TABLE `".$config['db']['pre']."stories` CHANGE `story_title` `story_title` VARCHAR( 255 )");
	echo "success<Br>";
	
	echo "Adding Log Table.. \t";
	mysql_query("CREATE TABLE `".$config['db']['pre']."logs` (  `log_id` int(11) unsigned NOT NULL auto_increment,  `log_date` int(11) unsigned NOT NULL default '0',  `log_summary` varchar(100) NOT NULL default '',  `log_details` mediumtext NOT NULL,  PRIMARY KEY  (`log_id`)) ENGINE=MyISAM AUTO_INCREMENT=7 ;");
	echo "success<Br>";
								
	echo "Adding Feed Log Table.. \t";
	mysql_query("CREATE TABLE `".$config['db']['pre']."feeds_log` (  `feed_id` int(11) unsigned NOT NULL default '0',  `feed_hash` varchar(40) NOT NULL default '',  `feed_time` int(11) unsigned NOT NULL default '0',  PRIMARY KEY  (`feed_id`,`feed_hash`)) ENGINE=MyISAM;");
	echo "success<Br>";
	
	echo "Adding Inbox Table.. \t";
	mysql_query("CREATE TABLE `".$config['db']['pre']."inbox` (  `message_id` int(11) unsigned NOT NULL auto_increment,  `message_from` int(11) unsigned NOT NULL default '0',  `message_to` int(11) unsigned NOT NULL default '0',  `message_date` int(11) unsigned NOT NULL default '0',  `message_subject` varchar(50) NOT NULL default '',  `message_body` mediumtext NOT NULL,  `message_read` enum('0','1') NOT NULL default '0',  PRIMARY KEY  (`message_id`),  KEY `message_from` (`message_from`),  KEY `message_to` (`message_to`)) ENGINE=MyISAM  AUTO_INCREMENT=1 ;");
	echo "success<Br>";
	
	echo "Adding Tab Field.. \t";
	mysql_query("ALTER TABLE `".$config['db']['pre']."stories` ADD `story_tags` VARCHAR( 100 ) NOT NULL ;");
	echo "success<Br>";
	
	echo "Adding Tags Table.. \t";
	mysql_query("CREATE TABLE `".$config['db']['pre']."tags` (  `tag_id` int(11) unsigned NOT NULL auto_increment, `story_id` int(11) unsigned NOT NULL default '0', `tag_phrase` varchar(100) NOT NULL default '',  `tag_time` int(11) NOT NULL default '0',  PRIMARY KEY  (`tag_id`),  KEY `tag_phrase` (`tag_phrase`))");
	echo "success<Br>";
	
	echo "Adding User Status Field.. \t";
	mysql_query("ALTER TABLE `".$config['db']['pre']."users` ADD `status` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '1' AFTER `user_id` ;");
	echo "success<Br>";
	
	echo "Check comments has a parent field.. \t";
	$noparentid = true;
	$query_result = mysql_query("SHOW COLUMNS FROM `".$config['db']['pre']."comm`");
	if (mysql_num_rows($query_result) > 0) 
	{
		while ($row = mysql_fetch_assoc($query_result)) 
		{
			foreach ($row as $value)
			{
				if($value['Field'] == 'parent_id')
				{
					$noparentid = false;
				}
			}
    	}
	}
	if($noparentid)
	{
		mysql_query("ALTER TABLE `".$config['db']['pre']."comm` ADD `parent_id` INT( 11 ) DEFAULT '0' NOT NULL ;") or install_error('ERROR ('.mysql_error().')');
	}
	echo "success<br>";
	
	echo "Adding feed tables.. \t";
	mysql_query("CREATE TABLE `".$config['db']['pre']."feeds` (  `feed_id` int(11) unsigned NOT NULL auto_increment,  `feed_url` varchar(255) NOT NULL default '',  `feed_lastch` int(11) unsigned NOT NULL default '0',  `feed_cat` int(11) unsigned NOT NULL default '0',  `feed_period` int(11) unsigned NOT NULL default '0',  `feed_user` int(11) unsigned NOT NULL default '0',  PRIMARY KEY  (`feed_id`)) AUTO_INCREMENT=6;");
	echo "success<Br>";
	
	echo "Updating story rank.. \t";
	$query = "SELECT story_id,story_time,story_digs,story_title,story_buries,story_prom FROM ".$config['db']['pre']."stories LIMIT 2000";
	$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
	while ($info = @mysql_fetch_array($query_result))
	{
		$promote = false;
		$ago = round(((time()-$info['story_time'])/60));
		$digs = $info['story_digs'];
		$buries = $info['story_buries'];
		$days = floor(($ago/60)/24);
		
		$rate = ($ago/$digs);
		
		if($info['story_time'] < (time()-43200))
		{
			$rate = 99999;
		}
		elseif($buries > 5)
		{
			$rate = 99999;
		}
		else
		{
			if(!$info['story_prom'])
			{
				if($digs >= $config['rating_digs'])
				{
					if($config['rating_days'] > $days)
					{
						$promote = true;
					}
				}
			}
		}
	
		if($promote)
		{
			mysql_query("UPDATE `".$config['db']['pre']."stories` SET `story_rating` = '".addslashes($rate)."',`story_prom` = '1' ,`story_prom_date` = '".time()."' WHERE `story_id` =".$info['story_id']." LIMIT 1;");
		}
		else
		{
			mysql_query("UPDATE `".$config['db']['pre']."stories` SET `story_rating` = '".addslashes($rate)."' WHERE `story_id` =".$info['story_id']." LIMIT 1;");
		}
	}
	echo "success<Br>";
	
	echo "Adding Settings table.. \t";
	mysql_query("CREATE TABLE `".$config['db']['pre']."settings` (  `setting_id` int(11) unsigned NOT NULL auto_increment,  `setting_file` varchar(100) NOT NULL default '',  `setting_title` varchar(200) NOT NULL default '',  `setting_name` varchar(100) NOT NULL default '',  `setting_type` varchar(30) NOT NULL default 'textfield',  `setting_options` mediumtext NOT NULL,  `setting_value` mediumtext NOT NULL,  `setting_display` tinyint(1) unsigned NOT NULL default '0',  PRIMARY KEY  (`setting_id`),  KEY `setting_name` (`setting_name`),  KEY `setting_file` (`setting_file`))");
	echo "success<Br>";
	
	echo "Modifying comment users table.. \t";
	mysql_query("ALTER TABLE `".$config['db']['pre']."users` ADD `group_id` INT( 11 ) UNSIGNED NOT NULL DEFAULT '2' AFTER `user_id`;");
	echo "success<br>";
	
	echo "Creating usergroups table.. \t";
	mysql_query("CREATE TABLE `".$config['db']['pre']."usergroups` (  `group_id` int(11) unsigned NOT NULL auto_increment,  `group_removable` tinyint(1) unsigned NOT NULL default '0',  `group_name` varchar(50) NOT NULL default '',  `group_dig` tinyint(1) unsigned NOT NULL default '0',  `group_cdig` tinyint(1) unsigned NOT NULL default '0',  PRIMARY KEY  (`group_id`))");
	mysql_query("INSERT INTO `".$config['db']['pre']."usergroups` VALUES(1, 0, 'Guest', 0, 0);");
	mysql_query("INSERT INTO `".$config['db']['pre']."usergroups` VALUES(2, 0, 'Registered', 1, 1);");
	echo "success<br>";
	
	echo "Modifying comment digs table.. \t";
	mysql_query("ALTER TABLE `".$config['db']['pre']."digs` ADD `user_ip` INT( 11 ) UNSIGNED NOT NULL AFTER `user_id`;");
	mysql_query("ALTER TABLE `".$config['db']['pre']."digs` DROP PRIMARY KEY , ADD PRIMARY KEY ( `story_id` , `user_id` , `user_ip` )");
	echo "success<br>";
	
	echo "Modifying comment digs table.. \t";
	mysql_query("ALTER TABLE `".$config['db']['pre']."cdigs` ADD `user_ip` INT( 11 ) UNSIGNED NOT NULL DEFAULT '0' AFTER `user_id`;");
	mysql_query("ALTER TABLE `".$config['db']['pre']."cdigs` DROP PRIMARY KEY ,ADD PRIMARY KEY ( `comment_id` , `user_id` , `user_ip` )");
	echo "success<br>";

	// Check that config file is writtable
	echo "Checking config file.. \t\t";
	if(@is_writable('../includes/config.php'))
	{
		echo "success<br>";
	}
	else
	{
		echo 'ERROR (config.php permisions not set correctly)';
		exit;
	}
		
	// Start updating the config file with new variables
	echo "Writting config.php updates.. \t";
	// Content that will be written to the config file
	$content = "<?php\n";
	$content.= "\$config['db']['host'] = '".addslashes($config['db']['host'])."';\n";
	$content.= "\$config['db']['name'] = '".addslashes($config['db']['name'])."';\n";
	$content.= "\$config['db']['user'] = '".addslashes($config['db']['user'])."';\n";
	$content.= "\$config['db']['pass'] = '".addslashes($config['db']['pass'])."';\n";
	$content.= "\$config['db']['pre'] = '".addslashes($config['db']['pre'])."';\n";
	$content.= "\n";
	$content.= "\$config['site_title'] = '".addslashes(stripslashes($config['site_title']))."';\n";
	$content.= "\$config['site_url'] = '".addslashes(stripslashes($config['site_url']))."';\n";
	$content.= "\$config['site_path'] = '".addslashes(stripslashes($config['site_path']))."';\n";
	$content.= "\$config['admin_email'] = '".addslashes(stripslashes($config['admin_email']))."';\n";
	$content.= "\$config['upcomtime'] = '".addslashes(stripslashes($config['upcomtime']))."';\n";
	$content.= "\$config['usedigg'] = '0';\n";
	$content.= "\$config['vidthumb'] = '".addslashes(stripslashes($config['vidthumb']))."';\n";
	$content.= "\$config['mod_rewrite'] = '".addslashes(stripslashes($config['mod_rewrite']))."';\n";
	$content.= "\$config['anounce_en'] = '".addslashes(stripslashes($config['anounce_en']))."';\n";
	$content.= "\$config['anounce_txt'] = '".addslashes(stripslashes($config['anounce_txt']))."';\n";
	$content.= "\$config['safe'] = '".addslashes(stripslashes($config['safe']))."';\n";
	$content.= "\$config['security'] = '0';\n";
	$content.= "\$config['rating_mode'] = '0';\n";
	$content.= "\$config['rating_digs'] = '5';\n";
	$content.= "\$config['rating_days'] = '2';\n";
	$content.= "\$config['cron_type'] = '1';\n";
	$content.= "\$config['cron_time'] = '';\n";
	$content.= "\$config['search_type'] = '0';\n";
	$content.= "\$config['mailbox_en'] = '0';\n";
	$content.= "\$config['tags_en'] = '0';\n";
	$content.= "\$config['validation'] = '0';\n";
	$content.= "\n";
	$content.= "\$config['cookie_time'] = '".addslashes(stripslashes($config['cookie_time']))."';\n";
	$content.= "\$config['cookie_name'] = '".addslashes(stripslashes($config['cookie_name']))."';\n";
	$content.= "\n";
	$content.= "\$config['email']['type'] = '".addslashes(stripslashes($config['email']['type']))."';\n";
	$content.= "\$config['email']['smtp']['host'] = '".addslashes(stripslashes($config['email']['smtp']['host']))."';\n";
	$content.= "\$config['email']['smtp']['user'] = '".addslashes(stripslashes($config['email']['smtp']['user']))."';\n";
	$content.= "\$config['email']['smtp']['pass'] = '".addslashes(stripslashes($config['email']['smtp']['pass']))."';\n";
	$content.= "\n";
	$content.= "\$config['tpl_name'] = '".addslashes(stripslashes($config['tpl_name']))."';\n";
	$content.= "\$config['version'] = '".addslashes(stripslashes($install_version))."';\n";
	$content.= "\$config['lang'] = '".addslashes(stripslashes($config['lang']))."';\n";
	$content.= "\$config['installed'] = '1';\n";
	$content.= "?>";
	
	// Open the includes/config.php for writting
	$handle = fopen('../includes/config.php', 'w');
	// Write the config file
	fwrite($handle, $content);
	// Close the file
	fclose($handle);
	echo "success<br>";
	
	echo "<br><Br><Br>Thank You! for upgrading PHPDug, Please <a href=\"../index.php\">click here</a> to access your site";

	echo '</pre>';
}
?>
Return current item: PHPDug