messu-compose.php 7.96 KB
Newer Older
1
<?php
2
// (c) Copyright 2002-2011 by authors of the Tiki Wiki CMS Groupware Project
3
// 
4 5
// 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.
6
// $Id$
changi67's avatar
changi67 committed
7

8
$section = 'user_messages';
9 10
require_once ('tiki-setup.php');
include_once ('lib/messu/messulib.php');
11 12
$access->check_user($user);
$access->check_feature('feature_messages');
13
$access->check_permission('tiki_p_messages');
14
if ($prefs['allowmsg_is_optional'] == 'y') {
15 16 17 18 19
	if ($tikilib->get_user_preference($user, 'allowMsgs', 'y') != 'y') {
		$smarty->assign('msg', tra("You have to be able to receive messages in order to send them. Goto your user preferences and enable 'Allow messages from other users'"));
		$smarty->display("error.tpl");
		die;
	}
20
}
changi67's avatar
changi67 committed
21
if (($prefs['messu_sent_size'] > 0) && ($messulib->count_messages($user, 'sent') >= $prefs['messu_sent_size'])) {
22 23 24
	$smarty->assign('msg', tra('Sent box is full. Archive or delete some sent messages first if you want to send more messages.'));
	$smarty->display("error.tpl");
	die;
lrargerich's avatar
lrargerich committed
25
}
changi67's avatar
changi67 committed
26 27 28 29 30 31 32
if (!isset($_REQUEST['to'])) $_REQUEST['to'] = '';
if (!isset($_REQUEST['cc'])) $_REQUEST['cc'] = '';
if (!isset($_REQUEST['bcc'])) $_REQUEST['bcc'] = '';
if (!isset($_REQUEST['subject'])) $_REQUEST['subject'] = '';
if (!isset($_REQUEST['body'])) $_REQUEST['body'] = '';
if (!isset($_REQUEST['replyto_hash'])) $_REQUEST['replyto_hash'] = '';
if (!isset($_REQUEST['priority'])) $_REQUEST['priority'] = 3;
33
// Strip Re:Re:Re: from subject
changi67's avatar
changi67 committed
34
if (!empty($_REQUEST['reply']) || !empty($_REQUEST['replyall'])) {
35
	$_REQUEST['subject'] = tra("Re:") . preg_replace('/^(' . tra('Re:') . ')+/', '', $_REQUEST['subject']);
36
	$smarty->assign('reply', 'y');
37
}
changi67's avatar
changi67 committed
38
foreach(array(
39 40 41
	'to',
	'cc',
	'bcc'
changi67's avatar
changi67 committed
42
) as $dest) if (is_array($_REQUEST[$dest])) $_REQUEST[$dest] = implode(', ', array_filter($_REQUEST[$dest], 'ctype_alnum'));
43 44 45 46 47 48
$smarty->assign('to', $_REQUEST['to']);
$smarty->assign('cc', $_REQUEST['cc']);
$smarty->assign('bcc', $_REQUEST['bcc']);
$smarty->assign('subject', $_REQUEST['subject']);
$smarty->assign('body', $_REQUEST['body']);
$smarty->assign('priority', $_REQUEST['priority']);
49
$smarty->assign('replyto_hash', $_REQUEST['replyto_hash']);
50 51 52
$smarty->assign('mid', 'messu-compose.tpl');
$smarty->assign('sent', 0);
if (isset($_REQUEST['send'])) {
53 54 55 56 57 58 59 60 61 62 63
	check_ticket('messu-compose');
	$smarty->assign('sent', 1);
	$message = '';
	// Validation:
	// must have a subject or body non-empty (or both)
	if (empty($_REQUEST['subject']) && empty($_REQUEST['body'])) {
		$smarty->assign('message', tra('ERROR: Either the subject or body must be non-empty'));
		$smarty->display("tiki.tpl");
		die;
	}
	// Parse the to, cc and bcc fields into an array
64 65 66
	$arr_to = preg_split('/\s*(?<!\\\)[;,]\s*/', $_REQUEST['to']);
	$arr_cc = preg_split('/\s*(?<!\\\)[;,]\s*/', $_REQUEST['cc']);
	$arr_bcc = preg_split('/\s*(?<!\\\)[;,]\s*/', $_REQUEST['bcc']);
67 68 69 70 71 72
	if ($prefs['user_selector_realnames_messu'] == 'y') {
		$groups = '';
		$arr_to = $userlib->find_best_user($arr_to, $groups, 'login');
		$arr_cc = $userlib->find_best_user($arr_cc, $groups);
		$arr_bcc = $userlib->find_best_user($arr_bcc, $groups);
	}
73 74 75 76 77 78 79 80 81 82 83 84
	// Remove invalid users from the to, cc and bcc fields
	$users = array();
	foreach($arr_to as $a_user) {
		if (!empty($a_user)) {
			$a_user = str_replace('\\;', ';', $a_user);
			if ($userlib->user_exists($a_user)) {
				// mail only to users with activated message feature
				if ($prefs['allowmsg_is_optional'] != 'y' || $tikilib->get_user_preference($a_user, 'allowMsgs', 'y') == 'y') {
					// only send mail if nox mailbox size is defined or not reached yet
					if (($messulib->count_messages($a_user) < $prefs['messu_mailbox_size']) || ($prefs['messu_mailbox_size'] == 0)) {
						$users[] = $a_user;
					} else {
chealer's avatar
chealer committed
85
						$message.= sprintf(tra("User %s can not receive messages, mailbox is full") ,htmlspecialchars($a_user)) . "<br />";
86 87
					}
				} else {
chealer's avatar
chealer committed
88
					$message.= sprintf(tra("User %s can not receive messages") , htmlspecialchars($a_user)) . "<br />";
89 90
				}
			} else {
chealer's avatar
chealer committed
91
				$message.= sprintf(tra("Invalid user: %s") , htmlspecialchars($a_user)) . "<br />";
92 93 94 95 96 97 98 99 100 101 102 103 104
			}
		}
	}
	foreach($arr_cc as $a_user) {
		if (!empty($a_user)) {
			$a_user = str_replace('\\;', ';', $a_user);
			if ($userlib->user_exists($a_user)) {
				// mail only to users with activated message feature
				if ($prefs['allowmsg_is_optional'] != 'y' || $tikilib->get_user_preference($a_user, 'allowMsgs', 'y') == 'y') {
					// only send mail if nox mailbox size is defined or not reached yet
					if (($messulib->count_messages($a_user) < $prefs['messu_mailbox_size']) || ($prefs['messu_mailbox_size'] == 0)) {
						$users[] = $a_user;
					} else {
chealer's avatar
chealer committed
105
						$message.= sprintf(tra("User %s can not receive messages, mailbox is full") , htmlspecialchars($a_user)) . "<br />";
106 107
					}
				} else {
chealer's avatar
chealer committed
108
					$message.= sprintf(tra("User %s can not receive messages") , htmlspecialchars($a_user)) . "<br />";
109 110
				}
			} else {
chealer's avatar
chealer committed
111
				$message.= sprintf(tra("Invalid user: %s") , htmlspecialchars($a_user)) . "<br />";
112 113 114 115 116 117 118 119 120 121 122 123 124
			}
		}
	}
	foreach($arr_bcc as $a_user) {
		if (!empty($a_user)) {
			$a_user = str_replace('\\;', ';', $a_user);
			if ($userlib->user_exists($a_user)) {
				// mail only to users with activated message feature
				if ($prefs['allowmsg_is_optional'] != 'y' || $tikilib->get_user_preference($a_user, 'allowMsgs', 'y') == 'y') {
					// only send mail if nox mailbox size is defined or not reached yet
					if (($messulib->count_messages($a_user) < $prefs['messu_mailbox_size']) || ($prefs['messu_mailbox_size'] == 0)) {
						$users[] = $a_user;
					} else {
chealer's avatar
chealer committed
125
						$message.= sprintf(tra("User %s can not receive messages, mailbox is full") , htmlspecialchars($a_user)) . "<br />";
126 127
					}
				} else {
chealer's avatar
chealer committed
128
					$message.= sprintf(tra("User %s can not receive messages") , htmlspecialchars($a_user)) . "<br />";
129 130
				}
			} else {
chealer's avatar
chealer committed
131
				$message.= sprintf(tra("Invalid user: %s") , htmlspecialchars($a_user)) . "<br />";
132 133 134 135 136 137
			}
		}
	}
	$users = array_unique($users);
	// Validation: either to, cc or bcc must have a valid user
	if (count($users) > 0) {
chealer's avatar
chealer committed
138 139 140
		$users_formatted = array();
		foreach ($users as $rawuser)
			$users_formatted[] = htmlspecialchars($rawuser);
141
		$message.= tra("Message has been sent to: ") . implode(',', $users_formatted) . "<br />";
142 143 144 145 146 147 148 149
	} else {
		$message.= tra('ERROR: No valid users to send the message');
		$smarty->assign('message', $message);
		$smarty->display("tiki.tpl");
		die;
	}
	// Insert the message in the inboxes of each user
	foreach($users as $a_user) {
150 151 152 153 154 155 156 157 158 159 160 161 162
		$result = $messulib->post_message($a_user, $user, $_REQUEST['to'], $_REQUEST['cc'], $_REQUEST['subject'], $_REQUEST['body'], $_REQUEST['priority'], $_REQUEST['replyto_hash'],
								isset($_REQUEST['replytome']) ? 'y' : '', isset($_REQUEST['bccme']) ? 'y' : '');
		if ($result) {
			if ($prefs['feature_score'] == 'y') {
				$tikilib->score_event($user, 'message_send');
				$tikilib->score_event($a_user, 'message_receive');
			}
			// if this is a reply flag the original messages replied to
			if ($_REQUEST['replyto_hash'] <> '') {
				$messulib->mark_replied($a_user, $_REQUEST['replyto_hash']);
			}
		} else {
			$message = tra('An error occurred, please check your mail settings and try again');
163 164 165 166 167 168
		}
	}
	// Insert a copy of the message in the sent box of the sender
	$messulib->save_sent_message($user, $user, $_REQUEST['to'], $_REQUEST['cc'], $_REQUEST['subject'], $_REQUEST['body'], $_REQUEST['priority'], $_REQUEST['replyto_hash']);
	$smarty->assign('message', $message);
	if ($prefs['feature_actionlog'] == 'y') {
169 170 171 172 173
		if (isset($_REQUEST['reply']) && $_REQUEST['reply'] == 'y') {
			$logslib->add_action('Replied', '', 'message', 'add=' . $tikilib->strlen_quoted($_REQUEST['body']));
		} else {
			$logslib->add_action('Posted', '', 'message', 'add=' . strlen($_REQUEST['body']));
		}
174
	}
175
}
176
$allowMsgs = $prefs['allowmsg_is_optional'] != 'y' || $tikilib->get_user_preference($user, 'allowMsgs', 'y');
177
$smarty->assign('allowMsgs', $allowMsgs);
178
include_once ('tiki-section_options.php');
179
ask_ticket('messu-compose');
180
include_once ('tiki-mytiki_shared.php');
181
$smarty->display("tiki.tpl");