-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
quota.php
199 lines (168 loc) · 6 KB
/
quota.php
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
final class quota extends rcube_plugin
{
/**
* @var string
*/
public $task = 'mail|settings';
/**
* The loaded configuration.
*
* @var rcube_config
*/
private $config;
/**
* {@inheritdoc}
*/
public function init()
{
$this->load_plugin_config();
$this->add_texts('localization/', true);
$this->add_hook('quota', [$this, 'quota_message']);
$this->add_hook('settings_actions', [$this, 'settings_actions']);
$this->register_action('plugin.' . __CLASS__, [$this, 'quota_init']);
$this->add_plugin_assets();
}
public function settings_actions(array $args)
{
$args['actions'][] = [
'action' => 'plugin.' . __CLASS__,
'class' => 'quota',
'label' => 'quota_plugin_title',
'domain' => 'quota',
];
return $args;
}
public function quota_init()
{
$RCMAIL = rcmail::get_instance();
$this->register_handler('plugin.body', [$this, 'quota_form']);
$RCMAIL->output->set_pagetitle($this->gettext('quota_plugin_title'));
$RCMAIL->output->send('plugin');
}
public function quota_message(array $args)
{
$RCMAIL = rcmail::get_instance();
$thresholds = [
99 => 'error',
90 => 'warning',
];
krsort($thresholds);
foreach ($thresholds as $percent => $level) {
if ($args['percent'] >= $percent) {
$RCMAIL->output->show_message($this->gettext("quota_meet_{$percent}"), $level);
break;
}
}
}
public function quota_form()
{
$RCMAIL = rcmail::get_instance();
$STORAGE = $RCMAIL->get_storage();
$quota = $STORAGE->get_quota();
if (isset($quota['total'])) {
$quota_used_kb = $quota['used'];
$quota_free_kb = $quota['total'] - $quota['used'];
} else {
$quota_used_kb = 0;
$quota_free_kb = \INF;
}
$quota_total_kb = $quota_used_kb + $quota_free_kb;
$quota_used_humanized = $RCMAIL->show_bytes($quota_used_kb * 1024);
$quota_free_humanized = $RCMAIL->show_bytes($quota_free_kb * 1024);
$quota_total_humanized = $RCMAIL->show_bytes($quota_total_kb * 1024);
if (isset($quota['total'])) {
$quota_text = sprintf(
'%.2f%% ( %s of %s )',
$quota['percent'],
$quota_used_humanized,
$quota_total_humanized
);
} else {
$quota_text = $this->gettext('unknown');
}
$out = (
html::div(
['class' => 'box contentbox'],
html::div(
['id' => 'prefs-title', 'class' => 'boxtitle'],
$this->gettext('quota_plugin_title')
) .
html::div(
['class' => 'boxcontent'],
// debug information
(
$this->config->get('debug') ?
html::p(
['id' => 'quota-plugin-debug-info'],
(
'dump $quota = ' . print_r($quota, true)
)
) : ''
) .
// text reprecentation
(
$this->config->get('enable_text_presentation') ?
html::p(
null,
$this->gettext('space_used') . ': ' . $quota_text
) : ''
) .
// chart reprecentation
(
$this->config->get('enable_chart_presentation')
? '<canvas id="chart-container" style="height: 370px; width: 100%; max-width: 600px;"></canvas>'
: ''
) .
// admin contact
(
$this->config->get('show_admin_contact') ?
html::p(
null,
sprintf($this->gettext('problem_please_contact'), $this->config->get('admin_contact'))
) : ''
)
)
)
);
$js_variables = [
'char_title' => $this->gettext('chart_title'),
'label_used_space' => $this->gettext('space_used'),
'label_free_space' => $this->gettext('space_free'),
'quota_used_kb' => $quota_used_kb,
'quota_free_kb' => $quota_free_kb,
'quota_total_kb' => $quota_total_kb,
'quota_used_humanized' => $quota_used_humanized,
'quota_free_humanized' => $quota_free_humanized,
'quota_total_humanized' => $quota_total_humanized,
];
$js_variables_encoded = json_encode($js_variables, \JSON_UNESCAPED_UNICODE | \JSON_FORCE_OBJECT);
$out .= $this->config->get('enable_chart_presentation') ?
"<script>
// exposed by plugin: quota
var plugin_quota_chart_vars = {$js_variables_encoded};
drawDiskQuota();
</script>" : '';
$out = '<style> .contentbox { overflow: auto; } </style>' . $out;
return $out;
}
/**
* Add plugin assets.
*/
private function add_plugin_assets()
{
$this->include_stylesheet($this->local_skin_path() . '/main.css');
$this->include_script('assets/Chart-2.7.3.min.js');
$this->include_script('assets/draw.min.js');
}
/**
* Load plugin configuration.
*/
private function load_plugin_config()
{
$RCMAIL = rcmail::get_instance();
$this->load_config('config.inc.php.dist');
$this->load_config('config.inc.php');
$this->config = $RCMAIL->config;
}
}