123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- require_once 'session.php';
- require_once 'database.php';
- require_once 'date.php';
- require_once 'twig.php';
- $db = new Database();
- $db->connect();
- if (!Session::is_valid ())
- {
- header ('Location: ./');
- exit ();
- }
- if ($_SERVER['REQUEST_METHOD'] === 'POST')
- {
-
- if (isset ($_POST['comment']))
- {
- $comment = $db->get_comment ($_POST['comment']);
-
-
- if ($comment['userId'] != Session::get_userid ())
- {
- header ('Location: ./');
- exit ();
- }
-
- $new_comment_data =
- [
- 'text' => isset ($_POST['text']) ? trim ($_POST['text']) : ''
- ];
-
- $db->edit_comment (
- $new_comment_data['text'],
- $comment['hashId'],
- Session::get_userid ());
-
- header ('Location: ./post/' . $comment['postHashId'] . '#comment-' . $comment['hashId']);
- exit ();
- }
-
-
- if (isset ($_POST['post']))
- {
- $post = $db->get_post ($_POST['post']);
-
-
- if ($post['userId'] != Session::get_userid ())
- {
- header ('Location: ./');
- exit ();
- }
-
-
- $new_post_data =
- [
- 'title' => isset ($_POST['title']) ? trim ($_POST['title']) : '',
- 'link' => isset ($_POST['link']) ? trim ($_POST['link']) : '',
- 'text' => isset ($_POST['text']) ? trim ($_POST['text']) : ''
- ];
-
-
- if (strlen ($new_post_data['title']) == 0)
- $new_post_data['title'] = $post['title'];
-
-
- if (strlen ($new_post_data['link']) > 0)
- {
- $link_components = parse_url ($new_post_data['link']);
-
-
- if (!isset ($link_components['scheme']))
- $new_post_data['link'] = 'http://' . $new_post_data['link'];
- }
-
- $db->edit_post (
- $new_post_data['title'],
- $new_post_data['link'],
- $new_post_data['text'],
- $post['hashId'],
- Session::get_userid ());
-
- header ('Location: ./post/' . $post['hashId']);
- exit ();
- }
-
-
-
- header ('Location: ./');
- exit ();
- }
- if (!isset ($_GET['post']) && !isset ($_GET['comment']))
- {
- header ('Location: ./');
- exit ();
- }
- if (isset ($_GET['post']))
- $item = array(
- 'type' => 'post',
- 'data' => $db->get_post ($_GET['post']));
- else
- $item = array(
- 'type' => 'comment',
- 'data' => $db->get_comment ($_GET['comment']));
- if ($item['data']['userId'] != Session::get_userid ())
- {
- header ('Location: ./');
- exit ();
- }
- switch ($item['type'])
- {
- case 'comment':
- $template = 'edit_comment.twig';
- break;
-
- case 'post':
- $template = 'edit_post.twig';
- break;
- }
- echo $twig->render (
- $template,
- array ('item' => $item));
-
-
-
-
-
-
|