[738] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | function GetMarkByComment($Comment)
|
---|
| 4 | {
|
---|
| 5 | global $Database;
|
---|
| 6 |
|
---|
| 7 | $DbResult = $Database->query('SELECT `Id` FROM `NetworkMark` WHERE `Comment`="'.$Comment.'"');
|
---|
[873] | 8 | if ($DbResult->num_rows > 0)
|
---|
[738] | 9 | {
|
---|
| 10 | $DbRow = $DbResult->fetch_assoc();
|
---|
[873] | 11 | return ($DbRow['Id']);
|
---|
[738] | 12 | } else
|
---|
| 13 | {
|
---|
| 14 | $DbResult = $Database->query('INSERT INTO `NetworkMark` (`Comment`) VALUES ("'.$Comment.'")');
|
---|
[873] | 15 | return ($Database->insert_id);
|
---|
[738] | 16 | }
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | function GetSubgroupByRange($AddressRange)
|
---|
| 20 | {
|
---|
| 21 | global $Database;
|
---|
| 22 |
|
---|
| 23 | $DbResult = $Database->query('SELECT `Id` FROM `NetworkMangleSubgroup` WHERE `AddressRange`="'.$AddressRange.'"');
|
---|
[873] | 24 | if ($DbResult->num_rows > 0)
|
---|
[738] | 25 | {
|
---|
| 26 | $DbRow = $DbResult->fetch_assoc();
|
---|
[873] | 27 | return ($DbRow['Id']);
|
---|
[738] | 28 | } else
|
---|
| 29 | {
|
---|
| 30 | $DbResult = $Database->query('INSERT INTO `NetworkMangleSubgroup` (`AddressRange`) VALUES ("'.$AddressRange.'")');
|
---|
[873] | 31 | return ($Database->insert_id);
|
---|
[738] | 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[870] | 35 | function InsertToAddressTreeIPv4(&$Tree, $Address, $Name, $InterSubnets = false, $ForceMark = false)
|
---|
[738] | 36 | {
|
---|
| 37 | global $Config;
|
---|
| 38 |
|
---|
| 39 | $Found = false;
|
---|
[873] | 40 | foreach ($Tree['Items'] as $Index => $Node)
|
---|
[738] | 41 | {
|
---|
[873] | 42 | if ($Node['Address']->Contain($Address))
|
---|
[738] | 43 | {
|
---|
[870] | 44 | InsertToAddressTreeIPv4($Tree['Items'][$Index], $Address, $Name, true);
|
---|
[738] | 45 | $Found = true;
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
[873] | 48 | if ($Found == false)
|
---|
[738] | 49 | {
|
---|
[873] | 50 | if ($InterSubnets and ($Tree['Address']->Prefix < $Config['MainRouter']['MangleRuleSubgroupMinPrefix']) and
|
---|
[738] | 51 | ($Address->Prefix > ($Tree['Address']->Prefix + 1)))
|
---|
| 52 | {
|
---|
| 53 | $NewAddress = new NetworkAddressIPv4();
|
---|
| 54 | $NewAddress->Address = $Address->Address;
|
---|
| 55 | $NewAddress->ChangePrefix($Tree['Address']->Prefix + 1);
|
---|
| 56 | $Tree['Items'][] = array('Address' => $NewAddress, 'Name' => $Name, 'Items' => array(), 'ForceMark' => false);
|
---|
[870] | 57 | InsertToAddressTreeIPv4($Tree['Items'][count($Tree['Items']) - 1], $Address, $Name, true);
|
---|
[738] | 58 | } else
|
---|
| 59 | {
|
---|
[870] | 60 | $NewNode = array('Address' => $Address, 'Name' => $Name, 'Items' => array(), 'ForceMark' => $ForceMark);
|
---|
[738] | 61 |
|
---|
[870] | 62 | // Should be existed items placed under new node?
|
---|
| 63 | $Found = false;
|
---|
[873] | 64 | foreach ($Tree['Items'] as $Index => $Node)
|
---|
[870] | 65 | {
|
---|
[873] | 66 | if (($Node['Address']->Address == $NewNode['Address']->Address) and
|
---|
[870] | 67 | ($Node['Address']->Prefix == $NewNode['Address']->Prefix)) $Found = true;
|
---|
| 68 |
|
---|
[873] | 69 | if ($Address->Contain($Node['Address']))
|
---|
[870] | 70 | {
|
---|
| 71 | $NewNode['Items'][] = $Node;
|
---|
| 72 | unset($Tree['Items'][$Index]);
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
[873] | 75 | if ($Found == false) $Tree['Items'][] = $NewNode;
|
---|
[870] | 76 | }
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | function InsertToAddressTreeIPv6(&$Tree, $Address, $Name, $InterSubnets = false, $ForceMark = false)
|
---|
| 81 | {
|
---|
| 82 | global $Config;
|
---|
| 83 |
|
---|
| 84 | $Found = false;
|
---|
[873] | 85 | foreach ($Tree['Items'] as $Index => $Node)
|
---|
[870] | 86 | {
|
---|
[873] | 87 | if ($Node['Address']->Contain($Address))
|
---|
[870] | 88 | {
|
---|
| 89 | InsertToAddressTreeIPv6($Tree['Items'][$Index], $Address, $Name, true);
|
---|
| 90 | $Found = true;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[873] | 93 | if ($Found == false)
|
---|
[870] | 94 | {
|
---|
[873] | 95 | if ($InterSubnets and ($Tree['Address']->Prefix < $Config['MainRouter']['MangleRuleSubgroupMinPrefix']) and
|
---|
[870] | 96 | ($Address->Prefix > ($Tree['Address']->Prefix + 1)))
|
---|
| 97 | {
|
---|
| 98 | $NewAddress = new NetworkAddressIPv6();
|
---|
| 99 | $NewAddress->Address = $Address->Address;
|
---|
| 100 | $NewAddress->ChangePrefix($Tree['Address']->Prefix + 1);
|
---|
| 101 | $Tree['Items'][] = array('Address' => $NewAddress, 'Name' => $Name, 'Items' => array(), 'ForceMark' => false);
|
---|
| 102 | InsertToAddressTreeIPv6($Tree['Items'][count($Tree['Items']) - 1], $Address, $Name, true);
|
---|
| 103 | } else
|
---|
| 104 | {
|
---|
[738] | 105 | $NewNode = array('Address' => $Address, 'Name' => $Name, 'Items' => array(), 'ForceMark' => $ForceMark);
|
---|
| 106 |
|
---|
| 107 | // Should be existed items placed under new node?
|
---|
| 108 | $Found = false;
|
---|
[873] | 109 | foreach ($Tree['Items'] as $Index => $Node)
|
---|
[738] | 110 | {
|
---|
[873] | 111 | if (($Node['Address']->Address == $NewNode['Address']->Address) and
|
---|
[738] | 112 | ($Node['Address']->Prefix == $NewNode['Address']->Prefix)) $Found = true;
|
---|
| 113 |
|
---|
[873] | 114 | if ($Address->Contain($Node['Address']))
|
---|
[738] | 115 | {
|
---|
| 116 | $NewNode['Items'][] = $Node;
|
---|
| 117 | unset($Tree['Items'][$Index]);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
[873] | 120 | if ($Found == false) $Tree['Items'][] = $NewNode;
|
---|
[738] | 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | function ShowSubnetNode($Node, $Indent = 0)
|
---|
| 126 | {
|
---|
| 127 | echo(str_repeat(' ', $Indent).$Node['Address']->AddressToString().'/'.$Node['Address']->Prefix.' '.$Node['Name']."\n");
|
---|
[873] | 128 | foreach ($Node['Items'] as $Index => $Item)
|
---|
[738] | 129 | {
|
---|
| 130 | ShowSubnetNode($Item, $Indent + 1);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|