Location: PHPKode > projects > BugIn and Ticketing System > bugin-0.65/lib/notify.inc.php
<?php
// Notification Functions {{{1
/* function parseEmailTemplate($text) {{{2
		parses notify text. */
function parseEmailTemplate($template, $replaceStrings) {

	global $options;
	$searchStrings = array("%ACTION%", "%MOD%", "%LONG_MOD%", "%DESC%", "%LONG_DESC%", "%NUM%", "%MOD_BY%", "%CREATOR%", "%OWNER%", "%DATE%", "%BASE_URL%", "%SYSTEM_NAME%", "%TICKET_NAME%", "\%");
	$replaceStrings[] = date("l dS of F Y h:i:s: A");
	$replaceStrings[] = $options['baseURL'];
	$replaceStrings[] = $options['systemName'];
	$replaceStrings[] = $options['ticketName'];
	$replaceStrings[] = "%";

	$template = str_replace($searchStrings, $replaceStrings, $template);

	return $template;
}
/* }}}2 */

/* function generateNotifyList($ticket, $newTicket) {{{2
	generates a notify list from the supplied tickets */
function generateNotifyList($ticket, $mod) {
	global $bugDatabase;

	$email = array();
	$emailCount = 0;
	// Project e-mail addresses....
	if ($bugDatabase->lists['project'][$ticket['project']]['email'])
		$email[++$emailCount] = array('email' => $bugDatabase->lists['project'][$ticket['project']]['email']);

	if ($bugDatabase->lists['project'][$mod['project']]['email'])
		$email[++$emailCount] = array('email' => $bugDatabase->lists['project'][$mod['project']]['email']);

	// Creator e-mail address
	if ($ticket['creator'] && ($bugDatabase->lists['user'][$ticket['creator']]['options'] & OPTION_NOTIFY)) {
		$email[++$emailCount] = array(
			'email' => $bugDatabase->lists['user'][$ticket['creator']]['email'],
			'name' => $bugDatabase->lists['user'][$ticket['creator']]['name']
		);
	}

	// Owner e-mail addresses
	if ($ticket['owner'] && $bugDatabase->lists['user'][$ticket['owner']]['options'] & OPTION_NOTIFY) {
		$email[++$emailCount] = array(
			'email' => $bugDatabase->lists['user'][$ticket['owner']]['email'],
			'name' => $bugDatabase->lists['user'][$ticket['owner']]['name']
		);
	}

	// new owner e-mail address
	if ($mod['owner'] && $bugDatabase->lists['user'][$mod['owner']]['options'] & OPTION_NOTIFY) {
		$email[++$emailCount] = array(
			'email' => $bugDatabase->lists['user'][$mod['owner']]['email'],
			'name' => $bugDatabase->lists['user'][$mod['owner']]['name']
		);
	}

	$modDistEmail = array();

	// Quickly over-complicate the problem, and VOLA!
	while (list($key, $value) = @each($email)) {
		if ($value['email']) {
			$value['email'] = trim($value['email']);
			if (!in_array($value['email'], $modDistEmail) &&
					(isset($value['name']) && (!in_array("{$value['name']} <{$value['email']}>", $modDistEmail)))) {
				if (isset($value['name'])) {
					$modDistEmail[] = "{$value['name']} <{$value['email']}>";
				}
				else {
					$modDistEmail[] = $value['email'];
				}
			}
		}
	}

	$notifyList = implode(",", $modDistEmail);

	return ($notifyList);

}
/* }}}2 */

function getEmailName($ticket, $seed) {
	//printVar($ticket);
	$seeding = (($seed * $ticket['id']) / 1000000);
	mt_srand($seeding);
	global $options;
	$st = "";
	for ($i = 0; $i < 10; $i ++) {
		$st .= strtoupper(getBaseString(mt_rand(1,36), 36));
	}
	
	return "<{$seed}.{$st}@{$_SERVER['SERVER_NAME']}>";
}

/* function runNotify($ticket, $newTicket, $mod, $notifyList) {{{2
		Used to notify all the people who need to be notified. */
function runNotify($ticket, $newTicket, $mod, $notifyList) {
	global $options;
	global $bugDatabase;
	global $systemName;
	global $buginVersion;
	global $modEmail;
	global $userinfo;
	global $verbage;

	$firstMessageId = getEmailName($newTicket, $ticket['date_added']);
	$messageId = getEmailName($newTicket, $newTicket['date_modified']);

	$msgid = "In-Reply-To: {$firstMessageId}\nMessage-ID: {$messageId}";
	switch($_REQUEST['area']) {
		case "add_submit":
			$action = $verbage['added'];
			$msgid = "Message-ID: {$firstMessageId}";
			break;
		case "modify":
			if ($ticket['status'] != $newTicket['status']) {
				if ($bugDatabase->lists['status'][$newTicket['status']]['descr'] != "")
					$action = $bugDatabase->lists['status'][$newTicket['status']]['descr'];
				else
					$action = $verbage['modified'];
			}
			else {
				$action = $verbage['modified'];
			}
			break;
		default:
			$action = $verbage['modified'];
			break;
	}

	$longModArray = array ();
	$emailCount = 0;

	if ($ticket['creator'])
		$tCreator = $bugDatabase->lists['user'][$ticket['creator']]['name'];
  else
		$tCreator = $verbage['unknown'];

	if ($ticket['owner']) {
		$tOwner = $bugDatabase->lists['user'][$ticket['owner']]['name'];
	}
	else {
		$tOwner = $verbage['unowned'];
	}

	if ($options['notifyByName'] == 1) {
		$from = $userinfo['name']." <".$userinfo['email'].">";
	}
	else
		$from = $options['systemName']. " <".$options['modEmail'].">";

	$revMods = @array_reverse($newTicket['modifications']);

	while (list($key, $value) = @each($revMods)) {
		$longModArray[] = "{$value['date']}\n{$value['text']}";
	}

	$longMod = @implode("\n---\n", $longModArray);

	// Notify the users that the ticket has been changed.
	if (!isset($mod['modification']))
		$mod['modification'] = "";

	if (!isset($_SESSION['userinfo'])) {
		if ($_REQUEST['area'] == "scheduler")
			$modby = USER_SCHEDULER;
		else
			$modby = USER_SYSTEM;
	}
	else
		$modby = $_SESSION['userinfo']['uid'];

		$templateArr = array($action,
	                     $mod['modification'],
	                     $longMod, $newTicket['short_desc'],
	                     $newTicket['long_desc'],
	                     $ticket['id'],
	                     $bugDatabase->lists['user'][$modby]['name'],
	                     $tCreator,
	                     $tOwner);
	$notification = wordwrap(parseEmailTemplate($options['notificationTemplate'], $templateArr));
	$subject = parseEmailTemplate($options['notificationSubjectTemplate'], $templateArr);

	// Clean subject and notify list....
	$subject = str_replace("\n", "", $subject);
	$subject = str_replace("\r", "", $subject);
	$notifyList = str_replace("\n", "", $notifyList);
	$notifyList = str_replace("\r", "", $notifyList);

	$headers = "From: {$from}\n{$msgid}";
        
	// Return message....
	$message = htmlentities("Sending alert to Notify List: {$notifyList}\nFrom: $from\n".$msgid);
	if (!($userinfo['options'] & OPTION_SHORT_NOTIFICATION_NOTIFY))
		$message .= $notification;

	buginError(nl2br(htmlentities($message)), MESSAGE_DEBUG);
	if (((!isset($_REQUEST['noEmail']) || !$_REQUEST['noEmail'])) && !$options['noEmail']) {
		mail(chop($notifyList), $subject, $notification, $headers);
		buginError("mail sent", MESSAGE_DEBUG);
		return $message;
	}
	else
		return "Modification Complete, but no notification was sent.";

}

/* }}}2 */
/* Notification Functions }}}1 */
?>
Return current item: BugIn and Ticketing System