source: trunk/Modules/Forum/Forum.php@ 852

Last change on this file since 852 was 852, checked in by chronos, 9 years ago
  • Fixed: HTML entities need to be converted before BB code is applied as BB parser itself produce HTML code.
File size: 14.8 KB
Line 
1<?php
2
3class ModuleForum extends AppModule
4{
5 function __construct(System $System)
6 {
7 parent::__construct($System);
8 $this->Name = 'Forum';
9 $this->Version = '1.0';
10 $this->Creator = 'Maron';
11 $this->License = 'GNU/GPL';
12 $this->Description = '';
13 $this->Dependencies = array();
14 }
15
16 function DoStart()
17 {
18 $this->System->RegisterPage('forum', 'PageForum');
19 $this->System->ModuleManager->Modules['News']->RegisterRSS(array(
20 'Title' => T('Forum'), 'Channel' => 'forum', 'Callback' => array('PageForum', 'ShowRSS'),
21 'Permission' => LICENCE_ANONYMOUS));
22
23 if(array_key_exists('Search', $this->System->ModuleManager->Modules))
24 $this->System->ModuleManager->Modules['Search']->RegisterSearch('forum',
25 T('Forum'), array('UserName', 'Text'), '`ForumText`', $this->System->Link('/forum/?search='));
26 if(array_key_exists('Search', $this->System->ModuleManager->Modules))
27 $this->System->ModuleManager->Modules['Search']->RegisterSearch('forumthread',
28 T('Name of thread forum'), array('UserName', 'Text'), '`ForumThread`',
29 $this->System->Link('/forum/?search='));
30
31 $this->System->RegisterMenuItem(array(
32 'Title' => T('Forum'),
33 'Hint' => T('Forum about translation wow'),
34 'Link' => $this->System->Link('/forum/'),
35 'Permission' => LICENCE_ANONYMOUS,
36 'Icon' => '',
37 ), 17);
38 }
39
40 function ShowBox()
41 {
42 $Parser = new HTML_BBCodeParser2(array('filters' => array('Basic', 'Extended',
43 'Images', 'Links', 'Lists', 'Email')));
44 $Count = 20;
45 $Output = '<strong><a href="'.$this->System->Link('/forum/').'">'.T('Last forum posts').':</a></strong>';
46
47 $Query = 'SELECT `ForumText`.`Text`, `ForumText`.`Date`, `User`.`Name` AS `UserName`, '.
48 '`User`.`Id` AS `UserId`, `ForumText`.`Thread` '.
49 'FROM `ForumText` '.
50 'JOIN `User` ON `User`.`Id` = `ForumText`.`User` '.
51 'ORDER BY `Date` DESC LIMIT '.$Count;
52 $DbResult = $this->Database->query($Query);
53 $Output .= '<table class="MiniTable"><tr><th>'.T('Date').'</th><th>'.T('User').'</th><th>'.T('Post').'</th></tr>';
54 while($DbRow = $DbResult->fetch_assoc())
55 {
56 $Output .= '<tr>'.
57 '<td><a href="'.$this->System->Link('/forum/?Thread='.$DbRow['Thread']).'">'.HumanDate($DbRow['Date']).'</a></td>'.
58 '<td><a href="'.$this->System->Link('/user/?user='.$DbRow['UserId']).'">'.$DbRow['UserName'].'</a></td>'.
59 '<td>'.$Parser->qparse(htmlspecialchars($DbRow['Text'])).'</td>'.
60 '</tr>';
61 }
62 $Output .= '</table>';
63 return($Output);
64 }
65}
66
67class PageForum extends Page
68{
69 function Show()
70 {
71 $Output = '';
72 $this->Title = T('Forum');
73 if(array_key_exists('a', $_POST)) $Action = $_POST['a'];
74 else if(array_key_exists('a', $_GET)) $Action = $_GET['a'];
75 else $Action = '';
76 if (array_key_exists('Edit', $_GET)) {
77 if (array_key_exists('text', $_POST))
78 $Output .= $this->Edit();
79 $Output .= $this->ShowEditForm();
80
81 } else
82 if (array_key_exists('search', $_GET))
83 $Output .= $this->ShowSearchForum();
84 else
85 if (array_key_exists('Thread', $_GET)) {
86 $Output .= '<h3>'.T('Forum - Thread').'</h3>';
87 if($Action == 'add2') $Output .= $this->AddFinish();
88 if($this->System->User->Licence(LICENCE_USER)) $Output .= $this->ShowAddForm();
89 $Output .= $this->ShowList();
90 } else {
91 $Output .= '<h3>'.T('Forum - List of Thread').'</h3>';
92 if($Action == 'add2') $Output .= $this->AddFinish('ForumThread');
93 if($this->System->User->Licence(LICENCE_USER)) $Output .= $this->ShowAddFormThread();
94 $Output .= $this->ShowListThread();
95 }
96 return($Output);
97 }
98
99 function Edit()
100 {
101 $Output = '';
102 $Text = $_POST['text'];
103 $DbResult = $this->System->Database->query('UPDATE `ForumText` SET `Text`="'.$_POST['text'].'" WHERE `User` = '.$this->System->User->Id.' AND `ID` = '.$_GET['Edit']);
104 $Output .= ShowMessage(T('Text edited.'));
105 return ($Output);
106 }
107
108 function ShowEditForm()
109 {
110 $Output = '';
111 if($this->System->User->Licence(LICENCE_USER))
112 {
113 $DbResult = $this->System->Database->query('SELECT * FROM `ForumText` WHERE `User` = '.$this->System->User->Id.' AND `ID` = '.$_GET['Edit']);
114 if ( $DbResult->num_rows > 0) {
115 $DbRow = $DbResult->fetch_assoc();
116 $Output .= '<form action="?Edit='.$_GET['Edit'].'" method="post">'.
117 '<fieldset><legend>'.T('Edit message').'</legend>'.
118 T('User').': ';
119 if($this->System->User->Licence(LICENCE_USER)) $Output .= '<b>'.$this->System->User->Name.'</b><br />';
120 else $Output .= '<input type="text" name="user" /><br />';
121 $Output .= T('Message text').': ('.T('You can use').' <a href="http://www.bbcode.org/reference.php">'.T('BB code').'</a>)<br/>'.
122 '<textarea onkeydown="ResizeTextArea(this)" rows="8" name="text" cols="80">'.htmlspecialchars($DbRow['Text']).'</textarea> <br/>'.
123 '<input type="hidden" name="a" value="add2"/>'.
124 '<input type="submit" value="'.T('Send').'" /><br /></fieldset>'.
125 '</form>';
126 } else $Output .= ShowMessage(T('You have to be registered for adding message.'), MESSAGE_CRITICAL);
127 } else $Output .= ShowMessage(T('You can edit only your own message.'), MESSAGE_CRITICAL);
128 return($Output);
129 }
130
131 function ShowSearchForum()
132 {
133 $parser = new HTML_BBCodeParser2(array('filters' => array('Basic','Extended','Images','Links','Lists','Email')));
134 $Count = 20;
135 $Output = '';
136
137 $Output .= '<div class="shoutbox">';
138 $where = '`ForumText`.`Text` LIKE "%'.($_GET['search'] ).'%" OR '.
139 ' `ForumThread`.`Text` LIKE "%'.($_GET['search'] ).'%" OR `ForumThread`.`UserName` LIKE "%'.($_GET['search'] ).'%" OR '.
140 ' `ForumText`.`UserName` LIKE "%'.($_GET['search'] ).'%"';
141 $join = ' JOIN `ForumThread` ON `ForumThread`.`ID` = `ForumText`.`Thread`';
142
143 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `ForumText` '.$join.' WHERE '.$where);
144 $DbRow = $DbResult->fetch_row();
145 $PageList = GetPageList($DbRow[0]);
146
147 $Output .= $PageList['Output'];
148
149 $DbResult = $this->System->Database->query('SELECT `ForumText`.`Text`, `ForumText`.`Date`, `ForumText`.`UserName`,'.
150 '`ForumThread`.`Text` as `ThreadName`,`ForumText`.`Thread` FROM `ForumText` '.$join.' WHERE '.$where.' ORDER BY `ForumText`.`Date` DESC '.$PageList['SQLLimit']);
151 while($Line = $DbResult->fetch_assoc())
152 $Output .= '<div><a href="'.$this->System->Link('/forum/?Thread='.$Line['Thread']).'">'.
153 htmlspecialchars($Line['ThreadName']).'</a><br /><strong>'.$Line['UserName'].
154 '</strong> ('.HumanDate($Line['Date']).'): '.$parser->qparse(htmlspecialchars($Line['Text'])).'</div> ';
155 $Output .= '</div>'.$PageList['Output'];
156 return($Output);
157 }
158
159 function ShowListThread()
160 {
161 $Output = '';
162
163 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `ForumThread` WHERE 1');
164 $DbRow = $DbResult->fetch_row();
165 $PageList = GetPageList($DbRow[0]);
166
167 $Output .= $PageList['Output'];
168 $Output .= '<div class="shoutbox">';
169 $DbResult = $this->System->Database->query('SELECT * FROM `ForumThread` WHERE 1 ORDER BY `ID` DESC '.$PageList['SQLLimit']);
170 while($Line = $DbResult->fetch_assoc())
171 $Output .= '<div><span style="float:right;"><strong>'.$Line['UserName'].
172 '</strong> - ('.HumanDate($Line['Date']).')</span> <a href="?Thread='.$Line['ID'].'">'.
173 str_replace("\n", '', htmlspecialchars($Line['Text'])).'</a></div>';
174 $Output .= '</div>'.$PageList['Output'];
175 return($Output);
176 }
177
178 function ShowList()
179 {
180 $Output = '';
181
182 $parser = new HTML_BBCodeParser2(array('filters' => array('Basic','Extended','Images','Links','Lists','Email')));
183
184 if(array_key_exists('search', $_GET)) $_SESSION['search'] = $_GET['search'];
185 else if(!array_key_exists('search', $_SESSION)) $_SESSION['search'] = '';
186 if(array_key_exists('search', $_GET) and ($_GET['search'] == '')) $_SESSION['search'] = '';
187 if($_SESSION['search'] != '')
188 {
189 $SearchQuery = ' AND (`Text` LIKE "%'.$_SESSION['search'].'%")';
190 $Output .= '<div><a href="?search=">'.sprintf(T('Disable filter "%s"'), $_SESSION['search']).'</a></div>';
191 } else $SearchQuery = '';
192
193 $DbResult = $this->System->Database->query('SELECT * FROM `ForumThread` WHERE ID='.($_GET['Thread']*1).' LIMIT 1');
194 if($DbResult->num_rows > 0)
195 {
196 $Thread = $DbResult->fetch_assoc();
197 $Output .= '<h3>'.htmlspecialchars($Thread['Text']).'</h3>';
198
199 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `ForumText` WHERE `Thread` = '.($_GET['Thread']*1).' '.$SearchQuery);
200 $DbRow = $DbResult->fetch_row();
201 $PageList = GetPageList($DbRow[0]);
202
203 $Output .= $PageList['Output'];
204 $Output .= '<div class="shoutbox">';
205 $DbResult = $this->System->Database->query('SELECT * FROM `ForumText` WHERE `Thread` = '.
206 ($_GET['Thread']*1).' '.$SearchQuery.' ORDER BY `ID` DESC '.$PageList['SQLLimit']);
207 while($Line = $DbResult->fetch_assoc()) {
208 if ($this->System->User->Id == $Line['User'])
209 $edit = '<a href="?Edit='.$Line['ID'].'">'.T('edit').'</a>';
210 else $edit = '';
211 $Output .= '<div><span style="float:right;">'.$edit.' ('.HumanDate($Line['Date']).
212 ')</span><strong>'.$Line['UserName'].'</strong>: '.str_replace("\n", '<br />',
213 $parser->qparse(htmlspecialchars($Line['Text']))).' </div> ';
214 }
215 $Output .= '</div>'.$PageList['Output'];
216 } else $Output .= ShowMessage(T('Item not found'), MESSAGE_CRITICAL);
217 return($Output);
218 }
219
220 function ShowAddForm()
221 {
222 $Output = '';
223 if($this->System->User->Licence(LICENCE_USER))
224 {
225 $Output .= '<form action="?Thread='.$_GET['Thread'].'" method="post">'.
226 '<fieldset><legend>'.T('New Forum Message').'</legend>'.
227 T('User').': ';
228 if($this->System->User->Licence(LICENCE_USER)) $Output .= '<b>'.$this->System->User->Name.'</b><br />';
229 else $Output .= '<input type="text" name="user" /><br />';
230 $Output .= T('Message text').': ('.T('You can use').' <a href="http://www.bbcode.org/reference.php">'.T('BB code').'</a>)<br/>'.
231 '<textarea onkeydown="ResizeTextArea(this)" name="text" cols="80"></textarea> <br/>'.
232 '<input type="hidden" name="a" value="add2"/>'.
233 '<input type="submit" value="'.T('Send').'" /><br /></fieldset>'.
234 '</form>';
235 } else $Output .= ShowMessage(T('You have to be registered for adding message.'), MESSAGE_CRITICAL);
236 return($Output);
237 }
238
239 function ShowAddFormThread()
240 {
241 $Output = '';
242 if($this->System->User->Licence(LICENCE_USER))
243 {
244 $Output .= '<form action="?" method="post">'.
245 '<fieldset><legend>'.T('New thread').'</legend>'.
246 T('User').': ';
247 if($this->System->User->Licence(LICENCE_USER)) $Output .= '<b>'.$this->System->User->Name.'</b><br />';
248 else $Output .= '<input type="text" name="user" /><br />';
249 $Output .= T('Name of thread').': <br />'.
250 '<textarea onkeydown="ResizeTextArea(this)" name="text" rows="1" cols="30"></textarea> <br/>'.
251 '<input type="hidden" name="a" value="add2"/>'.
252 '<input type="submit" value="'.T('Send').'" /><br /></fieldset>'.
253 '</form>';
254 } else $Output .= ShowMessage(T('You have to be registered for adding message.'), MESSAGE_CRITICAL);
255 return($Output);
256 }
257
258 function AddFinish($Table = 'ForumText')
259 {
260 $Output = '';
261 if($this->System->User->Licence(LICENCE_USER))
262 {
263 if(array_key_exists('text', $_POST))
264 {
265 $Text = $_POST['text'];
266 if(trim($Text) == '') $Output .= ShowMessage('Nelze vložit prázdnou zprávu.', MESSAGE_WARNING);
267 else
268 {
269 // Protection against mutiple post of same message
270 $Thread = '';
271 if ($Table == 'ForumText') $Thread = 'AND `Thread` = '.$_GET['Thread'];
272 $DbResult = $this->System->Database->query('SELECT `Text` FROM `'.$Table.'` WHERE 1 '.$Thread.' AND (`User` = "'.
273 $this->System->User->Id.'") ORDER BY `Date` DESC LIMIT 1');
274 if($DbResult->num_rows > 0)
275 {
276 $DbRow = $DbResult->fetch_assoc();
277 } else $DbRow['Text'] = '';
278
279 if($DbRow['Text'] == $Text) $Output .= ShowMessage(T('You can\' add same message twice.'), MESSAGE_WARNING);
280 else
281 {
282 if ($Table == 'ForumText') {
283
284 $DbResult = $this->System->Database->query('SELECT * FROM `ForumThread` WHERE ID='.($_GET['Thread']*1).' LIMIT 1');
285 if($DbResult->num_rows > 0)
286 {
287 $this->System->Database->query('INSERT INTO `'.$Table.'` ( `User`, `UserName` , `Text` , `Date` , `IP` , `Thread` ) '.
288 ' VALUES ('.$this->System->User->Id.', "'.$this->System->User->Name.
289 '", "'.$Text.'", NOW(), "'.GetRemoteAddress().'","'.$_GET['Thread'].'")');
290 } else $Output .= ShowMessage(T('Item not found'), MESSAGE_CRITICAL);
291 } else
292 $this->System->Database->query('INSERT INTO `'.$Table.'` ( `User`, `UserName` , `Text` , `Date` , `IP`) '.
293 ' VALUES ('.$this->System->User->Id.', "'.$this->System->User->Name.
294 '", "'.$Text.'", NOW(), "'.GetRemoteAddress().'")');
295 $Output .= ShowMessage(T('Added.'));
296 }
297 }
298 } else $Output .= ShowMessage(T('You have to specified new message.'), MESSAGE_CRITICAL);
299 $Output .= '<br/>';
300 } else $Output .= ShowMessage(T('You have to be registered for adding message.'), MESSAGE_CRITICAL);
301 return($Output);
302 }
303
304 function ShowRSS()
305 {
306 $parser = new HTML_BBCodeParser2(array('filters' => array('Basic','Extended','Images','Links','Lists','Email')));
307
308 $Items = array();
309 $TitleLength = 50;
310 mb_internal_encoding('utf-8');
311 $DbResult = $this->Database->query('SELECT `Thread`, `ID`, UNIX_TIMESTAMP(`Date`) AS `UnixDate`, '.
312 '`User`, `UserName`, `Text`, ( SELECT `Text` FROM `ForumThread` '.
313 'WHERE `ID` = `ForumText`.`Thread`) AS `ThreadText` FROM `ForumText` ORDER BY `ID` DESC LIMIT 20');
314 while($DbRow = $DbResult->fetch_assoc())
315 {
316 $Title = mb_substr($DbRow['Text'], 0, $TitleLength);
317 if(mb_strlen($Title) == $TitleLength) $Title .= '...';
318 $Items[] = array
319 (
320 'Title' => htmlspecialchars($DbRow['ThreadText']).' - '.$DbRow['UserName'].': ',
321 'Link' => 'http://'.$this->System->Config['Web']['Host'].$this->System->Link('/forum/?Thread='.$DbRow['Thread']),
322 'Description' => $parser->qparse(htmlspecialchars($DbRow['Text'])),
323 'Time' => $DbRow['UnixDate'],
324 );
325 }
326 $Output = GenerateRSS(array
327 (
328 'Title' => $this->System->Config['Web']['Title'].' - '.T('Forum'),
329 'Link' => 'http://'.$this->System->Config['Web']['Host'].$this->System->Link('/forum/'),
330 'Description' => $this->System->Config['Web']['Description'],
331 'WebmasterEmail' => $this->System->Config['Web']['AdminEmail'],
332 'Items' => $Items,
333 ));
334 return($Output);
335 }
336}
Note: See TracBrowser for help on using the repository browser.