Subversion Repositories oidplus

Rev

Rev 1445 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
635 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
635 daniel-mar 6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
635 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
635 daniel-mar 26
class OIDplusPageAdminLogEvents extends OIDplusPagePluginAdmin {
27
 
1116 daniel-mar 28
        /**
29
         * @param bool $html
30
         * @return void
31
         */
32
        public function init(bool $html=true) {
635 daniel-mar 33
        }
34
 
1116 daniel-mar 35
        /**
36
         * @param string $id
37
         * @param array $out
38
         * @param bool $handled
39
         * @return void
40
         * @throws OIDplusException
41
         */
42
        public function gui(string $id, array &$out, bool &$handled) {
1445 daniel-mar 43
                $parts = explode('$', $id);
44
                if ($parts[0] == 'oidplus:system_log') {
635 daniel-mar 45
                        $handled = true;
46
                        $out['title'] = _L('All log messages');
801 daniel-mar 47
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
635 daniel-mar 48
 
49
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
1266 daniel-mar 50
                                throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), $out['title'], 401);
635 daniel-mar 51
                        }
52
 
1445 daniel-mar 53
                        $page = $parts[1] ?? null;
54
                        if ($page == null) {
55
                                $res = OIDplus::db()->query("select max(id) as cnt from ###log");
1448 daniel-mar 56
                                $page = floor($res->fetch_array()['cnt'] / 500) + 1;
1445 daniel-mar 57
                        }
1448 daniel-mar 58
                        $min = ($page-1) * 500 + 1;
59
                        $max = ($page  ) * 500;
1445 daniel-mar 60
 
61
                        $res = OIDplus::db()->query("select id, unix_ts, addr, event from ###log ".
62
                                                    "where id >= ? and id <= ? ".
63
                                                    "order by unix_ts desc", [$min, $max]);
64
 
65
                        $out['text'] = '<h2>'._L('Page %1 (Log ID %2 till %3)', $page, $min, $max).'</h2>';
66
 
67
                        $out['text'] .= '<p>';
68
                        if (!is_null($parts[1] ?? null)) $out['text'] .= '<a '.OIDplus::gui()->link($parts[0].'$'.($page+1)).'>Newer log entries</a> -- ';
69
                        $out['text'] .= '<a '.OIDplus::gui()->link($parts[0].'$'.($page-1)).'>Older log entries</a>';
70
                        $out['text'] .= '<p>';
71
 
1448 daniel-mar 72
                        $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
73
                        $out['text'] .= '<table class="table table-bordered table-striped">';
74
                        $out['text'] .= '<thead>';
75
                        $out['text'] .= '<tr><th>'._L('Time').'</th><th>'._L('Event').'</th><th>'._L('Affected users').'</th><th>'._L('Affected objects').'</th><th>'._L('IP Address').'</th></tr>';
76
                        $out['text'] .= '</thead>';
77
                        $out['text'] .= '<tbody>';
78
 
790 daniel-mar 79
                        if ($res->any()) {
635 daniel-mar 80
                                while ($row = $res->fetch_array()) {
81
                                        $severity = 0;
1366 daniel-mar 82
                                        $contains_messages_for_me = false;
635 daniel-mar 83
                                        // ---
84
                                        $users = array();
85
                                        $res2 = OIDplus::db()->query("select username, severity from ###log_user ".
1171 daniel-mar 86
                                                                     "where log_id = ?", array((int)$row['id']));
635 daniel-mar 87
                                        while ($row2 = $res2->fetch_array()) {
88
                                                $users[] = $row2['username'];
1366 daniel-mar 89
                                                if ($row2['username'] == 'admin') {
90
                                                        $severity = $row2['severity'];
91
                                                        $contains_messages_for_me = true;
92
                                                }
635 daniel-mar 93
                                        }
1448 daniel-mar 94
                                        $users = implode("\n",$users);
635 daniel-mar 95
                                        // ---
96
                                        $objects = array();
97
                                        $res2 = OIDplus::db()->query("select object, severity from ###log_object ".
1171 daniel-mar 98
                                                                     "where log_id = ?", array((int)$row['id']));
635 daniel-mar 99
                                        while ($row2 = $res2->fetch_array()) {
100
                                                $objects[] = $row2['object'];
101
                                        }
1448 daniel-mar 102
                                        $objects = implode("\n",$objects);
635 daniel-mar 103
                                        // ---
104
                                        $addr = empty($row['addr']) ? _L('no address') : $row['addr'];
105
                                        // ---
1448 daniel-mar 106
 
107
                                        $a = '<span class="severity_'.$severity.'">';
108
                                        $b = '</span>';
109
                                        if ($contains_messages_for_me) $a = '<b>'.$a;
110
                                        if ($contains_messages_for_me) $b = $b.'</b>';
111
                                        $out['text'] .= '<tr>';
112
                                        $out['text'] .= '<td>'.$a.date('Y-m-d H:i:s', (int)$row['unix_ts']).$b.'</td>';
113
                                        $out['text'] .= '<td>'.$a.htmlentities($row['event']).$b.'</td>';
114
                                        $out['text'] .= '<td>'.$a.nl2br(htmlentities($users)).$b.'</td>';
115
                                        $out['text'] .= '<td>'.$a.nl2br(htmlentities($objects)).$b.'</td>';
116
                                        $out['text'] .= '<td>'.$a.htmlentities($addr).$b.'</td>';
117
                                        $out['text'] .= '<tr>';
118
 
635 daniel-mar 119
                                }
120
                        } else {
1448 daniel-mar 121
                                $out['text'] .= '<tr><td colspan="5">'._L('There are no log entries on this page').'</td></tr>';
635 daniel-mar 122
                        }
123
 
1448 daniel-mar 124
                        $out['text'] .= '</tbody>';
125
                        $out['text'] .= '</table>';
126
                        $out['text'] .= '</div></div>';
127
 
635 daniel-mar 128
                }
129
        }
130
 
1116 daniel-mar 131
        /**
132
         * @param array $json
133
         * @param string|null $ra_email
134
         * @param bool $nonjs
135
         * @param string $req_goto
136
         * @return bool
137
         * @throws OIDplusException
138
         */
139
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
635 daniel-mar 140
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
141
 
800 daniel-mar 142
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
801 daniel-mar 143
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
635 daniel-mar 144
                } else {
145
                        $tree_icon = null; // default icon (folder)
146
                }
147
 
148
                $json[] = array(
149
                        'id' => 'oidplus:system_log',
150
                        'icon' => $tree_icon,
151
                        'text' => _L('All log messages')
152
                );
153
 
154
                return true;
155
        }
156
 
1116 daniel-mar 157
        /**
158
         * @param string $request
159
         * @return array|false
160
         */
161
        public function tree_search(string $request) {
635 daniel-mar 162
                return false;
163
        }
1116 daniel-mar 164
}