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