Skip to content

Commit

Permalink
Implement notification support
Browse files Browse the repository at this point in the history
Tidy up the log file cleanup check
  • Loading branch information
austinwbest committed Nov 9, 2024
1 parent fe8bd8b commit b7d6281
Show file tree
Hide file tree
Showing 23 changed files with 642 additions and 196 deletions.
2 changes: 2 additions & 0 deletions root/app/www/public/ajax/logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
----------------------------------
*/

error_reporting(E_ERROR | E_PARSE);

if (!$_SESSION) {
session_start();
}
Expand Down
226 changes: 226 additions & 0 deletions root/app/www/public/ajax/notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?php

/*
----------------------------------
------ Created: 110924 ------
------ Austin Best ------
----------------------------------
*/

error_reporting(E_ERROR | E_PARSE);

if (!$_SESSION) {
session_start();
}

if (!$_SESSION['IN_UI']) {
exit('Invalid session, refresh the page');
}

require '../loader.php';

if ($_POST['m'] == 'openNotificationTriggers') {
$_POST['linkId'] = $_POST['linkId'] ?: 0;
$notificationPlatformTable = $proxyDb->getNotificationPlatforms();
$notificationTriggersTable = $proxyDb->getNotificationTriggers();
$notificationLinkTable = $proxyDb->getNotificationLinks();
$platformParameters = json_decode($notificationPlatformTable[$_POST['platformId']]['parameters'], true);
$platformName = $notifications->getNotificationPlatformNameFromId($_POST['platformId'], $notificationPlatformTable);
$linkRow = $notificationLinkTable[$_POST['linkId']];
$existingTriggers = $existingParameters = [];

if ($linkRow) {
$existingTriggers = $linkRow['trigger_ids'] ? json_decode($linkRow['trigger_ids'], true) : [];
$existingParameters = $linkRow['platform_parameters'] ? json_decode($linkRow['platform_parameters'], true) : [];
$existingName = $linkRow['name'];
}

?>
<div class="container">
<h3><?= $platformName ?></h3>
<div class="bg-primary rounded p-2">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th><input type="checkbox" class="form-check-input" onchange="$('.notification-trigger').prop('checked', $(this).prop('checked'))"></th>
<th width="25%">Trigger</th>
<th>Description</th>
<th>Event</th>
</tr>
</thead>
<tbody>
<?php
foreach ($notificationTriggersTable as $notificationTrigger) {
?>
<tr>
<td><input <?= in_array($notificationTrigger['id'], $existingTriggers) ? 'checked' : '' ?> type="checkbox" class="form-check-input notification-trigger" id="notificationTrigger-<?= $notificationTrigger['id'] ?>"></td>
<td><i class="far fa-bell text-light" style="cursor: pointer;" title="Send test notification" onclick="testNotify(<?= $linkRow['id'] ?>, '<?= $notificationTrigger['name'] ?>')"></i> <?= $notificationTrigger['label'] ?></td>
<td><?= $notificationTrigger['description'] ?></td>
<td><?= $notificationTrigger['event'] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Setting</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
Name <span class="ms-2 small-text text-danger">Required</span><br>
<span class="small-text">The name of this notification sender</span>
</td>
<td><input data-required="true" type="text" class="form-control" value="<?= $existingName ?: $platformName ?>" id="notificationPlatformParameter-name"></td>
</tr>
<?php
foreach ($platformParameters as $platformParameterField => $platformParameterData) {
?>
<tr>
<td width="50%"><?= $platformParameterData['label'] . ($platformParameterData['required'] ? '<span class="ms-2 small-text text-danger">Required</span>' : '') ?><br><span class="small-text"><?= $platformParameterData['description'] ?></span></td>
<td>
<?php
switch ($platformParameterData['type']) {
case 'text':
?><input <?= $platformParameterData['required'] ? 'data-required="true"' : '' ?> type="text" id="notificationPlatformParameter-<?= $platformParameterField ?>" class="form-control" value="<?= $existingParameters[$platformParameterField] ?>"><?php
break;
}
?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<hr>
<div class="text-center w-100">
<?php if ($linkRow) { ?>
<button class="btn btn-outline-success" onclick="saveNotification(<?= $_POST['platformId'] ?>, <?= $_POST['linkId'] ?>)">Save</button>
<button class="btn btn-outline-danger" onclick="deleteNotification(<?= $_POST['linkId'] ?>)">Remove</button>
<?php } else { ?>
<button class="btn btn-outline-success" onclick="addNotification(<?= $_POST['platformId'] ?>)">Add</button>
<?php } ?>
</div>
</div>
</div>
<?php
}

if ($_POST['m'] == 'addNotification') {
if (!$_POST['platformId']) {
$error = 'Missing required platform id';
}

if (!$error) {
$notificationPlatformTable = $proxyDb->getNotificationPlatforms();
$notificationTriggersTable = $proxyDb->getNotificationTriggers();
$notificationLinkTable = $proxyDb->getNotificationLinks();
$platformParameters = json_decode($notificationPlatformTable[$_POST['platformId']]['parameters'], true);
$platformName = $notifications->getNotificationPlatformNameFromId($_POST['platformId'], $notificationPlatformTable);

//-- CHECK FOR REQUIRED FIELDS
foreach ($platformParameters as $platformParameterField => $platformParameterData) {
if ($platformParameterData['required'] && !$_POST['notificationPlatformParameter-' . $platformParameterField]) {
$error = 'Missing required platform field: ' . $platformParameterData['label'];
break;
}
}

if (!$error) {
$triggerIds = $platformParameters = [];
$senderName = $platformName;

foreach ($_POST as $key => $val) {
if (str_contains($key, 'notificationTrigger-') && $val) {
$triggerIds[] = str_replace('notificationTrigger-', '', $key);
}

if (str_contains($key, 'notificationPlatformParameter-')) {
$field = str_replace('notificationPlatformParameter-', '', $key);

if ($field != 'name') {
$platformParameters[$field] = $val;
} else {
$senderName = $val;
}
}
}

$proxyDb->addNotificationLink($_POST['platformId'], $triggerIds, $platformParameters, $senderName);
}
}

echo json_encode(['error' => $error]);
}

if ($_POST['m'] == 'saveNotification') {
if (!$_POST['platformId']) {
$error = 'Missing required platform id';
}
if (!$_POST['linkId']) {
$error = 'Missing required link id';
}

if (!$error) {
$notificationPlatformTable = $proxyDb->getNotificationPlatforms();
$notificationTriggersTable = $proxyDb->getNotificationTriggers();
$notificationLinkTable = $proxyDb->getNotificationLinks();
$platformParameters = json_decode($notificationPlatformTable[$_POST['platformId']]['parameters'], true);
$platformName = $notifications->getNotificationPlatformNameFromId($_POST['platformId'], $notificationPlatformTable);

//-- CHECK FOR REQUIRED FIELDS
foreach ($platformParameters as $platformParameterField => $platformParameterData) {
if ($platformParameterData['required'] && !$_POST['notificationPlatformParameter-' . $platformParameterField]) {
$error = 'Missing required platform field: ' . $platformParameterData['label'];
break;
}
}

if (!$error) {
$triggerIds = $platformParameters = [];
$senderName = $platformName;

foreach ($_POST as $key => $val) {
if (str_contains($key, 'notificationTrigger-') && $val) {
$triggerIds[] = str_replace('notificationTrigger-', '', $key);
}

if (str_contains($key, 'notificationPlatformParameter-')) {
$field = str_replace('notificationPlatformParameter-', '', $key);

if ($field != 'name') {
$platformParameters[$field] = $val;
} else {
$senderName = $val;
}
}
}
$proxyDb->updateNotificationLink($_POST['linkId'], $triggerIds, $platformParameters, $senderName);
}
}

echo json_encode(['error' => $error]);
}

if ($_POST['m'] == 'deleteNotification') {
$proxyDb->deleteNotificationLink($_POST['linkId']);
}

if ($_POST['m'] == 'testNotify') {
$result = $error = '';
$test = $notifications->sendTestNotification($_POST['linkId'], $_POST['name']);

if ((is_array($test['result']) && $test['result']['code'] && $test['result']['code'] == 200) || ($test['code'] == 200)) {
$result = 'Test notification sent';
} else {
$error = 'Failed to send test notification. ' . (is_array($test['result']) ? $test['result']['result'] : $test['result']);
}

echo json_encode(['error' => $error, 'result' => $result]);
}
2 changes: 2 additions & 0 deletions root/app/www/public/ajax/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
----------------------------------
*/

error_reporting(E_ERROR | E_PARSE);

if (!$_SESSION) {
session_start();
}
Expand Down
2 changes: 2 additions & 0 deletions root/app/www/public/ajax/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
----------------------------------
*/

error_reporting(E_ERROR | E_PARSE);

if (!$_SESSION) {
session_start();
}
Expand Down
23 changes: 23 additions & 0 deletions root/app/www/public/api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@
logger($logfile, $apikey, $endpoint, 401);
logger(str_replace('access.log', 'access_' . $proxiedApp['proxiedAppDetails']['name'] . '.log', $logfile), $apikey, $endpoint, 401);
$usageDb->adjustAppUsage($proxiedApp['proxiedAppDetails']['id'], 401);

if ($proxyDb->isNotificationTriggerEnabled('blocked')) {
$payload = [
'event' => 'blocked',
'proxyApp' => $proxiedApp['proxiedAppDetails']['name'],
'starrApp' => $proxiedApp['starrAppDetails']['name'],
'endpoint' => $endpoint
];
$notifications->notify(0, 'blocked', $payload);
}

apiResponse(401, ['error' => sprintf(APP_API_ERROR, 'provided apikey is missing access to ' . $endpoint)]);
}
}
Expand All @@ -215,6 +226,18 @@
logger($logfile, $apikey, $endpoint, 405);
logger(str_replace('access.log', 'access_' . $proxiedApp['proxiedAppDetails']['name'] . '.log', $logfile), $apikey, $endpoint, 405);
$usageDb->adjustAppUsage($proxiedApp['proxiedAppDetails']['id'], 405);

if ($proxyDb->isNotificationTriggerEnabled('blocked')) {
$payload = [
'event' => 'blocked',
'proxyApp' => $proxiedApp['proxiedAppDetails']['name'],
'starrApp' => $proxiedApp['starrAppDetails']['name'],
'endpoint' => $endpoint,
'method' => $method
];
$notifications->notify(0, 'blocked', $payload);
}

apiResponse(405, ['error' => sprintf(APP_API_ERROR, 'provided apikey is missing access to ' . $endpoint . ' using the ' . $method . ' method')]);
}

Expand Down
2 changes: 1 addition & 1 deletion root/app/www/public/classes/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function sendTestNotification($linkId, $name)
logger($logfile, 'test payload=' . json_encode($tests[$name]));

$result = $this->notify($linkId, $name, $tests[$name], true);

if ($result['code'] != 200) {
$return = 'Code ' . $result['code'] . ', ' . $result['error'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getNotificationLinks()
}

$this->notificationLinkTable = $notificationLinkTable;
return $notificationLinkTable;
return $notificationLinkTable ?: [];
}

public function updateNotificationLink($linkId, $triggerIds, $platformParameters, $senderName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public function getNotificationPlatforms()
}

$this->notificationPlatformTable = $notificationPlatformTable;
return $notificationPlatformTable;
return $notificationPlatformTable ?: [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getNotificationTriggers()
}

$this->notificationTriggersTable = $notificationTriggersTable;
return $notificationTriggersTable;
return $notificationTriggersTable ?: [];
}

public function getNotificationTriggerFromName($name)
Expand Down
15 changes: 11 additions & 4 deletions root/app/www/public/classes/traits/Notifications/Notifiarr.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,28 @@ trait Notifiarr
public function notifiarr($logfile, $apikey, $payload, $test = false)
{
$headers = ['x-api-key:' . $apikey];
$url = 'https://notifiarr.com/api/v1/notification/dockwatch';
$url = 'https://notifiarr.com/api/v1/notification/starrproxy';
$curl = curl($url, $headers, 'POST', json_encode($payload));

logger($logfile, 'notification response:' . json_encode($curl), ($curl['code'] != 200 ? 'error' : ''));
logger($logfile, 'notification response:' . json_encode($curl));

$return = ['code' => 200];

if ($curl['code'] != 200) {
$error = is_array($curl['response']) && $curl['response']['details'] && $curl['response']['details']['response'] ? $curl['response']['details']['response'] : 'Unknown error';
$return = ['code' => $curl['code'], 'error' => $error];
}

if (!str_equals_any($curl['code'], [200, 201, 400, 401])) {
logger($logfile, 'sending a retry in 5s...');
sleep(5);

$curl = curl($url, $headers, 'POST', json_encode($payload));
logger($logfile, 'notification response:' . json_encode($curl), ($curl['code'] != 200 ? 'error' : ''));
logger($logfile, 'notification response:' . json_encode($curl));

if ($curl['code'] != 200) {
$return = ['code' => $curl['code'], 'error' => $curl['response']['details']['response']];
$error = is_array($curl['response']) && $curl['response']['details'] && $curl['response']['details']['response'] ? $curl['response']['details']['response'] : 'Unknown error';
$return = ['code' => $curl['code'], 'error' => $error];
}
}

Expand Down
Loading

0 comments on commit b7d6281

Please sign in to comment.