1<?php
2/**
3 * Postfix Admin
4 *
5 * LICENSE
6 * This source file is subject to the GPL license that is bundled with
7 * this package in the file LICENSE.TXT.
8 *
9 * Further details on the project are available at http://postfixadmin.sf.net
10 *
11 * @version $Id$
12 * @license GNU GPL v2 or later.
13 *
14 * File: viewlog.php
15 * Shows entries from the log table to users.
16 *
17 * Template File: viewlog.tpl
18 *
19 * Template Variables:
20 *
21 * tLog
22 *
23 * Form POST \ GET Variables:
24 *
25 * fDomain
26 */
27
28require_once('common.php');
29
30authentication_require_role('admin');
31
32$CONF = Config::getInstance()->getAll();
33$smarty = PFASmarty::getInstance();
34
35$PALANG = $CONF['__LANG'];
36
37$SESSID_USERNAME = authentication_get_username();
38if (authentication_has_role('global-admin')) {
39    $list_domains = list_domains();
40} else {
41    $list_domains = list_domains_for_admin($SESSID_USERNAME);
42}
43
44$fDomain = '';
45$error = 0;
46
47if ($_SERVER['REQUEST_METHOD'] == "GET") {
48    if ((is_array($list_domains) and sizeof($list_domains) > 0) and !authentication_has_role('global-admin')) {
49        $fDomain = $list_domains[0];
50    }
51} elseif ($_SERVER['REQUEST_METHOD'] == "POST") {
52    if (isset($_POST['fDomain'])) {
53        $fDomain = escape_string($_POST['fDomain']);
54    }
55} else {
56    die('Unknown request method');
57}
58
59if (! (check_owner($SESSID_USERNAME, $fDomain) || authentication_has_role('global-admin'))) {
60    $error = 1;
61    flash_error($PALANG['pViewlog_result_error']);
62}
63
64$tLog = array();
65
66if ($error != 1) {
67    $table_log = table_by_key('log');
68    $page_size = isset($CONF['page_size']) ? intval($CONF['page_size']) : 35;
69
70    $where = [];
71    $params = [];
72    if ($fDomain) {
73        $where[] = 'domain = :domain' ;
74        $params['domain'] = $fDomain;
75    }
76
77    $where_sql = '';
78    if (!empty($where)) {
79        $where_sql = 'WHERE ' . implode(' AND ', $where);
80    }
81
82    $query = "SELECT timestamp,username,domain,action,data FROM $table_log $where_sql ORDER BY timestamp DESC LIMIT $page_size";
83
84    if (db_pgsql()) {
85        $query = "SELECT extract(epoch from timestamp) as timestamp,username,domain,action,data FROM $table_log $where_sql ORDER BY timestamp DESC LIMIT $page_size";
86    }
87    $result = db_query_all($query, $params);
88    foreach ($result as $row) {
89        if (is_array($row) && db_pgsql()) {
90            $row['timestamp'] = gmstrftime('%c %Z', $row['timestamp']);
91        }
92        $tLog[] = $row;
93    }
94}
95
96foreach ($tLog as $k => $v) {
97    if (isset($v['action'])) {
98        $v['action'] = $PALANG['pViewlog_action_' . $v['action']];
99        $tLog[$k] = $v;
100    }
101}
102
103$domain_options = array();
104if (authentication_has_role('global-admin')) {
105    $domain_options = array('' => '');
106}
107$domain_options = array_merge($domain_options, array_combine($list_domains, $list_domains));
108
109$smarty->assign('domain_options', $domain_options);
110$smarty->assign('domain_selected', $fDomain);
111$smarty->assign('tLog', $tLog, false);
112$smarty->assign('fDomain', $fDomain);
113$smarty->assign('smarty_template', 'viewlog');
114$smarty->display('index.tpl');
115
116/* vim: set expandtab softtabstop=3 tabstop=3 shiftwidth=3: */
117