123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- function get_field_name($type) {
- switch ($type) {
- case 'dir':
- return 'dir';
- case 'image':
- return 'image';
- default:
- throw new Exception('Invalid $type');
- }
- }
- function get_comments($id, $type) {
- global $pdo;
- $sql = 'SELECT * FROM comments WHERE ' . get_field_name($type) . ' = :id ORDER BY date DESC';
- $values = array(
- ':id' => $id,
- );
- try {
- $res = $pdo->prepare($sql);
- $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- $comments = [];
- while($row = $res->fetch(PDO::FETCH_ASSOC)) {
- $comments[] = $row;
- }
- return $comments;
- }
- function count_comments($id, $type) {
- global $pdo;
- $sql = 'SELECT count(*) AS count FROM comments WHERE ' . get_field_name($type) . ' = :id';
- $values = array(
- ':id' => $id,
- );
- try {
- $res = $pdo->prepare($sql);
- $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- if($row = $res->fetch(PDO::FETCH_ASSOC)) {
- return $row['count'];
- }
- return 0;
- }
- function count_comments_all_dir_images($id) {
- global $pdo;
- $sql = 'SELECT count(*) AS count FROM comments WHERE image IN (SELECT id FROM images WHERE dir = :dir)';
- $values = array(
- ':dir' => $id,
- );
- try {
- $res = $pdo->prepare($sql);
- $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- if($row = $res->fetch(PDO::FETCH_ASSOC)) {
- return $row['count'];
- }
- return 0;
- }
- function db_store_comment($nick, $email, $type, $id, $content) {
- global $pdo;
- $sql = 'INSERT INTO comments (ip, ' . get_field_name($type) . ', email, author, date, content) VALUES (:ip, :id, :email, :nick, :date, :content)';
- $values = array (
- ':ip' => $_SERVER['REMOTE_ADDR'],
- ':id' => $id,
- ':email' => $email,
- ':nick' => $nick,
- ':date' => time(),
- ':content' => $content,
- );
- try {
- $res = $pdo->prepare($sql);
- $res->execute($values);
- } catch (PDOException $e) {
- echo 'Query error: ' . $e->getMessage();
- die();
- }
- }
- function generate_comment_field($comments, $type, $id) {
- $emptytexts = array(
- 'dir' => 'Brak komentarzy do tego katalogu, możesz być pierwszy!',
- 'image' => 'Brak komentarzy do tego obrazka, możesz być pierwszy!',
- );
- $headertexts = array(
- 'dir' => 'Komentarze do katalogu',
- 'image' => 'Komentarze do obrazka',
- );
- if (count($comments) == 0) {
- $output = '<h2>' . $emptytexts[$type] . '</h2>';
- } else {
- $output = '<h2>' . $headertexts[$type] . '</h2>';
- }
- foreach ($comments as $comment) {
- $output .= '<div class="comment-block"><div class="comment-header">Autor: <span class="comment-author">' . htmlspecialchars($comment['author']) . '</span>, data: <span class="comment-date">' . date('j.m.Y G:i:s', $comment['date']) . '</span></div><div class="comment-text">' . htmlspecialchars($comment['content']) . '</div></div>';
- }
- $output .= '<h2>Dodaj nowy komentarz</h2><form id="comment-form" method="post" action="sendcomment.php"><table id="comment-form-table"><tr><td class="form-caption">Autor:</td><td><input type="text" name="comment-nick" id="comment-nick"></td></tr>';
- $output .= '<tr><td class="form-caption">E-mail (nie pokażę go nikomu):</td><td><input type="text" name="comment-email" id="comment-email"></td></tr>';
- $output .= '<tr><td colspan="2"><textarea id="comment-content" name="comment-content"></textarea></td></tr></table>';
- $output .= '<input type="submit" value="Wyślij">';
- $output .= '<input type="hidden" name="comment-type" id="comment-type" value="' . $type . '"><input type="hidden" name="comment-id" id="comment-id" value="' . $id . '"></form>';
- return $output;
- }
- ?>
|