<?php

echo 'Updating from version 0.77 to 0.80<br>';

// Create Attachments Table
echo "Create Attachments Table...";
$sql = "CREATE TABLE ".$bbTable['attachments']." (
	attach_id int(10) unsigned NOT NULL auto_increment,
	post_id int(10) unsigned NOT NULL default '0',
	file_name varchar(255) NOT NULL default '',
	file_pseudoname varchar(255) NOT NULL default '',
	file_size int(10) unsigned NOT NULL default '0',
	file_hits int(10) unsigned NOT NULL default '0',
	is_approved TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0',
	PRIMARY KEY  (attach_id)
	) TYPE=MyISAM;";
if(!$db->query($sql)) die('ERROR: '.$sql."<br>".$db->error());
echo "&nbsp;&nbsp;&nbsp;&nbsp;Done.<br>";


// Move Attachments from Posts to Attachments table and approve them
echo "Move Attachments to Attachments Table...";
$sql = "ALTER TABLE ".$bbTable['posts']." ADD has_attachment TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'";
if(!$db->query($sql)) die('ERROR: '.$db->error());

$sql = "SELECT * FROM ".$bbTable['posts']." WHERE attachment!=''";
$result = $db->query($sql);
if(!$result) die('ERROR: '.$db->error());
while ($row = $db->fetch_object($result))
{
	$attach_sql  = "INSERT INTO ".$bbTable['attachments']." SET ";
	$attach_sql .= "post_id=".$row->post_id.", ";

	$attachment_csv = explode("|",$row->attachment);
	$attach_sql .= "file_name='".$attachment_csv[0]."', ";
	$attach_sql .= "file_pseudoname='".$attachment_csv[1]."', ";
	$attach_sql .= "file_size=".$attachment_csv[2].", ";
	$attach_sql .= "file_hits=".$attachment_csv[3].", ";
	$attach_sql .= "is_approved=1";
	if(!$db->query($attach_sql)) die('ERROR: '.$db->error());

	$update_sql  = "UPDATE ".$bbTable['posts']." SET has_attachment=1 WHERE post_id=".$row->post_id;
	if(!$db->query($update_sql)) die('ERROR: '.$db->error());
}
	
$sql = "ALTER TABLE ".$bbTable['posts']." DROP attachment";
if(!$db->query($sql)) die('ERROR: '.$db->error());
echo "&nbsp;&nbsp;&nbsp;&nbsp;Done.<br>";

// Update Posts table setting all existing posts as approved
echo "Update Posts table and approve all existing posts...";
$sql = "ALTER TABLE ".$bbTable['posts']." ADD is_approved TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'";
if(!$db->query($sql)) die('ERROR: '.$db->error());

$sql = "ALTER TABLE ".$bbTable['posts']." ADD anon_uname VARCHAR( 64 ) NOT NULL DEFAULT ''";
if(!$db->query($sql)) die('ERROR: '.$db->error());


$sql = "SELECT * FROM ".$bbTable['posts'];
$result = $db->query($sql);
if(!$result) die('ERROR: '.$db->error());
while ($row = $db->fetch_object($result))
{
	$update_sql  = "UPDATE ".$bbTable['posts']." SET is_approved=1 WHERE post_id=".$row->post_id;
	if(!$db->query($update_sql)) die('ERROR: '.$db->error());
}
echo "&nbsp;&nbsp;&nbsp;&nbsp;Done.<br>";


// Add Auto approve posts/attachments permissions and enable them for existing forums
echo "Add Auto approve posts/attachments permissions...";
$sql = "ALTER TABLE ".$bbTable['forum_access']." ADD autoapprove_post TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'";
if(!$db->query($sql)) die('ERROR: '.$db->error());

$sql = "ALTER TABLE ".$bbTable['forum_access']." ADD autoapprove_attach TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'";
if(!$db->query($sql)) die('ERROR: '.$db->error());

$sql = "ALTER TABLE ".$bbTable['forum_group_access']." ADD autoapprove_post TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'";
if(!$db->query($sql)) die('ERROR: '.$db->error());

$sql = "ALTER TABLE ".$bbTable['forum_group_access']." ADD autoapprove_attach TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'";
if(!$db->query($sql)) die('ERROR: '.$db->error());

$sql = "SELECT * FROM ".$bbTable['forum_access'];
$result = $db->query($sql);
if(!$result) die('ERROR: '.$db->error());
while ($row = $db->fetch_object($result))
{
	$update_sql  = "UPDATE ".$bbTable['forum_access']." SET autoapprove_post=1 WHERE forum_id=".$row->forum_id." AND user_id=".$row->user_id;
	if(!$db->query($update_sql)) die('ERROR: '.$db->error());
}

$sql = "SELECT * FROM ".$bbTable['forum_group_access'];
$result = $db->query($sql);
if(!$result) die('ERROR: '.$db->error());
while ($row = $db->fetch_object($result))
{
	$update_sql  = "UPDATE ".$bbTable['forum_group_access']." SET autoapprove_post=1 WHERE forum_id=".$row->forum_id." AND group_id=".$row->group_id;
	if(!$db->query($update_sql)) die('ERROR: '.$db->error());
}
echo "&nbsp;&nbsp;&nbsp;&nbsp;Done.<br>";
?>
