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