<?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">
    <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>
<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/noteikumi.php"><p>LASĪT NOTEIKUMUS. SPIED ŠEIT!</p></a>
    </div>
	
	
	<div class="w3-container">
	<form action="" method="post" enctype="multipart/form-data">
		<p>Virsraksts:</p>
			<input style="width:75%;" name="title"></input>
		<p>Saturs:</p>
			<textarea style="width:100%;height:350px;resize:none;" name="content"></textarea>
		<p>Bilde priekš raksta: *nav obligāts*</p>
<!-- Atļauj ielikt tikai bildes formāta failus -->
		<input type="file" name="fileName" accept="image/x-png,image/gif,image/jpeg,image/png"><br><br>
		<p>Spiest pogu "Iesniegt", lai pabeigtu rakstīt rakstu.</p>
	<button type='submit' name='iesniegtrakstu'>Iesniegt</button><br>
	</form>
	<br>
	<?php
//iekļauj šajā php failā config.php failu.
include 'config.php';
//ja tiek piespiesta poga
if(isset($_POST['iesniegtrakstu'])){
    //saglabā raksta datus mainīgajos
	$author=$_SESSION['username'];
	$title=addslashes($_POST['title']);
	$content=addslashes($_POST['content']);
	$fileName = $_FILES['fileName']['name'];
    $tempName = $_FILES['fileName']['tmp_name'];
	//pārbauda, vai ir ierakstīts virsrakstā un saturā teksts, ja nav, tad parāda kļūdu
  if(isset($_POST['title']) && !empty($_POST['title']) &&
		isset($_POST['content']) && !empty($_POST['content'])){
		    //ja ir faila nosaukums, tad saglabā un pārvieto to uz mājaslapas bildes mapīti, un pārmaina nosaukumu uz unikālu kodu, lai nebūtu vairākas bildes ar vienādiem nosaukumiem.
			if(isset($fileName) && !empty($fileName)){
        {
            $location = "bildesmafiarakstiem/";
			$temp = explode(".", $_FILES["fileName"]["name"]);
			$newfilename = round(microtime(true)) . '.' . end($temp);
            if(move_uploaded_file($tempName, $location.$newfilename))
            {
                echo 'Bilde augšupielādēta,';
            }
        }
	  $sqlk = 'INSERT INTO mafiapost (author, title, content, nosaukumsbilde)
VALUES ("'.$author.'","'.$title.'","'.$content.'","'.$newfilename.'")';

if ($conn->query($sqlk) === TRUE) {
      header("Location: https://gamingforever.xyz/mafia.php");
} else {
    echo 'Kluda: ' . $sqlk . '<br>' . $conn->error;
}
} else {
	//Ja nav bilde ievietota, tad ievieto tāpat, tikai nosaukumsbilde vietā ieliek Logo.png
	 $sqlk = 'INSERT INTO mafiapost (author, title, content, nosaukumsbilde)
VALUES ("'.$author.'","'.$title.'","'.$content.'", "Logo.png")';

if ($conn->query($sqlk) === TRUE) {
    header("Location: https://gamingforever.xyz/mafia.php");
} else {
    echo 'Kļūda: ' . $sqlk . '<br>' . $conn->error;
}
		}
		}else { 
		echo 'Lūdzu aizpildiet visus lodziņus';
		}
		}else { 
		echo '';
		}
?>
	<a href="https://gamingforever.xyz/mafia.php"><h3>Iet atpakaļ uz Mafia Foruma lapu.</h3></a>
    </div>
	
	<div class="w3-container">
    </div>
  </div>
  <hr>
<!-- END BLOG ENTRIES -->
</div>

<!-- Introduction menu -->
<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 ?>
    <!-- logged in user information -->
    <?php  if (isset($_SESSION['username'])) : ?>
    	<p>Sveicināts - <strong><?php echo $_SESSION['username']; ?></strong></p>
    	<form action="index.php?logout='1'" class="lout">
		<button class="logout" name="logout">Logout</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>
  
  <!-- Posts -->
  <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="FT 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> 
 
  <!-- Labels / tags -->
  <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 Mafia Foruma raksts</h4>
    </div>
    <div class="w3-container w3-white">
	<?php
	// Izraksta VIENU jaunāko rakstu, no datubāzes
	$sql='SELECT * FROM mafiapost 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="mafiapost.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 echo ' <a href="mafiapost.php?post='.$row["id"].'" style="margin:5px;">Spied šeit lai lasītu visu rakstu. </a>  ';
	?> </div> <div class="two"> <?php 
		// tas pats kods, kas nosaka, cik komentāri ir rakstam, kurš tika izdrukāts.
	$postid=$row["id"];
	$query = $conn->prepare("SELECT * FROM mafiakomenti WHERE postid = '$postid' ");
		$query->execute();
		$query->store_result();
	$rows = $query->num_rows;
echo '<a href="mafiapost.php?post='.$row["id"].'">Komentāri: <b>'.$rows.'</b></a>';
	}?> </div>
		
</section>
    </div>
  </div>
  

  
<!-- END Introduction Menu -->
</div>

<!-- 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>
