webservices.class.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. * Implementation of iTop SOAP services
  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. 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. $this->m_aResult[$sLabel] = array(
  123. 'id' => $oObject->GetKey(),
  124. 'name' => $oObject->GetRawName(),
  125. 'url' => $oObject->GetHyperlink(),
  126. );
  127. }
  128. /**
  129. * Add result details - a table row
  130. *
  131. * @param string sLabel
  132. * @param object oObject
  133. */
  134. public function AddResultRow($sLabel, $aRow)
  135. {
  136. $this->m_aResult[$sLabel] = $aRow;
  137. }
  138. /**
  139. * Log an error
  140. *
  141. * @param string sDescription
  142. */
  143. public function LogError($sDescription)
  144. {
  145. $this->m_aErrors[] = $sDescription;
  146. // Note: SOAP do transform false into null
  147. $this->m_bStatus = 0;
  148. }
  149. /**
  150. * Log a warning
  151. *
  152. * @param string sDescription
  153. */
  154. public function LogWarning($sDescription)
  155. {
  156. $this->m_aWarnings[] = $sDescription;
  157. }
  158. /**
  159. * Log an error or a warning
  160. *
  161. * @param string sDescription
  162. * @param boolean bIsStopper
  163. */
  164. public function LogIssue($sDescription, $bIsStopper = true)
  165. {
  166. if ($bIsStopper) $this->LogError($sDescription);
  167. else $this->LogWarning($sDescription);
  168. }
  169. /**
  170. * Log operation details
  171. *
  172. * @param description $sDescription
  173. */
  174. public function LogInfo($sDescription)
  175. {
  176. $this->m_aInfos[] = $sDescription;
  177. }
  178. protected static function LogToText($aLog)
  179. {
  180. return implode("\n", $aLog);
  181. }
  182. public function GetInfoAsText()
  183. {
  184. return self::LogToText($this->m_aInfos);
  185. }
  186. public function GetWarningsAsText()
  187. {
  188. return self::LogToText($this->m_aWarnings);
  189. }
  190. public function GetErrorsAsText()
  191. {
  192. return self::LogToText($this->m_aErrors);
  193. }
  194. public function GetReturnedDataAsText()
  195. {
  196. $sRet = '';
  197. foreach ($this->m_aResult as $sKey => $value)
  198. {
  199. $sRet .= "===== $sKey =====\n";
  200. $sRet .= print_r($value, true);
  201. }
  202. return $sRet;
  203. }
  204. }
  205. /**
  206. * Generic response of iTop SOAP services - failed login
  207. *
  208. * @package iTopORM
  209. */
  210. class WebServiceResultFailedLogin extends WebServiceResult
  211. {
  212. public function __construct($sLogin)
  213. {
  214. parent::__construct();
  215. $this->LogError("Wrong credentials: '$sLogin'");
  216. }
  217. }
  218. /**
  219. * Implementation of the Services
  220. *
  221. * @package iTopORM
  222. */
  223. abstract class WebServicesBase
  224. {
  225. static public function GetWSDLContents($sServiceCategory = '')
  226. {
  227. if ($sServiceCategory == '')
  228. {
  229. $sServiceCategory = 'BasicServices';
  230. }
  231. $sWsdlFilePath = call_user_func(array($sServiceCategory, 'GetWSDLFilePath'));
  232. return file_get_contents($sWsdlFilePath);
  233. }
  234. /**
  235. * Helper to log a service delivery
  236. *
  237. * @param string sVerb
  238. * @param array aArgs
  239. * @param WebServiceResult oRes
  240. *
  241. */
  242. protected function LogUsage($sVerb, $oRes)
  243. {
  244. if (!MetaModel::IsLogEnabledWebService()) return;
  245. $oLog = new EventWebService();
  246. if ($oRes->IsOk())
  247. {
  248. $oLog->Set('message', $sVerb.' was successfully invoked');
  249. }
  250. else
  251. {
  252. $oLog->Set('message', $sVerb.' returned errors');
  253. }
  254. $oLog->Set('userinfo', UserRights::GetUser());
  255. $oLog->Set('verb', $sVerb);
  256. $oLog->Set('result', $oRes->IsOk());
  257. $oLog->Set('log_info', $oRes->GetInfoAsText());
  258. $oLog->Set('log_warning', $oRes->GetWarningsAsText());
  259. $oLog->Set('log_error', $oRes->GetErrorsAsText());
  260. $oLog->Set('data', $oRes->GetReturnedDataAsText());
  261. $oLog->DBInsertNoReload();
  262. }
  263. /**
  264. * Helper to set a scalar attribute
  265. *
  266. * @param string sAttCode
  267. * @param scalar value
  268. * @param DBObject oTargetObj
  269. * @param WebServiceResult oRes
  270. *
  271. */
  272. protected function MyObjectSetScalar($sAttCode, $sParamName, $value, &$oTargetObj, &$oRes)
  273. {
  274. $res = $oTargetObj->CheckValue($sAttCode, $value);
  275. if ($res === true)
  276. {
  277. $oTargetObj->Set($sAttCode, $value);
  278. }
  279. else
  280. {
  281. // $res contains the error description
  282. $oRes->LogError("Unexpected value for parameter $sParamName: $res");
  283. }
  284. }
  285. /**
  286. * Helper to set an external key
  287. *
  288. * @param string sAttCode
  289. * @param array aExtKeyDesc
  290. * @param DBObject oTargetObj
  291. * @param WebServiceResult oRes
  292. *
  293. */
  294. protected function MyObjectSetExternalKey($sAttCode, $sParamName, $aExtKeyDesc, &$oTargetObj, &$oRes)
  295. {
  296. $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
  297. $bIsMandatory = !$oExtKey->IsNullAllowed();
  298. if (is_null($aExtKeyDesc))
  299. {
  300. if ($bIsMandatory)
  301. {
  302. $oRes->LogError("Parameter $sParamName: found null for a mandatory key");
  303. }
  304. else
  305. {
  306. // skip silently
  307. return;
  308. }
  309. }
  310. if (count($aExtKeyDesc) == 0)
  311. {
  312. $oRes->LogIssue("Parameter $sParamName: no search condition has been specified", $bIsMandatory);
  313. return;
  314. }
  315. $sKeyClass = $oExtKey->GetTargetClass();
  316. $oReconFilter = new CMDBSearchFilter($sKeyClass);
  317. foreach ($aExtKeyDesc as $sForeignAttCode => $value)
  318. {
  319. if (!MetaModel::IsValidFilterCode($sKeyClass, $sForeignAttCode))
  320. {
  321. $aCodes = array_keys(MetaModel::GetClassFilterDefs($sKeyClass));
  322. $sMsg = "Parameter $sParamName: '$sForeignAttCode' is not a valid filter code for class '$sKeyClass', expecting a value in {".implode(', ', $aCodes)."}";
  323. $oRes->LogIssue($sMsg, $bIsMandatory);
  324. }
  325. // The foreign attribute is one of our reconciliation key
  326. $oReconFilter->AddCondition($sForeignAttCode, $value, '=');
  327. }
  328. $oExtObjects = new CMDBObjectSet($oReconFilter);
  329. switch($oExtObjects->Count())
  330. {
  331. case 0:
  332. $sMsg = "Parameter $sParamName: no match (searched: '".$oReconFilter->ToOQL(true)."')";
  333. $oRes->LogIssue($sMsg, $bIsMandatory);
  334. break;
  335. case 1:
  336. // Do change the external key attribute
  337. $oForeignObj = $oExtObjects->Fetch();
  338. $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
  339. // Report it (no need to report if the object already had this value
  340. if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
  341. {
  342. $oRes->LogInfo("Parameter $sParamName: found match ".get_class($oForeignObj)."::".$oForeignObj->GetKey()." '".$oForeignObj->GetName()."'");
  343. }
  344. break;
  345. default:
  346. $sMsg = "Parameter $sParamName: Found ".$oExtObjects->Count()." matches (searched: '".$oReconFilter->ToOQL(true)."')";
  347. $oRes->LogIssue($sMsg, $bIsMandatory);
  348. }
  349. }
  350. /**
  351. * Helper to link objects
  352. *
  353. * @param string sLinkAttCode
  354. * @param string sLinkedClass
  355. * @param array $aLinkList
  356. * @param DBObject oTargetObj
  357. * @param WebServiceResult oRes
  358. *
  359. * @return array List of objects that could not be found
  360. */
  361. protected function AddLinkedObjects($sLinkAttCode, $sParamName, $sLinkedClass, $aLinkList, &$oTargetObj, &$oRes)
  362. {
  363. $oLinkAtt = MetaModel::GetAttributeDef(get_class($oTargetObj), $sLinkAttCode);
  364. $sLinkClass = $oLinkAtt->GetLinkedClass();
  365. $sExtKeyToItem = $oLinkAtt->GetExtKeyToRemote();
  366. $aItemsFound = array();
  367. $aItemsNotFound = array();
  368. if (is_null($aLinkList))
  369. {
  370. return $aItemsNotFound;
  371. }
  372. foreach ($aLinkList as $aItemData)
  373. {
  374. if (!array_key_exists('class', $aItemData))
  375. {
  376. $oRes->LogWarning("Parameter $sParamName: missing 'class' specification");
  377. continue; // skip
  378. }
  379. $sTargetClass = $aItemData['class'];
  380. if (!MetaModel::IsValidClass($sTargetClass))
  381. {
  382. $oRes->LogError("Parameter $sParamName: invalid class '$sTargetClass'");
  383. continue; // skip
  384. }
  385. if (!MetaModel::IsParentClass($sLinkedClass, $sTargetClass))
  386. {
  387. $oRes->LogError("Parameter $sParamName: '$sTargetClass' is not a child class of '$sLinkedClass'");
  388. continue; // skip
  389. }
  390. $oReconFilter = new CMDBSearchFilter($sTargetClass);
  391. $aCIStringDesc = array();
  392. foreach ($aItemData['search'] as $sAttCode => $value)
  393. {
  394. if (!MetaModel::IsValidFilterCode($sTargetClass, $sAttCode))
  395. {
  396. $aCodes = array_keys(MetaModel::GetClassFilterDefs($sTargetClass));
  397. $oRes->LogError("Parameter $sParamName: '$sAttCode' is not a valid filter code for class '$sTargetClass', expecting a value in {".implode(', ', $aCodes)."}");
  398. continue 2; // skip the entire item
  399. }
  400. $aCIStringDesc[] = "$sAttCode: $value";
  401. // The attribute is one of our reconciliation key
  402. $oReconFilter->AddCondition($sAttCode, $value, '=');
  403. }
  404. if (count($aCIStringDesc) == 1)
  405. {
  406. // take the last and unique value to describe the object
  407. $sItemDesc = $value;
  408. }
  409. else
  410. {
  411. // describe the object by the given keys
  412. $sItemDesc = $sTargetClass.'('.implode('/', $aCIStringDesc).')';
  413. }
  414. $oExtObjects = new CMDBObjectSet($oReconFilter);
  415. switch($oExtObjects->Count())
  416. {
  417. case 0:
  418. $oRes->LogWarning("Parameter $sParamName: object to link $sLinkedClass / $sItemDesc could not be found (searched: '".$oReconFilter->ToOQL(true)."')");
  419. $aItemsNotFound[] = $sItemDesc;
  420. break;
  421. case 1:
  422. $aItemsFound[] = array (
  423. 'object' => $oExtObjects->Fetch(),
  424. 'link_values' => @$aItemData['link_values'],
  425. 'desc' => $sItemDesc,
  426. );
  427. break;
  428. default:
  429. $oRes->LogWarning("Parameter $sParamName: Found ".$oExtObjects->Count()." matches for item '$sItemDesc' (searched: '".$oReconFilter->ToOQL(true)."')");
  430. $aItemsNotFound[] = $sItemDesc;
  431. }
  432. }
  433. if (count($aItemsFound) > 0)
  434. {
  435. $aLinks = array();
  436. foreach($aItemsFound as $aItemData)
  437. {
  438. $oLink = MetaModel::NewObject($sLinkClass);
  439. $oLink->Set($sExtKeyToItem, $aItemData['object']->GetKey());
  440. foreach($aItemData['link_values'] as $sKey => $value)
  441. {
  442. if(!MetaModel::IsValidAttCode($sLinkClass, $sKey))
  443. {
  444. $oRes->LogWarning("Parameter $sParamName: Attaching item '".$aItemData['desc']."', the attribute code '$sKey' is not valid ; check the class '$sLinkClass'");
  445. }
  446. else
  447. {
  448. $oLink->Set($sKey, $value);
  449. }
  450. }
  451. $aLinks[] = $oLink;
  452. }
  453. $oImpactedInfraSet = DBObjectSet::FromArray($sLinkClass, $aLinks);
  454. $oTargetObj->Set($sLinkAttCode, $oImpactedInfraSet);
  455. }
  456. return $aItemsNotFound;
  457. }
  458. protected function MyObjectInsert($oTargetObj, $sResultLabel, $oChange, &$oRes)
  459. {
  460. if ($oRes->IsOk())
  461. {
  462. list($bRes, $aIssues) = $oTargetObj->CheckToWrite();
  463. if ($bRes)
  464. {
  465. $iId = $oTargetObj->DBInsertTrackedNoReload($oChange);
  466. $oRes->LogInfo("Created object ".get_class($oTargetObj)."::$iId");
  467. $oRes->AddResultObject($sResultLabel, $oTargetObj);
  468. }
  469. else
  470. {
  471. $oRes->LogError("The ticket could not be created due to forbidden values (or inconsistent values)");
  472. foreach($aIssues as $iIssue => $sIssue)
  473. {
  474. $oRes->LogError("Issue #$iIssue: $sIssue");
  475. }
  476. }
  477. }
  478. }
  479. static protected function SoapStructToExternalKeySearch($oExternalKeySearch)
  480. {
  481. if (is_null($oExternalKeySearch)) return null;
  482. if ($oExternalKeySearch->IsVoid()) return null;
  483. $aRes = array();
  484. foreach($oExternalKeySearch->conditions as $oSearchCondition)
  485. {
  486. $aRes[$oSearchCondition->attcode] = $oSearchCondition->value;
  487. }
  488. return $aRes;
  489. }
  490. static protected function SoapStructToLinkCreationSpec(SoapLinkCreationSpec $oLinkCreationSpec)
  491. {
  492. $aRes = array
  493. (
  494. 'class' => $oLinkCreationSpec->class,
  495. 'search' => array(),
  496. 'link_values' => array(),
  497. );
  498. foreach($oLinkCreationSpec->conditions as $oSearchCondition)
  499. {
  500. $aRes['search'][$oSearchCondition->attcode] = $oSearchCondition->value;
  501. }
  502. foreach($oLinkCreationSpec->attributes as $oAttributeValue)
  503. {
  504. $aRes['link_values'][$oAttributeValue->attcode] = $oAttributeValue->value;
  505. }
  506. return $aRes;
  507. }
  508. static protected function SoapStructToAssociativeArray($aArrayOfAssocArray)
  509. {
  510. if (is_null($aArrayOfAssocArray)) return array();
  511. $aRes = array();
  512. foreach($aArrayOfAssocArray as $aAssocArray)
  513. {
  514. $aRow = array();
  515. foreach ($aAssocArray as $oKeyValuePair)
  516. {
  517. $aRow[$oKeyValuePair->key] = $oKeyValuePair->value;
  518. }
  519. $aRes[] = $aRow;
  520. }
  521. return $aRes;
  522. }
  523. }
  524. ?>