<?php
/***************************************************************************
 *                            album_quote_fix.php
 *                            -------------------
 *   begin                : Monday, March 03, 2003
 *   copyright            : (C) 2003 Smartor
 *   email                : smartor_xp@hotmail.com
 *
 *   $Id: album_quote_fix.php,v 1.0.1 2003/03/04 12:02:21 ngoctu 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.
 *
 ***************************************************************************/

@set_time_limit(3600); // <grin>

define('IN_PHPBB', true);

$phpbb_root_path = './';
include($phpbb_root_path.'extension.inc');
include($phpbb_root_path.'common.'.$phpEx);

$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);

include($phpbb_root_path.'includes/db.'.$phpEx);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css">
<!--

font,th,td,p,body { font-family: "Courier New", courier; font-size: 11pt }

a:link,a:active,a:visited { color : #006699; }
a:hover		{ text-decoration: underline; color : #DD6900;}

hr	{ height: 0px; border: solid #D1D7DC 0px; border-top-width: 1px;}

.maintitle,h1,h2	{font-weight: bold; font-size: 22px; font-family: "Trebuchet MS",Verdana, Arial, Helvetica, sans-serif; text-decoration: none; line-height : 120%; color : #000000;}

.ok {color:green}

-->
</style>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#006699" vlink="#5584AA">

<table width="100%" border="0" cellspacing="0" cellpadding="10" align="center">
	<tr>
		<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td align="center" width="100%" valign="middle"><span class="maintitle">This script is for updating Album older than v2.0.4 only</span></td>
			</tr>
		</table></td>
	</tr>
</table>

<br clear="all" />

<?php

if( !isset($HTTP_GET_VARS['step']) )
{
	echo 'Click <a href="album_quote_fix.'.$phpEx.'?step=1">here</a> to make sure to run this script!';
	exit;
}

$start = isset($HTTP_GET_VARS['start']) ? intval($HTTP_GET_VARS['start']) : 0;
$limit_sql = ($start) ? $start . ', 100' : 100;
$queries_count = 0;

if ($HTTP_GET_VARS['step'] == 1)
{
	echo '<b>Updating table:</b>&nbsp;'. $table_prefix .'album&nbsp;';
	flush();

	$sql = "SELECT pic_id, pic_title, pic_desc, pic_username
			FROM ". $table_prefix ."album
			ORDER BY pic_id ASC
			LIMIT $limit_sql";
	
	if( $result = $db->sql_query($sql) )
	{
		$queries_count++;
	}

	$picrow = array();

	while( $row = $db->sql_fetchrow($result) )
	{
		$picrow[] = $row;
	}

	for ($i = 0; $i < count($picrow); $i++)
	{
		$sql = "UPDATE ". $table_prefix ."album
				SET pic_title = '". str_replace("'", "''", stripslashes($picrow[$i]['pic_title'])) ."',
				pic_desc = '". str_replace("'", "''", stripslashes($picrow[$i]['pic_desc'])) ."',
				pic_username = '". str_replace("'", "''", stripslashes($picrow[$i]['pic_username'])) ."'
				WHERE pic_id = ". $picrow[$i]['pic_id'];

		if( $db->sql_query($sql) )
		{
			$queries_count++;
		}
		else
		{
			$error = $db->sql_error();
			echo "<br />Error : ". $error['message'] . " - " . $sql . "<br />";
		}
	}

	echo "--> ". $queries_count . " queries done\n<br /><br />";

	if( count($picrow) == 100 )
	{
		echo 'Click <a href="album_quote_fix.'.$phpEx.'?step=1&amp;start='.($start+100).'">here</a> to continue!';
	}
	else
	{
		echo 'Click <a href="album_quote_fix.'.$phpEx.'?step=2">here</a> to continue!';
	}

	exit;
}

if ($HTTP_GET_VARS['step'] == 2)
{
	echo '<b>Updating table:</b>&nbsp;'. $table_prefix .'album_comment&nbsp;';
	flush();

	$sql = "SELECT comment_id, comment_username, comment_text
			FROM ". $table_prefix ."album_comment
			ORDER BY comment_id ASC
			LIMIT $limit_sql";
	
	if ($result = $db->sql_query($sql))
	{
		$queries_count++;
	}

	$picrow = array();

	while( $row = $db->sql_fetchrow($result) )
	{
		$picrow[] = $row;
	}

	for ($i = 0; $i < count($picrow); $i++)
	{
		$sql = "UPDATE ". $table_prefix ."album_comment
				SET comment_username = '". str_replace("'", "''", stripslashes($picrow[$i]['comment_username'])) ."',
				comment_text = '". str_replace("'", "''", stripslashes($picrow[$i]['comment_text'])) ."'
				WHERE comment_id = ". $picrow[$i]['comment_id'];

		if( $db->sql_query($sql) )
		{
			$queries_count++;
		}
		else
		{
			$error = $db->sql_error();
			echo "<br />Error : ". $error['message'] . " - " . $sql . "<br />";
		}
	}

	echo "--> ". $queries_count . " queries done\n<br /><br />";

	if( count($picrow) == 100 )
	{
		echo 'Click <a href="album_quote_fix.'.$phpEx.'?step=2&amp;start='.($start+100).'">here</a> to continue!';
	}
	else
	{
		echo 'Click <a href="album_quote_fix.'.$phpEx.'?step=3">here</a> to continue!';
	}

	exit;
}


if ($HTTP_GET_VARS['step'] == 3)
{
	echo '<b>Updating table:</b>&nbsp;'. $table_prefix .'album_cat&nbsp;';
	flush();

	$sql = "SELECT cat_id, cat_title, cat_desc
			FROM ". $table_prefix ."album_cat
			ORDER BY cat_id ASC
			LIMIT $limit_sql";
	
	if ($result = $db->sql_query($sql))
	{
		$queries_count++;
	}

	$picrow = array();

	while( $row = $db->sql_fetchrow($result) )
	{
		$picrow[] = $row;
	}

	for ($i = 0; $i < count($picrow); $i++)
	{
		$sql = "UPDATE ". $table_prefix ."album_cat
				SET cat_title = '". str_replace("'", "''", stripslashes($picrow[$i]['cat_title'])) ."',
				cat_desc = '". str_replace("'", "''", stripslashes($picrow[$i]['cat_desc'])) ."'
				WHERE cat_id = ". $picrow[$i]['cat_id'];

		if( $db->sql_query($sql) )
		{
			$queries_count++;
		}
		else
		{
			$error = $db->sql_error();
			echo "<br />Error : ". $error['message'] . " - " . $sql . "<br />";
		}
	}

	echo "--> ". $queries_count . " queries done\n<br /><br />";

	if( count($picrow) == 100 )
	{
		echo 'Click <a href="album_quote_fix.'.$phpEx.'?step=3&amp;start='.($start+100).'">here</a> to continue!';
	}
}


echo "\n<br />\n<b>COMPLETED!</b><br />\n";
echo $message . '<br />';
echo "<br /><b>NOW DELETE THIS FILE</b><br />\n";
echo "</body>";
echo "</html>";

?>