From 7d264396df47c17675dd70601a19cb3a06a45ff0 Mon Sep 17 00:00:00 2001
From: Meik Sievertsen <acydburn@phpbb.com>
Date: Fri, 2 Jun 2006 13:26:27 +0000
Subject: [PATCH] some adjustments nils: please have a look at the @todo
 comment in mcp_queue.php

git-svn-id: file:///svn/phpbb/trunk@6002 89ea8834-ac86-4346-8a33-228a782c2dd0
---
 phpBB/includes/db/dbal.php       |  17 +--
 phpBB/includes/db/firebird.php   |  44 +++---
 phpBB/includes/db/mssql.php      |  28 ++--
 phpBB/includes/db/mssql_odbc.php |  29 ++--
 phpBB/includes/db/mysql.php      |  29 ++--
 phpBB/includes/db/mysql4.php     |  31 ++--
 phpBB/includes/db/mysqli.php     |  29 ++--
 phpBB/includes/db/oracle.php     |  29 ++--
 phpBB/includes/db/postgres.php   |  24 ++--
 phpBB/includes/db/sqlite.php     |  23 ++-
 phpBB/includes/mcp/mcp_queue.php |   5 +-
 phpBB/language/en/groups.php     |   9 +-
 phpBB/viewtopic.php              | 237 +++++++++++++++----------------
 13 files changed, 256 insertions(+), 278 deletions(-)

diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php
index 2d8d2e7624..02072c1c60 100644
--- a/phpBB/includes/db/dbal.php
+++ b/phpBB/includes/db/dbal.php
@@ -225,17 +225,7 @@ class dbal
 			case 'SELECT':
 			case 'SELECT_DISTINCT';
 
-				if ($query == 'SELECT_DISTINCT')
-				{
-					$sql .= 'SELECT DISTINCT';
-				}
-				else
-				{
-					$sql .= 'SELECT';
-				}
-
-				$sql .= ' ' . $array['SELECT'];
-				$sql .= ' FROM ';
+				$sql = str_replace('_', ' ', $query) . ' ' . $array['SELECT'] . ' FROM ';
 
 				$table_array = array();
 				foreach ($array['FROM'] as $table_name => $alias)
@@ -270,6 +260,7 @@ class dbal
 
 			break;
 		}
+
 		return $sql;
 	}
 
@@ -316,7 +307,7 @@ class dbal
 			{
 				$this->sql_transaction('rollback');
 			}
-			
+
 			if (strlen($message) > 1024)
 			{
 				// We need to define $msg_long_text here to circumvent text stripping.
@@ -487,8 +478,6 @@ class dbal
 
 				$this->_sql_report($mode, $query);
 
-//				$this->num_queries['cache']++;
-
 			break;
 
 			case 'record_fromcache':
diff --git a/phpBB/includes/db/firebird.php b/phpBB/includes/db/firebird.php
index c876483a2b..61d76e275c 100644
--- a/phpBB/includes/db/firebird.php
+++ b/phpBB/includes/db/firebird.php
@@ -58,7 +58,7 @@ class dbal_firebird extends dbal
 			case 'begin':
 				$result = true;
 				$this->transaction = true;
-				break;
+			break;
 
 			case 'commit':
 				$result = @ibase_commit();
@@ -68,12 +68,12 @@ class dbal_firebird extends dbal
 				{
 					@ibase_rollback();
 				}
-				break;
+			break;
 
 			case 'rollback':
 				$result = @ibase_rollback();
 				$this->transaction = false;
-				break;
+			break;
 
 			default:
 				$result = true;
@@ -91,7 +91,6 @@ class dbal_firebird extends dbal
 		{
 			global $cache;
 
-
 			$this->last_query_text = $query;
 			$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
 			$this->sql_add_num_queries($this->query_result);
@@ -103,7 +102,6 @@ class dbal_firebird extends dbal
 					$this->sql_error($query);
 				}
 
-				// TODO: have to debug the commit states in firebird
 				if (!$this->transaction)
 				{
 					@ibase_commit_ret();
@@ -132,19 +130,19 @@ class dbal_firebird extends dbal
 	* Build LIMIT query
 	*/
 	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
-	{ 
-		if ($query != '') 
+	{
+		if ($query != '')
 		{
-			$this->query_result = false; 
+			$this->query_result = false;
 
 			$query = 'SELECT FIRST ' . $total . ((!empty($offset)) ? ' SKIP ' . $offset : '') . substr($query, 6);
 
 			return $this->sql_query($query, $cache_ttl); 
-		} 
-		else 
-		{ 
-			return false; 
-		} 
+		}
+		else
+		{
+			return false;
+		}
 	}
 
 	/**
@@ -222,7 +220,7 @@ class dbal_firebird extends dbal
 			{
 				$this->sql_rowseek($rownum, $query_id);
 			}
-			
+
 			$row = $this->sql_fetchrow($query_id);
 			return isset($row[$field]) ? $row[$field] : false;
 		}
@@ -241,16 +239,6 @@ class dbal_firebird extends dbal
 			$query_id = $this->query_result;
 		}
 
-/*
-		$this->sql_freeresult($query_id);
-		$query_id = $this->sql_query($this->last_query_text);
-
-		if (!$query_id)
-		{
-			return false;
-		}
-*/
-
 		// We do not fetch the row for rownum == 0 because then the next resultset would be the second row
 		for ($i = 0; $i < $rownum; $i++)
 		{
@@ -274,9 +262,9 @@ class dbal_firebird extends dbal
 		{
 			if ($this->query_result && preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#is', $this->last_query_text, $tablename))
 			{
-				$query = "SELECT GEN_ID(" . $tablename[1] . "_gen, 0) AS new_id  
-					FROM RDB\$DATABASE";
-				if (!($temp_q_id =  @ibase_query($this->db_connect_id, $query)))
+				$sql = "SELECT GEN_ID(" . $tablename[1] . "_gen, 0) AS new_id FROM RDB\$DATABASE";
+
+				if (!($temp_q_id =  @ibase_query($this->db_connect_id, $sql)))
 				{
 					return false;
 				}
@@ -287,7 +275,7 @@ class dbal_firebird extends dbal
 				return ($temp_result) ? $temp_result->NEW_ID : false;
 			}
 		}
-	
+
 		return false;
 	}
 
diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php
index 2f16d6cf22..c700525577 100644
--- a/phpBB/includes/db/mssql.php
+++ b/phpBB/includes/db/mssql.php
@@ -65,7 +65,7 @@ class dbal_mssql extends dbal
 			case 'begin':
 				$result = @mssql_query('BEGIN TRANSACTION', $this->db_connect_id);
 				$this->transaction = true;
-				break;
+			break;
 
 			case 'commit':
 				$result = @mssql_query('commit', $this->db_connect_id);
@@ -75,12 +75,12 @@ class dbal_mssql extends dbal
 				{
 					@mssql_query('ROLLBACK', $this->db_connect_id);
 				}
-				break;
+			break;
 
 			case 'rollback':
 				$result = @mssql_query('ROLLBACK', $this->db_connect_id);
 				$this->transaction = false;
-				break;
+			break;
 
 			default:
 				$result = true;
@@ -146,10 +146,10 @@ class dbal_mssql extends dbal
 	* Build LIMIT query
 	*/
 	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
-	{ 
-		if ($query != '') 
+	{
+		if ($query != '')
 		{
-			$this->query_result = false; 
+			$this->query_result = false;
 
 			// if $total is set to 0 we do not want to limit the number of rows
 			if ($total == 0)
@@ -170,11 +170,11 @@ class dbal_mssql extends dbal
 			}
 
 			return $this->sql_query($query, $cache_ttl); 
-		} 
-		else 
-		{ 
-			return false; 
-		} 
+		}
+		else
+		{
+			return false;
+		}
 	}
 
 	/**
@@ -217,7 +217,7 @@ class dbal_mssql extends dbal
 		}
 
 		$row = @mssql_fetch_assoc($query_id);
-		
+
 		// I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
 		if ($row)
 		{
@@ -247,11 +247,11 @@ class dbal_mssql extends dbal
 			{
 				$this->sql_rowseek($rownum, $query_id);
 			}
-			
+
 			$row = $this->sql_fetchrow($query_id);
 			return isset($row[$field]) ? $row[$field] : false;
 		}
-	
+
 		return false;
 	}
 
diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php
index 55e1608d89..4ae5339a62 100644
--- a/phpBB/includes/db/mssql_odbc.php
+++ b/phpBB/includes/db/mssql_odbc.php
@@ -59,7 +59,7 @@ class dbal_mssql_odbc extends dbal
 			case 'begin':
 				$result = @odbc_autocommit($this->db_connect_id, false);
 				$this->transaction = true;
-				break;
+			break;
 
 			case 'commit':
 				$result = @odbc_commit($this->db_connect_id);
@@ -71,13 +71,13 @@ class dbal_mssql_odbc extends dbal
 					@odbc_rollback($this->db_connect_id);
 					@odbc_autocommit($this->db_connect_id, true);
 				}
-				break;
+			break;
 
 			case 'rollback':
 				$result = @odbc_rollback($this->db_connect_id);
 				@odbc_autocommit($this->db_connect_id, true);
 				$this->transaction = false;
-				break;
+			break;
 
 			default:
 				$result = true;
@@ -100,7 +100,6 @@ class dbal_mssql_odbc extends dbal
 			{
 				$this->sql_report('start', $query);
 			}
-			
 
 			$this->last_query_text = $query;
 			$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
@@ -145,10 +144,10 @@ class dbal_mssql_odbc extends dbal
 	* Build LIMIT query
 	*/
 	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
-	{ 
-		if ($query != '') 
+	{
+		if ($query != '')
 		{
-			$this->query_result = false; 
+			$this->query_result = false;
 
 			// if $total is set to 0 we do not want to limit the number of rows
 			if ($total == 0)
@@ -168,12 +167,12 @@ class dbal_mssql_odbc extends dbal
 				$query = 'SELECT TOP ' . ($row_offset + $num_rows) . ' ' . substr($query, 6);
 			}
 
-			return $this->sql_query($query, $cache_ttl); 
-		} 
-		else 
-		{ 
-			return false; 
-		} 
+			return $this->sql_query($query, $cache_ttl);
+		}
+		else
+		{
+			return false;
+		}
 	}
 
 	/**
@@ -214,7 +213,7 @@ class dbal_mssql_odbc extends dbal
 		{
 			return $cache->sql_fetchrow($query_id);
 		}
-		
+
 		return ($query_id) ? @odbc_fetch_array($query_id) : false;
 	}
 
@@ -235,7 +234,7 @@ class dbal_mssql_odbc extends dbal
 			{
 				$this->sql_rowseek($rownum, $query_id);
 			}
-			
+
 			$row = $this->sql_fetchrow($query_id);
 			return isset($row[$field]) ? $row[$field] : false;
 		}
diff --git a/phpBB/includes/db/mysql.php b/phpBB/includes/db/mysql.php
index 8fc9e2a04b..c31132b990 100644
--- a/phpBB/includes/db/mysql.php
+++ b/phpBB/includes/db/mysql.php
@@ -65,7 +65,7 @@ class dbal_mysql extends dbal
 			case 'begin':
 				$result = @mysql_query('BEGIN', $this->db_connect_id);
 				$this->transaction = true;
-				break;
+			break;
 
 			case 'commit':
 				$result = @mysql_query('COMMIT', $this->db_connect_id);
@@ -75,12 +75,12 @@ class dbal_mysql extends dbal
 				{
 					@mysql_query('ROLLBACK', $this->db_connect_id);
 				}
-				break;
+			break;
 
 			case 'rollback':
 				$result = @mysql_query('ROLLBACK', $this->db_connect_id);
 				$this->transaction = false;
-				break;
+			break;
 
 			default:
 				$result = true;
@@ -146,10 +146,10 @@ class dbal_mysql extends dbal
 	* Build LIMIT query
 	*/
 	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
-	{ 
-		if ($query != '') 
+	{
+		if ($query != '')
 		{
-			$this->query_result = false; 
+			$this->query_result = false;
 
 			// if $total is set to 0 we do not want to limit the number of rows
 			if ($total == 0)
@@ -159,12 +159,12 @@ class dbal_mysql extends dbal
 
 			$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
 
-			return $this->sql_query($query, $cache_ttl); 
-		} 
-		else 
-		{ 
-			return false; 
-		} 
+			return $this->sql_query($query, $cache_ttl);
+		}
+		else
+		{
+			return false;
+		}
 	}
 
 	/**
@@ -302,9 +302,10 @@ class dbal_mysql extends dbal
 				$data = '(' . $data . ')';
 			break;
 		}
+
 		return $data;
 	}
-	
+
 	/**
 	* return sql error array
 	* @private
@@ -318,7 +319,7 @@ class dbal_mysql extends dbal
 				'code'		=> @mysql_errno()
 			);
 		}
-		
+
 		return array(
 			'message'	=> @mysql_error($this->db_connect_id),
 			'code'		=> @mysql_errno($this->db_connect_id)
diff --git a/phpBB/includes/db/mysql4.php b/phpBB/includes/db/mysql4.php
index ac896ce78f..26da6432c1 100644
--- a/phpBB/includes/db/mysql4.php
+++ b/phpBB/includes/db/mysql4.php
@@ -67,22 +67,22 @@ class dbal_mysql4 extends dbal
 			case 'begin':
 				$result = @mysql_query('BEGIN', $this->db_connect_id);
 				$this->transaction = true;
-				break;
+			break;
 
 			case 'commit':
 				$result = @mysql_query('COMMIT', $this->db_connect_id);
 				$this->transaction = false;
-				
+
 				if (!$result)
 				{
 					@mysql_query('ROLLBACK', $this->db_connect_id);
 				}
-				break;
+			break;
 
 			case 'rollback':
 				$result = @mysql_query('ROLLBACK', $this->db_connect_id);
 				$this->transaction = false;
-				break;
+			break;
 
 			default:
 				$result = true;
@@ -148,10 +148,10 @@ class dbal_mysql4 extends dbal
 	* Build LIMIT query
 	*/
 	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
-	{ 
-		if ($query != '') 
+	{
+		if ($query != '')
 		{
-			$this->query_result = false; 
+			$this->query_result = false;
 
 			// if $total is set to 0 we do not want to limit the number of rows
 			if ($total == 0)
@@ -162,12 +162,12 @@ class dbal_mysql4 extends dbal
 
 			$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
 
-			return $this->sql_query($query, $cache_ttl); 
-		} 
-		else 
-		{ 
-			return false; 
-		} 
+			return $this->sql_query($query, $cache_ttl);
+		}
+		else
+		{
+			return false;
+		}
 	}
 
 	/**
@@ -289,7 +289,7 @@ class dbal_mysql4 extends dbal
 		{
 			return @mysql_real_escape_string($msg);
 		}
-	
+
 		return @mysql_real_escape_string($msg, $this->db_connect_id);
 	}
 
@@ -305,6 +305,7 @@ class dbal_mysql4 extends dbal
 				$data = '(' . $data . ')';
 			break;
 		}
+
 		return $data;
 	}
 	
@@ -321,7 +322,7 @@ class dbal_mysql4 extends dbal
 				'code'		=> @mysql_errno()
 			);
 		}
-		
+
 		return array(
 			'message'	=> @mysql_error($this->db_connect_id),
 			'code'		=> @mysql_errno($this->db_connect_id)
diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php
index 79230b178a..1a969684a2 100644
--- a/phpBB/includes/db/mysqli.php
+++ b/phpBB/includes/db/mysqli.php
@@ -67,7 +67,7 @@ class dbal_mysqli extends dbal
 			case 'begin':
 				$result = @mysqli_autocommit($this->db_connect_id, false);
 				$this->transaction = true;
-				break;
+			break;
 
 			case 'commit':
 				$result = @mysqli_commit($this->db_connect_id);
@@ -79,13 +79,13 @@ class dbal_mysqli extends dbal
 					@mysqli_rollback($this->db_connect_id);
 					@mysqli_autocommit($this->db_connect_id, true);
 				}
-				break;
+			break;
 
 			case 'rollback':
 				$result = @mysqli_rollback($this->db_connect_id);
 				@mysqli_autocommit($this->db_connect_id, true);
 				$this->transaction = false;
-				break;
+			break;
 
 			default:
 				$result = true;
@@ -146,10 +146,10 @@ class dbal_mysqli extends dbal
 	* Build LIMIT query
 	*/
 	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
-	{ 
-		if ($query != '') 
+	{
+		if ($query != '')
 		{
-			$this->query_result = false; 
+			$this->query_result = false;
 
 			// if $total is set to 0 we do not want to limit the number of rows
 			if ($total == 0)
@@ -160,12 +160,12 @@ class dbal_mysqli extends dbal
 
 			$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
 
-			return $this->sql_query($query, $cache_ttl); 
-		} 
-		else 
-		{ 
-			return false; 
-		} 
+			return $this->sql_query($query, $cache_ttl);
+		}
+		else
+		{
+			return false;
+		}
 	}
 
 	/**
@@ -227,7 +227,7 @@ class dbal_mysqli extends dbal
 			{
 				$this->sql_rowseek($rownum, $query_id);
 			}
-			
+
 			$row = $this->sql_fetchrow($query_id);
 			return isset($row[$field]) ? $row[$field] : false;
 		}
@@ -296,9 +296,10 @@ class dbal_mysqli extends dbal
 				$data = '(' . $data . ')';
 			break;
 		}
+
 		return $data;
 	}
-	
+
 	/**
 	* return sql error array
 	* @private
diff --git a/phpBB/includes/db/oracle.php b/phpBB/includes/db/oracle.php
index f2499c5456..d9a7298c04 100644
--- a/phpBB/includes/db/oracle.php
+++ b/phpBB/includes/db/oracle.php
@@ -57,7 +57,7 @@ class dbal_oracle extends dbal
 			case 'begin':
 				$result = true;
 				$this->transaction = true;
-				break;
+			break;
 
 			case 'commit':
 				$result = @ocicommit($this->db_connect_id);
@@ -67,12 +67,12 @@ class dbal_oracle extends dbal
 				{
 					@ocirollback($this->db_connect_id);
 				}
-				break;
+			break;
 
 			case 'rollback':
 				$result = @ocirollback($this->db_connect_id);
 				$this->transaction = false;
-				break;
+			break;
 
 			default:
 				$result = true;
@@ -90,13 +90,12 @@ class dbal_oracle extends dbal
 		{
 			global $cache;
 
-
 			// EXPLAIN only in extra debug mode
 			if (defined('DEBUG_EXTRA'))
 			{
 				$this->sql_report('start', $query);
 			}
-		
+
 			$this->last_query_text = $query;
 			$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
 			$this->sql_add_num_queries($this->query_result);
@@ -128,7 +127,7 @@ class dbal_oracle extends dbal
 						$this->sql_transaction('commit');
 					}
 				}
-				
+
 				if (defined('DEBUG_EXTRA'))
 				{
 					$this->sql_report('stop', $query);
@@ -161,19 +160,19 @@ class dbal_oracle extends dbal
 	* Build LIMIT query
 	*/
 	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
-	{ 
-		if ($query != '') 
+	{
+		if ($query != '')
 		{
 			$this->query_result = false; 
 
 			$query = 'SELECT * FROM (SELECT /*+ FIRST_ROWS */ rownum AS xrownum, a.* FROM (' . $query . ') a WHERE rownum <= ' . ($offset + $total) . ') WHERE xrownum >= ' . $offset;
 
 			return $this->sql_query($query, $cache_ttl); 
-		} 
-		else 
-		{ 
+		}
+		else
+		{
 			return false; 
-		} 
+		}
 	}
 
 	/**
@@ -220,7 +219,7 @@ class dbal_oracle extends dbal
 		{
 			return $cache->sql_fetchrow($query_id);
 		}
-		
+
 		$row = array();
 		$result = @ocifetchinto($query_id, $row, OCI_ASSOC + OCI_RETURN_NULLS);
 
@@ -261,11 +260,11 @@ class dbal_oracle extends dbal
 			{
 				$this->sql_rowseek($rownum, $query_id);
 			}
-			
+
 			$row = $this->sql_fetchrow($query_id);
 			return isset($row[$field]) ? $row[$field] : false;
 		}
-		
+
 		return false;
 	}
 
diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php
index f9c2cd1388..fbb113178a 100644
--- a/phpBB/includes/db/postgres.php
+++ b/phpBB/includes/db/postgres.php
@@ -94,7 +94,7 @@ class dbal_postgres extends dbal
 			case 'begin':
 				$result = @pg_query($this->db_connect_id, 'BEGIN');
 				$this->transaction = true;
-				break;
+			break;
 
 			case 'commit':
 				$result = @pg_query($this->db_connect_id, 'COMMIT');
@@ -104,12 +104,12 @@ class dbal_postgres extends dbal
 				{
 					@pg_query($this->db_connect_id, 'ROLLBACK');
 				}
-				break;
+			break;
 
 			case 'rollback':
 				$result = @pg_query($this->db_connect_id, 'ROLLBACK');
 				$this->transaction = false;
-				break;
+			break;
 
 			default:
 				$result = true;
@@ -127,7 +127,6 @@ class dbal_postgres extends dbal
 		{
 			global $cache;
 
-
 			// EXPLAIN only in extra debug mode
 			if (defined('DEBUG_EXTRA'))
 			{
@@ -187,7 +186,7 @@ class dbal_postgres extends dbal
 	*/
 	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
 	{ 
-		if ($query != '') 
+		if ($query != '')
 		{
 			$this->query_result = false; 
 
@@ -200,11 +199,11 @@ class dbal_postgres extends dbal
 			$query .= "\n LIMIT $total OFFSET $offset";
 
 			return $this->sql_query($query, $cache_ttl); 
-		} 
-		else 
-		{ 
+		}
+		else
+		{
 			return false; 
-		} 
+		}
 	}
 
 	/**
@@ -266,7 +265,7 @@ class dbal_postgres extends dbal
 			{
 				$this->sql_rowseek($rownum, $query_id);
 			}
-			
+
 			$row = $this->sql_fetchrow($query_id);
 			return isset($row[$field]) ? $row[$field] : false;
 		}
@@ -301,6 +300,7 @@ class dbal_postgres extends dbal
 			{
 				$query = "SELECT currval('" . $tablename[1] . "_seq') AS last_value";
 				$temp_q_id =  @pg_query($this->db_connect_id, $query);
+
 				if (!$temp_q_id)
 				{
 					return false;
@@ -325,7 +325,7 @@ class dbal_postgres extends dbal
 		{
 			$query_id = $this->query_result;
 		}
-		
+
 		if (isset($this->open_queries[(int) $query_id]))
 		{
 			unset($this->open_queries[(int) $query_id]);
@@ -337,10 +337,10 @@ class dbal_postgres extends dbal
 
 	/**
 	* Escape string used in sql query
+	* Note: Do not use for bytea values if we may use them at a later stage
 	*/
 	function sql_escape($msg)
 	{
-		// Do not use for bytea values
 		return @pg_escape_string($msg);
 	}
 
diff --git a/phpBB/includes/db/sqlite.php b/phpBB/includes/db/sqlite.php
index 85c6986c79..0bfe9a40ca 100644
--- a/phpBB/includes/db/sqlite.php
+++ b/phpBB/includes/db/sqlite.php
@@ -61,7 +61,7 @@ class dbal_sqlite extends dbal
 			case 'begin':
 				$result = @sqlite_query('BEGIN', $this->db_connect_id);
 				$this->transaction = true;
-				break;
+			break;
 
 			case 'commit':
 				$result = @sqlite_query('COMMIT', $this->db_connect_id);
@@ -71,12 +71,12 @@ class dbal_sqlite extends dbal
 				{
 					@sqlite_query('ROLLBACK', $this->db_connect_id);
 				}
-				break;
+			break;
 
 			case 'rollback':
 				$result = @sqlite_query('ROLLBACK', $this->db_connect_id);
 				$this->transaction = false;
-				break;
+			break;
 
 			default:
 				$result = true;
@@ -94,7 +94,6 @@ class dbal_sqlite extends dbal
 		{
 			global $cache;
 
-
 			// EXPLAIN only in extra debug mode
 			if (defined('DEBUG_EXTRA'))
 			{
@@ -143,8 +142,8 @@ class dbal_sqlite extends dbal
 	* Build LIMIT query
 	*/
 	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 
-	{ 
-		if ($query != '') 
+	{
+		if ($query != '')
 		{
 			$this->query_result = false; 
 
@@ -157,11 +156,11 @@ class dbal_sqlite extends dbal
 			$query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
 
 			return $this->sql_query($query, $cache_ttl); 
-		} 
-		else 
-		{ 
-			return false; 
-		} 
+		}
+		else
+		{
+			return false;
+		}
 	}
 
 	/**
@@ -225,7 +224,7 @@ class dbal_sqlite extends dbal
 			}
 			else
 			{
-				@sqlite_seek($query_id, $rownum);
+				$this->sql_rowseek($rownum, $query_id);
 				return @sqlite_column($query_id, $field);
 			}
 		}
diff --git a/phpBB/includes/mcp/mcp_queue.php b/phpBB/includes/mcp/mcp_queue.php
index 9e7da838b5..c202637092 100644
--- a/phpBB/includes/mcp/mcp_queue.php
+++ b/phpBB/includes/mcp/mcp_queue.php
@@ -367,7 +367,10 @@ function approve_post($post_id_list, $mode)
 			$topic_id_list[$post_data['topic_id']] = 1;
 
 			// Topic or Post. ;)
-			if ($post_data['topic_first_post_id'] == $post_id && $post_data['topic_last_post_id'] == $post_id)
+			/**
+			* @todo What happens if a user posts a new topic which is not approved, then a second user replies and the post is automatically approved ;)
+			*/
+			if ($post_data['topic_first_post_id'] == $post_id/* && $post_data['topic_last_post_id'] == $post_id*/)
 			{
 				if ($post_data['forum_id'])
 				{
diff --git a/phpBB/language/en/groups.php b/phpBB/language/en/groups.php
index 93573e43c3..a7b34e67c1 100644
--- a/phpBB/language/en/groups.php
+++ b/phpBB/language/en/groups.php
@@ -67,10 +67,11 @@ $lang = array_merge($lang, array(
 
 	'LOGIN_EXPLAIN_GROUP'	=> 'You need to login to view group details',
 
-	'NO_LEADERS'			=> 'No group leaders defined',
-	'NOT_LEADER_OF_GROUP'	=> 'The requested operation cannot be taken because you are not a leader of the selected group.',
-	'NOT_MEMBER_OF_GROUP'	=> 'The requested operation cannot be taken because you are not a member of the selected group.',
-
+	'NO_LEADERS'					=> 'You are not a leader of any group',
+	'NOT_LEADER_OF_GROUP'			=> 'The requested operation cannot be taken because you are not a leader of the selected group.',
+	'NOT_MEMBER_OF_GROUP'			=> 'The requested operation cannot be taken because you are not a member of the selected group.',
+	'NOT_RESIGN_FROM_DEFAULT_GROUP'	=> 'You are not allowed to resign from your default group.',
+	
 	'PRIMARY_GROUP'		=> 'Primary group',
 
 	'REMOVE_SELECTED'		=> 'Remove selected',
diff --git a/phpBB/viewtopic.php b/phpBB/viewtopic.php
index 66535b6312..9738a22dfa 100644
--- a/phpBB/viewtopic.php
+++ b/phpBB/viewtopic.php
@@ -14,7 +14,7 @@
 define('IN_PHPBB', true);
 $phpbb_root_path = './';
 $phpEx = substr(strrchr(__FILE__, '.'), 1);
-include($phpbb_root_path . 'common.'.$phpEx);
+include($phpbb_root_path . 'common.' . $phpEx);
 include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
 
 // Start session management
@@ -29,7 +29,6 @@ $voted_id	= request_var('vote_id', array('' => 0));
 
 $start		= request_var('start', 0);
 $view		= request_var('view', '');
-$rate		= request_var('rate', 0);
 
 $sort_days	= request_var('st', ((!empty($user->data['user_post_show_days'])) ? $user->data['user_post_show_days'] : 0));
 $sort_key	= request_var('sk', ((!empty($user->data['user_post_sortby_type'])) ? $user->data['user_post_sortby_type'] : 't'));
@@ -37,7 +36,7 @@ $sort_dir	= request_var('sd', ((!empty($user->data['user_post_sortby_dir'])) ? $
 
 $update		= request_var('update', false);
 
-$hilit_words	= (isset($_REQUEST['hilit'])) ? request_var('hilit', '', true) : '';
+$hilit_words	= request_var('hilit', '', true);
 
 // Do we have a topic or post id?
 if (!$topic_id && !$post_id)
@@ -50,7 +49,8 @@ if ($view && !$post_id)
 {
 	if (!$forum_id)
 	{
-		$sql = 'SELECT forum_id FROM ' . TOPICS_TABLE . "
+		$sql = 'SELECT forum_id
+			FROM ' . TOPICS_TABLE . "
 			WHERE topic_id = $topic_id";
 		$result = $db->sql_query($sql);
 		$forum_id = (int) $db->sql_fetchfield('forum_id');
@@ -58,9 +58,6 @@ if ($view && !$post_id)
 
 		if (!$forum_id)
 		{
-			// Setup user environment so we can process lang string
-			$user->setup('viewtopic');
-
 			trigger_error('NO_TOPIC');
 		}
 	}
@@ -89,9 +86,10 @@ if ($view && !$post_id)
 			// Setup user environment so we can process lang string
 			$user->setup('viewtopic');
 
-			meta_refresh(3, "viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id");
-			$message = $user->lang['NO_UNREAD_POSTS'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], "<a href=\"viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id\">", '</a>');
-			trigger_error($message);
+			$redirect = "{$phpbb_root_path}viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id";
+
+			meta_refresh(3, $redirect);
+			trigger_error($user->lang['NO_UNREAD_POSTS'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect . '">', '</a>'));
 		}
 
 		$post_id = $row['post_id'];
@@ -114,11 +112,8 @@ if ($view && !$post_id)
 
 		if (!$row)
 		{
-			// Setup user environment so we can process lang string
 			$user->setup('viewtopic');
-
-			$message = ($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS';
-			trigger_error($message);
+			trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
 		}
 		else
 		{
@@ -127,9 +122,6 @@ if ($view && !$post_id)
 			// Check for global announcement correctness?
 			if (!$row['forum_id'] && !$forum_id)
 			{
-				// Setup user environment so we can process lang string
-				$user->setup('viewtopic');
-		
 				trigger_error('NO_TOPIC');
 			}
 			else if ($row['forum_id'])
@@ -142,9 +134,6 @@ if ($view && !$post_id)
 	// Check for global announcement correctness?
 	if ((!isset($row) || !$row['forum_id']) && !$forum_id)
 	{
-		// Setup user environment so we can process lang string
-		$user->setup('viewtopic');
-
 		trigger_error('NO_TOPIC');
 	}
 	else if (isset($row) && $row['forum_id'])
@@ -160,17 +149,16 @@ $sql_array = array(
 	'SELECT'	=> 't.*, f.*',
 
 	'FROM'		=> array(
-				FORUMS_TABLE	=> 'f',
+		FORUMS_TABLE	=> 'f',
 	)
 );
 
 if ($user->data['is_registered'])
 {
-	// We pop on left joins into the array for later insertion
-	$left_join = array();
-
 	$sql_array['SELECT'] .= ', tw.notify_status';
-	$left_join[] = array(
+	$sql_array['LEFT_JOIN'] = array();
+
+	$sql_array['LEFT_JOIN'][] = array(
 		'FROM'	=> array(TOPICS_WATCH_TABLE => 'tw'),
 		'ON'	=> 'tw.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tw.topic_id'
 	);
@@ -178,7 +166,7 @@ if ($user->data['is_registered'])
 	if ($config['allow_bookmarks'])
 	{
 		$sql_array['SELECT'] .= ', bm.order_id as bookmarked';
-		$left_join[] = array(
+		$sql_array['LEFT_JOIN'][] = array(
 			'FROM'	=> array(BOOKMARKS_TABLE => 'bm'),
 			'ON'	=> 'bm.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = bm.topic_id'
 		);
@@ -187,18 +175,17 @@ if ($user->data['is_registered'])
 	if ($config['load_db_lastread'])
 	{
 		$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
-		$left_join[] = array(
+		
+		$sql_array['LEFT_JOIN'][] = array(
 			'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
 			'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
 		);
-		$left_join[] = array(
+
+		$sql_array['LEFT_JOIN'][] = array(
 			'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
 			'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
 		);
 	}
-
-	// Insert the joins into the query array
-	$sql_array['LEFT_JOIN'] = $left_join;
 }
 
 if (!$post_id)
@@ -219,28 +206,26 @@ $sql_array['FROM'][TOPICS_TABLE] = 't';
 // is done so navigation, forum name, etc. remain consistent with where
 // user clicked to view a global topic
 $sql = $db->sql_build_query('SELECT', $sql_array);
-
 $result = $db->sql_query($sql);
+$topic_data = $db->sql_fetchrow($result);
+$db->sql_freeresult($result);
 
-if (!($topic_data = $db->sql_fetchrow($result)))
+if (!$topic_data)
 {
 	// If post_id was submitted, we try at least to display the topic as a last resort...
 	if ($post_id && $forum_id && $topic_id)
 	{
-		redirect("viewtopic.$phpEx$SID&f=$forum_id&t=$topic_id");
+		redirect("{$phpbb_root_path}viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id");
 	}
 
-	// Setup user environment so we can process lang string
-	$user->setup('viewtopic');
-
 	trigger_error('NO_TOPIC');
 }
 
 // This is for determining where we are (page)
 if ($post_id)
 {
-	$sql = "SELECT COUNT(p.post_id) AS prev_posts
-		FROM " . POSTS_TABLE . " p
+	$sql = 'SELECT COUNT(p.post_id) AS prev_posts
+		FROM ' . POSTS_TABLE . " p
 		WHERE p.post_approved = 1
 			AND p.topic_id = {$topic_data['topic_id']}
 			" . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . "
@@ -248,6 +233,7 @@ if ($post_id)
 	$result = $db->sql_query($sql);
 	$row = $db->sql_fetchrow($result);
 	$db->sql_freeresult($result);
+
 	$topic_data['prev_posts'] = $row['prev_posts'];
 }
 
@@ -282,7 +268,7 @@ if (!$auth->acl_get('f_read', $forum_id))
 {
 	if ($user->data['user_id'] != ANONYMOUS)
 	{
-		trigger_error($user->lang['SORRY_AUTH_READ']);
+		trigger_error('SORRY_AUTH_READ');
 	}
 
 	login_box('', $user->lang['LOGIN_VIEWFORUM']);
@@ -300,11 +286,11 @@ if (isset($_GET['e']))
 {
 	$jump_to = request_var('e', 0);
 
-	$redirect_url = "viewtopic.$phpEx$SID&f=$forum_id&t=$topic_id";
+	$redirect_url = "{$phpbb_root_path}viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id";
 
 	if ($user->data['user_id'] == ANONYMOUS)
 	{
-		login_box("{$phpbb_root_path}viewtopic.$phpEx?f=$forum_id&amp;t=$topic_id&amp;p=$post_id&amp;e=$jump_to", $user->lang['LOGIN_NOTIFY_TOPIC']);
+		login_box($redirect_url . "&amp;p=$post_id&amp;e=$jump_to", $user->lang['LOGIN_NOTIFY_TOPIC']);
 	}
 
 	if ($jump_to > 0)
@@ -357,13 +343,15 @@ if ($sort_days)
 			AND post_time >= $min_post_time
 		" . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1');
 	$result = $db->sql_query($sql);
+	$total_posts = (int) $db->sql_fetchfield('num_posts');
+	$db->sql_freeresult($result);
+
+	$limit_posts_time = "AND p.post_time >= $min_post_time ";
 
 	if (isset($_POST['sort']))
 	{
 		$start = 0;
 	}
-	$total_posts = ($row = $db->sql_fetchrow($result)) ? $row['num_posts'] : 0;
-	$limit_posts_time = "AND p.post_time >= $min_post_time ";
 }
 else
 {
@@ -427,8 +415,8 @@ if ($config['allow_bookmarks'] && $user->data['is_registered'] && request_var('b
 	// Re-Sort Bookmarks
 	$sql = 'UPDATE ' . BOOKMARKS_TABLE . "
 		SET order_id = order_id $sign 1
-			WHERE user_id = {$user->data['user_id']}
-			$where_sql";
+		WHERE user_id = {$user->data['user_id']}
+		$where_sql";
 	$db->sql_query($sql);
 
 	meta_refresh(3, $viewtopic_url);
@@ -597,6 +585,7 @@ if (!empty($topic_data['poll_start']))
 		if (isset($_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]))
 		{
 			$cur_voted_id = explode(',', $_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]);
+			$cur_voted_id = array_map('intval', $cur_voted_id);
 		}
 	}
 
@@ -611,10 +600,12 @@ if (!empty($topic_data['poll_start']))
 	{
 		if (!sizeof($voted_id) || sizeof($voted_id) > $topic_data['poll_max_options'])
 		{
-			meta_refresh(5, "viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id");
+			$redirect_url = "{$phpbb_root_path}viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id";
+
+			meta_refresh(5, $redirect_url);
 
 			$message = (!sizeof($voted_id)) ? 'NO_VOTE_OPTION' : 'TOO_MANY_VOTE_OPTIONS';
-			$message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], "<a href=\"viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id\">", '</a>');
+			$message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
 			trigger_error($message);
 		}
 
@@ -625,16 +616,22 @@ if (!empty($topic_data['poll_start']))
 				continue;
 			}
 
-			$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . "
+			$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
 				SET poll_option_total = poll_option_total + 1
-				WHERE poll_option_id = $option
-					AND topic_id = $topic_id";
+				WHERE poll_option_id = ' . (int) $option . '
+					AND topic_id = ' . (int) $topic_id;
 			$db->sql_query($sql);
 
 			if ($user->data['is_registered'])
 			{
-				$sql = 'INSERT INTO  ' . POLL_VOTES_TABLE . " (topic_id, poll_option_id, vote_user_id, vote_user_ip)
-					VALUES ($topic_id, $option, " . $user->data['user_id'] . ", '$user->ip')";
+				$sql_ary = array(
+					'topic_id'			=> (int) $topic_id,
+					'poll_option_id'	=> (int) $option,
+					'vote_user_id'		=> (int) $user->data['user_id'],
+					'vote_user_ip'		=> (string) $user->ip,
+				);
+				
+				$sql = 'INSERT INTO  ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
 				$db->sql_query($sql);
 			}
 		}
@@ -643,18 +640,18 @@ if (!empty($topic_data['poll_start']))
 		{
 			if (!in_array($option, $voted_id))
 			{
-				$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . "
+				$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
 					SET poll_option_total = poll_option_total - 1
-					WHERE poll_option_id = $option
-						AND topic_id = $topic_id";
+					WHERE poll_option_id = ' . (int) $option . '
+						AND topic_id = ' . (int) $topic_id;
 				$db->sql_query($sql);
 
 				if ($user->data['is_registered'])
 				{
-					$sql = 'DELETE FROM ' . POLL_VOTES_TABLE . "
-						WHERE topic_id = $topic_id
-							AND poll_option_id = $option
-							AND vote_user_id = " . $user->data['user_id'];
+					$sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
+						WHERE topic_id = ' . (int) $topic_id . '
+							AND poll_option_id = ' . (int) $option . '
+							AND vote_user_id = ' . (int) $user->data['user_id'];
 					$db->sql_query($sql);
 				}
 			}
@@ -671,10 +668,10 @@ if (!empty($topic_data['poll_start']))
 		//, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
 		$db->sql_query($sql);
 
-		meta_refresh(5, "viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id");
+		$redirect_url = "{$phpbb_root_path}viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id";
 
-		$message = $user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], "<a href=\"viewtopic.$phpEx$SID&amp;f=$forum_id&amp;t=$topic_id\">", '</a>');
-		trigger_error($message);
+		meta_refresh(5, $redirect_url);
+		trigger_error($user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>'));
 	}
 
 	$poll_total = 0;
@@ -685,7 +682,7 @@ if (!empty($topic_data['poll_start']))
 
 	if ($poll_info[0]['bbcode_bitfield'])
 	{
-		include_once($phpbb_root_path . 'includes/bbcode.'.$phpEx);
+		include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
 		$poll_bbcode = new bbcode();
 	}
 	else
@@ -793,13 +790,13 @@ $i = ($store_reverse) ? $sql_limit - 1 : 0;
 while ($row = $db->sql_fetchrow($result))
 {
 	$post_list[$i] = $row['post_id'];
-	($store_reverse) ? --$i : ++$i;
+	($store_reverse) ? $i-- : $i++;
 }
 $db->sql_freeresult($result);
 
 if (!sizeof($post_list))
 {
-	trigger_error($user->lang['NO_TOPIC']);
+	trigger_error('NO_TOPIC');
 }
 
 // Holding maximum post time for marking topic read
@@ -807,7 +804,7 @@ if (!sizeof($post_list))
 $max_post_time = 0;
 
 $sql = $db->sql_build_query('SELECT', array(
-	'SELECT'	=> 'u.username, u.user_id, u.user_colour, u.user_posts, u.user_from, u.user_website, u.user_email, u.user_icq, u.user_aim, u.user_yim, u.user_jabber, u.user_regdate, u.user_msnm, u.user_allow_viewemail, u.user_allow_viewonline, u.user_rank, u.user_sig, u.user_sig_bbcode_uid, u.user_sig_bbcode_bitfield, u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height, u.user_warnings, z.friend, z.foe, p.*',
+	'SELECT'	=> 'u.*, z.friend, z.foe, p.*',
 
 	'FROM'		=> array(
 		USERS_TABLE		=> 'u',
@@ -822,7 +819,7 @@ $sql = $db->sql_build_query('SELECT', array(
 	),
 
 	'WHERE'		=> 'p.post_id IN (' . implode(', ', $post_list) . ')
-						AND u.user_id = p.poster_id'
+		AND u.user_id = p.poster_id'
 ));
 
 $result = $db->sql_query($sql);
@@ -948,7 +945,7 @@ while ($row = $db->sql_fetchrow($result))
 			}
 
 			$id_cache[] = $poster_id;
-			
+
 			$user_cache[$poster_id] = array(
 				'joined'		=> $user->format_date($row['user_regdate']),
 				'posts'			=> $row['user_posts'],
@@ -981,20 +978,21 @@ while ($row = $db->sql_fetchrow($result))
 				{
 					case AVATAR_UPLOAD:
 						$avatar_img = $config['avatar_path'] . '/';
-						break;
+					break;
+
 					case AVATAR_GALLERY:
 						$avatar_img = $config['avatar_gallery_path'] . '/';
-						break;
+					break;
 				}
 				$avatar_img .= $row['user_avatar'];
 
-				$user_cache[$poster_id]['avatar'] = '<img src="' . $avatar_img . '" width="' . $row['user_avatar_width'] . '" height="' . $row['user_avatar_height'] . '" border="0" alt="" />';
+				$user_cache[$poster_id]['avatar'] = '<img src="' . $avatar_img . '" width="' . $row['user_avatar_width'] . '" height="' . $row['user_avatar_height'] . '" alt="" />';
 			}
 
 			if (!empty($row['user_rank']))
 			{
 				$user_cache[$poster_id]['rank_title'] = (isset($ranks['special'][$row['user_rank']])) ? $ranks['special'][$row['user_rank']]['rank_title'] : '';
-				$user_cache[$poster_id]['rank_image'] = (!empty($ranks['special'][$row['user_rank']]['rank_image'])) ? '<img src="' . $config['ranks_path'] . '/' . $ranks['special'][$row['user_rank']]['rank_image'] . '" border="0" alt="' . $ranks['special'][$row['user_rank']]['rank_title'] . '" title="' . $ranks['special'][$row['user_rank']]['rank_title'] . '" /><br />' : '';
+				$user_cache[$poster_id]['rank_image'] = (!empty($ranks['special'][$row['user_rank']]['rank_image'])) ? '<img src="' . $config['ranks_path'] . '/' . $ranks['special'][$row['user_rank']]['rank_image'] . '" alt="' . $ranks['special'][$row['user_rank']]['rank_title'] . '" title="' . $ranks['special'][$row['user_rank']]['rank_title'] . '" /><br />' : '';
 				$user_cache[$poster_id]['rank_image_src'] = (!empty($ranks['special'][$row['user_rank']]['rank_image'])) ? $config['ranks_path'] . '/' . $ranks['special'][$row['user_rank']]['rank_image'] : '';
 			}
 			else
@@ -1006,7 +1004,7 @@ while ($row = $db->sql_fetchrow($result))
 						if ($row['user_posts'] >= $rank['rank_min'])
 						{
 							$user_cache[$poster_id]['rank_title'] = $rank['rank_title'];
-							$user_cache[$poster_id]['rank_image'] = (!empty($rank['rank_image'])) ? '<img src="' . $config['ranks_path'] . '/' . $rank['rank_image'] . '" border="0" alt="' . $rank['rank_title'] . '" title="' . $rank['rank_title'] . '" /><br />' : '';
+							$user_cache[$poster_id]['rank_image'] = (!empty($rank['rank_image'])) ? '<img src="' . $config['ranks_path'] . '/' . $rank['rank_image'] . '" alt="' . $rank['rank_title'] . '" title="' . $rank['rank_title'] . '" /><br />' : '';
 							$user_cache[$poster_id]['rank_image_src'] = (!empty($rank['rank_image'])) ? $config['ranks_path'] . '/' . $rank['rank_image'] : '';
 							break;
 						}
@@ -1032,7 +1030,7 @@ while ($row = $db->sql_fetchrow($result))
 			if (!empty($row['user_icq']))
 			{
 				$user_cache[$poster_id]['icq'] =  "{$phpbb_root_path}memberlist.$phpEx$SID&amp;mode=contact&amp;action=icq&amp;u=$poster_id";
-				$user_cache[$poster_id]['icq_status_img'] = '<img src="http://web.icq.com/whitepages/online?icq=' . $row['user_icq'] . '&amp;img=5" width="18" height="18" border="0" />';
+				$user_cache[$poster_id]['icq_status_img'] = '<img src="http://web.icq.com/whitepages/online?icq=' . $row['user_icq'] . '&amp;img=5" width="18" height="18" alt="" />';
 			}
 			else
 			{
@@ -1108,8 +1106,10 @@ if (sizeof($attach_list))
 						AND p.post_approved = 1
 						AND p.topic_id = a.topic_id";
 				$result = $db->sql_query_limit($sql, 1);
+				$row = $db->sql_fetchrow($result);
+				$db->sql_freeresult($result);
 
-				if (!$db->sql_fetchrow($result))
+				if (!$row)
 				{
 					$sql = 'UPDATE ' . TOPICS_TABLE . "
 						SET topic_attachment = 0
@@ -1145,7 +1145,7 @@ if (sizeof($attach_list))
 // Instantiate BBCode if need be
 if ($bbcode_bitfield)
 {
-	include_once($phpbb_root_path . 'includes/bbcode.'.$phpEx);
+	include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
 	$bbcode = new bbcode($bbcode_bitfield);
 }
 
@@ -1163,15 +1163,14 @@ for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
 	$row =& $rowset[$post_list[$i]];
 	$poster_id = $row['user_id'];
 
-	// Three situations can prevent a post being display:
-	// i)   The posters karma is below the minimum of the user ... not in 2.2.x
-	// ii)  The poster is on the users ignore list
-	// iii) The post was made in a codepage different from the users
+	// Two situations can prevent a post being display:
+	// i)  The poster is on the users ignore list
+	// ii) The post was made in a codepage different from the users
 	if (isset($row['foe']) && $row['foe'])
 	{
 		$template->assign_block_vars('postrow', array(
 			'S_IGNORE_POST' => true,
-			'L_IGNORE_POST' => sprintf($user->lang['POST_BY_FOE'], $row['poster'], '<a href="' . $viewtopic_url . '&amp;p=' . $row['post_id'] . '&amp;view=show#p' . $row['post_id'] . '">', '</a>'))
+			'L_IGNORE_POST' => sprintf($user->lang['POST_BY_FOE'], $row['poster'], '<a href="' . $viewtopic_url . "&amp;p={$row['post_id']}&amp;view=show#p{$row['post_id']}" . '">', '</a>'))
 		);
 
 		continue;
@@ -1186,7 +1185,7 @@ for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
 		{
 			$template->assign_block_vars('postrow', array(
 				'S_IGNORE_POST'	=> true,
-				'L_IGNORE_POST'	=> sprintf($user->lang['POST_ENCODING'], $row['poster'], '<a href="' . $viewtopic_url . '&amp;p=' . $row['post_id'] . '&amp;view=encoding#p' . $row['post_id'] . '">', '</a>'))
+				'L_IGNORE_POST'	=> sprintf($user->lang['POST_ENCODING'], $row['poster'], '<a href="' . $viewtopic_url . "&amp;p={$row['post_id']}&amp;view=encoding#p{$row['post_id']}" . '">', '</a>'))
 			);
 
 			continue;
@@ -1323,37 +1322,37 @@ for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
 		'POSTER_AVATAR'		=> $user_cache[$poster_id]['avatar'],
 		'POSTER_WARNINGS'	=> $user_cache[$poster_id]['warnings'],
 
-		'POST_DATE' 	=> $user->format_date($row['post_time']),
-		'POST_SUBJECT' 	=> $row['post_subject'],
-		'MESSAGE' 		=> $message,
-		'SIGNATURE' 	=> ($row['enable_sig']) ? $user_cache[$poster_id]['sig'] : '',
-		'EDITED_MESSAGE'=> $l_edited_by,
-		'EDIT_REASON'	=> $row['post_edit_reason'],
-		'BUMPED_MESSAGE'=> $l_bumped_by,
+		'POST_DATE'			=> $user->format_date($row['post_time']),
+		'POST_SUBJECT'		=> $row['post_subject'],
+		'MESSAGE'			=> $message,
+		'SIGNATURE'			=> ($row['enable_sig']) ? $user_cache[$poster_id]['sig'] : '',
+		'EDITED_MESSAGE'	=> $l_edited_by,
+		'EDIT_REASON'		=> $row['post_edit_reason'],
+		'BUMPED_MESSAGE'	=> $l_bumped_by,
 
-		'MINI_POST_IMG'		=> ($post_unread) ? $user->img('icon_post_new', 'NEW_POST') : $user->img('icon_post', 'POST'),
-		'POST_ICON_IMG'		=> (!empty($row['icon_id'])) ? $icons[$row['icon_id']]['img'] : '',
+		'MINI_POST_IMG'			=> ($post_unread) ? $user->img('icon_post_new', 'NEW_POST') : $user->img('icon_post', 'POST'),
+		'POST_ICON_IMG'			=> (!empty($row['icon_id'])) ? $icons[$row['icon_id']]['img'] : '',
 		'POST_ICON_IMG_WIDTH'	=> (!empty($row['icon_id'])) ? $icons[$row['icon_id']]['width'] : '',
 		'POST_ICON_IMG_HEIGHT'	=> (!empty($row['icon_id'])) ? $icons[$row['icon_id']]['height'] : '',
-		'ICQ_STATUS_IMG'	=> $user_cache[$poster_id]['icq_status_img'],
-		'ONLINE_IMG'		=> ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? '' : (($user_cache[$poster_id]['online']) ? $user->img('btn_online', 'ONLINE') : $user->img('btn_offline', 'OFFLINE')),
-		'S_ONLINE'	=> ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? false : (($user_cache[$poster_id]['online']) ? true : false),
+		'ICQ_STATUS_IMG'		=> $user_cache[$poster_id]['icq_status_img'],
+		'ONLINE_IMG'			=> ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? '' : (($user_cache[$poster_id]['online']) ? $user->img('btn_online', 'ONLINE') : $user->img('btn_offline', 'OFFLINE')),
+		'S_ONLINE'				=> ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? false : (($user_cache[$poster_id]['online']) ? true : false),
 
-		'U_EDIT' 			=> (($user->data['user_id'] == $poster_id && $auth->acl_get('f_edit', $forum_id) && ($row['post_time'] > time() - $config['edit_time'] || !$config['edit_time'])) || $auth->acl_get('m_edit', $forum_id)) ? "posting.$phpEx$SID&amp;mode=edit&amp;f=$forum_id&amp;p=" . $row['post_id'] : '',
-		'U_QUOTE' 			=> ($auth->acl_get('f_reply', $forum_id)) ? "posting.$phpEx$SID&amp;mode=quote&amp;f=$forum_id&amp;p=" . $row['post_id'] : '',
-		'U_INFO'			=> ($auth->acl_get('m_info', $forum_id)) ? "mcp.$phpEx$SID&amp;i=main&amp;mode=post_details&amp;p=" . $row['post_id'] : '',
-		'U_DELETE' 			=> (($user->data['user_id'] == $poster_id && $auth->acl_get('f_delete', $forum_id) && $topic_data['topic_last_post_id'] == $row['post_id'] && ($row['post_time'] > time() - $config['edit_time'] || !$config['edit_time'])) || $auth->acl_get('m_delete', $forum_id)) ? "posting.$phpEx$SID&amp;mode=delete&amp;f=$forum_id&amp;p=" . $row['post_id'] : '',
+		'U_EDIT'			=> (($user->data['user_id'] == $poster_id && $auth->acl_get('f_edit', $forum_id) && ($row['post_time'] > time() - $config['edit_time'] || !$config['edit_time'])) || $auth->acl_get('m_edit', $forum_id)) ? "{$phpbb_root_path}posting.$phpEx$SID&amp;mode=edit&amp;f=$forum_id&amp;p={$row['post_id']}" : '',
+		'U_QUOTE'			=> ($auth->acl_get('f_reply', $forum_id)) ? "{$phpbb_root_path}posting.$phpEx$SID&amp;mode=quote&amp;f=$forum_id&amp;p={$row['post_id']}" : '',
+		'U_INFO'			=> ($auth->acl_get('m_info', $forum_id)) ? "{$phpbb_root_path}mcp.$phpEx$SID&amp;i=main&amp;mode=post_details&amp;p={$row['post_id']}" : '',
+		'U_DELETE'			=> (($user->data['user_id'] == $poster_id && $auth->acl_get('f_delete', $forum_id) && $topic_data['topic_last_post_id'] == $row['post_id'] && ($row['post_time'] > time() - $config['edit_time'] || !$config['edit_time'])) || $auth->acl_get('m_delete', $forum_id)) ? "{$phpbb_root_path}posting.$phpEx$SID&amp;mode=delete&amp;f=$forum_id&amp;p={$row['post_id']}" : '',
 
-		'U_PROFILE' 		=> $user_cache[$poster_id]['profile'],
-		'U_SEARCH' 			=> $user_cache[$poster_id]['search'],
-		'U_PM' 				=> ($poster_id != ANONYMOUS) ? "{$phpbb_root_path}ucp.$phpEx$SID&amp;i=pm&amp;mode=compose&amp;action=quotepost&amp;p=" . $row['post_id'] : '',
-		'U_EMAIL' 			=> $user_cache[$poster_id]['email'],
-		'U_WWW' 			=> $user_cache[$poster_id]['www'],
-		'U_ICQ' 			=> $user_cache[$poster_id]['icq'],
-		'U_AIM' 			=> $user_cache[$poster_id]['aim'],
-		'U_MSN' 			=> $user_cache[$poster_id]['msn'],
-		'U_YIM' 			=> $user_cache[$poster_id]['yim'],
-		'U_JABBER'			=> $user_cache[$poster_id]['jabber'],
+		'U_PROFILE'		=> $user_cache[$poster_id]['profile'],
+		'U_SEARCH'		=> $user_cache[$poster_id]['search'],
+		'U_PM'			=> ($poster_id != ANONYMOUS) ? "{$phpbb_root_path}ucp.$phpEx$SID&amp;i=pm&amp;mode=compose&amp;action=quotepost&amp;p={$row['post_id']}" : '',
+		'U_EMAIL'		=> $user_cache[$poster_id]['email'],
+		'U_WWW'			=> $user_cache[$poster_id]['www'],
+		'U_ICQ'			=> $user_cache[$poster_id]['icq'],
+		'U_AIM'			=> $user_cache[$poster_id]['aim'],
+		'U_MSN'			=> $user_cache[$poster_id]['msn'],
+		'U_YIM'			=> $user_cache[$poster_id]['yim'],
+		'U_JABBER'		=> $user_cache[$poster_id]['jabber'],
 
 		'U_REPORT'			=> ($auth->acl_get('f_report', $forum_id)) ? "{$phpbb_root_path}report.$phpEx$SID&amp;p=" . $row['post_id'] : '',
 		'U_MCP_REPORT'		=> ($auth->acl_gets('m_report', $forum_id)) ? "{$phpbb_root_path}mcp.$phpEx$SID&amp;i=reports&amp;mode=report_details&amp;p=" . $row['post_id'] : '',
@@ -1361,12 +1360,12 @@ for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
 		'U_MINI_POST'		=> "{$phpbb_root_path}viewtopic.$phpEx$SID&amp;p=" . $row['post_id'] . '#p' . $row['post_id'],
 		'U_NEXT_POST_ID'	=> ($i < $i_total && isset($rowset[$post_list[$i + 1]])) ? $rowset[$post_list[$i + 1]]['post_id'] : '',
 		'U_PREV_POST_ID'	=> $prev_post_id,
-		'U_NOTES'			=> ($auth->acl_gets('m_', 'a_')) ? "{$phpbb_root_path}mcp.$phpEx$SID&amp;i=notes&amp;mode=user_notes&amp;u=" . $poster_id : '',
+		'U_NOTES'			=> ($auth->acl_gets('m_', 'a_', $forum_id)) ? "{$phpbb_root_path}mcp.$phpEx$SID&amp;i=notes&amp;mode=user_notes&amp;u=" . $poster_id : '',
 		'U_WARN'			=> ($auth->acl_gets('m_', 'a_', $forum_id)) ? "{$phpbb_root_path}mcp.$phpEx$SID&amp;i=warn&amp;mode=warn_post&amp;p=" . $row['post_id'] : '',
 
 		'POST_ID'			=> $row['post_id'],
 
-		'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
+		'S_HAS_ATTACHMENTS'	=> (!empty($attachments[$row['post_id']])) ? true : false,
 		'S_POST_UNAPPROVED'	=> ($row['post_approved']) ? false : true,
 		'S_POST_REPORTED'	=> ($row['post_reported'] && $auth->acl_get('m_report', $forum_id)) ? true : false,
 		'S_DISPLAY_NOTICE'	=> $display_notice && $row['post_attachment'],
@@ -1375,7 +1374,7 @@ for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
 		'S_FIRST_UNREAD'	=> $s_first_unread,
 		'S_CUSTOM_FIELDS'	=> (isset($cp_row['row']) && sizeof($cp_row['row'])) ? true : false
 	);
-	
+
 	if (isset($cp_row['row']) && sizeof($cp_row['row']))
 	{
 		$postrow = array_merge($postrow, $cp_row['row']);
@@ -1384,7 +1383,7 @@ for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
 	// Dump vars into template
 	$template->assign_block_vars('postrow', $postrow);
 
-	if (isset($cp_row['blockrow']) && sizeof($cp_row['blockrow']))
+	if (!empty($cp_row['blockrow']))
 	{
 		foreach ($cp_row['blockrow'] as $field_data)
 		{
@@ -1393,7 +1392,7 @@ for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
 	}
 
 	// Display not already displayed Attachments for this post, we already parsed them. ;)
-	if (isset($attachments[$row['post_id']]) && sizeof($attachments[$row['post_id']]))
+	if (!empty($attachments[$row['post_id']]))
 	{
 		foreach ($attachments[$row['post_id']] as $attachment)
 		{
@@ -1408,12 +1407,10 @@ for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
 	unset($rowset[$post_list[$i]]);
 	unset($attachments[$row['post_id']]);
 }
-unset($rowset);
-unset($user_cache);
+unset($rowset, $user_cache);
 
-// Update topic view and if necessary attachment view counters ... but only
-// if this is the first 'page view'
-if (isset($user->data['session_page']) && !preg_match("#&t=$topic_id#", $user->data['session_page']))
+// Update topic view and if necessary attachment view counters ... but only if this is the first 'page view'
+if (isset($user->data['session_page']) && strpos($user->data['session_page'], '&t=' . $topic_id) === false)
 {
 	$sql = 'UPDATE ' . TOPICS_TABLE . '
 		SET topic_views = topic_views + 1, topic_last_view_time = ' . time() . "
@@ -1448,7 +1445,7 @@ page_header($user->lang['VIEW_TOPIC'] .' - ' . $topic_data['topic_title']);
 $template->set_filenames(array(
 	'body' => ($view == 'print') ? 'viewtopic_print.html' : 'viewtopic_body.html')
 );
-make_jumpbox('viewforum.'.$phpEx, $forum_id);
+make_jumpbox('viewforum.' . $phpEx, $forum_id);
 
 page_footer();