index.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. if (cfr('SWITCHESEDIT')) {
  3. /**
  4. * Renders switches integrity report
  5. *
  6. * @return string
  7. */
  8. function web_SwitchesIntegrityReport() {
  9. $result = '';
  10. $result .= wf_BackLink('?module=switches');
  11. $messages = new UbillingMessageHelper();
  12. $allParents = array();
  13. $allLinks = array();
  14. $allIds = array();
  15. $allDeleted = array();
  16. $selfLoop = array();
  17. $query = "SELECT * from `switches`";
  18. $all = simple_queryall($query);
  19. if (!empty($all)) {
  20. //filling parent ids array
  21. foreach ($all as $io => $each) {
  22. if (!empty($each['parentid'])) {
  23. $allParents[$each['parentid']] = $each['id'];
  24. }
  25. }
  26. //filling alllinks array
  27. foreach ($all as $io => $each) {
  28. $allLinks[$each['id']] = $each['parentid'];
  29. if ($each['id'] == $each['parentid']) {
  30. $selfLoop[$each['id']] = $each['parentid'];
  31. }
  32. }
  33. //filling registered ids array
  34. foreach ($all as $io => $each) {
  35. $allIds[$each['id']] = $each['ip'];
  36. }
  37. $result .= $messages->getStyledMessage(__('Total switches in database') . ': ' . sizeof($all), 'info');
  38. $result .= $messages->getStyledMessage(__('Parent switches found') . ': ' . sizeof($allParents), 'info');
  39. //checking uplinks geo availability
  40. foreach ($all as $io => $each) {
  41. if (isset($allParents[$each['id']])) {
  42. if (empty($each['geo'])) {
  43. $result .= $messages->getStyledMessage(__('Geo location') . ' ' . __('is empty') . ': ' . web_SwitchProfileLink($each['id']) . ' ' . $each['ip'] . ' - ' . $each['location'], 'error');
  44. }
  45. }
  46. }
  47. //checking uplinks switches availability
  48. foreach ($all as $io => $each) {
  49. if (!empty($each['parentid'])) {
  50. if (!isset($allIds[$each['parentid']])) {
  51. $allDeleted[$each['parentid']] = $each['parentid'];
  52. $result .= $messages->getStyledMessage(__('Uplink switch is deleted from database') . ': ' . web_SwitchProfileLink($each['id']) . ' - ' . $each['ip'] . ' ' . $each['location'] . ', ' . __('uplink deleted') . ' : [ ' . $each['parentid'] . ' ]', 'error');
  53. }
  54. }
  55. }
  56. ///checking uplinks switches possible loop
  57. if (empty($allDeleted)) {
  58. if (empty($selfLoop)) {
  59. $roads = array();
  60. $failRoads = array();
  61. if (!empty($allLinks)) {
  62. foreach ($allLinks as $id => $parentid) {
  63. $roads[$id][] = $parentid;
  64. $trace = $parentid;
  65. while (!empty($trace)) {
  66. if (isset($allLinks[$trace])) {
  67. if ((array_search($allLinks[$trace], $roads[$id])) == false) {
  68. $roads[$id][] = $allLinks[$trace];
  69. } else {
  70. $failRoads[$id] = $allLinks[$trace];
  71. $trace = '';
  72. }
  73. $trace = (isset($allLinks[$trace])) ? $allLinks[$trace] : '';
  74. }
  75. }
  76. }
  77. }
  78. if (!empty($failRoads)) {
  79. $failRoads = array_flip($failRoads);
  80. $resultLoop = '';
  81. foreach ($failRoads as $io => $each) {
  82. $resultLoop .= web_SwitchProfileLink($io);
  83. }
  84. $result .= $messages->getStyledMessage(__('Following switches have loops between') . ': ' . $resultLoop, 'error');
  85. }
  86. } else {
  87. $resultLoop = '';
  88. foreach ($selfLoop as $io => $each) {
  89. $resultLoop .= web_SwitchProfileLink($io);
  90. }
  91. $result .= $messages->getStyledMessage(__('Because some of switches have itself as parent, check is skipped') . ': ' . $resultLoop, 'error');
  92. }
  93. } else {
  94. $result .= $messages->getStyledMessage(__('Because some of uplink switches deleted loop, check is skipped'), 'error');
  95. }
  96. //IP duplicates
  97. if (!empty($all)) {
  98. $tmp = array();
  99. $dupIpCount = 0;
  100. foreach ($all as $switchId => $switchData) {
  101. if (!empty($switchData['ip'])) {
  102. $tmp[$switchData['ip']][] = $switchData;
  103. }
  104. }
  105. foreach ($tmp as $switchIp => $switchesOn) {
  106. if (sizeof($switchesOn) > 1) {
  107. foreach ($switchesOn as $index => $swDupData) {
  108. $swLabel = wf_Link('?module=switches&edit=' . $swDupData['id'], '[' . $swDupData['id'] . ']') . ' ' . $swDupData['location'];
  109. $result .= $messages->getStyledMessage(__('Duplicate') . ' ' . __('IP') . ': ' . $switchIp . ' ' . $swLabel, 'warning');
  110. $dupIpCount++;
  111. }
  112. }
  113. }
  114. if (!$dupIpCount) {
  115. $result .= $messages->getStyledMessage(__('Duplicate') . ' ' . __('IP') . ': ' . __('Nothing found'), 'success');
  116. }
  117. }
  118. }
  119. return ($result);
  120. }
  121. show_window(__('Switches integrity check'), web_SwitchesIntegrityReport());
  122. } else {
  123. show_error(__('Access denied'));
  124. }