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