]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mail.php
Merge branch 'master' of http://goukihq.org/misc/git/laconica-locales
[quix0rs-gnu-social.git] / lib / mail.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once('Mail.php');
23
24 function mail_backend() {
25         static $backend = NULL;
26
27         if (!$backend) {
28                 global $config;
29                 $backend = Mail::factory($config['mail']['backend'],
30                                                                  ($config['mail']['params']) ? $config['mail']['params'] : array());
31                 if (PEAR::isError($backend)) {
32                         common_server_error($backend->getMessage(), 500);
33                 }
34         }
35         return $backend;
36 }
37
38 # XXX: use Mail_Queue... maybe
39
40 function mail_send($recipients, $headers, $body) {
41         $backend = mail_backend();
42     if (!isset($headers['Content-Type'])) {
43         $headers['Content-Type'] = 'text/plain; charset=UTF-8';
44     }
45         assert($backend); # throws an error if it's bad
46         $sent = $backend->send($recipients, $headers, $body);
47         if (PEAR::isError($sent)) {
48                 common_log(LOG_ERR, 'Email error: ' . $sent->getMessage());
49                 return false;
50         }
51         return true;
52 }
53
54 function mail_domain() {
55         $maildomain = common_config('mail', 'domain');
56         if (!$maildomain) {
57                 $maildomain = common_config('site', 'server');
58         }
59         return $maildomain;
60 }
61
62 function mail_notify_from() {
63         $notifyfrom = common_config('mail', 'notifyfrom');
64         if (!$notifyfrom) {
65                 $domain = mail_domain();
66                 $notifyfrom = common_config('site', 'name') .' <noreply@'.$domain.'>';
67         }
68         return $notifyfrom;
69 }
70
71 function mail_to_user(&$user, $subject, $body, $address=NULL) {
72         if (!$address) {
73                 $address = $user->email;
74         }
75
76         $recipients = $address;
77         $profile = $user->getProfile();
78
79         $headers['From'] = mail_notify_from();
80         $headers['To'] = $profile->getBestName() . ' <' . $address . '>';
81         $headers['Subject'] = $subject;
82
83         return mail_send($recipients, $headers, $body);
84 }
85
86 # For confirming a Jabber address
87
88 function mail_confirm_address($user, $code, $nickname, $address) {
89
90         $subject = _('Email address confirmation');
91
92     $body = sprintf(_("Hey, %s.\n\nSomeone just entered this email address on %s.\n\n" .
93         "If it was you, and you want to confirm your entry, use the URL below:\n\n\t%s\n\n" .
94         "If not, just ignore this message.\n\nThanks for your time, \n%s\n")
95         , $nickname, common_config('site', 'name')
96         , common_local_url('confirmaddress', array('code' => $code)), common_config('site', 'name'));
97      return mail_to_user($user, $subject, $body, $address);
98 }
99
100 function mail_subscribe_notify($listenee, $listener) {
101         $other = $listener->getProfile();
102         mail_subscribe_notify_profile($listenee, $other);
103 }
104
105 function mail_subscribe_notify_profile($listenee, $other) {
106         if ($listenee->email && $listenee->emailnotifysub) {
107         // use the recipients localization
108         common_init_locale($listenee->language);
109                 $profile = $listenee->getProfile();
110                 $name = $profile->getBestName();
111                 $long_name = ($other->fullname) ? ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
112                 $recipients = $listenee->email;
113                 $headers['From'] = mail_notify_from();
114                 $headers['To'] = $name . ' <' . $listenee->email . '>';
115                 $headers['Subject'] = sprintf(_('%1$s is now listening to your notices on %2$s.'), $other->getBestName(),
116                                                                           common_config('site', 'name'));
117                 $body  = sprintf(_('%1$s is now listening to your notices on %2$s.'."\n\n".
118                                                    "\t".'%3$s'."\n\n".
119                                                    'Faithfully yours,'."\n".'%4$s.'."\n"),
120                                                  $long_name,
121                                                  common_config('site', 'name'),
122                                                  $other->profileurl,
123                                                  common_config('site', 'name'));
124
125         // reset localization
126         common_init_locale();
127                 mail_send($recipients, $headers, $body);
128         }
129 }
130
131 function mail_new_incoming_notify($user) {
132
133         $profile = $user->getProfile();
134         $name = $profile->getBestName();
135
136         $headers['From'] = $user->incomingemail;
137         $headers['To'] = $name . ' <' . $user->email . '>';
138         $headers['Subject'] = sprintf(_('New email address for posting to %s'),
139                                                                   common_config('site', 'name'));
140
141         $body  = sprintf(_("You have a new posting address on %1\$s.\n\n".
142                                            "Send email to %2\$s to post new messages.\n\n".
143                                            "More email instructions at %3\$s.\n\n".
144                                            "Faithfully yours,\n%4\$s"),
145                                          common_config('site', 'name'),
146                                          $user->incomingemail,
147                                          common_local_url('doc', array('title' => 'email')),
148                                          common_config('site', 'name'));
149
150         mail_send($user->email, $headers, $body);
151 }
152
153 function mail_new_incoming_address() {
154         $prefix = common_confirmation_code(64);
155         $suffix = mail_domain();
156         return $prefix . '@' . $suffix;
157 }
158
159 function mail_broadcast_notice_sms($notice) {
160
161     # Now, get users subscribed to this profile
162
163         $user = new User();
164
165         $user->query('SELECT nickname, smsemail, incomingemail ' .
166                                  'FROM user JOIN subscription ' .
167                                  'ON user.id = subscription.subscriber ' .
168                                  'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
169                                  'AND user.smsemail IS NOT NULL ' .
170                                  'AND user.smsnotify = 1 ' .
171                  'AND subscription.sms = 1 ');
172
173         while ($user->fetch()) {
174                 common_log(LOG_INFO,
175                                    'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
176                                    __FILE__);
177                 $success = mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
178                 if (!$success) {
179                         # XXX: Not sure, but I think that's the right thing to do
180                         common_log(LOG_WARNING,
181                                            'Sending notice ' . $notice->id . ' to ' . $user->smsemail . ' FAILED, cancelling.',
182                                            __FILE__);
183                         return false;
184                 }
185         }
186
187         $user->free();
188         unset($user);
189
190         return true;
191 }
192
193 function mail_send_sms_notice($notice, $user) {
194         return mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
195 }
196
197 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail) {
198
199         $to = $nickname . ' <' . $smsemail . '>';
200         $other = $notice->getProfile();
201
202         common_log(LOG_INFO, "Sending notice " . $notice->id . " to " . $smsemail, __FILE__);
203
204         $headers = array();
205         $headers['From'] = (isset($incomingemail)) ? $incomingemail : mail_notify_from();
206         $headers['To'] = $to;
207         $headers['Subject'] = sprintf(_('%s status'),
208                                                                   $other->getBestName());
209         $body = $notice->content;
210
211         return mail_send($smsemail, $headers, $body);
212 }
213
214 function mail_confirm_sms($code, $nickname, $address) {
215
216         $recipients = $address;
217
218         $headers['From'] = mail_notify_from();
219         $headers['To'] = $nickname . ' <' . $address . '>';
220         $headers['Subject'] = _('SMS confirmation');
221
222         $body = "$nickname: confirm you own this phone number with this code:";
223         $body .= "\n\n";
224         $body .= $code;
225         $body .= "\n\n";
226
227         mail_send($recipients, $headers, $body);
228 }
229
230 function mail_notify_nudge($from, $to) {
231     common_init_locale($to->language);
232         $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
233
234         $from_profile = $from->getProfile();
235
236         $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".
237                                           "So let's hear from you :)\n\n".
238                                           "%3\$s\n\n".
239                                           "Don't reply to this email; it won't get to them.\n\n".
240                                           "With kind regards,\n".
241                                           "%4\$s\n"),
242                                         $from_profile->getBestName(),
243                                         $from->nickname,
244                                         common_local_url('all', array('nickname' => $to->nickname)),
245                                         common_config('site', 'name'));
246     common_init_locale();
247         return mail_to_user($to, $subject, $body);
248 }
249
250 function mail_notify_message($message, $from=NULL, $to=NULL) {
251
252         if (is_null($from)) {
253                 $from = User::staticGet('id', $message->from_profile);
254         }
255
256         if (is_null($to)) {
257                 $to = User::staticGet('id', $message->to_profile);
258         }
259
260         if (is_null($to->email) || !$to->emailnotifymsg) {
261                 return true;
262         }
263
264     common_init_locale($to->language);
265         $subject = sprintf(_('New private message from %s'), $from->nickname);
266
267         $from_profile = $from->getProfile();
268
269         $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
270                                           "------------------------------------------------------\n".
271                                           "%3\$s\n".
272                                           "------------------------------------------------------\n\n".
273                                           "You can reply to their message here:\n\n".
274                                           "%4\$s\n\n".
275                                           "Don't reply to this email; it won't get to them.\n\n".
276                                           "With kind regards,\n".
277                                           "%5\$s\n"),
278                                         $from_profile->getBestName(),
279                                         $from->nickname,
280                                         $message->content,
281                                         common_local_url('newmessage', array('to' => $from->id)),
282                                         common_config('site', 'name'));
283
284     common_init_locale();
285         return mail_to_user($to, $subject, $body);
286 }
287
288 function mail_notify_fave($other, $user, $notice) {
289
290         $profile = $user->getProfile();
291         $bestname = $profile->getBestName();
292     common_init_locale($other->language);
293         $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
294         $body = sprintf(_("%1\$s just added your notice from %2\$s as one of their favorites.\n\n" .
295                                           "In case you forgot, you can see the text of your notice here:\n\n" .
296                                           "%3\$s\n\n" .
297                                           "You can see the list of %1\$s's favorites here:\n\n" .
298                                           "%4\$s\n\n" .
299                                           "Faithfully yours,\n" .
300                                           "%5\$s\n"),
301                                         $bestname,
302                                         common_exact_date($notice->created),
303                                         common_local_url('shownotice', array('notice' => $notice->id)),
304                                         common_local_url('showfavorites', array('nickname' => $user->nickname)),
305                                         common_config('site', 'name'));
306
307     common_init_locale();
308         mail_to_user($other, $subject, $body);
309 }