9abde28111a70a48f90f7aad9345f69cdaf0a18f
[friendica-addons.git] / notifyall / notifyall.php
1 <?php
2 /**
3  *
4  * Name: Notifyall
5  * Description: Send admin email message to all account holders. <b>-><a href=/notifyall TARGET = "_blank">send now!</a><-</b>
6  * Version: 1.0
7  * Author: Mike Macgirvin (Inital Author of the hubbwall Addon for the Hubzilla Project)
8  * Author: Rabuzarus <https://friendica.kommune4.de/profile/rabuzarus> (Port to Friendica)
9  */
10 use Friendica\Core\L10n;
11 use Friendica\Util\Emailer;
12
13 function notifyall_install() {
14         logger("installed notifyall");
15 }
16
17 function notifyall_uninstall() {
18         logger("removed notifyall");
19 }
20
21 function notifyall_module() {}
22
23 function notifyall_addon_admin(&$a, &$o) {
24
25         $o = '<div></div>&nbsp;&nbsp;&nbsp;&nbsp;<a href="' . z_root() . '/notifyall">' . L10n::t('Send email to all members') . '</a></br/>';
26 }
27
28
29 function notifyall_post(&$a) {
30         if(! is_site_admin())
31                 return;
32
33         $text = trim($_REQUEST['text']);
34         if(! $text)
35                 return;
36
37         $sitename = $a->config['sitename'];
38
39         if (!x($a->config['admin_name']))
40                 $sender_name = sprintf(L10n::t('%s Administrator'), $sitename);
41         else
42                 $sender_name = sprintf(L10n::t('%1$s, %2$s Administrator'), $a->config['admin_name'], $sitename);
43
44         if (! x($a->config['sender_email']))
45                 $sender_email = 'noreply@' . $a->get_hostname();
46         else
47                 $sender_email = $a->config['sender_email'];
48
49         $subject = $_REQUEST['subject'];
50
51
52         $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(["\\r", "\\n"],[ "", "\n"], $text))),ENT_QUOTES,'UTF-8'));
53
54         $htmlversion = bbcode(stripslashes(str_replace(["\\r","\\n"], ["","<br />\n"],$text)));
55
56         // if this is a test, send it only to the admin(s)
57         // admin_email might be a comma separated list, but we need "a@b','c@d','e@f
58         if (intval($_REQUEST['test'])) {
59                 $email = $a->config['admin_email'];
60                 $email = "'" . str_replace([" ",","], ["","','"], $email) . "'";
61         }
62         $sql_extra = ((intval($_REQUEST['test'])) ? sprintf(" AND `email` in ( %s )", $email) : '');
63
64         $recips = q("SELECT DISTINCT `email` FROM `user` WHERE `verified` AND NOT `account_removed` AND NOT `account_expired` $sql_extra");
65
66         if (! $recips) {
67                 notice(L10n::t('No recipients found.') . EOL);
68                 return;
69         }
70
71         foreach ($recips as $recip) {
72                 Emailer::send([
73                         'fromName'             => $sender_name,
74                         'fromEmail'            => $sender_email,
75                         'replyTo'              => $sender_email,
76                         'toEmail'              => $recip['email'],
77                         'messageSubject'       => $subject,
78                         'htmlVersion'          => $htmlversion,
79                         'textVersion'          => $textversion
80                 ]);
81         }
82
83         notice(L10n::t('Emails sent'));
84         goaway('admin');
85 }
86
87 function notifyall_content(&$a)
88 {
89         if (! is_site_admin()) {
90                 return;
91         }
92
93         $title = L10n::t('Send email to all members of this Friendica instance.');
94
95         $o = replace_macros(get_markup_template('notifyall_form.tpl', 'addon/notifyall/'), [
96                 '$title' => $title,
97                 '$text' => htmlspecialchars($_REQUEST['text']),
98                 '$subject' => ['subject',L10n::t('Message subject'),$_REQUEST['subject'],''],
99                 '$test' => ['test',L10n::t('Test mode (only send to administrator)'), 0,''],
100                 '$submit' => L10n::t('Submit')
101         ]);
102
103         return $o;
104 }