<?php
switch($uebergabe3)
	{
		case 1:
			echo "<table align=center><tr><td><b>Answer deleted!</b></td></tr></table>";
			break;
		default:
			break;
	}	

echo "<br />";
$tbl_name="forum_question"; // Table name

// Connect to server and select database.
include ($_SERVER["DOCUMENT_ROOT"]."/Config/_dbconfig_.php");
@$y = mysql_connect($MYSQL_HOST,$MYSQL_USER,$MYSQL_PASS);
@$x = mysql_select_db($MYSQL_DATABASE);
if (empty($x)) 
{ 
  die ("Keine Verbindung zur Datenbank! [<b><font color=red>FAIL</b></font>]<br />"); 
} 
// get value of id that sent from address bar
$id=mysql_real_escape_string($uebergabe2);

$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
$settings = $rows;
if (($_SESSION["user_admin"]=="1" && $rows["r_adminonly"] == "1") || 
        $rows["r_guest"] == "1" || 
        ($rows["r_adminonly"] == "0" &&(isset ($_SESSION["user_id"])))) 
{ 
	if($rows["like"] == "")
	{
		$rows["like"] = "0";
	}
	if($rows["dislike"] == "")
	{
		$rows["dislike"] = "0";
	}

	if(isset($_GET['success']))
	{
    	echo "<table align=center><tr><td><b>Answer successfully created!</b></td></tr></table>";
	}
	echo "<table width=\"800\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\">
			<tr>
				<td valgin=top  height=\"100%\">
				    <table style=\"width: 100%; height: 100%;\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bordercolor=\"1\">
				        <tr>
				            <td valign=\"top\">
				                <a href=".$moduleurl."&uebergabe=0&uebergabe2=0&uebergabe3=0&uebergabe4=0><b># Forum Home</b></a>
				            </td>
				        </tr>
				        <tr>
				            <td valign=\"bottom\">
				                <a href=".$moduleurl."&uebergabe=0&uebergabe2=0&uebergabe3=0&uebergabe4=0><b># Forum Home</b></a>
				            </td>
				        </tr>
				    </table>
				</td>
				<td>
				    <table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\">
				        <tr>
				            <td width=\"90\">";
	$sql = "SELECT Avatar FROM userdata WHERE Nickname = '".$rows["name"]."'";
    $result = mysql_query ($sql); 
    $Avatar=mysql_fetch_array ($result);
    echo"<img src=/Modules/Forum/avatar/".$Avatar[0].".png width=\"90\" height=\"90\"></img>\n";
	echo "    				</td>
                    		<td>
                    			<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bordercolor=\"1\" bgcolor=\"grey\">
                        			<tr>
                            			<td bgcolor=\"lightgrey\"><strong>".$rows['topic']."</strong></td>
                        			</tr>
                        			<tr>
                            			<td bgcolor=\"lightgrey\"><font color=green>". $rows['detail'] ."</font></td>
                        			</tr>
                        			<tr>
                            			<td bgcolor=\"lightgrey\">
											<table width=100%>
												<tr>
													<td>
														<strong>By:</strong>".$rows['name']."<strong> Date: </strong>".$rows['datetime']."
													</td>
													<td align=\"right\">
														<strong>";
														if(isset($_SESSION["user_id"]))
														{
															$sql = "SELECT * FROM forum_like WHERE question = '".$id."' AND answer = '0'";
   			 												$result = mysql_query ($sql); 
															$liked = false;
															$disliked = false;
															while($Like=mysql_fetch_array($result))
															{
																if($Like[2]==$_SESSION["user_nickname"])
																{													
																	$liked = true;
																	$disliked = true;
																}
															}
															
															if(!$liked)
																echo $rows['like']."<a href=\"/Modules/Forum/include/like.php?user=".$_SESSION["user_nickname"]."&path=".$moduleurl."&topic=".$id."&answer=0\"><img src=/Modules/Forum/img/likekl.png width=20 height=20 border=0 /></a>";
															else
																echo $rows['like']."<img src=/Modules/Forum/img/likegreykl.png width=20 height=20 border=0 />";
															if(!$disliked)
																echo $rows['dislike']."<a href=\"/Modules/Forum/include/dislike.php?user=".$_SESSION["user_nickname"]."&path=".$moduleurl."&topic=".$id."&answer=0\"><img src=/Modules/Forum/img/dislikekl.png width=20 height=20 border=0 /></a>";
															else
																echo $rows['dislike']."<img src=/Modules/Forum/img/dislikegreykl.png width=20 height=20 border=0 />";
														}
														else
														{
															echo $rows['like']."<img src=/Modules/Forum/img/likegreykl.png width=20 height=20 border=0 />".
														$rows['dislike']."<img src=/Modules/Forum/img/dislikegreykl.png width=20 height=20 border=0 />";
														}
														echo "</strong>
													</td>
												</tr>
											</table>
										</td>
                        			</tr>	
                    			</table>
                    			<hr />
                    		</td>
                		</tr>
            		</table>    
    				<br />";
	$tbl_name2="forum_answer"; // Switch to table "forum_answer"
	$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id' ORDER BY a_id ASC";
	$result2=mysql_query($sql2);

	while($rows=mysql_fetch_array($result2))
	{
		echo "
        		    <table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\">
        	        <tr>
        	            <td width=\"90\">";

        $sql = "SELECT Avatar FROM userdata WHERE Nickname = '".$rows["a_name"]."'";
        $result = mysql_query ($sql); 
        $Avatar=mysql_fetch_array ($result);
		if($rows["a_name"] == "")
		{
			$rows["a_name"] = "guest";
		}
		
		if($rows["a_like"] == "")
		{
			$rows["a_like"] = "0";
		}
		if($rows["a_dislike"] == "")
		{
			$rows["a_dislike"] = "0";
		}

        echo"				<img src=/Modules/Forum/avatar/".$Avatar[0].".png width=\"90\" height=\"90\"></img>\n
                    	</td>
                    	<td>
                    		<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"lightgrey\">
		                    	<tr>
		                        	<td bgcolor=\"#F8F7F1\"><strong>#". $rows['a_id']."</strong> , <strong>by:</strong>".$rows['a_name'];
									
									if(($_SESSION["user_nickname"] == $rows["a_name"] && $settings["closed"] != 1)|| $_SESSION["user_admin"]=="1")
									{
										echo "<table align=\"right\"><td>";
										echo "<a href=\"/Modules/Forum/delete_answer.php?topic=".$id."&answer=".$rows['a_id']."&path=".$moduleurl."\"><font color=\"red\">X</font></a>";			
										echo "</td></table>";		
									}						
									echo "</td>									
		                    	</tr>
		                    	<tr>
		                        	<td bgcolor=\"#F8F7F1\"><font color=green>". $rows['a_answer']. "</font></td>
		                   		</tr>";

	if($rows['a_attachment'] != "")
	{
		$pos1 = strpos($rows['a_attachment'], "-");
		$pos2 = strpos($rows['a_attachment'], "-",$pos1+1);
		$attname = substr($rows['a_attachment'], $pos2+1);
		echo "
								<tr>
		                        	<td bgcolor=\"#F8F7F1\"><strong>Attachment:</strong> 
									<a href=\"/Modules/Forum/getattachment.php?download=".$rows['a_attachment']. "\">".$attname."</a></font></td>
		                   		</tr>";
	}
								echo "
		                    	<tr>
		                        	<td bgcolor=\"#F8F7F1\">
										<table width=100%>
											<tr>
												<td>					
													<strong>Date:</strong> ".$rows['a_datetime']."
												</td>
												<td align=\"right\">
													<strong>";
													if(isset($_SESSION["user_id"]))
													{
															$sql = "SELECT * FROM forum_like WHERE question = '".$id."' AND answer = '".$rows["a_id"]."'";
   			 												$result = mysql_query ($sql); 
															$liked = false;
															$disliked = false;
															while($Like=mysql_fetch_array($result))
															{
																if($Like[2]==$_SESSION["user_nickname"])
																{		
																	$liked = true;
																	$disliked = true;
																}
															}
															
															if(!$liked)
																echo $rows['a_like']."<a href=\"/Modules/Forum/include/like.php?user=".$_SESSION["user_nickname"]."&path=".$moduleurl."&topic=".$id."&answer=".$rows["a_id"]."\"><img src=/Modules/Forum/img/likekl.png width=20 height=20 border=0 /></a>";
															else
																echo $rows['a_like']."<img src=/Modules/Forum/img/likegreykl.png width=20 height=20 border=0 />";
															if(!$disliked)
																echo $rows['a_dislike']."<a href=\"/Modules/Forum/include/dislike.php?user=".$_SESSION["user_nickname"]."&path=".$moduleurl."&topic=".$id."&answer=".$rows["a_id"]."\"><img src=/Modules/Forum/img/dislikekl.png width=20 height=20 border=0 /></a>";
															else
																echo $rows['a_dislike']."<img src=/Modules/Forum/img/dislikegreykl.png width=20 height=20 border=0 />";
													}
													else
														{
															echo $rows['a_like']."<img src=/Modules/Forum/img/likegreykl.png width=20 height=20 border=0 />".
														$rows['a_dislike']."<img src=/Modules/Forum/img/dislikegreykl.png width=20 height=20 border=0 />";
														}
													echo "
													</strong>
												</td>
											</tr>
										</table>
									</td>
		                    	</tr>
                    		</table>
                    	</td>
                	</tr>
            	</table>
    			<br />";
	}
	
	$sql3="SELECT view FROM $tbl_name WHERE id='$id'";
	$result3=mysql_query($sql3);

	$rows=mysql_fetch_array($result3);
	$view=$rows['view'];

	// if have no counter value set counter = 1
	if(empty($view))
	{
		$view=1;
		$sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";
		$result4=mysql_query($sql4);
	}

	// count more value
	$addview=$view+1;
	$sql5="update $tbl_name set view='$addview' WHERE id='$id'";
	$result5=mysql_query($sql5);

	mysql_close();
	echo "
		    </td>
		</tr>
	</table>
	<br />";

	if (((isset ($_SESSION["user_id"]) && $settings["w_adminonly"]== "0") || 
		        $settings["w_guest"]=="1" || 
		        ($_SESSION["user_admin"]=="1"&&$settings["w_adminonly"]== "1")) 
		    && $settings["closed"] != "1") 
	{ 
		echo"
		<table style=\"width: 400\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#CCCCCC\">
			<tr>
				<td>
					<form name=\"form1\" method=\"post\" action=\"/Modules/Forum/add_answer.php\" enctype=\"multipart/form-data\">
						<table style=\"width: 100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#FFFFFF\">
							<tr>
								<td valign=\"top\"><strong>Answer</strong></td>
								<td valign=\"top\">:</td>
								<td><textarea name=\"a_answer\" cols=\"45\" rows=\"3\" id=\"a_answer\"></textarea></td>
							</tr>";
		if (isset ($_SESSION["user_id"]))
		{
			echo "
							<tr>
								<td valign=\"top\"><strong>Attachment</strong></td>
								<td valign=\"top\">:</td>
								<td><input type=\"file\" name=\"datei\" /></td>
							</tr>";
		}
		echo "
							<tr>
								<td>&nbsp;</td>
								<td><input name=\"id\" type=\"hidden\" value=".$id." /><input name=\"path\" type=\"hidden\" value=".$moduleurl." /></td>
								<td><input type=\"submit\" name=\"Submit\" value=\"Submit\" /> <input type=\"reset\" name=\"Submit2\" value=\"Reset\" /></td>
							</tr>
						</table>
					</form>				
				</td>
			</tr>
		</table>";
	}
}
?>
