
<?php
session_start();
//datubāzes konfigurācija, saglabājot tos datus mainīgajos
$dbservername = "localhost";
$dbusername = "gamizoge_admins";
$dbpassword = "^uuem[j6kjJ#";
$dbname = "gamizoge_Forums";

// pieslēgties datubāzei
$db = new mysqli($dbservername, $dbusername, $dbpassword, $dbname);
//saglabā esošās sesijas lietotājvārdu mainīgajā
$username = $_SESSION['username'];

// atlasa kolumnu verified no tabulas users kur lietotājvārds ir saglabātais esošais mainīgais
$sql = "SELECT verified FROM users WHERE username='$username'";
$result = $db->query($sql);
$row = $result->fetch_assoc();
// ja tabulas dati no verified NAV 'Y', tad sesiju pamet, noņem nost esošās sesijas lietotājvārdu un aiziet atpakaļ uz login.php lapu
if ($row["verified"] !== 'Y')
{
    session_destroy();
    unset($_SESSION['username']);
    header("location: login.php");
}   // ja nav lietotājvārds, tad parāda ziņu un aiziet uz lapu login.php
if (!isset($_SESSION['username']))
{
    $_SESSION['msg'] = "Jums no sākuma jāpieslēdzas.";
    header('location: login.php');
}
// ja piespiesta poga logout, tad sesiju pamet, noņem nost esošās sesijas lietotājvārdu un aiziet atpakaļ uz login.php lapu
if (isset($_GET['logout']))
{
    session_destroy();
    unset($_SESSION['username']);
    header("location: login.php");
}

?>
<!DOCTYPE html>
<html>
<title>GAMINGFOREVER</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<style>
body,h1,h2,h3,h4,h5 {font-family: "Raleway", sans-serif}
</style>
<body class="w3-light-grey" style="background-image: url('https://images.hdqwalls.com/download/colorful-galaxy-planets-4k-ih-2560x1700.jpg'); background-repeat: no-repeat;
  background-size: 100% 100%;
">
<header class="w3-container w3-dark-grey w3-padding-32 w3-margin-top" style="margin-top:0px!important; text-align: center; background-color: black!important;padding-top: 0px!important;padding-bottom: 0px!important;">
 	<h1 style="font-size: 100px;margin: 0px 0px 0px 0px;">GAMINGFOREVER</h1>
</header>
<header class="w3-container w3-dark-grey w3-padding-32 w3-margin-top" style="margin-top:0px!important; text-align: center; background-color: black!important;padding-top: 0px!important;padding-bottom: 0px!important;">
 	<ul style="list-style-type: none;margin-bottom:10px!important;margin-top:0px!important;padding-left: 0px!important;">
  <li style="display: inline;font-size: 20px;padding-right: 20px!important;"><a href="index.php">SĀKUMLAPA</a></li>
  <li style="display: inline;font-size: 20px;padding-right: 20px!important;"><a href="forumi.php">FORUMI</a></li>
  <li style="display: inline;font-size: 20px;padding-right: 20px!important;"><a href="noteikumi.php">NOTEIKUMI</a></li>
  <li style="display: inline;font-size: 20px;padding-right: 20px!important;"><a href="parmums.php">PAR MUMS</a></li>
  <li style="display: inline;font-size: 20px;padding-right: 20px!important;"><a href="kontakti.php">KONTAKTI</a></li>
</ul>
</header>
<body class="w3-light-grey">
<div class="w3-content" style="max-width:1400px">
<div class="w3-row">
<!-- Blog entries -->
<div class="w3-col l8 s12">
  <!-- Blog entry -->
  <div class="w3-card-4 w3-margin w3-white">

	
	<div class="w3-container">
	<a href="https://gamingforever.xyz/counterstrike.php"><h3>Iet atpakaļ uz Counter-Strike Forum lapu.</h3></a>
	</div></div>
	<div class="w3-card-4 w3-margin w3-white">
	<div class="w3-container">
	<?php 
//iekļauj šajā php failā config.php failu.
include 'config.php';
 //Raksta ID kuru izmanto vairākās vietās
$postid=$_GET['post'];
//Ja piespiež komentāra saglabāt pogu
if(isset($_POST['submitcomment'])){
// saglabā lietotājvārdu un saturu mainīgajos,
	$body2=$_POST['body2'];
	$author=$_SESSION['username'];
	// ja komentārs nav tukšs tad turpina
  if(isset($_POST['body2']) && !empty($_POST['body2'])){
//ieliek tabulā POSTID, kurš ir vienāds ar raksta ID, autoru(lietotājvārdu) un saturu.
	  $sqlk = 'INSERT INTO cskomenti (postid, username, body)
VALUES ("'.$postid.'","'.$author.'","'.$body2.'")';
//ja komentārs veiksmīgi pievienots, tad izlaiž 'popup' ekrāniņu, kur rakstīts, ka komentārs iesniegts
if ($conn->query($sqlk) === TRUE) {
    echo '<script type="text/javascript">alert("Komentārs iesniegts !")</script>';
} else {
            //echo 'Kluda: ' . $sqlk . '<br>' . $conn->error;
}       
//ja nav ievadīts nekas logā, tad izmet kļūdu
} else {
	echo 'Ludzu, ievadiet kautko teksta logā, lai izveidotu komentāru !';	
  }}

?>
	<?php
// saglabā lietotāju, kā mainīgo
	$username=$_SESSION['username'];
// paņem no datubāzes tabulu, kura ID ir šis posts
$sql = "SELECT id,title,author,content,nosaukumsbilde,created_at FROM counterstrikepost WHERE ID = '$postid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// ja lietotājs ir raksta autors, tad viņam pievieno pogu izdzēst rakstu
$author = $row["author"];
if ($username == $author){
	
	echo "<br>";
	?> 
	<form method='POST'>
                <input type=submit value="Spied šeit, lai dzēstu rakstu !" name=delete >
                </form>	<?php 
//ja nospiesta poga dzēst, tad izdzēš gan rakstu un tās komentārus, un automātiski aizsūta atpakaļ uz foruma lapu
	if(isset($_POST['delete'])){
		$sql = "DELETE FROM counterstrikepost WHERE ID = '$postid'";
		if ($db->query($sql) === TRUE) {
		}
		$sql = "DELETE FROM cskomenti WHERE POSTID = '$postid'";
		if ($db->query($sql) === TRUE) {
		echo "<script>alert('Raksts izdzēsts'); window.location.href='https://gamingforever.xyz/counterstrike.php';</script>";

	
} else {
  echo "Error deleting record: " . $db->error;
}
	}
	}
echo '<br><div id="blogi">
	<div class="blogautors"><h4>Blogu taisīja: '.$row["author"].' , '.$row["created_at"].'</h4></div>
	<div style="word-wrap:break-word;"><h4>Virsraksts: '.$row["title"].'</h4></div>
<div style="word-wrap:break-word;">
	'.$row["content"].'</div>
	</div><br>';
	?>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <!-- scripts priekš bildes samazināšanas un palielināšanas -->
 <script>
	$(document).ready(function () {
        var small={width: "200px",height: "140px"};
        var large={width: "100%",height: "100%"};
        var count=1; 
        $("#imgtab").css(small).on('click',function () { 
            $(this).animate((count==1)?large:small);
            count = 1-count;
        });
    });
	</script>
<p>*Spied uz bildes, lai palielināto to*</p>
<?php 
	echo '<img src="bildescsrakstiem/'.$row['nosaukumsbilde'].'"'?>alt="Bilde" id="imgtab">

	<?php echo '
	<div>
'; }}?>

<!-- Komentāru rakstīšana -->
	<div>
	<h5>Rakstīt komentāru: </h5>
	<form action='' method='post'>
	<p>Teksts:</p>
	<textarea type='text' placeholder='Ievadiet tekstu' name='body2' 
	style='height:120px;max-height:500px;min-height:100px;max-width:100%;
	width:100%;min-width:50%' required></textarea><br><br>
	<button type='submit' name='submitcomment' class='commentbutton1'>Iesniegt komentaru</button><br><br>
</div>
	<h3> Komentāru sekcija</h3>
<?php
//atlasa komentārus, kur POSTID ir raksta ID , sakārto pēc created_at(datuma), un izraksta tos dilstoši
$query1 = "SELECT username,body,created_at FROM cskomenti WHERE postid = '$postid' ORDER BY created_at DESC";
$result = $conn->query($query1);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {   
    //iet uz riņķi, kamēr izraksta visus komentārus
echo "<div id='blogicomment'>
<h4 style=display:inline>" . $row['username'] . "</h4><h5 style=display:inline> komentē:</h5>
<div style='word-wrap:break-word;'>" . $row['body'] . "</div></div>
<div> " .$row['created_at'] . " </div>";
  } } else { 
      //ja nav komentāri, tad rāda ka nav komentāri
	echo "<h3>Nav neviens komentārs !</h3>";
}
?>


</div>
    </div>
	
  </div>
  <hr>

</div>


<div class="w3-col l4">
  <!-- About Card -->
  <div class="w3-card w3-margin w3-margin-top">
  <a href="https://gamingforever.xyz/index.php"><img src="images/logo.png" style="width:100%"></a>
    <div class="w3-container w3-white">
        	<?php if (isset($_SESSION['success'])) : ?>
      <div class="error success" >
      	<h3>
          <?php 
          	echo $_SESSION['success']; 
          	unset($_SESSION['success']);
          ?>
      	</h3>
      </div>
  	<?php endif ?>
    <!-- Lietotāja informācija -->
    <?php  if (isset($_SESSION['username'])) : ?>
    	<p>Sveicināts - <strong><?php echo $_SESSION['username']; ?></strong></p>
    	<!-- ja logout = 1, tad, tad atvieno sesiju un iet uz login.php lapu -->
    	<form action="index.php?logout='1'" class="lout">
		<button class="logout" name="logout">Izlogoties</button>
		</form>
    <?php endif ?>
	<form method="post" class="lout">
	<?php
/* LAIKS */
date_default_timezone_set("Europe/Riga");
$today = date("H:i:s");
echo "Laiks Rīgā: ";
echo $today;
echo "<br>";
echo date_default_timezone_get();
?>
    </div>
  </div><hr>
  

  <div class="w3-card w3-margin">
    <div class="w3-container w3-padding">
      <h4>Populārākie forumi</h4>
    </div>
    <a href="https://gamingforever.xyz/fortnite.php"><ul class="w3-ul w3-hoverable w3-white">
      <li class="w3-padding-16">
        <img src="ikonas/fortnite.png" alt="MC Image" class="w3-left w3-margin-right" style="width:50px">
        <span class="w3-large">Fortnite</span><br>
        <span>Forums tikai par un ap spēli Fortnite.</span>
      </li></a>
      <a href="https://gamingforever.xyz/counterstrike.php"><ul class="w3-ul w3-hoverable w3-white">
      <li class="w3-padding-16">
        <img src="ikonas/counterstrike.png" alt="CS Image" class="w3-left w3-margin-right" style="width:50px">
        <span class="w3-large">Counter-Strike</span><br>
        <span>Forums tikai par un ap spēli Counter-Strike.</span>
      </li></a>
       <a href="https://gamingforever.xyz/farcry.php"><ul class="w3-ul w3-hoverable w3-white">
      <li class="w3-padding-16">
        <img src="ikonas/farcry.png" alt="Far Cry Image" class="w3-left w3-margin-right" style="width:50px">
        <span class="w3-large">Far Cry</span><br>
        <span>Forums tikai par un ap spēli Far Cry.</span>
      </li></a>
     
    </ul>
  </div>
  <hr> 
 

  <div class="w3-card w3-margin">
    <div class="w3-container w3-padding">
      <h4>Lapas</h4>
    </div>
    <div class="w3-container w3-white">
    <a href="https://gamingforever.xyz/parmumslapa.php"><p>Par mums</p></a>
    </div>
  </div>
  <div class="w3-card w3-margin">
    <div class="w3-container w3-padding">
      <h4>Jaunākais Counter-Strike Foruma raksts</h4>
    </div>
    <div class="w3-container w3-white">
	<?php
// Izraksta VIENU jaunāko rakstu, no datubāzes
	$sql='SELECT * FROM counterstrikepost ORDER BY created_at DESC LIMIT 1';
	$result = mysqli_query($conn, $sql);
	while($row = mysqli_fetch_array($result)){
		echo '<div style="margin:5px; border-style: solid;border-width: 1px; border-radius:10px;-moz-border-radius:10px;-webkit-border-radius:10px;">
		
		<a href="counterstrikepost.php?post='.$row["id"].'" style="text-decoration: none;"><div style="border: 1px solid #1C6EA4;
border-radius: 10px 10px 40px 0px; background-color:#f5f5f5;word-wrap:break-word; "><h4 style="margin:-1px 0px -2px 0px;">'.$row["title"].'</h4></a></div>

		<div style="margin:-10px 0px -10px 0px;"><p>Autors:<b> '.$row["author"].'</b> , '.$row["created_at"].'</p></div>'?>
		
<style>
.small {
    height: 60px;
    overflow:hidden;
}
</style>

    <div class="small" style="border: 1px solid #1C6EA4;border-radius: 10px 0px 0px 0px; background-color:#f5f5f5;">
		<p style="margin:0px 0px 0px 0px;"><?php echo ''.$row["content"].''?></p>
		
	</div>
<style>
.container {
  width: 100%;
  margin: auto;
  padding: 5px;
}

.one {
  width: 75%;
  float: left;
}

.two {
  margin-left: 25%;
}

</style>
		<section class="container">
			<div class="one">
			    
	<?php
	// tas pats kods, kas nosaka, cik komentāri ir rakstam, kurš tika izdrukāts.
	echo ' <a href="counterstrikepost.php?post='.$row["id"].'" style="margin:5px;">Spied šeit lai lasītu visu rakstu. </a>  ';
	?> </div> <div class="two"> <?php 
	$postid=$row["id"];
	$query = $conn->prepare("SELECT * FROM cskomenti WHERE postid = '$postid' ");
		$query->execute();
		$query->store_result();
	$rows = $query->num_rows;
echo '<a href="counterstrikepost.php?post='.$row["id"].'">Komentāri: <b>'.$rows.'</b></a>';
	}?> </div>
		
</section>
    </div>
  </div>
  

  
<!-- END Introduction Menu -->
</div>
<!-- Labels / tags -->
  
<!-- END GRID -->
</div><br>

<!-- END w3-content -->
</div>
</div>
<!-- Footer -->
<!-- Footer -->
<footer class="w3-container w3-dark-grey w3-padding-32 w3-margin-top" style="margin-top:0px!important; text-align: center; background-color: black!important;padding-top: 10px!important;padding-bottom: 30px!important;">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

/* Create three equal columns that floats next to each other */
.column {
  float: left;
  width: 33.33%;
  padding: 10px;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}
</style>
</head>
<body>
<div class="row">
  <div class="column" style="position:relative;">

  </div>
  <div class="column" style="min-width:200px;position:relative;">
    <h1 style="margin: 30px 0px 0px 0px;">GAMINGFOREVER</h1>
  </div>
  <div class="column" style="text-align: right; min-width:200px;position:relative;">
    <p style="margin: 25px 0px 0px 0px;">Autortiesības – Kristaps Švīpis<br>
Jelgavas Tehnikums<br>
e-pasts - forums@gamingforever.xyz<br></p>
  </div>
</div>

</footer>


</body>
</html>
