cmdbobject.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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('dict.class.inc.php');
  34. require_once('attributedef.class.inc.php');
  35. require_once('filterdef.class.inc.php');
  36. require_once('stimulus.class.inc.php');
  37. require_once('valuesetdef.class.inc.php');
  38. require_once('MyHelpers.class.inc.php');
  39. require_once('expression.class.inc.php');
  40. require_once('cmdbsource.class.inc.php');
  41. require_once('sqlquery.class.inc.php');
  42. require_once('oql/oqlquery.class.inc.php');
  43. require_once('oql/oqlexception.class.inc.php');
  44. require_once('oql/oql-parser.php');
  45. require_once('oql/oql-lexer.php');
  46. require_once('oql/oqlinterpreter.class.inc.php');
  47. require_once('dbobject.class.php');
  48. require_once('dbobjectsearch.class.php');
  49. require_once('dbobjectset.class.php');
  50. // db change tracking data model
  51. require_once('cmdbchange.class.inc.php');
  52. require_once('cmdbchangeop.class.inc.php');
  53. // customization data model
  54. // Romain: temporary moved into application.inc.php (see explanations there)
  55. //require_once('trigger.class.inc.php');
  56. //require_once('action.class.inc.php');
  57. // application log
  58. // Romain: temporary moved into application.inc.php (see explanations there)
  59. //require_once('event.class.inc.php');
  60. require_once('csvparser.class.inc.php');
  61. require_once('bulkchange.class.inc.php');
  62. //
  63. // Error handling
  64. // To be finalized... or removed ?
  65. //
  66. function cmdbErrorHandler($errno, $errstr, $errfile, $errline)
  67. {
  68. // font-family: Courier-New, Courier, Arial, Helevtica;
  69. $sErrorStyle = "
  70. background-color: #ffaaaa;
  71. color: #000000;
  72. border: 1px dashed #000000;
  73. padding: 0.25em;
  74. margin-top: 1em;
  75. ";
  76. $sCallStackStyle = "
  77. font-size: smaller;
  78. background-color: #ffcccc;
  79. color: #000000;
  80. border: 1px dashed #000000;
  81. padding: 0.25em;
  82. margin-top: 1em;
  83. ";
  84. switch ($errno)
  85. {
  86. case E_USER_ERROR:
  87. case E_ERROR:
  88. echo "<div style=\"$sErrorStyle\">\n";
  89. echo "<b>Error</b> [$errno] $errstr<br />\n";
  90. echo "<div style=\"$sCallStackStyle\">\n";
  91. MyHelpers::dump_callstack(1);
  92. echo "</div>\n";
  93. echo "Hereafter the biz model internals:<br />\n";
  94. echo "<pre>\n";
  95. MetaModel::static_var_dump();
  96. echo "</pre>\n";
  97. echo "Aborting...<br />\n";
  98. echo "</div>\n";
  99. exit(1);
  100. break;
  101. case E_USER_WARNING:
  102. case E_WARNING:
  103. echo "<div style=\"background-color:#FAA;\">\n";
  104. echo "<b>Warning</b> [$errno] $errstr<br />\n";
  105. echo "<div style=\"background-color:#FCC;\">\n";
  106. MyHelpers::dump_callstack(1);
  107. echo "</div>\n";
  108. echo "</div>\n";
  109. break;
  110. case E_USER_NOTICE:
  111. case E_NOTICE:
  112. echo "<div style=\"background-color:#FAA;\">\n";
  113. echo "<b>Notice</b> [$errno] $errstr<br />\n";
  114. echo "<div style=\"background-color:#FCC;\">\n";
  115. MyHelpers::dump_callstack(1);
  116. echo "</div>\n";
  117. echo "</div>\n";
  118. break;
  119. default:
  120. echo "Unknown error type: [$errno] $errstr<br />\n";
  121. MyHelpers::dump_callstack(1);
  122. break;
  123. }
  124. }
  125. error_reporting(E_ALL | E_STRICT);
  126. //set_error_handler("cmdbErrorHandler");
  127. //
  128. //
  129. //
  130. /**
  131. * A persistent object, which changes are accurately recorded
  132. *
  133. * @package iTopORM
  134. */
  135. abstract class CMDBObject extends DBObject
  136. {
  137. protected $m_datCreated;
  138. protected $m_datUpdated;
  139. protected static $m_oCurrChange = null;
  140. private function RecordObjCreation(CMDBChange $oChange)
  141. {
  142. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpCreate");
  143. $oMyChangeOp->Set("change", $oChange->GetKey());
  144. $oMyChangeOp->Set("objclass", get_class($this));
  145. $oMyChangeOp->Set("objkey", $this->GetKey());
  146. $iId = $oMyChangeOp->DBInsertNoReload();
  147. }
  148. private function RecordObjDeletion(CMDBChange $oChange, $objkey)
  149. {
  150. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpDelete");
  151. $oMyChangeOp->Set("change", $oChange->GetKey());
  152. $oMyChangeOp->Set("objclass", get_class($this));
  153. $oMyChangeOp->Set("objkey", $objkey);
  154. $iId = $oMyChangeOp->DBInsertNoReload();
  155. }
  156. private function RecordAttChanges(CMDBChange $oChange, array $aValues, array $aOrigValues)
  157. {
  158. // $aValues is an array of $sAttCode => $value
  159. //
  160. foreach ($aValues as $sAttCode=> $value)
  161. {
  162. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  163. if ($oAttDef->IsLinkSet()) continue; // #@# temporary
  164. if ($oAttDef instanceOf AttributeOneWayPassword)
  165. {
  166. // One Way encrypted passwords' history is stored -one way- encrypted
  167. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeOneWayPassword");
  168. $oMyChangeOp->Set("change", $oChange->GetKey());
  169. $oMyChangeOp->Set("objclass", get_class($this));
  170. $oMyChangeOp->Set("objkey", $this->GetKey());
  171. $oMyChangeOp->Set("attcode", $sAttCode);
  172. if (array_key_exists($sAttCode, $aOrigValues))
  173. {
  174. $original = $aOrigValues[$sAttCode];
  175. }
  176. else
  177. {
  178. $original = '';
  179. }
  180. $oMyChangeOp->Set("prev_pwd", $original);
  181. $iId = $oMyChangeOp->DBInsertNoReload();
  182. }
  183. elseif ($oAttDef instanceOf AttributeEncryptedString)
  184. {
  185. // Encrypted string history is stored encrypted
  186. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeEncrypted");
  187. $oMyChangeOp->Set("change", $oChange->GetKey());
  188. $oMyChangeOp->Set("objclass", get_class($this));
  189. $oMyChangeOp->Set("objkey", $this->GetKey());
  190. $oMyChangeOp->Set("attcode", $sAttCode);
  191. if (array_key_exists($sAttCode, $aOrigValues))
  192. {
  193. $original = $aOrigValues[$sAttCode];
  194. }
  195. else
  196. {
  197. $original = '';
  198. }
  199. $oMyChangeOp->Set("prevdata", $original);
  200. $iId = $oMyChangeOp->DBInsertNoReload();
  201. }
  202. elseif ($oAttDef instanceOf AttributeBlob)
  203. {
  204. // Data blobs
  205. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeBlob");
  206. $oMyChangeOp->Set("change", $oChange->GetKey());
  207. $oMyChangeOp->Set("objclass", get_class($this));
  208. $oMyChangeOp->Set("objkey", $this->GetKey());
  209. $oMyChangeOp->Set("attcode", $sAttCode);
  210. if (array_key_exists($sAttCode, $aOrigValues))
  211. {
  212. $original = $aOrigValues[$sAttCode];
  213. }
  214. else
  215. {
  216. $original = new ormDocument();
  217. }
  218. $oMyChangeOp->Set("prevdata", $original);
  219. $iId = $oMyChangeOp->DBInsertNoReload();
  220. }
  221. elseif ($oAttDef instanceOf AttributeText)
  222. {
  223. // Data blobs
  224. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeText");
  225. $oMyChangeOp->Set("change", $oChange->GetKey());
  226. $oMyChangeOp->Set("objclass", get_class($this));
  227. $oMyChangeOp->Set("objkey", $this->GetKey());
  228. $oMyChangeOp->Set("attcode", $sAttCode);
  229. if (array_key_exists($sAttCode, $aOrigValues))
  230. {
  231. $original = $aOrigValues[$sAttCode];
  232. }
  233. else
  234. {
  235. $original = null;
  236. }
  237. $oMyChangeOp->Set("prevdata", $original);
  238. $iId = $oMyChangeOp->DBInsertNoReload();
  239. }
  240. else
  241. {
  242. // Scalars
  243. //
  244. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar");
  245. $oMyChangeOp->Set("change", $oChange->GetKey());
  246. $oMyChangeOp->Set("objclass", get_class($this));
  247. $oMyChangeOp->Set("objkey", $this->GetKey());
  248. $oMyChangeOp->Set("attcode", $sAttCode);
  249. if (array_key_exists($sAttCode, $aOrigValues))
  250. {
  251. $sOriginalValue = $aOrigValues[$sAttCode];
  252. }
  253. else
  254. {
  255. $sOriginalValue = 'undefined';
  256. }
  257. $oMyChangeOp->Set("oldvalue", $sOriginalValue);
  258. $oMyChangeOp->Set("newvalue", $value);
  259. $iId = $oMyChangeOp->DBInsertNoReload();
  260. }
  261. }
  262. }
  263. public function DBInsert()
  264. {
  265. if(!is_object(self::$m_oCurrChange))
  266. {
  267. throw new CoreException("DBInsert() could not be used here, please use DBInsertTracked() instead");
  268. }
  269. return $this->DBInsertTracked_Internal();
  270. }
  271. public function DBInsertTracked(CMDBChange $oChange)
  272. {
  273. self::$m_oCurrChange = $oChange;
  274. $ret = $this->DBInsertTracked_Internal();
  275. self::$m_oCurrChange = null;
  276. return $ret;
  277. }
  278. public function DBInsertTrackedNoReload(CMDBChange $oChange)
  279. {
  280. self::$m_oCurrChange = $oChange;
  281. $ret = $this->DBInsertTracked_Internal(true);
  282. self::$m_oCurrChange = null;
  283. return $ret;
  284. }
  285. protected function DBInsertTracked_Internal($bDoNotReload = false)
  286. {
  287. if ($bDoNotReload)
  288. {
  289. $ret = parent::DBInsertNoReload();
  290. }
  291. else
  292. {
  293. $ret = parent::DBInsert();
  294. }
  295. $this->RecordObjCreation(self::$m_oCurrChange);
  296. return $ret;
  297. }
  298. public function DBClone($newKey = null)
  299. {
  300. if(!self::$m_oCurrChange)
  301. {
  302. throw new CoreException("DBClone() could not be used here, please use DBCloneTracked() instead");
  303. }
  304. return $this->DBCloneTracked_Internal();
  305. }
  306. public function DBCloneTracked(CMDBChange $oChange, $newKey = null)
  307. {
  308. self::$m_oCurrChange = $oChange;
  309. $this->DBCloneTracked_Internal($newKey);
  310. self::$m_oCurrChange = null;
  311. }
  312. protected function DBCloneTracked_Internal($newKey = null)
  313. {
  314. $newKey = parent::DBClone($newKey);
  315. $oClone = MetaModel::GetObject(get_class($this), $newKey);
  316. $oClone->RecordObjCreation(self::$m_oCurrChange);
  317. return $newKey;
  318. }
  319. public function DBUpdate()
  320. {
  321. if(!self::$m_oCurrChange)
  322. {
  323. throw new CoreException("DBUpdate() could not be used here, please use DBUpdateTracked() instead");
  324. }
  325. return $this->DBUpdateTracked_internal();
  326. }
  327. public function DBUpdateTracked(CMDBChange $oChange)
  328. {
  329. self::$m_oCurrChange = $oChange;
  330. $this->DBUpdateTracked_Internal();
  331. self::$m_oCurrChange = null;
  332. }
  333. protected function DBUpdateTracked_Internal()
  334. {
  335. // Copy the changes list before the update (the list should be reset afterwards)
  336. $aChanges = $this->ListChanges();
  337. if (count($aChanges) == 0)
  338. {
  339. //throw new CoreWarning("Attempting to update an unchanged object");
  340. return;
  341. }
  342. // Save the original values (will be reset to the new values when the object get written to the DB)
  343. $aOriginalValues = $this->m_aOrigValues;
  344. $ret = parent::DBUpdate();
  345. $this->RecordAttChanges(self::$m_oCurrChange, $aChanges, $aOriginalValues);
  346. return $ret;
  347. }
  348. public function DBDelete()
  349. {
  350. if(!self::$m_oCurrChange)
  351. {
  352. throw new CoreException("DBDelete() could not be used here, please use DBDeleteTracked() instead");
  353. }
  354. return $this->DBDeleteTracked_Internal();
  355. }
  356. public function DBDeleteTracked(CMDBChange $oChange)
  357. {
  358. self::$m_oCurrChange = $oChange;
  359. $this->DBDeleteTracked_Internal();
  360. self::$m_oCurrChange = null;
  361. }
  362. protected function DBDeleteTracked_Internal()
  363. {
  364. $prevkey = $this->GetKey();
  365. $ret = parent::DBDelete();
  366. $this->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  367. return $ret;
  368. }
  369. public static function BulkDelete(DBObjectSearch $oFilter)
  370. {
  371. if(!self::$m_oCurrChange)
  372. {
  373. throw new CoreException("BulkDelete() could not be used here, please use BulkDeleteTracked() instead");
  374. }
  375. return $this->BulkDeleteTracked_Internal($oFilter);
  376. }
  377. public static function BulkDeleteTracked(CMDBChange $oChange, DBObjectSearch $oFilter)
  378. {
  379. self::$m_oCurrChange = $oChange;
  380. $this->BulkDeleteTracked_Internal($oFilter);
  381. self::$m_oCurrChange = null;
  382. }
  383. protected static function BulkDeleteTracked_Internal(DBObjectSearch $oFilter)
  384. {
  385. throw new CoreWarning("Change tracking not tested for bulk operations");
  386. // Get the list of objects to delete (and record data before deleting the DB records)
  387. $oObjSet = new CMDBObjectSet($oFilter);
  388. $aObjAndKeys = array(); // array of id=>object
  389. while ($oItem = $oObjSet->Fetch())
  390. {
  391. $aObjAndKeys[$oItem->GetKey()] = $oItem;
  392. }
  393. $oObjSet->FreeResult();
  394. // Delete in one single efficient query
  395. $ret = parent::BulkDelete($oFilter);
  396. // Record... in many queries !!!
  397. foreach($aObjAndKeys as $prevkey=>$oItem)
  398. {
  399. $oItem->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  400. }
  401. return $ret;
  402. }
  403. public static function BulkUpdate(DBObjectSearch $oFilter, array $aValues)
  404. {
  405. if(!self::$m_oCurrChange)
  406. {
  407. throw new CoreException("BulkUpdate() could not be used here, please use BulkUpdateTracked() instead");
  408. }
  409. return $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  410. }
  411. public static function BulkUpdateTracked(CMDBChange $oChange, DBObjectSearch $oFilter, array $aValues)
  412. {
  413. self::$m_oCurrChange = $oChange;
  414. $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  415. self::$m_oCurrChange = null;
  416. }
  417. protected static function BulkUpdateTracked_Internal(DBObjectSearch $oFilter, array $aValues)
  418. {
  419. // $aValues is an array of $sAttCode => $value
  420. // Get the list of objects to update (and load it before doing the change)
  421. $oObjSet = new CMDBObjectSet($oFilter);
  422. $oObjSet->Load();
  423. // Keep track of the previous values (will be overwritten when the objects are synchronized with the DB)
  424. $aOriginalValues = array();
  425. $oObjSet->Rewind();
  426. while ($oItem = $oObjSet->Fetch())
  427. {
  428. $aOriginalValues[$oItem->GetKey()] = $oItem->m_aOrigValues;
  429. }
  430. // Update in one single efficient query
  431. $ret = parent::BulkUpdate($oFilter, $aValues);
  432. // Record... in many queries !!!
  433. $oObjSet->Rewind();
  434. while ($oItem = $oObjSet->Fetch())
  435. {
  436. $aChangedValues = $oItem->ListChangedValues($aValues);
  437. $oItem->RecordAttChanges(self::$m_oCurrChange, $aChangedValues, $aOriginalValues[$oItem->GetKey()]);
  438. }
  439. return $ret;
  440. }
  441. }
  442. /**
  443. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  444. *
  445. * @package iTopORM
  446. */
  447. class CMDBObjectSet extends DBObjectSet
  448. {
  449. // this is the public interface (?)
  450. // I have to define those constructors here... :-(
  451. // just to get the right object class in return.
  452. // I have to think again to those things: maybe it will work fine if a have a constructor define here (?)
  453. static public function FromScratch($sClass)
  454. {
  455. $oFilter = new CMDBSearchFilter($sClass);
  456. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  457. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  458. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  459. $oRetSet->m_bLoaded = true; // no DB load
  460. return $oRetSet;
  461. }
  462. static public function FromArray($sClass, $aObjects)
  463. {
  464. $oFilter = new CMDBSearchFilter($sClass);
  465. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  466. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  467. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  468. $oRetSet->m_bLoaded = true; // no DB load
  469. $oRetSet->AddObjectArray($aObjects);
  470. return $oRetSet;
  471. }
  472. static public function FromArrayAssoc($aClasses, $aObjects)
  473. {
  474. // In a perfect world, we should create a complete tree of DBObjectSearch,
  475. // but as we lack most of the information related to the objects,
  476. // let's create one search definition
  477. $sClass = reset($aClasses);
  478. $sAlias = key($aClasses);
  479. $oFilter = new CMDBSearchFilter($sClass, $sAlias);
  480. $oRetSet = new CMDBObjectSet($oFilter);
  481. $oRetSet->m_bLoaded = true; // no DB load
  482. foreach($aObjects as $rowIndex => $aObjectsByClassAlias)
  483. {
  484. $oRetSet->AddObjectExtended($aObjectsByClassAlias);
  485. }
  486. return $oRetSet;
  487. }
  488. }
  489. /**
  490. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  491. *
  492. * @package iTopORM
  493. */
  494. class CMDBSearchFilter extends DBObjectSearch
  495. {
  496. // this is the public interface (?)
  497. }
  498. ?>