webservices.basic.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/webservices.class.inc.php');
  25. class BasicServices extends WebServicesBase
  26. {
  27. static protected function GetWSDLFilePath()
  28. {
  29. return APPROOT.'/webservices/itop.wsdl.tpl';
  30. }
  31. /**
  32. * Get the server version (TODO: get it dynamically, where ?)
  33. *
  34. * @return WebServiceResult
  35. */
  36. static public function GetVersion()
  37. {
  38. if (ITOP_REVISION == '$WCREV$')
  39. {
  40. $sVersionString = ITOP_VERSION.' [dev]';
  41. }
  42. else
  43. {
  44. // This is a build made from SVN, let display the full information
  45. $sVersionString = ITOP_VERSION."-".ITOP_REVISION." ".ITOP_BUILD_DATE;
  46. }
  47. return $sVersionString;
  48. }
  49. public function CreateRequestTicket($sLogin, $sPassword, $sTitle, $sDescription, $oCallerDesc, $oCustomerDesc, $oServiceDesc, $oServiceSubcategoryDesc, $sProduct, $oWorkgroupDesc, $aSOAPImpactedCIs, $sImpact, $sUrgency)
  50. {
  51. if (!UserRights::CheckCredentials($sLogin, $sPassword))
  52. {
  53. $oRes = new WebServiceResultFailedLogin($sLogin);
  54. $this->LogUsage(__FUNCTION__, $oRes);
  55. return $oRes->ToSoapStructure();
  56. }
  57. UserRights::Login($sLogin);
  58. $aCallerDesc = self::SoapStructToExternalKeySearch($oCallerDesc);
  59. $aCustomerDesc = self::SoapStructToExternalKeySearch($oCustomerDesc);
  60. $aServiceDesc = self::SoapStructToExternalKeySearch($oServiceDesc);
  61. $aServiceSubcategoryDesc = self::SoapStructToExternalKeySearch($oServiceSubcategoryDesc);
  62. $aWorkgroupDesc = self::SoapStructToExternalKeySearch($oWorkgroupDesc);
  63. $aImpactedCIs = array();
  64. if (is_null($aSOAPImpactedCIs)) $aSOAPImpactedCIs = array();
  65. foreach($aSOAPImpactedCIs as $oImpactedCIs)
  66. {
  67. $aImpactedCIs[] = self::SoapStructToLinkCreationSpec($oImpactedCIs);
  68. }
  69. $oRes = $this->_CreateResponseTicket
  70. (
  71. 'UserRequest',
  72. $sTitle,
  73. $sDescription,
  74. $aCallerDesc,
  75. $aCustomerDesc,
  76. $aServiceDesc,
  77. $aServiceSubcategoryDesc,
  78. $sProduct,
  79. $aWorkgroupDesc,
  80. $aImpactedCIs,
  81. $sImpact,
  82. $sUrgency
  83. );
  84. return $oRes->ToSoapStructure();
  85. }
  86. public function CreateIncidentTicket($sLogin, $sPassword, $sTitle, $sDescription, $oCallerDesc, $oCustomerDesc, $oServiceDesc, $oServiceSubcategoryDesc, $sProduct, $oWorkgroupDesc, $aSOAPImpactedCIs, $sImpact, $sUrgency)
  87. {
  88. if (!UserRights::CheckCredentials($sLogin, $sPassword))
  89. {
  90. $oRes = new WebServiceResultFailedLogin($sLogin);
  91. $this->LogUsage(__FUNCTION__, $oRes);
  92. return $oRes->ToSoapStructure();
  93. }
  94. UserRights::Login($sLogin);
  95. $aCallerDesc = self::SoapStructToExternalKeySearch($oCallerDesc);
  96. $aCustomerDesc = self::SoapStructToExternalKeySearch($oCustomerDesc);
  97. $aServiceDesc = self::SoapStructToExternalKeySearch($oServiceDesc);
  98. $aServiceSubcategoryDesc = self::SoapStructToExternalKeySearch($oServiceSubcategoryDesc);
  99. $aWorkgroupDesc = self::SoapStructToExternalKeySearch($oWorkgroupDesc);
  100. $aImpactedCIs = array();
  101. if (is_null($aSOAPImpactedCIs)) $aSOAPImpactedCIs = array();
  102. foreach($aSOAPImpactedCIs as $oImpactedCIs)
  103. {
  104. $aImpactedCIs[] = self::SoapStructToLinkCreationSpec($oImpactedCIs);
  105. }
  106. $oRes = $this->_CreateResponseTicket
  107. (
  108. 'Incident',
  109. $sTitle,
  110. $sDescription,
  111. $aCallerDesc,
  112. $aCustomerDesc,
  113. $aServiceDesc,
  114. $aServiceSubcategoryDesc,
  115. $sProduct,
  116. $aWorkgroupDesc,
  117. $aImpactedCIs,
  118. $sImpact,
  119. $sUrgency
  120. );
  121. return $oRes->ToSoapStructure();
  122. }
  123. /**
  124. * Create an ResponseTicket (Incident or UserRequest) from an external system
  125. * Some CIs might be specified (by their name/IP)
  126. *
  127. * @param string sClass The class of the ticket: Incident or UserRequest
  128. * @param string sTitle
  129. * @param string sDescription
  130. * @param array aCallerDesc
  131. * @param array aCustomerDesc
  132. * @param array aServiceDesc
  133. * @param array aServiceSubcategoryDesc
  134. * @param string sProduct
  135. * @param array aWorkgroupDesc
  136. * @param array aImpactedCIs
  137. * @param string sImpact
  138. * @param string sUrgency
  139. *
  140. * @return WebServiceResult
  141. */
  142. protected function _CreateResponseTicket($sClass, $sTitle, $sDescription, $aCallerDesc, $aCustomerDesc, $aServiceDesc, $aServiceSubcategoryDesc, $sProduct, $aWorkgroupDesc, $aImpactedCIs, $sImpact, $sUrgency)
  143. {
  144. $oRes = new WebServiceResult();
  145. try
  146. {
  147. $oMyChange = MetaModel::NewObject("CMDBChange");
  148. $oMyChange->Set("date", time());
  149. $oMyChange->Set("userinfo", "Administrator");
  150. $iChangeId = $oMyChange->DBInsertNoReload();
  151. $oNewTicket = MetaModel::NewObject($sClass);
  152. $this->MyObjectSetScalar('title', 'title', $sTitle, $oNewTicket, $oRes);
  153. $this->MyObjectSetScalar('description', 'description', $sDescription, $oNewTicket, $oRes);
  154. $this->MyObjectSetExternalKey('org_id', 'customer', $aCustomerDesc, $oNewTicket, $oRes);
  155. $this->MyObjectSetExternalKey('caller_id', 'caller', $aCallerDesc, $oNewTicket, $oRes);
  156. $this->MyObjectSetExternalKey('service_id', 'service', $aServiceDesc, $oNewTicket, $oRes);
  157. if (!array_key_exists('service_id', $aServiceSubcategoryDesc))
  158. {
  159. $aServiceSubcategoryDesc['service_id'] = $oNewTicket->Get('service_id');
  160. }
  161. $this->MyObjectSetExternalKey('servicesubcategory_id', 'servicesubcategory', $aServiceSubcategoryDesc, $oNewTicket, $oRes);
  162. $this->MyObjectSetScalar('product', 'product', $sProduct, $oNewTicket, $oRes);
  163. $this->MyObjectSetExternalKey('workgroup_id', 'workgroup', $aWorkgroupDesc, $oNewTicket, $oRes);
  164. $aDevicesNotFound = $this->AddLinkedObjects('ci_list', 'impacted_cis', 'FunctionalCI', $aImpactedCIs, $oNewTicket, $oRes);
  165. if (count($aDevicesNotFound) > 0)
  166. {
  167. $this->MyObjectSetScalar('description', 'n/a', $sDescription.' - Related CIs: '.implode(', ', $aDevicesNotFound), $oNewTicket, $oRes);
  168. }
  169. else
  170. {
  171. $this->MyObjectSetScalar('description', 'n/a', $sDescription, $oNewTicket, $oRes);
  172. }
  173. $this->MyObjectSetScalar('impact', 'impact', $sImpact, $oNewTicket, $oRes);
  174. $this->MyObjectSetScalar('urgency', 'urgency', $sUrgency, $oNewTicket, $oRes);
  175. $this->MyObjectInsert($oNewTicket, 'created', $oMyChange, $oRes);
  176. }
  177. catch (CoreException $e)
  178. {
  179. $oRes->LogError($e->getMessage());
  180. }
  181. catch (Exception $e)
  182. {
  183. $oRes->LogError($e->getMessage());
  184. }
  185. $this->LogUsage(__FUNCTION__, $oRes);
  186. return $oRes;
  187. }
  188. /**
  189. * Given an OQL, returns a set of objects (several objects could be on the same row)
  190. *
  191. * @param string sOQL
  192. */
  193. public function SearchObjects($sLogin, $sPassword, $sOQL)
  194. {
  195. if (!UserRights::CheckCredentials($sLogin, $sPassword))
  196. {
  197. $oRes = new WebServiceResultFailedLogin($sLogin);
  198. $this->LogUsage(__FUNCTION__, $oRes);
  199. return $oRes->ToSoapStructure();
  200. }
  201. UserRights::Login($sLogin);
  202. $oRes = $this->_SearchObjects($sOQL);
  203. return $oRes->ToSoapStructure();
  204. }
  205. protected function _SearchObjects($sOQL)
  206. {
  207. $oRes = new WebServiceResult();
  208. try
  209. {
  210. $oSearch = DBObjectSearch::FromOQL($sOQL);
  211. $oSet = new DBObjectSet($oSearch);
  212. $aData = $oSet->ToArrayOfValues();
  213. foreach($aData as $iRow => $aRow)
  214. {
  215. $oRes->AddResultRow("row_$iRow", $aRow);
  216. }
  217. }
  218. catch (CoreException $e)
  219. {
  220. $oRes->LogError($e->getMessage());
  221. }
  222. catch (Exception $e)
  223. {
  224. $oRes->LogError($e->getMessage());
  225. }
  226. $this->LogUsage(__FUNCTION__, $oRes);
  227. return $oRes;
  228. }
  229. }
  230. ?>