Refactor

https://softwareengineering.stackexchange.com/a/178320


// set watch
if ($this->db->autosubscribe && !$comment_on_id)
{
	// subscribe the author
	if ($reg === true)
	{
		$this->set_watch($user_id, $page_id);
	}

	// subscribe & notify moderators
	if (!$mute)
	{
		$this->notify_new_page($page_id, $tag, $title, $user_id, $user_name);
	}
}

if ($comment_on_id && !$mute)
{
	// notifying watchers
	$this->notify_watcher($page_id, $comment_on_id, $tag, $title, $body, $user_id, $user_name, false, $minor_edit);
}


// send code
$msg = new Notification($this);
$msg->notify_password_reset($user, $code);



<?php

if (!defined('IN_WACKO'))
{
	exit('No direct script access allowed');
}

/*

########################################################
##              Notification Constructor              ##
########################################################

*/

class Notification
{
	// VARIABLES
	var $engine;

	// CONSTRUCTOR
	function __construct(&$engine)
	{
		$this->db		= & $config;
		$this->engine	= & $engine;
		#$this->engine->load_translation($this->engine->db->language);
	}

	// NOTIFICATIONS

	// user email wrapper
	function send_user_email($user, $subject, $body, $xtra_headers = [])
	{
		if ($user === 'System')
		{
			$user = [
				'user_name'		=> $this->db->admin_name,
				'email'			=> $this->db->admin_email,
				'user_lang'		=> $this->db->language
			];
		}

		$save		= $this->set_language($user['user_lang'], true);

		$email_to	=	$user['email'];
		$name_to	=	$user['user_name'];
		$subject	=	'[' . $this->db->site_name . '] ' . $subject;
		$body		=	$this->engine->_t('EmailHello') . $user['user_name'] . ",\n\n" .

						Ut::amp_decode($body) . "\n\n" .

						$this->engine->_t('EmailDoNotReply') . "\n\n" .
						# $this->engine->_t('EmailGoodbye') . "\n" .
						$this->db->site_name . "\n" .
						$this->db->base_url;

		$charset	=	$this->engine->get_charset($user['user_lang']);

		$this->engine->set_language($save, true);

		$email = new Email($this);
		$email->send_mail($email_to, $name_to, $subject, $body, null, $charset, $xtra_headers = []);
	}

	function notify_approved_account($user)
	{
		$save		= $this->engine->set_language($user['user_lang'], true);

		$subject	=	$this->engine->_t('RegistrationApproved');
		$body		=	Ut::perc_replace($this->engine->_t('UserApprovedInfo'), $this->db->site_name) . "\n\n" .
						$this->engine->_t('EmailRegisteredLogin') . "\n\n";

		$this->engine->set_language($save, true);

		$this->send_user_email($user, $subject, $body);
	}

	function notify_new_account($user)
	{
		/* TODO: set user language for email encoding */
		$lang_admin	= $this->db->language;
		$save		= $this->engine->set_language($lang_admin, true);

		$subject	=	$this->engine->_t('NewAccountSubject');
		$body		=	$this->engine->_t('NewAccountSignupInfo') . "\n\n" .
						$this->engine->_t('NewAccountUsername') . ' ' .	$user['user_name'] . "\n" .
						$this->engine->_t('RegistrationLang') . ' ' .	$user['user_lang'] . "\n" .
						$this->engine->_t('NewAccountEmail') . ' ' .	$user['email'] . "\n" .
						$this->engine->_t('NewAccountIP') . ' ' .		$user['user_ip'] . "\n\n";

		if ($this->db->approve_new_user)
		{
			$body .= Ut::perc_replace($this->engine->_t('UserRequiresApproval'), $this->db->site_name);
		}

		$this->send_user_email('System', $subject, $body);
		$this->engine->set_language($save, true);
	}

	function notify_new_owner($user)
	{
		$save		= $this->engine->set_language($user['user_lang'], true);

		$subject	=	$this->engine->_t('NewPageOwnership');
		// STS TODO ou, pure shit message!
		$body		=	Ut::perc_replace($this->engine->_t('YouAreNewOwner'), $this->engine->get_user_name()) . "\n\n" .
						$user['owned'] . "\n" .
						$this->engine->_t('PageOwnershipInfo') . "\n";

		$this->engine->set_language($save, true);

		$this->send_user_email($user, $subject, $body);
	}

	function notify_user_signup($user)
	{
		$save = $this->engine->set_language($user['user_lang'], true);

		$subject	=	$this->engine->_t('EmailWelcome');
		$body		=	Ut::perc_replace($this->engine->_t('EmailRegistered'),
							$this->db->site_name, $user['user_name'],
							$this->user_email_confirm($user['user_id'])) . "\n\n" .
						($this->db->approve_new_user
							? $this->engine->_t('UserWaitingApproval')
							: $this->engine->_t('EmailRegisteredLogin') ) . "\n\n" .
						$this->engine->_t('EmailRegisteredIgnore') . "\n\n";

		$this->send_user_email($user, $subject, $body);
		$this->engine->set_language($save, true);
	}

	function notify_password_reset($user, $code)
	{
		$save		= $this->engine->set_language($user['user_lang'], true);

		$subject	=	$this->engine->_t('EmailForgotSubject');
		$body		=	Ut::perc_replace($this->engine->_t('EmailForgotMessage'),
							$this->db->site_name,
							$user['user_name'],
							$this->engine->href('', '', ['secret_code' => $code])) . "\n\n";

		$this->send_user_email($user, $subject, $body);
		$this->engine->set_language($save, true);
	}

	function notify_pm($user, $subject, $body, $header, $msg_id)
	{
		$save	= $this->engine->set_language($user['user_lang'], true);

		$body		=	Ut::perc_replace($this->engine->_t('UsersPMBody'),
							$this->engine->get_user_name()) . "\n\n" .

						'----------------------------------------------------------------------' . "\n" .
						$body . "\n" .
						'----------------------------------------------------------------------' . "\n\n" .

						$this->engine->_t('UsersPMReply') . "\n\n" .
						Ut::amp_decode($this->engine->href('', '',
							['profile'	=> $this->engine->get_user_name(),
							'ref'		=> Ut::http64_encode(gzdeflate($msg_id . '@@' . $subject, 9)),
							'#'			=> 'contacts'])) . "\n\n";

						// XXX: do we really need this, less clutter we want
						# Ut::perc_replace($this->engine->_t('PMAbuseInfo'), $this->db->abuse_email);

		$this->send_user_email($user, $subject, $body, $header);
		$this->engine->set_language($save, true);
	}

	function notify_new_page($page_id, $tag, $title, $user_id, $user_name)
	{
		$subject[]	=	'NewPageCreatedSubj';
		$subject[]	=	$title;

		$body[]		=	'NewPageCreatedBody';
		$body[]		=	$user_name;
		$body[]		=	$title;
		$body[]		=	$this->engine->href('', $tag);

		$this->notify_moderator($page_id, $user_id, $subject, $body);
	}

	function notify_upload($page_id, $file_id, $tag, $file_name, $user_id, $user_name, $replace)
	{
		$subject[]	=	'FileUploadedSubj';
		$subject[]	=	$file_name;

		$body[]		=	$replace? 'FileReplacedBody' : 'FileUploadedBody';
		$body[]		=	$user_name;
		$body[]		=	$file_name . "\n" . $page_id? $tag : $this->engine->_t('UploadGlobal');
		$body[]		=	$this->engine->href('filemeta', '', ['m' => 'show', 'file_id' => (int) $file_id]);

		$this->notify_moderator($page_id, $user_id, $subject, $body);
	}

	function notify_moderator($page_id, $user_id, $subject, $body)
	{
		// subscribe & notify moderators
		if (isset($this->db->groups['Moderator']) && is_array($this->db->groups['Moderator']))
		{
			$members = $this->db->load_all(
				"SELECT m.user_id, u.user_name, u.email, s.user_lang, u.email_confirm, u.enabled, s.send_watchmail " .
				"FROM " . $this->db->table_prefix . "usergroup g " .
					"INNER JOIN " . $this->db->table_prefix . "usergroup_member m ON (g.group_id = m.group_id) " .
					"INNER JOIN " . $this->db->table_prefix . "user u ON (m.user_id = u.user_id) " .
					"LEFT JOIN " . $this->db->table_prefix . "user_setting s ON (u.user_id = s.user_id) " .
				"WHERE g.group_name = 'Moderator'");

			foreach ($members as $user)
			{
				if ($user_id != $user['user_id'])
				{
					if ($this->db->enable_email && $this->db->enable_email_notification && $user['enabled'] && !$user['email_confirm'] && $user['send_watchmail'])
					{
						$save		= $this->engine->set_language($user['user_lang'], true);

						$_subject	=	$this->engine->_t($subject[0]) . " '$subject[1]'";

						$_body		=	# $this->engine->_t('EmailModerator') . ".\n\n" .
										Ut::perc_replace($this->engine->_t($body[0]), ($body[1] == GUEST ? $this->engine->_t('Guest') : $body[1])) . "\n\n" .
										"'" . $body[2] . "'" . "\n" .
										$body[3] . "\n\n";

						$this->send_user_email($user, $_subject, $_body);

						$this->engine->set_language($save, true);

						if ($page_id)
						{
							$this->engine->set_watch($user['user_id'], $page_id);
						}
					}
				}
			}
		}
	}

	/*
	 * notify watchers on new comment creation or existing page change
	 */
	function notify_watcher($page_id, $comment_on_id, $tag, $title, $page_body, $user_id, $user_name, $minor_edit)
	{
		if (!$title)
		{
			$title = $tag;
		}

		if ($comment_on_id)
		{
			$object_id		= $comment_on_id;
			$page_title		= $this->engine->get_page_title('', $comment_on_id);
		}
		else
		{
			$object_id			= $page_id;
			// revisions diff
			$page = $this->db->load_single(
				"SELECT revision_id " .
				"FROM " . $this->db->table_prefix . "revision " .
				"WHERE tag = " . $this->db->q($tag) . " " .
				"ORDER BY modified DESC " .
				"LIMIT 1");

			$_GET['a']			= -1;
			$_GET['b']			= $page['revision_id'];
			$_GET['diffmode']	= 2; // 2 - source diff
			$diff				= $this->engine->method('diff');
		}

		// get watchers
		$watchers	= $this->db->load_all(
			"SELECT DISTINCT
				w.user_id, w.comment_id, w.pending,
				u.email, u.user_name, u.email_confirm, u.enabled,
				s.user_lang, s.send_watchmail, s.notify_minor_edit, s.notify_page, s.notify_comment " .
			"FROM " . $this->db->table_prefix . "watch w " .
				"LEFT JOIN " . $this->db->table_prefix . "user u ON (w.user_id = u.user_id) " .
				"LEFT JOIN " . $this->db->table_prefix . "user_setting s ON (w.user_id = s.user_id) " .
			"WHERE w.page_id = '" . (int) $object_id . "'");

		foreach ($watchers as $watcher)
		{
			if ($watcher['user_id'] != $user_id && $watcher['user_name'] != GUEST)
			{
				if ($comment_on_id)
				{
					// assert that user has no comments pending...
					if ($watcher['notify_comment'] > 1)
					{
						// ...and add one if so
						if (!$watcher['comment_id'])
						{
							$this->db->sql_query(
								"UPDATE " . $this->db->table_prefix . "watch SET " .
									"comment_id	= '" . (int) $page_id . "' " .
								"WHERE page_id = '" . (int) $comment_on_id . "' " .
									"AND user_id = '" . $watcher['user_id'] . "'");
						}
						else
						{
							continue;	// skip current watcher
						}
					}
					else if (!$watcher['notify_comment'])
					{
						continue;	// skip current watcher
					}
				}
				else
				{
					if (($minor_edit && !$watcher['notify_minor_edit']) || !$watcher['notify_page'])
					{
						continue;	// skip current watcher
					}

					// assert that user has no comments pending...
					if ($watcher['notify_page'] > 1)
					{
						// ...and add one if so
						if (!$watcher['pending'])
						{
							$this->db->sql_query(
								"UPDATE " . $this->db->table_prefix . "watch SET " .
									"pending	= '1' " .
								"WHERE page_id = '" . (int) $comment_on_id . "' " .
									"AND user_id = '" . $watcher['user_id'] . "'");
						}
						else
						{
							continue;	// skip current watcher
						}
					}
				}

				// removes user from subscription if access writes were revoked
				if (!$this->engine->has_access('read', $object_id, $watcher['user_name']))
				{
					$this->engine->clear_watch($watcher['user_id'], $object_id);
					continue;
				}

				if ($this->db->enable_email
					&& $this->db->enable_email_notification
					&& $watcher['enabled']
					&& !$watcher['email_confirm']
					&& $watcher['send_watchmail'])
				{
					$lang = $watcher['user_lang'];
					$save = $this->engine->set_language($lang, true);

					$body = ($user_name == GUEST ? $this->engine->_t('Guest') : $user_name);

					if ($comment_on_id)
					{
						$subject = $this->engine->_t('CommentForWatchedPage') . "'" . $page_title . "'";

						$body .=
								$this->engine->_t('SomeoneCommented') . "\n" .
								$this->engine->href('', $this->engine->get_page_tag($comment_on_id), '') . "\n\n" .
								$title . "\n" .
								"----------------------------------------------------------------------\n\n" .
								$page_body . "\n\n" .
								"----------------------------------------------------------------------\n\n";

						if ($watcher['notify_comment'] == 2)
						{
							$body .= $this->engine->_t('FurtherPending') . "\n\n";
						}
					}
					else
					{
						$subject = $this->engine->_t('WatchedPageChanged') . "'" . $tag . "'";

						$body .=
								$this->engine->_t('SomeoneChangedThisPage') . "\n" .
								$this->engine->href('', $tag) . "\n\n" .
								$title . "\n" .
								"======================================================================\n\n" .
								$this->engine->format($diff, 'html2mail') . "\n\n" .
								"======================================================================\n\n";

						if ($watcher['notify_page'] == 2)
						{
							$body .= $this->engine->_t('FurtherPending') . "\n\n";
						}
					}

					$this->send_user_email($watcher, $subject, $body);
					$this->engine->set_language($save, true);
				}
			}
		}
	}
}

Show Files (1 file)