tiki-webmail.php 27.8 KB
Newer Older
lrargerich's avatar
lrargerich committed
1
<?php
2

3
// $Id$
4

5
// Copyright (c) 2002-2007, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
6 7 8
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.

lrargerich's avatar
lrargerich committed
9
// Initialization
10
$section = 'webmail';
11
require_once ('tiki-setup.php');
12
if ($prefs['feature_ajax'] == 'y') {
13
	require_once ('lib/ajax/ajaxlib.php');
14
}
15
include_once ('lib/webmail/webmaillib.php');
16
include_once ('lib/webmail/contactlib.php');
lrargerich's avatar
lrargerich committed
17

18
if ($prefs['feature_webmail'] != 'y') {
19
	$smarty->assign('msg', tra('This feature is disabled').': feature_webmail');
20

21
	$smarty->display('error.tpl');
22
	die;
lrargerich's avatar
lrargerich committed
23 24
}

25
if ($tiki_p_use_webmail != 'y' && $tiki_p_use_group_webmail != 'y') {
26
	$smarty->assign('errortype', 401);
27
	$smarty->assign('msg', tra('Permission denied to use this feature'));
28

29
	$smarty->display('error.tpl');
30
	die;
lrargerich's avatar
lrargerich committed
31 32
}

33 34 35 36
require_once ('lib/webmail/net_pop3.php');
require_once ('lib/mail/mimelib.php');
include_once ('lib/webmail/class.rc4crypt.php');
include_once ('lib/webmail/tikimaillib.php');
37

38 39 40 41 42 43 44 45 46 47 48
function handleWebmailRedirect($inUrl) {		// TODO refactor into tikilib?
	global $prefs;
	
	if ($prefs['feature_ajax'] != 'y' || empty($_REQUEST['xjxfun'])) {
		header ('location: tiki-webmail.php?'.$inUrl);
		die();
	} else {
	    global $ajaxlib, $headerlib;
//			$objResponse = new xajaxResponse('UTF-8');					// should be possible server-side, no?
//			$objResponse->Redirect('tiki-webmail.php?'.$urlq);
	    $headerlib->add_js('window.location.replace("tiki-webmail.php?'.$inUrl.'")');
49 50 51
	    $ajaxlib->registerTemplate('tiki-webmail.tpl');
//   	    $ajaxlib->registerTemplate('error.tpl');
//	    $ajaxlib->registerFunction('loadComponent');
52 53 54 55 56 57
		$ajaxlib->processRequests();
		die();
	}
	
}

58
if (!$user) {
59
	$smarty->assign('msg', tra('You are not logged in'));
lrargerich's avatar
lrargerich committed
60

61
	$smarty->display('error.tpl');
62 63
	die;
}
lrargerich's avatar
lrargerich committed
64

65 66 67 68 69 70 71
$auto_query_args = array(
    'msgid',
	'locSection',
	'filter'
);


72 73
if (!isset($_REQUEST['locSection'])) {
	$_REQUEST['locSection'] = 'mailbox';
lrargerich's avatar
lrargerich committed
74
}
75
$headerlib->add_js('var webmailTimeoutId = null;',0);
76

77
$smarty->assign('locSection', $_REQUEST['locSection']);
lrargerich's avatar
lrargerich committed
78
// Search if we have to add some contacts
79 80
if (isset($_REQUEST['add_contacts'])) {
	if (isset($_REQUEST['add'])) {
81
		check_ticket('webmail');
82 83 84
		foreach (array_keys($_REQUEST['add'])as $i) {
			$contactlib->replace_contact(0, $_REQUEST['addFirstName'][$i], $_REQUEST['addLastName'][$i], $_REQUEST['addemail'][$i],
				$_REQUEST['addNickname'][$i], $user);
85 86
		}
	}
luciash's avatar
luciash committed
87
}
lrargerich's avatar
lrargerich committed
88

89 90 91
///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// Read an Email ////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
92

93 94
if ($_REQUEST['locSection'] == 'read') {
	if (isset($_REQUEST['fullheaders'])) {
95 96 97 98
		$smarty->assign('fullheaders', 'y');
	} else {
		$smarty->assign('fullheaders', 'n');
	}
99 100
	$headerlib->add_js('if (webmailTimeoutId) {window.clearTimeout(webmailTimeoutId);}',0);
	
101
	$current = $webmaillib->get_current_webmail_account($user);
102 103
	
	
104
	$smarty->assign_by_ref('current', $current);
105 106 107 108 109 110

	// connecting with Zend
	try {
		$mail = $webmaillib->get_mail_storage($current);
	} catch (Exception $e) {
		// do something better with the error
111
		$smarty->assign('conmsg', tra('There are a problem connecting to that account.').'<br />'.$e->getMessage());
112
	}
113

114
	if (isset($_REQUEST['delete_one'])) {
115
		check_ticket('webmail');
116
		$aux = $webmail_list[$_REQUEST['msgdel']-1];
117
		try {
118 119 120
			$mail->removeMessage($_REQUEST['msgdel']);
			$webmaillib->remove_webmail_message($current['accountId'], $user, $aux['realmsgid']);
			unset($_REQUEST['msgid']);
121 122 123
		} catch (Exception $e) {
			$smarty->assign('conmsg', tra('There are a problem deleting that mail.').'<br />'.$e->getMessage());
		}
124 125
	}

126 127
	if (isset($_REQUEST['msgid'])) {
		$message = $mail->getMessage($_REQUEST['msgid']);
128
		$aux = $message->getHeaders();
129 130
		$realmsgid = ereg_replace('[<>]','',$aux['message-id']);
		$smarty->assign('msgid', $_REQUEST['msgid']);
131
		$smarty->assign('realmsgid', $realmsgid);
132
		$webmaillib->set_mail_flag($current['accountId'], $user, $realmsgid, 'isRead', 'y');
133
		$mailsum = $mail->countMessages();
134
		$numshow = $current['msgs'];
135
	
136
		if ($_REQUEST['msgid'] == $mailsum) {
137 138
			$smarty->assign('next', '');
		} else {
139
			$smarty->assign('next', $_REQUEST['msgid'] + 1);
140
		}
141
	
142 143
		if ($_REQUEST['msgid'] > 1) {
			$smarty->assign('prev', $_REQUEST['msgid'] - 1);
144 145 146 147 148 149 150 151 152 153 154
		} else {
			$smarty->assign('prev', '');
		}
	
	
		$attachments = array();
		
		if ($message->isMultipart()) {
			// TODO	deal with attachments here??	
		}
		
155
		$bodies = $webmaillib->get_mail_content($user, $current['accountId'], $_REQUEST['msgid'], true);
156 157 158 159 160 161
		for ($i = 0; $i < count($bodies); $i++) {
			if ($bodies[$i]['contentType'] == 'text/html') {
				
				$bod = $bodies[$i]['body'];
				
				// Clean the string using HTML Purifier
162
				require_once('lib/htmlpurifier_tiki/HTMLPurifier.tiki.php');
163 164 165 166 167 168 169 170 171 172
				$bod = HTMLPurifier($bod);
				
				if (preg_match_all('/<[\/]?body[^>]*>/i', $bod, $m, PREG_OFFSET_CAPTURE) && count($m) > 0 && count($m[0]) > 1) {
					// gets positions of the start and end body tags then substr the bit inbetween
					$bod = substr($bod, $m[0][0][1] + strlen($m[0][0][0]), $m[0][1][1]);
				}
				$bod = strip_tags( $bod, '<a><b><i><table><tbody><tr><td><th><ul><li><img><hr><ol><br /><h1><h2><h3><h4><h5><h6><div><span><font><form><input><textarea><checkbox><select><style>');
				// try to close malformed html not fixed by the purifier - because people email Really Bad Things and this messes up *lite.css layout
				$bod = closetags($bod);
				$bodies[$i]['body'] = $bod;
173
			
174 175 176 177 178 179
			} else if ($bodies[$i]['contentType'] == 'text/plain') {
				// reply text
				$smarty->assign('plainbody', format_email_reply($bodies[$i]['body'], $aux['from'], $aux['date']));
				$bodies[$i]['body'] = nl2br( $bodies[$i]['body'] );
			} else {
				// attachments?
180 181
			}
		}
182 183
		
		array_multisort($bodies);	// this doesn't do what we need properly but seems to fluke it mostly - TODO a manual re-sort
184
	
185 186 187 188 189 190 191
		$smarty->assign_by_ref('attachs', $attachments);
		$smarty->assign_by_ref('bodies', $bodies);
	
		try {
			$allbodies = $message->getContent();
		} catch (Exception $e) {
			$allbodies = $e->getMessage();		
192
		}
193 194 195 196 197 198
	
		$smarty->assign('allbodies', htmlspecialchars($allbodies));
	
		// collect addresses for reply
		$to_addresses = $aux['from'];
	
199
		// Get email addresses from the 'from' portion
200 201 202
		$to_addresses = split(',', $to_addresses);
	
		$temp_max = count($to_addresses);
203
		for ($i = 0; $i < $temp_max; $i++) {
204
			preg_match('/<([^>]+)>/', $to_addresses[$i], $add);
205
	
206
			if (isset($add[1])) {
207
				$to_addresses[$i] = $add[1];
208 209
			}
		}
210 211
	
		if (isset($aux['cc']) || ereg(',', $aux['to'])) {
212
			$cc_addresses = '';
213 214 215 216
	
			if (isset($aux['cc']))
				$cc_addresses .= $aux['cc'];
	
217
			//add addresses to cc from 'to' field (for 'reply to all')
218 219 220 221 222 223 224 225
			if ($cc_addresses != '')
				$cc_addresses .= ',';
	
			$cc_addresses .= $aux['to'];
			$cc_addresses = split(',', $cc_addresses);
	
			$temp_max = count($cc_addresses);
			for ($i = 0; $i < $temp_max; $i++) {
226
				preg_match('/<([^>]+)>/', $cc_addresses[$i], $add);
227 228 229 230 231 232 233 234 235 236 237 238 239
	
				if (isset($add[1])) {
					$cc_addresses[$i] = $add[1];
				}
			}
		} else {
			$cc_addresses = array();
		}
	
		$to_addresses = join(',', $to_addresses);
		$cc_addresses = join(',', $cc_addresses);
	
		if (isset($aux['reply-to'])) {
240
			$aux['replyto'] = $aux['reply-to'];
241
	
242
			$aux['replycc'] = $cc_addresses;
243
		} else {
244
			$aux['replycc'] = $cc_addresses;
245
	
246
			$aux['replyto'] = $to_addresses;
247
		}
248
		if (!isset($aux['delivery-date'])) {
249 250 251 252
			$aux['delivery-date'] = $aux['date'];
		}
		$aux['timestamp'] = strtotime($aux['delivery-date']);
		
253 254 255 256 257
		$aux['subject'] = isset($aux['subject']) ? utf8_encode($aux['subject']) : '';
		$aux['from']    = isset($aux['from'])    ? utf8_encode($aux['from']) : '';
		$aux['to']      = isset($aux['to'])      ? utf8_encode($aux['to']) : '';
		$aux['cc']      = isset($aux['cc'])      ? utf8_encode($aux['cc']) : '';
		$aux['date']    = isset($aux['date'])    ? utf8_encode($aux['date']) : '';
258 259 260
			
		$smarty->assign('headers', $aux);
		
261 262
	} else {	// $_REQUEST['msgid'] unset by delete
		handleWebmailRedirect('locSection=mailbox');
263
	}
lrargerich's avatar
lrargerich committed
264 265
}

266 267 268 269
///////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Mailbox ////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

270
if ($_REQUEST['locSection'] == 'mailbox') {
271
	
272 273
	$current = $webmaillib->get_current_webmail_account($user);
	if (!$current) {
274
		handleWebmailRedirect('locSection=settings');
275 276 277
	}

	$autoRefresh = $current['autoRefresh'];
278 279 280 281 282 283 284 285
	$js = <<< END
function submit_form(msgname,flg)
{
  document.mailb.elements.quickFlag.value= flg;
  document.mailb.elements.quickFlagMsg.value= msgname;
  document.mailb.submit();
}
END;
286 287 288 289

	if ($autorefresh > 0) {
		$js .= 'webmailTimeoutId = window.setTimeout("window.location.reload(true);",$autoRefresh*1000);';
	}
290 291
	$headerlib->add_js($js,0);

292
	$h = opendir('temp/mail_attachs/');
293 294 295 296 297 298 299 300 301 302

	while ($file = readdir($h)) {
		if (substr($file, 0, strlen($user)) == $user) {
			@unlink ('temp/mail_attachs/' . $file);
		}
	}

	closedir ($h);

	$smarty->assign('current', $current);
303
	$smarty->assign('autoRefresh',$current['autoRefresh']);
304 305 306
	$smarty->assign('imap',$current['imap']);
	$smarty->assign('mbox',$current['mbox']);
	$smarty->assign('maildir',$current['maildir']);
307
	$smarty->assign('useSSL',$current['useSSL']);
308
	$smarty->assign('flagsPublic',$current['flagsPublic']);
309
	
310 311
	$webmail_reload = isset($_REQUEST['refresh_mail']);
	
312
	try {
313
		$webmail_list = $webmaillib->refresh_mailbox($user, $current['accountId'], $webmail_reload);
314 315
	} catch (Exception $e) {
		$err = $e->getMessage();
316

317
		$urlq = http_build_query(array('locSection'=>'settings', 'conmsg'=>$err),'','&');
318
		handleWebmailRedirect($urlq);
319
		return;
320
	}
321

322 323 324 325 326 327 328 329
	// connecting with Zend
	try {
		$mail = $webmaillib->get_mail_storage($current);
	} catch (Exception $e) {
		// do something better with the error
		$smarty->assign('conmsg', tra('There are a problem connecting to that account.').'<br />'.$e->getMessage());
	}

330
	// The user just clicked on one of the flags, so set up for flag change
331 332 333 334 335
	if (isset($_REQUEST['quickFlagMsg'])){
		$realmsg = $_REQUEST['quickFlagMsg'];
		switch ($_REQUEST['quickFlag']) {
		case 'y':
			$webmaillib->set_mail_flag($current['accountId'], $user, $realmsg, 'isFlagged', 'y');
336 337 338

			break;

339 340
		case 'n':
			$webmaillib->set_mail_flag($current['accountId'], $user, $realmsg, 'isFlagged', 'n');
341 342 343 344 345

			break;
		}
	}

346 347
	if (isset($_REQUEST['delete'])) {
		if (isset($_REQUEST['msg'])) {
348
			check_ticket('webmail');
349
			// Now we can delete the messages
350
			$err = '';
351
			foreach ($_REQUEST['msg'] as $msg) {
352 353 354 355 356 357 358 359 360 361 362 363
				$aux = $webmail_list[$msg-1];
				$realmsgid = $aux['realmsgid'];
				try {
					$mail->removeMessage($msg);
					$webmaillib->remove_webmail_message($current['accountId'], $user, $realmsgid);
					//$pop3->deleteMsg($msg);
				} catch (Exception $e) {
					$err .= $e->getMessage().' ('.tra('Mail ID').' '.$msg.')<br />';
				}
			}
			if (!empty($err)) {
				$smarty->assign('conmsg', tra('There are a problem deleting mails.').'<br />'.$err);
364 365 366 367
			}
		}
	}

368
	if (isset($_REQUEST['delete_one'])) {	// currently unused?
369
		check_ticket('webmail');
370 371
		$aux = $webmail_list[$_REQUEST['msgdel']-1];
		$webmaillib->remove_webmail_message($current['accountId'], $user, $aux['realmsgid']);
372
		try {
373
			$mail->removeMessage($_REQUEST['msgdel']);
374 375 376
		} catch (Exception $e) {
			$smarty->assign('conmsg', tra('There are a problem deleting that mail.').'<br />'.$e->getMessage());
		}
377
	}
378 379
	
	
380
	if (isset($_REQUEST['delete_one']) || isset($_REQUEST['delete'])) {
381 382
		// Now reopen the mailbox to renumber messages
		try {
383
			$webmail_list = $webmaillib->refresh_mailbox($user, $current['accountId'], true);	// really need a smarter way of caching the whole mailbox...
384 385 386 387 388 389 390
		} catch (Exception $e) {
			$err = $e->getMessage();
	
			$urlq = http_build_query(array('locSection'=>'settings', 'conmsg'=>$err),'','&');
			handleWebmailRedirect($urlq);
			return;
		}
391

392
	}
393
	$mailsum = count($webmail_list);
394

395 396
	if (isset($_REQUEST['operate'])) {
		if (isset($_REQUEST['msg'])) {
397
			check_ticket('webmail');
398
			// Now we can operate the messages
399
			foreach ($_REQUEST['msg'] as $msg) {
400 401 402
				$aux = $webmail_list[$msg-1];
				$realmsg = $aux['realmsgid'];
				
403
				switch ($_REQUEST['action']) {
404
					
405 406
				case 'flag':
					$webmaillib->set_mail_flag($current['accountId'], $user, $realmsg, 'isFlagged', 'y');
407 408
					break;

409 410
				case 'unflag':
					$webmaillib->set_mail_flag($current['accountId'], $user, $realmsg, 'isFlagged', 'n');
411 412
					break;

413 414
				case 'read':
					$webmaillib->set_mail_flag($current['accountId'], $user, $realmsg, 'isRead', 'y');
415 416
					break;

417 418
				case 'unread':
					$webmaillib->set_mail_flag($current['accountId'], $user, $realmsg, 'isRead', 'n');
419 420 421 422 423 424
					break;
				}
			}
		}
	}

425
	$numshow = $current['msgs'];
426

427 428
	if (isset($_REQUEST['start']) && $_REQUEST['start'] > $mailsum)
		$_REQUEST['start'] = $mailsum;
429

430
	if (!isset($_REQUEST['filter']))
431 432
		$smarty->assign('filter', '');
	else
433
		$smarty->assign('filter', $_REQUEST['filter']);
434 435

	// If we have a filter then we have to
436
	if (!empty($_REQUEST['filter'])) {
437 438
		$tot = 0;

439
		$aux['msgid'] = 'foo';
440 441 442
		$filtered = array();
		$filtered[] = $aux;

443
		for ($i = 0; $i < $mailsum; $i++) {
444
			$aux = $webmail_list[$i];
445 446 447
			$webmaillib->replace_webmail_message($current['accountId'], $user, $aux['realmsgid']);
			list($aux['isRead'], $aux['isFlagged'], $aux['isReplied'])
				= $webmaillib->get_mail_flags($current['accountId'], $user, $aux['realmsgid']);
448

449
			if ($_REQUEST['filter'] == 'unread' && $aux['isRead'] == 'n') {
450 451
				$tot++;
				$filtered[] = $aux;
452
			} elseif ($_REQUEST['filter'] == 'flagged' && $aux['isFlagged'] == 'y') {
453 454 455 456 457 458 459 460
				$tot++;
				$filtered[] = $aux;
			}
		}

		$mailsum = count($filtered) - 1;
	}

461 462
	if (!isset($_REQUEST['start']))
		$_REQUEST['start'] = $mailsum;
463

464 465
	$upperlimit = $_REQUEST['start'];
	$smarty->assign('start', $_REQUEST['start']);
466
	$webmail_list_page = array();
467

468
	for ($i = $upperlimit; $i > 0 && count($webmail_list_page) < $numshow; $i--) {
469
		if (!empty($_REQUEST['filter'])) {
470 471
			$aux = $filtered[$i];
		} else {
472
			$aux = $webmail_list[$i-1];
473 474
			$webmaillib->replace_webmail_message($current['accountId'], $user, $aux['realmsgid']);
			list($aux['isRead'], $aux['isFlagged'], $aux['isReplied']) = $webmaillib->get_mail_flags($current['accountId'], $user, $aux['realmsgid']);
475
		}
476
		$webmail_list_page[] = $aux;
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
	}
	$lowerlimit = $i;

	if ($lowerlimit < 0)
		$lowerlimit = 0;

	$showstart = $mailsum - $upperlimit + 1;
	$showend = $mailsum - $lowerlimit;
	$smarty->assign('showstart', $showstart);
	$smarty->assign('showend', $showend);
	$smarty->assign('total', $mailsum);

	if ($lowerlimit > 0) {
		$smarty->assign('nextstart', $lowerlimit);
	} else {
		$smarty->assign('nextstart', '');
	}

	if ($upperlimit <> $mailsum) {
		$prevstart = $upperlimit + $numshow;

		if ($prevstart > $mailsum)
			$prevstart = $mailsum;

		$smarty->assign('prevstart', $prevstart);
	} else {
		$smarty->assign('prevstart', '');
	}

506
	if ($_REQUEST['start'] <> $mailsum) {
507 508 509 510 511 512 513 514
		$smarty->assign('first', $mailsum);
	} else {
		$smarty->assign('first', '');
	}

	// Now calculate the last message block
	$last = $mailsum % $numshow;

515
	if ($_REQUEST['start'] <> $last) {
516 517 518 519 520
		$smarty->assign('last', $last);
	} else {
		$smarty->assign('last', '');
	}

521
	$smarty->assign('list', $webmail_list_page);
lrargerich's avatar
lrargerich committed
522 523
}

524 525 526 527
///////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Settings //////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

528
if ($_REQUEST['locSection'] == 'settings') {
529

530 531 532 533
	if ($prefs['feature_jquery']) {
		$deleteTitle = tra('Delete');
		$deleteConfirm = tra('Are you sure you want to delete this account?');
		$js = <<< END
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

// validate edit/add form
\$jq('[name=settings]').submit(function() {
	if (!\$jq('[name=account]').val()) {
		\$jq('[name=account]').css('background-color', '#fcc').focus();
		return false;
	}
	if (!\$jq('[name=imap]').val() && !\$jq('[name=pop]').val() && !\$jq('[name=mbox]').val() && !\$jq('[name=maildir]').val()) {
		\$jq('[name=imap]').css('background-color', '#fcc').focus();
		\$jq('[name=pop]').css('background-color', '#fcc');
		\$jq('[name=mbox]').css('background-color', '#fcc');
		\$jq('[name=maildir]').css('background-color', '#fcc');
		return false;
	}
});
// set port for imap
\$jq('[name=imap]').change(function() {
	if (\$jq('[name=imap]').val()) {
		\$jq('[name=port]').val(\$jq('[name=useSSL]').attr('checked')? '993' : '143');
	}
});
// set port for pop
\$jq('[name=pop]').change(function() {
	if (\$jq('[name=pop]').val() && !\$jq('[name=imap]').val()) {
		\$jq('[name=port]').val(\$jq('[name=useSSL]').attr('checked')? '995' : '110');
	}
});
// set ports for ssl
\$jq('[name=useSSL]').change(function(v,a) {
	if (\$jq('[name=useSSL]').attr('checked')) {
		\$jq('[name=port]').val(\$jq('[name=imap]').val() ? '933' : '995');
		\$jq('[name=smtpPort]').val('465');
	} else {
		\$jq('[name=port]').val(\$jq('[name=imap]').val() ? '143' : '110');
		\$jq('[name=smtpPort]').val('25');
	}
});
// confirm deletes
\$jq('a[title=$deleteTitle]').click(function() {
	return confirm('$deleteConfirm');
});
// open/close account form
\$jq('#addAccountIcon').click(function() {
	flip(\$jq('#settingsFormDiv').attr('id'));
}).css("cursor", "pointer");
579 580

END;
581
		$headerlib->add_jq_onready($js);
582
	}
583
	$headerlib->add_js('if (webmailTimeoutId) {window.clearTimeout(webmailTimeoutId);}',0);
584
	
585
	if (isset($_REQUEST['conmsg'])) {
586
		check_ticket('webmail');
587
 		$smarty->assign('conmsg', $_REQUEST['conmsg']);
588
	}
589
	
590
	if (isset($_REQUEST['cancel_acc'])) {
591
		check_ticket('webmail');
592 593
	 	unset($_REQUEST['cancel_acc']);
		unset($_REQUEST['accountId']);
594 595
	}

596
	// The New/Update button was pressed
597
	if (isset($_REQUEST['new_acc'])) {
598 599
		check_ticket('webmail');
		
600
		if (empty($_REQUEST['accountId'])) {
601
			// Add new account
602 603 604 605 606
			$_REQUEST['accountId'] = $webmaillib->new_webmail_account($user,
					$_REQUEST['account'], $_REQUEST['pop'], $_REQUEST['port'], $_REQUEST['username'],
					$_REQUEST['pass'], $_REQUEST['msgs'], $_REQUEST['smtp'], $_REQUEST['useAuth'],
					$_REQUEST['smtpPort'], $_REQUEST['flagsPublic'], $_REQUEST['autoRefresh'],
					$_REQUEST['imap'], $_REQUEST['mbox'], $_REQUEST['maildir'], isset($_REQUEST['useSSL']) ? $_REQUEST['useSSL'] : 'n');
607 608

			if ($webmaillib->count_webmail_accounts($user) == 1) {	// first account?
609
				$webmaillib->current_webmail_account($user, $_REQUEST['accountId']);
610
			}
611 612 613
			
		} else {
			// Update existing account
614 615 616 617 618
			$webmaillib->replace_webmail_account($_REQUEST['accountId'], $user,
					$_REQUEST['account'], $_REQUEST['pop'], $_REQUEST['port'], $_REQUEST['username'],
					$_REQUEST['pass'], $_REQUEST['msgs'], $_REQUEST['smtp'], $_REQUEST['useAuth'],
					$_REQUEST['smtpPort'], $_REQUEST['flagsPublic'], $_REQUEST['autoRefresh'],
					$_REQUEST['imap'], $_REQUEST['mbox'], $_REQUEST['maildir'], isset($_REQUEST['useSSL']) ? $_REQUEST['useSSL'] : 'n');
619
		}
620
		unset($_REQUEST['accountId']);
621 622
	}
	
623
	if (empty($_REQUEST['accountId']) || isset($_REQUEST['new_acc']) && $webmaillib->count_webmail_accounts($user) > 0) {
624 625
		$headerlib->add_jq_onready('$jq("#settingsFormDiv").hide();');
	}
626
	// The red cross was pressed
627
	if (isset($_REQUEST['remove'])) {
628
		check_ticket('webmail');
629
		$webmaillib->remove_webmail_account($user, $_REQUEST['remove']);
630 631
	}

632 633
	if (isset($_REQUEST['current'])) {
		$webmaillib->current_webmail_account($user, $_REQUEST['current']);
634
		$headerlib->add_js('if (typeof doRefreshWebmail == "function") { doRefreshWebmail(); }');
635 636
	}

637
	$smarty->assign('mailCurrentAccount', $tikilib->get_user_preference($user, 'mailCurrentAccount', 0));
638

639
	$smarty->assign('accountId', empty($_REQUEST['accountId']) ? 0 : $_REQUEST['accountId']);
640 641


642 643
	if ($_REQUEST['accountId']) {
		$info = $webmaillib->get_webmail_account($user, $_REQUEST['accountId']);
644
	} else {
645 646 647 648 649 650 651 652 653 654 655
		$info['account'] = '';
		$info['username'] = '';
		$info['pass'] = '';
		$info['pop'] = '';
		$info['smtp'] = '';
		$info['useAuth'] = 'n';
		$info['port'] = 110;
		$info['smtpPort'] = 25;
		$info['msgs'] = 20;
		$info['flagsPublic'] = 'n';
		$info['autoRefresh'] = 0;
656 657 658 659
		$info['imap'] = '';
		$info['mbox'] = '';
		$info['maildir'] = '';
		$info['useSSL'] = 'n';
660 661 662 663 664
	}

	$smarty->assign('info', $info);
	// List
	$accounts = $webmaillib->list_webmail_accounts($user, 0, -1, 'account_asc', '');
665
	$smarty->assign('accounts', $accounts['data']);
666 667
	
	$pubAccounts = $webmaillib->list_webmail_group_accounts($user, 0, -1, 'account_asc', '');
668
	$smarty->assign('pubAccounts', $pubAccounts['data']);
lrargerich's avatar
lrargerich committed
669 670
}

671 672 673 674
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////// Compose /////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

675
if ($_REQUEST['locSection'] == 'compose') {
676 677 678
	$current = $webmaillib->get_current_webmail_account($user);

	if (!$current) {
679
		handleWebmailRedirect('locSection=settings');
680
	}
681 682
	$headerlib->add_js('if (webmailTimeoutId) {window.clearTimeout(webmailTimeoutId);}',0);
	
683
	// Send a message
684
	if (isset($_REQUEST['reply']) || isset($_REQUEST['replyall'])) {
685
		check_ticket('webmail');
686
		$webmaillib->set_mail_flag($current['accountId'], $user, $_REQUEST['realmsgid'], 'isReplied', 'y');
687 688 689 690 691
	}

	$smarty->assign('sent', 'n');
	$smarty->assign('attaching', 'n');

692
	if (isset($_REQUEST['send'])) {
693
		$mail = new TikiMail($user);
694

695 696
		$email = $userlib->get_user_email($user);
		$mail->setFrom($email);
697 698
		if (!empty($_REQUEST['cc'])) {
			$mail->setCc($_REQUEST['cc']);
699
		}
700 701
		if (!empty($_REQUEST['bcc'])) {
			$mail->setBcc($_REQUEST['bcc']);
702
		}
703
		$mail->setSubject($_REQUEST['subject']);
704

705
		if ($_REQUEST['attach1']) {
706
			check_ticket('webmail');
707
			$a1 = $mail->getFile('temp/mail_attachs/' . $_REQUEST['attach1file']);
708

709 710
			$mail->addAttachment($a1, $_REQUEST['attach1'], $_REQUEST['attach1type']);
			@unlink ('temp/mail_attachs/' . $_REQUEST['attach1file']);
711 712
		}

713
		if ($_REQUEST['attach2']) {
714
			check_ticket('webmail');
715
			$a2 = $mail->getFile('temp/mail_attachs/' . $_REQUEST['attach2file']);
716

717 718
			$mail->addAttachment($a2, $_REQUEST['attach2'], $_REQUEST['attach2type']);
			@unlink ('temp/mail_attachs/' . $_REQUEST['attach2file']);
719 720
		}

721
		if ($_REQUEST['attach3']) {
722
			check_ticket('webmail');
723
			$a3 = $mail->getFile('temp/mail_attachs/' . $_REQUEST['attach3file']);
724

725 726
			$mail->addAttachment($a3, $_REQUEST['attach3'], $_REQUEST['attach3type']);
			@unlink ('temp/mail_attachs/' . $_REQUEST['attach3file']);
727 728
		}

729
		$mail->setSMTPParams($current['smtp'], $current['smtpPort'], '', $current['useAuth'], $current['username'], $current['pass']);
730

731 732
		if (isset($_REQUEST['useHTML']) && $_REQUEST['useHTML'] == 'on') {
			$mail->setHTML($_REQUEST['body'], strip_tags($_REQUEST['body']));
733
		} else {
734
			$mail->setText($_REQUEST['body']);
735 736
		}

737
		$to_array_1 = split('[, ;]', $_REQUEST['to']);
738 739 740 741 742 743 744 745
		$to_array = array();

		foreach ($to_array_1 as $to_1) {
			if (!empty($to_1)) {
				$to_array[] = $to_1;
			}
		}

746
		$to_array = $contactlib->parse_nicknames($to_array);
747 748

		// Get email addresses not in the address book
sylvieg's avatar
sylvieg committed
749
		$not_contacts = $contactlib->are_contacts($to_array, $user);
750 751 752 753 754 755 756 757 758

		if (count($not_contacts) > 0) {
			$smarty->assign('notcon', 'y');
		} else {
			$smarty->assign('notcon', 'n');
		}

		$smarty->assign('not_contacts', $not_contacts);

luciash's avatar
luciash committed
759 760
		if ($mail->send($to_array,'smtp')) {
			$msg=tra('Your email was sent');
761
		} else {
luciash's avatar
luciash committed
762
			if (is_array($mail->errors)) {
763
				$msg = '';
764 765
				$temp_max = count($mail->errors);
				for ($i = 0; $i < $temp_max; $i ++) {
766
					$msg .= $mail->errors[$i].'<br />';
luciash's avatar
luciash committed
767 768 769 770
				}
			} else {
				$msg=$mail->errors;
			}
771 772 773 774 775 776
		}

		$smarty->assign('sent', 'y');
		$smarty->assign('msg', $msg);
	}

777
	if (isset($_REQUEST['attach'])) {
778 779 780
		$smarty->assign('attaching', 'y');
	}

781
	if (isset($_REQUEST['remove_attach1'])) {
782
		check_ticket('webmail');
783
		@unlink ($_REQUEST['attach1file']);
784

785 786 787
		$_REQUEST['attach1'] = '';
		$_REQUEST['attach1file'] = '';
		$_REQUEST['attach1type'] = '';
788 789
	}

790
	if (isset($_REQUEST['remove_attach2'])) {
791
		check_ticket('webmail');
792
		@unlink ($_REQUEST['attach2file']);
793

794 795 796
		$_REQUEST['attach2'] = '';
		$_REQUEST['attach2file'] = '';
		$_REQUEST['attach2type'] = '';
797 798
	}

799
	if (isset($_REQUEST['remove_attach3'])) {
800
		check_ticket('webmail');
801
		@unlink ($_REQUEST['attach3file']);
802

803 804 805
		$_REQUEST['attach3'] = '';
		$_REQUEST['attach3file'] = '';
		$_REQUEST['attach3type'] = '';
806 807
	}

808
	if (isset($_REQUEST['attached'])) {
809 810
		// Now process the uploads
		if (isset($_FILES['userfile1']) && is_uploaded_file($_FILES['userfile1']['tmp_name'])) {
811
			check_ticket('webmail');
812 813 814 815 816 817
			$size = $_FILES['userfile1']['size'];

			if ($size < 1500000) {
				$name = $_FILES['userfile1']['name'];

				$type = $_FILES['userfile1']['type'];
818 819 820 821
				$_REQUEST['attach1file'] = $user . md5($webmaillib->genPass());
				$_REQUEST['attach1type'] = $type;
				$_REQUEST['attach1'] = $name;
				move_uploaded_file($_FILES['userfile1']['tmp_name'], 'temp/mail_attachs/' . $_REQUEST['attach1file']);
822 823 824 825
			}
		}

		if (isset($_FILES['userfile2']) && is_uploaded_file($_FILES['userfile2']['tmp_name'])) {
826
			check_ticket('webmail');
827 828 829 830 831 832
			$size = $_FILES['userfile2']['size'];

			if ($size < 1500000) {
				$name = $_FILES['userfile2']['name'];

				$type = $_FILES['userfile2']['type'];
833 834 835 836
				$_REQUEST['attach2file'] = $user . md5($webmaillib->genPass());
				$_REQUEST['attach2type'] = $type;
				$_REQUEST['attach2'] = $name;
				move_uploaded_file($_FILES['userfile2']['tmp_name'], 'temp/mail_attachs/' . $_REQUEST['attach2file']);
837 838 839 840
			}
		}

		if (isset($_FILES['userfile3']) && is_uploaded_file($_FILES['userfile3']['tmp_name'])) {
841
			check_ticket('webmail');
842 843 844 845 846 847
			$size = $_FILES['userfile3']['size'];

			if ($size < 1500000) {
				$name = $_FILES['userfile3']['name'];

				$type = $_FILES['userfile3']['type'];
848 849 850 851
				$_REQUEST['attach3file'] = $user . md5($webmaillib->genPass());
				$_REQUEST['attach3type'] = $type;
				$_REQUEST['attach3'] = $name;
				move_uploaded_file($_FILES['userfile3']['tmp_name'], 'temp/mail_attachs/' . $_REQUEST['attach3file']);
852 853 854 855 856
			}
		}
	}

	// Build the to array
857 858
	if (!isset($_REQUEST['attach1']))
		$_REQUEST['attach1'] = '';
859

860 861
	if (!isset($_REQUEST['attach2']))
		$_REQUEST['attach2'] = '';
862

863 864
	if (!isset($_REQUEST['attach3']))
		$_REQUEST['attach3'] = '';
865

866 867
	if (!isset($_REQUEST['attach1file']))
		$_REQUEST['attach1file'] = '';
868

869 870
	if (!isset($_REQUEST['attach2file']))
		$_REQUEST['attach2file'] = '';
871

872 873
	if (!isset($_REQUEST['attach3file']))
		$_REQUEST['attach3file'] = '';
874

875 876
	if (!isset($_REQUEST['attach1type']))
		$_REQUEST['attach1type'] = '';
877

878 879
	if (!isset($_REQUEST['attach2type']))
		$_REQUEST['attach2type'] = '';
880

881 882
	if (!isset($_REQUEST['attach3type']))
		$_REQUEST['attach3type'] = '';
883

884 885
	if (!isset($_REQUEST['to']))
		$_REQUEST['to'] = '';
886

887 888
	if (!isset($_REQUEST['cc']))
		$_REQUEST['cc'] = '';
889

890 891
	if (!isset($_REQUEST['bcc']))
		$_REQUEST['bcc'] = '';
892

893 894
	if (!isset($_REQUEST['body']))
		$_REQUEST['body'] = '';
895

896 897
	if (!isset($_REQUEST['subject']))
		$_REQUEST['subject'] = '';
898

899 900 901 902 903 904 905 906 907 908 909 910 911 912
	$smarty->assign('cc', $_REQUEST['cc']);
	$smarty->assign('to', $_REQUEST['to']);
	$smarty->assign('bcc', $_REQUEST['bcc']);
	$smarty->assign('body', $_REQUEST['body']);
	$smarty->assign('subject', $_REQUEST['subject']);
	$smarty->assign('attach1', $_REQUEST['attach1']);
	$smarty->assign('attach2', $_REQUEST['attach2']);
	$smarty->assign('attach3', $_REQUEST['attach3']);
	$smarty->assign('attach1file', $_REQUEST['attach1file']);
	$smarty->assign('attach2file', $_REQUEST['attach2file']);
	$smarty->assign('attach3file', $_REQUEST['attach3file']);
	$smarty->assign('attach1type', $_REQUEST['attach1type']);
	$smarty->assign('attach2type', $_REQUEST['attach2type']);
	$smarty->assign('attach3type', $_REQUEST['attach3type']);
lrargerich's avatar
lrargerich committed
913 914
}

915 916 917
include_once ('tiki-mytiki_shared.php');

include_once ('tiki-section_options.php');
lrargerich's avatar
lrargerich committed
918

919
ask_ticket('webmail');
920
if ($prefs['feature_ajax'] == 'y') {
921
    global $ajaxlib;
922 923
    $ajaxlib->registerTemplate('tiki-webmail.tpl');
    $ajaxlib->registerFunction('loadComponent');
924 925
    $ajaxlib->processRequests();
}
926
$smarty->assign('mid', 'tiki-webmail.tpl');
927
$smarty->display('tiki.tpl');