2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
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
),
42 public function __construct($uid, $email, $type, $data, $from, $sender = null
)
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
);
51 $this->sender
= $sender;
54 private function getUser($uid, $email)
56 require_once("xorg.misc.inc.php");
57 $res = XDB
::query("SELECT FIND_IN_SET('femme', flags) AS sexe, nom, prenom, promo
59 WHERE user_id = {?}", $uid);
60 if ($res->numRows() == 0) {
63 $user = $res->fetchOneAssoc();
65 $user['forlife'] = make_forlife($user['prenom'], $user['nom'], $user['promo']);
66 $user['mail'] = $email;
67 $user['to'] = '"' . $user['prenom'] . ' ' . $user['nom'] . '" <' . $email . '>';
71 private function getFrom($from, $sender)
75 if ($from == 'staff') {
76 return '"Equipe Polytechnique.org" <register@' . $globals->mail
->domain
. '>';
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()) {
83 return '"Equipe Polytechnique.org" <register@' . $globals->mail
->domain
. '>';
85 $sender = $res->fetchOneAssoc();
86 return '"' . $sender['prenom'] . ' ' . $sender['nom'] . '" <' . $sender['alias'] . '@' . $globals->mail
->domain
. '>';
90 private function &getEngine($type, $data, $from)
92 $class = $type . 'Marketing';
93 if (!class_exists($class, false
)) {
94 $class= 'DefaultMarketing';
96 $engine = new $class($data, $from);
97 if (!$engine instanceof MarketingEngine
) {
103 public function getTitle()
105 return $this->engine
->getTitle();
108 public function getText()
110 return $this->engine
->getText($this->user
);
113 public function send($title = null
, $text = null
)
115 $this->hash
= rand_url_id(12);
117 $title = $this->engine
->getTitle();
120 $text = $this->engine
->getText($this->user
);
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
),
126 $mailer = new PlMailer();
127 $mailer->setFrom($this->sender_mail
);
128 $mailer->addTo($this->user
['mail']);
129 $mailer->setSubject($title);
130 $mailer->setTxtBody($text);
135 public function add($valid = true
)
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
);
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
);
152 private function incr()
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']);
160 static public function getEngineList($exclude_data = true
)
163 foreach (Marketing
::$engines as $e => $d) {
164 if (!$d[1] ||
!$exclude_data) {
171 static public function get($uid, $email, $recentOnly = false
)
173 $res = XDB
::query("SELECT uid, email, message, message_data, type, sender
174 FROM register_marketing
177 $recentOnly ?
' AND DATEDIFF(NOW(), last) < 30' : ''), $uid, $email);
179 if ($res->numRows() == 0) {
182 list ($uid, $email, $type, $data, $from, $sender) = $res->fetchOneRow();
183 return new Marketing($uid, $email, $type, $data, $from, $sender);
186 static public function clear($uid, $email = null
)
189 XDB
::execute("DELETE FROM register_marketing WHERE uid = {?}", $uid);
191 XDB
::execute("DELETE FROM register_marketing WHERE uid = {?} AND email = {?}", $uid, $email);
195 static public function relance($uid, $nbx = -1)
200 $res = XDB
::query("SELECT COUNT(*) FROM auth_user_md5 WHERE deces=0");
201 $nbx = $res->fetchOneCell();
204 $res = XDB
::query("SELECT r.date, u.promo, u.nom, u.prenom, r.email, r.bestalias
205 FROM register_pending AS r
206 INNER JOIN auth_user_md5 AS u ON u.user_id = r.uid
207 WHERE hash!='INSCRIT' AND uid={?} AND TO_DAYS(relance) < TO_DAYS(NOW())", $uid);
208 if (!list($date, $promo, $nom, $prenom, $email, $alias) = $res->fetchOneRow()) {
212 require_once('secure_hash.inc.php');
213 $hash = rand_url_id(12);
215 $pass_encrypted = hash_encrypt($pass);
216 $fdate = strftime('%d %B %Y', strtotime($date));
218 $mymail = new PlMailer('marketing/mail.relance.tpl');
219 $mymail->assign('nbdix', $nbx);
220 $mymail->assign('fdate', $fdate);
221 $mymail->assign('lusername', $alias);
222 $mymail->assign('nveau_pass', $pass);
223 $mymail->assign('baseurl', $globals->baseurl
);
224 $mymail->assign('lins_id', $hash);
225 $mymail->assign('lemail', $email);
226 $mymail->assign('subj', $alias.'@'.$globals->mail
->domain
);
228 XDB
::execute('UPDATE register_pending
229 SET hash={?}, password={?}, relance=NOW()
230 WHERE uid={?}', $hash, $pass_encrypted, $uid);
231 return "$prenom $nom ($promo)";
235 interface MarketingEngine
237 public function __construct($data, $from);
238 public function getTitle();
239 public function getText(array $user);
240 public function process(array $user);
244 class AnnuaireMarketing
implements MarketingEngine
249 public function __construct($data, $from)
251 $this->titre
= "Annuaire en ligne des Polytechniciens";
252 $this->intro
= " Ta fiche n'est pas à jour dans l'annuaire des Polytechniciens sur Internet. "
253 . "Pour la mettre à jour, il te it de visiter cette page ou de copier cette adresse "
254 . "dans la barre de ton navigateur :";
257 public function getTitle()
262 private function getIntro()
267 protected function prepareText(PlatalPage
&$page, array $user)
269 $page->assign('intro', $this->getIntro());
270 $page->assign('u', $user);
271 $res = XDB
::query("SELECT COUNT(*) FROM auth_user_md5 WHERE perms IN ('user', 'admin') AND deces = 0");
272 $page->assign('num_users', $res->fetchOneCell());
275 public function getText(array $user)
277 $page = new XorgPage('marketing/mail.marketing.tpl', NO_SKIN
);
278 $this->prepareText($page, $user);
282 public function process(array $user)
287 class ListMarketing
extends AnnuaireMarketing
291 public function __construct($data, $from)
293 list($this->name
, $this->domain
) = explode('@', $data);
294 $res = XDB
::query("SELECT prenom, IF (nom_usage != '', nom_usage, nom)
296 WHERE user_id = {?} AND user_id != 0", $from ?
$from : 0);
297 if ($res->numRows()) {
298 list($prenom, $nom) = $res->fetchOneRow();
299 $from = "$prenom $nom";
303 $this->titre
= "Un camarade solicite ton inscription à $data";
304 $this->intro
= "Polytechnique.org, l'annuaire des Polytechniciens sur internet, "
305 . "fournit de nombreux services aux groupes X, ainsi que des listes "
306 . "de diffusion pour les X en faisant la demande.\n\n"
307 . "$from solicite ton inscription à la liste <$data>. "
308 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent "
309 . "profiter de l'ensemble de nos services, c'est pourquoi nous te "
310 . "proposons auparavant de t'inscrire sur notre site. Pour cela, il "
311 . "te suffit de visiter cette page ou de copier cette adresse dans "
312 . "la barre de ton navigateur :";
315 public function process(array $user)
317 return XDB
::execute("REPLACE INTO register_subs (uid, type, sub, domain)
318 VALUES ({?}, 'list', {?}, {?})",
319 $user['id'], $this->name
, $this->domain
);
323 class GroupMarketing
extends AnnuaireMarketing
326 public function __construct($data, $from)
328 $this->group
= $data;
329 $res = XDB
::query("SELECT prenom, IF (nom_usage != '', nom_usage, nom)
331 WHERE user_id = {?} AND user_id != 0", $from ?
$from : 0);
332 if ($res->numRows()) {
333 list($prenom, $nom) = $res->fetchOneRow();
334 $from = "$prenom $nom vient";
338 $this->titre
= "Profite de ton inscription au groupe \"$data\" pour découvrir Polytechnique.org";
339 $this->intro
= "Polytechnique.org, l'annuaire des Polytechniciens sur internet, fournit "
340 . "de nombreux services aux groupes X ( listes de diffusion, paiement en "
341 . "ligne, sites internet...), en particulier pour le groupe \"$data\"\n\n"
342 . "$from de t'inscrire dans l'annuaire du groupe \"$data\". "
343 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent profiter "
344 . "de l'ensemble de nos services, c'est pourquoi nous te proposons de "
345 . "t'inscrire sur notre site . Pour cela, il te suffit de visiter cette page "
346 . "ou de copier cette adresse dans la barre de ton navigateur :";
349 public function process(array $user)
351 return XDB
::execute("REPLACE INTO register_subs (uid, type, sub, domain)
352 VALUES ({?}, 'group', {?}, '')",
353 $user['id'], $this->group
);
357 /// Make AnnuaireMarketing to be the default message
358 class DefaultMarketing
extends AnnuaireMarketing
362 // vim:set et sw=4 sts=4 sws=4 enc=utf-8: