PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/var/www/html/site/admin/manageFolderEditSubmit.php

https://gitlab.com/Rodrigj98/phpipam
PHP | 143 lines | 90 code | 24 blank | 29 comment | 31 complexity | 3df5d46371d9e3827d957224c782f4d0 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Function to add / edit / delete section
  4. ********************************************/
  5. /* required functions */
  6. require_once('../../functions/functions.php');
  7. /* verify that user is logged in */
  8. isUserAuthenticated(true);
  9. /* filter input */
  10. $_POST = filter_user_input($_POST, true, true, false);
  11. $_POST['action'] = filter_user_input($_POST['action'], false, false, true);
  12. /* must be numeric */
  13. if($_POST['action']!="add") {
  14. if(!is_numeric($_POST['subnetId'])) { die('<div class="alert alert-danger">'._("Invalid ID").'</div>'); }
  15. }
  16. /* verify that user has permissions if add */
  17. if($_POST['action'] == "add") {
  18. $sectionPerm = checkSectionPermission ($_POST['sectionId']);
  19. if($sectionPerm != 3) {
  20. die("<div class='alert alert alert-danger'>"._('You do not have permissions to add new subnet in this section')."!</div>");
  21. }
  22. }
  23. /* otherwise check subnet permission */
  24. else {
  25. $subnetPerm = checkSubnetPermission ($_POST['subnetId']);
  26. if($subnetPerm != 3) {
  27. die("<div class='alert alert alert-danger'>"._('You do not have permissions to add edit/delete this subnet')."!</div>");
  28. }
  29. }
  30. /* verify post */
  31. CheckReferrer();
  32. /* get all settings */
  33. $settings = getAllSettings();
  34. /* get section details */
  35. $section = getSectionDetailsById($_POST['sectionId']);
  36. //custom
  37. $myFields = getCustomFields('subnets');
  38. if(sizeof($myFields) > 0) {
  39. foreach($myFields as $myField) {
  40. # replace possible ___ back to spaces!
  41. $myField['nameTest'] = str_replace(" ", "___", $myField['name']);
  42. if(isset($_POST[$myField['nameTest']])) { $_POST[$myField['name']] = $_POST[$myField['nameTest']];}
  43. }
  44. }
  45. //we need old values for mailing
  46. if($_POST['action']=="edit" || $_POST['action']=="delete") {
  47. $old = getSubnetDetailsById($_POST['subnetId']);
  48. }
  49. $new = $_POST;
  50. unset ($new['subnet'],$new['allowRequests'],$new['showName'],$new['pingSubnet'],$new['discoverSubnet']);
  51. unset ($old['subnet'],$old['allowRequests'],$old['showName'],$old['pingSubnet'],$old['discoverSubnet']);
  52. /* sanitize description */
  53. $_POST['description'] = htmlentities($_POST['description'], ENT_COMPAT | ENT_HTML401, "UTF-8"); //prevent XSS
  54. /* Set permissions for add! */
  55. if($_POST['action'] == "add") {
  56. # root
  57. if($_POST['masterSubnetId'] == 0) {
  58. $_POST['permissions'] = $section['permissions'];
  59. }
  60. # nested - inherit parent permissions
  61. else {
  62. # get parent
  63. $parent = getSubnetDetailsById($_POST['masterSubnetId']);
  64. $_POST['permissions'] = $parent['permissions'];
  65. }
  66. }
  67. # check for name length - 2 is minimum!
  68. if(strlen($_POST['description'])<2 && $_POST['action']!="delete") {
  69. die("<div class='alert alert alert-danger'>"._('Folder name must have at least 2 characters')."!</div>");
  70. }
  71. # set folder flag!
  72. $_POST['isFolder'] = true;
  73. # failed
  74. if ($_POST['action']=="delete" && !isset($_POST['deleteconfirm'])) {
  75. # for ajax to prevent reload
  76. print "<div style='display:none'>alert alert-danger</div>";
  77. # result
  78. print "<div class='alert alert-warning'>";
  79. # print what will be deleted
  80. getAllSlaves($_POST['subnetId'], false);
  81. $removeSlaves = array_unique($removeSlaves);
  82. # check if folder?
  83. $foldercnt = 0;
  84. $subnetcnt = 0;
  85. foreach($removeSlaves as $s) {
  86. $f=getSubnetDetailsById($s);
  87. if($f['isFolder']==1) $foldercnt++;
  88. else $subnetcnt++;
  89. }
  90. $ipcnt = countAllSlaveIPAddresses($_POST['subnetId']);
  91. print "<strong>"._("Warning")."</strong>: "._("I will delete").":<ul>";
  92. print " <li>$foldercnt "._("folders")."</li>";
  93. if($subnetcnt>0) {
  94. print " <li>$subnetcnt "._("subnets")."</li>";
  95. }
  96. if($ipcnt>0) {
  97. print " <li>$ipcnt "._("IP addresses")."</li>";
  98. }
  99. print "</ul>";
  100. print "<hr><div style='text-align:right'>";
  101. print _("Are you sure you want to delete above items?")." ";
  102. print "<div class='btn-group'>";
  103. print " <a class='btn btn-sm btn-danger editFolderSubmitDelete' id='editFolderSubmitDelete' data-subnetId='".$_POST['subnetId']."'>"._("Confirm")."</a>";
  104. print "</div>";
  105. print "</div>";
  106. print "</div>";
  107. }
  108. else {
  109. if (!modifySubnetDetails ($_POST)) { print '<div class="alert alert alert-danger">'._('Error adding new folder').'!</div>'; }
  110. # all good
  111. else {
  112. /* @mail functions ------------------- */
  113. include_once('../../functions/functions-mail.php');
  114. sendObjectUpdateMails("folder", $_POST['action'], $old, $new);
  115. if($_POST['action'] == "delete") { print '<div class="alert alert-success">'._('Folder, IP addresses and all belonging subnets deleted successfully').'!</div>'; }
  116. else { print '<div class="alert alert-success">'._("Folder $_POST[action] successfull").'!</div>'; }
  117. }
  118. }
  119. ?>