Vanilla commit.
[tinybbs.git] / edit_content.php
blobfb47a318305b25322115a39c97b07fb34bdbc00f
1 <?php
3 require('includes/header.php');
5 if( ! $administrator)
7 add_error('You are not wise enough.', true);
10 $page_data = array();
12 if($_POST['form_sent'])
14 $page_data['url'] = ltrim($_POST['url'], '/');
15 $page_data['title'] = $_POST['title'];
16 $page_data['content'] = $_POST['content'];
19 if($_GET['edit'])
21 if( ! ctype_digit($_GET['edit']))
23 add_error('Invalid page ID.', true);
26 $stmt = $link->prepare('SELECT url, page_title, content FROM pages WHERE id = ?');
27 $stmt->bind_param('i', $_GET['edit']);
28 $stmt->execute();
29 $stmt->store_result();
30 if($stmt->num_rows < 1)
32 $page_title = 'Non-existent page';
33 add_error('There is no page with that ID.', true);
35 if( ! $_POST['form_sent'])
37 $stmt->bind_result($page_data['url'], $page_data['title'], $page_data['content']);
38 $stmt->fetch();
40 $stmt->close();
42 $editing = true;
43 $page_title = 'Editing page: <a href="/' . $page_data['url'] . '">' . htmlspecialchars($page_data['title']) . '</a>';
45 $page_data['id'] = $_GET['edit'];
47 else // new page
49 $page_title = 'New page';
50 if( ! empty($page_data['title']))
52 $page_title .= ': ' . htmlspecialchars($page_data['title']);
56 if($_POST['post'])
58 check_token();
60 if(empty($page_data['url']))
62 add_error('A path is required.');
65 if( ! $erred)
67 // Undo the effects of sanitize_for_textarea:
68 $page_data['content'] = str_replace('&#47;textarea', '/textarea', $page_data['content']);
70 if($editing)
72 $edit_page = $link->prepare('UPDATE pages SET url = ?, page_title = ?, content = ? WHERE id = ?');
73 $edit_page->bind_param('sssi', $page_data['url'], $page_data['title'], $page_data['content'], $page_data['id']);
74 $edit_page->execute();
75 $edit_page->close();
77 $notice = 'Page successfully edited.';
79 else // new page
81 $add_page = $link->prepare('INSERT INTO pages (url, page_title, content) VALUES (?, ?, ?)');
82 $add_page->bind_param('sss', $page_data['url'], $page_data['title'], $page_data['content']);
83 $add_page->execute();
84 $add_page->close();
86 $notice = 'Page successfully created.';
89 redirect($notice, $page_data['url']);
93 print_errors();
95 if( $_POST['preview'] && ! empty($page_data['content']) && check_token() )
97 echo '<h3 id="preview">Preview</h3><div class="body standalone"> <h2>' . $page_data['title'] . '</h2>' . $page_data['content'] . '</div>';
102 <form action="" method="post">
103 <?php csrf_token() ?>
104 <div class="noscreen">
105 <input type="hidden" name="form_sent" value="1" />
106 </div>
108 <div class="row">
109 <label for="url">Path</label>
110 <input id="url" name="url" value="<?php echo htmlspecialchars($page_data['url']) ?>" />
111 </div>
113 <div class="row">
114 <label for="title">Page title</label>
115 <input id="title" name="title" value="<?php echo htmlspecialchars($page_data['title']) ?>" />
116 </div>
118 <div class="row">
119 <textarea id="content" name="content" cols="120" rows="18"><?php echo sanitize_for_textarea($page_data['content']) ?></textarea>
120 <p>Use pure HTML.</p>
121 </div>
123 <div class="row">
124 <input type="submit" name="preview" value="Preview" class="inline" />
125 <input type="submit" name="post" value="Submit" class="inline">
126 </div>
127 </form>
129 <?php
131 require('includes/footer.php');