<?php
// $Id: mailusers.php,v 1.1 2004/07/11 10:28:51 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS                                                   //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// 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($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_once(XOOPS_ROOT_PATH."/modules/system/admin/mailusers/mailform.php");
		$form->display();
		CloseTable();
		xoops_cp_footer();
		exit();
	}

	if ( $op == "send" && !empty($HTTP_POST_VARS['mail_send_to']) ) {
		$to_groups   = array();
		$to_users    = array();
		$send_users  = array();
		$criteria    = array();
		$xoopsMailer =& getMailer();

		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) {
					if ($groupid != 0) {
						$mailgroup = new XoopsGroup($groupid);
						$members   = $mailgroup->getMembers();
						foreach ($members as $member) {
							$to_groups[] = $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 = (86400 * $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 = (86400 * $f_mail_idle_less);
				$time = (time() - $time);
				if ( $time > 0 ) {
					$criteria[] = "last_login > $time";
				}
			}
			if ( !empty($HTTP_POST_VARS['mail_ok']) ) {
				$criteria[] = "user_mailok = 1";
			}
		}

		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);
			foreach ($getusers as $getuser) {
				$to_users[] = $getuser;
			}
		}
	}

		if ( !empty($HTTP_POST_VARS['mail_to_user']) ) {
			foreach ($HTTP_POST_VARS['mail_to_user'] as $getuser) {
				$to_users[] = $getuser;
			}
		}

		if ( !empty($HTTP_POST_VARS['mail_to_emails']) ) {
			$to_emails = explode(",", $HTTP_POST_VARS['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($HTTP_POST_VARS['mail_fromname']);
			$xoopsMailer->setFromEmail($HTTP_POST_VARS['mail_fromemail']);
			$xoopsMailer->setSubject($HTTP_POST_VARS['mail_subject']);
			$xoopsMailer->setBody($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();
			}
			if ( !empty($HTTP_POST_VARS['mail_reciept']) ) {
				$xoopsMailer->setReciept($HTTP_POST_VARS['mail_fromemail']);
			}

			$xoopsMailer->setType($HTTP_POST_VARS['mail_type']);
			$xoopsMailer->setPriority($HTTP_POST_VARS['mail_priority']);

			if ( !empty($HTTP_POST_FILES['attachment']['name']) ) {
				include_once(XOOPS_ROOT_PATH."/class/fileupload.php");
				$upload = new fileupload();
				$upload->set_upload_dir(XOOPS_ROOT_PATH . "/cache/", 'attachment');
				$upload->set_overwrite(1, 'attachment');
				$result = $upload->upload();
				if ($result['attachment']['filename']) {
					$attach_type = $HTTP_POST_VARS['inline'] ? 'inline' : 'attachment';
					$xoopsMailer->attachFile(XOOPS_ROOT_PATH."/cache/".$result['attachment']['filename'], $result['attachment']['type'], $attach_type);
					@unlink(XOOPS_ROOT_PATH."/cache/".$result['attachment']['filename']);
					} 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";
	}
?>
