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.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 21:45:26 +00:00
|
|
|
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 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
|
|
|
|
$isJson = self::isJson($settings['array']['oc_appconfig']);
|
|
|
|
$array = ($isJson) ? json_decode($settings['array']['oc_appconfig'], true) : $settings['array']['oc_appconfig'];
|
|
|
|
$array['filesReader']['enableEpub'] = Config::get('epub_enable', 'true');
|
|
|
|
$array['filesReader']['enablePdf'] = Config::get('pdf_enable', 'true');
|
|
|
|
$array['filesReader']['enableCbx'] = Config::get('cbx_enable', 'true');
|
|
|
|
$settings['array']['oc_appconfig'] = ($isJson) ? json_encode($array) : $array;
|
|
|
|
}
|
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
|
|
|
}
|