add check for correct ipv6 addresses
[racktables-contribs] / linkmgmt.php
CommitLineData
9276dc97
ME
1<?php
2/*
fdc74cf0 3 * Link Management for RT 0.20.x
9276dc97 4 *
fdc74cf0 5 * Features:
6 * - create links between ports
7 * - create backend links between ports
8 * - visually display links / chains
9 * e.g.
10 * (Object)>[port] -- front --> [port]<(Object) == back == > (Object)>[port] -- front --> [port]<(Object)
11 * - Link object backend ports by name (e.g. handy for patch panels)
12 * - change/create CableID (needs jquery.jeditable.mini.js)
13 * - change/create Port Reservation Comment (needs jquery.jeditable.mini.js)
14 * - multiple backend links for supported port types (e.g. AC-in, DC)
15 * - GraphViz Maps (Objects, Ports and Links) (needs GraphViz_Image 1.3.0)
16 * - object,port or link highligthing (just click on it)
17 * - context menu to link and unlink ports
9276dc97 18 *
fdc74cf0 19 * Usage:
20 * 1. select "Link Management" tab
21 * 2. you should see link chains of all linked ports
22 * 3. to display all ports hit "Show All Ports" in the left upper corner
23 * 4. to link all ports with the same name of two different objects use "Link Object Ports by Name"
24 * a. select the other object you want to backend link to
25 * b. "show back ports" gives you the list of possible backend links
26 * !! Important port names have to be the same on both objects !!
27 * e.g. (Current Object):Portname -?-> Portname:(Selected Object)
28 * c. select all backend link to create (Ctrl + a for all)
29 * d. Enter backend CableID for all selected links
30 * e. "Link back" create the backend links
31 * 5. If you have an backend link within the same Object the link isn't shown until
32 * "Expand Backend Links on same Object" is hit
33 * 6. "Object Map" displays Graphviz Map of current object
34 * 7. To get a Graphviz Map of a single port click the port name on the left
9276dc97
ME
35 *
36 *
fdc74cf0 37 * Requirements:
38 * PHP 5 (http://php.net/)
39 * GraphViz_Image 1.3.0 or newer (http://pear.php.net/package/Image_GraphViz)
40 * GraphViz (http://www.graphviz.org/)
9276dc97
ME
41 *
42 * INSTALL:
43 *
fdc74cf0 44 * 1. create LinkBackend Table in your RackTables database
45 *
46 * Multilink table
9276dc97
ME
47
48CREATE TABLE `LinkBackend` (
49 `porta` int(10) unsigned NOT NULL DEFAULT '0',
50 `portb` int(10) unsigned NOT NULL DEFAULT '0',
51 `cable` char(64) DEFAULT NULL,
52 PRIMARY KEY (`porta`,`portb`),
cb15aecc 53 KEY `LinkBackend_FK_a` (`porta`),
fdc74cf0 54 KEY `LinkBackend_FK_b` (`portb`),
55 CONSTRAINT `LinkBackend_FK_a` FOREIGN KEY (`porta`) REFERENCES `Port` (`id`) ON DELETE CASCADE,
56 CONSTRAINT `LinkBackend_FK_b` FOREIGN KEY (`portb`) REFERENCES `Port` (`id`) ON DELETE CASCADE
9276dc97
ME
57) ENGINE=InnoDB DEFAULT CHARSET=utf8;
58
fdc74cf0 59 * 2. copy jquery.jeditable.mini.js to js/ directory (http://www.appelsiini.net/download/jquery.jeditable.mini.js)
60 * 3. copy linkmgmt.php to plugins directory
61 *
62 * Ready to go!
63 *
64 *
65 * UPDATE TABLE:
66 *
67 * Update from non-multilink table
68 * ALTER TABLE
69
70ALTER TABLE LinkBackend ADD KEY `LinkBackend_FK_b` (`portb`);
71ALTER TABLE LinkBackend DROP INDEX porta;
72ALTER TABLE LinkBackend DROP INDEX portb;
73
74 *
9276dc97
ME
75 *
76 * TESTED on FreeBSD 9.0, nginx/1.0.11, php 5.3.9
fdc74cf0 77 * GraphViz_Image 1.3.0
78 * and RackTables <= 0.20.3
9276dc97 79 *
cb15aecc 80 * (c)2012-2014 Maik Ehinger <m.ehinger@ltur.de>
9276dc97
ME
81 */
82
fdc74cf0 83/**
84 * The newest version of this plugin can be found at:
9276dc97 85 *
fdc74cf0 86 * https://github.com/github138/myRT-contribs/tree/develop-0.20.x
9276dc97
ME
87 *
88 */
89
90/*************************
91 * TODO
92 *
93 * - code cleanups
94 * - bug fixing
95 *
fdc74cf0 96 * - fix loopdectect for multiport
97 * MAX_LOOP_COUNT
98 * loop highlight gv map
9276dc97 99 *
fdc74cf0 100 * - fix column alignment with multilinks
101 *
102 * - put selected object/port top left of graph
103 * - multlink count for Graphviz maps empty or full dot
104 *
105 * - csv list
9276dc97
ME
106 *
107 */
108
fdc74cf0 109/* DEBUG */
110//error_reporting(E_ALL);
9276dc97
ME
111
112$tab['object']['linkmgmt'] = 'Link Management';
113$tabhandler['object']['linkmgmt'] = 'linkmgmt_tabhandler';
114//$trigger['object']['linkmgmt'] = 'linkmgmt_tabtrigger';
115
116$ophandler['object']['linkmgmt']['update'] = 'linkmgmt_opupdate';
117$ophandler['object']['linkmgmt']['unlinkPort'] = 'linkmgmt_opunlinkPort';
118$ophandler['object']['linkmgmt']['PortLinkDialog'] = 'linkmgmt_opPortLinkDialog';
119$ophandler['object']['linkmgmt']['Help'] = 'linkmgmt_opHelp';
120
fdc74cf0 121$ophandler['object']['linkmgmt']['map'] = 'linkmgmt_opmap';
122$ajaxhandler['lm_mapinfo'] = 'linkmgmt_ajax_mapinfo';
123
124/* ------------------------------------------------- */
125
1e6c47e6 126define('LM_MULTILINK',TRUE);
fdc74cf0 127
128/* -------------------------------------------------- */
129
130$lm_multilink_port_types = array(
131 16, /* AC-in */
132 //1322, /* AC-out */
133 1399, /* DC */
134 );
9276dc97
ME
135
136/* -------------------------------------------------- */
137
138$lm_cache = array(
139 'allowcomment' => TRUE, /* RackCode ${op_set_reserve_comment} */
140 'allowlink' => TRUE, /* RackCode ${op_set_link} */
141 'rackinfo' => array(),
142 );
143
144/* -------------------------------------------------- */
145
146//function linkmgmt_tabtrigger() {
147// return 'std';
148//} /* linkmgmt_tabtrigger */
149
150/* -------------------------------------------------- */
151
152function linkmgmt_opHelp() {
153?>
154 <table cellspacing=10><tr><th>Help</th><tr>
155 <tr><td width=150></td><td width=150 style="font-weight:bold;color:<?php echo portlist::CURRENT_OBJECT_BGCOLOR; ?>">Current Object</td></tr>
156 <tr><td></td><td bgcolor=<?php echo portlist::CURRENT_PORT_BGCOLOR; ?>>[current port]</td></tr>
157 <tr><td>front link</td><td>[port]<(Object)</td><td>back link</td></tr>
158 <tr><td>back link</td><td>(Object)>[port]</td><td>front link</td></tr>
159 <tr><td></td><td><pre>----></pre></td><td>Front link</td></tr>
160 <tr><td></td><td><pre>====></pre></td><td>Backend link</td></tr>
161 <tr><td></td><td>Link Symbol</td><td>Create new link</td></tr>
162 <tr><td></td><td>Cut Symbol</td><td>Delete link</td></tr>
163
164 </table>
165
166<?php
167 exit;
168} /* opHelp */
169
170/* -------------------------------------------------- */
171
fdc74cf0 172function linkmgmt_ajax_mapinfo() {
173
174 require_once 'inc/interface.php'; /* renderCell */
175
176 $object_id = NULL;
177 $port_id = NULL;
178 $remote_id = NULL;
179 $linktype = NULL;
180
181 if(isset($_REQUEST['object_id']))
182 $object_id = $_REQUEST['object_id'];
183
184 if(isset($_REQUEST['port_id']))
185 $port_id = $_REQUEST['port_id'];
186
187 if(isset($_REQUEST['remote_id']))
188 $remote_id = $_REQUEST['remote_id'];
189
190 if(isset($_REQUEST['linktype']))
191 $linktype = $_REQUEST['linktype'];
192
193 $debug = NULL;
194 if(isset($_REQUEST['debug']))
195 $debug['value'] = $_REQUEST['debug'];
196
197 $info = array();
198
199 echo "<table style=\"font-size:12;\"><tr>";
200
201 if($port_id != NULL)
202 {
203 $port = new linkmgmt_RTport($port_id);
204
205 echo "<td>";
206 $port->printtable('both');
207 echo "</td>";
208
209 if($debug)
210 $debug['port'] = &$port;
211
212 if($remote_id != NULL)
213 {
214
215 $remote_port = new linkmgmt_RTport($remote_id);
216
217 echo "<td><table align=\"center\">";
218
219 // TODO cableid
220 echo "<tr><td><pre>".($linktype == 'back' ? ' ===> ' : ' ---> ')."</pre></td></tr>";
221
222 $port->printunlinktr($linktype, $remote_port);
223
224 echo "</table></td>";
225
226
227 echo "<td>";
228 $remote_port->printtable('both');
229 echo "</td>";
230
231 if($debug)
232 $debug['remote_port'] = &$remote_port;
233
234 }
235 else
236 $port->printunlinktr();
237
238
239 }
240 echo "</tr><tr>";
241
242 echo "<td>";
243 $object = linkmgmt_RTport::printobjecttable($object_id);
244 echo "</td>";
245
246 if($debug)
247 $debug['object'] = &$object;
248
249 if($remote_id != NULL)
250 {
251
252 echo "<td></td>"; /* link */
253 echo "<td>";
254 $remote_object = linkmgmt_RTport::printobjecttable($remote_port->port['object_id']);
255 echo "</td>";
256
257 if($debug)
258 $debug['remote_object'] = &$remote_object;
259 }
260
261 echo "</tr></table>";
262
263 if($debug)
264 {
265 echo "<pre>--- Debug ---";
266 var_dump($debug);
267 echo "</pre>";
268 }
269
270 exit;
271}
272
273/* -------------------------------------- */
274
275class linkmgmt_RTport {
276
277 private $port_id = NULL;
278
279 public $port = false;
280
281 function __construct($port_id) {
282
283 $this->port = getPortInfo($port_id);
284
285 if($this->port === false)
286 return;
287
288 /* successfully get port info */
289 $this->port_id = $port_id;
290
291 } /* __construct */
292
293 function isvalid() {
294 return ($port_id !== NULL);
295 }
296
297 function getlinks($type = 'front') {
298 } /* getlinks */
299
300 function printtable($linktype = 'front') {
301
302 if($this->port_id == NULL)
303 return;
304
305 echo "<table>";
306
307 $urlparams = array(
308 'module' => 'redirect',
309 'page' => 'object',
310 'tab' => 'linkmgmt',
311 'op' => 'map',
312 'object_id' => $this->port['object_id'],
313 'port_id' => $this->port_id,
314 'usemap' => 1,
315 );
316
317 echo '<tr><td><a title="don\'t highlight port" href="?'.http_build_query($urlparams).'">-phl</a></td>';
318
319 $urlparams['hl'] = 'p';
320 echo '<td><a title="highlight port" href="?'.http_build_query($urlparams).'">+phl</a></td></tr>';
321
322 $this->_printinforow($this->port,
323 array(
324 'id' => 'Port ID',
325 'name' => 'Port Name',
326 'oif_name' => 'Port Type',
327 'l2address' => 'MAC',
328 'reservation_comment' => 'comment',
329 )
330 ); /* printinforow */
331
332 $this->printlinktr($linktype);
333
334 echo "</table>";
335 } /* printtable */
336
337 function printlinktr($linktype = 'front') {
338 if($this->port_id === NULL)
339 return;
340
341 $urlparams = array(
342 'tab' => 'linkmgmt',
343 'page' => 'object',
344 'op'=>'PortLinkDialog',
345 'port'=>$this->port_id,
346 'object_id'=>$this->port['object_id'],
347 'linktype' => $linktype,
348 );
349
350 echo "<tr><td align=\"center\"><a href='".
351 makeHrefProcess($urlparams).
352 "'>";
353 printImageHREF ('plug', 'Link this port');
354 echo "</a></td></tr>";
355 } /* link */
356
357 function printunlinktr($linktype = 'front', $remote_port = NULL) {
358 if($this->port_id === NULL)
359 return;
360
361 $urlparams = array(
362 'tab' => 'linkmgmt',
363 'op'=>'unlinkPort',
364 'port_id'=>$this->port_id,
365 'object_id'=>$this->port['object_id'],
366 'linktype' => $linktype,
367 );
368
369 $confirmmsg = "unlink port ".$this->port['name'];
370
371 if($remote_port !== NULL)
372 {
373 $urlparams['remote_id'] = $remote_port->port['id'];
374 $confirmmsg .= ' -> '.$remote_port->port['name'];
375 }
376
377 $confirmmsg .= " ($linktype)"; // TODO cableid
378
379 echo "<tr><td align=\"center\"><a href='".makeHrefProcess($urlparams).
380 "' onclick=\"return confirm('$confirmmsg');\">";
381 printImageHREF ('cut', 'Unlink this port');
382 echo "</a></td></tr>";
383
384 } /* unlink */
385
386 /* TODO move to object class */
387 static function printobjecttable($object_id = NULL) {
388
389 if($object_id === NULL)
390 return;
391
392 $object = spotEntity ('object', $object_id);
393
394 if($object === false)
395 return;
396
397 if($object['rack_id'])
398 {
399 $rack = spotEntity('rack', $object['rack_id']);
400
401 $object['row_name'] = $rack['row_name'];
402 $object['rack_name'] = $rack['name'];
403 }
404
405 echo "<table><tr><td>";
406 renderCell($object);
407 echo "</td></tr><tr><td><table>";
408
409 self::_printinforow($object,
410 array(
411 'id' => 'ID',
412 'dname' => 'Name',
413 'label' => 'Label',
414 'rack_name' => 'Rack',
415 'row_name' => 'Row',
416 )
417
418 ); /* printinforow */
419
420 $urlparams = array(
421 'module' => 'redirect',
422 'page' => 'object',
423 'tab' => 'linkmgmt',
424 'op' => 'map',
425 'object_id' => $object_id,
426 'usemap' => 1,
427 );
428
429 echo '<tr><td><a title="don\'t highlight object" href="?'.http_build_query($urlparams).'">-ohl</a></td>';
430
431 $urlparams['hl'] = 'o';
432 echo '<td><a title="highlight object" href="?'.http_build_query($urlparams).'">+ohl</a></td></tr>';
433
434 echo "</table></td></tr></table>";
435
436 return $object;
437
438 } /* printobjecttable */
439
440 static function _printinforow(&$data, $config) {
441
442 foreach($config as $key => $name)
443 {
444 if(isset($data[$key]))
445 {
446 $value = $data[$key];
447 if(!empty($value))
448 echo "<tr><td align=\"right\" nowrap=\"nowrap\" style=\"font-size:10;\">$name:</td><td nowrap=\"nowrap\">$value</td></tr>";
449 }
450 }
451
452 } /* _printinforow */
453} /* class RTport */
454
455/* -------------------------------------------------- */
456
457function linkmgmt_opmap() {
458
459 /* display require errors "white screen of death" */
460 $errorlevel = error_reporting();
461 error_reporting(E_ALL);
462
463 require_once 'Image/GraphViz.php';
464
83b49cf7 465/*
466 *
467 */
468class lm_Image_GraphViz extends Image_GraphViz {
469
470 /* extend renderDotFile with additional output file
471 */
472 function renderDotFile($dotfile, $outputfile, $format = 'svg',
473 $command = null, $outputfile2 = null, $format2 = null)
474 {
475 if (!file_exists($dotfile)) {
476 if ($this->_returnFalseOnError) {
477 return false;
478 }
479 $error = PEAR::raiseError('Could not find dot file');
480 return $error;
481 }
482
483 $oldmtime = file_exists($outputfile) ? filemtime($outputfile) : 0;
484
485 switch ($command) {
486 case 'dot':
487 case 'neato':
488 break;
489 default:
490 $command = $this->graph['directed'] ? 'dot' : 'neato';
491 }
492 $command_orig = $command;
493
494 $command = $this->binPath.(($command == 'dot') ? $this->dotCommand
495 : $this->neatoCommand);
496
497 $command .= ' -T'.escapeshellarg($format)
498 .' -o'.escapeshellarg($outputfile)
499 .($format2 !== null && $outputfile2 !== null ? ' -T'.escapeshellarg($format2).' -o'.escapeshellarg($outputfile2) : '')
500 .' '.escapeshellarg($dotfile)
501 .' 2>&1';
502
503 exec($command, $msg, $return_val);
504
505 clearstatcache();
506 if (file_exists($outputfile) && filemtime($outputfile) > $oldmtime
507 && $return_val == 0) {
508 return true;
509 } elseif ($this->_returnFalseOnError) {
510 return false;
511 }
512 $error = PEAR::raiseError($command_orig.' command failed: '
513 .implode("\n", $msg));
514 return $error;
515 }
516 // renderDotFile
517
518
519 /*
520 */
521 function fetch($format = 'svg', $command = null, $format2 = null, &$data2 = null)
522 {
523
524 $file = $this->saveParsedGraph();
525 if (!$file || PEAR::isError($file)) {
526 return $file;
527 }
528
529 $outputfile = $file . '.' . $format;
530
531 if($format2 != null && $data2 !== null)
532 $outputfile2 = $file . '.' . $format2;
533 else
534 $outputfile2 = null;
535
536 $rendered = $this->renderDotFile($file, $outputfile, $format,
537 $command, $outputfile2, $format2);
538 if ($rendered !== true) {
539 return $rendered;
540 }
541
542 @unlink($file);
543
544 if($format2 !== null && $data2 !== null) {
545 $fp = fopen($outputfile2, 'rb');
546
547 if ($fp) {
548 $data = fread($fp, filesize($outputfile2));
549 fclose($fp);
550 @unlink($outputfile2);
551
552 $data2 = $data;
553 } else {
554 return $error;
555 }
556 }
557
558
559 $fp = fopen($outputfile, 'rb');
560
561 if (!$fp) {
562 if ($this->_returnFalseOnError) {
563 return false;
564 }
565 $error = PEAR::raiseError('Could not read rendered file');
566 return $error;
567 }
568
569 $data = fread($fp, filesize($outputfile));
570 fclose($fp);
571 @unlink($outputfile);
572
573 return $data;
574 }
575 // fetch
576
577
578
579} /* class lm_Image_GraphViz */
580
fdc74cf0 581 error_reporting($errorlevel);
582
583 $object_id = NULL;
584 $port_id = NULL;
585 $remote_id = NULL;
586 $allports = false;
587 $usemap = false;
588 $command = NULL;
589
590 /* highlight object */
591 $hl = NULL;
592 if(isset($_REQUEST['hl']))
593 {
594 $hl = $_REQUEST['hl'];
595 unset($_REQUEST['hl_object_id']);
596 unset($_REQUEST['hl_port_id']);
597
598 if($hl == 'o')
599 {
600 unset($_GET['port_id']);
601 unset($_GET['remote_id']);
602 }
603
604 }
605
606 if(!$hl && isset($_REQUEST['hl_object_id']))
607 {
608 $hl = 'o';
609 $object_id = $_REQUEST['hl_object_id'];
4eeb9282 610 $_REQUEST['object_id'] = $object_id;
611 unset($_REQUEST['hl_object_id']);
fdc74cf0 612 unset($_REQUEST['hl_port_id']);
613 unset($_REQUEST['port_id']);
614 }
615
616 if(isset($_REQUEST['object_id']))
617 $object_id = $_REQUEST['object_id'];
618
619 if(isset($_REQUEST['type']))
620 {
621 $type = $_REQUEST['type'];
622 }
623 else
624 $type = 'gif';
625
626 /* highlight port */
627 if(!$hl && isset($_REQUEST['hl_port_id']))
628 {
629 $hl = 'p';
630 $port_id = $_REQUEST['hl_port_id'];
4eeb9282 631 $_REQUEST['port_id'] = $port_id;
632 unset($_REQUEST['hl_port_id']);
fdc74cf0 633 }
634
635 if(isset($_REQUEST['allports']))
636 {
637 $allports = $_REQUEST['allports'];
638 }
639
640 if(isset($_REQUEST['port_id']))
641 {
642 $port_id = $_REQUEST['port_id'];
643 }
644
645 if(isset($_REQUEST['usemap']))
646 $usemap = $_REQUEST['usemap'];
647
648 if($hl == 'p' && $port_id === NULL)
649 {
650 unset($_GET['hl']);
651 unset($_GET['port_id']);
652 unset($_GET['remote_id']);
653 }
654
655 if($hl == 'o')
656 unset($_GET['remote_id']);
657
658 if(isset($_REQUEST['remote_id']))
659 $remote_id = $_REQUEST['remote_id'];
660
661 /* show all objects */
662 if(isset($_REQUEST['all']))
663 {
664 $object_id = NULL;
665 $port_id = NULL;
666 $hl = NULL;
667 unset($_GET['hl']);
668 }
669
670 if(isset($_REQUEST['cmd']))
671 $command = $_REQUEST['cmd'];
672
673 if(isset($_REQUEST['debug']))
674 $debug = $_REQUEST['debug'];
675 else
676 $debug = False;
677
678 if($debug) echo "-- DEBUG --<br>";
679
680 $gvmap = new linkmgmt_gvmap($object_id, $port_id, $allports, $hl, $remote_id);
681
682 if($debug) echo "-- after gvmap --<br>";
683
684 switch($type) {
685 case 'gif':
686 case 'png':
687 case 'bmp':
688 case 'jpeg':
689 case 'tif':
690 case 'wbmp':
691 $ctype = "image/$type";
692 break;
693 case 'jpg':
694 $ctype = "image/jpeg";
695 break;
696 case 'svg':
697 $ctype = 'image/svg+xml';
698 break;
699 case 'pdf':
700 $ctype = 'application/pdf';
701 break;
702 case 'cmapx':
703 $ctype = 'text/plain';
704 break;
705
706 }
707
708 if($usemap)
709 {
710
711 if($debug) echo "-- usemap --<br>";
712
713 /* add context menu to Ports, Objects, Links, ...
714 */
715
716 echo "<script>
717 function initcontextmenu() {
718 var maps = document.getElementsByTagName('map');
719 for(var i=0;i<maps.length;i++) {
720 var areas = maps[i].childNodes;
721
722 for(j=0;j<areas.length;j++) {
723 if(areas[j].nodeType == 1)
724 {
725 // console.log(areas[j].id);
726 // attr = document.createAttribute('onmouseover','ahh');
727 // areas[j].setAttribute(attr);
728 // areas[j].onmouseover = 'menu(this);';
729
730 areas[j].addEventListener('contextmenu',menu,false);
731 // areas[j].oncontextmenu = 'menu(this, event);';
732 // console.log(areas[j].oncontextmenu);
733 }
734 }
735
736 }
737
738 };
739
740 function menu(event) {
741
742 // console.log('Menu');
743
744 if(!event)
745 event = window.event;
746
747 var parent = event.target;
748
749 // console.log('--' + parent);
750
751 var ids = parent.id.split('-');
752
753 if(ids[0] == 'graph1')
754 return false;
755
756 var object_id = ids[0];
757
758 var url ='?module=ajax&ac=lm_mapinfo&object_id=' + object_id;
759
760 // links ='<li><a href=' + object_id + '>Object</a></li>';
761
762 if(ids[1] != '')
763 {
764 var port_id = ids[1];
765 url += '&port_id=' + port_id;
766 // links += '<li><a href=' + port_id + '>Port</a></li>';
767
768 if(ids[2] != '')
769 {
770 var remote_id = ids[2];
771
772 if(ids[3] != '')
773 {
774 var linktype = ids[3];
775 url += '&remote_id=' + remote_id + '&linktype=' + linktype;
776 // links += '<li><a href=' + port_id + '_' + remote_id + '_' + linktype + '>Unlink</a></li>';
777 }
778 }
779
780 }
781
782
783 var xmlHttp = new XMLHttpRequest();
784 xmlHttp.open('GET', url, false);
785 xmlHttp.send(null);
786
787 var infodiv = document.getElementById('info');
788 infodiv.innerHTML = xmlHttp.responseText;
789
790 // linkdiv = document.getElementById('link');
791 // linkdiv.innerHTML = links;
792
793 var menudiv = document.getElementById('menu');
794 menudiv.style.position = 'absolute';
795 menudiv.style.top = (event.clientY + document.body.scrollTop) + 'px';
796 menudiv.style.left = (event.clientX + document.body.scrollLeft) + 'px';
797 menudiv.style.display = '';
798
799 return false;
800 };
801
802 function mousedown(event) {
803 // console.log('mouse down');
804
805 if(!event)
806 event = window.event;
807
808 if(event.button != 2)
809 return true;
810
811 var menudiv = document.getElementById('menu');
812
813 menudiv.style.display = 'none';
814
815 return false;
816 };
817
818 </script>";
819
820 echo "<body oncontextmenu=\"return false\" onmousedown=\"mousedown(event);\" onload=\"initcontextmenu();\">";
821
822 echo "<div id=\"menu\" style=\"display:none; background-color:#ffff90\">
823 <div id=\"info\"></div>
824 <ul id=\"link\" style=\"list-style-type:none\"></ul>
825 </div>";
826
83b49cf7 827 $data2 = '';
828 $data = $gvmap->fetch($type, $command, 'cmapx', $data2);
829
830 //echo $gvmap->fetch('cmapx', $command);
831 echo $data2;
fdc74cf0 832
833 if($debug) echo "-- after map gvmap --<br>";
834
835 echo "<img src=\"data:$ctype;base64,".
83b49cf7 836 base64_encode($data).
fdc74cf0 837 "\" usemap=#map$object_id />";
838
839 if($debug)
840 {
841 echo "<pre>";
842 echo $gvmap->export();
843 echo "</pre>";
844
845 echo "<pre>".$gvmap->parse()."</pre>";
846 }
847 }
848 else
849 {
850 $gvmap->image($type);
851 }
852
853 exit;
854
855} /* linkmgmt_opmap */
856
857/* ------------------------------------- */
858class linkmgmt_gvmap {
859
860 private $object_id = NULL;
861 private $port_id = NULL;
862 private $remote_id = NULL;
863
864 private $gv = NULL;
865
866 private $ports = array();
867
868 private $allports = false;
869 private $back = NULL;
870
adc1e20b 871 private $alpha = 'ff';
fdc74cf0 872
873 private $errorlevel = NULL;
874
875 function __construct($object_id = NULL, $port_id = NULL, $allports = false, $hl = NULL, $remote_id = NULL) {
876 $this->allports = $allports;
877
4eeb9282 878 $this->object_id = $object_id;
879 $this->port_id = $port_id;
880 $this->remote_id = $remote_id;
fdc74cf0 881
882 $hllabel = "";
883
884 /* suppress strict standards warnings for Image_GraphViz and PHP 5.4.0
885 * output would corrupt image data
886 */
887 $this->errorlevel = error_reporting();
857ba778 888 error_reporting($this->errorlevel & ~E_STRICT);
fdc74cf0 889
890 $graphattr = array(
891 'rankdir' => 'RL',
892 // 'ranksep' => '0',
893 'nodesep' => '0',
894 // 'overlay' => false,
895 );
896
897 unset($_GET['module']);
898
899 $_GET['all'] = 1;
900
901 $graphattr['URL'] = makeHrefProcess($_GET);
902
903 unset($_GET['all']);
904
83b49cf7 905 //$this->gv = new Image_GraphViz(true, $graphattr, "map".$object_id);
906 $this->gv = new lm_Image_GraphViz(true, $graphattr, "map".$object_id);
fdc74cf0 907
fdc74cf0 908 if($object_id === NULL)
909 {
4eeb9282 910 /* all objects ! */
fdc74cf0 911 unset($_GET['all']);
912 $_GET['hl'] = 'o';
913
914 $this->gv->addAttributes(array(
915 'label' => 'Showing all objects'.$hllabel,
916 'labelloc' => 't',
917 )
918 );
919
920 $objects = listCells('object');
921
922 foreach($objects as $obj)
923 $this->_add($this->gv, $obj['id'], NULL);
4eeb9282 924
925 return;
fdc74cf0 926 }
927 else
928 {
929 $object = spotEntity ('object', $object_id);
930
931 $this->gv->addAttributes(array(
932 'label' => "Graph for ${object['dname']}$hllabel",
933 'labelloc' => 't',
934 )
935 );
936
4eeb9282 937 $this->_add($this->gv, $object_id, $port_id);
fdc74cf0 938
939 $children = getEntityRelatives ('children', 'object', $object_id); //'entity_id'
940
941 foreach($children as $child)
942 $this->_add($this->gv, $child['entity_id'], NULL);
943 }
944
4eeb9282 945 switch($hl)
fdc74cf0 946 {
4eeb9282 947 case 'p':
948 case 'port':
949 $hllabel = " (Port highlight)";
950 $this->alpha = '30';
951 $this->_add($this->gv, $object_id, NULL);
952 break;
953 case 'o':
954 case 'object':
955 $hllabel = " (Object highlight)";
956 $this->alpha = '30';
957 /* all objects */
958 $objects = listCells('object');
fdc74cf0 959
4eeb9282 960 foreach($objects as $obj)
961 $this->_add($this->gv, $obj['id'], NULL);
fdc74cf0 962
4eeb9282 963 break;
fdc74cf0 964
965 }
966
4eeb9282 967 /* add hl label */
968 $this->gv->addAttributes(array(
969 'label' => $this->gv->graph['attributes']['label'].$hllabel,
970 ));
971
fdc74cf0 972 // portlist::var_dump_html($this->gv);
973
83b49cf7 974 // $this->gv->saveParsedGraph('/tmp/graph.txt');
fdc74cf0 975 // error_reporting( E_ALL ^ E_NOTICE);
976 } /* __construct */
977
978 function __destruct() {
979 error_reporting($this->errorlevel);
980 }
981
982 // !!!recursiv !!!
983 function _add($gv, $object_id, $port_id = NULL) {
984 global $lm_multilink_port_types;
985
4eeb9282 986 if($port_id !== NULL) {
987 if(isset($this->ports[$port_id])) {
fdc74cf0 988 return;
4eeb9282 989 }
fdc74cf0 990 }
991
2d257b3a 992 if($this->back != 'front' || $port_id === NULL || $this->allports)
993 $front = $this->_getObjectPortsAndLinks($object_id, 'front', $port_id, $this->allports);
994 else
995 $front = array();
996
997 if($this->back != 'back' || $port_id === NULL || $this->allports)
998 $backend = $this->_getObjectPortsAndLinks($object_id, 'back', $port_id, $this->allports);
999 else
1000 $backend = array();
1001
1002 $ports = array_merge($front,$backend);
1003
630a86c8 1004 /* used only for Graphviz ...
1005 * !! numeric ids cause Image_Graphviz problems on nested clusters !!
1006 */
1007 $cluster_id = "c$object_id";
1008
2d257b3a 1009 if(empty($ports))
1010 {
1011 /* needed because of gv_image empty cluster bug (invalid foreach argument) */
1012 $gv->addNode('dummy', array(
1013 // 'label' =>'No Ports found/connected',
1014 'label' =>'',
1015 'fontsize' => 0,
1016 'size' => 0,
1017 'width' => 0,
1018 'height' => 0,
1019 'shape' => 'point',
1020 'style' => 'invis',
1021 ), $cluster_id);
1022
1023 /* show objects without ports */
1024 if($object_id === NULL)
1025 return;
1026 }
1027
1028 if($object_id !== NULL) {
4eeb9282 1029 if(
1030 !isset($gv->graph['clusters'][$cluster_id]) &&
1031 !isset($gv->graph['subgraphs'][$cluster_id])
1032 ) {
fdc74cf0 1033
4eeb9282 1034 $object = spotEntity ('object', $object_id);
1035 // $object['attr'] = getAttrValues($object_id);
fdc74cf0 1036
4eeb9282 1037 $clusterattr = array();
fdc74cf0 1038
4eeb9282 1039 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'color');
1040 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'fontcolor');
fdc74cf0 1041
4eeb9282 1042 if($this->object_id == $object_id)
1043 {
1044 $clusterattr['rank'] = 'source';
fdc74cf0 1045
4eeb9282 1046 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'color');
1047 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'fontcolor');
1048 }
fdc74cf0 1049
4eeb9282 1050 $clustertitle = "${object['dname']}";
1051 $clusterattr['tooltip'] = $clustertitle;
fdc74cf0 1052
4eeb9282 1053 unset($_GET['module']); // makeHrefProcess adds this
1054 unset($_GET['port_id']);
1055 unset($_GET['remote_id']);
1056 $_GET['object_id'] = $object_id;
1057 //$_GET['hl'] = 'o';
fdc74cf0 1058
4eeb9282 1059 $clusterattr['URL'] = makeHrefProcess($_GET);
fdc74cf0 1060
4eeb9282 1061 //has_problems
1062 if($object['has_problems'] != 'no')
1063 {
1064 $clusterattr['style'] = 'filled';
1065 $this->_getcolor('cluster', 'problem', $this->alpha, $clusterattr, 'fillcolor');
1066 }
fdc74cf0 1067
4eeb9282 1068 if(!empty($object['container_name']))
1069 $clustertitle .= "<BR/>${object['container_name']}";
fdc74cf0 1070
4eeb9282 1071 if($object['rack_id'])
1072 {
1073 $rack = spotEntity('rack', $object['rack_id']);
fdc74cf0 1074
4eeb9282 1075 if(!empty($rack['row_name']) || !empty($rack['name']))
1076 $clustertitle .= "<BR/>${rack['row_name']} / ${rack['name']}";
1077 }
1078
1079 $embedin = $object['container_id'];
1080 if(empty($embedin))
1081 $embedin = 'default';
1082 else
1083 {
1084 $embedin = "c$embedin"; /* see cluster_id */
1085
1086 /* add container / cluster if not already exists */
1087 $this->_add($gv, $object['container_id'], NULL);
1088 }
fdc74cf0 1089
2d257b3a 1090 $clusterattr['id'] = "$object_id----"; /* used for js context menu */
fdc74cf0 1091
2d257b3a 1092 $gv->addCluster($cluster_id, $clustertitle, $clusterattr, $embedin);
fdc74cf0 1093
2d257b3a 1094 } /* isset cluster_id */
1095 } /* object_id !== NULL */
fca4e38a 1096
fdc74cf0 1097 foreach($ports as $key => $port) {
1098
1099 $this->back = $port['linktype'];
1100
4eeb9282 1101 if(!isset($this->ports[$port['id']])) {
fdc74cf0 1102
fdc74cf0 1103
4eeb9282 1104 $nodelabel = "${port['name']}";
fdc74cf0 1105
4eeb9282 1106 if($port['iif_id'] != '1' )
1107 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['iif_name']}</FONT>";
fdc74cf0 1108
4eeb9282 1109 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['oif_name']}</FONT>";
fdc74cf0 1110
4eeb9282 1111 $nodeattr = array(
1112 'label' => $nodelabel,
1113 );
fdc74cf0 1114
4eeb9282 1115 $this->_getcolor('port', 'default',$this->alpha, $nodeattr, 'fontcolor');
1116 $this->_getcolor('oif_id', $port['oif_id'],$this->alpha, $nodeattr, 'color');
fdc74cf0 1117
4eeb9282 1118 if($this->port_id == $port['id']) {
1119 $nodeattr['style'] = 'filled';
1120 $nodeattr['fillcolor'] = $this->_getcolor('port', 'current', $this->alpha);
1121 }
fdc74cf0 1122
4eeb9282 1123 if($this->remote_id == $port['id']) {
1124 $nodeattr['style'] = 'filled';
1125 $nodeattr['fillcolor'] = $this->_getcolor('port', 'remote', $this->alpha);
1126 }
1127
1128 $nodeattr['tooltip'] = "${port['name']}";
1129
1130 unset($_GET['module']);
1131 unset($_GET['remote_id']);
1132 $_GET['object_id'] = $port['object_id'];
1133 $_GET['port_id'] = $port['id'];
1134 $_GET['hl'] = 'p';
fdc74cf0 1135
4eeb9282 1136 $nodeattr['URL'] = makeHrefProcess($_GET);
1137 $nodeattr['id'] = "${port['object_id']}-${port['id']}--"; /* for js context menu */
fdc74cf0 1138
4eeb9282 1139 $gv->addNode($port['id'],
fdc74cf0 1140 $nodeattr,
1141 "c${port['object_id']}"); /* see cluster_id */
1142
4eeb9282 1143 $this->ports[$port['id']] = true;
2d257b3a 1144
4eeb9282 1145 } /* isset port */
fdc74cf0 1146
1147 if(!empty($port['remote_id'])) {
1148
bb000ce6 1149 if($this->object_id !== NULL)
1150 $this->_add($gv, $port['remote_object_id'], $port['remote_id']);
fdc74cf0 1151
1152 if(
1153 !isset($gv->graph['edgesFrom'][$port['id']][$port['remote_id']]) &&
1154 !isset($gv->graph['edgesFrom'][$port['remote_id']][$port['id']])
1155 ) {
1156
1157 $linktype = $port['linktype'];
1158
1159 $edgetooltip = $port['object_name'].':'.$port['name'].
1160 ' - '.$port['cableid'].' -> '.
1161 $port['remote_name'].':'.$port['remote_object_name'];
1162
1163 $edgeattr = array(
1164 'fontsize' => 8,
1165 'label' => $port['cableid'],
1166 'tooltip' => $edgetooltip,
1167 'sametail' => $linktype,
1168 'samehead' => $linktype,
1169 );
1170
1171 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'color');
1172 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'fontcolor');
1173
1174 if($linktype == 'back' )
1175 {
1176 $edgeattr['style'] = 'dashed';
1177 $edgeattr['arrowhead'] = 'none';
1178 $edgeattr['arrowtail'] = 'none';
1179
1180 /* multilink ports */
1181 if(in_array($port['oif_id'], $lm_multilink_port_types))
1182 {
1183 $edgeattr['dir'] = 'both';
1184 $edgeattr['arrowtail'] = 'dot';
1185 }
1186
1187 if(in_array($port['remote_oif_id'], $lm_multilink_port_types))
1188 {
1189 $edgeattr['dir'] = 'both';
1190 $edgeattr['arrowhead'] = 'dot';
1191 }
1192 }
1193
1194 if(
1195 ($port['id'] == $this->port_id && $port['remote_id'] == $this->remote_id) ||
1196 ($port['id'] == $this->remote_id && $port['remote_id'] == $this->port_id)
1197 )
1198 {
1199 $this->_getcolor('edge', 'highlight', 'ff', $edgeattr, 'color');
1200 $edgeattr['penwidth'] = 2; /* bold */
1201 }
1202
1203 unset($_GET['module']);
1204 $_GET['object_id'] = $port['object_id'];
1205 $_GET['port_id'] = $port['id'];
1206 $_GET['remote_id'] = $port['remote_id'];
1207
1208 $edgeattr['URL'] = makeHrefProcess($_GET);
1209
1210 $edgeattr['id'] = $port['object_id']."-".$port['id']."-".$port['remote_id']."-".$linktype; /* for js context menu */
1211
1212 $gv->addEdge(array($port['id'] => $port['remote_id']),
1213 $edgeattr,
1214 array(
1215 $port['id'] => $linktype,
1216 $port['remote_id'] => $linktype,
1217 )
1218 );
1219 }
1220 }
1221
1222 }
1223
1224 // portlist::var_dump_html($port);
1225 }
1226
83b49cf7 1227 function fetch($type = 'png', $command = NULL, $format2 = NULL, &$data2 = NULL) {
1228
1229 $tmpdata = $data2;
1230 $ret = $this->gv->fetch($type, $command, $format2, $tmpdata);
1231 if($data2 !== NULL)
1232 $data2 = $tmpdata;
fdc74cf0 1233 return $ret;
1234 }
1235
1236 function image($type = 'png', $command = NULL) {
1237 $this->gv->image($type, $command);
1238 }
1239
1240 function parse() {
1241 return $this->gv->parse();
1242 }
1243
1244 /* should be compatible with getObjectPortsAndLinks from RT database.php */
1245 function _getObjectPortsAndLinks($object_id, $linktype = 'front', $port_id = NULL, $allports = false) {
1246
1247 if($linktype == 'front')
1248 $linktable = 'Link';
1249 else
1250 $linktable = 'LinkBackend';
1251
1252 $qparams = array();
1253
1254 $query = "SELECT
1255 '$linktype' as linktype,
1256 Port.*,
1257 Port.type AS oif_id,
1258 PortInnerInterface.iif_name as iif_name,
1259 Dictionary.dict_value as oif_name,
1260 Object.id as object_id, Object.name as object_name,
2a5cd259 1261 IFNULL(LinkTable_a.cable,LinkTable_b.cable) as cableid,
fdc74cf0 1262 remoteObject.id as remote_object_id, remoteObject.name as remote_object_name,
1263 remotePort.id as remote_id, remotePort.name as remote_name,
1264 remotePort.type AS remote_oif_id,
1265 remotePortInnerInterface.iif_name as remote_iif_name,
1266 remoteDictionary.dict_value as remote_oif_name
1267 FROM Port";
1268
1269 // JOIN
1270 $join = " LEFT JOIN PortInnerInterface on PortInnerInterface.id = Port.iif_id
1271 LEFT JOIN Dictionary on Dictionary.dict_key = Port.type
2a5cd259 1272 LEFT JOIN $linktable as LinkTable_a on Port.id = LinkTable_a.porta
1273 LEFT JOIN $linktable as LinkTable_b on Port.id = LinkTable_b.portb
fdc74cf0 1274 LEFT JOIN Object on Object.id = Port.object_id
2a5cd259 1275 LEFT JOIN Port as remotePort on remotePort.id = IFNULL(LinkTable_a.portb, LinkTable_b.porta)
fdc74cf0 1276 LEFT JOIN Object as remoteObject on remoteObject.id = remotePort.object_id
1277 LEFT JOIN PortInnerInterface as remotePortInnerInterface on remotePortInnerInterface.id = remotePort.iif_id
1278 LEFT JOIN Dictionary as remoteDictionary on remoteDictionary.dict_key = remotePort.type
1279 ";
1280
1281 // WHERE
1282 if($port_id === NULL)
1283 {
1284 $where = " WHERE Object.id = ?";
1285 $qparams[] = $object_id;
1286
09749fb9 1287 if(!$allports) {
fdc74cf0 1288 $where .= " AND remotePort.id is not NULL";
09749fb9 1289
1290 if($linktype != 'front') {
2a5cd259 1291 $join .= "
1292 LEFT JOIN Link as FrontLink_a on Port.id = FrontLink_a.porta
1293 LEFT JOIN Link as FrontLink_b on Port.id = FrontLink_b.portb
1294 LEFT JOIN Link as FrontRemoteLink_a on remotePort.id = FrontRemoteLink_a.porta
1295 LEFT JOIN Link as FrontRemoteLink_b on remotePort.id = FrontRemoteLink_b.portb
09749fb9 1296 ";
2a5cd259 1297 $where .= " AND ( (FrontLink_a.porta is not NULL or FrontLink_b.portb is not NULL )
1298 OR (FrontRemoteLink_a.porta is not NULL or FrontRemoteLink_b.portb is not NULL) )";
09749fb9 1299 }
1300 }
fdc74cf0 1301 }
1302 else
1303 {
1304 // $where = " WHERE Port.id = ? and remotePort.id is not NULL";
1305 $where = " WHERE Port.id = ?";
1306 $qparams[] = $port_id;
1307 }
1308
1309 // ORDER
1310 $order = " ORDER by oif_name, Port.Name";
1311
1312 $query .= $join.$where.$order;
1313
2a5cd259 1314 //echo "$port_id: $query<br><br>";
1315
fdc74cf0 1316 $result = usePreparedSelectBlade ($query, $qparams);
1317
1318 $row = $result->fetchAll(PDO::FETCH_ASSOC);
1319
1320 $result->closeCursor();
1321
1322 return $row;
1323 }
1324
1325 function _getcolor($type = 'object', $key = 'default', $alpha = 'ff', &$array = NULL , $arraykey = 'color') {
1326
1327 $object = array(
1328 'current' => '#ff0000',
1329 );
1330 $port = array(
1331 'current' => '#ffff90',
1332 'remote' => '#ffffD0',
1333 );
1334
1335 $cluster = array(
1336 'current' => '#ff0000',
1337 'problem' => '#ff3030',
1338 );
1339
1340 $edge = array (
1341 'highlight' => '#ff0000',
1342 );
1343
1344 $oif_id = array(
1345 '16' => '#800000', /* AC-in */
1346 '1322' => '#ff4500', /* AC-out */
1347 '24' => '#000080', /* 1000base-t */
1348 );
1349
1350 $defaultcolor = '#000000'; /* black */
1351 $default = true;
1352
1353 if(isset(${$type}[$key]))
1354 {
1355 $default = false;
1356 $color = ${$type}[$key];
1357 }
1358 else
1359 $color = $defaultcolor;
1360
1361
1362 if($alpha != 'ff' || $default == false)
1363 {
1364 $color .= $alpha;
1365
1366 if($array !== NULL)
1367 $array[$arraykey] = $color;
1368 else
1369 return $color;
1370 }
1371 else
1372 return $defaultcolor;
1373
1374 } /* _getcolor */
1375
1376 function dump() {
1377 var_dump($this->gv);
1378 }
1379
1380 function export() {
1381 var_export($this->gv);
1382 }
1383
1384} /* class gvmap */
1385
1386/* -------------------------------------------------- */
1387
9276dc97 1388function linkmgmt_opupdate() {
fdc74cf0 1389
9276dc97
ME
1390 if(!isset($_POST['id']))
1391 exit;
fdc74cf0 1392
9276dc97
ME
1393 $ids = explode('_',$_POST['id'],3);
1394 $retval = strip_tags($_POST['value']);
1395
1396 if(isset($ids[1])) {
1397 if(permitted(NULL, NULL, 'set_link'))
1398 if(isset($ids[2]) && $ids[2] == 'back')
fdc74cf0 1399 linkmgmt_commitUpdatePortLink($ids[0], $ids[1], $retval, TRUE);
9276dc97 1400 else
fdc74cf0 1401 linkmgmt_commitUpdatePortLink($ids[0], $ids[1], $retval);
9276dc97
ME
1402 else
1403 $retval = "Permission denied!";
1404 } else {
1405 if(permitted(NULL, NULL, 'set_reserve_comment'))
1406 commitUpdatePortComment($ids[0], $retval);
1407 else
1408 $retval = "Permission denied!";
1409 }
fdc74cf0 1410
9276dc97
ME
1411 /* return what jeditable should display after edit */
1412 echo $retval;
1413
fdc74cf0 1414 exit;
9276dc97
ME
1415} /* opupdate */
1416
1417/* -------------------------------------------------- */
1418
1419/* similar to commitUpatePortLink in database.php with backend support */
fdc74cf0 1420function linkmgmt_commitUpdatePortLink($port_id1, $port_id2, $cable = NULL, $backend = FALSE) {
9276dc97
ME
1421
1422 /* TODO check permissions */
1423
1424 if($backend)
1425 $table = 'LinkBackend';
1426 else
1427 $table = 'Link';
1428
fdc74cf0 1429 return usePreparedExecuteBlade
9276dc97 1430 (
fdc74cf0 1431 "UPDATE $table SET cable=\"".(mb_strlen ($cable) ? $cable : NULL).
1432 "\" WHERE ( porta = ? and portb = ?) or (portb = ? and porta = ?)",
1433 array (
1434 $port_id1, $port_id2,
1435 $port_id1, $port_id2)
1436 );
1437
9276dc97
ME
1438} /* linkmgmt_commitUpdatePortLink */
1439
1440/* -------------------------------------------------- */
1441
1442function linkmgmt_opunlinkPort() {
1443 $port_id = $_REQUEST['port_id'];
1444 $linktype = $_REQUEST['linktype'];
1445
fdc74cf0 1446 portlist::var_dump_html($_REQUEST);
1447
9276dc97
ME
1448 /* check permissions */
1449 if(!permitted(NULL, NULL, 'set_link')) {
1450 exit;
1451 }
1452
1453 if($linktype == 'back')
fdc74cf0 1454 {
9276dc97 1455 $table = 'LinkBackend';
fdc74cf0 1456 $remote_id = $_REQUEST['remote_id'];
1457
1458 $retval = usePreparedExecuteBlade
1459 (
1460 "DELETE FROM $table WHERE ( porta = ? and portb = ?) or (portb = ? and porta = ?)",
1461 array (
1462 $port_id, $remote_id,
1463 $port_id, $remote_id)
1464 );
1465 }
9276dc97 1466 else
fdc74cf0 1467 {
9276dc97
ME
1468 $table = 'Link';
1469
fdc74cf0 1470 $retval = usePreparedDeleteBlade ($table, array('porta' => $port_id, 'portb' => $port_id), 'OR');
1471 }
1472
9276dc97
ME
1473 if($retval == 0)
1474 echo " Link not found";
1475 else
1476 echo " $retval Links deleted";
1477
fdc74cf0 1478
1479 unset($_GET['module']);
1480 unset($_GET['op']);
1481
1482 header('Location: ?'.http_build_query($_GET));
1483 //header('Location: ?page='.$_REQUEST['page'].'&tab='.$_REQUEST['tab'].'&object_id='.$_REQUEST['object_id']);
9276dc97
ME
1484 exit;
1485} /* opunlinkPort */
1486
1487/* -------------------------------------------------- */
1488
1489function linkmgmt_oplinkPort() {
1490
1491 $linktype = $_REQUEST['linktype'];
1492 $cable = $_REQUEST['cable'];
1493
1494 /* check permissions */
1495 if(!permitted(NULL, NULL, 'set_link')) {
1496 echo("Permission denied!");
1497 return;
1498 }
1499
1500 if(!isset($_REQUEST['link_list'])) {
1501 //portlist::var_dump_html($_REQUEST);
1502 $porta = $_REQUEST['port'];
9276dc97 1503
fdc74cf0 1504 foreach($_REQUEST['remote_ports'] as $portb)
1505 {
1506 $link_list[] = "${porta}_${portb}";
1507
1e6c47e6 1508 /* with no LM_MULTILINK process first value only */
1509 if(!LM_MULTILINK)
fdc74cf0 1510 break;
1511 }
9276dc97
ME
1512 } else
1513 $link_list = $_REQUEST['link_list'];
1514
1515 foreach($link_list as $link){
fdc74cf0 1516
9276dc97
ME
1517 $ids = preg_split('/[^0-9]/',$link);
1518 $porta = $ids[0];;
1519 $portb = $ids[1];
1520
1521 $ret = linkmgmt_linkPorts($porta, $portb, $linktype, $cable);
1522
fdc74cf0 1523 //error_log("$ret - $porta - $portb");
1524 $port_info = getPortInfo ($porta);
1525 $remote_port_info = getPortInfo ($portb);
9276dc97
ME
1526 showSuccess(
1527 sprintf
1528 (
1529 'Port %s %s successfully linked with port %s %s',
1530 formatPortLink ($port_info['id'], $port_info['name'], NULL, NULL),
1531 $linktype,
1532 formatPort ($remote_port_info),
1533 $linktype
1534 )
1535 );
1536 }
1537
9276dc97
ME
1538 addJS (<<<END
1539window.opener.location.reload(true);
1540window.close();
1541END
1542 , TRUE);
1543
1544 return;
1545} /* oplinkPort */
1546
1547/* -------------------------------------------------- */
1548
1549/*
1550 * same as in database.php extendend with linktype
1551 */
1552function linkmgmt_linkPorts ($porta, $portb, $linktype, $cable = NULL)
1553{
1554 if ($porta == $portb)
1555 throw new InvalidArgException ('porta/portb', $porta, "Ports can't be the same");
1556
1557 if($linktype == 'back')
fdc74cf0 1558 {
9276dc97 1559 $table = 'LinkBackend';
1e6c47e6 1560 $multilink = LM_MULTILINK;
fdc74cf0 1561 }
9276dc97 1562 else
fdc74cf0 1563 {
9276dc97 1564 $table = 'Link';
fdc74cf0 1565 $multilink = false;
1566 }
9276dc97
ME
1567
1568 global $dbxlink;
1569 $dbxlink->exec ('LOCK TABLES '.$table.' WRITE');
fdc74cf0 1570
1571 if(!$multilink)
1572 {
1573 $result = usePreparedSelectBlade
1574 (
1575 'SELECT COUNT(*) FROM '.$table.' WHERE porta IN (?,?) OR portb IN (?,?)',
1576 array ($porta, $portb, $porta, $portb)
1577 );
1578
1579 if ($result->fetchColumn () != 0)
1580 {
1581 $dbxlink->exec ('UNLOCK TABLES');
1582 return "$linktype Port ${porta} or ${portb} is already linked";
1583 }
1584 $result->closeCursor ();
1585 }
1586
9276dc97
ME
1587 if ($porta > $portb)
1588 {
1589 $tmp = $porta;
1590 $porta = $portb;
1591 $portb = $tmp;
1592 }
1593 $ret = FALSE !== usePreparedInsertBlade
1594 (
1595 $table,
1596 array
1597 (
1598 'porta' => $porta,
1599 'portb' => $portb,
1600 'cable' => mb_strlen ($cable) ? $cable : ''
1601 )
1602 );
1603 $dbxlink->exec ('UNLOCK TABLES');
1604 $ret = $ret and FALSE !== usePreparedExecuteBlade
1605 (
1606 'UPDATE Port SET reservation_comment=NULL WHERE id IN(?, ?)',
1607 array ($porta, $portb)
1608 );
1609 return $ret ? '' : 'query failed';
1610}
1611
1612/* -------------------------------------------------- */
1613
1614/*
1615 * similar to renderPopupHTML in popup.php
1616 */
1617function linkmgmt_opPortLinkDialog() {
1618// portlist::var_dump_html($_REQUEST);
1619header ('Content-Type: text/html; charset=UTF-8');
1620?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1621<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" style="height: 100%;">
1622<?php
1623
1624 $text = '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; text-align: center;
1625 margin: 5px;">';
1626
fdc74cf0 1627 if(permitted(NULL,NULL,"set_link"))
1628 if (isset ($_REQUEST['do_link'])) {
1629 $text .= getOutputOf ('linkmgmt_oplinkPort');
1630 }
1631 else
1632 if(isset($_REQUEST['byname']))
1633 $text .= getOutputOf ('linkmgmt_renderPopupPortSelectorbyName');
1634 else
1635 $text .= getOutputOf ('linkmgmt_renderPopupPortSelector');
1636 else
1637 $text .= "Permission denied!";
1638
1639 $text .= '</div>';
1640
1641 echo '<head><title>RackTables pop-up</title>';
1642 printPageHeaders();
1643 echo '</head>';
1644 echo '<body style="height: 100%;">' . $text . '</body>';
1645?>
1646</html>
1647<?php
1648 exit;
1649} /* opPortLinkDialog */
1650
1651/* -------------------------------------------------- */
1652
1653/*
1654 * like findSparePorts in popup.php extended with linktype
1655 *
1656 * multilink
1657 *
1658 */
1659function linkmgmt_findSparePorts($port_info, $filter, $linktype, $multilink = false, $objectsonly = false, $byname = false, $portcompat = true, $src_object_id = NULL) {
1660
1661
1662 /*
1663 $linktable ports that will be returned if not linked in this table
1664 $linkinfotable display link for info only show backend links if you want front link a port
1665
1666 front: select ports no front connection and port compat, filter, ...
1667
1668 back:
1669
1670 */
1671
1672 if($linktype == 'back')
1673 {
1674 $linktable = 'LinkBackend';
1675 $linkinfotable = 'Link';
1676 }
1677 else
1678 {
1679 $linktable = 'Link';
1680 $linkinfotable = 'LinkBackend';
1681 }
1682
1683 $qparams = array();
1684 $whereparams = array();
1685
1686 // all ports with no link
1687 /* port:object -> linked port:object */
1688 $query = 'SELECT';
1689 $join = "";
1690 $where = " WHERE";
1691 $group = "";
1692 $order = " ORDER BY";
1693
1694 if($objectsonly)
1695 {
1696 $query .= " remotePort.object_id, CONCAT(IFNULL(remoteObject.name, CONCAT('[',remoteObjectDictionary.dict_value,']')), ' (', count(remotePort.id), ')') as name";
1697 $group .= " GROUP by remoteObject.id";
1698 }
1699 else
1700 if($byname)
1701 {
1702 if($linktype == 'back')
1703 $arrow = '=?=>';
1704 else
1705 $arrow = '-?->';
1706
1707 $query .= ' CONCAT(localPort.id, "_", remotePort.id),
1708 CONCAT(IFNULL(localObject.name, CONCAT("[",localObjectDictionary.dict_value,"]")), " : ", localPort.Name, " '.$arrow.'", remotePort.name, " : ", IFNULL(remoteObject.name,CONCAT("[",remoteObjectDictionary.dict_value,"]")))';
9276dc97 1709 }
fdc74cf0 1710 else
1711 {
1712
1713 if($linktype == 'front')
1714 $arrow = '==';
9276dc97 1715 else
fdc74cf0 1716 $arrow = '--';
9276dc97 1717
fdc74cf0 1718 $query .= " remotePort.id, CONCAT(IFNULL(remoteObject.name, CONCAT('[',remoteObjectDictionary.dict_value,']')), ' : ', remotePort.name,
2a5cd259 1719 IFNULL(CONCAT(' $arrow ', IFNULL(IFNULL(infolnk_a.cable,infolnk_b.cable),''), ' $arrow> ', InfoPort.name, ' : ', IFNULL(InfoObject.name,CONCAT('[',InfoObjectDictionary.dict_value,']'))),'') ) as Text";
fdc74cf0 1720 }
9276dc97 1721
fdc74cf0 1722 $query .= " FROM Port as remotePort";
1723 $join .= " LEFT JOIN Object as remoteObject on remotePort.object_id = remoteObject.id";
1724 $order .= " remoteObject.name";
9276dc97 1725
fdc74cf0 1726 /* object type name */
1727 $join .= " LEFT JOIN Dictionary as remoteObjectDictionary on (remoteObjectDictionary.chapter_id = 1 AND remoteObject.objtype_id = remoteObjectDictionary.dict_key)";
9276dc97 1728
fdc74cf0 1729 if($byname)
1730 {
1731 /* by name */
1732 $join .= " JOIN Port as localPort on remotePort.name = localPort.name";
1733 $where .= " remotePort.object_id <> ? AND localPort.object_id = ?";
1734 $whereparams[] = $src_object_id;
1735 $whereparams[] = $src_object_id;
9276dc97 1736
fdc74cf0 1737 /* own port not linked */
2a5cd259 1738 $join .= " LEFT JOIN $linktable as localLink_a on localPort.id = localLink_a.porta";
1739 $where .= " AND localLink_a.porta is NULL";
1740 $join .= " LEFT JOIN $linktable as localLink_b on localPort.id = localLink_b.portb";
1741 $where .= " AND localLink_b.portb is NULL";
fdc74cf0 1742 $join .= " LEFT JOIN Object as localObject on localObject.id = localPort.object_id";
fdc74cf0 1743
1744 /* object type name */
1745 $join .= " LEFT JOIN Dictionary as localObjectDictionary on (localObject.objtype_id = localObjectDictionary.dict_key AND localObjectDictionary.chapter_id = 1)";
1746 }
1747 else
1748 {
1749 /* exclude current port */
1750 $where .= " remotePort.id <> ?";
1751 $whereparams[] = $port_info['id'];
1752 $order .= " ,remotePort.name";
1753
1754 /* add info to remoteport */
2a5cd259 1755 $join .= " LEFT JOIN $linkinfotable as infolnk_a on remotePort.id = infolnk_a.porta";
1756 $join .= " LEFT JOIN $linkinfotable as infolnk_b on remotePort.id = infolnk_b.portb";
1757 $join .= " LEFT JOIN Port as InfoPort on InfoPort.id = IFNULL(infolnk_a.portb, infolnk_b.porta)";
fdc74cf0 1758 $join .= " LEFT JOIN Object as InfoObject on InfoObject.id = InfoPort.object_id";
1759
1760 /* object type name */
1761 $join .= " LEFT JOIN Dictionary as InfoObjectDictionary on (InfoObject.objtype_id = InfoObjectDictionary.dict_key AND InfoObjectDictionary.chapter_id = 1)";
1762 }
1763
1764 /* only ports which are not linked already */
2a5cd259 1765 $join .= " LEFT JOIN $linktable as lnk_a on remotePort.id = lnk_a.porta";
1766 $where .= " AND lnk_a.porta is NULL";
1767 $join .= " LEFT JOIN $linktable as lnk_b on remotePort.id = lnk_b.portb";
1768 $where .= " AND lnk_b.portb is NULL";
1769
fdc74cf0 1770 if($portcompat)
1771 {
1772 /* port compat */
1773 $join .= ' INNER JOIN PortInnerInterface pii ON remotePort.iif_id = pii.id
1774 INNER JOIN Dictionary d ON d.dict_key = remotePort.type';
1775 // porttype filter (non-strict match)
1776 $join .= ' INNER JOIN (
1777 SELECT Port.id FROM Port
1778 INNER JOIN
1779 (
1780 SELECT DISTINCT pic2.iif_id
1781 FROM PortInterfaceCompat pic2
1782 INNER JOIN PortCompat pc ON pc.type2 = pic2.oif_id';
1783
1784 if ($port_info['iif_id'] != 1)
1785 {
1786 $join .= " INNER JOIN PortInterfaceCompat pic ON pic.oif_id = pc.type1 WHERE pic.iif_id = ?";
1787 $qparams[] = $port_info['iif_id'];
1788 }
1789 else
1790 {
1791 $join .= " WHERE pc.type1 = ?";
1792 $qparams[] = $port_info['oif_id'];
1793 }
1794 $join .= " AND pic2.iif_id <> 1
1795 ) AS sub1 USING (iif_id)
1796 UNION
1797 SELECT Port.id
1798 FROM Port
1799 INNER JOIN PortCompat ON type1 = type
1800 WHERE iif_id = 1 and type2 = ?
1801 ) AS sub2 ON sub2.id = remotePort.id";
1802 $qparams[] = $port_info['oif_id'];
1803 }
9276dc97 1804
fdc74cf0 1805
1806 $qparams = array_merge($qparams, $whereparams);
9276dc97
ME
1807
1808 // rack filter
1809 if (! empty ($filter['racks']))
1810 {
fdc74cf0 1811 $where .= ' AND remotePort.object_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (' .
9276dc97
ME
1812 questionMarks (count ($filter['racks'])) . ')) ';
1813 $qparams = array_merge ($qparams, $filter['racks']);
1814 }
1815
fdc74cf0 1816 // object_id filter
1817 if (! empty ($filter['object_id']))
1818 {
1819 $where .= ' AND remoteObject.id = ?';
1820 $qparams[] = $filter['object_id'];
1821 }
1822 else
9276dc97
ME
1823 // objectname filter
1824 if (! empty ($filter['objects']))
1825 {
fdc74cf0 1826 $where .= ' AND remoteObject.name like ? ';
9276dc97
ME
1827 $qparams[] = '%' . $filter['objects'] . '%';
1828 }
fdc74cf0 1829
9276dc97
ME
1830 // portname filter
1831 if (! empty ($filter['ports']))
1832 {
fdc74cf0 1833 $where .= ' AND remotePort.name LIKE ? ';
9276dc97
ME
1834 $qparams[] = '%' . $filter['ports'] . '%';
1835 }
9276dc97 1836
fdc74cf0 1837 $query .= $join.$where.$group.$order;
9276dc97
ME
1838
1839 $result = usePreparedSelectBlade ($query, $qparams);
1840
1841 $row = $result->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_COLUMN);
1842
fdc74cf0 1843 $result->closeCursor();
1844
1845 /* [id] => displaystring */
9276dc97 1846 return $row;
fdc74cf0 1847
9276dc97
ME
1848} /* findSparePorts */
1849
1850/* -------------------------------------------------- */
1851
1852/*
fdc74cf0 1853 * like renderPopupPortSelector in popup.php extenden with linktype
9276dc97 1854 */
fdc74cf0 1855function linkmgmt_renderPopupPortSelector()
1856{
1857 require_once 'inc/popup.php'; /* getProximateRacks */
9276dc97 1858
fdc74cf0 1859 global $lm_multilink_port_types;
9276dc97 1860
fdc74cf0 1861 assertUIntArg ('port');
1862 $port_id = $_REQUEST['port'];
9276dc97 1863
fdc74cf0 1864 $showlinktypeswitch = false;
9276dc97 1865
fdc74cf0 1866 if(isset($_GET['linktype']))
1867 $linktype = $_GET['linktype'];
1868 else
1869 $linktype = 'front';
9276dc97 1870
fdc74cf0 1871 if($linktype == 'both')
1872 {
9276dc97 1873
fdc74cf0 1874 /*
1875 * use POST front/back_view to set linktype
1876 * and show linktype switch button
1877 */
9276dc97 1878
fdc74cf0 1879 $showlinktypeswitch = true;
1880
1881 if(isset($_POST['front_view']))
1882 $linktype = 'front';
1883 else
1884 if(isset($_POST['back_view']))
1885 $linktype = 'back';
1886 else
1887 $linktype = 'front';
1888 }
1889
1890// portlist::var_dump_html($_POST);
1891
1892 $portcompat = true;
1893
1894 if($linktype == 'back')
1895 {
1896 if(isset($_POST['portcompat']))
1897 $portcompat = $_POST['portcompat'];
1898 }
9276dc97 1899
9276dc97
ME
1900 $object_id = $_REQUEST['object_id'];
1901 $port_info = getPortInfo ($port_id);
fdc74cf0 1902
1e6c47e6 1903 $multilink = LM_MULTILINK && $linktype == 'back' && in_array($port_info['oif_id'], $lm_multilink_port_types);
fdc74cf0 1904
1905 if(isset ($_REQUEST['in_rack']))
1906 $in_rack = $_REQUEST['in_rack'] != 'off';
1907 else
1908 $in_rack = true;
9276dc97
ME
1909
1910// portlist::var_dump_html($port_info);
fdc74cf0 1911// portlist::var_dump_html($_GET);
1912// portlist::var_dump_html($_POST);
9276dc97
ME
1913
1914 // fill port filter structure
1915 $filter = array
1916 (
1917 'racks' => array(),
1918 'objects' => '',
fdc74cf0 1919 'object_id' => '',
9276dc97
ME
1920 'ports' => '',
1921 );
fdc74cf0 1922
1923 $remote_object = NULL;
1924 if(isset($_REQUEST['remote_object']))
1925 {
1926 $remote_object = $_REQUEST['remote_object'];
1927
1928 if($remote_object != 'NULL')
1929 $filter['object_id'] = $remote_object;
1930 }
1931
9276dc97
ME
1932 if (isset ($_REQUEST['filter-obj']))
1933 $filter['objects'] = $_REQUEST['filter-obj'];
1934 if (isset ($_REQUEST['filter-port']))
1935 $filter['ports'] = $_REQUEST['filter-port'];
1936 if ($in_rack)
1937 {
1938 $object = spotEntity ('object', $port_info['object_id']);
1939 if ($object['rack_id'])
1940 $filter['racks'] = getProximateRacks ($object['rack_id'], getConfigVar ('PROXIMITY_RANGE'));
1941 }
fdc74cf0 1942
1943 $objectlist = array('NULL' => '- Show All -');
1944 $objectlist = $objectlist + linkmgmt_findSparePorts($port_info, $filter, $linktype, $multilink, true, false, $portcompat);
1945
1946 $spare_ports = linkmgmt_findSparePorts ($port_info, $filter, $linktype, $multilink, false, false, $portcompat);
1947
1948 $maxsize = getConfigVar('MAXSELSIZE');
1949 $objectcount = count($objectlist);
1950
1951 if($linktype == 'back')
1952 $notlinktype = 'front';
1953 else
1954 $notlinktype = 'back';
9276dc97
ME
1955
1956 // display search form
1957 echo 'Link '.$linktype.' of ' . formatPort ($port_info) . ' to...';
1958 echo '<form method=POST>';
1959 startPortlet ($linktype.' Port list filter');
1960 // echo '<input type=hidden name="module" value="popup">';
1961 // echo '<input type=hidden name="helper" value="portlist">';
1962
1963 echo '<input type=hidden name="port" value="' . $port_id . '">';
fdc74cf0 1964 echo '<table><tr><td valign="top"><table><tr><td>';
1965
1966 echo '<table align="center"><tr>';
1967
1968// echo '<td nowrap="nowrap"><input type="hidden" name="linktype" value="front" /><input type="checkbox" name="linktype" value="back"'.($linktype == 'back' ? ' checked="checked"' : '' ).'>link backend</input></td></tr><tr>';
9276dc97
ME
1969 echo '<td class="tdleft"><label>Object name:<br><input type=text size=8 name="filter-obj" value="' . htmlspecialchars ($filter['objects'], ENT_QUOTES) . '"></label></td>';
1970 echo '<td class="tdleft"><label>Port name:<br><input type=text size=6 name="filter-port" value="' . htmlspecialchars ($filter['ports'], ENT_QUOTES) . '"></label></td>';
fdc74cf0 1971 echo '<td class="tdleft" valign="bottom"><input type="hidden" name="in_rack" value="off" /><label><input type=checkbox value="1" name="in_rack"'.($in_rack ? ' checked="checked"' : '').' onchange="this.form.submit();">Nearest racks</label></td>';
9276dc97 1972 echo '</tr></table>';
fdc74cf0 1973
1974 echo '</td></tr><tr><td>';
1975 echo 'Object name (count ports)<br>';
1976 echo getSelect ($objectlist, array ('name' => 'remote_object',
1977 'size' => ($objectcount <= $maxsize ? $objectcount : $maxsize)),
1978 $remote_object, FALSE);
1979
1980 echo '</td></tr></table></td>';
1981 echo '<td valign="top"><table><tr><td><input type=submit value="update objects / ports"></td></tr>';
1982
1983 if($showlinktypeswitch)
1984 echo '<tr height=150px><td><input type=submit value="Switch to '.$notlinktype.' view" name="'.$notlinktype.'_view"></tr></td>';
1985
1986 if($linktype == 'back')
1987 {
1988 echo '<input type="hidden" name="portcompat" value="0">';
1989 echo '<tr height=150px><td><input type=checkbox onchange="this.form.submit();" name="portcompat"'.( $portcompat ? 'checked="checked" ' : '' ).'value="1">Port Compatibility</input></tr></td>';
1990 echo '<input type="hidden" name="back_view">';
1991 }
1992
1993 echo '</table></td>';
1994
9276dc97 1995 finishPortlet();
fdc74cf0 1996 echo '</td><td>';
9276dc97
ME
1997
1998 // display results
1999 startPortlet ('Compatible spare '.$linktype.' ports');
fdc74cf0 2000 echo "spare $linktype Object:Port -- $notlinktype cableID --> $notlinktype Port:Object<br>";
2001
2002 if($multilink)
2003 echo "Multilink<br>";
9276dc97
ME
2004
2005 if (empty ($spare_ports))
2006 echo '(nothing found)';
2007 else
2008 {
fdc74cf0 2009 $linkcount = count($spare_ports);
2010
2011 $options = array(
2012 'name' => 'remote_ports[]',
2013 'size' => getConfigVar ('MAXSELSIZE'),
2014 'size' => ($linkcount <= $maxsize ? $linkcount : $maxsize),
2015 );
2016
2017 if($multilink)
2018 $options['multiple'] = 'multiple';
2019
2020 echo getSelect ($spare_ports, $options, NULL, FALSE);
2021
9276dc97
ME
2022 echo "<p>$linktype Cable ID: <input type=text id=cable name=cable>";
2023 echo "<p><input type='submit' value='Link $linktype' name='do_link'>";
2024 }
2025 finishPortlet();
fdc74cf0 2026 echo '</td></tr></table>';
9276dc97
ME
2027 echo '</form>';
2028
2029} /* linkmgmt_renderPopUpPortSelector */
2030
2031/* -------------------------------------------------- */
2032
2033/*
2034 * similar to renderPopupPortSelector but let you select the destination object
2035 * and displays possible backend links with ports of the same name
2036 */
2037function linkmgmt_renderPopupPortSelectorbyName()
2038{
2039 $linktype = $_REQUEST['linktype'];
2040 $object_id = $_REQUEST['object_id'];
2041
2042 $object = spotEntity ('object', $object_id);
2043
fdc74cf0 2044 $objectlist = linkmgmt_findSparePorts(NULL, NULL, $linktype, false, true, TRUE, false, $object_id);
2045
2046 $objectname = $object['dname'];
2047
2048 /* remove self from list */
2049 unset($objectlist[$object_id]);
2050
9276dc97
ME
2051 if(isset($_REQUEST['remote_object']))
2052 $remote_object = $_REQUEST['remote_object'];
2053 else
fdc74cf0 2054 {
2055 /* choose first object from list */
2056 $keys = array_keys($objectlist);
2057
2058 if(isset($keys[0]))
2059 $remote_object = $keys[0];
2060 else
2061 $remote_object = NULL;
2062 }
9276dc97
ME
2063
2064 if($remote_object)
fdc74cf0 2065 {
2066 $filter['object_id'] = $remote_object;
2067 $link_list = linkmgmt_findSparePorts(NULL, $filter, $linktype, false, false, TRUE, false, $object_id);
2068 }
2069 else
2070 $link_list = linkmgmt_findSparePorts(NULL, NULL, $linktype, false, false, TRUE, false, $object_id);
9276dc97
ME
2071
2072 // display search form
fdc74cf0 2073 echo 'Link '.$linktype.' of ' . formatPortLink($object_id, $objectname, NULL, NULL) . ' Ports by Name to...';
9276dc97 2074 echo '<form method=POST>';
9276dc97 2075
fdc74cf0 2076 echo '<table align="center"><tr><td>';
2077 startPortlet ('Object list');
9276dc97 2078
fdc74cf0 2079 $maxsize = getConfigVar('MAXSELSIZE');
2080 $objectcount = count($objectlist);
9276dc97 2081
fdc74cf0 2082 echo 'Object name (count ports)<br>';
2083 echo getSelect ($objectlist, array ('name' => 'remote_object',
2084 'size' => ($objectcount <= $maxsize ? $objectcount : $maxsize)),
2085 $remote_object, FALSE);
2086 echo '</td><td><input type=submit value="show '.$linktype.' ports>"></td>';
9276dc97
ME
2087 finishPortlet();
2088
fdc74cf0 2089 echo '<td>';
9276dc97
ME
2090 // display results
2091 startPortlet ('Possible Backend Link List');
2092 echo "Select links to create:<br>";
2093 if (empty ($link_list))
2094 echo '(nothing found)';
2095 else
2096 {
fdc74cf0 2097 $linkcount = count($link_list);
2098
2099 $options = array(
2100 'name' => 'link_list[]',
2101 'size' => ($linkcount <= $maxsize ? $linkcount : $maxsize),
2102 'multiple' => 'multiple',
2103 );
2104
2105 echo getSelect ($link_list,$options, NULL, FALSE);
2106
9276dc97
ME
2107 echo "<p>$linktype Cable ID: <input type=text id=cable name=cable>";
2108 echo "<p><input type='submit' value='Link $linktype' name='do_link'>";
2109 }
2110 finishPortlet();
fdc74cf0 2111 echo '</td></tr></table>';
9276dc97
ME
2112 echo '</form>';
2113
fdc74cf0 2114} /* linkmgmt_renderPopUpPortSelectorByName */
9276dc97 2115
fdc74cf0 2116/* ------------------------------------------------ */
9276dc97
ME
2117
2118function linkmgmt_tabhandler($object_id) {
2119 global $lm_cache;
2120
2121 $target = makeHrefProcess(portlist::urlparams('op','update'));
2122
fdc74cf0 2123 addJS('js/jquery.jeditable.mini.js');
9276dc97 2124
fdc74cf0 2125 /* TODO if (permitted (NULL, 'ports', 'set_reserve_comment')) */
9276dc97
ME
2126 /* TODO Link / unlink permissions */
2127
2128 $lm_cache['allowcomment'] = permitted(NULL, NULL, 'set_reserve_comment'); /* RackCode {$op_set_reserve_comment} */
2129 $lm_cache['allowlink'] = permitted(NULL, NULL, 'set_link'); /* RackCode {$op_set_link} */
fdc74cf0 2130
9276dc97
ME
2131 //portlist::var_dump_html($lm_cache);
2132
2133 /* init jeditable fields/tags */
2134 if($lm_cache['allowcomment'])
2135 addJS('$(document).ready(function() { $(".editcmt").editable("'.$target.'",{placeholder : "add comment"}); });' , TRUE);
2136
2137 if($lm_cache['allowlink'])
2138 addJS('$(document).ready(function() { $(".editcable").editable("'.$target.'",{placeholder : "edit cableID"}); });' , TRUE);
2139
9276dc97
ME
2140 /* linkmgmt for current object */
2141 linkmgmt_renderObjectLinks($object_id);
2142
2143 /* linkmgmt for every child */
2144 //$parents = getEntityRelatives ('parents', 'object', $object_id);
2145 $children = getEntityRelatives ('children', 'object', $object_id); //'entity_id'
2146
2147 //portlist::var_dump_html($children);
2148
2149 foreach($children as $child) {
2150 echo '<h1>Links for Child: '.$child['name'].'</h1>';
2151 linkmgmt_renderObjectLinks($child['entity_id']);
2152 }
2153
9276dc97
ME
2154 return;
2155
2156} /* tabhandler */
2157
2158/* -------------------------------------------------- */
2159function linkmgmt_renderObjectLinks($object_id) {
2160
fdc74cf0 2161 $object = spotEntity ('object', $object_id);
9276dc97
ME
2162 $object['attr'] = getAttrValues($object_id);
2163
2164 /* get ports */
2165 /* calls getObjectPortsAndLinks */
2166 amplifyCell ($object);
2167
2168 //$ports = getObjectPortsAndLinks($object_id);
2169 $ports = $object['ports'];
2170
fdc74cf0 2171 /* reindex array so key starts at 0 */
2172 $ports = array_values($ports);
9276dc97
ME
2173
2174 /* URL param handling */
2175 if(isset($_GET['allports'])) {
2176 $allports = $_GET['allports'];
2177 } else
2178 $allports = FALSE;
2179
2180 if(isset($_GET['allback'])) {
2181 $allback = $_GET['allback'];
2182 } else
2183 $allback = FALSE;
2184
2185 echo '<table><tr>';
2186
2187 if($allports) {
fdc74cf0 2188
9276dc97
ME
2189 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allports','0','0'))
2190 .'">Hide Ports without link</a></td>';
2191 } else
2192 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allports','1','0'))
2193 .'">Show All Ports</a></td>';
2194
2195 echo '<td width=200><span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
fdc74cf0 2196 array('op' => 'PortLinkDialog','linktype' => 'back','byname' => '1'))).'","name","height=700,width=800,scrollbars=yes");><a>Link Object Ports by Name</a></span></td>';
9276dc97
ME
2197
2198 if($allback) {
fdc74cf0 2199
9276dc97
ME
2200 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allback','0','0'))
2201 .'">Collapse Backend Links on same Object</a></td>';
2202 } else
2203 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allback','1','0'))
2204 .'">Expand Backend Links on same Object</a></td>';
2205
fdc74cf0 2206 /* Graphviz map */
2207 echo '<td width=100><span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2208 array('op' => 'map','usemap' => 1))).'","name","height=800,width=800,scrollbars=yes");><a>Object Map</a></span></td>';
2209
9276dc97
ME
2210 /* Help */
2211 echo '<td width=200><span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2212 array('op' => 'Help'))).'","name","height=400,width=500");><a>Help</a></span></td>';
2213
2214 if(isset($_REQUEST['hl_port_id']))
2215 $hl_port_id = $_REQUEST['hl_port_id'];
2216 else
fdc74cf0 2217 $hl_port_id = NULL;
9276dc97
ME
2218
2219 echo '</tr></table>';
2220
fdc74cf0 2221
2222 echo '<br><br><table id=renderobjectlinks0>';
9276dc97
ME
2223
2224 /* switch display order depending on backend links */
2225 $first = portlist::hasbackend($object_id);
2226
fdc74cf0 2227 $rowcount = 0;
2228 foreach($ports as $key => $port) {
9276dc97
ME
2229
2230 $plist = new portlist($port, $object_id, $allports, $allback);
2231
fdc74cf0 2232 //echo "<td><img src=\"index.php?module=redirect&page=object&tab=linkmgmt&op=map&object_id=$object_id&port_id=${port['id']}&allports=$allports\" ></td>";
2233
2234 if($plist->printportlistrow($first, $hl_port_id, ($rowcount % 2 ? portlist::ALTERNATE_ROW_BGCOLOR : "#ffffff")) )
2235 $rowcount++;
2236
9276dc97
ME
2237 }
2238
2239 echo "</table>";
2240
2241} /* renderObjectLinks */
2242
9276dc97
ME
2243/* -------------------------------------------------- */
2244
2245/*
2246 * Portlist class
fdc74cf0 2247 * gets all linked ports to spezified port
9276dc97
ME
2248 * and prints this list as table row
2249 *
2250 */
2251class portlist {
2252
2253 public $list = array();
2254
2255 private $object_id;
2256 private $port_id;
2257 private $port;
2258
2259 private $first_id;
2260 private $front_count;
2261
2262 private $last_id;
2263 private $back_count;
2264
2265 private $count = 0;
2266
2267 private $allback = FALSE;
2268
1e6c47e6 2269 private $multilink = LM_MULTILINK;
fdc74cf0 2270
9276dc97
ME
2271 const B2B_LINK_BGCOLOR = '#d8d8d8';
2272 const CURRENT_PORT_BGCOLOR = '#ffff99';
2273 const CURRENT_OBJECT_BGCOLOR = '#ff0000';
2274 const HL_PORT_BGCOLOR = '#00ff00';
fdc74cf0 2275 const ALTERNATE_ROW_BGCOLOR = '#f0f0f0';
9276dc97
ME
2276
2277 /* Possible LOOP detected after count links print only */
2278 const MAX_LOOP_COUNT = 13;
2279
2280 private $loopcount;
2281
fdc74cf0 2282 private $gv = NULL;
2283
9276dc97 2284 function __construct($port, $object_id, $allports = FALSE, $allback = FALSE) {
9276dc97
ME
2285
2286 $this->object_id = $object_id;
2287
2288 $this->port = $port;
2289
2290 $port_id = $port['id'];
fdc74cf0 2291
9276dc97 2292 $this->port_id = $port_id;
fdc74cf0 2293
9276dc97
ME
2294 $this->first_id = $port_id;
2295 $this->last_id = $port_id;
2296
2297 $this->allback = $allback;
2298
fdc74cf0 2299 $this->_getportlists($port_id);
9276dc97
ME
2300
2301 if(!$allports)
2302 if($this->count == 0 || ( ($this->count == 1) && (!empty($this->list[$port_id]['back'])) ) ) {
2303 $this->list = array();
2304 $this->first_id = NULL;
2305 }
2306
fdc74cf0 2307 // $this->var_dump_html($this->list);
9276dc97
ME
2308
2309 } /* __construct */
2310
2311
2312 /*
fdc74cf0 2313 * get front and back portlist
2314 */
2315 function _getportlists($port_id) {
2316
2317 /* Front Port */
2318 $this->count = 0;
2319 $this->_getportlist($this->_getportdata($port_id),FALSE, TRUE);
2320 $this->front_count = $this->count;
2321
2322 /* Back Port */
2323 $this->count = 0;
2324 $this->_getportlist($this->_getportdata($port_id), TRUE, FALSE);
2325 $this->back_count = $this->count;
2326
2327 $this->count = $this->front_count + $this->back_count;
2328 }
2329
2330 /*
2331 * gets front or back port of src_port
2332 * and adds it to the list
9276dc97
ME
2333 */
2334 /* !!! recursive */
2335 function _getportlist(&$src_port, $back = FALSE, $first = TRUE) {
9276dc97 2336
fdc74cf0 2337 $src_port_id = $src_port['id'];
2338
2339 if($back)
9276dc97
ME
2340 $linktype = 'back';
2341 else
2342 $linktype = 'front';
fdc74cf0 2343
9276dc97
ME
2344 if(!empty($src_port[$linktype])) {
2345
fdc74cf0 2346 /* multilink */
2347 foreach($src_port[$linktype] as &$src_link) {
2348 $dst_port_id = $src_link['id'];
9276dc97 2349
fdc74cf0 2350 if(!$this->_loopdetect($src_port,$dst_port_id,$src_link,$linktype)) {
2351 //error_log("no loop $linktype>".$dst_port_id);
2352 $this->count++;
2353 $this->_getportlist($this->_getportdata($dst_port_id), !$back, $first);
2354 }
9276dc97 2355 }
fdc74cf0 2356
9276dc97
ME
2357 } else {
2358 if($first) {
fdc74cf0 2359 $this->first_id = $src_port_id;
9276dc97
ME
2360 // $this->front_count = $this->count; /* doesn't work on loops */
2361 } else {
fdc74cf0 2362 $this->last_id = $src_port_id;
9276dc97
ME
2363 // $this->back_count = $this->count; /* doesn't work on loops */
2364 }
2365
2366 }
2367
fdc74cf0 2368 } /* _getportlist */
9276dc97
ME
2369
2370 /*
2371 * as name suggested
2372 */
fdc74cf0 2373 function _loopdetect(&$src_port, $dst_port_id, &$src_link, $linktype) {
9276dc97 2374
fdc74cf0 2375 /* TODO multilink*/
9276dc97
ME
2376 if(array_key_exists($dst_port_id, $this->list)) {
2377
fdc74cf0 2378 // $dst_port = $this->list[$dst_port_id];
9276dc97 2379
fdc74cf0 2380 //echo "LOOP :".$src_port['id']."-->".$dst_port_id;
9276dc97 2381
fdc74cf0 2382 /* print loop at least once */
2383 if($dst_port_id == $this->port_id)
2384 {
2385 $src_link['loop'] = $dst_port_id;
2386 return TRUE;
2387 }
9276dc97 2388
9276dc97 2389 }
fdc74cf0 2390
2391 return FALSE;
2392
9276dc97 2393 } /* _loopdetect */
fdc74cf0 2394
9276dc97
ME
2395 /*
2396 * get all data for one port
2397 * name, object, front link, back link
2398 */
2399 function &_getportdata($port_id) {
2400 /* sql bitwise xor: porta ^ portb */
2401 //select cable, ((porta ^ portb) ^ 4556) as port from Link where (4556 in (porta, portb));
2402
2403 //error_log("_getportdata $port_id");
fdc74cf0 2404
9276dc97
ME
2405 /* TODO single sql ? */
2406
fdc74cf0 2407 $result = usePreparedSelectBlade
2408 (
2409 'SELECT Port.id, Port.name, Port.label, Port.type, Port.l2address, Port.object_id, Port.reservation_comment,
2410 Object.name as "obj_name"
9276dc97 2411 from Port
fdc74cf0 2412 join Object on Object.id = Port.object_id
9276dc97
ME
2413 where Port.id = ?',
2414 array($port_id)
fdc74cf0 2415 );
2416 $datarow = $result->fetchAll(PDO::FETCH_ASSOC);
9276dc97 2417
fdc74cf0 2418 $result->closeCursor();
2419 unset($result);
2420
2421 $result = usePreparedSelectBlade
2422 (
2423 'SELECT Port.id, Link.cable, Port.name, Port.label, Port.type, Port.l2address, Port.object_id,
9276dc97
ME
2424 CONCAT(Link.porta,"_",Link.portb) as link_id from Link
2425 join Port
2a5cd259 2426 where (? = Link.porta or ? = Link.portb) and ((Link.porta ^ Link.portb) ^ ? ) = Port.id',
2427 array($port_id, $port_id, $port_id)
fdc74cf0 2428 );
2429 $frontrow = $result->fetchAll(PDO::FETCH_ASSOC);
2430
2431 $result->closeCursor();
2432 unset($result);
9276dc97 2433
fdc74cf0 2434 $result = usePreparedSelectBlade
2435 (
2436 'SELECT Port.id, LinkBackend.cable, Port.name, Port.label, Port.type, Port.l2address, Port.object_id,
9276dc97
ME
2437 CONCAT(LinkBackend.porta,"_",LinkBackend.portb,"_back") as link_id from LinkBackend
2438 join Port
2a5cd259 2439 where (? = LinkBackend.porta or ? = LinkBackend.portb) and ((LinkBackend.porta ^ LinkBackend.portb) ^ ? ) = Port.id',
2440 array($port_id, $port_id, $port_id)
fdc74cf0 2441 );
2442 $backrow = $result->fetchAll(PDO::FETCH_ASSOC);
2443
2444 $result->closeCursor();
2445 unset($result);
9276dc97
ME
2446
2447 $retval = $datarow[0];
2448
2449 if(!empty($frontrow))
fdc74cf0 2450 $retval['front']= $frontrow;
9276dc97
ME
2451 else
2452 $retval['front'] = array();
2453
2454 if(!empty($backrow))
fdc74cf0 2455 $retval['back'] = $backrow;
9276dc97
ME
2456 else
2457 $retval['back'] = array();
2458
2459 // $this->var_dump_html($retval);
2460
2461 /* return reference */
2462 return ($this->list[$port_id] = &$retval);
2463
2464 } /* _getportdata */
2465
2466 /*
2467 */
fdc74cf0 2468 function printport(&$port, $multilink = false) {
2469
9276dc97 2470 /* set bgcolor for current port */
fdc74cf0 2471 if($port['id'] == $this->port_id) {
9276dc97
ME
2472 $bgcolor = 'bgcolor='.self::CURRENT_PORT_BGCOLOR;
2473 $idtag = ' id='.$port['id'];
2474 } else {
2475 $bgcolor = '';
2476 $idtag = '';
2477 }
2478
2479 $mac = trim(preg_replace('/(..)/','$1:',$port['l2address']),':');
2480
fdc74cf0 2481 $title = "Label: ${port['label']}\nMAC: $mac\nTypeID: ${port['type']}\nPortID: ${port['id']}";
9276dc97
ME
2482
2483 echo '<td'.$idtag.' align=center '.$bgcolor.' title="'.$title.'"><pre>[<a href="'
2484 .makeHref(array('page'=>'object', 'tab' => 'linkmgmt', 'object_id' => $port['object_id'], 'hl_port_id' => $port['id']))
2485 .'#'.$port['id']
fdc74cf0 2486 .'">'.$port['name'].'</a>]</pre>'.($multilink ? $this->_getlinkportsymbol($port['id'], 'back') : '' ).'</td>';
9276dc97
ME
2487
2488 } /* printport */
2489
2490 /*
2491 */
2492 function printcomment(&$port) {
fdc74cf0 2493
9276dc97
ME
2494 if(!empty($port['reservation_comment'])) {
2495 $prefix = '<b>Reserved: </b>';
2496 } else
2497 $prefix = '';
2498
2499 echo '<td>'.$prefix.'<i><a class="editcmt" id='.$port['id'].'>'.$port['reservation_comment'].'</a></i></td>';
2500
2501 } /* printComment */
2502
2503
2504 /*
2505 */
2506 function printobject($object_id, $object_name) {
2507 if($object_id == $this->object_id) {
2508 $color='color: '.self::CURRENT_OBJECT_BGCOLOR;
2509 } else {
2510 $color='';
2511 }
2512
2513 echo '<td><table align=center cellpadding=5 cellspacing=0 border=1><tr><td align=center><a style="font-weight:bold;'
2514 .$color.'" href="'.makeHref(array('page'=>'object', 'tab' => 'linkmgmt', 'object_id' => $object_id))
2515 .'"><pre>'.$object_name.'</pre></a><pre>'.$this->_getRackInfo($object_id, 'font-size:80%')
2516 .'</pre></td></tr></table></td>';
2517
2518 } /* printobject */
2519
2520 /*
2521 */
fdc74cf0 2522 function printlink($src_port_id, &$dst_link, $linktype) {
9276dc97
ME
2523
2524 if($linktype == 'back')
2525 $arrow = '====>';
2526 else
2527 $arrow = '---->';
2528
2529 /* link */
2530 echo '<td align=center>';
2531
fdc74cf0 2532 echo '<pre><a class="editcable" id='.$dst_link['link_id'].'>'.$dst_link['cable']
9276dc97 2533 ."</a></pre><pre>$arrow</pre>"
fdc74cf0 2534 .$this->_printUnLinkPort($src_port_id, $dst_link, $linktype);
9276dc97
ME
2535
2536 echo '</td>';
2537 } /* printlink */
2538
2539 /*
2540 * print cableID dst_port:dst_object
2541 */
fdc74cf0 2542 function _printportlink($src_port_id, $dst_port_id, &$dst_link, $back = FALSE) {
9276dc97 2543
fdc74cf0 2544 global $lm_multilink_port_types;
9276dc97 2545
1e6c47e6 2546 $multilink = LM_MULTILINK;
9276dc97 2547
fdc74cf0 2548 if(!isset($this->list[$dst_port_id]))
2549 {
2550 /* get port not in list */
2551 // echo "<td>AHHH $src_port_id $dst_port_id --> $back</td>";
2552 // echo "<td>load".$this->var_dump_html($dst_link)." tree</td>";
2553// echo "<td>".$dst_link['cable']." ".$dst_link['name']."</td><td>not displayed</td>";
9276dc97 2554
fdc74cf0 2555 if($back)
2556 echo "<td>></td>";
2557
2558 // TODO check if multilink is needed here
2559 $this->printport($dst_link, $multilink && in_array($dst_link['type'], $lm_multilink_port_types));
2560 echo "<td>...</td>";
9276dc97 2561
fdc74cf0 2562 return TRUE;
9276dc97 2563
fdc74cf0 2564 // $this->_getportlist($this->list[$src_port_id], $back, !$back);
2565 }
9276dc97 2566
fdc74cf0 2567 $dst_port = $this->list[$dst_port_id];
2568 $object_id = $dst_port['object_id'];
2569 $obj_name = $dst_port['obj_name'];
9276dc97 2570
fdc74cf0 2571 if($obj_name == NULL)
2572 {
2573 $tmpobj = spotEntity('object', $dst_port['object_id']);
2574 $dst_port['obj_name'] = $tmpobj['dname'];
2575 $obj_name = $tmpobj['dname'];
2576 }
9276dc97 2577
fdc74cf0 2578 $loop = FALSE;
2579 $edgeport = ($dst_link == NULL) || empty($dst_port['front']) || empty($dst_port['back']);
2580
2581 if($back) {
2582 $linktype = 'back';
2583 } else {
2584 $linktype = 'front';
2585 }
9276dc97 2586
fdc74cf0 2587 $sameobject = FALSE;
2588
2589 if(isset($dst_link['loop']))
2590 $loop = TRUE;
2591
2592 if($dst_link != NULL) {
2593
2594 $src_object_id = $this->list[$src_port_id]['object_id'];
2595
2596 if(!$this->allback && $object_id == $src_object_id && $back) {
2597 $sameobject = TRUE;
9276dc97 2598 } else {
fdc74cf0 2599 $this->printlink($src_port_id, $dst_link, $linktype);
2600 }
2601
2602 } else {
2603 $this->_printlinkportsymbol($dst_port_id, $linktype);
2604 $edgeport = true;
9276dc97
ME
2605
2606 if(!$back)
fdc74cf0 2607 $this->printcomment($dst_port);
2608 }
9276dc97
ME
2609
2610 if($back) {
2611 if(!$sameobject)
2612 $this->printobject($object_id,$obj_name);
fdc74cf0 2613
9276dc97
ME
2614 echo "<td>></td>";
2615
2616 /* align ports nicely */
fdc74cf0 2617 if($dst_port['id'] == $this->port_id)
2618 echo '</td></tr></table id=printportlink1></td><td><table align=left><tr>';
9276dc97
ME
2619 }
2620
2621 /* print [portname] */
fdc74cf0 2622 // TODO check multilink symbols front/back edgeports
2623 $this->printport($dst_port, $multilink && in_array($dst_port['type'], $lm_multilink_port_types));
9276dc97
ME
2624
2625 if($loop)
2626 echo '<td bgcolor=#ff9966>LOOP</td>';
2627
2628 if(!$back) {
2629
2630 /* align ports nicely */
fdc74cf0 2631 if($dst_port['id'] == $this->port_id)
2632 echo '</td></tr></table id=printportlink2></td><td><table align=left><tr>';
9276dc97
ME
2633
2634 echo "<td><</td>";
2635 $this->printobject($object_id,$obj_name);
2636
fdc74cf0 2637 if(empty($dst_port['back']))
2638 $this->_printlinkportsymbol($dst_port_id, 'back');
9276dc97 2639 } else
fdc74cf0 2640 if(empty($dst_port['front'])) {
2641 $this->printcomment($dst_port);
2642 $this->_printlinkportsymbol($dst_port_id, 'front');
9276dc97 2643 }
fdc74cf0 2644
9276dc97 2645 if($loop) {
fdc74cf0 2646 if(isset($dst_link['loopmaxcount']))
9276dc97
ME
2647 $reason = " (MAX LOOP COUNT reached)";
2648 else
2649 $reason = '';
2650
fdc74cf0 2651 showWarning("Possible Loop on Port ($linktype) ".$dst_port['name'].$reason);
9276dc97
ME
2652 return FALSE;
2653 }
2654
2655 return TRUE;
2656
fdc74cf0 2657 } /* _printportlink */
9276dc97
ME
2658
2659 /*
2660 * print <tr>..</tr>
2661 */
fdc74cf0 2662 function printportlistrow($first = TRUE, $hl_port_id = NULL, $rowbgcolor = '#ffffff') {
2663
9276dc97
ME
2664 $this->loopcount = 0;
2665
2666 if($this->first_id == NULL)
fdc74cf0 2667 return false;
9276dc97
ME
2668
2669 if($first)
2670 $id = $this->first_id;
2671 else
2672 $id = $this->last_id;
2673
9276dc97
ME
2674 if($hl_port_id == $this->port_id)
2675 $hlbgcolor = "bgcolor=".self::HL_PORT_BGCOLOR;
2676 else
fdc74cf0 2677 $hlbgcolor = "bgcolor=$rowbgcolor";
9276dc97
ME
2678
2679 $link = NULL;
2680
2681 $port = $this->list[$id];
2682
fdc74cf0 2683 $urlparams = array(
2684 'module' => 'redirect',
2685 'page' => 'object',
2686 'tab' => 'linkmgmt',
2687 'op' => 'map',
2688 'usemap' => 1,
2689 'object_id' => $port['object_id'],
2690 );
2691
2692 if($hl_port_id !== NULL)
2693 $urlparams['hl_port_id'] = $hl_port_id;
2694 else
2695 $urlparams['port_id'] = $id;
2696
2697 $title = "linkcount: ".$this->count." (".$this->front_count."/".$this->back_count.")\nTypeID: ${port['type']}\nPortID: $id";
2698
2699 $onclick = 'onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2700 $urlparams)).'","Map","height=500,width=800,scrollbars=yes");';
9276dc97
ME
2701
2702 /* Current Port */
fdc74cf0 2703 echo '<tr '.$hlbgcolor.'><td nowrap="nowrap" bgcolor='.self::CURRENT_PORT_BGCOLOR.' title="'.$title.
2704 '"><a '.$onclick.'>'.
2705 $this->port['name'].': </a></td>';
9276dc97 2706
fdc74cf0 2707 echo "<td><table id=printportlistrow1 align=right><tr><td>";
2708
2709 $back = empty($this->list[$id]['back']);
2710
2711 $this->_printportlink(NULL, $id, $link, $back);
9276dc97
ME
2712
2713 $this->_printportlist($id, !$back);
fdc74cf0 2714 echo "</td></tr></table id=printportlistrow2></td></tr>";
9276dc97
ME
2715
2716 /* horizontal line */
2717 echo '<tr><td height=1 colspan=3 bgcolor=#e0e0e0></td></tr>';
2718
fdc74cf0 2719 return true;
2720
9276dc97
ME
2721 } /* printportlist */
2722
2723 /*
fdc74cf0 2724 * print <td>
9276dc97
ME
2725 * prints all ports in a list starting with start_port_id
2726 */
2727 /* !!! recursive */
2728 function _printportlist($src_port_id, $back = FALSE) {
2729
2730 if($back)
2731 $linktype = 'back';
2732 else
2733 $linktype = 'front';
2734
fdc74cf0 2735 if(!empty($this->list[$src_port_id][$linktype])) {
9276dc97 2736
fdc74cf0 2737 $linkcount = count($this->list[$src_port_id][$linktype]);
9276dc97 2738
fdc74cf0 2739 if($linkcount > 1)
2740 echo "<td bgcolor=#f00000></td><td><table id=_printportlist1>";
9276dc97 2741
fdc74cf0 2742 $lastkey = $linkcount - 1;
9276dc97 2743
fdc74cf0 2744 foreach($this->list[$src_port_id][$linktype] as $key => &$link) {
2745
2746 if($linkcount > 1) {
2747 echo "<tr style=\"background-color:".( $key % 2 ? self::ALTERNATE_ROW_BGCOLOR : "#ffffff" )."\"><td><table id=_printportlist2><tr>";
2748 }
2749
2750 $dst_port_id = $link['id'];
2751
2752 $this->loopcount++;
2753
2754 if($this->loopcount > self::MAX_LOOP_COUNT) {
2755 // $src_port_name = $this->list[$src_port_id]['name'];
2756 // $dst_port_name = $this->list[$dst_port_id]['name'];
9276dc97 2757
fdc74cf0 2758 $link['loop'] = $dst_port_id;
2759 $link['loopmaxcount'] = $dst_port_id;
2760
2761 /* loop warning is handeld in _printportlink() */
2762 //showWarning("MAX LOOP COUNT reached $src_port_name -> $dst_port_name".self::MAX_LOOP_COUNT);
2763 //return; /* return after _printportlink */
2764 }
2765
2766 if(!$this->_printportlink($src_port_id, $dst_port_id, $link, $back))
2767 {
9276dc97 2768 return;
fdc74cf0 2769 }
9276dc97 2770
fdc74cf0 2771 $this->_printportlist($dst_port_id,!$back);
9276dc97 2772
fdc74cf0 2773 if($linkcount > 1) {
2774 echo "</tr></table></td></tr>"
2775 .( $key != $lastkey ? "<tr><td height=1 colspan=100% bgcolor=#c0c0c0><td></tr>" : "");
2776 }
2777 }
9276dc97 2778
fdc74cf0 2779 if($linkcount > 1)
2780 echo "</table></td>";
2781 }
9276dc97
ME
2782 } /* _printportlist */
2783
fdc74cf0 2784 /*
9276dc97
ME
2785 * returns linked Row / Rack Info for object_id
2786 *
2787 */
2788 function _getRackInfo($object_id, $style = '') {
2789 global $lm_cache;
2790
2791 $rackinfocache = $lm_cache['rackinfo'];
2792
2793 /* if not in cache get it */
2794 if(!array_key_exists($object_id,$rackinfocache)) {
2795
fdc74cf0 2796 $object = spotEntity('object', $object_id);
2797 $rack_id = $object['rack_id'];
9276dc97 2798
fdc74cf0 2799 if($rack_id)
2800 $rackinfocache[$object_id] = spotEntity('rack', $object['rack_id']);
9276dc97
ME
2801 }
2802
9276dc97 2803
fdc74cf0 2804 $rack = &$rackinfocache[$object_id];
2805
2806 if(empty($rack))
9276dc97
ME
2807 return '<span style="'.$style.'">Unmounted</span>';
2808 else
fdc74cf0 2809 return '<a style="'.$style.'" href='.makeHref(array('page'=>'row', 'row_id'=>$rack['row_id'])).'>'.$rack['row_name']
2810 .'</a>/<a style="'.$style.'" href='.makeHref(array('page'=>'rack', 'rack_id'=>$rack['id'])).'>'
2811 .$rack['name'].'</a>';
9276dc97
ME
2812
2813 } /* _getRackInfo */
2814
fdc74cf0 2815 /*
2816 * return link symbol
2817 */
2818 function _getlinkportsymbol($port_id, $linktype) {
2819 $retval = '<span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2820 array('op' => 'PortLinkDialog','port' => $port_id,'linktype' => $linktype ))).'","name","height=800,width=800");'
2821 .'>';
2822
2823 $img = getImageHREF ('plug', $linktype.' Link this port');
2824
2825 if($linktype == 'back')
2826 $img = str_replace('<img',
2827 '<img style="transform:rotate(180deg);-o-transform:rotate(180deg);-ms-transform:rotate(180deg);-moz-transform:rotate(180deg);-webkit-transform:rotate(180deg);"',
2828 $img);
2829
2830 $retval .= $img;
2831 $retval .= "</span>";
2832 return $retval;
2833
2834 } /* _getlinkportsymbol */
9276dc97 2835
fdc74cf0 2836 /*
2837 * print link symbol
9276dc97
ME
2838 *
2839 */
fdc74cf0 2840 function _printlinkportsymbol($port_id, $linktype = 'front') {
9276dc97
ME
2841 global $lm_cache;
2842
2843 if(!$lm_cache['allowlink'])
2844 return;
9276dc97
ME
2845
2846 echo "<td align=center>";
2847
fdc74cf0 2848 echo $this->_getlinkportsymbol($port_id, $linktype);
9276dc97
ME
2849
2850 echo "</td>";
2851
fdc74cf0 2852 } /* _printlinkportsymbol */
9276dc97 2853
fdc74cf0 2854 /*
2855 * return link cut symbol
9276dc97
ME
2856 *
2857 * TODO $opspec_list
2858 */
fdc74cf0 2859 function _printUnLinkPort($src_port_id, &$dst_link, $linktype) {
9276dc97
ME
2860 global $lm_cache;
2861
2862 if(!$lm_cache['allowlink'])
2863 return '';
2864
fdc74cf0 2865 $src_port = $this->list[$src_port_id];
9276dc97 2866
fdc74cf0 2867 $dst_port = $this->list[$dst_link['id']];
9276dc97
ME
2868
2869 /* use RT unlink for front link, linkmgmt unlink for back links */
2870 if($linktype == 'back')
2871 $tab = 'linkmgmt';
2872 else
2873 $tab = 'ports';
9276dc97 2874
fdc74cf0 2875 return '<a href='.
9276dc97
ME
2876 makeHrefProcess(array(
2877 'op'=>'unlinkPort',
fdc74cf0 2878 'port_id'=>$src_port_id,
2879 'remote_id' => $dst_port['id'],
9276dc97
ME
2880 'object_id'=>$this->object_id,
2881 'tab' => $tab,
2882 'linktype' => $linktype)).
2883 ' onclick="return confirm(\'unlink ports '.$src_port['name']. ' -> '.$dst_port['name']
fdc74cf0 2884 .' ('.$linktype.') with cable ID: '.$dst_link['cable'].'?\');">'.
9276dc97
ME
2885 getImageHREF ('cut', $linktype.' Unlink this port').'</a>';
2886
2887 } /* _printUnLinkPort */
2888
2889
2890 /*
fdc74cf0 2891 *
9276dc97 2892 */
fdc74cf0 2893 static function urlparams($name, $value, $defaultvalue = NULL) {
9276dc97
ME
2894
2895 $urlparams = $_GET;
2896
2897 if($value == $defaultvalue) {
2898
fdc74cf0 2899 /* remove param */
2900 unset($urlparams[$name]);
9276dc97 2901
fdc74cf0 2902 } else {
9276dc97 2903
fdc74cf0 2904 $urlparams[$name] = $value;
9276dc97
ME
2905
2906 }
2907
2908 return $urlparams;
2909
2910 } /* urlparams */
2911
2912 /*
2913 * $params = array('name' => 'value', ...)
2914 */
fdc74cf0 2915 static function urlparamsarray($params) {
9276dc97
ME
2916
2917 $urlparams = $_GET;
2918
2919 foreach($params as $name => $value) {
2920
2921 if($value == NULL) {
2922
fdc74cf0 2923 /* remove param */
2924 unset($urlparams[$name]);
9276dc97 2925
fdc74cf0 2926 } else {
9276dc97 2927
fdc74cf0 2928 $urlparams[$name] = $value;
9276dc97 2929
fdc74cf0 2930 }
9276dc97
ME
2931 }
2932
2933 return $urlparams;
2934
2935 } /* urlparamsarray */
2936
2937 /* */
fdc74cf0 2938 static function hasbackend($object_id) {
9276dc97
ME
2939 /* sql bitwise xor: porta ^ portb */
2940 //select cable, ((porta ^ portb) ^ 4556) as port from Link where (4556 in (porta, portb));
2941
fdc74cf0 2942 $result = usePreparedSelectBlade
2943 (
9276dc97 2944 'SELECT count(*) from Port
2a5cd259 2945 left join LinkBackend as LinkBackend_a on LinkBackend_a.porta = id
2946 left join LinkBackend as LinkBackend_b on LinkBackend_b.portb = id
9276dc97
ME
2947 where object_id = ?',
2948 array($object_id)
fdc74cf0 2949 );
2950 $retval = $result->fetchColumn();
2951
2952 $result->closeCursor();
2953
2954 return $retval != 0;
9276dc97
ME
2955
2956 } /* hasbackend */
2957
2958 /* for debugging only */
2959 function var_dump_html(&$var) {
2960 echo "<pre>------------------Start Var Dump -------------------------\n";
2961 var_dump($var);
2962 echo "\n---------------------END Var Dump ------------------------</pre>";
2963 }
2964
2965} /* portlist */
2966
2967/* -------------------------------------------------- */
2968
2969?>