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

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