public_html/admin/mail.php
author Dirk Haun <dirk@haun-online.de>
Sun, 04 Oct 2009 13:32:11 +0200
branchHEAD
changeset 7358 b6d086ec0e01
parent 6978 04bf6514d64c
child 7596 a411f5d356bb
permissions -rw-r--r--
More consistency for the Mail Users dialog (cf. bug #0000992)
     1 <?php
     2 
     3 /* Reminder: always indent with 4 spaces (no tabs). */
     4 // +---------------------------------------------------------------------------+
     5 // | Geeklog 1.6                                                               |
     6 // +---------------------------------------------------------------------------+
     7 // | mail.php                                                                  |
     8 // |                                                                           |
     9 // | Geeklog mail administration page.                                         |
    10 // +---------------------------------------------------------------------------+
    11 // | Copyright (C) 2001-2009 by the following authors:                         |
    12 // |                                                                           |
    13 // | Authors: Tony Bibbs - tony AT tonybibbs DOT com                           |
    14 // |          Dirk Haun  - dirk AT haun-online DOT de                          |
    15 // +---------------------------------------------------------------------------+
    16 // |                                                                           |
    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.                    |
    21 // |                                                                           |
    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.                              |
    26 // |                                                                           |
    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.           |
    30 // |                                                                           |
    31 // +---------------------------------------------------------------------------+
    32 
    33 /**
    34 * Simple email form that lets you send emails to certain groups of users.
    35 *
    36 */
    37 
    38 /**
    39 * Geeklog common function library
    40 */
    41 require_once '../lib-common.php';
    42 
    43 /**
    44 * Security check to ensure user even belongs on this page
    45 */
    46 require_once 'auth.inc.php';
    47 
    48 $display = '';
    49 
    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])
    54              . COM_siteFooter();
    55     COM_accessLog("User {$_USER['username']} tried to illegally access the mail administration screen.");
    56     COM_output($display);
    57     exit;
    58 }
    59 
    60 /**
    61 * Shows the form the admin uses to send Geeklog members a message. Right now
    62 * you can only email an entire group.
    63 *
    64 * @return   string      HTML for the email form
    65 *
    66 */
    67 function display_mailform ()
    68 {
    69     global $_CONF, $LANG31, $LANG_ADMIN, $_IMAGE_TYPE;
    70 
    71     require_once $_CONF['path_system'] . 'lib-admin.php';
    72 
    73     $retval = '';
    74 
    75     $retval .= COM_startBlock($LANG31[1], '',
    76                         COM_getBlockTemplate('_admin_block', 'header'));
    77 
    78     $menu_arr = array(
    79         array('url'  => $_CONF['site_admin_url'],
    80               'text' => $LANG_ADMIN['admin_home'])
    81     );
    82 
    83     $desc = '<p>' . $LANG31[19] . '</p>';
    84     $icon = $_CONF['layout_url'] . '/images/icons/mail.' . $_IMAGE_TYPE;
    85     $retval .= ADMIN_createMenu($menu_arr, $desc, $icon);
    86 
    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']
    95                                          . '/mail.php');
    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]);
    99 
   100     $thisUsersGroups = SEC_getUserGroups();
   101     uksort($thisUsersGroups, 'strcasecmp');
   102     $group_options = '';
   103     foreach ($thisUsersGroups as $groupName => $groupID) {
   104         if ($groupName != 'All Users') {
   105             $group_options .= '<option value="' . $groupID . '">'
   106                            . ucwords($groupName) . '</option>';
   107         }
   108     }
   109 
   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());
   130 
   131     $mail_templates->parse('output', 'form');
   132     $retval .= $mail_templates->finish($mail_templates->get_var('output'));
   133 
   134     $retval .= COM_endBlock(COM_getBlockTemplate('_admin_block', 'footer'));
   135 
   136     return $retval;
   137 }
   138 
   139 /**
   140 * This function actually sends the messages to the specified group
   141 *
   142 * @param    array   $vars   Same as $_POST, holds all the email info
   143 * @return   string          HTML with success or error message
   144 *
   145 */
   146 function send_messages($vars)
   147 {
   148     global $_CONF, $_TABLES, $LANG31;
   149 
   150     require_once $_CONF['path_system'] . 'lib-user.php';
   151 
   152     $retval = '';
   153 
   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]);
   158 
   159         return $retval;
   160     }
   161 
   162     $to_group = COM_applyFilter($vars['to_group'], true);
   163     if ($to_group > 0) {
   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');
   168         }
   169     } else {
   170         return COM_refresh($_CONF['site_admin_url'] . '/mail.php');
   171     }
   172 
   173     // Urgent message!
   174     if (isset ($vars['priority'])) {
   175         $priority = 1;
   176     } else {
   177         $priority = 0;
   178     }
   179 
   180     // If you want to send html mail
   181     if (isset ($vars['html'])) {
   182         $html = true;
   183     } else {
   184         $html = false;
   185     }
   186 
   187     $groupList = implode(',', USER_getChildGroups($to_group));
   188 
   189     // and now mail it
   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})";
   194     } else {
   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})";
   199     }
   200 
   201     $result = DB_query ($sql);
   202     $nrows = DB_numRows ($result);
   203 
   204     $from = COM_formatEmailAddress ($vars['fra'], $vars['fraepost']);
   205     $subject = COM_stripslashes ($vars['subject']);
   206     $message = COM_stripslashes ($vars['message']);
   207 
   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']);
   215         } else {
   216             $to = COM_formatEmailAddress ($A['fullname'], $A['email']);
   217         }
   218 
   219         if (!COM_mail ($to, $subject, $message, $from, $html, $priority)) {
   220             $failures[] = htmlspecialchars ($to);
   221         } else {
   222             $successes[] = htmlspecialchars ($to);
   223         }
   224     }
   225 
   226     $retval .= COM_startBlock ($LANG31[1]);
   227 
   228     $failcount = count ($failures);
   229     $successcount = count ($successes);
   230     $mailresult = str_replace ('<successcount>', $successcount, $LANG31[20]);
   231     $retval .= str_replace ('<failcount>', $failcount, $mailresult);
   232 
   233     $retval .= '<h2>' . $LANG31[21] . '</h2>';
   234     for ($i = 0; $i < count ($failures); $i++) {
   235         $retval .= current ($failures) . '<br' . XHTML . '>';
   236         next ($failures);
   237     }
   238     if (count ($failures) == 0) {
   239         $retval .= $LANG31[23];
   240     }
   241 
   242     $retval .= '<h2>' . $LANG31[22] . '</h2>';
   243     for ($i = 0; $i < count ($successes); $i++) {
   244         $retval .= current ($successes) . '<br' . XHTML . '>';
   245         next ($successes);
   246     }
   247     if (count ($successes) == 0) {
   248         $retval .= $LANG31[24];
   249     }
   250 
   251     $retval .= COM_endBlock ();
   252 
   253     return $retval;
   254 }
   255 
   256 // MAIN
   257 
   258 $display .= COM_siteHeader ('menu', $LANG31[1]);
   259 
   260 if (isset($_POST['mail']) && ($_POST['mail'] == 'mail') && SEC_checkToken()) {
   261     $display .= send_messages ($_POST);
   262 } else {
   263     $display .= COM_showMessageFromParameter();
   264     $display .= display_mailform ();
   265 }
   266 
   267 $display .= COM_siteFooter ();
   268 
   269 COM_output($display);
   270 
   271 ?>