webservices.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. <?php
  2. require_once('../webservices/itopsoaptypes.class.inc.php');
  3. /**
  4. * Create Ticket web service
  5. * Web Service API wrapper
  6. *
  7. * @package iTopORM
  8. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  9. * @author Denis Flaven <denisflave@free.fr>
  10. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  11. * @link www.itop.com
  12. * @since 1.0
  13. * @version 1.1.1.1 $
  14. */
  15. class WebServiceResult
  16. {
  17. /**
  18. * Overall status
  19. *
  20. * @var m_bStatus
  21. */
  22. public $m_bStatus;
  23. /**
  24. * Error log
  25. *
  26. * @var m_aErrors
  27. */
  28. public $m_aErrors;
  29. /**
  30. * Warning log
  31. *
  32. * @var m_aWarnings
  33. */
  34. public $m_aWarnings;
  35. /**
  36. * Information log
  37. *
  38. * @var m_aInfos
  39. */
  40. public $m_aInfos;
  41. /**
  42. * Constructor
  43. *
  44. * @param status $bStatus
  45. */
  46. public function __construct()
  47. {
  48. $this->m_bStatus = true;
  49. $this->m_aResult = array();
  50. $this->m_aErrors = array();
  51. $this->m_aWarnings = array();
  52. $this->m_aInfos = array();
  53. }
  54. public function ToSoapStructure()
  55. {
  56. $aResults = array();
  57. foreach($this->m_aResult as $sLabel => $aData)
  58. {
  59. $aValues = array();
  60. foreach($aData as $sKey => $value)
  61. {
  62. $aValues[] = new SoapResultData($sKey, $value);
  63. }
  64. $aResults[] = new SoapResultMessage($sLabel, $aValues);
  65. }
  66. $aInfos = array();
  67. foreach($this->m_aInfos as $sMessage)
  68. {
  69. $aInfos[] = new SoapLogMessage($sMessage);
  70. }
  71. $aWarnings = array();
  72. foreach($this->m_aWarnings as $sMessage)
  73. {
  74. $aWarnings[] = new SoapLogMessage($sMessage);
  75. }
  76. $aErrors = array();
  77. foreach($this->m_aErrors as $sMessage)
  78. {
  79. $aErrors[] = new SoapLogMessage($sMessage);
  80. }
  81. $oRet = new SOAPResult(
  82. $this->m_bStatus,
  83. $aResults,
  84. new SOAPResultLog($aErrors),
  85. new SOAPResultLog($aWarnings),
  86. new SOAPResultLog($aInfos)
  87. );
  88. return $oRet;
  89. }
  90. /**
  91. * Did the current processing encounter a stopper issue ?
  92. *
  93. * @return bool
  94. */
  95. public function IsOk()
  96. {
  97. return $this->m_bStatus;
  98. }
  99. /**
  100. * Add result details - object reference
  101. *
  102. * @param string sLabel
  103. * @param object oObject
  104. */
  105. public function AddResultObject($sLabel, $oObject)
  106. {
  107. $this->m_aResult[$sLabel] = array(
  108. 'id' => $oObject->GetKey(),
  109. 'name' => $oObject->GetName(),
  110. 'url' => $oObject->GetHyperlink(),
  111. );
  112. }
  113. /**
  114. * Log an error
  115. *
  116. * @param string sDescription
  117. */
  118. public function LogError($sDescription)
  119. {
  120. $this->m_aErrors[] = $sDescription;
  121. // Note: SOAP do transform false into null
  122. $this->m_bStatus = 0;
  123. }
  124. /**
  125. * Log a warning
  126. *
  127. * @param string sDescription
  128. */
  129. public function LogWarning($sDescription)
  130. {
  131. $this->m_aWarnings[] = $sDescription;
  132. }
  133. /**
  134. * Log an error or a warning
  135. *
  136. * @param string sDescription
  137. * @param boolean bIsStopper
  138. */
  139. public function LogIssue($sDescription, $bIsStopper = true)
  140. {
  141. if ($bIsStopper) $this->LogError($sDescription);
  142. else $this->LogWarning($sDescription);
  143. }
  144. /**
  145. * Log operation details
  146. *
  147. * @param description $sDescription
  148. */
  149. public function LogInfo($sDescription)
  150. {
  151. $this->m_aInfos[] = $sDescription;
  152. }
  153. protected static function LogToText($aLog)
  154. {
  155. return implode("\n", $aLog);
  156. }
  157. public function GetInfoAsText()
  158. {
  159. return self::LogToText($this->m_aInfos);
  160. }
  161. public function GetWarningsAsText()
  162. {
  163. return self::LogToText($this->m_aWarnings);
  164. }
  165. public function GetErrorsAsText()
  166. {
  167. return self::LogToText($this->m_aErrors);
  168. }
  169. public function GetReturnedDataAsText()
  170. {
  171. $sRet = '';
  172. foreach ($this->m_aResult as $sKey => $value)
  173. {
  174. $sRet .= "===== $sKey =====\n";
  175. $sRet .= print_r($value, true);
  176. }
  177. return $sRet;
  178. }
  179. }
  180. class WebServiceResultFailedLogin extends WebServiceResult
  181. {
  182. public function __construct($sLogin)
  183. {
  184. parent::__construct();
  185. $this->LogError("Wrong credentials: '$sLogin'");
  186. }
  187. }
  188. class WebServices
  189. {
  190. /**
  191. * Helper to log a service delivery
  192. *
  193. * @param string sVerb
  194. * @param array aArgs
  195. * @param WebServiceResult oRes
  196. *
  197. */
  198. protected function LogUsage($sVerb, $oRes)
  199. {
  200. $oLog = new EventWebService();
  201. if ($oRes->IsOk())
  202. {
  203. $oLog->Set('message', $sVerb.' was successfully invoked');
  204. }
  205. else
  206. {
  207. $oLog->Set('message', $sVerb.' returned errors');
  208. }
  209. $oLog->Set('userinfo', UserRights::GetUser());
  210. $oLog->Set('verb', $sVerb);
  211. $oLog->Set('result', $oRes->IsOk());
  212. $oLog->Set('log_info', $oRes->GetInfoAsText());
  213. $oLog->Set('log_warning', $oRes->GetWarningsAsText());
  214. $oLog->Set('log_error', $oRes->GetErrorsAsText());
  215. $oLog->Set('data', $oRes->GetReturnedDataAsText());
  216. $oLog->DBInsertNoReload();
  217. }
  218. /**
  219. * Helper to set a scalar attribute
  220. *
  221. * @param string sAttCode
  222. * @param scalar value
  223. * @param DBObject oTargetObj
  224. * @param WebServiceResult oRes
  225. *
  226. */
  227. protected function MyObjectSetScalar($sAttCode, $sParamName, $value, &$oTargetObj, &$oRes)
  228. {
  229. if ($oTargetObj->CheckValue($sAttCode, $value))
  230. {
  231. $oTargetObj->Set($sAttCode, $value);
  232. }
  233. else
  234. {
  235. $aAllowedValues = MetaModel::GetAllowedValues_att(get_class($oTargetObj), $sAttCode);
  236. $sValues = implode(', ', $aAllowedValues);
  237. $oRes->LogError("Parameter $sParamName: found '$value' while expecting a value in {".$sValues."}");
  238. }
  239. }
  240. /**
  241. * Helper to set an external key
  242. *
  243. * @param string sAttCode
  244. * @param array aExtKeyDesc
  245. * @param DBObject oTargetObj
  246. * @param WebServiceResult oRes
  247. *
  248. */
  249. protected function MyObjectSetExternalKey($sAttCode, $sParamName, $aExtKeyDesc, &$oTargetObj, &$oRes)
  250. {
  251. $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  252. $bIsMandatory = !$oExtKey->IsNullAllowed();
  253. if (is_null($aExtKeyDesc))
  254. {
  255. if ($bIsMandatory)
  256. {
  257. $oRes->LogError("Parameter $sParamName: found null for a mandatory key");
  258. }
  259. else
  260. {
  261. // skip silently
  262. return;
  263. }
  264. }
  265. if (count($aExtKeyDesc) == 0)
  266. {
  267. $oRes->LogIssue("Parameter $sParamName: no search condition has been specified", $bIsMandatory);
  268. return;
  269. }
  270. $sKeyClass = $oExtKey->GetTargetClass();
  271. $oReconFilter = new CMDBSearchFilter($sKeyClass);
  272. foreach ($aExtKeyDesc as $sForeignAttCode => $value)
  273. {
  274. if (!MetaModel::IsValidFilterCode($sKeyClass, $sForeignAttCode))
  275. {
  276. $aCodes = array_keys(MetaModel::GetClassFilterDefs($sKeyClass));
  277. $sMsg = "Parameter $sParamName: '$sForeignAttCode' is not a valid filter code for class '$sKeyClass', expecting a value in {".implode(', ', $aCodes)."}";
  278. $oRes->LogIssue($sMsg, $bIsMandatory);
  279. }
  280. // The foreign attribute is one of our reconciliation key
  281. $oReconFilter->AddCondition($sForeignAttCode, $value, '=');
  282. }
  283. $oExtObjects = new CMDBObjectSet($oReconFilter);
  284. switch($oExtObjects->Count())
  285. {
  286. case 0:
  287. $sMsg = "Parameter $sParamName: no match (searched: '".$oReconFilter->ToOQL()."')";
  288. $oRes->LogIssue($sMsg, $bIsMandatory);
  289. break;
  290. case 1:
  291. // Do change the external key attribute
  292. $oForeignObj = $oExtObjects->Fetch();
  293. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  294. // Report it (no need to report if the object already had this value
  295. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  296. {
  297. $oRes->LogInfo("Parameter $sParamName: found match ".get_class($oForeignObj)."::".$oForeignObj->GetKey()." '".$oForeignObj->GetName()."'");
  298. }
  299. break;
  300. default:
  301. $sMsg = "Parameter $sParamName: Found ".$oExtObjects->Count()." matches (searched: '".$oReconFilter->ToOQL()."')";
  302. $oRes->LogIssue($sMsg, $bIsMandatory);
  303. }
  304. }
  305. /**
  306. * Helper to link objects
  307. *
  308. * @param string sLinkAttCode
  309. * @param string sLinkedClass
  310. * @param array $aLinkList
  311. * @param DBObject oTargetObj
  312. * @param WebServiceResult oRes
  313. *
  314. * @return array List of objects that could not be found
  315. */
  316. protected function AddLinkedObjects($sLinkAttCode, $sParamName, $sLinkedClass, $aLinkList, &$oTargetObj, &$oRes)
  317. {
  318. $oLinkAtt = MetaModel::GetAttributeDef(get_class($oTargetObj), $sLinkAttCode);
  319. $sLinkClass = $oLinkAtt->GetLinkedClass();
  320. $sExtKeyToItem = $oLinkAtt->GetExtKeyToRemote();
  321. $aItemsFound = array();
  322. $aItemsNotFound = array();
  323. if (is_null($aLinkList))
  324. {
  325. return $aItemsNotFound;
  326. }
  327. foreach ($aLinkList as $aItemData)
  328. {
  329. if (!array_key_exists('class', $aItemData))
  330. {
  331. $oRes->LogWarning("Parameter $sParamName: missing 'class' specification");
  332. continue; // skip
  333. }
  334. $sTargetClass = $aItemData['class'];
  335. if (!MetaModel::IsValidClass($sTargetClass))
  336. {
  337. $oRes->LogError("Parameter $sParamName: invalid class '$sTargetClass'");
  338. continue; // skip
  339. }
  340. if (!MetaModel::IsParentClass($sLinkedClass, $sTargetClass))
  341. {
  342. $oRes->LogError("Parameter $sParamName: '$sTargetClass' is not a child class of '$sLinkedClass'");
  343. continue; // skip
  344. }
  345. $oReconFilter = new CMDBSearchFilter($sTargetClass);
  346. $aCIStringDesc = array();
  347. foreach ($aItemData['search'] as $sAttCode => $value)
  348. {
  349. if (!MetaModel::IsValidFilterCode($sTargetClass, $sAttCode))
  350. {
  351. $aCodes = array_keys(MetaModel::GetClassFilterDefs($sTargetClass));
  352. $oRes->LogError("Parameter $sParamName: '$sAttCode' is not a valid filter code for class '$sTargetClass', expecting a value in {".implode(', ', $aCodes)."}");
  353. continue 2; // skip the entire item
  354. }
  355. $aCIStringDesc[] = "$sAttCode: $value";
  356. // The attribute is one of our reconciliation key
  357. $oReconFilter->AddCondition($sAttCode, $value, '=');
  358. }
  359. if (count($aCIStringDesc) == 1)
  360. {
  361. // take the last and unique value to describe the object
  362. $sItemDesc = $value;
  363. }
  364. else
  365. {
  366. // describe the object by the given keys
  367. $sItemDesc = $sTargetClass.'('.implode('/', $aCIStringDesc).')';
  368. }
  369. $oExtObjects = new CMDBObjectSet($oReconFilter);
  370. switch($oExtObjects->Count())
  371. {
  372. case 0:
  373. $oRes->LogWarning("Parameter $sParamName: object to link $sLinkedClass / $sItemDesc could not be found (searched: '".$oReconFilter->ToOQL()."')");
  374. $aItemsNotFound[] = $sItemDesc;
  375. break;
  376. case 1:
  377. $aItemsFound[] = array (
  378. 'object' => $oExtObjects->Fetch(),
  379. 'link_values' => @$aItemData['link_values'],
  380. 'desc' => $sItemDesc,
  381. );
  382. break;
  383. default:
  384. $oRes->LogWarning("Parameter $sParamName: Found ".$oExtObjects->Count()." matches for item '$sItemDesc' (searched: '".$oReconFilter->ToOQL()."')");
  385. $aItemsNotFound[] = $sItemDesc;
  386. }
  387. }
  388. if (count($aItemsFound) > 0)
  389. {
  390. $aLinks = array();
  391. foreach($aItemsFound as $aItemData)
  392. {
  393. $oLink = MetaModel::NewObject($sLinkClass);
  394. $oLink->Set($sExtKeyToItem, $aItemData['object']->GetKey());
  395. foreach($aItemData['link_values'] as $sKey => $value)
  396. {
  397. if(!MetaModel::IsValidAttCode($sLinkClass, $sKey))
  398. {
  399. $oRes->LogWarning("Parameter $sParamName: Attaching item '".$aItemData['desc']."', the attribute code '$sKey' is not valid ; check the class '$sLinkClass'");
  400. }
  401. else
  402. {
  403. $oLink->Set($sKey, $value);
  404. }
  405. }
  406. $aLinks[] = $oLink;
  407. }
  408. $oImpactedInfraSet = DBObjectSet::FromArray($sLinkClass, $aLinks);
  409. $oTargetObj->Set($sLinkAttCode, $oImpactedInfraSet);
  410. }
  411. return $aItemsNotFound;
  412. }
  413. protected function MyObjectInsert($oTargetObj, $sResultLabel, $oChange, &$oRes)
  414. {
  415. if ($oRes->IsOk())
  416. {
  417. list($bRes, $aIssues) = $oTargetObj->CheckToInsert();
  418. if ($bRes)
  419. {
  420. $iId = $oTargetObj->DBInsertTrackedNoReload($oChange);
  421. $oRes->LogInfo("Created object ".get_class($$oTargetObj)."::$iId");
  422. $oRes->AddResultObject($sResultLabel, $oTargetObj);
  423. }
  424. else
  425. {
  426. $oRes->LogError("The ticket could not be created due to forbidden values (or inconsistent values)");
  427. }
  428. }
  429. }
  430. static protected function SoapStructToExternalKeySearch(SoapExternalKeySearch $oExternalKeySearch)
  431. {
  432. if (is_null($oExternalKeySearch)) return null;
  433. $aRes = array();
  434. foreach($oExternalKeySearch->conditions as $oSearchCondition)
  435. {
  436. $aRes[$oSearchCondition->attcode] = $oSearchCondition->value;
  437. }
  438. return $aRes;
  439. }
  440. static protected function SoapStructToLinkCreationSpec(SoapLinkCreationSpec $oLinkCreationSpec)
  441. {
  442. $aRes = array
  443. (
  444. 'class' => $oLinkCreationSpec->class,
  445. 'search' => array(),
  446. 'link_values' => array(),
  447. );
  448. foreach($oLinkCreationSpec->conditions as $oSearchCondition)
  449. {
  450. $aRes['search'][$oSearchCondition->attcode] = $oSearchCondition->value;
  451. }
  452. foreach($oLinkCreationSpec->attributes as $oAttributeValue)
  453. {
  454. $aRes['link_values'][$oAttributeValue->attcode] = $oAttributeValue->value;
  455. }
  456. return $aRes;
  457. }
  458. /**
  459. * Get the server version (TODO: get it dynamically, where ?)
  460. *
  461. * @return WebServiceResult
  462. */
  463. public function GetVersion()
  464. {
  465. return "0.8";
  466. }
  467. public function CreateIncidentTicket($sLogin, $sPassword, $sType, $sDescription, $sInitialSituation, $sImpact, $oCallerDesc, $oCustomerDesc, $oWorkgroupDesc, $aSOAPImpactedCIs, $sSeverity)
  468. {
  469. if (!UserRights::Login($sLogin, $sPassword))
  470. {
  471. $oRes = new WebServiceResultFailedLogin($sLogin);
  472. $this->LogUsage(__FUNCTION__, $oRes);
  473. return $oRes->ToSoapStructure();
  474. }
  475. $aCallerDesc = self::SoapStructToExternalKeySearch($oCallerDesc);
  476. $aCustomerDesc = self::SoapStructToExternalKeySearch($oCustomerDesc);
  477. $aWorkgroupDesc = self::SoapStructToExternalKeySearch($oWorkgroupDesc);
  478. $aImpactedCIs = array();
  479. foreach($aSOAPImpactedCIs as $oImpactedCIs)
  480. {
  481. $aImpactedCIs[] = self::SoapStructToLinkCreationSpec($oImpactedCIs);
  482. }
  483. $oRes = $this->_CreateIncidentTicket
  484. (
  485. $sType,
  486. $sDescription,
  487. $sInitialSituation,
  488. $sImpact,
  489. $aCallerDesc,
  490. $aCustomerDesc,
  491. $aWorkgroupDesc,
  492. $aImpactedCIs,
  493. $sSeverity
  494. );
  495. return $oRes->ToSoapStructure();
  496. }
  497. /**
  498. * Create an incident ticket from a monitoring system
  499. * Some CIs might be specified (by their name/IP)
  500. *
  501. * @param string sDecription
  502. * @param string sInitialSituation
  503. * @param array aCallerDesc
  504. * @param array aCustomerDesc
  505. * @param array aWorkgroupDesc
  506. * @param array aImpactedCIs
  507. * @param string sSeverity
  508. *
  509. * @return WebServiceResult
  510. */
  511. protected function _CreateIncidentTicket($sType, $sDescription, $sInitialSituation, $sImpact, $aCallerDesc, $aCustomerDesc, $aWorkgroupDesc, $aImpactedCIs, $sSeverity)
  512. {
  513. $oRes = new WebServiceResult();
  514. try
  515. {
  516. new CMDBChange();
  517. $oMyChange = MetaModel::NewObject("CMDBChange");
  518. $oMyChange->Set("date", time());
  519. $oMyChange->Set("userinfo", "Administrator");
  520. $iChangeId = $oMyChange->DBInsertNoReload();
  521. $oNewTicket = MetaModel::NewObject('bizIncidentTicket');
  522. $this->MyObjectSetScalar('type', 'type', $sType, $oNewTicket, $oRes);
  523. $this->MyObjectSetScalar('title', 'title', $sDescription, $oNewTicket, $oRes);
  524. $this->MyObjectSetScalar('initial_situation', 'initialsituation', $sInitialSituation, $oNewTicket, $oRes);
  525. $this->MyObjectSetScalar('severity', 'severity', $sSeverity, $oNewTicket, $oRes);
  526. $this->MyObjectSetExternalKey('org_id', 'customer', $aCustomerDesc, $oNewTicket, $oRes);
  527. $this->MyObjectSetExternalKey('caller_id', 'caller', $aCallerDesc, $oNewTicket, $oRes);
  528. $this->MyObjectSetExternalKey('workgroup_id', 'workgroup', $aWorkgroupDesc, $oNewTicket, $oRes);
  529. $aDevicesNotFound = $this->AddLinkedObjects('impacted_infra_manual', 'impacted_cis', 'logInfra', $aImpactedCIs, $oNewTicket, $oRes);
  530. if (count($aDevicesNotFound) > 0)
  531. {
  532. $this->MyObjectSetScalar('impact', 'n/a', $sImpact.' - Related CIs: '.implode(', ', $aDevicesNotFound), $oNewTicket, $oRes);
  533. }
  534. else
  535. {
  536. $this->MyObjectSetScalar('impact', 'n/a', $sImpact, $oNewTicket, $oRes);
  537. }
  538. $this->MyObjectInsert($oNewTicket, 'created', $oMyChange, $oRes);
  539. }
  540. catch (CoreException $e)
  541. {
  542. $oRes->LogError($e->getMessage());
  543. }
  544. catch (Exception $e)
  545. {
  546. $oRes->LogError($e->getMessage());
  547. }
  548. $this->LogUsage(__FUNCTION__, $oRes);
  549. return $oRes;
  550. }
  551. }
  552. ?>