]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mail.php
use full alphanum for incoming email addresses
[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         assert($backend); # throws an error if it's bad
43         $sent = $backend->send($recipients, $headers, $body);
44         if (PEAR::isError($sent)) {
45                 common_log(LOG_ERR, 'Email error: ' . $sent->getMessage());
46                 return false;
47         }
48         return true;
49 }
50
51 function mail_domain() {
52         $maildomain = common_config('mail', 'domain');
53         if (!$maildomain) {
54                 $maildomain = common_config('site', 'server');
55         }
56         return $maildomain;
57 }
58
59 function mail_notify_from() {
60         $notifyfrom = common_config('mail', 'notifyfrom');
61         if (!$notifyfrom) {
62                 $domain = mail_domain();
63                 $notifyfrom = common_config('site', 'name') .' <noreply@'.$domain.'>';
64         }
65         return $notifyfrom;
66 }
67
68 function mail_to_user(&$user, $subject, $body, $address=NULL) {
69         if (!$address) {
70                 $address = $user->email;
71         }
72
73         $recipients = $address;
74         $profile = $user->getProfile();
75
76         $headers['From'] = mail_notify_from();
77         $headers['To'] = $profile->getBestName() . ' <' . $address . '>';
78         $headers['Subject'] = $subject;
79
80         return mail_send($recipients, $headers, $body);
81 }
82
83 # For confirming a Jabber address
84 # XXX: change to use mail_to_user() above
85
86 function mail_confirm_address($code, $nickname, $address) {
87         $recipients = $address;
88         $headers['From'] = mail_notify_from();
89         $headers['To'] = $nickname . ' <' . $address . '>';
90         $headers['Subject'] = _('Email address confirmation');
91
92         $body = "Hey, $nickname.";
93         $body .= "\n\n";
94         $body .= 'Someone just entered this email address on ' . common_config('site', 'name') . '.';
95         $body .= "\n\n";
96         $body .= 'If it was you, and you want to confirm your entry, use the URL below:';
97         $body .= "\n\n";
98         $body .= "\t".common_local_url('confirmaddress',
99                                                                    array('code' => $code));
100         $body .= "\n\n";
101         $body .= 'If not, just ignore this message.';
102         $body .= "\n\n";
103         $body .= 'Thanks for your time, ';
104         $body .= "\n";
105         $body .= common_config('site', 'name');
106         $body .= "\n";
107
108         mail_send($recipients, $headers, $body);
109 }
110
111 function mail_subscribe_notify($listenee, $listener) {
112         $other = $listener->getProfile();
113         mail_subscribe_notify_profile($listenee, $other);
114 }
115
116 function mail_subscribe_notify_profile($listenee, $other) {
117         if ($listenee->email && $listenee->emailnotifysub) {
118                 $profile = $listenee->getProfile();
119                 $name = $profile->getBestName();
120                 $long_name = ($other->fullname) ? ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
121                 $recipients = $listenee->email;
122                 $headers['From'] = mail_notify_from();
123                 $headers['To'] = $name . ' <' . $listenee->email . '>';
124                 $headers['Subject'] = sprintf(_('%1$s is now listening to your notices on %2$s.'), $other->getBestName(),
125                                                                           common_config('site', 'name'));
126                 $body  = sprintf(_('%1$s is now listening to your notices on %2$s.'."\n\n".
127                                                    "\t".'%3$s'."\n\n".
128                                                    'Faithfully yours,'."\n".'%4$s.'."\n"),
129                                                  $long_name,
130                                                  common_config('site', 'name'),
131                                                  $other->profileurl,
132                                                  common_config('site', 'name'));
133                 mail_send($recipients, $headers, $body);
134         }
135 }
136
137 function mail_new_incoming_notify($user) {
138
139         $profile = $user->getProfile();
140         $name = $profile->getBestName();
141
142         $headers['From'] = $user->incomingemail;
143         $headers['To'] = $name . ' <' . $user->email . '>';
144         $headers['Subject'] = sprintf(_('New email address for posting to %s'),
145                                                                   common_config('site', 'name'));
146
147         $body  = sprintf(_("You have a new posting address on %1\$s.\n\n".
148                                            "Send email to %2\$s to post new messages.\n\n".
149                                            "More email instructions at %3\$s.\n\n".
150                                            "Faithfully yours,\n%4\$s"),
151                                          common_config('site', 'name'),
152                                          $user->incomingemail,
153                                          common_local_url('doc', array('title' => 'email')),
154                                          common_config('site', 'name'));
155
156         mail_send($user->email, $headers, $body);
157 }
158
159 function mail_new_incoming_address() {
160         $prefix = common_confirmation_code(64);
161         $suffix = mail_domain();
162         return $prefix . '@' . $suffix;
163 }
164
165 function mail_broadcast_notice_sms($notice) {
166
167     # Now, get users subscribed to this profile
168
169         $user = new User();
170
171         $user->query('SELECT nickname, smsemail, incomingemail ' .
172                                  'FROM user JOIN subscription ' .
173                                  'ON user.id = subscription.subscriber ' .
174                                  'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
175                                  'AND user.smsemail IS NOT NULL ' .
176                                  'AND user.smsnotify = 1');
177
178         while ($user->fetch()) {
179                 common_log(LOG_INFO,
180                                    'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
181                                    __FILE__);
182                 $success = mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
183                 if (!$success) {
184                         # XXX: Not sure, but I think that's the right thing to do
185                         common_log(LOG_WARNING,
186                                            'Sending notice ' . $notice->id . ' to ' . $user->smsemail . ' FAILED, cancelling.',
187                                            __FILE__);
188                         return false;
189                 }
190         }
191
192         $user->free();
193         unset($user);
194
195         return true;
196 }
197
198 function mail_send_sms_notice($notice, $user) {
199         return mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
200 }
201
202 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail) {
203
204         $to = $nickname . ' <' . $smsemail . '>';
205         $other = $notice->getProfile();
206
207         common_log(LOG_INFO, "Sending notice " . $notice->id . " to " . $smsemail, __FILE__);
208
209         $headers = array();
210         $headers['From'] = (isset($incomingemail)) ? $incomingemail : mail_notify_from();
211         $headers['To'] = $to;
212         $headers['Subject'] = sprintf(_('%s status'),
213                                                                   $other->getBestName());
214         $body = $notice->content;
215
216         return mail_send($smsemail, $headers, $body);
217 }
218
219 function mail_confirm_sms($code, $nickname, $address) {
220
221         $recipients = $address;
222
223         $headers['From'] = mail_notify_from();
224         $headers['To'] = $nickname . ' <' . $address . '>';
225         $headers['Subject'] = _('SMS confirmation');
226
227         $body = "$nickname: confirm you own this phone number with this code:";
228         $body .= "\n\n";
229         $body .= $code;
230         $body .= "\n\n";
231
232         mail_send($recipients, $headers, $body);
233 }
234
235
236 function mail_notify_nudge($from, $to) {
237
238         $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
239
240         $from_profile = $from->getProfile();
241
242         $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".
243                                           "So let's hear from you :)\n\n".
244                                           "%3\$s\n\n".
245                                           "Don't reply to this email; it won't get to them.\n\n".
246                                           "With kind regards,\n".
247                                           "%4\$s\n"),
248                                         $from_profile->getBestName(),
249                                         $from->nickname,
250                                         common_local_url('all', array('nickname' => $to->nickname)),
251                                         common_config('site', 'name'));
252
253         return mail_to_user($to, $subject, $body);
254 }
255
256
257
258 function mail_notify_message($message, $from=NULL, $to=NULL) {
259
260         if (is_null($from)) {
261                 $from = User::staticGet('id', $message->from_profile);
262         }
263
264         if (is_null($to)) {
265                 $to = User::staticGet('id', $message->to_profile);
266         }
267
268         if (is_null($to->email) || !$to->emailnotifymsg) {
269                 return true;
270         }
271
272         $subject = sprintf(_('New private message from %s'), $from->nickname);
273
274         $from_profile = $from->getProfile();
275
276         $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
277                                           "------------------------------------------------------\n".
278                                           "%3\$s\n".
279                                           "------------------------------------------------------\n\n".
280                                           "You can reply to their message here:\n\n".
281                                           "%4\$s\n\n".
282                                           "Don't reply to this email; it won't get to them.\n\n".
283                                           "With kind regards,\n".
284                                           "%5\$s\n"),
285                                         $from_profile->getBestName(),
286                                         $from->nickname,
287                                         $message->content,
288                                         common_local_url('newmessage', array('to' => $from->id)),
289                                         common_config('site', 'name'));
290
291         return mail_to_user($to, $subject, $body);
292 }
293
294 function mail_notify_fave($other, $user, $notice) {
295
296         $profile = $user->getProfile();
297         $bestname = $profile->getBestName();
298         $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
299         $body = sprintf(_("%1\$s just added your notice from %2\$s as one of their favorites.\n\n" .
300                                           "In case you forgot, you can see the text of your notice here:\n\n" .
301                                           "%3\$s\n\n" .
302                                           "You can see the list of %1\$s's favorites here:\n\n" .
303                                           "%4\$s\n\n" .
304                                           "Faithfully yours,\n" .
305                                           "%5\$s\n"),
306                                         $bestname,
307                                         common_exact_date($notice->created),
308                                         common_local_url('shownotice', array('notice' => $notice->id)),
309                                         common_local_url('showfavorites', array('nickname' => $user->nickname)),
310                                         common_config('site', 'name'));
311
312         mail_to_user($other, $subject, $body);
313 }