1<?php
2/**
3 * MyBB 1.8
4 * Copyright 2014 MyBB Group, All Rights Reserved
5 *
6 * Website: http://www.mybb.com
7 * License: http://www.mybb.com/about/license
8 *
9 */
10
11/**
12 * Build the mass email SQL query for the specified conditions.
13 *
14 * @param array $conditions Array of conditions to match users against.
15 * @return string The generated search SQL
16 */
17function build_mass_mail_query($conditions)
18{
19	global $db;
20
21	if(!is_array($conditions))
22	{
23		return '';
24	}
25
26	$search_sql = 'u.allownotices=1';
27
28	// List of valid LIKE search fields
29	$user_like_fields = array("username", "email");
30	foreach($user_like_fields as $search_field)
31	{
32		if($conditions[$search_field])
33		{
34			$search_sql .= " AND u.{$search_field} LIKE '%".$db->escape_string_like($conditions[$search_field])."%'";
35		}
36	}
37
38	// LESS THAN or GREATER THAN
39	$direction_fields = array("postnum");
40	foreach($direction_fields as $search_field)
41	{
42		$direction_field = $search_field."_dir";
43		if(!empty($conditions[$search_field]) && $conditions[$direction_field])
44		{
45			switch($conditions[$direction_field])
46			{
47				case "greater_than":
48					$direction = ">";
49					break;
50				case "less_than":
51					$direction = "<";
52					break;
53				default:
54					$direction = "=";
55			}
56			$search_sql .= " AND u.{$search_field}{$direction}'".(int)$conditions[$search_field]."'";
57		}
58	}
59
60	// Time-based search fields
61	$time_fields = array("regdate", "lastactive");
62	foreach($time_fields as $search_field)
63	{
64		$time_field = $search_field."_date";
65		$direction_field = $search_field."_dir";
66		if(!empty($conditions[$search_field]) && $conditions[$time_field] && $conditions[$direction_field])
67		{
68			switch($conditions[$time_field])
69			{
70				case "hours":
71					$date = $conditions[$search_field]*60*60;
72					break;
73				case "days":
74					$date = $conditions[$search_field]*60*60*24;
75					break;
76				case "weeks":
77					$date = $conditions[$search_field]*60*60*24*7;
78					break;
79				case "months":
80					$date = $conditions[$search_field]*60*60*24*30;
81					break;
82				case "years":
83					$date = $conditions[$search_field]*60*60*24*365;
84					break;
85				default:
86					$date = $conditions[$search_field]*60*60*24;
87			}
88
89			switch($conditions[$direction_field])
90			{
91				case "less_than":
92					$direction = ">";
93					break;
94				case "more_than":
95					$direction = "<";
96					break;
97				default:
98					$direction = "<";
99			}
100			$search_sql .= " AND u.{$search_field}{$direction}'".(TIME_NOW-$date)."'";
101		}
102	}
103
104	// Usergroup based searching
105	if(!empty($conditions['usergroup']))
106	{
107		if(!is_array($conditions['usergroup']))
108		{
109			$conditions['usergroup'] = array($conditions['usergroup']);
110		}
111
112		$conditions['usergroup'] = array_map('intval', $conditions['usergroup']);
113
114		$additional_sql = '';
115		foreach($conditions['usergroup'] as $usergroup)
116		{
117			switch($db->type)
118			{
119				case "pgsql":
120				case "sqlite":
121					$additional_sql .= " OR ','||additionalgroups||',' LIKE '%,{$usergroup},%'";
122					break;
123				default:
124					$additional_sql .= " OR CONCAT(',',additionalgroups,',') LIKE '%,{$usergroup},%'";
125			}
126		}
127		$search_sql .= " AND (u.usergroup IN (".implode(",", $conditions['usergroup']).") {$additional_sql})";
128	}
129
130	return $search_sql;
131}
132
133/**
134 * Create a text based version of a HTML mass email.
135 *
136 * @param string $message The HTML version.
137 * @return string The generated text based version.
138 */
139function create_text_message($message)
140{
141	// Cut out all current line breaks
142	// Makes links CONTENT (link)
143	$message = make_pretty_links($message);
144	$message = str_replace(array("\r\n", "\n"), "\n", $message);
145	$message = preg_replace("#</p>#i", "\n\n", $message);
146	$message = preg_replace("#<br( \/?)>#i", "\n", $message);
147	$message = preg_replace("#<p[^>]*?>#i", "", $message);
148	$message = preg_replace("#<hr[^>]*?>\s*#i", "-----------\n", $message);
149	$message = html_entity_decode($message);
150	$message = str_replace("\t", "", $message);
151	do
152	{
153		$message = str_replace("  ", " ", $message);
154	}
155	while(strpos($message, "  ") !== false);
156
157	$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
158				   '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
159				   '@<title[^>]*?>.*?</title>@siU',    // Strip title tags
160				   '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
161				   '@<![\s\S]*?--[ \t\n\r]*>@'        // Strip multi-line comments including CDATA
162	);
163	$message = preg_replace($search, '', $message);
164	$message = preg_replace("#\n\n+#", "\n\n", $message);
165	$message = preg_replace("#^\s+#is", "", $message);
166	return $message;
167}
168
169/**
170 * Generates friendly links for a text based version of a mass email from the HTML version.
171 *
172 * @param string $message_html The HTML version.
173 * @return string The version with the friendly links and all <a> tags stripped.
174 */
175function make_pretty_links($message_html)
176{
177	do
178	{
179		$start = stripos($message_html, "<a");
180		if($start === false)
181		{
182			break;
183		}
184		$end = stripos($message_html, "</a>", $start);
185		if($end === false)
186		{
187			break;
188		}
189
190		$a_href = substr($message_html, $start, ($end-$start));
191
192		preg_match("#href=\"?([^\"> ]+)\"?#i", $a_href, $href_matches);
193		if(!$href_matches[1])
194		{
195			continue;
196		}
197		$link = $href_matches[1];
198
199		$contents = strip_tags($a_href);
200		if(!$contents)
201		{
202			preg_match("#alt=\"?([^\">]+)\"?#i", $a_href, $matches2);
203			if($matches2[1])
204			{
205				$contents = $matches2[1];
206			}
207			if(!$contents)
208			{
209				preg_match("#title=\"?([^\">]+)\"?#i", $a_href, $matches2);
210				if($matches2[1])
211				{
212					$contents = $matches2[1];
213				}
214			}
215		}
216
217		$replaced_link = $contents." ({$link}) ";
218
219		$message_html = substr_replace($message_html, $replaced_link, $start, ($end-$start));
220	} while(true);
221	return $message_html;
222}
223