<?php
// +-------------------------------------------------------------+
// | HiveMail™ version 1.3.1
// +-------------------------------------------------------------+
// | HIVEMAIL™ IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than Hivemail or if you have otherwise received
// | this software from someone who is not a representative of
// | this organization you are involved in an illegal activity.
// +-------------------------------------------------------------+
// | $RCSfile: distlist.php,v $ - $Revision: 1.14 $
// | $Date: 2004/08/03 04:28:44 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
require_once('./global.php');
cp_header(' &raquo; Distribution Lists');
cp_nav('emaildist');

// ############################################################################
// Set the default cmd
default_var($cmd, 'modify');

// ############################################################################
// Remove list
if ($_POST['cmd'] == 'kill') {
	$distlist = getinfo('distlist', $distlistid);

	$DB_site->query("
		DELETE FROM hive_distlist
		WHERE distlistid = $distlistid
	");

	adminlog($distlistid, true);
	cp_redirect('The list has been removed.', 'distlist.php');
}

// ############################################################################
// Remove list
if ($cmd == 'remove') {
	$distlist = getinfo('distlist', $distlistid);

	adminlog($distlistid);
	startform('distlist.php', 'kill', 'Are you sure you want to remove this list?');
	starttable('Remove distribution list "'.$distlist['toalias'].getop('domainname').'" (ID: '.$distlistid.')');
	textrow('Are you <b>sure</b> you want to remove this list? This procedure <b>cannot</b> be reversed!');
	hiddenfield('distlistid', $distlistid);
	endform('Remove list', '', 'Go Back');
	endtable();
}

// ############################################################################
// Create a new list or update an existing one
if ($_POST['cmd'] == 'update') {
	if (!preg_match('#^[a-z0-9][a-z0-9_.]+$#i', $distlist['toalias']) or user_exists($distlist['toalias'])) {
		adminlog($distlistid, false);
		cp_error('The email address you have entered is either invalid or taken already.');
	} else {
		$recips = preg_split('#\s#', $distlist['recipients']);
		$sqlrecips = array();
		foreach ($recips as $key => $recip) {
			$recips["$key"] = $recip = trim($recip);
			if (is_email($recip)) {
				// Remote recipients, it's ok
			} elseif (!preg_match('#^[a-z0-9][a-z0-9_.]+$#i', $recip)) {
				unset($recips["$key"]);
			} else {
				$sqlrecips[] = '"'.addslashes($recip).'"';
			}
		}
		$users = $DB_site->query('
			SELECT userid
			FROM hive_user
			WHERE username IN ('.implode(', ', $sqlrecips).')
		');
		if (empty($recips)) {
			cp_error('Please enter at least one target recipient.');
		} elseif (count($sqlrecips) > $DB_site->num_rows($users)) {
			cp_error('Some of the recipients were not found in the database.');
		} else {
			$distlist['recipients'] = serialize($recips);
			if ($distlistid == 0) {
				$DB_site->auto_query('distlist', $distlist);
				adminlog($distlistid, true);
				cp_redirect('The list has been created.', 'distlist.php');
			} else {
				$DB_site->auto_query('distlist', $distlist, "distlistid = $distlistid");
				adminlog($distlistid, true);
				cp_redirect('The list has been updated.', 'distlist.php');
			}
		}
	}
}

// ############################################################################
// Create a new list or update an existing one
if ($cmd == 'edit') {
	startform('distlist.php', 'update', '', array('distlist_toalias' => 'email address'), false, 'for (var i = 0; i < this.distlist_local_select.options.length; i++) this.distlist_recipients.value += this.distlist_local_select.options[i].value + \' \'; for (var i = 0; i < this.distlist_remote_select.options.length; i++) this.distlist_recipients.value += this.distlist_remote_select.options[i].value + \' \';');

	$distlist = getinfo('distlist', $distlistid, false, false, false);
	adminlog($distlistid);
	$localops = $remoteops = '';
	if ($distlist === false) {
		$distlist = array('local' => array(), 'remote' => array());
		$distlistid = 0;
		starttable('Create new distribution list');
	} else {
		$distlist['recipients'] = unserialize($distlist['recipients']);
		$distlist['local'] = $distlist['remote'] = array();
		foreach ($distlist['recipients'] as $recipient) {
			if (strpos($recipient, '@') === false) {
				$distlist['local'][] = $recipient;
				$localops .= "<option value=\"$recipient\">$recipient".getop('domainname')."</option>\n";
			} else {
				$distlist['remote'][] = $recipient;
				$remoteops .= "<option value=\"$recipient\">$recipient</option>\n";
			}
		}
		starttable('Update distribution list "'.$distlist['toalias'].getop('domainname').'" (ID: '.$distlistid.')');
	}

	hiddenfield('distlistid', $distlistid);
	hiddenfield('distlist[recipients]', '');
	inputfield('Email address to expand:', 'distlist[toalias]', $distlist['toalias'], 25, getop('domainname'));
	$localsize = iif(count($distlist['local']) > 5, count($distlist['local']), 5);
	$remotesize = iif(count($distlist['remote']) > 5, count($distlist['remote']), 5);
	tablerow(array(
		'Local recipients:<br /><span class="cp_small">(These are HiveMail&trade; users that will receive<br />messages sent to the address above.)</span><br /><br /><input type="button" class="button" name="new" value="New Recipient" onClick="name = prompt(\'Enter the new recipient (username only):\', \'\'); if (name == null || name == \'\') return false; this.form.distlist_local_select.options[this.form.distlist_local_select.options.length] = new Option(name + \''.getop('domainname').'\', name);" style="width: 150px;" /> <input type="button" class="button" name="remove_local" value="Remove Recipent" disabled="disabled" onClick="if (confirm(\'Are you sure you want to remove this recipient?\')) { this.form.distlist_local_select.options[this.form.distlist_local_select.selectedIndex] = null; this.disabled = true; }" style="width: 150px;" />',
		'<select name="distlist[recip_select]" id="distlist_local_select" style="width: 225px;" size="'.$localsize.'" onChange="this.form.remove_local.disabled = (this.selectedIndex == -1);">'.$localops.'</select>'
	), true, true);
	tablerow(array(
		'Remote recipients:<br /><span class="cp_small">(These are outside addresses that will receive<br />messages sent to the address above.)</span><br /><br /><input type="button" class="button" name="new" value="New Recipient" onClick="name = prompt(\'Enter the new recipient (complete email):\', \'\'); if (name == null || name == \'\') return false; this.form.distlist_remote_select.options[this.form.distlist_remote_select.options.length] = new Option(name, name);" style="width: 150px;" /> <input type="button" class="button" name="remove_remote" value="Remove Recipent" disabled="disabled" onClick="if (confirm(\'Are you sure you want to remove this recipient?\')) { this.form.distlist_remote_select.options[this.form.distlist_remote_select.selectedIndex] = null; this.disabled = true; }" style="width: 150px;" />',
		'<select name="distlist[recip_select]" id="distlist_remote_select" style="width: 225px;" size="'.$remotesize.'" onChange="this.form.remove_remote.disabled = (this.selectedIndex == -1);">'.$remoteops.'</select>'
	), true, true);

	if ($distlistid == 0) {
		endform('Create distribution list');
	} else {
		endform('Update distribution list');
	}
	endtable();
}

// ############################################################################
// List the lists
if ($cmd == 'modify') {
	adminlog();

	startform('user.php', 'edit');
	hiddenfield('auserid', 0);
	starttable('Existing Distribution Lists', '90%');
	$cells = array(
		'ID',
		'Email Address <span class="cp_small">(click to edit)</span>',
		'Recipients <span class="cp_small">(press Go to view user)</span>',
		'Options',
	);
	tablehead($cells);
	$lists = $DB_site->query('
		SELECT *
		FROM hive_distlist
	');
	if ($DB_site->num_rows($lists) < 1) {
		textrow('No lists found', count($cells), 1);
	} else {
		$recips = array();
		while ($list = $DB_site->fetch_array($lists)) {
			$thisrecips = unserialize($list['recipients']);
			foreach ($thisrecips as $key => $recip) {
				$thisrecips["$key"] = addslashes($recip);
			}
			$recips = array_merge($recips, $thisrecips);
		}
		$getusers = $DB_site->query('
			SELECT userid, username, domain
			FROM hive_user
			WHERE username IN ("'.implode('", "', $recips).'")
		');
		$users = array();
		while ($user = $DB_site->fetch_array($getusers)) {
			$users["$user[username]"] = array('userid' => $user['userid'], 'email' => $user['username'].$user['domain']);
		}
		$DB_site->reset($lists);
		while ($list = $DB_site->fetch_array($lists)) {
			$thisrecips = unserialize($list['recipients']);
			$recipoptions = '';
			foreach ($thisrecips as $recip) {
				if (isset($users["$recip"])) {
					$recipoptions .= "<option value=\"".$users["$recip"]['userid']."\">".$users["$recip"]['email']."</option>\n";
				} else {
					$recipoptions .= "<option value=\"0\">$recip".iif(!is_email($recip), getop('domainname'))."</option>\n";
				}
			}
			$cells = array(
				$list['distlistid'],
				'<a href="distlist.php?cmd=edit&distlistid='.$list['distlistid'].'">'.$list['toalias'].getop('domainname').'</a>',
				'<select name="recips'.$list['distlistid'].'" onChange="this.form.gobut.disabled = (this.options[this.selectedIndex].value == 0);">'.$recipoptions.'</select> <input type="submit" name="gobut" value="Go" class="button" onClick="var selUserID = this.form.recips'.$list['distlistid'].'.options[this.form.recips'.$list['distlistid'].'.selectedIndex].value; if (selUserID == 0) { alert(\'No user was found with this email address.\'); return false; } else { this.form.auserid.value = selUserID; return true; }" />',
				makelink('remove', "distlist.php?cmd=remove&distlistid=$list[distlistid]"),
			);
			tablerow($cells);
		}
	}
	emptyrow(count($cells));
	endform();
	endtable();

	button('', 'Create New List', "window.location='distlist.php?cmd=edit'", true);

	starttable('Help', '450');
	textrow('A <b>distribution list</b> allows you to set an address, that is not bound to any user in HiveMail&trade;, and make it forward any messages to specified users within or outside of Hivemail&trade;.');
	endtable();
}

cp_footer();
?>