Skip to content

Commit

Permalink
refactor: move classes to namespace (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkaser authored Oct 17, 2024
1 parent 238ddfb commit 368a4da
Show file tree
Hide file tree
Showing 34 changed files with 123 additions and 117 deletions.
40 changes: 20 additions & 20 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale-1-Settings.page
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Type="xmenu"
Tag="gears"
---
<?php
$tailscale_config = $tailscale_config ?? TailscaleHelpers::getPluginConfig();
$tr = $tr ?? new Translator();
$tailscale_config = $tailscale_config ?? Tailscale\Helpers::getPluginConfig();
$tr = $tr ?? new Tailscale\Translator();

if(!isset($tailscale_output)) {
echo("Tailscale output not defined");
Expand All @@ -28,8 +28,8 @@ Tag="gears"
}
.advanced{display:none}
</style>
<link type="text/css" rel="stylesheet" href="<?= TailscaleHelpers::auto_v('/webGui/styles/jquery.filetree.css')?>">
<link type="text/css" rel="stylesheet" href="<?= TailscaleHelpers::auto_v('/webGui/styles/jquery.switchbutton.css')?>">
<link type="text/css" rel="stylesheet" href="<?= Tailscale\Helpers::auto_v('/webGui/styles/jquery.filetree.css')?>">
<link type="text/css" rel="stylesheet" href="<?= Tailscale\Helpers::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"
Expand All @@ -42,22 +42,22 @@ Tag="gears"
<div class="advanced" markdown="1">
<?= $tr->tr("settings.enable_tailscale") ?>
: <select name='ENABLE_TAILSCALE' size='1'>
<?= TailscaleHelpers::make_option($tailscale_config["ENABLE_TAILSCALE"], '1', $tr->tr("yes"));?>
<?= TailscaleHelpers::make_option($tailscale_config["ENABLE_TAILSCALE"], '0', $tr->tr("no"));?>
<?= Tailscale\Helpers::make_option($tailscale_config["ENABLE_TAILSCALE"], '1', $tr->tr("yes"));?>
<?= Tailscale\Helpers::make_option($tailscale_config["ENABLE_TAILSCALE"], '0', $tr->tr("no"));?>
</select>

<?= $tr->tr("settings.unraid_listen") ?>
: <select name='INCLUDE_INTERFACE' size='1'>
<?= TailscaleHelpers::make_option($tailscale_config["INCLUDE_INTERFACE"], '1', $tr->tr("yes"));?>
<?= TailscaleHelpers::make_option($tailscale_config["INCLUDE_INTERFACE"], '0', $tr->tr("no"));?>
<?= Tailscale\Helpers::make_option($tailscale_config["INCLUDE_INTERFACE"], '1', $tr->tr("yes"));?>
<?= Tailscale\Helpers::make_option($tailscale_config["INCLUDE_INTERFACE"], '0', $tr->tr("no"));?>
</select>

> <?= $tr->tr("settings.context.unraid_listen") ?>

<?= $tr->tr("settings.ip_forward") ?>
: <select name='SYSCTL_IP_FORWARD' size='1'>
<?= TailscaleHelpers::make_option($tailscale_config['SYSCTL_IP_FORWARD'], '1', $tr->tr("yes"));?>
<?= TailscaleHelpers::make_option($tailscale_config['SYSCTL_IP_FORWARD'], '0', $tr->tr("no"));?>
<?= Tailscale\Helpers::make_option($tailscale_config['SYSCTL_IP_FORWARD'], '1', $tr->tr("yes"));?>
<?= Tailscale\Helpers::make_option($tailscale_config['SYSCTL_IP_FORWARD'], '0', $tr->tr("no"));?>
</select>

> <?= $tr->tr("settings.context.ip_forward") ?>
Expand All @@ -72,8 +72,8 @@ Tag="gears"

<?= $tr->tr("settings.usage") ?>
: <select name='USAGE' size='1'>
<?= TailscaleHelpers::make_option($tailscale_config["USAGE"], '1', $tr->tr("yes"));?>
<?= TailscaleHelpers::make_option($tailscale_config["USAGE"], '0', $tr->tr("no"));?>
<?= Tailscale\Helpers::make_option($tailscale_config["USAGE"], '1', $tr->tr("yes"));?>
<?= Tailscale\Helpers::make_option($tailscale_config["USAGE"], '0', $tr->tr("no"));?>
</select>

> <?= $tr->tr("settings.context.usage") ?>
Expand All @@ -92,19 +92,19 @@ Tag="gears"

<?= $tr->tr("settings.subnets") ?>
: <select name='ACCEPT_ROUTES' size='1'>
<?= TailscaleHelpers::make_option($tailscale_config['ACCEPT_ROUTES'], '0', $tr->tr("no"));?>
<?= TailscaleHelpers::make_option($tailscale_config['ACCEPT_ROUTES'], '1', $tr->tr("yes"));?>
<?= TailscaleHelpers::make_option($tailscale_config['ACCEPT_ROUTES'], '2', $tr->tr("ignore"));?>
<?= Tailscale\Helpers::make_option($tailscale_config['ACCEPT_ROUTES'], '0', $tr->tr("no"));?>
<?= Tailscale\Helpers::make_option($tailscale_config['ACCEPT_ROUTES'], '1', $tr->tr("yes"));?>
<?= Tailscale\Helpers::make_option($tailscale_config['ACCEPT_ROUTES'], '2', $tr->tr("ignore"));?>
</select>

> <?= $tr->tr("settings.context.subnets") ?>
> <?= $tr->tr("settings.context.ignore") ?>

<?= $tr->tr("settings.dns") ?>
: <select name='ACCEPT_DNS' size='1'>
<?= TailscaleHelpers::make_option($tailscale_config['ACCEPT_DNS'], '0', $tr->tr("no"));?>
<?= TailscaleHelpers::make_option($tailscale_config['ACCEPT_DNS'], '1', $tr->tr("yes"));?>
<?= TailscaleHelpers::make_option($tailscale_config['ACCEPT_DNS'], '2', $tr->tr("ignore"));?>
<?= Tailscale\Helpers::make_option($tailscale_config['ACCEPT_DNS'], '0', $tr->tr("no"));?>
<?= Tailscale\Helpers::make_option($tailscale_config['ACCEPT_DNS'], '1', $tr->tr("yes"));?>
<?= Tailscale\Helpers::make_option($tailscale_config['ACCEPT_DNS'], '2', $tr->tr("ignore"));?>
</select>

> <?= $tr->tr("settings.context.dns") ?>
Expand Down Expand Up @@ -149,8 +149,8 @@ Tag="gears"
<?= $tr->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="<?= TailscaleHelpers::auto_v('/webGui/javascript/jquery.filetree.js')?>" charset="utf-8"></script>
<script src="<?= TailscaleHelpers::auto_v('/webGui/javascript/jquery.switchbutton.js')?>"></script>
<script src="<?= Tailscale\Helpers::auto_v('/webGui/javascript/jquery.filetree.js')?>" charset="utf-8"></script>
<script src="<?= Tailscale\Helpers::auto_v('/webGui/javascript/jquery.switchbutton.js')?>"></script>
<script>
function requestErase(e) {
e.disabled = true;
Expand Down
4 changes: 2 additions & 2 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale-2-Lock.page
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Tag="lock"
---
<?php
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
$tailscale_config = $tailscale_config ?? TailscaleHelpers::getPluginConfig();
$tr = $tr ?? new Translator();
$tailscale_config = $tailscale_config ?? Tailscale\Helpers::getPluginConfig();
$tr = $tr ?? new Tailscale\Translator();

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

Expand Down
4 changes: 2 additions & 2 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale-3-Info.page
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Tag="info"
Type="xmenu"
---
<?php
$tailscale_config = $tailscale_config ?? TailscaleHelpers::getPluginConfig();
$tr = $tr ?? new Translator();
$tailscale_config = $tailscale_config ?? Tailscale\Helpers::getPluginConfig();
$tr = $tr ?? new Tailscale\Translator();

if ($tailscale_config['ENABLE_TAILSCALE'] == "1") {
if(!isset($tailscale_output)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tag="question"
Type="xmenu"
---
<?php
$tr = $tr ?? new Translator();
$tr = $tr ?? new Tailscale\Translator();
function help_block(string $section, string $header="h3") : string {
global $tr;
$title = $tr->tr("settings.{$section}");
Expand Down
4 changes: 2 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 @@ -21,7 +21,7 @@ Type="xmenu"
echo ("Error: Missing WebGUI vars");
return;
}
$tr = $tr ?? new Translator();
$tr = $tr ?? new Tailscale\Translator();

$zip = htmlspecialchars(str_replace(' ','_',strtolower($var['NAME'])));
$log = '/var/log/tailscale.log';
Expand All @@ -39,7 +39,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[] = TailscaleHelpers::make_option("1",$file,basename($file));
foreach ($logs as $file) $select[] = Tailscale\Helpers::make_option("1",$file,basename($file));
$select[] = "</select>";
}
$select = implode($select);
Expand Down
4 changes: 2 additions & 2 deletions src/usr/local/emhttp/plugins/tailscale/Tailscale.page
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Tabs="true"
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "{$docroot}/plugins/tailscale/include/common.php";

$tailscale_config = $tailscale_config ?? TailscaleHelpers::getPluginConfig();
$tr = $tr ?? new Translator();
$tailscale_config = $tailscale_config ?? Tailscale\Helpers::getPluginConfig();
$tr = $tr ?? new Tailscale\Translator();

if ($tailscale_config['ENABLE_TAILSCALE'] == "1") {
require_once "{$docroot}/plugins/tailscale/include/webgui-interface.php";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Menu="Dashboard:0"
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "{$docroot}/plugins/tailscale/include/common.php";

$tailscale_config = $tailscale_config ?? TailscaleHelpers::getPluginConfig();
$tr = $tr ?? new Translator();
$tailscale_config = $tailscale_config ?? Tailscale\Helpers::getPluginConfig();
$tr = $tr ?? new Tailscale\Translator();

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

Expand Down
2 changes: 1 addition & 1 deletion src/usr/local/emhttp/plugins/tailscale/approve-nodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
require_once "include/common.php";

foreach (array_slice($argv, 1) as $key => $value) {
TailscaleHelpers::logmsg("Tailnet lock: signing {$value}");
Tailscale\Helpers::logmsg("Tailnet lock: signing {$value}");
exec("tailscale lock sign {$value}");
}
6 changes: 3 additions & 3 deletions src/usr/local/emhttp/plugins/tailscale/include/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
try {
require $file;
} catch (Throwable $e) {
TailscaleHelpers::logmsg("Caught exception in {$file} : " . $e->getMessage());
Tailscale\Helpers::logmsg("Caught exception in {$file} : " . $e->getMessage());
}
}

$plugin = "tailscale";
$restart_command = '/usr/local/emhttp/webGui/scripts/reload_services';
$tailscale_config = TailscaleHelpers::getPluginConfig();
$tailscale_config = Tailscale\Helpers::getPluginConfig();
$configure_extra_interfaces = file_exists($restart_command);

$tr = $tr ?? new Translator();
$tr = $tr ?? new Tailscale\Translator();
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

class TailscaleHelpers
namespace Tailscale;

class Helpers
{
public static function make_option(string $select, string $value, string $text, string $extra = ""): string
{
Expand All @@ -26,12 +28,12 @@ public static function run_command(string $command, bool $alwaysShow = false, bo
$output = array();
$retval = null;
if ($show) {
TailscaleHelpers::logmsg($command);
self::logmsg($command);
}
exec("{$command} 2>&1", $output, $retval);

if (($retval != 0) || $alwaysShow) {
TailscaleHelpers::logmsg("Command returned {$retval}" . PHP_EOL . implode(PHP_EOL, $output));
self::logmsg("Command returned {$retval}" . PHP_EOL . implode(PHP_EOL, $output));
}

return $output;
Expand Down Expand Up @@ -59,7 +61,7 @@ public static function ip4_in_network(string $ip, string $network): bool

public static function refreshWebGuiCert(bool $restartIfChanged = true): void
{
$status = TailscaleInfo::getStatus();
$status = Info::getStatus();

$certDomains = $status->CertDomains;

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

class TailscaleInfo
namespace Tailscale;

class Info
{
private Translator $tr;
private stdClass $status;
private stdClass $prefs;
private stdClass $lock;
private \stdClass $status;
private \stdClass $prefs;
private \stdClass $lock;

public function __construct(Translator $tr)
{
Expand All @@ -15,19 +17,19 @@ public function __construct(Translator $tr)
$this->lock = self::getLock();
}

public static function getStatus(): stdClass
public static function getStatus(): \stdClass
{
exec("tailscale status --json", $out_status);
return (object) json_decode(implode($out_status));
}

public static function getPrefs(): stdClass
public static function getPrefs(): \stdClass
{
exec("tailscale debug prefs", $out_prefs);
return (object) json_decode(implode($out_prefs));
}

public static function getLock(): stdClass
public static function getLock(): \stdClass
{
exec("tailscale lock status -json=true", $out_status);
return (object) json_decode(implode($out_status));
Expand Down Expand Up @@ -138,11 +140,11 @@ public function getKeyExpirationWarning(): string
$status = $this->status;

if (isset($status->Self->KeyExpiry)) {
$expiryTime = new DateTime($status->Self->KeyExpiry);
$expiryTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
$expiryTime = new \DateTime($status->Self->KeyExpiry);
$expiryTime->setTimezone(new \DateTimeZone(date_default_timezone_get()));

$interval = $expiryTime->diff(new DateTime('now'));
$expiryPrint = $expiryTime->format(DateTimeInterface::RFC7231);
$interval = $expiryTime->diff(new \DateTime('now'));
$expiryPrint = $expiryTime->format(\DateTimeInterface::RFC7231);
$intervalPrint = $interval->format('%a');

switch (true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Tailscale;

class Translator
{
/** @var array<string, string> $tailscale_lang */
Expand All @@ -19,7 +21,7 @@ public function __construct()
$tailscale_locale = array_replace_recursive($tailscale_locale, $current_locale);
}

$ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($tailscale_locale));
$ritit = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($tailscale_locale));
$tailscale_lang = array();
foreach ($ritit as $leafValue) {
$keys = array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

$notify = "{$docroot}/webGui/scripts/notify";

$status = TailscaleInfo::getStatus();
$status = Tailscale\Info::getStatus();

if (isset($status->Self->KeyExpiry)) {
$expiryTime = new DateTime($status->Self->KeyExpiry);
Expand All @@ -15,7 +15,7 @@
$intervalPrint = $interval->format('%a');

$message = "The Tailscale key will expire in {$intervalPrint} days on {$expiryPrint}.";
TailscaleHelpers::logmsg($message);
Tailscale\Helpers::logmsg($message);

switch (true) {
case $interval->days <= 7:
Expand All @@ -29,10 +29,10 @@
}

$event = "Tailscale Key Expiration - {$priority} - {$expiryTime->format('Ymd')}";
TailscaleHelpers::logmsg("Sending notification for key expiration: {$event}");
Tailscale\Helpers::logmsg("Sending notification for key expiration: {$event}");

$command = "{$notify} -l '/Settings/Tailscale' -e " . escapeshellarg($event) . " -s " . escapeshellarg("Tailscale key is expiring") . " -d " . escapeshellarg("{$message}") . " -i \"{$priority}\" -x 2>/dev/null";
exec($command);
} else {
TailscaleHelpers::logmsg("Tailscale key expiration is not set.");
Tailscale\Helpers::logmsg("Tailscale key expiration is not set.");
}
14 changes: 7 additions & 7 deletions src/usr/local/emhttp/plugins/tailscale/include/daily/ping.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$tailscale_config = $tailscale_config ?? TailscaleHelpers::getPluginConfig();
$tailscale_config = $tailscale_config ?? Tailscale\Helpers::getPluginConfig();

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

Expand Down Expand Up @@ -57,10 +57,10 @@ function send_usage(string $url, array $content): int

$version = parse_ini_file('/var/local/emhttp/plugins/tailscale/tailscale.ini') ?: array();

$prefs = TailscaleInfo::getPrefs();
$prefs = Tailscale\Info::getPrefs();

if (isset($prefs->LoggedOut) ? ($prefs->LoggedOut ? true : false) : true) {
TailscaleHelpers::logmsg("Skipping usage data collection; not logged in.");
Tailscale\Helpers::logmsg("Skipping usage data collection; not logged in.");
return;
}

Expand Down Expand Up @@ -101,18 +101,18 @@ function send_usage(string $url, array $content): int
$attempts = 0;
$delay = rand(0, 300);
do {
TailscaleHelpers::logmsg("Waiting for {$delay} seconds before sending usage data.");
Tailscale\Helpers::logmsg("Waiting for {$delay} seconds before sending usage data.");
sleep($delay);
$delay += 300;
$attempts++;

$result = send_usage($endpoint, $content);
TailscaleHelpers::logmsg("Usage data sent.");
Tailscale\Helpers::logmsg("Usage data sent.");
} while (($result != '200') && ($attempts < 3));

if ($result != '200') {
TailscaleHelpers::logmsg("Error occurred while transmitting usage data.");
Tailscale\Helpers::logmsg("Error occurred while transmitting usage data.");
}
} else {
TailscaleHelpers::logmsg("Usage collection disabled.");
Tailscale\Helpers::logmsg("Usage collection disabled.");
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

TailscaleHelpers::refreshWebGuiCert();
Tailscale\Helpers::refreshWebGuiCert();
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$tailscale_config = $tailscale_config ?? TailscaleHelpers::getPluginConfig();
$tailscale_config = $tailscale_config ?? Tailscale\Helpers::getPluginConfig();

$custom_params = "";

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

TailscaleHelpers::applyGRO();
Tailscale\Helpers::applyGRO();
Loading

0 comments on commit 368a4da

Please sign in to comment.