]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mail.php
a9c94f2f9679b5aa97f4b038a06cb3a8708c0961
[quix0rs-gnu-social.git] / lib / mail.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * utilities for sending email
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Mail
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @author    Robin Millette <millette@status.net>
27  * @author    Sarven Capadisli <csarven@status.net>
28  * @copyright 2008 StatusNet, Inc.
29  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30  * @link      http://status.net/
31  */
32
33 if (!defined('LACONICA')) {
34     exit(1);
35 }
36
37 require_once 'Mail.php';
38
39 /**
40  * return the configured mail backend
41  *
42  * Uses the $config array to make a mail backend. Cached so it is safe to call
43  * more than once.
44  *
45  * @return Mail backend
46  */
47
48 function mail_backend()
49 {
50     static $backend = null;
51
52     if (!$backend) {
53         $backend = Mail::factory(common_config('mail', 'backend'),
54                                  (common_config('mail', 'params')) ?
55                                  common_config('mail', 'params') :
56                                  array());
57         if (PEAR::isError($backend)) {
58             common_server_error($backend->getMessage(), 500);
59         }
60     }
61     return $backend;
62 }
63
64 /**
65  * send an email to one or more recipients
66  *
67  * @param array  $recipients array of strings with email addresses of recipients
68  * @param array  $headers    array mapping strings to strings for email headers
69  * @param string $body       body of the email
70  *
71  * @return boolean success flag
72  */
73
74 function mail_send($recipients, $headers, $body)
75 {
76     // XXX: use Mail_Queue... maybe
77     $backend = mail_backend();
78     if (!isset($headers['Content-Type'])) {
79         $headers['Content-Type'] = 'text/plain; charset=UTF-8';
80     }
81     assert($backend); // throws an error if it's bad
82     $sent = $backend->send($recipients, $headers, $body);
83     if (PEAR::isError($sent)) {
84         common_log(LOG_ERR, 'Email error: ' . $sent->getMessage());
85         return false;
86     }
87     return true;
88 }
89
90 /**
91  * returns the configured mail domain
92  *
93  * Defaults to the server name.
94  *
95  * @return string mail domain, suitable for making email addresses.
96  */
97
98 function mail_domain()
99 {
100     $maildomain = common_config('mail', 'domain');
101     if (!$maildomain) {
102         $maildomain = common_config('site', 'server');
103     }
104     return $maildomain;
105 }
106
107 /**
108  * returns a good address for sending email from this server
109  *
110  * Uses either the configured value or a faked-up value made
111  * from the mail domain.
112  *
113  * @return string notify from address
114  */
115
116 function mail_notify_from()
117 {
118     $notifyfrom = common_config('mail', 'notifyfrom');
119
120     if (!$notifyfrom) {
121
122         $domain = mail_domain();
123
124         $notifyfrom = '"'.common_config('site', 'name') .'" <noreply@'.$domain.'>';
125     }
126
127     return $notifyfrom;
128 }
129
130 /**
131  * sends email to a user
132  *
133  * @param User   &$user   user to send email to
134  * @param string $subject subject of the email
135  * @param string $body    body of the email
136  * @param string $address optional specification of email address
137  *
138  * @return boolean success flag
139  */
140
141 function mail_to_user(&$user, $subject, $body, $address=null)
142 {
143     if (!$address) {
144         $address = $user->email;
145     }
146
147     $recipients = $address;
148     $profile    = $user->getProfile();
149
150     $headers['From']    = mail_notify_from();
151     $headers['To']      = $profile->getBestName() . ' <' . $address . '>';
152     $headers['Subject'] = $subject;
153
154     return mail_send($recipients, $headers, $body);
155 }
156
157 /**
158  * Send an email to confirm a user's control of an email address
159  *
160  * @param User   $user     User claiming the email address
161  * @param string $code     Confirmation code
162  * @param string $nickname Nickname of user
163  * @param string $address  email address to confirm
164  *
165  * @see common_confirmation_code()
166  *
167  * @return success flag
168  */
169
170 function mail_confirm_address($user, $code, $nickname, $address)
171 {
172     $subject = _('Email address confirmation');
173
174     $body = sprintf(_("Hey, %s.\n\n".
175                       "Someone just entered this email address on %s.\n\n" .
176                       "If it was you, and you want to confirm your entry, ".
177                       "use the URL below:\n\n\t%s\n\n" .
178                       "If not, just ignore this message.\n\n".
179                       "Thanks for your time, \n%s\n"),
180                     $nickname, common_config('site', 'name'),
181                     common_local_url('confirmaddress', array('code' => $code)),
182                     common_config('site', 'name'));
183     return mail_to_user($user, $subject, $body, $address);
184 }
185
186 /**
187  * notify a user of subscription by another user
188  *
189  * This is just a wrapper around the profile-based version.
190  *
191  * @param User $listenee user who is being subscribed to
192  * @param User $listener user who is subscribing
193  *
194  * @see mail_subscribe_notify_profile()
195  *
196  * @return void
197  */
198
199 function mail_subscribe_notify($listenee, $listener)
200 {
201     $other = $listener->getProfile();
202     mail_subscribe_notify_profile($listenee, $other);
203 }
204
205 /**
206  * notify a user of subscription by a profile (remote or local)
207  *
208  * This function checks to see if the listenee has an email
209  * address and wants subscription notices.
210  *
211  * @param User    $listenee user who's being subscribed to
212  * @param Profile $other    profile of person who's listening
213  *
214  * @return void
215  */
216
217 function mail_subscribe_notify_profile($listenee, $other)
218 {
219     if ($listenee->email && $listenee->emailnotifysub) {
220
221         // use the recipient's localization
222         common_init_locale($listenee->language);
223
224         $profile = $listenee->getProfile();
225
226         $name = $profile->getBestName();
227
228         $long_name = ($other->fullname) ?
229           ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
230
231         $recipients = $listenee->email;
232
233         $headers['From']    = mail_notify_from();
234         $headers['To']      = $name . ' <' . $listenee->email . '>';
235         $headers['Subject'] = sprintf(_('%1$s is now listening to '.
236                                         'your notices on %2$s.'),
237                                       $other->getBestName(),
238                                       common_config('site', 'name'));
239
240         $body = sprintf(_('%1$s is now listening to your notices on %2$s.'."\n\n".
241                           "\t".'%3$s'."\n\n".
242                           '%4$s'.
243                           '%5$s'.
244                           '%6$s'.
245                           "\n".'Faithfully yours,'."\n".'%7$s.'."\n\n".
246                           "----\n".
247                           "Change your email address or ".
248                           "notification options at ".'%8$s' ."\n"),
249                         $long_name,
250                         common_config('site', 'name'),
251                         $other->profileurl,
252                         ($other->location) ?
253                         sprintf(_("Location: %s\n"), $other->location) : '',
254                         ($other->homepage) ?
255                         sprintf(_("Homepage: %s\n"), $other->homepage) : '',
256                         ($other->bio) ?
257                         sprintf(_("Bio: %s\n\n"), $other->bio) : '',
258                         common_config('site', 'name'),
259                         common_local_url('emailsettings'));
260
261         // reset localization
262         common_init_locale();
263         mail_send($recipients, $headers, $body);
264     }
265 }
266
267 /**
268  * notify a user of their new incoming email address
269  *
270  * User's email and incoming fields should already be updated.
271  *
272  * @param User $user user with the new address
273  *
274  * @return void
275  */
276
277 function mail_new_incoming_notify($user)
278 {
279     $profile = $user->getProfile();
280
281     $name = $profile->getBestName();
282
283     $headers['From']    = $user->incomingemail;
284     $headers['To']      = $name . ' <' . $user->email . '>';
285     $headers['Subject'] = sprintf(_('New email address for posting to %s'),
286                                   common_config('site', 'name'));
287
288     $body = sprintf(_("You have a new posting address on %1\$s.\n\n".
289                       "Send email to %2\$s to post new messages.\n\n".
290                       "More email instructions at %3\$s.\n\n".
291                       "Faithfully yours,\n%4\$s"),
292                     common_config('site', 'name'),
293                     $user->incomingemail,
294                     common_local_url('doc', array('title' => 'email')),
295                     common_config('site', 'name'));
296
297     mail_send($user->email, $headers, $body);
298 }
299
300 /**
301  * generate a new address for incoming messages
302  *
303  * @todo check the database for uniqueness
304  *
305  * @return string new email address for incoming messages
306  */
307
308 function mail_new_incoming_address()
309 {
310     $prefix = common_confirmation_code(64);
311     $suffix = mail_domain();
312     return $prefix . '@' . $suffix;
313 }
314
315 /**
316  * broadcast a notice to all subscribers with SMS notification on
317  *
318  * This function sends SMS messages to all users who have sms addresses;
319  * have sms notification on; and have sms enabled for this particular
320  * subscription.
321  *
322  * @param Notice $notice The notice to broadcast
323  *
324  * @return success flag
325  */
326
327 function mail_broadcast_notice_sms($notice)
328 {
329     // Now, get users subscribed to this profile
330
331     $user = new User();
332
333     $UT = common_config('db','type')=='pgsql'?'"user"':'user';
334     $user->query('SELECT nickname, smsemail, incomingemail ' .
335                  "FROM $UT JOIN subscription " .
336                  "ON $UT.id = subscription.subscriber " .
337                  'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
338                  'AND subscription.subscribed != subscription.subscriber ' .
339                  "AND $UT.smsemail IS NOT null " .
340                  "AND $UT.smsnotify = 1 " .
341                  'AND subscription.sms = 1 ');
342
343     while ($user->fetch()) {
344         common_log(LOG_INFO,
345                    'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
346                    __FILE__);
347         $success = mail_send_sms_notice_address($notice,
348                                                 $user->smsemail,
349                                                 $user->incomingemail);
350         if (!$success) {
351             // XXX: Not sure, but I think that's the right thing to do
352             common_log(LOG_WARNING,
353                        'Sending notice ' . $notice->id . ' to ' .
354                        $user->smsemail . ' FAILED, cancelling.',
355                        __FILE__);
356             return false;
357         }
358     }
359
360     $user->free();
361     unset($user);
362
363     return true;
364 }
365
366 /**
367  * send a notice to a user via SMS
368  *
369  * A convenience wrapper around mail_send_sms_notice_address()
370  *
371  * @param Notice $notice notice to send
372  * @param User   $user   user to receive notice
373  *
374  * @see mail_send_sms_notice_address()
375  *
376  * @return boolean success flag
377  */
378
379 function mail_send_sms_notice($notice, $user)
380 {
381     return mail_send_sms_notice_address($notice,
382                                         $user->smsemail,
383                                         $user->incomingemail);
384 }
385
386 /**
387  * send a notice to an SMS email address from a given address
388  *
389  * We use the user's incoming email address as the "From" address to make
390  * replying to notices easier.
391  *
392  * @param Notice $notice        notice to send
393  * @param string $smsemail      email address to send to
394  * @param string $incomingemail email address to set as 'from'
395  *
396  * @return boolean success flag
397  */
398
399 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail)
400 {
401     $to = $nickname . ' <' . $smsemail . '>';
402
403     $other = $notice->getProfile();
404
405     common_log(LOG_INFO, 'Sending notice ' . $notice->id .
406                ' to ' . $smsemail, __FILE__);
407
408     $headers = array();
409
410     $headers['From']    = ($incomingemail) ? $incomingemail : mail_notify_from();
411     $headers['To']      = $to;
412     $headers['Subject'] = sprintf(_('%s status'),
413                                   $other->getBestName());
414
415     $body = $notice->content;
416
417     return mail_send($smsemail, $headers, $body);
418 }
419
420 /**
421  * send a message to confirm a claim for an SMS number
422  *
423  * @param string $code     confirmation code
424  * @param string $nickname nickname of user claiming number
425  * @param string $address  email address to send the confirmation to
426  *
427  * @see common_confirmation_code()
428  *
429  * @return void
430  */
431
432 function mail_confirm_sms($code, $nickname, $address)
433 {
434     $recipients = $address;
435
436     $headers['From']    = mail_notify_from();
437     $headers['To']      = $nickname . ' <' . $address . '>';
438     $headers['Subject'] = _('SMS confirmation');
439
440     // FIXME: I18N
441
442     $body  = "$nickname: confirm you own this phone number with this code:";
443     $body .= "\n\n";
444     $body .= $code;
445     $body .= "\n\n";
446
447     mail_send($recipients, $headers, $body);
448 }
449
450 /**
451  * send a mail message to notify a user of a 'nudge'
452  *
453  * @param User $from user nudging
454  * @param User $to   user being nudged
455  *
456  * @return boolean success flag
457  */
458
459 function mail_notify_nudge($from, $to)
460 {
461     common_init_locale($to->language);
462     $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
463
464     $from_profile = $from->getProfile();
465
466     $body = sprintf(_("%1\$s (%2\$s) is wondering what you are up to ".
467                       "these days and is inviting you to post some news.\n\n".
468                       "So let's hear from you :)\n\n".
469                       "%3\$s\n\n".
470                       "Don't reply to this email; it won't get to them.\n\n".
471                       "With kind regards,\n".
472                       "%4\$s\n"),
473                     $from_profile->getBestName(),
474                     $from->nickname,
475                     common_local_url('all', array('nickname' => $to->nickname)),
476                     common_config('site', 'name'));
477     common_init_locale();
478     return mail_to_user($to, $subject, $body);
479 }
480
481 /**
482  * send a message to notify a user of a direct message (DM)
483  *
484  * This function checks to see if the recipient wants notification
485  * of DMs and has a configured email address.
486  *
487  * @param Message $message message to notify about
488  * @param User    $from    user sending message; default to sender
489  * @param User    $to      user receiving message; default to recipient
490  *
491  * @return boolean success code
492  */
493
494 function mail_notify_message($message, $from=null, $to=null)
495 {
496     if (is_null($from)) {
497         $from = User::staticGet('id', $message->from_profile);
498     }
499
500     if (is_null($to)) {
501         $to = User::staticGet('id', $message->to_profile);
502     }
503
504     if (is_null($to->email) || !$to->emailnotifymsg) {
505         return true;
506     }
507
508     common_init_locale($to->language);
509     $subject = sprintf(_('New private message from %s'), $from->nickname);
510
511     $from_profile = $from->getProfile();
512
513     $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
514                       "------------------------------------------------------\n".
515                       "%3\$s\n".
516                       "------------------------------------------------------\n\n".
517                       "You can reply to their message here:\n\n".
518                       "%4\$s\n\n".
519                       "Don't reply to this email; it won't get to them.\n\n".
520                       "With kind regards,\n".
521                       "%5\$s\n"),
522                     $from_profile->getBestName(),
523                     $from->nickname,
524                     $message->content,
525                     common_local_url('newmessage', array('to' => $from->id)),
526                     common_config('site', 'name'));
527
528     common_init_locale();
529     return mail_to_user($to, $subject, $body);
530 }
531
532 /**
533  * notify a user that one of their notices has been chosen as a 'fave'
534  *
535  * Doesn't check that the user has an email address nor if they
536  * want to receive notification of faves. Maybe this happens higher
537  * up the stack...?
538  *
539  * @param User   $other  The user whose notice was faved
540  * @param User   $user   The user who faved the notice
541  * @param Notice $notice The notice that was faved
542  *
543  * @return void
544  */
545
546 function mail_notify_fave($other, $user, $notice)
547 {
548     $profile = $user->getProfile();
549
550     $bestname = $profile->getBestName();
551
552     common_init_locale($other->language);
553
554     $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
555
556     $body = sprintf(_("%1\$s just added your notice from %2\$s".
557                       " as one of their favorites.\n\n" .
558                       "The URL of your notice is:\n\n" .
559                       "%3\$s\n\n" .
560                       "The text of your notice is:\n\n" .
561                       "%4\$s\n\n" .
562                       "You can see the list of %1\$s's favorites here:\n\n" .
563                       "%5\$s\n\n" .
564                       "Faithfully yours,\n" .
565                       "%6\$s\n"),
566                     $bestname,
567                     common_exact_date($notice->created),
568                     common_local_url('shownotice',
569                                      array('notice' => $notice->id)),
570                     $notice->content,
571                     common_local_url('showfavorites',
572                                      array('nickname' => $user->nickname)),
573                     common_config('site', 'name'));
574
575     common_init_locale();
576     mail_to_user($other, $subject, $body);
577 }
578
579 /**
580  * notify a user that they have received an "attn:" message AKA "@-reply"
581  *
582  * @param User   $user   The user who recevied the notice
583  * @param Notice $notice The notice that was sent
584  *
585  * @return void
586  */
587
588 function mail_notify_attn($user, $notice)
589 {
590     if (!$user->email || !$user->emailnotifyattn) {
591         return;
592     }
593
594     $sender = $notice->getProfile();
595
596     $bestname = $sender->getBestName();
597
598     common_init_locale($user->language);
599         
600         if ($notice->conversation != $notice->id) {
601                 $conversationEmailText = "The full conversation can be read here:\n\n".
602                                                                  "\t%5\$s\n\n ";
603                 $conversationUrl           = common_local_url('conversation',
604                                  array('id' => $notice->conversation)).'#notice-'.$notice->id;
605         } else {
606                 $conversationEmailText = "%5\$s";
607                 $conversationUrl = null;
608         }
609         
610     $subject = sprintf(_('%s sent a notice to your attention'), $bestname);
611         
612         $body = sprintf(_("%1\$s just sent a notice to your attention (an '@-reply') on %2\$s.\n\n".
613                       "The notice is here:\n\n".
614                       "\t%3\$s\n\n" .
615                       "It reads:\n\n".
616                       "\t%4\$s\n\n" .
617                       $conversationEmailText .
618                       "You can reply back here:\n\n".
619                       "\t%6\$s\n\n" .
620                       "The list of all @-replies for you here:\n\n" .
621                       "%7\$s\n\n" .
622                       "Faithfully yours,\n" .
623                       "%2\$s\n\n" .
624                       "P.S. You can turn off these email notifications here: %8\$s\n"),
625                     $bestname,//%1
626                     common_config('site', 'name'),//%2
627                     common_local_url('shownotice',
628                                      array('notice' => $notice->id)),//%3
629                     $notice->content,//%4
630                                         $conversationUrl,//%5
631                     common_local_url('newnotice',
632                                      array('replyto' => $sender->nickname)),//%6
633                     common_local_url('replies',
634                                      array('nickname' => $user->nickname)),//%7
635                     common_local_url('emailsettings'));//%8
636         
637     common_init_locale();
638     mail_to_user($user, $subject, $body);
639 }
640
641 /**
642  * Send a mail message to notify a user that her Twitter bridge link
643  * has stopped working, and therefore has been removed.  This can
644  * happen when the user changes her Twitter password, or otherwise
645  * revokes access.
646  *
647  * @param User $user   user whose Twitter bridge link has been removed
648  *
649  * @return boolean success flag
650  */
651
652 function mail_twitter_bridge_removed($user)
653 {
654     common_init_locale($user->language);
655
656     $profile = $user->getProfile();
657
658     $subject = sprintf(_('Your Twitter bridge has been disabled.'));
659
660     $site_name = common_config('site', 'name');
661
662     $body = sprintf(_('Hi, %1$s. We\'re sorry to inform you that your ' .
663         'link to Twitter has been disabled. We no longer seem to have ' .
664     'permission to update your Twitter status. (Did you revoke ' .
665     '%3$s\'s access?)' . "\n\n" .
666     'You can re-enable your Twitter bridge by visiting your ' .
667     "Twitter settings page:\n\n\t%2\$s\n\n" .
668         "Regards,\n%3\$s\n"),
669         $profile->getBestName(),
670         common_local_url('twittersettings'),
671         common_config('site', 'name'));
672
673     common_init_locale();
674     return mail_to_user($user, $subject, $body);
675 }
676
677 /**
678  * Send a mail message to notify a user that her Facebook Application
679  * access has been removed.
680  *
681  * @param User $user   user whose Facebook app link has been removed
682  *
683  * @return boolean success flag
684  */
685
686 function mail_facebook_app_removed($user)
687 {
688     common_init_locale($user->language);
689
690     $profile = $user->getProfile();
691
692     $site_name = common_config('site', 'name');
693
694     $subject = sprintf(
695         _('Your %1$s Facebook application access has been disabled.',
696             $site_name));
697
698     $body = sprintf(_("Hi, %1\$s. We're sorry to inform you that we are " .
699         'unable to update your Facebook status from %2$s, and have disabled ' .
700         'the Facebook application for your account. This may be because ' .
701         'you have removed the Facebook application\'s authorization, or ' .
702         'have deleted your Facebook account.  You can re-enable the ' .
703         'Facebook application and automatic status updating by ' .
704         "re-installing the %2\$s Facebook application.\n\nRegards,\n\n%2\$s"),
705         $user->nickname, $site_name);
706
707     common_init_locale();
708     return mail_to_user($user, $subject, $body);
709
710 }
711
712