Skip to content

Commit

Permalink
Store which template was used for a 3rd party app so it can be tracke…
Browse files Browse the repository at this point in the history
…d when it changes
  • Loading branch information
austinwbest committed Nov 10, 2024
1 parent e0fb2c6 commit 85a26f5
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
7 changes: 5 additions & 2 deletions root/app/www/public/ajax/starr.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,12 @@
$endpoints[$val][] = $_POST['method-' . $id];
}

$fields = [];
$fields['name'] = $_POST['name'];
$fields['apikey'] = $_POST['apikey'];
$fields['starr_id'] = $_POST['starr_id'];
$fields['endpoints'] = $endpoints;
$fields['starr_id'] = intval($_POST['starr_id']);
$fields['endpoints'] = json_encode($endpoints, JSON_UNESCAPED_SLASHES);
$fields['template'] = $_POST['template'];

if ($_POST['id'] != 99) {
$error = $proxyDb->updateApp($_POST['id'], $fields);
Expand All @@ -230,6 +232,7 @@
$app = $proxyDb->getAppFromId($_POST['id'], $appsTable);
$app['endpoints'] = json_decode($app['endpoints'], true);
$app['endpoints'][$_POST['endpoint']][] = $_POST['method'];
$app['endpoints'] = json_encode($app['endpoints'], JSON_UNESCAPED_SLASHES);

$error = $proxyDb->updateApp($_POST['id'], $app);

Expand Down
4 changes: 2 additions & 2 deletions root/app/www/public/api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@
$fields = [
'name' => $request['name'],
'apikey' => $scopeKey,
'starr_id' => $starrApp['id'],
'endpoints' => $scopeAccess
'starr_id' => intval($starrApp['id']),
'endpoints' => json_encode($scopeAccess, JSON_UNESCAPED_SLASHES)
];
$error = $proxyDb->addApp($fields);

Expand Down
24 changes: 21 additions & 3 deletions root/app/www/public/classes/traits/Database/Apps.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@ public function getAppFromId($appId, $appsTable)

public function addApp($fields = [])
{
$fieldList = $valueList = '';

foreach ($fields as $field => $val) {
$val = str_equals_any($field, ['endpoints']) ? $val : $this->prepare($val);

$fieldList .= ($fieldList ? ', ' : '') . "`" . $field . "`";
$valueList .= ($valueList ? ', ' : '') . "'" . $val . "'";
}

$q = "INSERT INTO " . APPS_TABLE . "
(`name`, `apikey`, `endpoints`, `starr_id`)
(" . $fieldList . ")
VALUES
('" . $this->prepare($fields['name']) . "', '" . $this->prepare($fields['apikey']) . "', '" . json_encode($fields['endpoints']) . "', '" . intval($fields['starr_id']) . "')";
(" . $valueList . ")";
$this->query($q);

if ($this->error() != 'not an error') {
Expand All @@ -54,8 +63,17 @@ public function addApp($fields = [])

public function updateApp($appId, $fields = [])
{
$fieldList = '';

foreach ($fields as $field => $val) {
$val = str_equals_any($field, ['endpoints']) ? $val : $this->prepare($val);

$fieldList .= ($fieldList ? ', ' : '') . "`" . $field . "` = '" . $val . "'";
$fieldList .= ($fieldList ? ', ' : '') . "`" . $field . "` = '" . $val . "'";
}

$q = "UPDATE " . APPS_TABLE . "
SET name = '" . $this->prepare($fields['name']) . "', apikey = '" . $this->prepare($fields['apikey']) . "', endpoints = '" . json_encode($fields['endpoints']) . "', starr_id = '" . intval($fields['starr_id']) . "'
SET " . $fieldList . "
WHERE id = " . intval($appId);
$this->query($q);

Expand Down
18 changes: 16 additions & 2 deletions root/app/www/public/js/starr.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ function deleteStarr(starrId, app)
// -------------------------------------------------------------------------------------------
function openAppStarrAccess(app, id, clone = '')
{
loadingStart();

$.ajax({
type: 'POST',
url: 'ajax/starr.php',
Expand All @@ -88,6 +90,8 @@ function openAppStarrAccess(app, id, clone = '')
$('#access-template').select2({
theme: 'bootstrap-5'
});

loadingStop();
}
});
}
Expand All @@ -112,7 +116,15 @@ function saveAppStarrAccess(app, id)
return;
}

let params = '';
loadingStart();

let params = '&app=' + app;
params += '&name=' + $('#access-name').val();
params += '&apikey=' + $('#access-apikey').val();
params += '&id=' + id;
params += '&starr_id=' + $('#access-instance').val();
params += '&template=' + $('#access-template').val();

$.each($('[id^=endpoint-counter-]'), function() {
const counter = $(this).attr('id').replace('endpoint-counter-', '');
params += '&endpoint-' + counter + '=' + $(this).data('endpoint');
Expand All @@ -123,8 +135,10 @@ function saveAppStarrAccess(app, id)
$.ajax({
type: 'POST',
url: 'ajax/starr.php',
data: '&m=saveAppStarrAccess&app=' + app + '&name=' + $('#access-name').val() + '&apikey=' + $('#access-apikey').val() + '&id=' + id + '&starr_id=' + $('#access-instance').val() + params,
data: '&m=saveAppStarrAccess' + params,
success: function (resultData) {
loadingStop();

if (resultData) {
toast('App access', resultData, 'error');
return;
Expand Down
1 change: 0 additions & 1 deletion root/app/www/public/js/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ function applyTemplateOptions()
});
});

$('#access-template').select2('val', '0');
toast('Templates', 'The selected template access has been applied', 'info');
}
});
Expand Down
30 changes: 30 additions & 0 deletions root/app/www/public/migrations/004_save_app_template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
----------------------------------
------ Created: 111024 ------
------ Austin Best ------
----------------------------------
*/

//-- RESET THE LIST
$q = [];

//-- ALWAYS NEED TO BUMP THE MIGRATION ID
$q[] = "UPDATE " . SETTINGS_TABLE . "
SET value = '004'
WHERE name = 'migration'";

$q[] = "ALTER TABLE " . APPS_TABLE . " ADD template TEXT NULL";

foreach ($q as $query) {
logger(MIGRATION_LOG, '<span class="text-success">[Q]</span> ' . preg_replace('!\s+!', ' ', $query));

$proxyDb->query($query);

if ($proxyDb->error() != 'not an error') {
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> ' . $proxyDb->error(), 'error');
} else {
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> query applied!');
}
}
5 changes: 3 additions & 2 deletions root/app/www/public/pages/starr.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@
$accessApp['endpoints'] = json_decode($accessApp['endpoints'], true) ?: [];
$usage = $usageDb->getStarrAppUsage($accessApp['id']);

if (file_exists('templates/' . $app . '/' . strtolower($accessApp['name']) . '.json')) {
$templateEndpoints = getFile('templates/' . $app . '/' . strtolower($accessApp['name']) . '.json');
$templateFile = file_exists($accessApp['template']) ? $accessApp['template'] : str_replace('../', './', $accessApp['template']);
if (file_exists($templateFile)) {
$templateEndpoints = getFile($templateFile);
$template = ', Template: ' . count($templateEndpoints) . ' endpoint' . (count($templateEndpoints) == 1 ? '' : 's');
}
?>
Expand Down

0 comments on commit 85a26f5

Please sign in to comment.