<?php
    /*
     * $Id: wblist.php 1501 2010-12-30 16:15:07Z dmorton $
     *
     * MAIA MAILGUARD LICENSE v.1.0
     *
     * Copyright 2004 by Robert LeBlanc <rjl@renaissoft.com>
     *                   David Morton   <mortonda@dgrmm.net>
     * All rights reserved.
    *
     * PREAMBLE
     *
     * This License is designed for users of Maia Mailguard
     * ("the Software") who wish to support the Maia Mailguard project by
     * leaving "Maia Mailguard" branding information in the HTML output
     * of the pages generated by the Software, and providing links back
     * to the Maia Mailguard home page.  Users who wish to remove this
     * branding information should contact the copyright owner to obtain
     * a Rebranding License.
     *
     * DEFINITION OF TERMS
     *
     * The "Software" refers to Maia Mailguard, including all of the
     * associated PHP, Perl, and SQL scripts, documentation files, graphic
     * icons and logo images.
     *
     * GRANT OF LICENSE
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * 1. Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * 2. Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * 3. The end-user documentation included with the redistribution, if
     *    any, must include the following acknowledgment:
     *
     *    "This product includes software developed by Robert LeBlanc
     *    <rjl@renaissoft.com>."
     *
     *    Alternately, this acknowledgment may appear in the software itself,
     *    if and wherever such third-party acknowledgments normally appear.
     *
     * 4. At least one of the following branding conventions must be used:
     *
     *    a. The Maia Mailguard logo appears in the page-top banner of
     *       all HTML output pages in an unmodified form, and links
     *       directly to the Maia Mailguard home page; or
     *
     *    b. The "Powered by Maia Mailguard" graphic appears in the HTML
     *       output of all gateway pages that lead to this software,
     *       linking directly to the Maia Mailguard home page; or
     *
     *    c. A separate Rebranding License is obtained from the copyright
     *       owner, exempting the Licensee from 4(a) and 4(b), subject to
     *       the additional conditions laid out in that license document.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */

    require_once ("core.php");
    require_once ("authcheck.php");
    require_once ("display.php");
    require_once ("maia_db.php");
    require_once ("mailtools.php");
    $display_language = get_display_language($euid);
    require_once ("./locale/$display_language/display.php");
    require_once ("./locale/$display_language/db.php");
    require_once ("./locale/$display_language/wblist.php");

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      switch ($_REQUEST['action']) {
        case 'addallow';
          $result = add_address_to_wb_list($euid, $_REQUEST['newaddr'], 'W');
          break;
        case 'addblock';
          $result = add_address_to_wb_list($euid, $_REQUEST['newaddr'], 'B');
          break;
        case 'block';
          $result = set_wb_status($euid, $_REQUEST['id'], 'B');
          $actiontext = $lang['text_allow_address'];
          break;
        case 'allow';
          $result = set_wb_status($euid, $_REQUEST['id'], 'W');
          $actiontext = $lang['text_block_address'];
          break;
        case 'remove';
          $result = delete_wb_entry($euid, $_REQUEST['id']);
          $actiontext = $lang['text_remove_rule'];
          break;
      }
      if ($_REQUEST['ajax'] == 'true') {
         ?>
          $('#viewmessage').html('<div class="messagebox"><?php echo htmlspecialchars($lang[$result]); ?></div>');
          $('#viewmessage div').effect("highlight", {
                     color: "#FFD324"
                 },
                 2000);
          <?php
          if ($_REQUEST['id']) {
            $id = $_REQUEST['id'];
            if ($_REQUEST['action'] == 'remove') {
               ?>
                  if ($('#wb<?php echo $id; ?>').size() > 0) {
                    $('#wb<?php echo $id; ?>').remove();
                  }
                <?php
            } else {
                ?>
                  if ($('#wb<?php echo $id; ?>').size() > 0) {
                    $('#wb<?php echo $id; ?> span.wbball').toggleClass('wbBball').toggleClass('wbWball');
                    $('#wb<?php echo $id; ?> span.actionicon').toggleClass('wballow').toggleClass('wbblock');
                    $('#wb<?php echo $id; ?> span.actiontext').html("<?php echo $actiontext; ?>");
                    $('#wb<?php echo $id; ?> a.wb_action').attr('href', "wblist.php?action=allow&id=<?php echo $id ?>&ajax=true")
                  }
                <?php
            }
          }
          exit;
      } else {
        $_SESSION["message"] = $lang[$result];
        header("Location: wblist.php{$sid}");
        exit;
      }
    } else {
      if (isset($_REQUEST['action'])) {
        $message = $lang['text_activate_javascript'];
      }
    }

    $rows = get_user_wb_rows($dbh, $euid);

    $domain_rows = get_domain_wb_rows($dbh, $euid);

    $system_rows = get_system_wb_rows($dbh);

    require_once("smarty.php");
    $smarty->assign("show_user_table", count($rows) > 0 ? true : false );
    $smarty->assign("rows", $rows);
    $smarty->assign("show_domain_table", count($domain_rows) > 0 ? (! is_a_domain_default_user($euid)) : false );
    $smarty->assign("domain_rows", $domain_rows);
    $smarty->assign("show_system_table", count($system_rows) > 0 ? (! is_system_default_user($euid)) : false );
    $smarty->assign("system_rows", $system_rows);
    $smarty->display("wblist.tpl");

    exit;

function get_user_wb_rows($dbh, $user_id) {
    $select = "SELECT mailaddr.email, mailaddr.id, wblist.wb " .
              "FROM mailaddr, wblist " .
              "WHERE mailaddr.id = wblist.sid " .
              "AND wblist.rid = ? " .
              "ORDER BY mailaddr.email ASC";

    $sth = $dbh->query($select, array($user_id));
    $rows = array();

    if ($sth->numRows() > 0) {
      $count = 0;
        while ($row = $sth->fetchRow())
        {
          $rows[$count]['email'] = $row['email'];
          $rows[$count]['id'] = $row['id'];
          $rows[$count]['type'] = $row['wb'];
          $count++;
        }
    }
    $sth->free();
    return $rows;
}

function get_domain_wb_rows($dbh, $maia_user_id) {
  global $logger;
  if (substr(get_database_type($dbh),0,5) == "mysql") {
    $query =<<<EOQ
      SELECT mailaddr.email, wb, user_name
        FROM mailaddr LEFT JOIN wblist ON mailaddr.id = wblist.sid
             LEFT JOIN maia_users ON wblist.rid=maia_users.id
        WHERE maia_users.user_name
          IN (SELECT DISTINCT substring(email,locate('@',email))
                 FROM maia_users left join users on users.maia_user_id = maia_users.id
                     WHERE maia_users.id=?)
        ORDER BY mailaddr.email ASC
EOQ;
  } else {
    $query =<<<EOQ
      SELECT mailaddr.email, wb, user_name
        FROM mailaddr LEFT JOIN wblist ON mailaddr.id = wblist.sid
             LEFT JOIN maia_users ON wblist.rid=maia_users.id
        WHERE maia_users.user_name
          IN (SELECT DISTINCT substr(email,strpos(email,'@'))
                 FROM maia_users left join users on users.maia_user_id = maia_users.id
                     WHERE maia_users.id=?)
        ORDER BY mailaddr.email ASC
EOQ;
  }
  $rows = array();

  $sth = $dbh->query($query, array($maia_user_id));
   if (PEAR::isError($sth)) {
     $logger->err("Error getting domain wblist: " . $sth->getMessage() . " Query: " . $query . "USer: " . $maia_user_id );
     return $rows;
   }
  if ($sth->numRows() > 0) {
    $count = 0;
      while ($row = $sth->fetchRow())
      {
        $rows[$count]['email'] = $row['email'];
        $rows[$count]['domain'] = $row['user_name'];
        $rows[$count]['type'] = $row['wb'];
        $count++;
      }
  }
  $sth->free();
  return $rows;
}

function get_system_wb_rows($dbh) {
    $select =<<<EOQ
      SELECT mailaddr.email, mailaddr.id, wblist.wb
          FROM mailaddr LEFT JOIN wblist ON  mailaddr.id = wblist.sid 
          LEFT JOIN maia_users ON wblist.rid=maia_users.id
          WHERE maia_users.user_name='@.'
          ORDER BY mailaddr.email ASC
EOQ;

    $sth = $dbh->query($select);
    $rows = array();

    if ($sth->numRows() > 0) {
      $count = 0;
        while ($row = $sth->fetchRow())
        {
          $rows[$count]['email'] = $row['email'];
          $rows[$count]['domain'] = $row['user_name'];
          $rows[$count]['type'] = $row['wb'];
          $count++;
        }
    }
    $sth->free();
    return $rows;
}
