Subversion Repositories ALCASAR

Rev

Rev 2479 | Rev 2559 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 2479 Rev 2558
Line 1... Line 1...
1
<?php
1
<?php
2
# $Id: network.php 2479 2018-01-04 13:53:44Z tom.houdayer $
2
# $Id: network.php 2558 2018-06-05 21:56:34Z rexy $
3
 
3
 
4
// written by steweb57, Rexy & Tom HOUDAYER
4
// written by steweb57, Rexy & Tom HOUDAYER
5
 
5
 
6
/********************
6
/********************
7
*  READ CONF FILES  *
7
*  READ CONF FILES  *
8
*********************/
8
*********************/
9
define('CONF_FILE', '/usr/local/etc/alcasar.conf');
9
define('CONF_FILE', '/usr/local/etc/alcasar.conf');
10
define('ETHERS_FILE', '/usr/local/etc/alcasar-ethers');
10
define('ETHERS_FILE', '/usr/local/etc/alcasar-ethers');
11
define('ETHERS_INFO_FILE', '/usr/local/etc/alcasar-ethers-info');
11
define('ETHERS_INFO_FILE', '/usr/local/etc/alcasar-ethers-info');
12
define('DNS_LOCAL_FILE', '/usr/local/etc/alcasar-dns-name');
12
define('DNS_LOCAL_FILE', '/etc/hosts');
13
define('LETS_ENCRYPT_FILE', '/usr/local/etc/alcasar-letsencrypt');
13
define('LETS_ENCRYPT_FILE', '/usr/local/etc/alcasar-letsencrypt');
14
$conf_files = [CONF_FILE, ETHERS_FILE, ETHERS_INFO_FILE, DNS_LOCAL_FILE, LETS_ENCRYPT_FILE];
14
$conf_files = [CONF_FILE, ETHERS_FILE, ETHERS_INFO_FILE, DNS_LOCAL_FILE, LETS_ENCRYPT_FILE];
15
 
15
 
16
// Files reading test
16
// Files reading test
17
foreach ($conf_files as $file) {
17
foreach ($conf_files as $file) {
Line 221... Line 221...
221
		$add_ip   = trim($_POST['add_ip']);
221
		$add_ip   = trim($_POST['add_ip']);
222
		if (((!empty($add_host)) && (preg_match($reg_host, $add_host))) && ((!empty($add_ip)) && (preg_match($reg_ip, $add_ip)))) {
222
		if (((!empty($add_host)) && (preg_match($reg_host, $add_host))) && ((!empty($add_ip)) && (preg_match($reg_ip, $add_ip)))) {
223
			$tab = file(DNS_LOCAL_FILE);
223
			$tab = file(DNS_LOCAL_FILE);
224
			if ($tab) { // the file isn't empty
224
			if ($tab) { // the file isn't empty
225
				$insert = true;
225
				$insert = true;
226
				foreach ($tab as $line) { // verify that host or IP address doesn't exist
226
				foreach ($tab as $line) { // verify that host or IP address doesn't already exist
227
					if (preg_match('/^address/', $line)) {
-
 
228
						$field = explode('/', $line);
227
					$field = explode('\t', $line);
229
						$host_name = trim($field[1]);
228
					$host_name = trim($field[1]);
230
						$ip_addr   = trim($field[2]);
229
					$ip_addr   = trim($field[2]);
231
						if (strcasecmp($add_host, $host_name) === 0) {
230
					if (strcasecmp($add_host, $host_name) === 0) {
232
							$insert = false;
-
 
233
							break;
-
 
234
						}
-
 
235
						if (strcmp($add_ip, $ip_addr) === 0) {
-
 
236
							$insert = false;
231
						$insert = false;
237
							break;
232
						break;
238
						}
-
 
239
					}
233
					}
-
 
234
					if (strcmp($add_ip, $ip_addr) === 0) {
-
 
235
						$insert = false;
-
 
236
						break;
240
				}
237
					}
-
 
238
			}
241
				if ($insert) {
239
				if ($insert) {
242
					$line1 = 'address=/'.$add_host.'/'.$add_ip."\n";
240
					$line1 = $add_ip.'\t'.$add_host."\n";
243
					$explode_ip = explode('.', $add_ip);
-
 
244
					$reverse_ip = implode('.', array_reverse($explode_ip));
-
 
245
					$line2 = 'ptr-record='.$reverse_ip.'.in-addr.arpa,'.$add_host."\n";
-
 
246
					$pointeur=fopen(DNS_LOCAL_FILE, 'a');
241
					$pointeur=fopen(DNS_LOCAL_FILE, 'a');
247
					fwrite($pointeur, $line1);
242
					fwrite($pointeur, $line1);
248
					fwrite($pointeur, $line2);
-
 
249
					fclose($pointeur);
243
					fclose($pointeur);
250
					exec('sudo /usr/bin/systemctl restart dnsmasq');
244
					exec('sudo /usr/bin/systemctl restart dnsmasq');
251
					exec('sudo /usr/bin/systemctl restart dnsmasq-blacklist');
245
					exec('sudo /usr/bin/systemctl restart dnsmasq-blacklist');
252
					exec('sudo /usr/bin/systemctl restart dnsmasq-whitelist');
246
					exec('sudo /usr/bin/systemctl restart dnsmasq-whitelist');
253
				}
247
				}