Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
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
b1a6a5 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 {
b1a6a5 52     var $_template_additional = array();
MC 53
b488b5 54     function onShowNew() {
T 55         global $app, $conf;
b1a6a5 56
b488b5 57         // we will check only users, not admins
T 58         if($_SESSION["s"]["user"]["typ"] == 'user') {
b1a6a5 59
b488b5 60             // Get the limits of the client
604c0c 61             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
cc7a82 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);
b1a6a5 63
b488b5 64             // Check if the user may add another website.
T 65             if($client["limit_client"] >= 0) {
cc7a82 66                 $tmp = $app->db->queryOneRecord("SELECT count(client_id) as number FROM client WHERE sys_groupid = ?", $client_group_id);
b488b5 67                 if($tmp["number"] >= $client["limit_client"]) {
T 68                     $app->error($app->tform->wordbook["limit_client_txt"]);
69                 }
70             }
71         }
b1a6a5 72
b488b5 73         parent::onShowNew();
T 74     }
b1a6a5 75
MC 76
b488b5 77     function onSubmit() {
T 78         global $app, $conf;
b1a6a5 79
b488b5 80         // we will check only users, not admins
T 81         if($_SESSION["s"]["user"]["typ"] == 'user' && $this->id == 0) {
b1a6a5 82
b488b5 83             // Get the limits of the client
T 84             $client_group_id = $_SESSION["s"]["user"]["default_group"];
cc7a82 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);
b1a6a5 86
b488b5 87             // Check if the user may add another website.
T 88             if($client["limit_client"] >= 0) {
cc7a82 89                 $tmp = $app->db->queryOneRecord("SELECT count(client_id) as number FROM client WHERE sys_groupid = ?", $client_group_id);
b488b5 90                 if($tmp["number"] >= $client["limit_client"]) {
T 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;
babacd 99         } else {
SC 100             if($this->dataRecord["reseller"]) {
7e509f 101                 $this->dataRecord["limit_client"] = 1; // allow 1 client, template limits will be applied later, if we set -1 it would override template limits
babacd 102             }
94c961 103         }
b1a6a5 104
MC 105         if($this->id != 0) {
2af58c 106             $this->oldTemplatesAssigned = $app->db->queryAllRecords('SELECT * FROM `client_template_assigned` WHERE `client_id` = ?', $this->id);
b1a6a5 107             if(!is_array($this->oldTemplatesAssigned) || count($this->oldTemplatesAssigned) < 1) {
MC 108                 // check previous type of storing templates
109                 $tpls = explode('/', $this->oldDataRecord['template_additional']);
110                 $this->oldTemplatesAssigned = array();
111                 foreach($tpls as $item) {
112                     $item = trim($item);
113                     if(!$item) continue;
114                     $this->oldTemplatesAssigned[] = array('assigned_template_id' => 0, 'client_template_id' => $item, 'client_id' => $this->id);
115                 }
116                 unset($tpls);
117             }
118         } else {
119             $this->oldTemplatesAssigned = array();
120         }
121
122         $this->_template_additional = explode('/', $this->dataRecord['template_additional']);
123         $this->dataRecord['template_additional'] = '';
124
125         parent::onSubmit();
b488b5 126     }
T 127
128     function onShowEnd() {
129
130         global $app;
131
141480 132         $sql = "SELECT template_id,template_name FROM client_template WHERE template_type = 'a' and ".$app->tform->getAuthSQL('r')." ORDER BY template_name ASC";
b488b5 133         $tpls = $app->db->queryAllRecords($sql);
T 134         $option = '';
135         $tpl = array();
136         foreach($tpls as $item){
137             $option .= '<option value="' . $item['template_id'] . '|' .  $item['template_name'] . '">' . $item['template_name'] . '</option>';
138             $tpl[$item['template_id']] = $item['template_name'];
139         }
b1a6a5 140         $app->tpl->setVar('tpl_add_select', $option);
b488b5 141
b1a6a5 142         // check for new-style records
2af58c 143         $result = $app->db->queryAllRecords('SELECT assigned_template_id, client_template_id FROM client_template_assigned WHERE client_id = ?', $this->id);
b1a6a5 144         if($result && count($result) > 0) {
MC 145             // new style
146             $items = array();
147             $text = '';
148             foreach($result as $item){
149                 if (trim($item['client_template_id']) != ''){
150                     if ($text != '') $text .= '';
151                     $text .= '<li rel="' . $item['assigned_template_id'] . '">' . $tpl[$item['client_template_id']];
152                     $text .= '<a href="#" class="button icons16 icoDelete"></a>';
153                     $tmp = new stdClass();
154                     $tmp->id = $item['assigned_template_id'];
155                     $tmp->data = '';
156                     $app->plugin->raiseEvent('get_client_template_details', $tmp);
157                     if($tmp->data != '') $text .= '<br /><em>' . $tmp->data . '</em>';
7b47c0 158
b1a6a5 159                     $text .= '</li>';
MC 160                     $items[] = $item['assigned_template_id'] . ':' . $item['client_template_id'];
161                 }
162             }
7b47c0 163
b1a6a5 164             $tmprec = $app->tform->getHTML(array('template_additional' => implode('/', $items)), $this->active_tab, 'EDIT');
MC 165             $app->tpl->setVar('template_additional', $tmprec['template_additional']);
166             unset($tmprec);
167         } else {
168             // old style
2af58c 169             $sql = "SELECT template_additional FROM client WHERE client_id = ?";
MC 170             $result = $app->db->queryOneRecord($sql, $this->id);
b1a6a5 171             $tplAdd = explode("/", $result['template_additional']);
MC 172             $text = '';
173             foreach($tplAdd as $item){
174                 if (trim($item) != ''){
175                     if ($text != '') $text .= '';
176                     $text .= '<li>' . $tpl[$item]. '<a href="#" class="button icons16 icoDelete"></a></li>';
177                 }
178             }
179         }
b488b5 180
T 181         $app->tpl->setVar('template_additional_list', $text);
b1a6a5 182         $app->tpl->setVar('app_module', 'client');
3d96c5 183         
b488b5 184
3d96c5 185         //* Set the 'customer no' default value
TB 186         if($this->id == 0) {
187             
188             if($app->auth->is_admin()) {
189                 //* Logged in User is admin
190                 //* get the system config
191                 $app->uses('getconf');
192                 $system_config = $app->getconf->get_global_config();
193                 if($system_config['misc']['customer_no_template'] != '') {
194                 
195                     //* Set customer no default
196                     $customer_no = $app->functions->intval($system_config['misc']['customer_no_start']+$system_config['misc']['customer_no_counter']);
197                     $customer_no_string = str_replace('[CUSTOMER_NO]',$customer_no,$system_config['misc']['customer_no_template']);
198                     $app->tpl->setVar('customer_no',$customer_no_string);
199                 }
200             } else {
201                 //* Logged in user must be a reseller
202                 //* get the record of the reseller
203                 $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
2af58c 204                 $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);
3d96c5 205                 
TB 206                 if($reseller['customer_no_template'] != '') {
207                     //* Set customer no default
208                     $customer_no = $app->functions->intval($reseller['customer_no_start']+$reseller['customer_no_counter']);
a3ce7d 209                     $customer_no_string = str_replace(array('[CUSTOMER_NO]','[CLIENTID]'),array($customer_no, $reseller['client_id']),$reseller['customer_no_template']);
3d96c5 210                     $app->tpl->setVar('customer_no',$customer_no_string);
TB 211                 }
212             }
213         }
6a8a67 214         
5192db 215         if($app->auth->is_admin()) {
TB 216             // Fill the client select field
d35098 217             $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 218             $clients = $app->db->queryAllRecords($sql);
TB 219             $client_select = "<option value='0'>- ".$app->tform->lng('none_txt')." -</option>";
220             //$tmp_data_record = $app->tform->getDataRecord($this->id);
221             if(is_array($clients)) {
222                 $selected_client_id = 0; // needed to get list of PHP versions
223                 foreach($clients as $client) {
224                     if(is_array($this->dataRecord) && ($client["client_id"] == $this->dataRecord['parent_client_id']) && !$selected_client_id) $selected_client_id = $client["client_id"];
225                     $selected = @(is_array($this->dataRecord) && ($client["client_id"] == $this->dataRecord['parent_client_id']))?'SELECTED':'';
226                     if($selected == 'SELECTED') $selected_client_id = $client["client_id"];
227                     $client_select .= "<option value='$client[client_id]' $selected>$client[contactname]</option>\r\n";
228                 }
229             }
230             $app->tpl->setVar("parent_client_id", $client_select);
b488b5 231         }
6a8a67 232         
b488b5 233         parent::onShowEnd();
T 234
235     }
b1a6a5 236
b488b5 237     /*
T 238      This function is called automatically right after
239      the data was successful inserted in the database.
240     */
241     function onAfterInsert() {
242         global $app, $conf;
243         // Create the group for the client
3a11d2 244         $groupid = $app->db->datalogInsert('sys_group', array("name" => $this->dataRecord["username"], "description" => '', "client_id" => $this->id), 'groupid');
b488b5 245         $groups = $groupid;
b1a6a5 246
2af58c 247         $username = $this->dataRecord["username"];
MC 248         $password = $this->dataRecord["password"];
b488b5 249         $modules = $conf['interface_modules_enabled'];
T 250         if(isset($this->dataRecord["limit_client"]) && $this->dataRecord["limit_client"] > 0) $modules .= ',client';
b1a6a5 251         $startmodule = (stristr($modules, 'dashboard'))?'dashboard':'client';
e9d5c9 252         $usertheme = (isset($this->dataRecord["usertheme"]) && $this->dataRecord["usertheme"] != ''? $this->dataRecord["usertheme"] : 'default');
b488b5 253         $type = 'user';
T 254         $active = 1;
2af58c 255         $language = $this->dataRecord["language"];
b488b5 256         $password = $app->auth->crypt_password($password);
b1a6a5 257
b488b5 258         // Create the controlpaneluser for the client
T 259         //Generate ssh-rsa-keys
260         exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
2af58c 261         $app->db->query("UPDATE client SET created_at = UNIX_TIMESTAMP(), id_rsa = ?, ssh_rsa = ? WHERE client_id = ?", @file_get_contents('/tmp/id_rsa'), @file_get_contents('/tmp/id_rsa.pub'), $this->id);
b488b5 262         exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub');
b1a6a5 263
b488b5 264         // Create the controlpaneluser for the client
T 265         $sql = "INSERT INTO sys_user (username,passwort,modules,startmodule,app_theme,typ,active,language,groups,default_group,client_id)
2af58c 266         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
MC 267         $app->db->query($sql, $username, $password, $modules, $startmodule, $usertheme, $type, $active, $language, $groups, $groupid, $this->id);
b1a6a5 268
MC 269         //* If the user who inserted the client is a reseller (not admin), we will have to add this new client group
b488b5 270         //* to his groups, so he can administrate the records of this client.
T 271         if($_SESSION['s']['user']['typ'] == 'user') {
b1a6a5 272             $app->auth->add_group_to_user($_SESSION['s']['user']['userid'], $groupid);
2af58c 273             $app->db->query("UPDATE client SET parent_client_id = ? WHERE client_id = ?", $_SESSION['s']['user']['client_id'], $this->id);
5192db 274         } else {
TB 275             if($this->dataRecord['parent_client_id'] > 0) {
276                 //* get userid of the reseller and add it to the group of the client
2af58c 277                 $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 = ?", $this->dataRecord['parent_client_id']);
5192db 278                 $app->auth->add_group_to_user($tmp['userid'], $groupid);
2af58c 279                 $app->db->query("UPDATE client SET parent_client_id = ? WHERE client_id = ?", $this->dataRecord['parent_client_id'], $this->id);
5192db 280                 unset($tmp);
TB 281             }
b488b5 282         }
b1a6a5 283
8cf78b 284         //* Set the default servers
7d0827 285         $tmp = $app->getconf->get_global_config('mail');
SC 286         $default_mailserver = $app->functions->intval($tmp['default_mailserver']);
287         if (!$default_mailserver) {
288             $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE mail_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
289             $default_mailserver = $app->functions->intval($tmp['server_id']);
290         }
291         $tmp = $app->getconf->get_global_config('sites');
292         $default_webserver = $app->functions->intval($tmp['default_webserver']);
293         $default_dbserver = $app->functions->intval($tmp['default_dbserver']);
294         if (!$default_webserver) {
295             $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE web_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
296             $default_webserver = $app->functions->intval($tmp['server_id']);
297         }
298         if (!$default_dbserver) {
299             $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE db_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
300             $default_dbserver = $app->functions->intval($tmp['server_id']);
301         }
302         $tmp = $app->getconf->get_global_config('dns');
303         $default_dnsserver = $app->functions->intval($tmp['default_dnsserver']);
304         if (!$default_dnsserver) {
305             $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE dns_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
306             $default_dnsserver = $app->functions->intval($tmp['server_id']);
307         }
b1a6a5 308
2af58c 309         $sql = "UPDATE client SET mail_servers = ?, web_servers = ?, dns_servers = ?, default_slave_dnsserver = ?, db_servers = ? WHERE client_id = ?";
MC 310         $app->db->query($sql, $default_mailserver, $default_webserver, $default_dnsserver, $default_dnsserver, $default_dbserver, $this->id);
b1a6a5 311
MC 312         if(isset($this->dataRecord['template_master'])) {
313             $app->uses('client_templates');
314             $app->client_templates->update_client_templates($this->id, $this->_template_additional);
315         }
23229c 316         
TB 317         if($this->dataRecord['customer_no'] == $this->dataRecord['customer_no_org']) {
318             if($app->auth->is_admin()) {
319                 //* Logged in User is admin
320                 //* get the system config
321                 $app->uses('getconf');
322                 $system_config = $app->getconf->get_global_config();
323                 if($system_config['misc']['customer_no_template'] != '') {
324                 
325                     //* save new counter value
326                     $system_config['misc']['customer_no_counter']++;
327                     $system_config_str = $app->ini_parser->get_ini_string($system_config);
3a11d2 328                     $app->db->datalogUpdate('sys_ini', array("config" => $system_config_str), 'sysini_id', 1);
23229c 329                 }
TB 330             } else {
331                 //* Logged in user must be a reseller
332                 //* get the record of the reseller
333                 $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
2af58c 334                 $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);
23229c 335                 
TB 336                 if($reseller['customer_no_template'] != '') {
337                     //* save new counter value
338                     $customer_no_counter = $app->functions->intval($reseller['customer_no_counter']+1);
2af58c 339                     $app->db->query("UPDATE client SET customer_no_counter = ? WHERE client_id = ?", $customer_no_counter, $reseller['client_id']);
23229c 340                 }
TB 341             }
342         }
fedbca 343         
TB 344         //* Send welcome email
345         $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
2af58c 346         $sql = "SELECT * FROM client_message_template WHERE template_type = 'welcome' AND sys_groupid = ?";
MC 347         $email_template = $app->db->queryOneRecord($sql, $client_group_id);
fedbca 348         $client = $app->tform->getDataRecord($this->id);
TB 349
350         if(is_array($email_template) && $client['email'] != '') {
351             //* Parse client details into message
352             $message = $email_template['message'];
353             $subject = $email_template['subject'];
354             foreach($client as $key => $val) {
355                 switch ($key) {
356                 case 'password':
357                     $message = str_replace('{password}', $this->dataRecord['password'], $message);
358                     $subject = str_replace('{password}', $this->dataRecord['password'], $subject);
359                     break;
360                 case 'gender':
237f35 361                     $message = str_replace('{salutation}', $app->tform->lng('gender_'.$val.'_txt'), $message);
MC 362                     $subject = str_replace('{salutation}', $app->tform->lng('gender_'.$val.'_txt'), $subject);
fedbca 363                     break;
TB 364                 default:
365                     $message = str_replace('{'.$key.'}', $val, $message);
366                     $subject = str_replace('{'.$key.'}', $val, $subject);
367                 }
368             }
369             
370             //* Get sender address
371             if($app->auth->is_admin()) {
372                 $app->uses('getconf');
aff82c 373                 $system_config = $app->getconf->get_global_config('mail');
fedbca 374                 $from = $system_config['admin_mail'];
TB 375             } else {
376                 $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
2af58c 377                 $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);
fedbca 378                 $from = $reseller["email"];
TB 379             }
380
381             //* Send the email
382             $app->functions->mail($client['email'], $subject, $message, $from);
383         }
384         
b488b5 385
T 386         parent::onAfterInsert();
387     }
b1a6a5 388
MC 389
b488b5 390     /*
T 391      This function is called automatically right after
392      the data was successful updated in the database.
393     */
394     function onAfterUpdate() {
bfcdef 395         global $app, $conf;
b488b5 396         // username changed
b9ce1a 397         if(isset($conf['demo_mode']) && $conf['demo_mode'] != true && isset($this->dataRecord['username']) && $this->dataRecord['username'] != '' && $this->oldDataRecord['username'] != $this->dataRecord['username']) {
2af58c 398             $username = $this->dataRecord["username"];
b488b5 399             $client_id = $this->id;
2af58c 400             $sql = "UPDATE sys_user SET username = ? WHERE client_id = ?";
MC 401             $app->db->query($sql, $username, $client_id);
b1a6a5 402
2af58c 403             $tmp = $app->db->queryOneRecord("SELECT * FROM sys_group WHERE client_id = ?", $client_id);
MC 404             $app->db->datalogUpdate("sys_group", array("name" => $username), 'groupid', $tmp['groupid']);
b488b5 405             unset($tmp);
T 406         }
b1a6a5 407
b488b5 408         // password changed
b9ce1a 409         if(isset($conf['demo_mode']) && $conf['demo_mode'] != true && isset($this->dataRecord["password"]) && $this->dataRecord["password"] != '') {
2af58c 410             $password = $this->dataRecord["password"];
743892 411             $password = $app->auth->crypt_password($password);
b488b5 412             $client_id = $this->id;
2af58c 413             $sql = "UPDATE sys_user SET passwort = ? WHERE client_id = ?";
MC 414             $app->db->query($sql, $password, $client_id);
b488b5 415         }
b1a6a5 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
2af58c 423             $client_data = $app->db->queryOneRecord('SELECT `tmp_data` FROM `client` WHERE `client_id` = ?', $this->id);
b1a6a5 424
MC 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',
b1a6a5 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
2af58c 447             $udata = $app->db->queryOneRecord('SELECT `userid` FROM `sys_user` WHERE `client_id` = ?', $this->id);
MC 448             $gdata = $app->db->queryOneRecord('SELECT `groupid` FROM `sys_group` WHERE `client_id` = ?', $this->id);
b1a6a5 449             $sys_groupid = $gdata['groupid'];
MC 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();
b1a6a5 469
2af58c 470                     $entries = $app->db->queryAllRecords('SELECT ?? as `id`, `sys_userid`, ?? FROM ?? WHERE `sys_groupid` = ?', $keycolumn, $active_col, $current, $sys_groupid);
b1a6a5 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'];
b1a6a5 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']);
b1a6a5 479                     }
MC 480                 }
481
482                 $tmp_data['prev_active'] = $prev_active;
483                 $tmp_data['prev_sys_userid'] = $prev_sysuser;
2af58c 484                 $app->db->query("UPDATE `client` SET `tmp_data` = ? WHERE `client_id` = ?", serialize($tmp_data), $this->id);
b1a6a5 485                 unset($prev_active);
MC 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                     
2af58c 499                     $entries = $app->db->queryAllRecords('SELECT ?? as `id` FROM ?? WHERE `sys_groupid` = ?', $keycolumn, $current, $sys_groupid);
b1a6a5 500                     foreach($entries as $item) {
44c2dd 501                         $set_active = ($reverse == true ? 'n' : 'y');
MC 502                         $set_inactive = ($reverse == true ? 'y' : 'n');
b1a6a5 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;
b1a6a5 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']);
b1a6a5 514                     }
MC 515                 }
516                 if(array_key_exists('prev_active', $tmp_data)) unset($tmp_data['prev_active']);
2af58c 517                 $app->db->query("UPDATE `client` SET `tmp_data` = ? WHERE `client_id` = ?", serialize($tmp_data), $this->id);
b1a6a5 518             }
MC 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') {
2af58c 527                 $sql = "UPDATE sys_user SET active = '0' WHERE client_id = ?";
MC 528                 $app->db->query($sql, $this->id);
b1a6a5 529             } elseif($this->dataRecord['canceled'] == 'n') {
2af58c 530                 $sql = "UPDATE sys_user SET active = '1' WHERE client_id = ?";
MC 531                 $app->db->query($sql, $this->id);
b1a6a5 532             }
MC 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']) {
2af58c 537             $language = $this->dataRecord["language"];
cab924 538             $client_id = $this->id;
2af58c 539             $sql = "UPDATE sys_user SET language = ? WHERE client_id = ?";
MC 540             $app->db->query($sql, $language, $client_id);
cab924 541         }
b1a6a5 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             $client_id = $this->id;
2af58c 548             $sql = "UPDATE sys_user SET modules = ? WHERE client_id = ?";
MC 549             $app->db->query($sql, $modules, $client_id);
b488b5 550         }
5192db 551         
TB 552         //* Client has been moved to another reseller
553         if($_SESSION['s']['user']['typ'] == 'admin' && isset($this->dataRecord['parent_client_id']) && $this->dataRecord['parent_client_id'] != $this->oldDataRecord['parent_client_id']) {
554             //* Get groupid of the client
2af58c 555             $tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ?", $this->id);
5192db 556             $groupid = $tmp['groupid'];
TB 557             unset($tmp);
558             
559             //* Remove sys_user of old reseller from client group
560             if($this->oldDataRecord['parent_client_id'] > 0) {
561                 //* get userid of the old reseller remove it from the group of the client
2af58c 562                 $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 = ?", $this->oldDataRecord['parent_client_id']);
5192db 563                 $app->auth->remove_group_from_user($tmp['userid'], $groupid);
TB 564                 unset($tmp);
565             }
566             
567             //* Add sys_user of new reseller to client group
568             if($this->dataRecord['parent_client_id'] > 0) {
569                 //* get userid of the reseller and add it to the group of the client
2af58c 570                 $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 = ?", $this->dataRecord['parent_client_id']);
5192db 571                 $app->auth->add_group_to_user($tmp['userid'], $groupid);
2af58c 572                 $app->db->query("UPDATE client SET sys_userid = ?, sys_groupid = ?, parent_client_id = ? WHERE client_id = ?", $tmp['userid'], $tmp['default_group'], $this->dataRecord['parent_client_id'], $this->id);
5192db 573                 unset($tmp);
TB 574             } else {
575                 //* Client is not assigned to a reseller anymore, so we assign it to the admin
2af58c 576                 $app->db->query("UPDATE client SET sys_userid = 1, sys_groupid = 1, parent_client_id = 0 WHERE client_id = ?", $this->id);
5192db 577             }
TB 578         }
b1a6a5 579
MC 580         if(isset($this->dataRecord['template_master'])) {
581             $app->uses('client_templates');
582             $app->client_templates->update_client_templates($this->id, $this->_template_additional);
583         }
584
b488b5 585         parent::onAfterUpdate();
T 586     }
b1a6a5 587
b488b5 588 }
T 589
590 $page = new page_action;
591 $page->onLoad();
592
89bbd1 593 ?>