2 /***************************************************************************
3 * Copyright (C) 2003-2010 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
),
39 private $personal_notes;
43 public function __construct($uid, $email, $type, $data, $from, $sender = null
, $personal_notes = null
)
45 $this->user
= $this->getUser($uid, $email);
46 $this->sender_mail
= $this->getFrom($from, $sender);
47 $this->engine
=& $this->getEngine($type, $data, $from == 'user' ?
$sender : null
, $personal_notes);
52 $this->sender
= $sender;
53 $this->personal_notes
= $personal_notes;
56 private function getUser($uid, $email)
58 $user = User
::getSilent($uid);
67 'sexe' => $user->isFemale(),
69 'to' => '"' . $user->fullName() . '" <' . $email . '>',
70 'forlife_email' => $user->login() . '@' . $globals->mail
->domain
,
71 'forlife_email2' => $user->login() . '@' . $globals->mail
->domain2
,
75 private function getFrom($from, $sender)
79 if ($from == 'staff' ||
!($user = User
::getSilent($sender))) {
80 return "\"L'équipe de Polytechnique.org\" <register@" . $globals->mail
->domain
. '>';
82 return '"' . $user->fullName() . '" <' . $user->bestEmail() . '>';
85 private function &getEngine($type, $data, $from, $personal_notes)
87 $class = $type . 'Marketing';
88 if (!class_exists($class, false
)) {
89 $class= 'DefaultMarketing';
91 $engine = new $class($data, $from, $personal_notes);
92 if (!$engine instanceof MarketingEngine
) {
98 public function getTitle()
100 return $this->engine
->getTitle();
103 public function getText()
105 return $this->engine
->getText($this->user
);
108 public function send($title = null
, $text = null
)
110 $this->hash
= rand_url_id(12);
112 $title = $this->engine
->getTitle();
115 $text = $this->engine
->getText($this->user
);
117 $sender = substr($this->sender_mail
, 1, strpos($this->sender_mail
, '"', 2)-1);
118 $text = str_replace(array('%%hash%%', '%%sender%%', '%%personal_notes%%'),
119 array($this->hash
, "Cordialement,\n-- \n" . $this->sender_mail
, ''), $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);
129 public function add($valid = true
)
131 XDB
::execute('INSERT IGNORE INTO register_marketing
132 (uid, sender, email, date, last, nb, type, hash, message, message_data, personal_notes)
133 VALUES ({?}, {?}, {?}, NOW(), 0, 0, {?}, {?}, {?}, {?}, {?})',
134 $this->user
['id'], $this->sender
, $this->user
['mail'], $this->from
, $this->hash
,
135 $this->type
, $this->data
, $this->personal_notes
);
136 $this->engine
->process($this->user
);
138 require_once 'validations.inc.php';
139 $sender = User
::getSilent($this->sender
);
140 $valid = new MarkReq($sender, $this->user
['user'], $this->user
['mail'],
141 $this->from
== 'user', $this->type
, $this->data
, $this->personal_notes
);
147 private function incr()
149 XDB
::execute('UPDATE register_marketing
150 SET nb=nb+1, hash={?}, last=NOW()
151 WHERE uid={?} AND email={?}',
152 $this->hash
, $this->user
['id'], $this->user
['mail']);
155 static public function getEngineList($exclude_data = true
)
158 foreach (Marketing
::$engines as $e => $d) {
159 if (!$d[1] ||
!$exclude_data) {
166 static public function get($uid, $email, $recentOnly = false
)
168 $res = XDB
::query("SELECT uid, email, message, message_data, type, sender, personal_notes
169 FROM register_marketing
172 $recentOnly ?
' AND DATEDIFF(NOW(), last) < 30' : ''), $uid, $email);
174 if ($res->numRows() == 0) {
177 list ($uid, $email, $type, $data, $from, $sender, $personal_notes) = $res->fetchOneRow();
178 return new Marketing($uid, $email, $type, $data, $from, $sender, $personal_notes);
181 static public function clear($uid, $email = null
)
184 XDB
::execute("DELETE FROM register_marketing WHERE uid = {?}", $uid);
186 XDB
::execute("DELETE FROM register_marketing WHERE uid = {?} AND email = {?}", $uid, $email);
190 static public function relance(PlUser
&$user, $nbx = -1)
195 $nbx = $globals->core
->NbIns
;
198 $res = XDB
::fetchOneCell('SELECT r.date, r.email, r.bestalias
199 FROM register_pending
200 WHERE r.hash = \'INSCRIT\' AND uid = {?}',
205 list($date, $email, $alias) = $res;
208 $hash = rand_url_id(12);
210 $pass_encrypted = sha1($pass);
211 $fdate = strftime('%d %B %Y', strtotime($date));
213 $mymail = new PlMailer('marketing/relance.mail.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
);
223 XDB
::execute('UPDATE register_pending
224 SET hash={?}, password={?}, relance=NOW()
225 WHERE uid={?}', $hash, $pass_encrypted, $user->id());
226 return $user->fullName();
230 interface MarketingEngine
232 public function __construct($data, $from, $personal_notes = null
);
233 public function getTitle();
234 public function getText(array $user);
235 public function process(array $user);
238 class AnnuaireMarketing
implements MarketingEngine
242 protected $signature;
243 protected $personal_notes;
245 public function __construct($data, $from, $personal_notes = null
)
247 $this->titre
= "Rejoins la communauté polytechnicienne sur Internet";
248 $this->intro
= " Tu n'as pas de fiche dans l'annuaire des polytechniciens sur Internet. "
249 . "Pour y figurer, il te suffit de visiter cette page ou de copier cette adresse "
250 . "dans la barre de ton navigateur :";
251 if ($from === null
) {
252 $page = new XorgPage();
253 $page->changeTpl('include/signature.mail.tpl', NO_SKIN
);
254 $page->assign('mail_part', 'text');
255 $this->signature
= $page->raw();
257 $this->signature
= '%%sender%%';
259 if (is_null($personal_notes) ||
$personal_notes == '') {
260 $this->personal_notes
= '%%personal_notes%%';
262 $this->personal_notes
= "\n" . $personal_notes . "\n";
266 public function getTitle()
271 private function getIntro()
276 public function getSignature()
278 return $this->signature
;
281 public function getPersonalNotes()
283 return $this->personal_notes
;
286 protected function prepareText(PlPage
&$page, array $user)
288 $page->assign('intro', $this->getIntro());
289 $page->assign('u', $user);
290 $page->assign('sign', $this->getSignature());
291 $page->assign('personal_notes', $this->getPersonalNotes());
294 public function getText(array $user)
296 $page = new XorgPage();
297 $page->changeTpl('marketing/marketing.mail.tpl', NO_SKIN
);
298 $this->prepareText($page, $user);
302 public function process(array $user)
307 class ListMarketing
extends AnnuaireMarketing
311 public function __construct($data, $from, $personal_notes = null
)
313 list($this->name
, $this->domain
) = explode('@', $data);
314 if ($from && ($user = User
::getSilent($from))) {
315 $from = $user->fullName();
319 $this->titre
= "Un camarade solicite ton inscription à $data";
320 $this->intro
= "Polytechnique.org, l'annuaire des polytechniciens sur internet, "
321 . "fournit de nombreux services aux groupes X, ainsi que des listes "
322 . "de diffusion pour les X en faisant la demande.\n\n"
323 . "$from solicite ton inscription à la liste <$data>. "
324 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent "
325 . "profiter de l'ensemble de nos services, c'est pourquoi nous te "
326 . "proposons auparavant de t'inscrire sur notre site. Pour cela, il "
327 . "te suffit de visiter cette page ou de copier cette adresse dans "
328 . "la barre de ton navigateur :";
331 public function process(array $user)
333 return XDB
::execute("REPLACE INTO register_subs (uid, type, sub, domain)
334 VALUES ({?}, 'list', {?}, {?})",
335 $user['id'], $this->name
, $this->domain
);
339 class GroupMarketing
extends AnnuaireMarketing
342 public function __construct($data, $from, $personal_notes = null
)
344 $this->group
= $data;
345 if ($from && ($user = User
::getSilent($from))) {
346 $from = $user->fullName() . " vient";
350 $this->titre
= "Profite de ton inscription au groupe \"$data\" pour découvrir Polytechnique.org";
351 $this->intro
= "Polytechnique.org, l'annuaire des polytechniciens sur internet, fournit "
352 . "de nombreux services aux groupes X ( listes de diffusion, paiement en "
353 . "ligne, sites internet...), en particulier pour le groupe \"$data\"\n\n"
354 . "$from de t'inscrire dans l'annuaire du groupe \"$data\". "
355 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent profiter "
356 . "de l'ensemble de nos services, c'est pourquoi nous te proposons de "
357 . "t'inscrire sur notre site . Pour cela, il te suffit de visiter cette page "
358 . "ou de copier cette adresse dans la barre de ton navigateur :";
361 public function process(array $user)
363 return XDB
::execute("REPLACE INTO register_subs (uid, type, sub, domain)
364 VALUES ({?}, 'group', {?}, '')",
365 $user['id'], $this->group
);
369 /// Make AnnuaireMarketing to be the default message
370 class DefaultMarketing
extends AnnuaireMarketing
374 // vim:set et sw=4 sts=4 sws=4 enc=utf-8: