mirror of
https://github.com/billz/raspap-webgui.git
synced 2025-03-01 10:31:47 +00:00
Implemented start of web interface to update Static IP addresses or use DHCP.
Currently saves to files in /etc/raspap/networking, still need to build something to generate a working config for dhcpcd
This commit is contained in:
4
ajax/networking/get_all_interfaces.php
Normal file
4
ajax/networking/get_all_interfaces.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
exec("ls /sys/class/net | grep -v lo", $interfaces);
|
||||
echo json_encode($interfaces);
|
||||
?>
|
27
ajax/networking/get_int_config.php
Normal file
27
ajax/networking/get_int_config.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
session_start();
|
||||
include_once('../../includes/config.php');
|
||||
include_once('../../includes/functions.php');
|
||||
|
||||
|
||||
if(isset($_POST['interface']) && isset($_POST['csrf_token']) && CSRFValidate()) {
|
||||
$int = $_POST['interface'];
|
||||
if(!file_exists(RASPI_CONFIG_NETWORKING.'/DHCP-'.$int)) {
|
||||
touch(RASPI_CONFIG_NETWORKING.'/DHCP-'.$int.'.ini');
|
||||
}
|
||||
|
||||
if(!file_exists(RASPI_CONFIG_NETWORKING.'/STATIC-'.$int)) {
|
||||
touch(RASPI_CONFIG_NETWORKING.'/STATIC-'.$int.'.ini');
|
||||
}
|
||||
|
||||
$intDHCPConfig = parse_ini_file(RASPI_CONFIG_NETWORKING.'/DHCP-'.$int.'.ini');
|
||||
$intStaticConfig = parse_ini_file(RASPI_CONFIG_NETWORKING.'/STATIC-'.$int.'.ini');
|
||||
$jsonData = ['return'=>1,'output'=>['DHCPConfig'=>$intDHCPConfig,'StaticConfig'=>$intStaticConfig]];
|
||||
echo json_encode($jsonData);
|
||||
|
||||
} else {
|
||||
$jsonData = ['return'=>2,'output'=>['Error getting data']];
|
||||
echo json_encode($jsonData);
|
||||
}
|
||||
|
||||
?>
|
15
ajax/networking/get_ip_summary.php
Normal file
15
ajax/networking/get_ip_summary.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
session_start();
|
||||
include_once('../../includes/functions.php');
|
||||
|
||||
if(isset($_POST['interface']) && isset($_POST['csrf_token']) && CSRFValidate()) {
|
||||
$int = preg_replace('/[^a-z0-9]/','',$_POST['interface']);
|
||||
exec('ip a s '.$int,$intOutput,$intResult);
|
||||
$jsonData = ['return'=>$intResult,'output'=>$intOutput];
|
||||
echo json_encode($jsonData);
|
||||
} else {
|
||||
$jsonData = ['return'=>2,'output'=>['Error getting data']];
|
||||
echo json_encode($jsonData);
|
||||
}
|
||||
|
||||
?>
|
75
ajax/networking/save_int_config.php
Normal file
75
ajax/networking/save_int_config.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
function mask2cidr($mask){
|
||||
$long = ip2long($mask);
|
||||
$base = ip2long('255.255.255.255');
|
||||
return 32-log(($long ^ $base)+1,2);
|
||||
}
|
||||
|
||||
function write_php_ini($array, $file) {
|
||||
$res = array();
|
||||
foreach($array as $key => $val) {
|
||||
if(is_array($val)) {
|
||||
$res[] = "[$key]";
|
||||
foreach($val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"');
|
||||
}
|
||||
else $res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');
|
||||
}
|
||||
if(safefilerewrite($file, implode("\r\n", $res))) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function safefilerewrite($fileName, $dataToSave) {
|
||||
if ($fp = fopen($fileName, 'w')) {
|
||||
$startTime = microtime(TRUE);
|
||||
do {
|
||||
$canWrite = flock($fp, LOCK_EX);
|
||||
// If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
|
||||
if(!$canWrite) usleep(round(rand(0, 100)*1000));
|
||||
} while ((!$canWrite)and((microtime(TRUE)-$startTime) < 5));
|
||||
|
||||
//file was locked so now we can store information
|
||||
if ($canWrite) {
|
||||
fwrite($fp, $dataToSave);
|
||||
flock($fp, LOCK_UN);
|
||||
}
|
||||
fclose($fp);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
session_start();
|
||||
include_once('../../includes/config.php');
|
||||
include_once('../../includes/functions.php');
|
||||
var_dump($_POST);
|
||||
if(isset($_POST['interface']) && isset($_POST['csrf_token']) && CSRFValidate()) {
|
||||
$int = $_POST['interface'];
|
||||
$cfg = [];
|
||||
if($_POST[$int.'-static'] == 'true') {
|
||||
$file = "STATIC-".$int.".ini";
|
||||
$ip = $_POST[$int.'-ipaddress'];
|
||||
$netmask = mask2cidr($_POST[$int.'-netmask']);
|
||||
$dns1 = $_POST[$int.'-dnssvr'];
|
||||
$dns2 = $_POST[$int.'-dnssvralt'];
|
||||
|
||||
$cfg['static'] = $_POST[$int.'-static'];
|
||||
$cfg['interface'] = $int;
|
||||
$cfg['routers'] = $_POST[$int.'-gateway'];
|
||||
$cfg['ip_address'] = $ip."/".$netmask;
|
||||
$cfg['domain_name_server'] = $dns1." ".$dns2;
|
||||
|
||||
if(write_php_ini($cfg,RASPI_CONFIG_NETWORKING.'/'.$file)) {
|
||||
$jsonData = ['return'=>0,'output'=>['Successfully Updated Network Configuration']];
|
||||
} else {
|
||||
$jsonData = ['return'=>1,'output'=>['Error saving network configuration']];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$jsonData = ['return'=>2,'output'=>['Error saving network configuration']];
|
||||
}
|
||||
echo json_encode($jsonData);
|
||||
?>
|
Reference in New Issue
Block a user