<?php
if (!eregi("admin.php", $PHP_SELF)) { 
	die ("Access Denied"); 
}
if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {
	include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
	$op = "form";

	if (!empty($HTTP_POST_VARS['op']) && $HTTP_POST_VARS['op'] == "send") {
		$op =  $HTTP_POST_VARS['op'];
	}

	if ( $op == "form" ) {
		xoops_cp_header();
		OpenTable();
		$display_criteria = 1;
		include(XOOPS_ROOT_PATH."/modules/system/admin/mailusers/mailform.php");
		$form->display();
		CloseTable();
		xoops_cp_footer();
	}

	if ( $op == "send" && !empty($HTTP_POST_VARS['mail_send_to']) ) {
		$added = array();
		$added_id = array();
		$criteria = array();
		if ( !empty($HTTP_POST_VARS['mail_inactive']) ) {
			$criteria[] = "level = 0";
		} else {
			if ( !empty($HTTP_POST_VARS['mail_to_group']) ) {
				foreach ($HTTP_POST_VARS['mail_to_group'] as $groupid ) {
					$mailgroup = new XoopsGroup($groupid);
					$members = $mailgroup->getMembers(true);
					foreach ( $members as $member ) {
						if ( !in_array($member_id, $added) ) {
							$added_id[] = $member->uid;
							$added[] = $member;
						}
					}
				}
			}
			if ( !empty($HTTP_POST_VARS['mail_lastlog_min']) ) {
				$f_mail_lastlog_min = trim($HTTP_POST_VARS['mail_lastlog_min']);
				$time = mktime(0,0,0,substr($f_mail_lastlog_min,5,2),substr($f_mail_lastlog_min,8,2),substr($f_mail_lastlog_min,0,4));
				if ( $time > 0 ) {
					$criteria[] = "last_login > $time";
				}
			}
			if ( !empty($HTTP_POST_VARS['mail_lastlog_max']) ) {
				$f_mail_lastlog_max = trim($HTTP_POST_VARS['mail_lastlog_max']);
				$time = mktime(0,0,0,substr($f_mail_lastlog_max,5,2),substr($f_mail_lastlog_max,8,2),substr($f_mail_lastlog_max,0,4));
				if ( $time > 0 ) {
					$criteria[] = "last_login < $time";
				}
			}
			if ( !empty($HTTP_POST_VARS['mail_idle_more']) && is_numeric($HTTP_POST_VARS['mail_idle_more']) ) {
				$f_mail_idle_more = intval(trim($HTTP_POST_VARS['mail_idle_more']));
				$time = 60 * 60 * 24 * $f_mail_idle_more;
				$time = time() - $time;
				if ( $time > 0 ) {
					$criteria[] = "last_login < $time";
				}
			}
			if ( !empty($HTTP_POST_VARS['mail_idle_less']) && is_numeric($HTTP_POST_VARS['mail_idle_less']) ) {
				$f_mail_idle_less = intval(trim($HTTP_POST_VARS['mail_idle_less']));
				$time = 60 * 60 * 24 * $f_mail_idle_less;
				$time = time() - $time;
				if ( $time > 0 ) {
					$criteria[] = "last_login > $time";
				}
			}
		}
		if ( !empty($HTTP_POST_VARS['mail_regd_min']) ) {
			$f_mail_regd_min = trim($HTTP_POST_VARS['mail_regd_min']);
			$time = mktime(0,0,0,substr($f_mail_regd_min,5,2),substr($f_mail_regd_min,8,2),substr($f_mail_regd_min,0,4));
			if ( $time > 0 ) {
				$criteria[] = "user_regdate > $time";
			}
		}
		if ( !empty($HTTP_POST_VARS['mail_regd_max']) ) {
			$f_mail_regd_max = trim($HTTP_POST_VARS['mail_regd_max']);
			$time = mktime(0,0,0,substr($f_mail_regd_max,5,2),substr($f_mail_regd_max,8,2),substr($f_mail_regd_max,0,4));
			if ( $time > 0 ) {
				$criteria[] = "user_regdate < $time";
			}
		}
		if ( !empty($criteria) ) {
			if ( empty($HTTP_POST_VARS['mail_inactive']) ) {
				$criteria[] = "level > 0";
			}
			$getusers = XoopsUser::getAllUsers($criteria, true);
			foreach ($getusers as $getuser) {
				if ( !in_array($getuser->getVar("uid"), $added_id) ) {
					$added[] = $getuser;
					$added_id[] = $getuser->getVar("uid");
				}
			}
		}
		if ( !empty($HTTP_POST_VARS['mail_to_user']) ) {
			foreach ($HTTP_POST_VARS['mail_to_user'] as $to_user) {
				if ( !in_array($to_user, $added_id) ) {
					$added[] = new XoopsUser($to_user);
					$added_id[] = $to_user;
				}
			}
		}
		$added_count = count($added);
		xoops_cp_header();
		OpenTable();
		if ( $added_count > 0 ) {
			$mail_end = ($added_count > ($HTTP_POST_VARS['mail_start'] + 100)) ? ($HTTP_POST_VARS['mail_start'] + 100) : $added_count;
			$myts = MyTextSanitizer::getInstance();
			$xoopsMailer =& getMailer();
			for ( $i = $mail_start; $i < $mail_end; $i++) {
				$xoopsMailer->setToUsers($added[$i]);
			}
			$xoopsMailer->setFromName($myts->oopsStripSlashesGPC($HTTP_POST_VARS['mail_fromname']));
			$xoopsMailer->setFromEmail($myts->oopsStripSlashesGPC($HTTP_POST_VARS['mail_fromemail']));
			$xoopsMailer->setSubject($myts->oopsStripSlashesGPC($HTTP_POST_VARS['mail_subject']));
			$xoopsMailer->setBody($myts->oopsStripSlashesGPC($HTTP_POST_VARS['mail_body']));
			if ( in_array("mail", $HTTP_POST_VARS['mail_send_to']) ) {
				$xoopsMailer->useMail();
			}
			if ( in_array("pm", $HTTP_POST_VARS['mail_send_to']) && empty($HTTP_POST_VARS['mail_inactive']) ) {
				$xoopsMailer->usePM();
			}
			$xoopsMailer->send();
			echo $xoopsMailer->getSuccess();
			echo $xoopsMailer->getErrors();
			
		
			if ( $added_count > $mail_end ) {
				$form = new XoopsThemeForm(_AM_SENDMTOUSERS, "mailusers", "admin.php?fct=mailusers");
				if ( !empty($HTTP_POST_VARS['mail_to_group']) ) {
					foreach ( $HTTP_POST_VARS['mail_to_group'] as $mailgroup) {
						$group_hidden = new XoopsFormHidden("mail_to_group[]", $mailgroup);
						$form->addElement($group_hidden);
					}
				}
				$inactive_hidden = new XoopsFormHidden("mail_inactive", $mail_inactive);
				$lastlog_min_hidden = new XoopsFormHidden("mail_lastlog_min", $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['mail_lastlog_min']));
				$lastlog_max_hidden = new XoopsFormHidden("mail_lastlog_max", $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['mail_lastlog_max']));
				$regd_min_hidden = new XoopsFormHidden("mail_regd_min", $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['mail_regd_max']));
				$regd_max_hidden = new XoopsFormHidden("mail_regd_max", $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['mail_regd_max']));
				$idle_more_hidden = new XoopsFormHidden("mail_idle_more", $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['mail_idle_more']));
				$idle_less_hidden = new XoopsFormHidden("mail_idle_less", $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['mail_idle_less']));
				$fname_hidden = new XoopsFormHidden("mail_fromname", $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['mail_fromname']));
				$femail_hidden = new XoopsFormHidden("mail_fromemail", $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['mail_fromemail']));
				$subject_hidden = new XoopsFormHidden("mail_subject", $myts->makeTboxData4PreviewInForm($HTTP_POST_VARS['mail_subject']));
				$body_hidden = new XoopsFormHidden("mail_body", $myts->makeTareaData4PreviewInForm($HTTP_POST_VARS['mail_body']));
				$start_hidden = new XoopsFormHidden("mail_start", $mail_end);
				$to_hidden = new XoopsFormHidden("mail_send_to[]", "mail");
				$op_hidden = new XoopsFormHidden("op", "send");
				$submit_button = new XoopsFormButton("", "mail_submit", _AM_SENDNEXT, "submit");
				$sent_label = new XoopsFormLabel(_AM_SENT, sprintf(_AM_SENTNUM, $HTTP_POST_VARS['mail_start']+1, $mail_end, $added_count));
				$form->addElement($sent_label);
				$form->addElement($inactive_hidden);
				$form->addElement($lastlog_min_hidden);
				$form->addElement($lastlog_max_hidden);
				$form->addElement($regd_min_hidden);
				$form->addElement($regd_max_hidden);
				$form->addElement($idle_more_hidden);
				$form->addElement($idle_less_hidden);
				$form->addElement($fname_hidden);
				$form->addElement($femail_hidden);
				$form->addElement($subject_hidden);
				$form->addElement($body_hidden);
				$form->addElement($to_hidden);
				$form->addElement($op_hidden);
				$form->addElement($start_hidden);
				$form->addElement($submit_button);
				$form->display();
			} else {
				echo "<h4>"._AM_SENDCOMP."</h4>";
			}
		} else {
			echo "<h4>"._AM_NOUSERMATCH."</h4>";
		}
		CloseTable();
		xoops_cp_footer();
	}

} else {
    echo "Access Denied";
}
?>
