-
Notifications
You must be signed in to change notification settings - Fork 0
/
MikrotikTable.hooks.php
153 lines (131 loc) · 4.61 KB
/
MikrotikTable.hooks.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
<?php
/**
* Hooks for MikrotikTable extension
*
* @file
* @ingroup Extensions
*/
require_once( 'routeros_api.class.php' );
class MikrotikTableHooks {
private $comment_style = "column";
public static function onParserFirstCallInit( Parser $parser ) {
$parser->setHook( 'mikrotik', array ('MikrotikTableHooks','mikrotikTableRender') );
return true;
}
public function buildRow(array $row_data, array $columns)
{
$row_style = "";
global $comment_style;
if(array_key_exists("disabled", $row_data)){
$row_style = $row_data["disabled"] == "true" ? 'class="mt-tr-disabled"' : 'class="mt-tr-default"';
}
$row_comment = "";
if($comment_style == "line") {
if(array_key_exists("comment", $row_data)){
$row_comment = "<tr $row_style><td colspan='".count($columns)."'>;;; ".$row_data["comment"]."</td>";
}
}
$row = $row_comment."<tr $row_style>";
foreach($columns as $column) {
$div_icon = "";
if($column == "action") {
switch ($row_data[$column]) {
case "accept":
$div_icon = '<div class="mt-div-default action-accept"></div>';
break;
case "drop":
$div_icon = '<div class="mt-div-default action-drop"></div>';
break;
case "fasttrack-connection":
$div_icon = '<div class="mt-div-default action-fasttrack"></div>';
break;
case "passthrough":
$div_icon = '<div class="mt-div-default action-passthrough"></div>';
break;
case "masquerade":
case "redirect":
$div_icon = '<div class="mt-div-default action-masquerade"></div>';
break;
case "src-nat":
case "dst-nat":
case "netmap":
$div_icon = '<div class="mt-div-default action-nat"></div>';
break;
}
}
$column_value = "";
if(array_key_exists($column, $row_data))
{
if($column == "bytes") { $column_value = (new self)->formatBytes($row_data[$column]); }
else { $column_value = $row_data[$column]; }
$row .= "<td>".$div_icon.$column_value."</td>";
}
else {
$row .= "<td>".$div_icon."</td>";
}
}
return $row."</tr>";
}
public function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
public static function mikrotikTableRender( $input, array $args, Parser $parser, PPFrame $frame ) {
static $hasRun = false;
if ($hasRun) return;
$hasRun = true;
$parser->disableCache();
$parser->getOutput()->addModuleStyles( array('ext.mikrotikTable') );
global $wgLanguageCode;
global $comment_style;
$language = $wgLanguageCode;
$allowed_columns = "";
$firewall_table = "nat";
# Checks minimal conf
if(!isset($args['ip'])) { return "IP not set"; }
if(!isset($args['login'])) { return "Login not set"; }
if(!isset($args['password'])) { return "Password not set"; }
if(isset($args['comment'])) { $comment_style = $args['comment'] == "line" ? "line" : $comment_style; }
if(isset($args['table'])) { $firewall_table = $args['table'] == "filter" ? "filter" : $firewall_table; }
if(isset($args['lng'])) { $language = $args['lng']; }
#Show only specified columns names
if(isset($args['columns'])) { $allowed_columns = explode(",", $args['columns']); }
$API = new RouterosAPI();
#$API->debug = true;
$API->attempts = 1;
$API->ssl = true;
$API->port = isset($args['port']) ? $args['port'] : 8729;
if ($API->connect($args['ip'], $args['login'], $args['password'])) {
$API->write("/ip/firewall/".$firewall_table."/print");
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
# Collect all (unique) columns
$columns = array();
# Rewrite this. Need check passed names
if(count($allowed_columns) > 0) { $columns = $allowed_columns; }
else {
foreach($ARRAY as $arr){
foreach($arr as $key => $value){
#skip comment column if user set $comment_style as 'line'
if($comment_style == "line" and $key == "comment") { continue; }
if(!in_array($key, $columns)) { $columns[] = $key; }
}
}
}
$tbl2 = "<table class='wikitable mt-table-default'>";
# Prepare table headers
foreach($columns as $column){ $tbl2.= "<th>".(wfMessage( $column )->inLanguage( $language )->isBlank() == 1 ?
$column : wfMessage( $column )->inLanguage( $language ) )."</th>"; }
foreach( $ARRAY as $arr) {
$tbl2 .= (new self)->buildRow($arr, $columns);
}
$API->disconnect();
return $tbl2."</table>". htmlspecialchars( $input );;
}
else { return wfMessage( "mikrotiktable-connection-error" ); }
}
}