source: branches/1.0/content/plugins/admin_email/admin_email.php @ 1189

Revision 1189, 13.7 KB checked in by nick_ramsay, 3 years ago (diff)

[Branch 1.0] Missing close slash on meta tags.

Line 
1<?php
2/**
3 * name: Admin Email
4 * description: Send email to all members, groups or users
5 * version: 0.3
6 * folder: admin_email
7 * class: adminEmail
8 * requires: users 1.1
9 * hooks: install_plugin, admin_theme_index_top, admin_header_include, admin_plugin_settings, admin_sidebar_plugin_settings, user_settings_pre_save, user_settings_fill_form, user_settings_extra_settings
10 * author: Nick Ramsay
11 * authorurl: http://hotarucms.org/member.php?1-Nick
12 *
13 * PHP version 5
14 *
15 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation, either version 3 of
18 * the License, or (at your option) any later version.
19 *
20 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 * FITNESS FOR A PARTICULAR PURPOSE.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
26 *
27 * @category  Content Management System
28 * @package   HotaruCMS
29 * @author    Nick Ramsay <admin@hotarucms.org>
30 * @copyright Copyright (c) 2009, Hotaru CMS
31 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
32 * @link      http://www.hotarucms.org/
33 */
34
35class adminEmail
36{
37    /**
38     * Default settings on install
39     */
40    public function install_plugin($h)
41    {
42        // Default settings
43        $admin_email_settings = $h->getSerializedSettings();
44       
45        if (!isset($admin_email_settings['admin_email_batch_size'])) { $admin_email_settings['admin_email_batch_size'] = 20; }
46        if (!isset($admin_email_settings['admin_email_pause'])) { $admin_email_settings['admin_email_pause'] = 10; }
47        if (!isset($admin_email_settings['admin_email_recipients'])) { $admin_email_settings['admin_email_recipients'] = serialize(array()); }
48        if (!isset($admin_email_settings['admin_email_subject'])) { $admin_email_settings['admin_email_subject'] = ''; }
49        if (!isset($admin_email_settings['admin_email_body'])) { $admin_email_settings['admin_email_body'] = ''; }
50        if (!isset($admin_email_settings['admin_email_send_self'])) { $admin_email_settings['admin_email_send_self'] = ''; }
51        if (!isset($admin_email_settings['admin_email_send_opted_out'])) { $admin_email_settings['admin_email_send_opted_out'] = ''; }
52        if (!isset($admin_email_settings['admin_email_simulation'])) { $admin_email_settings['admin_email_simulation'] = ''; }
53        if (!isset($admin_email_settings['admin_email_id_list'])) { $admin_email_settings['admin_email_id_list'] = serialize(array()); }
54       
55        $h->updateSetting('admin_email_settings', serialize($admin_email_settings));
56       
57        // Add "admin notify" option to the default user settings
58        $base_settings = $h->getDefaultSettings('base'); // originals from plugins
59        $site_settings = $h->getDefaultSettings('site'); // site defaults updated by admin
60        if (!isset($base_settings['admin_notify'])) {
61            $base_settings['admin_notify'] = "checked";
62            $site_settings['admin_notify'] = "checked";
63            $h->updateDefaultSettings($base_settings, 'base');
64            $h->updateDefaultSettings($site_settings, 'site');
65        }
66    }
67   
68   
69    /**
70     * Disable sidebar
71     */
72    public function admin_theme_index_top($h)
73    {
74        if (($h->cage->get->testPage('plugin') == 'admin_email') && ($h->cage->get->testInt('mailing') == 1)) {
75            $this->sendAdminEmail($h);
76        }
77    }
78   
79    /**
80     * Do admin email
81     */
82    public function doAdminEmail($h)
83    {
84        // get latest changes:
85        $admin_email_settings = $h->getSerializedSettings();
86
87        $recipients = unserialize($admin_email_settings['admin_email_recipients']);
88        $send_self = $admin_email_settings['admin_email_send_self'];
89        $send_opted_out = $admin_email_settings['admin_email_send_opted_out'];
90       
91        // make recipients an array:
92        if (is_object($recipients)) {
93            $recipients_array = array();
94            foreach ($recipients as $recip) {
95                array_push($recipients_array, $recip);
96            }
97            $recipients = $recipients_array;
98        }
99       
100        $id_list = array(); // this will store the ids of all email recipients
101       
102        $groups = array('admin', 'supermod', 'moderator', 'member', 'undermod', 'banned', 'killspammed');
103        foreach ($groups as $gname) {
104            if (in_array($gname, $recipients)) {
105                $group_members = $h->userIdNameList($gname);
106                if (!$group_members) { continue; }
107                foreach ($group_members as $gm) {
108                    array_push($id_list, $gm->user_id);
109                }
110            }
111        }
112       
113        // strip groups from $recipients because we've just put them in $id_list
114        $recipients = array_diff($recipients, $groups);
115       
116        // complete list of user ids for each recipient
117        $id_list = array_merge($id_list, $recipients);
118       
119        // if not force sending to users who have opted out of getting admin emails, remove them now:
120        if (!$send_opted_out) {
121            // we need to remove anyone who has opted out of emails from admin.
122            // First get the user_settings for every user on the site with SAVED settings:
123            $all_settings = $h->userSettingsList();
124            $default_settings = $h->getDefaultSettings();
125           
126            // Next, make a list of opted out users (by id)
127            $opted_out = array();
128            if ($all_settings) {
129                foreach ($all_settings as $set) {
130                    $u_settings = unserialize($set->usermeta_value);
131                    $merged_settings = array_merge($default_settings, $u_settings);
132                    if (!$merged_settings['admin_notify']) {
133                        array_push($opted_out, $set->usermeta_userid);
134                    }
135                }
136            }
137           
138            // strip opted out users from $recipients
139            $id_list = array_diff($id_list, $opted_out);
140        }
141       
142        // if sending a copy to yourself, add your id to the list
143        if ($send_self) { array_push($id_list, $h->currentUser->id); }
144       
145        // remove any duplicates:
146        $id_list = array_unique($id_list);
147       
148        // Save the id list to the database.
149        $admin_email_settings['admin_email_id_list'] = serialize($id_list);
150        $h->updateSetting('admin_email_settings', serialize($admin_email_settings));
151
152        // reload the page, without any html...
153        $url = BASEURL . "admin_index.php?page=plugin_settings&plugin=admin_email&mailing=1";
154        echo "<meta http-equiv='Refresh' content='0; URL=" . $url . "' />";
155        echo $h->lang["admin_email_redirecting"];
156        ob_flush();
157        flush();
158        exit;
159    }
160   
161   
162    /**
163     * Send emails
164     *
165     * @param object $recipients
166     */
167    public function sendAdminEmail($h)
168    {
169        // get latest changes:
170        $admin_email_settings   = $h->getSerializedSettings();
171        $id_list                = unserialize($admin_email_settings['admin_email_id_list']);
172        $batch_size             = $admin_email_settings['admin_email_batch_size'];
173        $pause                  = $admin_email_settings['admin_email_pause'];
174        $orig_subject           = $admin_email_settings['admin_email_subject'];
175        $orig_body              = $admin_email_settings['admin_email_body'];
176        $simulation             = $admin_email_settings['admin_email_simulation'];
177        $send_self              = $admin_email_settings['admin_email_send_self'];
178        $delimiter              = "\r\n";
179        $start                  = 0;    // how many rows down the resulting users table do we start our batch from
180       
181        $batch = $h->cage->get->testInt('batch');
182        if ($batch) { $start = ($batch-1) * $batch_size; } else { $batch = 1; }
183       
184        // get information for each user here:
185        if ($id_list) {
186            $batches = ceil(count($id_list) / $batch_size);
187            $users = $h->userListFull($id_list, $start, $batch_size); // batches returned alphabetically
188        }
189               
190        if ($simulation) {
191            echo "<p style='color: red;'><b>" . $h->lang["admin_email_simulation_mode"] . "</b></p>\n";
192        } else {
193            echo "<p style='color: blue;'><b>" . $h->lang["admin_email_real_mode"] . "</b></p>\n";
194        }
195       
196        if(!$users) {
197            echo "<p>" . $h->lang["admin_email_no_recipients"] . "</p>\n";
198            echo "<p><a href='" . BASEURL . "admin_index.php?page=plugin_settings&plugin=admin_email&status=done'>";
199            echo $h->lang['admin_email'] . "</a></p>\n";
200            exit;
201        }
202       
203        echo "<p><b>" . $h->lang["admin_email_email_batch"] . $batch . "/" . $batches . "</b></p>\n";
204               
205        foreach($users as $recipient) {
206
207            $subject = preg_replace('/\{username\}/i', $recipient->user_username, trim(html_entity_decode($orig_subject, ENT_QUOTES,'UTF-8')));
208            $body = preg_replace('/\{username\}/i', $recipient->user_username, trim(html_entity_decode($orig_body, ENT_QUOTES,'UTF-8')));
209            $message = preg_replace("#(\r\n|\r|\n)#s", $delimiter, $body);
210            $message .= " \r\n\r\n";
211            $message .= $h->lang['admin_email_pre_remove'] . "\r\n";
212            $message .= $h->lang['admin_email_remove'] . "\r\n";
213            $message .= BASEURL . "index.php?page=user-settings&user=" . $recipient->user_username;
214            $from = SITE_EMAIL; // Send_From_Email is admin
215            $to = $recipient->user_email; 
216            $headers = "From: " . $from . "\r\nReply-To: " . $from . "\r\nX-Priority: 3\r\n";
217           
218            if ($send_self && ($h->currentUser->id == $recipient->user_id)) {
219                echo $h->lang["admin_email_sent_to"] . "<i>" . $to . "</i><br />\n";
220                @mail($to, $subject, $message, $headers);   // This sends an email to the requesting admin
221            } elseif ($simulation) {
222                echo $h->lang["admin_email_fake_sending"] . $to . "<br />\n";
223            } else {
224                echo $h->lang["admin_email_sent_to"] . $to . "<br />\n";
225                @mail($to, $subject, $message, $headers);   // This does the actual sending!
226            }
227            ob_flush();
228            flush();
229            sleep(1); // one second pause between sending emails
230        }
231       
232        if ($batch < $batches) {
233            $batch++;
234            echo "<p>" . $h->lang["admin_email_waiting"] . $pause . $h->lang["admin_email_before_next_batch"] . "</p>\n";
235            echo "<p><a href='" . $h->url(array('page'=>'plugin_settings', 'plugin'=>'admin_email'), 'admin') . "'>";
236            echo "<span style='color: red;'>" . $h->lang['admin_email_abort'] . "</span></a></p>\n";
237            // reload the page, without any html...
238            $url = BASEURL . "admin_index.php?page=plugin_settings&plugin=admin_email&mailing=1&batch=" . $batch;
239            echo "<meta http-equiv='Refresh' content='" . $pause . "; URL=" . $url . "'>";
240        } else {
241            if ($simulation) {
242                if ($send_self) { echo "<p>" . $h->lang["admin_email_sent_to_self"] . "</p>\n"; }
243                echo "<p>" . $h->lang["admin_email_after_simulation"] . "\n";
244                echo "<a href='" . BASEURL . "admin_index.php?page=plugin_settings&plugin=admin_email&status=simulated'>";
245                echo $h->lang['admin_email'] . "</a>\n";
246                echo $h->lang["admin_email_after_simulation2"] . "</p>\n";
247            } else {
248                echo "<p>" . $h->lang["admin_email_after_real"] . "\n";
249                echo "<a href='" . BASEURL . "admin_index.php?page=plugin_settings&plugin=admin_email&status=done'>";
250                echo $h->lang['admin_email'] . "</a>\n";
251                echo $h->lang["admin_email_after_real2"] . "</p>\n";
252            }
253        }
254       
255        exit;
256    }
257
258   
259    /**
260     * User Settings - before saving
261     */
262    public function user_settings_pre_save($h)
263    {
264        // Emails from Admins:
265        if ($h->cage->post->getAlpha('admin_notify') == 'yes') {
266            $h->vars['settings']['admin_notify'] = "checked";
267        } else {
268            $h->vars['settings']['admin_notify'] = "";
269        }
270    }
271   
272   
273    /**
274     * User Settings - fill the form
275     */
276    public function user_settings_fill_form($h)
277    {
278        if (!isset($h->vars['settings']) || !$h->vars['settings']) { return false; }
279       
280        if ($h->vars['settings']['admin_notify']) {
281            $h->vars['admin_notify_yes'] = "checked";
282            $h->vars['admin_notify_no'] = "";
283        } else {
284            $h->vars['admin_notify_yes'] = "";
285            $h->vars['admin_notify_no'] = "checked";
286        }
287    }
288   
289   
290    /**
291     * User Settings - html for form
292     */
293    public function user_settings_extra_settings($h)
294    {
295        if (!isset($h->vars['settings']) || !$h->vars['settings']) { return false; }
296       
297        echo "<tr>\n";
298            // ACCEPT EMAIL FROM ADMINS?
299        echo "<td>" . $h->lang['users_settings_email_from_admin'] . "</td>\n";
300        echo "<td><input type='radio' name='admin_notify' value='yes' " . $h->vars['admin_notify_yes'] . "> " . $h->lang['users_settings_yes'] . " &nbsp;&nbsp;\n";
301        echo "<input type='radio' name='admin_notify' value='no' " . $h->vars['admin_notify_no'] . "> " . $h->lang['users_settings_no'] . "</td>\n";
302        echo "</tr>\n";
303    }
304}
305?>
Note: See TracBrowser for help on using the repository browser.