<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	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 opinion) 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, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

//======================================================================
// MemHT Portal 4.0.1 Update (from 4.0.0)
//======================================================================

if (isAuthTemp()) {
	$dblink->query("ALTER TABLE `memht_config` ADD `mail_usesmtp` INT( 1 ) NOT NULL DEFAULT '0',
					ADD `mail_smtp` VARCHAR( 255 ) NOT NULL ,
					ADD `mail_smtpauth` INT( 1 ) NOT NULL DEFAULT '0',
					ADD `mail_smtpuser` VARCHAR( 255 ) NOT NULL ,
					ADD `mail_smtppass` VARCHAR( 255 ) NOT NULL");
	//Newsletter
	$dblink->query("ALTER TABLE `memht_newsletter` ADD `sent` INT( 1 ) NOT NULL DEFAULT '0', ADD `date` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00'");
	$dblink->query("ALTER TABLE `memht_newsletter` ADD UNIQUE (`email`)");
	$dblink->query("DROP TABLE `memht_newsletter_inviate`");
	$dblink->query("DROP TABLE `memht_newsletter_status`");
	$dblink->query("DROP TABLE `memht_newsletter_busy`");
	$dblink->query("ALTER TABLE `memht_newsletter_config` DROP `mailfirstsession`, DROP `mailpause`");
	$dblink->query("CREATE TABLE `memht_newsletter` (
					  `id` int(10) NOT NULL auto_increment,
					  `email` varchar(255) NOT NULL default '',
					  `ip` varchar(15) NOT NULL default '',
					  `sent` int(1) NOT NULL default '0',
					  `date` datetime NOT NULL default '0000-00-00 00:00:00',
					  PRIMARY KEY  (`id`),
					  UNIQUE KEY `email` (`email`)
					) ENGINE=MyISAM");
	$dblink->query("CREATE TABLE `memht_newsletter_busy` (
					  `busy` int(1) NOT NULL default '0',
					  `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
					) ENGINE=MyISAM");
	$dblink->query("CREATE TABLE `memht_newsletter_status` (
					  `date` datetime NOT NULL default '0000-00-00 00:00:00',
					  `title` varchar(255) NOT NULL,
					  `content` text NOT NULL,
					  `emails` int(10) NOT NULL
					) ENGINE=MyISAM");
	$dblink->query("CREATE TABLE `memht_newsletter_sent` (
					  `id` int(10) NOT NULL auto_increment,
					  `title` varchar(255) NOT NULL,
					  `content` text NOT NULL,
					  `date` datetime NOT NULL default '0000-00-00 00:00:00',
					  `numemails` int(10) NOT NULL,
					  PRIMARY KEY  (`id`)
					) ENGINE=MyISAM");
		echo "<div>&nbsp;&nbsp;- Reset newsletter data: Ok</div>";
	//Content language
	$dblink->query("CREATE TABLE `memht_content_languages` (
					  `id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
					  `language` VARCHAR( 255 ) NOT NULL ,
					  `def` INT( 0 ) NOT NULL
					) ENGINE=MyISAM");
	$dblink->query("ALTER TABLE `memht_articoli` ADD `language` INT( 2 ) NOT NULL DEFAULT '0' AFTER `usecomments`");
	$dblink->query("ALTER TABLE `memht_blog_posts` ADD `language` INT( 2 ) NOT NULL DEFAULT '0' AFTER `usecomments`");
	$dblink->query("ALTER TABLE `memht_guide` ADD `language` INT( 2 ) NOT NULL DEFAULT '0' AFTER `usecomments`");
	$dblink->query("ALTER TABLE `memht_news` ADD `language` VARCHAR( 2 ) NOT NULL DEFAULT '0' AFTER `usecomments`");
	//New privs
	$dblink->query("ALTER TABLE `memht_utenti` ADD `rank` INT( 1 ) NOT NULL DEFAULT '1' AFTER `feed`");
	$dblink->query("CREATE TABLE `memht_privs` (
					  `userid` int(10) NOT NULL,
					  `page` varchar(255) NOT NULL
					) ENGINE=MyISAM");
	$dblink->query("TRUNCATE TABLE `memht_utenti_profile_data`");
	$dblink->query("ALTER TABLE `memht_utenti_profile_data` CHANGE `user` `uid` INT( 10 ) NOT NULL");
		echo "<div>&nbsp;&nbsp;- Reset custom users profile data: Ok</div>";
	$dblink->query("TRUNCATE TABLE `memht_groups`");
	$dblink->query("TRUNCATE TABLE `memht_groups_blacklist`");
	$dblink->query("TRUNCATE TABLE `memht_groups_members`");
	$dblink->query("TRUNCATE TABLE `memht_groups_pages`");
	$dblink->query("ALTER TABLE `memht_groups_members` CHANGE `user` `user` INT( 10 ) NOT NULL");
	$dblink->query("ALTER TABLE `memht_groups_blacklist` CHANGE `user` `user` INT( 10 ) NOT NULL");
	$dblink->query("ALTER TABLE `memht_groups_blacklist` ADD UNIQUE (`user`)");
		echo "<div>&nbsp;&nbsp;- Reset groups: Ok</div>";
	$dblink->query("CREATE TABLE `memht_admin_tools` (
					  `id` varchar(255) NOT NULL,
					  `value` text NOT NULL,
					  PRIMARY KEY  (`id`)
					) ENGINE=MyISAM");
	$dblink->query("INSERT INTO `memht_admin_tools` (`id`, `value`) VALUES ('quicknote', 'You can save here your quick notes')");
	
	//Convert admins to users
	echo "<div>&nbsp;&nbsp;- Updating superadmin account <span style='font-size:90%;'><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>Note: Only the superadmin will be updated, you'll need to add other admins by yourself</i></span></div>";
	$row = $dblink->get_row("SELECT * FROM memht_amministratori ORDER BY id ASC LIMIT 1");
	$user = $row['user'];
	$pass = $row['pass'];
	$email = $row['email'];
	$superadmin = intval($row['superadmin']);
		
	$dblink->query("UPDATE memht_utenti SET rank='4' WHERE user='$user'");
		echo "<div style='color:#900;'>&nbsp;&nbsp;&nbsp;- <i>$user</i>: Updated</div>";
	$dblink->query("DROP TABLE `memht_amministratori`, `memht_amministratori_privs`;");
	
	//Update old into new ranks
		/*
		OLD RANKS		NEW RANKS
		2 = admin	=>	3 = admin
		*/
	$dblink->query("UPDATE memht_pagine SET rank='3' WHERE rank=2");
	//Blocks
	$dblink->query("UPDATE memht_blocks SET rank='3' WHERE rank=2");
	//MyPage
	$dblink->query("UPDATE memht_mypage SET rank='3' WHERE rank=2");
	
	//Forums
	$dblink->query("TRUNCATE TABLE `memht_forum_moderation`");
		echo "<div>&nbsp;&nbsp;- Reset forum moderators: Ok</div>";
	$dblink->query("ALTER TABLE `memht_forum_moderation` CHANGE `user` `groupid` INT( 10 ) NOT NULL DEFAULT '0'");
	
	//Create group "moderators" and apply it to all forums
	$dblink->query("INSERT INTO memht_groups (id,name,type,amount) VALUES (NULL,'Moderators','0','0')");
		echo "<div>&nbsp;&nbsp;- Create group `Moderators`: Ok <span style='font-size:90%;'><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>Note: The `Moderators` group is empty, you'll need to add moderators when the update is finished</i></span></div>";
	$result = $dblink->get_list("SELECT id FROM memht_forum_forums WHERE parent=0 ORDER BY id");
	foreach ($result as $row) {
		$fid = intval($row['id']);
		$dblink->query("INSERT INTO memht_forum_moderation (forum,groupid) VALUES ('$fid','1')");
	}
		echo "<div>&nbsp;&nbsp;- Apply `Moderators` to be the mod in all forums: Ok</div>";
	
	$dblink->query("ALTER TABLE `memht_config` ADD `offlinemsg` TEXT NOT NULL");
	//----
	$dblink->query("UPDATE `memht_info` SET `version` = '401'");
	echo "<br><br><div>Update 4.0.0 to 4.0.1: <b>Ok</b></div>";
	
} else {
	die("<b>Access Denied!</b><br>You must be logged as administrator to execute this file");
}

?>