subscriptions_option.php
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Subscription Options
*
* A plugin which can enable or disable the use of imap subscriptions.
* It includes a toggle on the settings page under "Server Settings".
* The preference can also be locked
*
* Add it to the plugins list in config.inc.php to enable the user option
* The user option can be hidden and set globally by adding 'use_subscriptions'
* to the 'dont_override' configure line:
* $config['dont_override'] = ['use_subscriptions'];
* and then set the global preference
* $config['use_subscriptions'] = true; // or false
*
* Roundcube caches folder lists. When a user changes this option or visits
* their folder list, this cache is refreshed. If the option is on the
* 'dont_override' list and the global option has changed, don't expect
* to see the change until the folder list cache is refreshed.
*
* @author Ziba Scott
* @license GNU GPLv3+
*/
class subscriptions_option extends rcube_plugin
{
public $task = 'mail|settings';
/**
* Plugin initialization
*/
function init()
{
$dont_override = rcmail::get_instance()->config->get('dont_override', []);
if (!in_array('use_subscriptions', $dont_override)) {
$this->add_hook('preferences_list', [$this, 'prefs_list']);
$this->add_hook('preferences_save', [$this, 'prefs_save']);
}
$this->add_hook('storage_folders', [$this, 'mailboxes_list']);
$this->add_hook('folders_list', [$this, 'folders_list']);
}
/**
* Hook to inject plugin-specific user settings
*
* @param array $args Hook arguments
*
* @return array Modified hook arguments
*/
function prefs_list($args)
{
if ($args['section'] == 'server') {
$this->add_texts('localization/', false);
$use_subscriptions = rcmail::get_instance()->config->get('use_subscriptions', true);
$field_id = 'rcmfd_use_subscriptions';
$checkbox = new html_checkbox(['name' => '_use_subscriptions', 'id' => $field_id, 'value' => 1]);
$args['blocks']['main']['options']['use_subscriptions'] = [
'title' => html::label($field_id, rcube::Q($this->gettext('useimapsubscriptions'))),
'content' => $checkbox->show($use_subscriptions ? 1 : 0),
];
}
return $args;
}
/**
* Hook to save plugin-specific user settings
*
* @param array $args Hook arguments
*
* @return array Modified hook arguments
*/
function prefs_save($args)
{
if ($args['section'] == 'server') {
$rcmail = rcmail::get_instance();
$use_subscriptions = $rcmail->config->get('use_subscriptions', true);
$args['prefs']['use_subscriptions'] = isset($_POST['_use_subscriptions']);
// if the use_subscriptions preference changes, flush the folder cache
if (
($use_subscriptions && !isset($_POST['_use_subscriptions']))
|| (!$use_subscriptions && isset($_POST['_use_subscriptions']))
) {
$storage = $rcmail->get_storage();
$storage->clear_cache('mailboxes');
}
}
return $args;
}
function mailboxes_list($args)
{
$rcmail = rcmail::get_instance();
if (!$rcmail->config->get('use_subscriptions', true)) {
$storage = $rcmail->get_storage();
if ($folders = $storage->list_folders_direct($args['root'], $args['name'])) {
$folders = array_filter($folders, function($folder) use ($storage) {
$attrs = $storage->folder_attributes($folder);
return !in_array_nocase('\\Noselect', $attrs);
});
$args['folders'] = $folders;
}
}
return $args;
}
function folders_list($args)
{
$rcmail = rcmail::get_instance();
if (!$rcmail->config->get('use_subscriptions', true)) {
foreach ($args['list'] as $idx => $data) {
$args['list'][$idx]['content'] = preg_replace('/<input [^>]+>/', '', $data['content']);
}
}
return $args;
}
}