webservices.class.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <?php
  2. // Copyright (C) 2010-2015 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Implementation of iTop SOAP services
  20. *
  21. * @copyright Copyright (C) 2010-2015 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once(APPROOT.'/webservices/itopsoaptypes.class.inc.php');
  25. /**
  26. * Generic response of iTop SOAP services
  27. *
  28. * @package iTopORM
  29. */
  30. class WebServiceResult
  31. {
  32. /**
  33. * Overall status
  34. *
  35. * @var m_bStatus
  36. */
  37. public $m_bStatus;
  38. /**
  39. * Error log
  40. *
  41. * @var m_aErrors
  42. */
  43. public $m_aErrors;
  44. /**
  45. * Warning log
  46. *
  47. * @var m_aWarnings
  48. */
  49. public $m_aWarnings;
  50. /**
  51. * Information log
  52. *
  53. * @var m_aInfos
  54. */
  55. public $m_aInfos;
  56. /**
  57. * Constructor
  58. *
  59. * @param status $bStatus
  60. */
  61. public function __construct()
  62. {
  63. $this->m_bStatus = true;
  64. $this->m_aResult = array();
  65. $this->m_aErrors = array();
  66. $this->m_aWarnings = array();
  67. $this->m_aInfos = array();
  68. }
  69. public function ToSoapStructure()
  70. {
  71. $aResults = array();
  72. foreach($this->m_aResult as $sLabel => $aData)
  73. {
  74. $aValues = array();
  75. foreach($aData as $sKey => $value)
  76. {
  77. $aValues[] = new SOAPKeyValue($sKey, $value);
  78. }
  79. $aResults[] = new SoapResultMessage($sLabel, $aValues);
  80. }
  81. $aInfos = array();
  82. foreach($this->m_aInfos as $sMessage)
  83. {
  84. $aInfos[] = new SoapLogMessage($sMessage);
  85. }
  86. $aWarnings = array();
  87. foreach($this->m_aWarnings as $sMessage)
  88. {
  89. $aWarnings[] = new SoapLogMessage($sMessage);
  90. }
  91. $aErrors = array();
  92. foreach($this->m_aErrors as $sMessage)
  93. {
  94. $aErrors[] = new SoapLogMessage($sMessage);
  95. }
  96. $oRet = new SOAPResult(
  97. $this->m_bStatus,
  98. $aResults,
  99. new SOAPResultLog($aErrors),
  100. new SOAPResultLog($aWarnings),
  101. new SOAPResultLog($aInfos)
  102. );
  103. return $oRet;
  104. }
  105. /**
  106. * Did the current processing encounter a stopper issue ?
  107. *
  108. * @return bool
  109. */
  110. public function IsOk()
  111. {
  112. return $this->m_bStatus;
  113. }
  114. /**
  115. * Add result details - object reference
  116. *
  117. * @param string sLabel
  118. * @param object oObject
  119. */
  120. public function AddResultObject($sLabel, $oObject)
  121. {
  122. $oAppContext = new ApplicationContext();
  123. $this->m_aResult[$sLabel] = array(
  124. 'id' => $oObject->GetKey(),
  125. 'name' => $oObject->GetRawName(),
  126. 'url' => $oAppContext->MakeObjectUrl(get_class($oObject), $oObject->GetKey(), null, false), // Raw URL without HTML tags
  127. );
  128. }
  129. /**
  130. * Add result details - a table row
  131. *
  132. * @param string sLabel
  133. * @param object oObject
  134. */
  135. public function AddResultRow($sLabel, $aRow)
  136. {
  137. $this->m_aResult[$sLabel] = $aRow;
  138. }
  139. /**
  140. * Log an error
  141. *
  142. * @param string sDescription
  143. */
  144. public function LogError($sDescription)
  145. {
  146. $this->m_aErrors[] = $sDescription;
  147. // Note: SOAP do transform false into null
  148. $this->m_bStatus = 0;
  149. }
  150. /**
  151. * Log a warning
  152. *
  153. * @param string sDescription
  154. */
  155. public function LogWarning($sDescription)
  156. {
  157. $this->m_aWarnings[] = $sDescription;
  158. }
  159. /**
  160. * Log an error or a warning
  161. *
  162. * @param string sDescription
  163. * @param boolean bIsStopper
  164. */
  165. public function LogIssue($sDescription, $bIsStopper = true)
  166. {
  167. if ($bIsStopper) $this->LogError($sDescription);
  168. else $this->LogWarning($sDescription);
  169. }
  170. /**
  171. * Log operation details
  172. *
  173. * @param description $sDescription
  174. */
  175. public function LogInfo($sDescription)
  176. {
  177. $this->m_aInfos[] = $sDescription;
  178. }
  179. protected static function LogToText($aLog)
  180. {
  181. return implode("\n", $aLog);
  182. }
  183. public function GetInfoAsText()
  184. {
  185. return self::LogToText($this->m_aInfos);
  186. }
  187. public function GetWarningsAsText()
  188. {
  189. return self::LogToText($this->m_aWarnings);
  190. }
  191. public function GetErrorsAsText()
  192. {
  193. return self::LogToText($this->m_aErrors);
  194. }
  195. public function GetReturnedDataAsText()
  196. {
  197. $sRet = '';
  198. foreach ($this->m_aResult as $sKey => $value)
  199. {
  200. $sRet .= "===== $sKey =====\n";
  201. $sRet .= print_r($value, true);
  202. }
  203. return $sRet;
  204. }
  205. }
  206. /**
  207. * Generic response of iTop SOAP services - failed login
  208. *
  209. * @package iTopORM
  210. */
  211. class WebServiceResultFailedLogin extends WebServiceResult
  212. {
  213. public function __construct($sLogin)
  214. {
  215. parent::__construct();
  216. $this->LogError("Wrong credentials: '$sLogin'");
  217. }
  218. }
  219. /**
  220. * Implementation of the Services
  221. *
  222. * @package iTopORM
  223. */
  224. abstract class WebServicesBase
  225. {
  226. static public function GetWSDLContents($sServiceCategory = '')
  227. {
  228. if ($sServiceCategory == '')
  229. {
  230. $sServiceCategory = 'BasicServices';
  231. }
  232. $sWsdlFilePath = call_user_func(array($sServiceCategory, 'GetWSDLFilePath'));
  233. return file_get_contents($sWsdlFilePath);
  234. }
  235. /**
  236. * Helper to log a service delivery
  237. *
  238. * @param string sVerb
  239. * @param array aArgs
  240. * @param WebServiceResult oRes
  241. *
  242. */
  243. protected function LogUsage($sVerb, $oRes)
  244. {
  245. if (!MetaModel::IsLogEnabledWebService()) return;
  246. $oLog = new EventWebService();
  247. if ($oRes->IsOk())
  248. {
  249. $oLog->Set('message', $sVerb.' was successfully invoked');
  250. }
  251. else
  252. {
  253. $oLog->Set('message', $sVerb.' returned errors');
  254. }
  255. $oLog->Set('userinfo', UserRights::GetUser());
  256. $oLog->Set('verb', $sVerb);
  257. $oLog->Set('result', $oRes->IsOk());
  258. $this->TrimAndSetValue($oLog, 'log_info', (string)$oRes->GetInfoAsText());
  259. $this->TrimAndSetValue($oLog, 'log_warning', (string)$oRes->GetWarningsAsText());
  260. $this->TrimAndSetValue($oLog, 'log_error', (string)$oRes->GetErrorsAsText());
  261. $this->TrimAndSetValue($oLog, 'data', (string)$oRes->GetReturnedDataAsText());
  262. $oLog->DBInsertNoReload();
  263. }
  264. protected function TrimAndSetValue($oLog, $sAttCode, $sValue)
  265. {
  266. $oAttDef = MetaModel::GetAttributeDef(get_class($oLog), $sAttCode);
  267. if (is_object($oAttDef))
  268. {
  269. $iMaxSize = $oAttDef->GetMaxSize();
  270. if ($iMaxSize && (strlen($sValue) > $iMaxSize))
  271. {
  272. $sValue = substr($sValue, 0, $iMaxSize);
  273. }
  274. $oLog->Set($sAttCode, $sValue);
  275. }
  276. }
  277. /**
  278. * Helper to set a scalar attribute
  279. *
  280. * @param string sAttCode
  281. * @param scalar value
  282. * @param DBObject oTargetObj
  283. * @param WebServiceResult oRes
  284. *
  285. */
  286. protected function MyObjectSetScalar($sAttCode, $sParamName, $value, &$oTargetObj, &$oRes)
  287. {
  288. $res = $oTargetObj->CheckValue($sAttCode, $value);
  289. if ($res === true)
  290. {
  291. $oTargetObj->Set($sAttCode, $value);
  292. }
  293. else
  294. {
  295. // $res contains the error description
  296. $oRes->LogError("Unexpected value for parameter $sParamName: $res");
  297. }
  298. }
  299. /**
  300. * Helper to set an external key
  301. *
  302. * @param string sAttCode
  303. * @param array aExtKeyDesc
  304. * @param DBObject oTargetObj
  305. * @param WebServiceResult oRes
  306. *
  307. */
  308. protected function MyObjectSetExternalKey($sAttCode, $sParamName, $aExtKeyDesc, &$oTargetObj, &$oRes)
  309. {
  310. $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  311. $bIsMandatory = !$oExtKey->IsNullAllowed();
  312. if (is_null($aExtKeyDesc))
  313. {
  314. if ($bIsMandatory)
  315. {
  316. $oRes->LogError("Parameter $sParamName: found null for a mandatory key");
  317. }
  318. else
  319. {
  320. // skip silently
  321. return;
  322. }
  323. }
  324. if (count($aExtKeyDesc) == 0)
  325. {
  326. $oRes->LogIssue("Parameter $sParamName: no search condition has been specified", $bIsMandatory);
  327. return;
  328. }
  329. $sKeyClass = $oExtKey->GetTargetClass();
  330. $oReconFilter = new DBObjectSearch($sKeyClass);
  331. foreach ($aExtKeyDesc as $sForeignAttCode => $value)
  332. {
  333. if (!MetaModel::IsValidFilterCode($sKeyClass, $sForeignAttCode))
  334. {
  335. $aCodes = array_keys(MetaModel::GetClassFilterDefs($sKeyClass));
  336. $sMsg = "Parameter $sParamName: '$sForeignAttCode' is not a valid filter code for class '$sKeyClass', expecting a value in {".implode(', ', $aCodes)."}";
  337. $oRes->LogIssue($sMsg, $bIsMandatory);
  338. }
  339. // The foreign attribute is one of our reconciliation key
  340. $oReconFilter->AddCondition($sForeignAttCode, $value, '=');
  341. }
  342. $oExtObjects = new CMDBObjectSet($oReconFilter);
  343. switch($oExtObjects->Count())
  344. {
  345. case 0:
  346. $sMsg = "Parameter $sParamName: no match (searched: '".$oReconFilter->ToOQL(true)."')";
  347. $oRes->LogIssue($sMsg, $bIsMandatory);
  348. break;
  349. case 1:
  350. // Do change the external key attribute
  351. $oForeignObj = $oExtObjects->Fetch();
  352. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  353. // Report it (no need to report if the object already had this value
  354. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  355. {
  356. $oRes->LogInfo("Parameter $sParamName: found match ".get_class($oForeignObj)."::".$oForeignObj->GetKey()." '".$oForeignObj->GetName()."'");
  357. }
  358. break;
  359. default:
  360. $sMsg = "Parameter $sParamName: Found ".$oExtObjects->Count()." matches (searched: '".$oReconFilter->ToOQL(true)."')";
  361. $oRes->LogIssue($sMsg, $bIsMandatory);
  362. }
  363. }
  364. /**
  365. * Helper to link objects
  366. *
  367. * @param string sLinkAttCode
  368. * @param string sLinkedClass
  369. * @param array $aLinkList
  370. * @param DBObject oTargetObj
  371. * @param WebServiceResult oRes
  372. *
  373. * @return array List of objects that could not be found
  374. */
  375. protected function AddLinkedObjects($sLinkAttCode, $sParamName, $sLinkedClass, $aLinkList, &$oTargetObj, &$oRes)
  376. {
  377. $oLinkAtt = MetaModel::GetAttributeDef(get_class($oTargetObj), $sLinkAttCode);
  378. $sLinkClass = $oLinkAtt->GetLinkedClass();
  379. $sExtKeyToItem = $oLinkAtt->GetExtKeyToRemote();
  380. $aItemsFound = array();
  381. $aItemsNotFound = array();
  382. if (is_null($aLinkList))
  383. {
  384. return $aItemsNotFound;
  385. }
  386. foreach ($aLinkList as $aItemData)
  387. {
  388. if (!array_key_exists('class', $aItemData))
  389. {
  390. $oRes->LogWarning("Parameter $sParamName: missing 'class' specification");
  391. continue; // skip
  392. }
  393. $sTargetClass = $aItemData['class'];
  394. if (!MetaModel::IsValidClass($sTargetClass))
  395. {
  396. $oRes->LogError("Parameter $sParamName: invalid class '$sTargetClass'");
  397. continue; // skip
  398. }
  399. if (!MetaModel::IsParentClass($sLinkedClass, $sTargetClass))
  400. {
  401. $oRes->LogError("Parameter $sParamName: '$sTargetClass' is not a child class of '$sLinkedClass'");
  402. continue; // skip
  403. }
  404. $oReconFilter = new DBObjectSearch($sTargetClass);
  405. $aCIStringDesc = array();
  406. foreach ($aItemData['search'] as $sAttCode => $value)
  407. {
  408. if (!MetaModel::IsValidFilterCode($sTargetClass, $sAttCode))
  409. {
  410. $aCodes = array_keys(MetaModel::GetClassFilterDefs($sTargetClass));
  411. $oRes->LogError("Parameter $sParamName: '$sAttCode' is not a valid filter code for class '$sTargetClass', expecting a value in {".implode(', ', $aCodes)."}");
  412. continue 2; // skip the entire item
  413. }
  414. $aCIStringDesc[] = "$sAttCode: $value";
  415. // The attribute is one of our reconciliation key
  416. $oReconFilter->AddCondition($sAttCode, $value, '=');
  417. }
  418. if (count($aCIStringDesc) == 1)
  419. {
  420. // take the last and unique value to describe the object
  421. $sItemDesc = $value;
  422. }
  423. else
  424. {
  425. // describe the object by the given keys
  426. $sItemDesc = $sTargetClass.'('.implode('/', $aCIStringDesc).')';
  427. }
  428. $oExtObjects = new CMDBObjectSet($oReconFilter);
  429. switch($oExtObjects->Count())
  430. {
  431. case 0:
  432. $oRes->LogWarning("Parameter $sParamName: object to link $sLinkedClass / $sItemDesc could not be found (searched: '".$oReconFilter->ToOQL(true)."')");
  433. $aItemsNotFound[] = $sItemDesc;
  434. break;
  435. case 1:
  436. $aItemsFound[] = array (
  437. 'object' => $oExtObjects->Fetch(),
  438. 'link_values' => @$aItemData['link_values'],
  439. 'desc' => $sItemDesc,
  440. );
  441. break;
  442. default:
  443. $oRes->LogWarning("Parameter $sParamName: Found ".$oExtObjects->Count()." matches for item '$sItemDesc' (searched: '".$oReconFilter->ToOQL(true)."')");
  444. $aItemsNotFound[] = $sItemDesc;
  445. }
  446. }
  447. if (count($aItemsFound) > 0)
  448. {
  449. $aLinks = array();
  450. foreach($aItemsFound as $aItemData)
  451. {
  452. $oLink = MetaModel::NewObject($sLinkClass);
  453. $oLink->Set($sExtKeyToItem, $aItemData['object']->GetKey());
  454. foreach($aItemData['link_values'] as $sKey => $value)
  455. {
  456. if(!MetaModel::IsValidAttCode($sLinkClass, $sKey))
  457. {
  458. $oRes->LogWarning("Parameter $sParamName: Attaching item '".$aItemData['desc']."', the attribute code '$sKey' is not valid ; check the class '$sLinkClass'");
  459. }
  460. else
  461. {
  462. $oLink->Set($sKey, $value);
  463. }
  464. }
  465. $aLinks[] = $oLink;
  466. }
  467. $oImpactedInfraSet = DBObjectSet::FromArray($sLinkClass, $aLinks);
  468. $oTargetObj->Set($sLinkAttCode, $oImpactedInfraSet);
  469. }
  470. return $aItemsNotFound;
  471. }
  472. protected function MyObjectInsert($oTargetObj, $sResultLabel, $oChange, &$oRes)
  473. {
  474. if ($oRes->IsOk())
  475. {
  476. list($bRes, $aIssues) = $oTargetObj->CheckToWrite();
  477. if ($bRes)
  478. {
  479. $iId = $oTargetObj->DBInsertTrackedNoReload($oChange);
  480. $oRes->LogInfo("Created object ".get_class($oTargetObj)."::$iId");
  481. $oRes->AddResultObject($sResultLabel, $oTargetObj);
  482. }
  483. else
  484. {
  485. $oRes->LogError("The ticket could not be created due to forbidden values (or inconsistent values)");
  486. foreach($aIssues as $iIssue => $sIssue)
  487. {
  488. $oRes->LogError("Issue #$iIssue: $sIssue");
  489. }
  490. }
  491. }
  492. }
  493. static protected function SoapStructToExternalKeySearch($oExternalKeySearch)
  494. {
  495. if (is_null($oExternalKeySearch)) return null;
  496. if ($oExternalKeySearch->IsVoid()) return null;
  497. $aRes = array();
  498. foreach($oExternalKeySearch->conditions as $oSearchCondition)
  499. {
  500. $aRes[$oSearchCondition->attcode] = $oSearchCondition->value;
  501. }
  502. return $aRes;
  503. }
  504. static protected function SoapStructToLinkCreationSpec(SoapLinkCreationSpec $oLinkCreationSpec)
  505. {
  506. $aRes = array
  507. (
  508. 'class' => $oLinkCreationSpec->class,
  509. 'search' => array(),
  510. 'link_values' => array(),
  511. );
  512. foreach($oLinkCreationSpec->conditions as $oSearchCondition)
  513. {
  514. $aRes['search'][$oSearchCondition->attcode] = $oSearchCondition->value;
  515. }
  516. foreach($oLinkCreationSpec->attributes as $oAttributeValue)
  517. {
  518. $aRes['link_values'][$oAttributeValue->attcode] = $oAttributeValue->value;
  519. }
  520. return $aRes;
  521. }
  522. static protected function SoapStructToAssociativeArray($aArrayOfAssocArray)
  523. {
  524. if (is_null($aArrayOfAssocArray)) return array();
  525. $aRes = array();
  526. foreach($aArrayOfAssocArray as $aAssocArray)
  527. {
  528. $aRow = array();
  529. foreach ($aAssocArray as $oKeyValuePair)
  530. {
  531. $aRow[$oKeyValuePair->key] = $oKeyValuePair->value;
  532. }
  533. $aRes[] = $aRow;
  534. }
  535. return $aRes;
  536. }
  537. }
  538. ?>