Commit | Line | Data |
---|---|---|
8da0d3c1 | 1 | <?php |
2 | /*************************************************************************** | |
8d84c630 | 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
8da0d3c1 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | require_once("massmailer.inc.php"); | |
23 | ||
24 | class AXLetter extends MassMailer | |
25 | { | |
26 | public $_body; | |
27 | public $_signature; | |
28 | public $_promo_min; | |
29 | public $_promo_max; | |
2a3f4f57 | 30 | public $_subset; |
8da0d3c1 | 31 | public $_echeance; |
32 | public $_date; | |
33 | public $_bits; | |
34 | ||
35 | function __construct($id) | |
36 | { | |
b71f7275 | 37 | parent::__construct('axletter/letter.mail.tpl', 'ax.css', 'ax/show', 'axletter', 'axletter_ins'); |
8da0d3c1 | 38 | $this->_head = '<cher> <prenom>,'; |
39 | ||
40 | if (!is_array($id)) { | |
41 | if ($id == 'last') { | |
42 | $res = XDB::query("SELECT * | |
43 | FROM axletter | |
44 | WHERE FIND_IN_SET('sent', bits) | |
45 | ORDER BY id DESC"); | |
46 | } else { | |
47 | $res = XDB::query("SELECT * | |
48 | FROM axletter | |
b35106ba | 49 | WHERE id = {?} OR short_name = {?}", $id, $id); |
8da0d3c1 | 50 | } |
51 | if (!$res->numRows()) { | |
4a71cf67 | 52 | throw new MailNotFound(); |
8da0d3c1 | 53 | } |
54 | $id = $res->fetchOneRow(); | |
55 | } | |
56 | list($this->_id, $this->_shortname, $this->_title_mail, $this->_title, | |
57 | $this->_body, $this->_signature, $this->_promo_min, $this->_promo_max, | |
2a3f4f57 | 58 | $this->_subset, $this->_echeance, $this->_date, $this->_bits) = $id; |
9b242f17 | 59 | if ($this->_date == '0000-00-00') { |
60 | $this->_date = 0; | |
61 | } | |
8da0d3c1 | 62 | } |
63 | ||
64 | protected function assignData(&$smarty) | |
65 | { | |
66 | $smarty->assign_by_ref('am', $this); | |
67 | } | |
68 | ||
69 | public function body($format) | |
70 | { | |
71 | return format_text($this->_body, $format); | |
72 | } | |
73 | ||
74 | public function signature($format) | |
75 | { | |
76 | return format_text($this->_signature, $format, 10); | |
77 | } | |
78 | ||
8da0d3c1 | 79 | public function valid() |
80 | { | |
81 | return XDB::execute("UPDATE axletter | |
82 | SET echeance = NOW() | |
83 | WHERE id = {?}", $this->_id); | |
84 | } | |
85 | ||
86 | public function invalid() | |
87 | { | |
88 | return XDB::execute("UPDATE axletter | |
89 | SET bits = 'invalid', date = CURDATE() | |
90 | WHERE id = {?}", $this->_id); | |
91 | } | |
92 | ||
93 | protected function setSent() | |
94 | { | |
95 | XDB::execute("UPDATE axletter | |
96 | SET bits='sent', date=CURDATE() | |
97 | WHERE id={?}", $this->_id); | |
98 | } | |
99 | ||
a0f05027 | 100 | protected function getAllRecipients() |
101 | { | |
74f583b3 VZ |
102 | global $globals; |
103 | return "SELECT ni.user_id, IF(ni.user_id = 0, NULL, u.hruid) AS hruid, | |
104 | IF(ni.user_id = 0, ni.email, CONCAT(a.alias, '@{$globals->mail->domain}')) AS alias, | |
a0f05027 | 105 | IF(ni.user_id = 0, ni.prenom, u.prenom) AS prenom, |
106 | IF(ni.user_id = 0, ni.nom, IF(u.nom_usage='', u.nom, u.nom_usage)) AS nom, | |
107 | FIND_IN_SET('femme', IF(ni.user_id = 0, ni.flag, u.flags)) AS sexe, | |
108 | IF(ni.user_id = 0, 'html', q.core_mail_fmt) AS pref, | |
109 | IF(ni.user_id = 0, ni.hash, 0) AS hash | |
110 | FROM axletter_ins AS ni | |
2a3f4f57 | 111 | {$this->subsetJoin()} |
a0f05027 | 112 | LEFT JOIN auth_user_md5 AS u USING(user_id) |
113 | LEFT JOIN auth_user_quick AS q ON(q.user_id = u.user_id) | |
114 | LEFT JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags)) | |
115 | LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active') | |
4e25428d | 116 | WHERE ni.last < {?} AND {$this->subscriptionWhere()} |
27614f4c | 117 | AND (e.email IS NOT NULL OR FIND_IN_SET('googleapps', u.mail_storage) OR ni.user_id = 0) |
a0f05027 | 118 | GROUP BY u.user_id"; |
eaf30d86 | 119 | } |
a0f05027 | 120 | |
8da0d3c1 | 121 | static public function subscriptionState($uid = null) |
122 | { | |
123 | $user = is_null($uid) ? S::v('uid') : $uid; | |
124 | $res = XDB::query("SELECT 1 | |
125 | FROM axletter_ins | |
126 | WHERE user_id={?}", $user); | |
127 | return $res->fetchOneCell(); | |
eaf30d86 PH |
128 | } |
129 | ||
a0f05027 | 130 | static public function unsubscribe($uid = null, $hash = false) |
8da0d3c1 | 131 | { |
132 | $user = is_null($uid) ? S::v('uid') : $uid; | |
a0f05027 | 133 | $field = !$hash ? 'user_id' : 'hash'; |
134 | if (is_null($uid) && $hash) { | |
135 | return false; | |
136 | } | |
137 | $res = XDB::query("SELECT * | |
138 | FROM axletter_ins | |
139 | WHERE $field={?}", $user); | |
140 | if (!$res->numRows()) { | |
141 | return false; | |
142 | } | |
8da0d3c1 | 143 | XDB::execute("DELETE FROM axletter_ins |
a0f05027 | 144 | WHERE $field = {?}", $user); |
145 | return true; | |
8da0d3c1 | 146 | } |
147 | ||
148 | static public function subscribe($uid = null) | |
149 | { | |
150 | $user = is_null($uid) ? S::v('uid') : $uid; | |
151 | XDB::execute("REPLACE INTO axletter_ins (user_id,last) | |
152 | VALUES ({?}, 0)", $user); | |
153 | } | |
154 | ||
155 | static public function hasPerms() | |
156 | { | |
dd70cd28 | 157 | if (S::admin()) { |
8da0d3c1 | 158 | return true; |
159 | } | |
40b6aa0f | 160 | $res = XDB::query("SELECT COUNT(*) |
8da0d3c1 | 161 | FROM axletter_rights |
162 | WHERE user_id = {?}", S::i('uid')); | |
40b6aa0f | 163 | return ($res->fetchOneCell() > 0); |
8da0d3c1 | 164 | } |
165 | ||
0d75939a | 166 | static public function grantPerms($uid) |
167 | { | |
168 | if (!is_numeric($uid)) { | |
169 | $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid); | |
170 | $uid = $res->fetchOneCell(); | |
171 | } | |
172 | if (!$uid) { | |
173 | return false; | |
174 | } | |
175 | return XDB::execute("INSERT IGNORE INTO axletter_rights SET user_id = {?}", $uid); | |
176 | } | |
177 | ||
178 | static public function revokePerms($uid) | |
179 | { | |
180 | if (!is_numeric($uid)) { | |
181 | $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid); | |
182 | $uid = $res->fetchOneCell(); | |
eaf30d86 | 183 | } |
0d75939a | 184 | if (!$uid) { |
185 | return false; | |
186 | } | |
187 | return XDB::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid); | |
188 | } | |
189 | ||
2a3f4f57 RB |
190 | protected function subsetJoin() |
191 | { | |
192 | if ($this->_subset) { | |
4a08dc73 | 193 | return "INNER JOIN axletter_subsets AS c ON (c.letter_id = ".XDB::escape($this->_id)." AND ni.user_id = c.uid)"; |
2a3f4f57 | 194 | } |
4a08dc73 | 195 | return ''; |
2a3f4f57 RB |
196 | // TODO : force use of the adresses given by AX, not "canonical" ones |
197 | } | |
198 | ||
8da0d3c1 | 199 | protected function subscriptionWhere() |
200 | { | |
4e25428d | 201 | if (!$this->_promo_min && !$this->_promo_max) { |
202 | return '1'; | |
203 | } | |
204 | $where = array(); | |
205 | if ($this->_promo_min) { | |
206 | $where[] = "((ni.user_id = 0 AND ni.promo >= {$this->_promo_min}) OR (ni.user_id != 0 AND u.promo >= {$this->_promo_min}))"; | |
207 | } | |
208 | if ($this->_promo_max) { | |
209 | $where[] = "((ni.user_id = 0 AND ni.promo <= {$this->_promo_max}) OR (ni.user_id != 0 AND u.promo <= {$this->_promo_max}))"; | |
210 | } | |
211 | return implode(' AND ', $where); | |
8da0d3c1 | 212 | } |
213 | ||
214 | static public function awaiting() | |
215 | { | |
a0f05027 | 216 | $res = XDB::query("SELECT * |
8da0d3c1 | 217 | FROM axletter |
218 | WHERE FIND_IN_SET('new', bits)"); | |
a0f05027 | 219 | if ($res->numRows()) { |
220 | return new AXLetter($res->fetchOneRow()); | |
221 | } | |
222 | return null; | |
223 | } | |
224 | ||
225 | static public function toSend() | |
226 | { | |
227 | $res = XDB::query("SELECT * | |
228 | FROM axletter | |
229 | WHERE FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0"); | |
230 | if ($res->numRows()) { | |
231 | return new AXLetter($res->fetchOneRow()); | |
232 | } | |
233 | return null; | |
8da0d3c1 | 234 | } |
235 | ||
236 | static public function listSent() | |
eaf30d86 | 237 | { |
b35106ba | 238 | $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre |
8da0d3c1 | 239 | FROM axletter |
5b46c60b | 240 | WHERE NOT FIND_IN_SET('new', bits) AND NOT FIND_IN_SET('invalid', bits) |
8da0d3c1 | 241 | ORDER BY date DESC"); |
242 | return $res->fetchAllAssoc(); | |
243 | } | |
eaf30d86 | 244 | |
8da0d3c1 | 245 | static public function listAll() |
eaf30d86 | 246 | { |
b35106ba | 247 | $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre |
8da0d3c1 | 248 | FROM axletter |
249 | ORDER BY date DESC"); | |
250 | return $res->fetchAllAssoc(); | |
251 | } | |
252 | } | |
253 | ||
a7de4ef7 | 254 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
8da0d3c1 | 255 | ?> |