Merge pull request #723 from annando/fix-pumpio
[friendica-addons.git/.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
11 use Friendica\App;
12 use Friendica\Content\Text\BBCode;
13 use Friendica\Core\Config;
14 use Friendica\Core\L10n;
15 use Friendica\Util\Emailer;
16
17 function notifyall_install()
18 {
19         logger("installed notifyall");
20 }
21
22 function notifyall_uninstall()
23 {
24         logger("removed notifyall");
25 }
26
27 function notifyall_module() {}
28
29 function notifyall_addon_admin(App $a, &$o)
30 {
31         $o = '<div></div>&nbsp;&nbsp;&nbsp;&nbsp;<a href="' . z_root() . '/notifyall">' . L10n::t('Send email to all members') . '</a></br/>';
32 }
33
34
35 function notifyall_post(App $a)
36 {
37         if(!is_site_admin()) {
38                 return;
39         }
40
41         $text = trim($_REQUEST['text']);
42
43         if(! $text) {
44                 return;
45         }
46
47         $sitename = Config::get('config', 'sitename');
48
49         if (empty(Config::get('config', 'admin_name'))) {
50                 $sender_name = '"' . L10n::t('%s Administrator', $sitename) . '"';
51         } else {
52                 $sender_name = '"' . L10n::t('%1$s, %2$s Administrator', Config::get('config', 'admin_name'), $sitename) . '"';
53         }
54
55         if (! x(Config::get('config', 'sender_email'))) {
56                 $sender_email = 'noreply@' . $a->get_hostname();
57         } else {
58                 $sender_email = Config::get('config', 'sender_email');
59         }
60
61         $subject = $_REQUEST['subject'];
62
63
64         $textversion = strip_tags(html_entity_decode(BBCode::convert(stripslashes(str_replace(["\\r", "\\n"], ["", "\n"], $text))), ENT_QUOTES, 'UTF-8'));
65
66         $htmlversion = BBCode::convert(stripslashes(str_replace(["\\r", "\\n"], ["", "<br />\n"], $text)));
67
68         // if this is a test, send it only to the admin(s)
69         // admin_email might be a comma separated list, but we need "a@b','c@d','e@f
70         if (intval($_REQUEST['test'])) {
71                 $email = Config::get('config', 'admin_email');
72                 $email = "'" . str_replace([" ",","], ["","','"], $email) . "'";
73         }
74         $sql_extra = ((intval($_REQUEST['test'])) ? sprintf(" AND `email` in ( %s )", $email) : '');
75
76         $recips = q("SELECT DISTINCT `email` FROM `user` WHERE `verified` AND NOT `account_removed` AND NOT `account_expired` $sql_extra");
77
78         if (! $recips) {
79                 notice(L10n::t('No recipients found.') . EOL);
80                 return;
81         }
82
83         foreach ($recips as $recip) {
84                 Emailer::send([
85                         'fromName'             => $sender_name,
86                         'fromEmail'            => $sender_email,
87                         'replyTo'              => $sender_email,
88                         'toEmail'              => $recip['email'],
89                         'messageSubject'       => $subject,
90                         'htmlVersion'          => $htmlversion,
91                         'textVersion'          => $textversion
92                 ]);
93         }
94
95         notice(L10n::t('Emails sent'));
96         goaway('admin');
97 }
98
99 function notifyall_content(&$a)
100 {
101         if (! is_site_admin()) {
102                 return;
103         }
104
105         $title = L10n::t('Send email to all members of this Friendica instance.');
106
107         $o = replace_macros(get_markup_template('notifyall_form.tpl', 'addon/notifyall/'), [
108                 '$title' => $title,
109                 '$text' => htmlspecialchars($_REQUEST['text']),
110                 '$subject' => ['subject',L10n::t('Message subject'),$_REQUEST['subject'],''],
111                 '$test' => ['test',L10n::t('Test mode (only send to administrator)'), 0,''],
112                 '$submit' => L10n::t('Submit')
113         ]);
114
115         return $o;
116 }