This repository has been archived by the owner on Nov 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathaliases.php
416 lines (329 loc) · 14.2 KB
/
aliases.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/*
+-----------------------------------------------------------------------+
| PostfixAdmin Aliases Plugin for RoundCube |
| Version: 1.3.6 |
| Author: Gianluca Giacometti <php@gianlucagiacometti.it> |
| Copyright (C) 2012 Gianluca Giacometti |
| License: GNU General Public License |
+-----------------------------------------------------------------------+
code structure based on:
+-----------------------------------------------------------------------+
| SieveRules Module for RoundCube |
| by Philip Weir |
| Licensed under the GNU GPL |
+-----------------------------------------------------------------------+
*/
class aliases extends rcube_plugin
{
public $task = 'settings';
protected $alias = array();
protected $action;
protected $rc;
function check_driver_error($ret) {
switch ($ret) {
case PLUGIN_ERROR_CONNECT:
$this->rc->output->command('display_message', $this->gettext('aliasesdriverconnecterror'), 'error');
$this->init_setup();
return FALSE;
break;
case PLUGIN_ERROR_PROCESS:
$this->rc->output->command('display_message', $this->gettext('aliasesdriverprocesserror'), 'error');
$this->init_setup();
return FALSE;
break;
case PLUGIN_SUCCESS:
default:
return TRUE;
break;
}
}
function init()
{
$rcmail = rcube::get_instance();
$this->load_config();
$this->add_texts('localization/');
// load required plugin
$this->require_plugin('jqueryui');
$this->action = $rcmail->action;
$this->include_stylesheet($this->local_skin_path() . '/tabstyles.css');
$this->add_hook('settings_actions', array($this, 'settings_tab'));
// register internal plugin actions
$this->register_action('plugin.aliases', array($this, 'init_html'));
$this->register_action('plugin.aliases.add', array($this, 'init_html'));
$this->register_action('plugin.aliases.edit', array($this, 'init_html'));
$this->register_action('plugin.aliases.setup', array($this, 'init_setup'));
$this->register_action('plugin.aliases.move', array($this, 'move'));
$this->register_action('plugin.aliases.save', array($this, 'save'));
$this->register_action('plugin.aliases.delete', array($this, 'delete'));
$this->register_action('plugin.aliases.check', array($this, 'check_alias_domain'));
}
function settings_tab($p)
{
$p['actions'][] = array('action' => 'plugin.aliases', 'class' => 'aliases', 'label' => 'aliases.aliases', 'title' => 'aliases.aliases', 'role' => 'button', 'aria-disabled' => 'false', 'tabindex' => '0');
return $p;
}
function init_html()
{
// create aliases UI
$rcmail = rcube::get_instance();
$this->_startup();
$this->include_script('aliases.js');
// add handlers for the various UI elements
$this->api->output->add_handlers(array(
'aliaseslisttitle' => array($this, 'gen_list_title'),
'aliaseslist' => array($this, 'gen_list'),
'aliasessetup' => array($this, 'gen_setup'),
'aliasform' => array($this, 'gen_form'),
'aliasesframe' => array($this, 'aliases_frame'),
));
$this->api->output->include_script('list.js');
if ($this->action == 'plugin.aliases.add') {
// show add alias
$this->api->output->set_pagetitle($this->gettext('aliasesnewalias'));
$this->api->output->send('aliases.editalias');
}
if ($this->action == 'plugin.aliases.edit') {
// show edit alias
$this->api->output->set_pagetitle($this->gettext('aliaseseditalias'));
$this->api->output->send('aliases.editalias');
}
else {
// show main UI
$this->api->output->set_pagetitle($this->gettext('aliases'));
$this->api->output->send('aliases.aliases');
}
}
function init_setup()
{
// redirect setup UI, see gen_setup()
$this->_startup();
$this->include_script('aliases.js');
$this->api->output->add_handlers(array('aliasessetup' => array($this, 'gen_setup')));
$this->api->output->set_pagetitle($this->gettext('aliases'));
$this->api->output->send('aliases.setupaliases');
}
function aliases_frame($attrib)
{
if (!$attrib['id'])
$attrib['id'] = 'rcmprefsframe';
return $this->api->output->frame($attrib, true);
}
function gen_list_title($attrib)
{
$title = $this->gettext('aliases');
return $title;
}
function gen_list($attrib)
{
// create alias list for UI
$this->api->output->add_label('loading', 'aliases.aliasesaliasdeleteconfirm');
$this->api->output->add_gui_object('aliases_list', 'aliases-table');
$table = new html_table($attrib + array('cols' => 2));
if (!$attrib['noheader']) {
$table->add_header(null, $this->gen_list_title($attrib));
}
if (sizeof($this->alias) == 0) {
// no alias exist
$table->add(array('colspan' => '2'), rcube_utils::rep_specialchars_output($this->gettext('aliasesnoalias')));
}
else foreach($this->alias as $idx => $aliasx) {
$args = rcube::get_instance()->plugins->exec_hook('aliases_list_aliases', array('idx' => $idx, 'name' => $aliasx['name'], 'enable' => $aliasx['enable']));
$table->set_row_attribs(array('id' => 'rcmrow' . $idx));
$table->add(null, rcmail::Q($aliasx['name']));
$content = $aliasx['active'] == false ? "(".$this->gettext('aliasesaliasdisabled').")" : " ";
$table->add(null, $content);
}
return html::tag('div', array('id' => 'aliases-list-aliases'), $table->show($attrib));
}
function gen_setup()
{
$rcmail = rcube::get_instance();
if (isset($_GET['_framed']) || isset($_POST['_framed'])) {
$this->api->output->add_script("parent.". rcmail_output::JS_OBJECT_NAME .".goto_url('plugin.aliases');");
}
else {
// go to aliases page
$rcmail->overwrite_action('plugin.aliases');
$this->api->output->send('aliases.aliases');
}
}
function gen_form($attrib)
{
$rcmail = rcube::get_instance();
$this->include_script('jquery.maskedinput.min.js');
$this->api->output->add_label(
'aliases.aliasesnovalidalias', 'aliases.aliasesaliasexists', 'aliases.actiondeleteconfirm');
$iid = rcube_utils::get_input_value('_iid', rcube_utils::INPUT_GPC);
if ($iid == '')
$iid = sizeof($this->alias);
$cur_script = $this->alias[$iid];
$this->api->output->set_env('iid', $iid);
if (isset($this->alias[$iid]))
$this->api->output->add_script("parent.". rcmail_output::JS_OBJECT_NAME .".aliases_ready('".$iid."');");
list($iid, $cur_script) = array_values($rcmail->plugins->exec_hook('aliases_init', array('id' => $iid, 'script' => $cur_script)));
list($form_start, $form_end) = get_form_tags($attrib, 'plugin.aliases.save');
$out = $form_start;
$hidden_iid = new html_hiddenfield(array('name' => '_iid', 'value' => $iid));
$out .= $hidden_iid->show();
// alias name input
$field_id = 'rcmfd_name';
$input_name = new html_inputfield(array('name' => '_name', 'id' => $field_id, 'required' => 'required'));
$out .= html::label($field_id, rcmail::Q($this->gettext('aliasesaliasname')));
$out .= " " . $input_name->show($cur_script['name']);
// alias active input
$field_id = 'rcmfd_active';
$input_active = new html_select(array('name' => '_active', 'id' => $field_id));
$input_active->add(array($this->gettext('aliasestrue'),$this->gettext('aliasesfalse')), array('TRUE','FALSE'));
$content = $cur_script['active'] == false ? "FALSE" : "TRUE";
$out .= html::span('enableLink', html::label($field_id, rcmail::Q($this->gettext('aliasesaliasenabled')))
. " " . $input_active->show($content));
$out .= "<br /><br />";
$out .= $form_end;
return $out;
}
function check_alias_domain()
{
$rcmail = rcmail::get_instance();
$driver = $this->home . '/lib/drivers/' . $rcmail->config->get('aliases_driver', 'sql').'.php';
$new_alias = rcube_utils::get_input_value('_newalias', rcube_utils::INPUT_POST, true);
$this->api->output->add_label('aliases.aliasesaliasexistsindomain');
if (!is_readable($driver)) {
rcube::raise_error(array('code' => 600, 'type' => 'php', 'file' => __FILE__, 'message' => "aliases plugin: Unable to open driver file $driver"), true, false);
return $this->gettext('aliasesinternalerror');
}
require_once($driver);
if (!function_exists('mail_alias')) {
rcube::raise_error(array('code' => 600, 'type' => 'php', 'file' => __FILE__, 'message' => "aliases plugin: function mail_alias_read not found in driver $driver"), true, false);
return $this->gettext('aliasesinternalerror');
}
$data = array();
$data['goto'] = rcmail::get_instance()->user->get_username();
$elements = explode("@", trim($data['goto']));
$data['address'] = $name . "@" . $elements[1];
$data['domain'] = $elements[1];
$ret = mail_alias('allaliases', $data);
if (!$this->check_driver_error($ret)) { return FALSE; }
$error = "";
foreach ($data['address'] as $alias) {
$element = explode("@", trim($alias['address']));
if ($element[0] == $new_alias) {
$error = $this->gettext('aliasesaliasexistsindomain');
}
}
$rcmail->output->command('plugin.aliases.checkaliasdomain', $error);
}
function save()
{
$rcmail = rcube::get_instance();
$this->_startup();
$name = mb_strtolower(trim(rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST, true)), 'UTF-8');
$active = trim(rcube_utils::get_input_value('_active', rcube_utils::INPUT_POST));
$iid = trim(rcube_utils::get_input_value('_iid', rcube_utils::INPUT_POST));
if (!preg_match('/[a-zA-Z0-9_.]/', $name)) {
$this->api->output->command('display_message', $this->gettext('aliasesaliasnameerror'), 'error');
$this->init_setup();
return FALSE;
}
$driver = $this->home . '/lib/drivers/' . $rcmail->config->get('aliases_driver', 'sql').'.php';
if (!is_readable($driver)) {
rcube::raise_error(array('code' => 600, 'type' => 'php', 'file' => __FILE__, 'message' => "aliases plugin: Unable to open driver file $driver"), true, false);
return $this->gettext('aliasesinternalerror');
}
require_once($driver);
if (!function_exists('mail_alias')) {
rcube::raise_error(array('code' => 600, 'type' => 'php', 'file' => __FILE__, 'message' => "aliases plugin: function mail_alias_read not found in driver $driver"), true, false);
return $this->gettext('aliasesinternalerror');
}
$data = array();
$data['goto'] = rcmail::get_instance()->user->get_username();
$elements = explode("@", trim($data['goto']));
$data['address'] = $name . "@" . $elements[1];
$data['domain'] = $elements[1];
$ret = mail_alias('read', $data);
if (!$this->check_driver_error($ret)) { return FALSE; }
// create new alias
if (!isset($this->alias[$iid]) && ($name != "") && empty($data)) {
$data = array();
$data['goto'] = rcmail::get_instance()->user->get_username();
$elements = explode("@", trim($data['goto']));
$data['address'] = $name . "@" . $elements[1];
$data['domain'] = $elements[1];
$data['created'] = date('Y-m-d H:i:s');
$data['modified'] = date('Y-m-d H:i:s');
$data['active'] = $active;
$ret = mail_alias('create', $data);
if (!$this->check_driver_error($ret)) { return FALSE; }
$this->api->output->command('display_message', $this->gettext('aliasesaliascreated'), 'confirmation');
$this->gen_setup();
}
// update existing alias
else {
$data = array();
$data['goto'] = rcmail::get_instance()->user->get_username();
$elements = explode("@", trim($data['goto']));
$data['address'] = $this->alias[$iid]['name'] . "@" . $elements[1];
$data['newalias'] = $name . "@" . $elements[1];
$data['modified'] = date('Y-m-d H:i:s');
$data['active'] = $active;
$ret = mail_alias('update', $data);
if (!$this->check_driver_error($ret)) { return FALSE; }
$this->api->output->command('display_message', $this->gettext('aliasesaliasupdated'), 'confirmation');
$this->gen_setup();
}
}
function delete()
{
$rcmail = rcube::get_instance();
$this->_startup();
$driver = $this->home . '/lib/drivers/' . $rcmail->config->get('aliases_driver', 'sql').'.php';
if (!is_readable($driver)) {
rcube::raise_error(array('code' => 600, 'type' => 'php', 'file' => __FILE__, 'message' => "aliases plugin: Unable to open driver file $driver"), true, false);
return $this->gettext('aliasesinternalerror');
}
require_once($driver);
if (!function_exists('mail_alias')) {
rcube::raise_error(array('code' => 600, 'type' => 'php', 'file' => __FILE__, 'message' => "aliases plugin: function mail_alias_read not found in driver $driver"), true, false);
return $this->gettext('aliasesinternalerror');
}
$data = array();
$data['goto'] = rcmail::get_instance()->user->get_username();
$elements = explode("@", trim($data['goto']));
$ids = rcube_utils::get_input_value('_iid', rcube_utils::INPUT_GET);
$data['address'] = $this->alias[$ids]['name'] . "@" . $elements[1];
$ret = mail_alias('delete', $data);
if (!$this->check_driver_error($ret)) { return FALSE; }
$this->api->output->command('display_message', $this->gettext('aliasesaliasdeleted'), 'confirmation');
$this->gen_setup();
}
protected function _startup()
{
$rcmail = rcube::get_instance();
$driver = $this->home . '/lib/drivers/' . $rcmail->config->get('aliases_driver', 'sql').'.php';
if (!is_readable($driver)) {
rcube::raise_error(array('code' => 600, 'type' => 'php', 'file' => __FILE__, 'message' => "aliases plugin: Unable to open driver file $driver"), true, false);
return $this->gettext('aliasesinternalerror');
}
require_once($driver);
if (!function_exists('mail_alias')) {
rcube::raise_error(array('code' => 600, 'type' => 'php', 'file' => __FILE__, 'message' => "aliases plugin: function mail_alias_read not found in driver $driver"), true, false);
return $this->gettext('aliasesinternalerror');
}
$data = array();
$data['goto'] = rcmail::get_instance()->user->get_username();
$elements = explode("@", trim($data['goto']));
$data['domain'] = $elements[1];
$ret = mail_alias('aliases', $data);
if (!$this->check_driver_error($ret)) { return FALSE; }
foreach ($data['address'] as $alias) {
$active = $alias['active'];
$elements = explode("@", trim($alias['address']));
if ($elements[0] != "") {
$this->alias[] = array("name" => $elements[0], "domain" => $elements[1], "active" => $active);
}
}
sort($this->alias);
return TRUE;
}
}
?>