diff options
Diffstat (limited to 'phpBB/includes')
-rwxr-xr-x | phpBB/includes/emailer.php | 194 | ||||
-rw-r--r-- | phpBB/includes/functions_posting.php | 68 |
2 files changed, 225 insertions, 37 deletions
diff --git a/phpBB/includes/emailer.php b/phpBB/includes/emailer.php index a25735ca79..0f6a08d6de 100755 --- a/phpBB/includes/emailer.php +++ b/phpBB/includes/emailer.php @@ -24,11 +24,18 @@ class emailer var $msg, $subject, $extra_headers; var $to_addres, $cc_address, $bcc_address; var $reply_to, $from; + var $use_queue, $queue; var $tpl_msg = array(); - function emailer() + function emailer($use_queue = false) { + $this->use_queue = $use_queue; + if ($use_queue) + { + $this->queue = new Queue(); + $this->queue->init('emailer', 100); + } $this->reset(); } @@ -141,7 +148,7 @@ class emailer return false; } - // Escape all quotes, else the eval will fail. + // Escape all quotes, else the eval will fail. $this->msg = str_replace ("'", "\'", $this->msg); $this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->msg); @@ -202,7 +209,22 @@ class emailer $this->extra_headers = (($this->replyto !='') ? "Reply-to: <$this->replyto>\r\n" : '') . (($this->from != '') ? "From: <$this->from>\r\n" : "From: <" . $config['board_email'] . ">\r\n") . "Return-Path: <" . $config['board_email'] . ">\r\nMessage-ID: <" . md5(uniqid(time())) . "@" . $config['server_name'] . ">\r\nMIME-Version: 1.0\r\nContent-type: text/plain; charset=" . $this->encoding . "\r\nContent-transfer-encoding: 8bit\r\nDate: " . gmdate('D, d M Y H:i:s Z', time()) . "\r\nX-Priority: 3\r\nX-MSMail-Priority: Normal\r\nX-Mailer: PHP\r\n" . (($cc != '') ? "Cc:$cc\r\n" : '') . (($bcc != '') ? "Bcc:$bcc\r\n" : '') . trim($this->extra_headers); // Send message ... removed $this->encode() from subject for time being - $result = ($config['smtp_delivery']) ? smtpmail($to, $this->subject, $this->msg, $this->extra_headers) : mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\r\n", $this->msg), $this->extra_headers); + if (!$this->use_queue) + { + $result = ($config['smtp_delivery']) ? smtpmail($to, $this->subject, $this->msg, $this->extra_headers) : mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\r\n", $this->msg), $this->extra_headers); + } + else + { + $this->queue->put('emailer', array( + 'smtp_delivery' => $config['smtp_delivery'], + 'to' => $to, + 'subject' => $this->subject, + 'msg' => $this->msg, + 'extra_headers' => $this->extra_headers) + ); + + $result = true; + } // Did it work? if (!$result) @@ -432,4 +454,170 @@ function smtpmail($mail_to, $subject, $message, $headers = '') return TRUE; } +// This class is for handling queues - to be placed into another file ? +// At the moment it is only handling the email queue +class Queue +{ + var $data = array(); + var $queue_data = array(); + var $package_size = 0; + var $cache_file = ''; + + function Queue() + { + global $phpEx, $phpbb_root_path; + + $this->data = array(); + $this->cache_file = $phpbb_root_path . 'cache/queue.' . $phpEx; + } + + //--TEMP + function queue_filled() + { + if (file_exists($this->cache_file)) + { + return true; + } + + return false; + } + + function init($object, $package_size) + { + $this->data[$object] = array(); + $this->data[$object]['package_size'] = $package_size; + $this->data[$object]['data'] = array(); + } + + function put($object, $scope) + { + $this->data[$object]['data'][] = $scope; + } + + //--TEMP + function show() + { + echo ";<pre>"; + print_r($this->data); + echo "</pre>;"; + } + + function process() + { + global $_SERVER, $_ENV; + + if (file_exists($this->cache_file)) + { + include($this->cache_file); + } + + foreach ($this->queue_data as $object => $data_array) + { + $package_size = $data_array['package_size']; + + $num_items = (count($data_array['data']) < $package_size) ? count($data_array['data']) : $package_size; + + if ($object == 'emailer') + { + @set_time_limit(60); + } + + for ($i = 0; $i < $num_items; $i++) + { + foreach ($data_array['data'][0] as $var => $value) + { + $$var = $value; + } + + if ($object == 'emailer') + { + $result = ($smtp_delivery) ? smtpmail($to, $subject, $msg, $extra_headers) : mail($to, $subject, preg_replace("#(?<!\r)\n#s", "\r\n", $msg), $extra_headers); + + if (!$result) + { + $message = '<u>EMAIL ERROR</u> [ ' . (($smtp_delivery) ? 'SMTP' : 'PHP') . ' ]<br /><br />' . $result . '<br /><br /><u>CALLING PAGE</u><br /><br />' . ((!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']) . '<br />'; + trigger_error($message, E_USER_ERROR); + } + } + array_shift($this->queue_data[$object]['data']); + } + + if (count($this->queue_data[$object]['data']) == 0) + { + unset($this->queue_data[$object]); + } + } + + if (count($this->queue_data) == 0) + { + unlink($this->cache_file); + } + else + { + $file = '<?php $this->queue_data=' . $this->format_array($this->queue_data) . '; ?>'; + + if ($fp = @fopen($this->cache_file, 'wb')) + { + @flock($fp, LOCK_EX); + fwrite($fp, $file); + @flock($fp, LOCK_UN); + fclose($fp); + } + } + } + + function save() + { + if (file_exists($this->cache_file)) + { + include($this->cache_file); + + foreach ($this->queue_data as $object => $data_array) + { + if (count($this->data[$object])) + { + $this->data[$object]['data'] = array_merge($data_array['data'], $this->data[$object]['data']); + } + } + } + + $file = '<?php $this->queue_data=' . $this->format_array($this->data) . '; ?>'; + + if ($fp = @fopen($this->cache_file, 'wb')) + { + @flock($fp, LOCK_EX); + fwrite($fp, $file); + @flock($fp, LOCK_UN); + fclose($fp); + } + } + + // From acm_file.php + function format_array($array) + { + $lines = array(); + foreach ($array as $k => $v) + { + if (is_array($v)) + { + $lines[] = "'$k'=>" . $this->format_array($v); + } + elseif (is_int($v)) + { + $lines[] = "'$k'=>$v"; + } + elseif (is_bool($v)) + { + $lines[] = "'$k'=>" . (($v) ? 'TRUE' : 'FALSE'); + } + else + { + $lines[] = "'$k'=>'" . str_replace("'", "\'", str_replace('\\', '\\\\', $v)) . "'"; + } + } + return 'array(' . implode(',', $lines) . ')'; + } + +} + ?>
\ No newline at end of file diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php index b790d2831a..8e26cc00b3 100644 --- a/phpBB/includes/functions_posting.php +++ b/phpBB/includes/functions_posting.php @@ -1239,6 +1239,7 @@ function user_notification($mode, $subject, $forum_id, $topic_id, $post_id) if ($ids != '') { + // TODO: Paul - correct call to check f_read for specific users ? $sql = "SELECT a.user_id FROM " . ACL_OPTIONS_TABLE . " ao, " . ACL_USERS_TABLE . " a WHERE a.user_id IN (" . $ids . ") @@ -1288,24 +1289,24 @@ function user_notification($mode, $subject, $forum_id, $topic_id, $post_id) // if ($topic_notification) { - $sql = "SELECT u.user_id, u.username, u.user_email, u.user_lang, t.topic_title, f.forum_name - FROM " . TOPICS_WATCH_TABLE . " tw, " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . FORUMS_TABLE . " f - WHERE tw.topic_id = $topic_id - AND tw.user_id NOT IN ($sql_ignore_users) + $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, t.topic_title, f.forum_name + FROM ' . TOPICS_WATCH_TABLE . ' tw, ' . TOPICS_TABLE . ' t, ' . USERS_TABLE . ' u, ' . FORUMS_TABLE . ' f + WHERE tw.topic_id = ' . $topic_id . ' + AND tw.user_id NOT IN (' . $sql_ignore_users . ') AND tw.notify_status = 0 - AND f.forum_id = $forum_id + AND f.forum_id = ' . $forum_id . ' AND t.topic_id = tw.topic_id - AND u.user_id = tw.user_id"; + AND u.user_id = tw.user_id'; } else if ($newtopic_notification) { - $sql = "SELECT u.user_id, u.username, u.user_email, u.user_lang, f.forum_name - FROM " . USERS_TABLE . " u, " . FORUMS_WATCH_TABLE . " fw, " . FORUMS_TABLE . " f - WHERE fw.forum_id = $forum_id - AND fw.user_id NOT IN ($sql_ignore_users) + $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, f.forum_name + FROM ' . USERS_TABLE . ' u, ' . FORUMS_WATCH_TABLE . ' fw, ' . FORUMS_TABLE . ' f + WHERE fw.forum_id = ' . $forum_id . ' + AND fw.user_id NOT IN (' . $sql_ignore_users . ') AND fw.notify_status = 0 AND f.forum_id = fw.forum_id - AND u.user_id = fw.user_id"; + AND u.user_id = fw.user_id'; } else { @@ -1354,14 +1355,14 @@ function user_notification($mode, $subject, $forum_id, $topic_id, $post_id) $already_notified = ($update_watched_sql_topic == '') ? '' : $update_watched_sql_topic . ', '; $already_notified .= ($update_watched_sql_forum == '') ? '' : $update_watched_sql_forum . ', '; - $sql = "SELECT u.user_id, u.username, u.user_email, u.user_lang, t.topic_title, f.forum_name - FROM " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . FORUMS_WATCH_TABLE . " fw, " . FORUMS_TABLE . " f - WHERE fw.forum_id = $forum_id - AND fw.user_id NOT IN ($already_notified $sql_ignore_users) + $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, t.topic_title, f.forum_name + FROM ' . TOPICS_TABLE . ' t, ' . USERS_TABLE . ' u, ' . FORUMS_WATCH_TABLE . ' fw, ' . FORUMS_TABLE . ' f + WHERE fw.forum_id = ' . $forum_id . ' + AND fw.user_id NOT IN (' . $already_notified . ' ' . $sql_ignore_users . ') AND fw.notify_status = 0 - AND t.topic_id = $topic_id + AND t.topic_id = ' . $topic_id . ' AND f.forum_id = fw.forum_id - AND u.user_id = fw.user_id"; + AND u.user_id = fw.user_id'; $result = $db->sql_query($sql); if ($row = $db->sql_fetchrow($result)) @@ -1382,9 +1383,8 @@ function user_notification($mode, $subject, $forum_id, $topic_id, $post_id) } } - // We're going to try and minimise the number of emails we send by using bcc. - // The complication here is that different templates and/or localisations may - // be required so we need to account for these. + // We are using an email queue here, no emails are sent now, only queued. + // Returned to use the TO-Header, default package size is 100 (should be admin-definable) !? if (sizeof($email_users) && $config['email_enable']) { global $phpbb_root_path, $phpEx; @@ -1392,28 +1392,26 @@ function user_notification($mode, $subject, $forum_id, $topic_id, $post_id) @set_time_limit(60); include($phpbb_root_path . 'includes/emailer.'.$phpEx); - $emailer = new emailer(); + $emailer = new emailer(true); // use queue - $bcc_list_ary = array(); + $email_list_ary = array(); foreach ($email_users as $row) { - $pos = sizeof($bcc_list_ary[$row['email_template']][$row['user_lang']]); - $bcc_list_ary[$row['email_template']][$row['user_lang']][$pos]['email'] = $row['user_email']; - $bcc_list_ary[$row['email_template']][$row['user_lang']][$pos]['name'] = $row['username']; + $pos = sizeof($email_list_ary[$row['email_template']]); + $email_list_ary[$row['email_template']][$pos]['email'] = $row['user_email']; + $email_list_ary[$row['email_template']][$pos]['name'] = $row['username']; + $email_list_ary[$row['email_template']][$pos]['lang'] = $row['user_lang']; } unset($email_users); - foreach ($bcc_list_ary as $email_template => $bcc_list) + foreach ($email_list_ary as $email_template => $email_list) { - foreach ($bcc_list as $lang => $bcc) + foreach ($email_list as $addr) { - $emailer->template($email_template, $lang); + $emailer->template($email_template, $addr['lang']); $emailer->replyto($config['board_email']); - foreach ($bcc as $addr) - { - $emailer->bcc($addr['email'], $addr['name']); - } + $emailer->to($addr['email'], $addr['name']); $emailer->assign_vars(array( 'EMAIL_SIG' => str_replace('<br />', "\n", "-- \n" . $config['board_email_sig']), @@ -1431,9 +1429,11 @@ function user_notification($mode, $subject, $forum_id, $topic_id, $post_id) $emailer->reset(); } } + + $emailer->queue->save(); } - unset($bcc_list_ary); - + unset($email_list_ary); + if ($delete_users_topic != '') { $sql = "DELETE FROM " . TOPICS_WATCH_TABLE . " |