-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.php
209 lines (186 loc) · 6.47 KB
/
function.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
function username() {
if (isset ($_SERVER['REMOTE_USER'])) $user = $_SERVER['REMOTE_USER'];
else if (isset ($_SERVER['USER'])) $user = $_SERVER['USER'];
else $user='unknown';
return $user;
}
function mysql_conn ($dbhost,$dbuser,$dbpass,$dbname,$dbport,$userlog) {
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport);
if ($mysqli->connect_error) {
syslog (LOG_EMERG, $userlog."\t".'Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
exit ($userlog."\t".'Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
syslog(LOG_INFO, $userlog."\t".'Successfully mysql connected to ' . $mysqli->host_info) ;
return $mysqli;
}
function report($mysqli,$sql,$lim,$desc) {
if ($result = $mysqli->query($sql)) {
/* fetch associative array */
if ( $row = $result->fetch_assoc() ) { /* First line */
print '<table id="rounded-corner" summary="DMARC Domains asking RUA report">';
printTableRowH($col = array_keys($row),$desc);
printTableRow($mysqli,$col, $row, $lim);
}
else print('<p>No domains found.</p>');
while ($row = $result->fetch_assoc()) {
printTableRow($mysqli,$col, $row,$lim);
}
print '</table>';
print '<p>Click on domain name to see more detailed statistics.</p>';
/* free result set */
$result->free();
}
else print("<p>Error in query:<br><pre>$sql</pre>.</p>");
}
function to_json($myconn, $sql) {
$rows = array();
if ($result = $myconn->query($sql)) {
$keys = NULL;
/* fetch associative array */
while ($r = $result->fetch_assoc()) {
if (is_null($keys)) {
$keys = array_keys($r);
if ( count($keys) != 2 ) {
syslog (LOG_EMERG, 'Database integrity compromised on domain '.$r['name']);
return 'ERR'; /* Database integrity compromised */
}
$table['cols'] = array(
array('label' => $keys[0], 'type' => 'string'),
array('label' => $keys[1], 'type' => 'number')
);
}
$temp = array();
// the following line will be used to slice the Pie chart
foreach ($keys as $key)
switch ( $key ) {
case 'policy':
$temp[] = array('v' => (string) human($myconn,$key,$r["$key"]));
break;
default:
$temp[] = array('v' => (int) $r["$key"]);
}
$rows[] = array('c' => $temp);
}
$result->free();
$table['rows'] = $rows;
return json_encode($table);
}
return NULL;
}
function to_table($myconn, $sql,$desc) {
if ($result = $myconn->query($sql)) {
/* fetch associative array */
if ( $row = $result->fetch_assoc() ) { /* First line */
print '<table id="rounded-corner" summary="DMARC Domains asking RUA report">';
printTableRowH($col = array_keys($row),$desc);
printTableRow($myconn,$col, $row, NULL);
}
else print '<p>No record found.</p>';
while ($row = $result->fetch_assoc()) {
printTableRow($myconn,$col, $row, NULL);
}
print '</table>';
/* free result set */
$result->free();
}
else print("<p>Error in query:<br><pre>$sql</pre>.</p>");
}
function printTableRowH($headers,$description) {
print '<thead><tr>';
$count = count($headers)-1;
foreach ($headers as $h)
if ($h != 'from_domain') print "<th scope=\"col\">$h</th>";
else $count --;
print '</tr></thead>'."\n";
print <<<END
<tfoot>
<tr>
<td colspan="$count"><em>$description</em></td>
<td> </td>
</tr>
</tfoot>
END;
}
function printTableRow($myconn,$k, $r,$lim) {
print '<tr>';
foreach ($k as $keyvalue)
switch ( $keyvalue ) {
case 'name':
$domencoded = base64_encode($r["$keyvalue"]);
print <<<ENDED
<td onClick="xmlhttpPost('result.php', 'Request-$domencoded-$r[policy]', 'Result', '<img src=\'/include/pleasewait.gif\'>', true); return false;" id="$keyvalue" class="selectable" nowrap><form method="POST" name="Request-$domencoded-$r[policy]" action="result.php" onSubmit="xmlhttpPost('result.php', 'Request-$domencoded-$r[policy]', 'Result', '<img src=\'/include/pleasewait.gif\'>', true); return false;">$r[$keyvalue]<input type="hidden" name="$domencoded" size="0" value="$r[from_domain]"><input type="hidden" name="policy" value="$r[policy]"><input type="hidden" name="limit" value="$lim"></form></td>
ENDED;
break;
case 'from_domain':
break;
default:
print '<td id="'.$keyvalue.'" nowrap>'.human($myconn,$keyvalue,$r["$keyvalue"]).'</td>';
}
print '</tr>'."\n";
}
function human($myconn,$key,$value) {
switch ($key) {
case 'policy':
switch ($value) {
case 14: return 'Unknown';
case 15: return 'Pass';
case 16: return 'Reject';
case 17: return 'Quarantine';
case 18: return 'None';
}
break;
case 'align_dkim':
case 'align_spf' :
switch ($value) {
case 4: return 'Yes';
case 5: return 'No';
}
break;
case 'spf' :
case 'dkim':
switch ($value) {
case NULL: return 'N/A';
case 0: return 'Pass';
case 2: return 'SoftFail';
case 3: return 'Neutral';
case 4: return 'TmpError';
case 7: return 'Fail';
case 12: return 'Discard';
case 6: return 'None';
case -1: return 'N/A';
}
break;
case 'sigdomain' :
case 'Env Domain':
case 'RFC5322 From Dom':
return id_to_domain($myconn,'domains',$value);
break;
case 'reporter':
return id_to_domain($myconn,'reporters',$value);
break;
default: return $value;
}
}
function id_to_domain($myconn,$table,$val) {
if ( is_null($val) ) {
return 'N/A'; /* No value */
}
$sql = "SELECT DISTINCT `name` FROM `$table` WHERE `id`=$val";
if ($result = $myconn->query($sql)) {
$row = $result->fetch_assoc();
$num = $result->num_rows;
/* free result set */
$result->free();
if ( $num != 1 ) {
syslog (LOG_EMERG, 'Database integrity compromised on $table '.$row['name']);
return 'ERR'; /* Database integrity compromised */
}
return $row['name'];
}
syslog (LOG_ERR,"Error in query: $sql");
return 'ERR';
}
?>