Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.90 KB | None | 0 0
  1. <?php
  2. $path     = explode('/', dirname(__FILE__));
  3. $dirname  = array_pop($path);
  4. $username = array_pop($path);
  5. $path     = implode('/', $path);
  6. $tasks    = array();
  7. foreach (scandir("$path/$username") as $dir)
  8.     if (is_dir("$path/$username/$dir") and $dir != '.' and $dir != '..')
  9.         $tasks[] = $dir;
  10. ?>
  11.  
  12. <!DOCTYPE html>
  13. <html>
  14. <head>
  15.     <title>Zadanie 2 - WWW i jzyki skryptowe</title>
  16.     <meta charset="utf-8">
  17.     <meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate" />
  18.     <meta http-equiv="Pragma" content="no-cache" />
  19.     <link rel="stylesheet" type="text/css" href="style.css">
  20. </head>
  21. <body>
  22.     <header>
  23.       <h1>
  24.           Zadanie 2 - Krystian Tworzewski
  25.       </h1>
  26.       <h2>
  27.           Proste forum
  28.       </h2>
  29.     </header>
  30.     <nav>
  31.             <a href="../">Home</a>
  32.                         <a href="../zadanie1">Zadanie 1</a>
  33.                         <a href="../zadanie2">Zadanie 2</a>
  34.                         <a href="../zadanie3">Zadanie 3</a>
  35.                         <a href="../zadanie4">Zadanie 4</a>
  36.                         <a href="../zadanie5">Zadanie 5</a>
  37.                         <a href="../zadanie6">Zadanie 6</a>
  38.                         <a href="../zadanie8">Zadanie 8</a>
  39.                 </nav>
  40. <section>    
  41.     <?php
  42.  
  43.  
  44. function get_topics($datafile = "tematy.txt", $separator = "&!@sep@#!#&")
  45. {
  46.     // wczytanie pliku do tablicy stringów
  47.     if ($data = file($datafile)) {
  48.         // utworzenie pustej tablicy wynikowej
  49.         $topics = array();
  50.         // dla każdego elementu tablicy $data
  51.         //    $k - klucz ementu,  $v - wartość elementu
  52.         foreach ($data as $k => $v) {
  53.             // umieszcza kolejne elementy wiersza rozdzielone separatoerm
  54.             // w kolejnych elementach zwracanej tablicy
  55.             $record = explode($separator, trim($v));
  56.             // jesli pasuje identyfikator tematu
  57.             // przepakowanie do $posts[] i dekodowanie danych użytkownika
  58.            
  59.             $topics[] = array(
  60.                 "topicid" => $record[0],
  61.                 "topic" => hex2bin($record[1]),
  62.                 "topic_body" => hex2bin($record[2]),
  63.                 "username" => hex2bin($record[3]),
  64.                 "date" => $record[4],
  65.                 "postCounter" => $record[5]
  66.             );
  67.         }
  68.         return $topics;
  69.     }
  70.     // zwraca tablice z wynikami
  71.     else {
  72.         // zwraca kod błędu
  73.         return FALSE;
  74.     }
  75.    
  76. }
  77.  
  78.  
  79. //------------------------------------------------------------------------------
  80. // funkcja zapisu do pliku tematy.txt
  81. function put_topic($topic, $topic_body, $username, $postCounter = 0, $datafile = "tematy.txt", $separator = "&!@sep@#!#&")
  82. {
  83.     // ostatni wiersz zawiera najmłodszy wpis
  84.     if (is_file($datafile)) {
  85.         // odczyt pliku
  86.         $data    = file($datafile);
  87.         // pobranie danych z ostatniego elementu tablicy $data
  88.         $record  = explode($separator, trim(array_pop($data)));
  89.         $topicid = ((int) $record[0]) + 1;
  90.     } else {
  91.         $topicid = 1;
  92.     }
  93.     // utworzenie nowego wiersz danych
  94.     // zakodowanie przez bin2hex() danych przesłanych przez użtykownika
  95.     $data = implode($separator, array(
  96.         $topicid,
  97.         bin2hex($topic),
  98.         bin2hex($topic_body),
  99.         bin2hex($username),
  100.         date("Y-m-d H:i:s"),
  101.         $postCounter
  102.     ));
  103.     // zapis danych na końcu pliku
  104.     if ($fh = fopen($datafile, "a+")) {
  105.         fwrite($fh, $data . "\n");
  106.         fclose($fh);
  107.         return $topicid;
  108.     } else {
  109.         return FALSE;
  110.     }
  111. }
  112.  
  113.  
  114. function put_post($topicid, $post, $username, $datafile = "wypowiedzi.txt", $separator = "&!@sep@#!#&")
  115. {
  116.     // ostatni wiersz zawiera najmłodszy wpis
  117.     if (is_file($datafile)) {
  118.         // odczyt pliku
  119.         $data   = file($datafile);
  120.         // pobranie danych z ostatniego elementu tablicy $data
  121.         $record = explode($separator, trim(array_pop($data)));
  122.         $postid = ((int) $record[0]) + 1;
  123.     } else {
  124.         $postid = 1;
  125.     }
  126.     // utworzenie nowego wiersz danych
  127.     // zakodowanie przez bin2hex() danych przesłanych przez użtykownika
  128.     $data = implode($separator, array(
  129.         $postid,
  130.         $topicid,
  131.         bin2hex($post),
  132.         bin2hex($username),
  133.         date("Y-m-d H:i:s")
  134.     ));
  135.     // zapis danych na końcu pliku
  136.     if ($fh = fopen($datafile, "a+")) {
  137.         fwrite($fh, $data . "\n");
  138.         fclose($fh);
  139.         return $postid;
  140.     } else {
  141.         return FALSE;
  142.     }
  143.     ;
  144. }
  145.  
  146.  
  147. function get_posts($topicid, $datafile = "wypowiedzi.txt", $separator = "&!@sep@#!#&")
  148. {
  149.     // wczytanie pliku do tablicy stringów
  150.     if ($data = file($datafile)) {
  151.         // utworzenie pustej tablicy wynikowej
  152.         $posts = array();
  153.         // dla każdego elementu tablicy $data
  154.         //    $k - klucz ementu,  $v - wartość elementu
  155.         foreach ($data as $k => $v) {
  156.             // umieszcza kolejne elementy wiersza rozdzielone separatoerm
  157.             // w kolejnych elementach zwracanej tablicy
  158.             $record = explode($separator, trim($v));
  159.             // jesli pasuje identyfikator tematu
  160.             if ($record[1] == $topicid) {
  161.                 // przepakowanie do $posts[] i dekodowanie danych użytkownika
  162.                 $posts[] = array(
  163.                     "postid" => $record[0],
  164.                     "topicid" => $record[1],
  165.                     "post" => hex2bin($record[2]),
  166.                     "username" => hex2bin($record[3]),
  167.                     "date" => $record[4]
  168.                 );
  169.             } else if ($topicid == NULL) {
  170.                 $posts[] = array(
  171.                     "postid" => $record[0],
  172.                     "topicid" => $record[1],
  173.                     "post" => hex2bin($record[2]),
  174.                     "username" => hex2bin($record[3]),
  175.                     "date" => $record[4]
  176.                 );
  177.             }
  178.         }
  179.         // zwraca tablice z wynikami
  180.         return $posts;
  181.     } else {
  182.         // zwraca kod błędu
  183.         return FALSE;
  184.     }
  185. }
  186.  
  187. function updateCounter($topicid, $action, $datafile = "tematy.txt", $separator = "&!@sep@#!#&")
  188. {
  189.     // wczytanie pliku do tablicy stringów
  190.    
  191.     if ($data = file($datafile)) {
  192.         // utworzenie pustej tablicy wynikowej
  193.         $topics = array();
  194.         // dla każdego elementu tablicy $data
  195.         //    $k - klucz ementu,  $v - wartość elementu
  196.         foreach ($data as $k => $v) {
  197.             // umieszcza kolejne elementy wiersza rozdzielone separatoerm
  198.             // w kolejnych elementach zwracanej tablicy
  199.             $record = explode($separator, trim($v));
  200.             // jesli pasuje identyfikator tematu
  201.             // przepakowanie do $posts[] i dekodowanie danych użytkownika
  202.             if ($record[0] == $topicid) {
  203.                 if ($action) {
  204.                     $topics[] = array(
  205.                         "topicid" => $record[0],
  206.                         "topic" => hex2bin($record[1]),
  207.                         "topic_body" => hex2bin($record[2]),
  208.                         "username" => hex2bin($record[3]),
  209.                         "date" => $record[4],
  210.                         "postCounter" => ($record[5] + 1)
  211.                     );
  212.                 } else if (!$action) {
  213.                     $topics[] = array(
  214.                         "topicid" => $record[0],
  215.                         "topic" => hex2bin($record[1]),
  216.                         "topic_body" => hex2bin($record[2]),
  217.                         "username" => hex2bin($record[3]),
  218.                         "date" => $record[4],
  219.                         "postCounter" => ($record[5] - 1)
  220.                     );
  221.                    
  222.                 }
  223.             } else {
  224.                 $topics[] = array(
  225.                     "topicid" => $record[0],
  226.                     "topic" => hex2bin($record[1]),
  227.                     "topic_body" => hex2bin($record[2]),
  228.                     "username" => hex2bin($record[3]),
  229.                     "date" => $record[4],
  230.                     "postCounter" => $record[5]
  231.                 );
  232.             }
  233.         }
  234.        
  235.     } else {
  236.         // zwraca kod błędu
  237.         return FALSE;
  238.     }
  239.    
  240.     $file = fopen($datafile, "w+");
  241.     fclose($file);
  242.    
  243.     //print_r($topics);
  244.    
  245.     foreach ($topics as $value) {
  246.         $data2 = implode($separator, array(
  247.             $value['topicid'],
  248.             bin2hex($value['topic']),
  249.             bin2hex($value['topic_body']),
  250.             bin2hex($value['username']),
  251.             $value['date'],
  252.             $value['postCounter']
  253.         ));
  254.         // zapis danych na końcu pliku
  255.         if ($fh = fopen($datafile, "a+")) {
  256.             fwrite($fh, $data2 . "\n");
  257.             fclose($fh);
  258.            
  259.         } else {
  260.             return FALSE;
  261.         }
  262.     }
  263. }
  264.  
  265.  
  266. function editPost($topicid, $datafile = "wypowiedzi.txt", $separator = "&!@sep@#!#&")
  267. {
  268.     if (isset($_POST['submitEdit'])) {
  269.        
  270.         $post     = $_POST["post"];
  271.         $username = $_POST["username"];
  272.         $postid   = $_POST["indekspostname"];
  273.        
  274.        
  275.         // wczytanie pliku do tablicy stringów
  276.         if ($data = file($datafile)) {
  277.             // utworzenie pustej tablicy wynikowej
  278.             $posts = array();
  279.             // dla każdego elementu tablicy $data
  280.             //    $k - klucz ementu,  $v - wartość elementu
  281.             foreach ($data as $k => $v) {
  282.                 // umieszcza kolejne elementy wiersza rozdzielone separatoerm
  283.                 // w kolejnych elementach zwracanej tablicy
  284.                 $record = explode($separator, trim($v));
  285.                 // jesli pasuje identyfikator tematu
  286.                 //echo $record[0];
  287.                 if ($record[1] == $topicid and $record[0] == $postid) {
  288.                    
  289.                     // przepakowanie do $posts[] i dekodowanie danych użytkownika
  290.                     $postToEdit[] = array(
  291.                         "postid" => $record[0],
  292.                         "topicid" => $record[1],
  293.                         "post" => hex2bin(bin2hex($post)),
  294.                         "username" => hex2bin(bin2hex($username)),
  295.                         "date" => $record[4]
  296.                     );
  297.                 } else {
  298.                     $postToEdit[] = array(
  299.                         "postid" => $record[0],
  300.                         "topicid" => $record[1],
  301.                         "post" => hex2bin($record[2]),
  302.                         "username" => hex2bin($record[3]),
  303.                         "date" => $record[4]
  304.                     );
  305.                 }
  306.             }
  307.             // zwraca tablice z wynikami
  308.         } else {
  309.             // zwraca kod błędu
  310.             return FALSE;
  311.         }
  312.        
  313.         $file = fopen($datafile, "w+");
  314.         fclose($file);
  315.        
  316.         //print_r($topics);
  317.        
  318.         foreach ($postToEdit as $value) {
  319.             $data2 = implode($separator, array(
  320.                 $value['postid'],
  321.                 $value['topicid'],
  322.                 bin2hex($value['post']),
  323.                 bin2hex($value['username']),
  324.                 $value['date']
  325.             ));
  326.             // zapis danych na końcu pliku
  327.             if ($fh = fopen($datafile, "a+")) {
  328.                 fwrite($fh, $data2 . "\n");
  329.                 fclose($fh);
  330.                
  331.             } else {
  332.                 return FALSE;
  333.             }
  334.         }
  335.     }
  336. }
  337.  
  338. function deletePost($topicid, $datafile = "wypowiedzi.txt", $separator = "&!@sep@#!#&")
  339. {
  340.    
  341.     $postid = $_GET['id'];
  342.    
  343.     // wczytanie pliku do tablicy stringów
  344.     if ($data = file($datafile)) {
  345.         // utworzenie pustej tablicy wynikowej
  346.         $posts = array();
  347.         // dla każdego elementu tablicy $data
  348.         //    $k - klucz ementu,  $v - wartość elementu
  349.         foreach ($data as $k => $v) {
  350.             // umieszcza kolejne elementy wiersza rozdzielone separatoerm
  351.             // w kolejnych elementach zwracanej tablicy
  352.             $record = explode($separator, trim($v));
  353.             // jesli pasuje identyfikator tematu
  354.             //echo $record[0];
  355.             if ($record[1] == $topicid and $record[0] == $postid) {
  356.                
  357.                 // przepakowanie do $posts[] i dekodowanie danych użytkownika
  358.             } else {
  359.                 $postToEdit[] = array(
  360.                     "postid" => $record[0],
  361.                     "topicid" => $record[1],
  362.                     "post" => hex2bin($record[2]),
  363.                     "username" => hex2bin($record[3]),
  364.                     "date" => $record[4]
  365.                 );
  366.             }
  367.         }
  368.         // zwraca tablice z wynikami
  369.     } else {
  370.         // zwraca kod błędu
  371.         return FALSE;
  372.     }
  373.    
  374.     $file = fopen($datafile, "w+");
  375.     fclose($file);
  376.    
  377.     //print_r($topics);
  378.     if ($posts != NULL) {
  379.         foreach ($postToEdit as $value) {
  380.             $data2 = implode($separator, array(
  381.                 $value['postid'],
  382.                 $value['topicid'],
  383.                 bin2hex($value['post']),
  384.                 bin2hex($value['username']),
  385.                 $value['date']
  386.             ));
  387.             // zapis danych na końcu pliku
  388.             if ($fh = fopen($datafile, "a+")) {
  389.                 fwrite($fh, $data2 . "\n");
  390.                 fclose($fh);
  391.                
  392.             } else {
  393.                 return FALSE;
  394.             }
  395.         }
  396.     }
  397.    
  398. }
  399.  
  400.  
  401. if (isset($_GET['topic'])) {
  402.     if (is_numeric($_GET['topic'])) {
  403.         $id = $_GET['topic'];
  404.     }
  405. }
  406.  
  407.  
  408. function clearFile($datafile = "tematy.txt", $datafile2 = "wypowiedzi.txt")
  409. {
  410.     $file = fopen($datafile, "w+");
  411.     fclose($file);
  412.    
  413.     $file2 = fopen($datafile2, "w+");
  414.     fclose($file2);
  415. }
  416.  
  417. //clearFile();
  418.  
  419.  
  420. if (isset($_POST["submit"])) {
  421.     if (!isset($_GET['topic'])) {
  422.         $topic      = htmlspecialchars($_POST["topic"]);
  423.         $topic_body = htmlspecialchars($_POST["topic_body"]);
  424.         $username   = htmlspecialchars($_POST["username"]);
  425.        
  426.         $topic      = preg_replace('/[^(\x20-\x7F)]*/', '', $topic);
  427.         $topic_body = preg_replace('/[^(\x20-\x7F)]*/', '', $topic_body);
  428.         $username   = preg_replace('/[^(\x20-\x7F)]*/', '', $username);
  429.        
  430.         if ($topic != NULL and $topic_body != NULL and $username != NULL)
  431.             put_topic($topic, $topic_body, $username);
  432.     } else {
  433.         $post     = htmlspecialchars($_POST["post"]);
  434.         $username = htmlspecialchars($_POST["username"]);
  435.        
  436.         $post     = preg_replace('/[^(\x20-\x7F)]*/', '', $post);
  437.         $username = preg_replace('/[^(\x20-\x7F)]*/', '', $username);
  438.        
  439.         if ($post != NULL and $username != NULL) {
  440.             put_post($id, $post, $username);
  441.            
  442.             updateCounter($id, true);
  443.         }
  444.     }
  445. }
  446.  
  447. $topics = get_topics();
  448.  
  449.  
  450. if (!isset($_GET['topic'])) {
  451.     echo "<p>Możesz dodac nowy temat za pomocą <a href='#topic_form'>formularza</a>.</p>";
  452.     foreach ((array) $topics as $key => $value) {
  453.         if ($value['topicid'] != 0) {
  454.             echo "<article class='topic'>
  455.                        <header> </header>
  456.                        <div><a href='?topic=" . $value['topicid'] . "'>" . $value['topic'] . "</a></div>
  457.                        <footer>ID: " . $value['topicid'] . ", Autor: " . $value['username'] . ", Utworzono: " . $value['date'] . ", Liczba wpisów: " . $value['postCounter'] . "</footer>
  458.                      </article>";
  459.         }
  460.     }
  461.    
  462. } else {
  463.     if (is_numeric($_GET['topic'])) {
  464.         $indeks = -1;
  465.         foreach ($topics as $key => $topic) {
  466.             if ($topic["topicid"] == $_GET["topic"])
  467.                 $indeks = $key;
  468.         }
  469.         echo '<nav>
  470.                                      <table><tr>
  471.                                      <td>';
  472.         if ($id > 1)
  473.             echo '<a style="float:left;" href="index.php?topic=' . ($id - 1) . '"><-- Poprzedni temat</a>';
  474.         echo '</td><td  style="width: 33%;">
  475.                                        <a href="index.php">Lista tematów</a>
  476.                                      </td><td  style="width: 33%;">';
  477.         if (isset($topics[$id]))
  478.             echo '<a  style="float:right;" href="index.php?topic=' . ($id + 1) . '">Następny temat --></a>';
  479.         echo '</td>
  480.                                      </tr></table>
  481.                                   </nav>';
  482.        
  483.         echo "<article  class='topic'>
  484.                                <header>Temat dyskusji: <b>" . $topics[$indeks]['topic'] . "</b></header>
  485.                                <div>" . $topics[$indeks]['topic_body'] . "</div>
  486.                                <footer>
  487.                                ID: " . $id . ", Autor: " . $topics[$indeks]['username'] . ", Data: " . $topics[$indeks]['date'] . " </footer>
  488.                              </article>";
  489.         echo "<p>Możesz dodac nowy post za pomocą <a href='#post_form'>formularza</a>.</p>";
  490.        
  491.     } else
  492.         return FALSE;
  493.    
  494. }
  495.  
  496. if (isset($_GET['topic'])) {
  497.     editPost($id);
  498. }
  499.  
  500. if (isset($_GET['cmd']) and $_GET['cmd'] == 'delete') {
  501.     deletePost($id);
  502.     updateCounter($id, false);
  503. }
  504.  
  505. if (isset($_GET['topic'])) {
  506.    
  507.     $posts = get_posts($id);
  508.     if ($posts != NULL) {
  509.         foreach ((array) $posts as $key => $value) {
  510.             echo '<article>
  511.                      <div>' . $value['post'] . '</div>
  512.                      <footer>
  513.                      <nav>
  514.                      <a href="?topic=' . $value["topicid"] . '&id=' . $value["postid"] . '&cmd=edit">EDYTUJ</a>  
  515.                      <a class="danger" href="?topic=' . $value["topicid"] . '&id=' . $value["postid"] . '&cmd=delete">KASUJ</a>
  516.                      </nav>
  517.                      ID: ' . $value["postid"] . ', Autor: ' . $value["username"] . ', Utworzono dnia: ' . $value["date"] . '</footer>
  518.                      </article>';
  519.         }
  520.     }
  521. }
  522.  
  523. $indeksPost = -1;
  524. if (isset($_GET['cmd']) and $_GET['cmd'] == 'edit') {
  525.     foreach ($posts as $key => $post) {
  526.         if ($post["postid"] == $_GET["id"])
  527.             $indeksPost = $key;
  528.     }
  529. }
  530.  
  531. if (!isset($_GET['topic'])) {
  532.     echo '<form action="index.php" method="post">
  533.                  <a name="topic_form"></a>
  534.                  <header><h2>Dodaj nowy temat do dyskusji</h2></header>
  535.                  <input type="text" name="topic" placeholder="Nowy temat" autofocus \><br />
  536.                 <textarea name="topic_body" cols="80" rows="10" placeholder="Opis nowego tematu" ></textarea><br />
  537.                 <input type="text" name="username" placeholder="Imię autora" \><br />
  538.                 <button type="submit" name="submit" >Zapisz</button>
  539.               </form>';
  540. } else {
  541.     echo '<form action="index.php?topic=' . $id . '" method="post">
  542.                  <a name="post_form"></a>
  543.                  <header><h2>Dodaj nowy post do dyskusji</h2></header>
  544.                 <textarea name="post" cols="80" rows="10" placeholder="Opis nowego postu" >';
  545.    
  546.     if (isset($_GET['cmd']) and $_GET['cmd'] == 'edit') {
  547.         echo $posts[$indeksPost]['post'] . '</textarea><br /><input type="text" name="username" placeholder="Imię autora" value ="' . $posts[$indeksPost]['username'] . '">';
  548.         echo "<input type='hidden' value='$_GET[id]' name='indekspostname'";
  549.         echo '<br /><button type="submit" name="submitEdit" >Zapisz</button></form>';
  550.        
  551.     } else {
  552.         echo '</textarea><br /><input type="text" name="username" placeholder="Imię autora"">
  553.                        <br /><button type="submit" name="submit" >Zapisz</button></form>';
  554.     }
  555. }
  556.  
  557.  
  558.  
  559.  
  560. ?>
  561. </section>
  562.  
  563.  
  564. <footer>
  565. Ostatni wpis na formu powstał dnia: <?php
  566. $posts2 = get_posts(NULL); //get all posts
  567. $oldest = $posts2[0]['date'];
  568. if ($posts2 != NULL) {
  569.     foreach ($posts2 as $value) {
  570.         if ($oldest < $value['date'])
  571.             $oldest = $value['date'];
  572.     }
  573. }
  574. echo $oldest;
  575. ?>
  576. </footer>
  577. </body>
  578. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement