<commit_before><?php
session_start();
if (!$_SESSION)
{die("You must be logged in to view this page.");}

include 'db.php';
$body_mod_a = addslashes($_POST[body]);
$body_mod = nl2br($body_mod_a);

//If option to message all users on a particular case is requested, this function determines their usernames.
function get_all_on_case($case_id)

	{
		$get_profs = mysql_query("SELECT * FROM `cm` WHERE `id` = '$_POST[assoc_case]' LIMIT 1");
		$get_students = mysql_query("SELECT * FROM `cm_cases_students` WHERE `case_id` = '$_POST[assoc_case]' AND `status` = 'active'");
		
		while ($a = mysql_fetch_object($get_profs))
			{$targets = $a->professor;}
		
		while ($b = mysql_fetch_object($get_students))
			{$targets .= $b->username . ",";}
			
		return $targets;	
	
	}
	
if (!empty($_POST[group]))
{

	if ($_POST[group] == 'All on this Case' || $_POST[group] == 'All on a Case')
	
	{$tos_list = get_all_on_case($_POST[assoc_case]);}

		else
		{
			if ($_POST[group] == 'All Your Students')
			{$group_query = "SELECT * FROM `cm_users` WHERE `assigned_prof` LIKE '%$_SESSION[login]%' AND `status` = 'active'";}

			if ($_POST[group] == 'All Professors')
			{$group_query = "SELECT * FROM `cm_users` WHERE `class` = 'prof' AND `status` = 'active'";}

			if ($_POST[group] == 'All Users')
			{$group_query = "SELECT * FROM `cm_users` WHERE `status` = 'active'";}

			if ($_POST[group] == 'All Students')
			{$group_query = "SELECT * FROM `cm_users` WHERE `class` = 'student' AND `status` = 'active'";}



			$send_to_group = mysql_query("$group_query");
				while ($result = mysql_fetch_array($send_to_group))
					{

						$tos_list .=  $result[username] . ",";
					}
			}

$tos = substr($tos_list,0,-1);

}

//This is if no group is selected
else
{
	$tos = substr($_POST[to],0,-1);
}

	$ccs = substr($_POST[cc1],0,-1);



		$send_msg = mysql_query("INSERT INTO `cm_messages` (`id`,`thread_id`,`to`,`from`,`ccs`,`subject`,`body`,`assoc_case`) VALUES (NULL,'$_POST[thread_id]','$tos','$_POST[from]','$ccs','$_POST[subject]','$body_mod','$_POST[assoc_case]')");

			if (empty($_POST[thread_id]))
				{
					$lst_id = mysql_insert_id();
					$set_thread = mysql_query("UPDATE `cm_messages` SET `thread_id` = '$lst_id' WHERE `id` = '$lst_id' LIMIT 1");


				}




//Now, we shove the to parties and cc parties into one array for the sending of notifications.

if (empty($ccs))//There are no cc's
	{$all = $tos;}
	else
	{$all = $tos . ',' . $ccs;}

$all_array = explode(",", $all);

foreach ($all_array as $recips)
	{
		$get_data = mysql_query("SELECT * FROM `cm_users` WHERE `username` = '$recips' LIMIT 1");
		while ($line = mysql_fetch_array($get_data, MYSQL_ASSOC)) {
			$i=0;
			foreach ($line as $col_value) {
				$field=mysql_field_name($get_data,$i);
				$d[$field] = $col_value;
				$i++;

			}

	$get_sender_name = mysql_query("SELECT * FROM `cm_users` WHERE `username` = '$_POST[from]' LIMIT 1");
	$x = mysql_fetch_array($get_sender_name);
	$sender_name = "$x[first_name] $x[last_name]";



	$sms_message = "Preview:  " . substr($_POST[body],0,50) . "...";
	$email_message = "You have a new message re: $_POST[subject] from $sender_name on the ClinicCases system." . "\r\n" . $sms_message . "\r\n" . "Please log on to " . $CC_base_url . " to view or " . $CC_base_url . "mobile from a mobile browser." ;
	$subject = "ClinicCases Message from $sender_name";

	$email_to = "$d[email]";
	$headers = 'From: ' . $CC_default_email . "\r\n" .
	   'Reply-To: ' . $CC_default_email . "\r\n" .
	   'X-Mailer: PHP/' . phpversion();
<commit_msg>Fix to message send to avoid php bug which causes mail sent to some servers to return a "554 Failed: Malformed MIME header (in reply to end of DATA command))" error.<commit_after><?php
session_start();
if (!$_SESSION)
{die("You must be logged in to view this page.");}

include 'db.php';
$body_mod_a = addslashes($_POST[body]);
$body_mod = nl2br($body_mod_a);

//If option to message all users on a particular case is requested, this function determines their usernames.
function get_all_on_case($case_id)

	{
		$get_profs = mysql_query("SELECT * FROM `cm` WHERE `id` = '$_POST[assoc_case]' LIMIT 1");
		$get_students = mysql_query("SELECT * FROM `cm_cases_students` WHERE `case_id` = '$_POST[assoc_case]' AND `status` = 'active'");
		
		while ($a = mysql_fetch_object($get_profs))
			{$targets = $a->professor;}
		
		while ($b = mysql_fetch_object($get_students))
			{$targets .= $b->username . ",";}
			
		return $targets;	
	
	}
	
if (!empty($_POST[group]))
{

	if ($_POST[group] == 'All on this Case' || $_POST[group] == 'All on a Case')
	
	{$tos_list = get_all_on_case($_POST[assoc_case]);}

		else
		{
			if ($_POST[group] == 'All Your Students')
			{$group_query = "SELECT * FROM `cm_users` WHERE `assigned_prof` LIKE '%$_SESSION[login]%' AND `status` = 'active'";}

			if ($_POST[group] == 'All Professors')
			{$group_query = "SELECT * FROM `cm_users` WHERE `class` = 'prof' AND `status` = 'active'";}

			if ($_POST[group] == 'All Users')
			{$group_query = "SELECT * FROM `cm_users` WHERE `status` = 'active'";}

			if ($_POST[group] == 'All Students')
			{$group_query = "SELECT * FROM `cm_users` WHERE `class` = 'student' AND `status` = 'active'";}



			$send_to_group = mysql_query("$group_query");
				while ($result = mysql_fetch_array($send_to_group))
					{

						$tos_list .=  $result[username] . ",";
					}
			}

$tos = substr($tos_list,0,-1);

}

//This is if no group is selected
else
{
	$tos = substr($_POST[to],0,-1);
}

	$ccs = substr($_POST[cc1],0,-1);



		$send_msg = mysql_query("INSERT INTO `cm_messages` (`id`,`thread_id`,`to`,`from`,`ccs`,`subject`,`body`,`assoc_case`) VALUES (NULL,'$_POST[thread_id]','$tos','$_POST[from]','$ccs','$_POST[subject]','$body_mod','$_POST[assoc_case]')");

			if (empty($_POST[thread_id]))
				{
					$lst_id = mysql_insert_id();
					$set_thread = mysql_query("UPDATE `cm_messages` SET `thread_id` = '$lst_id' WHERE `id` = '$lst_id' LIMIT 1");


				}




//Now, we shove the to parties and cc parties into one array for the sending of notifications.

if (empty($ccs))//There are no cc's
	{$all = $tos;}
	else
	{$all = $tos . ',' . $ccs;}

$all_array = explode(",", $all);

foreach ($all_array as $recips)
	{
		$get_data = mysql_query("SELECT * FROM `cm_users` WHERE `username` = '$recips' LIMIT 1");
		while ($line = mysql_fetch_array($get_data, MYSQL_ASSOC)) {
			$i=0;
			foreach ($line as $col_value) {
				$field=mysql_field_name($get_data,$i);
				$d[$field] = $col_value;
				$i++;

			}

	$get_sender_name = mysql_query("SELECT * FROM `cm_users` WHERE `username` = '$_POST[from]' LIMIT 1");
	$x = mysql_fetch_array($get_sender_name);
	$sender_name = "$x[first_name] $x[last_name]";



	$sms_message = "Preview:  " . substr($_POST[body],0,50) . "...";
	$email_message = "You have a new message re: $_POST[subject] from $sender_name on the ClinicCases system." . "\r\n" . $sms_message . "\r\n" . "Please log on to " . $CC_base_url . " to view or " . $CC_base_url . "mobile from a mobile browser." ;
	
	//Change here to take out the extra /r.  PHP apparently adds an extra /r when sending which causes a "554 Failed: Malformed MIME header (in reply to end of DATA command))" error when sending to some servers.  Full discussion here:http://jeremygustafson.blogspot.com/2010/02/red-condor-visi-bouncing-emails-with.html
	$subject = "ClinicCases Message from $sender_name" . "\n";

	$email_to = "$d[email]" . "\n";
	$headers = 'From: ' . $CC_default_email . "\n" .
	   'Reply-To: ' . $CC_default_email . "\n" .
	   'X-Mailer: PHP/' . phpversion();

{'fraction_non_alphanumeric': 0.18747517542698267, 'fraction_numerical': 0.0058254998014034155, 'mean_word_length': 4.231301939058172, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '60947', 'n_tokens_mistral': 3046, 'n_tokens_neox': 2775, 'n_words': 682}