cmdbobject.class.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. protected static $m_oCurrChange = null;
  73. private function RecordObjCreation(CMDBChange $oChange)
  74. {
  75. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpCreate");
  76. $oMyChangeOp->Set("change", $oChange->GetKey());
  77. $oMyChangeOp->Set("objclass", get_class($this));
  78. $oMyChangeOp->Set("objkey", $this->GetKey());
  79. $iId = $oMyChangeOp->DBInsertNoReload();
  80. }
  81. private function RecordObjDeletion(CMDBChange $oChange, $objkey)
  82. {
  83. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpDelete");
  84. $oMyChangeOp->Set("change", $oChange->GetKey());
  85. $oMyChangeOp->Set("objclass", get_class($this));
  86. $oMyChangeOp->Set("objkey", $objkey);
  87. $iId = $oMyChangeOp->DBInsertNoReload();
  88. }
  89. private function RecordAttChanges(CMDBChange $oChange, array $aValues, array $aOrigValues)
  90. {
  91. // $aValues is an array of $sAttCode => $value
  92. //
  93. foreach ($aValues as $sAttCode=> $value)
  94. {
  95. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  96. if ($oAttDef->IsLinkSet()) continue; // #@# temporary
  97. if ($oAttDef instanceOf AttributeOneWayPassword)
  98. {
  99. // One Way encrypted passwords' history is stored -one way- encrypted
  100. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeOneWayPassword");
  101. $oMyChangeOp->Set("change", $oChange->GetKey());
  102. $oMyChangeOp->Set("objclass", get_class($this));
  103. $oMyChangeOp->Set("objkey", $this->GetKey());
  104. $oMyChangeOp->Set("attcode", $sAttCode);
  105. if (array_key_exists($sAttCode, $aOrigValues))
  106. {
  107. $original = $aOrigValues[$sAttCode];
  108. }
  109. else
  110. {
  111. $original = '';
  112. }
  113. $oMyChangeOp->Set("prev_pwd", $original);
  114. $iId = $oMyChangeOp->DBInsertNoReload();
  115. }
  116. elseif ($oAttDef instanceOf AttributeEncryptedString)
  117. {
  118. // Encrypted string history is stored encrypted
  119. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeEncrypted");
  120. $oMyChangeOp->Set("change", $oChange->GetKey());
  121. $oMyChangeOp->Set("objclass", get_class($this));
  122. $oMyChangeOp->Set("objkey", $this->GetKey());
  123. $oMyChangeOp->Set("attcode", $sAttCode);
  124. if (array_key_exists($sAttCode, $aOrigValues))
  125. {
  126. $original = $aOrigValues[$sAttCode];
  127. }
  128. else
  129. {
  130. $original = '';
  131. }
  132. $oMyChangeOp->Set("prevdata", $original);
  133. $iId = $oMyChangeOp->DBInsertNoReload();
  134. }
  135. elseif ($oAttDef instanceOf AttributeBlob)
  136. {
  137. // Data blobs
  138. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeBlob");
  139. $oMyChangeOp->Set("change", $oChange->GetKey());
  140. $oMyChangeOp->Set("objclass", get_class($this));
  141. $oMyChangeOp->Set("objkey", $this->GetKey());
  142. $oMyChangeOp->Set("attcode", $sAttCode);
  143. if (array_key_exists($sAttCode, $aOrigValues))
  144. {
  145. $original = $aOrigValues[$sAttCode];
  146. }
  147. else
  148. {
  149. $original = new ormDocument();
  150. }
  151. $oMyChangeOp->Set("prevdata", $original);
  152. $iId = $oMyChangeOp->DBInsertNoReload();
  153. }
  154. elseif ($oAttDef instanceOf AttributeText)
  155. {
  156. // Data blobs
  157. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeText");
  158. $oMyChangeOp->Set("change", $oChange->GetKey());
  159. $oMyChangeOp->Set("objclass", get_class($this));
  160. $oMyChangeOp->Set("objkey", $this->GetKey());
  161. $oMyChangeOp->Set("attcode", $sAttCode);
  162. if (array_key_exists($sAttCode, $aOrigValues))
  163. {
  164. $original = $aOrigValues[$sAttCode];
  165. }
  166. else
  167. {
  168. $original = null;
  169. }
  170. $oMyChangeOp->Set("prevdata", $original);
  171. $iId = $oMyChangeOp->DBInsertNoReload();
  172. }
  173. else
  174. {
  175. // Scalars
  176. //
  177. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar");
  178. $oMyChangeOp->Set("change", $oChange->GetKey());
  179. $oMyChangeOp->Set("objclass", get_class($this));
  180. $oMyChangeOp->Set("objkey", $this->GetKey());
  181. $oMyChangeOp->Set("attcode", $sAttCode);
  182. if (array_key_exists($sAttCode, $aOrigValues))
  183. {
  184. $sOriginalValue = $aOrigValues[$sAttCode];
  185. }
  186. else
  187. {
  188. $sOriginalValue = 'undefined';
  189. }
  190. $oMyChangeOp->Set("oldvalue", $sOriginalValue);
  191. $oMyChangeOp->Set("newvalue", $value);
  192. $iId = $oMyChangeOp->DBInsertNoReload();
  193. }
  194. }
  195. }
  196. public function DBInsert()
  197. {
  198. if(!is_object(self::$m_oCurrChange))
  199. {
  200. throw new CoreException("DBInsert() could not be used here, please use DBInsertTracked() instead");
  201. }
  202. return $this->DBInsertTracked_Internal();
  203. }
  204. public function DBInsertTracked(CMDBChange $oChange)
  205. {
  206. self::$m_oCurrChange = $oChange;
  207. $ret = $this->DBInsertTracked_Internal();
  208. self::$m_oCurrChange = null;
  209. return $ret;
  210. }
  211. public function DBInsertTrackedNoReload(CMDBChange $oChange)
  212. {
  213. self::$m_oCurrChange = $oChange;
  214. $ret = $this->DBInsertTracked_Internal(true);
  215. self::$m_oCurrChange = null;
  216. return $ret;
  217. }
  218. protected function DBInsertTracked_Internal($bDoNotReload = false)
  219. {
  220. if ($bDoNotReload)
  221. {
  222. $ret = parent::DBInsertNoReload();
  223. }
  224. else
  225. {
  226. $ret = parent::DBInsert();
  227. }
  228. $this->RecordObjCreation(self::$m_oCurrChange);
  229. return $ret;
  230. }
  231. public function DBClone($newKey = null)
  232. {
  233. if(!self::$m_oCurrChange)
  234. {
  235. throw new CoreException("DBClone() could not be used here, please use DBCloneTracked() instead");
  236. }
  237. return $this->DBCloneTracked_Internal();
  238. }
  239. public function DBCloneTracked(CMDBChange $oChange, $newKey = null)
  240. {
  241. self::$m_oCurrChange = $oChange;
  242. $this->DBCloneTracked_Internal($newKey);
  243. self::$m_oCurrChange = null;
  244. }
  245. protected function DBCloneTracked_Internal($newKey = null)
  246. {
  247. $newKey = parent::DBClone($newKey);
  248. $oClone = MetaModel::GetObject(get_class($this), $newKey);
  249. $oClone->RecordObjCreation(self::$m_oCurrChange);
  250. return $newKey;
  251. }
  252. public function DBUpdate()
  253. {
  254. if(!self::$m_oCurrChange)
  255. {
  256. throw new CoreException("DBUpdate() could not be used here, please use DBUpdateTracked() instead");
  257. }
  258. return $this->DBUpdateTracked_internal();
  259. }
  260. public function DBUpdateTracked(CMDBChange $oChange)
  261. {
  262. self::$m_oCurrChange = $oChange;
  263. $this->DBUpdateTracked_Internal();
  264. self::$m_oCurrChange = null;
  265. }
  266. protected function DBUpdateTracked_Internal()
  267. {
  268. // Copy the changes list before the update (the list should be reset afterwards)
  269. $aChanges = $this->ListChanges();
  270. if (count($aChanges) == 0)
  271. {
  272. //throw new CoreWarning("Attempting to update an unchanged object");
  273. return;
  274. }
  275. // Save the original values (will be reset to the new values when the object get written to the DB)
  276. $aOriginalValues = $this->m_aOrigValues;
  277. $ret = parent::DBUpdate();
  278. $this->RecordAttChanges(self::$m_oCurrChange, $aChanges, $aOriginalValues);
  279. return $ret;
  280. }
  281. public function DBDelete()
  282. {
  283. if(!self::$m_oCurrChange)
  284. {
  285. throw new CoreException("DBDelete() could not be used here, please use DBDeleteTracked() instead");
  286. }
  287. return $this->DBDeleteTracked_Internal();
  288. }
  289. public function DBDeleteTracked(CMDBChange $oChange)
  290. {
  291. self::$m_oCurrChange = $oChange;
  292. $this->DBDeleteTracked_Internal();
  293. self::$m_oCurrChange = null;
  294. }
  295. protected function DBDeleteTracked_Internal()
  296. {
  297. $prevkey = $this->GetKey();
  298. $ret = parent::DBDelete();
  299. $this->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  300. return $ret;
  301. }
  302. public static function BulkDelete(DBObjectSearch $oFilter)
  303. {
  304. if(!self::$m_oCurrChange)
  305. {
  306. throw new CoreException("BulkDelete() could not be used here, please use BulkDeleteTracked() instead");
  307. }
  308. return $this->BulkDeleteTracked_Internal($oFilter);
  309. }
  310. public static function BulkDeleteTracked(CMDBChange $oChange, DBObjectSearch $oFilter)
  311. {
  312. self::$m_oCurrChange = $oChange;
  313. $this->BulkDeleteTracked_Internal($oFilter);
  314. self::$m_oCurrChange = null;
  315. }
  316. protected static function BulkDeleteTracked_Internal(DBObjectSearch $oFilter)
  317. {
  318. throw new CoreWarning("Change tracking not tested for bulk operations");
  319. // Get the list of objects to delete (and record data before deleting the DB records)
  320. $oObjSet = new CMDBObjectSet($oFilter);
  321. $aObjAndKeys = array(); // array of id=>object
  322. while ($oItem = $oObjSet->Fetch())
  323. {
  324. $aObjAndKeys[$oItem->GetKey()] = $oItem;
  325. }
  326. $oObjSet->FreeResult();
  327. // Delete in one single efficient query
  328. $ret = parent::BulkDelete($oFilter);
  329. // Record... in many queries !!!
  330. foreach($aObjAndKeys as $prevkey=>$oItem)
  331. {
  332. $oItem->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  333. }
  334. return $ret;
  335. }
  336. public static function BulkUpdate(DBObjectSearch $oFilter, array $aValues)
  337. {
  338. if(!self::$m_oCurrChange)
  339. {
  340. throw new CoreException("BulkUpdate() could not be used here, please use BulkUpdateTracked() instead");
  341. }
  342. return $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  343. }
  344. public static function BulkUpdateTracked(CMDBChange $oChange, DBObjectSearch $oFilter, array $aValues)
  345. {
  346. self::$m_oCurrChange = $oChange;
  347. $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  348. self::$m_oCurrChange = null;
  349. }
  350. protected static function BulkUpdateTracked_Internal(DBObjectSearch $oFilter, array $aValues)
  351. {
  352. // $aValues is an array of $sAttCode => $value
  353. // Get the list of objects to update (and load it before doing the change)
  354. $oObjSet = new CMDBObjectSet($oFilter);
  355. $oObjSet->Load();
  356. // Keep track of the previous values (will be overwritten when the objects are synchronized with the DB)
  357. $aOriginalValues = array();
  358. $oObjSet->Rewind();
  359. while ($oItem = $oObjSet->Fetch())
  360. {
  361. $aOriginalValues[$oItem->GetKey()] = $oItem->m_aOrigValues;
  362. }
  363. // Update in one single efficient query
  364. $ret = parent::BulkUpdate($oFilter, $aValues);
  365. // Record... in many queries !!!
  366. $oObjSet->Rewind();
  367. while ($oItem = $oObjSet->Fetch())
  368. {
  369. $aChangedValues = $oItem->ListChangedValues($aValues);
  370. $oItem->RecordAttChanges(self::$m_oCurrChange, $aChangedValues, $aOriginalValues[$oItem->GetKey()]);
  371. }
  372. return $ret;
  373. }
  374. }
  375. /**
  376. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  377. *
  378. * @package iTopORM
  379. */
  380. class CMDBObjectSet extends DBObjectSet
  381. {
  382. // this is the public interface (?)
  383. // I have to define those constructors here... :-(
  384. // just to get the right object class in return.
  385. // I have to think again to those things: maybe it will work fine if a have a constructor define here (?)
  386. static public function FromScratch($sClass)
  387. {
  388. $oFilter = new CMDBSearchFilter($sClass);
  389. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  390. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  391. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  392. $oRetSet->m_bLoaded = true; // no DB load
  393. return $oRetSet;
  394. }
  395. static public function FromArray($sClass, $aObjects)
  396. {
  397. $oFilter = new CMDBSearchFilter($sClass);
  398. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  399. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  400. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  401. $oRetSet->m_bLoaded = true; // no DB load
  402. $oRetSet->AddObjectArray($aObjects);
  403. return $oRetSet;
  404. }
  405. static public function FromArrayAssoc($aClasses, $aObjects)
  406. {
  407. // In a perfect world, we should create a complete tree of DBObjectSearch,
  408. // but as we lack most of the information related to the objects,
  409. // let's create one search definition
  410. $sClass = reset($aClasses);
  411. $sAlias = key($aClasses);
  412. $oFilter = new CMDBSearchFilter($sClass, $sAlias);
  413. $oRetSet = new CMDBObjectSet($oFilter);
  414. $oRetSet->m_bLoaded = true; // no DB load
  415. foreach($aObjects as $rowIndex => $aObjectsByClassAlias)
  416. {
  417. $oRetSet->AddObjectExtended($aObjectsByClassAlias);
  418. }
  419. return $oRetSet;
  420. }
  421. }
  422. /**
  423. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  424. *
  425. * @package iTopORM
  426. */
  427. class CMDBSearchFilter extends DBObjectSearch
  428. {
  429. // this is the public interface (?)
  430. }
  431. ?>