<?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: rules.apply.php,v $ - $Revision: 1.22 $
// | $Date: 2004/08/03 04:28:44 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
$templatesused = 'rules_apply,index_jumpbit,redirect_ruleapplied';
require_once('./global.php');

// ############################################################################
// Set the default cmd
if (!isset($cmd) or $cmd == 'update') {
	$cmd = 'select';
}
$applyall = (int) $applyall;

// ############################################################################
// Get navigation bar
makemailnav(4);

// ############################################################################
if ($cmd == 'select') {
	if (!$applyall) {
		// Verify rule
		$rule = getinfo('rule', $ruleid);

		// Extract the data
		extract(parse_rule($rule));

		// What is the condition
		$condition = $phrase['when'].' ';
		if ($condtype == 1) {
			if ($condsubject == substr($_rules['conds']['emaileq'], 0, 1)) {
				$condition .= $phrase['email_address'].' ';
			} elseif ($condsubject == substr($_rules['conds']['msgeq'], 0, 1)) {
				$condition .= $phrase['message'].' ';
			} elseif ($condsubject == substr($_rules['conds']['recipseq'], 0, 1)) {
				$condition .= $phrase['recipients'].' ';
			} elseif ($condsubject == substr($_rules['conds']['subjecteq'], 0, 1)) {
				$condition .= $phrase['subject'].' ';
			}
			if ($condhow == substr($_rules['conds']['emaileq'], 1, 1)) {
				$condition .= $phrase['equals'].' ';
			} elseif ($condhow == substr($_rules['conds']['emailcon'], 1, 1)) {
				$condition .= $phrase['contains'].' ';
			} elseif ($condhow == substr($_rules['conds']['emailnotcon'], 1, 1)) {
				$condition .= $phrase['does_not_contain'].' ';
			} elseif ($condhow == substr($_rules['conds']['emailstars'], 1, 1)) {
				$condition .= $phrase['begins_with'].' ';
			} elseif ($condhow == substr($_rules['conds']['emailends'], 1, 1)) {
				$condition .= $phrase['ends_with'].' ';
			}
			$condition .= '"'.htmlchars($condextra).'"';
		} else {
			if ($condextra == 0) {
				$title = getop('appname');
			} else {
				$pop = getinfo('pop', $condextra);
				$title = $pop['accountname'];
			}
			$condition .= $phrase['email_is_from_account'].' '.htmlchars($title);
		}

		// What is the action
		$doarray = array();
		if ($action & $_rules['actions']['delete']) {
			$doarray[] = $phrase['delete_it'];
		}
		if ($action & $_rules['actions']['read']) {
			$doarray[] = $phrase['mark_it_as_read'];
		}
		if ($action & $_rules['actions']['flag']) {
			$doarray[] = $phrase['flag_it'];
		}
		if ($action & $_rules['actions']['move'] or $action & $_rules['actions']['copy']) {
			switch (intval($folderaction)) {
				case -1:
				case -2:
				case -3:
				case -4:
					$folder['title'] = $_folders["$folderaction"]['title'];
					break;
				default:
					$folder = getinfo('folder', $folderaction, false, false);
			}
			if ($action & $_rules['actions']['copy']) {
				$doarray[] = $phrase['copy_it_to_folder']." $folder[title]";
			} else {
				$doarray[] = $phrase['move_it_to_folder']." $folder[title]";
			}
		}
		if ($action & $_rules['actions']['color']) {
			$doarray[] = $phrase['highlight_it_in'].' '.ucwords($coloraction);
		}

		// Word the action
		$dowhat = '';
		$totalactions = count($doarray);
		for ($i = 0; $i < $totalactions; $i++) {
			if ($totalactions == 1 or $totalactions != $i + 1) {
				$dowhat .= ',';
			} else {
				$dowhat .= ' '.$phrase['and'];
			}
			$dowhat .= " $doarray[$i]";
		}
		$dowhat = substr($dowhat, 1);
	}

	// Create the folder jump
	$folderjump = $deffolders = $separator = '';
	$selectsize = 7;
	foreach ($_folders as $folderid => $folderinfo) {
		$deffolders .= '		<option value="'.$folderid.'">'.$folderinfo['title'].'</option>';
	}
	foreach ($foldertitles as $folderid => $foldertitle) {
		$folderjump .= '		<option value="'.$folderid.'">'.$foldertitle.'</option>';
		$selectsize++;
		$separator = '			<option value="-">---------------------</option>';
	}
	$folderjump = $deffolders.$separator.$folderjump;
	if (empty($separator)) {
		$selectsize--;
	}

	$youarehere = '<a href="'.INDEX_FILE.'">'.getop('appname').'</a> &raquo; <a href="rules.list.php">'.$phrase['message_rules'].'</a> &raquo; '.$phrase['apply_rule'];
	eval(makeeval('echo', 'rules_apply'));
}

// ############################################################################
if ($_POST['cmd'] == 'doit') {
	// Apply the rule
	if (!$applyall) {
		$rule = getinfo('rule', $ruleid);
		apply_rule($rule, $folderids);
	} else {
		$rules = $DB_site->query("
			SELECT *
			FROM hive_rule
			WHERE userid = $hiveuser[userid]
		");
		while ($rule = $DB_site->fetch_array($rules)) {
			apply_rule($rule, $folderids);
		}
	}

	// We probably have to do this here... and if we don't, who cares
	updatefolders();

	eval(makeredirect("redirect_ruleapplied", "rules.list.php"));
}

?>