webservices.class.inc.php 14 KB

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