-
Notifications
You must be signed in to change notification settings - Fork 6
/
guifi_hosts.inc.php
400 lines (355 loc) · 13 KB
/
guifi_hosts.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
* @file guifi_hosts.inc.php
*/
/* guifi_host_form(): Main host form (Common parameters)*/
function guifi_host_form($edit,$form_weight) {
global $user;
guifi_log(GUIFILOG_TRACE,'function guifi_host_form()',$edit);
$collapse = FALSE;
$msg = (count($edit['hosts'])) ?
format_plural(count($edit['hosts']),'1 host','@count hosts') :
t('No hosts');
if (count($edit['hosts']))
foreach ($edit['hosts'] as $value)
if ($value['unfold'])
$collapse =FALSE;
$form['r'] = array(
'#type' => 'fieldset',
'#title' => $msg ,
'#collapsible' => TRUE,
'#collapsed' => $collapse,
'#tree' => FALSE,
'#prefix' => '<img src="/'.
drupal_get_path('module', 'guifi').
'/icons/home.png"> '.t("Host Name's for this domain"),
'#weight' => $form_weight++,
);
$form['r']['hosts'] = array('#tree' => TRUE);
$rc = 0;
if (!empty($edit['hosts']))
foreach ($edit['hosts'] as $key => $host) {
$form['r']['hosts'][$key] = guifi_host_host_form($host,$key,$form_weight);
$form['r']['hosts'][$key]['aliases'] = array(
'#type' => 'fieldset',
'#title' => t('Aliases (CNAME)'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Host aliases. Save or press "Preview" to get more entries'),
'#weight' => $form_weight++,
);
// after rebuild forms, check serialized values.
unset ($alias);
if (!empty($host['aliases'])) {
$checkalias = substr((string)$host['aliases'], 0, 2);
if ($checkalias == 'a:') {
$alias = unserialize($host['aliases']);
} else {
$alias = $host['aliases'];
}
}
$cname_description = t('Enter the host alias. ex: www<br /> Must put a DOT "<strong>.</strong>" at the end of the domain name if the alias points to an external domain. ex: "externalhost.dyndns.org<strong>.</strong>". <strong>ONLY ONE</strong> external domain alias allowed');
if (count($alias)) {
foreach ($alias as $delta => $value)
if (!empty($value))
$form['r']['hosts'][$key]['aliases'][] = array(
'#type' => 'textfield',
'#title' => t('CNAME'),
'#size' => 48,
'#maxlength' => 48,
'#description' => $cname_description,
'#default_value' => $value,
);
}
for ($i = 0; $i < 1; $i++)
$form['r']['hosts'][$key]['aliases'][] = array(
'#type' => 'textfield',
'#size' => 48,
'#maxlength' => 48,
'#description' => $cname_description,
);
if (($host['host'] == 'ns1' ) AND ($host['counter'] == '0')) {
$access = FALSE;
$options = array('MX' => 'MX');
$optionsdesc = t('Select these options only if this feature has been made by this host.
<br /><b>NS</b>: Selected as default on host \'ns1\'. This is your primary NameServer forced, cannot uncheck it.
<br /><b>MX</b>: Choose this option if this host has to perform tasks of <b>MailHost</b> server. Select, then Save and edit again to set priority on a new field.');
} else {
$access = TRUE;
$options = array('NS' => 'NS','MX' => 'MX');
$optionsdesc = t('Select these options only if this feature has been made by this host.
<br /><b>NS</b>: Choose this option if this host has to perform tasks of <b>NameServer</b>.
<br /><b>MX</b>: Choose this option if this host has to perform tasks of <b>MailHost</b> server. Select, then Save and edit again to set priority on a new field.');
}
unset ($nsoptions);
$form['r']['hosts'][$key]['opt'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => $optionsdesc,
'#weight' => $form_weight++,
);
if (!empty($host['options'])) {
$checkoptions = substr((string)$host['options'], 0, 2);
if ($checkoptions == 'a:')
$nsoptions = unserialize((string)$host['options']);
$nsmx = unserialize((string)$host['options']);
} else {
$nsoptions = array($host['opt']['options']['NS'],$host['opt']['options']['MX']);
if ($host['opt']['options']['MX'] === 'MX') {
$nsmx['mxprior']= $host['opt']['mxprior'];
}
}
$form['r']['hosts'][$key]['opt']['options'] = array(
'#type' => 'checkboxes',
'#title' => t('Options'),
'#default_value' => $nsoptions,
'#options' => $options,
);
if ($host['opt']['options']['MX'] === 'MX') {
$mxprior = $nsmx['mxprior'] ? $nsmx['mxprior'] : 10;
}
if ( $nsoptions['MX'] === 'MX') {
$mxprior = $nsmx['mxprior'] ? $nsmx['mxprior'] : 10;
}
if (($host['opt']['options']['MX'] === 'MX') OR ( $nsoptions['MX'] === 'MX')) {
$form['r']['hosts'][$key]['opt']['mxprior'] = array(
'#type' => 'textfield',
'#title' => t('MX Priority'),
'#size' => 3,
'#maxlength' => 3,
'#default_value' => $mxprior,
'#description' => t('One can have several mail servers, with priorities of 10, 20 and so on. A mail server attempting to deliver to example.org would first try the highest priority MX (the record with the lowest priority number), then the second highest, etc, until the mail can be properly delivered. They must be a multiple of 10 and can not be repeated.'),
);
}
$bw = $form_weight - 1000;
if (!isset($host['deleted'])) {
// Only allow delete and move functions if the host has been saved
if ($host['new']==FALSE) {
$form['r']['hosts'][$key]['delete'] = array(
'#type' => 'image_button',
'#access' => $access,
'#src' => drupal_get_path('module', 'guifi').'/icons/drop.png',
'#parents' => array('hosts',$key,'delete'),
'#attributes' => array('title' => t('Delete host')),
'#submit' => array('guifi_host_delete_submit'),
'#weight' => $bw++
);
}
$rc++;
}
} // foreach host
// Add host?
$newhostname = 'HostName-'.(count($edit['hosts'])+1);
$form['r']['hosts']['newhost_name'] = array(
'#type' => 'hidden',
'#parents' => array('newhost_name'),
'#default_value' => $newhostname,
);
$form['r']['hosts']['AddHost'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'guifi').'/icons/add.png',
'#attributes' => array('title' => t('Add New Host')),
'#parents' => array('AddHost'),
'#executes_submit_callback' => TRUE,
'#submit' => array(guifi_host_add_host_submit),
'#weight' => $form_weight++,
);
return $form;
}
function guifi_host_host_form($host, $key, &$form_weight = -200) {
guifi_log(GUIFILOG_TRACE,sprintf('function _guifi_host_host_form(key=%d)',$key),$host);
$f['storage'] = guifi_form_hidden_var(
$host,
array('counter'),
array('hosts',$key)
);
if (empty($host['ipv4'])) {
$lsav4 = ' ';
} else {
$lsav4 = ' ··· '.t('IPv4:').' '.$host['ipv4'];
}
if (empty($host['ipv6'])) {
$lsav6 = ' ';
} else {
$lsav6 = ' ··· '.t('IPv6:').' '.$host['ipv6'];
}
$f = array(
'#type' => 'fieldset',
'#title' => t('Host:').' '.$host['host'].$lsav4.$lsav6,
'#collapsible' => TRUE,
'#collapsed' => !(isset($host['unfold'])),
'#tree'=> TRUE,
'#weight' => $form_weight++,
);
if (($host['host'] == 'ns1' ) AND ($host['counter'] == '0')) {
$access = FALSE;
} else {
$access = TRUE;
}
$f[] = array(
'#type' => 'textfield',
'#access' => $access,
'#title' => t('Host Name'),
'#parents' => array('hosts',$key,'host'),
'#default_value' => $host['host'],
'#weight' => $form_weight++,
);
$f[] = array(
'#type' => 'textfield',
'#title' => t("IPv4 Address"),
'#parents' => array('hosts',$key,'ipv4'),
'#default_value' => $host['ipv4'],
'#element_validate' => array('guifi_ipv4_validate'),
'#description' => t('Leave it blank if you want to use an alias (CNAME ) to an external domain.<br /> ex: hostname is an alias of hostname.dyndns.org.').
t('<br>This address is usually the same IP as the domain NameServer.'),
'#weight' => $form_weight++,
);
$f[] = array(
'#type' => 'textfield',
'#title' => t("IPv6 Address"),
'#parents' => array('hosts',$key,'ipv6'),
'#default_value' => $host['ipv6'],
'#element_validate' => array('guifi_ipv6_validate'),
'#description' => t('Leave it blank if you want to use an alias (CNAME ) to an external domain.<br /> ex: hostname is an alias of hostname.dyndns.org.').
t('<br>This address is usually the same IP as the domain NameServer.'),
'#weight' => $form_weight++,
);
if ($host['deleted']) {
$f['deletedMsg'] = array(
'#type' => 'item',
'#value' => guifi_device_item_delete_msg(
"This host has been deleted"),
'#weight' => $form_weight++
);
$f['deleted'] = array(
'#type' => 'hidden',
'#value' => TRUE
);
}
if ($host['new']) {
$f['new'] = array(
'#type' => 'hidden',
'#parents' => array('hosts',$key,'new'),
'#value' => TRUE
);
}
return $f;
}
function guifi_ipv4_validate($element,&$form_state) {
guifi_log(GUIFILOG_TRACE,"function _guifi_host_ipv4_validate()");
if ($form_state['clicked_button']['#value'] == t('Reset'))
return;
if ($element['#value']) {
$value = $element['#value'];
if(filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return $element;
} else {
form_error($element, t('Invalid %ipv4 address',array('%ipv4' => $value)));
}
}
}
function guifi_ipv6_validate($element,&$form_state) {
guifi_log(GUIFILOG_TRACE,"function _guifi_host_ipv4_validate()");
if ($form_state['clicked_button']['#value'] == t('Reset'))
return;
if ($element['#value']) {
$value = $element['#value'];
if(filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return $element;
} else {
form_error($element, t('Invalid %ipv6 address',array('%ipv6' => $value)));
}
}
}
/* Add a host to the device */
function guifi_host_add_host_submit(&$form, &$form_state) {
guifi_log(GUIFILOG_TRACE, "function guifi_host_add_host_submit()",$form_state['values']);
if ($form_state['values']['newhost_name'] == NULL)
return TRUE;
$edit = $form_state['values'];
// next id
$rc = 0; // Host hostdev_counter next pointer
$tc = 0; // Total active hosts
// fills $rc & $tc proper values
if (isset($edit['hosts'])) foreach ($edit['hosts'] as $k => $r)
if ($k+1 > $rc) {
$rc = $k+1;
if (!$edit['hosts'][$k][delete])
$tc++;
}
$node=node_load(array('nid' => $edit['sid']));
$host=array();
$host['new']=TRUE;
$host['host']=$edit['newhost_name'];
$host['counter'] = $rc;
$host['unfold'] = TRUE;
$form_state['rebuild'] = TRUE;
$form_state['values']['hosts'][] = $host;
drupal_set_message(t(' %host added',
array('%host' => $host['host'])));
return;
}
function guifi_host_delete_submit($form, &$form_state) {
guifi_log(GUIFILOG_TRACE,"function guifi_host_delete_submit()",$form_state['clicked_button]']);
$host_id = $form_state['clicked_button']['#parents'][1];
$form_state['values']['hosts'][$host_id]['deleted'] = TRUE;
$form_state['values']['hosts'][$host_id]['unfold'] = TRUE;
drupal_set_message(t('Hostname <strong>%hostname</strong> has been deleted.',
array('%hostname' => $form_state['values']['hosts'][$host_id]['host'])));
$form_state['rebuild'] = TRUE;
return;
}
function guifi_hosts_print_data($id) {
$querydom = db_query("
SELECT *
FROM {guifi_dns_domains}
WHERE id = %d",
$id
);
$domain = db_fetch_object($querydom);
$queryhosts = db_query("
SELECT *
FROM {guifi_dns_hosts}
WHERE id = '%d'",
$domain->id
);
while ($host = db_fetch_array($queryhosts)) {
$aliases = unserialize($host['aliases']);
$options = unserialize($host['options']);
if ($aliases) {
$alias = implode(' | ', $aliases);
} else {
$alias = NULL;
}
if ($options) {
if ($options['NS'] === 'NS')
$nameserver = 'yes';
else
$nameserver = 'no';
if ($options['MX'] === 'MX') {
$mailserver = 'yes';
$priority = $options['mxprior'];
} else {
$mailserver = 'no';
$priority = '';
}
} else {
$nameserver = 'no';
$mailserver = 'no';
}
$rows[] = array(
strtolower($host['host']).'.'.$domain->name,
$alias,
$host['ipv4'],
$host['ipv6'],
$nameserver,
$mailserver,
$priority
);
}
if ($rows)
return array_merge($rows);
}
?>