1<?php
2    /*
3     * $Id: wblist.php 1501 2010-12-30 16:15:07Z dmorton $
4     *
5     * MAIA MAILGUARD LICENSE v.1.0
6     *
7     * Copyright 2004 by Robert LeBlanc <rjl@renaissoft.com>
8     *                   David Morton   <mortonda@dgrmm.net>
9     * All rights reserved.
10    *
11     * PREAMBLE
12     *
13     * This License is designed for users of Maia Mailguard
14     * ("the Software") who wish to support the Maia Mailguard project by
15     * leaving "Maia Mailguard" branding information in the HTML output
16     * of the pages generated by the Software, and providing links back
17     * to the Maia Mailguard home page.  Users who wish to remove this
18     * branding information should contact the copyright owner to obtain
19     * a Rebranding License.
20     *
21     * DEFINITION OF TERMS
22     *
23     * The "Software" refers to Maia Mailguard, including all of the
24     * associated PHP, Perl, and SQL scripts, documentation files, graphic
25     * icons and logo images.
26     *
27     * GRANT OF LICENSE
28     *
29     * Redistribution and use in source and binary forms, with or without
30     * modification, are permitted provided that the following conditions
31     * are met:
32     *
33     * 1. Redistributions of source code must retain the above copyright
34     *    notice, this list of conditions and the following disclaimer.
35     *
36     * 2. Redistributions in binary form must reproduce the above copyright
37     *    notice, this list of conditions and the following disclaimer in the
38     *    documentation and/or other materials provided with the distribution.
39     *
40     * 3. The end-user documentation included with the redistribution, if
41     *    any, must include the following acknowledgment:
42     *
43     *    "This product includes software developed by Robert LeBlanc
44     *    <rjl@renaissoft.com>."
45     *
46     *    Alternately, this acknowledgment may appear in the software itself,
47     *    if and wherever such third-party acknowledgments normally appear.
48     *
49     * 4. At least one of the following branding conventions must be used:
50     *
51     *    a. The Maia Mailguard logo appears in the page-top banner of
52     *       all HTML output pages in an unmodified form, and links
53     *       directly to the Maia Mailguard home page; or
54     *
55     *    b. The "Powered by Maia Mailguard" graphic appears in the HTML
56     *       output of all gateway pages that lead to this software,
57     *       linking directly to the Maia Mailguard home page; or
58     *
59     *    c. A separate Rebranding License is obtained from the copyright
60     *       owner, exempting the Licensee from 4(a) and 4(b), subject to
61     *       the additional conditions laid out in that license document.
62     *
63     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
64     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
65     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
66     * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
67     * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
68     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
69     * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
70     * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
71     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
72     * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
73     * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74     *
75     */
76
77    require_once ("core.php");
78    require_once ("authcheck.php");
79    require_once ("display.php");
80    require_once ("maia_db.php");
81    require_once ("mailtools.php");
82    $display_language = get_display_language($euid);
83    require_once ("./locale/$display_language/display.php");
84    require_once ("./locale/$display_language/db.php");
85    require_once ("./locale/$display_language/wblist.php");
86
87    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
88      switch ($_REQUEST['action']) {
89        case 'addallow';
90          $result = add_address_to_wb_list($euid, $_REQUEST['newaddr'], 'W');
91          break;
92        case 'addblock';
93          $result = add_address_to_wb_list($euid, $_REQUEST['newaddr'], 'B');
94          break;
95        case 'block';
96          $result = set_wb_status($euid, $_REQUEST['id'], 'B');
97          $actiontext = $lang['text_allow_address'];
98          break;
99        case 'allow';
100          $result = set_wb_status($euid, $_REQUEST['id'], 'W');
101          $actiontext = $lang['text_block_address'];
102          break;
103        case 'remove';
104          $result = delete_wb_entry($euid, $_REQUEST['id']);
105          $actiontext = $lang['text_remove_rule'];
106          break;
107      }
108      if (isset($_REQUEST['ajax']) && $_REQUEST['ajax'] == 'true') {
109         ?>
110          $('#viewmessage').html('<div class="messagebox"><?php echo htmlspecialchars($lang[$result]); ?></div>');
111          $('#viewmessage div').effect("highlight", {
112                     color: "#FFD324"
113                 },
114                 2000);
115          <?php
116          if ($_REQUEST['id']) {
117            $id = $_REQUEST['id'];
118            if ($_REQUEST['action'] == 'remove') {
119               ?>
120                  if ($('#wb<?php echo $id; ?>').size() > 0) {
121                    $('#wb<?php echo $id; ?>').remove();
122                  }
123                <?php
124            } else {
125                ?>
126                  if ($('#wb<?php echo $id; ?>').size() > 0) {
127                    $('#wb<?php echo $id; ?> span.wbball').toggleClass('wbBball').toggleClass('wbWball');
128                    $('#wb<?php echo $id; ?> span.actionicon').toggleClass('wballow').toggleClass('wbblock');
129                    $('#wb<?php echo $id; ?> span.actiontext').html("<?php echo $actiontext; ?>");
130                    $('#wb<?php echo $id; ?> a.wb_action').attr('href', "wblist.php?action=allow&id=<?php echo $id ?>&ajax=true")
131                  }
132                <?php
133            }
134          }
135          exit;
136      } else {
137        $_SESSION["message"] = $lang[$result];
138        header("Location: wblist.php{$sid}");
139        exit;
140      }
141    } else {
142      if (isset($_REQUEST['action'])) {
143        $message = $lang['text_activate_javascript'];
144      }
145    }
146
147    $rows = get_user_wb_rows($dbh, $euid);
148
149    $domain_rows = get_domain_wb_rows($dbh, $euid);
150
151    $system_rows = get_system_wb_rows($dbh);
152
153    require_once("smarty.php");
154    $smarty->assign("show_user_table", count($rows) > 0 ? true : false );
155    $smarty->assign("rows", $rows);
156    $smarty->assign("show_domain_table", count($domain_rows) > 0 ? (! is_a_domain_default_user($euid)) : false );
157    $smarty->assign("domain_rows", $domain_rows);
158    $smarty->assign("show_system_table", count($system_rows) > 0 ? (! is_system_default_user($euid)) : false );
159    $smarty->assign("system_rows", $system_rows);
160    $smarty->display("wblist.tpl");
161
162    exit;
163
164function get_user_wb_rows($dbh, $user_id) {
165    $sth = $dbh->prepare("SELECT mailaddr.email, mailaddr.id, wblist.wb " .
166              "FROM mailaddr, wblist " .
167              "WHERE mailaddr.id = wblist.sid " .
168              "AND wblist.rid = ? " .
169              "ORDER BY mailaddr.email ASC");
170
171    $res = $sth->execute(array($user_id));
172    if (PEAR::isError($sth)) {
173        die($sth->getMessage());
174    }
175    $rows = array();
176
177    if ($res->numRows() > 0) {
178      $count = 0;
179        while ($row = $res->fetchRow())
180        {
181          $rows[$count]['email'] = $row['email'];
182          $rows[$count]['id'] = $row['id'];
183          $rows[$count]['type'] = $row['wb'];
184          $count++;
185        }
186    }
187    $sth->free();
188    return $rows;
189}
190
191function get_domain_wb_rows($dbh, $maia_user_id) {
192  global $logger;
193  if (substr(get_database_type($dbh),0,5) == "mysql") {
194    $query =<<<EOQ
195      SELECT mailaddr.email, wb, user_name
196        FROM mailaddr LEFT JOIN wblist ON mailaddr.id = wblist.sid
197             LEFT JOIN maia_users ON wblist.rid=maia_users.id
198        WHERE maia_users.user_name
199          IN (SELECT DISTINCT substring(email,locate('@',email))
200                 FROM maia_users left join users on users.maia_user_id = maia_users.id
201                     WHERE maia_users.id=?)
202        ORDER BY mailaddr.email ASC
203EOQ;
204  } else {
205    $query =<<<EOQ
206      SELECT mailaddr.email, wb, user_name
207        FROM mailaddr LEFT JOIN wblist ON mailaddr.id = wblist.sid
208             LEFT JOIN maia_users ON wblist.rid=maia_users.id
209        WHERE maia_users.user_name
210          IN (SELECT DISTINCT substr(email,strpos(email,'@'))
211                 FROM maia_users left join users on users.maia_user_id = maia_users.id
212                     WHERE maia_users.id=?)
213        ORDER BY mailaddr.email ASC
214EOQ;
215  }
216  $rows = array();
217
218  $sth = $dbh->prepare($query);
219  $res = $sth->execute(array($maia_user_id));
220   if (PEAR::isError($sth)) {
221     $logger->err("Error getting domain wblist: " . $sth->getMessage() . " Query: " . $query . "User: " . $maia_user_id );
222     return $rows;
223   }
224  if ($res->numRows() > 0) {
225    $count = 0;
226      while ($row = $res->fetchRow())
227      {
228        $rows[$count]['email'] = $row['email'];
229        $rows[$count]['domain'] = $row['user_name'];
230        $rows[$count]['type'] = $row['wb'];
231        $count++;
232      }
233  }
234  $sth->free();
235  return $rows;
236}
237
238function get_system_wb_rows($dbh) {
239    $select =<<<EOQ
240      SELECT mailaddr.email, mailaddr.id, wblist.wb
241          FROM mailaddr LEFT JOIN wblist ON  mailaddr.id = wblist.sid
242          LEFT JOIN maia_users ON wblist.rid=maia_users.id
243          WHERE maia_users.user_name='@.'
244          ORDER BY mailaddr.email ASC
245EOQ;
246
247    $sth = $dbh->prepare($select);
248    $res = $sth->execute();
249    if (PEAR::isError($sth)) {
250         die($sth->getMessage());
251    }
252    $rows = array();
253
254    if ($res->numRows() > 0) {
255      $count = 0;
256        while ($row = $res->fetchRow())
257        {
258          $rows[$count]['email'] = $row['email'];
259          $rows[$count]['domain'] = $row['user_name'];
260          $rows[$count]['type'] = $row['wb'];
261          $count++;
262        }
263    }
264    $sth->free();
265    return $rows;
266}
267