2018-09-20 07:42:52 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nextcloud - Social Support
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @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\Social\Service;
|
|
|
|
|
2018-11-15 11:26:25 +00:00
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools;
|
2018-11-12 22:57:32 +00:00
|
|
|
use daita\MySmallPhpTools\Traits\TPathTools;
|
2018-12-05 18:37:49 +00:00
|
|
|
use OC\IntegrityCheck\Helpers\AppLocator;
|
|
|
|
use OCA\Files\App;
|
2018-09-20 07:42:52 +00:00
|
|
|
use OCA\Social\AppInfo\Application;
|
2018-11-15 11:26:25 +00:00
|
|
|
use OCA\Social\Exceptions\SocialAppConfigException;
|
2018-09-20 07:42:52 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IRequest;
|
2018-11-12 22:57:32 +00:00
|
|
|
use OCP\IURLGenerator;
|
2018-09-20 07:42:52 +00:00
|
|
|
use OCP\PreConditionNotMetException;
|
|
|
|
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
/**
|
|
|
|
* Class ConfigService
|
|
|
|
*
|
|
|
|
* @package OCA\Social\Service
|
|
|
|
*/
|
2018-09-20 07:42:52 +00:00
|
|
|
class ConfigService {
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
|
|
|
|
use TPathTools;
|
2018-11-15 11:26:25 +00:00
|
|
|
use TArrayTools;
|
2018-11-12 22:57:32 +00:00
|
|
|
|
|
|
|
|
2018-11-15 11:26:25 +00:00
|
|
|
const SOCIAL_ADDRESS = 'address';
|
2018-11-27 16:59:19 +00:00
|
|
|
const SOCIAL_SERVICE = 'service';
|
2018-11-25 12:50:12 +00:00
|
|
|
const SOCIAL_MAX_SIZE = 'max_size';
|
2018-11-15 11:26:25 +00:00
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
const BACKGROUND_CRON = 1;
|
|
|
|
const BACKGROUND_ASYNC = 2;
|
|
|
|
const BACKGROUND_SERVICE = 3;
|
|
|
|
const BACKGROUND_FULL_SERVICE = 4;
|
|
|
|
|
2018-09-20 07:42:52 +00:00
|
|
|
/** @var array */
|
|
|
|
public $defaults = [
|
2018-11-25 12:50:12 +00:00
|
|
|
self::SOCIAL_ADDRESS => '',
|
2018-11-27 16:59:19 +00:00
|
|
|
self::SOCIAL_SERVICE => 1,
|
2018-11-25 12:50:12 +00:00
|
|
|
self::SOCIAL_MAX_SIZE => 25
|
2018-09-20 07:42:52 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $userId;
|
|
|
|
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/** @var IRequest */
|
|
|
|
private $request;
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
|
2018-09-20 07:42:52 +00:00
|
|
|
/** @var MiscService */
|
|
|
|
private $miscService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConfigService constructor.
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param IRequest $request
|
2018-11-12 22:57:32 +00:00
|
|
|
* @param IURLGenerator $urlGenerator
|
2018-09-20 07:42:52 +00:00
|
|
|
* @param MiscService $miscService
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2018-11-12 22:57:32 +00:00
|
|
|
$userId, IConfig $config, IRequest $request, IURLGenerator $urlGenerator,
|
2018-09-28 11:41:24 +00:00
|
|
|
MiscService $miscService
|
2018-09-20 07:42:52 +00:00
|
|
|
) {
|
|
|
|
$this->userId = $userId;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->request = $request;
|
2018-11-12 22:57:32 +00:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2018-09-20 07:42:52 +00:00
|
|
|
$this->miscService = $miscService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a value by key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAppValue($key) {
|
|
|
|
$defaultValue = null;
|
|
|
|
if (array_key_exists($key, $this->defaults)) {
|
|
|
|
$defaultValue = $this->defaults[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
|
|
|
|
}
|
|
|
|
|
2018-11-25 12:50:12 +00:00
|
|
|
/**
|
|
|
|
* Get a value by key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getAppValueInt(string $key): int {
|
|
|
|
$defaultValue = null;
|
|
|
|
if (array_key_exists($key, $this->defaults)) {
|
|
|
|
$defaultValue = $this->defaults[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)$this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
|
|
|
|
}
|
|
|
|
|
2018-09-20 07:42:52 +00:00
|
|
|
/**
|
|
|
|
* Set a value by key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setAppValue($key, $value) {
|
|
|
|
$this->config->setAppValue(Application::APP_NAME, $key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove a key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function deleteAppValue($key) {
|
|
|
|
return $this->config->deleteAppValue(Application::APP_NAME, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a user value by key
|
|
|
|
*
|
|
|
|
* @param string $key
|
2018-12-05 18:37:49 +00:00
|
|
|
* @param string $userId
|
|
|
|
* @param string $app
|
2018-09-20 07:42:52 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-12-05 18:37:49 +00:00
|
|
|
public function getUserValue(string $key, string $userId = '', string $app = '') {
|
|
|
|
if ($userId === '') {
|
|
|
|
$userId = $this->userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
$defaultValue = '';
|
|
|
|
if ($app === '') {
|
|
|
|
$app = Application::APP_NAME;
|
|
|
|
if (array_key_exists($key, $this->defaults)) {
|
|
|
|
$defaultValue = $this->defaults[$key];
|
|
|
|
}
|
2018-09-20 07:42:52 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 18:37:49 +00:00
|
|
|
return $this->config->getUserValue($userId, $app, $key, $defaultValue);
|
2018-09-20 07:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a user value by key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws PreConditionNotMetException
|
|
|
|
*/
|
|
|
|
public function setUserValue($key, $value) {
|
|
|
|
return $this->config->setUserValue($this->userId, Application::APP_NAME, $key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a user value by key and user
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getValueForUser($userId, $key) {
|
|
|
|
return $this->config->getUserValue($userId, Application::APP_NAME, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a user value by key
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws PreConditionNotMetException
|
|
|
|
*/
|
|
|
|
public function setValueForUser($userId, $key, $value) {
|
|
|
|
return $this->config->setUserValue($userId, Application::APP_NAME, $key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
*/
|
|
|
|
public function setCoreValue(string $key, string $value) {
|
|
|
|
$this->config->setAppValue('core', $key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-20 07:42:52 +00:00
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getSystemValue($key) {
|
|
|
|
return $this->config->getSystemValue($key, '');
|
|
|
|
}
|
|
|
|
|
2018-11-19 20:54:26 +00:00
|
|
|
/**
|
2018-11-23 20:53:01 +00:00
|
|
|
* @param string $cloudAddress
|
2018-11-19 20:54:26 +00:00
|
|
|
*/
|
|
|
|
public function setCloudAddress(string $cloudAddress) {
|
|
|
|
$this->setAppValue(self::SOCIAL_ADDRESS, $cloudAddress);
|
|
|
|
}
|
2018-11-15 11:26:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $host
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws SocialAppConfigException
|
|
|
|
*/
|
|
|
|
public function getCloudAddress(bool $host = false) {
|
|
|
|
$address = $this->getAppValue(self::SOCIAL_ADDRESS);
|
|
|
|
if ($address === '') {
|
|
|
|
throw new SocialAppConfigException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($host === true) {
|
|
|
|
$parsed = parse_url($address);
|
|
|
|
$result = $this->get('host', $parsed, '');
|
|
|
|
$port = $this->get('port', $parsed, '');
|
|
|
|
// if ($port !== '') {
|
|
|
|
// $result .= ':' . $port;
|
|
|
|
// }
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-11-16 11:48:31 +00:00
|
|
|
return $this->withoutEndSlash($address, false, false);
|
2018-09-20 07:42:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
/**
|
2018-11-23 20:53:01 +00:00
|
|
|
* // TODO - check the Apps folder
|
2018-11-25 12:50:12 +00:00
|
|
|
*
|
2018-09-28 11:41:24 +00:00
|
|
|
* @return string
|
2018-11-15 11:26:25 +00:00
|
|
|
* @throws SocialAppConfigException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-11-23 20:53:01 +00:00
|
|
|
public function getUrlSocial(): string {
|
2018-11-16 11:48:31 +00:00
|
|
|
return $this->getCloudAddress() . '/apps/social/';
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param bool $generateId
|
|
|
|
*
|
|
|
|
* @return string
|
2018-11-15 11:26:25 +00:00
|
|
|
* @throws SocialAppConfigException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
|
|
|
public function generateId(string $path = '', $generateId = true): string {
|
2018-11-12 22:57:32 +00:00
|
|
|
$path = $this->withoutBeginSlash($this->withEndSlash($path));
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-11-23 20:53:01 +00:00
|
|
|
$id = $this->getUrlSocial() . $path;
|
2018-09-28 11:41:24 +00:00
|
|
|
if ($generateId === true) {
|
|
|
|
$id .= time() . crc32(uniqid());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
2018-09-20 07:42:52 +00:00
|
|
|
// /**
|
|
|
|
// * @return array
|
|
|
|
// */
|
|
|
|
// public function getServiceTypes(): array {
|
|
|
|
// return $this->serviceTypes;
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|