Skip to content

Commit

Permalink
fix: apply phpstan to page files (#4)
Browse files Browse the repository at this point in the history
* fix: apply phpstan to page files

* fix: bring webgui function local

* fix: style linter

* fix: formatting
  • Loading branch information
dkaser authored Oct 16, 2024
1 parent 0d73913 commit 99bef96
Show file tree
Hide file tree
Showing 33 changed files with 136 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
- uses: actions/checkout@v4
- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --diff --dry-run

phpstan:
name: PHPStan Analysis
Expand Down
5 changes: 4 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
parameters:
level: 6
fileExtensions:
- php
- page
paths:
- src
- src
46 changes: 27 additions & 19 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale-1-Settings.page
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Title="Settings"
Type="xmenu"
Tag="gears"
---
<?php
$tailscale_config = $tailscale_config ?? getPluginConfig();

if(!isset($tailscale_output)) {
echo("Tailscale output not defined");
return;
}
?>
<style>
.fileTree {
background:
Expand All @@ -19,12 +27,12 @@ Tag="gears"
}
.advanced{display:none}
</style>
<link type="text/css" rel="stylesheet" href="<?autov('/webGui/styles/jquery.filetree.css')?>">
<link type="text/css" rel="stylesheet" href="<?autov('/webGui/styles/jquery.switchbutton.css')?>">
<link type="text/css" rel="stylesheet" href="<?= auto_v('/webGui/styles/jquery.filetree.css')?>">
<link type="text/css" rel="stylesheet" href="<?= auto_v('/webGui/styles/jquery.switchbutton.css')?>">
<span class="status vhshift"><input type="checkbox" class="advancedview"></span>
<form markdown="1" method="POST" action="/update.php" target="progressFrame">
<input type="hidden" name="#file"
value="<?= $plugin; ?>/<?= $plugin; ?>.cfg" />
value="tailscale/tailscale.cfg" />
<input type="hidden" name="#cleanup" value="" />
<input type="hidden" name="#command" value="/usr/local/emhttp/plugins/tailscale/restart.sh" />

Expand All @@ -33,22 +41,22 @@ Tag="gears"
<div class="advanced" markdown="1">
<?= _tr("settings.enable_tailscale") ?>
: <select name='ENABLE_TAILSCALE' size='1'>
<?= mk_option($tailscale_config["ENABLE_TAILSCALE"], '1', _tr("yes"));?>
<?= mk_option($tailscale_config["ENABLE_TAILSCALE"], '0', _tr("no"));?>
<?= make_option($tailscale_config["ENABLE_TAILSCALE"], '1', _tr("yes"));?>
<?= make_option($tailscale_config["ENABLE_TAILSCALE"], '0', _tr("no"));?>
</select>

<?= _tr("settings.unraid_listen") ?>
: <select name='INCLUDE_INTERFACE' size='1'>
<?= mk_option($tailscale_config["INCLUDE_INTERFACE"], '1', _tr("yes"));?>
<?= mk_option($tailscale_config["INCLUDE_INTERFACE"], '0', _tr("no"));?>
<?= make_option($tailscale_config["INCLUDE_INTERFACE"], '1', _tr("yes"));?>
<?= make_option($tailscale_config["INCLUDE_INTERFACE"], '0', _tr("no"));?>
</select>

> <?= _tr("settings.context.unraid_listen") ?>

<?= _tr("settings.ip_forward") ?>
: <select name='SYSCTL_IP_FORWARD' size='1'>
<?= mk_option($tailscale_config['SYSCTL_IP_FORWARD'], '1', _tr("yes"));?>
<?= mk_option($tailscale_config['SYSCTL_IP_FORWARD'], '0', _tr("no"));?>
<?= make_option($tailscale_config['SYSCTL_IP_FORWARD'], '1', _tr("yes"));?>
<?= make_option($tailscale_config['SYSCTL_IP_FORWARD'], '0', _tr("no"));?>
</select>

> <?= _tr("settings.context.ip_forward") ?>
Expand All @@ -63,8 +71,8 @@ Tag="gears"

<?= _tr("settings.usage") ?>
: <select name='USAGE' size='1'>
<?= mk_option($tailscale_config["USAGE"], '1', _tr("yes"));?>
<?= mk_option($tailscale_config["USAGE"], '0', _tr("no"));?>
<?= make_option($tailscale_config["USAGE"], '1', _tr("yes"));?>
<?= make_option($tailscale_config["USAGE"], '0', _tr("no"));?>
</select>

> <?= _tr("settings.context.usage") ?>
Expand All @@ -83,19 +91,19 @@ Tag="gears"

<?= _tr("settings.subnets") ?>
: <select name='ACCEPT_ROUTES' size='1'>
<?= mk_option($tailscale_config['ACCEPT_ROUTES'], '0', _tr("no"));?>
<?= mk_option($tailscale_config['ACCEPT_ROUTES'], '1', _tr("yes"));?>
<?= mk_option($tailscale_config['ACCEPT_ROUTES'], '2', _tr("ignore"));?>
<?= make_option($tailscale_config['ACCEPT_ROUTES'], '0', _tr("no"));?>
<?= make_option($tailscale_config['ACCEPT_ROUTES'], '1', _tr("yes"));?>
<?= make_option($tailscale_config['ACCEPT_ROUTES'], '2', _tr("ignore"));?>
</select>

> <?= _tr("settings.context.subnets") ?>
> <?= _tr("settings.context.ignore") ?>

<?= _tr("settings.dns") ?>
: <select name='ACCEPT_DNS' size='1'>
<?= mk_option($tailscale_config['ACCEPT_DNS'], '0', _tr("no"));?>
<?= mk_option($tailscale_config['ACCEPT_DNS'], '1', _tr("yes"));?>
<?= mk_option($tailscale_config['ACCEPT_DNS'], '2', _tr("ignore"));?>
<?= make_option($tailscale_config['ACCEPT_DNS'], '0', _tr("no"));?>
<?= make_option($tailscale_config['ACCEPT_DNS'], '1', _tr("yes"));?>
<?= make_option($tailscale_config['ACCEPT_DNS'], '2', _tr("ignore"));?>
</select>

> <?= _tr("settings.context.dns") ?>
Expand Down Expand Up @@ -140,8 +148,8 @@ Tag="gears"
<?= _tr("settings.context.donate") ?>
: <input type="button" value="Paypal" onclick="window.open('https://paypal.me/edacerton', '_blank')"> <input type="button" value="GitHub" onclick="window.open('https://github.com/sponsors/dkaser', '_blank')">

<script src="<?autov('/webGui/javascript/jquery.filetree.js')?>" charset="utf-8"></script>
<script src="<?autov('/webGui/javascript/jquery.switchbutton.js')?>"></script>
<script src="<?= auto_v('/webGui/javascript/jquery.filetree.js')?>" charset="utf-8"></script>
<script src="<?= auto_v('/webGui/javascript/jquery.switchbutton.js')?>"></script>
<script>
function requestErase(e) {
e.disabled = true;
Expand Down
7 changes: 7 additions & 0 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale-2-Lock.page
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ Type="xmenu"
Tag="lock"
---
<?php
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
$tailscale_config = $tailscale_config ?? getPluginConfig();

if ($tailscale_config['ENABLE_TAILSCALE'] == "1") {

$signingNode = false;

if(!isset($tailscale_output)) {
echo("Tailscale output not defined");
return;
}

switch (true) {
case $tailscale_output['lock_signing']:
require "{$docroot}/plugins/tailscale/include/tailscale-lock/signing.php";
Expand Down
6 changes: 6 additions & 0 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale-3-Info.page
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ Tag="info"
Type="xmenu"
---
<?php
$tailscale_config = $tailscale_config ?? getPluginConfig();

if ($tailscale_config['ENABLE_TAILSCALE'] == "1") {
if(!isset($tailscale_output)) {
echo("Tailscale output not defined");
return;
}
?>
<table class="share_status">
<thead>
Expand Down
4 changes: 2 additions & 2 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale-4-Help.page
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Tag="question"
Type="xmenu"
---
<?php
function help_block($section, $header="h3") {
function help_block(string $section, string $header="h3") : string {
$title = _tr("settings.{$section}");
$body = _tr("help.{$section}");
echo <<<END
return <<<END
<{$header}>{$title}</{$header}>
<p>{$body}</p>
END;
Expand Down
9 changes: 7 additions & 2 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale-5-Log.page
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ Type="xmenu"
* all copies or substantial portions of the Software.
*/
?>
<?
<?php
if(!isset($var)) {
echo ("Error: Missing WebGUI vars");
return;
}

$zip = htmlspecialchars(str_replace(' ','_',strtolower($var['NAME'])));
$log = '/var/log/tailscale.log';
$prev = '/var/log/tailscale-utils.log';
Expand All @@ -33,7 +38,7 @@ if (count($logs)) {
// add syslog to front of logs array
array_unshift($logs, $log);
$select[] = "<select onchange='showLog(this.value)'>";
foreach ($logs as $file) $select[] = mk_option(1,$file,basename($file));
foreach ($logs as $file) $select[] = make_option("1",$file,basename($file));
$select[] = "</select>";
}
$select = implode($select);
Expand Down
7 changes: 7 additions & 0 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale.page
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ Type="xmenu"
Tabs="true"
---
<?php
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "{$docroot}/plugins/tailscale/include/common.php";

$tailscale_config = $tailscale_config ?? getPluginConfig();

if ($tailscale_config['ENABLE_TAILSCALE'] == "1") {
require_once "{$docroot}/plugins/tailscale/include/webgui-interface.php";
}
Expand All @@ -18,6 +21,10 @@ if ($tailscale_config['ENABLE_TAILSCALE'] == "1") {
</script>
<?php
if ($tailscale_config['ENABLE_TAILSCALE'] == "1") {
if(!isset($tailscale_output)) {
echo("Tailscale output not defined");
return;
}
?>
<?= $tailscale_output['lock_warning']; ?>
<?= $tailscale_output['key_expiry_warning']; ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ Menu="Dashboard:0"
---
<?php

$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "{$docroot}/plugins/tailscale/include/common.php";

$tailscale_config = $tailscale_config ?? getPluginConfig();

$tailscale_dashboard = "<tr><td>" . _tr("tailscale_disabled") . "</td></tr>";

if ($tailscale_config['ENABLE_TAILSCALE'] == "1") {
require_once "{$docroot}/plugins/tailscale/include/webgui-interface.php";
} else {
$tailscale_dashboard = "<tr><td>" . _tr("tailscale_disabled") . "</td></tr>";
}

$mytiles['tailscale']['column2'] =
Expand Down
2 changes: 1 addition & 1 deletion src/usr/local/emhttp/plugins/tailscale/include/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

$plugin = "tailscale";

$restart_command = '/usr/local/emhttp/webGui/scripts/reload_services';
$restart_command = '/usr/local/emhttp/webGui/scripts/reload_services';

$tailscale_config = getPluginConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/**
* @return array<string, mixed>
*/
function getPluginConfig() : array
function getPluginConfig(): array
{
$config_file = '/boot/config/plugins/tailscale/tailscale.cfg';
$defaults_file = '/usr/local/emhttp/plugins/tailscale/settings.json';
$config_file = '/boot/config/plugins/tailscale/tailscale.cfg';
$defaults_file = '/usr/local/emhttp/plugins/tailscale/settings.json';

// Load configuration file
if (file_exists($config_file)) {
Expand All @@ -24,4 +24,4 @@ function getPluginConfig() : array
}

return $tailscale_config;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function applyGRO() : void
function applyGRO(): void
{
$ip_route = json_decode(implode(run_command('ip -j route get 8.8.8.8')), true);

Expand Down
17 changes: 17 additions & 0 deletions src/usr/local/emhttp/plugins/tailscale/include/common/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

function make_option(string $select, string $value, string $text, string $extra = ""): string
{
return "<option value='{$value}'" . ($value == $select ? " selected" : "") . (strlen($extra) ? " {$extra}" : "") . ">{$text}</option>";
}

function auto_v(string $file): string
{
global $docroot;
$path = $docroot . $file;
clearstatcache(true, $path);
$time = file_exists($path) ? filemtime($path) : 'autov_fileDoesntExist';
$newFile = "{$file}?v=" . $time;

return $newFile;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function ip4_in_network(string $ip, string $network) : bool
function ip4_in_network(string $ip, string $network): bool
{
if (strpos($network, '/') === false) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function logmsg(string $message) : void
function logmsg(string $message): void
{
$timestamp = date('Y/m/d H:i:s');
$filename = basename($_SERVER['PHP_SELF']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* @return array<string>
*/
function run_command(string $command, bool $alwaysShow = false, bool $show = true) : array
function run_command(string $command, bool $alwaysShow = false, bool $show = true): array
{
$output = array();
$retval = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php

function getTailscaleStatus() : object
function getTailscaleStatus(): object
{
exec("tailscale status --json", $out_status);
return json_decode(implode($out_status));
}

function getTailscalePrefs() : object
function getTailscalePrefs(): object
{
exec("tailscale debug prefs", $out_prefs);
return json_decode(implode($out_prefs));
}

function getTailscaleLock() : object
function getTailscaleLock(): object
{
exec("tailscale lock status -json=true", $out_status);
return json_decode(implode($out_status));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function _tr(string $message) : string
function _tr(string $message): string
{
global $tailscale_lang;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function refreshWebGuiCert(bool $restartIfChanged = true) : void
function refreshWebGuiCert(bool $restartIfChanged = true): void
{
$status = getTailscaleStatus();

Expand Down
4 changes: 2 additions & 2 deletions src/usr/local/emhttp/plugins/tailscale/include/daily/ping.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

$endpoint = "https://plugin-usage.edacerton.win/";

function download_url(string $url) : string
function download_url(string $url): string
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
Expand All @@ -24,7 +24,7 @@ function download_url(string $url) : string
/**
* @param array<mixed> $content
*/
function send_usage(string $url, array $content) : int
function send_usage(string $url, array $content): int
{
$body = json_encode($content);
$token = download_url($url . '?connect');
Expand Down
2 changes: 1 addition & 1 deletion src/usr/local/emhttp/plugins/tailscale/include/get_log.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function getLog(string $log, int $max) : void
function getLog(string $log, int $max): void
{
$allowed_files = ["/var/log/tailscale.log", "/var/log/tailscale-utils.log"];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* @param array<mixed> $var
*/
function getTailscaleNetbiosWarning(array $var) : string
function getTailscaleNetbiosWarning(array $var): string
{
if (($var['USE_NETBIOS'] == "yes") && ($var['shareSMBEnabled'] != "no")) {
return "<span class='warn' style='text-align: center; font-size: 1.4em; font-weight: bold;'>" . _tr("warnings.netbios") . "</span>";
Expand Down
Loading

0 comments on commit 99bef96

Please sign in to comment.