<?php
/***************************************************************************
* copyright            : (C) 2001-2003 Advanced Internet Designs Inc.
* email                : forum@prohost.org
* $Id: admmassemail.php,v 1.1.1.1.8.1 2004/11/24 15:02:10 kazuyan Exp $
*
* This program is free software; you can redistribute it and/or modify it 
* under the terms of the GNU General Public License as published by the 
* Free Software Foundation; either version 2 of the License, or 
* (at your option) any later version.
***************************************************************************/

	require('./GLOBALS.php'); fud_phpgw();
	fud_use('adm.inc', true);
	fud_use('widgets.inc', true);
	fud_use('smtp.inc');

	/* find groups with members */
	$groups = array();
	$c = uq('select count(*), g.id, g.name from '.$DBHOST_TBL_PREFIX.'group_members gm INNER JOIN '.$DBHOST_TBL_PREFIX.'groups g ON g.id=gm.group_id WHERE gm.user_id NOT IN(0,2147483647) GROUP BY g.id, g.name');
	while (list($cnt, $gid, $gname) = db_rowarr($c)) {
		$groups[$gid] = array($gname, $cnt);
	}

	$err = 0;

	if (!empty($_POST['subject']) && !empty($_POST['body'])) {
		if (!$_POST['group']) {
			$c = uq('SELECT email FROM '.$DBHOST_TBL_PREFIX.'users '.(isset($POST['ignore_override']) ? '' : 'WHERE (users_opt & 8)=0'));
		} else if (!isset($groups[$_POST['group']])) {
			echo '<font color="+1" color="red">'.lang('Invalid group id').'</font><br />';
			$err = 1;
			$c = uq('SELECT id FROM '.$DBHOST_TBL_PREFIX.'users WHERE id=-1');
		} else {
			$c = uq('SELECT email FROM '.$DBHOST_TBL_PREFIX.'group_members gm INNER JOIN '.$DBHOST_TBL_PREFIX.'users u ON u.id=gm.user_id WHERE gm.group_id='.$_POST['group'].(isset($POST['ignore_override']) ? '' : ' AND (users_opt & 8)=0'));
		}

		$email_block = 50;
		$total = $email_block_stat = 0;
		$send_to = $ADMIN_EMAIL;
		$to = array();

		if (!($FUD_OPT_1 & 512)) {
			if (version_compare("4.3.3RC2", PHP_VERSION, ">")) {
				$_POST['body'] = str_replace("\n.", "\n..", $_POST['body']);
			}
			if (extension_loaded('mbstring') && function_exists('mb_send_mail') && $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'] == 'ja')
			{
				$mail_func = 'mb_send_mail';
				mb_language($GLOBALS['phpgw_info']['user']['preferences']['common']['lang']);
			}
			else
				$mail_func = 'mail';

			while ($r = db_rowarr($c)) {
				$to[] = $r[0];
				if (!(++$email_block_stat % $email_block)) {
					$email_block_stat = 0;
					$send_to = array_pop($to);
					$bcc = implode(', ', $to) . "\r\n";
					$mail_func($send_to, $_POST['subject'], $_POST['body'], "From: ".$ADMIN_EMAIL."\r\nReply-To: ".$ADMIN_EMAIL."\r\nErrors-To: ".$ADMIN_EMAIL."\r\nX-Mailer: FUDforum v".$GLOBALS['FORUM_VERSION']."\r\nBcc: ".$bcc);
					$to = array();
				}
				++$total;
			}
			if (count($to)) {
				$send_to = array_pop($to);
				$bcc = implode(', ', $to) . "\r\n";
				$mail_func($send_to, $_POST['subject'], $_POST['body'], "From: ".$ADMIN_EMAIL."\r\nReply-To: ".$ADMIN_EMAIL."\r\nErrors-To: ".$ADMIN_EMAIL."\r\nX-Mailer: FUDforum v".$GLOBALS['FORUM_VERSION']."\r\nBcc: ".$bcc);
			}
		} else {
			$smtp = new fud_smtp;
			$smtp->msg = str_replace("\n.", "\n..", $_POST['body']);
			$smtp->subject = $_POST['subject'];
			$smtp->from = $ADMIN_EMAIL;
			$smtp->headers = "Reply-To: ".$ADMIN_EMAIL."\r\nErrors-To: ".$ADMIN_EMAIL."\r\n";

			while ($r = db_rowarr($c)) {
				$to[] = $r[0];
				if (!(++$email_block_stat % $email_block)) {
					$email_block_stat = 0;

					$smtp->to = $to;
					$smtp->send_smtp_email();

					$to = array();
				}
				++$total;
			}
			if (count($to)) {
				$smtp->to = $to;
				$smtp->send_smtp_email();
			}
		}

		/*if (!$err) {
			echo '<font size="+1" color="green">'.lang('%1 E-mails were sent', $total).'</font><br />';
		}*/
	}

	require($WWW_ROOT_DISK . 'adm/admpanel.php');
?>
<h2><?php echo lang('Mass Mail System') ?></h2>
<?php
	if (isset($_POST['btn_submit']) && !$err) {
		echo '<font size="+1" color="green">'.lang('%1 E-mails were sent', $total).'</font><br />';
	}
?>
<form method="post" name="a_frm">
<?php echo _hs; ?>
<table border="0" cellspacing="1" cellpadding="3">
<?php
	if ($groups) {
		echo '<tr bgcolor="#bff8ff"><td valign="top">'.lang('Send E-mails To').'</td><td><select name="group">';
		echo '<option value="0">'.lang('All Forum Members').'</option>';
		foreach ($groups as $k => $v) {
			echo '<option value="'.$k.'">'.lang('%1 member(s) of group %2', $v[1], htmlspecialchars($v[0])).'</option>';
		}
		echo '</select></td></tr>';
	} else {
		echo '<input type="hidden" name="group" value="0">';
	}
?>
	<tr bgcolor="#bff8ff">
		<td valign="top"><?php echo lang('Subject') ?></td>
		<td><input type="text" name="subject" value=""></td>
	</tr>
	<tr bgcolor="#bff8ff">
		<td colspan="2" valign="top">
			<b><?php echo lang('Body') ?></b><br>
			<textarea name="body" cols="80" rows="25"></textarea>
		</td>
	</tr>
	<tr bgcolor="#bff8ff">
		<td colspan="2" align="right">
			<input type="checkbox" name="ignore_override" value="1"> <?php echo lang('Ignore User Override') ?> <input type="submit" value="<?php echo lang('Send') ?>" name="btn_submit">
		</td>
	</tr>
</table>
</form>
<?php require($WWW_ROOT_DISK . 'adm/admclose.php'); ?>
