nextcloud-swarm-plugin/lib/Settings/Admin.php
Mahyar Iranibazaz a3a0b0582e
pay-wall (#60)
* add env for swarm access

* check access

* use storage config from external storage to set access key

* Update README.md (#50)

updated settings menu in readme

* feat(intellisense): (#51)

- update: remap the docker volume for main nextcloud to local project directory to access the nc code

* add env for swarm access

* feat(hooks):
- add: entrypoint hooks volume
- add: change-owner.sh script

* feat(apache):
- update: Dockerfile apache user and id
- delete: hooks setup
- add: .env setup
- add: theme volume
- update: turn off debugger at start

* feat(.env):
- add: .env example file

* fix(docker):
- add: theme volume

* fix(docker):
- revert: xdebug start config

* backend options not persistant

* access key works

* feat(docker):
- update: reduce the volumes to lib and apps

* chore(env):
- add: swarm api key in example

* chore(env):
- add: env file instructions
- fix: typos

* docs(readme):
- fix: image path typo

* merge(docker):
- fix: revert the dev-env folder volumes

* chore(main):
- add: curl ext to project requirements

* feat(util): curl
- add: curl helper

* feat(swarm): curl
- update: request to use new Curl helper
- update: check connection using new endpoint of gateway
- update: type safety
- refactor: remove unused methods

* refactor(swarm):
- refactor: upload and download methods
- update: code style

* feat(pay-wall): licence
- add: License auth implementation
- update: external storage definition
- remove: nocodb access token setup
- remove: basic auth setup

* feat(swarm):
- update: disable custom settings page

* feat(gitignore):
- update: categorize gitignore
- add: DS_Store entry =)

* refactor(gitignore):
- update: url check using regex

* fix(swarm): upload
- update: remove extra param

* fix(swarm): file preview
- update: write file to memory to stream

* feat(settings): add link
- add: script to display link on settings page
- update: load script on plugin boot

---------

Co-authored-by: Henry Bergstrom <henrybergstrom@protonmail.com>
Co-authored-by: rampall <rameshpallikara@gmail.com>
2024-08-12 16:50:32 +02:00

236 lines
6.8 KiB
PHP
Executable file

<?php
/**
* @copyright Copyright (c) 2022, MetaProvide Holding EKF
*
* @author Ron Trevor <ecoron@proton.me>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Files_External_Ethswarm\Settings;
use OCA\Files_External\Service\BackendService;
use OCA\Files_External\Service\GlobalStoragesService;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\Settings\ISettings;
use OCP\Util;
class Admin implements ISettings {
/** @var string */
protected $appName;
/** @var IConfig */
private $config;
/** @var GlobalStoragesService */
private $globalStoragesService;
public function __construct($appName,
IConfig $config,
GlobalStoragesService $globalStoragesService) {
$this->appName = $appName;
$this->config = $config;
$this->globalStoragesService = $globalStoragesService;
}
/**
* @return TemplateResponse
*/
public function getForm(): TemplateResponse {
// Get all valid Beeswarm storages
$temp = $this->globalStoragesService->getStorages();
$storageBackends = array_filter($this->globalStoragesService->getStorages(), function ($storageBackend) {
return $storageBackend->getBackend()->getStorageClass() == '\OCA\Files_External_Ethswarm\Storage\BeeSwarm';
});
// Get Mount settings
$mounts_json = $this->config->getAppValue($this->appName,"storageconfig",""); //default
$mounts = json_decode($mounts_json, true);
if (!is_array($mounts)) {
$mounts = [];
}
// remove configurations that are no longer in Storages
foreach ($mounts as $key => $mount) {
$eachmountId = $mount['mount_id'];
if (!isset($storageBackends[$eachmountId])) {
// remove by key index
unset($mounts[$key]);
}
}
$mountIds = array_column($mounts, 'mount_id');
foreach ($storageBackends as $backend) {
$backendId = $backend->getId();
$backendName = $backend->getMountPoint();
$urlOptions = $backend->getBackendOptions();
$batchId = "";
$encrypt = 1;
// Is it configured?
$key = array_search($backendId, $mountIds);
$isConfigured = !empty($key) || $key === 0;
if ($isConfigured) {
$encrypt = isset($mounts[$key]['encrypt']) ? $mounts[$key]['encrypt'] : 0;
$batchId = isset($mounts[$key]['batchid']) ? $mounts[$key]['batchid'] : "";
}
$batches = $this->getBatches($urlOptions);
if ($batches === null) {
$batches["stamps"] = [];
}
$batcharray = [];
foreach ($batches["stamps"] as $batch) {
$newbatch = [
"batchID" => $batch["batchID"],
"amount" => $batch["amount"],
"batchTTL" => $batch["batchTTL"],
"isActive" => ($batch["batchID"] === $batchId ? true : false),
"isDisabled" => ($batch["batchID"] === $batchId ? true : false),
"isUsable" => ($batch["usable"]),
];
$batcharray[] = $newbatch;
}
$chequeBalance = $this->getChequebookBalance($urlOptions);
$newMounts[] = ['mount_id' => $backendId, 'mount_name' => $backendName, 'mount_urloptions' => $urlOptions, 'encrypt' => $encrypt, 'batchid' => $batchId, 'chequebalance' => $chequeBalance, 'batches' => $batcharray];
unset($batcharray);
}
$parameters = [
'visibilityType' => BackendService::VISIBILITY_ADMIN,
'mounts' => json_encode($newMounts),
];
Util::addScript($this->appName, 'nextcloud-swarm-plugin-main');
$response = new TemplateResponse($this->appName, 'vue-admin-settings', ['params' => $parameters], '');
return $response;
}
/**
* @return string the section ID, e.g. 'sharing'
*/
public function getSection(): string {
return ""; // disable the settings page
return $this->appName;
}
/**
* @return int whether the form should be rather on the top or bottom of
* the admin section. The forms are arranged in ascending order of the
* priority values. It is required to return a value between 0 and 100.
*
* E.g.: 70
*/
public function getPriority(): int {
return 5;
}
public function getBatches($urlOptions) {
$uri = "/stamps";
$curl = $this->setCurl($uri, $urlOptions);
$output = curl_exec($curl);
$response_data = json_decode($output, true);
curl_close($curl);
return $response_data;
}
public function getBatchBalance(string $batchId, $urlOptions) {
$uri = "/stamps/" . $batchId;
$curl = $this->setCurl($uri, $urlOptions);
$output = curl_exec($curl);
$response_data = json_decode($output, true);
curl_close($curl);
if (isset($response_data["batchTTL"])) {
return $response_data["batchTTL"];
} elseif (isset($response_data["message"])) {
return $response_data["message"];
}
return null;
}
public function getChequebookBalance($urlOptions) {
$uri = "/chequebook/balance";
$curl = $this->setCurl($uri, $urlOptions);
$output = curl_exec($curl);
$response_data = json_decode($output, true);
curl_close($curl);
if (isset($response_data["totalBalance"])) {
return $response_data["totalBalance"];
} elseif (isset($response_data["message"])) {
return $response_data["message"];
}
return null;
}
/**
* Buys a new postage batch method by using the client libary handler
* @return \mixed
*/
public function buyPostageStamp($amount, $depth, $urlOptions) : mixed {
$uri = "/stamps/" . $amount . "/" . $depth;
$curl = $this->setCurl($uri, $urlOptions);
curl_setopt($curl, CURLOPT_POST, 1);
$output = curl_exec($curl);
$response_data = json_decode($output, true);
curl_close($curl);
return $response_data;
}
/**
* Top up a postage batch by using the client libary handler
* @return \mixed
*/
public function topUpPostageStamp($batchid, $amount, $urlOptions) : mixed {
$uri = "/stamps/topup/" . $batchid . "/" . $amount;
$curl = $this->setCurl($uri, $urlOptions);
curl_setopt($curl, CURLOPT_POST, 1);
$output = curl_exec($curl);
$response_data = json_decode($output, true);
curl_close($curl);
return $response_data;
}
/**
* initializes a curl handler
* @return \CurlHandle
*/
protected function setCurl(string $uri_params, array $urlOptions, $useDebugApi = true) : \CurlHandle {
$url_endpoint = $urlOptions['ip'] . ":" . ($useDebugApi ? $urlOptions['debug_api_port'] : $urlOptions['port']);
$url_endpoint .= $uri_params;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url_endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
return $curl;
}
}