1+ <?php
2+ /*
3+ Copyright (c) 2008, Till Brehm, projektfarm Gmbh
4+ All rights reserved.
5+
6+ Redistribution and use in source and binary forms, with or without modification,
7+ are permitted provided that the following conditions are met:
8+
9+ * Redistributions of source code must retain the above copyright notice,
10+ this list of conditions and the following disclaimer.
11+ * Redistributions in binary form must reproduce the above copyright notice,
12+ this list of conditions and the following disclaimer in the documentation
13+ and/or other materials provided with the distribution.
14+ * Neither the name of ISPConfig nor the names of its contributors
15+ may be used to endorse or promote products derived from this software without
16+ specific prior written permission.
17+
18+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21+ IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+ */
29+
30+
31+ /******************************************
32+ * Begin Form configuration
33+ ******************************************/
34+
35+ $ tform_def_file = "form/database.tform.php " ;
36+
37+ /******************************************
38+ * End Form configuration
39+ ******************************************/
40+
41+ require_once ('../../lib/config.inc.php ' );
42+ require_once ('../../lib/app.inc.php ' );
43+
44+ //* Check permissions for module
45+ $ app ->auth ->check_module_permissions ('sites ' );
46+
47+ // Loading classes
48+ $ app ->uses ('tpl,tform,tform_actions ' );
49+ $ app ->load ('tform_actions ' );
50+
51+ class page_action extends tform_actions {
52+
53+ function onShowNew () {
54+ global $ app , $ conf ;
55+
56+ // we will check only users, not admins
57+ if ($ _SESSION ["s " ]["user " ]["typ " ] == 'user ' ) {
58+
59+ // Get the limits of the client
60+ $ client_group_id = $ _SESSION ["s " ]["user " ]["default_group " ];
61+ $ client = $ app ->db ->queryOneRecord ("SELECT limit_database FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $ client_group_id " );
62+
63+ // Check if the user may add another database.
64+ if ($ client ["limit_database " ] >= 0 ) {
65+ $ tmp = $ app ->db ->queryOneRecord ("SELECT count(database_id) as number FROM web_database WHERE sys_groupid = $ client_group_id " );
66+ if ($ tmp ["number " ] >= $ client ["limit_database " ]) {
67+ $ app ->error ($ app ->tform ->wordbook ["limit_database_txt " ]);
68+ }
69+ }
70+ }
71+
72+ parent ::onShowNew ();
73+ }
74+
75+ function onShowEnd () {
76+ global $ app , $ conf ;
77+
78+ if ($ _SESSION ["s " ]["user " ]["typ " ] != 'admin ' && !$ app ->auth ->has_clients ($ _SESSION ['s ' ]['user ' ]['userid ' ])) {
79+
80+ // Get the limits of the client
81+ $ client_group_id = $ _SESSION ["s " ]["user " ]["default_group " ];
82+ $ client = $ app ->db ->queryOneRecord ("SELECT default_dbserver FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $ client_group_id " );
83+
84+ // Set the webserver to the default server of the client
85+ $ tmp = $ app ->db ->queryOneRecord ("SELECT server_name FROM server WHERE server_id = $ client [default_dbserver]" );
86+ $ app ->tpl ->setVar ("server_id " ,"<option value=' $ client [default_dbserver]'> $ tmp [server_name]</option> " );
87+ unset($ tmp );
88+
89+ } elseif ($ _SESSION ["s " ]["user " ]["typ " ] != 'admin ' && $ app ->auth ->has_clients ($ _SESSION ['s ' ]['user ' ]['userid ' ])) {
90+
91+ // Get the limits of the client
92+ $ client_group_id = $ _SESSION ["s " ]["user " ]["default_group " ];
93+ $ client = $ app ->db ->queryOneRecord ("SELECT client_id, default_dbserver FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $ client_group_id " );
94+
95+ // Set the webserver to the default server of the client
96+ $ tmp = $ app ->db ->queryOneRecord ("SELECT server_name FROM server WHERE server_id = $ client [default_dbserver]" );
97+ $ app ->tpl ->setVar ("server_id " ,"<option value=' $ client [default_dbserver]'> $ tmp [server_name]</option> " );
98+ unset($ tmp );
99+
100+ // Fill the client select field
101+ $ sql = "SELECT groupid, name FROM sys_group, client WHERE sys_group.client_id = client.parent_client_id AND client.parent_client_id = " .$ client ['client_id ' ];
102+ $ clients = $ app ->db ->queryAllRecords ($ sql );
103+ $ client_select = '' ;
104+ if (is_array ($ clients )) {
105+ foreach ( $ clients as $ client ) {
106+ $ selected = @($ client ["groupid " ] == $ this ->dataRecord ["sys_groupid " ])?'SELECTED ' :'' ;
107+ $ client_select .= "<option value=' $ client [groupid]' $ selected> $ client [name]</option> \r\n" ;
108+ }
109+ }
110+ $ app ->tpl ->setVar ("client_group_id " ,$ client_select );
111+
112+ } else {
113+
114+ // The user is admin
115+ if ($ this ->id > 0 ) {
116+ $ server_id = $ this ->dataRecord ["server_id " ];
117+ } else {
118+ // Get the first server ID
119+ $ tmp = $ app ->db ->queryOneRecord ("SELECT server_id FROM server WHERE web_server = 1 ORDER BY server_name LIMIT 0,1 " );
120+ $ server_id = $ tmp ['server_id ' ];
121+ }
122+
123+ $ sql = "SELECT ip_address FROM server_ip WHERE server_id = $ server_id " ;
124+ $ ips = $ app ->db ->queryAllRecords ($ sql );
125+ $ ip_select = "<option value='*'>*</option> " ;
126+ //$ip_select = "";
127+ if (is_array ($ ips )) {
128+ foreach ( $ ips as $ ip ) {
129+ $ selected = ($ ip ["ip_address " ] == $ this ->dataRecord ["ip_address " ])?'SELECTED ' :'' ;
130+ $ ip_select .= "<option value=' $ ip [ip_address]' $ selected> $ ip [ip_address]</option> \r\n" ;
131+ }
132+ }
133+ $ app ->tpl ->setVar ("ip_address " ,$ ip_select );
134+ unset($ tmp );
135+ unset($ ips );
136+
137+ // Fill the client select field
138+ $ sql = "SELECT groupid, name FROM sys_group WHERE client_id > 0 " ;
139+ $ clients = $ app ->db ->queryAllRecords ($ sql );
140+ $ client_select = "<option value='0'></option> " ;
141+ if (is_array ($ clients )) {
142+ foreach ( $ clients as $ client ) {
143+ $ selected = @($ client ["groupid " ] == $ this ->dataRecord ["sys_groupid " ])?'SELECTED ' :'' ;
144+ $ client_select .= "<option value=' $ client [groupid]' $ selected> $ client [name]</option> \r\n" ;
145+ }
146+ }
147+ $ app ->tpl ->setVar ("client_group_id " ,$ client_select );
148+
149+ }
150+
151+ parent ::onShowEnd ();
152+ }
153+
154+ function onSubmit () {
155+ global $ app , $ conf ;
156+
157+ if ($ _SESSION ["s " ]["user " ]["typ " ] != 'admin ' ) {
158+ // Get the limits of the client
159+ $ client_group_id = $ _SESSION ["s " ]["user " ]["default_group " ];
160+ $ client = $ app ->db ->queryOneRecord ("SELECT default_dbserver, limit_database FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $ client_group_id " );
161+
162+ // When the record is updated
163+ if ($ this ->id > 0 ) {
164+ // restore the server ID if the user is not admin and record is edited
165+ $ tmp = $ app ->db ->queryOneRecord ("SELECT server_id FROM web_database WHERE database_id = " .intval ($ this ->id ));
166+ $ this ->dataRecord ["server_id " ] = $ tmp ["server_id " ];
167+ unset($ tmp );
168+ // When the record is inserted
169+ } else {
170+ // set the server ID to the default dbserver of the client
171+ $ this ->dataRecord ["server_id " ] = $ client ["default_dbserver " ];
172+
173+
174+ // Check if the user may add another database
175+ if ($ client ["limit_web_domain " ] >= 0 ) {
176+ $ tmp = $ app ->db ->queryOneRecord ("SELECT count(database_id) as number FROM web_database WHERE sys_groupid = $ client_group_id " );
177+ if ($ tmp ["number " ] >= $ client ["limit_database " ]) {
178+ $ app ->error ($ app ->tform ->wordbook ["limit_database_txt " ]);
179+ }
180+ }
181+
182+ }
183+
184+ // Clients may not set the client_group_id, so we unset them if user is not a admin and the client is not a reseller
185+ if (!$ app ->auth ->has_clients ($ _SESSION ['s ' ]['user ' ]['userid ' ])) unset($ this ->dataRecord ["client_group_id " ]);
186+ }
187+
188+
189+ parent ::onSubmit ();
190+ }
191+
192+ function onAfterInsert () {
193+ global $ app , $ conf ;
194+
195+ // make sure that the record belongs to the clinet group and not the admin group when a dmin inserts it
196+ // also make sure that the user can not delete domain created by a admin
197+ if ($ _SESSION ["s " ]["user " ]["typ " ] == 'admin ' && isset ($ this ->dataRecord ["client_group_id " ])) {
198+ $ client_group_id = intval ($ this ->dataRecord ["client_group_id " ]);
199+ $ app ->db ->query ("UPDATE web_database SET sys_groupid = $ client_group_id, sys_perm_group = 'ru' WHERE database_id = " .$ this ->id );
200+ }
201+ if ($ app ->auth ->has_clients ($ _SESSION ['s ' ]['user ' ]['userid ' ]) && isset ($ this ->dataRecord ["client_group_id " ])) {
202+ $ client_group_id = intval ($ this ->dataRecord ["client_group_id " ]);
203+ $ app ->db ->query ("UPDATE web_database SET sys_groupid = $ client_group_id, sys_perm_group = 'riud' WHERE database_id = " .$ this ->id );
204+ }
205+ }
206+
207+ function onAfterUpdate () {
208+ global $ app , $ conf ;
209+
210+ // make sure that the record belongs to the clinet group and not the admin group when a dmin inserts it
211+ // also make sure that the user can not delete domain created by a admin
212+ if ($ _SESSION ["s " ]["user " ]["typ " ] == 'admin ' && isset ($ this ->dataRecord ["client_group_id " ])) {
213+ $ client_group_id = intval ($ this ->dataRecord ["client_group_id " ]);
214+ $ app ->db ->query ("UPDATE web_database SET sys_groupid = $ client_group_id, sys_perm_group = 'ru' WHERE database_id = " .$ this ->id );
215+ }
216+ if ($ app ->auth ->has_clients ($ _SESSION ['s ' ]['user ' ]['userid ' ]) && isset ($ this ->dataRecord ["client_group_id " ])) {
217+ $ client_group_id = intval ($ this ->dataRecord ["client_group_id " ]);
218+ $ app ->db ->query ("UPDATE web_database SET sys_groupid = $ client_group_id, sys_perm_group = 'riud' WHERE database_id = " .$ this ->id );
219+ }
220+
221+ }
222+
223+ }
224+
225+ $ page = new page_action ;
226+ $ page ->onLoad ();
227+
228+ ?>
0 commit comments