Made logins apply to whole site instead of boards.
[specialops2.git] / post.php
blob5d0f645da07b78da46b3e57052d9f8df3c345ab0
1 <?php
2 // $Id$
4 $prefetch = array('`points`', '`sig`');
5 require 'con.php';
6 $page->title = 'Post Message';
8 define('MSG_MIN_LENGTH', 3);
9 define('MSG_MAX_LENGTH', 10000);
10 define('TOPIC_MIN_LENGTH', 3);
11 define('TOPIC_MAX_LENGTH', 60);
13 if ( isset($_GET['message']) ) {
14 $messageid = intval($_GET['message']);
15 $q = $DB->query('SELECT `topicid` FROM `messages` WHERE `messageid` = '.$messageid)->fetch_row();
17 if ( !is_array($q) )
18 $page->errorfooter('messageid');
20 $topicid = $q[0];
21 $topic = $DB->query('SELECT `topic_title`, `boardid` FROM `topics` WHERE `topicid` = '.$topicid)->fetch_row();
23 if ( !is_array($topic) )
24 $page->errorfooter('topicid');
26 $boardid = $topic[1];
27 } elseif ( isset($_GET['topic']) ) {
28 $topicid = intval($_GET['topic']);
29 $topic = $DB->query('SELECT `topic_title`, `boardid` FROM `topics` WHERE `topicid` = '.$topicid)->fetch_row();
31 if ( !is_array($topic) )
32 $page->errorfooter('topicid');
34 $boardid = $topic[1];
35 } else
36 $boardid = intval($_GET['board']);
38 $board = $DB->query('SELECT `board_name`, `restrict` FROM `boards` WHERE `boardid` = '.$boardid)->fetch_row();
40 // This board doesn't exist
41 if ( !is_array($board) )
42 $page->errorfooter('boardid');
44 // Board view restriction is on
45 if ( $board[1] >= 3 && !defined('DEVELOPER') )
46 $page->errorfooter('level', $board[2]);
48 // If they get this far they're probably allowed to view these pages too
49 $page->nav['Topic List: '.$board[0]] = 'topiclist?'.$boardid;
50 if ( isset($topicid) )
51 $page->nav['Message List: '.$topic[0]] = 'messagelist?'.$topicid;
53 // You're supposed to be logged in to even see the links to this page.
54 if ( ! ($user instanceof authuser) )
55 $page->errorfooter('login');
57 $user->userheader();
59 // Check whether they can post at all
60 if ( $board[1] >= 2 && !defined('DEVELOPER') )
61 $page->errorfooter('level', $board[1]);
62 // Then check whether they're allowed to post a topic
63 if ( isset($_GET['board']) && $board[1] >= 1 && !defined('DEVELOPER') )
64 $page->errorfooter('level', $board[1]);
67 /* Form submit code */
68 if ( isset($_POST['post']) || isset($_POST['preview']) ) {
70 if ( 'none' === $_POST['html'] )
71 $html_type = message::M_HTML_NONE;
72 elseif ( defined('DEVELOPER') && 'all' === $_POST['html'] )
73 $html_type = message::M_HTML_ALL;
74 else
75 $html_type = message::M_HTML_FILTERED;
77 if ( isset($_POST['nobr']) )
78 $html_type |= message::M_NO_NEWLINES;
80 try {
81 $message = new message($_POST['message_text'], $html_type);
82 if ( !isset($topic) )
83 $topic_title = trim(htmlspecialchars($_POST['topic_title']));
85 if ( ($m = strlen(trim(strip_tags($message->output)))) < MSG_MIN_LENGTH )
86 throw new LengthException('Your message is %d characters too short.', MSG_MIN_LENGTH - $m);
87 if ( ($m = strlen($message->output)) > MSG_MAX_LENGTH )
88 throw new LengthException('Your message is %d characters too long.', $m - MSG_MAX_LENGTH);
90 if ( !isset($topic) ) {
91 $t = strlen($topic_title);
92 if ( $t < TOPIC_MIN_LENGTH )
93 throw new LengthException('Your topic title is %d characters too short.', TOPIC_MIN_LENGTH - $t);
94 if ( $t > TOPIC_MAX_LENGTH )
95 throw new LengthException('Your topic title is %d characters too long.', $t - TOPIC_MAX_LENGTH);
96 if ( $DB->query('SELECT COUNT(*) AS `c` FROM `topics`
97 WHERE `topic_title` = \''.$DB->escape_string($topic_title).'\'
98 AND `boardid` = '.$boardid)->fetch_object()->c )
99 throw new InvalidInputException('A topic with that name already exists.');
102 if ( isset($_POST['post']) ) {
103 $DB->query('START TRANSACTION');
105 if ( !isset($topic) ) {
106 $DB->query('INSERT INTO `topics` (`topic_title`, `boardid`, `userid`)
107 VALUES (
108 \''.$DB->escape_string($topic_title).'\',
109 '.$boardid.',
110 @userid
111 )');
112 $topicid = $DB->insert_id;
113 $user->points += 2;
115 else
116 $user->points++;
118 if ( !isset($messageid) )
119 $messageid = 'NULL';
121 $DB->query('INSERT INTO `messages` (`topicid`, `userid`, `mtime`, `replyto`, `origin_ip`)
122 VALUES (
123 '.$topicid.',
124 @userid,
125 UNIX_TIMESTAMP(),
126 '.$messageid.',
127 INET_ATON(\''.$_SERVER['REMOTE_ADDR'].'\')
128 )');
129 $DB->query('UPDATE `topics` SET `lastpost` = LAST_INSERT_ID() WHERE `topicid` = '.$topicid);
130 $DB->query('INSERT INTO `message-data` (`messageid`, `mtext`)
131 VALUES (
132 LAST_INSERT_ID(),
133 \''.$DB->escape_string($message->output).'\'
134 )');
135 $DB->commit();
137 $r = 'topiclist?'.$boardid;
138 if ( isset($topicid) )
139 $r = 'messagelist?'.$topicid;
141 echo '<p class="notice">Message Posted. Return to from which you <a href="'.$r.'">came</a>.</p>';
142 $page->pagefooter();
144 } elseif ( isset($_POST['preview']) ) {
145 echo '<fieldset><legend>Message Preview</legend>',"\n",
146 ( isset($topic_title) ? '<h2>'.$topic_title."</h2>\n" : '' ),
147 ' <div class="info">From: ',$user->alias,' at ',$user->fdate(time()),"</div>\n",
148 ' <div class="content">',$message->output,"</div>\n",
149 '</fieldset>',"\n";
151 } catch ( InvalidInputException $e ) {
152 echo '<p class="error">',$e->getMessage(),"</p>\n";
153 } catch ( LengthException $e ) {
154 echo '<p class="error">',sprintf($e->getMessage(), $e->getCode()),"</p>\n";
155 } catch ( InvalidMessageException $e ) {
156 echo '<p class="error">Your message contains formatting errors (only the first error is shown):</p>',"\n",
157 '<p class="error">',$e->getMessage(),' at line ',$e->getXMLLine(),"</p>\n";
159 } // End of submit bit
162 $message = '';
163 if ( !empty($_POST['message_text']) )
164 $message = htmlspecialchars($_POST['message_text']);
165 elseif ( $user->sig )
166 $message = "\n".htmlspecialchars($user->sig);
168 if ( isset($messageid) )
169 echo '<form action="post?message=',$messageid,'" method="post">';
170 elseif ( isset($topic) )
171 echo '<form action="post?topic=',$topicid,'" method="post">';
172 else
173 echo '<form action="post?board=',$boardid,'" method="post">',"\n",
174 ' <fieldset class="content"><legend>Topic</legend>',"\n",
175 ' <input type="text" name="topic_title" maxlength="',TOPIC_MAX_LENGTH,'" size="80"',
176 ( !empty($topic_title) ? ' value="'.$topic_title.'"' : '' ),"/>\n",
177 " </fieldset>\n";
179 if ( empty($_POST['html']) )
180 $_POST['html'] = '';
182 $html = new form_select('html', 3, $_POST['html']);
183 if ( defined('DEVELOPER') )
184 $html->add_item('all', 'All');
185 $html->add_item('normal', 'Normal');
186 $html->add_item('none', 'Plaintext');
188 <fieldset class="content"><legend>Message</legend>
189 <textarea rows="15" cols="80" name="message_text" id="messagebox"><?php echo $message ?></textarea>
190 <button type="submit" name="post" value="post" accesskey="p">Post (P)</button>
191 <button type="submit" name="preview" value="preview" accesskey="r">Preview (R)</button>
192 <fieldset><legend>Options</legend>
193 <label for="html">HTML</label>
194 <?php echo $html->display(); ?><br/>
195 <label for="nobr">Disable automatic linebreaks</label>
196 <input type="checkbox" name="nobr" id="nobr"<?php if ( isset($_POST['nobr']) ) echo ' checked="checked"' ?>/>
197 </fieldset>
198 </fieldset>
199 </form>
201 <dl>
202 <dt>Default HTML allowed (no attributes):</dt>
203 <dd><?php echo implode(', ', message::$allowed_html); ?></dd>
204 </dl>
206 <?php
207 $page->pagefooter();