<?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: usergroup.php,v $ - $Revision: 1.45 $
// | $Date: 2004/08/04 06:04:39 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
require_once('./global.php');
cp_header(' &raquo; User Groups');
cp_nav('usergroup');

// ############################################################################
// Set the default cmd
default_var($cmd, 'modify');
if ($cmd == 'add') {
	$cmd = 'edit';
}

// ############################################################################
// Remove user group
if ($_POST['cmd'] == 'kill') {
	$usergroup = getinfo('usergroup', $usergroupid);
	if ($usergroupid <= 3) {
		adminlog($usergroupid, false);
		cp_error('You cannot remove original user groups.');
	} elseif (($usergroup['perms'] & GROUP_CANADMIN) and !$DB_site->query_first("SELECT usergroupid FROM hive_usergroup WHERE usergroupid <> $usergroupid AND (perms & ".GROUP_CANADMIN.")")) {
		adminlog($usergroupid, false);
		cp_error('You cannot remove administrative privileges from the only group that has them.');
	}

	$DB_site->query("
		DELETE FROM hive_usergroup
		WHERE usergroupid = $usergroupid
	");
	$DB_site->query("
		UPDATE hive_user
		SET usergroupid = $moveto
		WHERE usergroupid = $usergroupid
	");

	adminlog($usergroupid, true, 'kill', "Users will now be in user group: $moveto");
	cp_redirect('The user group has been removed.', 'usergroup.php');
}

// ############################################################################
// Remove user group
if ($cmd == 'remove') {
	$usergroup = getinfo('usergroup', $usergroupid);
	if ($usergroupid <= 3) {
		adminlog($usergroupid, false);
		cp_error('You cannot remove original user groups.');
	} elseif (($usergroup['perms'] & GROUP_CANADMIN) and !$DB_site->query_first("SELECT usergroupid FROM hive_usergroup WHERE usergroupid <> $usergroupid AND (perms & ".GROUP_CANADMIN.")")) {
		adminlog($usergroupid, false);
		cp_error('You cannot remove administrative privileges from the only group that has them.');
	}

	adminlog($usergroupid);
	startform('usergroup.php', 'kill', 'Are you sure you want to remove this user group?');
	starttable('Remove user group "'.$usergroup['title'].'" (ID: '.$usergroupid.')');
	textrow('Are you <b>sure</b> you want to remove this group? This procedure <b>cannot</b> be reversed!');
	tableselect('Move users from this group to:', 'moveto', 'usergroup', -1, "usergroupid <> $usergroupid");
	hiddenfield('usergroupid', $usergroupid);
	endform('Remove User Group', '', 'Go Back');
	endtable();
}

// ############################################################################
// Does what it says on the tin
if ($_POST['cmd'] == 'doduplicate') {
	$usergroup = getinfo('usergroup', $usergroupid);
	if ($usergroup['title'] == $_POST['title']) {
		adminlog($usergroupid, false);
		cp_error('You cannot have two user groups with the same name. Please press back and choose a different name for the new group.');
	}

	foreach ($usergroup AS $name => $val) {
		if ($name != 'usergroupid' && $name != 'title') {
			$toins["$name"] = $val;
		}
	}
	$toins['title'] = $_POST['title'];
	$DB_site->auto_query('usergroup', $toins);
	$goto = $DB_site->insert_id();
	adminlog($goto, false);
	cp_redirect('The user group was successfully duplicated.', 'usergroup.php?cmd=edit&usergroupid='.$goto);
}

// ############################################################################
// Ask for a name to create a new group with, based on the selected group
if ($_REQUEST['cmd'] == 'duplicate') {
	$usergroup = getinfo('usergroup', $usergroupid);
	adminlog($usergroupid);
	startform('usergroup.php', 'doduplicate');
	starttable('Duplicate User Group "'.$usergroup['title'].'" (ID: '.$usergroupid.')');
	textrow('Enter a name for the new user group below.');
	inputfield('Title:', 'title', $usergroup['title']);
	hiddenfield('usergroupid', $usergroupid);
	endform('Complete Duplication', '', 'Go Back');
	endtable();
}

// ############################################################################
// Create a new usergroup or update an existing one
if ($_POST['cmd'] == 'update') {
	if (empty($usergroup['title'])) {
		adminlog($usergroupid, false);
		cp_error('The user group must have a title.');
	} elseif (!is_array($usergroup['allowedskins'])) {
		adminlog($usergroupid, false);
		cp_error('You must select at least one skin for users to choose from.');
	} elseif (!$usergroup['GROUP_CANADMIN'] and !$DB_site->query_first("SELECT usergroupid FROM hive_usergroup WHERE usergroupid <> $usergroupid AND (perms & ".GROUP_CANADMIN.")")) {
		adminlog($usergroupid, false);
		cp_error('You cannot take away administrative privileges from the only group that has them.');
	} elseif ($usergroup['removetime'] > 0 and $usergroup['emptytime'] > 0 and $usergroup['removetime'] < $usergroup['emptytime']) {
		adminlog($usergroupid, false);
		cp_error('You cannot set accounts to be deleted before the messages are deleted. Please either disable the setting to delete messages, or set it to a fewer number of days than the \'Days to wait before removing account\' setting.');
	} else {
		// Permission links
		$permlinks = $usergroup['permlink'];
		if (is_array($usergroup['permlink'])) {
			$usergroup['permlink'] = $usergroupid.','.implode(',', $usergroup['permlink']);
		}
		if ($_POST['oldpermlink'] != $usergroup['permlink']) {
			if (!empty($_POST['oldpermlink'])) {
				$oldpermlink = explode(',', $_POST['oldpermlink']);
			} else {
				$oldpermlink = array();
			}
			$newpermlink = explode(',', $usergroup['permlink']);
			$diff = array_diff($oldpermlink, $newpermlink);
			if (is_array($diff) and !empty($diff)) {
				$changeup = $DB_site->query("
					SELECT usergroupid, permlink
					FROM hive_usergroup
					WHERE usergroupid IN (".implode(',', $diff).")
				");
				while ($ug = $DB_site->fetch_array($changeup)) {
					$DB_site->query("
						UPDATE hive_usergroup
						SET permlink = ''
						WHERE usergroupid = ".$ug['usergroupid']."
					");
				}
			}
		}

		// Skins
		$usergroup['allowedskins'] = implode(',', $usergroup['allowedskins']);

		// Limits
		$limitFields = array('maxmb', 'maxpopstorage', 'maxsigs', 'maxresponses', 'maxrecips', 'maxcontacts', 'maxattach', 'maxaliases', 'changepass');
		foreach ($limitFields as $limitField) {
			if (${"usergroup_$limitField"."_nolimit"}) {
				$usergroup["$limitField"] = 0;
			}
		}
		if (!$maxmsgs_every_nolimit) {
			$usergroup['msgpersec'] = serialize($maxmsgs);
		} else {
			$usergroup['msgpersec'] = serialize(array('every' => 0, 'unit' => 60));
		}

		// Permissions
		$usergroup['perms'] = 0;
		foreach ($usergroup as $conname => $value) {
			if (substr($conname, 0, strlen('GROUP_')) != 'GROUP_') {
				continue;
			}
			if ($value) {
				$usergroup['perms'] += constant($conname);
			}
			unset($usergroup["$conname"]);
		}

		foreach ($usergroup AS $name => $val) {
			if ($name != 'title') {
				$secondary["$name"] = $val;
			}
		}

		
		if ($usergroupid == 0) {
			$DB_site->auto_query('usergroup', $usergroup);
			cp_redirect('The user group has been created.', 'usergroup.php');
		} else {
			if (is_array($permlinks)) {
				$permlinks = implode(',', $permlinks);
				if (!($usergroup['perms'] & GROUP_CANADMIN)) {
					if (!$DB_site->query_first("SELECT * FROM hive_usergroup WHERE usergroupid NOT IN (".$usergroup['permlink'].") AND (perms & ".GROUP_CANADMIN.")")) {
						adminlog($usergroupid, false);
						cp_error('You must have at least one user group with administrator permissions. (You attempted to link a non-administrator group with one or more administrator groups, removing their administrator permissions.)');
					}
				}
				$DB_site->auto_query('usergroup', $usergroup, "usergroupid = $usergroupid");
				$DB_site->auto_query('usergroup', $secondary, "usergroupid IN ($permlinks)");
				adminlog($usergroupid.','.$permlinks, true);
				cp_redirect('The user group and all linked group(s) have been updated.', 'usergroup.php');
			} else {
				adminlog($usergroupid, true);
				$DB_site->auto_query('usergroup', $usergroup, "usergroupid = $usergroupid");
				cp_redirect('The user group has been updated.', 'usergroup.php');
			}
		}
	}
}

// ############################################################################
// Create a new usergroup or update an existing one
if ($cmd == 'edit') {
	startform('usergroup.php', 'update', '', array('usergroup_title' => 'title'));

	$usergroup = getinfo('usergroup', $usergroupid, false, false);
	adminlog($usergroupid);
	if ($usergroup === false) {
		$usergroup = array(
			'title' => '',
			'maxmb' => '5',
			'maxpopstorage' => '5',
			'maxattach' => '1',
			'maxsigs' => '0',
			'maxrecips' => '0',
			'maxresponses' => '0',
			'maxcontacts' => '0',
			'maxaliases' => '1',
			'msgpersec' => array('every' => 0, 'unit' => 1),
			'perms' => GROUP_DEFAULTBITS,
			'changepass' => '0',
		);
		$usergroupid = 0;
		hiddenfield('usergroupid', 0);
		starttable('Create new user group');
	} else {
		$usergroup['allowedskins'] = explode(',', $usergroup['allowedskins']);
		$usergroup['msgpersec'] = unserialize($usergroup['msgpersec']);
		hiddenfield('usergroupid', $usergroupid);
		hiddenfield('oldpermlink', $usergroup['permlink']);
		starttable('Update User Group "'.$usergroup['title'].'" (ID: '.$usergroupid.')');
	}

	// Skin selection
	$checkboxes = '';
	$theclass = getclass();
	$skins = $DB_site->query('SELECT * FROM hive_skin');
	while ($skin = $DB_site->fetch_array($skins)) {
		$checkboxes .= "<input type=\"checkbox\" name=\"usergroup[allowedskins][]\" class=\"radio_$theclass\" id=\"skin_$skin[skinid]\" value=\"$skin[skinid]\"".iif(!is_array($usergroup['allowedskins']) or in_array($skin['skinid'], $usergroup['allowedskins']), ' checked="checked"')." /> <label for=\"skin_$skin[skinid]\">$skin[title]</label><br />\n";
	}

	inputfield('Title:', 'usergroup[title]', $usergroup['title']);
	echo "	<tr class=\"$theclass\">\n";
	echo "		<td valign=\"top\">Available skins:<br /><span class=\"cp_small\">Users from this user group will only be able to<br />choose from the skins that are selected to the right.</span></td>\n";
	echo "		<td valign=\"top\">$checkboxes</td>\n";
	echo "	</tr>\n";
	getclass();

	if ($usergroupid > 0) {
		$plgroups = explode(',', $usergroup['permlink']);
		$checkboxes = '';
		$theclass = getclass();
		foreach($_usergroups as $usergid => $usergtitle) {
			if ($usergid != $usergroupid) {
				$checkboxes .= "<input type=\"checkbox\" name=\"usergroup[permlink][]\" class=\"radio_$theclass\" id=\"group_$usergid\" value=\"$usergid\"";
				if (is_array($plgroups) and in_array($usergid, $plgroups)) {
					$checkboxes .= ' checked="checked"';
				}
				$checkboxes .= " /> <label for=\"group_$usergid\">$usergtitle</label><br />\n";
			}
		}

		echo "	<tr class=\"$theclass\">\n";
		echo "		<td valign=\"top\">Permission links: <a href=\"#\" onClick=\"alert('The user groups you select will be linked to this group, so that whenever you make changes to one group, those changes will be applied to all groups linked to that group.\\n\\nYou can have multiple sets of links; that is, you could link Group A, Group B, and Group C together, and then link Group D, Group E, and Group F together. In this example, changes to A, B, or C would only affect A, B, and C, and changes to D, E, or F would only affect D, E, and F.\\n\\nAll permissions and settings are affected, except for group title.'); return false;\"><img src=\"../images/help.gif\" border=\"0\" /></a><br /><span class=\"cp_small\">The user groups you select will be \"linked\" to this group, so that whenever you make changes to one group, those changes will be applied to all groups linked to that group.</span></td>\n";
		echo "		<td valign=\"top\">$checkboxes</td>\n";
		echo "	</tr>\n";
		getclass();
	}

	/* --- */
	tablehead(array('<input class="button" type="submit" value="  '.iif($usergroupid == 0, 'Create User Group', 'Update User Group').'  " />'.iif($usergroupid > 0, '&nbsp;<input class="button" type="reset" value="  Reset Fields  " />', '')), 2);
	// &nbsp;<input class="button" type="submit" value="  Duplicate usergroup  " onClick="this.form.cmd.value = \'duplicate\';" />
	endtable();
	starttable('General Permissions');
	foreach ($_groupbits as $conname => $text) {
		radiofield($text, 'usergroup['.$conname.']', ($usergroup['perms'] & constant($conname)));
	}

	/* --- */
	tablehead(array('<input class="button" type="submit" value="  '.iif($usergroupid == 0, 'Create User Group', 'Update User Group').'  " />'.iif($usergroupid > 0, '&nbsp;<input class="button" type="reset" value="  Reset Fields  " />', '')), 2);
	endtable();
	starttable('User Limitations');
	tablerow(array('Users can send emails once every:<br /><span class="cp_small">Set either fields to 0 to impose no limit at all.</span>',
		'<input type="radio" name="'.reform_field_name('maxmsgs[every]').'_nolimit" id="'.reform_field_name('maxmsgs[every]').'_nolimit_false" value="0"'.iif($usergroup['msgpersec']['every'] != 0, ' checked="checked"').' />
		<input type="text" class="bginput" name="maxmsgs[every]" onClick="this.form.'.reform_field_name('maxmsgs[every]').'_nolimit_false.checked = true;" value="'.$usergroup['msgpersec']['every'].'" size="3" />
		<select name="maxmsgs[unit]" onChange="this.form.'.reform_field_name('maxmsgs[every]').'_nolimit_false.checked = true;">
			<option value="1"'.iif($usergroup['msgpersec']['unit'] == 1, ' selected="selected"').'>seconds</option>
			<option value="60"'.iif($usergroup['msgpersec']['unit'] == 60, ' selected="selected"').'>minutes</option>
			<option value="3600"'.iif($usergroup['msgpersec']['unit'] == 3600, ' selected="selected"').'>hours</option>
			<option value="86400"'.iif($usergroup['msgpersec']['unit'] == 86400, ' selected="selected"').'>days</option>
		</select><br />
		<input type="radio" name="'.reform_field_name('maxmsgs[every]').'_nolimit" id="'.reform_field_name('maxmsgs[every]').'_nolimit_true" value="1"'.iif($usergroup['msgpersec']['every'] == 0, ' checked="checked"').' />
		<label for="'.reform_field_name('maxmsgs[every]').'_nolimit_true">Unlimited</label>'), true, true);
	limitfield('Maximum storage (in megabytes):', 'usergroup[maxmb]', $usergroup['maxmb'], '31', 0, false);
	limitfield('Maximum signatures:<br /><span class="cp_small">The maximum number of signatures every user can have.<br />Set this to 1 to only allow one signature per user.</span>', 'usergroup[maxsigs]', $usergroup['maxsigs']);
	limitfield('Maximum auto-responders:<br /><span class="cp_small">The maximum number of auto-responders every user can have.<br />Set this to 1 to only allow one automatic response per user.</span>', 'usergroup[maxresponses]', $usergroup['maxresponses']);
	limitfield('Maximum recipients per message:<br /><span class="cp_small">The maximum number of recipients for every message users send. Useful for preventing mass-distribution of spam emails.</span>', 'usergroup[maxrecips]', $usergroup['maxrecips']);
	limitfield('Maximum address book contacts:<br /><span class="cp_small">The maximum number of contacts in the address book every user can have.</span>', 'usergroup[maxcontacts]', $usergroup['maxcontacts']);
	limitfield('Maximum outgoing attachments size (in megabytes):<br /><span class="cp_small">This is the maximum size of attachments in outgoing messages for users.</span>', 'usergroup[maxattach]', $usergroup['maxattach'], '31', 0, false);
	limitfield('Maximum number of aliases:<br /><span class="cp_small">This is the maximum number of aliases users can have.</span>', 'usergroup[maxaliases]', $usergroup['maxaliases']);
	limitfield('Remind users to change password after:<br /><span class="cp_small">Users will be reminded to change their password after this amount of days have passed since they last changed it.</span>', 'usergroup[changepass]', $usergroup['changepass'], 26, 0, true, "Don't remind at all", ' days');
	if (HIVEPOP_RUNNING) {
		limitfield('Maximum HivePOP3 storage (if enabled):', 'usergroup[maxpopstorage]', $usergroup['maxpopstorage'], '31', 0, false);
	}

	/* --- */
	tablehead(array('<input class="button" type="submit" value="  '.iif($usergroupid == 0, 'Create User Group', 'Update User Group').'  " />'.iif($usergroupid > 0, '&nbsp;<input class="button" type="reset" value="  Reset Fields  " />', '')), 2);
	endtable();
	starttable('Group Signature');
	textarea('Email signature (text version):<br /><span class="cp_small">This signature will be attached to all outgoing messages of users in this group. This version will be used for normal text messages.</span>', 'usergroup[groupsig_text]', $usergroup['groupsig_text'], 6, 40);
	textarea('Email signature (HTML version):<br /><span class="cp_small">Same as above, but this version will be added to HTML messages.<br />Please remember to use &lt;br /&gt; for new lines! This signature will be used as-is and new lines will not be added automatically.</span>', 'usergroup[groupsig_html]', $usergroup['groupsig_html'], 6, 40);

	/* --- */
	tablehead(array('<input class="button" type="submit" value="  '.iif($usergroupid == 0, 'Create User Group', 'Update User Group').'  " />'.iif($usergroupid > 0, '&nbsp;<input class="button" type="reset" value="  Reset Fields  " />', '')), 2);
	endtable();
	starttable('Account Expiration');
	inputfield('Days to wait before deleting messages:<br /><span class="cp_small">If the user has not logged in for this period of time, the messages in the account will automatically be deleted. Set this to 0 to disable this feature.</span>', 'usergroup[emptytime]', $usergroup['emptytime']);
	radiofield('Notify users before messages are deleted?<br /><span class="cp_small">This will allow the user to log-in in time before the messages are deleted.<br />Notifications will only be sent to users who have entered a secondary email address.<br />The contents of this notification is taken from the "Warning" <a href="template.php">templates</a>.</span>', 'usergroup[GROUP_NOTIFY_EMPTY]', $usergroup['perms'] & GROUP_NOTIFY_EMPTY);
	inputfield('Days to wait before removing account:<br /><span class="cp_small">If the user has not logged in for this period of time, the account will automatically be removed from the system. Set this to 0 to disable this feature.</span>', 'usergroup[removetime]', $usergroup['removetime']);
	radiofield('Notify users before account is removed?<br /><span class="cp_small">This will allow the user to log-in in time before the account is removed.<br />Notifications will only be sent to users who have entered a secondary email address.<br />The contents of this notification is taken from the "Warning" <a href="template.php">templates</a>.</span>', 'usergroup[GROUP_NOTIFY_REMOVE]', $usergroup['perms'] & GROUP_NOTIFY_REMOVE);
	inputfield('Notification times:<br /><span class="cp_small">Here you can specify exactly when users will be notified by email if any of the options above are enabled. For example, to send an email to the users 3 days before their account or messages are deleted, enter 3 in this box. Multiple notifications can also be sent, simply separate intervals with a space or comma. For example, if you enter 2,5,10 in this box, an email will be sent to users 10, 5, and finally 2 days before any action is taken. The numbers can appear in any order. <b>Note</b>: This must be set correctly or no notifications will be sent at all.</span>', 'usergroup[notifytime]', $usergroup['notifytime']);

	if ($usergroupid == 0) {
		endform('Create User Group');
	} else {
		endform('Update User Group', 'Reset Fields');
	}
	endtable();
}

// ############################################################################
// List the user groups
if ($cmd == 'modify') {
	adminlog();
	starttable('Existing User Groups', '450');
	tablehead(array('ID', 'Title', 'Users', 'Options'));
	$groups = $DB_site->query('
		SELECT usergroup.*, COUNT(user.userid) AS users
		FROM hive_usergroup AS usergroup
		LEFT JOIN hive_user AS user USING (usergroupid)
		GROUP BY usergroup.usergroupid
	');
	if ($DB_site->num_rows($groups) < 1) {
		textrow('No groups found', 4, 1);
	} else {
		while ($group = $DB_site->fetch_array($groups)) {
			tablerow(array(
				$group['usergroupid'],
				"<a href=\"usergroup.php?cmd=edit&usergroupid=$group[usergroupid]\">$group[title]</a>",
				iif($group['users'] > 0, "<a href=\"user.php?cmd=results&find[eusergroupid]=$group[usergroupid]\">$group[users] user".iif($group['users'] != 1, 's').'</a>', "$group[users] users"),
				makelink('edit', "usergroup.php?cmd=edit&usergroupid=$group[usergroupid]").iif($group['usergroupid'] > 3, '-'.makelink('remove', "usergroup.php?cmd=remove&usergroupid=$group[usergroupid]")).makelink('duplicate', "usergroup.php?cmd=duplicate&usergroupid=$group[usergroupid]")
			), false, false, false);
		}
	}
	emptyrow(4);
	endtable();

	button('', 'Create New User Group', "window.location='usergroup.php?cmd=edit'", true);
	
	starttable('Help', '450');
	textrow('A <b>user group</b> is a group of users that has special options and attributes. You can set different options and permissions for each group, either independently, or you link two or more groups together so they have identical permissions ("Permission Links"). The use of this may not be immediately apparent, so here\'s an example. You can, for example, have two groups of users who have the same permissions, but are each offered different subscription plans. Another example: you have two groups of users which are each comprised of a different department in your organization. Then, using the Global Address Book you can pre-fill the address book of each group with only users from their department, and set the permissions on each contact so that only the proper group can see that contact in their Global Address Book. Coupled with the user group permission "Can only share calendar events with users in the Global Address Book", you can limit them to only sharing events with other users from their department.<br /><br />
	Every user can belong to one user group. The group users are originally put in depends on whether or not you have turned on the validation queue for users. If you have, new users will be placed in the "Awaiting Validation" group, which should have very restricted permissions or the validation process will be pointless. If the option is turned off, new registrants will be placed directly in the "Regular Users" group which should have normal permissions. The "Administrators" user group is meant for administrators only, and you should not lower its permissions, or you may find yourself unable to administrate your HiveMail&trade; installation!<br /><br />
	(The original user groups (IDs: 1 - 3) cannot be removed.)');
	endtable();
}

cp_footer();
?>