<?php
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.6 www.exoops.info 2006/03 
// $Id: mailusers.php,v 1.5 2003/03/19 07:03:32 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: Kazumi Ono
// Author Website : http://www.mywebaddons.com/ , http://www.myweb.ne.jp
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //
if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {
	include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
	$op = "form";
	if (!empty($_POST['op']) && $_POST['op'] == "send") {
		$op =  $_POST['op'];
	}
	if ( $op == "form" ) {
		xoops_cp_header();
		OpenTable();
		$display_criteria = 1;
		include_once(XOOPS_ROOT_PATH."/modules/system/admin/mailusers/mailform.php");
		$form->display();
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	if ( $op == "send" && !empty($_POST['mail_send_to']) ) {
		$to_groups   = array();
		$to_users    = array();
		$send_users  = array();
		$criteria    = array();
		$xoopsMailer =& getMailer();
		if ( !empty($_POST['mail_inactive']) ) {
			$criteria[] = "level = 0";
		} else {
			if ( !empty($_POST['mail_to_group']) ) {
				foreach ($_POST['mail_to_group'] as $groupid) {
					if ($groupid != 0) {
						$mailgroup = new XoopsGroup($groupid);
						$members   = $mailgroup->getMembers();
						foreach ($members as $member) {
							$to_groups[] = $member;
						}
					}
				}
			}
			if ( !empty($_POST['mail_lastlog_min']) ) {
				$f_mail_lastlog_min = trim($_POST['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($_POST['mail_lastlog_max']) ) {
				$f_mail_lastlog_max = trim($_POST['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($_POST['mail_idle_more']) && is_numeric($_POST['mail_idle_more']) ) {
				$f_mail_idle_more = intval(trim($_POST['mail_idle_more']));
				$time = (86400 * $f_mail_idle_more);
				$time = (time() - $time);
				if ( $time > 0 ) {
					$criteria[] = "last_login < $time";
				}
			}
			if ( !empty($_POST['mail_idle_less']) && is_numeric($_POST['mail_idle_less']) ) {
				$f_mail_idle_less = intval(trim($_POST['mail_idle_less']));
				$time = (86400 * $f_mail_idle_less);
				$time = (time() - $time);
				if ( $time > 0 ) {
					$criteria[] = "last_login > $time";
				}
			}
			if ( !empty($_POST['mail_ok']) ) {
				$criteria[] = "user_mailok = 1";
			}
		}
		if ( !empty($_POST['mail_regd_min']) ) {
			$f_mail_regd_min = trim($_POST['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($_POST['mail_regd_max']) ) {
			$f_mail_regd_max = trim($_POST['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($_POST['mail_inactive']) ) {
				$criteria[] = "level > 0";
			}
			$getusers =& XoopsUser::getAllUsers($criteria);
			foreach ($getusers as $getuser) {
				$to_users[] = $getuser;
			}
		}
	}
		if ( !empty($_POST['mail_to_user']) ) {
			foreach ($_POST['mail_to_user'] as $getuser) {
				$to_users[] = $getuser;
			}
		}
		if ( !empty($_POST['mail_to_emails']) ) {
			$to_emails = explode(",", $_POST['mail_to_emails']);
			foreach($to_emails as $email) {
				$xoopsMailer->setToEmails(trim($email));
			}
		}
		if ( !empty($to_users) && !empty($to_groups) ) {
			$send_users = array_intersect($to_users, $to_groups);
			} elseif (!empty($to_users)) {
				$send_users = $to_users;
				} elseif (!empty($to_groups)) {
					$send_users = $to_groups;
				}
		$send_users = array_unique($send_users);
		$xoopsMailer->setToUsers($send_users);
		xoops_cp_header();
		OpenTable();
		if ( count(($send_users + $xoopsMailer->toEmails)) > 0 ) {
			$xoopsMailer->setFromName($_POST['mail_fromname']);
			$xoopsMailer->setFromEmail($_POST['mail_fromemail']);
			$xoopsMailer->setSubject($_POST['mail_subject']);
			$xoopsMailer->setBody($_POST['mail_body']);
			if ( in_array("mail", $_POST['mail_send_to']) ) {
				$xoopsMailer->useMail();
			}
			if ( in_array("pm", $_POST['mail_send_to']) && empty($_POST['mail_inactive']) ) {
				$xoopsMailer->usePM();
			}
			if ( !empty($_POST['mail_reciept']) ) {
				$xoopsMailer->setReciept($_POST['mail_fromemail']);
			}
			$xoopsMailer->setType($_POST['mail_type']);
			$xoopsMailer->setPriority($_POST['mail_priority']);
			if ( !empty($_FILES['attachment']['name']) ) {
				include_once(XOOPS_ROOT_PATH."/class/upload.class.php");
				$upload = new Upload();
				$upload->setUploadPath(XOOPS_ROOT_PATH ."/cache");
				$upload->setDestinationFileName('attachment');
				$result = $upload->doUpload();
				if ($result) {
					$attach_type = $_POST['inline'] ? 'inline' : 'attachment';
					$xoopsMailer->attachFile(XOOPS_ROOT_PATH."/cache/".$upload->HTTP_POST_FILES[$upload->uploadFieldName]['name'], $upload->HTTP_POST_FILES[$upload->uploadFieldName]['type'], $attach_type);
					@unlink(XOOPS_ROOT_PATH."/cache/".$upload->HTTP_POST_FILES[$upload->uploadFieldName]['name']);
					} else {
						$upload->errors(1);
					}
			}
			$xoopsMailer->useToggle(1);
			$xoopsMailer->send();
			echo $xoopsMailer->getSuccess();
			echo $xoopsMailer->getErrors();
			} else {
				echo "<h4>"._AM_NOUSERMATCH."</h4>";
			}
	CloseTable();
	xoops_cp_footer();
	exit();
	} else {
		echo "Access Denied";
	}
?>
