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