Till Brehm
2016-04-22 ebd0e986ed11f2a34fb58cdd33efbfab192083ad
commit | author | age
b488b5 1 <?php
T 2 /*
e94a9f 3 Copyright (c) 2005 - 2012, Till Brehm, projektfarm Gmbh, ISPConfig UG
b488b5 4 All rights reserved.
T 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/client.tform.php";
36
37 /******************************************
38 * End Form configuration
39 ******************************************/
40
7fe908 41 require_once '../../lib/config.inc.php';
MC 42 require_once '../../lib/app.inc.php';
b488b5 43
T 44 //* Check permissions for module
45 $app->auth->check_module_permissions('client');
46
47 // Loading classes
48 $app->uses('tpl,tform,tform_actions');
49 $app->load('tform_actions');
50
51 class page_action extends tform_actions {
7fe908 52     var $_template_additional = array();
MC 53
b488b5 54     function onShowNew() {
T 55         global $app, $conf;
7fe908 56
b488b5 57         // we will check only users, not admins
T 58         if($_SESSION["s"]["user"]["typ"] == 'user') {
7fe908 59
b488b5 60             // Get the limits of the client
604c0c 61             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
b488b5 62             $client = $app->db->queryOneRecord("SELECT limit_client FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
7fe908 63
b488b5 64             // Check if the user may add another website.
T 65             if($client["limit_client"] >= 0) {
66                 $tmp = $app->db->queryOneRecord("SELECT count(client_id) as number FROM client WHERE sys_groupid = $client_group_id");
67                 if($tmp["number"] >= $client["limit_client"]) {
68                     $app->error($app->tform->wordbook["limit_client_txt"]);
69                 }
70             }
71         }
7fe908 72
b488b5 73         parent::onShowNew();
T 74     }
7fe908 75
MC 76
b488b5 77     function onSubmit() {
T 78         global $app, $conf;
7fe908 79
b488b5 80         // we will check only users, not admins
T 81         if($_SESSION["s"]["user"]["typ"] == 'user' && $this->id == 0) {
7fe908 82
b488b5 83             // Get the limits of the client
T 84             $client_group_id = $_SESSION["s"]["user"]["default_group"];
85             $client = $app->db->queryOneRecord("SELECT limit_client FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
7fe908 86
b488b5 87             // Check if the user may add another website.
T 88             if($client["limit_client"] >= 0) {
89                 $tmp = $app->db->queryOneRecord("SELECT count(client_id) as number FROM client WHERE sys_groupid = $client_group_id");
90                 if($tmp["number"] >= $client["limit_client"]) {
91                     $app->error($app->tform->wordbook["limit_client_txt"]);
92                 }
93             }
94         }
94c961 95         
TB 96         //* Resellers shall not be able to create another reseller
97         if($_SESSION["s"]["user"]["typ"] == 'user') {
98             $this->dataRecord['limit_client'] = 0;
99         }
7fe908 100
MC 101         if($this->id != 0) {
102             $this->oldTemplatesAssigned = $app->db->queryAllRecords('SELECT * FROM `client_template_assigned` WHERE `client_id` = ' . $this->id);
103             if(!is_array($this->oldTemplatesAssigned) || count($this->oldTemplatesAssigned) < 1) {
104                 // check previous type of storing templates
105                 $tpls = explode('/', $this->oldDataRecord['template_additional']);
106                 $this->oldTemplatesAssigned = array();
107                 foreach($tpls as $item) {
108                     $item = trim($item);
109                     if(!$item) continue;
110                     $this->oldTemplatesAssigned[] = array('assigned_template_id' => 0, 'client_template_id' => $item, 'client_id' => $this->id);
111                 }
112                 unset($tpls);
113             }
114         } else {
115             $this->oldTemplatesAssigned = array();
116         }
117
118         $this->_template_additional = explode('/', $this->dataRecord['template_additional']);
119         $this->dataRecord['template_additional'] = '';
120
121         parent::onSubmit();
b488b5 122     }
T 123
124     function onShowEnd() {
125
126         global $app;
127
141480 128         $sql = "SELECT template_id,template_name FROM client_template WHERE template_type = 'a' and ".$app->tform->getAuthSQL('r')." ORDER BY template_name ASC";
b488b5 129         $tpls = $app->db->queryAllRecords($sql);
T 130         $option = '';
131         $tpl = array();
132         foreach($tpls as $item){
133             $option .= '<option value="' . $item['template_id'] . '|' .  $item['template_name'] . '">' . $item['template_name'] . '</option>';
134             $tpl[$item['template_id']] = $item['template_name'];
135         }
7fe908 136         $app->tpl->setVar('tpl_add_select', $option);
b488b5 137
7fe908 138         // check for new-style records
MC 139         $result = $app->db->queryAllRecords('SELECT assigned_template_id, client_template_id FROM client_template_assigned WHERE client_id = ' . $this->id);
140         if($result && count($result) > 0) {
141             // new style
142             $items = array();
143             $text = '';
144             foreach($result as $item){
145                 if (trim($item['client_template_id']) != ''){
146                     if ($text != '') $text .= '';
147                     $text .= '<li rel="' . $item['assigned_template_id'] . '">' . $tpl[$item['client_template_id']];
148                     $text .= '<a href="#" class="button icons16 icoDelete"></a>';
149                     $tmp = new stdClass();
150                     $tmp->id = $item['assigned_template_id'];
151                     $tmp->data = '';
152                     $app->plugin->raiseEvent('get_client_template_details', $tmp);
153                     if($tmp->data != '') $text .= '<br /><em>' . $tmp->data . '</em>';
7b47c0 154
7fe908 155                     $text .= '</li>';
MC 156                     $items[] = $item['assigned_template_id'] . ':' . $item['client_template_id'];
157                 }
158             }
7b47c0 159
7fe908 160             $tmprec = $app->tform->getHTML(array('template_additional' => implode('/', $items)), $this->active_tab, 'EDIT');
MC 161             $app->tpl->setVar('template_additional', $tmprec['template_additional']);
162             unset($tmprec);
163         } else {
164             // old style
165             $sql = "SELECT template_additional FROM client WHERE client_id = " . $this->id;
166             $result = $app->db->queryOneRecord($sql);
167             $tplAdd = explode("/", $result['template_additional']);
168             $text = '';
169             foreach($tplAdd as $item){
170                 if (trim($item) != ''){
171                     if ($text != '') $text .= '';
172                     $text .= '<li>' . $tpl[$item]. '<a href="#" class="button icons16 icoDelete"></a></li>';
173                 }
174             }
175         }
b488b5 176
T 177         $app->tpl->setVar('template_additional_list', $text);
7fe908 178         $app->tpl->setVar('app_module', 'client');
3d96c5 179         
6a8a67 180
3d96c5 181         //* Set the 'customer no' default value
TB 182         if($this->id == 0) {
183             
184             if($app->auth->is_admin()) {
185                 //* Logged in User is admin
186                 //* get the system config
187                 $app->uses('getconf');
188                 $system_config = $app->getconf->get_global_config();
189                 if($system_config['misc']['customer_no_template'] != '') {
190                 
191                     //* Set customer no default
192                     $customer_no = $app->functions->intval($system_config['misc']['customer_no_start']+$system_config['misc']['customer_no_counter']);
193                     $customer_no_string = str_replace('[CUSTOMER_NO]',$customer_no,$system_config['misc']['customer_no_template']);
194                     $app->tpl->setVar('customer_no',$customer_no_string);
195                 
196                     //* save new counter value
23229c 197                     /*
3d96c5 198                     $system_config['misc']['customer_no_counter']++;
TB 199                     $system_config_str = $app->ini_parser->get_ini_string($system_config);
200                     $app->db->datalogUpdate('sys_ini', "config = '".$app->db->quote($system_config_str)."'", 'sysini_id', 1);
23229c 201                     */
3d96c5 202                 }
TB 203             } else {
204                 //* Logged in user must be a reseller
205                 //* get the record of the reseller
206                 $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
207                 $reseller = $app->db->queryOneRecord("SELECT client.client_id, client.customer_no_template, client.customer_no_counter, client.customer_no_start FROM sys_group,client WHERE client.client_id = sys_group.client_id and sys_group.groupid = ".$client_group_id);
208                 
209                 if($reseller['customer_no_template'] != '') {
210                     //* Set customer no default
211                     $customer_no = $app->functions->intval($reseller['customer_no_start']+$reseller['customer_no_counter']);
212                     $customer_no_string = str_replace('[CUSTOMER_NO]',$customer_no,$reseller['customer_no_template']);
213                     $app->tpl->setVar('customer_no',$customer_no_string);
214                     
215                     //* save new counter value
23229c 216                     /*
3d96c5 217                     $customer_no_counter = $app->functions->intval($reseller['customer_no_counter']+1);
TB 218                     $app->db->query("UPDATE client SET customer_no_counter = $customer_no_counter WHERE client_id = ".$app->functions->intval($reseller['client_id']));
23229c 219                     */
3d96c5 220                 }
TB 221             }
222         }
6a8a67 223         
5192db 224         if($app->auth->is_admin()) {
TB 225             // Fill the client select field
ca4b71 226             $sql = "SELECT client.client_id, sys_group.groupid, sys_group.name, CONCAT(IF(client.company_name != '', CONCAT(client.company_name, ' :: '), ''), client.contact_name, ' (', client.username, IF(client.customer_no != '', CONCAT(', ', client.customer_no), ''), ')') as contactname FROM sys_group, client WHERE sys_group.client_id = client.client_id AND sys_group.client_id > 0 AND client.limit_client > 0 ORDER BY client.company_name, client.contact_name, sys_group.name";
5192db 227             $clients = $app->db->queryAllRecords($sql);
TB 228             $client_select = "<option value='0'>- ".$app->tform->lng('none_txt')." -</option>";
229             //$tmp_data_record = $app->tform->getDataRecord($this->id);
230             if(is_array($clients)) {
231                 $selected_client_id = 0; // needed to get list of PHP versions
232                 foreach($clients as $client) {
233                     if(is_array($this->dataRecord) && ($client["client_id"] == $this->dataRecord['parent_client_id']) && !$selected_client_id) $selected_client_id = $client["client_id"];
234                     $selected = @(is_array($this->dataRecord) && ($client["client_id"] == $this->dataRecord['parent_client_id']))?'SELECTED':'';
235                     if($selected == 'SELECTED') $selected_client_id = $client["client_id"];
236                     $client_select .= "<option value='$client[client_id]' $selected>$client[contactname]</option>\r\n";
237                 }
238             }
239             $app->tpl->setVar("parent_client_id", $client_select);
240         }
241         
b488b5 242         parent::onShowEnd();
T 243
244     }
7fe908 245
b488b5 246     /*
T 247      This function is called automatically right after
248      the data was successful inserted in the database.
249     */
250     function onAfterInsert() {
3398c2 251         global $app, $conf;
b488b5 252         // Create the group for the client
cb904e 253         $groupid = $app->db->datalogInsert('sys_group', "(name,description,client_id) VALUES ('".$app->db->quote($this->dataRecord["username"])."','',".$this->id.")", 'groupid');
b488b5 254         $groups = $groupid;
7fe908 255
b488b5 256         $username = $app->db->quote($this->dataRecord["username"]);
T 257         $password = $app->db->quote($this->dataRecord["password"]);
3398c2 258         $modules = $conf['interface_modules_enabled'];
b9ce1a 259         if(isset($this->dataRecord["limit_client"]) && $this->dataRecord["limit_client"] > 0) $modules .= ',client';
7fe908 260         $startmodule = (stristr($modules, 'dashboard'))?'dashboard':'client';
b488b5 261         $usertheme = $app->db->quote($this->dataRecord["usertheme"]);
T 262         $type = 'user';
263         $active = 1;
264         $language = $app->db->quote($this->dataRecord["language"]);
c614f1 265         $password = $app->auth->crypt_password($password);
7fe908 266
f5d954 267         // Create the controlpaneluser for the client
08c588 268         //Generate ssh-rsa-keys
L 269         exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
b74ef5 270         $app->db->query("UPDATE client SET created_at = ".time().", id_rsa = '".$app->db->quote(@file_get_contents('/tmp/id_rsa'))."', ssh_rsa = '".$app->db->quote(@file_get_contents('/tmp/id_rsa.pub'))."' WHERE client_id = ".$this->id);
08c588 271         exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub');
7fe908 272
b488b5 273         // Create the controlpaneluser for the client
28a1b8 274         $sql = "INSERT INTO sys_user (username,passwort,modules,startmodule,app_theme,typ,active,language,groups,default_group,client_id)
f5d954 275         VALUES ('$username','$password','$modules','$startmodule','$usertheme','$type','$active','$language',$groups,$groupid,".$this->id.")";
b488b5 276         $app->db->query($sql);
7fe908 277
MC 278         //* If the user who inserted the client is a reseller (not admin), we will have to add this new client group
b488b5 279         //* to his groups, so he can administrate the records of this client.
T 280         if($_SESSION['s']['user']['typ'] == 'user') {
7fe908 281             $app->auth->add_group_to_user($_SESSION['s']['user']['userid'], $groupid);
65ea2e 282             $app->db->query("UPDATE client SET parent_client_id = ".$app->functions->intval($_SESSION['s']['user']['client_id'])." WHERE client_id = ".$this->id);
5192db 283         } else {
TB 284             if($this->dataRecord['parent_client_id'] > 0) {
285                 //* get userid of the reseller and add it to the group of the client
286                 $tmp = $app->db->queryOneRecord("SELECT sys_user.userid FROM sys_user,sys_group WHERE sys_user.default_group = sys_group.groupid AND sys_group.client_id = ".$app->functions->intval($this->dataRecord['parent_client_id']));
287                 $app->auth->add_group_to_user($tmp['userid'], $groupid);
288                 $app->db->query("UPDATE client SET parent_client_id = ".$app->functions->intval($this->dataRecord['parent_client_id'])." WHERE client_id = ".$this->id);
289                 unset($tmp);
290             }
b488b5 291         }
7fe908 292
8cf78b 293         //* Set the default servers
b67344 294         $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE mail_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
65ea2e 295         $default_mailserver = $app->functions->intval($tmp['server_id']);
b67344 296         $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE web_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
65ea2e 297         $default_webserver = $app->functions->intval($tmp['server_id']);
b67344 298         $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE dns_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
65ea2e 299         $default_dnsserver = $app->functions->intval($tmp['server_id']);
b67344 300         $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE db_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
65ea2e 301         $default_dbserver = $app->functions->intval($tmp['server_id']);
7fe908 302
615a0a 303         $sql = "UPDATE client SET default_mailserver = $default_mailserver, default_webserver = $default_webserver, default_dnsserver = $default_dnsserver, default_slave_dnsserver = $default_dnsserver, default_dbserver = $default_dbserver WHERE client_id = ".$this->id;
8cf78b 304         $app->db->query($sql);
7fe908 305
MC 306         if(isset($this->dataRecord['template_master'])) {
307             $app->uses('client_templates');
308             $app->client_templates->update_client_templates($this->id, $this->_template_additional);
309         }
23229c 310         
TB 311         if($this->dataRecord['customer_no'] == $this->dataRecord['customer_no_org']) {
312             if($app->auth->is_admin()) {
313                 //* Logged in User is admin
314                 //* get the system config
315                 $app->uses('getconf');
316                 $system_config = $app->getconf->get_global_config();
317                 if($system_config['misc']['customer_no_template'] != '') {
318                 
319                     //* save new counter value
320                     $system_config['misc']['customer_no_counter']++;
321                     $system_config_str = $app->ini_parser->get_ini_string($system_config);
322                     $app->db->datalogUpdate('sys_ini', "config = '".$app->db->quote($system_config_str)."'", 'sysini_id', 1);
323                 }
324             } else {
325                 //* Logged in user must be a reseller
326                 //* get the record of the reseller
327                 $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
328                 $reseller = $app->db->queryOneRecord("SELECT client.client_id, client.customer_no_template, client.customer_no_counter, client.customer_no_start FROM sys_group,client WHERE client.client_id = sys_group.client_id and sys_group.groupid = ".$client_group_id);
329                 
330                 if($reseller['customer_no_template'] != '') {
331                     //* save new counter value
332                     $customer_no_counter = $app->functions->intval($reseller['customer_no_counter']+1);
333                     $app->db->query("UPDATE client SET customer_no_counter = $customer_no_counter WHERE client_id = ".$app->functions->intval($reseller['client_id']));
334                 }
335             }
336         }
fedbca 337         
TB 338         //* Send welcome email
339         $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
340         $sql = "SELECT * FROM client_message_template WHERE template_type = 'welcome' AND sys_groupid = ".$client_group_id;
341         $email_template = $app->db->queryOneRecord($sql);
342         $client = $app->tform->getDataRecord($this->id);
343
344         if(is_array($email_template) && $client['email'] != '') {
345             //* Parse client details into message
346             $message = $email_template['message'];
347             $subject = $email_template['subject'];
348             foreach($client as $key => $val) {
349                 switch ($key) {
350                 case 'password':
351                     $message = str_replace('{password}', $this->dataRecord['password'], $message);
352                     $subject = str_replace('{password}', $this->dataRecord['password'], $subject);
353                     break;
354                 case 'gender':
237f35 355                     $message = str_replace('{salutation}', $app->tform->lng('gender_'.$val.'_txt'), $message);
MC 356                     $subject = str_replace('{salutation}', $app->tform->lng('gender_'.$val.'_txt'), $subject);
fedbca 357                     break;
TB 358                 default:
359                     $message = str_replace('{'.$key.'}', $val, $message);
360                     $subject = str_replace('{'.$key.'}', $val, $subject);
361                 }
362             }
363             
364             //* Get sender address
365             if($app->auth->is_admin()) {
366                 $app->uses('getconf');
aff82c 367                 $system_config = $app->getconf->get_global_config('mail');
fedbca 368                 $from = $system_config['admin_mail'];
TB 369             } else {
370                 $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
371                 $reseller = $app->db->queryOneRecord("SELECT client.email FROM sys_group,client WHERE client.client_id = sys_group.client_id and sys_group.groupid = ".$client_group_id);
372                 $from = $reseller["email"];
373             }
374
375             //* Send the email
376             $app->functions->mail($client['email'], $subject, $message, $from);
377         }
378         
b488b5 379
T 380         parent::onAfterInsert();
381     }
7fe908 382
MC 383
b488b5 384     /*
T 385      This function is called automatically right after
386      the data was successful updated in the database.
387     */
388     function onAfterUpdate() {
bfcdef 389         global $app, $conf;
b488b5 390         // username changed
b9ce1a 391         if(isset($conf['demo_mode']) && $conf['demo_mode'] != true && isset($this->dataRecord['username']) && $this->dataRecord['username'] != '' && $this->oldDataRecord['username'] != $this->dataRecord['username']) {
b488b5 392             $username = $app->db->quote($this->dataRecord["username"]);
T 393             $client_id = $this->id;
394             $sql = "UPDATE sys_user SET username = '$username' WHERE client_id = $client_id";
395             $app->db->query($sql);
7fe908 396
b488b5 397             $tmp = $app->db->queryOneRecord("SELECT * FROM sys_group WHERE client_id = $client_id");
T 398             $app->db->datalogUpdate("sys_group", "name = '$username'", 'groupid', $tmp['groupid']);
399             unset($tmp);
400         }
7fe908 401
b488b5 402         // password changed
b9ce1a 403         if(isset($conf['demo_mode']) && $conf['demo_mode'] != true && isset($this->dataRecord["password"]) && $this->dataRecord["password"] != '') {
b488b5 404             $password = $app->db->quote($this->dataRecord["password"]);
f5d954 405             $salt="$1$";
T 406             $base64_alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
407             for ($n=0;$n<8;$n++) {
7fe908 408                 $salt.=$base64_alphabet[mt_rand(0, 63)];
f5d954 409             }
T 410             $salt.="$";
7fe908 411             $password = crypt(stripslashes($password), $salt);
b488b5 412             $client_id = $this->id;
f5d954 413             $sql = "UPDATE sys_user SET passwort = '$password' WHERE client_id = $client_id";
b488b5 414             $app->db->query($sql);
T 415         }
7fe908 416
MC 417         if(!isset($this->dataRecord['locked'])) $this->dataRecord['locked'] = 'n';
418         if(isset($conf['demo_mode']) && $conf['demo_mode'] != true && $this->dataRecord["locked"] != $this->oldDataRecord['locked']) {
419             /** lock all the things like web, mail etc. - easy to extend */
420
421
422             // get tmp_data of client
423             $client_data = $app->db->queryOneRecord('SELECT `tmp_data` FROM `client` WHERE `client_id` = ' . $this->id);
424
425             if($client_data['tmp_data'] == '') $tmp_data = array();
426             else $tmp_data = unserialize($client_data['tmp_data']);
427
428             if(!is_array($tmp_data)) $tmp_data = array();
429
430             // database tables with their primary key columns
431             $to_disable = array('cron' => 'id',
432                 'ftp_user' => 'ftp_user_id',
433                 'mail_domain' => 'domain_id',
44c2dd 434                 'mail_user' => 'mailuser_id',
MC 435                 'mail_user_smtp' => 'mailuser_id',
7fe908 436                 'mail_forwarding' => 'forwarding_id',
MC 437                 'mail_get' => 'mailget_id',
438                 'openvz_vm' => 'vm_id',
439                 'shell_user' => 'shell_user_id',
440                 'webdav_user' => 'webdav_user_id',
441                 'web_database' => 'database_id',
442                 'web_domain' => 'domain_id',
443                 'web_folder' => 'web_folder_id',
444                 'web_folder_user' => 'web_folder_user_id'
445             );
446
447             $udata = $app->db->queryOneRecord('SELECT `userid` FROM `sys_user` WHERE `client_id` = ' . $this->id);
448             $gdata = $app->db->queryOneRecord('SELECT `groupid` FROM `sys_group` WHERE `client_id` = ' . $this->id);
449             $sys_groupid = $gdata['groupid'];
450             $sys_userid = $udata['userid'];
451
452             $entries = array();
453             if($this->dataRecord['locked'] == 'y') {
454                 $prev_active = array();
455                 $prev_sysuser = array();
456                 foreach($to_disable as $current => $keycolumn) {
44c2dd 457                     $active_col = 'active';
MC 458                     $reverse = false;
459                     if($current == 'mail_user') {
460                         $active_col = 'postfix';
461                     } elseif($current == 'mail_user_smtp') {
462                         $current = 'mail_user';
463                         $active_col = 'disablesmtp';
464                         $reverse = true;
465                     }
466                     
467                     if(!isset($prev_active[$current])) $prev_active[$current] = array();
468                     if(!isset($prev_sysuser[$current])) $prev_sysuser[$current] = array();
7fe908 469
44c2dd 470                     $entries = $app->db->queryAllRecords('SELECT `' . $keycolumn . '` as `id`, `sys_userid`, `' . $active_col . '` FROM `' . $current . '` WHERE `sys_groupid` = ' . $sys_groupid);
7fe908 471                     foreach($entries as $item) {
MC 472
44c2dd 473                         if($item[$active_col] != 'y' && $reverse == false) $prev_active[$current][$item['id']][$active_col] = 'n';
MC 474                         elseif($item[$active_col] == 'y' && $reverse == true) $prev_active[$current][$item['id']][$active_col] = 'y';
475                         if($item['sys_userid'] != $sys_userid) $prev_sysuser[$current][$item['id']] = $item['sys_userid'];
7fe908 476                         // we don't have to store these if y, as everything without previous state gets enabled later
MC 477
44c2dd 478                         $app->db->datalogUpdate($current, array($active_col => ($reverse == true ? 'y' : 'n'), 'sys_userid' => $_SESSION["s"]["user"]["userid"]), $keycolumn, $item['id']);
7fe908 479                     }
MC 480                 }
481
482                 $tmp_data['prev_active'] = $prev_active;
483                 $tmp_data['prev_sys_userid'] = $prev_sysuser;
484                 $app->db->query("UPDATE `client` SET `tmp_data` = '" . $app->db->quote(serialize($tmp_data)) . "' WHERE `client_id` = " . $this->id);
485                 unset($prev_active);
486                 unset($prev_sysuser);
487             } elseif($this->dataRecord['locked'] == 'n') {
488                 foreach($to_disable as $current => $keycolumn) {
44c2dd 489                     $active_col = 'active';
MC 490                     $reverse = false;
491                     if($current == 'mail_user') {
492                         $active_col = 'postfix';
493                     } elseif($current == 'mail_user_smtp') {
494                         $current = 'mail_user';
495                         $active_col = 'disablesmtp';
496                         $reverse = true;
497                     }
498                     
7fe908 499                     $entries = $app->db->queryAllRecords('SELECT `' . $keycolumn . '` as `id` FROM `' . $current . '` WHERE `sys_groupid` = ' . $sys_groupid);
MC 500                     foreach($entries as $item) {
44c2dd 501                         $set_active = ($reverse == true ? 'n' : 'y');
MC 502                         $set_inactive = ($reverse == true ? 'y' : 'n');
7fe908 503                         $set_sysuser = $sys_userid;
MC 504                         if(array_key_exists('prev_active', $tmp_data) == true
505                             && array_key_exists($current, $tmp_data['prev_active']) == true
506                             && array_key_exists($item['id'], $tmp_data['prev_active'][$current]) == true
44c2dd 507                             && $tmp_data['prev_active'][$current][$item['id']][$active_col] == $set_inactive) $set_active = $set_inactive;
7fe908 508                         if(array_key_exists('prev_sysuser', $tmp_data) == true
MC 509                             && array_key_exists($current, $tmp_data['prev_sysuser']) == true
510                             && array_key_exists($item['id'], $tmp_data['prev_sysuser'][$current]) == true
511                             && $tmp_data['prev_sysuser'][$current][$item['id']] != $sys_userid) $set_sysuser = $tmp_data['prev_sysuser'][$current][$item['id']];
512
44c2dd 513                         $app->db->datalogUpdate($current, array($active_col => $set_active, 'sys_userid' => $set_sysuser), $keycolumn, $item['id']);
7fe908 514                     }
MC 515                 }
516                 if(array_key_exists('prev_active', $tmp_data)) unset($tmp_data['prev_active']);
517                 $app->db->query("UPDATE `client` SET `tmp_data` = '" . $app->db->quote(serialize($tmp_data)) . "' WHERE `client_id` = " . $this->id);
518             }
519             unset($tmp_data);
520             unset($entries);
521             unset($to_disable);
522         }
523
524         if(!isset($this->dataRecord['canceled'])) $this->dataRecord['canceled'] = 'n';
525         if(isset($conf['demo_mode']) && $conf['demo_mode'] != true && $this->dataRecord["canceled"] != $this->oldDataRecord['canceled']) {
526             if($this->dataRecord['canceled'] == 'y') {
527                 $sql = "UPDATE sys_user SET active = '0' WHERE client_id = " . $this->id;
528                 $app->db->query($sql);
529             } elseif($this->dataRecord['canceled'] == 'n') {
530                 $sql = "UPDATE sys_user SET active = '1' WHERE client_id = " . $this->id;
531                 $app->db->query($sql);
532             }
533         }
534
cab924 535         // language changed
b9ce1a 536         if(isset($conf['demo_mode']) && $conf['demo_mode'] != true && isset($this->dataRecord['language']) && $this->dataRecord['language'] != '' && $this->oldDataRecord['language'] != $this->dataRecord['language']) {
cab924 537             $language = $app->db->quote($this->dataRecord["language"]);
T 538             $client_id = $this->id;
539             $sql = "UPDATE sys_user SET language = '$language' WHERE client_id = $client_id";
540             $app->db->query($sql);
541         }
7fe908 542
5192db 543         //* reseller status changed
b488b5 544         if(isset($this->dataRecord["limit_client"]) && $this->dataRecord["limit_client"] != $this->oldDataRecord["limit_client"]) {
3398c2 545             $modules = $conf['interface_modules_enabled'];
b488b5 546             if($this->dataRecord["limit_client"] > 0) $modules .= ',client';
T 547             $modules = $app->db->quote($modules);
548             $client_id = $this->id;
549             $sql = "UPDATE sys_user SET modules = '$modules' WHERE client_id = $client_id";
550             $app->db->query($sql);
551         }
5192db 552         
TB 553         //* Client has been moved to another reseller
554         if($_SESSION['s']['user']['typ'] == 'admin' && isset($this->dataRecord['parent_client_id']) && $this->dataRecord['parent_client_id'] != $this->oldDataRecord['parent_client_id']) {
555             //* Get groupid of the client
556             $tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ".intval($this->id));
557             $groupid = $tmp['groupid'];
558             unset($tmp);
559             
560             //* Remove sys_user of old reseller from client group
561             if($this->oldDataRecord['parent_client_id'] > 0) {
562                 //* get userid of the old reseller remove it from the group of the client
563                 $tmp = $app->db->queryOneRecord("SELECT sys_user.userid FROM sys_user,sys_group WHERE sys_user.default_group = sys_group.groupid AND sys_group.client_id = ".$app->functions->intval($this->oldDataRecord['parent_client_id']));
564                 $app->auth->remove_group_from_user($tmp['userid'], $groupid);
565                 unset($tmp);
566             }
567             
568             //* Add sys_user of new reseller to client group
569             if($this->dataRecord['parent_client_id'] > 0) {
570                 //* get userid of the reseller and add it to the group of the client
571                 $tmp = $app->db->queryOneRecord("SELECT sys_user.userid, sys_user.default_group FROM sys_user,sys_group WHERE sys_user.default_group = sys_group.groupid AND sys_group.client_id = ".$app->functions->intval($this->dataRecord['parent_client_id']));
572                 $app->auth->add_group_to_user($tmp['userid'], $groupid);
573                 $app->db->query("UPDATE client SET sys_userid = ".$app->functions->intval($tmp['userid']).", sys_groupid = ".$app->functions->intval($tmp['default_group']).", parent_client_id = ".$app->functions->intval($this->dataRecord['parent_client_id'])." WHERE client_id = ".$this->id);
574                 unset($tmp);
575             } else {
576                 //* Client is not assigned to a reseller anymore, so we assign it to the admin
577                 $app->db->query("UPDATE client SET sys_userid = 1, sys_groupid = 1, parent_client_id = 0 WHERE client_id = ".$this->id);
578             }
579         }
7fe908 580
MC 581         if(isset($this->dataRecord['template_master'])) {
582             $app->uses('client_templates');
583             $app->client_templates->update_client_templates($this->id, $this->_template_additional);
584         }
585
b488b5 586         parent::onAfterUpdate();
T 587     }
7fe908 588
b488b5 589 }
T 590
591 $page = new page_action;
592 $page->onLoad();
593
89bbd1 594 ?>