summaryrefslogtreecommitdiff
path: root/api/img_adt_svc_relais.php
blob: 9b5e44d696c16756ff4a7e499d56a607078f3201 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
 * Copyright 2016 Ludovic Pouzenc <ludovic@pouzenc.fr>
 * Copyright 2016 Nicolas Goaziou <mail@nicolasgoaziou.fr>
 * 
 * This file is part of CHD Gestion.
 * 
 * CHD Gestion is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * CHD Gestion is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with CHD Gestion.  If not, see <http://www.gnu.org/licenses/>.
**/

include_once('inc/config.php');
$mysqli = new mysqli($db_config['host'], $db_config['username'], $db_config['password'], $db_config['database']);
if (mysqli_connect_errno()) {
	die(mysqli_connect_error());
}
unset($db_config);
$mysqli->set_charset("utf8") or die($mysqli->error);

$opt_show_source=array_key_exists('s', $_GET);
$opt_embed=array_key_exists('e', $_GET);
$opt_ping=array_key_exists('p', $_GET);
$arg_adt=(array_key_exists('a', $_GET) && is_numeric($_GET['a']))?(0+$_GET['a']):NULL;

if (!$arg_adt) {
	die("Wrong args");
}

if ($opt_ping) {
?>
<script  type='text/javascript'>
	var nodes = new Array();

	var xhttp = new XMLHttpRequest();
	xhttp.onreadystatechange = function() {
		if (xhttp.readyState == 4 && xhttp.status == 200) {
			var json = JSON.parse(xhttp.responseText);
			console.log('json', json);
			for ( o of json ) {
				nodes[o.eid].style.fill = o.color;
			}
		}
	};
	window.onload = function () {
		/* for (var n of ...) incompatible Safari récents
        for ( var n of document.getElementsByClassName("node") ) {
           nodes[n.firstElementChild.innerHTML] = n;
        }*/
	    items = document.getElementsByClassName("node");
        for (var i = 0; i < items.length; i++) {
            n = items[i];
			nodes[n.firstElementChild.innerHTML] = n;
		}
		console.log('nodes', nodes);
		xhttp.open("GET", "https://priv.chd.sx/api/get_node_status.php?k=" + Object.keys(nodes), true);
		xhttp.send();
	}
</script>
<?php
}
// Piping to Graphviz' dot command or to browser if opt_show_source is set
if (!$opt_show_source) {
	$descriptorspec = array(
		0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
		1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
		2 => array("pipe", "w")
	);
	$pipes=array();
	$proc = proc_open('dot -Tsvg', $descriptorspec, $pipes, '/var/www/priv/api');
	if (!is_resource($proc)) die("Pb ouverture dot");

	$fd=$pipes[0];
} else {
	header('Content-Type: text/plain');
	$fd=fopen("php://output","w");
}

function graph_header() {
   return <<<EOD
digraph {
	rankdir=LR;
	concentrate=true;

EOD;
}

function node_adt($adherent_id, $shape, $adt, $adt_title) {
	$adt_title = htmlentities($adt_title);
	$adt_title_len = strlen($adt_title);
	foreach ( [ 10, 30 ] as $limit ) {
		if ( $adt_title_len > $limit ) {
			$breakpos = strpos($adt_title, ' ', $limit);
			if ( $breakpos !== FALSE ) {
				$adt_title = substr($adt_title, 0, $breakpos) . '<br/>' . substr($adt_title, $breakpos);
			}
		}
	}
	return <<<EOD
	"a$adherent_id" [ margin=0, shape=$shape, label=<
		<TABLE border="0" cellborder="0">
		<TR><TD><IMG SRC="icons/adt.png"/></TD></TR>
		<TR><TD>$adt<br/><font point-size="8">$adt_title</font></TD></TR>
		</TABLE>
	>, URL="/gestion/adherents/view/$adherent_id"];

EOD;
}
//	>];

function node_equipement($eid, $img, $label, $ip) {
	$label = htmlentities($label);
	$sub = (strlen($ip)>0)?$ip:"eid $eid";
		return <<<EOD
	"e$eid" [ margin=0, shape=none, label=<
		<TABLE border="0" cellborder="0">
		<TR><TD><IMG SRC="icons/$img"/></TD></TR>
		<TR><TD><font point-size="11">$label</font><br/><font point-size="8">$sub</font></TD></TR>
		</TABLE>
	>, URL="/gestion/equipements/view/$eid"];

EOD;
}

function edge_service($adherent_id, $routeur_eid, $service) {
		$service = htmlentities($service);
		return <<<EOD
	"a$adherent_id" -> "e$routeur_eid" [ label=<<font point-size="8">$service</font>> ]

EOD;
}

function node_relais($rid, $eid, $shape, $label, $rtitle) {
	$label = htmlentities($label);
	$rtitle = htmlentities($rtitle);
		return <<<EOD
	"r$rid" [ margin=0, shape=$shape, label=<
		<font point-size="8">$rtitle</font><br/>
		$label<br/>
		<font point-size="8">rid $rid</font>
	>, URL="/gestion/relais/view/$rid"];
	"e$eid" -> "r$rid" [ dir=back, arrowtail=empty, style=dashed ];

EOD;
}
//	>, URL="/gestion/relais/view/$rid"];

fwrite($fd, graph_header());
$sql = "SELECT v.adt, v.adt_title, v.service_id, v.service, v.relais_id, v.relais_degre, v.relais, v.secteur_id,
	v.routeur_equipement_id, v.antenne1_equipement_id, v.antenne2_equipement_id,
	e1.ipmgmt_id AS e1_ip, IF(LENGTH(e1.hostname)>0,e1.hostname,'Routeur') AS e1_title, CONCAT('emid', e1.equipement_modele_id, '.png') AS e1_img,
	e2.ipmgmt_id AS e2_ip, IF(LENGTH(e2.hostname)>0,e2.hostname,'Antenne1') AS e2_title, CONCAT('emid', e2.equipement_modele_id, '.png') AS e2_img,
	e3.ipmgmt_id AS e3_ip, IF(LENGTH(e3.hostname)>0,e3.hostname,'Antenne2') AS e3_title, CONCAT('emid', e3.equipement_modele_id, '.png') AS e3_img
FROM v_relais_detail v
LEFT JOIN equipements e1 ON (e1.id = v.routeur_equipement_id)
LEFT JOIN equipements e2 ON (e2.id = v.antenne1_equipement_id)
LEFT JOIN equipements e3 ON (e3.id = v.antenne2_equipement_id)
WHERE adherent_id=" . $arg_adt;

$res = $mysqli->query($sql);
$empty=true;
while ( $row = $res->fetch_assoc() ) {
	$empty=false;

	if ( isset($row['adt']) && isset($row['service_id']) && isset($row['routeur_equipement_id']) ) {
		fwrite($fd, node_adt($arg_adt, 'none', $row['adt'], $row['adt_title'])); //egg
		fwrite($fd, node_equipement($row['routeur_equipement_id'], $row['e1_img'], $row['e1_title'], $row['e1_ip']));
		fwrite($fd, edge_service($arg_adt,$row['routeur_equipement_id'], $row['service']));
	}

	if ( isset($row['antenne1_equipement_id']) ) {
		fwrite($fd, node_equipement($row['antenne1_equipement_id'], $row['e2_img'], $row['e2_title'], $row['e2_ip']));
		fwrite($fd, "\te".$row['routeur_equipement_id']." -> e".$row['antenne1_equipement_id'].";\n");
	}
	if ( isset($row['antenne2_equipement_id']) ) {
		fwrite($fd, node_equipement($row['antenne2_equipement_id'], $row['e3_img'], $row['e3_title'], $row['e3_ip']));
		fwrite($fd, "\te".$row['antenne1_equipement_id']." -> e".$row['antenne2_equipement_id'].";\n");
	}
	if ( isset($row['relais_id']) && isset($row['relais_degre']) ) {
		switch ($row['relais_degre']) {
		case 1:
			$e_node = $row['routeur_equipement_id']; break;
		case 2:
			$e_node = $row['antenne1_equipement_id']; break;
		case 3:
			$e_node = $row['antenne2_equipement_id']; break;
		default:
			continue;
		}
		fwrite($fd, node_relais($row['relais_id'], $e_node, 'none', 'Secteur '.$row['secteur_id'], $row['relais']));
	}
}
if ($empty) {
	fwrite($fd, "\tnothing;\n");
}

fwrite($fd, "}\n");

if (!$opt_show_source) {
	fclose($fd);
	if ($opt_embed) {
		// Skip <?xml> and <!doctype>
		for ($i=0 ; $i<3 && !feof($pipes[1]); $i++) {
			fgets($pipes[1]);
		}
	}
	$image_data = stream_get_contents($pipes[1]);
	if ($opt_embed) {
		$image_data = str_replace('image xlink:href="icons', 'image xlink:href="/api/icons', $image_data);
		$image_data = str_replace('xlink:title="&lt;TABLE&gt;"', 'target="_blank"', $image_data);
	}
	fclose($pipes[1]);
	$err_data = stream_get_contents($pipes[2]);
	fclose($pipes[2]);
	$return_value = proc_close($proc);

	if ( $return_value !== 0 || strlen($err_data) !== 0 ) {
		header('Content-Type: text/plain');
		echo $err_data;
	} else {
		header('Content-Type: image/svg+xml');	
		echo $image_data;
	}
	flush();
}