Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $path = explode('/', dirname(__FILE__));
- $dirname = array_pop($path);
- $username = array_pop($path);
- $path = implode('/', $path);
- $tasks = array();
- foreach (scandir("$path/$username") as $dir)
- if (is_dir("$path/$username/$dir") and $dir != '.' and $dir != '..')
- $tasks[] = $dir;
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Zadanie 2 - WWW i jzyki skryptowe</title>
- <meta charset="utf-8">
- <meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate" />
- <meta http-equiv="Pragma" content="no-cache" />
- <link rel="stylesheet" type="text/css" href="style.css">
- </head>
- <body>
- <header>
- <h1>
- Zadanie 2 - Krystian Tworzewski
- </h1>
- <h2>
- Proste forum
- </h2>
- </header>
- <nav>
- <a href="../">Home</a>
- <a href="../zadanie1">Zadanie 1</a>
- <a href="../zadanie2">Zadanie 2</a>
- <a href="../zadanie3">Zadanie 3</a>
- <a href="../zadanie4">Zadanie 4</a>
- <a href="../zadanie5">Zadanie 5</a>
- <a href="../zadanie6">Zadanie 6</a>
- <a href="../zadanie8">Zadanie 8</a>
- </nav>
- <section>
- <?php
- function get_topics($datafile = "tematy.txt", $separator = "&!@sep@#!#&")
- {
- // wczytanie pliku do tablicy stringów
- if ($data = file($datafile)) {
- // utworzenie pustej tablicy wynikowej
- $topics = array();
- // dla każdego elementu tablicy $data
- // $k - klucz ementu, $v - wartość elementu
- foreach ($data as $k => $v) {
- // umieszcza kolejne elementy wiersza rozdzielone separatoerm
- // w kolejnych elementach zwracanej tablicy
- $record = explode($separator, trim($v));
- // jesli pasuje identyfikator tematu
- // przepakowanie do $posts[] i dekodowanie danych użytkownika
- $topics[] = array(
- "topicid" => $record[0],
- "topic" => hex2bin($record[1]),
- "topic_body" => hex2bin($record[2]),
- "username" => hex2bin($record[3]),
- "date" => $record[4],
- "postCounter" => $record[5]
- );
- }
- return $topics;
- }
- // zwraca tablice z wynikami
- else {
- // zwraca kod błędu
- return FALSE;
- }
- }
- //------------------------------------------------------------------------------
- // funkcja zapisu do pliku tematy.txt
- function put_topic($topic, $topic_body, $username, $postCounter = 0, $datafile = "tematy.txt", $separator = "&!@sep@#!#&")
- {
- // ostatni wiersz zawiera najmłodszy wpis
- if (is_file($datafile)) {
- // odczyt pliku
- $data = file($datafile);
- // pobranie danych z ostatniego elementu tablicy $data
- $record = explode($separator, trim(array_pop($data)));
- $topicid = ((int) $record[0]) + 1;
- } else {
- $topicid = 1;
- }
- // utworzenie nowego wiersz danych
- // zakodowanie przez bin2hex() danych przesłanych przez użtykownika
- $data = implode($separator, array(
- $topicid,
- bin2hex($topic),
- bin2hex($topic_body),
- bin2hex($username),
- date("Y-m-d H:i:s"),
- $postCounter
- ));
- // zapis danych na końcu pliku
- if ($fh = fopen($datafile, "a+")) {
- fwrite($fh, $data . "\n");
- fclose($fh);
- return $topicid;
- } else {
- return FALSE;
- }
- }
- function put_post($topicid, $post, $username, $datafile = "wypowiedzi.txt", $separator = "&!@sep@#!#&")
- {
- // ostatni wiersz zawiera najmłodszy wpis
- if (is_file($datafile)) {
- // odczyt pliku
- $data = file($datafile);
- // pobranie danych z ostatniego elementu tablicy $data
- $record = explode($separator, trim(array_pop($data)));
- $postid = ((int) $record[0]) + 1;
- } else {
- $postid = 1;
- }
- // utworzenie nowego wiersz danych
- // zakodowanie przez bin2hex() danych przesłanych przez użtykownika
- $data = implode($separator, array(
- $postid,
- $topicid,
- bin2hex($post),
- bin2hex($username),
- date("Y-m-d H:i:s")
- ));
- // zapis danych na końcu pliku
- if ($fh = fopen($datafile, "a+")) {
- fwrite($fh, $data . "\n");
- fclose($fh);
- return $postid;
- } else {
- return FALSE;
- }
- ;
- }
- function get_posts($topicid, $datafile = "wypowiedzi.txt", $separator = "&!@sep@#!#&")
- {
- // wczytanie pliku do tablicy stringów
- if ($data = file($datafile)) {
- // utworzenie pustej tablicy wynikowej
- $posts = array();
- // dla każdego elementu tablicy $data
- // $k - klucz ementu, $v - wartość elementu
- foreach ($data as $k => $v) {
- // umieszcza kolejne elementy wiersza rozdzielone separatoerm
- // w kolejnych elementach zwracanej tablicy
- $record = explode($separator, trim($v));
- // jesli pasuje identyfikator tematu
- if ($record[1] == $topicid) {
- // przepakowanie do $posts[] i dekodowanie danych użytkownika
- $posts[] = array(
- "postid" => $record[0],
- "topicid" => $record[1],
- "post" => hex2bin($record[2]),
- "username" => hex2bin($record[3]),
- "date" => $record[4]
- );
- } else if ($topicid == NULL) {
- $posts[] = array(
- "postid" => $record[0],
- "topicid" => $record[1],
- "post" => hex2bin($record[2]),
- "username" => hex2bin($record[3]),
- "date" => $record[4]
- );
- }
- }
- // zwraca tablice z wynikami
- return $posts;
- } else {
- // zwraca kod błędu
- return FALSE;
- }
- }
- function updateCounter($topicid, $action, $datafile = "tematy.txt", $separator = "&!@sep@#!#&")
- {
- // wczytanie pliku do tablicy stringów
- if ($data = file($datafile)) {
- // utworzenie pustej tablicy wynikowej
- $topics = array();
- // dla każdego elementu tablicy $data
- // $k - klucz ementu, $v - wartość elementu
- foreach ($data as $k => $v) {
- // umieszcza kolejne elementy wiersza rozdzielone separatoerm
- // w kolejnych elementach zwracanej tablicy
- $record = explode($separator, trim($v));
- // jesli pasuje identyfikator tematu
- // przepakowanie do $posts[] i dekodowanie danych użytkownika
- if ($record[0] == $topicid) {
- if ($action) {
- $topics[] = array(
- "topicid" => $record[0],
- "topic" => hex2bin($record[1]),
- "topic_body" => hex2bin($record[2]),
- "username" => hex2bin($record[3]),
- "date" => $record[4],
- "postCounter" => ($record[5] + 1)
- );
- } else if (!$action) {
- $topics[] = array(
- "topicid" => $record[0],
- "topic" => hex2bin($record[1]),
- "topic_body" => hex2bin($record[2]),
- "username" => hex2bin($record[3]),
- "date" => $record[4],
- "postCounter" => ($record[5] - 1)
- );
- }
- } else {
- $topics[] = array(
- "topicid" => $record[0],
- "topic" => hex2bin($record[1]),
- "topic_body" => hex2bin($record[2]),
- "username" => hex2bin($record[3]),
- "date" => $record[4],
- "postCounter" => $record[5]
- );
- }
- }
- } else {
- // zwraca kod błędu
- return FALSE;
- }
- $file = fopen($datafile, "w+");
- fclose($file);
- //print_r($topics);
- foreach ($topics as $value) {
- $data2 = implode($separator, array(
- $value['topicid'],
- bin2hex($value['topic']),
- bin2hex($value['topic_body']),
- bin2hex($value['username']),
- $value['date'],
- $value['postCounter']
- ));
- // zapis danych na końcu pliku
- if ($fh = fopen($datafile, "a+")) {
- fwrite($fh, $data2 . "\n");
- fclose($fh);
- } else {
- return FALSE;
- }
- }
- }
- function editPost($topicid, $datafile = "wypowiedzi.txt", $separator = "&!@sep@#!#&")
- {
- if (isset($_POST['submitEdit'])) {
- $post = $_POST["post"];
- $username = $_POST["username"];
- $postid = $_POST["indekspostname"];
- // wczytanie pliku do tablicy stringów
- if ($data = file($datafile)) {
- // utworzenie pustej tablicy wynikowej
- $posts = array();
- // dla każdego elementu tablicy $data
- // $k - klucz ementu, $v - wartość elementu
- foreach ($data as $k => $v) {
- // umieszcza kolejne elementy wiersza rozdzielone separatoerm
- // w kolejnych elementach zwracanej tablicy
- $record = explode($separator, trim($v));
- // jesli pasuje identyfikator tematu
- //echo $record[0];
- if ($record[1] == $topicid and $record[0] == $postid) {
- // przepakowanie do $posts[] i dekodowanie danych użytkownika
- $postToEdit[] = array(
- "postid" => $record[0],
- "topicid" => $record[1],
- "post" => hex2bin(bin2hex($post)),
- "username" => hex2bin(bin2hex($username)),
- "date" => $record[4]
- );
- } else {
- $postToEdit[] = array(
- "postid" => $record[0],
- "topicid" => $record[1],
- "post" => hex2bin($record[2]),
- "username" => hex2bin($record[3]),
- "date" => $record[4]
- );
- }
- }
- // zwraca tablice z wynikami
- } else {
- // zwraca kod błędu
- return FALSE;
- }
- $file = fopen($datafile, "w+");
- fclose($file);
- //print_r($topics);
- foreach ($postToEdit as $value) {
- $data2 = implode($separator, array(
- $value['postid'],
- $value['topicid'],
- bin2hex($value['post']),
- bin2hex($value['username']),
- $value['date']
- ));
- // zapis danych na końcu pliku
- if ($fh = fopen($datafile, "a+")) {
- fwrite($fh, $data2 . "\n");
- fclose($fh);
- } else {
- return FALSE;
- }
- }
- }
- }
- function deletePost($topicid, $datafile = "wypowiedzi.txt", $separator = "&!@sep@#!#&")
- {
- $postid = $_GET['id'];
- // wczytanie pliku do tablicy stringów
- if ($data = file($datafile)) {
- // utworzenie pustej tablicy wynikowej
- $posts = array();
- // dla każdego elementu tablicy $data
- // $k - klucz ementu, $v - wartość elementu
- foreach ($data as $k => $v) {
- // umieszcza kolejne elementy wiersza rozdzielone separatoerm
- // w kolejnych elementach zwracanej tablicy
- $record = explode($separator, trim($v));
- // jesli pasuje identyfikator tematu
- //echo $record[0];
- if ($record[1] == $topicid and $record[0] == $postid) {
- // przepakowanie do $posts[] i dekodowanie danych użytkownika
- } else {
- $postToEdit[] = array(
- "postid" => $record[0],
- "topicid" => $record[1],
- "post" => hex2bin($record[2]),
- "username" => hex2bin($record[3]),
- "date" => $record[4]
- );
- }
- }
- // zwraca tablice z wynikami
- } else {
- // zwraca kod błędu
- return FALSE;
- }
- $file = fopen($datafile, "w+");
- fclose($file);
- //print_r($topics);
- if ($posts != NULL) {
- foreach ($postToEdit as $value) {
- $data2 = implode($separator, array(
- $value['postid'],
- $value['topicid'],
- bin2hex($value['post']),
- bin2hex($value['username']),
- $value['date']
- ));
- // zapis danych na końcu pliku
- if ($fh = fopen($datafile, "a+")) {
- fwrite($fh, $data2 . "\n");
- fclose($fh);
- } else {
- return FALSE;
- }
- }
- }
- }
- if (isset($_GET['topic'])) {
- if (is_numeric($_GET['topic'])) {
- $id = $_GET['topic'];
- }
- }
- function clearFile($datafile = "tematy.txt", $datafile2 = "wypowiedzi.txt")
- {
- $file = fopen($datafile, "w+");
- fclose($file);
- $file2 = fopen($datafile2, "w+");
- fclose($file2);
- }
- //clearFile();
- if (isset($_POST["submit"])) {
- if (!isset($_GET['topic'])) {
- $topic = htmlspecialchars($_POST["topic"]);
- $topic_body = htmlspecialchars($_POST["topic_body"]);
- $username = htmlspecialchars($_POST["username"]);
- $topic = preg_replace('/[^(\x20-\x7F)]*/', '', $topic);
- $topic_body = preg_replace('/[^(\x20-\x7F)]*/', '', $topic_body);
- $username = preg_replace('/[^(\x20-\x7F)]*/', '', $username);
- if ($topic != NULL and $topic_body != NULL and $username != NULL)
- put_topic($topic, $topic_body, $username);
- } else {
- $post = htmlspecialchars($_POST["post"]);
- $username = htmlspecialchars($_POST["username"]);
- $post = preg_replace('/[^(\x20-\x7F)]*/', '', $post);
- $username = preg_replace('/[^(\x20-\x7F)]*/', '', $username);
- if ($post != NULL and $username != NULL) {
- put_post($id, $post, $username);
- updateCounter($id, true);
- }
- }
- }
- $topics = get_topics();
- if (!isset($_GET['topic'])) {
- echo "<p>Możesz dodac nowy temat za pomocą <a href='#topic_form'>formularza</a>.</p>";
- foreach ((array) $topics as $key => $value) {
- if ($value['topicid'] != 0) {
- echo "<article class='topic'>
- <header> </header>
- <div><a href='?topic=" . $value['topicid'] . "'>" . $value['topic'] . "</a></div>
- <footer>ID: " . $value['topicid'] . ", Autor: " . $value['username'] . ", Utworzono: " . $value['date'] . ", Liczba wpisów: " . $value['postCounter'] . "</footer>
- </article>";
- }
- }
- } else {
- if (is_numeric($_GET['topic'])) {
- $indeks = -1;
- foreach ($topics as $key => $topic) {
- if ($topic["topicid"] == $_GET["topic"])
- $indeks = $key;
- }
- echo '<nav>
- <table><tr>
- <td>';
- if ($id > 1)
- echo '<a style="float:left;" href="index.php?topic=' . ($id - 1) . '"><-- Poprzedni temat</a>';
- echo '</td><td style="width: 33%;">
- <a href="index.php">Lista tematów</a>
- </td><td style="width: 33%;">';
- if (isset($topics[$id]))
- echo '<a style="float:right;" href="index.php?topic=' . ($id + 1) . '">Następny temat --></a>';
- echo '</td>
- </tr></table>
- </nav>';
- echo "<article class='topic'>
- <header>Temat dyskusji: <b>" . $topics[$indeks]['topic'] . "</b></header>
- <div>" . $topics[$indeks]['topic_body'] . "</div>
- <footer>
- ID: " . $id . ", Autor: " . $topics[$indeks]['username'] . ", Data: " . $topics[$indeks]['date'] . " </footer>
- </article>";
- echo "<p>Możesz dodac nowy post za pomocą <a href='#post_form'>formularza</a>.</p>";
- } else
- return FALSE;
- }
- if (isset($_GET['topic'])) {
- editPost($id);
- }
- if (isset($_GET['cmd']) and $_GET['cmd'] == 'delete') {
- deletePost($id);
- updateCounter($id, false);
- }
- if (isset($_GET['topic'])) {
- $posts = get_posts($id);
- if ($posts != NULL) {
- foreach ((array) $posts as $key => $value) {
- echo '<article>
- <div>' . $value['post'] . '</div>
- <footer>
- <nav>
- <a href="?topic=' . $value["topicid"] . '&id=' . $value["postid"] . '&cmd=edit">EDYTUJ</a>
- <a class="danger" href="?topic=' . $value["topicid"] . '&id=' . $value["postid"] . '&cmd=delete">KASUJ</a>
- </nav>
- ID: ' . $value["postid"] . ', Autor: ' . $value["username"] . ', Utworzono dnia: ' . $value["date"] . '</footer>
- </article>';
- }
- }
- }
- $indeksPost = -1;
- if (isset($_GET['cmd']) and $_GET['cmd'] == 'edit') {
- foreach ($posts as $key => $post) {
- if ($post["postid"] == $_GET["id"])
- $indeksPost = $key;
- }
- }
- if (!isset($_GET['topic'])) {
- echo '<form action="index.php" method="post">
- <a name="topic_form"></a>
- <header><h2>Dodaj nowy temat do dyskusji</h2></header>
- <input type="text" name="topic" placeholder="Nowy temat" autofocus \><br />
- <textarea name="topic_body" cols="80" rows="10" placeholder="Opis nowego tematu" ></textarea><br />
- <input type="text" name="username" placeholder="Imię autora" \><br />
- <button type="submit" name="submit" >Zapisz</button>
- </form>';
- } else {
- echo '<form action="index.php?topic=' . $id . '" method="post">
- <a name="post_form"></a>
- <header><h2>Dodaj nowy post do dyskusji</h2></header>
- <textarea name="post" cols="80" rows="10" placeholder="Opis nowego postu" >';
- if (isset($_GET['cmd']) and $_GET['cmd'] == 'edit') {
- echo $posts[$indeksPost]['post'] . '</textarea><br /><input type="text" name="username" placeholder="Imię autora" value ="' . $posts[$indeksPost]['username'] . '">';
- echo "<input type='hidden' value='$_GET[id]' name='indekspostname'";
- echo '<br /><button type="submit" name="submitEdit" >Zapisz</button></form>';
- } else {
- echo '</textarea><br /><input type="text" name="username" placeholder="Imię autora"">
- <br /><button type="submit" name="submit" >Zapisz</button></form>';
- }
- }
- ?>
- </section>
- <footer>
- Ostatni wpis na formu powstał dnia: <?php
- $posts2 = get_posts(NULL); //get all posts
- $oldest = $posts2[0]['date'];
- if ($posts2 != NULL) {
- foreach ($posts2 as $value) {
- if ($oldest < $value['date'])
- $oldest = $value['date'];
- }
- }
- echo $oldest;
- ?>
- </footer>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement