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