use RT inplace-edit for port reservation comments and cable ids remove jquery.jeditab...
[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)
16ec374c 16 * - change/create CableID
17 * - change/create Port Reservation Comment
fdc74cf0 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
16ec374c 89 * 2. copy linkmgmt.php to plugins directory
fdc74cf0 90 *
91 * Ready to go!
92 *
93 *
94 * UPDATE TABLE:
95 *
96 * Update from non-multilink table
97 * ALTER TABLE
98
99ALTER TABLE LinkBackend ADD KEY `LinkBackend_FK_b` (`portb`);
100ALTER TABLE LinkBackend DROP INDEX porta;
101ALTER TABLE LinkBackend DROP INDEX portb;
102
b695efd7 103 * UPDATE to RT 0.20.7
104
105ALTER TABLE LinkBackend CONVERT to CHARACTER SET utf8 COLLATE utf8_unicode_ci;
106
fdc74cf0 107 *
9276dc97
ME
108 *
109 * TESTED on FreeBSD 9.0, nginx/1.0.11, php 5.3.9
fdc74cf0 110 * GraphViz_Image 1.3.0
9276dc97 111 *
16ec374c 112 * (c)2012-2016 Maik Ehinger <m.ehinger@ltur.de>
9276dc97
ME
113 */
114
fdc74cf0 115/**
116 * The newest version of this plugin can be found at:
9276dc97 117 *
3badc888 118 * https://github.com/github138/myRT-contribs/tree/develop-0.20.x
9276dc97
ME
119 *
120 */
121
122/*************************
123 * TODO
124 *
125 * - code cleanups
126 * - bug fixing
127 *
fdc74cf0 128 * - put selected object/port top left of graph
129 * - multlink count for Graphviz maps empty or full dot
130 *
131 * - csv list
9276dc97
ME
132 *
133 */
134
fdc74cf0 135/* DEBUG */
136//error_reporting(E_ALL);
9276dc97
ME
137
138$tab['object']['linkmgmt'] = 'Link Management';
139$tabhandler['object']['linkmgmt'] = 'linkmgmt_tabhandler';
140//$trigger['object']['linkmgmt'] = 'linkmgmt_tabtrigger';
141
142$ophandler['object']['linkmgmt']['update'] = 'linkmgmt_opupdate';
143$ophandler['object']['linkmgmt']['unlinkPort'] = 'linkmgmt_opunlinkPort';
144$ophandler['object']['linkmgmt']['PortLinkDialog'] = 'linkmgmt_opPortLinkDialog';
145$ophandler['object']['linkmgmt']['Help'] = 'linkmgmt_opHelp';
146
fdc74cf0 147$ophandler['object']['linkmgmt']['map'] = 'linkmgmt_opmap';
148$ajaxhandler['lm_mapinfo'] = 'linkmgmt_ajax_mapinfo';
16ec374c 149$ajaxhandler['lm-upd-reservation-cable'] = 'linkmgmt_updateCableIdAJAX';
fdc74cf0 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
3badc888 967 if($linktype == 'back')
3badc888 968 $arrow = '====>';
3badc888 969 else
970 $arrow = '---->';
971
972 $port_id = $port['id'];
973
974 /* link */
975 return '<td align=center>'
16ec374c 976 .'<pre><span class="editable id1-'.$port_id.' id2-'.$port['remote_id'].' op-lm-upd-reservation-cable linktype-'.$linktype.'">'.$port['cableid']
977 ."</span></pre><pre>$arrow</pre>"
3badc888 978 .$this->_printUnLinkPort($port, $linktype)
979 .'</td>';
980 } /* printlink */
981
982 /*
983 * return link symbol
984 */
985 function _getlinkportsymbol($port_id, $linktype) {
986 $retval = '<span onclick=window.open("'.makeHrefProcess(array_merge($_GET,
987 array('op' => 'PortLinkDialog','port' => $port_id,'linktype' => $linktype ))).'","name","height=800,width=800");'
988 .'>';
989
990 $img = getImageHREF ('plug', $linktype.' Link this port');
991
992 if($linktype == 'back')
993 $img = str_replace('<img',
994 '<img style="transform:rotate(180deg);-o-transform:rotate(180deg);-ms-transform:rotate(180deg);-moz-transform:rotate(180deg);-webkit-transform:rotate(180deg);"',
995 $img);
996
997 $retval .= $img;
998 $retval .= "</span>";
999 return $retval;
1000
1001 } /* _getlinkportsymbol */
1002
1003 /*
1004 * print link symbol
1005 *
1006 */
1007 function _printlinkportsymbol($port_id, $linktype = 'front') {
1008 global $lm_cache;
1009
1010 if($linktype == 'front' && !$lm_cache['allowlink'])
1011 return;
1012
1013 if($linktype != 'front' && !$lm_cache['allowbacklink'])
1014 return;
1015
1016 return "<td align=center>"
1017 .$this->_getlinkportsymbol($port_id, $linktype)
1018 ."</td>";
1019
1020 } /* _printlinkportsymbol */
1021
1022 /*
1023 */
1024 function printcomment($port) {
1025
1026 if(!empty($port['reservation_comment'])) {
1027 $prefix = '<b>Reserved: </b>';
1028 } else
1029 $prefix = '';
1030
16ec374c 1031 return '<td>'.$prefix.'</td><td><i><span class="editable op-upd-reservation-port id-'.$port['id'].'">'.$port['reservation_comment'].'</span</i></td>';
3badc888 1032
1033 } /* printComment */
1034
1035 /*
1036 * return link cut symbol
1037 *
1038 * TODO $opspec_list
1039 */
1040 function _printUnLinkPort($src_port, $linktype) {
1041 global $lm_cache;
1042
1043 if($linktype == 'front' && !$lm_cache['allowlink'])
1044 return;
1045
1046 if($linktype != 'front' && !$lm_cache['allowbacklink'])
1047 return;
1048
1049 $dst_port = $this->ports[$src_port['remote_id']];
1050
1051 return '<a href='.
1052 makeHrefProcess(array(
1053 'op'=>'unlinkPort',
1054 'port_id'=>$src_port['id'],
1055 'remote_id' => $dst_port['id'],
1056 'object_id'=> $this->object_id, //$this->ports[$this->init]['object_id'],
1057 'tab' => 'linkmgmt',
1058 'linktype' => $linktype)).
1059 ' onclick="return confirm(\'unlink ports '.$src_port['name']. ' -> '.$dst_port['name']
1060 .' ('.$linktype.') with cable ID: '.$src_port['cableid'].'?\');">'.
1061 getImageHREF ('cut', $linktype.' Unlink this port').'</a>';
1062
1063 } /* _printUnLinkPort */
1064
3d9d3e22 1065 function printports()
1066 {
1067
1068 $back = isset($this->ports[$this->first]['back']['remote_id']);
d894f236 1069 //$this->getporttds($this->first, $back);
1070 foreach($this as $port_id => $port)
1071 {
1072 $class = 'hidden';
1073 if($port_id == $this->first || $port_id == $this->last)
1074 $class = '';
1075
1076 $this->getporttds($port, $class);
1077 }
3d9d3e22 1078 }
1079
d894f236 1080 function getporttds($port, $class = '')
569219fa 1081 {
d894f236 1082 $back = ($port['linktype'] == 'front' ? false : true );
569219fa
CR
1083
1084 // see interface.php renderObjectPortRow()
1085 // highlight port name with yellow if it's name is not canonical
1086 $canon_pn = shortenPortName ($port['name'], $port['object_id']);
1087 $name_class = $canon_pn == $port['name'] ? '' : 'trwarning';
1088
1089 echo "<td class='tdleft $class'>" .
1090 formatPortLink ($port['object_id'], $port['object_name'], $port['id'], NULL) .
1091 "</td>";
1092
1093 $a_class = isEthernetPort ($port) ? 'port-menu' : '';
1094 echo "<td class='tdleft $class' NOWRAP><a name='port-${port['id']}' class='interactive-portname nolink $a_class'>${port['name']}</a></td>";
1095 echo "<td class='tdleft $class'>${port['label']}</td>";
1096 echo "<td class='tdleft $class'>" . formatPortIIFOIF ($port) . "</td><td class='tdleft $class'><tt>${port['l2address']}</tt></td>";
1097
1098 $remote_port = $port[( $back ? 'back' : 'front')];
1099 if ($remote_port['remote_id'])
1100 {
3d9d3e22 1101 if($port['id'] == $this->first)
1102 {
1103 echo "<td class='tdcenter nothidden'>";
1104
1105 if( $this->linkcount == 1 )
1106 if($back)
1107 echo "=${remote_port['cableid']}=>";
1108 else
1109 echo "-${remote_port['cableid']}->";
1110 else
1111 if( $this->linkcount > 1 )
1112 echo "*".$this->linkcount."*>";
1113 echo "</td>";
1114 }
1115
569219fa 1116 $editable = permitted ('object', 'ports', 'editPort') ? 'editable' : '';
d894f236 1117 echo "<td class='tdcenter hidden'>";
569219fa
CR
1118 if($back)
1119 echo "=${remote_port['cableid']}=>";
1120 else
1121 echo "-${remote_port['cableid']}->";
1122
d894f236 1123 echo "</td>";
569219fa
CR
1124 }
1125 else
d894f236 1126 {
1127 if(!empty($port['reservation_comment']))
1128 echo implode ('', formatPortReservation ($port));
1129 }
569219fa 1130
569219fa
CR
1131 } // getporttds
1132
3badc888 1133 // TODO
1134 // html table
1135 function getchainhtml()
1136 {
1137 $remote_id = $this->first;
1138
1139 // if not Link use LinkBackend
1140 $back = $this->ports[$remote_id]['front']['remote_id'];
1141
1142 $chain = "<table>";
1143
1144 for(;$remote_id;)
1145 {
1146 $back = !$back;
1147 $linktype = $port['linktype'];
1148
1149 if($back)
1150 {
1151 // $linktable = 'LinkBackend';
1152 $arrow = ' => ';
1153 }
1154 else
1155 {
1156 // $linktable = 'Link';
1157 $arrow = ' --> ';
1158 }
1159
1160 $port = $this->ports[$remote_id][$linktype];
1161
1162 if($this->init == $remote_id)
1163 $chain .= "<tr><td><b>".$port['object_name']."</b></td><td><b> [".$port['name']."]</b></td>";
1164 else
1165 $chain .= "<tr><td>".$port['object_name']."</td><td> [".$port['name']."]</td>";
1166
1167 if($remote_id == $this->first || $remote_id == $this->last)
1168 $chain .= "<td><div name=\"port${remote_id}-status\"></div></td>";
1169 else
1170 $chain .= "<td></td>";
1171
1172 $remote_id = $port['remote_id'];
1173
1174 if($remote_id)
1175 $chain .= "<td>$arrow</td></tr>";
1176 else
1177 $chain .= "<td></td></tr>";
1178
1179 if($this->loop && $remote_id == $this->first)
1180 {
1181 $chain .= "LOOP!<br>";
1182 break;
1183 }
1184
1185 }
1186
1187 $chain .= "</table>";
1188
1189 return $chain;
1190 }
1191
1192 function getport($id)
1193 {
1194 return $this->ports[$id];
1195 }
1196
1197 /* Iterator */
1198 function rewind() {
1199 $this->currentid = $this->first;
1200
1201 $this->back = isset($this->ports[$this->currentid]['back']['remote_id']);
1202
1203 $this->icount = 0;
1204 }
1205
1206 function current() {
1207 return $this->_getportlink($this->ports[$this->currentid]);
1208 }
1209
1210 function key() {
1211 return $this->currentid;
1212 }
1213
1214 function next() {
1215 $port = $this->current();
1216 $remote_id = $port['remote_id'];
1217
1218 if($this->loop && $remote_id == $this->first)
1219 $this->currentid = false;
1220 else
1221 $this->currentid = $remote_id;
1222
1223 $this->back = !$this->back;
1224
1225 $this->icount++;
1226 }
1227
1228 function valid() {
1229
1230 /* linkcout exceeded */
1231 if($this->icount > $this->linkcount+1)
1232 {
1233 $this->exceed = true;
1234 return false;
1235 }
1236
1237 return $this->currentid;
1238 }
1239
1240 /* for debugging only */
1241 function var_dump_html(&$var, $msg = "") {
1242 echo "<pre>------------------Start Var Dump -------------$msg------------\n";
1243 var_dump($var);
1244 echo "\n---------------------END Var Dump -----------$msg-------------</pre>";
1245 }
1246} // lm_linkchain
1247
1248/*
1249 * from RT database.php fetchPortList()
1250 * with Link table selection
1251 * and multilink changes
1252 */
1253function lm_fetchPortList ($sql_where_clause, $query_params = array(), $linktable = 'Link')
1254{
1255 $query = <<<END
1256SELECT
1257 Port.id,
1258 Port.name,
1259 Port.object_id,
1260 Object.name AS object_name,
1261 Port.l2address,
1262 Port.label,
1263 Port.reservation_comment,
1264 Port.iif_id,
1265 Port.type AS oif_id,
1266 (SELECT PortInnerInterface.iif_name FROM PortInnerInterface WHERE PortInnerInterface.id = Port.iif_id) AS iif_name,
1267 (SELECT PortOuterInterface.oif_name FROM PortOuterInterface WHERE PortOuterInterface.id = Port.type) AS oif_name,
1268
1269 lk.cable AS cableid,
1270 IF(lk.porta = Port.id, pb.id, pa.id) AS remote_id,
1271 IF(lk.porta = Port.id, pb.name, pa.name) AS remote_name,
1272 IF(lk.porta = Port.id, pb.object_id, pa.object_id) AS remote_object_id,
1273 IF(lk.porta = Port.id, ob.name, oa.name) AS remote_object_name,
1274
1275 (SELECT COUNT(*) FROM PortLog WHERE PortLog.port_id = Port.id) AS log_count,
1276 PortLog.user,
1277 UNIX_TIMESTAMP(PortLog.date) as time
1278FROM
1279 Port
1280 INNER JOIN Object ON Port.object_id = Object.id
1281
1282 LEFT JOIN $linktable AS lk ON lk.porta = Port.id or lk.portb = Port.id
1283 LEFT JOIN Port AS pa ON pa.id = lk.porta
1284 LEFT JOIN Object AS oa ON pa.object_id = oa.id
1285 LEFT JOIN Port AS pb ON pb.id = lk.portb
1286 LEFT JOIN Object AS ob ON pb.object_id = ob.id
1287
1288 LEFT JOIN PortLog ON PortLog.id = (SELECT id FROM PortLog WHERE PortLog.port_id = Port.id ORDER BY date DESC LIMIT 1)
1289WHERE
1290 $sql_where_clause
1291END;
1292
1293 $result = usePreparedSelectBlade ($query, $query_params);
1294
1295 $ret = array();
1296 while ($row = $result->fetch (PDO::FETCH_ASSOC))
1297 {
1298 $row['l2address'] = l2addressFromDatabase ($row['l2address']);
1299 $row['linked'] = isset ($row['remote_id']) ? 1 : 0;
1300
1301 // last changed log
1302 $row['last_log'] = array();
1303 if ($row['log_count'])
1304 {
1305 $row['last_log']['user'] = $row['user'];
1306 $row['last_log']['time'] = $row['time'];
1307 }
1308 unset ($row['user']);
1309 unset ($row['time']);
1310
1311 $ret[] = $row;
1312 }
1313 return $ret;
1314} /* lm_fetchPortList */
1315
1316function lm_getPortInfo ($port_id, $back = false)
1317{
1318 $linktable = ($back ? 'LinkBackend' : 'Link');
1319 if($back)
1320 $result = lm_fetchPortList ('Port.id = ?', array ($port_id), $linktable);
1321 else
1322 $result = fetchPortList ('Port.id = ?', array ($port_id));
1323
1324 //return empty ($result) ? NULL : $result[0];
1325 return $result;
1326} /* lm_getPortInfo */
1327
1328/* -------------------------------------------------------------------------- */
1329/* -------------------------------------------------- */
1330
1331function linkmgmt_opHelp() {
1332?>
1333 <table cellspacing=10><tr><th>Help</th><tr>
1334 <tr><td width=150></td><td width=150 style="font-weight:bold;color:<?php echo portlist::CURRENT_OBJECT_BGCOLOR; ?>">Current Object</td></tr>
1335 <tr><td></td><td bgcolor=<?php echo portlist::CURRENT_PORT_BGCOLOR; ?>>[current port]</td></tr>
1336 <tr><td>front link</td><td>[port]<(Object)</td><td>back link</td></tr>
1337 <tr><td>back link</td><td>(Object)>[port]</td><td>front link</td></tr>
1338 <tr><td></td><td><pre>----></pre></td><td>Front link</td></tr>
1339 <tr><td></td><td><pre>====></pre></td><td>Backend link</td></tr>
1340 <tr><td></td><td>Link Symbol</td><td>Create new link</td></tr>
1341 <tr><td></td><td>Cut Symbol</td><td>Delete link</td></tr>
1342
1343 </table>
1344
1345<?php
1346 exit;
1347} /* opHelp */
1348
1349/* -------------------------------------------------- */
1350
1351function linkmgmt_ajax_mapinfo() {
1352
1353 $object_id = NULL;
1354 $port_id = NULL;
1355 $remote_id = NULL;
1356 $linktype = NULL;
1357
1358 if(isset($_REQUEST['object_id']))
1359 $object_id = $_REQUEST['object_id'];
1360
1361 if(isset($_REQUEST['port_id']))
1362 $port_id = $_REQUEST['port_id'];
1363
1364 if(isset($_REQUEST['remote_id']))
1365 $remote_id = $_REQUEST['remote_id'];
1366
1367 if(isset($_REQUEST['linktype']))
1368 $linktype = $_REQUEST['linktype'];
1369
1370 $debug = NULL;
1371 if(isset($_REQUEST['debug']))
1372 $debug['value'] = $_REQUEST['debug'];
1373
1374 $info = array();
1375
1376 echo "<table style=\"font-size:12;\"><tr>";
1377
1378 if($port_id != NULL)
1379 {
1380 $port = new linkmgmt_RTport($port_id);
1381
1382 echo "<td>";
1383 $port->printtable('both');
1384 echo "</td>";
1385
1386 if($debug)
1387 $debug['port'] = &$port;
1388
1389 if($remote_id != NULL)
1390 {
1391
1392 $remote_port = new linkmgmt_RTport($remote_id);
1393
1394 echo "<td><table align=\"center\">";
1395
1396 // TODO cableid
1397 echo "<tr><td><pre>".($linktype == 'back' ? ' ===> ' : ' ---> ')."</pre></td></tr>";
1398
1399 $port->printunlinktr($linktype, $remote_port);
1400
1401 echo "</table></td>";
1402
1403
1404 echo "<td>";
1405 $remote_port->printtable('both');
1406 echo "</td>";
1407
1408 if($debug)
1409 $debug['remote_port'] = &$remote_port;
1410
1411 }
1412 else
1413 $port->printunlinktr();
1414
1415
1416 }
1417 echo "</tr><tr>";
1418
1419 echo "<td>";
1420 $object = linkmgmt_RTport::printobjecttable($object_id);
1421 echo "</td>";
1422
1423 if($debug)
1424 $debug['object'] = &$object;
1425
1426 if($remote_id != NULL)
1427 {
1428
1429 echo "<td></td>"; /* link */
1430 echo "<td>";
1431 $remote_object = linkmgmt_RTport::printobjecttable($remote_port->port['object_id']);
1432 echo "</td>";
1433
1434 if($debug)
1435 $debug['remote_object'] = &$remote_object;
1436 }
1437
1438 echo "</tr></table>";
1439
1440 if($debug)
1441 {
1442 echo "<pre>--- Debug ---";
1443 var_dump($debug);
1444 echo "</pre>";
1445 }
1446
1447 exit;
1448}
1449
1450/* -------------------------------------- */
1451function lm_renderObjectCell ($cell)
1452{
1453 echo "<table class='slbcell vscell'><tr><td rowspan=2 width='5%'>";
1454 printImageHREF ('OBJECT');
1455 echo '</td><td>';
1456 echo mkA ('<strong>' . niftyString ($cell['dname']) . '</strong>', 'object', $cell['id']);
1457 echo '</td></tr><tr><td>';
1458 echo count ($cell['etags']) ? ("<small>" . serializeTags ($cell['etags']) . "</small>") : '&nbsp;';
1459 echo "</td></tr></table>";
1460}
1461/* -------------------------------------- */
1462
1463// TODO replace..
1464class linkmgmt_RTport {
1465
1466 private $port_id = NULL;
1467
1468 public $port = false;
1469
1470 function __construct($port_id) {
1471
1472 $this->port = getPortInfo($port_id);
1473
1474 if($this->port === false)
1475 return;
1476
1477 /* successfully get port info */
1478 $this->port_id = $port_id;
1479
1480 } /* __construct */
1481
1482 function isvalid() {
1483 return ($port_id !== NULL);
1484 }
1485
1486 function getlinks($type = 'front') {
1487 } /* getlinks */
1488
1489 function printtable($linktype = 'front') {
1490
1491 if($this->port_id == NULL)
1492 return;
1493
1494 echo "<table>";
1495
1496 $urlparams = array(
1497 'module' => 'redirect',
1498 'page' => 'object',
1499 'tab' => 'linkmgmt',
1500 'op' => 'map',
1501 'object_id' => $this->port['object_id'],
1502 'port_id' => $this->port_id,
1503 'usemap' => 1,
1504 );
1505
1506 echo '<tr><td><a title="don\'t highlight port" href="?'.http_build_query($urlparams).'">-phl</a></td>';
1507
1508 $urlparams['hl'] = 'p';
1509 echo '<td><a title="highlight port" href="?'.http_build_query($urlparams).'">+phl</a></td></tr>';
1510
1511 $this->_printinforow($this->port,
1512 array(
1513 'id' => 'Port ID',
1514 'name' => 'Port Name',
1515 'oif_name' => 'Port Type',
1516 'l2address' => 'MAC',
1517 'reservation_comment' => 'comment',
1518 )
1519 ); /* printinforow */
1520
1521 $this->printlinktr($linktype);
1522
1523 echo "</table>";
1524 } /* printtable */
1525
1526 function printlinktr($linktype = 'front') {
1527 if($this->port_id === NULL)
1528 return;
1529
1530 $urlparams = array(
1531 'tab' => 'linkmgmt',
1532 'page' => 'object',
1533 'op'=>'PortLinkDialog',
1534 'port'=>$this->port_id,
1535 'object_id'=>$this->port['object_id'],
1536 'linktype' => $linktype,
1537 );
1538
1539 echo "<tr><td align=\"center\"><a href='".
1540 makeHrefProcess($urlparams).
1541 "'>";
1542 printImageHREF ('plug', 'Link this port');
1543 echo "</a></td></tr>";
1544 } /* link */
1545
1546 function printunlinktr($linktype = 'front', $remote_port = NULL) {
1547 if($this->port_id === NULL)
1548 return;
1549
1550 $urlparams = array(
1551 'tab' => 'linkmgmt',
1552 'op'=>'unlinkPort',
1553 'port_id'=>$this->port_id,
1554 'object_id'=>$this->port['object_id'],
1555 'linktype' => $linktype,
1556 );
1557
1558 $confirmmsg = "unlink port ".$this->port['name'];
1559
1560 if($remote_port !== NULL)
1561 {
1562 $urlparams['remote_id'] = $remote_port->port['id'];
1563 $confirmmsg .= ' -> '.$remote_port->port['name'];
1564 }
1565
1566 $confirmmsg .= " ($linktype)"; // TODO cableid
1567
1568 echo "<tr><td align=\"center\"><a href='".makeHrefProcess($urlparams).
1569 "' onclick=\"return confirm('$confirmmsg');\">";
1570 printImageHREF ('cut', 'Unlink this port');
1571 echo "</a></td></tr>";
1572
1573 } /* unlink */
1574
1575 /* TODO move to object class */
1576 static function printobjecttable($object_id = NULL) {
1577
1578 if($object_id === NULL)
1579 return;
1580
1581 $object = spotEntity ('object', $object_id);
1582
1583 if($object === false)
1584 return;
1585
1586 if($object['rack_id'])
1587 {
1588 $rack = spotEntity('rack', $object['rack_id']);
1589
1590 $object['row_name'] = $rack['row_name'];
1591 $object['rack_name'] = $rack['name'];
1592 }
1593
1594 echo "<table><tr><td>";
1595 lm_renderObjectCell($object);
1596 echo "</td></tr><tr><td><table>";
1597
1598 self::_printinforow($object,
1599 array(
1600 'id' => 'ID',
1601 'dname' => 'Name',
1602 'label' => 'Label',
1603 'rack_name' => 'Rack',
1604 'row_name' => 'Row',
1605 )
1606
1607 ); /* printinforow */
1608
1609 $urlparams = array(
1610 'module' => 'redirect',
1611 'page' => 'object',
1612 'tab' => 'linkmgmt',
1613 'op' => 'map',
1614 'object_id' => $object_id,
1615 'usemap' => 1,
1616 );
1617
1618 echo '<tr><td><a title="don\'t highlight object" href="?'.http_build_query($urlparams).'">-ohl</a></td>';
1619
1620 $urlparams['hl'] = 'o';
1621 echo '<td><a title="highlight object" href="?'.http_build_query($urlparams).'">+ohl</a></td></tr>';
1622
1623 echo "</table></td></tr></table>";
1624
1625 return $object;
1626
1627 } /* printobjecttable */
1628
1629 static function _printinforow(&$data, $config) {
1630
1631 foreach($config as $key => $name)
1632 {
1633 if(isset($data[$key]))
1634 {
1635 $value = $data[$key];
1636 if(!empty($value))
1637 echo "<tr><td align=\"right\" nowrap=\"nowrap\" style=\"font-size:10;\">$name:</td><td nowrap=\"nowrap\">$value</td></tr>";
1638 }
1639 }
1640
1641 } /* _printinforow */
1642} /* class RTport */
1643
1644/* -------------------------------------------------- */
1645
1646function linkmgmt_opmap() {
1647
1648 /* display require errors "white screen of death" */
1649 $errorlevel = error_reporting();
1650 error_reporting(E_ALL);
1651
1652 require_once 'Image/GraphViz.php';
1653
1654/*
1655 *
1656 */
1657class lm_Image_GraphViz extends Image_GraphViz {
1658
1659 /* extend renderDotFile with additional output file
1660 */
1661 function renderDotFile($dotfile, $outputfile, $format = 'svg',
1662 $command = null, $outputfile2 = null, $format2 = null)
1663 {
1664 if (!file_exists($dotfile)) {
1665 if ($this->_returnFalseOnError) {
1666 return false;
1667 }
1668 $error = PEAR::raiseError('Could not find dot file');
1669 return $error;
1670 }
1671
1672 $oldmtime = file_exists($outputfile) ? filemtime($outputfile) : 0;
1673
1674 switch ($command) {
1675 case 'dot':
1676 case 'neato':
1677 break;
1678 default:
1679 $command = $this->graph['directed'] ? 'dot' : 'neato';
1680 }
1681 $command_orig = $command;
1682
1683 $command = $this->binPath.(($command == 'dot') ? $this->dotCommand
1684 : $this->neatoCommand);
1685
1686 $command .= ' -T'.escapeshellarg($format)
1687 .' -o'.escapeshellarg($outputfile)
1688 .($format2 !== null && $outputfile2 !== null ? ' -T'.escapeshellarg($format2).' -o'.escapeshellarg($outputfile2) : '')
1689 .' '.escapeshellarg($dotfile)
1690 .' 2>&1';
1691
1692 exec($command, $msg, $return_val);
1693
1694 clearstatcache();
1695 if (file_exists($outputfile) && filemtime($outputfile) > $oldmtime
1696 && $return_val == 0) {
1697 return true;
1698 } elseif ($this->_returnFalseOnError) {
1699 return false;
1700 }
1701 $error = PEAR::raiseError($command_orig.' command failed: '
1702 .implode("\n", $msg));
1703 return $error;
1704 }
1705 // renderDotFile
1706
1707
1708 /*
1709 */
1710 function fetch($format = 'svg', $command = null, $format2 = null, &$data2 = null)
1711 {
1712
1713 $file = $this->saveParsedGraph();
1714 if (!$file || PEAR::isError($file)) {
1715 return $file;
1716 }
1717
1718 $outputfile = $file . '.' . $format;
1719
1720 if($format2 != null && $data2 !== null)
1721 $outputfile2 = $file . '.' . $format2;
1722 else
1723 $outputfile2 = null;
1724
1725 $rendered = $this->renderDotFile($file, $outputfile, $format,
1726 $command, $outputfile2, $format2);
1727 if ($rendered !== true) {
1728 return $rendered;
1729 }
1730
1731 @unlink($file);
1732
1733 if($format2 !== null && $data2 !== null) {
1734 $fp = fopen($outputfile2, 'rb');
1735
1736 if ($fp) {
1737 $data = fread($fp, filesize($outputfile2));
1738 fclose($fp);
1739 @unlink($outputfile2);
1740
1741 $data2 = $data;
1742 } else {
1743 return $error;
1744 }
1745 }
1746
1747
1748 $fp = fopen($outputfile, 'rb');
1749
1750 if (!$fp) {
1751 if ($this->_returnFalseOnError) {
1752 return false;
83b49cf7 1753 }
1754 $error = PEAR::raiseError('Could not read rendered file');
1755 return $error;
1756 }
1757
3badc888 1758 $data = fread($fp, filesize($outputfile));
1759 fclose($fp);
1760 @unlink($outputfile);
1761
1762 return $data;
1763 }
1764 // fetch
1765
1766
1767
1768} /* class lm_Image_GraphViz */
1769
1770 error_reporting($errorlevel);
1771
1772 $object_id = NULL;
1773 $port_id = NULL;
1774 $remote_id = NULL;
1775 $allports = false;
1776 $usemap = false;
1777 $command = NULL;
1778
1779 /* highlight object */
1780 $hl = NULL;
1781 if(isset($_REQUEST['hl']))
1782 {
1783 $hl = $_REQUEST['hl'];
1784 unset($_REQUEST['hl_object_id']);
1785 unset($_REQUEST['hl_port_id']);
1786
1787 if($hl == 'o')
1788 {
1789 unset($_GET['port_id']);
1790 unset($_GET['remote_id']);
1791 }
1792
1793 }
1794
1795 if(!$hl && isset($_REQUEST['hl_object_id']))
1796 {
1797 $hl = 'o';
1798 $object_id = $_REQUEST['hl_object_id'];
1799 $_REQUEST['object_id'] = $object_id;
1800 unset($_REQUEST['hl_object_id']);
1801 unset($_REQUEST['hl_port_id']);
1802 unset($_REQUEST['port_id']);
1803 }
1804
1805 if(isset($_REQUEST['object_id']))
1806 $object_id = $_REQUEST['object_id'];
1807
1808 if(isset($_REQUEST['type']))
1809 {
1810 $type = $_REQUEST['type'];
1811 }
1812 else
1813 $type = 'gif';
1814
1815 /* highlight port */
1816 if(!$hl && isset($_REQUEST['hl_port_id']))
1817 {
1818 $hl = 'p';
1819 $port_id = $_REQUEST['hl_port_id'];
1820 $_REQUEST['port_id'] = $port_id;
1821 unset($_REQUEST['hl_port_id']);
1822 }
1823
1824 if(isset($_REQUEST['allports']))
1825 {
1826 $allports = $_REQUEST['allports'];
1827 }
1828
1829 if(isset($_REQUEST['port_id']))
1830 {
1831 $port_id = $_REQUEST['port_id'];
1832 }
1833
1834 if(isset($_REQUEST['usemap']))
1835 $usemap = $_REQUEST['usemap'];
1836
1837 if($hl == 'p' && $port_id === NULL)
1838 {
1839 unset($_GET['hl']);
1840 unset($_GET['port_id']);
1841 unset($_GET['remote_id']);
1842 }
1843
1844 if($hl == 'o')
1845 unset($_GET['remote_id']);
1846
1847 if(isset($_REQUEST['remote_id']))
1848 $remote_id = $_REQUEST['remote_id'];
1849
1850 /* show all objects */
1851 if(isset($_REQUEST['all']))
1852 {
1853 $object_id = NULL;
1854 $port_id = NULL;
1855 $hl = NULL;
1856 unset($_GET['hl']);
1857 }
1858
1859 if(isset($_REQUEST['cmd']))
1860 $command = $_REQUEST['cmd'];
1861
1862 if(isset($_REQUEST['debug']))
1863 $debug = $_REQUEST['debug'];
1864 else
1865 $debug = False;
1866
1867 if($debug) echo "-- DEBUG --<br>";
1868
1869
1870 switch($type) {
1871 case 'gif':
1872 case 'png':
1873 case 'bmp':
1874 case 'jpeg':
1875 case 'tif':
1876 case 'wbmp':
1877 $ctype = "image/$type";
1878 break;
1879 case 'jpg':
1880 $ctype = "image/jpeg";
1881 break;
1882 case 'svg':
1883 $ctype = 'image/svg+xml';
1884 break;
1885 case 'pdf':
1886 $ctype = 'application/pdf';
1887 break;
1888 case 'cmapx':
1889 $ctype = 'text/plain';
1890 break;
1891 }
1892
1893 $start = microtime(true);
1894 $gvmap = new linkmgmt_gvmap($object_id, $port_id, $allports, $hl, $remote_id);
1895 $stop = microtime(true);
1896
1897 if($debug)
1898 echo "gvmap Time: ".( $stop - $start )."<br>";
1899
1900 if($debug) echo "-- after gvmap --<br>";
1901
1902 if($usemap)
1903 {
1904
1905 if($debug) echo "-- usemap --<br>";
1906
1907 /* add context menu to Ports, Objects, Links, ...
1908 */
1909
1910 echo "<script>
1911 function initcontextmenu() {
1912 var maps = document.getElementsByTagName('map');
1913 for(var i=0;i<maps.length;i++) {
1914 var areas = maps[i].childNodes;
1915
1916 for(j=0;j<areas.length;j++) {
1917 if(areas[j].nodeType == 1)
1918 {
1919 // console.log(areas[j].id);
1920 // attr = document.createAttribute('onmouseover','ahh');
1921 // areas[j].setAttribute(attr);
1922 // areas[j].onmouseover = 'menu(this);';
1923
1924 areas[j].addEventListener('contextmenu',menu,false);
1925 // areas[j].oncontextmenu = 'menu(this, event);';
1926 // console.log(areas[j].oncontextmenu);
1927 }
1928 }
1929
1930 }
1931
1932 };
1933
1934 function menu(event) {
1935
1936 // console.log('Menu');
1937
1938 if(!event)
1939 event = window.event;
1940
1941 var parent = event.target;
1942
1943 // console.log('--' + parent);
1944
1945 var ids = parent.id.split('-');
1946
1947 if(ids[0] == 'graph1')
1948 return false;
1949
1950 var object_id = ids[0];
1951
1952 var url ='?module=ajax&ac=lm_mapinfo&object_id=' + object_id;
1953
1954 // links ='<li><a href=' + object_id + '>Object</a></li>';
1955
1956 if(ids[1] != '')
1957 {
1958 var port_id = ids[1];
1959 url += '&port_id=' + port_id;
1960 // links += '<li><a href=' + port_id + '>Port</a></li>';
1961
1962 if(ids[2] != '')
1963 {
1964 var remote_id = ids[2];
1965
1966 if(ids[3] != '')
1967 {
1968 var linktype = ids[3];
1969 url += '&remote_id=' + remote_id + '&linktype=' + linktype;
1970 // links += '<li><a href=' + port_id + '_' + remote_id + '_' + linktype + '>Unlink</a></li>';
1971 }
1972 }
1973
1974 }
1975
1976
1977 var xmlHttp = new XMLHttpRequest();
1978 xmlHttp.open('GET', url, false);
1979 xmlHttp.send(null);
1980
1981 var infodiv = document.getElementById('info');
1982 infodiv.innerHTML = xmlHttp.responseText;
1983
1984 // linkdiv = document.getElementById('link');
1985 // linkdiv.innerHTML = links;
1986
1987 var menudiv = document.getElementById('menu');
1988 menudiv.style.position = 'absolute';
1989 menudiv.style.top = (event.clientY + document.body.scrollTop) + 'px';
1990 menudiv.style.left = (event.clientX + document.body.scrollLeft) + 'px';
1991 menudiv.style.display = '';
1992
1993 return false;
1994 };
1995
1996 function mousedown(event) {
1997 // console.log('mouse down');
1998
1999 if(!event)
2000 event = window.event;
2001
2002 if(event.button != 2)
2003 return true;
2004
2005 var menudiv = document.getElementById('menu');
2006
2007 menudiv.style.display = 'none';
2008
2009 return false;
2010 };
2011
2012 </script>";
2013
2014 echo "<body oncontextmenu=\"return false\" onmousedown=\"mousedown(event);\" onload=\"initcontextmenu();\">";
2015
2016 echo "<div id=\"menu\" style=\"display:none; background-color:#ffff90\">
2017 <div id=\"info\"></div>
2018 <ul id=\"link\" style=\"list-style-type:none\"></ul>
2019 </div>";
2020
2021 if($debug)
2022 $gvmap->setFalseOnError(False);
2023
2024 $data2 = '';
2025
2026 $start = microtime(true);
2027 $data = $gvmap->fetch($type, $command, 'cmapx', $data2);
2028 $stop = microtime(true);
2029
2030 if($debug)
2031 echo "DOT time: ".( $stop - $start )."<br>";
2032
2033 if($data === false)
2034 echo "ERROR Fetching image data!<br>";
2035
2036 if(PEAR::isError($data))
2037 echo $data->getMessage();
2038
2039 //echo $gvmap->fetch('cmapx', $command);
2040 echo $data2;
2041
2042 if($debug) echo "-- after map gvmap --<br>";
2043
2044 echo "<img src=\"data:$ctype;base64,".
2045 base64_encode($data).
2046 "\" usemap=#map$object_id />";
2047
2048 if($debug)
2049 {
2050 echo "<pre>";
2051 echo $gvmap->export();
2052 echo "</pre>";
2053
2054 echo "<pre>".$gvmap->parse()."</pre>";
2055 }
2056 }
2057 else
2058 {
2059 $gvmap->image($type);
2060 }
2061
2062 exit;
2063
2064} /* linkmgmt_opmap */
2065
2066class cytoscapedata
2067{
2068 public $objects = array();
2069
2070 public $pids = array();
2071
2072 public $parents = array();
2073 public $nodes = array();
2074
2075 public $edges = NULL;
2076
2077 public $debug = null;
2078
2079 function __construct()
2080 {
2081
2082 $this->edges['parents'] = array();
2083 $this->edges['nodes'] = array();
2084 }
2085
2086 function addnode($id, $values = NULL, &$arr = NULL)
2087 {
2088 $data = array( 'id' => $id );
2089
2090 if($values != NULL)
2091 $data = $data + $values;
2092
2093 $node['data'] = $data;
2094
2095 $this->objects[] = array('group' => 'nodes') + $node;
2096
2097 switch($values['type'])
2098 {
2099 case 'port':
2100 $this->nodes[$id] = array('group' => 'nodes') + $node;
2101 break;
2102 case 'object':
2103 case 'container':
2104 $this->parents[$id] = array('group' => 'nodes') + $node;
2105 break;
2106 }
2107
2108 if($arr !== NULL)
2109 $arr[] = array('group' => 'nodes') + $node;
2110 }
2111
2112 function addedge($id, $source, $target, $values = NULL, &$arr = NULL)
2113 {
2114 $data = array(
2115 'id' => $id,
2116 'source' => $source,
2117 'target' => $target
2118 );
2119
2120 if($values != NULL)
2121 $data = $data + $values;
2122
2123 $edge['data'] = $data;
2124
2125 //$this->elements['edges'][] = $edge;
2126
2127 if($arr !== NULL)
2128 {
2129 $arr[$id] = array('group' => 'edges') + $edge;
2130 }
2131 else
2132 $this->objects[] = array('group' => 'edges') + $edge;
2133
2134 //$this->edges[] = array('group' => 'edges') + $edge;
2135 }
2136
2137 function _addobjectnode($object_id, $type = 'object')
2138 {
2139 global $lc_cache;
2140
2141 if(!isset($this->parents["o$object_id"]))
2142 {
2143 $rack = null;
2144 $object = $lc_cache->getobject($object_id, $rack);
2145
2146 $clustertitle = "${object['dname']}";
2147
2148 //has_problems
2149 //if($object['has_problems'] != 'no')
2150
2151 $rack_text = "";
2152 if(!empty($rack['row_name']) || !empty($rack['name']))
2153 {
2154 $rack_text = "${rack['row_name']} / ${rack['name']}";
2155 }
2156
2157 $data = array('label' => $object['name'], 'text' => $rack_text, 'type' => $type, 'has_problems' => $object['has_problems']);
2158
2159 $container_id = $object['container_id'];
2160 if($container_id)
2161 {
2162 $data['parent'] = "o$container_id";
2163 $this->_addobjectnode($container_id, 'container');
2164 }
2165
2166 $this->addnode('o'.$object_id, $data);
2167 }
2168
2169 }
2170
2171 // cytoscape
2172 function addlinkchain($linkchain, $index) {
2173
2174 foreach($linkchain as $id => $port)
2175 {
2176
2177 if(!$linkchain->linked)
2178 continue;
2179
2180 $this->_addobjectnode($port['object_id']);
2181
2182 $text = (isset($port['portip']) ? $port['portip'] : "" );
2183 $nodedata = array('label' => $port['name'], 'parent' => 'o'.$port['object_id'], 'text' => $text, 'type' => 'port', 'index' => $index , 'loop' => ($linkchain->loop ? '1' : '0'));
2184
2185 //$this->addnode('l_'.$port['id'], array( 'label' => $port['name'], 'parent' => 'p'.$port['id'], 'text' => $text ));
2186
2187 if($port['portcount'] > 1)
2188 foreach($port['chains'] as $mlc)
2189 {
2190 $this->addlinkchain($mlc, 0); // TODO index
2191 }
2192
2193 $prevlinktype = ($port['linktype'] == 'front' ? 'back' : 'front');
2194 if($port[$prevlinktype]['portcount'] > 1)
2195 {
2196 foreach($port[$prevlinktype]['chains'] as $mlc)
2197 {
2198 $this->addlinkchain($mlc, 0, false); // TODO index
2199 }
2200 }
83b49cf7 2201
3badc888 2202 if($port['remote_id'])
2203 {
83b49cf7 2204
3badc888 2205 $this->_addobjectnode($port['remote_object_id']);
83b49cf7 2206
3badc888 2207 $linktype = $port['linktype'];
2208 $edgedata = array('label' => $port['cableid'], 'type' => $linktype, 'loop' => ($linkchain->loop ? '1' : '0'));
83b49cf7 2209
3badc888 2210 if($linkchain->loop && $port['remote_id'] == $linkchain->first)
2211 {
2212 $nodedata['loopedge'] = array('group' => 'edges', 'data' => array( 'id' => 'le'.$port['id']."_".$port['remote_id'], 'source' => 'p'.$port['id'], 'target' => 'p'.$port['remote_id']) + $edgedata);
2213 }
2214 else
2215 {
83b49cf7 2216
3badc888 2217 $this->addedge('e'.$port['id']."_".$port['remote_id'], 'p'.$port['id'], 'p'.$port['remote_id'], $edgedata);
2218 $this->addedge('e'.$port['id']."_".$port['remote_id'], 'p'.$port['id'], 'p'.$port['remote_id'], $edgedata, $this->edges['nodes']);
2219 $id1 = $port['object_id'];
2220 if($id1 > $port['remote_object_id'])
2221 {
2222 $id1 = $port['remote_object_id'];
2223 $id2 = $port['object_id'];
2224 }
2225 else
2226 $id2 = $port['remote_object_id'];
fdc74cf0 2227
3badc888 2228 $peid = "pe".$id1."_".$id2;
fdc74cf0 2229
3badc888 2230 if(isset($this->edges['parents'][$peid]))
2231 $this->edges['parents'][$peid]['data']['linkcount']++;
2232 else
2233 $this->addedge($peid, 'o'.$id1, 'o'.$id2, array('type' => $linktype, 'linkcount' => 1), $this->edges['parents']);
2234 }
2235 }
fdc74cf0 2236
3badc888 2237 $this->addnode('p'.$port['id'], $nodedata);
2238 }
2239
2240 if(0)
2241 if($linkchain->first != $linkchain->last )
fdc74cf0 2242 {
3badc888 2243 $first = $linkchain->first;
2244 $last = $linkchain->last;
2245 $this->addedge("l${first}_${last}",'p'.$first, 'p'.$last, array('type' => 'logical', 'label' => "logical"));
fdc74cf0 2246 }
2247
3badc888 2248 //portlist::var_dump_html($this->parents);
fdc74cf0 2249 }
2250
3badc888 2251 function getlinkchains($object_id) {
fdc74cf0 2252
3badc888 2253 $this->objects = array();
2254 $this->parents = array();
2255 $this->nodes = array();
2256 $this->edges['parents'] = array();
2257 $this->edges['nodes'] = array();
fdc74cf0 2258
3badc888 2259 $this->_getlinkchains($object_id);
fdc74cf0 2260 }
fdc74cf0 2261
3badc888 2262 function getelements()
fdc74cf0 2263 {
3badc888 2264 //lm_linkchain::var_dump_html($this);
2265 return array('parents' => array_values($this->parents),
2266 'nodes' => array_values($this->nodes),
2267 'edges' => array(
2268 'parents' => array_values($this->edges['parents']),
2269 'nodes' => array_values($this->edges['nodes'])
2270 ),
2271 'debug' => $this->debug
2272 );
fdc74cf0 2273
fdc74cf0 2274 }
2275
3badc888 2276 function gettest()
fdc74cf0 2277 {
3badc888 2278 return array_merge(array_values($this->parents), array_values($this->edges['parents']));
2279 //return array_merge(array_values($this->parents), array_values($this->edges['parents']), array_values($this->nodes), array_values($this->edges['nodes']));
fdc74cf0 2280 }
2281
3badc888 2282 function _getlinkchains($object_id) {
fdc74cf0 2283
fdc74cf0 2284
3badc888 2285 // container
2286 // $object = spotEntity('object', $object_id);
2287 $object['ports'] = getObjectPortsAndLinks ($object_id);
fdc74cf0 2288
3badc888 2289 $i = 0;
2290 foreach($object['ports'] as $key => $port)
2291 {
fdc74cf0 2292
3badc888 2293 if(isset($this->pids[$port['id']]))
2294 continue;
2295
2296 $i++;
2297 $lc = new lm_linkchain($port['id']);
2298
2299 $this->pids += $lc->pids;
2300
2301 $this->addlinkchain($lc, $i);
2302
2303 }
2304
2305 $children = getEntityRelatives ('children', 'object', $object_id);
2306
2307 foreach($children as $child)
2308 $this->_getlinkchains($child['entity_id']);
2309 }
2310
2311 function allobjects()
fdc74cf0 2312 {
3badc888 2313
2314 $this->objects = array();
2315 $this->parents = array();
2316 $this->nodes = array();
2317 $this->edges['parents'] = array();
2318 $this->edges['nodes'] = array();
2319
2320 $objects = listCells('object');
2321
2322 $i = 0;
2323 foreach($objects as $object)
2324 {
2325 //echo $object['id']."<br>";
2326 $this->_getlinkchains($object['id']);
2327 $i++;
2328 if($i > 20 ) break;
2329 }
fdc74cf0 2330 }
3badc888 2331}
fdc74cf0 2332
3badc888 2333function linkmgmt_cytoscapemap() {
fdc74cf0 2334
fdc74cf0 2335
3badc888 2336 $object_id = $_GET['object_id'];
fdc74cf0 2337
3badc888 2338 if(isset($_GET['json']))
2339 {
2340 ob_start();
2341 $data = new cytoscapedata();
2342 $data->getlinkchains($object_id);
2343 //$data->allobjects(); // ugly graph;
2344 //echo json_encode($data->objects);
fdc74cf0 2345
3badc888 2346 if(ob_get_length())
2347 $data->debug = ob_get_contents();
fdc74cf0 2348
3badc888 2349 ob_end_clean();
2350
2351 echo json_encode($data->getelements());
fdc74cf0 2352
3badc888 2353 exit;
fdc74cf0 2354 }
2355
3badc888 2356 echo (<<<HTMLEND
2357<!DOCTYPE html>
2358<html>
2359<head>
2360<style>
2361body {
2362 font: 14px helvetica neue, helvetica, arial, sans-serif;
2363}
fdc74cf0 2364
3badc888 2365#cy {
2366 height: 100%;
2367 width: 100%;
2368 position: absolute;
2369 left: 0;
2370 top: 0;
2371}
2372</style>
2373<meta charset=utf-8 />
2374<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
2375<title>Compound nodes</title>
2376<!--<script src="js/jquery-1.4.4.min.js"></script>-->
2377
383230c5 2378<script src="?module=chrome&uri=linkmgmt/jquery-1.11.3.min.js"></script>
2379<script src="?module=chrome&uri=linkmgmt/cytoscape.min.js"></script>
2380<script src="?module=chrome&uri=linkmgmt/dagre.min.js"></script>
2381<script src="?module=chrome&uri=linkmgmt/cytoscape-dagre.js"></script>
2382<link rel="stylesheet" type="text/css" href="?module=chrome&uri=linkmgmt/jquery.qtip.min.css">
2383<script src="?module=chrome&uri=linkmgmt/jquery.qtip.min.js"></script>
2384<script src="?module=chrome&uri=linkmgmt/cytoscape-qtip.js"></script>
3badc888 2385
383230c5 2386<!--<script src="?module=chrome&uri=linkmgmt/cytoscape-css-renderer_mod.js"></script>-->
2387<!--<script src="?module=chrome&uri=linkmgmt/cytoscape.js-navigator.js_mod"></script>-->
3badc888 2388<script>
2389var cy = null;
2390var cy2 = null;
2391var layout = null;
2392var data = null;
2393
2394$(function(){ // on dom ready
2395 var cystyle = [
2396 {
2397 selector: 'node',
2398 css: {
2399 'content': 'data(id)',
2400 'text-wrap': 'wrap'
2401 },
2402 style: {
2403 'background-color': '#666',
2404 'label': 'data(label)',
2405 'width': 'label',
2406 'min-zoomed-font-size' : 8,
2407 'text-valign': 'center',
2408 'text-halign': 'center',
2409 'text-wrap': 'wrap',
2410 'shape': function(ele) {
2411 if(ele.data('type') != 'port')
2412 return 'rectangle';
2413 else
2414 return 'ellipse';
2415 },
fdc74cf0 2416
3badc888 2417 }
2418 },
2419 {
2420 selector: '\$node > node',
2421 css: {
2422 'padding-top': '10px',
2423 'padding-left': '10px',
2424 'padding-bottom': '10px',
2425 'padding-right': '10px',
2426 'text-valign': 'top',
2427 'text-halign': 'center',
2428 'background-color': '#bbb',
2429 'min-zoomed-font-size' : 6,
2430 }
2431 },
2432 {
2433 selector: 'edge',
2434 css: {
2435 'target-arrow-shape': 'triangle',
2436 'line-color': function(ele){
2437 if(ele.data('type') == 'front')
2438 return 'black';
2439 else
2440 return 'grey';
2441 },
2442 'line-style': function(ele){
2443 if(ele.data('type') == 'front')
2444 return 'solid';
2445 else
2446 return 'dashed';
2447 },
2448 'width': function(ele){
2449 var ret = 1;
2450 if(ele.data('type') == 'front')
2451 ret = 3;
2452 else
2453 ret = 5;
fdc74cf0 2454
3badc888 2455 if(1)
2456 if(ele.data('linkcount'))
2457 {
2458 ret = (ele.data('linkcount') * ret);
2459 }
fdc74cf0 2460
3badc888 2461 //console.log(ele.data('id') + ret);
2462 return ret;
2463 },
2464// 'curve-style': 'segments',
2465 'font-size': '8',
2466 'min-zoomed-font-size' : 8,
2467 'label': 'data(label)',
2468 'edge-text-rotation': 'autorotate',
2469 'source-arrow-shape': 'none',
2470 'target-arrow-shape': 'none'
2471 }
2472 },
2473 {
2474 selector: '.logical',
2475 css: {
2476 'line-color': '#0000ff',
2477 'width': 5,
2478 'curve-style': 'segments',
2479 'z-index': 0
2480 }
2481 },
2482 {
2483 selector: ':selected',
2484 css: {
2485 'background-color': 'black',
2486 'line-color': 'black',
2487 'target-arrow-color': 'black',
2488 'source-arrow-color': 'black'
2489 }
2490 },
2491 {
2492 selector: '.highlighted',
2493 css: {
2494 'line-color': '#ff0000',
2495 'background-color': '#ff0000'
2496 }
2497 },
2498 {
2499 selector: '.clhighlighted',
2500 css: {
2501 'line-color': '#00ff00',
2502 'background-color': '#00ff00'
2503 }
2504 }
2505 ];
fdc74cf0 2506
3badc888 2507var cylayout = { name: 'dagre', nodeSep: 3, ready: layoutready, stop: layoutstop };
fdc74cf0 2508
3badc888 2509function highlight(evt) {
fdc74cf0 2510
3badc888 2511 //cy = evt.cy;
fdc74cf0 2512
3badc888 2513 var hlclass = evt.data.hlclass;
2514 var ele = evt.cyTarget;
fdc74cf0 2515
3badc888 2516 if(!ele.data)
2517 return;
fdc74cf0 2518
3badc888 2519 if(ele.data('source'))
2520 ele = cy.$( '#' + ele.data('source'));
fdc74cf0 2521
3badc888 2522 var id = ele.data('id');
fdc74cf0 2523
3badc888 2524 if(ele.isParent())
2525 {
2526 // var childs = cy.$('#' + id).children()
2527 // childs.layout({name:'grid', cols: 2, condense: true});
2528 return;
2529 }
fdc74cf0 2530
fdc74cf0 2531
3badc888 2532// console.log('Event ' + hlclass + ' ' + id );
fdc74cf0 2533
3badc888 2534 // remove existing highlights
2535 cy.$('.' + hlclass).removeClass(hlclass);
fdc74cf0 2536
3badc888 2537 var hleles = ele.closedNeighborhood();
fdc74cf0 2538
3badc888 2539 var hlcount = hleles.length;
fdc74cf0 2540
3badc888 2541 // max 100
2542 for(i=0;i<100;i++)
2543 {
2544 hleles = hleles.closedNeighborhood();
fdc74cf0 2545
3badc888 2546 if(hlcount == hleles.length)
2547 break;
fdc74cf0 2548
3badc888 2549 hlcount = hleles.length;
fdc74cf0 2550
3badc888 2551 }
2552 //console.log('End Loop ' + i );
2553 hleles.addClass(hlclass);
fdc74cf0 2554
3badc888 2555// console.log('Event Type: ' + evt.originalEvent.type)
2556// console.log('Event Type: ' + evt.type)
fdc74cf0 2557
3badc888 2558 if(evt.type != 'click')
2559 return;
fdc74cf0 2560
3badc888 2561 var hleles2 = hleles.clone();
2562 hleles2 = hleles2.add(hleles.parents().clone());
fdc74cf0 2563
3badc888 2564 //var cy2 = evt.data.cy2;
fdc74cf0 2565
3badc888 2566 //var ret = evt.data.ret;
fdc74cf0 2567
3badc888 2568 //var j = ret.parents.concat(ret.nodes).concat(ret.edges.nodes);
fdc74cf0 2569
3badc888 2570 cy2.remove(cy2.elements());
2571 cy2.add(hleles2);
2572 //cy2.add(j);
2573 cy2.layout({name: 'dagre', rankDir: 'LR', ready: layoutready});
2574}
fdc74cf0 2575
3badc888 2576function cytoscapeswitch(evt)
2577{
2578 var b = evt.target;
fdc74cf0 2579
3badc888 2580 var j = null;
2581 if(b.value == "1")
2582 {
2583 j = data.parents.concat(data.edges.parents);
2584 b.value = "0";
2585 b.innerHTML = "Ports";
2586 }
2587 else
2588 {
2589 j = data.parents.concat(data.nodes).concat(data.edges.nodes);
2590 b.value = "1";
2591 b.innerHTML = "Objects";
2592 }
fdc74cf0 2593
3badc888 2594 //layout.stop();
2595 cy.remove(cy.elements());
fdc74cf0 2596
3badc888 2597 cy.add(j);
fdc74cf0 2598
3badc888 2599 cy.layout(cylayout);
2600 //layout.run();
2601}
fdc74cf0 2602
3badc888 2603$.ajax({
2604 type: "GET",
2605 url: "{$_SERVER['PHP_SELF']}",
2606 data: { module: 'redirect',
2607 page: 'object',
2608 tab: 'linkmgmt',
2609 object_id: $object_id,
2610 op: 'cytoscapemap',
2611 json: 'json'
2612 },
2613 dataTye: 'json',
2614 error: function(){ alert("Error loading"); },
2615 success: function(jdata) {
2616
2617 data = JSON.parse(jdata);
2618
2619 if(data.debug)
2620 $('#debug').html(data.debug);
2621
2622 //j = data.parents.concat(data.edges.parents);
2623 j = data.parents.concat(data.nodes).concat(data.edges.nodes);
2624
2625 if(j.length == 0)
2626 {
2627 alert("No Links to display. Closing Window");
2628 window.close();
2629 return;
2630 }
fdc74cf0 2631
3badc888 2632 cy2 = cytoscape({
2633 container: document.getElementById('cy2'),
2634 //renderer: { name: 'css' },
2635
2636 boxSelectionEnabled: false,
2637 autounselectify: true,
2638 style: cystyle,
2639 wheelSensitivity: 0.1,
2640 });
2641 cy2.style().selector('node').style('label', function(node) { return node.data('label') + '\\n' + node.data('text'); });
2642
2643 cystyle.push({
2644 selector: '.loopedge',
2645 css: {
2646 'curve-style': 'segments',
2647 }
2648 });
2649
2650 cy = cytoscape({
2651 container: document.getElementById('cy'),
2652
2653 boxSelectionEnabled: false,
2654 autounselectify: true,
2655 style: cystyle,
2656 wheelSensitivity: 0.1,
2657 elements: j,
2658 layout: cylayout,
2659 });
2660
2661 cy.on('mouseover', { hlclass: 'highlighted' }, highlight );
2662 cy.on('click', { hlclass: 'clhighlighted' }, highlight );
2663
2664 $('#switch').click(cytoscapeswitch);
2665
2666 /*
2667 TODO: node ranking
2668 */
2669
2670 if(0)
2671 cy.layout({
2672 name: 'dagre',
2673 /*
2674 nodeSep: 5,
2675 rankDir: 'TB',
2676 */
2677 //edgeSep: 10,
2678 /*
2679 edgeWeight: function(edge) {
2680 if(edge.data('type') == 'front')
2681 return 10;
2682 else
2683 return 1;
2684
2685 },
2686 */
2687 ready: layoutready,
2688 stop: layoutstop
2689 });
2690
2691 if(1)
2692 cy.$(':child').qtip({
2693
2694 content: function() {
2695
2696 return this.id() + '<br>'
2697 + 'Index: ' + this.data('index') + '<br>'
2698 + 'Name: ' + this.data('label') + '<br>'
2699 + 'Text: ' + this.data('text');
2700 },
2701 position: {
2702 my: 'top center',
2703 at: 'bottom center'
2704 },
2705 style: {
2706 classes: 'qtip-bootstrap',
2707 tip: {
2708 width: 16,
2709 height: 8
2710 },
2711 }
2712 }); // qtip
2713 } // success function
2714 });
2715
2716
2717}); // on dom ready
2718
2719function layoutready(evt) {
2720 var _cy = evt.cy;
fdc74cf0 2721
3badc888 2722 // highlight current object
2723 var object = _cy.$('#o$object_id');
2724 if(object.data('has_problems') == 'no')
2725 object.style('background-color','#ffcccc');
fdc74cf0 2726
3badc888 2727 // highlight object with problems
2728 _cy.$('node[type = "object"][has_problems != "no"]').style('background-color','#ff0000');
de3d42dc 2729
3badc888 2730 var e = _cy.$('node[loop = "1"]').style('background-color','#ff6666');
83b49cf7 2731
3badc888 2732// console.log(e[0].data('loop'));
de3d42dc 2733
3badc888 2734// cy.add({group: 'edges', data: {id:'l691_2991', source: 'p691', target: 'p2991', label: 'test'}, classes: 'logical'});
de3d42dc 2735
3badc888 2736}
fdc74cf0 2737
3badc888 2738function layoutstop(evt) {
2739 var _cy = evt.cy;
2740// cy.elements().locked = true;
2741// cy.add({group: 'nodes', data: {id:'l2493', parent:'p2943', label: 'test'}});
fdc74cf0 2742
fdc74cf0 2743
3badc888 2744 var les = _cy.$('[loopedge]');
fdc74cf0 2745
3badc888 2746 if(les)
fdc74cf0 2747 {
3badc888 2748 _cy.batch( function() {
2749 les.each(function(i, ele) {
2750 var le = ele.data('loopedge');
2751 var edge = _cy.add(le);
2752 edge.addClass('loopedge');
2753 //edge.style('line-color', '#ffffff'); // TypeError text-transform undefined
2754 });
2755 });
2756 _cy.$('edge[loop = "1"]').style('line-color','#ff6666');
fdc74cf0 2757 }
3badc888 2758}
2759</script>
2760</head>
2761<body>
2762<div id="cy" style="position: absolute; height: 80%; width: 100%; left: 0; top: 20%;"></div>
2763<div id="cy2" style="position: absolute; height: 20%; width: 100%; left: 0; top: 0%;"></div>
2764<div id="debug"></div>
2765<button type="button" id="switch" style="position: absolute;" value="1">Object</button>
2766</body>
2767</html>
2768HTMLEND
2769); // echo
fdc74cf0 2770 exit;
3badc888 2771}
fdc74cf0 2772
2773/* ------------------------------------- */
2774class linkmgmt_gvmap {
2775
2776 private $object_id = NULL;
2777 private $port_id = NULL;
2778 private $remote_id = NULL;
3badc888 2779 private $hl = NULL;
fdc74cf0 2780
2781 private $gv = NULL;
2782
2783 private $ports = array();
2784
2785 private $allports = false;
2786 private $back = NULL;
2787
adc1e20b 2788 private $alpha = 'ff';
fdc74cf0 2789
2790 private $errorlevel = NULL;
2791
3badc888 2792 public $data = NULL;
2793
2794 private $pids = array();
2795
2796 function addlinkchainsobject($object_id)
2797 {
2798
2799 $object['ports'] = getObjectPortsAndLinks ($object_id);
2800
2801 if(empty($object['ports']))
2802 {
2803
2804 $hl = false;
2805 $alpha = $this->alpha;
2806 if($this->hl == 'o' && $this->object_id == $object_id)
2807 {
2808 $hl = true;
2809 $this->alpha = 'ff';
2810 }
2811
2812 $this->_addCluster($object_id, $hl, empty($object['ports']));
2813
2814 $this->alpha = $alpha;
2815 return;
2816 }
2817
2818 $i = 0;
2819 foreach($object['ports'] as $key => $port)
2820 {
2821
2822 if(isset($this->pids[$port['id']]))
2823 continue;
2824
2825 $i++;
2826 $lc = new lm_linkchain($port['id']);
2827
2828 $this->pids += $lc->pids;
2829
2830 if($this->allports ||($lc->linkcount > 0))
2831 $this->addlinkchain($lc, $i);
2832 }
2833
2834 }
2835
fdc74cf0 2836 function __construct($object_id = NULL, $port_id = NULL, $allports = false, $hl = NULL, $remote_id = NULL) {
2837 $this->allports = $allports;
2838
4eeb9282 2839 $this->object_id = $object_id;
2840 $this->port_id = $port_id;
2841 $this->remote_id = $remote_id;
3badc888 2842 $this->hl = $hl;
fdc74cf0 2843
2844 $hllabel = "";
2845
2846 /* suppress strict standards warnings for Image_GraphViz and PHP 5.4.0
2847 * output would corrupt image data
2848 */
2849 $this->errorlevel = error_reporting();
857ba778 2850 error_reporting($this->errorlevel & ~E_STRICT);
fdc74cf0 2851
2852 $graphattr = array(
3badc888 2853 'rankdir' => 'LR',
fdc74cf0 2854 // 'ranksep' => '0',
2855 'nodesep' => '0',
2856 // 'overlay' => false,
2857 );
2858
2859 unset($_GET['module']);
2860
2861 $_GET['all'] = 1;
2862
2dbca6fb 2863 $graphattr['URL'] = $this->_makeHrefProcess($_GET);
fdc74cf0 2864
2865 unset($_GET['all']);
2866
3badc888 2867 switch($hl)
2868 {
2869 case 'o':
2870 case 'p':
2871 $this->alpha = '30';
2872 break;
2873 }
2874
83b49cf7 2875 //$this->gv = new Image_GraphViz(true, $graphattr, "map".$object_id);
2876 $this->gv = new lm_Image_GraphViz(true, $graphattr, "map".$object_id);
fdc74cf0 2877
3badc888 2878 /* --------------------------- */
fdc74cf0 2879 if($object_id === NULL)
2880 {
4eeb9282 2881 /* all objects ! */
fdc74cf0 2882 unset($_GET['all']);
2883 $_GET['hl'] = 'o';
2884
2885 $this->gv->addAttributes(array(
2886 'label' => 'Showing all objects'.$hllabel,
2887 'labelloc' => 't',
2888 )
2889 );
2890
2891 $objects = listCells('object');
2892
2893 foreach($objects as $obj)
3badc888 2894 //$this->addlinkchainsobject($obj['id']); // longer runtimes !!
2895 $this->_add($this->gv, $obj['id'], NULL); // for all still faster and nicer looking graph
4eeb9282 2896
2897 return;
fdc74cf0 2898 }
2899 else
2900 {
2901 $object = spotEntity ('object', $object_id);
2902
2903 $this->gv->addAttributes(array(
2904 'label' => "Graph for ${object['dname']}$hllabel",
2905 'labelloc' => 't',
2906 )
2907 );
2908
3badc888 2909 $this->addlinkchainsobject($object_id);
2910 //$this->_add($this->gv, $object_id, $port_id);
fdc74cf0 2911
2912 $children = getEntityRelatives ('children', 'object', $object_id); //'entity_id'
2913
2914 foreach($children as $child)
3badc888 2915 $this->addlinkchainsobject($child['entity_id']);
2916 // $this->_add($this->gv, $child['entity_id'], NULL);
fdc74cf0 2917 }
2918
4eeb9282 2919 /* add hl label */
2920 $this->gv->addAttributes(array(
2921 'label' => $this->gv->graph['attributes']['label'].$hllabel,
2922 ));
2923
fdc74cf0 2924 // portlist::var_dump_html($this->gv);
3badc888 2925// portlist::var_dump_html($this->data);
2926
2927// echo json_encode($this->data);
fdc74cf0 2928
83b49cf7 2929 // $this->gv->saveParsedGraph('/tmp/graph.txt');
fdc74cf0 2930 // error_reporting( E_ALL ^ E_NOTICE);
2931 } /* __construct */
2932
2933 function __destruct() {
2934 error_reporting($this->errorlevel);
2935 }
2936
3badc888 2937 function _addCluster($object_id, $hl = false, $adddummy = false)
2938 {
2939 global $lc_cache;
2940
2941 $cluster_id = "c".$object_id;
2942
2943 if(
2944 !isset($this->gv->graph['clusters'][$cluster_id]) &&
2945 !isset($this->gv->graph['subgraphs'][$cluster_id])
2946 || $hl
2947 ) {
2948 $rack = null;
2949 $object = $lc_cache->getobject($object_id, $rack);
2950
2951 // $object['attr'] = getAttrValues($object_id);
2952
2953 $clusterattr = array();
2954
2955 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'color');
2956 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'fontcolor');
2957
2958 if($this->object_id == $object_id)
2959 {
2960 $clusterattr['rank'] = 'source';
2961
2962 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'color');
2963 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'fontcolor');
2964 }
2965
2966 $clustertitle = htmlspecialchars($object['dname']);
2967 $clusterattr['tooltip'] = $clustertitle;
2968
2969 unset($_GET['module']); // makeHrefProcess adds this
2970 unset($_GET['port_id']);
2971 unset($_GET['remote_id']);
2972 $_GET['object_id'] = $object_id;
2973 //$_GET['hl'] = 'o';
2974
2975 $clusterattr['URL'] = $this->_makeHrefProcess($_GET);
2976
2977 //has_problems
2978 if($object['has_problems'] != 'no')
2979 {
2980 $clusterattr['style'] = 'filled';
2981 $this->_getcolor('cluster', 'problem', $this->alpha, $clusterattr, 'fillcolor');
2982 }
2983
2984 if(!empty($object['container_name']))
2985 $clustertitle .= "<BR/>(${object['container_name']})";
2986
2987 if(!empty($rack['row_name']) || !empty($rack['name']))
2988 $clustertitle .= "<BR/><FONT point-size=\"10\">{$rack['row_name']} / {$rack['name']}</FONT>";
2989
2990 $embedin = $object['container_id'];
2991 if(empty($embedin))
2992 $embedin = 'default';
2993 else
2994 {
2995 $embedin = "c$embedin"; /* see cluster_id */
2996
2997 // TODO
2998 /* add container / cluster if not already exists */
2999 $this->addlinkchainsobject($object['container_id']);
3000 }
3001
3002 $clusterattr['id'] = "$object_id----"; /* used for js context menu */
3003
3004 $this->gv->addCluster($cluster_id, $clustertitle, $clusterattr, $embedin);
3005
3006 /* needed because of gv_image empty cluster bug (invalid foreach argument) */
3007 if($adddummy)
3008 $this->gv->addNode("dummy$cluster_id", array(
3009 // 'label' =>'No Ports found/connected',
3010 'label' =>'',
3011 'fontsize' => 0,
3012 'size' => 0,
3013 'width' => 0,
3014 'height' => 0,
3015 'shape' => 'point',
3016 'style' => 'invis',
3017 ), $cluster_id);
3018
3019
3020 }
3021 } // _addCluster
3022
3023 function _addEdge($port, $linkchain, $loopedge = false)
3024 {
3025 global $lm_multilink_port_types;
3026 if(
3027 !isset($this->gv->graph['edgesFrom'][$port['id']][$port['remote_id']]) &&
3028 !isset($this->gv->graph['edgesFrom'][$port['remote_id']][$port['id']])
3029 || $loopedge
3030 ) {
3031 $remote_id = $port['remote_id'];
3032
3033 $linktype = $port['linktype'];
3034
3035 $edgetooltip = $port['object_name'].':'.$port['name'].
3036 ' - '.$port['cableid'].' -> '.
3037 $port['remote_name'].':'.$port['remote_object_name'];
3038
3039 $edgeattr = array(
3040 'fontsize' => 8,
3041 'label' => htmlspecialchars($port['cableid']),
3042 'tooltip' => $edgetooltip,
3043 'sametail' => $linktype,
3044 'samehead' => $linktype,
3045 'arrowhead' => 'none',
3046 'arrowtail' => 'none',
3047 );
3048
3049 $this->_getcolor('edge', ($linkchain->loop ? 'loop' : 'default'), $this->alpha, $edgeattr, 'color');
3050 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'fontcolor');
3051
3052 if($linktype == 'back' )
3053 {
3054 $edgeattr['style'] = 'dashed';
3055
3056 /* multilink ports */
3057 if(in_array($port['oif_id'], $lm_multilink_port_types))
3058 {
3059 $edgeattr['dir'] = 'both';
3060 $edgeattr['arrowtail'] = 'dot';
3061 }
3062
3063 if(in_array($linkchain->ports[$remote_id]['oif_id'], $lm_multilink_port_types))
3064 {
3065 $edgeattr['dir'] = 'both';
3066 $edgeattr['arrowhead'] = 'dot';
3067 }
3068 }
3069
3070 if(
3071 ($port['id'] == $this->port_id && $port['remote_id'] == $this->remote_id) ||
3072 ($port['id'] == $this->remote_id && $port['remote_id'] == $this->port_id)
3073 )
3074 {
3075 $this->_getcolor('edge', 'highlight', 'ff', $edgeattr, 'color');
3076 $edgeattr['penwidth'] = 2; /* bold */
3077 }
3078
3079 unset($_GET['module']);
3080 $_GET['object_id'] = $port['object_id'];
3081 $_GET['port_id'] = $port['id'];
3082 $_GET['remote_id'] = $port['remote_id'];
3083
3084 $edgeattr['URL'] = $this->_makeHrefProcess($_GET);
3085
3086 $edgeattr['id'] = $port['object_id']."-".$port['id']."-".$port['remote_id']."-".$linktype; /* for js context menu */
3087
3088 if($loopedge)
3089 {
3090 $edgeattr = array_merge($edgeattr, array(
3091 'sametail' => 'loop',
3092 'samehead' => 'loop',
3093 'dir' => 'both',
3094 'arrowhead' => 'invodot',
3095 'arrowtail' => 'invodot',
3096 ));
3097 }
3098
3099 $this->gv->addEdge(array($port['id'] => $port['remote_id']),
3100 $edgeattr,
3101 array(
3102 $port['id'] => $linktype,
3103 $port['remote_id'] => $linktype,
3104 )
3105 );
3106
3107 }
3108 } // _addEdge
3109
3110 function addlinkchain($linkchain, $index)
3111 {
3112 global $lm_multilink_port_types;
3113
3114 $hl = false;
3115 $alpha = $this->alpha;
3116 if(
3117 ($this->hl == 'p' && $linkchain->hasport_id($this->port_id))
3118 || ($this->hl == 'o' && $linkchain->hasobject_id($this->object_id))
3119 )
3120 {
3121 $hl = true;
3122 $this->alpha = 'ff';
3123 }
3124
3125 $remote_id = null;
3126 foreach($linkchain as $id => $port)
3127 {
3128 $this->_addCluster($port['object_id'], $hl);
3129
3130 $nodelabel = htmlspecialchars("${port['name']}");
3131 $text = $nodelabel;
3132
3133 if($port['iif_id'] != '1' )
3134 {
3135 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['iif_name']}</FONT>";
3136 $text .= "\n".$port['iif_name'];
3137 }
3138
3139 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['oif_name']}</FONT>";
3140 $text .= "\n".$port['oif_name'];
3141
3142 // add ip address
3143 if(isset($port['portip']))
3144 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">".$port['portip']."</FONT>";
3145
3146 $nodeattr = array(
3147 'label' => $nodelabel,
3148 );
3149
3150 $this->_getcolor('port', ($linkchain->loop ? 'loop' : 'default'),$this->alpha, $nodeattr, 'fontcolor');
3151 $this->_getcolor('oif_id', $port['oif_id'],$this->alpha, $nodeattr, 'color');
3152
3153 if($this->port_id == $port['id']) {
3154 $nodeattr['style'] = 'filled';
3155 $nodeattr['fillcolor'] = $this->_getcolor('port', 'current', $this->alpha);
3156 }
3157
3158 if($this->remote_id == $port['id']) {
3159 $nodeattr['style'] = 'filled';
3160 $nodeattr['fillcolor'] = $this->_getcolor('port', 'remote', $this->alpha);
3161 }
3162
3163 $nodeattr['tooltip'] = htmlspecialchars("${port['name']}");
3164
3165 unset($_GET['module']);
3166 unset($_GET['remote_id']);
3167 $_GET['object_id'] = $port['object_id'];
3168 $_GET['port_id'] = $port['id'];
3169 $_GET['hl'] = 'p';
3170
3171 $nodeattr['URL'] = $this->_makeHrefProcess($_GET);
3172 $nodeattr['id'] = "${port['object_id']}-${port['id']}--"; /* for js context menu */
3173
3174 $this->gv->addNode($port['id'],
3175 $nodeattr,
3176 "c${port['object_id']}"); /* see cluster_id */
3177
3178 $remote_id = $port['remote_id'];
3179
3180 if($port['portcount'] > 1)
3181 foreach($port['chains'] as $mlc)
3182 {
3183 $this->addlinkchain($mlc, 0); // TODO index
3184 }
3185
3186 $prevlinktype = ($port['linktype'] == 'front' ? 'back' : 'front');
3187 if($port[$prevlinktype]['portcount'] > 1)
3188 {
3189 foreach($port[$prevlinktype]['chains'] as $mlc)
3190 {
3191 $this->addlinkchain($mlc, 0, false); // TODO index
3192 }
3193 }
3194
3195 if($remote_id)
3196 $this->_addEdge($port, $linkchain);
3197
3198 } //foreach
3199
3200 if($linkchain->loop && $remote_id)
3201 {
3202 // TODO separate loop link
3203 // add loop edge
3204 $this->_addEdge($port, $linkchain, true);
3205 }
3206
3207 // reset alpha to start value
3208 $this->alpha = $alpha;
3209 }
3210
de3d42dc 3211 function setFalseOnError($newvalue)
3212 {
3213 $this->gv->_returnFalseOnError = $newvalue;
3214 }
3215
2dbca6fb 3216 function _makeHrefProcess($array)
3217 {
3218 return str_replace('&','&amp;',makeHrefProcess($array));
3219 }
3220
fdc74cf0 3221 // !!!recursiv !!!
3222 function _add($gv, $object_id, $port_id = NULL) {
3223 global $lm_multilink_port_types;
3224
4eeb9282 3225 if($port_id !== NULL) {
3226 if(isset($this->ports[$port_id])) {
fdc74cf0 3227 return;
4eeb9282 3228 }
fdc74cf0 3229 }
3230
2d257b3a 3231 if($this->back != 'front' || $port_id === NULL || $this->allports)
3232 $front = $this->_getObjectPortsAndLinks($object_id, 'front', $port_id, $this->allports);
3233 else
3234 $front = array();
3235
3236 if($this->back != 'back' || $port_id === NULL || $this->allports)
3237 $backend = $this->_getObjectPortsAndLinks($object_id, 'back', $port_id, $this->allports);
3238 else
3239 $backend = array();
3240
3241 $ports = array_merge($front,$backend);
3242
630a86c8 3243 /* used only for Graphviz ...
3244 * !! numeric ids cause Image_Graphviz problems on nested clusters !!
3245 */
3246 $cluster_id = "c$object_id";
3247
2d257b3a 3248 if(empty($ports))
3249 {
3250 /* needed because of gv_image empty cluster bug (invalid foreach argument) */
da6d05d0 3251 $gv->addNode("dummy$cluster_id", array(
2d257b3a 3252 // 'label' =>'No Ports found/connected',
3253 'label' =>'',
3254 'fontsize' => 0,
3255 'size' => 0,
3256 'width' => 0,
3257 'height' => 0,
3258 'shape' => 'point',
3259 'style' => 'invis',
3260 ), $cluster_id);
3261
3262 /* show objects without ports */
3263 if($object_id === NULL)
3264 return;
3265 }
3266
3badc888 3267 $object = NULL;
2d257b3a 3268 if($object_id !== NULL) {
4eeb9282 3269 if(
3270 !isset($gv->graph['clusters'][$cluster_id]) &&
3271 !isset($gv->graph['subgraphs'][$cluster_id])
3272 ) {
fdc74cf0 3273
4eeb9282 3274 $object = spotEntity ('object', $object_id);
3badc888 3275
3276 // ip addresses
3277 amplifyCell($object);
3278 $object['portip'] = array();
3279 foreach($object['ipv4'] as $ipv4)
3280 {
3281 $object['portip'][$ipv4['osif']] = $ipv4['addrinfo']['ip'];
3282 }
3283
4eeb9282 3284 // $object['attr'] = getAttrValues($object_id);
fdc74cf0 3285
4eeb9282 3286 $clusterattr = array();
fdc74cf0 3287
4eeb9282 3288 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'color');
3289 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'fontcolor');
fdc74cf0 3290
4eeb9282 3291 if($this->object_id == $object_id)
3292 {
3293 $clusterattr['rank'] = 'source';
fdc74cf0 3294
4eeb9282 3295 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'color');
3296 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'fontcolor');
3297 }
fdc74cf0 3298
4eeb9282 3299 $clustertitle = "${object['dname']}";
3badc888 3300 $text = "${object['dname']}";
4eeb9282 3301 $clusterattr['tooltip'] = $clustertitle;
fdc74cf0 3302
4eeb9282 3303 unset($_GET['module']); // makeHrefProcess adds this
3304 unset($_GET['port_id']);
3305 unset($_GET['remote_id']);
3306 $_GET['object_id'] = $object_id;
3307 //$_GET['hl'] = 'o';
fdc74cf0 3308
2dbca6fb 3309 $clusterattr['URL'] = $this->_makeHrefProcess($_GET);
fdc74cf0 3310
4eeb9282 3311 //has_problems
3312 if($object['has_problems'] != 'no')
3313 {
3314 $clusterattr['style'] = 'filled';
3315 $this->_getcolor('cluster', 'problem', $this->alpha, $clusterattr, 'fillcolor');
3316 }
fdc74cf0 3317
4eeb9282 3318 if(!empty($object['container_name']))
3badc888 3319 {
4eeb9282 3320 $clustertitle .= "<BR/>${object['container_name']}";
3badc888 3321 $text .= "\n${object['container_name']}";
3322 }
fdc74cf0 3323
4eeb9282 3324 if($object['rack_id'])
3325 {
3326 $rack = spotEntity('rack', $object['rack_id']);
fdc74cf0 3327
4eeb9282 3328 if(!empty($rack['row_name']) || !empty($rack['name']))
3badc888 3329 {
4eeb9282 3330 $clustertitle .= "<BR/>${rack['row_name']} / ${rack['name']}";
3badc888 3331 $text .= "\n${rack['row_name']} / ${rack['name']}";
3332 }
4eeb9282 3333 }
3334
3335 $embedin = $object['container_id'];
3336 if(empty($embedin))
3337 $embedin = 'default';
3338 else
3339 {
3340 $embedin = "c$embedin"; /* see cluster_id */
3341
3342 /* add container / cluster if not already exists */
3343 $this->_add($gv, $object['container_id'], NULL);
3344 }
fdc74cf0 3345
2d257b3a 3346 $clusterattr['id'] = "$object_id----"; /* used for js context menu */
fdc74cf0 3347
2d257b3a 3348 $gv->addCluster($cluster_id, $clustertitle, $clusterattr, $embedin);
fdc74cf0 3349
2d257b3a 3350 } /* isset cluster_id */
3351 } /* object_id !== NULL */
fca4e38a 3352
fdc74cf0 3353 foreach($ports as $key => $port) {
3354
3355 $this->back = $port['linktype'];
3356
4eeb9282 3357 if(!isset($this->ports[$port['id']])) {
fdc74cf0 3358
fdc74cf0 3359
c613fc73 3360 $nodelabel = htmlspecialchars("${port['name']}");
3badc888 3361 $text = $nodelabel;
fdc74cf0 3362
4eeb9282 3363 if($port['iif_id'] != '1' )
3badc888 3364 {
4eeb9282 3365 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['iif_name']}</FONT>";
3badc888 3366 $text .= "\n".$port['iif_name'];
3367 }
fdc74cf0 3368
4eeb9282 3369 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['oif_name']}</FONT>";
3badc888 3370 $text .= "\n".$port['oif_name'];
3371
3372 // add ip address
3373 if($object)
3374 if(isset($object['portip'][$port['name']]))
3375 {
3376 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">".$object['portip'][$port['name']]."</FONT>";
3377 $text .= "\n".$object['portip'][$port['name']];
3378 }
fdc74cf0 3379
4eeb9282 3380 $nodeattr = array(
3381 'label' => $nodelabel,
3382 );
fdc74cf0 3383
4eeb9282 3384 $this->_getcolor('port', 'default',$this->alpha, $nodeattr, 'fontcolor');
3385 $this->_getcolor('oif_id', $port['oif_id'],$this->alpha, $nodeattr, 'color');
fdc74cf0 3386
4eeb9282 3387 if($this->port_id == $port['id']) {
3388 $nodeattr['style'] = 'filled';
3389 $nodeattr['fillcolor'] = $this->_getcolor('port', 'current', $this->alpha);
3390 }
fdc74cf0 3391
4eeb9282 3392 if($this->remote_id == $port['id']) {
3393 $nodeattr['style'] = 'filled';
3394 $nodeattr['fillcolor'] = $this->_getcolor('port', 'remote', $this->alpha);
3395 }
3396
c613fc73 3397 $nodeattr['tooltip'] = htmlspecialchars("${port['name']}");
4eeb9282 3398
3399 unset($_GET['module']);
3400 unset($_GET['remote_id']);
3401 $_GET['object_id'] = $port['object_id'];
3402 $_GET['port_id'] = $port['id'];
3403 $_GET['hl'] = 'p';
fdc74cf0 3404
2dbca6fb 3405 $nodeattr['URL'] = $this->_makeHrefProcess($_GET);
4eeb9282 3406 $nodeattr['id'] = "${port['object_id']}-${port['id']}--"; /* for js context menu */
fdc74cf0 3407
4eeb9282 3408 $gv->addNode($port['id'],
fdc74cf0 3409 $nodeattr,
3410 "c${port['object_id']}"); /* see cluster_id */
3411
4eeb9282 3412 $this->ports[$port['id']] = true;
2d257b3a 3413
4eeb9282 3414 } /* isset port */
fdc74cf0 3415
3416 if(!empty($port['remote_id'])) {
3417
bb000ce6 3418 if($this->object_id !== NULL)
3419 $this->_add($gv, $port['remote_object_id'], $port['remote_id']);
fdc74cf0 3420
3421 if(
3422 !isset($gv->graph['edgesFrom'][$port['id']][$port['remote_id']]) &&
3423 !isset($gv->graph['edgesFrom'][$port['remote_id']][$port['id']])
3424 ) {
3425
3426 $linktype = $port['linktype'];
3427
3428 $edgetooltip = $port['object_name'].':'.$port['name'].
3429 ' - '.$port['cableid'].' -> '.
3430 $port['remote_name'].':'.$port['remote_object_name'];
3431
3432 $edgeattr = array(
3433 'fontsize' => 8,
c613fc73 3434 'label' => htmlspecialchars($port['cableid']),
fdc74cf0 3435 'tooltip' => $edgetooltip,
3436 'sametail' => $linktype,
3437 'samehead' => $linktype,
3438 );
3439
3440 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'color');
3441 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'fontcolor');
3442
3443 if($linktype == 'back' )
3444 {
3445 $edgeattr['style'] = 'dashed';
3446 $edgeattr['arrowhead'] = 'none';
3447 $edgeattr['arrowtail'] = 'none';
3448
3449 /* multilink ports */
3450 if(in_array($port['oif_id'], $lm_multilink_port_types))
3451 {
3452 $edgeattr['dir'] = 'both';
3453 $edgeattr['arrowtail'] = 'dot';
3454 }
3455
3456 if(in_array($port['remote_oif_id'], $lm_multilink_port_types))
3457 {
3458 $edgeattr['dir'] = 'both';
3459 $edgeattr['arrowhead'] = 'dot';
3460 }
3461 }
3462
3463 if(
3464 ($port['id'] == $this->port_id && $port['remote_id'] == $this->remote_id) ||
3465 ($port['id'] == $this->remote_id && $port['remote_id'] == $this->port_id)
3466 )
3467 {
3468 $this->_getcolor('edge', 'highlight', 'ff', $edgeattr, 'color');
3469 $edgeattr['penwidth'] = 2; /* bold */
3470 }
3471
3472 unset($_GET['module']);
3473 $_GET['object_id'] = $port['object_id'];
3474 $_GET['port_id'] = $port['id'];
3475 $_GET['remote_id'] = $port['remote_id'];
3476
2dbca6fb 3477 $edgeattr['URL'] = $this->_makeHrefProcess($_GET);
fdc74cf0 3478
3479 $edgeattr['id'] = $port['object_id']."-".$port['id']."-".$port['remote_id']."-".$linktype; /* for js context menu */
3480
3481 $gv->addEdge(array($port['id'] => $port['remote_id']),
3482 $edgeattr,
3483 array(
3484 $port['id'] => $linktype,
3485 $port['remote_id'] => $linktype,
3486 )
3487 );
3badc888 3488
fdc74cf0 3489 }
3490 }
3491
3492 }
3493
3494 // portlist::var_dump_html($port);
3495 }
3496
83b49cf7 3497 function fetch($type = 'png', $command = NULL, $format2 = NULL, &$data2 = NULL) {
3498
3499 $tmpdata = $data2;
3500 $ret = $this->gv->fetch($type, $command, $format2, $tmpdata);
3501 if($data2 !== NULL)
3502 $data2 = $tmpdata;
fdc74cf0 3503 return $ret;
3504 }
3505
3506 function image($type = 'png', $command = NULL) {
3507 $this->gv->image($type, $command);
3508 }
3509
3510 function parse() {
3511 return $this->gv->parse();
3512 }
3513
3514 /* should be compatible with getObjectPortsAndLinks from RT database.php */
3515 function _getObjectPortsAndLinks($object_id, $linktype = 'front', $port_id = NULL, $allports = false) {
3516
3517 if($linktype == 'front')
3518 $linktable = 'Link';
3519 else
3520 $linktable = 'LinkBackend';
3521
3522 $qparams = array();
3523
3524 $query = "SELECT
3525 '$linktype' as linktype,
3526 Port.*,
3527 Port.type AS oif_id,
3528 PortInnerInterface.iif_name as iif_name,
262da468 3529 POI.oif_name as oif_name,
fdc74cf0 3530 Object.id as object_id, Object.name as object_name,
2a5cd259 3531 IFNULL(LinkTable_a.cable,LinkTable_b.cable) as cableid,
fdc74cf0 3532 remoteObject.id as remote_object_id, remoteObject.name as remote_object_name,
3533 remotePort.id as remote_id, remotePort.name as remote_name,
3534 remotePort.type AS remote_oif_id,
3535 remotePortInnerInterface.iif_name as remote_iif_name,
262da468 3536 remotePOI.oif_name as remote_oif_name
fdc74cf0 3537 FROM Port";
3538
3539 // JOIN
3540 $join = " LEFT JOIN PortInnerInterface on PortInnerInterface.id = Port.iif_id
262da468 3541 LEFT JOIN PortOuterInterface AS POI on POI.id = Port.type
2be16f85 3542 LEFT JOIN $linktable as LinkTable_a on Port.id = LinkTable_a.porta
2a5cd259 3543 LEFT JOIN $linktable as LinkTable_b on Port.id = LinkTable_b.portb
fdc74cf0 3544 LEFT JOIN Object on Object.id = Port.object_id
2a5cd259 3545 LEFT JOIN Port as remotePort on remotePort.id = IFNULL(LinkTable_a.portb, LinkTable_b.porta)
fdc74cf0 3546 LEFT JOIN Object as remoteObject on remoteObject.id = remotePort.object_id
3547 LEFT JOIN PortInnerInterface as remotePortInnerInterface on remotePortInnerInterface.id = remotePort.iif_id
262da468 3548 LEFT JOIN PortOuterInterface as remotePOI on remotePOI.id = remotePort.type
fdc74cf0 3549 ";
3550
3551 // WHERE
3552 if($port_id === NULL)
3553 {
3554 $where = " WHERE Object.id = ?";
3555 $qparams[] = $object_id;
3556
09749fb9 3557 if(!$allports) {
fdc74cf0 3558 $where .= " AND remotePort.id is not NULL";
09749fb9 3559
3560 if($linktype != 'front') {
2a5cd259 3561 $join .= "
3562 LEFT JOIN Link as FrontLink_a on Port.id = FrontLink_a.porta
3563 LEFT JOIN Link as FrontLink_b on Port.id = FrontLink_b.portb
3564 LEFT JOIN Link as FrontRemoteLink_a on remotePort.id = FrontRemoteLink_a.porta
3565 LEFT JOIN Link as FrontRemoteLink_b on remotePort.id = FrontRemoteLink_b.portb
09749fb9 3566 ";
2a5cd259 3567 $where .= " AND ( (FrontLink_a.porta is not NULL or FrontLink_b.portb is not NULL )
3568 OR (FrontRemoteLink_a.porta is not NULL or FrontRemoteLink_b.portb is not NULL) )";
09749fb9 3569 }
3570 }
fdc74cf0 3571 }
3572 else
3573 {
3574 // $where = " WHERE Port.id = ? and remotePort.id is not NULL";
3575 $where = " WHERE Port.id = ?";
3576 $qparams[] = $port_id;
3577 }
3578
3579 // ORDER
3580 $order = " ORDER by oif_name, Port.Name";
3581
3582 $query .= $join.$where.$order;
3583
2a5cd259 3584 //echo "$port_id: $query<br><br>";
3585
fdc74cf0 3586 $result = usePreparedSelectBlade ($query, $qparams);
3587
3588 $row = $result->fetchAll(PDO::FETCH_ASSOC);
3589
3590 $result->closeCursor();
3591
3592 return $row;
3593 }
3594
3595 function _getcolor($type = 'object', $key = 'default', $alpha = 'ff', &$array = NULL , $arraykey = 'color') {
3596
3597 $object = array(
3598 'current' => '#ff0000',
3599 );
3600 $port = array(
3601 'current' => '#ffff90',
3602 'remote' => '#ffffD0',
3badc888 3603 'loop' => '#ff6666',
fdc74cf0 3604 );
3605
3606 $cluster = array(
3607 'current' => '#ff0000',
3608 'problem' => '#ff3030',
3609 );
3610
3611 $edge = array (
3612 'highlight' => '#ff0000',
3badc888 3613 'loop' => '#ff6666',
fdc74cf0 3614 );
3615
3616 $oif_id = array(
3617 '16' => '#800000', /* AC-in */
3618 '1322' => '#ff4500', /* AC-out */
3619 '24' => '#000080', /* 1000base-t */
3620 );
3621
3622 $defaultcolor = '#000000'; /* black */
3623 $default = true;
3624
3625 if(isset(${$type}[$key]))
3626 {
3627 $default = false;
3628 $color = ${$type}[$key];
3629 }
3630 else
3631 $color = $defaultcolor;
3632
3633
3634 if($alpha != 'ff' || $default == false)
3635 {
3636 $color .= $alpha;
3637
3638 if($array !== NULL)
3639 $array[$arraykey] = $color;
3640 else
3641 return $color;
3642 }
3643 else
3644 return $defaultcolor;
3645
3646 } /* _getcolor */
3647
3648 function dump() {
3649 var_dump($this->gv);
3650 }
3651
3652 function export() {
3653 var_export($this->gv);
3654 }
3655
3656} /* class gvmap */
3657
3658/* -------------------------------------------------- */
3659
16ec374c 3660function linkmgmt_updateCableIdAJAX()
3661{
3662 $text = strip_tags(genericAssertion ('text', 'string0'));
3663 $linktype = genericAssertion ('linktype', 'string');
3664 $id1 = genericAssertion ('id1', 'uint');
3665 $id2 = genericAssertion ('id2', 'uint');
fdc74cf0 3666
16ec374c 3667 $port_info = getPortInfo ($id1);
3668 fixContext (spotEntity ('object', $port_info['object_id']));
3669 assertPermission ('object', 'ports', 'editPort');
fdc74cf0 3670
16ec374c 3671 if(!permitted(NULL, NULL, 'set_link'))
3672 return 'Permission denied!';
9276dc97 3673