1+ <?php
2+
3+ /*
4+ Copyright (c) 2025, Falko Timme, Timme Hosting GmbH & Co. KG
5+ All rights reserved.
6+
7+ Redistribution and use in source and binary forms, with or without modification,
8+ are permitted provided that the following conditions are met:
9+
10+ * Redistributions of source code must retain the above copyright notice,
11+ this list of conditions and the following disclaimer.
12+ * Redistributions in binary form must reproduce the above copyright notice,
13+ this list of conditions and the following disclaimer in the documentation
14+ and/or other materials provided with the distribution.
15+ * Neither the name of ISPConfig nor the names of its contributors
16+ may be used to endorse or promote products derived from this software without
17+ specific prior written permission.
18+
19+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+ IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+ */
30+
31+ /*
32+ * This library allows it to display messages on the ISPConfig dashboard which the user
33+ * has to actively acknowledge to hide them. The messages are stored in the sys_message table.
34+ */
35+
36+ class message {
37+
38+ /*
39+ * This function is used to add a new message
40+ */
41+
42+ public function add ($ message , $ message_state = 'info ' , $ sys_userid = 0 , $ sys_groupid = 0 , $ relation = '' ) {
43+ global $ app , $ conf ;
44+
45+ if (!in_array ($ message_state ,['info ' ,'warning ' ,'error ' ])) $ app ->debug_log ('Unknown message_state in message add function. ' );
46+ $ message_date = date ('Y-m-d H:i:s ' );
47+ $ sql = "INSERT INTO `sys_message` (`sys_userid`,`sys_groupid`,`message_state`,`message_date`,`message`,`relation`) VALUES (?,?,?,?,?,?) " ;
48+ if (is_object ($ app ->dbmaster )) {
49+ // server
50+ if (!$ app ->dbmaster ->query ($ sql ,(int )$ sys_userid ,(int )$ sys_groupid ,$ message_state ,$ message_date ,$ message ,$ relation )) {
51+ $ app ->debug_log ('Adding ' .$ message_state .' message to sys_message failed. ' );
52+ }
53+ } else {
54+ // interface
55+ if (!$ app ->db ->query ($ sql ,(int )$ sys_userid ,(int )$ sys_groupid ,$ message_state ,$ message_date ,$ message ,$ relation )) {
56+ $ app ->debug_log ('Adding ' .$ message_state .' message to sys_message failed. ' );
57+ }
58+ }
59+ }
60+
61+ /*
62+ * This function is used to hide a message by message_id
63+ */
64+
65+ public function hide_by_id ($ message_id ) {
66+ global $ app , $ conf ;
67+ $ sql = "UPDATE `sys_message` SET `message_ack` = 'y' WHERE `message_id` = ? " ;
68+ if (is_object ($ app ->dbmaster )) {
69+ // server
70+ if (!$ app ->dbmaster ->query ($ sql ,(int )$ message_id )) {
71+ $ app ->debug_log ('Hiding message ' .$ message_id .' in sys_message failed. ' );
72+ }
73+ } else {
74+ // interface
75+ if (!$ app ->db ->query ($ sql ,(int )$ message_id )) {
76+ $ app ->debug_log ('Hiding message ' .$ message_id .' in sys_message failed. ' );
77+ }
78+ }
79+ }
80+
81+ /*
82+ * This function is used to hide a message by message_state
83+ */
84+
85+ public function hide_by_message_state ($ message_state , $ relation = '' ) {
86+ global $ app , $ conf ;
87+
88+ if (!in_array ($ message_state ,['info ' ,'warning ' ,'error ' ])) $ app ->debug_log ('Unknown message_state in message add function. ' );
89+
90+ if (is_object ($ app ->dbmaster )) {
91+ // server
92+ $ sql = "UPDATE `sys_message` SET `message_ack` = 'y' WHERE `message_state` = ? " ;
93+ if (!empty ($ relation )) $ sql .= " AND relation = ? " ;
94+
95+ if (!$ app ->dbmaster ->query ($ sql , $ message_state , $ relation )) {
96+ $ app ->debug_log ('Hiding message by ' .$ message_state .' in sys_message failed. ' );
97+ }
98+ } else {
99+ // interface
100+ $ app ->uses ('tform_base ' );
101+ $ sql = "UPDATE `sys_message` SET `message_ack` = 'y' WHERE `message_state` = ? AND " .$ app ->tform_base ->getAuthSQL ('r ' );
102+ if (!empty ($ relation )) $ sql .= " AND relation = ? " ;
103+
104+ if (!$ app ->db ->query ($ sql , $ message_state , $ relation )) {
105+ $ app ->debug_log ('Hiding message by ' .$ message_state .' in sys_message failed. ' );
106+ }
107+
108+ // Do some cleanup
109+ $ this ->cleanup ();
110+ }
111+ }
112+
113+ /*
114+ * This function is used to hide a message by relation
115+ */
116+
117+ public function hide_by_message_relation ($ relation ) {
118+ global $ app , $ conf ;
119+
120+ if (is_object ($ app ->dbmaster )) {
121+ // server
122+ $ sql = "UPDATE `sys_message` SET `message_ack` = 'y' WHERE `relation` = ? " ;
123+
124+ if (!$ app ->dbmaster ->query ($ sql , $ relation )) {
125+ $ app ->debug_log ('Hiding message by ' .$ relation .' in sys_message failed. ' );
126+ }
127+ } else {
128+ // interface
129+ $ app ->uses ('tform_base ' );
130+ $ sql = "UPDATE `sys_message` SET `message_ack` = 'y' WHERE `relation` = ? AND " .$ app ->tform_base ->getAuthSQL ('r ' );
131+
132+ if (!$ app ->db ->query ($ sql , $ relation )) {
133+ $ app ->debug_log ('Hiding message by ' .$ relation .' in sys_message failed. ' );
134+ }
135+
136+ // Do some cleanup
137+ $ this ->cleanup ();
138+ }
139+ }
140+
141+ /*
142+ * This function is used to delete a message by message_id
143+ */
144+
145+ public function delete ($ message_id ) {
146+ global $ app , $ conf ;
147+ $ sql = "DELETE FROM `sys_message` WHERE `message_id` = ? " ;
148+
149+ if (is_object ($ app ->dbmaster )) {
150+ // server
151+ if (!$ app ->dbmaster ->query ($ sql ,(int )$ message_id )) {
152+ $ app ->debug_log ('Deleting message ' .$ message_id .' from sys_message failed. ' );
153+ }
154+ } else {
155+ // interface
156+ if (!$ app ->db ->query ($ sql ,(int )$ message_id )) {
157+ $ app ->debug_log ('Deleting message ' .$ message_id .' from sys_message failed. ' );
158+ }
159+ }
160+ }
161+
162+ /*
163+ * This function is used to clean up old messages
164+ */
165+
166+ private function cleanup () {
167+ global $ app , $ conf ;
168+ $ message_cleanup_date = date ('Y-m-d H:i:s ' ,strtotime ('- 1 month ' ));
169+ $ sql = "DELETE FROM `sys_message` WHERE `message_ack` = 'y' and `message_date` < ? " ;
170+ if (!$ app ->db ->query ($ sql ,$ message_cleanup_date )) {
171+ $ app ->debug_log ('Cleaning up messages from sys_message failed. ' );
172+ }
173+ }
174+
175+ /*
176+ * This function returns an array with all not acknowledged messages
177+ * for the currently logged-in user (use in interface only)
178+ */
179+
180+ public function get_current_messages ($ relation = '' ) {
181+ global $ app , $ conf ;
182+
183+ $ app ->uses ('tform_base ' );
184+ if (!is_object ($ app ->tform_base )) {
185+ $ app ->debug_log ('No tform_base object. Do not use this function in server part. ' );
186+ return false ;
187+ }
188+
189+ if ($ relation != '' ) {
190+ $ sql = "SELECT * FROM `sys_message` WHERE `message_ack` = 'n' AND `relation` = ? AND " .$ app ->tform_base ->getAuthSQL ('r ' );
191+ } else {
192+ $ sql = "SELECT * FROM `sys_message` WHERE `message_ack` = 'n' AND " .$ app ->tform_base ->getAuthSQL ('r ' );
193+ }
194+ $ messages = $ app ->db ->queryAllRecords ($ sql ,$ relation );
195+
196+ // Translate messages
197+ $ app ->load_language_file ('web/dashboard/lib/lang/ ' .$ _SESSION ['s ' ]['language ' ].'_message.lng ' );
198+ foreach ($ messages as $ key => $ msg ) {
199+ $ messages [$ key ]['message ' ] = $ app ->lng ($ msg ['message ' ]);
200+ }
201+
202+ return $ messages ;
203+ }
204+ }
0 commit comments