<?php
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*\
<< ******************************************************************** >>
<< * ---------------------------------------------------------------- * >>
<< * Copyright ©2011-2012 Ozzy47                                      * >>
<< * All Rights Reserved. 											  * >>
<< * This file may not be redistributed in whole or significant part. * >>
<< * ---------------------------------------------------------------- * >>
<< * You are not allowed to use this on your server unless the files  * >>
<< * you downloaded were done so with permission.					  * >>
<< * ---------------------------------------------------------------- * >>
<< ******************************************************************** >>
\*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/

// Init some arrays
$usernames = $quoteText = array();

do
{
	if (!$vbulletin->options['dbtech_vbsuper_pm_post_quote_active'] OR !$vbulletin->options['dbtech_vbsuper_pm_system_enabled'])
	{
		// System is inactive, don't send PM quote
		break;
	}
	
	if (!preg_match_all("/\[quote=([^\\\"\]]*?)\](.*?)\[\/quote\]/is", $post['message'], $matches))
	{
		// No quote tags
		break;
	}

	// Shorthand
	$quotes = $matches[1];

	for ($i = 0; $i < count($quotes); $i++)
	{
		// Ensure we support both quotes with and without date stamp
		$username = (strpos($quotes[$i], ';')) ? substr($quotes[$i], 0, strpos($quotes[$i], ';')) : $quotes[$i];
		
		if (strtolower($username) == strtolower($vbulletin->userinfo['username'])) 
		{
			// We're not notifying ourselves
			continue;
		}

		if (!isset($quoteText[$username]))
		{
			// Avoid strict standards notice in PHP 5.4
			$quoteText[$username] = '';
		}

		if (!in_array($username, $usernames)) 
		{
			// New notification user
			$usernames[] = $username;
		}

		// Add text
		$quoteText[$username] .= '[quote=' . $username . ']' . $matches[2][$i] . '[/quote]';
	}

	if (!count($usernames))
	{
		// No users to notify
		break;
	}

	foreach ($usernames as &$quoteUser)
	{
		// Escape naughty stuffs
		$quoteUser = $vbulletin->db->escape_string(htmlspecialchars_uni($quoteUser));
	}

	// Default sender information
	$fromuserid = $vbulletin->userinfo['userid'];
	$fromusername = $vbulletin->userinfo['username'];
	
	if ($vbulletin->options['dbtech_vbsuper_pm_quotenotify_sender']) 
	{
		// We have a custom sender
		$fromuserid = $vbulletin->options['dbtech_vbsuper_pm_quotenotify_sender'];
		$fromtempuser = fetch_userinfo($fromuserid);
		$fromusername = $fromtempuser['username'];
	}

	$users = $vbulletin->db->query_read_slave("
		SELECT user.*
		FROM " . TABLE_PREFIX . "user AS user 
		LEFT JOIN " . TABLE_PREFIX . "userfield AS userfield ON (user.userid = userfield.userid) 
		WHERE user.username IN('" . implode("', '", $usernames) . "')
			AND userfield.dbtech_vbsuper_pm_quote_post_active = '1'
	");
	while ($user = $vbulletin->db->fetch_array($users))
	{
		// Grab our permissions
		cache_permissions($user);

		if (
			!($user['forumpermissions'][$threadinfo['forumid']] & $vbulletin->bf_ugp_forumpermissions['canview']) OR 
			!($user['forumpermissions'][$threadinfo['forumid']] & $vbulletin->bf_ugp_forumpermissions['canviewthreads']) OR 
			!verify_forum_password($threadinfo['forumid'], $foruminfo['password'], false)
		)
		{
			// No permissions to view this forum
			continue;
		}
		
		// Send pm
		$pmdm =& datamanager_init('PM', $vbulletin, ERRTYPE_SILENT);
			$pmdm->set_info('is_automated', true); // implies overridequota
			$pmdm->set('fromuserid', $fromuserid);
			$pmdm->set('fromusername', $fromusername);
			$pmdm->set_recipients(unhtmlspecialchars($user['username']), $user['permissions'], 'cc');
			$pmdm->set('title', construct_phrase($vbphrase['dbtech_vbsuper_pm_quote_post_title'], 
				$user['username']
			));
			$pmdm->set('message', construct_phrase($vbphrase['dbtech_vbsuper_pm_quote_post_message'], 
				$user['username'], 
				$vbulletin->userinfo['username'], 
				$quoteText[$user['username']], 
				(intval($vbulletin->versionnumber) == 4 ? fetch_seo_url('thread|bburl', $threadinfo, array('p' => $post['postid']), 'threadid', 'title') . "#post$post[postid]" : $vbulletin->options['bburl'] . '/showthread.php?' . $vbulletin->session->vars['sessionurl'] . 'p=' . $post['postid'])
			));
			$pmdm->set('dateline', TIMENOW);
			$pmdm->set('showsignature', 1);
			$pmdm->set('allowsmilie', 	0);
		$pmdm->save();
	}
	$vbulletin->db->free_result($users);
	unset($user);
}
while (false);

/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*\
<< ********************************************************************* >>
<< * Created: 09:30, Fri June 8th 2012                                 * >>
<< * VER: 1.0.0                                                        * >>
<< ********************************************************************* >>
\*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
?>