webservices.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. if (!MetaModel::IsLogEnabledWebService()) return;
  201. $oLog = new EventWebService();
  202. if ($oRes->IsOk())
  203. {
  204. $oLog->Set('message', $sVerb.' was successfully invoked');
  205. }
  206. else
  207. {
  208. $oLog->Set('message', $sVerb.' returned errors');
  209. }
  210. $oLog->Set('userinfo', UserRights::GetUser());
  211. $oLog->Set('verb', $sVerb);
  212. $oLog->Set('result', $oRes->IsOk());
  213. $oLog->Set('log_info', $oRes->GetInfoAsText());
  214. $oLog->Set('log_warning', $oRes->GetWarningsAsText());
  215. $oLog->Set('log_error', $oRes->GetErrorsAsText());
  216. $oLog->Set('data', $oRes->GetReturnedDataAsText());
  217. $oLog->DBInsertNoReload();
  218. }
  219. /**
  220. * Helper to set a scalar attribute
  221. *
  222. * @param string sAttCode
  223. * @param scalar value
  224. * @param DBObject oTargetObj
  225. * @param WebServiceResult oRes
  226. *
  227. */
  228. protected function MyObjectSetScalar($sAttCode, $sParamName, $value, &$oTargetObj, &$oRes)
  229. {
  230. if ($oTargetObj->CheckValue($sAttCode, $value))
  231. {
  232. $oTargetObj->Set($sAttCode, $value);
  233. }
  234. else
  235. {
  236. $aAllowedValues = MetaModel::GetAllowedValues_att(get_class($oTargetObj), $sAttCode);
  237. $sValues = implode(', ', $aAllowedValues);
  238. $oRes->LogError("Parameter $sParamName: found '$value' while expecting a value in {".$sValues."}");
  239. }
  240. }
  241. /**
  242. * Helper to set an external key
  243. *
  244. * @param string sAttCode
  245. * @param array aExtKeyDesc
  246. * @param DBObject oTargetObj
  247. * @param WebServiceResult oRes
  248. *
  249. */
  250. protected function MyObjectSetExternalKey($sAttCode, $sParamName, $aExtKeyDesc, &$oTargetObj, &$oRes)
  251. {
  252. $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  253. $bIsMandatory = !$oExtKey->IsNullAllowed();
  254. if (is_null($aExtKeyDesc))
  255. {
  256. if ($bIsMandatory)
  257. {
  258. $oRes->LogError("Parameter $sParamName: found null for a mandatory key");
  259. }
  260. else
  261. {
  262. // skip silently
  263. return;
  264. }
  265. }
  266. if (count($aExtKeyDesc) == 0)
  267. {
  268. $oRes->LogIssue("Parameter $sParamName: no search condition has been specified", $bIsMandatory);
  269. return;
  270. }
  271. $sKeyClass = $oExtKey->GetTargetClass();
  272. $oReconFilter = new CMDBSearchFilter($sKeyClass);
  273. foreach ($aExtKeyDesc as $sForeignAttCode => $value)
  274. {
  275. if (!MetaModel::IsValidFilterCode($sKeyClass, $sForeignAttCode))
  276. {
  277. $aCodes = array_keys(MetaModel::GetClassFilterDefs($sKeyClass));
  278. $sMsg = "Parameter $sParamName: '$sForeignAttCode' is not a valid filter code for class '$sKeyClass', expecting a value in {".implode(', ', $aCodes)."}";
  279. $oRes->LogIssue($sMsg, $bIsMandatory);
  280. }
  281. // The foreign attribute is one of our reconciliation key
  282. $oReconFilter->AddCondition($sForeignAttCode, $value, '=');
  283. }
  284. $oExtObjects = new CMDBObjectSet($oReconFilter);
  285. switch($oExtObjects->Count())
  286. {
  287. case 0:
  288. $sMsg = "Parameter $sParamName: no match (searched: '".$oReconFilter->ToOQL()."')";
  289. $oRes->LogIssue($sMsg, $bIsMandatory);
  290. break;
  291. case 1:
  292. // Do change the external key attribute
  293. $oForeignObj = $oExtObjects->Fetch();
  294. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  295. // Report it (no need to report if the object already had this value
  296. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  297. {
  298. $oRes->LogInfo("Parameter $sParamName: found match ".get_class($oForeignObj)."::".$oForeignObj->GetKey()." '".$oForeignObj->GetName()."'");
  299. }
  300. break;
  301. default:
  302. $sMsg = "Parameter $sParamName: Found ".$oExtObjects->Count()." matches (searched: '".$oReconFilter->ToOQL()."')";
  303. $oRes->LogIssue($sMsg, $bIsMandatory);
  304. }
  305. }
  306. /**
  307. * Helper to link objects
  308. *
  309. * @param string sLinkAttCode
  310. * @param string sLinkedClass
  311. * @param array $aLinkList
  312. * @param DBObject oTargetObj
  313. * @param WebServiceResult oRes
  314. *
  315. * @return array List of objects that could not be found
  316. */
  317. protected function AddLinkedObjects($sLinkAttCode, $sParamName, $sLinkedClass, $aLinkList, &$oTargetObj, &$oRes)
  318. {
  319. $oLinkAtt = MetaModel::GetAttributeDef(get_class($oTargetObj), $sLinkAttCode);
  320. $sLinkClass = $oLinkAtt->GetLinkedClass();
  321. $sExtKeyToItem = $oLinkAtt->GetExtKeyToRemote();
  322. $aItemsFound = array();
  323. $aItemsNotFound = array();
  324. if (is_null($aLinkList))
  325. {
  326. return $aItemsNotFound;
  327. }
  328. foreach ($aLinkList as $aItemData)
  329. {
  330. if (!array_key_exists('class', $aItemData))
  331. {
  332. $oRes->LogWarning("Parameter $sParamName: missing 'class' specification");
  333. continue; // skip
  334. }
  335. $sTargetClass = $aItemData['class'];
  336. if (!MetaModel::IsValidClass($sTargetClass))
  337. {
  338. $oRes->LogError("Parameter $sParamName: invalid class '$sTargetClass'");
  339. continue; // skip
  340. }
  341. if (!MetaModel::IsParentClass($sLinkedClass, $sTargetClass))
  342. {
  343. $oRes->LogError("Parameter $sParamName: '$sTargetClass' is not a child class of '$sLinkedClass'");
  344. continue; // skip
  345. }
  346. $oReconFilter = new CMDBSearchFilter($sTargetClass);
  347. $aCIStringDesc = array();
  348. foreach ($aItemData['search'] as $sAttCode => $value)
  349. {
  350. if (!MetaModel::IsValidFilterCode($sTargetClass, $sAttCode))
  351. {
  352. $aCodes = array_keys(MetaModel::GetClassFilterDefs($sTargetClass));
  353. $oRes->LogError("Parameter $sParamName: '$sAttCode' is not a valid filter code for class '$sTargetClass', expecting a value in {".implode(', ', $aCodes)."}");
  354. continue 2; // skip the entire item
  355. }
  356. $aCIStringDesc[] = "$sAttCode: $value";
  357. // The attribute is one of our reconciliation key
  358. $oReconFilter->AddCondition($sAttCode, $value, '=');
  359. }
  360. if (count($aCIStringDesc) == 1)
  361. {
  362. // take the last and unique value to describe the object
  363. $sItemDesc = $value;
  364. }
  365. else
  366. {
  367. // describe the object by the given keys
  368. $sItemDesc = $sTargetClass.'('.implode('/', $aCIStringDesc).')';
  369. }
  370. $oExtObjects = new CMDBObjectSet($oReconFilter);
  371. switch($oExtObjects->Count())
  372. {
  373. case 0:
  374. $oRes->LogWarning("Parameter $sParamName: object to link $sLinkedClass / $sItemDesc could not be found (searched: '".$oReconFilter->ToOQL()."')");
  375. $aItemsNotFound[] = $sItemDesc;
  376. break;
  377. case 1:
  378. $aItemsFound[] = array (
  379. 'object' => $oExtObjects->Fetch(),
  380. 'link_values' => @$aItemData['link_values'],
  381. 'desc' => $sItemDesc,
  382. );
  383. break;
  384. default:
  385. $oRes->LogWarning("Parameter $sParamName: Found ".$oExtObjects->Count()." matches for item '$sItemDesc' (searched: '".$oReconFilter->ToOQL()."')");
  386. $aItemsNotFound[] = $sItemDesc;
  387. }
  388. }
  389. if (count($aItemsFound) > 0)
  390. {
  391. $aLinks = array();
  392. foreach($aItemsFound as $aItemData)
  393. {
  394. $oLink = MetaModel::NewObject($sLinkClass);
  395. $oLink->Set($sExtKeyToItem, $aItemData['object']->GetKey());
  396. foreach($aItemData['link_values'] as $sKey => $value)
  397. {
  398. if(!MetaModel::IsValidAttCode($sLinkClass, $sKey))
  399. {
  400. $oRes->LogWarning("Parameter $sParamName: Attaching item '".$aItemData['desc']."', the attribute code '$sKey' is not valid ; check the class '$sLinkClass'");
  401. }
  402. else
  403. {
  404. $oLink->Set($sKey, $value);
  405. }
  406. }
  407. $aLinks[] = $oLink;
  408. }
  409. $oImpactedInfraSet = DBObjectSet::FromArray($sLinkClass, $aLinks);
  410. $oTargetObj->Set($sLinkAttCode, $oImpactedInfraSet);
  411. }
  412. return $aItemsNotFound;
  413. }
  414. protected function MyObjectInsert($oTargetObj, $sResultLabel, $oChange, &$oRes)
  415. {
  416. if ($oRes->IsOk())
  417. {
  418. list($bRes, $aIssues) = $oTargetObj->CheckToInsert();
  419. if ($bRes)
  420. {
  421. $iId = $oTargetObj->DBInsertTrackedNoReload($oChange);
  422. $oRes->LogInfo("Created object ".get_class($$oTargetObj)."::$iId");
  423. $oRes->AddResultObject($sResultLabel, $oTargetObj);
  424. }
  425. else
  426. {
  427. $oRes->LogError("The ticket could not be created due to forbidden values (or inconsistent values)");
  428. }
  429. }
  430. }
  431. static protected function SoapStructToExternalKeySearch(SoapExternalKeySearch $oExternalKeySearch)
  432. {
  433. if (is_null($oExternalKeySearch)) return null;
  434. $aRes = array();
  435. foreach($oExternalKeySearch->conditions as $oSearchCondition)
  436. {
  437. $aRes[$oSearchCondition->attcode] = $oSearchCondition->value;
  438. }
  439. return $aRes;
  440. }
  441. static protected function SoapStructToLinkCreationSpec(SoapLinkCreationSpec $oLinkCreationSpec)
  442. {
  443. $aRes = array
  444. (
  445. 'class' => $oLinkCreationSpec->class,
  446. 'search' => array(),
  447. 'link_values' => array(),
  448. );
  449. foreach($oLinkCreationSpec->conditions as $oSearchCondition)
  450. {
  451. $aRes['search'][$oSearchCondition->attcode] = $oSearchCondition->value;
  452. }
  453. foreach($oLinkCreationSpec->attributes as $oAttributeValue)
  454. {
  455. $aRes['link_values'][$oAttributeValue->attcode] = $oAttributeValue->value;
  456. }
  457. return $aRes;
  458. }
  459. /**
  460. * Get the server version (TODO: get it dynamically, where ?)
  461. *
  462. * @return WebServiceResult
  463. */
  464. public function GetVersion()
  465. {
  466. return "0.8";
  467. }
  468. public function CreateIncidentTicket($sLogin, $sPassword, $sType, $sDescription, $sInitialSituation, $sImpact, $oCallerDesc, $oCustomerDesc, $oWorkgroupDesc, $aSOAPImpactedCIs, $sSeverity)
  469. {
  470. if (!UserRights::Login($sLogin, $sPassword))
  471. {
  472. $oRes = new WebServiceResultFailedLogin($sLogin);
  473. $this->LogUsage(__FUNCTION__, $oRes);
  474. return $oRes->ToSoapStructure();
  475. }
  476. $aCallerDesc = self::SoapStructToExternalKeySearch($oCallerDesc);
  477. $aCustomerDesc = self::SoapStructToExternalKeySearch($oCustomerDesc);
  478. $aWorkgroupDesc = self::SoapStructToExternalKeySearch($oWorkgroupDesc);
  479. $aImpactedCIs = array();
  480. foreach($aSOAPImpactedCIs as $oImpactedCIs)
  481. {
  482. $aImpactedCIs[] = self::SoapStructToLinkCreationSpec($oImpactedCIs);
  483. }
  484. $oRes = $this->_CreateIncidentTicket
  485. (
  486. $sType,
  487. $sDescription,
  488. $sInitialSituation,
  489. $sImpact,
  490. $aCallerDesc,
  491. $aCustomerDesc,
  492. $aWorkgroupDesc,
  493. $aImpactedCIs,
  494. $sSeverity
  495. );
  496. return $oRes->ToSoapStructure();
  497. }
  498. /**
  499. * Create an incident ticket from a monitoring system
  500. * Some CIs might be specified (by their name/IP)
  501. *
  502. * @param string sDecription
  503. * @param string sInitialSituation
  504. * @param array aCallerDesc
  505. * @param array aCustomerDesc
  506. * @param array aWorkgroupDesc
  507. * @param array aImpactedCIs
  508. * @param string sSeverity
  509. *
  510. * @return WebServiceResult
  511. */
  512. protected function _CreateIncidentTicket($sType, $sDescription, $sInitialSituation, $sImpact, $aCallerDesc, $aCustomerDesc, $aWorkgroupDesc, $aImpactedCIs, $sSeverity)
  513. {
  514. $oRes = new WebServiceResult();
  515. try
  516. {
  517. new CMDBChange();
  518. $oMyChange = MetaModel::NewObject("CMDBChange");
  519. $oMyChange->Set("date", time());
  520. $oMyChange->Set("userinfo", "Administrator");
  521. $iChangeId = $oMyChange->DBInsertNoReload();
  522. $oNewTicket = MetaModel::NewObject('bizIncidentTicket');
  523. $this->MyObjectSetScalar('type', 'type', $sType, $oNewTicket, $oRes);
  524. $this->MyObjectSetScalar('title', 'title', $sDescription, $oNewTicket, $oRes);
  525. $this->MyObjectSetScalar('initial_situation', 'initialsituation', $sInitialSituation, $oNewTicket, $oRes);
  526. $this->MyObjectSetScalar('severity', 'severity', $sSeverity, $oNewTicket, $oRes);
  527. $this->MyObjectSetExternalKey('org_id', 'customer', $aCustomerDesc, $oNewTicket, $oRes);
  528. $this->MyObjectSetExternalKey('caller_id', 'caller', $aCallerDesc, $oNewTicket, $oRes);
  529. $this->MyObjectSetExternalKey('workgroup_id', 'workgroup', $aWorkgroupDesc, $oNewTicket, $oRes);
  530. $aDevicesNotFound = $this->AddLinkedObjects('impacted_infra_manual', 'impacted_cis', 'logInfra', $aImpactedCIs, $oNewTicket, $oRes);
  531. if (count($aDevicesNotFound) > 0)
  532. {
  533. $this->MyObjectSetScalar('impact', 'n/a', $sImpact.' - Related CIs: '.implode(', ', $aDevicesNotFound), $oNewTicket, $oRes);
  534. }
  535. else
  536. {
  537. $this->MyObjectSetScalar('impact', 'n/a', $sImpact, $oNewTicket, $oRes);
  538. }
  539. $this->MyObjectInsert($oNewTicket, 'created', $oMyChange, $oRes);
  540. }
  541. catch (CoreException $e)
  542. {
  543. $oRes->LogError($e->getMessage());
  544. }
  545. catch (Exception $e)
  546. {
  547. $oRes->LogError($e->getMessage());
  548. }
  549. $this->LogUsage(__FUNCTION__, $oRes);
  550. return $oRes;
  551. }
  552. }
  553. ?>