######################################################################################
##
## MOD Title:           New_Remove_Remarks
##
## MOD Author:          Ron, Berlin, Germany        eMail: Ron@RonGS.de
##
## MOD Description:     The standard function "remove_remarks" located in file
##                      "include/sql_parse.php" has an important bug: whenever a
##                      hashmark ("#")is written in a posting text at the first
##                      column of a line, this line will be identified as a comment
##                      line in the database backupfile. 
##
##                      This mod extends the standard function by checking, if a
##                      hashmark is part of a preopened string. In this case, the
##                      line will be handled as normal data to restore. 
##
##                      In addition to this another not so important bug will be
##                      fixed: the number of seconds the script with database
##                      utilities is able to run is significant increased. In the
##                      phpbb software the value of 1200 seconds is used. These
##                      20 minutes are often not enough to process a big backup
##                      file. This value is set to 14400 seconds (4 hours).
## 
## Files to edit:       2  
##
##                      admin/admin_db_utilities.php
##                      include/sql_parse.php
##
## Included Files:      1
##
##                      New_Remove_Remarks-Install.txt (this file)
##
## Install Level:       easy
##
## Install Time:        10 minutes
##
## Tested Version:      2.0.22
##
## MOD Version:         1.0
##
## MOD History:         2007/10/01  initial release
##
######################################################################################
##
## Important Notice:    For safety reasons make a backup of the related files
##                      and/or the database first!
##        
##                      Although this mod was tested, the author can't guarantee, 
##                      that it will work properly in each system environment nor 
##                      in each version of the phpbb forum software.
##
######################################################################################
##
## Copyright Notice:    Copyright (c) 2007 by Roland Reiche, Berlin, Germany
##
##                      This mod is a contribution to the uncommercial internet.
##                      Feel free to use, copy and modify it as you like.
##                      The only restriction is, that it must not be distributed
##                      for commercial purposes. If you want to use it this way,
##                      please contact me under the eMail address as noted above.
##
##                      I would appreciate it, if my nickname (Ron) is noted in any 
##                      further software development which is using this mod.
##
######################################################################################
#
#
#-----[ OPEN FILE ]----------------------------------------------
admin/admin_db_utilities.php
#
#-----[ FIND ]---------------------------------------------------
#
@set_time_limit(1200);
#
#-----[ REPLACE WITH ]-------------------------------------------
#
@set_time_limit(14400);
#
#-----[ OPEN FILE ]----------------------------------------------
#
include/sql_parse.php
#
#-----[ FIND ]---------------------------------------------------
#
function remove_remarks($sql)
{
	$lines = explode("\n", $sql);
	
	// try to keep mem. use down
	$sql = "";
	
	$linecount = count($lines);
	$output = "";

	for ($i = 0; $i < $linecount; $i++)
	{
		if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0))
		{
			if ($lines[$i][0] != "#")
			{
				$output .= $lines[$i] . "\n";
			}
			else
			{
				$output .= "\n";
			}
			// Trading a bit of speed for lower mem. use here.
			$lines[$i] = "";
		}
	}
	
	return $output;
	
}
#
#-----[ REPLACE WITH ]-------------------------------------------
#
function remove_remarks($sql)
{
	$matches = array();

	$lines = explode("\n", $sql);

	// try to keep mem. use down
	$sql = "";

	$linecount = count($lines);
	$output = "";
	$total_quotes = 0;
	$is_in_string = false;

	for ($i = 0; $i < $linecount; $i++)
	{
		if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0))
		{
			if (($lines[$i][0] != "#") || ($is_in_string))
			{
				$output .= $lines[$i] . "\n";

			    	// add the total number of single quotes in this line
			    	$total_quotes_in_line = preg_match_all("/'/", $lines[$i], $matches);
			    	// and subtract all single quotes that are preceded by an odd number
                      	// of backslashes, which means they're escaped quotes.
			    	$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $lines[$i], $matches);

			    	$total_quotes += $total_quotes_in_line - $escaped_quotes;

			    	if (($total_quotes % 2) != 0){
					// the following line is part of an open string.
                         	// so a possible hashmark (#) at the first column do also.
				   	$is_in_string = true;
			    	}
                      	else {
                          	$is_in_string = false;
                      	}
			}
			else
			{
				$output .= "\n";
			}

			// Trading a bit of speed for lower mem. use here.
			$lines[$i] = "";
		}
	}

	return $output;

}
#
#-----[ SAVE/CLOSE ALL FILES ]-----------------------------------
#
# EoM
