PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/cron/centAcl-Func.php

https://gitlab.com/florianocomercial/centreon
PHP | 418 lines | 295 code | 45 blank | 78 comment | 49 complexity | 96397e8dffae899eb63a4da667f1aa4a 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. */
  35. /*
  36. * Init functions
  37. */
  38. function microtime_float2() {
  39. list($usec, $sec) = explode(" ", microtime());
  40. return ((float) $usec + (float) $sec);
  41. }
  42. /**
  43. * Return host tab after poller filter
  44. */
  45. function getFilteredPollers($host, $acl_group_id, $res_id) {
  46. global $pearDB, $hostCache;
  47. $request = "SELECT COUNT(*) AS count FROM acl_resources_poller_relations WHERE acl_res_id = '".$res_id."'";
  48. $DBRESULT = $pearDB->query($request);
  49. $row = $DBRESULT->fetchRow();
  50. $isPollerFilter = $row['count'];
  51. $hostTmp = $host;
  52. $request = "SELECT host_host_id " .
  53. "FROM acl_resources_poller_relations, acl_res_group_relations, acl_resources, ns_host_relation " .
  54. "WHERE acl_resources_poller_relations.acl_res_id = acl_res_group_relations.acl_res_id " .
  55. "AND acl_res_group_relations.acl_group_id = '" . $acl_group_id . "' " .
  56. "AND acl_resources_poller_relations.acl_res_id = acl_resources.acl_res_id " .
  57. "AND acl_resources.acl_res_id = '" . $res_id . "' " .
  58. "AND ns_host_relation.nagios_server_id = acl_resources_poller_relations.poller_id " .
  59. "AND acl_res_activate = '1'";
  60. $DBRESULT = $pearDB->query($request);
  61. if ($DBRESULT->numRows()) {
  62. $host = array();
  63. while ($row = $DBRESULT->fetchRow()) {
  64. if (isset($hostTmp[$row['host_host_id']])) {
  65. $host[$row['host_host_id']] = $hostCache[$row['host_host_id']];
  66. }
  67. }
  68. } else {
  69. # If result of query is empty and user have poller restriction, clean host table.
  70. if ($isPollerFilter) {
  71. $host = array();
  72. }
  73. }
  74. return $host;
  75. }
  76. /**
  77. * Return host tab after host categories filter
  78. */
  79. function getFilteredHostCategories($host, $acl_group_id, $res_id) {
  80. global $pearDB, $hostCache, $hostTemplateCache;
  81. $hostTmp = $host;
  82. $request = "SELECT host_host_id " .
  83. "FROM acl_resources_hc_relations, acl_res_group_relations, acl_resources, hostcategories_relation " .
  84. "WHERE acl_resources_hc_relations.acl_res_id = acl_res_group_relations.acl_res_id " .
  85. "AND acl_res_group_relations.acl_group_id = '" . $acl_group_id . "' " .
  86. "AND acl_resources_hc_relations.acl_res_id = acl_resources.acl_res_id " .
  87. "AND acl_resources.acl_res_id = '" . $res_id . "' " .
  88. "AND hostcategories_relation.hostcategories_hc_id = acl_resources_hc_relations.hc_id " .
  89. "AND acl_res_activate = '1'";
  90. $DBRESULT = $pearDB->query($request);
  91. if ($DBRESULT->numRows()) {
  92. $host = array();
  93. }
  94. while ($row = $DBRESULT->fetchRow()) {
  95. if (isset($hostTemplateCache[$row['host_host_id']])) {
  96. // is a template
  97. foreach ($hostTemplateCache[$row['host_host_id']] as $hId) {
  98. if (isset($hostTmp[$hId])) {
  99. $host[$hId] = $hostCache[$hId];
  100. }
  101. }
  102. } elseif (isset($hostTmp[$row['host_host_id']])) {
  103. // is not a template
  104. $host[$row['host_host_id']] = $hostCache[$row['host_host_id']];
  105. }
  106. }
  107. return $host;
  108. }
  109. /*
  110. * Return enable categories for this resource access
  111. */
  112. function getAuthorizedCategories($groupstr, $res_id) {
  113. global $pearDB;
  114. if (strlen($groupstr) == 0)
  115. return array();
  116. $tab_categories = array();
  117. $request = "SELECT sc_id " .
  118. "FROM acl_resources_sc_relations, acl_res_group_relations, acl_resources " .
  119. "WHERE acl_resources_sc_relations.acl_res_id = acl_res_group_relations.acl_res_id " .
  120. "AND acl_res_group_relations.acl_group_id IN (" . $groupstr . ") " .
  121. "AND acl_resources_sc_relations.acl_res_id = acl_resources.acl_res_id " .
  122. "AND acl_resources.acl_res_id = '" . $res_id . "' " .
  123. "AND acl_res_activate = '1'";
  124. $DBRESULT = $pearDB->query($request);
  125. while ($res = $DBRESULT->fetchRow()) {
  126. $tab_categories[$res["sc_id"]] = $res["sc_id"];
  127. }
  128. $DBRESULT->free();
  129. unset($res);
  130. unset($DBRESULT);
  131. return $tab_categories;
  132. }
  133. function getServiceTemplateCategoryList($service_id = NULL) {
  134. global $pearDB, $svcTplCache, $svcCatCache;
  135. $tabCategory = array();
  136. if (!$service_id)
  137. return;
  138. if (isset($svcCatCache[$service_id])) {
  139. foreach ($svcCatCache[$service_id] as $ct_id => $flag) {
  140. $tabCategory[$ct_id] = $ct_id;
  141. }
  142. }
  143. /*
  144. * Init Table of template
  145. */
  146. $loopBreak = array();
  147. while (1) {
  148. if (isset($svcTplCache[$service_id]) && !isset($loopBreak[$service_id])) {
  149. if (isset($svcCatCache[$service_id])) {
  150. foreach ($svcCatCache[$service_id] as $ct_id => $flag) {
  151. $tabCategory[$ct_id] = $ct_id;
  152. }
  153. }
  154. $loopBreak[$service_id] = true;
  155. $service_id = $svcTplCache[$service_id];
  156. } else {
  157. return $tabCategory;
  158. }
  159. }
  160. }
  161. function getACLSGForHost($pearDB, $host_id, $groupstr) {
  162. global $svcCache, $sgCache;
  163. if (!$pearDB || !isset($host_id))
  164. return;
  165. $svc = array();
  166. if (isset($sgCache[$groupstr])) {
  167. foreach ($sgCache[$groupstr] as $key => $tab) {
  168. foreach ($tab as $hostId => $tab2) {
  169. if ($host_id == $hostId) {
  170. foreach ($tab2 as $svcDesc => $svcId) {
  171. $svc[$svcDesc] = $svcId;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. return $svc;
  178. /*
  179. * Init Acl Table
  180. */
  181. $svc = array();
  182. $condition = "";
  183. if ($groupstr != "") {
  184. $condition = " WHERE `acl_group_id` IN (" . $groupstr . ") AND ";
  185. } else {
  186. $condition = " WHERE ";
  187. }
  188. $DBRESULT = $pearDB->query("SELECT argr.`acl_res_id` FROM `acl_res_group_relations` argr, `acl_resources` ar " . $condition . " " .
  189. "argr.acl_res_id = ar.acl_res_id " .
  190. "AND ar.acl_res_activate = '1'");
  191. while ($res = $DBRESULT->fetchRow()) {
  192. $DBRESULT2 = $pearDB->query("SELECT `service_service_id` " .
  193. "FROM `servicegroup`, `acl_resources_sg_relations`, `servicegroup_relation` " .
  194. "WHERE `acl_res_id` = '" . $res["acl_res_id"] . "' " .
  195. "AND `acl_resources_sg_relations`.`sg_id` = `servicegroup`.`sg_id` " .
  196. "AND `servicegroup_relation`.`servicegroup_sg_id` = `servicegroup`.`sg_id` " .
  197. "AND `servicegroup_relation`.`host_host_id` = '" . $host_id . "'");
  198. while ($service = $DBRESULT2->fetchRow()) {
  199. if (isset($svcCache[$service["service_service_id"]])) {
  200. $svc[$svcCache[$service["service_service_id"]]] = $service["service_service_id"];
  201. }
  202. }
  203. $DBRESULT2->free();
  204. }
  205. $DBRESULT->free();
  206. return $svc;
  207. }
  208. /**
  209. * If the ressource ACL has poller filter
  210. *
  211. * @param int $res_id The ACL ressource id
  212. * @return bool
  213. */
  214. function hasPollerFilter($res_id) {
  215. global $pearDB;
  216. if (!is_numeric($res_id)) {
  217. return false;
  218. }
  219. $query = 'SELECT COUNT(*) as c FROM acl_resources_poller_relations WHERE acl_res_id = ' . $res_id;
  220. $res = $pearDB->query($query);
  221. if (PEAR::isError($res)) {
  222. return false;
  223. }
  224. $row = $res->fetchRow();
  225. if ($row['c'] > 0) {
  226. return true;
  227. }
  228. return false;
  229. }
  230. /**
  231. * If the ressource ACL has host category filter
  232. *
  233. * @param int $res_id The ACL ressource id
  234. * @return bool
  235. */
  236. function hasHostCategoryFilter($res_id) {
  237. global $pearDB;
  238. if (!is_numeric($res_id)) {
  239. return false;
  240. }
  241. $query = 'SELECT COUNT(*) as c FROM acl_resources_hc_relations WHERE acl_res_id = ' . $res_id;
  242. $res = $pearDB->query($query);
  243. if (PEAR::isError($res)) {
  244. return false;
  245. }
  246. $row = $res->fetchRow();
  247. if ($row['c'] > 0) {
  248. return true;
  249. }
  250. return false;
  251. }
  252. /**
  253. * If the ressource ACL has service category filter
  254. *
  255. * @param int $res_id The ACL ressource id
  256. * @return bool
  257. */
  258. function hasServiceCategoryFilter($res_id) {
  259. global $pearDB;
  260. if (!is_numeric($res_id)) {
  261. return false;
  262. }
  263. $query = 'SELECT COUNT(*) as c FROM acl_resources_sc_relations WHERE acl_res_id = ' . $res_id;
  264. $res = $pearDB->query($query);
  265. if (PEAR::isError($res)) {
  266. return false;
  267. }
  268. $row = $res->fetchRow();
  269. if ($row['c'] > 0) {
  270. return true;
  271. }
  272. return false;
  273. }
  274. function getAuthorizedServicesHost($host_id, $groupstr, $res_id, $authorizedCategories) {
  275. global $pearDB, $svcCache, $hostCache;
  276. $tab_svc = getMyHostServicesByName($host_id);
  277. /*
  278. * Get Service Groups
  279. */
  280. $svc_SG = getACLSGForHost($pearDB, $host_id, $groupstr);
  281. $tab_services = array();
  282. if (count($authorizedCategories)) {
  283. if ($tab_svc) {
  284. foreach ($tab_svc as $svc_descr => $svc_id) {
  285. $tab = getServiceTemplateCategoryList($svc_id);
  286. foreach ($tab as $t) {
  287. if (isset($authorizedCategories[$t])) {
  288. $tab_services[$svc_descr] = $svc_id;
  289. }
  290. }
  291. }
  292. }
  293. } else {
  294. $tab_services = $tab_svc;
  295. if ($svc_SG) {
  296. foreach ($svc_SG as $key => $value) {
  297. $tab_services[$key] = $value;
  298. }
  299. }
  300. }
  301. return $tab_services;
  302. }
  303. function hostIsAuthorized($host_id, $group_id) {
  304. global $pearDB;
  305. $query = "SELECT rhr.host_host_id " .
  306. "FROM acl_resources_host_relations rhr, acl_resources res, acl_res_group_relations rgr " .
  307. "WHERE rhr.acl_res_id = res.acl_res_id " .
  308. "AND res.acl_res_id = rgr.acl_res_id " .
  309. "AND rgr.acl_group_id = '" . $group_id . "' " .
  310. "AND rhr.host_host_id = '" . $host_id . "' " .
  311. "AND res.acl_res_activate = '1'";
  312. $DBRES = $pearDB->query($query);
  313. if ($DBRES->numRows())
  314. return true;
  315. $query2 = "SELECT hgr.host_host_id FROM " .
  316. "hostgroup_relation hgr, acl_resources_hg_relations rhgr, acl_resources res, acl_res_group_relations rgr " .
  317. "WHERE rhgr.acl_res_id = res.acl_res_id " .
  318. "AND res.acl_res_id = rgr.acl_res_id " .
  319. "AND rgr.acl_group_id = '" . $group_id . "' " .
  320. "AND hgr.hostgroup_hg_id = rhgr.hg_hg_id " .
  321. "AND hgr.host_host_id = '" . $host_id . "' " .
  322. "AND res.acl_res_activate = '1' " .
  323. "AND hgr.host_host_id NOT IN (SELECT host_host_id FROM acl_resources_hostex_relations WHERE acl_res_id = rhgr.acl_res_id)";
  324. $DBRES2 = $pearDB->query($query2);
  325. if (PEAR::isError($DBRES2))
  326. print "DB Error : " . $DBRES2->getDebugInfo() . "<br />";
  327. if ($DBRES2->numRows())
  328. return true;
  329. return false;
  330. }
  331. /*
  332. * Retreive service description
  333. */
  334. function getMyHostServicesByName($host_id = NULL) {
  335. global $pearDB, $hsRelation, $svcCache;
  336. if (!$host_id)
  337. return;
  338. $hSvs = array();
  339. if (isset($hsRelation[$host_id])) {
  340. foreach ($hsRelation[$host_id] as $service_id => $flag) {
  341. $service_description = str_replace('#S#', '/', $svcCache[$service_id]);
  342. $service_description = str_replace('#BS#', '\\', $service_description);
  343. $hSvs[$service_description] = html_entity_decode($service_id, ENT_QUOTES);
  344. }
  345. }
  346. return $hSvs;
  347. }
  348. /**
  349. * Get meta services
  350. *
  351. * @param int $resId
  352. * @param CentreonDB $db
  353. * @param CentreonMeta $metaObj
  354. * @return array
  355. */
  356. function getMetaServices($resId, $db, $metaObj) {
  357. $sql = "SELECT meta_id
  358. FROM acl_resources_meta_relations
  359. WHERE acl_res_id = {$db->escape($resId)}";
  360. $res = $db->query($sql);
  361. $arr = array();
  362. if ($res->numRows()) {
  363. $hostId = $metaObj->getRealHostId();
  364. while ($row = $res->fetchRow()) {
  365. $svcId = $metaObj->getRealServiceId($row['meta_id']);
  366. $arr['_Module_Meta']['meta_' . $row['meta_id']] = "$hostId,$svcId";
  367. }
  368. }
  369. return $arr;
  370. }
  371. ?>