More consistency for the Mail Users dialog (cf. bug #0000992)
3 /* Reminder: always indent with 4 spaces (no tabs). */
4 // +---------------------------------------------------------------------------+
6 // +---------------------------------------------------------------------------+
9 // | Geeklog mail administration page. |
10 // +---------------------------------------------------------------------------+
11 // | Copyright (C) 2001-2009 by the following authors: |
13 // | Authors: Tony Bibbs - tony AT tonybibbs DOT com |
14 // | Dirk Haun - dirk AT haun-online DOT de |
15 // +---------------------------------------------------------------------------+
17 // | This program is free software; you can redistribute it and/or |
18 // | modify it under the terms of the GNU General Public License |
19 // | as published by the Free Software Foundation; either version 2 |
20 // | of the License, or (at your option) any later version. |
22 // | This program is distributed in the hope that it will be useful, |
23 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 // | GNU General Public License for more details. |
27 // | You should have received a copy of the GNU General Public License |
28 // | along with this program; if not, write to the Free Software Foundation, |
29 // | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
31 // +---------------------------------------------------------------------------+
34 * Simple email form that lets you send emails to certain groups of users.
39 * Geeklog common function library
41 require_once '../lib-common.php';
44 * Security check to ensure user even belongs on this page
46 require_once 'auth.inc.php';
50 // Make sure user has access to this page
51 if (!SEC_inGroup('Mail Admin') && !SEC_hasrights('user.mail')) {
52 $display .= COM_siteHeader('menu', $MESSAGE[30])
53 . COM_showMessageText($MESSAGE[29], $MESSAGE[30])
55 COM_accessLog("User {$_USER['username']} tried to illegally access the mail administration screen.");
61 * Shows the form the admin uses to send Geeklog members a message. Right now
62 * you can only email an entire group.
64 * @return string HTML for the email form
67 function display_mailform ()
69 global $_CONF, $LANG31, $LANG_ADMIN, $_IMAGE_TYPE;
71 require_once $_CONF['path_system'] . 'lib-admin.php';
75 $retval .= COM_startBlock($LANG31[1], '',
76 COM_getBlockTemplate('_admin_block', 'header'));
79 array('url' => $_CONF['site_admin_url'],
80 'text' => $LANG_ADMIN['admin_home'])
83 $desc = '<p>' . $LANG31[19] . '</p>';
84 $icon = $_CONF['layout_url'] . '/images/icons/mail.' . $_IMAGE_TYPE;
85 $retval .= ADMIN_createMenu($menu_arr, $desc, $icon);
87 $mail_templates = new Template($_CONF['path_layout'] . 'admin/mail');
88 $mail_templates->set_file(array('form' => 'mailform.thtml'));
89 $mail_templates->set_var('site_url', $_CONF['site_url']);
90 $mail_templates->set_var('site_admin_url', $_CONF['site_admin_url']);
91 $mail_templates->set_var('layout_url', $_CONF['layout_url']);
92 $mail_templates->set_var('startblock_email', COM_startBlock($LANG31[1],
93 '', COM_getBlockTemplate('_admin_block', 'header')));
94 $mail_templates->set_var('php_self', $_CONF['site_admin_url']
96 $mail_templates->set_var('lang_note', $LANG31[19]);
97 $mail_templates->set_var('lang_to', $LANG31[18]);
98 $mail_templates->set_var('lang_selectgroup', $LANG31[25]);
100 $thisUsersGroups = SEC_getUserGroups();
101 uksort($thisUsersGroups, 'strcasecmp');
103 foreach ($thisUsersGroups as $groupName => $groupID) {
104 if ($groupName != 'All Users') {
105 $group_options .= '<option value="' . $groupID . '">'
106 . ucwords($groupName) . '</option>';
110 $mail_templates->set_var('group_options', $group_options);
111 $mail_templates->set_var('lang_from', $LANG31[2]);
112 $mail_templates->set_var('site_name', $_CONF['site_name']);
113 $mail_templates->set_var('lang_replyto', $LANG31[3]);
114 $mail_templates->set_var('site_mail', $_CONF['site_mail']);
115 $mail_templates->set_var('lang_subject', $LANG31[4]);
116 $mail_templates->set_var('lang_body', $LANG31[5]);
117 $mail_templates->set_var('lang_sendto', $LANG31[6]);
118 $mail_templates->set_var('lang_allusers', $LANG31[7]);
119 $mail_templates->set_var('lang_admin', $LANG31[8]);
120 $mail_templates->set_var('lang_options', $LANG31[9]);
121 $mail_templates->set_var('lang_HTML', $LANG31[10]);
122 $mail_templates->set_var('lang_urgent', $LANG31[11]);
123 $mail_templates->set_var('lang_ignoreusersettings', $LANG31[14]);
124 $mail_templates->set_var('lang_send', $LANG31[12]);
125 $mail_templates->set_var('end_block',
126 COM_endBlock(COM_getBlockTemplate('_admin_block', 'footer')));
127 $mail_templates->set_var('xhtml', XHTML);
128 $mail_templates->set_var('gltoken_name', CSRF_TOKEN);
129 $mail_templates->set_var('gltoken', SEC_createToken());
131 $mail_templates->parse('output', 'form');
132 $retval .= $mail_templates->finish($mail_templates->get_var('output'));
134 $retval .= COM_endBlock(COM_getBlockTemplate('_admin_block', 'footer'));
140 * This function actually sends the messages to the specified group
142 * @param array $vars Same as $_POST, holds all the email info
143 * @return string HTML with success or error message
146 function send_messages($vars)
148 global $_CONF, $_TABLES, $LANG31;
150 require_once $_CONF['path_system'] . 'lib-user.php';
154 if (empty($vars['fra']) OR empty($vars['fraepost']) OR
155 empty($vars['subject']) OR empty($vars['message']) OR
156 empty($vars['to_group']) OR (strpos($vars['fra'], '@') !== false)) {
157 $retval .= COM_showMessageText($LANG31[26]);
162 $to_group = COM_applyFilter($vars['to_group'], true);
164 $group_name = DB_getItem($_TABLES['groups'], 'grp_name',
165 "grp_id = $to_group");
166 if (! SEC_inGroup($group_name)) {
167 return COM_refresh($_CONF['site_admin_url'] . '/mail.php');
170 return COM_refresh($_CONF['site_admin_url'] . '/mail.php');
174 if (isset ($vars['priority'])) {
180 // If you want to send html mail
181 if (isset ($vars['html'])) {
187 $groupList = implode(',', USER_getChildGroups($to_group));
190 if (isset ($vars['overstyr'])) {
191 $sql = "SELECT DISTINCT username,fullname,email FROM {$_TABLES['users']},{$_TABLES['group_assignments']} WHERE uid > 1";
192 $sql .= " AND {$_TABLES['users']}.status = 3 AND ((email IS NOT NULL) and (email != ''))";
193 $sql .= " AND {$_TABLES['users']}.uid = ug_uid AND ug_main_grp_id IN ({$groupList})";
195 $sql = "SELECT DISTINCT username,fullname,email,emailfromadmin FROM {$_TABLES['users']},{$_TABLES['userprefs']},{$_TABLES['group_assignments']} WHERE {$_TABLES['users']}.uid > 1";
196 $sql .= " AND {$_TABLES['users']}.status = 3 AND ((email IS NOT NULL) and (email != ''))";
197 $sql .= " AND {$_TABLES['users']}.uid = {$_TABLES['userprefs']}.uid AND emailfromadmin = 1";
198 $sql .= " AND ug_uid = {$_TABLES['users']}.uid AND ug_main_grp_id IN ({$groupList})";
201 $result = DB_query ($sql);
202 $nrows = DB_numRows ($result);
204 $from = COM_formatEmailAddress ($vars['fra'], $vars['fraepost']);
205 $subject = COM_stripslashes ($vars['subject']);
206 $message = COM_stripslashes ($vars['message']);
208 // Loop through and send the messages!
209 $successes = array ();
210 $failures = array ();
211 for ($i = 0; $i < $nrows; $i++) {
212 $A = DB_fetchArray ($result);
213 if (empty ($A['fullname'])) {
214 $to = COM_formatEmailAddress ($A['username'], $A['email']);
216 $to = COM_formatEmailAddress ($A['fullname'], $A['email']);
219 if (!COM_mail ($to, $subject, $message, $from, $html, $priority)) {
220 $failures[] = htmlspecialchars ($to);
222 $successes[] = htmlspecialchars ($to);
226 $retval .= COM_startBlock ($LANG31[1]);
228 $failcount = count ($failures);
229 $successcount = count ($successes);
230 $mailresult = str_replace ('<successcount>', $successcount, $LANG31[20]);
231 $retval .= str_replace ('<failcount>', $failcount, $mailresult);
233 $retval .= '<h2>' . $LANG31[21] . '</h2>';
234 for ($i = 0; $i < count ($failures); $i++) {
235 $retval .= current ($failures) . '<br' . XHTML . '>';
238 if (count ($failures) == 0) {
239 $retval .= $LANG31[23];
242 $retval .= '<h2>' . $LANG31[22] . '</h2>';
243 for ($i = 0; $i < count ($successes); $i++) {
244 $retval .= current ($successes) . '<br' . XHTML . '>';
247 if (count ($successes) == 0) {
248 $retval .= $LANG31[24];
251 $retval .= COM_endBlock ();
258 $display .= COM_siteHeader ('menu', $LANG31[1]);
260 if (isset($_POST['mail']) && ($_POST['mail'] == 'mail') && SEC_checkToken()) {
261 $display .= send_messages ($_POST);
263 $display .= COM_showMessageFromParameter();
264 $display .= display_mailform ();
267 $display .= COM_siteFooter ();
269 COM_output($display);