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