⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 general_functions.php

📁 本代码是为客户联系管理而做的系统
💻 PHP
📖 第 1 页 / 共 5 页
字号:
		$user_details = update_user_details($user_details);
			
		eval(makeemaileval('message', 'BODY_question_staff_reply', $subject, $ticket));
		dp_mail($user_details[email], $subject, trim($message), $gateway[email], $settings[email_return], $attachments, NULL, NULL, NULL, $extrainfo['headers']);	

		ticketlog($ticket['id'], 'email_sent_to_user', NULL, NULL, "To: $user_details[email], Subject: $subject", '', $ticketlog_attachments);
	}

	return trim($message);
}

/*****************************************************
	function notify_technicians

-----DESCRIPTION: -----------------------------------
	- used to send emails to techs based on ticket changes

-----ARGUMENTS: -------------------------------------
	type			:	is either new (new ticket created) / reply (reply made) / assigned (ticket assigned to user)
	ticket			:	this is the full ticket array, used in emails
	user			:	this is the full user details, used in emails
	message			:	this is the last message (for new/reply)
	attachments		:	array/one of attachments (data, filename, filetype, id)

-----RETURNS:----------------------------------------
	null

*****************************************************/

function notify_technicians($type, $ticket, $user_details, $message = NULL, $attachments='') {
	global $db, $settings, $user;

	// Don't notify for tickets from non-validated users
	if ($user_details['awaiting_validation'] OR $user_details['awaiting_manual_validation']) {
		return;
	}

	// get attachment data for ticketlog
	if (is_array($attachments)) {
		foreach ($attachments AS $key => $var) {
			$ticketlog_attachments[] = array(
				'name' => $var[name],
				'size' => $var[size]
			);
		}
	}
	$ticketlog_attachments = serialize($ticketlog_attachments);
	
	################################################################
	###############          NEW TICKET              ###############
	################################################################
	
	if ($type == 'new') {

		//	- user if the ticket has been assigned to them
		// do this first because attachment option overrides here
		$data = $db->query_return_array("
			SELECT email, '1' AS send_email, GREATEST(email_attachments, email_own_attachments) AS send_attachments, username, cats_admin
			FROM tech
			WHERE email_assigned AND id = '$ticket[tech]'
		");

		//	- gets users who want all new new ticket notifications
		$data2 = $db->query_return_array("
			SELECT email, sms, email_new_email AS send_email, email_new_sms AS send_sms, email_attachments AS send_attachments, username, cats_admin
			FROM tech
			WHERE email_new_email OR email_new_sms
		");

		// - get notifications linked to category / priority
		$data3 = $db->query_return_array("
			SELECT tech.email, tech.sms, tech_email.email AS send_email, tech_email.sms AS send_sms, tech.email_attachments AS send_attachments, tech.username AS username, cats_admin
			FROM tech_email
			LEFT JOIN tech ON (tech_email.techid = tech.id)
			WHERE	(	
				(fieldname = 'category' AND value = '" . mysql_escape_string($ticket[category]) . "')
			OR	(fieldname = 'priority' AND value = '" . mysql_escape_string($ticket[priority]) . "')
			)
				AND	newticket
		");

		$notifications = array_merge($data, $data2, $data3);

		/*********************************************
					Process the data
		*********************************************/

		foreach ($notifications AS $key => $result) {
		
			// check that ticket is in a category the tech has access to
			if ($categories) {
				$categories = explode(',',$result['cats_admin']);
			} else {
				$categories = array();
			}

			// either no category restrictions or this category not in the list
			if ((!in_array($ticket['category'], $categories)) OR !count($categories)) {

				// build array of notifications (including attachment info)
				if ($result['send_email'] == "1") {
					$send_email[] = array(
							'email' => $result[email], 
							'send_attachments' => $result['send_attachments'],
							'tech_username' => $result[username]
						);
				}
				if ($result['send_sms'] == "1") {
					$send_sms[] = array(
							'email' => $result[sms],
							'tech_username' => $result[username]
						);
				}
			}
		}

		$send_email = unique_multi_array($send_email, 'email');
		$send_sms = unique_multi_array($send_sms, 'email');

		$ticket['category'] = get_category_name($ticket['category']);
		$ticket['priority'] = get_priority_name($ticket['priority']);

		/*********************************************
					Send the Emails
		*********************************************/

		if (is_array($send_email)) {
			
			 // are we sending attachments?
			foreach ($send_email AS $toemail) {

				// get rid of last message sent
				unset($email_message);

				if ($toemail[1]) {
					$full_attachments = 1;
					unset($email_attachments);
				} else {
					$email_attachments = 1;
					unset($full_attachments);
				}

				$user_details = update_user_details($user_details);
				eval(makeemaileval('email_message', 'TECHBODY_newquestion', $email_subject, $ticket));

				dp_mail($toemail['email'], $email_subject, $email_message, $settings['email_tech'], NULL, iff($full_attachments, $attachments));	
				$emails_to_tech[] = $toemail['tech_username'];
			}

			// log the emails sent
			ticketlog($ticket[id], 'email_sent_to_tech', NULL, NULL, "To: " . join(', ', $emails_to_tech) . ", Subject: $email_subject", '', $ticketlog_attachments);
		}

		/*********************************************
					Send the SMS
		*********************************************/

		if (is_array($send_sms)) {
			foreach ($send_sms AS $toemail) {

				// get rid of last message sent
				unset($email_message);

				eval(makeemaileval('email_message', 'TECHBODY_newquestion', $email_subject, $ticket));
				dp_mail($toemail['email'], $email_subject, $email_message, $settings['email_tech']);	

				$sms_to_tech[] = $toemail['tech_username'];
			}

			// log the sms messages sent
			ticketlog($ticket[id], 'sms_sent_to_tech', NULL, NULL, "To: " . join(', ', $sms_to_tech) . ", Subject: $email_subject");
		}

	################################################################
	###############          TICKET REPLY            ###############
	################################################################

	} elseif ($type == 'reply') { 
		
		// those for ticket ownership
		$data = $db->query_return_array("
			SELECT email, sms, email_own_email AS send_email, email_own_sms AS send_sms, GREATEST(email_attachments, email_own_attachments) AS send_attachments, username, cats_admin
			FROM tech
			WHERE id = '$ticket[tech]'
				AND (email_own_sms OR email_own_email)
		");		

		// those that get email for all replies
		$data2 = $db->query_return_array("
			SELECT email, sms, email_reply_email AS send_email, email_reply_sms AS send_sms, email_attachments AS send_attachments, username, cats_admin
			FROM tech
			WHERE email_reply_email OR email_reply_sms
		");

		// - get notifications linked to category / priority
		$data3 = $db->query_return_array("
			SELECT tech.email, tech.sms, tech_email.email AS send_email, tech_email.sms AS send_sms, tech.email_attachments AS send_attachments, tech.username AS username, cats_admin
			FROM tech_email
			LEFT JOIN tech ON (tech_email.techid = tech.id)
			WHERE	(	
				(fieldname = 'category' AND value = '" . mysql_escape_string($ticket[category]) . "')
			OR	(fieldname = 'priority' AND value = '" . mysql_escape_string($ticket[priority]) . "')
			)
				AND	newreply
		");			

		$notifications = array_merge($data, $data2, $data3);

		/*********************************************
					Process the data
		*********************************************/

		foreach ($notifications AS $key => $result) {
		
			// check that ticket is in a category the tech has access to
			if ($categories) {
				$categories = explode(',',$result['cats_admin']);
			} else {
				$categories = array();
			}

			// either no category restrictions or this category not in the list
			if ((!in_array($ticket['category'], $categories)) OR !count($categories)) {

				// build array of notifications (including attachment info)
				if ($result['send_email'] == "1") {
					$send_email[] = array(
							'email' => $result[email], 
							'send_attachments' => $result['send_attachments'],
							'tech_username' => $result[username]
						);
				}
				if ($result['send_sms'] == "1") {
					$send_sms[] = array(
							'email' => $result[sms],
							'tech_username' => $result[username]
						);
				}
			}
		}

		$send_email = unique_multi_array($send_email, 'email');
		$send_sms = unique_multi_array($send_sms, 'email');

		$ticket['category'] = get_category_name($ticket['category']);
		$ticket['priority'] = get_priority_name($ticket['priority']);

		/*********************************************
					Send the Emails
		*********************************************/

		if (is_array($send_email)) {
			
			 // are we sending attachments?
			foreach ($send_email AS $toemail) {

				// get rid of last message sent
				unset($email_message);

				if ($toemail[1]) {
					$full_attachments = 1;
					unset($email_attachments);
				} else {
					$email_attachments = 1;
					unset($full_attachments);
				}

				$user_details = update_user_details($user_details);
				eval(makeemaileval('email_message', 'TECHBODY_reply', $email_subject, $ticket));

				dp_mail($toemail['email'], $email_subject, $email_message, $settings['email_tech'], NULL, iff($full_attachments, $attachments));	
				$emails_to_tech[] = $toemail['tech_username'];
			}

			// log the emails sent
			ticketlog($ticket[id], 'email_sent_to_tech', NULL, NULL, "To: " . join(', ', $emails_to_tech) . ", Subject: $email_subject", '', $ticketlog_attachments);
		}

		/*********************************************
					Send the SMS
		*********************************************/

		if (is_array($send_sms)) {
			foreach ($send_sms AS $toemail) {

				// get rid of last message sent
				unset($email_message);

				eval(makeemaileval('email_message', 'TECHBODY_reply_sms', $email_subject, $ticket));
				dp_mail($toemail['email'], $email_subject, $email_message, $settings['email_tech']);	

				$sms_to_tech[] = $toemail['tech_username'];
			}

			// log the sms messages sent
			ticketlog($ticket[id], 'sms_sent_to_tech', NULL, NULL, "To: " . join(', ', $sms_to_tech) . ", Subject: $email_subject");
		}

	################################################################
	###############          TICKET ASSIGNED         ###############
	################################################################

	} elseif ($type == 'assigned') {

		$tech = $db->query_return("
			SELECT username, email
			FROM tech 
			WHERE id = '$ticket[tech]' 
			AND email_assigned
		");

		$newtech['username'] = $tech['username'];
		
		if ($db->num_rows() > 0) {
			
			$ticket['category'] = get_category_name($ticket['category']);
			$ticket['priority'] = get_priority_name($ticket['priority']);

			eval(makeemaileval('email_message', 'TECHBODY_ownership', $email_subject, $ticket));
			dp_mail($tech[email], $email_subject, $email_message, $settings['email_tech'], NULL, $attachments);	
			
			ticketlog($ticket[id], 'email_sent_to_tech', NULL, NULL, "To: $tech[username], Subject: $email_subject", '', $ticketlog_attachments);
		}
	}

	return ($email_message);
}

/*****************************************************
	function get_category_name

-----DESCRIPTION: -----------------------------------
	- displays print_r in <pre> tags for easy viewing

-----ARGUMENTS: -------------------------------------
	var				:	the variable to display

-----RETURNS:----------------------------------------
	nothing

*****************************************************/

function get_category_name($id) {

	global $db;

	if (is_numeric($id)) {
		$tmp = $db->query_return("SELECT name FROM ticket_cat WHERE id = '" . mysql_escape_string($id) . "'");
		$id = $tmp['name'];
	}
	if (!$id) {
		$id = 'Not categorized';
	}
	return $id;
}

/*****************************************************
	function get_priority_name

-----DESCRIPTION: -----------------------------------
	- displays print_r in <pre> tags for easy viewing

-----ARGUMENTS: -------------------------------------
	var				:	the variable to display

-----RETURNS:----------------------------------------
	nothing

*****************************************************/

function get_priority_name($id) {

	global $db;

	if (is_numeric($id)) {
		$tmp = $db->query_return("SELECT name FROM ticket_pri WHERE id = '" . mysql_escape_string($id) . "'");
		$id = $tmp['name'];
	}
	if (!$id) {
		$id = 'Not prioritized';
	}
	return $id;
}

/*****************************************************
	function print_rr

-----DESCRIPTION: -----------------------------------
	- displays print_r in <pre> tags for easy viewing

-----ARGUMENTS: -------------------------------------
	var				:	the variable to display

-----RETURNS:----------------------------------------
	nothing

*****************************************************/

function print_rr($var) {
	echo "<PRE>";
	print_r($var);
	echo "</PRE>";
	return;
}

/*****************************************************
	function fetchip

-----DESCRIPTION: -----------------------------------

	- get ip address

*****************************************************/

function fetchip() { 
	global $_SERVER;
	
	//get useful vars: 
	$client_ip = $_SERVER['HTTP_CLIENT_IP']; 
	$x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR']; 
	$remote_addr = $_SERVER['REMOTE_ADDR']; 
	 
	// then the script itself 
	if (!empty ($client_ip) ) { 
	// Turning the ip adress around if it's saved backwards 	
		$ip_expl = explode('.',$client_ip); 
		$referer = explode('.',$remote_addr); 
	
		if($referer[0] != $ip_expl[0]) { 
			$ip=array_reverse($ip_expl); 
			$return=implode('.',$ip); 
		} else { 
			$return = $client_ip; 
		}
	} elseif (!empty($x_forwarded_for) ) { 
		if (strstr($x_forwarded_for,',')) { // making sure the ip adress isn't a large chain of proxy's, and retrieving only the real one. 
			$ip_expl = explode(',',$x_forwarded_for); 
			return end($ip_expl); 
		} else { 
			return $x_forwarded_for; 
		}
	} else { 
		return $remote_addr; 
	}
	return $return; 
}

/*****************************************************
	function addslashes_like

-----DESCRIPTION: -----------------------------------

	- mysql_escape_string version that removes % and _ as well

*****************************************************/

function addslashes_like($text) {
    return str_replace(array('%', '_'), array('\%', '\_'), mysql_escape_string($text));
}

/*****************************************************
	function addslashes_js

-----DESCRIPTION: -----------------------------------

	- mysql_escape_string for js (' not " and also line carriages)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -