Till Brehm
2015-06-03 69a57d959dc422eaa4d10ba7005bd884b87b5fdf
commit | author | age
3dbcc3 1 <?php
T 2
3 /*
4 Copyright (c) 2008, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
7fe908 31 require_once '../../lib/config.inc.php';
MC 32 require_once '../../lib/app.inc.php';
3dbcc3 33
T 34 //* Check permissions for module
35 $app->auth->check_module_permissions('dns');
36
37
38 // Loading the template
39 $app->uses('tpl,validate_dns');
40 $app->tpl->newTemplate("form.tpl.htm");
7fe908 41 $app->tpl->setInclude('content_tpl', 'templates/dns_wizard.htm');
44d2a7 42 $app->load_language_file('/web/dns/lib/lang/'.$_SESSION['s']['language'].'_dns_wizard.lng');
3dbcc3 43
T 44 // import variables
65ea2e 45 $template_id = (isset($_POST['template_id']))?$app->functions->intval($_POST['template_id']):0;
M 46 $sys_groupid = (isset($_POST['client_group_id']))?$app->functions->intval($_POST['client_group_id']):0;
3dbcc3 47
T 48 // get the correct server_id
49 if($_SESSION['s']['user']['typ'] == 'admin') {
65ea2e 50     $server_id = (isset($_POST['server_id']))?$app->functions->intval($_POST['server_id']):1;
3dbcc3 51 } else {
35509d 52     $client_group_id = intval($_SESSION["s"]["user"]["default_group"]);
3dbcc3 53     $client = $app->db->queryOneRecord("SELECT default_dnsserver FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
35509d 54     $server_id = intval($client["default_dnsserver"]);
3dbcc3 55 }
T 56
57
58 // Load the templates
222ea2 59 $records = $app->db->queryAllRecords("SELECT * FROM dns_template WHERE visible = 'Y' ORDER BY name ASC");
3dbcc3 60 $template_id_option = '';
T 61 $n = 0;
62 foreach($records as $rec){
63     $checked = ($rec['template_id'] == $template_id)?' SELECTED':'';
64     $template_id_option .= '<option value="'.$rec['template_id'].'"'.$checked.'>'.$rec['name'].'</option>';
65     if($n == 0 && $template_id == 0) $template_id = $rec['template_id'];
66     $n++;
67 }
68 unset($n);
7fe908 69 $app->tpl->setVar("template_id_option", $template_id_option);
3dbcc3 70
T 71 // If the user is administrator
72 if($_SESSION['s']['user']['typ'] == 'admin') {
7fe908 73
3dbcc3 74     // Load the list of servers
0da7c5 75     $records = $app->db->queryAllRecords("SELECT server_id, server_name FROM server WHERE mirror_server_id = 0 AND dns_server = 1 ORDER BY server_name");
3dbcc3 76     $server_id_option = '';
T 77     foreach($records as $rec){
78         $checked = ($rec['server_id'] == $server_id)?' SELECTED':'';
79         $server_id_option .= '<option value="'.$rec['server_id'].'"'.$checked.'>'.$rec['server_name'].'</option>';
80     }
7fe908 81     $app->tpl->setVar("server_id", $server_id_option);
MC 82
3dbcc3 83     // load the list of clients
ca4b71 84     $sql = "SELECT 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 ORDER BY client.company_name, client.contact_name, sys_group.name";
3dbcc3 85     $clients = $app->db->queryAllRecords($sql);
T 86     $client_select = '';
87     if($_SESSION["s"]["user"]["typ"] == 'admin') $client_select .= "<option value='0'></option>";
88     if(is_array($clients)) {
89         foreach( $clients as $client) {
90             $selected = ($client["groupid"] == $sys_groupid)?'SELECTED':'';
615a0a 91             $client_select .= "<option value='$client[groupid]' $selected>$client[contactname]</option>\r\n";
3dbcc3 92         }
T 93     }
94
7fe908 95     $app->tpl->setVar("client_group_id", $client_select);
3dbcc3 96 }
T 97
0fd51f 98 if ($_SESSION["s"]["user"]["typ"] != 'admin' && $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
7fe908 99
0fd51f 100     // Get the limits of the client
604c0c 101     $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
615a0a 102     $client = $app->db->queryOneRecord("SELECT client.client_id, client.contact_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, sys_group.name FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
0fd51f 103
7fe908 104
0fd51f 105     // load the list of clients
ca4b71 106     $sql = "SELECT 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 client.parent_client_id = ".$app->functions->intval($client['client_id'])." ORDER BY client.company_name, client.contact_name, sys_group.name";
0fd51f 107     $clients = $app->db->queryAllRecords($sql);
604c0c 108     $tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ".$app->functions->intval($client['client_id']));
615a0a 109     $client_select = '<option value="'.$tmp['groupid'].'">'.$client['contactname'].'</option>';
0fd51f 110     if(is_array($clients)) {
T 111         foreach( $clients as $client) {
112             $selected = ($client["groupid"] == $sys_groupid)?'SELECTED':'';
615a0a 113             $client_select .= "<option value='$client[groupid]' $selected>$client[contactname]</option>\r\n";
0fd51f 114         }
T 115     }
116
7fe908 117     $app->tpl->setVar("client_group_id", $client_select);
0fd51f 118 }
T 119
120
604c0c 121 $template_record = $app->db->queryOneRecord("SELECT * FROM dns_template WHERE template_id = '".$app->functions->intval($template_id)."'");
7fe908 122 $fields = explode(',', $template_record['fields']);
3dbcc3 123 if(is_array($fields)) {
T 124     foreach($fields as $field) {
7fe908 125         $app->tpl->setVar($field."_VISIBLE", 1);
3dbcc3 126         $field = strtolower($field);
7fe908 127         $app->tpl->setVar($field, $_POST[$field]);
3dbcc3 128     }
T 129 }
130
131 if($_POST['create'] == 1) {
69a57d 132     
TB 133     //* CSRF Check
134     $app->auth->csrf_token_check();
135     
3dbcc3 136     $error = '';
7fe908 137
992797 138     // apply filters
MC 139     if(isset($_POST['domain']) && $_POST['domain'] != ''){
140         $_POST['domain'] = $app->functions->idn_encode($_POST['domain']);
141         $_POST['domain'] = strtolower($_POST['domain']);
142     }
143     if(isset($_POST['ns1']) && $_POST['ns1'] != ''){
144         $_POST['ns1'] = $app->functions->idn_encode($_POST['ns1']);
145         $_POST['ns1'] = strtolower($_POST['ns1']);
146     }
147     if(isset($_POST['ns2']) && $_POST['ns2'] != ''){
148         $_POST['ns2'] = $app->functions->idn_encode($_POST['ns2']);
149         $_POST['ns2'] = strtolower($_POST['ns2']);
150     }
151     if(isset($_POST['email']) && $_POST['email'] != ''){
152         $_POST['email'] = $app->functions->idn_encode($_POST['email']);
153         $_POST['email'] = strtolower($_POST['email']);
154     }
7fe908 155
MC 156
44d2a7 157     if(isset($_POST['domain']) && $_POST['domain'] == '') $error .= $app->lng('error_domain_empty').'<br />';
7fe908 158     elseif(isset($_POST['domain']) && !preg_match('/^[\w\.\-]{2,64}\.[a-zA-Z0-9\-]{2,30}$/', $_POST['domain'])) $error .= $app->lng('error_domain_regex').'<br />';
72695f 159
44d2a7 160     if(isset($_POST['ip']) && $_POST['ip'] == '') $error .= $app->lng('error_ip_empty').'<br />';
72695f 161
44d2a7 162     if(isset($_POST['ns1']) && $_POST['ns1'] == '') $error .= $app->lng('error_ns1_empty').'<br />';
7fe908 163     elseif(isset($_POST['ns1']) && !preg_match('/^[\w\.\-]{2,64}\.[a-zA-Z0-9]{2,30}$/', $_POST['ns1'])) $error .= $app->lng('error_ns1_regex').'<br />';
72695f 164
44d2a7 165     if(isset($_POST['ns2']) && $_POST['ns2'] == '') $error .= $app->lng('error_ns2_empty').'<br />';
7fe908 166     elseif(isset($_POST['ns2']) && !preg_match('/^[\w\.\-]{2,64}\.[a-zA-Z0-9]{2,30}$/', $_POST['ns2'])) $error .= $app->lng('error_ns2_regex').'<br />';
72695f 167
44d2a7 168     if(isset($_POST['email']) && $_POST['email'] == '') $error .= $app->lng('error_email_empty').'<br />';
7fe908 169     elseif(isset($_POST['email']) && !preg_match('/^\w+[\w.-]*\w+@\w+[\w.-]*\w+\.[a-z0-9\-]{2,30}$/i', $_POST['email'])) $error .= $app->lng('error_email_regex').'<br />';
MC 170
10b4c8 171     // make sure that the record belongs to the client group and not the admin group when admin inserts it
721fb2 172     if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($_POST['client_group_id'])) {
65ea2e 173         $sys_groupid = $app->functions->intval($_POST['client_group_id']);
721fb2 174     } elseif($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($_POST['client_group_id'])) {
65ea2e 175         $sys_groupid = $app->functions->intval($_POST['client_group_id']);
721fb2 176     } else {
T 177         $sys_groupid = $_SESSION["s"]["user"]["default_group"];
178     }
7fe908 179
8fb58f 180     $tform_def_file = "form/dns_soa.tform.php";
T 181     $app->uses('tform');
182     $app->tform->loadFormDef($tform_def_file);
7fe908 183
616d34 184     if($_SESSION['s']['user']['typ'] != 'admin') {
T 185         if(!$app->tform->checkClientLimit('limit_dns_zone')) {
186             $error .= $app->tform->wordbook["limit_dns_zone_txt"];
187         }
188         if(!$app->tform->checkResellerLimit('limit_dns_zone')) {
189             $error .= $app->tform->wordbook["limit_dns_zone_txt"];
190         }
3cebc3 191     }
7fe908 192
MC 193
3dbcc3 194     // replace template placeholders
T 195     $tpl_content = $template_record['template'];
7fe908 196     if($_POST['domain'] != '') $tpl_content = str_replace('{DOMAIN}', $_POST['domain'], $tpl_content);
MC 197     if($_POST['ip'] != '') $tpl_content = str_replace('{IP}', $_POST['ip'], $tpl_content);
198     if($_POST['ns1'] != '') $tpl_content = str_replace('{NS1}', $_POST['ns1'], $tpl_content);
199     if($_POST['ns2'] != '') $tpl_content = str_replace('{NS2}', $_POST['ns2'], $tpl_content);
200     if($_POST['email'] != '') $tpl_content = str_replace('{EMAIL}', $_POST['email'], $tpl_content);
201
992797 202     // Parse the template
7fe908 203     $tpl_rows = explode("\n", $tpl_content);
3dbcc3 204     $section = '';
T 205     $vars = array();
206     $dns_rr = array();
207     foreach($tpl_rows as $row) {
208         $row = trim($row);
7fe908 209         if(substr($row, 0, 1) == '[') {
3dbcc3 210             if($row == '[ZONE]') {
T 211                 $section = 'zone';
212             } elseif($row == '[DNS_RECORDS]') {
213                 $section = 'dns_records';
214             } else {
215                 die('Unknown section type');
216             }
217         } else {
218             if($row != '') {
219                 // Handle zone section
220                 if($section == 'zone') {
7fe908 221                     $parts = explode('=', $row);
3dbcc3 222                     $key = trim($parts[0]);
T 223                     $val = trim($parts[1]);
224                     if($key != '') $vars[$key] = $val;
225                 }
226                 // Handle DNS Record rows
227                 if($section == 'dns_records') {
7fe908 228                     $parts = explode('|', $row);
3dbcc3 229                     $dns_rr[] = array(
T 230                         'name' => $app->db->quote($parts[1]),
231                         'type' => $app->db->quote($parts[0]),
232                         'data' => $app->db->quote($parts[2]),
233                         'aux'  => $app->db->quote($parts[3]),
234                         'ttl'  => $app->db->quote($parts[4])
235                     );
236                 }
237             }
238         }
7fe908 239
3dbcc3 240     } // end foreach
7fe908 241
44d2a7 242     if($vars['origin'] == '') $error .= $app->lng('error_origin_empty').'<br />';
T 243     if($vars['ns'] == '') $error .= $app->lng('error_ns_empty').'<br />';
244     if($vars['mbox'] == '') $error .= $app->lng('error_mbox_empty').'<br />';
245     if($vars['refresh'] == '') $error .= $app->lng('error_refresh_empty').'<br />';
246     if($vars['retry'] == '') $error .= $app->lng('error_retry_empty').'<br />';
247     if($vars['expire'] == '') $error .= $app->lng('error_expire_empty').'<br />';
248     if($vars['minimum'] == '') $error .= $app->lng('error_minimum_empty').'<br />';
249     if($vars['ttl'] == '') $error .= $app->lng('error_ttl_empty').'<br />';
7fe908 250
3dbcc3 251     if($error == '') {
T 252         // Insert the soa record
253         $sys_userid = $_SESSION['s']['user']['userid'];
254         $origin = $app->db->quote($vars['origin']);
255         $ns = $app->db->quote($vars['ns']);
7fe908 256         $mbox = $app->db->quote(str_replace('@', '.', $vars['mbox']));
3dbcc3 257         $refresh = $app->db->quote($vars['refresh']);
T 258         $retry = $app->db->quote($vars['retry']);
259         $expire = $app->db->quote($vars['expire']);
260         $minimum = $app->db->quote($vars['minimum']);
261         $ttl = $app->db->quote($vars['ttl']);
262         $xfer = $app->db->quote($vars['xfer']);
e699a5 263         $also_notify = $app->db->quote($vars['also_notify']);
90368d 264         $update_acl = $app->db->quote($vars['update_acl']);
3dbcc3 265         $serial = $app->validate_dns->increase_serial(0);
7fe908 266
MC 267         $insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `origin`, `ns`, `mbox`, `serial`, `refresh`, `retry`, `expire`, `minimum`, `ttl`, `active`, `xfer`, `also_notify`, `update_acl`) VALUES
90368d 268         ('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$origin', '$ns', '$mbox', '$serial', '$refresh', '$retry', '$expire', '$minimum', '$ttl', 'Y', '$xfer', '$also_notify', '$update_acl')";
3dbcc3 269         $dns_soa_id = $app->db->datalogInsert('dns_soa', $insert_data, 'id');
7fe908 270
3dbcc3 271         // Insert the dns_rr records
T 272         if(is_array($dns_rr) && $dns_soa_id > 0) {
273             foreach($dns_rr as $rr) {
7fe908 274                 $insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `zone`, `name`, `type`, `data`, `aux`, `ttl`, `active`) VALUES
3dbcc3 275                 ('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$dns_soa_id', '$rr[name]', '$rr[type]', '$rr[data]', '$rr[aux]', '$rr[ttl]', 'Y')";
T 276                 $dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
277             }
278         }
7fe908 279
3dbcc3 280         header("Location: dns_soa_list.php");
T 281         exit;
7fe908 282
3dbcc3 283     } else {
7fe908 284         $app->tpl->setVar("error", $error);
3dbcc3 285     }
7fe908 286
3dbcc3 287 }
T 288
289
290
7fe908 291 $app->tpl->setVar("title", 'DNS Wizard');
3dbcc3 292
69a57d 293 //* SET csrf token
TB 294 $csrf_token = $app->auth->csrf_token_get('dns_wizard');
295 $app->tpl->setVar('_csrf_id',$csrf_token['csrf_id']);
296 $app->tpl->setVar('_csrf_key',$csrf_token['csrf_key']);
297
3dbcc3 298 $lng_file = 'lib/lang/'.$_SESSION['s']['language'].'_dns_wizard.lng';
7fe908 299 include $lng_file;
3dbcc3 300 $app->tpl->setVar($wb);
T 301
302 $app->tpl_defaults();
992797 303 $app->tpl->pparse();
MC 304
305
7fe908 306 ?>