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