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