autocompletion (suite)
[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);
f62bd784 46 $this->engine =& $this->getEngine($type, $data, $from == 'user' ? null : $this->sender);
e654517d 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
f62bd784 88 private function &getEngine($type, $data, $from)
e654517d 89 {
90 $class = $type . 'Marketing';
91 if (!class_exists($class, false)) {
92 $class= 'DefaultMarketing';
93 }
f62bd784 94 if (!is_subclass_of($class, 'MarketingEngine')) {
95 $engine = null;
96 } else {
97 $engine = new $class($data, $from);
98 }
99 return $engine;
e654517d 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)
171 {
172 $res = XDB::query("SELECT uid, email, message, message_data, type, sender
173 FROM register_marketing
174 WHERE uid = {?} AND email = {?}", $uid, $email);
175 if ($res->numRows() == 0) {
176 return null;
177 }
178 list ($uid, $email, $type, $data, $from, $sender) = $res->fetchOneRow();
179 return new Marketing($uid, $email, $type, $data, $from, $sender);
180 }
181
182 static public function clear($uid, $email = null)
183 {
184 if (!$email) {
185 XDB::execute("DELETE FROM register_marketing WHERE uid = {?}", $uid);
186 } else {
187 XDB::execute("DELETE FROM register_marketing WHERE uid = {?} AND email = {?}", $uid, $email);
e654517d 188 }
189 }
190
191 static public function relance($uid, $nbx = -1)
192 {
193 global $globals;
194
195 if ($nbx < 0) {
196 $res = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE deces=0");
197 $nbx = $res->fetchOneCell();
198 }
199
200 $res = XDB::query("SELECT r.date, u.promo, u.nom, u.prenom, r.email, r.bestalias
201 FROM register_pending AS r
202 INNER JOIN auth_user_md5 AS u ON u.user_id = r.uid
203 WHERE hash!='INSCRIT' AND uid={?} AND TO_DAYS(relance) < TO_DAYS(NOW())", $uid);
204 if (!list($date, $promo, $nom, $prenom, $email, $alias) = $res->fetchOneRow()) {
205 return false;
206 }
207
208 require_once('secure_hash.inc.php');
209 $hash = rand_url_id(12);
210 $pass = rand_pass();
211 $pass_encrypted = hash_encrypt($pass);
212 $fdate = strftime('%d %B %Y', strtotime($date));
213
214 $mymail = new PlMailer('marketing/mail.relance.tpl');
215 $mymail->assign('nbdix', $nbx);
216 $mymail->assign('fdate', $fdate);
217 $mymail->assign('lusername', $alias);
218 $mymail->assign('nveau_pass', $pass);
219 $mymail->assign('baseurl', $globals->baseurl);
220 $mymail->assign('lins_id', $hash);
221 $mymail->assign('lemail', $email);
222 $mymail->assign('subj', $alias.'@'.$globals->mail->domain);
223 $mymail->send();
224 XDB::execute('UPDATE register_pending
225 SET hash={?}, password={?}, relance=NOW()
226 WHERE uid={?}', $hash, $pass_encrypted, $uid);
227 return "$prenom $nom ($promo)";
228 }
0337d704 229}
0337d704 230
e654517d 231interface MarketingEngine
0337d704 232{
e654517d 233 public function __construct($data, $from);
234 public function getTitle();
235 public function getText(array $user);
236 public function process(array $user);
0337d704 237}
238
e654517d 239//
240class AnnuaireMarketing implements MarketingEngine
241{
242 protected $titre;
243 protected $intro;
0337d704 244
e654517d 245 public function __construct($data, $from)
246 {
247 $this->titre = "Annuaire en ligne des Polytechniciens";
248 $this->intro = " Ta fiche n'est pas à jour dans l'annuaire des Polytechniciens sur Internet. "
249 . "Pour la mettre à jour, il te it de visiter cette page ou de copier cette adresse "
250 . "dans la barre de ton navigateur :";
251 }
252
253 public function getTitle()
254 {
255 return $this->titre;
256 }
257
258 private function getIntro()
259 {
260 return $this->intro;
261 }
262
263 protected function prepareText(PlatalPage &$page, array $user)
264 {
265 $page->assign('intro', $this->getIntro());
266 $page->assign('u', $user);
267 $res = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE perms IN ('user', 'admin') AND deces = 0");
268 $page->assign('num_users', $res->fetchOneCell());
269 }
270
271 public function getText(array $user)
272 {
273 $page = new PlatalPage('marketing/mail.marketing.tpl', NO_SKIN);
274 $this->prepareText($page, $user);
275 return $page->raw();
276 }
277
278 public function process(array $user)
279 {
280 }
281}
282
283class ListMarketing extends AnnuaireMarketing
0337d704 284{
e654517d 285 private $name;
286 private $domain;
287 public function __construct($data, $from)
288 {
289 list($this->name, $this->domain) = explode('@', $data);
290 $res = XDB::query("SELECT prenom, IF (nom_usage != '', nom_usage, nom)
291 FROM auth_user_md5
292 WHERE user_id = {?} AND user_id != 0", $from ? $from : 0);
293 if ($res->numRows()) {
294 list($prenom, $nom) = $res->fetchOneRow();
295 $from = "$prenom $nom";
296 } else {
297 $from = "Je";
298 }
299 $this->titre = "Un camarade solicite ton inscription à $data";
300 $this->intro = "Polytechnique.org, l'annuaire des Polytechniciens sur internet, "
301 . "fournit de nombreux services aux groupes X, ainsi que des listes "
302 . "de diffusion pour les X en faisant la demande.\n\n"
303 . "$from solicite ton inscription à la liste <$data>. "
304 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent "
305 . "profiter de l'ensemble de nos services, c'est pourquoi nous te "
306 . "proposons auparavant de t'inscrire sur notre site. Pour cela, il "
307 . "te suffit de visiter cette page ou de copier cette adresse dans "
308 . "la barre de ton navigateur :";
309 }
0337d704 310
e654517d 311 public function process(array $user)
312 {
313 return XDB::execute("REPLACE INTO register_subs (uid, type, sub, domain)
314 VALUES ({?}, 'list', {?}, {?})",
315 $user['id'], $this->name, $this->domain);
0337d704 316 }
e654517d 317}
0337d704 318
e654517d 319class GroupMarketing extends AnnuaireMarketing
320{
321 private $group;
322 public function __construct($data, $from)
323 {
324 $this->group = $data;
325 $res = XDB::query("SELECT prenom, IF (nom_usage != '', nom_usage, nom)
326 FROM auth_user_md5
327 WHERE user_id = {?} AND user_id != 0", $from ? $from : 0);
328 if ($res->numRows()) {
329 list($prenom, $nom) = $res->fetchOneRow();
330 $from = "$prenom $nom vient";
331 } else {
332 $from = "Je viens";
333 }
334 $this->titre = "Profite de ton inscription au groupe \"$data\" pour découvrir Polytechnique.org";
335 $this->intro = "Polytechnique.org, l'annuaire des Polytechniciens sur internet, fournit "
336 . "de nombreux services aux groupes X ( listes de diffusion, paiement en "
337 . "ligne, sites internet...), en particulier pour le groupe \"$data\"\n\n"
338 . "$from de t'inscrire dans l'annuaire du groupe \"$data\". "
339 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent profiter "
340 . "de l'ensemble de nos services, c'est pourquoi nous te proposons de "
341 . "t'inscrire sur notre site . Pour cela, il te suffit de visiter cette page "
342 . "ou de copier cette adresse dans la barre de ton navigateur :";
0337d704 343 }
344
e654517d 345 public function process(array $user)
346 {
347 return XDB::execute("REPLACE INTO register_subs (uid, type, sub, domain)
348 VALUES ({?}, 'group', {?}, '')",
349 $user['id'], $this->group);
350 }
0337d704 351}
352
e654517d 353/// Make AnnuaireMarketing to be the default message
354class DefaultMarketing extends AnnuaireMarketing
355{
356}
0337d704 357
a7de4ef7 358// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
0337d704 359?>