diff --git a/img/uploads/astronaut.jpg b/img/uploads/astronaut.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a4515611f82fa11be991409a09a4fab21093ac1d Binary files /dev/null and b/img/uploads/astronaut.jpg differ diff --git a/img/uploads/nasa_launch.jpg b/img/uploads/nasa_launch.jpg new file mode 100644 index 0000000000000000000000000000000000000000..c81af2c24ed4f248c2a574595dc358ffeff29b1f Binary files /dev/null and b/img/uploads/nasa_launch.jpg differ diff --git a/php/Admin.php b/php/Admin.php index dfbaac9498ad607128e4661521f0e90526dedd06..25f192276d5957389751f145515dffa970fd7c36 100644 --- a/php/Admin.php +++ b/php/Admin.php @@ -6,15 +6,21 @@ require_once 'constants.php'; class Admin extends Page { private const FILE = 'file'; + private const IMAGES = 'images'; + private const IMAGES_WITH_BRACKETS = 'images[]'; private const EXPIRES = 'EXPIRES'; + private const TITLE = 'title'; + private const IMGURL = 'imgurl'; + private const CONTENT = 'content'; + protected function __construct() { parent::__construct(); session_start(); - if (!isset($_SESSION[EXPIRES]) || time() - $_SESSION[EXPIRES] > 60) { + if (!isset($_SESSION[EXPIRES]) || time() - $_SESSION[EXPIRES] > 300) { session_unset(); session_destroy(); header("Location: Login.php"); @@ -37,6 +43,8 @@ class Admin extends Page $this->generatePageHeader('NASA Admin'); $file = self::FILE; + $images = self::IMAGES; + $imagesWithBrackets = self::IMAGES_WITH_BRACKETS; echo <<<EOT <body> @@ -44,8 +52,15 @@ class Admin extends Page <form class="admin-form" action="Admin.php" method="post" enctype="multipart/form-data" accept-charset="UTF-8"> <fieldset> - <legend>Select a News file</legend> - <input id="$file" name="$file" type="file" accept=".json" required /> + <legend>Upload</legend> + <div> + <label for="$file">News file:</label> + <input style="backgorund-color: red" id="$file" name="$file" type="file" accept=".json" required /> + </div> + <div> + <label for="$images">Images:</label> + <input id="$images" name="$imagesWithBrackets" type="file" accept="image/*" multiple /> + </div> <input type="submit" value="Apply" /> </fieldset> </form> @@ -60,17 +75,60 @@ EOT; parent::processReceivedData(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { - if (isset($_FILES[self::FILE])) { - $name = $_FILES[self::FILE]["name"]; - $tmp = $_FILES[self::FILE]["tmp_name"]; - $target = "../data/uploads/" . $name; - move_uploaded_file($tmp, $target); + $this->processNews(); + $this->uploadImages(); + + $_SESSION[NEWS_FILE] = "../data/uploads/news_01.json"; + + header("Location: Index.php"); + die(); + } + } + + private function processNews() { + if (!isset($_FILES[self::FILE])) + return; + + $name = $_FILES[self::FILE]["name"]; + + $path = $_FILES[self::FILE]["tmp_name"]; + + $jsonString = file_get_contents($path); + $json = json_decode($jsonString, true); + + $this->insertNews($json); + } + + private function insertNews(array $json):void { + $listOfNews = $json['news']; + + $stmt = $this->database->prepare("INSERT INTO news(title, imgurl, content) VALUES (?, ?, ?);"); + $stmt->bind_param("sss", $title, $imgurl, $content); + + foreach ($listOfNews as $news) { + $title = $news[self::TITLE]; + $imgurl = $news[self::IMGURL]; + $content = $news[self::CONTENT]; + + $stmt->execute(); + } + + $stmt->close(); + } + + private function uploadImages():void { + if (!isset($_FILES[self::IMAGES])) + return; + + $count = count($_FILES[self::IMAGES]['name']); - $_SESSION[NEWS_FILE] = $target; + for ($i = 0; $i < $count; ++$i) { + $tmpFilePath = $_FILES[self::IMAGES]['tmp_name'][$i]; + $name = $_FILES[self::IMAGES]['name'][$i]; + + $target = "../img/uploads/" . $name; - header("Location: Index.php"); - die(); - } + move_uploaded_file($tmpFilePath, $target); } } diff --git a/php/Index.php b/php/Index.php index 868b04596eb2619844c4803fecb6c20672aac2c5..1b25c234babc3730a3220678b473fa8a719c16db 100644 --- a/php/Index.php +++ b/php/Index.php @@ -6,6 +6,10 @@ require_once 'data/News.php'; class Index extends Page { + private const TITLE = 'title'; + private const IMGURL = 'imgurl'; + private const CONTENT = 'content'; + protected function __construct() { parent::__construct(); @@ -20,32 +24,17 @@ class Index extends Page { protected function getViewData():array { - $file = DEFAULT_NEWS_FILE; - - if (isset($_SESSION[NEWS_FILE])) { - $file = $_SESSION[NEWS_FILE]; - } - - if (!file_exists($file)) { - die("File not found"); - } - - $fileContents = file_get_contents($file); - - if ($fileContents === NULL) - return array(); - - $json = json_decode($fileContents, true); - $news = array(); - foreach ($json["news"] as $elem) { - $title = htmlspecialchars($elem["title"]); - $imgurl = htmlspecialchars($elem["imgurl"]); - $content = htmlspecialchars($elem["content"]); + $resultSet = $this->database->query("SELECT n.title, n.imgurl, n.content FROM news n ORDER BY n.date DESC LIMIT 5"); - $news[] = new News($title, $imgurl, $content); - } + while ($row = $resultSet->fetch_assoc()) { + $title = htmlspecialchars($row[self::TITLE]); + $imgurl = htmlspecialchars($row[self::IMGURL]); + $content = htmlspecialchars($row[self::CONTENT]); + + $news[] = new News($title, $imgurl, $content); + } return $news; } diff --git a/php/Page.php b/php/Page.php index 786ccda31d8bc56dd48f081808bdcba0eff39208..c56d9d5988aef0d9e2ba2f50247c956240f36257 100644 --- a/php/Page.php +++ b/php/Page.php @@ -7,7 +7,7 @@ abstract class Page { protected function __construct() { error_reporting(E_ALL); - $this->database = new mysqli("localhost","root","","nasa"); + $this->database = new mysqli("localhost","root", "","nasa"); // Check connection if ($this->database->connect_errno) { diff --git a/styles.css b/styles.css index 41d8d7bc4aefadbd5c9a880844c6edea3240a3bd..bf34357d6c5530ad63cb27ca473d71eebf3c4956 100644 --- a/styles.css +++ b/styles.css @@ -458,6 +458,19 @@ main { display: inline; } +.admin-form > fieldset > div { + display: flex; + width: 100%; + justify-content: space-around; + align-items: center; + column-gap: 1rem; + padding: 0.5rem 0; +} + +.admin-form > fieldset > div > label { + font-size: 0.8rem; +} + .admin-form input[type="submit"] { background-color: lightgray; cursor: pointer;