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

79 lines
2.9 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 14:58:23 +00:00
use \OC\User\User as User;
2020-04-21 20:37:42 +00:00
use OCP\Files\Node;
2023-06-16 14:58:23 +00:00
use OCP\IDBConnection;
2020-04-21 20:37:42 +00:00
use OCP\Util;
class Hooks {
2023-06-16 19:20:03 +00:00
public static function register(): void {
2023-06-16 19:31:46 +00:00
/** @psalm-suppress DeprecatedMethod */
2023-06-16 14:58:23 +00:00
Util::connectHook('\OCP\Config', 'js', 'OCA\Epubreader\Hooks', 'announce_settings');
2020-04-21 20:37:42 +00:00
2023-06-16 14:58:23 +00:00
\OC::$server->getRootFolder()->listen('\OC\Files', 'preDelete', function (Node $node) {
$fileId = $node->getId();
$connection = \OC::$server->getDatabaseConnection();
self::deleteFile($connection, $fileId);
});
\OC::$server->getUserManager()->listen('\OC\User', 'preDelete', function (User $user) {
$userId = $user->getUID();
$connection = \OC::$server->getDatabaseConnection();
self::deleteUser($connection, $userId);
});
}
2020-04-21 20:37:42 +00:00
2023-06-16 19:20: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)
// TODO: rmeove this when Owncloud starts encoding oc_appconfig as JSON just like it already encodes most other properties
2023-06-16 19:31:46 +00:00
if (array_key_exists('array', $settings) &&
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'] = [
'enableEpub' => Config::get('epub_enable', 'true'),
'enablePdf' => Config::get('pdf_enable', 'true'),
'enableCbx' => Config::get('cbx_enable', 'true'),
];
$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-16 19:20:03 +00:00
protected static function deleteFile(IDBConnection $connection, int $fileId): void {
2023-06-16 14:58:23 +00:00
$queryBuilder = $connection->getQueryBuilder();
$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-16 14:58:23 +00:00
$queryBuilder = $connection->getQueryBuilder();
$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-16 19:20:03 +00:00
protected static function deleteUser(IDBConnection $connection, string $userId): void {
2023-06-16 14:58:23 +00:00
$queryBuilder = $connection->getQueryBuilder();
$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-16 14:58:23 +00:00
$queryBuilder = $connection->getQueryBuilder();
$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-16 19:20:03 +00:00
private static function isJson(mixed $string): bool {
2023-06-16 14:58:23 +00:00
return is_string($string) && is_array(json_decode($string, true)) && (json_last_error() == JSON_ERROR_NONE) ? true : false;
}
2020-04-21 20:37:42 +00:00
}