Diff between a5a7ba58cea2eeba79604ede752e14a857e16cc6 and 043ab83de3d4305704d714bc32efcc1959319da5

Changed Files

File Additions Deletions Status
connect.php +21 -0 added
images/cat.webp +0 -0 added
images/cur.jpg +0 -0 added
images/jaakko.jpg +0 -0 added
images/koira.jpg +0 -0 added
images/kpedu.jpeg +0 -0 added
images/plane.jpeg +0 -0 added
index.php +105 -0 added
logged_in.php +14 -0 added
login.php +21 -0 added
logout.php +6 -0 added
write.php +9 -0 added

Full Patch

diff --git a/connect.php b/connect.php
new file mode 100755
index 0000000..5e938c4
--- /dev/null
+++ b/connect.php
@@ -0,0 +1,21 @@
+<?php
+// aloittaa session ja luo yhteyden tietokantaan
+if (session_status() === PHP_SESSION_NONE) {
+    session_start();
+}
+
+$servername = "localhost";
+$username = "root";
+$password = "";
+$database = "neoyle";
+
+// Create connection
+$conn = new mysqli($servername, $username, $password, $database);
+$conn->set_charset("utf8mb4");
+
+// Check connection
+if ($conn->connect_error) {
+  die("Tietokanta virhe: " . $conn->connect_error);
+}
+//echo "Tietokanta yhteys toimii :)";
+?> 
diff --git a/images/cat.webp b/images/cat.webp
new file mode 100644
index 0000000..c635e0b
Binary files /dev/null and b/images/cat.webp differ
diff --git a/images/cur.jpg b/images/cur.jpg
new file mode 100644
index 0000000..5b0a2b5
Binary files /dev/null and b/images/cur.jpg differ
diff --git a/images/jaakko.jpg b/images/jaakko.jpg
new file mode 100644
index 0000000..9f42b86
Binary files /dev/null and b/images/jaakko.jpg differ
diff --git a/images/koira.jpg b/images/koira.jpg
new file mode 100644
index 0000000..df579c8
Binary files /dev/null and b/images/koira.jpg differ
diff --git a/images/kpedu.jpeg b/images/kpedu.jpeg
new file mode 100644
index 0000000..0fc1188
Binary files /dev/null and b/images/kpedu.jpeg differ
diff --git a/images/plane.jpeg b/images/plane.jpeg
new file mode 100644
index 0000000..4b08779
Binary files /dev/null and b/images/plane.jpeg differ
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..77a002e
--- /dev/null
+++ b/index.php
@@ -0,0 +1,105 @@
+<?php
+
+include"connect.php";
+include"logged_in.php";
+
+// Hanki kategoriat
+$sql = "SELECT * FROM kategoriat";
+$result = $conn->query($sql); // Aja komento
+// Jos tuloksia
+$kategoriat = [];
+if ($result->num_rows>0){
+	while($row = $result->fetch_assoc()){
+		$kategoriat[] = $row;
+	}
+}
+// Hanki artikkelit
+$sql = "SELECT * FROM uutiset"; //TODO ota vain oikean kategorian
+$result = $conn->query($sql); // Aja komento
+// Jos tuloksia
+$artikkelit = [];
+if ($result->num_rows>0){
+	while($row = $result->fetch_assoc()){
+		$artikkelit[] = $row;
+	}
+}
+// Hanki toimittajat
+$sql = "SELECT * FROM kayttajat"; //TODO ota vain oikean kategorian
+$result = $conn->query($sql); // Aja komento
+// Jos tuloksia
+$kayttajat = [];
+if ($result->num_rows>0){
+	while($row = $result->fetch_assoc()){
+		$kayttajat[] = $row;
+	}
+}
+
+if (isset($_GET["kategoria"])) {
+	foreach ($kategoriat as $kat) {
+		if ($kat["nimi"] == $_GET["kategoria"]) {
+			$sivu_cat = $kat["id"];
+		}
+	}
+}
+
+?>
+<!DOCTYPE html>
+<html lang="fi">
+	<head>
+		<meta charset="UTF-8" />
+		<meta name="viewport" content="width=device-width, initial-scale=1" />
+		<link rel="stylesheet" href="style.css" type="text/css" media="all">
+		<title>NeoYle, uutisten tulevaisuus</title>
+		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> <!-- En voinut hillitä itseäni -->
+		<link href="https://fonts.googleapis.com/css2?family=Stack+Sans+Text:wght@200..700&display=swap" rel="stylesheet">
+	</head>
+	<body class="light-theme">
+		<header>
+			<nav>
+				<a href="." id="logocontainer">
+					<object data="neoyle.svg" type="image/svg+xml" id="logo"></object>
+				</a>
+				<?php foreach ($kategoriat as $kategoria): ?>
+					<a href="?kategoria=<?php echo $kategoria["nimi"]; ?>"> <?php echo $kategoria["nimi"]; ?> </a>
+				<?php endforeach; ?>
+				<a id="themebtn">Teema</a>
+				<noscript>Vaalean ja pimäen teeman vaihtoon vaaditaan Javascript</noscript>
+				<?php if ($logged_in): ?>
+					<a href="write.php"> Kirjoita </a>
+					<a href="logout.php"> Kirjaudu ulos </a>
+				<?php else: ?>
+					<a href="login.php"> Kirjaudu </a>
+				<?php endif; ?>
+			</nav>
+		</header>
+		<main>
+			<?php foreach ($artikkelit as $artikkeli): ?>
+				<?php if ( !isset($sivu_cat) or $sivu_cat == $artikkeli["kategoria_id"] ): ?>
+					<section>
+						<a href="#">
+							<div class="imgcontainer">
+								<img src="images/<?php echo $artikkeli["kuva"]; ?>">
+							</div>
+						<h1><?php echo $artikkeli["otsikko"]; ?></h1>
+						<p>
+							<?php echo $artikkeli["teksti"]; ?>
+						</p>
+						<p class="right">
+							<?php 
+								$toimittaja_id = $artikkeli["toimittaja_id"];
+								foreach ($kayttajat as $kayttaja) { // Säästää SQL queryjä mutta ehkä vähän hidas kuitenkin
+									if ($kayttaja["id"] == $toimittaja_id) {
+										echo $kayttaja["nimi"];
+										break;
+									}
+								}
+							?>
+						</p>
+						</a>
+					</section>
+				<?php endif; ?>
+			<?php endforeach; ?>
+		</main>
+		<script src="theme-switch.js" charset="utf-8"></script>
+	</body>
+</html>
diff --git a/logged_in.php b/logged_in.php
new file mode 100644
index 0000000..f16d77f
--- /dev/null
+++ b/logged_in.php
@@ -0,0 +1,14 @@
+<?php
+$logged_in = false;
+
+if (isset($_SESSION["name"]) and isset($_SESSION["pass"]) ) {
+	include "connect.php";
+	// Hanki kayttajat
+	$sql = "SELECT * FROM kayttajat WHERE nimi='" . $_SESSION["name"] . "'";
+	$result = $conn->query($sql); // Aja komento
+	$kayttaja = $result->fetch_assoc();
+	if (password_verify($_SESSION["pass"], $kayttaja["salasana"] )) {
+		$logged_in = true;
+	}
+}
+?>
diff --git a/login.php b/login.php
new file mode 100644
index 0000000..d007f1c
--- /dev/null
+++ b/login.php
@@ -0,0 +1,21 @@
+<?php
+if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+	if (session_status() === PHP_SESSION_NONE) {
+		session_start();
+	}
+	$_SESSION["name"] = $_POST["name"];
+	$_SESSION["pass"] = $_POST["pass"];
+
+	header("Location: .");
+	die();
+}
+?>
+<form action="" method="POST">
+	<label for="name">Name</label>
+	<input type="text" id="name" name="name" required>
+	<br>
+	<label for="pass">Password</label>
+	<input type="password" id="pass" name="pass" required>
+	<br>
+	<button type="submit">Kirjaudu</button>
+</form>
diff --git a/logout.php b/logout.php
new file mode 100644
index 0000000..7d2e8a5
--- /dev/null
+++ b/logout.php
@@ -0,0 +1,6 @@
+<?php 
+require_once "connect.php";
+session_destroy();
+header("Location: .");
+die();
+?>
diff --git a/write.php b/write.php
new file mode 100644
index 0000000..6e5b3b0
--- /dev/null
+++ b/write.php
@@ -0,0 +1,9 @@
+<?php
+
+
+
+?>
+<form>
+	<label for="text">Teksti</label>
+	<input type="text" id="text" name="text">
+</form>