<commit_before><?php
/**
 * @package tikiwiki
 */
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
//
// 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.
// $Id$

$section = 'user_messages';
$inputConfiguration =	[[
	'staticKeyFilters'	=> [
		'body'			=> 'text',
		'groupbr'		=> 'groupname',
		'priority'		=> 'digits',
		'replyto_hash'	=> 'alnumdash',
		'preview'		=> 'alphaspace',
		'send'			=> 'alphaspace',
		'subject'		=> 'text',
	],
	'catchAllUnset' => null,
]];
require_once ('tiki-setup.php');
$messulib = TikiLib::lib('message');
$access->check_user($user);
$access->check_feature('feature_messages');
$access->checkAuthenticity();
$auto_query_args = array('subject', 'body', 'priority', 'replyto_hash', 'groupbr');

if (!isset($_POST['subject'])) $_POST['subject'] = '';
if (!isset($_POST['body'])) $_POST['body'] = '';
if (!isset($_POST['priority'])) $_POST['priority'] = 3;
if (!isset($_POST['replyto_hash'])) $_POST['replyto_hash'] = '';
$smarty->assign('subject', $_POST['subject']);
$smarty->assign('body', $_POST['body']);
$smarty->assign('priority', $_POST['priority']);
$smarty->assign('replyto_hash', $_POST['replyto_hash']);
$smarty->assign('mid', 'messu-broadcast.tpl');
$smarty->assign('sent', 0);
perm_broadcast_check($access, $userlib);
$groups = $userlib->get_user_groups($user);

if (in_array('Admins', $groups)){
    //admins can write to members of all groups
    $groups = $userlib->list_all_groups();
    $groups = array_diff($groups, array('Registered', 'Anonymous'));
}
else {
    //registered users can write to members of groups they belong to
    $groups = array_diff($groups, array('Registered', 'Anonymous'));
}

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

if ((isset($_POST['send']) || isset($_POST['preview'])) && $access->ticketMatch()) {
	$message = '';
	// Validation:
	// must have a subject or body non-empty (or both)
	if (empty($_POST['subject']) && empty($_POST['body'])) {
		$smarty->assign('message', tra('ERROR: Either the subject or body must be non-empty'));
		$smarty->display("tiki.tpl");
		die;
	}
	// Remove invalid users from the to, cc and bcc fields
	if (isset($_POST['groupbr'])) {
		if ($_POST['groupbr'] == 'all' && $tiki_p_broadcast_all == 'y') {
			$a_all_users = $userlib->get_users(0, -1, 'login_desc', '');
			$all_users = array();
			foreach ($a_all_users['data'] as $a_user) {
				$all_users[] = $a_user['user'];
			}
		} elseif (in_array($_POST['groupbr'], $groups)) {
			$all_users = $userlib->get_group_users($_POST['groupbr']);
		} else {
			$access->display_error('', tra("You do not have permission to use this feature").": ". $permission, '403', false);
		}
		$smarty->assign('groupbr', $_POST['groupbr']);
	}

	$users = array();
	foreach ($all_users as $a_user) {
		if (!empty($a_user)) {
			if ($userlib->user_exists($a_user)) {
				if (!$userlib->user_has_permission($a_user, 'tiki_p_messages')) {
					$message .= sprintf(tra('User %s does not have the permission'), htmlspecialchars($a_user)). "<br />" ;
				} elseif ($tikilib->get_user_preference($a_user, 'allowMsgs', 'y') == 'y') {
					$users[] = $a_user;
				} else {
					$message.= sprintf(tra("User %s does not want to receive messages"), htmlspecialchars($a_user)). "<br />" ;
				}
			} else {
				$message.= tra("Invalid user") . "$a_user<br />";
			}
		}
	}
	$users = array_unique($users);
	// Validation: either to, cc or bcc must have a valid user
	if (count($users) > 0) {
		$users_formatted = array();
		foreach ($users as $rawuser)
			$users_formatted[] = htmlspecialchars($rawuser);
		if (isset($_POST['send'])) {
			$message .= tra('The message has been sent to:').' ';
		} else {
			$message .= tra('The message will be sent to:').' ';
		}
		$message .= implode(',', $users_formatted) . "<br />";
	} else {
		$message .= tra('ERROR: No valid users to send the message');
		$smarty->assign('message', $message);
		$smarty->display("tiki.tpl");
		die;
	}
	if (isset($_POST['send'])) {
		$smarty->assign('sent', 1);
		// Insert the message in the inboxes of each user
		foreach ($users as $a_user) {
			$messulib->post_message($a_user, $user, $a_user, '', $_POST['subject'], $_POST['body'], $_POST['priority']);
			// if this is a reply flag the original messages replied to
			if ($_POST['replyto_hash'] <> '') {
				$messulib->mark_replied($a_user, $_POST['replyto_hash']);
			}
		}
		// Insert a copy of the message in the sent box of the sender
		$messulib->save_sent_message($user, $user, $_POST['groupbr'], null, $_POST['subject'], $_POST['body'], $_POST['priority'], $_POST['replyto_hash']);
		$smarty->assign('message', $message);
		if ($prefs['feature_actionlog'] == 'y') {
			$logslib->add_action('Posted', '', 'message', 'add=' . strlen($_POST['body']));
		}
	} else {
		$smarty->assign('message', $message);
		$smarty->assign('preview', 1);
	}
}<commit_msg>[REF] Arrange code more logically to better place feedback; use Feedback class for errors; sort users for feedback.<commit_after><?php
/**
 * @package tikiwiki
 */
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
//
// 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.
// $Id$

$section = 'user_messages';
$inputConfiguration =	[[
	'staticKeyFilters'	=> [
		'body'			=> 'text',
		'groupbr'		=> 'groupname',
		'priority'		=> 'digits',
		'replyto_hash'	=> 'alnumdash',
		'preview'		=> 'alphaspace',
		'send'			=> 'alphaspace',
		'subject'		=> 'text',
	],
	'catchAllUnset' => null,
]];
require_once ('tiki-setup.php');
$messulib = TikiLib::lib('message');
$access->check_user($user);
$access->check_feature('feature_messages');
$access->checkAuthenticity();
$auto_query_args = array('subject', 'body', 'priority', 'replyto_hash', 'groupbr');

if (!isset($_POST['subject'])) $_POST['subject'] = '';
if (!isset($_POST['body'])) $_POST['body'] = '';
if (!isset($_POST['priority'])) $_POST['priority'] = 3;
if (!isset($_POST['replyto_hash'])) $_POST['replyto_hash'] = '';
$smarty->assign('subject', $_POST['subject']);
$smarty->assign('body', $_POST['body']);
$smarty->assign('priority', $_POST['priority']);
$smarty->assign('replyto_hash', $_POST['replyto_hash']);
$smarty->assign('mid', 'messu-broadcast.tpl');
$smarty->assign('sent', 0);
perm_broadcast_check($access, $userlib);
$groups = $userlib->get_user_groups($user);

if (in_array('Admins', $groups)){
    //admins can write to members of all groups
    $groups = $userlib->list_all_groups();
    $groups = array_diff($groups, array('Registered', 'Anonymous'));
}
else {
    //registered users can write to members of groups they belong to
    $groups = array_diff($groups, array('Registered', 'Anonymous'));
}

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

if ((isset($_POST['send']) || isset($_POST['preview'])) && $access->ticketMatch()) {
	$message = '';
	// Validation:
	// must have a subject or body non-empty (or both)
	if (empty($_POST['subject']) && empty($_POST['body'])) {
		Feedback::error(tra('The message must have either a subject or a body.'));
	} else {
		// Remove invalid users from the to, cc and bcc fields
		if (isset($_POST['groupbr'])) {
			if ($_POST['groupbr'] == 'all' && $tiki_p_broadcast_all == 'y') {
				$a_all_users = $userlib->get_users(0, -1, 'login_desc', '');
				$all_users = array();
				foreach ($a_all_users['data'] as $a_user) {
					$all_users[] = $a_user['user'];
				}
			} elseif (in_array($_POST['groupbr'], $groups)) {
				$all_users = $userlib->get_group_users($_POST['groupbr']);
			} else {
				$access->display_error('', tra("You do not have permission to use this feature").": ". $permission, '403', false);
			}
			$smarty->assign('groupbr', $_POST['groupbr']);
		}

		$users = array();
		asort($all_users);
		foreach ($all_users as $a_user) {
			if (!empty($a_user)) {
				if ($userlib->user_exists($a_user)) {
					if (!$userlib->user_has_permission($a_user, 'tiki_p_messages')) {
						$message .= sprintf(tra('User %s does not have the permission'), htmlspecialchars($a_user)). "<br />" ;
					} elseif ($tikilib->get_user_preference($a_user, 'allowMsgs', 'y') == 'y') {
						$users[] = $a_user;
					} else {
						$message.= sprintf(tra("User %s does not want to receive messages"), htmlspecialchars($a_user)). "<br />" ;
					}
				} else {
					$message.= tra("Invalid user") . "$a_user<br />";
				}
			}
		}
		$users = array_unique($users);
		// Validation: either to, cc or bcc must have a valid user
		if (count($users) > 0) {
			$users_formatted = array();
			foreach ($users as $rawuser) {
				$users_formatted[] = htmlspecialchars($rawuser);
			}
			if (isset($_POST['send'])) {
				$message .= tra('The message has been sent to:').' ';
			} else {
				$message .= tra('The message will be sent to:').' ';
			}
			$message .= implode(',', $users_formatted) . "<br />";
			if (isset($_POST['send'])) {
				$smarty->assign('sent', 1);
				// Insert the message in the inboxes of each user
				foreach ($users as $a_user) {
					$messulib->post_message($a_user, $user, $a_user, '', $_POST['subject'], $_POST['body'], $_POST['priority']);
					// if this is a reply flag the original messages replied to
					if ($_POST['replyto_hash'] <> '') {
						$messulib->mark_replied($a_user, $_POST['replyto_hash']);
					}
				}
				// Insert a copy of the message in the sent box of the sender
				$messulib->save_sent_message($user, $user, $_POST['groupbr'], null, $_POST['subject'], $_POST['body'], $_POST['priority'], $_POST['replyto_hash']);
				$smarty->assign('message', $message);
				if ($prefs['feature_actionlog'] == 'y') {
					$logslib->add_action('Posted', '', 'message', 'add=' . strlen($_POST['body']));
				}
			} else {
				$smarty->assign('preview', 1);
			}
		} else {
			Feedback::error(tra('No valid users to send the message to.'));
		}
	}
	$smarty->assign('message', $message);
}
{'fraction_non_alphanumeric': 0.2112887112887113, 'fraction_numerical': 0.0035964035964035964, 'mean_word_length': 4.555493895671476, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4898741', 'n_tokens_mistral': 3748, 'n_tokens_neox': 3400, 'n_words': 806}