123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888 |
- <?php
- defined('GNUSOCIAL') || die();
- require_once 'Mail.php';
- function mail_backend()
- {
- static $backend = null;
- global $_PEAR;
- if (!$backend) {
- $mail = new Mail();
- $backend = $mail->factory(
- common_config('mail', 'backend'),
- common_config('mail', 'params') ?: []
- );
- if ($_PEAR->isError($backend)) {
- throw new EmailException($backend->getMessage(), $backend->getCode());
- }
- }
- return $backend;
- }
- function mail_send($recipients, $headers, $body)
- {
- global $_PEAR;
- try {
-
- $backend = mail_backend();
- if (!isset($headers['Content-Type'])) {
- $headers['Content-Type'] = 'text/plain; charset=UTF-8';
- }
- assert($backend);
- $sent = $backend->send($recipients, $headers, $body);
- if ($_PEAR->isError($sent)) {
- throw new EmailException($sent->getMessage(), $sent->getCode());
- }
- return true;
- } catch (PEAR_Exception $e) {
- common_log(
- LOG_ERR,
- "Unable to send email - '{$e->getMessage()}'. "
- . 'Is your mail subsystem set up correctly?'
- );
- return false;
- }
- }
- function mail_domain()
- {
- $maildomain = common_config('mail', 'domain');
- if (!$maildomain) {
- $maildomain = common_config('site', 'server');
- }
- return $maildomain;
- }
- function mail_notify_from()
- {
- $notifyfrom = common_config('mail', 'notifyfrom');
- if (!$notifyfrom) {
- $domain = mail_domain();
- $notifyfrom = '"'. str_replace('"', '\\"', common_config('site', 'name')) .'" <noreply@'.$domain.'>';
- }
- return $notifyfrom;
- }
- function mail_to_user($user, $subject, $body, $headers=array(), $address=null)
- {
- if (!$address) {
- $address = $user->email;
- }
- $recipients = $address;
- $profile = $user->getProfile();
- $headers['Date'] = date("r", time());
- $headers['From'] = mail_notify_from();
- $headers['To'] = $profile->getBestName() . ' <' . $address . '>';
- $headers['Subject'] = $subject;
- return mail_send($recipients, $headers, $body);
- }
- function mail_subscribe_notify($listenee, $listener)
- {
- $other = $listener->getProfile();
- mail_subscribe_notify_profile($listenee, $other);
- }
- function mail_subscribe_notify_profile($listenee, $other)
- {
- if ($other->hasRight(Right::EMAILONSUBSCRIBE) &&
- $listenee->email && $listenee->emailnotifysub) {
- $profile = $listenee->getProfile();
- $name = $profile->getBestName();
- $long_name = $other->getFancyName();
- $recipients = $listenee->email;
-
- common_switch_locale($listenee->language);
- $headers = _mail_prepare_headers('subscribe', $listenee->nickname, $other->nickname);
- $headers['From'] = mail_notify_from();
- $headers['To'] = $name . ' <' . $listenee->email . '>';
-
-
- $headers['Subject'] = sprintf(
- _('%1$s is now following you on %2$s.'),
- $other->getBestName(),
- common_config('site', 'name')
- );
-
-
- $body = sprintf(
- _('%1$s is now following you on %2$s.'),
- $long_name,
- common_config('site', 'name')
- ) .
- mail_profile_block($other) .
- mail_footer_block();
-
- common_switch_locale();
- mail_send($recipients, $headers, $body);
- }
- }
- function mail_subscribe_pending_notify_profile($listenee, $other)
- {
- if ($other->hasRight(Right::EMAILONSUBSCRIBE) &&
- $listenee->email && $listenee->emailnotifysub) {
- $profile = $listenee->getProfile();
- $name = $profile->getBestName();
- $long_name = ($other->fullname) ?
- ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
- $recipients = $listenee->email;
-
- common_switch_locale($listenee->language);
- $headers = _mail_prepare_headers('subscribe', $listenee->nickname, $other->nickname);
- $headers['From'] = mail_notify_from();
- $headers['To'] = $name . ' <' . $listenee->email . '>';
-
-
- $headers['Subject'] = sprintf(
- _('%1$s would like to listen to '.
- 'your notices on %2$s.'),
- $other->getBestName(),
- common_config('site', 'name')
- );
-
-
- $body = sprintf(
- _('%1$s would like to listen to your notices on %2$s. ' .
- 'You may approve or reject their subscription at %3$s'),
- $long_name,
- common_config('site', 'name'),
- common_local_url('subqueue', ['nickname' => $listenee->nickname])
- ) .
- mail_profile_block($other) .
- mail_footer_block();
-
- common_switch_locale();
- mail_send($recipients, $headers, $body);
- }
- }
- function mail_footer_block()
- {
-
-
-
- return "\n\n" . sprintf(
- _('Faithfully yours,'.
- "\n".'%1$s.'."\n\n".
- "----\n".
- "Change your email address or ".
- "notification options at ".'%2$s'),
- common_config('site', 'name'),
- common_local_url('emailsettings')
- ) . "\n";
- }
- function mail_profile_block($profile)
- {
-
-
-
- $out = array();
- $out[] = "";
- $out[] = "";
-
-
- $out[] = sprintf(_("Profile: %s"), $profile->profileurl);
- if ($profile->location) {
-
-
- $out[] = sprintf(_("Location: %s"), $profile->location);
- }
- if ($profile->homepage) {
-
-
- $out[] = sprintf(_("Homepage: %s"), $profile->homepage);
- }
- if ($profile->bio) {
-
-
- $out[] = sprintf(_("Bio: %s"), $profile->bio);
- }
- $blocklink = common_local_url('block', array('profileid' => $profile->id));
-
-
- Event::handle('MailProfileInfoBlockLink', array($profile, &$blocklink));
-
-
- $out[] = sprintf(
- _('If you believe this account is being used abusively, ' .
- 'you can block them from your subscribers list and ' .
- 'report as spam to site administrators at %s.'),
- $blocklink
- );
- $out[] = "";
- return implode("\n", $out);
- }
- function mail_new_incoming_notify($user)
- {
- $profile = $user->getProfile();
- $name = $profile->getBestName();
- $headers['From'] = $user->incomingemail;
- $headers['To'] = $name . ' <' . $user->email . '>';
-
-
- $headers['Subject'] = sprintf(
- _('New email address for posting to %s'),
- common_config('site', 'name')
- );
-
-
-
- $body = sprintf(
- _("You have a new posting address on %1\$s.\n\n".
- "Send email to %2\$s to post new messages.\n\n".
- "More email instructions at %3\$s."),
- common_config('site', 'name'),
- $user->incomingemail,
- common_local_url('doc', ['title' => 'email'])
- ) .
- mail_footer_block();
- mail_send($user->email, $headers, $body);
- }
- function mail_new_incoming_address()
- {
- $prefix = common_confirmation_code(64);
- $suffix = mail_domain();
- return $prefix . '@' . $suffix;
- }
- function mail_broadcast_notice_sms($notice)
- {
-
- $user = new User();
- $replies = $notice->getReplies();
- $repliesQry = '';
- if (!empty($replies)) {
- $repliesQry = sprintf(
- 'OR %s.id IN (%s)',
- $user->escapedTableName(),
- implode(',', $replies)
- );
- }
- $user->query(sprintf(
- 'SELECT nickname, smsemail, incomingemail ' .
- 'FROM %1$s LEFT JOIN subscription ' .
- 'ON %1$s.id = subscription.subscriber ' .
- 'AND subscription.subscribed = %2$d ' .
- 'AND subscription.subscribed <> subscription.subscriber ' .
-
- 'WHERE %1$s.id <> %2$d ' .
- 'AND %1$s.smsemail IS NOT NULL ' .
- 'AND %1$s.smsnotify = TRUE ' .
-
-
-
- 'AND (subscription.sms = TRUE ' .
-
-
- $repliesQry .
- ')',
- $user->escapedTableName(),
- $notice->profile_id
- ));
- while ($user->fetch()) {
- common_log(
- LOG_INFO,
- 'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
- __FILE__
- );
- $success = mail_send_sms_notice_address(
- $notice,
- $user->smsemail,
- $user->incomingemail,
- $user->nickname
- );
- if (!$success) {
-
- common_log(
- LOG_WARNING,
- 'Sending notice ' . $notice->id . ' to ' . $user->smsemail . ' FAILED, canceling.',
- __FILE__
- );
- return false;
- }
- }
- $user->free();
- unset($user);
- return true;
- }
- function mail_send_sms_notice($notice, $user)
- {
- return mail_send_sms_notice_address(
- $notice,
- $user->smsemail,
- $user->incomingemail,
- $user->nickname
- );
- }
- function mail_send_sms_notice_address($notice, $smsemail, $incomingemail, $nickname)
- {
- $to = $nickname . ' <' . $smsemail . '>';
- $other = $notice->getProfile();
- common_log(LOG_INFO, 'Sending notice ' . $notice->id .
- ' to ' . $smsemail, __FILE__);
- $headers = array();
- $headers['From'] = ($incomingemail) ? $incomingemail : mail_notify_from();
- $headers['To'] = $to;
-
-
- $headers['Subject'] = sprintf(_('%s status'), $other->getBestName());
- $body = $notice->content;
- return mail_send($smsemail, $headers, $body);
- }
- function mail_confirm_sms($code, $nickname, $address)
- {
- $recipients = $address;
- $headers['From'] = mail_notify_from();
- $headers['To'] = $nickname . ' <' . $address . '>';
-
- $headers['Subject'] = _('SMS confirmation');
-
-
- $body = sprintf(_('%s: confirm you own this phone number with this code:'), $nickname);
- $body .= "\n\n";
- $body .= $code;
- $body .= "\n\n";
- mail_send($recipients, $headers, $body);
- }
- function mail_notify_nudge($from, $to)
- {
- common_switch_locale($to->language);
-
-
- $subject = sprintf(_('You have been nudged by %s'), $from->nickname);
- $from_profile = $from->getProfile();
-
-
-
- $body = sprintf(
- _('%1$s (%2$s) is wondering what you are up to ' .
- "these days and is inviting you to post some news.\n\n" .
- "So let's hear from you :)\n\n" .
- "%3\$s\n\n" .
- "Don't reply to this email; it won't get to them."),
- $from_profile->getBestName(),
- $from->nickname,
- common_local_url('all', ['nickname' => $to->nickname])
- ) .
- mail_footer_block();
- common_switch_locale();
- $headers = _mail_prepare_headers('nudge', $to->nickname, $from->nickname);
- return mail_to_user($to, $subject, $body, $headers);
- }
- function mail_notify_message(Notice $message, Profile $from = null, ?array $to = null)
- {
- if (is_null($from)) {
- $from = $message->getProfile();
- }
- if (is_null($to)) {
- $to = [];
- foreach ($message->getAttentionProfiles() as $attention) {
- if ($attention->isLocal()) {
- $to[] = $attention;
- }
- }
- }
- $success = true;
- foreach ($to as $t) {
- if (is_null($t->email) || !$t->emailnotifymsg) {
- continue;
- }
- common_switch_locale($t->language);
-
-
- $subject = sprintf(_('New private message from %s'), $from->getNickname());
-
-
-
- $body = sprintf(
- _("%1\$s (%2\$s) sent you a private message:\n\n".
- "------------------------------------------------------\n".
- "%3\$s\n".
- "------------------------------------------------------\n\n".
- "You can reply to their message here:\n\n".
- "%4\$s\n\n".
- "Don't reply to this email; it won't get to them."),
- $from->getBestName(),
- $from->getNickname(),
- $message->getContent(),
- common_local_url('newmessage', ['to' => $from->getID()])
- ) .
- mail_footer_block();
- $headers = _mail_prepare_headers('message', $t->getNickname(), $from->getNickname());
- common_switch_locale();
- if (!mail_to_user($t, $subject, $body, $headers)) {
- common_log(LOG_ERR, "Failed to notify user:{$t->getID()} about the new message:{$message->getID()} sent by user:{$from->getID()}");
- $success = false;
- }
- }
- return $success;
- }
- function mail_notify_attn(Profile $rcpt, Notice $notice)
- {
- if (!$rcpt->isLocal()) {
- return;
- }
- $sender = $notice->getProfile();
- if ($rcpt->sameAs($sender)) {
- return;
- }
-
- if (!$sender->hasRight(Right::EMAILONREPLY)) {
- return;
- }
-
- if ($rcpt->hasBlocked($sender)) {
- return;
- }
- $user = $rcpt->getUser();
- if (!$user->receivesEmailNotifications()) {
- return;
- }
- common_switch_locale($user->language);
- if ($notice->hasConversation()) {
- $conversationUrl = common_local_url(
- 'conversation',
- ['id' => $notice->conversation]
- ) . '#notice-'.$notice->getID();
-
- $conversationEmailText = sprintf(_("The full conversation can be read here:\n\n".
- "\t%s"), $conversationUrl) . "\n\n";
- } else {
- $conversationEmailText = '';
- }
-
-
- $subject = sprintf(_('%1$s sent a notice to your attention'), $sender->getFancyName());
-
-
-
-
-
- $body = sprintf(
- _("%1\$s just sent a notice to your attention (an '@-reply') on %2\$s.\n\n".
- "The notice is here:\n\n".
- "\t%3\$s\n\n" .
- "It reads:\n\n".
- "\t%4\$s\n\n" .
- "%5\$s" .
- "You can reply back here:\n\n".
- "\t%6\$s\n\n" .
- "The list of all @-replies for you here:\n\n" .
- "%7\$s"),
- $sender->getFancyName(),
- common_config('site', 'name'),
- common_local_url(
- 'shownotice',
- ['notice' => $notice->getID()]
- ),
- $notice->getContent(),
- $conversationEmailText,
- common_local_url(
- 'newnotice',
- ['replyto' => $sender->getNickname(), 'inreplyto' => $notice->getID()]
- ),
- common_local_url(
- 'replies',
- ['nickname' => $rcpt->getNickname()]
- )
- ) .
- mail_footer_block();
- $headers = _mail_prepare_headers('mention', $rcpt->getNickname(), $sender->getNickname());
- common_switch_locale();
- mail_to_user($user, $subject, $body, $headers);
- }
- function _mail_prepare_headers($msg_type, $to, $from)
- {
- $headers = array(
- 'X-StatusNet-MessageType' => $msg_type,
- 'X-StatusNet-TargetUser' => $to,
- 'X-StatusNet-SourceUser' => $from,
- 'X-StatusNet-Domain' => common_config('site', 'server')
- );
- return $headers;
- }
- function mail_notify_group_join($group, $joiner)
- {
-
- $admin = $group->getAdmins();
- while ($admin->fetch()) {
-
- $adminUser = User::getKV('id', $admin->id);
-
- if ($adminUser && $adminUser->email) {
-
- common_switch_locale($adminUser->language);
- $headers = _mail_prepare_headers('join', $admin->nickname, $joiner->nickname);
- $headers['From'] = mail_notify_from();
- $headers['To'] = $admin->getBestName() . ' <' . $adminUser->email . '>';
-
-
- $headers['Subject'] = sprintf(
- _('%1$s has joined your group %2$s on %3$s'),
- $joiner->getBestName(),
- $group->getBestName(),
- common_config('site', 'name')
- );
-
-
-
-
- $body = sprintf(
- _('%1$s has joined your group %2$s on %3$s.'),
- $joiner->getFancyName(),
- $group->getFancyName(),
- common_config('site', 'name')
- ) .
- mail_profile_block($joiner) .
- mail_footer_block();
-
- common_switch_locale();
- mail_send($adminUser->email, $headers, $body);
- }
- }
- }
- function mail_notify_group_join_pending($group, $joiner)
- {
- $admin = $group->getAdmins();
- while ($admin->fetch()) {
-
- $adminUser = User::getKV('id', $admin->id);
-
- if ($adminUser && $adminUser->email) {
-
- common_switch_locale($adminUser->language);
- $headers = _mail_prepare_headers('join', $admin->nickname, $joiner->nickname);
- $headers['From'] = mail_notify_from();
- $headers['To'] = $admin->getBestName() . ' <' . $adminUser->email . '>';
-
-
- $headers['Subject'] = sprintf(
- _('%1$s wants to join your group %2$s on %3$s.'),
- $joiner->getBestName(),
- $group->getBestName(),
- common_config('site', 'name')
- );
-
-
-
- $body = sprintf(
- _('%1$s would like to join your group %2$s on %3$s. ' .
- 'You may approve or reject their group membership at %4$s'),
- $joiner->getFancyName(),
- $group->getFancyName(),
- common_config('site', 'name'),
- common_local_url('groupqueue', ['nickname' => $group->nickname])
- ) .
- mail_profile_block($joiner) .
- mail_footer_block();
-
- common_switch_locale();
- mail_send($adminUser->email, $headers, $body);
- }
- }
- }
|