cmdbobject.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Class cmdbObject
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. /**
  25. * cmdbObjectClass
  26. * the file to include, then the core is yours
  27. *
  28. * @package iTopORM
  29. */
  30. require_once('coreexception.class.inc.php');
  31. require_once('config.class.inc.php');
  32. require_once('log.class.inc.php');
  33. require_once('kpi.class.inc.php');
  34. require_once('dict.class.inc.php');
  35. require_once('attributedef.class.inc.php');
  36. require_once('filterdef.class.inc.php');
  37. require_once('stimulus.class.inc.php');
  38. require_once('valuesetdef.class.inc.php');
  39. require_once('MyHelpers.class.inc.php');
  40. require_once('expression.class.inc.php');
  41. require_once('cmdbsource.class.inc.php');
  42. require_once('sqlquery.class.inc.php');
  43. require_once('oql/oqlquery.class.inc.php');
  44. require_once('oql/oqlexception.class.inc.php');
  45. require_once('oql/oql-parser.php');
  46. require_once('oql/oql-lexer.php');
  47. require_once('oql/oqlinterpreter.class.inc.php');
  48. require_once('dbobject.class.php');
  49. require_once('dbobjectsearch.class.php');
  50. require_once('dbobjectset.class.php');
  51. // db change tracking data model
  52. require_once('cmdbchange.class.inc.php');
  53. require_once('cmdbchangeop.class.inc.php');
  54. // customization data model
  55. // Romain: temporary moved into application.inc.php (see explanations there)
  56. //require_once('trigger.class.inc.php');
  57. //require_once('action.class.inc.php');
  58. // application log
  59. // Romain: temporary moved into application.inc.php (see explanations there)
  60. //require_once('event.class.inc.php');
  61. require_once('csvparser.class.inc.php');
  62. require_once('bulkchange.class.inc.php');
  63. /**
  64. * A persistent object, which changes are accurately recorded
  65. *
  66. * @package iTopORM
  67. */
  68. abstract class CMDBObject extends DBObject
  69. {
  70. protected $m_datCreated;
  71. protected $m_datUpdated;
  72. // Note: this value is static, but that could be changed because it is sometimes a real issue (see update of interfaces / connected_to
  73. protected static $m_oCurrChange = null;
  74. private function RecordObjCreation(CMDBChange $oChange)
  75. {
  76. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpCreate");
  77. $oMyChangeOp->Set("change", $oChange->GetKey());
  78. $oMyChangeOp->Set("objclass", get_class($this));
  79. $oMyChangeOp->Set("objkey", $this->GetKey());
  80. $iId = $oMyChangeOp->DBInsertNoReload();
  81. }
  82. private function RecordObjDeletion(CMDBChange $oChange, $objkey)
  83. {
  84. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpDelete");
  85. $oMyChangeOp->Set("change", $oChange->GetKey());
  86. $oMyChangeOp->Set("objclass", get_class($this));
  87. $oMyChangeOp->Set("objkey", $objkey);
  88. $iId = $oMyChangeOp->DBInsertNoReload();
  89. }
  90. private function RecordAttChanges(CMDBChange $oChange, array $aValues, array $aOrigValues)
  91. {
  92. // $aValues is an array of $sAttCode => $value
  93. //
  94. foreach ($aValues as $sAttCode=> $value)
  95. {
  96. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  97. if ($oAttDef->IsLinkSet()) continue; // #@# temporary
  98. if ($oAttDef instanceOf AttributeOneWayPassword)
  99. {
  100. // One Way encrypted passwords' history is stored -one way- encrypted
  101. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeOneWayPassword");
  102. $oMyChangeOp->Set("change", $oChange->GetKey());
  103. $oMyChangeOp->Set("objclass", get_class($this));
  104. $oMyChangeOp->Set("objkey", $this->GetKey());
  105. $oMyChangeOp->Set("attcode", $sAttCode);
  106. if (array_key_exists($sAttCode, $aOrigValues))
  107. {
  108. $original = $aOrigValues[$sAttCode];
  109. }
  110. else
  111. {
  112. $original = '';
  113. }
  114. $oMyChangeOp->Set("prev_pwd", $original);
  115. $iId = $oMyChangeOp->DBInsertNoReload();
  116. }
  117. elseif ($oAttDef instanceOf AttributeEncryptedString)
  118. {
  119. // Encrypted string history is stored encrypted
  120. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeEncrypted");
  121. $oMyChangeOp->Set("change", $oChange->GetKey());
  122. $oMyChangeOp->Set("objclass", get_class($this));
  123. $oMyChangeOp->Set("objkey", $this->GetKey());
  124. $oMyChangeOp->Set("attcode", $sAttCode);
  125. if (array_key_exists($sAttCode, $aOrigValues))
  126. {
  127. $original = $aOrigValues[$sAttCode];
  128. }
  129. else
  130. {
  131. $original = '';
  132. }
  133. $oMyChangeOp->Set("prevdata", $original);
  134. $iId = $oMyChangeOp->DBInsertNoReload();
  135. }
  136. elseif ($oAttDef instanceOf AttributeBlob)
  137. {
  138. // Data blobs
  139. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeBlob");
  140. $oMyChangeOp->Set("change", $oChange->GetKey());
  141. $oMyChangeOp->Set("objclass", get_class($this));
  142. $oMyChangeOp->Set("objkey", $this->GetKey());
  143. $oMyChangeOp->Set("attcode", $sAttCode);
  144. if (array_key_exists($sAttCode, $aOrigValues))
  145. {
  146. $original = $aOrigValues[$sAttCode];
  147. }
  148. else
  149. {
  150. $original = new ormDocument();
  151. }
  152. $oMyChangeOp->Set("prevdata", $original);
  153. $iId = $oMyChangeOp->DBInsertNoReload();
  154. }
  155. elseif ($oAttDef instanceOf AttributeText)
  156. {
  157. // Data blobs
  158. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeText");
  159. $oMyChangeOp->Set("change", $oChange->GetKey());
  160. $oMyChangeOp->Set("objclass", get_class($this));
  161. $oMyChangeOp->Set("objkey", $this->GetKey());
  162. $oMyChangeOp->Set("attcode", $sAttCode);
  163. if (array_key_exists($sAttCode, $aOrigValues))
  164. {
  165. $original = $aOrigValues[$sAttCode];
  166. }
  167. else
  168. {
  169. $original = null;
  170. }
  171. $oMyChangeOp->Set("prevdata", $original);
  172. $iId = $oMyChangeOp->DBInsertNoReload();
  173. }
  174. else
  175. {
  176. // Scalars
  177. //
  178. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar");
  179. $oMyChangeOp->Set("change", $oChange->GetKey());
  180. $oMyChangeOp->Set("objclass", get_class($this));
  181. $oMyChangeOp->Set("objkey", $this->GetKey());
  182. $oMyChangeOp->Set("attcode", $sAttCode);
  183. if (array_key_exists($sAttCode, $aOrigValues))
  184. {
  185. $sOriginalValue = $aOrigValues[$sAttCode];
  186. }
  187. else
  188. {
  189. $sOriginalValue = 'undefined';
  190. }
  191. $oMyChangeOp->Set("oldvalue", $sOriginalValue);
  192. $oMyChangeOp->Set("newvalue", $value);
  193. $iId = $oMyChangeOp->DBInsertNoReload();
  194. }
  195. }
  196. }
  197. /**
  198. * Helper to ultimately check user rights before writing (Insert, Update or Delete)
  199. * The check should never fail, because the UI should prevent from such a usage
  200. * Anyhow, if the user has found a workaround... the security gets enforced here
  201. */
  202. protected function CheckUserRights($bSkipStrongSecurity, $iActionCode)
  203. {
  204. if (is_null($bSkipStrongSecurity))
  205. {
  206. // This is temporary
  207. // We have implemented this safety net right before releasing iTop 1.0
  208. // and we decided that it was too risky to activate it
  209. // Anyhow, users willing to have a very strong security could set
  210. // skip_strong_security = 0, in the config file
  211. $bSkipStrongSecurity = utils::GetConfig()->Get('skip_strong_security');
  212. }
  213. if (!$bSkipStrongSecurity)
  214. {
  215. $sClass = get_class($this);
  216. $oSet = DBObjectSet::FromObject($this);
  217. if (!UserRights::IsActionAllowed($sClass, $iActionCode, $oSet))
  218. {
  219. // Intrusion detected
  220. throw new SecurityException('You are not allowed to modify objects of class: '.$sClass);
  221. }
  222. }
  223. }
  224. public function DBInsert()
  225. {
  226. if(!is_object(self::$m_oCurrChange))
  227. {
  228. throw new CoreException("DBInsert() could not be used here, please use DBInsertTracked() instead");
  229. }
  230. return $this->DBInsertTracked_Internal();
  231. }
  232. public function DBInsertTracked(CMDBChange $oChange, $bSkipStrongSecurity = null)
  233. {
  234. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_MODIFY);
  235. self::$m_oCurrChange = $oChange;
  236. $ret = $this->DBInsertTracked_Internal();
  237. self::$m_oCurrChange = null;
  238. return $ret;
  239. }
  240. public function DBInsertTrackedNoReload(CMDBChange $oChange, $bSkipStrongSecurity = null)
  241. {
  242. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_MODIFY);
  243. self::$m_oCurrChange = $oChange;
  244. $ret = $this->DBInsertTracked_Internal(true);
  245. self::$m_oCurrChange = null;
  246. return $ret;
  247. }
  248. protected function DBInsertTracked_Internal($bDoNotReload = false)
  249. {
  250. if ($bDoNotReload)
  251. {
  252. $ret = parent::DBInsertNoReload();
  253. }
  254. else
  255. {
  256. $ret = parent::DBInsert();
  257. }
  258. $this->RecordObjCreation(self::$m_oCurrChange);
  259. return $ret;
  260. }
  261. public function DBClone($newKey = null)
  262. {
  263. if(!self::$m_oCurrChange)
  264. {
  265. throw new CoreException("DBClone() could not be used here, please use DBCloneTracked() instead");
  266. }
  267. return $this->DBCloneTracked_Internal();
  268. }
  269. public function DBCloneTracked(CMDBChange $oChange, $newKey = null)
  270. {
  271. self::$m_oCurrChange = $oChange;
  272. $this->DBCloneTracked_Internal($newKey);
  273. self::$m_oCurrChange = null;
  274. }
  275. protected function DBCloneTracked_Internal($newKey = null)
  276. {
  277. $newKey = parent::DBClone($newKey);
  278. $oClone = MetaModel::GetObject(get_class($this), $newKey);
  279. $oClone->RecordObjCreation(self::$m_oCurrChange);
  280. return $newKey;
  281. }
  282. public function DBUpdate()
  283. {
  284. if(!self::$m_oCurrChange)
  285. {
  286. throw new CoreException("DBUpdate() could not be used here, please use DBUpdateTracked() instead");
  287. }
  288. return $this->DBUpdateTracked_internal();
  289. }
  290. public function DBUpdateTracked(CMDBChange $oChange, $bSkipStrongSecurity = null)
  291. {
  292. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_MODIFY);
  293. self::$m_oCurrChange = $oChange;
  294. $this->DBUpdateTracked_Internal();
  295. self::$m_oCurrChange = null;
  296. }
  297. protected function DBUpdateTracked_Internal()
  298. {
  299. // Copy the changes list before the update (the list should be reset afterwards)
  300. $aChanges = $this->ListChanges();
  301. if (count($aChanges) == 0)
  302. {
  303. //throw new CoreWarning("Attempting to update an unchanged object");
  304. return;
  305. }
  306. // Save the original values (will be reset to the new values when the object get written to the DB)
  307. $aOriginalValues = $this->m_aOrigValues;
  308. $ret = parent::DBUpdate();
  309. $this->RecordAttChanges(self::$m_oCurrChange, $aChanges, $aOriginalValues);
  310. return $ret;
  311. }
  312. public function DBDelete()
  313. {
  314. if(!self::$m_oCurrChange)
  315. {
  316. throw new CoreException("DBDelete() could not be used here, please use DBDeleteTracked() instead");
  317. }
  318. return $this->DBDeleteTracked_Internal();
  319. }
  320. public function DBDeleteTracked(CMDBChange $oChange, $bSkipStrongSecurity = null)
  321. {
  322. $this->CheckUserRights($bSkipStrongSecurity, UR_ACTION_DELETE);
  323. self::$m_oCurrChange = $oChange;
  324. $this->DBDeleteTracked_Internal();
  325. self::$m_oCurrChange = null;
  326. }
  327. protected function DBDeleteTracked_Internal()
  328. {
  329. $prevkey = $this->GetKey();
  330. $ret = parent::DBDelete();
  331. $this->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  332. return $ret;
  333. }
  334. public static function BulkDelete(DBObjectSearch $oFilter)
  335. {
  336. if(!self::$m_oCurrChange)
  337. {
  338. throw new CoreException("BulkDelete() could not be used here, please use BulkDeleteTracked() instead");
  339. }
  340. return $this->BulkDeleteTracked_Internal($oFilter);
  341. }
  342. public static function BulkDeleteTracked(CMDBChange $oChange, DBObjectSearch $oFilter)
  343. {
  344. self::$m_oCurrChange = $oChange;
  345. $this->BulkDeleteTracked_Internal($oFilter);
  346. self::$m_oCurrChange = null;
  347. }
  348. protected static function BulkDeleteTracked_Internal(DBObjectSearch $oFilter)
  349. {
  350. throw new CoreWarning("Change tracking not tested for bulk operations");
  351. // Get the list of objects to delete (and record data before deleting the DB records)
  352. $oObjSet = new CMDBObjectSet($oFilter);
  353. $aObjAndKeys = array(); // array of id=>object
  354. while ($oItem = $oObjSet->Fetch())
  355. {
  356. $aObjAndKeys[$oItem->GetKey()] = $oItem;
  357. }
  358. $oObjSet->FreeResult();
  359. // Delete in one single efficient query
  360. $ret = parent::BulkDelete($oFilter);
  361. // Record... in many queries !!!
  362. foreach($aObjAndKeys as $prevkey=>$oItem)
  363. {
  364. $oItem->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  365. }
  366. return $ret;
  367. }
  368. public static function BulkUpdate(DBObjectSearch $oFilter, array $aValues)
  369. {
  370. if(!self::$m_oCurrChange)
  371. {
  372. throw new CoreException("BulkUpdate() could not be used here, please use BulkUpdateTracked() instead");
  373. }
  374. return $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  375. }
  376. public static function BulkUpdateTracked(CMDBChange $oChange, DBObjectSearch $oFilter, array $aValues)
  377. {
  378. self::$m_oCurrChange = $oChange;
  379. $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  380. self::$m_oCurrChange = null;
  381. }
  382. protected static function BulkUpdateTracked_Internal(DBObjectSearch $oFilter, array $aValues)
  383. {
  384. // $aValues is an array of $sAttCode => $value
  385. // Get the list of objects to update (and load it before doing the change)
  386. $oObjSet = new CMDBObjectSet($oFilter);
  387. $oObjSet->Load();
  388. // Keep track of the previous values (will be overwritten when the objects are synchronized with the DB)
  389. $aOriginalValues = array();
  390. $oObjSet->Rewind();
  391. while ($oItem = $oObjSet->Fetch())
  392. {
  393. $aOriginalValues[$oItem->GetKey()] = $oItem->m_aOrigValues;
  394. }
  395. // Update in one single efficient query
  396. $ret = parent::BulkUpdate($oFilter, $aValues);
  397. // Record... in many queries !!!
  398. $oObjSet->Rewind();
  399. while ($oItem = $oObjSet->Fetch())
  400. {
  401. $aChangedValues = $oItem->ListChangedValues($aValues);
  402. $oItem->RecordAttChanges(self::$m_oCurrChange, $aChangedValues, $aOriginalValues[$oItem->GetKey()]);
  403. }
  404. return $ret;
  405. }
  406. }
  407. /**
  408. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  409. *
  410. * @package iTopORM
  411. */
  412. class CMDBObjectSet extends DBObjectSet
  413. {
  414. // this is the public interface (?)
  415. // I have to define those constructors here... :-(
  416. // just to get the right object class in return.
  417. // I have to think again to those things: maybe it will work fine if a have a constructor define here (?)
  418. static public function FromScratch($sClass)
  419. {
  420. $oFilter = new CMDBSearchFilter($sClass);
  421. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  422. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  423. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  424. $oRetSet->m_bLoaded = true; // no DB load
  425. return $oRetSet;
  426. }
  427. static public function FromArray($sClass, $aObjects)
  428. {
  429. $oFilter = new CMDBSearchFilter($sClass);
  430. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  431. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  432. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  433. $oRetSet->m_bLoaded = true; // no DB load
  434. $oRetSet->AddObjectArray($aObjects);
  435. return $oRetSet;
  436. }
  437. static public function FromArrayAssoc($aClasses, $aObjects)
  438. {
  439. // In a perfect world, we should create a complete tree of DBObjectSearch,
  440. // but as we lack most of the information related to the objects,
  441. // let's create one search definition
  442. $sClass = reset($aClasses);
  443. $sAlias = key($aClasses);
  444. $oFilter = new CMDBSearchFilter($sClass, $sAlias);
  445. $oRetSet = new CMDBObjectSet($oFilter);
  446. $oRetSet->m_bLoaded = true; // no DB load
  447. foreach($aObjects as $rowIndex => $aObjectsByClassAlias)
  448. {
  449. $oRetSet->AddObjectExtended($aObjectsByClassAlias);
  450. }
  451. return $oRetSet;
  452. }
  453. }
  454. /**
  455. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  456. *
  457. * @package iTopORM
  458. */
  459. class CMDBSearchFilter extends DBObjectSearch
  460. {
  461. // this is the public interface (?)
  462. }
  463. ?>