-
Notifications
You must be signed in to change notification settings - Fork 1
/
qmailforward.php
355 lines (305 loc) · 13.1 KB
/
qmailforward.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* qmailforward
*
* Plugin to build the forward and copy for qmail
* vpopmail must be patched accordingly and configured to store the
* aliases in the database (--enable-valias). Adjustments to
* qmailadmin are needed as well.
*
* @version 1.0.3
* @author Roberto Puzzanghera
* @url https://notes.sagredo.eu/en/qmail-notes-185/roundcube-plugins-35.html#qmailforward
* https://notes.sagredo.eu/en/qmail-notes-185/sql-valias-with-sieve-solution-for-qmail-new-features-and-roundcube-plugin-301.html
*
* Credits for the scheme of several functions and classes go to the
* sauserprefs and managesieve authors. They are specified before the
* relevant code.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Roundcube. If not, see https://www.gnu.org/licenses/.
*/
class qmailforward extends rcube_plugin
{
public $task = 'settings';
private $rc;
private $storage;
private $user;
private $domain;
private $error;
public function init()
{
$this->rc = rcmail::get_instance();
$this->load_config('config.inc.dist.php');
$this->load_config();
if ($this->plugin_enabled($this->domain)) {
if (!$this->get_username()) {
rcube::write_log('errors', 'qmailforward error: cannot retrieve username/domain');
return 1;
}
$this->add_texts('localization/', true);
$this->include_script('qmailforward.js');
// insert the menu button
$this->add_hook('settings_actions', [$this, 'settings_tab']);
$this->_init_storage();
$this->register_action('plugin.qmailforward', [$this, 'init_html']);
$this->register_action('plugin.qmailforward-save', [$this, 'forward_save']);
}
}
/* allow qmailforward_allowed_hosts only, unless that array is empty */
private function plugin_enabled($domain) {
$denied_domains = $this->rc->config->get('qmailforward_allowed_hosts');
if (in_array($domain, $denied_domains)) return false;
else return true;
}
/* build the html */
public function init_html()
{
// page title
$this->rc->output->set_pagetitle($this->gettext('page-title'));
// register content handler
// 'forwardform' = container's name/id inside template
$this->rc->output->add_handler('forwardform', array($this, 'forward_form'));
// send content to template 'form_template'
$this->rc->output->send('qmailforward.form_template');
}
/**********************************************************************************************
* forward form
*
* function derived from the RC managesieve-forward plugin, which already has a nice
* html form
*
* $attrib is an array containing the xml attrib of the rc obj <roundcube:object name="forwardform" id="forwardform" class="propform" />
* array(3) { ["name"]=> string(11) "forwardform" ["id"]=> string(11) "forwardform" ["class"]=> string(8) "propform" }
*
**********************************************************************************************/
public function forward_form($attrib)
{
$stored = array(); // container array for db data
// load data from the db
$stored = $this->storage->load($this->user, $this->domain);
// build FORM tag
$form_id = $attrib['id']; // = forwardform
$out = $this->rc->output->request_form([
'id' => $form_id,
'name' => $form_id,
'method' => 'post',
'task' => 'settings',
'action' => 'plugin.qmailforward-save', // we have registered an action for that to handle the post
'noclose' => true
] + $attrib
);
// form elements
$status = new html_select(['name' => 'forward_status', 'id' => 'forward_status', 'class' => 'custom-select']);
$action = new html_select(['name' => 'forward_action', 'id' => 'forward_action', 'class' => 'custom-select']);
$status->add($this->gettext('on'), 'on');
$status->add($this->gettext('off'), 'off');
$action->add($this->gettext('copy'), 'copy');
$action->add($this->gettext('redirect'), 'redirect');
// force domain selection in redirect email input
$domains = (array) $this->rc->config->get('qmailforward_domains');
if (!empty($domains)) {
sort($domains);
$domain_select = new html_select(['name' => 'action_domain', 'id' => 'action_domain', 'class' => 'custom-select']);
$domain_select->add(array_combine($domains, $domains));
if (!empty($stored)) {
$parts = explode('@', $stored[$this->rc->config->get('qmailforward_sql_valias_field')]);
if (!empty($parts)) {
$the_domain = in_array($parts[1], $domains) ? $parts[1] : '';
$the_username = !empty($the_domain) ? $parts[0] : '';
}
}
}
// the value is the entire address, unless we are allowing only a set of domains
$target_value = !empty($domain_select) ? $the_username
: $stored[$this->rc->config->get('qmailforward_sql_valias_field')]??'';
// redirect target
$domain_selected = !empty($the_domain) ? $the_domain : null;
$action_target = '<span id="action_target_span" class="input-group">'
. '<input type="text" name="action_target" id="action_target"'
. ' value="' .$target_value. '"'
. (!empty($domain_select) ? ' size="20"' : ' size="35"') . '/>'
. (!empty($domain_select) ? ' <span class="input-group-prepend input-group-append"><span class="input-group-text">@</span></span> '
. $domain_select->show($domain_selected)
: '')
. '</span>';
// Message tab
$table = new html_table(['cols' => 2]);
// forward
$table->add('title', html::label('forward_action', $this->gettext('action')));
$copy = ($stored[$this->rc->config->get('qmailforward_sql_copy_field')]??null)==1 ? 'copy' : 'redirect';
$table->add('forward input-group input-group-combo', $action->show($copy).' '.$action_target);
// status
$table->add('title', html::label('forward_status', $this->gettext('status')));
$on_off = ( empty($stored) || (!empty($domains) && $the_domain=='') ) ? 'off' : 'on';
$table->add(null, $status->show($on_off));
$out .= $table->show($attrib);
$out .= '</form>';
$this->rc->output->add_gui_object('qmailforward_form', $form_id);
return $out;
}
/**********************************************
* collect the POST data
* forward_action => copy/redirect
* action_target => [email protected] (or just user if action_domain defined)
* action_domain = domain.tld
* forward_status => on/off
**********************************************/
public function forward_save()
{
$success = true;
$error = '';
// build email
$email = !is_null($_POST['action_domain']??null) ?
$_POST['action_target'].'@'.$_POST['action_domain'] :
$_POST['action_target'];
// sanity check
if (!$this->validEmail($email)) {
$error = $this->gettext('invalid_email').": ".$email.$_SERVER['REQUEST_METHOD'] ;
$success = false;
}
/***************************************
* save to db
*
* result = true on success
* false on error
***************************************/
if (!$this->storage->save($this->user, $this->domain)) $success = false;
// return feedback
if ($success) {
$this->rc->output->command('display_message', $this->gettext('success'), 'confirmation');
}
else {
$error = !empty($error) ? ": ".$error : '';
$this->rc->output->command('display_message', $this->gettext('failed').$error, 'error');
}
}
/* set the button in the settings menu side bar */
public function settings_tab($p)
{
$this->include_stylesheet($this->local_skin_path() . '/style.css');
// add qmailforward tab
$p['actions'][] = [
'action' => 'plugin.qmailforward', // we defined an handler for this action
'class' => 'qmailforward',
'label' => 'qmailforward.forward',
'title' => 'qmailforward.manage-forward',
'role' => 'button',
'aria-disabled' => 'false',
'tabindex' => '0'];
return $p;
}
/* load the classes for db storage */
private function _init_storage()
{
if (!$this->storage) {
// Add include path for internal classes
$include_path = $this->home . '/lib' . \PATH_SEPARATOR;
$include_path .= ini_get('include_path');
set_include_path($include_path);
$class = $this->rc->config->get('qmailforward_storage', 'mysql');
$class = "rcube_qmailforward_storage_" . $class;
// try to instantiate class
if (class_exists($class)) {
$this->storage = new $class($this->rc->config);
}
else {
// no storage found, raise error
rcube::raise_error(['code' => 604, 'type' => 'qmailforward',
'line' => __LINE__, 'file' => __FILE__,
'message' => "Failed to find storage driver. Check qmailforward_storage config option",
], true, true);
}
}
}
/* save the username and the domain */
private function get_username() {
$username = explode('@', $_SESSION['username']);
$this->user = $username[0];
$this->domain = $username[1];
if (!is_null($this->user) && !is_null($this->domain)) return true;
else return false;
}
/********************************************************
* http://www.linuxjournal.com/article/9585?page=0,3
* Validate an email address.
* Provide email address (raw input)
* Returns true if the email address has the email
* address format and the domain exists.
********************************************************/
private function validEmail($email)
{
// DNS check disabled by default
$checkDNS = $this->rc->config->get('qmailforward_dnscheck');
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($checkDNS && $isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}
}