New evolutive marketing engine
[platal.git] / include / marketing.inc.php
CommitLineData
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 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);
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);
183 XDB::execute("DELETE FROM register_subs WHERE uid = {?}", $uid);
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 }
195
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 }
203
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));
209
210 $mymail = new PlMailer('marketing/mail.relance.tpl');
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
e654517d 235//
236class AnnuaireMarketing implements MarketingEngine
237{
238 protected $titre;
239 protected $intro;
0337d704 240
e654517d 241 public function __construct($data, $from)
242 {
243 $this->titre = "Annuaire en ligne des Polytechniciens";
244 $this->intro = " Ta fiche n'est pas à jour dans l'annuaire des Polytechniciens sur Internet. "
245 . "Pour la mettre à jour, il te it de visiter cette page ou de copier cette adresse "
246 . "dans la barre de ton navigateur :";
247 }
248
249 public function getTitle()
250 {
251 return $this->titre;
252 }
253
254 private function getIntro()
255 {
256 return $this->intro;
257 }
258
259 protected function prepareText(PlatalPage &$page, array $user)
260 {
261 $page->assign('intro', $this->getIntro());
262 $page->assign('u', $user);
263 $res = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE perms IN ('user', 'admin') AND deces = 0");
264 $page->assign('num_users', $res->fetchOneCell());
265 }
266
267 public function getText(array $user)
268 {
269 $page = new PlatalPage('marketing/mail.marketing.tpl', NO_SKIN);
270 $this->prepareText($page, $user);
271 return $page->raw();
272 }
273
274 public function process(array $user)
275 {
276 }
277}
278
279class ListMarketing extends AnnuaireMarketing
0337d704 280{
e654517d 281 private $name;
282 private $domain;
283 public function __construct($data, $from)
284 {
285 list($this->name, $this->domain) = explode('@', $data);
286 $res = XDB::query("SELECT prenom, IF (nom_usage != '', nom_usage, nom)
287 FROM auth_user_md5
288 WHERE user_id = {?} AND user_id != 0", $from ? $from : 0);
289 if ($res->numRows()) {
290 list($prenom, $nom) = $res->fetchOneRow();
291 $from = "$prenom $nom";
292 } else {
293 $from = "Je";
294 }
295 $this->titre = "Un camarade solicite ton inscription à $data";
296 $this->intro = "Polytechnique.org, l'annuaire des Polytechniciens sur internet, "
297 . "fournit de nombreux services aux groupes X, ainsi que des listes "
298 . "de diffusion pour les X en faisant la demande.\n\n"
299 . "$from solicite ton inscription à la liste <$data>. "
300 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent "
301 . "profiter de l'ensemble de nos services, c'est pourquoi nous te "
302 . "proposons auparavant de t'inscrire sur notre site. Pour cela, il "
303 . "te suffit de visiter cette page ou de copier cette adresse dans "
304 . "la barre de ton navigateur :";
305 }
0337d704 306
e654517d 307 public function process(array $user)
308 {
309 return XDB::execute("REPLACE INTO register_subs (uid, type, sub, domain)
310 VALUES ({?}, 'list', {?}, {?})",
311 $user['id'], $this->name, $this->domain);
0337d704 312 }
e654517d 313}
0337d704 314
e654517d 315class GroupMarketing extends AnnuaireMarketing
316{
317 private $group;
318 public function __construct($data, $from)
319 {
320 $this->group = $data;
321 $res = XDB::query("SELECT prenom, IF (nom_usage != '', nom_usage, nom)
322 FROM auth_user_md5
323 WHERE user_id = {?} AND user_id != 0", $from ? $from : 0);
324 if ($res->numRows()) {
325 list($prenom, $nom) = $res->fetchOneRow();
326 $from = "$prenom $nom vient";
327 } else {
328 $from = "Je viens";
329 }
330 $this->titre = "Profite de ton inscription au groupe \"$data\" pour découvrir Polytechnique.org";
331 $this->intro = "Polytechnique.org, l'annuaire des Polytechniciens sur internet, fournit "
332 . "de nombreux services aux groupes X ( listes de diffusion, paiement en "
333 . "ligne, sites internet...), en particulier pour le groupe \"$data\"\n\n"
334 . "$from de t'inscrire dans l'annuaire du groupe \"$data\". "
335 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent profiter "
336 . "de l'ensemble de nos services, c'est pourquoi nous te proposons de "
337 . "t'inscrire sur notre site . Pour cela, il te suffit de visiter cette page "
338 . "ou de copier cette adresse dans la barre de ton navigateur :";
0337d704 339 }
340
e654517d 341 public function process(array $user)
342 {
343 return XDB::execute("REPLACE INTO register_subs (uid, type, sub, domain)
344 VALUES ({?}, 'group', {?}, '')",
345 $user['id'], $this->group);
346 }
0337d704 347}
348
e654517d 349/// Make AnnuaireMarketing to be the default message
350class DefaultMarketing extends AnnuaireMarketing
351{
352}
0337d704 353
a7de4ef7 354// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
0337d704 355?>