<?php
    /*
     * $Id: display.php 1346 2008-11-19 00:16:21Z 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 ("constants.php");
   require_once ("maia_db.php");
   require_once ("virus.php");

   /*
    * linkify(): Replaces $label in $text with a hyperlink to $link
    */
   function linkify($text, $label, $link)
   {
       $preg_label = preg_replace("/\//", "\/", $label);
       return preg_replace("/" . $preg_label . "/s",
                           "<a href=\"" . $link . "\">" . $label . "</a>",
                           $text);
   }


   /*
    * add_links_to_text(): Looks for certain "hot" words in a text string
    *                      and replaces them with corresponding URLs.
    */
   function add_links_to_text($text)
   {
       // Explicit URLs
       if (preg_match("/\((http:\/\/.+?)\)/si", $text, $match)) {
           $text = linkify($text, $match[1], $match[1]);
       }

       // SORBS: Spam and Open Relay Blocking System
       $text = linkify($text, "SORBS", "http://www.sorbs.net/");

       // Razor2: Vipul's Razor
       $text = linkify($text, "Razor2", "http://razor.sourceforge.net/");

       // Pyzor
       $text = linkify($text, "Pyzor", "http://pyzor.sourceforge.net/");

       // DCC: Distributed Checksum Clearinghouse
       $text = linkify($text, "DCC", "http://rhyolite.com/anti-spam/dcc/");

       // NJABL: Not Just Another Bogus List
       $text = linkify($text, "NJABL", "http://www.njabl.org/");
       $text = linkify($text, "dnsbl.njabl.org", "http://www.njabl.org/");

       // SpamCop Blocking List
       $text = linkify($text, "bl.spamcop.net", "http://www.spamcop.net/bl.shtml");

       // OPM: Blitzed Open Proxy Monitor
       $text = linkify($text, "opm.blitzed.org", "http://www.blitzed.org/bopm/");
       $text = linkify($text, "OPM", "http://www.blitzed.org/bopm/");

       // DSBL: Distributed Server Boycott List
       $text = linkify($text, "list.dsbl.org", "http://www.dsbl.org/");

       // RFCI: RFC-Ignorant
       $text = linkify($text, "ipwhois.rfc-ignorant.org", "http://www.rfc-ignorant.org/policy-ipwhois.php");
       $text = linkify($text, "dsn.rfc-ignorant.org", "http://www.rfc-ignorant.org/policy-dsn.php");

       // SBL: Spamhaus Block List
       $text = linkify($text, "Spamhaus Block List", "http://www.spamhaus.org/sbl/");

       // MAPS: Mail Abuse Prevention System
       $text = linkify($text, "RBL", "http://www.mail-abuse.org/rbl/");
       $text = linkify($text, "DUL", "http://www.mail-abuse.org/dul/");
       $text = linkify($text, "RSS", "http://www.mail-abuse.org/rss/");
       $text = linkify($text, "NML", "http://www.mail-abuse.org/nml/");

       // BSP: Bonded Sender Program
       $text = linkify($text, "Bonded Sender Program", "http://www.bondedsender.com/");

       // Habeas Sender Warranted E-mail
       $text = linkify($text, "Habeas", "http://www.habeas.com/");

       // CBL: Composite Blocking List
       $text = linkify($text, "cbl.abuseat.org", "http://cbl.abuseat.org/");

       // Easynet DNSBL
       $text = linkify($text, "proxies.blackholes.easynet.nl", "http://abuse.easynet.nl/proxies.html");

       // AHBL: Abusive Hosts Blocking List
       $text = linkify($text, "AHBL", "http://www.ahbl.org");
       $text = linkify($text, "dnsbl.ahbl.org", "http://www.ahbl.org");

       return $text;
   }


   /*
    * display_spam_report(): Produces a table listing of the SpamAssassin
    *                        rules that were triggered by the specified
    *                        mail item, for use in the Mail Viewer.
    */
   function display_spam_report($mail_id)
   {
       global $dbh;
       $rows = array();

       $select = "SELECT rule_name, rule_description, maia_sa_rules_triggered.rule_score " .
                 "FROM maia_sa_rules, maia_sa_rules_triggered " .
                 "WHERE maia_sa_rules.id = maia_sa_rules_triggered.rule_id " .
                 "AND maia_sa_rules_triggered.mail_id = ? " .
                 "ORDER BY maia_sa_rules_triggered.rule_score DESC";
       $sth = $dbh->query($select, array($mail_id));
       if ($sth->numrows() > 0) {

           while ($row = $sth->fetchrow()) {
               $rows[] = array ( 'rule_score' => sprintf("%.3f", $row["rule_score"]),
                                 'rule_name'  => $row["rule_name"],
                                 'description' => add_links_to_text($row["rule_description"]));

           }
           $sth->free();

       }
       return $rows;

   }

?>
