Init Repo

This commit is contained in:
Gregor Schulte
2021-06-15 11:08:34 +02:00
parent d0b72a118d
commit b303dd00d6
330 changed files with 93268 additions and 0 deletions

View File

@@ -0,0 +1,223 @@
<?php
if (!defined('PICONTROL')) exit();
class NetworkInterface
{
private $tpl;
private $file;
private $interfaces = array();
private $comments = array();
private $others = array();
public function __construct($tpl)
{
$this->tpl = $tpl;
$this->readInterfaces();
}
public function getInterfaces($removeEmpty = true)
{
$return = $this->interfaces;
if ($removeEmpty === true)
{
foreach ($return as $interface => $value)
{
if (!isset($value['protocol']) || !isset($value['method']))
unset($return[$interface]);
}
}
return $return;
}
public function getInterface($interface, $removeEmpty = true)
{
if (!isset($this->interfaces[$interface]))
return false;
$return = $this->interfaces[$interface];
if ($removeEmpty === true)
{
if (!isset($return['protocol']) || !isset($return['method']))
return false;
}
return $return;
}
public function addInterface($interface, $interfaceSettings, $writeToFile = true)
{
if (empty($interface) || empty($interfaceSettings))
return 0;
if (isset($this->interfaces[$interface], $this->interfaces[$interface]['protocol'], $this->interfaces[$interface]['method']))
return 1;
$this->interfaces[$interface] = $interfaceSettings;
if ($writeToFile === true)
return $this->writeNetworkInterface();
else
return true;
}
public function editInterface($interface, $interfaceSettings, $newInterfaceName = NULL, $writeToFile = true)
{
if (empty($interface) || empty($interfaceSettings))
return 0;
$this->interfaces[$interface] = array_merge($this->interfaces[$interface], $interfaceSettings);
$this->interfaces[$interface] = array_filter($this->interfaces[$interface]);
if ($newInterfaceName != NULL)
{
$this->interfaces[$newInterfaceName] = $this->interfaces[$interface];
unset($this->interfaces[$interface]);
}
if ($writeToFile === true)
return $this->writeNetworkInterface();
else
return true;
}
public function deleteInterface($interface, $writeToFile = true)
{
if (empty($interface))
return false;
unset($this->interfaces[$interface]);
if ($writeToFile === true)
return $this->writeNetworkInterface();
else
return true;
}
public function existsInterface($interface, $removeEmpty = true)
{
if (!isset($this->interfaces[$interface]))
return false;
if ($removeEmpty === true)
{
if (!isset($this->interfaces[$interface]['protocol']) || !isset($this->interfaces[$interface]['method']))
return false;
}
return true;
}
public function getInterfaceHash()
{
return md5(serialize($this->file));
}
private function readInterfaces()
{
$this->file = shell_exec('cat /etc/network/interfaces');
$lines = explode(PHP_EOL, $this->file);
$isInterface = NULL;
foreach ($lines as $index => $line)
{
if (isset($line[0]) && trim($line)[0] != '#')
{
if (substr($line, 0, 4) == 'auto')
{
$isInterface = NULL;
$this->interfaces[trim(substr($line, 5))]['auto'] = true;
continue;
}
if (substr($line, 0, 10) == 'allow-auto')
{
$isInterface = NULL;
$this->interfaces[trim(substr($line, 10))]['allow-auto'] = true;
continue;
}
if (substr($line, 0, 13) == 'allow-hotplug')
{
$isInterface = NULL;
$this->interfaces[trim(substr($line, 13))]['allow-hotplug'] = true;
continue;
}
if (substr($line, 0, 5) == 'iface')
{
$interface = explode(' ', $line);
$isInterface = trim($interface[1]);
$this->interfaces[$isInterface]['protocol'] = $interface[2];
$this->interfaces[$isInterface]['method'] = $interface[3];
continue;
}
if ($isInterface != NULL)
{
preg_match('/^[\s]*([\w\d\-]*)[\s]+(.*)$/im', $line, $match);
$this->interfaces[$isInterface]['iface'][$match[1]] = $match[2];
}
}
elseif (isset($line[0]) && trim($line)[0] == '#')
$this->comments[] = $line;
elseif (trim($line) != '')
$this->others[] = $line;
}
return true;
}
private function writeNetworkInterface()
{
$fileLines = '';
foreach ($this->interfaces as $interface => $line)
{
if (is_array($line))
{
if (isset($line['auto']) && $line['auto'] == true)
$fileLines .= 'auto '.$interface.PHP_EOL;
if (isset($line['allow-auto']) && $line['allow-auto'] == true)
$fileLines .= 'allow-auto '.$interface.PHP_EOL;
if (isset($line['allow-hotplug']) && $line['allow-hotplug'] == true)
$fileLines .= 'allow-hotplug '.$interface.PHP_EOL;
if (isset($line['protocol'], $line['method']) && $line['protocol'] != '' && $line['method'] != '')
{
$fileLines .= 'iface '.$interface.' '.$line['protocol'].' '.$line['method'].PHP_EOL;
if (isset($line['iface']))
{
foreach ($line['iface'] as $key => $value)
{
if ($value != NULL)
$fileLines .= ' '.$key.' '.$value.PHP_EOL;
}
}
}
$fileLines .= PHP_EOL;
}
}
foreach ($this->others as $other)
$fileLines .= trim($other).PHP_EOL;
foreach ($this->comments as $comment)
$fileLines .= trim($comment).PHP_EOL;
list ($status, $error, $exitStatus) = $this->tpl->executeSSH('echo -e '.escapeshellarg($fileLines).' | sudo /bin/su -c "cat > /etc/network/interfaces"');
if ($status == '')
return $this->readInterfaces();
return false;
}
}
?>

View File

@@ -0,0 +1,170 @@
<?php
if (!defined('PICONTROL')) exit();
function writeNetworkWPASupplicant($lines)
{
global $tpl;
if (empty($lines))
return false;
$fileLines = '';
foreach ($lines as $line)
{
if (is_array($line))
{
$fileLines .= 'network={'.PHP_EOL;
foreach ($line as $key => $value)
$fileLines .= ' '.$key.'='.$value.PHP_EOL;
$fileLines .= '}'.PHP_EOL.PHP_EOL;
}
else
$fileLines .= $line.PHP_EOL;
}
list ($status, $error, $exitStatus) = $tpl->executeSSH('echo -e '.escapeshellarg($fileLines).' | sudo /bin/su -c "cat > /etc/wpa_supplicant/wpa_supplicant.conf"');
if ($status == '')
return true;
return $error;
}
function getAllNetworkWPASupplicant()
{
global $tpl;
list ($status, $error, $exitStatus) = $tpl->executeSSH('sudo cat /etc/wpa_supplicant/wpa_supplicant.conf');
$splits = explode(PHP_EOL, $status);
$lines = array();
$isNetwork = false;
$network = array();
foreach ($splits as $index => $split)
{
if (count($split) == 0 || (isset($split[0]) && trim($split)[0] == '#'))
{
$lines[] = $split;
continue;
}
if (substr($split, 0, 8) == 'network=' && $isNetwork == false)
{
$network = array();
$isNetwork = true;
continue;
}
if (isset($split[0]) && $split[0] == '}' && $isNetwork == true)
{
$lines[] = $network;
$isNetwork = false;
continue;
}
if ($isNetwork == true)
{
preg_match_all('/^(.*)=(.*)$/', trim($split), $matches);
if (count($matches) == 3 && isset($matches[1][0]) && $matches[1][0] != '')
$network[$matches[1][0]] = $matches[2][0];
continue;
}
if (trim($split) != '')
$lines[] = $split;
}
return $lines;
}
function addNetworkWPASupplicant($network)
{
global $tpl;
$lines = getAllNetworkWPASupplicant();
$isAdded = false;
foreach ($lines as $index => $line)
{
if (!is_array($line))
continue;
if (!isset($line['id_str']) && $line['ssid'] == $network['ssid'])
{
$lines[$index] = $network;
$isAdded = true;
break;
}
}
if ($isAdded == false)
$lines[] = $network;
return writeNetworkWPASupplicant($lines);
}
function editHostname($hostname)
{
global $tpl;
$hosts = shell_exec('cat /etc/hosts');
if (empty($hosts))
return 5;
$new = preg_replace('/^(127\.0\.1\.1[\s]+)(.+)$/im', '$1'.$hostname, $hosts);
list ($status, $error, $exitStatus) = $tpl->executeSSH('echo -e '.escapeshellarg($new).' | sudo /bin/su -c "cat > /etc/hosts"');
if ($status == '')
list ($status2, $error2, $exitStatus2) = $tpl->executeSSH('echo -e '.escapeshellarg($hostname).' | sudo /bin/su -c "cat > /etc/hostname"');
else
return $status;
if ($status2 == '')
return true;
return $status2;
}
function formatInterfaceProtocol($string)
{
switch ($string)
{
case 'inet':
return 'IPv4';
break;
case 'inet6':
return 'IPv6';
break;
case 'ipx':
return 'IPX/SPX';
break;
}
}
function formatInterfaceMethod($string)
{
switch ($string)
{
case 'loopback':
return 'Loopback';
break;
case 'dhcp':
return 'Dynamisch';
break;
case 'static':
return 'Statisch';
break;
case 'manual':
return 'Manuell';
break;
}
}
?>