1<?php
2
3/*
4	[UCenter] (C)2001-2099 Comsenz Inc.
5	This is NOT a freeware, use is subject to license terms
6
7	$Id: mail.php 1139 2012-05-08 09:02:11Z liulanbo $
8*/
9
10!defined('IN_UC') && exit('Access Denied');
11
12define('UC_MAIL_REPEAT', 5);
13
14class mailmodel {
15
16	var $db;
17	var $base;
18	var $apps;
19
20	function __construct(&$base) {
21		$this->mailmodel($base);
22	}
23
24	function mailmodel(&$base) {
25		$this->base = $base;
26		$this->db = $base->db;
27		$this->apps = &$this->base->cache['apps'];
28	}
29
30	function get_total_num() {
31		$data = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."mailqueue");
32		return $data;
33	}
34
35	function get_list($page, $ppp, $totalnum) {
36		$start = $this->base->page_get_start($page, $ppp, $totalnum);
37		$data = $this->db->fetch_all("SELECT m.*, u.username, u.email FROM ".UC_DBTABLEPRE."mailqueue m LEFT JOIN ".UC_DBTABLEPRE."members u ON m.touid=u.uid ORDER BY dateline DESC LIMIT $start, $ppp");
38		foreach((array)$data as $k => $v) {
39			$data[$k]['subject'] = dhtmlspecialchars($v['subject']);
40			$data[$k]['tomail'] = empty($v['tomail']) ? $v['email'] : $v['tomail'];
41			$data[$k]['dateline'] = $v['dateline'] ? $this->base->date($data[$k]['dateline']) : '';
42			$data[$k]['appname'] = $this->base->cache['apps'][$v['appid']]['name'];
43		}
44		return $data;
45	}
46
47	function delete_mail($ids) {
48		$ids = $this->base->implode($ids);
49		$this->db->query("DELETE FROM ".UC_DBTABLEPRE."mailqueue WHERE mailid IN ($ids)");
50		return $this->db->affected_rows();
51	}
52
53	function add($mail) {
54		if($mail['level']) {
55			$sql = "INSERT INTO ".UC_DBTABLEPRE."mailqueue (touid, tomail, subject, message, frommail, charset, htmlon, level, dateline, failures, appid) VALUES ";
56			$values_arr = array();
57			foreach($mail['uids'] as $uid) {
58				if(empty($uid)) continue;
59				$values_arr[] = "('$uid', '', '$mail[subject]', '$mail[message]', '$mail[frommail]', '$mail[charset]', '$mail[htmlon]', '$mail[level]', '$mail[dateline]', '0', '$mail[appid]')";
60			}
61			foreach($mail['emails'] as $email) {
62				if(empty($email)) continue;
63				$values_arr[] = "('', '$email', '$mail[subject]', '$mail[message]', '$mail[frommail]', '$mail[charset]', '$mail[htmlon]', '$mail[level]', '$mail[dateline]', '0', '$mail[appid]')";
64			}
65			$sql .= implode(',', $values_arr);
66			$this->db->query($sql);
67			$insert_id = $this->db->insert_id();
68			$insert_id && $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."vars SET name='mailexists', value='1'");
69			return $insert_id;
70		} else {
71			$mail['email_to'] = array();
72			$uids = 0;
73			foreach($mail['uids'] as $uid) {
74				if(empty($uid)) continue;
75				$uids .= ','.$uid;
76			}
77			$users = $this->db->fetch_all("SELECT uid, username, email FROM ".UC_DBTABLEPRE."members WHERE uid IN ($uids)");
78			foreach($users as $v) {
79				$mail['email_to'][] = $v['username'].'<'.$v['email'].'>';
80			}
81			foreach($mail['emails'] as $email) {
82				if(empty($email)) continue;
83				$mail['email_to'][] = $email;
84			}
85			$mail['message'] = str_replace('\"', '"', $mail['message']);
86			$mail['email_to'] = implode(',', $mail['email_to']);
87			return $this->send_one_mail($mail);
88		}
89	}
90
91	function send() {
92		register_shutdown_function(array($this, '_send'));
93	}
94
95	function _send() {
96
97		$mail = $this->_get_mail();
98		if(empty($mail)) {
99			$this->db->query("REPLACE INTO ".UC_DBTABLEPRE."vars SET name='mailexists', value='0'");
100			return NULL;
101		} else {
102			$mail['email_to'] = $mail['tomail'] ? $mail['tomail'] : $mail['username'].'<'.$mail['email'].'>';
103			if($this->send_one_mail($mail)) {
104				$this->_delete_one_mail($mail['mailid']);
105				return true;
106			} else {
107				$this->_update_failures($mail['mailid']);
108				return false;
109			}
110		}
111
112	}
113
114	function send_by_id($mailid) {
115		if ($this->send_one_mail($this->_get_mail_by_id($mailid))) {
116			$this->_delete_one_mail($mailid);
117			return true;
118		}
119	}
120
121	function send_one_mail($mail) {
122		if(empty($mail)) return;
123		$mail['email_to'] = $mail['email_to'] ? $mail['email_to'] : $mail['username'].'<'.$mail['email'].'>';
124		$mail_setting = $this->base->settings;
125		return include UC_ROOT.'lib/sendmail.inc.php';
126	}
127
128	function _get_mail() {
129		$data = $this->db->fetch_first("SELECT m.*, u.username, u.email FROM ".UC_DBTABLEPRE."mailqueue m LEFT JOIN ".UC_DBTABLEPRE."members u ON m.touid=u.uid WHERE failures<'".UC_MAIL_REPEAT."' ORDER BY level DESC, mailid ASC LIMIT 1");
130		return $data;
131	}
132
133	function _get_mail_by_id($mailid) {
134		$data = $this->db->fetch_first("SELECT m.*, u.username, u.email FROM ".UC_DBTABLEPRE."mailqueue m LEFT JOIN ".UC_DBTABLEPRE."members u ON m.touid=u.uid WHERE mailid='$mailid'");
135		return $data;
136	}
137
138	function _delete_one_mail($mailid) {
139		$mailid = intval($mailid);
140		return $this->db->query("DELETE FROM ".UC_DBTABLEPRE."mailqueue WHERE mailid='$mailid'");
141	}
142
143	function _update_failures($mailid) {
144		$mailid = intval($mailid);
145		return $this->db->query("UPDATE ".UC_DBTABLEPRE."mailqueue SET failures=failures+1 WHERE mailid='$mailid'");
146	}
147}
148
149?>