_tpl = $tpl; $this->_css = $css; $this->_prefix = $prefix; $this->_table = $tbl; $this->_subscriptionTable = $stbl; } public function id() { return is_null($this->_shortname) ? $this->_id : $this->_shortname; } private function selectId($where) { $res = XDB::query("SELECT IF (n.short_name IS NULL, n.id, n.short_name) FROM {$this->_table} AS n WHERE n.bits != 'new' AND {$where} LIMIT 1"); if ($res->numRows() != 1) { return null; } return $res->fetchOneCell(); } public function prev() { static $val; if (!isset($val)) { $val = $this->selectId("n.id < {$this->_id} ORDER BY n.id DESC"); } return $val; } public function next() { static $val; if (!isset($val)) { $val = $this->selectId("n.id > {$this->_id} ORDER BY n.id"); } return $val; } public function last() { static $val; if (!isset($val)) { $res = XDB::query("SELECT MAX(n.id) FROM {$this->_table} AS n WHERE n.bits != 'new' AND n.id > {?}", $this->_id); if ($res->numRows() != 1) { $val = null; } else { $val = $res->fetchOneCell(); } } return $val; } public function title($mail = false) { return $mail ? $this->_title_mail : $this->_title; } public function head($user = null, $type = 'text') { if (is_null($user)) { return $this->_head; } else { $head = $this->_head; $head = str_replace('', $user->isFemale() ? 'Chère' : 'Cher', $head); $head = str_replace('', $user->displayName(), $head); $head = str_replace('', '', $head); return format_text($head, $type, 2, 64); } } 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('@/\*.*?\*/@us', '', $css); } } public function toText(&$page, $user) { $this->css($page); $page->assign('is_mail', false); $page->assign('mail_part', 'text'); $page->assign('user', $user); $this->assignData($page); } public function toHtml(&$page, $user) { $this->css($page); $page->assign('prefix', $this->_prefix . '/' . $this->id()); $page->assign('is_mail', false); $page->assign('mail_part', 'html'); $page->assign('user', $user); $this->assignData($page); } private function createHash($line, $key = null) { $hash = implode(time(), $line) . rand(); $hash = md5($hash); return $hash; } public function sendTo($user, $hash = null) { if (is_null($hash)) { $hash = XDB::fetchOneCell("SELECT hash FROM {$this->_subscriptionTable} WHERE uid = {?}", $user->id()); } if (is_null($hash)) { $hash = $this->createHash(array($user->displayName(), $user->fullName(), $user->isFemale(), $user->isEmailFormatHtml(), rand(), "X.org rulez")); XDB::execute("UPDATE {$this->_subscriptionTable} as ni SET ni.hash = {?} WHERE ni.uid != {?}", $hash, $user->id()); } $mailer = new PlMailer($this->_tpl); $this->assignData($mailer); $mailer->assign('is_mail', true); $mailer->assign('user', $user); $mailer->assign('prefix', null); $mailer->assign('hash', $hash); $mailer->addTo('"' . $user->fullName() . '" <' . $user->bestEmail() . '>'); $mailer->send($user->isEmailFormatHtml()); } protected function getAllRecipients() { global $globals; return "SELECT a.uid FROM {$this->_subscriptionTable} AS ni INNER JOIN accounts AS a ON (ni.uid = a.uid) LEFT JOIN email_options AS eo ON (eo.uid = a.uid) LEFT JOIN emails AS e ON (e.uid = a.uid AND e.flags='active') WHERE ni.last < {?} AND ({$this->subscriptionWhere()}) AND (e.email IS NOT NULL OR FIND_IN_SET('googleapps', eo.storage)) GROUP BY a.uid"; } public function sendToAll() { $this->setSent(); $query = XDB::format($this->getAllRecipients(), $this->id()) . ' LIMIT 60'; while (true) { $users = User::getBulkUsersWithUIDs(XDB::fetchColumn($query)); if (count($users) == 0) { return; } foreach ($users as $user) { $sent[] = XDB::format('uid = {?}', $user->id()); $this->sendTo($user, $hash); } XDB::execute("UPDATE {$this->_subscriptionTable} SET last = {?} WHERE " . implode(' OR ', $sent), $this->_id); sleep(60); } } abstract protected function assignData(&$smarty); abstract protected function setSent(); abstract protected function subscriptionWhere(); } // }}} // {{{ Functions function format_text($input, $format, $indent = 0, $width = 68) { if ($format == 'text') { return MiniWiki::WikiToText($input, true, $indent, $width, "title"); } return MiniWiki::WikiToHTML($input, "title"); } // function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68) // }}} // vim:set et sw=4 sts=4 sws=4 enc=utf-8: ?>