Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
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
222ea2 132         $sql = "SELECT template_id,template_name FROM client_template WHERE template_type = 'a' 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']);
209                     $customer_no_string = str_replace('[CUSTOMER_NO]',$customer_no,$reseller['customer_no_template']);
210                     $app->tpl->setVar('customer_no',$customer_no_string);
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"];
f5d954 411             $salt="$1$";
T 412             $base64_alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
413             for ($n=0;$n<8;$n++) {
b1a6a5 414                 $salt.=$base64_alphabet[mt_rand(0, 63)];
f5d954 415             }
T 416             $salt.="$";
b1a6a5 417             $password = crypt(stripslashes($password), $salt);
b488b5 418             $client_id = $this->id;
2af58c 419             $sql = "UPDATE sys_user SET passwort = ? WHERE client_id = ?";
MC 420             $app->db->query($sql, $password, $client_id);
b488b5 421         }
b1a6a5 422
MC 423         if(!isset($this->dataRecord['locked'])) $this->dataRecord['locked'] = 'n';
424         if(isset($conf['demo_mode']) && $conf['demo_mode'] != true && $this->dataRecord["locked"] != $this->oldDataRecord['locked']) {
425             /** lock all the things like web, mail etc. - easy to extend */
426
427
428             // get tmp_data of client
2af58c 429             $client_data = $app->db->queryOneRecord('SELECT `tmp_data` FROM `client` WHERE `client_id` = ?', $this->id);
b1a6a5 430
MC 431             if($client_data['tmp_data'] == '') $tmp_data = array();
432             else $tmp_data = unserialize($client_data['tmp_data']);
433
434             if(!is_array($tmp_data)) $tmp_data = array();
435
436             // database tables with their primary key columns
437             $to_disable = array('cron' => 'id',
438                 'ftp_user' => 'ftp_user_id',
439                 'mail_domain' => 'domain_id',
44c2dd 440                 'mail_user' => 'mailuser_id',
MC 441                 'mail_user_smtp' => 'mailuser_id',
b1a6a5 442                 'mail_forwarding' => 'forwarding_id',
MC 443                 'mail_get' => 'mailget_id',
444                 'openvz_vm' => 'vm_id',
445                 'shell_user' => 'shell_user_id',
446                 'webdav_user' => 'webdav_user_id',
447                 'web_database' => 'database_id',
448                 'web_domain' => 'domain_id',
449                 'web_folder' => 'web_folder_id',
450                 'web_folder_user' => 'web_folder_user_id'
451             );
452
2af58c 453             $udata = $app->db->queryOneRecord('SELECT `userid` FROM `sys_user` WHERE `client_id` = ?', $this->id);
MC 454             $gdata = $app->db->queryOneRecord('SELECT `groupid` FROM `sys_group` WHERE `client_id` = ?', $this->id);
b1a6a5 455             $sys_groupid = $gdata['groupid'];
MC 456             $sys_userid = $udata['userid'];
457
458             $entries = array();
459             if($this->dataRecord['locked'] == 'y') {
460                 $prev_active = array();
461                 $prev_sysuser = array();
462                 foreach($to_disable as $current => $keycolumn) {
44c2dd 463                     $active_col = 'active';
MC 464                     $reverse = false;
465                     if($current == 'mail_user') {
466                         $active_col = 'postfix';
467                     } elseif($current == 'mail_user_smtp') {
468                         $current = 'mail_user';
469                         $active_col = 'disablesmtp';
470                         $reverse = true;
471                     }
472                     
473                     if(!isset($prev_active[$current])) $prev_active[$current] = array();
474                     if(!isset($prev_sysuser[$current])) $prev_sysuser[$current] = array();
b1a6a5 475
2af58c 476                     $entries = $app->db->queryAllRecords('SELECT ?? as `id`, `sys_userid`, ?? FROM ?? WHERE `sys_groupid` = ?', $keycolumn, $active_col, $current, $sys_groupid);
b1a6a5 477                     foreach($entries as $item) {
MC 478
44c2dd 479                         if($item[$active_col] != 'y' && $reverse == false) $prev_active[$current][$item['id']][$active_col] = 'n';
MC 480                         elseif($item[$active_col] == 'y' && $reverse == true) $prev_active[$current][$item['id']][$active_col] = 'y';
481                         if($item['sys_userid'] != $sys_userid) $prev_sysuser[$current][$item['id']] = $item['sys_userid'];
b1a6a5 482                         // we don't have to store these if y, as everything without previous state gets enabled later
MC 483
44c2dd 484                         $app->db->datalogUpdate($current, array($active_col => ($reverse == true ? 'y' : 'n'), 'sys_userid' => $_SESSION["s"]["user"]["userid"]), $keycolumn, $item['id']);
b1a6a5 485                     }
MC 486                 }
487
488                 $tmp_data['prev_active'] = $prev_active;
489                 $tmp_data['prev_sys_userid'] = $prev_sysuser;
2af58c 490                 $app->db->query("UPDATE `client` SET `tmp_data` = ? WHERE `client_id` = ?", serialize($tmp_data), $this->id);
b1a6a5 491                 unset($prev_active);
MC 492                 unset($prev_sysuser);
493             } elseif($this->dataRecord['locked'] == 'n') {
494                 foreach($to_disable as $current => $keycolumn) {
44c2dd 495                     $active_col = 'active';
MC 496                     $reverse = false;
497                     if($current == 'mail_user') {
498                         $active_col = 'postfix';
499                     } elseif($current == 'mail_user_smtp') {
500                         $current = 'mail_user';
501                         $active_col = 'disablesmtp';
502                         $reverse = true;
503                     }
504                     
2af58c 505                     $entries = $app->db->queryAllRecords('SELECT ?? as `id` FROM ?? WHERE `sys_groupid` = ?', $keycolumn, $current, $sys_groupid);
b1a6a5 506                     foreach($entries as $item) {
44c2dd 507                         $set_active = ($reverse == true ? 'n' : 'y');
MC 508                         $set_inactive = ($reverse == true ? 'y' : 'n');
b1a6a5 509                         $set_sysuser = $sys_userid;
MC 510                         if(array_key_exists('prev_active', $tmp_data) == true
511                             && array_key_exists($current, $tmp_data['prev_active']) == true
512                             && array_key_exists($item['id'], $tmp_data['prev_active'][$current]) == true
44c2dd 513                             && $tmp_data['prev_active'][$current][$item['id']][$active_col] == $set_inactive) $set_active = $set_inactive;
b1a6a5 514                         if(array_key_exists('prev_sysuser', $tmp_data) == true
MC 515                             && array_key_exists($current, $tmp_data['prev_sysuser']) == true
516                             && array_key_exists($item['id'], $tmp_data['prev_sysuser'][$current]) == true
517                             && $tmp_data['prev_sysuser'][$current][$item['id']] != $sys_userid) $set_sysuser = $tmp_data['prev_sysuser'][$current][$item['id']];
518
44c2dd 519                         $app->db->datalogUpdate($current, array($active_col => $set_active, 'sys_userid' => $set_sysuser), $keycolumn, $item['id']);
b1a6a5 520                     }
MC 521                 }
522                 if(array_key_exists('prev_active', $tmp_data)) unset($tmp_data['prev_active']);
2af58c 523                 $app->db->query("UPDATE `client` SET `tmp_data` = ? WHERE `client_id` = ?", serialize($tmp_data), $this->id);
b1a6a5 524             }
MC 525             unset($tmp_data);
526             unset($entries);
527             unset($to_disable);
528         }
529
530         if(!isset($this->dataRecord['canceled'])) $this->dataRecord['canceled'] = 'n';
531         if(isset($conf['demo_mode']) && $conf['demo_mode'] != true && $this->dataRecord["canceled"] != $this->oldDataRecord['canceled']) {
532             if($this->dataRecord['canceled'] == 'y') {
2af58c 533                 $sql = "UPDATE sys_user SET active = '0' WHERE client_id = ?";
MC 534                 $app->db->query($sql, $this->id);
b1a6a5 535             } elseif($this->dataRecord['canceled'] == 'n') {
2af58c 536                 $sql = "UPDATE sys_user SET active = '1' WHERE client_id = ?";
MC 537                 $app->db->query($sql, $this->id);
b1a6a5 538             }
MC 539         }
540
cab924 541         // language changed
b9ce1a 542         if(isset($conf['demo_mode']) && $conf['demo_mode'] != true && isset($this->dataRecord['language']) && $this->dataRecord['language'] != '' && $this->oldDataRecord['language'] != $this->dataRecord['language']) {
2af58c 543             $language = $this->dataRecord["language"];
cab924 544             $client_id = $this->id;
2af58c 545             $sql = "UPDATE sys_user SET language = ? WHERE client_id = ?";
MC 546             $app->db->query($sql, $language, $client_id);
cab924 547         }
b1a6a5 548
5192db 549         //* reseller status changed
b488b5 550         if(isset($this->dataRecord["limit_client"]) && $this->dataRecord["limit_client"] != $this->oldDataRecord["limit_client"]) {
3398c2 551             $modules = $conf['interface_modules_enabled'];
b488b5 552             if($this->dataRecord["limit_client"] > 0) $modules .= ',client';
T 553             $client_id = $this->id;
2af58c 554             $sql = "UPDATE sys_user SET modules = ? WHERE client_id = ?";
MC 555             $app->db->query($sql, $modules, $client_id);
b488b5 556         }
5192db 557         
TB 558         //* Client has been moved to another reseller
559         if($_SESSION['s']['user']['typ'] == 'admin' && isset($this->dataRecord['parent_client_id']) && $this->dataRecord['parent_client_id'] != $this->oldDataRecord['parent_client_id']) {
560             //* Get groupid of the client
2af58c 561             $tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ?", $this->id);
5192db 562             $groupid = $tmp['groupid'];
TB 563             unset($tmp);
564             
565             //* Remove sys_user of old reseller from client group
566             if($this->oldDataRecord['parent_client_id'] > 0) {
567                 //* get userid of the old reseller remove it from the group of the client
2af58c 568                 $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 569                 $app->auth->remove_group_from_user($tmp['userid'], $groupid);
TB 570                 unset($tmp);
571             }
572             
573             //* Add sys_user of new reseller to client group
574             if($this->dataRecord['parent_client_id'] > 0) {
575                 //* get userid of the reseller and add it to the group of the client
2af58c 576                 $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 577                 $app->auth->add_group_to_user($tmp['userid'], $groupid);
2af58c 578                 $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 579                 unset($tmp);
TB 580             } else {
581                 //* Client is not assigned to a reseller anymore, so we assign it to the admin
2af58c 582                 $app->db->query("UPDATE client SET sys_userid = 1, sys_groupid = 1, parent_client_id = 0 WHERE client_id = ?", $this->id);
5192db 583             }
TB 584         }
b1a6a5 585
MC 586         if(isset($this->dataRecord['template_master'])) {
587             $app->uses('client_templates');
588             $app->client_templates->update_client_templates($this->id, $this->_template_additional);
589         }
590
b488b5 591         parent::onAfterUpdate();
T 592     }
b1a6a5 593
b488b5 594 }
T 595
596 $page = new page_action;
597 $page->onLoad();
598
89bbd1 599 ?>