Skip to content

Commit

Permalink
persist preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
bennet0496 committed Feb 27, 2024
1 parent 0ff31ec commit f790e10
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 25 deletions.
23 changes: 22 additions & 1 deletion config.inc.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ $config["nextcloud_attachment_folder_layout"] = "flat";
// (Dis)Allow user to change the layout
// true => layout is locked, i.e. not user-changeable
// false => user may change the layout on their settings page
$config["nextcloud_attachment_folder_layout_lock"] = true;
$config["nextcloud_attachment_folder_layout_locked"] = true;

// Limit to show a warning at for large attachments.
// has to be smaller then $config['max_message_size']
Expand All @@ -65,6 +65,27 @@ $config["nextcloud_attachment_folder_translate_name"] = false;
// see https://www.php.net/manual/en/function.hash-algos.php
$config["nextcloud_attachment_checksum"] = "sha256";

// Generate Password protected link.
// Passwords will be included in the message body only, not in the HTML attachment
// Defaults to false
$config["nextcloud_attachment_password_protected_links"] = false;

// Allow/Disallow user to change the password protection option for themself
// true => user can not change the setting
// false => user may change the setting
$config["nextcloud_attachment_password_protected_links_locked"] = true;

// Expire share links after amount of days.
// Links will expire on the last valid day at 11:59:59pm AoE-Time
// Setting to "false" or negative value disables link expiry
// Defaults to false
$config["nextcloud_attachment_expire_links"] = false;

// Allow/Disallow user to change the link expiry option for themself
// true => user can not change the setting
// false => user may change the setting
$config["nextcloud_attachment_expire_links_locked"] = true;

// List of users to exclude
// They won't be able to interact with the plugin at all
// No prompts, no automatic uploading, no settings.
Expand Down
92 changes: 69 additions & 23 deletions hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function add_preferences(array $param): array
{
$prefs = $this->rcmail->user->get_prefs();

self::log($prefs);

$server = $this->rcmail->config->get(__("server"));
$blocks = $param["blocks"];

Expand All @@ -66,11 +68,13 @@ public function add_preferences(array $param): array
$tokens[1] ?? "Y/LLLL"
);

$layout_select = new \html_select(["id" => __("folder_layout"), "value" => $prefs[__("folder_layout")] ?? "default", "name" => "_".__("folder_layout")]);
$layout_select = new \html_select(["id" => __("folder_layout"), "value" => $prefs[__("user_folder_layout")] ?? "default", "name" => "_".__("folder_layout")]);
$pp_links = new \html_checkbox(["id" => __("password_protected_links"), "value" => "1", "name" => "_".__("password_protected_links")]);
$exp_links = new \html_checkbox(["id" => __("expire_links"), "value" => "1", "name" => "_".__("expire_links")]);

$server_format_tokens = explode(":", $this->rcmail->config->get(__("folder_layout", "flat")));
$server_format_tokens = explode(":", $this->rcmail->config->get(__("folder_layout"), "flat"));

self::log($server_format_tokens);

$server_format = match ($server_format_tokens[0]) {
"flat" => $this->gettext("folder_layout_flat"),
Expand Down Expand Up @@ -123,34 +127,76 @@ function ($method, $depth) {
$this->gettext("connected_as") . " " . $username . ($can_disconnect ? " (<a href=\"#\" onclick=\"rcmail.http_post('plugin.nextcloud_disconnect')\">" . $this->gettext("disconnect") . "</a>)" : "") :
$this->gettext("not_connected") . " (<a href=\"#\" onclick=\"window.rcmail.nextcloud_login_button_click_handler(null, null)\">" . $this->gettext("connect") . "</a>)"
],
"folder_layout" => [
"title" => $this->gettext("folder_layout"),
"content" => $layout_select->show()
],
"password_protected_links" => [
"title" => $this->gettext("password_protected_links"),
"content" => $pp_links->show($prefs[__("password_protected_links")] ?? "0")
],
"expire_links" => [
"title" => $this->gettext("expire_links"),
"content" => $exp_links->show($prefs[__("expire_links")] ?? "0")
],
"expire_links_after" => [
"title" => $this->gettext("expire_links_after"),
"content" => (new \html_inputfield([
"type" => "number",
"min" => "1",
"id" => __("expire_links_after"),
"name" => "_".__("expire_links_after"),
"value" => $prefs[__("expire_links_after")] ?? $this->rcmail->config->get(__("expire_links_after"), 7)]))->show()
]


]
];

if (!$this->rcmail->config->get("nextcloud_attachment_folder_layout_locked", true)) {
$blocks["plugin.nextcloud_attachments"]["options"]["folder_layout"] = [
"title" => $this->gettext("folder_layout"),
"content" => $layout_select->show()
];
}

if (!$this->rcmail->config->get("nextcloud_attachment_password_protected_links_locked", true)) {
$def = $this->rcmail->config->get("nextcloud_attachment_password_protected_links", false) ? "1" : "0";
$blocks["plugin.nextcloud_attachments"]["options"]["password_protected_links"] = [
"title" => $this->gettext("password_protected_links"),
"content" => $pp_links->show($prefs[__("user_password_protected_links")] ?? $def)
];
}

if (!$this->rcmail->config->get(__("expire_links_locked"), true)) {
$def = $this->rcmail->config->get(__("expire_links"), false);
$blocks["plugin.nextcloud_attachments"]["options"]["expire_links"] = [
"title" => $this->gettext("expire_links"),
"content" => $exp_links->show($prefs[__("user_expire_links")] ?? ($def === false ? "0" : "1"))
];
$blocks["plugin.nextcloud_attachments"]["options"]["expire_links_after"] = [
"title" => $this->gettext("expire_links_after"),
"content" => (new \html_inputfield([
"type" => "number",
"min" => "1",
"id" => __("expire_links_after"),
"name" => "_".__("expire_links_after"),
"value" => $prefs[__("user_expire_links_after")] ?? ($def !== false ? $def : 7)]))->show()
];
}
}

return ["blocks" => $blocks];
}

private function save_preferences(array $param) : array
{
$formats = ["default", "flat", "date:Y", "date:Y/LLLL", "date:Y/LLLL/dd", "date:Y/LL", "date:Y/LL/dd",
"date:Y/ww","date:Y/ww/EEEE","date:Y/ww/E", "hash:sha1:2", "hash:sha1:3", "hash:sha1:4", "hash:sha1:5"];
$folder_layout = $_POST["_".__("folder_layout")] ?? null;
if (!$this->rcmail->config->get("nextcloud_attachment_folder_layout_locked", true) &&
in_array($folder_layout, $formats)) {
$param["prefs"][__("user_folder_layout")] = $folder_layout;
}

$pass_prot = $_POST["_".__("password_protected_links")] ?? null;
if (!$this->rcmail->config->get("nextcloud_attachment_password_protected_links_locked", true) && $pass_prot == 1) {
$param["prefs"][__("user_password_protected_links")] = true;
}

$expire_links = $_POST["_".__("expire_links")] ?? null;
$expire_links_after = $_POST["_".__("expire_links_after")] ?? null;
if (!$this->rcmail->config->get("nextcloud_attachment_expire_links_locked", true)) {
$param["prefs"][__("user_expire_links")] = ($expire_links == 1 && intval($expire_links_after) > 0);
if (intval($expire_links_after) > 0) {
$param["user_prefs"][__("expire_links_after")] = intval($expire_links_after);
}
}

self::log($param);

return $param;
}

/**
* (message_ready hook) correct attachment parameters for nextcloud attachments where
* parameters couldn't be set otherwise
Expand Down
2 changes: 1 addition & 1 deletion nextcloud_attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function init(): void

$this->add_hook("preferences_list", function ($param) { return $this->add_preferences($param); });

$this->add_hook("preferences_save", function ($param) { self::log($param); return $param; });
$this->add_hook("preferences_save", function ($param) { return $this->save_preferences($param); });

$this->add_hook("attachment_delete", function ($param) { return $this->delete($param); });

Expand Down

0 comments on commit f790e10

Please sign in to comment.