-
Notifications
You must be signed in to change notification settings - Fork 3
/
ident_switch.php
921 lines (781 loc) · 27 KB
/
ident_switch.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
<?php
/**
* Identities IMAP
*
* This plugin allows fast switching between accounts.
*
* @url
*/
class ident_switch extends rcube_plugin
{
public $task = '?(?!login|logout).*';
private static $logging = true;
const TABLE = 'ident_switch';
const MY_POSTFIX = '_iswitch';
// Flags user in database
const DB_ENABLED = 1;
//const DB_SECURE_SSL = 2; // Not supported any more
const DB_SECURE_IMAP_TLS = 4;
// SMTP auth values
const SMTP_AUTH_IMAP = 1;
const SMTP_AUTH_NONE = 2;
function init()
{
$this->add_hook('startup', array($this, 'on_startup'));
$this->add_hook('render_page', array($this, 'on_render_page'));
$this->add_hook('smtp_connect', array($this, 'on_smtp_connect'));
$this->add_hook('identity_form', array($this, 'on_identity_form'));
$this->add_hook('identity_update', array($this, 'on_identity_update'));
$this->add_hook('identity_create', array($this, 'on_identity_create'));
$this->add_hook('identity_create_after', array($this, 'on_identity_create_after'));
$this->add_hook('identity_delete', array($this, 'on_identity_delete'));
$this->add_hook('template_object_composeheaders', array($this, 'on_template_object_composeheaders'));
$this->add_hook('preferences_list', array($this, 'on_special_folders_form'));
$this->add_hook('preferences_save', array($this, 'on_special_folders_update'));
$rc = rcmail::get_instance();
// check for LDAP
if ($rc->config->get('ldapAliasSync', null))
$this->add_hook('storage_connect', array($this, 'override_ldap_password'));
$this->register_action('plugin.ident_switch.switch', array($this, 'on_switch'));
foreach (rcube_storage::$folder_types as $type)
{
$key = $type . '_mbox_default' . self::MY_POSTFIX;
if (!isset($_SESSION[$key]))
$_SESSION[$key] = $rc->config->get($type . '_mbox');
}
$this->load_config(); // config.inc.php
self::$logging = rcmail::get_instance()->config->get('ident_switch.logging', true);
}
function on_startup($args)
{
$rc = rcmail::get_instance();
if (strcasecmp($rc->user->data['username'], $_SESSION['username']) !== 0)
{ // We are impersonating
$rc->config->set('imap_cache', null);
$rc->config->set('messages_cache', false);
if ($args['task'] == 'mail')
{
$this->add_texts('localization/');
$rc->config->set('create_default_folders', false);
}
}
foreach (rcube_storage::$folder_types as $type)
{
$defaultKey = $type . '_mbox_default' . self::MY_POSTFIX;
$otherKey = $type . '_mbox' . self::MY_POSTFIX;
$val = isset($_SESSION[$otherKey]) ? $_SESSION[$otherKey] : $_SESSION[$defaultKey];
$rc->config->set($type . '_mbox', $val);
}
return $args;
}
function on_render_page($args)
{
$rc = rcmail::get_instance();
switch ($rc->task)
{
case 'mail':
$this->render_switch($rc, $args);
break;
case 'settings':
$this->include_script('ident_switch-form-min.js');
break;
}
return $args;
}
private function render_switch($rc, $args)
{
// Currently selected identity
$iid_s = isset($_SESSION['iid' . self::MY_POSTFIX]) ? $_SESSION['iid' . self::MY_POSTFIX] : '-1';
$iid = 0;
if (is_int($iid_s))
$iid = $iid_s;
elseif ($iid_s === '-1')
$iid = -1;
elseif (ctype_digit($iid_s))
$iid = intval($iid_s);
$accNames = array(isset($_SESSION['global_alias']) ? $_SESSION['global_alias'] : $rc->user->data['username']);
$accValues = array(-1);
$accSelected = -1;
// Get list of alternative accounts
$sql = "SELECT "
. "isw.id, isw.iid, isw.label, isw.username, ii.email"
. " FROM"
. " {$rc->db->table_name(self::TABLE)} isw"
. " INNER JOIN {$rc->db->table_name('identities')} ii ON isw.iid=ii.identity_id"
. " WHERE isw.user_id = ? AND isw.flags & ? > 0";
$qRec = $rc->db->query($sql, $rc->user->data['user_id'], self::DB_ENABLED);
while ($r = $rc->db->fetch_assoc($qRec))
{
$accValues[] = $r['id'];
if ($iid == $r['iid'])
$accSelected = $r['id'];
// Make label
$lbl = $r['label'];
if (!$lbl)
{
if (!$r['username'])
$r['username'] = $r['email'];
if (strpos($r['username'], '@') === false)
$lbl = $r['username'] . '@' . ($r['host'] ? $r['host'] : 'localhost');
else
$lbl = $r['username'];
}
$accNames[] = rcube::Q($lbl);
}
// Render UI if user has extra accounts
if (count($accValues) > 1)
{
$this->include_script('ident_switch-switch-min.js');
$select = new html_select(array(
'id' => 'plugin-ident_switch-account',
'style' => 'display: none; padding: 0;',
'onchange' => 'plugin_switchIdent_switch(this.value);',
));
$select->add($accNames, $accValues);
$rc->output->add_footer($select->show(array($accSelected)));
}
}
function on_smtp_connect($args)
{
$iid = $_SESSION['iid' . self::MY_POSTFIX];
if (!is_numeric($iid) || $iid == -1)
{
self::write_log('no identity switch is selected... trying to find related smtp server from the from header');
$requestFrom = rcube_utils::get_input_value('_from', rcube_utils::INPUT_POST);
if (empty($requestFrom)) {
self::write_log('no _from post parameter found... falling back to original default config');
return $args;
} else {
$iid = intval($requestFrom);
if ($iid == 0) {
self::write_log('falling back to original default config as _from post field is no integer: ' . $_POST['_from']);
return $args;
}
}
}
$rc = rcmail::get_instance();
$sql = 'SELECT smtp_host, flags, smtp_port, username, smtp_auth, password FROM ' . $rc->db->table_name(self::TABLE) . ' WHERE iid = ? AND user_id = ?';
$q = $rc->db->query($sql, $iid ,$rc->user->ID);
$r = $rc->db->fetch_assoc($q);
if (is_array($r))
{
if (!$r['username'])
{ // Load email from identity
$sql = 'SELECT email FROM ' . $rc->db->table_name('identities') . ' WHERE identity_id = ?';
$q = $rc->db->query($sql, $iid);
$rIid = $rc->db->fetch_assoc($q);
$r['username'] = $rIid['email'];
}
$args['smtp_user'] = $r['username'];
$args['smtp_pass'] = $r['smtp_auth'] == self::SMTP_AUTH_IMAP ? $rc->decrypt($r['password']) : '';
# In 1.6 smtp_server was renamed to smtp_host and includes port. smtp_port was depricated.
$verParts = explode('.', RCMAIL_VERSION, 3);
$paramSmtpHost = 'smtp_host';
$paramSmtpPort = null;
if (intval($verParts[0]) <= 1 && intval($verParts[1]) < 6)
{
$paramSmtpHost = 'smtp_server';
$paramSmtpPort = 'smtp_port';
}
$args[$paramSmtpHost] = $r['smtp_host'] ? $r['smtp_host'] : 'localhost'; // Default SMTP host here
if ($r['flags'] & self::DB_SECURE_IMAP_TLS)
{
if (strpos($args[$paramSmtpHost], ':') !== false)
self::write_log('SMTP server already contains protocol, ignoring TLS flag.');
else
$args[$paramSmtpHost] = 'tls://' . $args[$paramSmtpHost];
}
$smtpPort = $r['smtp_port'] ? $r['smtp_port'] : 587; // Default SMTP port here
if ($paramSmtpPort)
$args[$paramSmtpPort] = $smtpPort;
else
$args[$paramSmtpHost] .= ':' . $smtpPort;
}
return $args;
}
private static function get_common_form(&$record)
{
$prefix = 'ident_switch.form.common.';
return array(
$prefix . 'enabled' => array('type' => 'checkbox', 'onchange' => 'plugin_switchIdent_enabled_onChange();'),
$prefix . 'label' => array('type' => 'text', 'size' => 32, 'placeholder' => $record['email']),
$prefix . 'readonly' => array('type' => 'hidden'),
);
}
private static function get_imap_form(&$record)
{
$prefix = 'ident_switch.form.imap.';
return array(
$prefix . 'host' => array('type' => 'text', 'size' => 64, 'placeholder' => 'localhost'),
$prefix . 'port' => array('type' => 'text', 'size' => 5, 'placeholder' => 143),
$prefix . 'tls' => array('type' => 'checkbox'),
$prefix . 'username' => array('type' => 'text', 'size' => 64, 'placeholder' => $record['email']),
$prefix . 'password' => array('type' => 'password', 'size' => 64),
$prefix . 'delimiter' => array('type' => 'text', 'size' => 1, 'placeholder' => '.'),
);
}
private function get_smtp_form(&$record)
{
$prefix = 'ident_switch.form.smtp.';
$authType = new html_select(array('name' => "_{$prefix}auth"));
$authType->add($this->gettext('form.smtp.auth.imap'), (string)(self::SMTP_AUTH_IMAP));
$authType->add($this->gettext('form.smtp.auth.none'), (string)(self::SMTP_AUTH_NONE));
return array(
$prefix . 'host' => array('type' => 'text', 'size' => 64, 'placeholder' => 'localhost'),
$prefix . 'port' => array('type' => 'text', 'size' => 5, 'placeholder' => 587),
$prefix . 'auth' => array('value' => $authType->show(array($record['ident_switch.form.smtp.auth']))),
);
}
function on_identity_form($args)
{
$rc = rcmail::get_instance();
// Do not show options for default identity
if( strcasecmp($args['record']['email'], $rc->user->data['username']) === 0)
return $args;
$this->add_texts('localization');
$row = null;
if (isset($args['record']['identity_id']))
{
$sql = 'SELECT * FROM ' . $rc->db->table_name(self::TABLE) . ' WHERE iid = ? AND user_id = ?';
$q = $rc->db->query($sql, $args['record']['identity_id'], $rc->user->ID);
$row = $rc->db->fetch_assoc($q);
}
$record = &$args['record'];
// Load data if exists
if ($row)
{
$dbToForm = array(
'label' => 'common.label',
'imap_host' => 'imap.host',
'imap_port' => 'imap.port',
'imap_delimiter' => 'imap.delimiter',
'username' => 'imap.username',
'password' => 'imap.password',
'smtp_host' => 'smtp.host',
'smtp_port' => 'smtp.port',
'smtp_auth' => 'smtp.auth',
);
foreach ($row as $k => $v)
if (isset($dbToForm[$k]))
$record['ident_switch.form.' . $dbToForm[$k]] = $v;
// Parse flags
$record['ident_switch.form.common.enabled'] = !!($row['flags'] & self::DB_ENABLED);
$record['ident_switch.form.imap.tls'] = !!($row['flags'] & self::DB_SECURE_IMAP_TLS);
// Set readonly if needed
$cfg = $this->get_preconfig($record['email']);
if (is_array($cfg) && $cfg['readonly'])
{
$record['ident_switch.form.common.readonly'] = 1;
if (in_array(strtoupper($cfg['user']), array('EMAIL', 'MBOX')))
$record['ident_switch.form.common.readonly'] = 2;
}
}
else
$this->apply_preconfig($record);
$args['form']['ident_switch.common'] = array(
'name' => $this->gettext('form.common.caption'),
'content' => ident_switch::get_common_form($record)
);
$args['form']['ident_switch.imap'] = array(
'name' => $this->gettext('form.imap.caption'),
'content' => ident_switch::get_imap_form($record)
);
$args['form']['ident_switch.smtp'] = array(
'name' => $this->gettext('form.smtp.caption'),
'content' => $this->get_smtp_form($record)
);
return $args;
}
function on_identity_update($args)
{
$rc = rcmail::get_instance();
// Do not do anything for default identity
if (strcasecmp($args['record']['email'], $rc->user->data['username']) === 0)
return $args;
if (!self::get_field_value('common', 'enabled', false))
{
self::sw_imap_off($args['id']);
return $args;
}
$data = self::check_field_values();
if (isset($data['err']))
{
$this->add_texts('localization');
$args['abort'] = true;
$args['message'] = 'ident_switch.err.' . $data['err'];
return $args;
}
$data['id'] = $args['id'];
self::save_field_values($rc, $data);
return $args;
}
function on_identity_create($args)
{
$rc = rcmail::get_instance();
// Do not do anything for default identity
if (strcasecmp($args['record']['email'], $rc->user->data['username']) === 0)
return $args;
if (!self::get_field_value('common', 'enabled', false))
return $args;
$data = self::check_field_values();
if ($data['err'])
{
$this->add_texts('localization');
$args['abort'] = true;
$args['message'] = 'ident_switch.err.' . $data['err'];
}
// Save data for _after (cannot pass with $args)
$_SESSION['createData' . self::MY_POSTFIX] = $data;
return $args;
}
function on_identity_create_after($args)
{
$rc = rcmail::get_instance();
// Do not do anything for default identity
if (strcasecmp($args['record']['email'], $rc->user->data['username']) === 0)
return $args;
$data = $_SESSION['createData' . self::MY_POSTFIX];
unset($_SESSION['createData' . self::MY_POSTFIX]);
if (!$data || count($data) == 0)
self::write_log("Object with ident_switch values not found in session for ID = {$args['id']}.");
else
{
$data['id'] = $args['id'];
self::save_field_values($rc, $data);
}
return $args;
}
function on_identity_delete($args)
{
$rc = rcmail::get_instance();
$sql = 'DELETE FROM ' . $rc->db->table_name(self::TABLE) . ' WHERE iid = ? AND user_id = ?';
$q = $rc->db->query($sql, $args['id'], $rc->user->ID);
if ($rc->db->affected_rows($q))
self::write_log("Deleted associated information for identity with ID = {$args['id']}.");
return $args;
}
function on_template_object_composeheaders($args)
{
if ($args['id'] == '_from')
{
$rc = rcmail::get_instance();
if (strcasecmp($_SESSION['username'], $rc->user->data['username']) !== 0)
{
if (isset($_SESSION['iid' . self::MY_POSTFIX]))
{
$iid = $_SESSION['iid' . self::MY_POSTFIX];
$rc->output->add_script("plugin_switchIdent_fixIdent({$iid});", 'docready');
}
else
self::write_log('Special session variable with active identity ID not found.');
}
}
}
function on_special_folders_form($args)
{
$rc = rcmail::get_instance();
if (($args['section'] == 'folders') &&
(strcasecmp($rc->user->data['username'], $_SESSION['username']) !== 0))
{
$no_override = array_flip((array)$rc->config->get('dont_override'));
$onchange = "if ($(this).val() == 'INBOX') $(this).val('')";
$select = rcmail_action::folder_selector(array('noselection' => '---',
'realnames' => true,
'maxlength' => 30,
'folder_filter' => 'mail',
'folder_rights' => 'w'));
$sql = 'SELECT label FROM ' . $rc->db->table_name(self::TABLE) . ' WHERE iid = ? AND user_id = ?';
$q = $rc->db->query($sql, $_SESSION['iid' . self::MY_POSTFIX], $rc->user->ID);
$r = $rc->db->fetch_assoc($q);
$args['blocks']['main']['name'] .= ' (' . ($r['label'] ? rcube::Q($rc->gettext('server')) . ': ' . $r['label'] : 'remote') . ')';
foreach (rcube_storage::$folder_types as $type)
{
if (isset($no_override[$type . '_mbox']))
continue;
$defaultKey = $type . '_mbox_default' . self::MY_POSTFIX;
$otherKey = $type . '_mbox' . self::MY_POSTFIX;
$selected = $_SESSION[$otherKey] ? $_SESSION[$otherKey] : $_SESSION[$defaultKey];
$attr = array('id' => '_' . $type . '_mbox', 'name' => '_' . $type . '_mbox', 'onchange' => $onchange);
$args['blocks']['main']['options'][$type . '_mbox']['content'] = $select->show($selected, $attr);
}
}
return $args;
}
function on_special_folders_update($args)
{
$rc = rcmail::get_instance();
if (($args['section'] == 'folders') &&
(strcasecmp($rc->user->data['username'], $_SESSION['username']) !== 0))
{
$sql = 'SELECT id FROM ' . $rc->db->table_name(self::TABLE) . ' WHERE iid = ? AND user_id = ?';
$q = $rc->db->query($sql, $_SESSION['iid' . self::MY_POSTFIX], $rc->user->ID);
$r = $rc->db->fetch_assoc($q);
if ($r)
{
$sql = 'UPDATE ' .
$rc->db->table_name(self::TABLE) .
' SET drafts_mbox = ?, sent_mbox = ?, junk_mbox = ?, trash_mbox = ?' .
' WHERE id = ?';
$rc->db->query(
$sql,
$args['prefs']['drafts_mbox'],
$args['prefs']['sent_mbox'],
$args['prefs']['junk_mbox'],
$args['prefs']['trash_mbox'],
$r['id']
);
//abuse $plugin['abort'] to prevent RC main from saving prefs
$args['abort'] = true;
$args['result'] = true;
foreach (rcube_storage::$folder_types as $type)
{
if ($args['prefs'][$type . '_mbox']) {
$otherKey = $type . '_mbox' . self::MY_POSTFIX;
$_SESSION[$otherKey] = $args['prefs'][$type . '_mbox'];
}
}
return $args;
}
$args['abort'] = true;
$args['result'] = false;
return $args;
}
foreach (rcube_storage::$folder_types as $type)
{
if ($args['prefs'][$type . '_mbox']) {
$key = $type . '_mbox_default' . self::MY_POSTFIX;
$_SESSION[$key] = $args['prefs'][$type . '_mbox'];
}
}
return $args;
}
function override_ldap_password($args)
{
$rc = rcmail::get_instance();
// Do not do anything for default identity
if (strcasecmp($args['user'], $rc->user->data['username']) === 0)
return $args;
$sql = 'SELECT password FROM ' . $rc->db->table_name(self::TABLE) . ' WHERE username = ?';
$q = $rc->db->query($sql, $args['user']);
$r = $rc->db->fetch_assoc($q);
if(is_array($r)) {
if($r['password']) {
self::write_log('Override imap password for user "' . $args['user'] . '"');
// Replace 'new_password' with the password you want to use
$args['pass'] = $rc->decrypt($r['password']);
}
}
return $args;
}
private static function check_field_values()
{
$retVal = array();
$retVal['label'] = self::get_field_value('common', 'label');
if (strlen($retVal['label']) > 32)
$retVal['err'] = 'label.long';
else
{
$retVal['imap.host'] = self::get_field_value('imap', 'host');
if (strlen($retVal['imap.host']) > 64)
$retVal['err'] = 'host.long';
else
{
$retVal['imap.port'] = self::get_field_value('imap', 'port');
if ($retVal['imap.port'] && !ctype_digit($retVal['imap.port']))
$retVal['err'] = 'port.num';
else
{
if (isset($retVal['imap.port']) && ($retVal['imap.port'] <= 0 || $retVal['imap.port'] > 65535))
$retVal['err'] = 'port.range';
else
{
$retVal['imap.delimiter'] = self::get_field_value('imap', 'delimiter');
if ($retVal['imap.delimiter'] && strlen($retVal['imap.delimiter']) > 1)
$retVal['err'] = 'delim.long';
else
{
$retVal['imap.user'] = self::get_field_value('imap', 'username');
if (strlen($retVal['imap.user']) > 64)
$retVal['err'] = 'user.long';
else
{
$retVal['smtp.host'] = self::get_field_value('smtp', 'host');
if (strlen($retVal['smtp.host']) > 64)
$retVal['err'] = 'host.long';
else
{
$retVal['smtp.port'] = self::get_field_value('smtp', 'port');
if ($retVal['smtp.port'] && !ctype_digit($retVal['smtp.port']))
$retVal['err'] = 'port.num';
else
{
if (isset($retVal['smtp.port']) && ($retVal['smtp.port'] <= 0 || $retVal['smtp.port'] > 65535))
$retVal['err'] = 'port.range';
else
{
$retVal['smtp.auth'] = self::get_field_value('smtp', 'auth');
if (!ctype_digit($retVal['smtp.auth']))
$retVal['err'] = 'auth.num';
}
}
}
}
}
}
}
}
}
// Get also password
$retVal['imap.pass'] = self::get_field_value('imap', 'password', false, true);
// Parse secure settings
$retVal['flags'] = self::DB_ENABLED;
$tls = self::get_field_value('imap', 'tls', false);
if ($tls)
$retVal['flags'] |= self::DB_SECURE_IMAP_TLS;
return $retVal;
}
private static function get_field_value($section, $field, $trim = true, $html = false)
{
$retVal = rcube_utils::get_input_value(
"_ident_switch_form_{$section}_{$field}",
rcube_utils::INPUT_POST,
$html
);
if (!$trim)
return $retVal;
if (is_null($retVal))
return null;
$s = trim($retVal);
if (!$s)
return null;
return $retVal;
}
private static function save_field_values($rc, $data)
{
$sql = 'SELECT id, password FROM ' . $rc->db->table_name(self::TABLE) . ' WHERE iid = ? AND user_id = ?';
$q = $rc->db->query($sql, $data['id'], $rc->user->ID);
$r = $rc->db->fetch_assoc($q);
if ($r)
{ // Record already exists, will update it
$sql = 'UPDATE ' .
$rc->db->table_name(self::TABLE) .
' SET flags = ?, label = ?, imap_host = ?, imap_port = ?, imap_delimiter = ?,'.
' username = ?, password = ?, smtp_host = ?, smtp_port = ?, smtp_auth = ?,'.
' user_id = ?, iid = ?' .
' WHERE id = ?';
}
else if ($data['flags'] & self::DB_ENABLED)
{ // No record exists, create new one
$sql = 'INSERT INTO ' .
$rc->db->table_name(self::TABLE) .
'(flags, label, imap_host, imap_port, imap_delimiter, username, password,'.
' smtp_host, smtp_port, smtp_auth, user_id, iid)'.
' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
}
if ($sql)
{
// Do we need to update pwd?
if ($data['imap.pass'] != $r['password'])
$data['imap.pass'] = $rc->encrypt($data['imap.pass']);
$rc->db->set_debug(true);
$rc->db->query(
$sql,
$data['flags'],
$data['label'],
$data['imap.host'],
$data['imap.port'],
$data['imap.delimiter'],
$data['imap.user'],
$data['imap.pass'],
$data['smtp.host'],
$data['smtp.port'],
$data['smtp.auth'],
$rc->user->ID,
$data['id'],
$r['id']
);
return true;
}
return false;
}
function on_switch()
{
$rc = rcmail::get_instance();
$my_postfix_len = strlen(self::MY_POSTFIX);
$identId = rcube_utils::get_input_value('_ident-id', rcube_utils::INPUT_POST);
$rc->session->remove('folders');
$rc->session->remove('unseen_count');
if (-1 == $identId)
{ // Switch to main account
self::write_log('Switching mailbox back to default.');
// Restore everything with STORAGE*my_postfix
foreach ($_SESSION as $k => $v)
{
if (strncasecmp($k, 'storage', 7) === 0 && substr_compare($k, self::MY_POSTFIX, -$my_postfix_len, $my_postfix_len) === 0)
{
$realKey = substr($k, 0, -$my_postfix_len);
$_SESSION[$realKey] = $_SESSION[$k];
$rc->session->remove($k);
}
}
$v; // disable Eclipse warning
if (!($delimiter = $rc->config->get('imap_delimiter')))
$delimiter = '.';
$_SESSION['imap_delimiter'] = $delimiter;
$_SESSION['username'] = $rc->user->data['username'];
$_SESSION['password'] = $_SESSION['password' . self::MY_POSTFIX];
$_SESSION['iid' . self::MY_POSTFIX] = -1;
foreach (rcube_storage::$folder_types as $type)
{
$otherKey = $type . '_mbox' . self::MY_POSTFIX;
if (isset($_SESSION[$otherKey]))
$rc->session->remove($otherKey);
}
}
else
{
$sql = 'SELECT imap_host, flags, imap_port, imap_delimiter, drafts_mbox, sent_mbox, junk_mbox, trash_mbox, username, password, iid FROM ' . $rc->db->table_name(self::TABLE) . ' WHERE id = ? AND user_id = ?';
$q = $rc->db->query($sql, $identId ,$rc->user->ID);
$r = $rc->db->fetch_assoc($q);
if (is_array($r))
{
if (!$r['username'])
{ // Load email from identity
$sql = 'SELECT email FROM ' . $rc->db->table_name('identities') . ' WHERE identity_id = ?';
$q = $rc->db->query($sql, $r['iid']);
$rIid = $rc->db->fetch_assoc($q);
$r['username'] = $rIid['email'];
}
self::write_log("Switching mailbox to one for identity with ID = {$r['iid']} (username = '{$r['username']}').");
if ($_SESSION['username'] == $rc->user->data['username'])
{ // If we are in default account now - save values
foreach ($_SESSION as $k => $v)
{
if (strncasecmp($k, 'storage', 7) === 0 && substr_compare($k, self::MY_POSTFIX, -$my_postfix_len, $my_postfix_len) !== 0)
{
if (!isset($_SESSION[$k . self::MY_POSTFIX]))
$_SESSION[$k . self::MY_POSTFIX] = $_SESSION[$k];
$rc->session->remove($k);
}
}
$moreToSave = array('password', 'imap_delimiter');
foreach ($moreToSave as $k)
{
if (!isset($_SESSION[$k . self::MY_POSTFIX]))
$_SESSION[$k . self::MY_POSTFIX] = $_SESSION[$k];
$rc->session->remove($k);
}
}
$def_port = 143; // Default IMAP port here!
$ssl = null;
if ($r['flags'] & self::DB_SECURE_IMAP_TLS)
{
$ssl = 'tls';
$def_port = 143; // Default IMAP TLS port here!
}
$port = $r['imap_port'] ? $r['imap_port'] : $def_port;
$host = $r['imap_host'] ? $r['imap_host'] : 'localhost'; // Default IMAP host here!
$hostProtocol = "{$ssl}://";
if ($ssl && strncasecmp($host, $hostProtocol, strlen($hostProtocol)) !== 0)
$host = $hostProtocol . $host;
$delimiter = isset($r['imap_delimiter']) ? $r['imap_delimiter'] : $rc->config->get('imap_delimiter'); // Default delimiter here
$_SESSION['storage_host'] = $host;
$_SESSION['storage_ssl'] = $ssl;
$_SESSION['storage_port'] = $port;
$_SESSION['imap_delimiter'] = $delimiter;
$_SESSION['username'] = $r['username'];
$_SESSION['password'] = $r['password'];
$_SESSION['iid' . self::MY_POSTFIX] = $r['iid'];
foreach (rcube_storage::$folder_types as $type)
{
if (isset($r[$type . '_mbox'])) {
$otherKey = $type . '_mbox' . self::MY_POSTFIX;
$_SESSION[$otherKey] = $r[$type . '_mbox'];
}
}
}
else
{
// TODO: Show message in browser
self::write_log("Requested remote mailbox with ID = {$identId} not found.");
return;
}
}
$rc->output->redirect(
array(
'_task' => 'mail',
'_mbox' => 'INBOX',
)
);
}
private static function sw_imap_off($iid)
{
$rc = rcmail::get_instance();
$sql = 'UPDATE ' . $rc->db->table_name(self::TABLE) . ' SET flags = flags & ? WHERE iid = ? AND user_id = ?';
$rc->db->query($sql, ~self::DB_ENABLED, $iid, $rc->user->ID);
}
private function get_preconfig($email)
{
$dom = substr(strstr($email, '@'), 1);
if (!$dom)
return false;
$this->load_config(); // config.inc.php
$cfg = rcmail::get_instance()->config->get('ident_switch.preconfig', array());
$cfg = isset($cfg[$dom]) ? $cfg[$dom] : false;
if ($cfg)
{
if (!$cfg['host'])
return false; # Host must be specified!
}
return $cfg;
}
private function apply_preconfig(&$record)
{
if (!isset($record['email']))
return false;
$email = $record['email'];
$cfg = $this->get_preconfig($email);
if (is_array($cfg))
{
self::write_log("Applying predefined configuration for '{$email}'.");
if ($cfg['host'])
{ // Parse and set host and related
$urlArr = parse_url($cfg['host']);
$record['ident_switch.form.imap.host'] = $record['ident_switch.form.smtp.host'] = $urlArr['host'] ? rcube::Q($urlArr['host'], 'url') : '';
$record['ident_switch.form.imap.port'] = $record['ident_switch.form.smtp.port'] = $urlArr['port'] ? intval($urlArr['port']) : '';
if (strcasecmp('tls', $urlArr['scheme']) === 0)
$record['ident_switch.form.imap.tls'] = true;
else
$record['ident_switch.form.imap.rls'] = false;
}
$loginSet = false;
if ($cfg['user'])
{ // Set up user name
switch (strtoupper($cfg['user']))
{
case 'EMAIL':
$record['ident_switch.form.imap.username'] = $email;
$loginSet = true;
break;
case 'MBOX':
$record['ident_switch.form.imap.username'] = strstr($email, '@', true);
$loginSet = true;
break;
}
}
if ($cfg['readonly'])
{
$record['ident_switch.form.common.readonly'] = 1;
if ($loginSet)
$record['ident_switch.form.common.readonly'] = 2;
}
return $cfg['readonly'];
}
return false;
}
private static function write_log($txt)
{
if (self::$logging)
rcmail::get_instance()->write_log('ident_switch', $txt);
}
}