+class Marketing
+{
+ static private $engines = array(
+ //user name => array(class name, require data)
+ 'annuaire' => array('AnnuaireMarketing', false),
+ 'groupe' => array('GroupMarketing', true),
+ 'liste' => array('ListMarketing', true),
+ );
+
+ private $engine;
+ public $sender_mail;
+ public $user;
+
+ private $type;
+ private $data;
+ private $from;
+ private $sender;
+
+ private $hash = '';
+
+ public function __construct($uid, $email, $type, $data, $from, $sender = null)
+ {
+ $this->user = $this->getUser($uid, $email);
+ $this->sender_mail = $this->getFrom($from, $sender);
+ $this->engine =& $this->getEngine($type, $data, $from == 'user' ? $sender : null);
+
+ $this->type = $type;
+ $this->data = $data;
+ $this->from = $from;
+ $this->sender = $sender;
+ }
+
+ private function getUser($uid, $email)
+ {
+ require_once("xorg.misc.inc.php");
+ $res = XDB::query("SELECT FIND_IN_SET('femme', flags) AS sexe, nom, prenom, promo
+ FROM auth_user_md5
+ WHERE user_id = {?}", $uid);
+ if ($res->numRows() == 0) {
+ return null;
+ }
+ $user = $res->fetchOneAssoc();
+ $user['id'] = $uid;
+ $user['forlife'] = make_forlife($user['prenom'], $user['nom'], $user['promo']);
+ $user['mail'] = $email;
+ $user['to'] = '"' . $user['prenom'] . ' ' . $user['nom'] . '" <' . $email . '>';
+ return $user;
+ }
+
+ private function getFrom($from, $sender)
+ {
+ global $globals;
+
+ if ($from == 'staff') {
+ return '"L\'équipe de Polytechnique.org" <register@' . $globals->mail->domain . '>';
+ } else {
+ $res = XDB::query("SELECT u.nom, u.prenom, a.alias
+ FROM auth_user_md5 AS u
+ INNER JOIN aliases AS a ON (a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags))
+ WHERE u.user_id = {?}", $sender);
+ if (!$res->numRows()) {
+ return '"L\'équipe de Polytechnique.org" <register@' . $globals->mail->domain . '>';
+ }
+ $sender = $res->fetchOneAssoc();
+ return '"' . $sender['prenom'] . ' ' . $sender['nom'] . '" <' . $sender['alias'] . '@' . $globals->mail->domain . '>';
+ }
+ }
+
+ private function &getEngine($type, $data, $from)
+ {
+ $class = $type . 'Marketing';
+ if (!class_exists($class, false)) {
+ $class= 'DefaultMarketing';
+ }
+ $engine = new $class($data, $from);
+ if (!$engine instanceof MarketingEngine) {
+ $engine = null;
+ }
+ return $engine;
+ }
+
+ public function getTitle()
+ {
+ return $this->engine->getTitle();
+ }
+
+ public function getText()
+ {
+ return $this->engine->getText($this->user);
+ }
+
+ public function send($title = null, $text = null)
+ {
+ $this->hash = rand_url_id(12);
+ if (!$title) {
+ $title = $this->engine->getTitle();
+ }
+ if (!$text) {
+ $text = $this->engine->getText($this->user);
+ }
+ $sender = substr($this->sender_mail, 1, strpos($this->sender_mail, '"', 2)-1);
+ $text = str_replace(array("%%hash%%", "%%sender%%"),
+ array($this->hash, $this->sender_mail),
+ $text);
+ $mailer = new PlMailer();
+ $mailer->setFrom($this->sender_mail);
+ $mailer->addTo($this->user['mail']);
+ $mailer->setSubject($title);
+ $mailer->setTxtBody($text);
+ $mailer->send();
+ $this->incr();
+ }
+
+ public function add($valid = true)
+ {
+ XDB::execute('INSERT IGNORE INTO register_marketing
+ (uid, sender, email, date, last, nb, type, hash, message, message_data)
+ VALUES ({?}, {?}, {?}, NOW(), 0, 0, {?}, {?}, {?}, {?})',
+ $this->user['id'], $this->sender, $this->user['mail'], $this->from, $this->hash,
+ $this->type, $this->data);
+ $this->engine->process($this->user);
+ if ($valid) {
+ require_once 'validations.inc.php';
+ $valid = new MarkReq($this->sender, $this->user['id'], $this->user['mail'],
+ $this->from == 'user', $this->type, $this->data);
+ $valid->submit();
+ }
+ return true;
+ }
+
+ private function incr()
+ {
+ XDB::execute('UPDATE register_marketing
+ SET nb=nb+1, hash={?}, last=NOW()
+ WHERE uid={?} AND email={?}',
+ $this->hash, $this->user['id'], $this->user['mail']);
+ }
+
+ static public function getEngineList($exclude_data = true)
+ {
+ $array = array();
+ foreach (Marketing::$engines as $e => $d) {
+ if (!$d[1] || !$exclude_data) {
+ $array[] = $e;
+ }
+ }
+ return $array;
+ }
+
+ static public function get($uid, $email, $recentOnly = false)
+ {
+ $res = XDB::query("SELECT uid, email, message, message_data, type, sender
+ FROM register_marketing
+ WHERE uid = {?}
+ AND email = {?}".(
+ $recentOnly ? ' AND DATEDIFF(NOW(), last) < 30' : ''), $uid, $email);
+
+ if ($res->numRows() == 0) {
+ return null;
+ }
+ list ($uid, $email, $type, $data, $from, $sender) = $res->fetchOneRow();
+ return new Marketing($uid, $email, $type, $data, $from, $sender);
+ }
+
+ static public function clear($uid, $email = null)
+ {
+ if (!$email) {
+ XDB::execute("DELETE FROM register_marketing WHERE uid = {?}", $uid);
+ } else {
+ XDB::execute("DELETE FROM register_marketing WHERE uid = {?} AND email = {?}", $uid, $email);
+ }
+ }
+
+ static public function relance($uid, $nbx = -1)
+ {
+ global $globals;
+
+ if ($nbx < 0) {
+ $res = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE deces=0");
+ $nbx = $res->fetchOneCell();
+ }
+
+ $res = XDB::query("SELECT r.date, u.promo, u.nom, u.prenom, r.email, r.bestalias
+ FROM register_pending AS r
+ INNER JOIN auth_user_md5 AS u ON u.user_id = r.uid
+ WHERE hash!='INSCRIT' AND uid={?} AND TO_DAYS(relance) < TO_DAYS(NOW())", $uid);
+ if (!list($date, $promo, $nom, $prenom, $email, $alias) = $res->fetchOneRow()) {
+ return false;
+ }