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

93 lines
3.1 KiB
PHP
Raw Permalink 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
2023-08-02 20:04:03 +00:00
class Hooks
{
2023-06-17 11:09:00 +00:00
private IRootFolder $rootFolder;
private IDBConnection $dbConnection;
2023-08-02 20:04:03 +00:00
public function __construct(IRootFolder $rootFolder, IDBConnection $dbConnection)
{
2023-06-17 11:09:00 +00:00
$this->rootFolder = $rootFolder;
$this->dbConnection = $dbConnection;
}
2023-08-02 20:04:03 +00:00
public function register(): void
{
2023-06-17 11:09:00 +00:00
$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-08-02 20:04:03 +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
if (is_array($settings['array'])
2023-08-02 20:04:03 +00:00
&& array_key_exists('oc_appconfig', $settings['array'])
2023-06-16 19:31:46 +00:00
) {
$isJson = self::isJson($settings['array']['oc_appconfig']);
$user = Server::get(IUserSession::class)->getUser();
$userId = $user ? $user->getUID() : null;
2023-08-02 20:04:03 +00:00
2023-06-16 19:31:46 +00:00
/** @var array $array */
$array = ($isJson) ? json_decode((string) $settings['array']['oc_appconfig'], true) : $settings['array']['oc_appconfig'];
$array['filesReader'] = [
'enableEpub' => Server::get(IConfig::class)->getUserValue($userId, Application::APP_ID, 'epub_enable', true),
'enablePdf' => Server::get(IConfig::class)->getUserValue($userId, Application::APP_ID, 'pdf_enable', true),
'enableCbx' => Server::get(IConfig::class)->getUserValue($userId, 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-08-02 20:04:03 +00:00
protected function deleteFile(int $fileId): void
{
2023-06-17 11:09:00 +00:00
$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-08-02 20:04:03 +00:00
protected function deleteUser(string $userId): void
{
2023-06-17 11:09:00 +00:00
$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-08-02 20:04:03 +00:00
private static function isJson(mixed $string): bool
{
return is_string($string) && is_array(json_decode($string, true)) && JSON_ERROR_NONE == json_last_error();
2023-06-16 14:58:23 +00:00
}
2020-04-21 20:37:42 +00:00
}