bad line endings fixed
[specialops2.git] / post.php
blob8f2b03eb414200164304ee551d415564ee69bd5b
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`, `topic_level`, `post_level` FROM `boards`
39 WHERE `boardid` = '.$boardid)->fetch_row();
41 // Nonexistent board id
42 if ( !is_array($board) )
43 $page->errorfooter('boardid');
45 // Access control
46 if ( $board[2] > $user->level )
47 $page->errorfooter('level', $board[2]);
49 // If this is a real board and they're allowed in add the links
50 $page->nav['Topic List: '.$board[0]] = 'topiclist?'.$boardid;
51 if ( isset($topicid) )
52 $page->nav['Message List: '.$topic[0]] = 'messagelist?'.$topicid;
54 $user->userheader();
56 // Topic post level
57 if ( isset($_GET['board']) && $board[1] > $user->level )
58 $page->errorfooter('level', $board[1]);
60 // Waste their time even more
61 if ( ! ($user instanceof authuser) )
62 $page->errorfooter('login');
64 /* Form submit code */
65 if ( isset($_POST['post']) || isset($_POST['preview']) ) {
67 if ( 'none' === $_POST['html'] )
68 $html_type = message::M_HTML_NONE;
69 elseif ( LVL_ADMIN <= $user->level && 'all' === $_POST['html'] )
70 $html_type = message::M_HTML_ALL;
71 else
72 $html_type = message::M_HTML_FILTERED;
74 if ( isset($_POST['nobr']) )
75 $html_type |= message::M_NO_NEWLINES;
77 try {
78 $message = new message($_POST['message_text'], $html_type);
79 if ( !isset($topic) )
80 $topic_title = trim(htmlspecialchars($_POST['topic_title']));
82 if ( ($m = strlen(trim(strip_tags($message->output)))) < MSG_MIN_LENGTH )
83 throw new LengthException('Your message is %d characters too short.', MSG_MIN_LENGTH - $m);
84 if ( ($m = strlen($message->output)) > MSG_MAX_LENGTH )
85 throw new LengthException('Your message is %d characters too long.', $m - MSG_MAX_LENGTH);
87 if ( !isset($topic) ) {
88 $t = strlen($topic_title);
89 if ( $t < TOPIC_MIN_LENGTH )
90 throw new LengthException('Your topic title is %d characters too short.', TOPIC_MIN_LENGTH - $t);
91 if ( $t > TOPIC_MAX_LENGTH )
92 throw new LengthException('Your topic title is %d characters too long.', $t - TOPIC_MAX_LENGTH);
93 if ( $DB->query('SELECT COUNT(*) AS `c` FROM `topics`
94 WHERE `topic_title` = \''.$DB->escape_string($topic_title).'\'
95 AND `boardid` = '.$boardid)->fetch_object()->c )
96 throw new InvalidInputException('A topic with that name already exists.');
99 if ( isset($_POST['post']) ) {
100 $DB->query('START TRANSACTION');
102 if ( !isset($topic) ) {
103 $DB->query('INSERT INTO `topics` (`topic_title`, `boardid`, `userid`)
104 VALUES (
105 \''.$DB->escape_string($topic_title).'\',
106 '.$boardid.',
107 @userid
108 )');
109 $topicid = $DB->insert_id;
110 $user->points += 2;
112 else
113 $user->points++;
115 if ( !isset($messageid) )
116 $messageid = 'NULL';
118 $DB->query('INSERT INTO `messages` (`topicid`, `userid`, `mtime`, `replyto`, `origin_ip`)
119 VALUES (
120 '.$topicid.',
121 @userid,
122 UNIX_TIMESTAMP(),
123 '.$messageid.',
124 INET_ATON(\''.$_SERVER['REMOTE_ADDR'].'\')
125 )');
126 $DB->query('UPDATE `topics` SET `lastpost` = LAST_INSERT_ID() WHERE `topicid` = '.$topicid);
127 $DB->query('INSERT INTO `message-data` (`messageid`, `mtext`)
128 VALUES (
129 LAST_INSERT_ID(),
130 \''.$DB->escape_string($message->output).'\'
131 )');
132 $DB->commit();
134 $r = 'topiclist?'.$boardid;
135 if ( isset($topicid) )
136 $r = 'messagelist?'.$topicid;
138 $page->footer('<p class="notice">Message Posted. Return to from which you <a href="'.$r.'">came</a>.</p>');
140 } elseif ( isset($_POST['preview']) ) {
141 echo '<fieldset><legend>Message Preview</legend>',"\n",
142 ( isset($topic_title) ? '<h2>'.$topic_title."</h2>\n" : '' ),
143 ' <div class="info">From: ',$user->alias,' at ',$user->fdate(time()),"</div>\n",
144 ' <div class="content">',$message->output,"</div>\n",
145 '</fieldset>',"\n";
147 } catch ( InvalidInputException $e ) {
148 echo '<p class="error">',$e->getMessage(),"</p>\n";
149 } catch ( LengthException $e ) {
150 echo '<p class="error">',sprintf($e->getMessage(), $e->getCode()),"</p>\n";
151 } catch ( InvalidMessageException $e ) {
152 echo '<p class="error">Your message contains formatting errors (only the first error is shown):</p>',"\n",
153 '<p class="error">',$e->getMessage(),' at line ',$e->getXMLLine(),"</p>\n";
155 } // Form submit
157 $message = '';
158 if ( !empty($_POST['message_text']) )
159 $message = htmlspecialchars($_POST['message_text']);
160 elseif ( $user->sig )
161 $message = "\n".htmlspecialchars($user->sig);
163 if ( isset($messageid) )
164 echo '<form action="post?message=',$messageid,'" method="post">';
165 elseif ( isset($topic) )
166 echo '<form action="post?topic=',$topicid,'" method="post">';
167 else
168 echo '<form action="post?board=',$boardid,'" method="post">',"\n",
169 ' <fieldset class="content"><legend>Topic</legend>',"\n",
170 ' <input type="text" name="topic_title" maxlength="',TOPIC_MAX_LENGTH,'" size="80"',
171 ( !empty($topic_title) ? ' value="'.$topic_title.'"' : '' ),"/>\n",
172 " </fieldset>\n";
174 if ( empty($_POST['html']) )
175 $_POST['html'] = '';
177 $html = new form_select('html', 3, $_POST['html']);
178 if ( $user->level >= LVL_ADMIN )
179 $html->add_item('all', 'All');
180 $html->add_item('normal', 'Normal');
181 $html->add_item('none', 'Plaintext');
183 <fieldset class="content"><legend>Message</legend>
184 <textarea rows="15" cols="80" name="message_text" id="messagebox"><?php echo $message ?></textarea>
185 <button type="submit" name="post" value="post" accesskey="p">Post (P)</button>
186 <button type="submit" name="preview" value="preview" accesskey="r">Preview (R)</button>
187 <fieldset><legend>Options</legend>
188 <label for="html">HTML</label>
189 <?php echo $html->display(); ?><br/>
190 <label for="nobr">Disable automatic linebreaks</label>
191 <input type="checkbox" name="nobr" id="nobr"<?php if ( isset($_POST['nobr']) ) echo ' checked="checked"' ?>/>
192 </fieldset>
193 </fieldset>
194 </form>
196 <dl>
197 <dt>Default HTML allowed (no attributes):</dt>
198 <dd><?php echo implode(', ', message::$allowed_html); ?></dd>
199 </dl>
201 <?php
202 $page->pagefooter();