This repository has been archived on 2024-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
epubreader/lib/Hooks.php

86 lines
3.1 KiB
PHP
Raw Normal View History

2020-04-21 20:37:42 +00:00
<?php
/**
* @author Frank de Lange
* @copyright 2017 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Epubreader;
2020-04-21 20:37:42 +00:00
2023-06-16 22:07:40 +00:00
use OCA\Epubreader\AppInfo\Application;
2023-06-22 11:41:48 +00:00
use OCA\Epubreader\Utility\Server;
2023-06-17 11:09:00 +00:00
use OCP\Files\IRootFolder;
use OCP\Files\Node;
2023-06-16 22:07:40 +00:00
use OCP\IConfig;
2023-06-16 14:58:23 +00:00
use OCP\IDBConnection;
2023-06-17 11:09:00 +00:00
use OCP\IUser;
2023-06-16 22:52:17 +00:00
use OCP\IUserSession;
2020-04-21 20:37:42 +00:00
class Hooks {
2023-06-17 11:09:00 +00:00
private IRootFolder $rootFolder;
private IDBConnection $dbConnection;
2023-06-18 00:06:54 +00:00
public function __construct(IRootFolder $rootFolder, IDBConnection $dbConnection) {
2023-06-17 11:09:00 +00:00
$this->rootFolder = $rootFolder;
$this->dbConnection = $dbConnection;
}
public function register(): void {
$this->rootFolder->listen('\OC\Files', 'preDelete', function (Node $node) {
$this->deleteFile($node->getId());
});
$this->rootFolder->listen('\OC\User', 'preDelete', function (IUser $user) {
$this->deleteUser($user->getUID());
});
}
2023-06-17 10:01:19 +00:00
public static function announce_settings(array $settings): void {
2023-06-16 14:58:23 +00:00
// Nextcloud encodes this as JSON, Owncloud does not (yet) (#75)
2023-06-16 22:07:40 +00:00
// TODO: remove this when Owncloud starts encoding oc_appconfig as JSON just like it already encodes most other properties
2023-06-16 22:52:17 +00:00
$user = Server::get(IUserSession::class)->getUser();
if ($user &&
2023-06-16 19:31:46 +00:00
is_array($settings['array']) &&
array_key_exists('oc_appconfig', $settings['array'])
) {
$isJson = self::isJson($settings['array']['oc_appconfig']);
/** @var array $array */
$array = ($isJson) ? json_decode((string) $settings['array']['oc_appconfig'], true) : $settings['array']['oc_appconfig'];
$array['filesReader'] = [
2023-06-17 22:55:37 +00:00
'enableEpub' => Server::get(IConfig::class)->getUserValue($user->getUID(), Application::APP_ID, 'epub_enable', true),
'enablePdf' => Server::get(IConfig::class)->getUserValue($user->getUID(), Application::APP_ID, 'pdf_enable', true),
'enableCbx' => Server::get(IConfig::class)->getUserValue($user->getUID(), Application::APP_ID, 'cbx_enable', true),
2023-06-16 19:31:46 +00:00
];
$settings['array']['oc_appconfig'] = ($isJson) ? json_encode($array) : $array;
}
2023-06-16 14:58:23 +00:00
}
2020-04-21 20:37:42 +00:00
2023-06-17 11:09:00 +00:00
protected function deleteFile(int $fileId): void {
$queryBuilder = $this->dbConnection->getQueryBuilder();
2023-06-16 14:58:23 +00:00
$queryBuilder->delete('reader_bookmarks')->where('file_id = file_id')->setParameter('file_id', $fileId);
2023-06-16 19:20:03 +00:00
$queryBuilder->executeStatement();
2020-04-21 20:37:42 +00:00
2023-06-17 11:09:00 +00:00
$queryBuilder = $this->dbConnection->getQueryBuilder();
2023-06-16 14:58:23 +00:00
$queryBuilder->delete('reader_prefs')->where('file_id = file_id')->setParameter('file_id', $fileId);
2023-06-16 19:20:03 +00:00
$queryBuilder->executeStatement();
2023-06-16 14:58:23 +00:00
}
2020-04-21 20:37:42 +00:00
2023-06-17 11:09:00 +00:00
protected function deleteUser(string $userId): void {
$queryBuilder = $this->dbConnection->getQueryBuilder();
2023-06-16 14:58:23 +00:00
$queryBuilder->delete('reader_bookmarks')->where('user_id = user_id')->setParameter('user_id', $userId);
2023-06-16 19:20:03 +00:00
$queryBuilder->executeStatement();
2020-04-21 20:37:42 +00:00
2023-06-17 11:09:00 +00:00
$queryBuilder = $this->dbConnection->getQueryBuilder();
2023-06-16 14:58:23 +00:00
$queryBuilder->delete('reader_prefs')->where('user_id = user_id')->setParameter('user_id', $userId);
2023-06-16 19:20:03 +00:00
$queryBuilder->executeStatement();
2023-06-16 14:58:23 +00:00
}
2020-04-21 20:37:42 +00:00
2023-06-22 11:54:41 +00:00
private static function isJson(mixed $string): bool {
2023-06-17 23:41:18 +00:00
return is_string($string) && is_array(json_decode($string, true)) && json_last_error() == JSON_ERROR_NONE;
2023-06-16 14:58:23 +00:00
}
2020-04-21 20:37:42 +00:00
}