RuleForm.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (27.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Firewall\Forms;
  20. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  21. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  22. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Validators\PortValidator;
  23. use ModulesGarden\Servers\ProxmoxCloudVps\Core\FileReader\Reader\Json;
  24. use ModulesGarden\Servers\ProxmoxCloudVps\Core\ModuleConstants;
  25. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
  26. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\BaseForm;
  27. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\BaseField;
  28. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Hidden;
  29. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Select;
  30. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Switcher;
  31. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Text;
  32. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Sections\HalfPageSection;
  33. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Sections\RawSection;
  34. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  35. class RuleForm extends BaseForm implements ClientArea
  36. {
  37. use ApiService;
  38. use ProductService;
  39. private $mainSection;
  40. private $topSection;
  41. private $leftSection;
  42. private $rightSection;
  43. private $bottomSection;
  44. private $sectionFields=[];
  45. protected function initSections()
  46. {
  47. //Main
  48. $this->mainSection = new RawSection('mainSection');
  49. $this->mainSection->setMainContainer($this->mainContainer);
  50. //Left
  51. $this->leftSection = new HalfPageSection('leftSection');
  52. $this->leftSection->setMainContainer($this->mainContainer);
  53. //Right
  54. $this->rightSection = new HalfPageSection('rightSection');
  55. $this->rightSection->setMainContainer($this->mainContainer);
  56. //Add Top
  57. $this->topSection = new RawSection('topSection');
  58. $this->topSection->setMainContainer($this->mainContainer);
  59. //add main
  60. $this->mainSection->addSection( $this->topSection )->addSection($this->leftSection)->addSection($this->rightSection);
  61. $this->addSection($this->mainSection);
  62. //Bottom
  63. $this->bottomSection = new RawSection('bottomSection');
  64. $this->bottomSection->setMainContainer($this->mainContainer);
  65. $this->addSection($this->bottomSection);
  66. }
  67. protected function initFields()
  68. {
  69. /*Left*/
  70. //enable
  71. $field = new Switcher('enable');
  72. $this->topSection->addField($field);
  73. //type
  74. $this->addFieldType();
  75. //action
  76. $this->addFieldAction();
  77. //iface
  78. $this->addFieldIface();
  79. //source
  80. $field = new Text('source');
  81. $this->addSectionField($field);
  82. //dest
  83. $field = new Text('dest');
  84. $this->addSectionField($field);
  85. /*Right*/
  86. //macro
  87. $this->addFieldMacro();
  88. //proto
  89. $this->addFieldProto();
  90. //sport
  91. $field = new Text('sport');
  92. $field->addValidator(new PortValidator());
  93. $this->addSectionField($field);
  94. //dport
  95. $field = new Text('dport');
  96. $field->addValidator(new PortValidator());
  97. $this->addSectionField($field);
  98. //comment
  99. $field = new Text('comment');
  100. $this->bottomSection->addField($field);
  101. $this->addFieldsToSections();
  102. }
  103. protected function addFieldPos()
  104. {
  105. $field = new Hidden('pos');
  106. $this->bottomSection->addField($field);
  107. }
  108. private function addFieldType()
  109. {
  110. $field = new Select('type');
  111. $field->setAvailableValues([
  112. "in" => sl('lang')->tr("in"),
  113. "out" => sl('lang')->tr("out")
  114. ]);
  115. $this->addSectionField($field);
  116. }
  117. private function addFieldAction()
  118. {
  119. $field = new Select('action');
  120. $entries = new Json('actions.json', ModuleConstants::getFullPath('storage', 'app', "firewall"));
  121. $options = [];
  122. foreach ($entries->get() as $key => $v)
  123. {
  124. $options[$key] = sl('lang')->tr($v);
  125. }
  126. $field->setAvailableValues($options);
  127. $this->addSectionField($field);
  128. }
  129. private function addFieldIface()
  130. {
  131. $field = new Select('iface');
  132. $ifaces = [];
  133. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  134. /**
  135. * @deprecated
  136. $interfaces = $this->configuration()->getFirewallInterfaces();
  137. if (in_array("venet", $interfaces))
  138. {
  139. $ifaces["venet"] = sl('lang')->tr("venet");
  140. }
  141. if (in_array("eth", $interfaces))
  142. {
  143. }
  144. */
  145. foreach ($vm->getNetworkDevices() as $nd)
  146. {
  147. $ifaces[$nd->getId()] = $nd->getId();
  148. }
  149. $field->setAvailableValues($ifaces);
  150. $this->addSectionField($field);
  151. }
  152. private function addFieldMacro()
  153. {
  154. $field = new Select('macro');
  155. $macros = ["0" => sl("lang")->tr("None")];
  156. foreach ($this->api()->get("/cluster/firewall/macros", []) as $response)
  157. {
  158. $macros[$response['macro']] = sl("lang")->tr($response['descr']);
  159. }
  160. $field->setAvailableValues($macros);
  161. $this->addSectionField($field);
  162. }
  163. private function addFieldProto()
  164. {
  165. $field = new Select('proto');
  166. $entries = new Json('protocols.json', ModuleConstants::getFullPath('storage', 'app', "firewall"));
  167. $options = [];
  168. foreach ($entries->get() as $key => $v)
  169. {
  170. $options[$key] = sl('lang')->tr($v);
  171. }
  172. $field->setAvailableValues($options);
  173. $this->addSectionField($field);
  174. }
  175. public function addSectionField(BaseField $field)
  176. {
  177. $this->sectionFields[] = $field;
  178. return $this;
  179. }
  180. private function addFieldsToSections(){
  181. foreach($this->sectionFields as $k => $field){
  182. if($k % 2 == 0 ){
  183. $this->leftSection->addField($field);
  184. }else{
  185. $this->rightSection->addField($field);
  186. }
  187. }
  188. unset($this->sectionFields);
  189. }
  190. }