Skip to content

Commit

Permalink
Merge pull request #1693 from RaspAP/feat/plugin-architecture
Browse files Browse the repository at this point in the history
Plugin architecture to support user add-ons
  • Loading branch information
billz authored Nov 10, 2024
2 parents a86c637 + 6fa4f3d commit b74db94
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 148 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_modules
yarn-error.log
*.swp
includes/config.php
plugins/
rootCA.pem
vendor
.env
32 changes: 30 additions & 2 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,13 @@ function ConvertToSecurity($security)
/**
* Renders a simple PHP template
*/
function renderTemplate($name, $__template_data = [])
function renderTemplate($name, $__template_data = [], $pluginName = null)
{
$file = realpath(dirname(__FILE__) . "/../templates/$name.php");
if (is_string($pluginName)) {
$file = realpath(dirname(__FILE__) . "/../plugins/$pluginName/templates/$name.php");
} else {
$file = realpath(dirname(__FILE__) . "/../templates/$name.php");
}
if (!file_exists($file)) {
return "template $name ($file) not found";
}
Expand Down Expand Up @@ -1005,3 +1009,27 @@ function lightenColor($color, $percent)

return sprintf("#%02x%02x%02x", $r, $g, $b);
}

function renderStatus($hostapd_led, $hostapd_status, $memused_led, $memused, $cputemp_led, $cputemp)
{
?>
<div class="row g-0">
<div class="col-4 ms-2 sidebar-brand-icon">
<img src="app/img/raspAP-logo.php" class="navbar-logo" width="60" height="60">
</div>
<div class="col ml-2">
<div class="ml-1 sb-status">Status</div>
<div class="info-item-xs"><span class="icon">
<i class="fas fa-circle <?php echo ($hostapd_led); ?>"></i></span> <?php echo _("Hotspot").' '. _($hostapd_status); ?>
</div>
<div class="info-item-xs"><span class="icon">
<i class="fas fa-circle <?php echo ($memused_led); ?>"></i></span> <?php echo _("Mem Use").': '. htmlspecialchars(strval($memused), ENT_QUOTES); ?>%
</div>
<div class="info-item-xs"><span class="icon">
<i class="fas fa-circle <?php echo ($cputemp_led); ?>"></i></span> <?php echo _("CPU").': '. htmlspecialchars($cputemp, ENT_QUOTES); ?>°C
</div>
</div>
</div>
<?php
}

33 changes: 26 additions & 7 deletions includes/page_actions.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
<?php
$extraFooterScripts = array();
$page = $_SERVER['PATH_INFO'];
// handle page actions
switch ($page) {
<?php

$pluginManager = \RaspAP\Plugins\PluginManager::getInstance();

// Get the requested page
$extraFooterScripts = array();
$page = $_SERVER['PATH_INFO'];

// Check if any plugin wants to handle the request
if (!$pluginManager->handlePageAction($page)) {
// If no plugin is available fall back to core page action handlers
handleCorePageAction($page, $extraFooterScripts);
}

/**
* Core application page handling
*
* @param string $page
* @param array $extraFooterScripts
* @return void
*/
function handleCorePageAction(string $page, array &$extraFooterScripts): void
{
switch ($page) {
case "/wlan0_info":
DisplayDashboard($extraFooterScripts);
break;
Expand Down Expand Up @@ -53,6 +72,6 @@
break;
default:
DisplayDashboard($extraFooterScripts);
}
?>
}
}

102 changes: 14 additions & 88 deletions includes/sidebar.php
Original file line number Diff line number Diff line change
@@ -1,89 +1,15 @@
<div class="row g-0">
<div class="col-4 ms-2 sidebar-brand-icon">
<img src="app/img/raspAP-logo.php" class="navbar-logo" width="60" height="60">
</div>
<div class="col ml-2">
<div class="ml-1 sb-status">Status</div>
<div class="info-item-xs"><span class="icon">
<i class="fas fa-circle <?php echo ($hostapd_led); ?>"></i></span> <?php echo _("Hotspot").' '. _($hostapd_status); ?>
</div>
<div class="info-item-xs"><span class="icon">
<i class="fas fa-circle <?php echo ($memused_led); ?>"></i></span> <?php echo _("Mem Use").': '. htmlspecialchars(strval($memused), ENT_QUOTES); ?>%
</div>
<div class="info-item-xs"><span class="icon">
<i class="fas fa-circle <?php echo ($cputemp_led); ?>"></i></span> <?php echo _("CPU").': '. htmlspecialchars($cputemp, ENT_QUOTES); ?>°C
</div>
</div>
</div>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="wlan0_info"><i class="sb-nav-link-icon fa-solid fa-gauge-high fa-fw mr-2"></i><span class="nav-label"><?php echo _("Dashboard"); ?></span></a>
</div>
<?php if (RASPI_HOTSPOT_ENABLED) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="hostapd_conf"><i class="sb-nav-link-icon far fa-dot-circle fa-fw mr-2"></i><span class="nav-label"><?php echo _("Hotspot"); ?></a>
</div>
<?php endif; ?>
<?php if (RASPI_DHCP_ENABLED && !$_SESSION["bridgedEnabled"]) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="dhcpd_conf"><i class="sb-nav-link-icon fas fa-exchange-alt fa-fw mr-2"></i><span class="nav-label"><?php echo _("DHCP Server"); ?></a>
</div>
<?php endif; ?>
<?php if (RASPI_ADBLOCK_ENABLED && !$_SESSION["bridgedEnabled"]) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="adblock_conf"><i class="sb-nav-link-icon far fa-hand-paper fa-fw mr-2"></i><span class="nav-label"><?php echo _("Ad Blocking"); ?></a>
</div>
<?php endif; ?>
<?php if (RASPI_NETWORK_ENABLED) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="network_conf"><i class="sb-nav-link-icon fas fa-network-wired fa-fw mr-2"></i><span class="nav-label"><?php echo _("Networking"); ?></a>
</div>
<?php endif; ?>
<?php if (RASPI_WIFICLIENT_ENABLED && !$_SESSION["bridgedEnabled"]) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="wpa_conf"><i class="sb-nav-link-icon fas fa-wifi fa-fw mr-2"></i><span class="nav-label"><?php echo _("WiFi client"); ?></span></a>
</div>
<?php endif; ?>
<?php if (RASPI_OPENVPN_ENABLED) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="openvpn_conf"><i class="sb-nav-link-icon fas fa-key fa-fw mr-2"></i><span class="nav-label"><?php echo _("OpenVPN"); ?></a>
</div>
<?php endif; ?>
<?php if (RASPI_WIREGUARD_ENABLED) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="wg_conf"><i class="sb-nav-link-icon ra-wireguard mr-2"></i><span class="nav-label"><?php echo _("WireGuard"); ?></a>
</div>
<?php endif; ?>
<?php if (RASPI_VPN_PROVIDER_ENABLED) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="provider_conf"><i class="sb-nav-link-icon fas fa-shield-alt fa-fw mr-2"></i><span class="nav-label"><?php echo _(getProviderValue($_SESSION["providerID"], "name")); ?></a>
</div>
<?php endif; ?>
<?php if (RASPI_TORPROXY_ENABLED) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="torproxy_conf"><i class="sb-nav-link-icon fas fa-eye-slash fa-fw mr-2"></i><span class="nav-label"><?php echo _("TOR proxy"); ?></a>
</div>
<?php endif; ?>
<?php if (RASPI_CONFAUTH_ENABLED) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="auth_conf"><i class="sb-nav-link-icon fas fa-user-lock fa-fw mr-2"></i><span class="nav-label"><?php echo _("Authentication"); ?></a>
</div>
<?php endif; ?>
<?php if (RASPI_VNSTAT_ENABLED) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="data_use"><i class="sb-nav-link-icon fas fa-chart-area fa-fw mr-2"></i><span class="nav-label"><?php echo _("Data usage"); ?></a>
</div>
<?php endif; ?>
<?php if (RASPI_RESTAPI_ENABLED) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="restapi_conf"><i class="sb-nav-link-icon fas fa-puzzle-piece mr-2"></i><span class="nav-label"><?php echo _("RestAPI"); ?></a>
</div>
<?php endif; ?>
<?php if (RASPI_SYSTEM_ENABLED) : ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="system_info"><i class="sb-nav-link-icon fas fa-cube fa-fw mr-2"></i><span class="nav-label"><?php echo _("System"); ?></a>
</div>
<?php endif; ?>
<div class="sb-nav-link-icon px-2">
<a class="nav-link" href="about"><i class="sb-nav-link-icon fas fa-info-circle fa-fw mr-2"></i><span class="nav-label"><?php echo _("About RaspAP"); ?></a>
</div>
<?php

use RaspAP\Plugins\PluginManager;

$pluginManager = PluginManager::getInstance();

// Display logo and status LEDs
renderStatus($hostapd_led, $hostapd_status,
$memused_led, $memused,
$cputemp_led, $cputemp);

// Render sidebar via the PluginManager
$sidebar = $pluginManager->getSidebar();
$sidebar->render();

50 changes: 0 additions & 50 deletions includes/torproxy.php

This file was deleted.

3 changes: 2 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
require_once 'includes/defaults.php';
require_once 'includes/locale.php';
require_once 'includes/functions.php';

// Default page actions
require_once 'includes/dashboard.php';
require_once 'includes/authenticate.php';
require_once 'includes/admin.php';
Expand All @@ -48,7 +50,6 @@
require_once 'includes/wireguard.php';
require_once 'includes/provider.php';
require_once 'includes/restapi.php';
require_once 'includes/torproxy.php';

initializeApp();
?>
Expand Down
34 changes: 34 additions & 0 deletions src/RaspAP/Plugins/PluginInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Plugin Interface
*
* @description Basic plugin interface for RaspAP
* @author Bill Zimmerman <billzimmerman@gmail.com>
* @license https://github.com/raspap/raspap-webgui/blob/master/LICENSE
* @see
*/

declare(strict_types=1);

namespace RaspAP\Plugins;

use RaspAP\UI\Sidebar;

interface PluginInterface
{
/**
* Initialize the plugin
* @param Sidebar $sidebar Sidebar instance for adding items
*/
public function initialize(Sidebar $sidebar): void;

/**
* Render a template within the plugin's template directory
* @param string $templateName
* @param array $__data
* @return string
*/
public function renderTemplate(string $templateName, array $__data = []): string;
}

Loading

0 comments on commit b74db94

Please sign in to comment.