PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/www/include/configuration/configObject/servicegroup/DB-Func.php

https://gitlab.com/florianocomercial/centreon
PHP | 268 lines | 196 code | 31 blank | 41 comment | 54 complexity | a682d8615839a10f5f5b3270612b2655 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2005-2015 Centreon
  4. * Centreon is developped by : Julien Mathis and Romain Le Merlus under
  5. * GPL Licence 2.0.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU General Public License as published by the Free Software
  9. * Foundation ; either version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  13. * PARTICULAR PURPOSE. See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, see <http://www.gnu.org/licenses>.
  17. *
  18. * Linking this program statically or dynamically with other modules is making a
  19. * combined work based on this program. Thus, the terms and conditions of the GNU
  20. * General Public License cover the whole combination.
  21. *
  22. * As a special exception, the copyright holders of this program give Centreon
  23. * permission to link this program with independent modules to produce an executable,
  24. * regardless of the license terms of these independent modules, and to copy and
  25. * distribute the resulting executable under terms of Centreon choice, provided that
  26. * Centreon also meet, for each linked independent module, the terms and conditions
  27. * of the license of that module. An independent module is a module which is not
  28. * derived from this program. If you modify this program, you may extend this
  29. * exception to your version of the program, but you are not obliged to do so. If you
  30. * do not wish to do so, delete this exception statement from your version.
  31. *
  32. * For more information : contact@centreon.com
  33. *
  34. * SVN : $URL$
  35. * SVN : $Id$
  36. *
  37. */
  38. if (!isset ($oreon))
  39. exit ();
  40. function testServiceGroupExistence ($name = NULL) {
  41. global $pearDB, $form, $oreon;
  42. $id = NULL;
  43. if (isset($form)) {
  44. $id = $form->getSubmitValue('sg_id');
  45. }
  46. $DBRESULT = $pearDB->query("SELECT sg_name, sg_id FROM servicegroup WHERE sg_name = '".htmlentities($oreon->checkIllegalChar($name), ENT_QUOTES, "UTF-8")."'");
  47. $sg = $DBRESULT->fetchRow();
  48. if ($DBRESULT->numRows() >= 1 && $sg["sg_id"] == $id) {
  49. # Modif
  50. return true;
  51. } else if ($DBRESULT->numRows() >= 1 && $sg["sg_id"] != $id) {
  52. # Duplicate
  53. return false;
  54. } else {
  55. return true;
  56. }
  57. }
  58. function enableServiceGroupInDB ($sg_id = null) {
  59. if (!$sg_id) return;
  60. global $pearDB, $oreon;
  61. $DBRESULT = $pearDB->query("UPDATE servicegroup SET sg_activate = '1' WHERE sg_id = '".$sg_id."'");
  62. $DBRESULT2 = $pearDB->query("SELECT sg_name FROM `servicegroup` WHERE `sg_id` = '".$sg_id."' LIMIT 1");
  63. $row = $DBRESULT2->fetchRow();
  64. $oreon->CentreonLogAction->insertLog("servicegroup", $sg_id, $row['sg_name'], "enable");
  65. }
  66. function disableServiceGroupInDB ($sg_id = null) {
  67. if (!$sg_id) return;
  68. global $pearDB, $oreon;
  69. $DBRESULT = $pearDB->query("UPDATE servicegroup SET sg_activate = '0' WHERE sg_id = '".$sg_id."'");
  70. $DBRESULT2 = $pearDB->query("SELECT sg_name FROM `servicegroup` WHERE `sg_id` = '".$sg_id."' LIMIT 1");
  71. $row = $DBRESULT2->fetchRow();
  72. $oreon->CentreonLogAction->insertLog("servicegroup", $sg_id, $row['sg_name'], "disable");
  73. }
  74. function deleteServiceGroupInDB ($serviceGroups = array()) {
  75. global $pearDB, $centreon;
  76. foreach($serviceGroups as $key=>$value) {
  77. $DBRESULT2 = $pearDB->query("SELECT sg_name FROM `servicegroup` WHERE `sg_id` = '".$key."' LIMIT 1");
  78. $row = $DBRESULT2->fetchRow();
  79. $DBRESULT = $pearDB->query("DELETE FROM servicegroup WHERE sg_id = '".$key."'");
  80. $centreon->CentreonLogAction->insertLog("servicegroup", $key, $row['sg_name'], "d");
  81. }
  82. $centreon->user->access->updateACL();
  83. }
  84. function multipleServiceGroupInDB ($serviceGroups = array(), $nbrDup = array()) {
  85. global $pearDB, $oreon, $is_admin;
  86. $sgAcl = array();
  87. foreach($serviceGroups as $key=>$value) {
  88. $DBRESULT = $pearDB->query("SELECT * FROM servicegroup WHERE sg_id = '".$key."' LIMIT 1");
  89. $row = $DBRESULT->fetchRow();
  90. $row["sg_id"] = '';
  91. for ($i = 1; $i <= $nbrDup[$key]; $i++) {
  92. $val = NULL;
  93. $rq = NULL;
  94. foreach ($row as $key2=>$value2) {
  95. $key2 == "sg_name" ? ($sg_name = $value2 = $value2."_".$i) : null;
  96. $val ? $val .= ($value2!=NULL?(", '".$value2."'"):", NULL") : $val .= ($value2!=NULL?("'".$value2."'"):"NULL");
  97. if ($key2 != "sg_id") {
  98. $fields[$key2] = $value2;
  99. }
  100. if (isset($sg_name)) {
  101. $fields["sg_name"] = $sg_name;
  102. }
  103. }
  104. if (testServiceGroupExistence($sg_name)) {
  105. $val ? $rq = "INSERT INTO servicegroup VALUES (".$val.")" : $rq = null;
  106. $DBRESULT = $pearDB->query($rq);
  107. $DBRESULT = $pearDB->query("SELECT MAX(sg_id) FROM servicegroup");
  108. $maxId = $DBRESULT->fetchRow();
  109. if (isset($maxId["MAX(sg_id)"])) {
  110. $sgAcl[$maxId["MAX(sg_id)"]] = $key;
  111. $DBRESULT->free();
  112. $DBRESULT = $pearDB->query("SELECT DISTINCT sgr.host_host_id, sgr.hostgroup_hg_id, sgr.service_service_id FROM servicegroup_relation sgr WHERE sgr.servicegroup_sg_id = '".$key."'");
  113. $fields["sg_hgServices"] = "";
  114. while($service = $DBRESULT->fetchRow()) {
  115. $val = null;
  116. foreach ($service as $key2=>$value2)
  117. $val ? $val .= ($value2!=NULL?(", '".$value2."'"):", NULL") : $val .= ($value2!=NULL?("'".$value2."'"):"NULL");
  118. $DBRESULT2 = $pearDB->query("INSERT INTO servicegroup_relation (host_host_id, hostgroup_hg_id, service_service_id, servicegroup_sg_id) VALUES (".$val.", '".$maxId["MAX(sg_id)"]."')");
  119. $fields["sg_hgServices"] .= $service["service_service_id"] . ",";
  120. }
  121. $fields["sg_hgServices"] = trim($fields["sg_hgServices"], ",");
  122. $oreon->CentreonLogAction->insertLog("servicegroup", $maxId["MAX(sg_id)"], $sg_name, "a", $fields);
  123. }
  124. }
  125. }
  126. }
  127. CentreonACL::duplicateSgAcl($sgAcl);
  128. $oreon->user->access->updateACL();
  129. }
  130. function insertServiceGroupInDB ($ret = array()) {
  131. global $centreon;
  132. $sg_id = insertServiceGroup($ret);
  133. updateServiceGroupServices($sg_id, $ret);
  134. $centreon->user->access->updateACL();
  135. return $sg_id;
  136. }
  137. function updateServiceGroupInDB ($sg_id = NULL, $ret = array(), $increment = false) {
  138. global $centreon;
  139. if (!$sg_id) return;
  140. updateServiceGroup($sg_id, $ret);
  141. updateServiceGroupServices($sg_id, $ret, $increment);
  142. $centreon->user->access->updateACL();
  143. }
  144. function insertServiceGroup($ret = array()) {
  145. global $form, $pearDB, $oreon;
  146. if (!count($ret))
  147. $ret = $form->getSubmitValues();
  148. $ret["sg_name"] = $oreon->checkIllegalChar($ret["sg_name"]);
  149. $rq = "INSERT INTO servicegroup (sg_name, sg_alias, sg_comment, sg_activate) ";
  150. $rq .= "VALUES (";
  151. isset($ret["sg_name"]) && $ret["sg_name"] != NULL ? $rq .= "'".htmlentities($ret["sg_name"], ENT_QUOTES, "UTF-8")."', " : $rq .= "NULL, ";
  152. isset($ret["sg_alias"]) && $ret["sg_alias"] != NULL ? $rq .= "'".htmlentities($ret["sg_alias"], ENT_QUOTES, "UTF-8")."', " : $rq .= "NULL, ";
  153. isset($ret["sg_comment"]) && $ret["sg_comment"] != NULL ? $rq .= "'".htmlentities($ret["sg_comment"], ENT_QUOTES, "UTF-8")."', " : $rq .= "NULL, ";
  154. isset($ret["sg_activate"]["sg_activate"]) && $ret["sg_activate"]["sg_activate"] != NULL ? $rq .= "'".$ret["sg_activate"]["sg_activate"]."'" : $rq .= "'0'";
  155. $rq .= ")";
  156. $DBRESULT = $pearDB->query($rq);
  157. $DBRESULT = $pearDB->query("SELECT MAX(sg_id) FROM servicegroup");
  158. $sg_id = $DBRESULT->fetchRow();
  159. $fields["sg_name"] = htmlentities($ret["sg_name"], ENT_QUOTES, "UTF-8");
  160. $fields["sg_alias"] = htmlentities($ret["sg_alias"], ENT_QUOTES, "UTF-8");
  161. $fields["sg_comment"] = htmlentities($ret["sg_comment"], ENT_QUOTES, "UTF-8");
  162. $fields["sg_activate"] = $ret["sg_activate"]["sg_activate"];
  163. if (isset($ret["sg_hgServices"]))
  164. $fields["sg_hgServices"] = implode(",", $ret["sg_hgServices"]);
  165. $oreon->CentreonLogAction->insertLog("servicegroup", $sg_id["MAX(sg_id)"], htmlentities($ret["sg_name"], ENT_QUOTES, "UTF-8"), "a", $fields);
  166. $DBRESULT->free();
  167. return ($sg_id["MAX(sg_id)"]);
  168. }
  169. function updateServiceGroup($sg_id, $ret = array()) {
  170. global $form, $pearDB, $oreon;
  171. if (!$sg_id) {
  172. return;
  173. }
  174. if (!count($ret)) {
  175. $ret = $form->getSubmitValues();
  176. }
  177. $ret["sg_name"] = $oreon->checkIllegalChar($ret["sg_name"]);
  178. $rq = "UPDATE servicegroup SET ";
  179. isset($ret["sg_name"]) && $ret["sg_name"] != NULL ? $rq .= "sg_name = '".htmlentities($ret["sg_name"], ENT_QUOTES, "UTF-8")."', " : $rq .= "sg_name = NULL,";
  180. isset($ret["sg_alias"]) && $ret["sg_alias"] != NULL ? $rq.= "sg_alias = '".htmlentities($ret["sg_alias"], ENT_QUOTES, "UTF-8")."', " : $rq .= "sg_alias = NULL";
  181. isset($ret["sg_comment"]) && $ret["sg_comment"] != NULL ? $rq .= "sg_comment = '".htmlentities($ret["sg_comment"], ENT_QUOTES, "UTF-8")."', " : $rq .= "sg_comment = NULL,";
  182. isset($ret["sg_activate"]["sg_activate"]) && $ret["sg_activate"]["sg_activate"] != NULL ? $rq .= "sg_activate = '".$ret["sg_activate"]["sg_activate"]."' " : $rq .= "sg_activate = '0'";
  183. $rq .= "WHERE sg_id = '".$sg_id."'";
  184. $DBRESULT = $pearDB->query($rq);
  185. $fields["sg_name"] = htmlentities($ret["sg_name"], ENT_QUOTES, "UTF-8");
  186. $fields["sg_alias"] = htmlentities($ret["sg_alias"], ENT_QUOTES, "UTF-8");
  187. $fields["sg_comment"] = htmlentities($ret["sg_comment"], ENT_QUOTES, "UTF-8");
  188. $fields["sg_activate"] = $ret["sg_activate"]["sg_activate"];
  189. if (isset($ret["sg_hgServices"]))
  190. $fields["sg_hgServices"] = implode(",", $ret["sg_hgServices"]);
  191. $oreon->CentreonLogAction->insertLog("servicegroup", $sg_id, htmlentities($ret["sg_name"], ENT_QUOTES, "UTF-8"), "c", $fields);
  192. }
  193. function updateServiceGroupServices($sg_id, $ret = array(), $increment = false) {
  194. if (!$sg_id)
  195. return;
  196. global $pearDB, $form;
  197. if ($increment == false) {
  198. $rq = "DELETE FROM servicegroup_relation ";
  199. $rq .= "WHERE servicegroup_sg_id = '".$sg_id."'";
  200. $DBRESULT = $pearDB->query($rq);
  201. }
  202. /* service templates */
  203. $retTmp = isset($ret["sg_tServices"]) ? $ret["sg_tServices"] : $form->getSubmitValue("sg_tServices");
  204. for ($i = 0; $i < count($retTmp); $i++) {
  205. if (isset($retTmp[$i]) && $retTmp[$i]){
  206. $t = preg_split("/\-/", $retTmp[$i]);
  207. $resTest = $pearDB->query("SELECT servicegroup_sg_id service FROM servicegroup_relation WHERE host_host_id = ".$t[0]." AND service_service_id = ".$t[1]." AND servicegroup_sg_id = ".$sg_id);
  208. if (!$resTest->numRows()) {
  209. $rq = "INSERT INTO servicegroup_relation (host_host_id, service_service_id, servicegroup_sg_id) VALUES ('".$t[0]."', '".$t[1]."', '".$sg_id."')";
  210. $DBRESULT = $pearDB->query($rq);
  211. }
  212. }
  213. }
  214. /* regular services */
  215. $retTmp = isset($ret["sg_hServices"]) ? $ret["sg_hServices"] : CentreonUtils::mergeWithInitialValues($form, 'sg_hServices');
  216. for ($i = 0; $i < count($retTmp); $i++) {
  217. if (isset($retTmp[$i]) && $retTmp[$i]){
  218. $t = preg_split("/\-/", $retTmp[$i]);
  219. $resTest = $pearDB->query("SELECT servicegroup_sg_id service FROM servicegroup_relation WHERE host_host_id = ".$t[0]." AND service_service_id = ".$t[1]." AND servicegroup_sg_id = ".$sg_id);
  220. if (!$resTest->numRows()) {
  221. $rq = "INSERT INTO servicegroup_relation (host_host_id, service_service_id, servicegroup_sg_id) VALUES ('".$t[0]."', '".$t[1]."', '".$sg_id."')";
  222. $DBRESULT = $pearDB->query($rq);
  223. }
  224. }
  225. }
  226. /* hostgroup services */
  227. $retTmp = isset($ret["sg_hgServices"]) ? $ret["sg_hgServices"] : CentreonUtils::mergeWithInitialValues($form, 'sg_hgServices');
  228. for ($i = 0; $i < count($retTmp); $i++) {
  229. $t = preg_split("/\-/", $retTmp[$i]);
  230. $resTest = $pearDB->query("SELECT servicegroup_sg_id service FROM servicegroup_relation WHERE hostgroup_hg_id = ".$t[0]." AND service_service_id = ".$t[1]." AND servicegroup_sg_id = ".$sg_id);
  231. if (!$resTest->numRows()) {
  232. $rq = "INSERT INTO servicegroup_relation (hostgroup_hg_id, service_service_id, servicegroup_sg_id) VALUES ('".$t[0]."', '".$t[1]."', '".$sg_id."')";
  233. $DBRESULT = $pearDB->query($rq);
  234. }
  235. }
  236. }
  237. ?>