use foreach in firstlast view avoid loop printing
[racktables-contribs] / LinkManagement / plugins / linkmgmt.php
CommitLineData
9276dc97 1<?php
3badc888 2// TODO linkchain cytoscape create libs?
3// linkchain all objects graph cytoscape takes ages
4// avoid create own linkchain for every port on global map
5// highlight port cytoscape maps
9276dc97 6/*
c7a681fc 7 * Link Management for RT >= 0.20.9
9276dc97 8 *
fdc74cf0 9 * Features:
10 * - create links between ports
11 * - create backend links between ports
12 * - visually display links / chains
13 * e.g.
14 * (Object)>[port] -- front --> [port]<(Object) == back == > (Object)>[port] -- front --> [port]<(Object)
15 * - Link object backend ports by name (e.g. handy for patch panels)
16 * - change/create CableID (needs jquery.jeditable.mini.js)
17 * - change/create Port Reservation Comment (needs jquery.jeditable.mini.js)
18 * - multiple backend links for supported port types (e.g. AC-in, DC)
19 * - GraphViz Maps (Objects, Ports and Links) (needs GraphViz_Image 1.3.0)
20 * - object,port or link highligthing (just click on it)
21 * - context menu to link and unlink ports
9276dc97 22 *
fdc74cf0 23 * Usage:
24 * 1. select "Link Management" tab
25 * 2. you should see link chains of all linked ports
26 * 3. to display all ports hit "Show All Ports" in the left upper corner
27 * 4. to link all ports with the same name of two different objects use "Link Object Ports by Name"
28 * a. select the other object you want to backend link to
29 * b. "show back ports" gives you the list of possible backend links
30 * !! Important port names have to be the same on both objects !!
31 * e.g. (Current Object):Portname -?-> Portname:(Selected Object)
32 * c. select all backend link to create (Ctrl + a for all)
33 * d. Enter backend CableID for all selected links
34 * e. "Link back" create the backend links
35 * 5. If you have an backend link within the same Object the link isn't shown until
36 * "Expand Backend Links on same Object" is hit
37 * 6. "Object Map" displays Graphviz Map of current object
38 * 7. To get a Graphviz Map of a single port click the port name on the left
9276dc97
ME
39 *
40 *
3badc888 41 * Changes:
42 * switch to new linkchain class
43 * - nicer looking graphviz maps
44 * - better multilink support
45 *
46 * add cytoscape.js maps
47 * - path higlighting
48 * - zooming
49 *
fdc74cf0 50 * Requirements:
51 * PHP 5 (http://php.net/)
52 * GraphViz_Image 1.3.0 or newer (http://pear.php.net/package/Image_GraphViz)
53 * GraphViz (http://www.graphviz.org/)
9276dc97 54 *
3badc888 55 * to user cytoscape js map the following is required:
383230c5 56 * create plugins/linkmgmt directory and place the following 7 files in it
3badc888 57 *
58 * Cytoscape.js (http://js.cytoscape.org/)
3badc888 59 * https://cytoscape.github.io/cytoscape.js/api/cytoscape.js-latest/cytoscape.min.js
60 * dagre.js
3badc888 61 * https://raw.githubusercontent.com/cpettitt/dagre/master/dist/dagre.min.js
62 * https://raw.githubusercontent.com/cytoscape/cytoscape.js-dagre/master/cytoscape-dagre.js
63 * qtip
3badc888 64 * https://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.1/jquery.qtip.min.css
65 *
3badc888 66 * https://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.1/jquery.qtip.min.js
67 * https://raw.githubusercontent.com/cytoscape/cytoscape.js-qtip/master/cytoscape-qtip.js
68 *
69 * jquery >= 1.10.0 (qtip requirement)
3badc888 70 * https://code.jquery.com/jquery-1.11.3.min.js
71 *
9276dc97
ME
72 * INSTALL:
73 *
fdc74cf0 74 * 1. create LinkBackend Table in your RackTables database
75 *
76 * Multilink table
9276dc97
ME
77
78CREATE TABLE `LinkBackend` (
79 `porta` int(10) unsigned NOT NULL DEFAULT '0',
80 `portb` int(10) unsigned NOT NULL DEFAULT '0',
81 `cable` char(64) DEFAULT NULL,
82 PRIMARY KEY (`porta`,`portb`),
cb15aecc 83 KEY `LinkBackend_FK_a` (`porta`),
fdc74cf0 84 KEY `LinkBackend_FK_b` (`portb`),
85 CONSTRAINT `LinkBackend_FK_a` FOREIGN KEY (`porta`) REFERENCES `Port` (`id`) ON DELETE CASCADE,
86 CONSTRAINT `LinkBackend_FK_b` FOREIGN KEY (`portb`) REFERENCES `Port` (`id`) ON DELETE CASCADE
1fb0f48b 87) ENGINE=InnoDB DEFAULT CHARSET=utf8 collate=utf8_unicode_ci;
9276dc97 88
383230c5 89 * 2. copy jquery.jeditable.mini.js to plugins/linkmgmt directory (http://www.appelsiini.net/download/jquery.jeditable.mini.js)
fdc74cf0 90 * 3. copy linkmgmt.php to plugins directory
91 *
92 * Ready to go!
93 *
94 *
95 * UPDATE TABLE:
96 *
97 * Update from non-multilink table
98 * ALTER TABLE
99
100ALTER TABLE LinkBackend ADD KEY `LinkBackend_FK_b` (`portb`);
101ALTER TABLE LinkBackend DROP INDEX porta;
102ALTER TABLE LinkBackend DROP INDEX portb;
103
b695efd7 104 * UPDATE to RT 0.20.7
105
106ALTER TABLE LinkBackend CONVERT to CHARACTER SET utf8 COLLATE utf8_unicode_ci;
107
fdc74cf0 108 *
9276dc97
ME
109 *
110 * TESTED on FreeBSD 9.0, nginx/1.0.11, php 5.3.9
fdc74cf0 111 * GraphViz_Image 1.3.0
9276dc97 112 *
c7a681fc 113 * (c)2012-2015 Maik Ehinger <m.ehinger@ltur.de>
9276dc97
ME
114 */
115
fdc74cf0 116/**
117 * The newest version of this plugin can be found at:
9276dc97 118 *
3badc888 119 * https://github.com/github138/myRT-contribs/tree/develop-0.20.x
9276dc97
ME
120 *
121 */
122
123/*************************
124 * TODO
125 *
126 * - code cleanups
127 * - bug fixing
128 *
fdc74cf0 129 * - put selected object/port top left of graph
130 * - multlink count for Graphviz maps empty or full dot
131 *
132 * - csv list
9276dc97
ME
133 *
134 */
135
fdc74cf0 136/* DEBUG */
137//error_reporting(E_ALL);
9276dc97
ME
138
139$tab['object']['linkmgmt'] = 'Link Management';
140$tabhandler['object']['linkmgmt'] = 'linkmgmt_tabhandler';
141//$trigger['object']['linkmgmt'] = 'linkmgmt_tabtrigger';
142
143$ophandler['object']['linkmgmt']['update'] = 'linkmgmt_opupdate';
144$ophandler['object']['linkmgmt']['unlinkPort'] = 'linkmgmt_opunlinkPort';
145$ophandler['object']['linkmgmt']['PortLinkDialog'] = 'linkmgmt_opPortLinkDialog';
146$ophandler['object']['linkmgmt']['Help'] = 'linkmgmt_opHelp';
147
fdc74cf0 148$ophandler['object']['linkmgmt']['map'] = 'linkmgmt_opmap';
149$ajaxhandler['lm_mapinfo'] = 'linkmgmt_ajax_mapinfo';
150
3badc888 151$ophandler['object']['linkmgmt']['cytoscapemap'] = 'linkmgmt_cytoscapemap';
152
fdc74cf0 153/* ------------------------------------------------- */
154
1e6c47e6 155define('LM_MULTILINK',TRUE);
fdc74cf0 156
157/* -------------------------------------------------- */
158
159$lm_multilink_port_types = array(
160 16, /* AC-in */
161 //1322, /* AC-out */
162 1399, /* DC */
163 );
9276dc97
ME
164
165/* -------------------------------------------------- */
166
167$lm_cache = array(
3254ec33 168 'allowcomment' => TRUE, /* RackCode {$op_set_reserve_comment} */
169 'allowlink' => TRUE, /* RackCode {$op_set_link} */
170 'allowbacklink' => TRUE, /* RackCode {$op_set_backlink} */
9276dc97
ME
171 'rackinfo' => array(),
172 );
173
174/* -------------------------------------------------- */
175
176//function linkmgmt_tabtrigger() {
177// return 'std';
178//} /* linkmgmt_tabtrigger */
179
3badc888 180/* -------------------linkchain stuff------------------------------- */
181class linkchain_cache
182{
183 public $cache = array();
9276dc97 184
3badc888 185 function getobject($object_id , &$rack = null)
186 {
187 if(!$object_id)
188 return null;
9276dc97 189
3badc888 190 if(!isset($this->cache['o'.$object_id]))
191 {
192 $object = spotEntity('object', $object_id);
193 $object['IPV4OBJ'] = considerConfiguredConstraint ($object, 'IPV4OBJ_LISTSRC');
9276dc97 194
3badc888 195 /* get more object info */
9276dc97 196
3badc888 197 if($object['IPV4OBJ'])
198 {
199 // ip addresses
200 //amplifyCell($object); /* get ports, ipv4, ipv6, nat4 and files */
201 $object['ipv4'] = getObjectIPv4Allocations ($object_id);
202 $object['portip'] = array();
203 foreach($object['ipv4'] as $ipv4)
204 {
205 $object['portip'][$ipv4['osif']] = $ipv4['addrinfo']['ip'];
206 }
207 }
9276dc97 208
3badc888 209 if($object['container_id'])
210 {
211 $container = $this->getobject($object['container_id']);
212 }
fdc74cf0 213
3badc888 214 $this->cache['o'.$object_id] = $object;
215 }
216 else
217 $object = $this->cache['o'.$object_id];
fdc74cf0 218
3badc888 219 $rack = $this->getrack($object['rack_id']);
fdc74cf0 220
3badc888 221 return $object;
222 }
fdc74cf0 223
3badc888 224 function getrack($rack_id)
225 {
226 // rack
227 if($rack_id)
228 {
229 if(!isset($this->cache['r'.$rack_id]))
230 {
231 $rack = spotEntity('rack', $rack_id);
232 $this->cache['r'.$rack_id] = $rack;
233 }
234 else
235 $rack = $this->cache['r'.$rack_id];
236 }
237 else
238 $rack = null;
fdc74cf0 239
3badc888 240 return $rack;
fdc74cf0 241
3badc888 242 }
243}
fdc74cf0 244
3badc888 245$lc_cache = new linkchain_cache();
fdc74cf0 246
3badc888 247/* recursive class */
248class lm_linkchain implements Iterator {
fdc74cf0 249
3badc888 250 const B2B_LINK_BGCOLOR = '#d8d8d8';
251 const CURRENT_PORT_BGCOLOR = '#ffff99';
252 const CURRENT_OBJECT_BGCOLOR = '#ff0000';
253 const HL_PORT_BGCOLOR = '#A0FFA0';
254 const ALTERNATE_ROW_BGCOLOR = '#f0f0f0';
255
256 public $first = null;
257 public $last = null;
258 public $init = null;
259 public $linked = null;
260 public $linkcount = 0;
261
262 public $loop = false;
263 private $lastipobjport = null;
264
265 public $ports = array();
266
267 private $currentid = null;
268 private $back = null;
269 public $initback = null;
270
271 //public $cache = null;
272 private $object_id = null;
273
274 private $icount = 0;
275 public $exceed = false;
276
277 public $initalign = true;
278
279 public $portmulti = 0;
280 public $prevportmulti = 0;
281
282 private $initport = false;
283
284 private $lids = array(); // link ids porta portb linktype
285
286 public $pids = null; // port ids
287 public $oids = null; // object ids
288
289 /* $back = null follow front and back
290 * true follow back only
291 * false follow front only
292 * $prevport first port
293 */
294 function __construct($port_id, $back = null, $prevport = null, $reverse = false, &$pids = null, &$oids = null)
fdc74cf0 295 {
3badc888 296 global $lc_cache;
fdc74cf0 297
3badc888 298 $this->init = $port_id;
fdc74cf0 299
3badc888 300 $this->initback = $back;
fdc74cf0 301
3badc888 302 if($pids === null)
303 $this->pids = array();
304 else
305 $this->pids = &$pids;
306
307 if($oids === null)
308 $this->oids = array();
309 else
310 $this->oids = &$oids;
311
312 if($back !== null)
fdc74cf0 313 {
3badc888 314 $prevport_id = $prevport['id'];
315 $prevlinktype = $this->getlinktype(!$back);
316 //if($this->setlinkid($port_id, $prevport_id, $prevlinktype))
317 {
318 $this->last = $this->_getlinks($port_id, $back, null, $reverse);
319 }
320 //else
321 {
322 // $this->loop = true;
323 // echo "SUBCHAIN LID exists $port_id $prevlinktype $prevport_id<br>";
324 }
fdc74cf0 325
3badc888 326 // TODO set previous port ..use _set... function()
327 $this->ports[$port_id][$this->getlinktype(!$back)]['portcount'] = 1;
328 $this->ports[$port_id][$this->getlinktype(!$back)]['linked'] = 1;
329 $this->ports[$port_id][$this->getlinktype(!$back)]['remote_id'] = $prevport['id'];
330 $this->ports[$port_id][$this->getlinktype(!$back)]['remote_object_id'] = $prevport['object_id'];
331 $this->ports[$port_id][$this->getlinktype(!$back)]['remote_name'] = $prevport['name'];
332 $this->ports[$port_id][$this->getlinktype(!$back)]['remote_object_name'] = $prevport['object_name'];
333 $this->ports[$port_id][$this->getlinktype(!$back)]['cableid'] = $prevport['cableid'];
fdc74cf0 334
fdc74cf0 335
3badc888 336 $prevport = $this->_setportlink($prevport, $this->getlinktype(!$back));
337 $prevport[$this->getlinktype($back)]['portcount'] = null;
338 $prevport[$this->getlinktype($back)]['remote_id'] = null;
339 $prevport[$this->getlinktype($back)]['remote_object_id'] = null;
fdc74cf0 340
3badc888 341 $this->ports[$prevport_id] = $prevport;
342 $this->first = $prevport_id;
343 $this->linkcount++;
fdc74cf0 344
3badc888 345 // self::var_dump_html($this->ports[$port_id], "PORT");
346 // self::var_dump_html($prevport, "PREVPORT");
347 $this->object_id = $_GET['object_id'];
348 }
349 else
350 {
351 // Link
352 $this->last = $this->_getlinks($port_id, false);
fdc74cf0 353
3badc888 354 if(!$this->loop)
355 $this->first = $this->_getlinks($port_id, true, null, true);
fdc74cf0 356
3badc888 357 $this->object_id = $this->ports[$this->init]['object_id'];
358 }
fdc74cf0 359
3badc888 360 if(0)
361 if($this->loop)
362 {
363 $linktype = $this->getlinktype($back);
364 $prevlinktype = $this->getlinktype(!$back);
365
366 /* set first object */
367 $object_id = $this->ports[$port_id]['object_id'];
368 $object = $lc_cache->getobject($object_id);
fdc74cf0 369
3badc888 370 if($object['IPV4OBJ'])
371 $this->lastipobjport = $port_id;
372
373 $this->first = $this->lastipobjport;
fdc74cf0 374 }
fdc74cf0 375
3badc888 376 $this->linked = ($this->linkcount > 0);
377
378 if($reverse)
379 $this->reverse();
fdc74cf0 380
381 }
fdc74cf0 382
3badc888 383 function setlinkid($porta, $portb, $linktype)
384 {
385 if($porta > $portb)
386 $lid = "${portb}_${porta}";
387 else
388 $lid = "${porta}_${portb}";
fdc74cf0 389
3badc888 390 $lid .= $linktype;
fdc74cf0 391
3badc888 392 if(isset($this->lids[$lid]))
393 return false;
fdc74cf0 394
3badc888 395 $this->lids[$lid] = true;
fdc74cf0 396
3badc888 397 return true;
fdc74cf0 398 }
399
3badc888 400 function hasport_id($port_id)
fdc74cf0 401 {
3badc888 402 if(isset($this->pids[$port_id]))
403 return true;
404 else
405 return false;
fdc74cf0 406 }
407
3badc888 408 function hasobject_id($object_id)
409 {
410 if(isset($this->oids[$object_id]))
411 return true;
412 else
413 return false;
414 }
fdc74cf0 415
3badc888 416 function _setportprevlink($port, $linktype, $prevport)
417 {
418 $port[$linktype] = array(
419 'cableid' => $prevport[$linktype]['cableid'],
420 'linked' => $prevport[$linktype]['linked'],
421 'portcount' => null, //$prevport[$linktype]['linked'],
422 'remote_id' => $prevport['id'],
423 'remote_name' => $prevport['name'],
424 'remote_object_id' => $prevport['object_id'],
425 'remote_object_name' => $prevport['object_name'],
426 );
fdc74cf0 427
3badc888 428 return $port;
429 }
fdc74cf0 430
3badc888 431 function _setportlink($port, $linktype)
432 {
433 $port[$linktype] = array(
434 'cableid' => $port['cableid'],
435 'linked' => $port['linked'],
436 'portcount' => $port['portcount'],
437 'remote_id' => $port['remote_id'],
438 'remote_name' => $port['remote_name'],
439 'remote_object_id' => $port['remote_object_id'],
440 'remote_object_name' => $port['remote_object_name'],
441 );
fdc74cf0 442
3badc888 443 unset($port['cableid']);
444 unset($port['linked']);
445 unset($port['portcount']);
446 unset($port['remote_id']);
447 unset($port['remote_name']);
448 unset($port['remote_object_id']);
449 unset($port['remote_object_name']);
fdc74cf0 450
3badc888 451 return $port;
452 }
fdc74cf0 453
3badc888 454 function _getportlink($port)
455 {
456 $linktype = $this->getlinktype($this->back);
457 return array_merge($port, $port[$linktype], array('linktype' => $linktype));
458 }
fdc74cf0 459
3badc888 460 function isback($linktable)
461 {
462 if($linktable == 'back')
463 return true;
464 else
465 return false;
466 }
fdc74cf0 467
3badc888 468 function getlinktype($back)
469 {
470 return ($back ? 'back' : 'front' );
471 }
fdc74cf0 472
3badc888 473 function reverse()
474 {
475 $tmp = $this->first;
476 $this->first = $this->last;
477 $this->last = $tmp;
fdc74cf0 478
fdc74cf0 479 }
480
fdc74cf0 481
3badc888 482 //recursive
483 function _getlinks($port_id, $back = false, $prevport_id = null, $reverse = false)
484 {
485 global $lc_cache;
486 $linktype = $this->getlinktype($back);
fdc74cf0 487
3badc888 488 if($port_id == $this->init)
489 $this->initport = true;
fdc74cf0 490
3badc888 491 $ports = lm_getPortInfo($port_id, $back);
fdc74cf0 492
3badc888 493 $portcount = count($ports);
fdc74cf0 494
3badc888 495 $port = $ports[0];
fdc74cf0 496
3badc888 497 // check for loops on multilinked ports
498 // set main port to looping one
499 if($portcount > 1)
500 foreach($ports as $mport)
501 {
502 if(isset($this->ports[$mport['remote_id']]))
503 {
504 $port = $mport;
505 break;
506 }
507 }
fdc74cf0 508
3badc888 509 $remote_id = $port['remote_id'];
fdc74cf0 510
3badc888 511 $port['portcount'] = $portcount;
fdc74cf0 512
3badc888 513 $object_id = $port['object_id'];
fdc74cf0 514
3badc888 515 $rack = null;
516 $object = $lc_cache->getobject($object_id, $rack);
fdc74cf0 517
3badc888 518 $this->oids[$object_id] = true;
fdc74cf0 519
3badc888 520 if($object['IPV4OBJ'])
521 $this->lastipobjport = $port_id;
fdc74cf0 522
3badc888 523 if($object)
524 if(isset($object['portip'][$port['name']]))
525 $port['portip'] = $object['portip'][$port['name']];
fdc74cf0 526
3badc888 527 $port = $this->_setportlink($port, $linktype);
fdc74cf0 528
3badc888 529 if($prevport_id)
fdc74cf0 530 {
3badc888 531 $prevlinktype = $this->getlinktype(!$back);
532 $port = $this->_setportprevlink($port, $prevlinktype, $this->ports[$prevport_id]);
fdc74cf0 533 }
534
3badc888 535 //if(!$back)
536 {
537 if($prevport_id)
538 {
539 /* mutlilink: multiple previous links */
540 $prevports = lm_getPortInfo($port_id, !$back);
fdc74cf0 541
3badc888 542 $prevportcount = count($prevports);
fdc74cf0 543
3badc888 544 if($prevportcount > 1)
545 {
546 if($this->initport)
547 $this->initalign = false;
fdc74cf0 548
3badc888 549 $port[$this->getlinktype(!$back)]['portcount'] = $prevportcount;
fdc74cf0 550
3badc888 551 $lcs = array();
552 foreach($prevports as $mport)
553 {
554 if($prevport_id != $mport['remote_id'])
555 {
556 if(isset($this->pids[$mport['remote_id']]))
557 continue;
fdc74cf0 558
3badc888 559 $mport['portcount'] = 1;
560 $lc = new lm_linkchain($mport['remote_id'], $back, $mport, !$reverse, $this->pids, $this->oids);
561 $lcs[$mport['remote_id']] = $lc;
562 $this->linkcount += $lc->linkcount;
563 }
564 }
fdc74cf0 565
3badc888 566 $port[$this->getlinktype(!$back)]['chains'] = $lcs;
567 }
568 }
569 }
fdc74cf0 570
3badc888 571 if($portcount > 1)
fdc74cf0 572 {
3badc888 573 /* mutlilink: multiple links */
574
575 $lcs = array();
576 foreach($ports as $mport)
577 {
578 if($remote_id != $mport['remote_id'])
579 {
580 if(isset($this->pids[$mport['remote_id']]))
581 continue;
582
583 $mport['portcount'] = 1;
584 $lc = new lm_linkchain($mport['remote_id'], !$back, $mport, $reverse, $this->pids, $this->oids);
585 $lcs[$mport['remote_id']] = $lc;
586 $this->linkcount += $lc->linkcount;
587 }
588 }
589
590 $port[$linktype]['chains'] = $lcs;
fdc74cf0 591
fdc74cf0 592 }
593
3badc888 594 if(isset($this->ports[$port_id]))
595 {
596 if(!isset($this->ports[$port_id][$linktype]))
597 {
598 $this->ports[$port_id][$linktype] = $port[$linktype];
599 }
600 }
601 else
602 $this->ports[$port_id] = $port;
fdc74cf0 603
3badc888 604 $this->pids[$port_id] = true;
fdc74cf0 605
3badc888 606 if($remote_id)
607 {
608 if($this->setlinkid($port_id, $remote_id, $linktype))
609 {
610 $this->linkcount++;
611 return $this->_getlinks($remote_id, !$back, $port_id, $reverse);
612 }
613 else
614 {
615 $prevlinktype = $this->getlinktype(!$back);
616 $this->loop = true;
617 if(isset($port[$prevlinktype]))
618 $this->ports[$port_id][$prevlinktype] = $port[$prevlinktype];
619
620 $this->first = $remote_id;
621 }
622 }
623
624 return $port_id;
625 }
626
627 // TODO
628 function getchaintext()
629 {
630 //$this::var_dump_html($this->ports);
631 $chain = "";
632 foreach($this as $id => $port)
633 {
634 $linktype = $port['linktype']; //$this->getlinktype();
635 if($linktype == 'front')
636 $arrow = ' --> ';
637 else
638 $arrow = ' => ';
639
640 $text = $port['object_name']." [".$port['name']."]";
641
642 if($id == $this->init)
643 $chain .= "*$text*";
644 else
645 $chain .= "$text";
646
647 $remote_id = $port['remote_id'];
648
649 if($remote_id)
650 $chain .= $arrow;
651
652 if($this->loop && $remote_id == $this->first)
653 {
654 $chain .= "LOOP!";
655 break;
656 }
657 }
658 return $chain;
659 }
660
661 function getchainlabeltrstart($rowbgcolor = '#ffffff')
662 {
663
664 $port_id = $this->init;
665
666 $initport = $this->ports[$port_id];
fdc74cf0 667
668 $urlparams = array(
3badc888 669 'module' => 'redirect',
670 'page' => 'object',
671 'tab' => 'linkmgmt',
672 'op' => 'map',
673 'usemap' => 1,
674 'object_id' => $initport['object_id'],
fdc74cf0 675 );
676
3badc888 677 $hl_port_id = NULL;
678 if($hl_port_id !== NULL)
679 $urlparams['hl_port_id'] = $hl_port_id;
680 else
681 $urlparams['port_id'] = $port_id;
fdc74cf0 682
3badc888 683 $title = "linkcount: ".$this->linkcount."\nTypeID: ${initport['oif_id']}\nPortID: $port_id";
fdc74cf0 684
3badc888 685 $onclick = 'onclick=window.open("'.makeHrefProcess(array_merge($_GET, $urlparams)).'","Map","height=500,width=800,scrollbars=yes");';
fdc74cf0 686
3badc888 687 if($hl_port_id == $port_id)
688 {
689 $hlbgcolor = "bgcolor=".self::HL_PORT_BGCOLOR;
690 }
691 else
692 $hlbgcolor = "bgcolor=$rowbgcolor";
fdc74cf0 693
3badc888 694 if($rowbgcolor == '#ffffff')
695 $troutlinecolor = 'grey';
696 else
697 $troutlinecolor = 'white';
fdc74cf0 698
3badc888 699 /* Current Port */
700 $chainlabel = '<tr '.$hlbgcolor.' style="outline: thin solid '.$troutlinecolor.';"><td nowrap="nowrap" bgcolor='.($this->loop ? '#ff9966' : self::CURRENT_PORT_BGCOLOR).' title="'.$title.
701 '"><a '.$onclick.'>'.
702 $initport['name'].': </a></td>';
fdc74cf0 703
3badc888 704 return $chainlabel;
705 }
706
707 function getchainrow($allback = false, $rowbgcolor = '#ffffff', $isprev = false)
708 {
709 $port_id = $this->init;
710
711 $initport = $this->ports[$port_id];
712 $portmulti = 0;
713 $prevportmulti = 0;
714
715 $chain = "";
716
717 $remote_id = null;
718 $portalign = false;
719 $portmultis = array();
720 $i=0;
721 foreach($this as $id => $port)
fdc74cf0 722 {
3badc888 723 if($this->loop && (($isprev && $id == $this->last) || ($this->initback === null && !$isprev && $id == $this->first)))
724 $chain .= '<td bgcolor=#ff9966>LOOP</td>';
725
726 $object_text = $this->getprintobject($port);
727 $port_text = $this->getprintport($port);
728
729 if($this->initback !== null && (($isprev && $id == $this->last) || (!$isprev && $id == $this->first)))
fdc74cf0 730 {
3badc888 731 $object_text = "";
732 $port_text = "";
fdc74cf0 733 }
fdc74cf0 734
3badc888 735 $linktype = $port['linktype'];
736 $prevlinktype = ($linktype == 'front' ? 'back' : 'front');
fdc74cf0 737
3badc888 738 if($port[$prevlinktype]['portcount'] > 1)
739 {
740 $prevportmulti++;
741 /* mutlilink: multiple previous links */
fdc74cf0 742
3badc888 743 $notrowbgcolor = ($rowbgcolor == lm_linkchain::ALTERNATE_ROW_BGCOLOR ? '#ffffff' : lm_linkchain::ALTERNATE_ROW_BGCOLOR );
744 if($this->prevportmulti % 2)
745 {
746 $oddbgcolor = $rowbgcolor;
747 $evenbgcolor = $notrowbgcolor;
748 }
749 else
750 {
751 $oddbgcolor = $notrowbgcolor;
752 $evenbgcolor = $rowbgcolor;
753 }
fdc74cf0 754
3badc888 755 $chain = "<td><table id=pmp frame=box><tr id=main><td><table id=pmmain align=right><tr>$chain</tr></table></td></tr><!-- main tr--><tr><td><table id=plcs align=right width=100%>"; // end table pmmain; end main tr
fdc74cf0 756
3badc888 757 $mi = 0;
758 foreach($port[$prevlinktype]['chains'] as $mlc)
759 {
760 $mbgcolor = ($mi % 2 ? $evenbgcolor : $oddbgcolor);
761 $chain .= "<tr bgcolor=$mbgcolor align=right><td><table id=plc><tr>".$mlc->getchainrow($allback,$mbgcolor, true)."</tr></table><!-- end plc --></td></tr>"; // close table plc
762 $mi++;
763 }
764 $chain .= "</table><!--plcs--></td></tr></table><!--pmp--></td><td bgcolor=#ff0000></td>"; // clode tables plcs; pmp
fdc74cf0 765
3badc888 766 }
83b49cf7 767
3badc888 768 if($port_text && !$this->loop && $id == $this->first)
769 {
770 $chain .= $this->_printlinkportsymbol($id, $prevlinktype);
83b49cf7 771
3badc888 772 if($prevlinktype == 'front')
773 $chain .= $this->printcomment($port);
774 }
83b49cf7 775
3badc888 776 // current port align
777 if($this->initback === null && $port_text && !$portmulti && !$prevportmulti && $this->initalign && $id == $port_id)
778 {
779 $portalign = true;
780 $port_text = "</tr></table><!--t1 current--></td><!--end 1st td--><td id=2nd width=100%><table id=t2><tr>$port_text"; // close table t1 current; clode 1st td
781 }
83b49cf7 782
3badc888 783 $object_id = $port['object_id'];
83b49cf7 784
3badc888 785 $prevobject_id = $port[$prevlinktype]['remote_object_id'];
786 $remote_object_id = $port['remote_object_id'];
83b49cf7 787
3badc888 788 if($linktype == 'front')
789 {
790 // $arrow = ' ---> ';
791 if($object_text)
792 {
793 if($prevobject_id != $object_id || $allback)
794 $chain .= $object_text."<td>></td>";
83b49cf7 795
3badc888 796 $chain .= $port_text;
797 }
83b49cf7 798
3badc888 799 }
800 else
801 {
802 // $arrow = ' ===> ';
803 if($object_text)
804 $chain .= $port_text."<td><</td>".$object_text;
805 }
83b49cf7 806
3badc888 807 if($port['portcount'] > 1)
808 {
809
810 /* mutlilink: multiple links */
83b49cf7 811
3badc888 812 $notrowbgcolor = ($rowbgcolor == lm_linkchain::ALTERNATE_ROW_BGCOLOR ? '#ffffff' : lm_linkchain::ALTERNATE_ROW_BGCOLOR );
813 if($this->portmulti % 2)
814 {
815 $oddbgcolor = $rowbgcolor;
816 $evenbgcolor = $notrowbgcolor;
817 }
818 else
819 {
820 $oddbgcolor = $notrowbgcolor;
821 $evenbgcolor = $rowbgcolor;
822 }
823
824 $chain .= "<td bgcolor=#ff0000></td><td><table id=mp frame=box>";
83b49cf7 825
3badc888 826 $multichain = "<tr><td><table id=mlcs width=100%>";
83b49cf7 827
3badc888 828 $mi = 0;
829 foreach($port['chains'] as $mlc)
830 {
83b49cf7 831
3badc888 832 $mbgcolor = ($mi % 2 ? $evenbgcolor : $oddbgcolor);
833 $multichain .= "<tr bgcolor=$mbgcolor><td><table id=t9><tr>".$mlc->getchainrow(false, $mbgcolor)."</tr></table></td></tr>";
834 $mi++;
835 }
83b49cf7 836
3badc888 837 $multichain .= "</table><!-- mlcs --></td></tr>"; // close table mlcs
83b49cf7 838
3badc888 839 $portmultis[] = $multichain;
840
841 // main
842 $chain .= "<tr id=mm><td><table id=pmm><tr>";
83b49cf7 843
3badc888 844 $portmulti++;
83b49cf7 845
3badc888 846 }
847
848 $remote_id = $port['remote_id'];
849
850 if($remote_id)
851 if($object_id != $remote_object_id || $allback || $linktype == 'front')
852 $chain .= $this->printlink($port, $linktype);
853 else
854 $chain .= "<td>></td>";
855
856 if($port_text && !($remote_id && $this->loop) && $id == $this->last)
857 {
858 $chain .= $this->_printlinkportsymbol($id, $linktype);
859
860 if($linktype == 'front')
861 $chain .= $this->printcomment($port);
862 }
863
864 $i++;
865 } // foreach port
866
867 if($this->loop && $remote_id)
868 {
869 $chain .= '<td bgcolor=#ff9966>LOOP</td>';
870 showWarning("Possible Loop on Port ($linktype) ".$initport['name']);
83b49cf7 871 }
83b49cf7 872
3badc888 873 if($this->exceed)
874 {
875 $chain .= '<td bgcolor=#ff9966>LINKCOUNT EXCEEDED</td>';
876 $linktype = $this->getlinktype($this->back);
877 showWarning("Possible Loop linkcount(".$this->linkcount.") exceeded on Port ($linktype) ".$initport['name']);
878 }
83b49cf7 879
3badc888 880 foreach(array_reverse($portmultis) as $multitr)
881 {
882 $chain .= "</tr><!-- tr mm --></table><!-- pmm --></td></tr>$multitr</table><!-- mp--></td>"; // close table pmm; mp
883 }
83b49cf7 884
3badc888 885 if($this->initback === null)
886 {
887 // TODO width..
888 $chain = "<td id=1st".(!$portalign ? " colspan=2" : " width=1%")."><table id=t1".(!$portalign ? "" : " align=right")."><tr>$chain";
889 $chain .= "</tr></table><!-- end t1/t2 --></td><!--1st/2nd td-->"; // close table t1/t2; close 1st/2nd td
890 }
891
892 $this->portmulti += $portmulti;
893 $this->prevportmulti += $prevportmulti;
894
895 return $chain;
896 }
897
898 /*
899 */
900 function getprintobject($port) {
901 global $lc_cache;
902
903 $object_id = $port['object_id'];
904
905 if($object_id == $this->object_id) {
906 $color='color: '.self::CURRENT_OBJECT_BGCOLOR;
907 } else {
908 $color='';
909 }
910
911 $style = "font-size: 80%;";
912
913 $rack = null;
914 $object = $lc_cache->getobject($object_id, $rack);
915
916 if(!$rack)
917 $rackinfo = '<span style="'.$style.'">Unmounted</span>';
918 else
919 {
920 $rackinfo = '<a style="'.$style.'" href='.makeHref(array('page'=>'row', 'row_id'=>$rack['row_id'])).'>'.$rack['row_name']
921 .'</a>/<a style="'.$style.'" href='.makeHref(array('page'=>'rack', 'rack_id'=>$rack['id'])).'>'
922 .$rack['name'].'</a>';
923 }
924
925 return '<td><table frame=box align=center cellpadding=5 cellspacing=0><tr><td align=center><a style="font-weight:bold;'
926 .$color.'" href="'.makeHref(array('page'=>'object', 'tab' => 'linkmgmt', 'object_id' => $object_id))
927 .'"><pre>'.$object['name'].($object['container_name'] ? " (".$object['container_name'].")" : "").'</pre></a><pre>'.$rackinfo
928 .'</pre></td></tr></table></td>';
929
930 } /* printobject */
931
932 function getprintport($port, $multilink = false) {
933 global $lm_cache, $lm_multilink_port_types;
934
935 /* multilink port */
936 $multilink = in_array($port['oif_id'], $lm_multilink_port_types);
937
938 /* set bgcolor for current port */
939 if($this->initback === null && $port['id'] == $this->init) {
940 $bgcolor = 'bgcolor='.self::CURRENT_PORT_BGCOLOR;
941 $idtag = ' id='.$port['id'];
942 } else {
943 $bgcolor = 'bgcolor=#e0e0f8';
944 $idtag = '';
945 }
946
947 $mac = trim(preg_replace('/(..)/','$1:',$port['l2address']),':');
948
949 $title = "Label: ${port['label']}\nMAC: $mac\nTypeID: ${port['oif_id']}\nPortID: ${port['id']}";
950
951 if(isset($port['portip']))
952 $ip = "<br><p style=\"font-size: 80%\">".$port['portip']."</p>";
953 else
954 $ip = "";
955
956 return '<td><table><tr><td'.$idtag.' align=center '.$bgcolor.' title="'.$title.'"><pre>[<a href="'
957 .makeHref(array('page'=>'object', 'tab' => 'linkmgmt', 'object_id' => $port['object_id'], 'hl_port_id' => $port['id']))
958 .'#'.$port['id']
959 .'">'.$port['name'].'</a>]'.$ip.'</pre>'.($multilink && $lm_cache['allowbacklink'] ? $this->_getlinkportsymbol($port['id'], 'back') : '' ).'</td></tr></table></td>';
960
961 } /* printport */
962
963 /*
964 */
965 function printlink($port, $linktype) {
966
967 $link_id = $port['id']."_".$port['remote_id'];
968
969 if($linktype == 'back')
970 {
971 $arrow = '====>';
972 $link_id .= '_back';
973 }
974 else
975 $arrow = '---->';
976
977 $port_id = $port['id'];
978
979 /* link */
980 return '<td align=center>'
981 .'<pre><a class="editcable" id='.$link_id.'>'.$port['cableid']
982 ."</a></pre><pre>$arrow</pre>"
983 .$this->_printUnLinkPort($port, $linktype)
984 .'</td>';
985 } /* printlink */
986
987 /*
988 * return link symbol
989 */
990 function _getlinkportsymbol($port_id, $linktype) {
991 $retval = '<span onclick=window.open("'.makeHrefProcess(array_merge($_GET,
992 array('op' => 'PortLinkDialog','port' => $port_id,'linktype' => $linktype ))).'","name","height=800,width=800");'
993 .'>';
994
995 $img = getImageHREF ('plug', $linktype.' Link this port');
996
997 if($linktype == 'back')
998 $img = str_replace('<img',
999 '<img style="transform:rotate(180deg);-o-transform:rotate(180deg);-ms-transform:rotate(180deg);-moz-transform:rotate(180deg);-webkit-transform:rotate(180deg);"',
1000 $img);
1001
1002 $retval .= $img;
1003 $retval .= "</span>";
1004 return $retval;
1005
1006 } /* _getlinkportsymbol */
1007
1008 /*
1009 * print link symbol
1010 *
1011 */
1012 function _printlinkportsymbol($port_id, $linktype = 'front') {
1013 global $lm_cache;
1014
1015 if($linktype == 'front' && !$lm_cache['allowlink'])
1016 return;
1017
1018 if($linktype != 'front' && !$lm_cache['allowbacklink'])
1019 return;
1020
1021 return "<td align=center>"
1022 .$this->_getlinkportsymbol($port_id, $linktype)
1023 ."</td>";
1024
1025 } /* _printlinkportsymbol */
1026
1027 /*
1028 */
1029 function printcomment($port) {
1030
1031 if(!empty($port['reservation_comment'])) {
1032 $prefix = '<b>Reserved: </b>';
1033 } else
1034 $prefix = '';
1035
1036 return '<td>'.$prefix.'<i><a class="editcmt" id='.$port['id'].'>'.$port['reservation_comment'].'</a></i></td>';
1037
1038 } /* printComment */
1039
1040 /*
1041 * return link cut symbol
1042 *
1043 * TODO $opspec_list
1044 */
1045 function _printUnLinkPort($src_port, $linktype) {
1046 global $lm_cache;
1047
1048 if($linktype == 'front' && !$lm_cache['allowlink'])
1049 return;
1050
1051 if($linktype != 'front' && !$lm_cache['allowbacklink'])
1052 return;
1053
1054 $dst_port = $this->ports[$src_port['remote_id']];
1055
1056 return '<a href='.
1057 makeHrefProcess(array(
1058 'op'=>'unlinkPort',
1059 'port_id'=>$src_port['id'],
1060 'remote_id' => $dst_port['id'],
1061 'object_id'=> $this->object_id, //$this->ports[$this->init]['object_id'],
1062 'tab' => 'linkmgmt',
1063 'linktype' => $linktype)).
1064 ' onclick="return confirm(\'unlink ports '.$src_port['name']. ' -> '.$dst_port['name']
1065 .' ('.$linktype.') with cable ID: '.$src_port['cableid'].'?\');">'.
1066 getImageHREF ('cut', $linktype.' Unlink this port').'</a>';
1067
1068 } /* _printUnLinkPort */
1069
3d9d3e22 1070 function printports()
1071 {
1072
1073 $back = isset($this->ports[$this->first]['back']['remote_id']);
d894f236 1074 //$this->getporttds($this->first, $back);
1075 foreach($this as $port_id => $port)
1076 {
1077 $class = 'hidden';
1078 if($port_id == $this->first || $port_id == $this->last)
1079 $class = '';
1080
1081 $this->getporttds($port, $class);
1082 }
3d9d3e22 1083 }
1084
d894f236 1085 function getporttds($port, $class = '')
569219fa 1086 {
d894f236 1087 $back = ($port['linktype'] == 'front' ? false : true );
569219fa
CR
1088
1089 // see interface.php renderObjectPortRow()
1090 // highlight port name with yellow if it's name is not canonical
1091 $canon_pn = shortenPortName ($port['name'], $port['object_id']);
1092 $name_class = $canon_pn == $port['name'] ? '' : 'trwarning';
1093
1094 echo "<td class='tdleft $class'>" .
1095 formatPortLink ($port['object_id'], $port['object_name'], $port['id'], NULL) .
1096 "</td>";
1097
1098 $a_class = isEthernetPort ($port) ? 'port-menu' : '';
1099 echo "<td class='tdleft $class' NOWRAP><a name='port-${port['id']}' class='interactive-portname nolink $a_class'>${port['name']}</a></td>";
1100 echo "<td class='tdleft $class'>${port['label']}</td>";
1101 echo "<td class='tdleft $class'>" . formatPortIIFOIF ($port) . "</td><td class='tdleft $class'><tt>${port['l2address']}</tt></td>";
1102
1103 $remote_port = $port[( $back ? 'back' : 'front')];
1104 if ($remote_port['remote_id'])
1105 {
3d9d3e22 1106 if($port['id'] == $this->first)
1107 {
1108 echo "<td class='tdcenter nothidden'>";
1109
1110 if( $this->linkcount == 1 )
1111 if($back)
1112 echo "=${remote_port['cableid']}=>";
1113 else
1114 echo "-${remote_port['cableid']}->";
1115 else
1116 if( $this->linkcount > 1 )
1117 echo "*".$this->linkcount."*>";
1118 echo "</td>";
1119 }
1120
569219fa 1121 $editable = permitted ('object', 'ports', 'editPort') ? 'editable' : '';
d894f236 1122 echo "<td class='tdcenter hidden'>";
569219fa
CR
1123 if($back)
1124 echo "=${remote_port['cableid']}=>";
1125 else
1126 echo "-${remote_port['cableid']}->";
1127
d894f236 1128 echo "</td>";
569219fa
CR
1129 }
1130 else
d894f236 1131 {
1132 if(!empty($port['reservation_comment']))
1133 echo implode ('', formatPortReservation ($port));
1134 }
569219fa 1135
569219fa
CR
1136 } // getporttds
1137
3badc888 1138 // TODO
1139 // html table
1140 function getchainhtml()
1141 {
1142 $remote_id = $this->first;
1143
1144 // if not Link use LinkBackend
1145 $back = $this->ports[$remote_id]['front']['remote_id'];
1146
1147 $chain = "<table>";
1148
1149 for(;$remote_id;)
1150 {
1151 $back = !$back;
1152 $linktype = $port['linktype'];
1153
1154 if($back)
1155 {
1156 // $linktable = 'LinkBackend';
1157 $arrow = ' => ';
1158 }
1159 else
1160 {
1161 // $linktable = 'Link';
1162 $arrow = ' --> ';
1163 }
1164
1165 $port = $this->ports[$remote_id][$linktype];
1166
1167 if($this->init == $remote_id)
1168 $chain .= "<tr><td><b>".$port['object_name']."</b></td><td><b> [".$port['name']."]</b></td>";
1169 else
1170 $chain .= "<tr><td>".$port['object_name']."</td><td> [".$port['name']."]</td>";
1171
1172 if($remote_id == $this->first || $remote_id == $this->last)
1173 $chain .= "<td><div name=\"port${remote_id}-status\"></div></td>";
1174 else
1175 $chain .= "<td></td>";
1176
1177 $remote_id = $port['remote_id'];
1178
1179 if($remote_id)
1180 $chain .= "<td>$arrow</td></tr>";
1181 else
1182 $chain .= "<td></td></tr>";
1183
1184 if($this->loop && $remote_id == $this->first)
1185 {
1186 $chain .= "LOOP!<br>";
1187 break;
1188 }
1189
1190 }
1191
1192 $chain .= "</table>";
1193
1194 return $chain;
1195 }
1196
1197 function getport($id)
1198 {
1199 return $this->ports[$id];
1200 }
1201
1202 /* Iterator */
1203 function rewind() {
1204 $this->currentid = $this->first;
1205
1206 $this->back = isset($this->ports[$this->currentid]['back']['remote_id']);
1207
1208 $this->icount = 0;
1209 }
1210
1211 function current() {
1212 return $this->_getportlink($this->ports[$this->currentid]);
1213 }
1214
1215 function key() {
1216 return $this->currentid;
1217 }
1218
1219 function next() {
1220 $port = $this->current();
1221 $remote_id = $port['remote_id'];
1222
1223 if($this->loop && $remote_id == $this->first)
1224 $this->currentid = false;
1225 else
1226 $this->currentid = $remote_id;
1227
1228 $this->back = !$this->back;
1229
1230 $this->icount++;
1231 }
1232
1233 function valid() {
1234
1235 /* linkcout exceeded */
1236 if($this->icount > $this->linkcount+1)
1237 {
1238 $this->exceed = true;
1239 return false;
1240 }
1241
1242 return $this->currentid;
1243 }
1244
1245 /* for debugging only */
1246 function var_dump_html(&$var, $msg = "") {
1247 echo "<pre>------------------Start Var Dump -------------$msg------------\n";
1248 var_dump($var);
1249 echo "\n---------------------END Var Dump -----------$msg-------------</pre>";
1250 }
1251} // lm_linkchain
1252
1253/*
1254 * from RT database.php fetchPortList()
1255 * with Link table selection
1256 * and multilink changes
1257 */
1258function lm_fetchPortList ($sql_where_clause, $query_params = array(), $linktable = 'Link')
1259{
1260 $query = <<<END
1261SELECT
1262 Port.id,
1263 Port.name,
1264 Port.object_id,
1265 Object.name AS object_name,
1266 Port.l2address,
1267 Port.label,
1268 Port.reservation_comment,
1269 Port.iif_id,
1270 Port.type AS oif_id,
1271 (SELECT PortInnerInterface.iif_name FROM PortInnerInterface WHERE PortInnerInterface.id = Port.iif_id) AS iif_name,
1272 (SELECT PortOuterInterface.oif_name FROM PortOuterInterface WHERE PortOuterInterface.id = Port.type) AS oif_name,
1273
1274 lk.cable AS cableid,
1275 IF(lk.porta = Port.id, pb.id, pa.id) AS remote_id,
1276 IF(lk.porta = Port.id, pb.name, pa.name) AS remote_name,
1277 IF(lk.porta = Port.id, pb.object_id, pa.object_id) AS remote_object_id,
1278 IF(lk.porta = Port.id, ob.name, oa.name) AS remote_object_name,
1279
1280 (SELECT COUNT(*) FROM PortLog WHERE PortLog.port_id = Port.id) AS log_count,
1281 PortLog.user,
1282 UNIX_TIMESTAMP(PortLog.date) as time
1283FROM
1284 Port
1285 INNER JOIN Object ON Port.object_id = Object.id
1286
1287 LEFT JOIN $linktable AS lk ON lk.porta = Port.id or lk.portb = Port.id
1288 LEFT JOIN Port AS pa ON pa.id = lk.porta
1289 LEFT JOIN Object AS oa ON pa.object_id = oa.id
1290 LEFT JOIN Port AS pb ON pb.id = lk.portb
1291 LEFT JOIN Object AS ob ON pb.object_id = ob.id
1292
1293 LEFT JOIN PortLog ON PortLog.id = (SELECT id FROM PortLog WHERE PortLog.port_id = Port.id ORDER BY date DESC LIMIT 1)
1294WHERE
1295 $sql_where_clause
1296END;
1297
1298 $result = usePreparedSelectBlade ($query, $query_params);
1299
1300 $ret = array();
1301 while ($row = $result->fetch (PDO::FETCH_ASSOC))
1302 {
1303 $row['l2address'] = l2addressFromDatabase ($row['l2address']);
1304 $row['linked'] = isset ($row['remote_id']) ? 1 : 0;
1305
1306 // last changed log
1307 $row['last_log'] = array();
1308 if ($row['log_count'])
1309 {
1310 $row['last_log']['user'] = $row['user'];
1311 $row['last_log']['time'] = $row['time'];
1312 }
1313 unset ($row['user']);
1314 unset ($row['time']);
1315
1316 $ret[] = $row;
1317 }
1318 return $ret;
1319} /* lm_fetchPortList */
1320
1321function lm_getPortInfo ($port_id, $back = false)
1322{
1323 $linktable = ($back ? 'LinkBackend' : 'Link');
1324 if($back)
1325 $result = lm_fetchPortList ('Port.id = ?', array ($port_id), $linktable);
1326 else
1327 $result = fetchPortList ('Port.id = ?', array ($port_id));
1328
1329 //return empty ($result) ? NULL : $result[0];
1330 return $result;
1331} /* lm_getPortInfo */
1332
1333/* -------------------------------------------------------------------------- */
1334/* -------------------------------------------------- */
1335
1336function linkmgmt_opHelp() {
1337?>
1338 <table cellspacing=10><tr><th>Help</th><tr>
1339 <tr><td width=150></td><td width=150 style="font-weight:bold;color:<?php echo portlist::CURRENT_OBJECT_BGCOLOR; ?>">Current Object</td></tr>
1340 <tr><td></td><td bgcolor=<?php echo portlist::CURRENT_PORT_BGCOLOR; ?>>[current port]</td></tr>
1341 <tr><td>front link</td><td>[port]<(Object)</td><td>back link</td></tr>
1342 <tr><td>back link</td><td>(Object)>[port]</td><td>front link</td></tr>
1343 <tr><td></td><td><pre>----></pre></td><td>Front link</td></tr>
1344 <tr><td></td><td><pre>====></pre></td><td>Backend link</td></tr>
1345 <tr><td></td><td>Link Symbol</td><td>Create new link</td></tr>
1346 <tr><td></td><td>Cut Symbol</td><td>Delete link</td></tr>
1347
1348 </table>
1349
1350<?php
1351 exit;
1352} /* opHelp */
1353
1354/* -------------------------------------------------- */
1355
1356function linkmgmt_ajax_mapinfo() {
1357
1358 $object_id = NULL;
1359 $port_id = NULL;
1360 $remote_id = NULL;
1361 $linktype = NULL;
1362
1363 if(isset($_REQUEST['object_id']))
1364 $object_id = $_REQUEST['object_id'];
1365
1366 if(isset($_REQUEST['port_id']))
1367 $port_id = $_REQUEST['port_id'];
1368
1369 if(isset($_REQUEST['remote_id']))
1370 $remote_id = $_REQUEST['remote_id'];
1371
1372 if(isset($_REQUEST['linktype']))
1373 $linktype = $_REQUEST['linktype'];
1374
1375 $debug = NULL;
1376 if(isset($_REQUEST['debug']))
1377 $debug['value'] = $_REQUEST['debug'];
1378
1379 $info = array();
1380
1381 echo "<table style=\"font-size:12;\"><tr>";
1382
1383 if($port_id != NULL)
1384 {
1385 $port = new linkmgmt_RTport($port_id);
1386
1387 echo "<td>";
1388 $port->printtable('both');
1389 echo "</td>";
1390
1391 if($debug)
1392 $debug['port'] = &$port;
1393
1394 if($remote_id != NULL)
1395 {
1396
1397 $remote_port = new linkmgmt_RTport($remote_id);
1398
1399 echo "<td><table align=\"center\">";
1400
1401 // TODO cableid
1402 echo "<tr><td><pre>".($linktype == 'back' ? ' ===> ' : ' ---> ')."</pre></td></tr>";
1403
1404 $port->printunlinktr($linktype, $remote_port);
1405
1406 echo "</table></td>";
1407
1408
1409 echo "<td>";
1410 $remote_port->printtable('both');
1411 echo "</td>";
1412
1413 if($debug)
1414 $debug['remote_port'] = &$remote_port;
1415
1416 }
1417 else
1418 $port->printunlinktr();
1419
1420
1421 }
1422 echo "</tr><tr>";
1423
1424 echo "<td>";
1425 $object = linkmgmt_RTport::printobjecttable($object_id);
1426 echo "</td>";
1427
1428 if($debug)
1429 $debug['object'] = &$object;
1430
1431 if($remote_id != NULL)
1432 {
1433
1434 echo "<td></td>"; /* link */
1435 echo "<td>";
1436 $remote_object = linkmgmt_RTport::printobjecttable($remote_port->port['object_id']);
1437 echo "</td>";
1438
1439 if($debug)
1440 $debug['remote_object'] = &$remote_object;
1441 }
1442
1443 echo "</tr></table>";
1444
1445 if($debug)
1446 {
1447 echo "<pre>--- Debug ---";
1448 var_dump($debug);
1449 echo "</pre>";
1450 }
1451
1452 exit;
1453}
1454
1455/* -------------------------------------- */
1456function lm_renderObjectCell ($cell)
1457{
1458 echo "<table class='slbcell vscell'><tr><td rowspan=2 width='5%'>";
1459 printImageHREF ('OBJECT');
1460 echo '</td><td>';
1461 echo mkA ('<strong>' . niftyString ($cell['dname']) . '</strong>', 'object', $cell['id']);
1462 echo '</td></tr><tr><td>';
1463 echo count ($cell['etags']) ? ("<small>" . serializeTags ($cell['etags']) . "</small>") : '&nbsp;';
1464 echo "</td></tr></table>";
1465}
1466/* -------------------------------------- */
1467
1468// TODO replace..
1469class linkmgmt_RTport {
1470
1471 private $port_id = NULL;
1472
1473 public $port = false;
1474
1475 function __construct($port_id) {
1476
1477 $this->port = getPortInfo($port_id);
1478
1479 if($this->port === false)
1480 return;
1481
1482 /* successfully get port info */
1483 $this->port_id = $port_id;
1484
1485 } /* __construct */
1486
1487 function isvalid() {
1488 return ($port_id !== NULL);
1489 }
1490
1491 function getlinks($type = 'front') {
1492 } /* getlinks */
1493
1494 function printtable($linktype = 'front') {
1495
1496 if($this->port_id == NULL)
1497 return;
1498
1499 echo "<table>";
1500
1501 $urlparams = array(
1502 'module' => 'redirect',
1503 'page' => 'object',
1504 'tab' => 'linkmgmt',
1505 'op' => 'map',
1506 'object_id' => $this->port['object_id'],
1507 'port_id' => $this->port_id,
1508 'usemap' => 1,
1509 );
1510
1511 echo '<tr><td><a title="don\'t highlight port" href="?'.http_build_query($urlparams).'">-phl</a></td>';
1512
1513 $urlparams['hl'] = 'p';
1514 echo '<td><a title="highlight port" href="?'.http_build_query($urlparams).'">+phl</a></td></tr>';
1515
1516 $this->_printinforow($this->port,
1517 array(
1518 'id' => 'Port ID',
1519 'name' => 'Port Name',
1520 'oif_name' => 'Port Type',
1521 'l2address' => 'MAC',
1522 'reservation_comment' => 'comment',
1523 )
1524 ); /* printinforow */
1525
1526 $this->printlinktr($linktype);
1527
1528 echo "</table>";
1529 } /* printtable */
1530
1531 function printlinktr($linktype = 'front') {
1532 if($this->port_id === NULL)
1533 return;
1534
1535 $urlparams = array(
1536 'tab' => 'linkmgmt',
1537 'page' => 'object',
1538 'op'=>'PortLinkDialog',
1539 'port'=>$this->port_id,
1540 'object_id'=>$this->port['object_id'],
1541 'linktype' => $linktype,
1542 );
1543
1544 echo "<tr><td align=\"center\"><a href='".
1545 makeHrefProcess($urlparams).
1546 "'>";
1547 printImageHREF ('plug', 'Link this port');
1548 echo "</a></td></tr>";
1549 } /* link */
1550
1551 function printunlinktr($linktype = 'front', $remote_port = NULL) {
1552 if($this->port_id === NULL)
1553 return;
1554
1555 $urlparams = array(
1556 'tab' => 'linkmgmt',
1557 'op'=>'unlinkPort',
1558 'port_id'=>$this->port_id,
1559 'object_id'=>$this->port['object_id'],
1560 'linktype' => $linktype,
1561 );
1562
1563 $confirmmsg = "unlink port ".$this->port['name'];
1564
1565 if($remote_port !== NULL)
1566 {
1567 $urlparams['remote_id'] = $remote_port->port['id'];
1568 $confirmmsg .= ' -> '.$remote_port->port['name'];
1569 }
1570
1571 $confirmmsg .= " ($linktype)"; // TODO cableid
1572
1573 echo "<tr><td align=\"center\"><a href='".makeHrefProcess($urlparams).
1574 "' onclick=\"return confirm('$confirmmsg');\">";
1575 printImageHREF ('cut', 'Unlink this port');
1576 echo "</a></td></tr>";
1577
1578 } /* unlink */
1579
1580 /* TODO move to object class */
1581 static function printobjecttable($object_id = NULL) {
1582
1583 if($object_id === NULL)
1584 return;
1585
1586 $object = spotEntity ('object', $object_id);
1587
1588 if($object === false)
1589 return;
1590
1591 if($object['rack_id'])
1592 {
1593 $rack = spotEntity('rack', $object['rack_id']);
1594
1595 $object['row_name'] = $rack['row_name'];
1596 $object['rack_name'] = $rack['name'];
1597 }
1598
1599 echo "<table><tr><td>";
1600 lm_renderObjectCell($object);
1601 echo "</td></tr><tr><td><table>";
1602
1603 self::_printinforow($object,
1604 array(
1605 'id' => 'ID',
1606 'dname' => 'Name',
1607 'label' => 'Label',
1608 'rack_name' => 'Rack',
1609 'row_name' => 'Row',
1610 )
1611
1612 ); /* printinforow */
1613
1614 $urlparams = array(
1615 'module' => 'redirect',
1616 'page' => 'object',
1617 'tab' => 'linkmgmt',
1618 'op' => 'map',
1619 'object_id' => $object_id,
1620 'usemap' => 1,
1621 );
1622
1623 echo '<tr><td><a title="don\'t highlight object" href="?'.http_build_query($urlparams).'">-ohl</a></td>';
1624
1625 $urlparams['hl'] = 'o';
1626 echo '<td><a title="highlight object" href="?'.http_build_query($urlparams).'">+ohl</a></td></tr>';
1627
1628 echo "</table></td></tr></table>";
1629
1630 return $object;
1631
1632 } /* printobjecttable */
1633
1634 static function _printinforow(&$data, $config) {
1635
1636 foreach($config as $key => $name)
1637 {
1638 if(isset($data[$key]))
1639 {
1640 $value = $data[$key];
1641 if(!empty($value))
1642 echo "<tr><td align=\"right\" nowrap=\"nowrap\" style=\"font-size:10;\">$name:</td><td nowrap=\"nowrap\">$value</td></tr>";
1643 }
1644 }
1645
1646 } /* _printinforow */
1647} /* class RTport */
1648
1649/* -------------------------------------------------- */
1650
1651function linkmgmt_opmap() {
1652
1653 /* display require errors "white screen of death" */
1654 $errorlevel = error_reporting();
1655 error_reporting(E_ALL);
1656
1657 require_once 'Image/GraphViz.php';
1658
1659/*
1660 *
1661 */
1662class lm_Image_GraphViz extends Image_GraphViz {
1663
1664 /* extend renderDotFile with additional output file
1665 */
1666 function renderDotFile($dotfile, $outputfile, $format = 'svg',
1667 $command = null, $outputfile2 = null, $format2 = null)
1668 {
1669 if (!file_exists($dotfile)) {
1670 if ($this->_returnFalseOnError) {
1671 return false;
1672 }
1673 $error = PEAR::raiseError('Could not find dot file');
1674 return $error;
1675 }
1676
1677 $oldmtime = file_exists($outputfile) ? filemtime($outputfile) : 0;
1678
1679 switch ($command) {
1680 case 'dot':
1681 case 'neato':
1682 break;
1683 default:
1684 $command = $this->graph['directed'] ? 'dot' : 'neato';
1685 }
1686 $command_orig = $command;
1687
1688 $command = $this->binPath.(($command == 'dot') ? $this->dotCommand
1689 : $this->neatoCommand);
1690
1691 $command .= ' -T'.escapeshellarg($format)
1692 .' -o'.escapeshellarg($outputfile)
1693 .($format2 !== null && $outputfile2 !== null ? ' -T'.escapeshellarg($format2).' -o'.escapeshellarg($outputfile2) : '')
1694 .' '.escapeshellarg($dotfile)
1695 .' 2>&1';
1696
1697 exec($command, $msg, $return_val);
1698
1699 clearstatcache();
1700 if (file_exists($outputfile) && filemtime($outputfile) > $oldmtime
1701 && $return_val == 0) {
1702 return true;
1703 } elseif ($this->_returnFalseOnError) {
1704 return false;
1705 }
1706 $error = PEAR::raiseError($command_orig.' command failed: '
1707 .implode("\n", $msg));
1708 return $error;
1709 }
1710 // renderDotFile
1711
1712
1713 /*
1714 */
1715 function fetch($format = 'svg', $command = null, $format2 = null, &$data2 = null)
1716 {
1717
1718 $file = $this->saveParsedGraph();
1719 if (!$file || PEAR::isError($file)) {
1720 return $file;
1721 }
1722
1723 $outputfile = $file . '.' . $format;
1724
1725 if($format2 != null && $data2 !== null)
1726 $outputfile2 = $file . '.' . $format2;
1727 else
1728 $outputfile2 = null;
1729
1730 $rendered = $this->renderDotFile($file, $outputfile, $format,
1731 $command, $outputfile2, $format2);
1732 if ($rendered !== true) {
1733 return $rendered;
1734 }
1735
1736 @unlink($file);
1737
1738 if($format2 !== null && $data2 !== null) {
1739 $fp = fopen($outputfile2, 'rb');
1740
1741 if ($fp) {
1742 $data = fread($fp, filesize($outputfile2));
1743 fclose($fp);
1744 @unlink($outputfile2);
1745
1746 $data2 = $data;
1747 } else {
1748 return $error;
1749 }
1750 }
1751
1752
1753 $fp = fopen($outputfile, 'rb');
1754
1755 if (!$fp) {
1756 if ($this->_returnFalseOnError) {
1757 return false;
83b49cf7 1758 }
1759 $error = PEAR::raiseError('Could not read rendered file');
1760 return $error;
1761 }
1762
3badc888 1763 $data = fread($fp, filesize($outputfile));
1764 fclose($fp);
1765 @unlink($outputfile);
1766
1767 return $data;
1768 }
1769 // fetch
1770
1771
1772
1773} /* class lm_Image_GraphViz */
1774
1775 error_reporting($errorlevel);
1776
1777 $object_id = NULL;
1778 $port_id = NULL;
1779 $remote_id = NULL;
1780 $allports = false;
1781 $usemap = false;
1782 $command = NULL;
1783
1784 /* highlight object */
1785 $hl = NULL;
1786 if(isset($_REQUEST['hl']))
1787 {
1788 $hl = $_REQUEST['hl'];
1789 unset($_REQUEST['hl_object_id']);
1790 unset($_REQUEST['hl_port_id']);
1791
1792 if($hl == 'o')
1793 {
1794 unset($_GET['port_id']);
1795 unset($_GET['remote_id']);
1796 }
1797
1798 }
1799
1800 if(!$hl && isset($_REQUEST['hl_object_id']))
1801 {
1802 $hl = 'o';
1803 $object_id = $_REQUEST['hl_object_id'];
1804 $_REQUEST['object_id'] = $object_id;
1805 unset($_REQUEST['hl_object_id']);
1806 unset($_REQUEST['hl_port_id']);
1807 unset($_REQUEST['port_id']);
1808 }
1809
1810 if(isset($_REQUEST['object_id']))
1811 $object_id = $_REQUEST['object_id'];
1812
1813 if(isset($_REQUEST['type']))
1814 {
1815 $type = $_REQUEST['type'];
1816 }
1817 else
1818 $type = 'gif';
1819
1820 /* highlight port */
1821 if(!$hl && isset($_REQUEST['hl_port_id']))
1822 {
1823 $hl = 'p';
1824 $port_id = $_REQUEST['hl_port_id'];
1825 $_REQUEST['port_id'] = $port_id;
1826 unset($_REQUEST['hl_port_id']);
1827 }
1828
1829 if(isset($_REQUEST['allports']))
1830 {
1831 $allports = $_REQUEST['allports'];
1832 }
1833
1834 if(isset($_REQUEST['port_id']))
1835 {
1836 $port_id = $_REQUEST['port_id'];
1837 }
1838
1839 if(isset($_REQUEST['usemap']))
1840 $usemap = $_REQUEST['usemap'];
1841
1842 if($hl == 'p' && $port_id === NULL)
1843 {
1844 unset($_GET['hl']);
1845 unset($_GET['port_id']);
1846 unset($_GET['remote_id']);
1847 }
1848
1849 if($hl == 'o')
1850 unset($_GET['remote_id']);
1851
1852 if(isset($_REQUEST['remote_id']))
1853 $remote_id = $_REQUEST['remote_id'];
1854
1855 /* show all objects */
1856 if(isset($_REQUEST['all']))
1857 {
1858 $object_id = NULL;
1859 $port_id = NULL;
1860 $hl = NULL;
1861 unset($_GET['hl']);
1862 }
1863
1864 if(isset($_REQUEST['cmd']))
1865 $command = $_REQUEST['cmd'];
1866
1867 if(isset($_REQUEST['debug']))
1868 $debug = $_REQUEST['debug'];
1869 else
1870 $debug = False;
1871
1872 if($debug) echo "-- DEBUG --<br>";
1873
1874
1875 switch($type) {
1876 case 'gif':
1877 case 'png':
1878 case 'bmp':
1879 case 'jpeg':
1880 case 'tif':
1881 case 'wbmp':
1882 $ctype = "image/$type";
1883 break;
1884 case 'jpg':
1885 $ctype = "image/jpeg";
1886 break;
1887 case 'svg':
1888 $ctype = 'image/svg+xml';
1889 break;
1890 case 'pdf':
1891 $ctype = 'application/pdf';
1892 break;
1893 case 'cmapx':
1894 $ctype = 'text/plain';
1895 break;
1896 }
1897
1898 $start = microtime(true);
1899 $gvmap = new linkmgmt_gvmap($object_id, $port_id, $allports, $hl, $remote_id);
1900 $stop = microtime(true);
1901
1902 if($debug)
1903 echo "gvmap Time: ".( $stop - $start )."<br>";
1904
1905 if($debug) echo "-- after gvmap --<br>";
1906
1907 if($usemap)
1908 {
1909
1910 if($debug) echo "-- usemap --<br>";
1911
1912 /* add context menu to Ports, Objects, Links, ...
1913 */
1914
1915 echo "<script>
1916 function initcontextmenu() {
1917 var maps = document.getElementsByTagName('map');
1918 for(var i=0;i<maps.length;i++) {
1919 var areas = maps[i].childNodes;
1920
1921 for(j=0;j<areas.length;j++) {
1922 if(areas[j].nodeType == 1)
1923 {
1924 // console.log(areas[j].id);
1925 // attr = document.createAttribute('onmouseover','ahh');
1926 // areas[j].setAttribute(attr);
1927 // areas[j].onmouseover = 'menu(this);';
1928
1929 areas[j].addEventListener('contextmenu',menu,false);
1930 // areas[j].oncontextmenu = 'menu(this, event);';
1931 // console.log(areas[j].oncontextmenu);
1932 }
1933 }
1934
1935 }
1936
1937 };
1938
1939 function menu(event) {
1940
1941 // console.log('Menu');
1942
1943 if(!event)
1944 event = window.event;
1945
1946 var parent = event.target;
1947
1948 // console.log('--' + parent);
1949
1950 var ids = parent.id.split('-');
1951
1952 if(ids[0] == 'graph1')
1953 return false;
1954
1955 var object_id = ids[0];
1956
1957 var url ='?module=ajax&ac=lm_mapinfo&object_id=' + object_id;
1958
1959 // links ='<li><a href=' + object_id + '>Object</a></li>';
1960
1961 if(ids[1] != '')
1962 {
1963 var port_id = ids[1];
1964 url += '&port_id=' + port_id;
1965 // links += '<li><a href=' + port_id + '>Port</a></li>';
1966
1967 if(ids[2] != '')
1968 {
1969 var remote_id = ids[2];
1970
1971 if(ids[3] != '')
1972 {
1973 var linktype = ids[3];
1974 url += '&remote_id=' + remote_id + '&linktype=' + linktype;
1975 // links += '<li><a href=' + port_id + '_' + remote_id + '_' + linktype + '>Unlink</a></li>';
1976 }
1977 }
1978
1979 }
1980
1981
1982 var xmlHttp = new XMLHttpRequest();
1983 xmlHttp.open('GET', url, false);
1984 xmlHttp.send(null);
1985
1986 var infodiv = document.getElementById('info');
1987 infodiv.innerHTML = xmlHttp.responseText;
1988
1989 // linkdiv = document.getElementById('link');
1990 // linkdiv.innerHTML = links;
1991
1992 var menudiv = document.getElementById('menu');
1993 menudiv.style.position = 'absolute';
1994 menudiv.style.top = (event.clientY + document.body.scrollTop) + 'px';
1995 menudiv.style.left = (event.clientX + document.body.scrollLeft) + 'px';
1996 menudiv.style.display = '';
1997
1998 return false;
1999 };
2000
2001 function mousedown(event) {
2002 // console.log('mouse down');
2003
2004 if(!event)
2005 event = window.event;
2006
2007 if(event.button != 2)
2008 return true;
2009
2010 var menudiv = document.getElementById('menu');
2011
2012 menudiv.style.display = 'none';
2013
2014 return false;
2015 };
2016
2017 </script>";
2018
2019 echo "<body oncontextmenu=\"return false\" onmousedown=\"mousedown(event);\" onload=\"initcontextmenu();\">";
2020
2021 echo "<div id=\"menu\" style=\"display:none; background-color:#ffff90\">
2022 <div id=\"info\"></div>
2023 <ul id=\"link\" style=\"list-style-type:none\"></ul>
2024 </div>";
2025
2026 if($debug)
2027 $gvmap->setFalseOnError(False);
2028
2029 $data2 = '';
2030
2031 $start = microtime(true);
2032 $data = $gvmap->fetch($type, $command, 'cmapx', $data2);
2033 $stop = microtime(true);
2034
2035 if($debug)
2036 echo "DOT time: ".( $stop - $start )."<br>";
2037
2038 if($data === false)
2039 echo "ERROR Fetching image data!<br>";
2040
2041 if(PEAR::isError($data))
2042 echo $data->getMessage();
2043
2044 //echo $gvmap->fetch('cmapx', $command);
2045 echo $data2;
2046
2047 if($debug) echo "-- after map gvmap --<br>";
2048
2049 echo "<img src=\"data:$ctype;base64,".
2050 base64_encode($data).
2051 "\" usemap=#map$object_id />";
2052
2053 if($debug)
2054 {
2055 echo "<pre>";
2056 echo $gvmap->export();
2057 echo "</pre>";
2058
2059 echo "<pre>".$gvmap->parse()."</pre>";
2060 }
2061 }
2062 else
2063 {
2064 $gvmap->image($type);
2065 }
2066
2067 exit;
2068
2069} /* linkmgmt_opmap */
2070
2071class cytoscapedata
2072{
2073 public $objects = array();
2074
2075 public $pids = array();
2076
2077 public $parents = array();
2078 public $nodes = array();
2079
2080 public $edges = NULL;
2081
2082 public $debug = null;
2083
2084 function __construct()
2085 {
2086
2087 $this->edges['parents'] = array();
2088 $this->edges['nodes'] = array();
2089 }
2090
2091 function addnode($id, $values = NULL, &$arr = NULL)
2092 {
2093 $data = array( 'id' => $id );
2094
2095 if($values != NULL)
2096 $data = $data + $values;
2097
2098 $node['data'] = $data;
2099
2100 $this->objects[] = array('group' => 'nodes') + $node;
2101
2102 switch($values['type'])
2103 {
2104 case 'port':
2105 $this->nodes[$id] = array('group' => 'nodes') + $node;
2106 break;
2107 case 'object':
2108 case 'container':
2109 $this->parents[$id] = array('group' => 'nodes') + $node;
2110 break;
2111 }
2112
2113 if($arr !== NULL)
2114 $arr[] = array('group' => 'nodes') + $node;
2115 }
2116
2117 function addedge($id, $source, $target, $values = NULL, &$arr = NULL)
2118 {
2119 $data = array(
2120 'id' => $id,
2121 'source' => $source,
2122 'target' => $target
2123 );
2124
2125 if($values != NULL)
2126 $data = $data + $values;
2127
2128 $edge['data'] = $data;
2129
2130 //$this->elements['edges'][] = $edge;
2131
2132 if($arr !== NULL)
2133 {
2134 $arr[$id] = array('group' => 'edges') + $edge;
2135 }
2136 else
2137 $this->objects[] = array('group' => 'edges') + $edge;
2138
2139 //$this->edges[] = array('group' => 'edges') + $edge;
2140 }
2141
2142 function _addobjectnode($object_id, $type = 'object')
2143 {
2144 global $lc_cache;
2145
2146 if(!isset($this->parents["o$object_id"]))
2147 {
2148 $rack = null;
2149 $object = $lc_cache->getobject($object_id, $rack);
2150
2151 $clustertitle = "${object['dname']}";
2152
2153 //has_problems
2154 //if($object['has_problems'] != 'no')
2155
2156 $rack_text = "";
2157 if(!empty($rack['row_name']) || !empty($rack['name']))
2158 {
2159 $rack_text = "${rack['row_name']} / ${rack['name']}";
2160 }
2161
2162 $data = array('label' => $object['name'], 'text' => $rack_text, 'type' => $type, 'has_problems' => $object['has_problems']);
2163
2164 $container_id = $object['container_id'];
2165 if($container_id)
2166 {
2167 $data['parent'] = "o$container_id";
2168 $this->_addobjectnode($container_id, 'container');
2169 }
2170
2171 $this->addnode('o'.$object_id, $data);
2172 }
2173
2174 }
2175
2176 // cytoscape
2177 function addlinkchain($linkchain, $index) {
2178
2179 foreach($linkchain as $id => $port)
2180 {
2181
2182 if(!$linkchain->linked)
2183 continue;
2184
2185 $this->_addobjectnode($port['object_id']);
2186
2187 $text = (isset($port['portip']) ? $port['portip'] : "" );
2188 $nodedata = array('label' => $port['name'], 'parent' => 'o'.$port['object_id'], 'text' => $text, 'type' => 'port', 'index' => $index , 'loop' => ($linkchain->loop ? '1' : '0'));
2189
2190 //$this->addnode('l_'.$port['id'], array( 'label' => $port['name'], 'parent' => 'p'.$port['id'], 'text' => $text ));
2191
2192 if($port['portcount'] > 1)
2193 foreach($port['chains'] as $mlc)
2194 {
2195 $this->addlinkchain($mlc, 0); // TODO index
2196 }
2197
2198 $prevlinktype = ($port['linktype'] == 'front' ? 'back' : 'front');
2199 if($port[$prevlinktype]['portcount'] > 1)
2200 {
2201 foreach($port[$prevlinktype]['chains'] as $mlc)
2202 {
2203 $this->addlinkchain($mlc, 0, false); // TODO index
2204 }
2205 }
83b49cf7 2206
3badc888 2207 if($port['remote_id'])
2208 {
83b49cf7 2209
3badc888 2210 $this->_addobjectnode($port['remote_object_id']);
83b49cf7 2211
3badc888 2212 $linktype = $port['linktype'];
2213 $edgedata = array('label' => $port['cableid'], 'type' => $linktype, 'loop' => ($linkchain->loop ? '1' : '0'));
83b49cf7 2214
3badc888 2215 if($linkchain->loop && $port['remote_id'] == $linkchain->first)
2216 {
2217 $nodedata['loopedge'] = array('group' => 'edges', 'data' => array( 'id' => 'le'.$port['id']."_".$port['remote_id'], 'source' => 'p'.$port['id'], 'target' => 'p'.$port['remote_id']) + $edgedata);
2218 }
2219 else
2220 {
83b49cf7 2221
3badc888 2222 $this->addedge('e'.$port['id']."_".$port['remote_id'], 'p'.$port['id'], 'p'.$port['remote_id'], $edgedata);
2223 $this->addedge('e'.$port['id']."_".$port['remote_id'], 'p'.$port['id'], 'p'.$port['remote_id'], $edgedata, $this->edges['nodes']);
2224 $id1 = $port['object_id'];
2225 if($id1 > $port['remote_object_id'])
2226 {
2227 $id1 = $port['remote_object_id'];
2228 $id2 = $port['object_id'];
2229 }
2230 else
2231 $id2 = $port['remote_object_id'];
fdc74cf0 2232
3badc888 2233 $peid = "pe".$id1."_".$id2;
fdc74cf0 2234
3badc888 2235 if(isset($this->edges['parents'][$peid]))
2236 $this->edges['parents'][$peid]['data']['linkcount']++;
2237 else
2238 $this->addedge($peid, 'o'.$id1, 'o'.$id2, array('type' => $linktype, 'linkcount' => 1), $this->edges['parents']);
2239 }
2240 }
fdc74cf0 2241
3badc888 2242 $this->addnode('p'.$port['id'], $nodedata);
2243 }
2244
2245 if(0)
2246 if($linkchain->first != $linkchain->last )
fdc74cf0 2247 {
3badc888 2248 $first = $linkchain->first;
2249 $last = $linkchain->last;
2250 $this->addedge("l${first}_${last}",'p'.$first, 'p'.$last, array('type' => 'logical', 'label' => "logical"));
fdc74cf0 2251 }
2252
3badc888 2253 //portlist::var_dump_html($this->parents);
fdc74cf0 2254 }
2255
3badc888 2256 function getlinkchains($object_id) {
fdc74cf0 2257
3badc888 2258 $this->objects = array();
2259 $this->parents = array();
2260 $this->nodes = array();
2261 $this->edges['parents'] = array();
2262 $this->edges['nodes'] = array();
fdc74cf0 2263
3badc888 2264 $this->_getlinkchains($object_id);
fdc74cf0 2265 }
fdc74cf0 2266
3badc888 2267 function getelements()
fdc74cf0 2268 {
3badc888 2269 //lm_linkchain::var_dump_html($this);
2270 return array('parents' => array_values($this->parents),
2271 'nodes' => array_values($this->nodes),
2272 'edges' => array(
2273 'parents' => array_values($this->edges['parents']),
2274 'nodes' => array_values($this->edges['nodes'])
2275 ),
2276 'debug' => $this->debug
2277 );
fdc74cf0 2278
fdc74cf0 2279 }
2280
3badc888 2281 function gettest()
fdc74cf0 2282 {
3badc888 2283 return array_merge(array_values($this->parents), array_values($this->edges['parents']));
2284 //return array_merge(array_values($this->parents), array_values($this->edges['parents']), array_values($this->nodes), array_values($this->edges['nodes']));
fdc74cf0 2285 }
2286
3badc888 2287 function _getlinkchains($object_id) {
fdc74cf0 2288
fdc74cf0 2289
3badc888 2290 // container
2291 // $object = spotEntity('object', $object_id);
2292 $object['ports'] = getObjectPortsAndLinks ($object_id);
fdc74cf0 2293
3badc888 2294 $i = 0;
2295 foreach($object['ports'] as $key => $port)
2296 {
fdc74cf0 2297
3badc888 2298 if(isset($this->pids[$port['id']]))
2299 continue;
2300
2301 $i++;
2302 $lc = new lm_linkchain($port['id']);
2303
2304 $this->pids += $lc->pids;
2305
2306 $this->addlinkchain($lc, $i);
2307
2308 }
2309
2310 $children = getEntityRelatives ('children', 'object', $object_id);
2311
2312 foreach($children as $child)
2313 $this->_getlinkchains($child['entity_id']);
2314 }
2315
2316 function allobjects()
fdc74cf0 2317 {
3badc888 2318
2319 $this->objects = array();
2320 $this->parents = array();
2321 $this->nodes = array();
2322 $this->edges['parents'] = array();
2323 $this->edges['nodes'] = array();
2324
2325 $objects = listCells('object');
2326
2327 $i = 0;
2328 foreach($objects as $object)
2329 {
2330 //echo $object['id']."<br>";
2331 $this->_getlinkchains($object['id']);
2332 $i++;
2333 if($i > 20 ) break;
2334 }
fdc74cf0 2335 }
3badc888 2336}
fdc74cf0 2337
3badc888 2338function linkmgmt_cytoscapemap() {
fdc74cf0 2339
fdc74cf0 2340
3badc888 2341 $object_id = $_GET['object_id'];
fdc74cf0 2342
3badc888 2343 if(isset($_GET['json']))
2344 {
2345 ob_start();
2346 $data = new cytoscapedata();
2347 $data->getlinkchains($object_id);
2348 //$data->allobjects(); // ugly graph;
2349 //echo json_encode($data->objects);
fdc74cf0 2350
3badc888 2351 if(ob_get_length())
2352 $data->debug = ob_get_contents();
fdc74cf0 2353
3badc888 2354 ob_end_clean();
2355
2356 echo json_encode($data->getelements());
fdc74cf0 2357
3badc888 2358 exit;
fdc74cf0 2359 }
2360
3badc888 2361 echo (<<<HTMLEND
2362<!DOCTYPE html>
2363<html>
2364<head>
2365<style>
2366body {
2367 font: 14px helvetica neue, helvetica, arial, sans-serif;
2368}
fdc74cf0 2369
3badc888 2370#cy {
2371 height: 100%;
2372 width: 100%;
2373 position: absolute;
2374 left: 0;
2375 top: 0;
2376}
2377</style>
2378<meta charset=utf-8 />
2379<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
2380<title>Compound nodes</title>
2381<!--<script src="js/jquery-1.4.4.min.js"></script>-->
2382
383230c5 2383<script src="?module=chrome&uri=linkmgmt/jquery-1.11.3.min.js"></script>
2384<script src="?module=chrome&uri=linkmgmt/cytoscape.min.js"></script>
2385<script src="?module=chrome&uri=linkmgmt/dagre.min.js"></script>
2386<script src="?module=chrome&uri=linkmgmt/cytoscape-dagre.js"></script>
2387<link rel="stylesheet" type="text/css" href="?module=chrome&uri=linkmgmt/jquery.qtip.min.css">
2388<script src="?module=chrome&uri=linkmgmt/jquery.qtip.min.js"></script>
2389<script src="?module=chrome&uri=linkmgmt/cytoscape-qtip.js"></script>
3badc888 2390
383230c5 2391<!--<script src="?module=chrome&uri=linkmgmt/cytoscape-css-renderer_mod.js"></script>-->
2392<!--<script src="?module=chrome&uri=linkmgmt/cytoscape.js-navigator.js_mod"></script>-->
3badc888 2393<script>
2394var cy = null;
2395var cy2 = null;
2396var layout = null;
2397var data = null;
2398
2399$(function(){ // on dom ready
2400 var cystyle = [
2401 {
2402 selector: 'node',
2403 css: {
2404 'content': 'data(id)',
2405 'text-wrap': 'wrap'
2406 },
2407 style: {
2408 'background-color': '#666',
2409 'label': 'data(label)',
2410 'width': 'label',
2411 'min-zoomed-font-size' : 8,
2412 'text-valign': 'center',
2413 'text-halign': 'center',
2414 'text-wrap': 'wrap',
2415 'shape': function(ele) {
2416 if(ele.data('type') != 'port')
2417 return 'rectangle';
2418 else
2419 return 'ellipse';
2420 },
fdc74cf0 2421
3badc888 2422 }
2423 },
2424 {
2425 selector: '\$node > node',
2426 css: {
2427 'padding-top': '10px',
2428 'padding-left': '10px',
2429 'padding-bottom': '10px',
2430 'padding-right': '10px',
2431 'text-valign': 'top',
2432 'text-halign': 'center',
2433 'background-color': '#bbb',
2434 'min-zoomed-font-size' : 6,
2435 }
2436 },
2437 {
2438 selector: 'edge',
2439 css: {
2440 'target-arrow-shape': 'triangle',
2441 'line-color': function(ele){
2442 if(ele.data('type') == 'front')
2443 return 'black';
2444 else
2445 return 'grey';
2446 },
2447 'line-style': function(ele){
2448 if(ele.data('type') == 'front')
2449 return 'solid';
2450 else
2451 return 'dashed';
2452 },
2453 'width': function(ele){
2454 var ret = 1;
2455 if(ele.data('type') == 'front')
2456 ret = 3;
2457 else
2458 ret = 5;
fdc74cf0 2459
3badc888 2460 if(1)
2461 if(ele.data('linkcount'))
2462 {
2463 ret = (ele.data('linkcount') * ret);
2464 }
fdc74cf0 2465
3badc888 2466 //console.log(ele.data('id') + ret);
2467 return ret;
2468 },
2469// 'curve-style': 'segments',
2470 'font-size': '8',
2471 'min-zoomed-font-size' : 8,
2472 'label': 'data(label)',
2473 'edge-text-rotation': 'autorotate',
2474 'source-arrow-shape': 'none',
2475 'target-arrow-shape': 'none'
2476 }
2477 },
2478 {
2479 selector: '.logical',
2480 css: {
2481 'line-color': '#0000ff',
2482 'width': 5,
2483 'curve-style': 'segments',
2484 'z-index': 0
2485 }
2486 },
2487 {
2488 selector: ':selected',
2489 css: {
2490 'background-color': 'black',
2491 'line-color': 'black',
2492 'target-arrow-color': 'black',
2493 'source-arrow-color': 'black'
2494 }
2495 },
2496 {
2497 selector: '.highlighted',
2498 css: {
2499 'line-color': '#ff0000',
2500 'background-color': '#ff0000'
2501 }
2502 },
2503 {
2504 selector: '.clhighlighted',
2505 css: {
2506 'line-color': '#00ff00',
2507 'background-color': '#00ff00'
2508 }
2509 }
2510 ];
fdc74cf0 2511
3badc888 2512var cylayout = { name: 'dagre', nodeSep: 3, ready: layoutready, stop: layoutstop };
fdc74cf0 2513
3badc888 2514function highlight(evt) {
fdc74cf0 2515
3badc888 2516 //cy = evt.cy;
fdc74cf0 2517
3badc888 2518 var hlclass = evt.data.hlclass;
2519 var ele = evt.cyTarget;
fdc74cf0 2520
3badc888 2521 if(!ele.data)
2522 return;
fdc74cf0 2523
3badc888 2524 if(ele.data('source'))
2525 ele = cy.$( '#' + ele.data('source'));
fdc74cf0 2526
3badc888 2527 var id = ele.data('id');
fdc74cf0 2528
3badc888 2529 if(ele.isParent())
2530 {
2531 // var childs = cy.$('#' + id).children()
2532 // childs.layout({name:'grid', cols: 2, condense: true});
2533 return;
2534 }
fdc74cf0 2535
fdc74cf0 2536
3badc888 2537// console.log('Event ' + hlclass + ' ' + id );
fdc74cf0 2538
3badc888 2539 // remove existing highlights
2540 cy.$('.' + hlclass).removeClass(hlclass);
fdc74cf0 2541
3badc888 2542 var hleles = ele.closedNeighborhood();
fdc74cf0 2543
3badc888 2544 var hlcount = hleles.length;
fdc74cf0 2545
3badc888 2546 // max 100
2547 for(i=0;i<100;i++)
2548 {
2549 hleles = hleles.closedNeighborhood();
fdc74cf0 2550
3badc888 2551 if(hlcount == hleles.length)
2552 break;
fdc74cf0 2553
3badc888 2554 hlcount = hleles.length;
fdc74cf0 2555
3badc888 2556 }
2557 //console.log('End Loop ' + i );
2558 hleles.addClass(hlclass);
fdc74cf0 2559
3badc888 2560// console.log('Event Type: ' + evt.originalEvent.type)
2561// console.log('Event Type: ' + evt.type)
fdc74cf0 2562
3badc888 2563 if(evt.type != 'click')
2564 return;
fdc74cf0 2565
3badc888 2566 var hleles2 = hleles.clone();
2567 hleles2 = hleles2.add(hleles.parents().clone());
fdc74cf0 2568
3badc888 2569 //var cy2 = evt.data.cy2;
fdc74cf0 2570
3badc888 2571 //var ret = evt.data.ret;
fdc74cf0 2572
3badc888 2573 //var j = ret.parents.concat(ret.nodes).concat(ret.edges.nodes);
fdc74cf0 2574
3badc888 2575 cy2.remove(cy2.elements());
2576 cy2.add(hleles2);
2577 //cy2.add(j);
2578 cy2.layout({name: 'dagre', rankDir: 'LR', ready: layoutready});
2579}
fdc74cf0 2580
3badc888 2581function cytoscapeswitch(evt)
2582{
2583 var b = evt.target;
fdc74cf0 2584
3badc888 2585 var j = null;
2586 if(b.value == "1")
2587 {
2588 j = data.parents.concat(data.edges.parents);
2589 b.value = "0";
2590 b.innerHTML = "Ports";
2591 }
2592 else
2593 {
2594 j = data.parents.concat(data.nodes).concat(data.edges.nodes);
2595 b.value = "1";
2596 b.innerHTML = "Objects";
2597 }
fdc74cf0 2598
3badc888 2599 //layout.stop();
2600 cy.remove(cy.elements());
fdc74cf0 2601
3badc888 2602 cy.add(j);
fdc74cf0 2603
3badc888 2604 cy.layout(cylayout);
2605 //layout.run();
2606}
fdc74cf0 2607
3badc888 2608$.ajax({
2609 type: "GET",
2610 url: "{$_SERVER['PHP_SELF']}",
2611 data: { module: 'redirect',
2612 page: 'object',
2613 tab: 'linkmgmt',
2614 object_id: $object_id,
2615 op: 'cytoscapemap',
2616 json: 'json'
2617 },
2618 dataTye: 'json',
2619 error: function(){ alert("Error loading"); },
2620 success: function(jdata) {
2621
2622 data = JSON.parse(jdata);
2623
2624 if(data.debug)
2625 $('#debug').html(data.debug);
2626
2627 //j = data.parents.concat(data.edges.parents);
2628 j = data.parents.concat(data.nodes).concat(data.edges.nodes);
2629
2630 if(j.length == 0)
2631 {
2632 alert("No Links to display. Closing Window");
2633 window.close();
2634 return;
2635 }
fdc74cf0 2636
3badc888 2637 cy2 = cytoscape({
2638 container: document.getElementById('cy2'),
2639 //renderer: { name: 'css' },
2640
2641 boxSelectionEnabled: false,
2642 autounselectify: true,
2643 style: cystyle,
2644 wheelSensitivity: 0.1,
2645 });
2646 cy2.style().selector('node').style('label', function(node) { return node.data('label') + '\\n' + node.data('text'); });
2647
2648 cystyle.push({
2649 selector: '.loopedge',
2650 css: {
2651 'curve-style': 'segments',
2652 }
2653 });
2654
2655 cy = cytoscape({
2656 container: document.getElementById('cy'),
2657
2658 boxSelectionEnabled: false,
2659 autounselectify: true,
2660 style: cystyle,
2661 wheelSensitivity: 0.1,
2662 elements: j,
2663 layout: cylayout,
2664 });
2665
2666 cy.on('mouseover', { hlclass: 'highlighted' }, highlight );
2667 cy.on('click', { hlclass: 'clhighlighted' }, highlight );
2668
2669 $('#switch').click(cytoscapeswitch);
2670
2671 /*
2672 TODO: node ranking
2673 */
2674
2675 if(0)
2676 cy.layout({
2677 name: 'dagre',
2678 /*
2679 nodeSep: 5,
2680 rankDir: 'TB',
2681 */
2682 //edgeSep: 10,
2683 /*
2684 edgeWeight: function(edge) {
2685 if(edge.data('type') == 'front')
2686 return 10;
2687 else
2688 return 1;
2689
2690 },
2691 */
2692 ready: layoutready,
2693 stop: layoutstop
2694 });
2695
2696 if(1)
2697 cy.$(':child').qtip({
2698
2699 content: function() {
2700
2701 return this.id() + '<br>'
2702 + 'Index: ' + this.data('index') + '<br>'
2703 + 'Name: ' + this.data('label') + '<br>'
2704 + 'Text: ' + this.data('text');
2705 },
2706 position: {
2707 my: 'top center',
2708 at: 'bottom center'
2709 },
2710 style: {
2711 classes: 'qtip-bootstrap',
2712 tip: {
2713 width: 16,
2714 height: 8
2715 },
2716 }
2717 }); // qtip
2718 } // success function
2719 });
2720
2721
2722}); // on dom ready
2723
2724function layoutready(evt) {
2725 var _cy = evt.cy;
fdc74cf0 2726
3badc888 2727 // highlight current object
2728 var object = _cy.$('#o$object_id');
2729 if(object.data('has_problems') == 'no')
2730 object.style('background-color','#ffcccc');
fdc74cf0 2731
3badc888 2732 // highlight object with problems
2733 _cy.$('node[type = "object"][has_problems != "no"]').style('background-color','#ff0000');
de3d42dc 2734
3badc888 2735 var e = _cy.$('node[loop = "1"]').style('background-color','#ff6666');
83b49cf7 2736
3badc888 2737// console.log(e[0].data('loop'));
de3d42dc 2738
3badc888 2739// cy.add({group: 'edges', data: {id:'l691_2991', source: 'p691', target: 'p2991', label: 'test'}, classes: 'logical'});
de3d42dc 2740
3badc888 2741}
fdc74cf0 2742
3badc888 2743function layoutstop(evt) {
2744 var _cy = evt.cy;
2745// cy.elements().locked = true;
2746// cy.add({group: 'nodes', data: {id:'l2493', parent:'p2943', label: 'test'}});
fdc74cf0 2747
fdc74cf0 2748
3badc888 2749 var les = _cy.$('[loopedge]');
fdc74cf0 2750
3badc888 2751 if(les)
fdc74cf0 2752 {
3badc888 2753 _cy.batch( function() {
2754 les.each(function(i, ele) {
2755 var le = ele.data('loopedge');
2756 var edge = _cy.add(le);
2757 edge.addClass('loopedge');
2758 //edge.style('line-color', '#ffffff'); // TypeError text-transform undefined
2759 });
2760 });
2761 _cy.$('edge[loop = "1"]').style('line-color','#ff6666');
fdc74cf0 2762 }
3badc888 2763}
2764</script>
2765</head>
2766<body>
2767<div id="cy" style="position: absolute; height: 80%; width: 100%; left: 0; top: 20%;"></div>
2768<div id="cy2" style="position: absolute; height: 20%; width: 100%; left: 0; top: 0%;"></div>
2769<div id="debug"></div>
2770<button type="button" id="switch" style="position: absolute;" value="1">Object</button>
2771</body>
2772</html>
2773HTMLEND
2774); // echo
fdc74cf0 2775 exit;
3badc888 2776}
fdc74cf0 2777
2778/* ------------------------------------- */
2779class linkmgmt_gvmap {
2780
2781 private $object_id = NULL;
2782 private $port_id = NULL;
2783 private $remote_id = NULL;
3badc888 2784 private $hl = NULL;
fdc74cf0 2785
2786 private $gv = NULL;
2787
2788 private $ports = array();
2789
2790 private $allports = false;
2791 private $back = NULL;
2792
adc1e20b 2793 private $alpha = 'ff';
fdc74cf0 2794
2795 private $errorlevel = NULL;
2796
3badc888 2797 public $data = NULL;
2798
2799 private $pids = array();
2800
2801 function addlinkchainsobject($object_id)
2802 {
2803
2804 $object['ports'] = getObjectPortsAndLinks ($object_id);
2805
2806 if(empty($object['ports']))
2807 {
2808
2809 $hl = false;
2810 $alpha = $this->alpha;
2811 if($this->hl == 'o' && $this->object_id == $object_id)
2812 {
2813 $hl = true;
2814 $this->alpha = 'ff';
2815 }
2816
2817 $this->_addCluster($object_id, $hl, empty($object['ports']));
2818
2819 $this->alpha = $alpha;
2820 return;
2821 }
2822
2823 $i = 0;
2824 foreach($object['ports'] as $key => $port)
2825 {
2826
2827 if(isset($this->pids[$port['id']]))
2828 continue;
2829
2830 $i++;
2831 $lc = new lm_linkchain($port['id']);
2832
2833 $this->pids += $lc->pids;
2834
2835 if($this->allports ||($lc->linkcount > 0))
2836 $this->addlinkchain($lc, $i);
2837 }
2838
2839 }
2840
fdc74cf0 2841 function __construct($object_id = NULL, $port_id = NULL, $allports = false, $hl = NULL, $remote_id = NULL) {
2842 $this->allports = $allports;
2843
4eeb9282 2844 $this->object_id = $object_id;
2845 $this->port_id = $port_id;
2846 $this->remote_id = $remote_id;
3badc888 2847 $this->hl = $hl;
fdc74cf0 2848
2849 $hllabel = "";
2850
2851 /* suppress strict standards warnings for Image_GraphViz and PHP 5.4.0
2852 * output would corrupt image data
2853 */
2854 $this->errorlevel = error_reporting();
857ba778 2855 error_reporting($this->errorlevel & ~E_STRICT);
fdc74cf0 2856
2857 $graphattr = array(
3badc888 2858 'rankdir' => 'LR',
fdc74cf0 2859 // 'ranksep' => '0',
2860 'nodesep' => '0',
2861 // 'overlay' => false,
2862 );
2863
2864 unset($_GET['module']);
2865
2866 $_GET['all'] = 1;
2867
2dbca6fb 2868 $graphattr['URL'] = $this->_makeHrefProcess($_GET);
fdc74cf0 2869
2870 unset($_GET['all']);
2871
3badc888 2872 switch($hl)
2873 {
2874 case 'o':
2875 case 'p':
2876 $this->alpha = '30';
2877 break;
2878 }
2879
83b49cf7 2880 //$this->gv = new Image_GraphViz(true, $graphattr, "map".$object_id);
2881 $this->gv = new lm_Image_GraphViz(true, $graphattr, "map".$object_id);
fdc74cf0 2882
3badc888 2883 /* --------------------------- */
fdc74cf0 2884 if($object_id === NULL)
2885 {
4eeb9282 2886 /* all objects ! */
fdc74cf0 2887 unset($_GET['all']);
2888 $_GET['hl'] = 'o';
2889
2890 $this->gv->addAttributes(array(
2891 'label' => 'Showing all objects'.$hllabel,
2892 'labelloc' => 't',
2893 )
2894 );
2895
2896 $objects = listCells('object');
2897
2898 foreach($objects as $obj)
3badc888 2899 //$this->addlinkchainsobject($obj['id']); // longer runtimes !!
2900 $this->_add($this->gv, $obj['id'], NULL); // for all still faster and nicer looking graph
4eeb9282 2901
2902 return;
fdc74cf0 2903 }
2904 else
2905 {
2906 $object = spotEntity ('object', $object_id);
2907
2908 $this->gv->addAttributes(array(
2909 'label' => "Graph for ${object['dname']}$hllabel",
2910 'labelloc' => 't',
2911 )
2912 );
2913
3badc888 2914 $this->addlinkchainsobject($object_id);
2915 //$this->_add($this->gv, $object_id, $port_id);
fdc74cf0 2916
2917 $children = getEntityRelatives ('children', 'object', $object_id); //'entity_id'
2918
2919 foreach($children as $child)
3badc888 2920 $this->addlinkchainsobject($child['entity_id']);
2921 // $this->_add($this->gv, $child['entity_id'], NULL);
fdc74cf0 2922 }
2923
4eeb9282 2924 /* add hl label */
2925 $this->gv->addAttributes(array(
2926 'label' => $this->gv->graph['attributes']['label'].$hllabel,
2927 ));
2928
fdc74cf0 2929 // portlist::var_dump_html($this->gv);
3badc888 2930// portlist::var_dump_html($this->data);
2931
2932// echo json_encode($this->data);
fdc74cf0 2933
83b49cf7 2934 // $this->gv->saveParsedGraph('/tmp/graph.txt');
fdc74cf0 2935 // error_reporting( E_ALL ^ E_NOTICE);
2936 } /* __construct */
2937
2938 function __destruct() {
2939 error_reporting($this->errorlevel);
2940 }
2941
3badc888 2942 function _addCluster($object_id, $hl = false, $adddummy = false)
2943 {
2944 global $lc_cache;
2945
2946 $cluster_id = "c".$object_id;
2947
2948 if(
2949 !isset($this->gv->graph['clusters'][$cluster_id]) &&
2950 !isset($this->gv->graph['subgraphs'][$cluster_id])
2951 || $hl
2952 ) {
2953 $rack = null;
2954 $object = $lc_cache->getobject($object_id, $rack);
2955
2956 // $object['attr'] = getAttrValues($object_id);
2957
2958 $clusterattr = array();
2959
2960 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'color');
2961 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'fontcolor');
2962
2963 if($this->object_id == $object_id)
2964 {
2965 $clusterattr['rank'] = 'source';
2966
2967 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'color');
2968 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'fontcolor');
2969 }
2970
2971 $clustertitle = htmlspecialchars($object['dname']);
2972 $clusterattr['tooltip'] = $clustertitle;
2973
2974 unset($_GET['module']); // makeHrefProcess adds this
2975 unset($_GET['port_id']);
2976 unset($_GET['remote_id']);
2977 $_GET['object_id'] = $object_id;
2978 //$_GET['hl'] = 'o';
2979
2980 $clusterattr['URL'] = $this->_makeHrefProcess($_GET);
2981
2982 //has_problems
2983 if($object['has_problems'] != 'no')
2984 {
2985 $clusterattr['style'] = 'filled';
2986 $this->_getcolor('cluster', 'problem', $this->alpha, $clusterattr, 'fillcolor');
2987 }
2988
2989 if(!empty($object['container_name']))
2990 $clustertitle .= "<BR/>(${object['container_name']})";
2991
2992 if(!empty($rack['row_name']) || !empty($rack['name']))
2993 $clustertitle .= "<BR/><FONT point-size=\"10\">{$rack['row_name']} / {$rack['name']}</FONT>";
2994
2995 $embedin = $object['container_id'];
2996 if(empty($embedin))
2997 $embedin = 'default';
2998 else
2999 {
3000 $embedin = "c$embedin"; /* see cluster_id */
3001
3002 // TODO
3003 /* add container / cluster if not already exists */
3004 $this->addlinkchainsobject($object['container_id']);
3005 }
3006
3007 $clusterattr['id'] = "$object_id----"; /* used for js context menu */
3008
3009 $this->gv->addCluster($cluster_id, $clustertitle, $clusterattr, $embedin);
3010
3011 /* needed because of gv_image empty cluster bug (invalid foreach argument) */
3012 if($adddummy)
3013 $this->gv->addNode("dummy$cluster_id", array(
3014 // 'label' =>'No Ports found/connected',
3015 'label' =>'',
3016 'fontsize' => 0,
3017 'size' => 0,
3018 'width' => 0,
3019 'height' => 0,
3020 'shape' => 'point',
3021 'style' => 'invis',
3022 ), $cluster_id);
3023
3024
3025 }
3026 } // _addCluster
3027
3028 function _addEdge($port, $linkchain, $loopedge = false)
3029 {
3030 global $lm_multilink_port_types;
3031 if(
3032 !isset($this->gv->graph['edgesFrom'][$port['id']][$port['remote_id']]) &&
3033 !isset($this->gv->graph['edgesFrom'][$port['remote_id']][$port['id']])
3034 || $loopedge
3035 ) {
3036 $remote_id = $port['remote_id'];
3037
3038 $linktype = $port['linktype'];
3039
3040 $edgetooltip = $port['object_name'].':'.$port['name'].
3041 ' - '.$port['cableid'].' -> '.
3042 $port['remote_name'].':'.$port['remote_object_name'];
3043
3044 $edgeattr = array(
3045 'fontsize' => 8,
3046 'label' => htmlspecialchars($port['cableid']),
3047 'tooltip' => $edgetooltip,
3048 'sametail' => $linktype,
3049 'samehead' => $linktype,
3050 'arrowhead' => 'none',
3051 'arrowtail' => 'none',
3052 );
3053
3054 $this->_getcolor('edge', ($linkchain->loop ? 'loop' : 'default'), $this->alpha, $edgeattr, 'color');
3055 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'fontcolor');
3056
3057 if($linktype == 'back' )
3058 {
3059 $edgeattr['style'] = 'dashed';
3060
3061 /* multilink ports */
3062 if(in_array($port['oif_id'], $lm_multilink_port_types))
3063 {
3064 $edgeattr['dir'] = 'both';
3065 $edgeattr['arrowtail'] = 'dot';
3066 }
3067
3068 if(in_array($linkchain->ports[$remote_id]['oif_id'], $lm_multilink_port_types))
3069 {
3070 $edgeattr['dir'] = 'both';
3071 $edgeattr['arrowhead'] = 'dot';
3072 }
3073 }
3074
3075 if(
3076 ($port['id'] == $this->port_id && $port['remote_id'] == $this->remote_id) ||
3077 ($port['id'] == $this->remote_id && $port['remote_id'] == $this->port_id)
3078 )
3079 {
3080 $this->_getcolor('edge', 'highlight', 'ff', $edgeattr, 'color');
3081 $edgeattr['penwidth'] = 2; /* bold */
3082 }
3083
3084 unset($_GET['module']);
3085 $_GET['object_id'] = $port['object_id'];
3086 $_GET['port_id'] = $port['id'];
3087 $_GET['remote_id'] = $port['remote_id'];
3088
3089 $edgeattr['URL'] = $this->_makeHrefProcess($_GET);
3090
3091 $edgeattr['id'] = $port['object_id']."-".$port['id']."-".$port['remote_id']."-".$linktype; /* for js context menu */
3092
3093 if($loopedge)
3094 {
3095 $edgeattr = array_merge($edgeattr, array(
3096 'sametail' => 'loop',
3097 'samehead' => 'loop',
3098 'dir' => 'both',
3099 'arrowhead' => 'invodot',
3100 'arrowtail' => 'invodot',
3101 ));
3102 }
3103
3104 $this->gv->addEdge(array($port['id'] => $port['remote_id']),
3105 $edgeattr,
3106 array(
3107 $port['id'] => $linktype,
3108 $port['remote_id'] => $linktype,
3109 )
3110 );
3111
3112 }
3113 } // _addEdge
3114
3115 function addlinkchain($linkchain, $index)
3116 {
3117 global $lm_multilink_port_types;
3118
3119 $hl = false;
3120 $alpha = $this->alpha;
3121 if(
3122 ($this->hl == 'p' && $linkchain->hasport_id($this->port_id))
3123 || ($this->hl == 'o' && $linkchain->hasobject_id($this->object_id))
3124 )
3125 {
3126 $hl = true;
3127 $this->alpha = 'ff';
3128 }
3129
3130 $remote_id = null;
3131 foreach($linkchain as $id => $port)
3132 {
3133 $this->_addCluster($port['object_id'], $hl);
3134
3135 $nodelabel = htmlspecialchars("${port['name']}");
3136 $text = $nodelabel;
3137
3138 if($port['iif_id'] != '1' )
3139 {
3140 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['iif_name']}</FONT>";
3141 $text .= "\n".$port['iif_name'];
3142 }
3143
3144 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['oif_name']}</FONT>";
3145 $text .= "\n".$port['oif_name'];
3146
3147 // add ip address
3148 if(isset($port['portip']))
3149 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">".$port['portip']."</FONT>";
3150
3151 $nodeattr = array(
3152 'label' => $nodelabel,
3153 );
3154
3155 $this->_getcolor('port', ($linkchain->loop ? 'loop' : 'default'),$this->alpha, $nodeattr, 'fontcolor');
3156 $this->_getcolor('oif_id', $port['oif_id'],$this->alpha, $nodeattr, 'color');
3157
3158 if($this->port_id == $port['id']) {
3159 $nodeattr['style'] = 'filled';
3160 $nodeattr['fillcolor'] = $this->_getcolor('port', 'current', $this->alpha);
3161 }
3162
3163 if($this->remote_id == $port['id']) {
3164 $nodeattr['style'] = 'filled';
3165 $nodeattr['fillcolor'] = $this->_getcolor('port', 'remote', $this->alpha);
3166 }
3167
3168 $nodeattr['tooltip'] = htmlspecialchars("${port['name']}");
3169
3170 unset($_GET['module']);
3171 unset($_GET['remote_id']);
3172 $_GET['object_id'] = $port['object_id'];
3173 $_GET['port_id'] = $port['id'];
3174 $_GET['hl'] = 'p';
3175
3176 $nodeattr['URL'] = $this->_makeHrefProcess($_GET);
3177 $nodeattr['id'] = "${port['object_id']}-${port['id']}--"; /* for js context menu */
3178
3179 $this->gv->addNode($port['id'],
3180 $nodeattr,
3181 "c${port['object_id']}"); /* see cluster_id */
3182
3183 $remote_id = $port['remote_id'];
3184
3185 if($port['portcount'] > 1)
3186 foreach($port['chains'] as $mlc)
3187 {
3188 $this->addlinkchain($mlc, 0); // TODO index
3189 }
3190
3191 $prevlinktype = ($port['linktype'] == 'front' ? 'back' : 'front');
3192 if($port[$prevlinktype]['portcount'] > 1)
3193 {
3194 foreach($port[$prevlinktype]['chains'] as $mlc)
3195 {
3196 $this->addlinkchain($mlc, 0, false); // TODO index
3197 }
3198 }
3199
3200 if($remote_id)
3201 $this->_addEdge($port, $linkchain);
3202
3203 } //foreach
3204
3205 if($linkchain->loop && $remote_id)
3206 {
3207 // TODO separate loop link
3208 // add loop edge
3209 $this->_addEdge($port, $linkchain, true);
3210 }
3211
3212 // reset alpha to start value
3213 $this->alpha = $alpha;
3214 }
3215
de3d42dc 3216 function setFalseOnError($newvalue)
3217 {
3218 $this->gv->_returnFalseOnError = $newvalue;
3219 }
3220
2dbca6fb 3221 function _makeHrefProcess($array)
3222 {
3223 return str_replace('&','&amp;',makeHrefProcess($array));
3224 }
3225
fdc74cf0 3226 // !!!recursiv !!!
3227 function _add($gv, $object_id, $port_id = NULL) {
3228 global $lm_multilink_port_types;
3229
4eeb9282 3230 if($port_id !== NULL) {
3231 if(isset($this->ports[$port_id])) {
fdc74cf0 3232 return;
4eeb9282 3233 }
fdc74cf0 3234 }
3235
2d257b3a 3236 if($this->back != 'front' || $port_id === NULL || $this->allports)
3237 $front = $this->_getObjectPortsAndLinks($object_id, 'front', $port_id, $this->allports);
3238 else
3239 $front = array();
3240
3241 if($this->back != 'back' || $port_id === NULL || $this->allports)
3242 $backend = $this->_getObjectPortsAndLinks($object_id, 'back', $port_id, $this->allports);
3243 else
3244 $backend = array();
3245
3246 $ports = array_merge($front,$backend);
3247
630a86c8 3248 /* used only for Graphviz ...
3249 * !! numeric ids cause Image_Graphviz problems on nested clusters !!
3250 */
3251 $cluster_id = "c$object_id";
3252
2d257b3a 3253 if(empty($ports))
3254 {
3255 /* needed because of gv_image empty cluster bug (invalid foreach argument) */
da6d05d0 3256 $gv->addNode("dummy$cluster_id", array(
2d257b3a 3257 // 'label' =>'No Ports found/connected',
3258 'label' =>'',
3259 'fontsize' => 0,
3260 'size' => 0,
3261 'width' => 0,
3262 'height' => 0,
3263 'shape' => 'point',
3264 'style' => 'invis',
3265 ), $cluster_id);
3266
3267 /* show objects without ports */
3268 if($object_id === NULL)
3269 return;
3270 }
3271
3badc888 3272 $object = NULL;
2d257b3a 3273 if($object_id !== NULL) {
4eeb9282 3274 if(
3275 !isset($gv->graph['clusters'][$cluster_id]) &&
3276 !isset($gv->graph['subgraphs'][$cluster_id])
3277 ) {
fdc74cf0 3278
4eeb9282 3279 $object = spotEntity ('object', $object_id);
3badc888 3280
3281 // ip addresses
3282 amplifyCell($object);
3283 $object['portip'] = array();
3284 foreach($object['ipv4'] as $ipv4)
3285 {
3286 $object['portip'][$ipv4['osif']] = $ipv4['addrinfo']['ip'];
3287 }
3288
4eeb9282 3289 // $object['attr'] = getAttrValues($object_id);
fdc74cf0 3290
4eeb9282 3291 $clusterattr = array();
fdc74cf0 3292
4eeb9282 3293 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'color');
3294 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'fontcolor');
fdc74cf0 3295
4eeb9282 3296 if($this->object_id == $object_id)
3297 {
3298 $clusterattr['rank'] = 'source';
fdc74cf0 3299
4eeb9282 3300 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'color');
3301 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'fontcolor');
3302 }
fdc74cf0 3303
4eeb9282 3304 $clustertitle = "${object['dname']}";
3badc888 3305 $text = "${object['dname']}";
4eeb9282 3306 $clusterattr['tooltip'] = $clustertitle;
fdc74cf0 3307
4eeb9282 3308 unset($_GET['module']); // makeHrefProcess adds this
3309 unset($_GET['port_id']);
3310 unset($_GET['remote_id']);
3311 $_GET['object_id'] = $object_id;
3312 //$_GET['hl'] = 'o';
fdc74cf0 3313
2dbca6fb 3314 $clusterattr['URL'] = $this->_makeHrefProcess($_GET);
fdc74cf0 3315
4eeb9282 3316 //has_problems
3317 if($object['has_problems'] != 'no')
3318 {
3319 $clusterattr['style'] = 'filled';
3320 $this->_getcolor('cluster', 'problem', $this->alpha, $clusterattr, 'fillcolor');
3321 }
fdc74cf0 3322
4eeb9282 3323 if(!empty($object['container_name']))
3badc888 3324 {
4eeb9282 3325 $clustertitle .= "<BR/>${object['container_name']}";
3badc888 3326 $text .= "\n${object['container_name']}";
3327 }
fdc74cf0 3328
4eeb9282 3329 if($object['rack_id'])
3330 {
3331 $rack = spotEntity('rack', $object['rack_id']);
fdc74cf0 3332
4eeb9282 3333 if(!empty($rack['row_name']) || !empty($rack['name']))
3badc888 3334 {
4eeb9282 3335 $clustertitle .= "<BR/>${rack['row_name']} / ${rack['name']}";
3badc888 3336 $text .= "\n${rack['row_name']} / ${rack['name']}";
3337 }
4eeb9282 3338 }
3339
3340 $embedin = $object['container_id'];
3341 if(empty($embedin))
3342 $embedin = 'default';
3343 else
3344 {
3345 $embedin = "c$embedin"; /* see cluster_id */
3346
3347 /* add container / cluster if not already exists */
3348 $this->_add($gv, $object['container_id'], NULL);
3349 }
fdc74cf0 3350
2d257b3a 3351 $clusterattr['id'] = "$object_id----"; /* used for js context menu */
fdc74cf0 3352
2d257b3a 3353 $gv->addCluster($cluster_id, $clustertitle, $clusterattr, $embedin);
fdc74cf0 3354
2d257b3a 3355 } /* isset cluster_id */
3356 } /* object_id !== NULL */
fca4e38a 3357
fdc74cf0 3358 foreach($ports as $key => $port) {
3359
3360 $this->back = $port['linktype'];
3361
4eeb9282 3362 if(!isset($this->ports[$port['id']])) {
fdc74cf0 3363
fdc74cf0 3364
c613fc73 3365 $nodelabel = htmlspecialchars("${port['name']}");
3badc888 3366 $text = $nodelabel;
fdc74cf0 3367
4eeb9282 3368 if($port['iif_id'] != '1' )
3badc888 3369 {
4eeb9282 3370 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['iif_name']}</FONT>";
3badc888 3371 $text .= "\n".$port['iif_name'];
3372 }
fdc74cf0 3373
4eeb9282 3374 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['oif_name']}</FONT>";
3badc888 3375 $text .= "\n".$port['oif_name'];
3376
3377 // add ip address
3378 if($object)
3379 if(isset($object['portip'][$port['name']]))
3380 {
3381 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">".$object['portip'][$port['name']]."</FONT>";
3382 $text .= "\n".$object['portip'][$port['name']];
3383 }
fdc74cf0 3384
4eeb9282 3385 $nodeattr = array(
3386 'label' => $nodelabel,
3387 );
fdc74cf0 3388
4eeb9282 3389 $this->_getcolor('port', 'default',$this->alpha, $nodeattr, 'fontcolor');
3390 $this->_getcolor('oif_id', $port['oif_id'],$this->alpha, $nodeattr, 'color');
fdc74cf0 3391
4eeb9282 3392 if($this->port_id == $port['id']) {
3393 $nodeattr['style'] = 'filled';
3394 $nodeattr['fillcolor'] = $this->_getcolor('port', 'current', $this->alpha);
3395 }
fdc74cf0 3396
4eeb9282 3397 if($this->remote_id == $port['id']) {
3398 $nodeattr['style'] = 'filled';
3399 $nodeattr['fillcolor'] = $this->_getcolor('port', 'remote', $this->alpha);
3400 }
3401
c613fc73 3402 $nodeattr['tooltip'] = htmlspecialchars("${port['name']}");
4eeb9282 3403
3404 unset($_GET['module']);
3405 unset($_GET['remote_id']);
3406 $_GET['object_id'] = $port['object_id'];
3407 $_GET['port_id'] = $port['id'];
3408 $_GET['hl'] = 'p';
fdc74cf0 3409
2dbca6fb 3410 $nodeattr['URL'] = $this->_makeHrefProcess($_GET);
4eeb9282 3411 $nodeattr['id'] = "${port['object_id']}-${port['id']}--"; /* for js context menu */
fdc74cf0 3412
4eeb9282 3413 $gv->addNode($port['id'],
fdc74cf0 3414 $nodeattr,
3415 "c${port['object_id']}"); /* see cluster_id */
3416
4eeb9282 3417 $this->ports[$port['id']] = true;
2d257b3a 3418
4eeb9282 3419 } /* isset port */
fdc74cf0 3420
3421 if(!empty($port['remote_id'])) {
3422
bb000ce6 3423 if($this->object_id !== NULL)
3424 $this->_add($gv, $port['remote_object_id'], $port['remote_id']);
fdc74cf0 3425
3426 if(
3427 !isset($gv->graph['edgesFrom'][$port['id']][$port['remote_id']]) &&
3428 !isset($gv->graph['edgesFrom'][$port['remote_id']][$port['id']])
3429 ) {
3430
3431 $linktype = $port['linktype'];
3432
3433 $edgetooltip = $port['object_name'].':'.$port['name'].
3434 ' - '.$port['cableid'].' -> '.
3435 $port['remote_name'].':'.$port['remote_object_name'];
3436
3437 $edgeattr = array(
3438 'fontsize' => 8,
c613fc73 3439 'label' => htmlspecialchars($port['cableid']),
fdc74cf0 3440 'tooltip' => $edgetooltip,
3441 'sametail' => $linktype,
3442 'samehead' => $linktype,
3443 );
3444
3445 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'color');
3446 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'fontcolor');
3447
3448 if($linktype == 'back' )
3449 {
3450 $edgeattr['style'] = 'dashed';
3451 $edgeattr['arrowhead'] = 'none';
3452 $edgeattr['arrowtail'] = 'none';
3453
3454 /* multilink ports */
3455 if(in_array($port['oif_id'], $lm_multilink_port_types))
3456 {
3457 $edgeattr['dir'] = 'both';
3458 $edgeattr['arrowtail'] = 'dot';
3459 }
3460
3461 if(in_array($port['remote_oif_id'], $lm_multilink_port_types))
3462 {
3463 $edgeattr['dir'] = 'both';
3464 $edgeattr['arrowhead'] = 'dot';
3465 }
3466 }
3467
3468 if(
3469 ($port['id'] == $this->port_id && $port['remote_id'] == $this->remote_id) ||
3470 ($port['id'] == $this->remote_id && $port['remote_id'] == $this->port_id)
3471 )
3472 {
3473 $this->_getcolor('edge', 'highlight', 'ff', $edgeattr, 'color');
3474 $edgeattr['penwidth'] = 2; /* bold */
3475 }
3476
3477 unset($_GET['module']);
3478 $_GET['object_id'] = $port['object_id'];
3479 $_GET['port_id'] = $port['id'];
3480 $_GET['remote_id'] = $port['remote_id'];
3481
2dbca6fb 3482 $edgeattr['URL'] = $this->_makeHrefProcess($_GET);
fdc74cf0 3483
3484 $edgeattr['id'] = $port['object_id']."-".$port['id']."-".$port['remote_id']."-".$linktype; /* for js context menu */
3485
3486 $gv->addEdge(array($port['id'] => $port['remote_id']),
3487 $edgeattr,
3488 array(
3489 $port['id'] => $linktype,
3490 $port['remote_id'] => $linktype,
3491 )
3492 );
3badc888 3493
fdc74cf0 3494 }
3495 }
3496
3497 }
3498
3499 // portlist::var_dump_html($port);
3500 }
3501
83b49cf7 3502 function fetch($type = 'png', $command = NULL, $format2 = NULL, &$data2 = NULL) {
3503
3504 $tmpdata = $data2;
3505 $ret = $this->gv->fetch($type, $command, $format2, $tmpdata);
3506 if($data2 !== NULL)
3507 $data2 = $tmpdata;
fdc74cf0 3508 return $ret;
3509 }
3510
3511 function image($type = 'png', $command = NULL) {
3512 $this->gv->image($type, $command);
3513 }
3514
3515 function parse() {
3516 return $this->gv->parse();
3517 }
3518
3519 /* should be compatible with getObjectPortsAndLinks from RT database.php */
3520 function _getObjectPortsAndLinks($object_id, $linktype = 'front', $port_id = NULL, $allports = false) {
3521
3522 if($linktype == 'front')
3523 $linktable = 'Link';
3524 else
3525 $linktable = 'LinkBackend';
3526
3527 $qparams = array();
3528
3529 $query = "SELECT
3530 '$linktype' as linktype,
3531 Port.*,
3532 Port.type AS oif_id,
3533 PortInnerInterface.iif_name as iif_name,
262da468 3534 POI.oif_name as oif_name,
fdc74cf0 3535 Object.id as object_id, Object.name as object_name,
2a5cd259 3536 IFNULL(LinkTable_a.cable,LinkTable_b.cable) as cableid,
fdc74cf0 3537 remoteObject.id as remote_object_id, remoteObject.name as remote_object_name,
3538 remotePort.id as remote_id, remotePort.name as remote_name,
3539 remotePort.type AS remote_oif_id,
3540 remotePortInnerInterface.iif_name as remote_iif_name,
262da468 3541 remotePOI.oif_name as remote_oif_name
fdc74cf0 3542 FROM Port";
3543
3544 // JOIN
3545 $join = " LEFT JOIN PortInnerInterface on PortInnerInterface.id = Port.iif_id
262da468 3546 LEFT JOIN PortOuterInterface AS POI on POI.id = Port.type
2be16f85 3547 LEFT JOIN $linktable as LinkTable_a on Port.id = LinkTable_a.porta
2a5cd259 3548 LEFT JOIN $linktable as LinkTable_b on Port.id = LinkTable_b.portb
fdc74cf0 3549 LEFT JOIN Object on Object.id = Port.object_id
2a5cd259 3550 LEFT JOIN Port as remotePort on remotePort.id = IFNULL(LinkTable_a.portb, LinkTable_b.porta)
fdc74cf0 3551 LEFT JOIN Object as remoteObject on remoteObject.id = remotePort.object_id
3552 LEFT JOIN PortInnerInterface as remotePortInnerInterface on remotePortInnerInterface.id = remotePort.iif_id
262da468 3553 LEFT JOIN PortOuterInterface as remotePOI on remotePOI.id = remotePort.type
fdc74cf0 3554 ";
3555
3556 // WHERE
3557 if($port_id === NULL)
3558 {
3559 $where = " WHERE Object.id = ?";
3560 $qparams[] = $object_id;
3561
09749fb9 3562 if(!$allports) {
fdc74cf0 3563 $where .= " AND remotePort.id is not NULL";
09749fb9 3564
3565 if($linktype != 'front') {
2a5cd259 3566 $join .= "
3567 LEFT JOIN Link as FrontLink_a on Port.id = FrontLink_a.porta
3568 LEFT JOIN Link as FrontLink_b on Port.id = FrontLink_b.portb
3569 LEFT JOIN Link as FrontRemoteLink_a on remotePort.id = FrontRemoteLink_a.porta
3570 LEFT JOIN Link as FrontRemoteLink_b on remotePort.id = FrontRemoteLink_b.portb
09749fb9 3571 ";
2a5cd259 3572 $where .= " AND ( (FrontLink_a.porta is not NULL or FrontLink_b.portb is not NULL )
3573 OR (FrontRemoteLink_a.porta is not NULL or FrontRemoteLink_b.portb is not NULL) )";
09749fb9 3574 }
3575 }
fdc74cf0 3576 }
3577 else
3578 {
3579 // $where = " WHERE Port.id = ? and remotePort.id is not NULL";
3580 $where = " WHERE Port.id = ?";
3581 $qparams[] = $port_id;
3582 }
3583
3584 // ORDER
3585 $order = " ORDER by oif_name, Port.Name";
3586
3587 $query .= $join.$where.$order;
3588
2a5cd259 3589 //echo "$port_id: $query<br><br>";
3590
fdc74cf0 3591 $result = usePreparedSelectBlade ($query, $qparams);
3592
3593 $row = $result->fetchAll(PDO::FETCH_ASSOC);
3594
3595 $result->closeCursor();
3596
3597 return $row;
3598 }
3599
3600 function _getcolor($type = 'object', $key = 'default', $alpha = 'ff', &$array = NULL , $arraykey = 'color') {
3601
3602 $object = array(
3603 'current' => '#ff0000',
3604 );
3605 $port = array(
3606 'current' => '#ffff90',
3607 'remote' => '#ffffD0',
3badc888 3608 'loop' => '#ff6666',
fdc74cf0 3609 );
3610
3611 $cluster = array(
3612 'current' => '#ff0000',
3613 'problem' => '#ff3030',
3614 );
3615
3616 $edge = array (
3617 'highlight' => '#ff0000',
3badc888 3618 'loop' => '#ff6666',
fdc74cf0 3619 );
3620
3621 $oif_id = array(
3622 '16' => '#800000', /* AC-in */
3623 '1322' => '#ff4500', /* AC-out */
3624 '24' => '#000080', /* 1000base-t */
3625 );
3626
3627 $defaultcolor = '#000000'; /* black */
3628 $default = true;
3629
3630 if(isset(${$type}[$key]))
3631 {
3632 $default = false;
3633 $color = ${$type}[$key];
3634 }
3635 else
3636 $color = $defaultcolor;
3637
3638
3639 if($alpha != 'ff' || $default == false)
3640 {
3641 $color .= $alpha;
3642
3643 if($array !== NULL)
3644 $array[$arraykey] = $color;
3645 else
3646 return $color;
3647 }
3648 else
3649 return $defaultcolor;
3650
3651 } /* _getcolor */
3652
3653 function dump() {
3654 var_dump($this->gv);
3655 }
3656
3657 function export() {
3658 var_export($this->gv);
3659 }
3660
3661} /* class gvmap */
3662
3663/* -------------------------------------------------- */
3664
9276dc97 3665function linkmgmt_opupdate() {
fdc74cf0 3666
9276dc97
ME
3667 if(!isset($_POST['id']))
3668 exit;
fdc74cf0 3669
9276dc97
ME
3670 $ids = explode('_',$_POST['id'],3);
3671 $retval = strip_tags($_POST['value']);
3672
3673 if(isset($ids[1])) {
3674 if(permitted(NULL, NULL, 'set_link'))
3675 if(isset($ids[2]) && $ids[2] == 'back')
fdc74cf0 3676 linkmgmt_commitUpdatePortLink($ids[0], $ids[1], $retval, TRUE);
9276dc97 3677 else
fdc74cf0 3678 linkmgmt_commitUpdatePortLink($ids[0], $ids[1], $retval);