source: trunk/Modules/Forum/Forum.php

Last change on this file was 902, checked in by chronos, 2 weeks ago
  • Fixed: Showing date from null values.
  • Fixed_ Forum id validity check.
File size: 14.8 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 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
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 } else
81 if (array_key_exists('search', $_GET))
82 $Output .= $this->ShowSearchForum();
83 else
84 if (array_key_exists('Thread', $_GET)) {
85 $Output .= '<h3>'.T('Forum - Thread').'</h3>';
86 if ($Action == 'add2') $Output .= $this->AddFinish();
87 if ($User->Licence(LICENCE_USER)) $Output .= $this->ShowAddForm();
88 $Output .= $this->ShowList();
89 } else {
90 $Output .= '<h3>'.T('Forum - List of Thread').'</h3>';
91 if ($Action == 'add2') $Output .= $this->AddFinish('ForumThread');
92 if ($User->Licence(LICENCE_USER)) $Output .= $this->ShowAddFormThread();
93 $Output .= $this->ShowListThread();
94 }
95 return $Output;
96 }
97
98 function Edit()
99 {
100 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
101 $Output = '';
102 $this->System->Database->query('UPDATE `ForumText` SET `Text`="'.$_POST['text'].'" WHERE `User` = '.$User->Id.' AND `ID` = '.$_GET['Edit']);
103 $Output .= ShowMessage(T('Text edited.'));
104 return $Output;
105 }
106
107 function ShowEditForm()
108 {
109 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
110 $Output = '';
111 if ($User->Licence(LICENCE_USER))
112 {
113 $DbResult = $this->System->Database->query('SELECT * FROM `ForumText` WHERE `User` = '.$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 ($User->Licence(LICENCE_USER)) $Output .= '<b>'.$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 $Output = '';
134
135 $Output .= '<div class="shoutbox">';
136 $where = '`ForumText`.`Text` LIKE "%'.($_GET['search'] ).'%" OR '.
137 ' `ForumThread`.`Text` LIKE "%'.($_GET['search'] ).'%" OR `ForumThread`.`UserName` LIKE "%'.($_GET['search'] ).'%" OR '.
138 ' `ForumText`.`UserName` LIKE "%'.($_GET['search'] ).'%"';
139 $join = ' JOIN `ForumThread` ON `ForumThread`.`ID` = `ForumText`.`Thread`';
140
141 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `ForumText` '.$join.' WHERE '.$where);
142 $DbRow = $DbResult->fetch_row();
143 $PageList = GetPageList($DbRow[0]);
144
145 $Output .= $PageList['Output'];
146
147 $DbResult = $this->System->Database->query('SELECT `ForumText`.`Text`, `ForumText`.`Date`, `ForumText`.`UserName`,'.
148 '`ForumThread`.`Text` as `ThreadName`,`ForumText`.`Thread` FROM `ForumText` '.$join.' WHERE '.$where.' ORDER BY `ForumText`.`Date` DESC '.$PageList['SQLLimit']);
149 while ($Line = $DbResult->fetch_assoc())
150 $Output .= '<div><a href="'.$this->System->Link('/forum/?Thread='.$Line['Thread']).'">'.
151 htmlspecialchars($Line['ThreadName']).'</a><br /><strong>'.$Line['UserName'].
152 '</strong> ('.HumanDate($Line['Date']).'): '.ShowBBcodes(htmlspecialchars($Line['Text'])).'</div> ';
153 $Output .= '</div>'.$PageList['Output'];
154 return $Output;
155 }
156
157 function ShowListThread()
158 {
159 $Output = '';
160
161 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `ForumThread` WHERE 1');
162 $DbRow = $DbResult->fetch_row();
163 $PageList = GetPageList($DbRow[0]);
164
165 $Output .= $PageList['Output'];
166 $Output .= '<div class="shoutbox">';
167 $DbResult = $this->System->Database->query('SELECT * FROM `ForumThread` WHERE 1 ORDER BY `ID` DESC '.$PageList['SQLLimit']);
168 while ($Line = $DbResult->fetch_assoc())
169 {
170 $Output .= '<div><span style="float:right;"><strong>'.$Line['UserName'].
171 '</strong> - ('.HumanDate($Line['Date']).')</span> <a href="?Thread='.$Line['ID'].'">'.
172 str_replace("\n", '', htmlspecialchars($Line['Text'])).'</a></div>';
173 }
174 $Output .= '</div>'.$PageList['Output'];
175 return $Output;
176 }
177
178 function ShowList()
179 {
180 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
181 $Output = '';
182
183 if (array_key_exists('search', $_GET)) $_SESSION['search'] = $_GET['search'];
184 else if (!array_key_exists('search', $_SESSION)) $_SESSION['search'] = '';
185 if (array_key_exists('search', $_GET) and ($_GET['search'] == '')) $_SESSION['search'] = '';
186 if ($_SESSION['search'] != '')
187 {
188 $SearchQuery = ' AND (`Text` LIKE "%'.$_SESSION['search'].'%")';
189 $Output .= '<div><a href="?search=">'.sprintf(T('Disable filter "%s"'), $_SESSION['search']).'</a></div>';
190 } else $SearchQuery = '';
191
192 $ThreadId = 0;
193 if (TryGetUrlParameterInt('Thread', $ThreadId))
194 {
195 $DbResult = $this->System->Database->query('SELECT * FROM `ForumThread` WHERE ID='.$ThreadId.' LIMIT 1');
196 if ($DbResult->num_rows > 0)
197 {
198 $Thread = $DbResult->fetch_assoc();
199 $Output .= '<h3>'.htmlspecialchars($Thread['Text']).'</h3>';
200
201 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `ForumText` WHERE `Thread` = '.$ThreadId.' '.$SearchQuery);
202 $DbRow = $DbResult->fetch_row();
203 $PageList = GetPageList($DbRow[0]);
204
205 $Output .= $PageList['Output'];
206 $Output .= '<div class="shoutbox">';
207 $DbResult = $this->System->Database->query('SELECT * FROM `ForumText` WHERE `Thread` = '.
208 $ThreadId.' '.$SearchQuery.' ORDER BY `ID` DESC '.$PageList['SQLLimit']);
209 while ($Line = $DbResult->fetch_assoc())
210 {
211 if ($User->Id == $Line['User'])
212 {
213 $Edit = '<a href="?Edit='.$Line['ID'].'">'.T('edit').'</a>';
214 } else $Edit = '';
215 $Text = str_replace("\n", '<br />', ShowBBcodes(htmlspecialchars($Line['Text'])));
216 $Output .= '<div><span style="float:right;">'.$Edit.' ('.HumanDate($Line['Date']).
217 ')</span><strong>'.$Line['UserName'].'</strong>: '.$Text.' </div> ';
218 }
219 $Output .= '</div>'.$PageList['Output'];
220 } else $Output .= ShowMessage(T('Item not found'), MESSAGE_CRITICAL);
221 } else $Output .= ShowMessage(T('Id not found'), MESSAGE_CRITICAL);
222 return $Output;
223 }
224
225 function ShowAddForm()
226 {
227 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
228 $Output = '';
229 if ($User->Licence(LICENCE_USER))
230 {
231 $Output .= '<form action="?Thread='.$_GET['Thread'].'" method="post">'.
232 '<fieldset><legend>'.T('New Forum Message').'</legend>'.
233 T('User').': ';
234 if ($User->Licence(LICENCE_USER)) $Output .= '<b>'.$User->Name.'</b><br />';
235 else $Output .= '<input type="text" name="user" /><br />';
236 $Output .= T('Message text').': ('.T('You can use').' <a href="http://www.bbcode.org/reference.php">'.T('BB code').'</a>)<br/>'.
237 '<textarea onkeydown="ResizeTextArea(this)" name="text" cols="80"></textarea> <br/>'.
238 '<input type="hidden" name="a" value="add2"/>'.
239 '<input type="submit" value="'.T('Send').'" /><br /></fieldset>'.
240 '</form>';
241 } else $Output .= ShowMessage(T('You have to be registered for adding message.'), MESSAGE_CRITICAL);
242 return $Output;
243 }
244
245 function ShowAddFormThread()
246 {
247 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
248 $Output = '';
249 if ($User->Licence(LICENCE_USER))
250 {
251 $Output .= '<form action="?" method="post">'.
252 '<fieldset><legend>'.T('New thread').'</legend>'.T('User').': ';
253 if ($User->Licence(LICENCE_USER)) $Output .= '<b>'.$User->Name.'</b><br />';
254 else $Output .= '<input type="text" name="user" /><br />';
255 $Output .= T('Name of thread').': <br />'.
256 '<textarea onkeydown="ResizeTextArea(this)" name="text" rows="1" cols="30"></textarea> <br/>'.
257 '<input type="hidden" name="a" value="add2"/>'.
258 '<input type="submit" value="'.T('Send').'" /><br /></fieldset>'.
259 '</form>';
260 } else $Output .= ShowMessage(T('You have to be registered for adding message.'), MESSAGE_CRITICAL);
261 return $Output;
262 }
263
264 function AddFinish($Table = 'ForumText')
265 {
266 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
267 $Output = '';
268 if ($User->Licence(LICENCE_USER))
269 {
270 if (array_key_exists('text', $_POST))
271 {
272 $Text = $_POST['text'];
273 if (trim($Text) == '') $Output .= ShowMessage('Nelze vložit prázdnou zprávu.', MESSAGE_WARNING);
274 else
275 {
276 // Protection against mutiple post of same message
277 $Thread = '';
278 if ($Table == 'ForumText') $Thread = 'AND `Thread` = '.$_GET['Thread'];
279 $DbResult = $this->System->Database->query('SELECT `Text` FROM `'.$Table.'` WHERE 1 '.$Thread.' AND (`User` = "'.
280 $User->Id.'") ORDER BY `Date` DESC LIMIT 1');
281 if ($DbResult->num_rows > 0)
282 {
283 $DbRow = $DbResult->fetch_assoc();
284 } else $DbRow['Text'] = '';
285
286 if ($DbRow['Text'] == $Text) $Output .= ShowMessage(T('You can\' add same message twice.'), MESSAGE_WARNING);
287 else
288 {
289 if ($Table == 'ForumText') {
290
291 $DbResult = $this->System->Database->query('SELECT * FROM `ForumThread` WHERE ID='.($_GET['Thread']*1).' LIMIT 1');
292 if ($DbResult->num_rows > 0)
293 {
294 $this->System->Database->query('INSERT INTO `'.$Table.'` ( `User`, `UserName` , `Text` , `Date` , `IP` , `Thread` ) '.
295 ' VALUES ('.$User->Id.', "'.$User->Name.
296 '", "'.$Text.'", NOW(), "'.GetRemoteAddress().'","'.$_GET['Thread'].'")');
297 } else $Output .= ShowMessage(T('Item not found'), MESSAGE_CRITICAL);
298 } else
299 $this->System->Database->query('INSERT INTO `'.$Table.'` ( `User`, `UserName` , `Text` , `Date` , `IP`) '.
300 ' VALUES ('.$User->Id.', "'.$User->Name.
301 '", "'.$Text.'", NOW(), "'.GetRemoteAddress().'")');
302 $Output .= ShowMessage(T('Added.'));
303 }
304 }
305 } else $Output .= ShowMessage(T('You have to specified new message.'), MESSAGE_CRITICAL);
306 $Output .= '<br/>';
307 } else $Output .= ShowMessage(T('You have to be registered for adding message.'), MESSAGE_CRITICAL);
308 return $Output;
309 }
310
311 function ShowRSS()
312 {
313 $Items = array();
314 $TitleLength = 50;
315 mb_internal_encoding('utf-8');
316 $DbResult = $this->Database->query('SELECT `Thread`, `ID`, UNIX_TIMESTAMP(`Date`) AS `UnixDate`, '.
317 '`User`, `UserName`, `Text`, ( SELECT `Text` FROM `ForumThread` '.
318 'WHERE `ID` = `ForumText`.`Thread`) AS `ThreadText` FROM `ForumText` ORDER BY `ID` DESC LIMIT 20');
319 while ($DbRow = $DbResult->fetch_assoc())
320 {
321 $Title = mb_substr($DbRow['Text'], 0, $TitleLength);
322 if (mb_strlen($Title) == $TitleLength) $Title .= '...';
323 $Items[] = array
324 (
325 'Title' => htmlspecialchars($DbRow['ThreadText']).' - '.$DbRow['UserName'].': ',
326 'Link' => 'https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/forum/?Thread='.$DbRow['Thread']),
327 'Description' => ShowBBcodes(htmlspecialchars($DbRow['Text'])),
328 'Time' => $DbRow['UnixDate'],
329 );
330 }
331 $Output = GenerateRSS(array
332 (
333 'Title' => Core::Cast($this->System)->Config['Web']['Title'].' - '.T('Forum'),
334 'Link' => 'https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/forum/'),
335 'Description' => Core::Cast($this->System)->Config['Web']['Description'],
336 'WebmasterEmail' => Core::Cast($this->System)->Config['Web']['AdminEmail'],
337 'Items' => $Items,
338 ));
339 return $Output;
340 }
341}
Note: See TracBrowser for help on using the repository browser.