+// {{{ class MassMailer
+
+abstract class MassMailer
+{
+ private $_tpl;
+ private $_css;
+ private $_prefix;
+
+ public $_id;
+ public $_shortname;
+ public $_title;
+ public $_title_mail;
+
+ public $_head;
+
+ function __construct($tpl, $css, $prefix)
+ {
+ $this->_tpl = $tpl;
+ $this->_css = $css;
+ $this->_prefix = $prefix;
+ }
+
+ public function id()
+ {
+ return is_null($this->_shortname) ? $this->_id : $this->_shortname;
+ }
+
+ public function title($mail = false)
+ {
+ return $mail ? $this->_title_mail : $this->_title;
+ }
+
+ public function head($prenom = null, $nom = null, $sexe = null, $type = 'text')
+ {
+ if (is_null($prenom)) {
+ return $this->_head;
+ } else {
+ $head = $this->_head;
+ $head = str_replace('<cher>', $sexe ? 'Chère' : 'Cher', $head);
+ $head = str_replace('<prenom>', $prenom, $head);
+ $head = str_replace('<nom>', $nom, $head);
+ if ($type == 'text') {
+ $head = enriched_to_text($head, false, true, 2, 64);
+ } else {
+ $head = enriched_to_text($head, true);
+ }
+ return $head;
+ }
+ }
+
+ public function css(&$page = null)
+ {
+ if (!is_null($page)) {
+ $page->addCssLink($this->_css);
+ return true;
+ } else {
+ $css = file_get_contents(dirname(__FILE__) . '/../htdocs/css/' . $this->_css);
+ return preg_replace('@/\*.*?\*/@s', '', $css);
+ }
+ }
+
+ public function toText(&$page, $prenom, $nom, $sexe)
+ {
+ $this->css($page);
+ $page->assign('is_mail', false);
+ $page->assign('html_version', false);
+ $page->assign('prenom', $prenom);
+ $page->assign('nom', $nom);
+ $page->assign('sexe', $sexe);
+ $this->assignData($page);
+ }
+
+ public function toHtml(&$page, $prenom, $nom, $sexe)
+ {
+ $this->css($page);
+ $page->assign('prefix', $this->_prefix . '/' . $this->id());
+ $page->assign('is_mail', false);
+ $page->assign('html_version', true);
+ $page->assign('prenom', $prenom);
+ $page->assign('nom', $nom);
+ $page->assign('sexe', $sexe);
+ $this->assignData($page);
+ }
+
+ public function sendTo($prenom, $nom, $login, $sexe, $html)
+ {
+ global $globals;
+
+ $mailer = new PlMailer($this->_tpl);
+ $this->assignData($mailer);
+ $mailer->assign('is_mail', true);
+ $mailer->assign('prenom', $prenom);
+ $mailer->assign('nom', $nom);
+ $mailer->assign('sexe', $sexe);
+ $mailer->assign('prefix', null);
+ $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
+ $mailer->send($html);
+ }
+
+ public function sendToAll()
+ {
+ $this->setSent();
+ $query = "SELECT u.user_id, a.alias,
+ u.prenom, IF(u.nom_usage='', u.nom, u.nom_usage),
+ FIND_IN_SET('femme', u.flags),
+ q.core_mail_fmt AS pref
+ FROM {$this->subscriptionTable()} AS ni
+ INNER JOIN auth_user_md5 AS u USING(user_id)
+ INNER JOIN auth_user_quick AS q ON(q.user_id = u.user_id)
+ INNER JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
+ LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active')
+ WHERE ({$this->subscriptionWhere()}) AND e.email IS NOT NULL
+ GROUP BY u.user_id
+ LIMIT 60";
+ while (true) {
+ $res = XDB::iterRow($query);
+ if (!$res->total()) {
+ exit;
+ }
+ $sent = array();
+ while (list($uid, $bestalias, $prenom, $nom, $sexe, $fmt) = $res->next()) {
+ $sent[] = "user_id='$uid'";
+ $this->sendTo($prenom, $nom, $bestalias, $sexe, $fmt=='html');
+ }
+ XDB::execute("UPDATE {$this->subscriptionTable()}
+ SET {$this->subscriptionUpdate()}
+ WHERE " . implode(' OR ', $sent));
+ sleep(60);
+ }
+ }
+
+ abstract protected function assignData(&$smarty);
+ abstract protected function setSent();
+ abstract static public function subscribe($uid = -1);
+ abstract static public function unsubscribe($uid = -1);
+ abstract static public function subscriptionState($uid = -1);
+
+ abstract protected function subscriptionTable();
+ abstract protected function subscriptionWhere();
+ abstract protected function subscriptionUpdate();
+}
+
+// }}}