df4e2dddec
Portable occ-based config, OCS endpoints for APK registration, notification relay, and server-side push dispatch.
81 lines
2.3 KiB
PHP
81 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\F7Push\Service;
|
|
|
|
use OCP\IConfig;
|
|
use OCP\IURLGenerator;
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
class ConfigService {
|
|
public const APP_ID = 'f7push';
|
|
|
|
public function __construct(
|
|
private IConfig $config,
|
|
private IURLGenerator $urlGenerator,
|
|
private ISecureRandom $secureRandom,
|
|
) {
|
|
}
|
|
|
|
public function isEnabled(): bool {
|
|
return $this->config->getAppValue(self::APP_ID, 'enabled', 'yes') === 'yes';
|
|
}
|
|
|
|
public function listenNotifications(): bool {
|
|
return $this->config->getAppValue(self::APP_ID, 'listen_notifications', 'yes') === 'yes';
|
|
}
|
|
|
|
public function getFirebaseProjectId(): string {
|
|
return trim($this->config->getAppValue(self::APP_ID, 'firebase_project_id', ''));
|
|
}
|
|
|
|
public function getFirebaseCredentials(): string {
|
|
return trim($this->config->getAppValue(self::APP_ID, 'firebase_credentials', ''));
|
|
}
|
|
|
|
public function isFirebaseConfigured(): bool {
|
|
return $this->getFirebaseProjectId() !== '' && $this->getFirebaseCredentials() !== '';
|
|
}
|
|
|
|
public function getApiSecret(): string {
|
|
$secret = trim($this->config->getAppValue(self::APP_ID, 'api_secret', ''));
|
|
if ($secret === '') {
|
|
$secret = $this->secureRandom->generate(48, ISecureRandom::CHAR_ALPHANUMERIC);
|
|
$this->config->setAppValue(self::APP_ID, 'api_secret', $secret);
|
|
}
|
|
return $secret;
|
|
}
|
|
|
|
public function regenerateApiSecret(): string {
|
|
$secret = $this->secureRandom->generate(48, ISecureRandom::CHAR_ALPHANUMERIC);
|
|
$this->config->setAppValue(self::APP_ID, 'api_secret', $secret);
|
|
return $secret;
|
|
}
|
|
|
|
public function isSourceEnabled(string $source): bool {
|
|
$raw = $this->config->getAppValue(self::APP_ID, 'enabled_sources', '*');
|
|
$raw = trim($raw);
|
|
if ($raw === '' || $raw === '*') {
|
|
return true;
|
|
}
|
|
$parts = array_map('trim', explode(',', strtolower($raw)));
|
|
return in_array(strtolower($source), $parts, true);
|
|
}
|
|
|
|
public function getDefaultClickUrl(): string {
|
|
$configured = trim($this->config->getAppValue(self::APP_ID, 'default_click_url', ''));
|
|
if ($configured !== '') {
|
|
return rtrim($configured, '/');
|
|
}
|
|
return rtrim($this->urlGenerator->getAbsoluteURL(''), '/');
|
|
}
|
|
|
|
public function verifyApiSecret(?string $provided): bool {
|
|
if ($provided === null || $provided === '') {
|
|
return false;
|
|
}
|
|
return hash_equals($this->getApiSecret(), $provided);
|
|
}
|
|
}
|