1<?php
2    /*
3     * $Id: stats.php 1439 2009-11-17 23:31:04Z 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 ("maia_db.php");
79    require_once ("authcheck.php");
80    require_once ("display.php");
81    $display_language = get_display_language($euid);
82    require_once ("./locale/$display_language/db.php");
83    require_once ("./locale/$display_language/display.php");
84    require_once ("./locale/$display_language/stats.php");
85
86    require_once ("smarty.php");
87
88    if (isset($_GET["id"])) {
89        $id = trim($_GET["id"]);
90        if (($id != 0) && (!is_an_administrator($uid))) {
91            $id = $euid;
92        }
93    } else {
94        $id = $euid;
95    }
96    $smarty->assign("user_id", $id);
97
98    $select = "SELECT currency_label, enable_false_negative_management, " .
99              "enable_virus_scanning, enable_spam_filtering, " .
100              "enable_bad_header_checking, enable_banned_files_checking, enable_charts " .
101              "FROM maia_config WHERE id = 0";
102    $sth = $dbh->prepare($select);
103    $res = $sth->execute();
104    if (PEAR::isError($sth)) {
105        die($sth->getMessage());
106    }
107
108    if ($row = $res->fetchrow()) {
109        $currency_label = $row["currency_label"];
110        $enable_false_negative_management = ($row["enable_false_negative_management"] == 'Y');
111        $enable_virus_scanning = ($row["enable_virus_scanning"] == 'Y');
112        $enable_spam_filtering = ($row["enable_spam_filtering"] == 'Y');
113        $enable_bad_header_checking = ($row["enable_bad_header_checking"] == 'Y');
114        $enable_banned_files_checking = ($row["enable_banned_files_checking"] == 'Y');
115        $enable_charts = ($row["enable_charts"] == 'Y');
116    }
117    $sth->free();
118
119    if ($enable_charts) {
120      $select = "SELECT charts FROM maia_users WHERE id=?";
121      $sth = $dbh->prepare($select);
122      $res = $sth->execute($euid);
123      if (PEAR::isError($sth)) {
124          die($sth->getMessage());
125      }
126
127      if ($row = $res->fetchrow()) {
128        $enable_charts = ($row["charts"] == 'Y');
129      }
130      $sth->free();
131    }
132
133    update_mail_stats($id, "suspected_ham");
134    update_mail_stats($id, "suspected_spam");
135
136    $showmail = ok_to_impersonate($euid, $uid);
137
138    if ($id > 0) {
139        $header = sprintf($lang['header_user'], get_user_name($id));
140    } else {
141        $header = $lang['header_systemwide'];
142    }
143    $smarty->assign("header", $header);
144    $smarty->assign("currency_label", $currency_label);
145    $smarty->assign("is_a_visitor", false);
146    $smarty->assign("msid", $msid);
147    $smarty->assign("sid", $sid);
148    $smarty->assign("enable_virus_scanning", $enable_virus_scanning);
149    $smarty->assign("enable_spam_filtering", $enable_spam_filtering);
150    $smarty->assign("enable_charts", $enable_charts);
151//    $smarty->assign("showmail", $showmail);
152
153    list($ppv_pct, $npv_pct, $sensitivity_pct, $specificity_pct, $efficiency_pct, $fp_pct, $fn_pct) = get_filter_stats($id);
154    $smarty->assign("ppv_pct", $ppv_pct);
155    $smarty->assign("npv_pct", $npv_pct);
156    $smarty->assign("sensitivity_pct", $sensitivity_pct);
157    $smarty->assign("specificity_pct", $specificity_pct);
158    $smarty->assign("eff_pct", $efficiency_pct);
159    $smarty->assign("fp_pct", $fp_pct);
160    $smarty->assign("fn_pct", $fn_pct);
161
162    $data = array();
163    if ($enable_spam_filtering && $enable_false_negative_management) {
164        $data['suspected_ham'] = get_item_row($id, 'suspected_ham');
165    }
166    $data['ham'] = get_item_row($id, 'ham');
167    if ($enable_spam_filtering) {
168        $data['fp'] = get_item_row($id, "fp");
169        $data['suspected_spam'] = get_item_row($id, "suspected_spam");
170        $data['spam'] = get_item_row($id, "spam");
171        if ($enable_false_negative_management) {
172            $data['fn'] = get_item_row($id, "fn");
173        }
174    }
175    $data['wl'] = get_item_row($id, "wl");
176    $data['bl'] = get_item_row($id, "bl");
177    if ($enable_virus_scanning) {
178        $data['virus'] = get_item_row($id, "virus");
179    }
180    if ($enable_banned_files_checking) {
181        $data['banned_file'] = get_item_row($id, "banned_file");
182    }
183    if ($enable_bad_header_checking) {
184        $data['bad_header'] = get_item_row($id, "bad_header");
185    }
186    $data['oversized'] = get_item_row($id, "oversized");
187
188    $links = array( 'suspected_ham'   => "ham",
189                    'suspected_spam'    => "spam",
190                    'virus' => "virus",
191                    'banned_file' =>    "attachment",
192                    'bad_header'    => "header",
193                    'ham' => false,  // these false values are here to
194                    'bl' => false,   // prevent smarty from issueing
195                    'fp' => false,   // warnings for undefined indices.
196                    'spam' => false, // I wish smarty would be smarter about this. :(
197                    'fn' => false,
198                    'wl' => false,
199                    'oversized' => false
200
201                    );
202    $smarty->assign('links', $links);
203
204
205    $smarty->assign('data', $data);
206
207    $smarty->display("stats.tpl");
208
209   /*
210    * get_item_row(): Produces a row of statistics about mail items
211    *                     of the specified type received by the specified
212    *                     user, or all users if $user_id == 0.
213    */
214   function get_item_row($user_id, $type)
215   {
216       global $dbh;
217       global $lang;
218
219       $bandwidth_cost = get_config_value("bandwidth_cost");
220       $items = count_items($user_id, $type);
221
222       if ($items > 0) {
223           $ret = array();
224           $ret['items'] = $items;
225           $total_mail = count_total_mail($user_id);
226           if ($total_mail > 0) {
227               $ret['pct'] = sprintf("%.1f%%", 100 * $items / $total_mail);
228           } else {
229       	       $ret['pct'] = "0.0%";
230           }
231           $item_size = total_item_size($user_id, $type);
232           $item_days = count_item_days($user_id, $type);
233           if ($item_days > 0) {
234               if ($item_days >= 1) {
235                   $ret['rate'] = sprintf("%.1f", $items / $item_days);
236                   $bandwidth_per_day = $item_size / $item_days;
237               } else {
238                   $ret['rate'] = $items;
239                   $bandwidth_per_day = $item_size;
240               }
241           } else {
242       	       $ret['rate'] = "-";
243       	       $bandwidth_per_day = 0;
244           }
245           if ($type == "ham" || $type == "spam" || $type == "suspected_spam" || $type == "suspected_ham" || $type == "fp" || $type == "fn") {
246               $ret['minscore'] = sprintf("%.3f", lowest_item_score($user_id, $type));
247               $ret['maxscore'] = sprintf("%.3f", highest_item_score($user_id, $type));
248               $ret['avgscore'] = sprintf("%.3f", total_item_score($user_id, $type) / $items);
249           } else {
250	          $ret['minscore'] = "-";
251	          $ret['maxscore'] = "-";
252	          $ret['avgscore'] = "-";
253           }
254           $ret['minsize'] = sprintf("%.1f", smallest_item_size($user_id, $type) / KILOBYTE);
255           $ret['maxsize'] = sprintf("%.1f", largest_item_size($user_id, $type) / KILOBYTE);
256           $ret['avgsize'] = sprintf("%.1f", $item_size / $items / KILOBYTE);
257           if ($bandwidth_per_day > 0) {
258               $ret['bandwidth'] = sprintf("%.2f", $bandwidth_per_day / MEGABYTE);
259           } else {
260      	       $ret['bandwidth'] = "-";
261           }
262           $ret['cost'] = sprintf("%.3f", $bandwidth_cost * $bandwidth_per_day / GIGABYTE);
263       } else {
264           $ret = array(
265              'items' => "-",
266              'rate' => "-",
267              'minscore' => "-",
268              'maxscore' => "-",
269              'avgscore' => "-",
270              'minsize' => "-",
271              'maxsize' => "-",
272              'avgsize' => "-",
273              'bandwidth' => "-",
274              'cost' => "-",
275              'pct' => "-"
276           );
277       }
278       return $ret;
279   }
280?>
281