0337d704 |
1 | <?php |
2 | /*************************************************************************** |
5ddeb07c |
3 | * Copyright (C) 2003-2007 Polytechnique.org * |
0337d704 |
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 | |
e654517d |
22 | class Marketing |
23 | { |
24 | static private $engines = array( |
25 | //user name => array(class name, require data) |
26 | 'annuaire' => array('AnnuaireMarketing', false), |
27 | 'groupe' => array('GroupMarketing', true), |
28 | 'liste' => array('ListMarketing', true), |
29 | ); |
0337d704 |
30 | |
e654517d |
31 | private $engine; |
32 | public $sender_mail; |
33 | public $user; |
0337d704 |
34 | |
e654517d |
35 | private $type; |
36 | private $data; |
37 | private $from; |
38 | private $sender; |
39 | |
40 | private $hash = ''; |
41 | |
42 | public function __construct($uid, $email, $type, $data, $from, $sender = null) |
43 | { |
44 | $this->user = $this->getUser($uid, $email); |
45 | $this->sender_mail = $this->getFrom($from, $sender); |
46 | $this->engine = $this->getEngine($type, $data, $from == 'user' ? null : $this->sender); |
47 | |
48 | $this->type = $type; |
49 | $this->data = $data; |
50 | $this->from = $from; |
51 | $this->sender = $sender; |
52 | } |
53 | |
54 | private function getUser($uid, $email) |
55 | { |
56 | require_once("xorg.misc.inc.php"); |
57 | $res = XDB::query("SELECT FIND_IN_SET('femme', flags) AS sexe, nom, prenom, promo |
58 | FROM auth_user_md5 |
59 | WHERE user_id = {?}", $uid); |
60 | if ($res->numRows() == 0) { |
61 | return null; |
62 | } |
63 | $user = $res->fetchOneAssoc(); |
64 | $user['id'] = $uid; |
65 | $user['forlife'] = make_forlife($user['prenom'], $user['nom'], $user['promo']); |
66 | $user['mail'] = $email; |
67 | $user['to'] = '"' . $user['prenom'] . ' ' . $user['nom'] . '" <' . $email . '>'; |
68 | return $user; |
69 | } |
70 | |
71 | private function getFrom($from, $sender) |
72 | { |
73 | if ($from == 'staff') { |
74 | return '"Equipe Polytechnique.org" <register@polytechnique.org>'; |
75 | } else { |
76 | $res = XDB::query("SELECT u.nom, u.prenom, a.alias |
77 | FROM auth_user_md5 AS u |
78 | INNER JOIN aliases AS a ON (a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags)) |
79 | WHERE u.user_id = {?}", $sender); |
80 | if (!$res->numRows()) { |
81 | return '"Equipe Polytechnique.org" <register@polytechnique.org>'; |
82 | } |
83 | $sender = $res->fetchOneAssoc(); |
84 | return '"' . $sender['prenom'] . ' ' . $sender['nom'] . '" <' . $sender['alias'] . '@polytechnique.org>'; |
85 | } |
86 | } |
87 | |
88 | private function getEngine($type, $data, $from) |
89 | { |
90 | $class = $type . 'Marketing'; |
91 | if (!class_exists($class, false)) { |
92 | $class= 'DefaultMarketing'; |
93 | } |
94 | return new $class($data, $from); |
95 | } |
96 | |
97 | public function getTitle() |
98 | { |
99 | return $this->engine->getTitle(); |
100 | } |
101 | |
102 | public function getText() |
103 | { |
104 | return $this->engine->getText($this->user); |
105 | } |
106 | |
107 | public function send($title = null, $text = null) |
108 | { |
109 | $this->hash = rand_url_id(12); |
110 | if (!$title) { |
111 | $title = $this->engine->getTitle(); |
112 | } |
113 | if (!$text) { |
114 | $text = $this->engine->getText($this->user); |
115 | } |
116 | $sender = substr($this->sender_mail, 1, strpos($this->sender_mail, '"', 2)-1); |
117 | $text = str_replace(array("%%hash%%", "%%sender%%"), |
118 | array($this->hash, $this->sender_mail), |
119 | $text); |
120 | $mailer = new PlMailer(); |
121 | $mailer->setFrom($this->sender_mail); |
122 | $mailer->addTo($this->user['mail']); |
123 | $mailer->setSubject($title); |
124 | $mailer->setTxtBody($text); |
125 | $mailer->send(); |
126 | $this->incr(); |
127 | } |
128 | |
129 | public function add($valid = true) |
130 | { |
131 | XDB::execute('INSERT IGNORE INTO register_marketing |
132 | (uid, sender, email, date, last, nb, type, hash, message, message_data) |
133 | VALUES ({?}, {?}, {?}, NOW(), 0, 0, {?}, {?}, {?}, {?})', |
134 | $this->user['id'], $this->sender, $this->user['mail'], $this->from, $this->hash, |
135 | $this->type, $this->data); |
136 | $this->engine->process($this->user); |
137 | if ($valid) { |
138 | require_once 'validations.inc.php'; |
139 | $valid = new MarkReq($this->sender, $this->user['id'], $this->user['mail'], |
140 | $this->from == 'user', $this->type, $this->data); |
141 | $valid->submit(); |
142 | } |
143 | return true; |
144 | } |
145 | |
146 | private function incr() |
147 | { |
148 | XDB::execute('UPDATE register_marketing |
149 | SET nb=nb+1, hash={?}, last=NOW() |
150 | WHERE uid={?} AND email={?}', |
151 | $this->hash, $this->user['id'], $this->user['mail']); |
152 | } |
153 | |
154 | static public function getEngineList($exclude_data = true) |
155 | { |
156 | $array = array(); |
157 | foreach (Marketing::$engines as $e => $d) { |
158 | if (!$d[1] || !$exclude_data) { |
159 | $array[] = $e; |
160 | } |
161 | } |
162 | return $array; |
163 | } |
164 | |
165 | static public function get($uid, $email) |
166 | { |
167 | $res = XDB::query("SELECT uid, email, message, message_data, type, sender |
168 | FROM register_marketing |
169 | WHERE uid = {?} AND email = {?}", $uid, $email); |
170 | if ($res->numRows() == 0) { |
171 | return null; |
172 | } |
173 | list ($uid, $email, $type, $data, $from, $sender) = $res->fetchOneRow(); |
174 | return new Marketing($uid, $email, $type, $data, $from, $sender); |
175 | } |
176 | |
177 | static public function clear($uid, $email = null) |
178 | { |
179 | if (!$email) { |
180 | XDB::execute("DELETE FROM register_marketing WHERE uid = {?}", $uid); |
181 | } else { |
182 | XDB::execute("DELETE FROM register_marketing WHERE uid = {?} AND email = {?}", $uid, $email); |
e654517d |
183 | } |
184 | } |
185 | |
186 | static public function relance($uid, $nbx = -1) |
187 | { |
188 | global $globals; |
189 | |
190 | if ($nbx < 0) { |
191 | $res = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE deces=0"); |
192 | $nbx = $res->fetchOneCell(); |
193 | } |
194 | |
195 | $res = XDB::query("SELECT r.date, u.promo, u.nom, u.prenom, r.email, r.bestalias |
196 | FROM register_pending AS r |
197 | INNER JOIN auth_user_md5 AS u ON u.user_id = r.uid |
198 | WHERE hash!='INSCRIT' AND uid={?} AND TO_DAYS(relance) < TO_DAYS(NOW())", $uid); |
199 | if (!list($date, $promo, $nom, $prenom, $email, $alias) = $res->fetchOneRow()) { |
200 | return false; |
201 | } |
202 | |
203 | require_once('secure_hash.inc.php'); |
204 | $hash = rand_url_id(12); |
205 | $pass = rand_pass(); |
206 | $pass_encrypted = hash_encrypt($pass); |
207 | $fdate = strftime('%d %B %Y', strtotime($date)); |
208 | |
209 | $mymail = new PlMailer('marketing/mail.relance.tpl'); |
210 | $mymail->assign('nbdix', $nbx); |
211 | $mymail->assign('fdate', $fdate); |
212 | $mymail->assign('lusername', $alias); |
213 | $mymail->assign('nveau_pass', $pass); |
214 | $mymail->assign('baseurl', $globals->baseurl); |
215 | $mymail->assign('lins_id', $hash); |
216 | $mymail->assign('lemail', $email); |
217 | $mymail->assign('subj', $alias.'@'.$globals->mail->domain); |
218 | $mymail->send(); |
219 | XDB::execute('UPDATE register_pending |
220 | SET hash={?}, password={?}, relance=NOW() |
221 | WHERE uid={?}', $hash, $pass_encrypted, $uid); |
222 | return "$prenom $nom ($promo)"; |
223 | } |
0337d704 |
224 | } |
0337d704 |
225 | |
e654517d |
226 | interface MarketingEngine |
0337d704 |
227 | { |
e654517d |
228 | public function __construct($data, $from); |
229 | public function getTitle(); |
230 | public function getText(array $user); |
231 | public function process(array $user); |
0337d704 |
232 | } |
233 | |
e654517d |
234 | // |
235 | class AnnuaireMarketing implements MarketingEngine |
236 | { |
237 | protected $titre; |
238 | protected $intro; |
0337d704 |
239 | |
e654517d |
240 | public function __construct($data, $from) |
241 | { |
242 | $this->titre = "Annuaire en ligne des Polytechniciens"; |
243 | $this->intro = " Ta fiche n'est pas à jour dans l'annuaire des Polytechniciens sur Internet. " |
244 | . "Pour la mettre à jour, il te it de visiter cette page ou de copier cette adresse " |
245 | . "dans la barre de ton navigateur :"; |
246 | } |
247 | |
248 | public function getTitle() |
249 | { |
250 | return $this->titre; |
251 | } |
252 | |
253 | private function getIntro() |
254 | { |
255 | return $this->intro; |
256 | } |
257 | |
258 | protected function prepareText(PlatalPage &$page, array $user) |
259 | { |
260 | $page->assign('intro', $this->getIntro()); |
261 | $page->assign('u', $user); |
262 | $res = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE perms IN ('user', 'admin') AND deces = 0"); |
263 | $page->assign('num_users', $res->fetchOneCell()); |
264 | } |
265 | |
266 | public function getText(array $user) |
267 | { |
268 | $page = new PlatalPage('marketing/mail.marketing.tpl', NO_SKIN); |
269 | $this->prepareText($page, $user); |
270 | return $page->raw(); |
271 | } |
272 | |
273 | public function process(array $user) |
274 | { |
275 | } |
276 | } |
277 | |
278 | class ListMarketing extends AnnuaireMarketing |
0337d704 |
279 | { |
e654517d |
280 | private $name; |
281 | private $domain; |
282 | public function __construct($data, $from) |
283 | { |
284 | list($this->name, $this->domain) = explode('@', $data); |
285 | $res = XDB::query("SELECT prenom, IF (nom_usage != '', nom_usage, nom) |
286 | FROM auth_user_md5 |
287 | WHERE user_id = {?} AND user_id != 0", $from ? $from : 0); |
288 | if ($res->numRows()) { |
289 | list($prenom, $nom) = $res->fetchOneRow(); |
290 | $from = "$prenom $nom"; |
291 | } else { |
292 | $from = "Je"; |
293 | } |
294 | $this->titre = "Un camarade solicite ton inscription à $data"; |
295 | $this->intro = "Polytechnique.org, l'annuaire des Polytechniciens sur internet, " |
296 | . "fournit de nombreux services aux groupes X, ainsi que des listes " |
297 | . "de diffusion pour les X en faisant la demande.\n\n" |
298 | . "$from solicite ton inscription à la liste <$data>. " |
299 | . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent " |
300 | . "profiter de l'ensemble de nos services, c'est pourquoi nous te " |
301 | . "proposons auparavant de t'inscrire sur notre site. Pour cela, il " |
302 | . "te suffit de visiter cette page ou de copier cette adresse dans " |
303 | . "la barre de ton navigateur :"; |
304 | } |
0337d704 |
305 | |
e654517d |
306 | public function process(array $user) |
307 | { |
308 | return XDB::execute("REPLACE INTO register_subs (uid, type, sub, domain) |
309 | VALUES ({?}, 'list', {?}, {?})", |
310 | $user['id'], $this->name, $this->domain); |
0337d704 |
311 | } |
e654517d |
312 | } |
0337d704 |
313 | |
e654517d |
314 | class GroupMarketing extends AnnuaireMarketing |
315 | { |
316 | private $group; |
317 | public function __construct($data, $from) |
318 | { |
319 | $this->group = $data; |
320 | $res = XDB::query("SELECT prenom, IF (nom_usage != '', nom_usage, nom) |
321 | FROM auth_user_md5 |
322 | WHERE user_id = {?} AND user_id != 0", $from ? $from : 0); |
323 | if ($res->numRows()) { |
324 | list($prenom, $nom) = $res->fetchOneRow(); |
325 | $from = "$prenom $nom vient"; |
326 | } else { |
327 | $from = "Je viens"; |
328 | } |
329 | $this->titre = "Profite de ton inscription au groupe \"$data\" pour découvrir Polytechnique.org"; |
330 | $this->intro = "Polytechnique.org, l'annuaire des Polytechniciens sur internet, fournit " |
331 | . "de nombreux services aux groupes X ( listes de diffusion, paiement en " |
332 | . "ligne, sites internet...), en particulier pour le groupe \"$data\"\n\n" |
333 | . "$from de t'inscrire dans l'annuaire du groupe \"$data\". " |
334 | . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent profiter " |
335 | . "de l'ensemble de nos services, c'est pourquoi nous te proposons de " |
336 | . "t'inscrire sur notre site . Pour cela, il te suffit de visiter cette page " |
337 | . "ou de copier cette adresse dans la barre de ton navigateur :"; |
0337d704 |
338 | } |
339 | |
e654517d |
340 | public function process(array $user) |
341 | { |
342 | return XDB::execute("REPLACE INTO register_subs (uid, type, sub, domain) |
343 | VALUES ({?}, 'group', {?}, '')", |
344 | $user['id'], $this->group); |
345 | } |
0337d704 |
346 | } |
347 | |
e654517d |
348 | /// Make AnnuaireMarketing to be the default message |
349 | class DefaultMarketing extends AnnuaireMarketing |
350 | { |
351 | } |
0337d704 |
352 | |
a7de4ef7 |
353 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
0337d704 |
354 | ?> |