Commit ad276991 authored by Ad Schellevis's avatar Ad Schellevis

code styling fixes (PSR) for captive new captive portal code

parent 6e46796a
......@@ -172,7 +172,7 @@ function portal_allow($clientip,$clientmac,$username,$password = null, $attribut
}
$sessionid = $cpc->portal_allow($cpzone,$clientip,$clientmac,$username,$password,$bw_up,$bw_down,$radiusctx,$session_timeout,$idle_timeout,$session_terminate_time,$interim_interval);
$sessionid = $cpc->portalAllow($cpzone,$clientip,$clientmac,$username,$password,$bw_up,$bw_down,$radiusctx,$session_timeout,$idle_timeout,$session_terminate_time,$interim_interval);
if (isset($config['captiveportal'][$cpzone]['radacct_enable']) && !empty($radiusservers[$radiusctx])) {
$acct_val = RADIUS_ACCOUNTING_START($pipeno, $username, $sessionid, $radiusservers[$radiusctx], $clientip, $clientmac);
......@@ -699,11 +699,11 @@ function captiveportal_prune_old() {
$cpcfg = $config['captiveportal'][$cpzone];
if ( !isset($cpcfg['radacct_enable'])) {
// cleanup session (default)
$cpc->portal_cleanup_sessions($cpzone);
$cpc->portalCleanupSessions($cpzone);
}else{
// cleanup sessions if radius accounting is enable
// TODO: this code needs a rewrite, probably the easiest thing todo is update the zone administration and run
// the normal cleanup (portal_cleanup_sessions) to detach both processes
// the normal cleanup (portalCleanupSessions) to detach both processes
//
$vcpcfg = $config['voucher'][$cpzone];
......
......@@ -41,7 +41,7 @@ class ARP
* pointer to shell object
* @var \Core\Shell
*/
private $shell ;
private $shell;
/**
* construct new ARP table handlers
......@@ -59,11 +59,9 @@ class ARP
public function setStatic($ipaddress, $mac)
{
// validate input, only set static entries for valid addresses
if (preg_match('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', trim($mac)))
{
if ( filter_var($ipaddress , FILTER_VALIDATE_IP, FILTER_FLAG_IPV4|FILTER_FLAG_IPV6) )
{
$this->shell->exec("/usr/sbin/arp -s ".trim($ipaddress)." ".trim($mac));
if (preg_match('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', trim($mac))) {
if (filter_var($ipaddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
$this->shell->exec("/usr/sbin/arp -s " . trim($ipaddress) . " " . trim($mac));
}
}
}
......@@ -72,41 +70,46 @@ class ARP
* drop static arp entry
* @param $ipaddress hosts ipaddress
*/
function dropStatic($ipaddress){
public function dropStatic($ipaddress)
{
// validate input, drop arp entry
if ( filter_var($ipaddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4|FILTER_FLAG_IPV6) ){
$this->shell->exec("/usr/sbin/arp -d ".trim($ipaddress) );
if (filter_var($ipaddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
$this->shell->exec("/usr/sbin/arp -d " . trim($ipaddress));
}
}
/**
* Return arp table hashed by mac address
*/
function getMACs(){
public function getMACs()
{
$result = array();
$shell_output = array();
// execute arp shell command and collect (only valid) info into named array
if ($this->shell->exec("arp -an",false,false,$shell_output) == 0 ){
foreach($shell_output as $line){
$line_parts = explode(" ",$line) ;
if ( sizeof($line_parts) >= 4 ) {
$ipaddress = substr($line_parts[1],1,strlen($line_parts[1])-2 ) ;
if ($this->shell->exec("arp -an", false, false, $shell_output) == 0) {
foreach ($shell_output as $line) {
$line_parts = explode(" ", $line);
if (sizeof($line_parts) >= 4) {
$ipaddress = substr($line_parts[1], 1, strlen($line_parts[1]) - 2);
// reformat mac addresses, sometimes arp return segments without trailing zero's
$mac_raw = strtolower($line_parts[3]);
$mac = "";
foreach(explode(":",$mac_raw) as $segment ){
if ( $mac != "") $mac .= ":";
if (strlen($segment) == 1) $mac .= "0".$segment;
else $mac .= $segment ;
foreach (explode(":", $mac_raw) as $segment) {
if ($mac != "") {
$mac .= ":";
}
if (strlen($segment) == 1) {
$mac .= "0" . $segment;
} else {
$mac .= $segment;
}
}
if (preg_match('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', trim($mac))){
$result[$mac]= array('ip'=>$ipaddress);
if (preg_match('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', trim($mac))) {
$result[$mac] = array('ip' => $ipaddress);
}
}
}
}
return $result;
}
}
......@@ -34,17 +34,14 @@
namespace OPNsense\Core;
/**
* Class ConfigException
* @package Core
*/
class ConfigException extends \Exception { }
use \Phalcon\DI\FactoryDefault;
/**
* Class Config
* @package Core
*/
class Config extends Singleton {
class Config extends Singleton
{
/**
* config file location ( path + name )
......@@ -68,32 +65,52 @@ class Config extends Singleton {
* status field: valid config loaded
* @var bool
*/
private $isValid = False;
private $isValid = false;
/**
* Load config file
* print config xml in dot notation
* @param DOMNode $node
* @param string $nodename
* @throws ConfigException
*/
private function load(){
// exception handling
if ( !file_exists($this->config_file) ) throw new ConfigException('file not found') ;
$xml = file_get_contents($this->config_file);
if (trim($xml) == '') {
throw new ConfigException('empty file') ;
public function dump($node = null, $nodename = "")
{
$this->checkvalid();
// root node
if ($node == null) {
$node = $this->configxml;
}
$this->configxml = new \DOMDocument;
$this->configxml->loadXML($xml);
$this->simplexml = simplexml_import_dom($this->configxml);
$this->isValid = true;
$subNodes = $node->childNodes ;
foreach ($subNodes as $subNode) {
if ($subNode->nodeType == XML_TEXT_NODE &&(strlen(trim($subNode->wholeText))>=1)) {
print($nodename.".". $node->tagName." " .$subNode->nodeValue ."\n");
}
if ($subNode->hasChildNodes()) {
if ($nodename != "") {
$tmp = $nodename.".".$node->tagName;
} elseif ($node != $this->configxml) {
$tmp = $node->tagName;
} else {
$tmp = "";
}
$this->dump($subNode, $tmp);
}
}
}
/**
* @throws ConfigException
*/
private function checkvalid(){
if ( !$this->isValid ) throw new ConfigException('no valid config loaded') ;
private function checkvalid()
{
if (!$this->isValid) {
throw new ConfigException('no valid config loaded') ;
}
}
/*
......@@ -102,41 +119,23 @@ class Config extends Singleton {
* @param string $nodename
* @throws ConfigException
*/
public function dump($node=null,$nodename=""){
$this->checkvalid();
// root node
if ($node == null ) $node = $this->configxml;
$subNodes = $node->childNodes ;
foreach($subNodes as $subNode){
if ( $subNode->nodeType == XML_TEXT_NODE &&(strlen(trim($subNode->wholeText))>=1)) {
print($nodename.".". $node->tagName." " .$subNode->nodeValue ."\n");
}
if ( $subNode->hasChildNodes() ){
if ( $nodename != "" ) $tmp = $nodename.".".$node->tagName;
elseif ($node != $this->configxml) $tmp = $node->tagName;
else $tmp = "";
$this->dump($subNode,$tmp);
}
}
public function xpath($query)
{
$this->checkvalid();
$xpath = new \DOMXPath($this->configxml);
return $xpath->query($query);
}
/*
* init new config object, try to load current configuration
* (executed via Singleton)
*/
protected function init() {
$this->config_file = \Phalcon\DI\FactoryDefault::getDefault()->get('config')->globals->config_path . "config.xml";
try {
$this->load();
} catch (\Exception $e){
$this->configxml = null ;
}
public function object()
{
$this->checkvalid();
return $this->simplexml;
}
/*
......@@ -145,10 +144,16 @@ class Config extends Singleton {
* @return \DOMNodeList
* @throws ConfigException
*/
function xpath($query){
$this->checkvalid();
$xpath = new \DOMXPath($this->configxml);
return $xpath->query($query);
protected function init()
{
$this->config_file = FactoryDefault::getDefault()->get('config')->globals->config_path . "config.xml";
try {
$this->load();
} catch (\Exception $e) {
$this->configxml = null ;
}
}
/*
......@@ -156,9 +161,26 @@ class Config extends Singleton {
* @return SimpleXML
* @throws ConfigException
*/
function object(){
$this->checkvalid();
return $this->simplexml;
}
/**
* Load config file
* @throws ConfigException
*/
private function load()
{
// exception handling
if (!file_exists($this->config_file)) {
throw new ConfigException('file not found') ;
}
$xml = file_get_contents($this->config_file);
if (trim($xml) == '') {
throw new ConfigException('empty file') ;
}
$this->configxml = new \DOMDocument;
$this->configxml->loadXML($xml);
$this->simplexml = simplexml_import_dom($this->configxml);
$this->isValid = true;
}
}
<?php
/**
* Created by PhpStorm.
* User: ad
* Date: 04-01-15
* Time: 20:06
*/
namespace app\models\OPNsense\Core;
/**
* Class ConfigException
* @package Core
*/
class ConfigException extends \Exception
{
}
......@@ -33,6 +33,7 @@
namespace OPNsense\Core;
use \Phalcon\DI\FactoryDefault;
class Shell
{
......@@ -52,11 +53,31 @@ class Shell
/**
* new shell object
*/
function __construct()
public function __construct()
{
// init, set simulation mode / debug autoput
$this->simulate = \Phalcon\DI\FactoryDefault::getDefault()->get('config')->globals->simulate_mode;
$this->debug = \Phalcon\DI\FactoryDefault::getDefault()->get('config')->globals->debug;
$this->simulate = FactoryDefault::getDefault()->get('config')->globals->simulate_mode;
$this->debug = FactoryDefault::getDefault()->get('config')->globals->debug;
}
/**
* execute command or list of commands
*
* @param string/Array() $command command to execute
* @param bool $mute
* @param bool $clearsigmask
* @param Array() &$output
*/
public function exec($command, $mute = false, $clearsigmask = false, &$output = null)
{
if (is_array($command)) {
foreach ($command as $comm) {
$this->execSingle($comm, $mute, $clearsigmask, $output);
}
} else {
$this->execSingle($command, $mute, $clearsigmask, $output);
}
}
......@@ -65,8 +86,11 @@ class Shell
* @param string $command command to execute
* @param bool $mute
* @param bool $clearsigmask
* @param Array() &$output
* @return int
*/
private function _exec($command, $mute = false, $clearsigmask = false,&$output=null){
private function execSingle($command, $mute = false, $clearsigmask = false, &$output = null)
{
$oarr = array();
$retval = 0;
......@@ -82,10 +106,11 @@ class Shell
pcntl_sigprocmask(SIG_SETMASK, array(), $oldset);
}
$garbage = exec("$command 2>&1", $output, $retval);
exec("$command 2>&1", $output, $retval);
if (($retval <> 0) && ($mute === false)) {
//log_error(sprintf(gettext("The command '%1\$s' returned exit code '%2\$d', the output was '%3\$s' "), implode(" ", $output);
//log_error(sprintf(gettext("The command '%1\$s'
// returned exit code '%2\$d', the output was '%3\$s' "), implode(" ", $output);
// TODO: log
unset($output);
}
......@@ -101,26 +126,4 @@ class Shell
return null;
}
/**
* execute command or list of commands
*
* @param string/Array() $command command to execute
* @param bool $mute
* @param bool $clearsigmask
* @param Array() &$output
*/
function exec($command, $mute = false, $clearsigmask = false,&$output=null)
{
if (is_array($command)){
foreach($command as $comm ){
$this->_exec($comm,$mute, $clearsigmask ,$output);
}
}
else{
$this->_exec($command,$mute, $clearsigmask ,$output);
}
}
}
......@@ -33,11 +33,6 @@
namespace OPNsense\Core;
/**
* Class Singleton
* @package Core
*/
/**
* Class Singleton
* @package Core
......@@ -65,7 +60,7 @@ abstract class Singleton
*/
final private function __clone()
{
throw new \Exception("An instance of ".get_called_class()." cannot be cloned.");
throw new \Exception("An instance of " . get_called_class() . " cannot be cloned.");
}
/**
......@@ -76,7 +71,7 @@ abstract class Singleton
{
$className = get_called_class();
if(isset(self::$instances[$className]) == false) {
if (isset(self::$instances[$className]) == false) {
self::$instances[$className] = new static();
}
return self::$instances[$className];
......@@ -88,6 +83,7 @@ abstract class Singleton
* so an extended class could simply
* specify its own init
*/
protected function init(){}
protected function init()
{
}
}
......@@ -79,7 +79,7 @@ if (!empty($cpzone)) {
$cpdb = $cpdb_handle->listClients(array(),"and",array($order) ) ;
if ($_GET['showact']) {
$accounting_info = $cpclient_handle->list_accounting();
$accounting_info = $cpclient_handle->listAccounting();
}
else {
$accounting_info = array() ;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment