Processed with phpcs for PSR-2 coding standard

This commit is contained in:
billz 2019-04-10 08:37:35 +00:00
parent 89852cc633
commit c0570b616e
17 changed files with 1461 additions and 1416 deletions

3
includes/about.php Normal file → Executable file
View File

@ -2,7 +2,8 @@
include_once('includes/status_messages.php'); include_once('includes/status_messages.php');
function DisplayAbout() { function DisplayAbout()
{
/** /**
* *
* Displays info about the RaspAP project * Displays info about the RaspAP project

View File

@ -2,7 +2,8 @@
include_once('includes/status_messages.php'); include_once('includes/status_messages.php');
function DisplayAuthConfig($username, $password){ function DisplayAuthConfig($username, $password)
{
$status = new StatusMessages(); $status = new StatusMessages();
if (isset($_POST['UpdateAdminPassword'])) { if (isset($_POST['UpdateAdminPassword'])) {
if (CSRFValidate()) { if (CSRFValidate()) {

View File

@ -15,4 +15,3 @@ if (!$validated) {
exit('Not authorized'.PHP_EOL); exit('Not authorized'.PHP_EOL);
} }

View File

@ -33,4 +33,3 @@ define('RASPI_VNSTAT_ENABLED', true );
// Locale settings // Locale settings
define('LOCALE_ROOT', 'locale'); define('LOCALE_ROOT', 'locale');
define('LOCALE_DOMAIN', 'messages'); define('LOCALE_DOMAIN', 'messages');

View File

@ -4,7 +4,8 @@
* *
* *
*/ */
function DisplayWPAConfig(){ function DisplayWPAConfig()
{
$status = new StatusMessages(); $status = new StatusMessages();
$networks = array(); $networks = array();
@ -52,8 +53,7 @@ function DisplayWPAConfig(){
if (isset($_POST['connect'])) { if (isset($_POST['connect'])) {
$result = 0; $result = 0;
exec('sudo wpa_cli -i ' . RASPI_WPA_CTRL_INTERFACE . ' select_network '.strval($_POST['connect'])); exec('sudo wpa_cli -i ' . RASPI_WPA_CTRL_INTERFACE . ' select_network '.strval($_POST['connect']));
} } elseif (isset($_POST['client_settings']) && CSRFValidate()) {
else if ( isset($_POST['client_settings']) && CSRFValidate() ) {
$tmp_networks = $networks; $tmp_networks = $networks;
if ($wpa_file = fopen('/tmp/wifidata', 'w')) { if ($wpa_file = fopen('/tmp/wifidata', 'w')) {
fwrite($wpa_file, 'ctrl_interface=DIR=' . RASPI_WPA_CTRL_INTERFACE . ' GROUP=netdev' . PHP_EOL); fwrite($wpa_file, 'ctrl_interface=DIR=' . RASPI_WPA_CTRL_INTERFACE . ' GROUP=netdev' . PHP_EOL);
@ -231,9 +231,13 @@ function DisplayWPAConfig(){
<div class="col-xs-6 col-md-6"> <div class="col-xs-6 col-md-6">
<?php echo htmlspecialchars($network['RSSI'], ENT_QUOTES); <?php echo htmlspecialchars($network['RSSI'], ENT_QUOTES);
echo "dB ("; echo "dB (";
if($network['RSSI'] >= -50) { echo 100; } if ($network['RSSI'] >= -50) {
else if($network['RSSI'] <= -100) { echo 0;} echo 100;
else {echo 2*($network['RSSI'] + 100); } } elseif ($network['RSSI'] <= -100) {
echo 0;
} else {
echo 2*($network['RSSI'] + 100);
}
echo "%)"; echo "%)";
?> ?>
</div> </div>

View File

@ -3,7 +3,8 @@
/** /**
* Show dashboard page. * Show dashboard page.
*/ */
function DisplayDashboard(){ function DisplayDashboard()
{
$status = new StatusMessages(); $status = new StatusMessages();
// Need this check interface name for proper shell execution. // Need this check interface name for proper shell execution.

View File

@ -7,7 +7,8 @@ include_once( 'includes/status_messages.php' );
* Manage DHCP configuration * Manage DHCP configuration
* *
*/ */
function DisplayDHCPConfig() { function DisplayDHCPConfig()
{
$status = new StatusMessages(); $status = new StatusMessages();
if (isset($_POST['savedhcpdsettings'])) { if (isset($_POST['savedhcpdsettings'])) {

View File

@ -1,7 +1,8 @@
<?php <?php
/* Functions for Networking */ /* Functions for Networking */
function mask2cidr($mask){ function mask2cidr($mask)
{
$long = ip2long($mask); $long = ip2long($mask);
$base = ip2long('255.255.255.255'); $base = ip2long('255.255.255.255');
return 32-log(($long ^ $base)+1, 2); return 32-log(($long ^ $base)+1, 2);
@ -9,14 +10,18 @@ function mask2cidr($mask){
/* Functions to write ini files */ /* Functions to write ini files */
function write_php_ini($array, $file) { function write_php_ini($array, $file)
{
$res = array(); $res = array();
foreach ($array as $key => $val) { foreach ($array as $key => $val) {
if (is_array($val)) { if (is_array($val)) {
$res[] = "[$key]"; $res[] = "[$key]";
foreach($val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"'); foreach ($val as $skey => $sval) {
$res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"');
}
} else {
$res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');
} }
else $res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');
} }
if (safefilerewrite($file, implode("\r\n", $res))) { if (safefilerewrite($file, implode("\r\n", $res))) {
return true; return true;
@ -25,14 +30,17 @@ function write_php_ini($array, $file) {
} }
} }
function safefilerewrite($fileName, $dataToSave) { function safefilerewrite($fileName, $dataToSave)
{
if ($fp = fopen($fileName, 'w')) { if ($fp = fopen($fileName, 'w')) {
$startTime = microtime(TRUE); $startTime = microtime(true);
do { do {
$canWrite = flock($fp, LOCK_EX); $canWrite = flock($fp, LOCK_EX);
// If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load // If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
if(!$canWrite) usleep(round(rand(0, 100)*1000)); if (!$canWrite) {
} while ((!$canWrite)and((microtime(TRUE)-$startTime) < 5)); usleep(round(rand(0, 100)*1000));
}
} while ((!$canWrite)and((microtime(true)-$startTime) < 5));
//file was locked so now we can store information //file was locked so now we can store information
if ($canWrite) { if ($canWrite) {
@ -46,16 +54,16 @@ function safefilerewrite($fileName, $dataToSave) {
} }
} }
/** /**
* *
* Add CSRF Token to form * Add CSRF Token to form
* *
*/ */
function CSRFToken() { function CSRFToken()
{
?> ?>
<input id="csrf_token" type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($_SESSION['csrf_token'], ENT_QUOTES);; ?>" /> <input id="csrf_token" type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($_SESSION['csrf_token'], ENT_QUOTES);
; ?>" />
<?php <?php
} }
@ -64,7 +72,8 @@ function CSRFToken() {
* Validate CSRF Token * Validate CSRF Token
* *
*/ */
function CSRFValidate() { function CSRFValidate()
{
if (hash_equals($_POST['csrf_token'], $_SESSION['csrf_token'])) { if (hash_equals($_POST['csrf_token'], $_SESSION['csrf_token'])) {
return true; return true;
} else { } else {
@ -76,7 +85,8 @@ function CSRFValidate() {
/** /**
* Test whether array is associative * Test whether array is associative
*/ */
function isAssoc($arr) { function isAssoc($arr)
{
return array_keys($arr) !== range(0, count($arr) - 1); return array_keys($arr) !== range(0, count($arr) - 1);
} }
@ -89,7 +99,8 @@ function isAssoc($arr) {
* If $options is an associative array this should be the key * If $options is an associative array this should be the key
* *
*/ */
function SelectorOptions($name, $options, $selected = null, $id = null) { function SelectorOptions($name, $options, $selected = null, $id = null)
{
echo '<select class="form-control" name="'.htmlspecialchars($name, ENT_QUOTES).'"'; echo '<select class="form-control" name="'.htmlspecialchars($name, ENT_QUOTES).'"';
if (isset($id)) { if (isset($id)) {
echo ' id="' . htmlspecialchars($id, ENT_QUOTES) .'"'; echo ' id="' . htmlspecialchars($id, ENT_QUOTES) .'"';
@ -118,7 +129,8 @@ function SelectorOptions($name, $options, $selected = null, $id = null) {
* @param string $separator * @param string $separator
* @return $string * @return $string
*/ */
function GetDistString( $input,$string,$offset,$separator ) { function GetDistString($input, $string, $offset, $separator)
{
$string = substr($input, strpos($input, $string)+$offset, strpos(substr($input, strpos($input, $string)+$offset), $separator)); $string = substr($input, strpos($input, $string)+$offset, strpos(substr($input, strpos($input, $string)+$offset), $separator));
return $string; return $string;
} }
@ -128,7 +140,8 @@ function GetDistString( $input,$string,$offset,$separator ) {
* @param array $arrConfig * @param array $arrConfig
* @return $config * @return $config
*/ */
function ParseConfig( $arrConfig ) { function ParseConfig($arrConfig)
{
$config = array(); $config = array();
foreach ($arrConfig as $line) { foreach ($arrConfig as $line) {
$line = trim($line); $line = trim($line);
@ -145,7 +158,8 @@ function ParseConfig( $arrConfig ) {
* @param string $freq * @param string $freq
* @return $channel * @return $channel
*/ */
function ConvertToChannel( $freq ) { function ConvertToChannel($freq)
{
if ($freq >= 2412 && $freq <= 2484) { if ($freq >= 2412 && $freq <= 2484) {
$channel = ($freq - 2407)/5; $channel = ($freq - 2407)/5;
} elseif ($freq >= 4915 && $freq <= 4980) { } elseif ($freq >= 4915 && $freq <= 4980) {
@ -167,7 +181,8 @@ function ConvertToChannel( $freq ) {
* @param string $security * @param string $security
* @return string * @return string
*/ */
function ConvertToSecurity( $security ) { function ConvertToSecurity($security)
{
$options = array(); $options = array();
preg_match_all('/\[([^\]]+)\]/s', $security, $matches); preg_match_all('/\[([^\]]+)\]/s', $security, $matches);
foreach ($matches[1] as $match) { foreach ($matches[1] as $match) {
@ -196,7 +211,8 @@ function ConvertToSecurity( $security ) {
* *
* *
*/ */
function DisplayOpenVPNConfig() { function DisplayOpenVPNConfig()
{
exec('cat '. RASPI_OPENVPN_CLIENT_CONFIG, $returnClient); exec('cat '. RASPI_OPENVPN_CLIENT_CONFIG, $returnClient);
exec('cat '. RASPI_OPENVPN_SERVER_CONFIG, $returnServer); exec('cat '. RASPI_OPENVPN_SERVER_CONFIG, $returnServer);
@ -324,7 +340,8 @@ function DisplayOpenVPNConfig() {
* *
* *
*/ */
function DisplayTorProxyConfig(){ function DisplayTorProxyConfig()
{
exec('cat '. RASPI_TORPROXY_CONFIG, $return); exec('cat '. RASPI_TORPROXY_CONFIG, $return);
exec('pidof tor | wc -l', $torproxystatus); exec('pidof tor | wc -l', $torproxystatus);
@ -466,7 +483,8 @@ function DisplayTorProxyConfig(){
* *
* *
*/ */
function SaveTORAndVPNConfig(){ function SaveTORAndVPNConfig()
{
if (isset($_POST['SaveOpenVPNSettings'])) { if (isset($_POST['SaveOpenVPNSettings'])) {
// TODO // TODO
} elseif (isset($_POST['SaveTORProxySettings'])) { } elseif (isset($_POST['SaveTORProxySettings'])) {

View File

@ -9,9 +9,7 @@ include_once( 'includes/status_messages.php' );
function DisplayHostAPDConfig() function DisplayHostAPDConfig()
{ {
$status = new StatusMessages(); $status = new StatusMessages();
$arrHostapdConf = parse_ini_file('/etc/raspap/hostapd.ini'); $arrHostapdConf = parse_ini_file('/etc/raspap/hostapd.ini');
$arrConfig = array(); $arrConfig = array();
$arr80211Standard = array('a','b','g','n'); $arr80211Standard = array('a','b','g','n');
$arrSecurity = array(1 => 'WPA', 2 => 'WPA2', 3 => 'WPA+WPA2', 'none' => _("None")); $arrSecurity = array(1 => 'WPA', 2 => 'WPA2', 3 => 'WPA+WPA2', 'none' => _("None"));
@ -513,7 +511,8 @@ for (var i = 0; i < ops.length; ++i) {
<?php <?php
} }
function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) { function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status)
{
// It should not be possible to send bad data for these fields so clearly // It should not be possible to send bad data for these fields so clearly
// someone is up to something if they fail. Fail silently. // someone is up to something if they fail. Fail silently.
if (!(array_key_exists($_POST['wpa'], $wpa_array) && if (!(array_key_exists($_POST['wpa'], $wpa_array) &&

View File

@ -1,11 +1,9 @@
<?php <?php
/** /**
* Sets locale information for i18n support * Sets locale information for i18n support
* *
*/ */
/** /**
* Rudimentary language detection via the browser. * Rudimentary language detection via the browser.
* Accept-Language returns a list of weighted values with a quality (or 'q') parameter. * Accept-Language returns a list of weighted values with a quality (or 'q') parameter.
@ -72,4 +70,3 @@ bindtextdomain(LOCALE_DOMAIN, LOCALE_ROOT);
bind_textdomain_codeset(LOCALE_DOMAIN, 'UTF-8'); bind_textdomain_codeset(LOCALE_DOMAIN, 'UTF-8');
textdomain(LOCALE_DOMAIN); textdomain(LOCALE_DOMAIN);

View File

@ -6,7 +6,8 @@ include_once( 'includes/status_messages.php' );
* *
* *
*/ */
function DisplayNetworkingConfig(){ function DisplayNetworkingConfig()
{
$status = new StatusMessages(); $status = new StatusMessages();

View File

@ -1,22 +1,30 @@
<?php <?php
class StatusMessages { class StatusMessages
{
public $messages = array(); public $messages = array();
public function addMessage($message, $level='success', $dismissable=true) { public function addMessage($message, $level = 'success', $dismissable = true)
{
$status = '<div class="alert alert-'.$level; $status = '<div class="alert alert-'.$level;
if ($dismissable) $status .= ' alert-dismissable'; if ($dismissable) {
$status .= ' alert-dismissable';
}
$status .= '">'. _($message); $status .= '">'. _($message);
if ($dismissable) $status .= '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">x</button>'; if ($dismissable) {
$status .= '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">x</button>';
}
$status .= '</div>'; $status .= '</div>';
array_push($this->messages, $status); array_push($this->messages, $status);
} }
public function showMessages($clear = true) { public function showMessages($clear = true)
{
foreach ($this->messages as $message) { foreach ($this->messages as $message) {
echo $message; echo $message;
} }
if ( $clear ) $this->messages = array(); if ($clear) {
$this->messages = array();
}
} }
} }
?>

View File

@ -9,7 +9,8 @@ include_once( 'includes/status_messages.php' );
* *
*/ */
function RPiVersion() { function RPiVersion()
{
// Lookup table from http://www.raspberrypi-spy.co.uk/2012/09/checking-your-raspberry-pi-board-version/ // Lookup table from http://www.raspberrypi-spy.co.uk/2012/09/checking-your-raspberry-pi-board-version/
$revisions = array( $revisions = array(
'0002' => 'Model B Revision 1.0', '0002' => 'Model B Revision 1.0',
@ -56,7 +57,8 @@ function RPiVersion() {
* *
* *
*/ */
function DisplaySystem(){ function DisplaySystem()
{
$status = new StatusMessages(); $status = new StatusMessages();
@ -101,25 +103,39 @@ function DisplaySystem(){
$hours = floor($hours - ($days * 24)); $hours = floor($hours - ($days * 24));
$minutes = floor($minutes - ($days * 24 * 60) - ($hours * 60)); $minutes = floor($minutes - ($days * 24 * 60) - ($hours * 60));
$uptime= ''; $uptime= '';
if ($days != 0) { $uptime .= $days . ' day' . (($days > 1)? 's ':' '); } if ($days != 0) {
if ($hours != 0) { $uptime .= $hours . ' hour' . (($hours > 1)? 's ':' '); } $uptime .= $days . ' day' . (($days > 1)? 's ':' ');
if ($minutes != 0) { $uptime .= $minutes . ' minute' . (($minutes > 1)? 's ':' '); } }
if ($hours != 0) {
$uptime .= $hours . ' hour' . (($hours > 1)? 's ':' ');
}
if ($minutes != 0) {
$uptime .= $minutes . ' minute' . (($minutes > 1)? 's ':' ');
}
// mem used // mem used
$memused_status = "primary"; $memused_status = "primary";
exec("free -m | awk '/Mem:/ { total=$2 ; used=$3 } END { print used/total*100}'", $memarray); exec("free -m | awk '/Mem:/ { total=$2 ; used=$3 } END { print used/total*100}'", $memarray);
$memused = floor($memarray[0]); $memused = floor($memarray[0]);
if ($memused > 90) { $memused_status = "danger"; } if ($memused > 90) {
elseif ($memused > 75) { $memused_status = "warning"; } $memused_status = "danger";
elseif ($memused > 0) { $memused_status = "success"; } } elseif ($memused > 75) {
$memused_status = "warning";
} elseif ($memused > 0) {
$memused_status = "success";
}
// cpu load // cpu load
$cores = exec("grep -c ^processor /proc/cpuinfo"); $cores = exec("grep -c ^processor /proc/cpuinfo");
$loadavg = exec("awk '{print $1}' /proc/loadavg"); $loadavg = exec("awk '{print $1}' /proc/loadavg");
$cpuload = floor(($loadavg * 100) / $cores); $cpuload = floor(($loadavg * 100) / $cores);
if ($cpuload > 90) { $cpuload_status = "danger"; } if ($cpuload > 90) {
elseif ($cpuload > 75) { $cpuload_status = "warning"; } $cpuload_status = "danger";
elseif ($cpuload > 0) { $cpuload_status = "success"; } } elseif ($cpuload > 75) {
$cpuload_status = "warning";
} elseif ($cpuload > 0) {
$cpuload_status = "success";
}
?> ?>
<div class="row"> <div class="row">

View File

@ -3,7 +3,8 @@
* *
* *
*/ */
function DisplayThemeConfig(){ function DisplayThemeConfig()
{
$cselected = ''; $cselected = '';
$hselected = ''; $hselected = '';

View File

@ -12,4 +12,3 @@ if(file_exists(RASPI_CONFIG.'/raspap.auth')) {
fclose($auth_details); fclose($auth_details);
} }
} }
?>