source: trunk/Modules/ShoutBox/ShoutBox.php

Last change on this file was 893, checked in by chronos, 15 months ago
  • Fixed: Class types casting for better type checking.
  • Fixed: XML direct export.
  • Modified: User class instance moved from Core class to ModuleUser class.
File size: 7.0 KB
Line 
1<?php
2
3class ModuleShoutBox extends Module
4{
5 function __construct(System $System)
6 {
7 parent::__construct($System);
8 $this->Name = 'ShoutBox';
9 $this->Version = '1.0';
10 $this->Creator = 'Chronos';
11 $this->License = 'GNU/GPL';
12 $this->Description = 'Simple user chat system.';
13 $this->Dependencies = array('News');
14 }
15
16 function DoStart(): void
17 {
18 $this->System->RegisterPage(['shoutbox'], 'PageShoutBox');
19 $this->System->ModuleManager->Modules['News']->RegisterRSS(array(
20 'Title' => T('Shoutbox'), 'Channel' => 'shoutbox', 'Callback' => array('PageShoutBox', 'ShowRSS'),
21 'Permission' => LICENCE_ANONYMOUS));
22 if (array_key_exists('Search', $this->System->ModuleManager->Modules))
23 $this->System->ModuleManager->Modules['Search']->RegisterSearch('shoutbox',
24 T('Shoutbox'), array('UserName', 'Text'), '`ShoutBox`', $this->System->Link('/shoutbox/?search='));
25 }
26
27 function ShowBox()
28 {
29 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
30 $Output = '<strong><a href="'.$this->System->Link('/shoutbox/').'">'.T('Shoutbox').':</a></strong>';
31
32 if ($User->Licence(LICENCE_USER))
33 $Output .= ' <a href="'.$this->System->Link('/shoutbox/?a=add').'">'.T('Add').'</a>';
34 $Output .= '<div class="box"><div class="shoutbox"><table>';
35 $DbResult = $this->Database->query('SELECT * FROM `ShoutBox` ORDER BY `ID` DESC LIMIT 30');
36 while ($Line = $DbResult->fetch_assoc())
37 $Output .= '<tr><td><strong>'.$Line['UserName'].'</strong>: '.MakeActiveLinks($Line['Text']).'</td></tr>';
38 $Output .= '</table></div></div>';
39 return $Output;
40 }
41}
42
43class PageShoutBox extends Page
44{
45 function Show(): string
46 {
47 $this->Title = T('Shoutbox');
48 if (array_key_exists('a', $_POST)) $Action = $_POST['a'];
49 else if (array_key_exists('a', $_GET)) $Action = $_GET['a'];
50 else $Action = '';
51 if ($Action == 'add2') $Output = $this->AddFinish();
52 if ($Action == 'add') $Output = $this->ShowAddForm();
53 else $Output = $this->ShowList();
54 return $Output;
55 }
56
57 function ShowList()
58 {
59 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
60 $Output = '';
61 if (array_key_exists('search', $_GET)) $_SESSION['search'] = $_GET['search'];
62 else if (!array_key_exists('search', $_SESSION)) $_SESSION['search'] = '';
63 if (array_key_exists('search', $_GET) and ($_GET['search'] == '')) $_SESSION['search'] = '';
64 if ($_SESSION['search'] != '')
65 {
66 $SearchQuery = ' AND (`Text` LIKE "%'.$_SESSION['search'].'%")';
67 $Output .= '<div><a href="?search=">'.sprintf(T('Disable filter "%s"'), htmlentities($_SESSION['search'])).'</a></div>';
68 } else $SearchQuery = '';
69
70 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `ShoutBox` WHERE 1'.$SearchQuery);
71 $DbRow = $DbResult->fetch_row();
72 $PageList = GetPageList($DbRow[0]);
73
74 $Output .= '<h3>'.T('Shoutbox').'</h3>';
75 if ($User->Licence(LICENCE_USER))
76 $Output .= ' <a href="'.$this->System->Link('/shoutbox/?a=add').'">'.T('Add').'</a>';
77 $Output .= $PageList['Output'];
78 $Output .= '<div class="shoutbox">';
79 $DbResult = $this->System->Database->query('SELECT * FROM `ShoutBox` WHERE 1'.$SearchQuery.' ORDER BY `ID` DESC '.$PageList['SQLLimit']);
80 while ($Line = $DbResult->fetch_assoc())
81 $Output .= '<div><strong>'.$Line['UserName'].'</strong>: '.MakeActiveLinks($Line['Text']).'</div>';
82 $Output .= '</div>'.$PageList['Output'];
83 return $Output;
84 }
85
86 function ShowAddForm()
87 {
88 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
89 $Output = '';
90 if ($User->Licence(LICENCE_USER))
91 {
92 $Output .= '<form action="?" method="post">'.
93 '<fieldset><legend>'.T('New message').'</legend>'.
94 'Uživatel: ';
95 if ($User->Licence(LICENCE_USER)) $Output .= '<b>'.$User->Name.'</b><br />';
96 else $Output .= '<input type="text" name="user" /><br />';
97 $Output .= 'Text zprávy: <br/>'.
98 '<textarea onkeydown="ResizeTextArea(this)" name="text" cols="40"></textarea> <br/>'.
99 '<input type="hidden" name="a" value="add2"/>'.
100 '<input type="submit" value="Odeslat" /><br /></fieldset>'.
101 '</form>';
102 } else $Output .= ShowMessage('Pro vkládaní zpráv musíte byt registrováni.', MESSAGE_CRITICAL);
103 $Output .= $this->ShowList();
104 return $Output;
105 }
106
107 function AddFinish()
108 {
109 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
110 $Output = '';
111 if ($User->Licence(LICENCE_USER))
112 {
113 if (array_key_exists('text', $_POST))
114 {
115 $Text = $_POST['text'];
116 if (trim($Text) == '') $Output .= ShowMessage('Nelze vložit prázdnou zprávu.', MESSAGE_WARNING);
117 else
118 {
119 // Protection against mutiple post of same message
120 $DbResult = $this->System->Database->query('SELECT `Text` FROM `ShoutBox` WHERE (`User` = "'.
121 $User->Id.'") ORDER BY `Date` DESC LIMIT 1');
122 if ($DbResult->num_rows > 0)
123 {
124 $DbRow = $DbResult->fetch_assoc();
125 } else $DbRow['Text'] = '';
126
127 if ($DbRow['Text'] == $Text) $Output .= ShowMessage('Nelze vložit stejnou zprávu vícekrát za sebou.', MESSAGE_WARNING);
128 else
129 {
130 $this->System->Database->query('INSERT INTO `ShoutBox` ( `User`, `UserName` , `Text` , `Date` , `IP` ) '.
131 ' VALUES ('.$User->Id.', "'.$User->Name.
132 '", "'.$Text.'", NOW(), "'.GetRemoteAddress().'")');
133 $Output .= ShowMessage('Zpráva vložena.');
134 }
135 }
136 } else $Output .= ShowMessage('Nezadán text pro novou zprávu.', MESSAGE_CRITICAL);
137 $Output .= '<br/>';
138 } else $Output .= ShowMessage('Pro vkládaní zpráv musíte byt registrováni.', MESSAGE_CRITICAL);
139 $Output .= $this->ShowList();
140 return $Output;
141 }
142
143 function ShowRSS()
144 {
145 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
146 $Items = array();
147 $TitleLength = 50;
148 mb_internal_encoding('utf-8');
149 $DbResult = $this->Database->query('SELECT UNIX_TIMESTAMP(`Date`) AS `UnixDate`, `User`, `UserName`, `Text` FROM `ShoutBox` ORDER BY `ID` DESC LIMIT 20');
150 while ($DbRow = $DbResult->fetch_assoc())
151 {
152 $Title = mb_substr($DbRow['Text'], 0, $TitleLength);
153 if (mb_strlen($Title) == $TitleLength) $Title .= '...';
154 $Items[] = array
155 (
156 'Title' => $DbRow['UserName'].': '.$Title,
157 'Link' => 'https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/shoutbox/'),
158 'Description' => $DbRow['Text'],
159 'Time' => $DbRow['UnixDate'],
160 );
161 }
162 $Output = GenerateRSS(array
163 (
164 'Title' => Core::Cast($this->System)->Config['Web']['Title'].' - '.T('Shoutbox'),
165 'Link' => 'https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/shoutbox/'),
166 'Description' => Core::Cast($this->System)->Config['Web']['Description'],
167 'WebmasterEmail' => Core::Cast($this->System)->Config['Web']['AdminEmail'],
168 'Items' => $Items,
169 ));
170 return $Output;
171 }
172}
Note: See TracBrowser for help on using the repository browser.