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