123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674 |
- <?php
- // Copyright (C) 2010 Combodo SARL
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; version 3 of the License.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- /**
- * Implementation of iTop SOAP services
- *
- * @author Erwan Taloc <erwan.taloc@combodo.com>
- * @author Romain Quetiez <romain.quetiez@combodo.com>
- * @author Denis Flaven <denis.flaven@combodo.com>
- * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
- */
- require_once(APPROOT.'/webservices/itopsoaptypes.class.inc.php');
- /**
- * Generic response of iTop SOAP services
- *
- * @package iTopORM
- */
- class WebServiceResult
- {
-
- /**
- * Overall status
- *
- * @var m_bStatus
- */
- public $m_bStatus;
- /**
- * Error log
- *
- * @var m_aErrors
- */
- public $m_aErrors;
- /**
- * Warning log
- *
- * @var m_aWarnings
- */
- public $m_aWarnings;
- /**
- * Information log
- *
- * @var m_aInfos
- */
- public $m_aInfos;
- /**
- * Constructor
- *
- * @param status $bStatus
- */
- public function __construct()
- {
- $this->m_bStatus = true;
- $this->m_aResult = array();
- $this->m_aErrors = array();
- $this->m_aWarnings = array();
- $this->m_aInfos = array();
- }
- public function ToSoapStructure()
- {
- $aResults = array();
- foreach($this->m_aResult as $sLabel => $aData)
- {
- $aValues = array();
- foreach($aData as $sKey => $value)
- {
- $aValues[] = new SoapResultData($sKey, $value);
- }
- $aResults[] = new SoapResultMessage($sLabel, $aValues);
- }
- $aInfos = array();
- foreach($this->m_aInfos as $sMessage)
- {
- $aInfos[] = new SoapLogMessage($sMessage);
- }
- $aWarnings = array();
- foreach($this->m_aWarnings as $sMessage)
- {
- $aWarnings[] = new SoapLogMessage($sMessage);
- }
- $aErrors = array();
- foreach($this->m_aErrors as $sMessage)
- {
- $aErrors[] = new SoapLogMessage($sMessage);
- }
- $oRet = new SOAPResult(
- $this->m_bStatus,
- $aResults,
- new SOAPResultLog($aErrors),
- new SOAPResultLog($aWarnings),
- new SOAPResultLog($aInfos)
- );
- return $oRet;
- }
- /**
- * Did the current processing encounter a stopper issue ?
- *
- * @return bool
- */
- public function IsOk()
- {
- return $this->m_bStatus;
- }
- /**
- * Add result details - object reference
- *
- * @param string sLabel
- * @param object oObject
- */
- public function AddResultObject($sLabel, $oObject)
- {
- $this->m_aResult[$sLabel] = array(
- 'id' => $oObject->GetKey(),
- 'name' => $oObject->GetName(),
- 'url' => $oObject->GetHyperlink(),
- );
- }
- /**
- * Log an error
- *
- * @param string sDescription
- */
- public function LogError($sDescription)
- {
- $this->m_aErrors[] = $sDescription;
- // Note: SOAP do transform false into null
- $this->m_bStatus = 0;
- }
- /**
- * Log a warning
- *
- * @param string sDescription
- */
- public function LogWarning($sDescription)
- {
- $this->m_aWarnings[] = $sDescription;
- }
- /**
- * Log an error or a warning
- *
- * @param string sDescription
- * @param boolean bIsStopper
- */
- public function LogIssue($sDescription, $bIsStopper = true)
- {
- if ($bIsStopper) $this->LogError($sDescription);
- else $this->LogWarning($sDescription);
- }
- /**
- * Log operation details
- *
- * @param description $sDescription
- */
- public function LogInfo($sDescription)
- {
- $this->m_aInfos[] = $sDescription;
- }
- protected static function LogToText($aLog)
- {
- return implode("\n", $aLog);
- }
- public function GetInfoAsText()
- {
- return self::LogToText($this->m_aInfos);
- }
- public function GetWarningsAsText()
- {
- return self::LogToText($this->m_aWarnings);
- }
- public function GetErrorsAsText()
- {
- return self::LogToText($this->m_aErrors);
- }
- public function GetReturnedDataAsText()
- {
- $sRet = '';
- foreach ($this->m_aResult as $sKey => $value)
- {
- $sRet .= "===== $sKey =====\n";
- $sRet .= print_r($value, true);
- }
- return $sRet;
- }
- }
- /**
- * Generic response of iTop SOAP services - failed login
- *
- * @package iTopORM
- */
- class WebServiceResultFailedLogin extends WebServiceResult
- {
- public function __construct($sLogin)
- {
- parent::__construct();
- $this->LogError("Wrong credentials: '$sLogin'");
- }
- }
- /**
- * Implementation of the Services
- *
- * @package iTopORM
- */
- class WebServices
- {
- /**
- * Helper to log a service delivery
- *
- * @param string sVerb
- * @param array aArgs
- * @param WebServiceResult oRes
- *
- */
- protected function LogUsage($sVerb, $oRes)
- {
- if (!MetaModel::IsLogEnabledWebService()) return;
- $oLog = new EventWebService();
- if ($oRes->IsOk())
- {
- $oLog->Set('message', $sVerb.' was successfully invoked');
- }
- else
- {
- $oLog->Set('message', $sVerb.' returned errors');
- }
- $oLog->Set('userinfo', UserRights::GetUser());
- $oLog->Set('verb', $sVerb);
- $oLog->Set('result', $oRes->IsOk());
- $oLog->Set('log_info', $oRes->GetInfoAsText());
- $oLog->Set('log_warning', $oRes->GetWarningsAsText());
- $oLog->Set('log_error', $oRes->GetErrorsAsText());
- $oLog->Set('data', $oRes->GetReturnedDataAsText());
- $oLog->DBInsertNoReload();
- }
- /**
- * Helper to set a scalar attribute
- *
- * @param string sAttCode
- * @param scalar value
- * @param DBObject oTargetObj
- * @param WebServiceResult oRes
- *
- */
- protected function MyObjectSetScalar($sAttCode, $sParamName, $value, &$oTargetObj, &$oRes)
- {
- $res = $oTargetObj->CheckValue($sAttCode, $value);
- if ($res === true)
- {
- $oTargetObj->Set($sAttCode, $value);
- }
- else
- {
- // $res contains the error description
- $oRes->LogError("Unexpected value for parameter $sParamName: $res");
- }
- }
- /**
- * Helper to set an external key
- *
- * @param string sAttCode
- * @param array aExtKeyDesc
- * @param DBObject oTargetObj
- * @param WebServiceResult oRes
- *
- */
- protected function MyObjectSetExternalKey($sAttCode, $sParamName, $aExtKeyDesc, &$oTargetObj, &$oRes)
- {
- $oExtKey = MetaModel::GetAttributeDef(get_class($oTargetObj), $sAttCode);
- $bIsMandatory = !$oExtKey->IsNullAllowed();
- if (is_null($aExtKeyDesc))
- {
- if ($bIsMandatory)
- {
- $oRes->LogError("Parameter $sParamName: found null for a mandatory key");
- }
- else
- {
- // skip silently
- return;
- }
- }
- if (count($aExtKeyDesc) == 0)
- {
- $oRes->LogIssue("Parameter $sParamName: no search condition has been specified", $bIsMandatory);
- return;
- }
- $sKeyClass = $oExtKey->GetTargetClass();
- $oReconFilter = new CMDBSearchFilter($sKeyClass);
- foreach ($aExtKeyDesc as $sForeignAttCode => $value)
- {
- if (!MetaModel::IsValidFilterCode($sKeyClass, $sForeignAttCode))
- {
- $aCodes = array_keys(MetaModel::GetClassFilterDefs($sKeyClass));
- $sMsg = "Parameter $sParamName: '$sForeignAttCode' is not a valid filter code for class '$sKeyClass', expecting a value in {".implode(', ', $aCodes)."}";
- $oRes->LogIssue($sMsg, $bIsMandatory);
- }
- // The foreign attribute is one of our reconciliation key
- $oReconFilter->AddCondition($sForeignAttCode, $value, '=');
- }
- $oExtObjects = new CMDBObjectSet($oReconFilter);
- switch($oExtObjects->Count())
- {
- case 0:
- $sMsg = "Parameter $sParamName: no match (searched: '".$oReconFilter->ToOQL(true)."')";
- $oRes->LogIssue($sMsg, $bIsMandatory);
- break;
- case 1:
- // Do change the external key attribute
- $oForeignObj = $oExtObjects->Fetch();
- $oTargetObj->Set($sAttCode, $oForeignObj->GetKey());
- // Report it (no need to report if the object already had this value
- if (array_key_exists($sAttCode, $oTargetObj->ListChanges()))
- {
- $oRes->LogInfo("Parameter $sParamName: found match ".get_class($oForeignObj)."::".$oForeignObj->GetKey()." '".$oForeignObj->GetName()."'");
- }
- break;
- default:
- $sMsg = "Parameter $sParamName: Found ".$oExtObjects->Count()." matches (searched: '".$oReconFilter->ToOQL(true)."')";
- $oRes->LogIssue($sMsg, $bIsMandatory);
- }
- }
- /**
- * Helper to link objects
- *
- * @param string sLinkAttCode
- * @param string sLinkedClass
- * @param array $aLinkList
- * @param DBObject oTargetObj
- * @param WebServiceResult oRes
- *
- * @return array List of objects that could not be found
- */
- protected function AddLinkedObjects($sLinkAttCode, $sParamName, $sLinkedClass, $aLinkList, &$oTargetObj, &$oRes)
- {
- $oLinkAtt = MetaModel::GetAttributeDef(get_class($oTargetObj), $sLinkAttCode);
- $sLinkClass = $oLinkAtt->GetLinkedClass();
- $sExtKeyToItem = $oLinkAtt->GetExtKeyToRemote();
- $aItemsFound = array();
- $aItemsNotFound = array();
-
- if (is_null($aLinkList))
- {
- return $aItemsNotFound;
- }
- foreach ($aLinkList as $aItemData)
- {
- if (!array_key_exists('class', $aItemData))
- {
- $oRes->LogWarning("Parameter $sParamName: missing 'class' specification");
- continue; // skip
- }
- $sTargetClass = $aItemData['class'];
- if (!MetaModel::IsValidClass($sTargetClass))
- {
- $oRes->LogError("Parameter $sParamName: invalid class '$sTargetClass'");
- continue; // skip
- }
- if (!MetaModel::IsParentClass($sLinkedClass, $sTargetClass))
- {
- $oRes->LogError("Parameter $sParamName: '$sTargetClass' is not a child class of '$sLinkedClass'");
- continue; // skip
- }
- $oReconFilter = new CMDBSearchFilter($sTargetClass);
- $aCIStringDesc = array();
- foreach ($aItemData['search'] as $sAttCode => $value)
- {
- if (!MetaModel::IsValidFilterCode($sTargetClass, $sAttCode))
- {
- $aCodes = array_keys(MetaModel::GetClassFilterDefs($sTargetClass));
- $oRes->LogError("Parameter $sParamName: '$sAttCode' is not a valid filter code for class '$sTargetClass', expecting a value in {".implode(', ', $aCodes)."}");
- continue 2; // skip the entire item
- }
- $aCIStringDesc[] = "$sAttCode: $value";
- // The attribute is one of our reconciliation key
- $oReconFilter->AddCondition($sAttCode, $value, '=');
- }
- if (count($aCIStringDesc) == 1)
- {
- // take the last and unique value to describe the object
- $sItemDesc = $value;
- }
- else
- {
- // describe the object by the given keys
- $sItemDesc = $sTargetClass.'('.implode('/', $aCIStringDesc).')';
- }
- $oExtObjects = new CMDBObjectSet($oReconFilter);
- switch($oExtObjects->Count())
- {
- case 0:
- $oRes->LogWarning("Parameter $sParamName: object to link $sLinkedClass / $sItemDesc could not be found (searched: '".$oReconFilter->ToOQL(true)."')");
- $aItemsNotFound[] = $sItemDesc;
- break;
- case 1:
- $aItemsFound[] = array (
- 'object' => $oExtObjects->Fetch(),
- 'link_values' => @$aItemData['link_values'],
- 'desc' => $sItemDesc,
- );
- break;
- default:
- $oRes->LogWarning("Parameter $sParamName: Found ".$oExtObjects->Count()." matches for item '$sItemDesc' (searched: '".$oReconFilter->ToOQL(true)."')");
- $aItemsNotFound[] = $sItemDesc;
- }
- }
- if (count($aItemsFound) > 0)
- {
- $aLinks = array();
- foreach($aItemsFound as $aItemData)
- {
- $oLink = MetaModel::NewObject($sLinkClass);
- $oLink->Set($sExtKeyToItem, $aItemData['object']->GetKey());
- foreach($aItemData['link_values'] as $sKey => $value)
- {
- if(!MetaModel::IsValidAttCode($sLinkClass, $sKey))
- {
- $oRes->LogWarning("Parameter $sParamName: Attaching item '".$aItemData['desc']."', the attribute code '$sKey' is not valid ; check the class '$sLinkClass'");
- }
- else
- {
- $oLink->Set($sKey, $value);
- }
- }
- $aLinks[] = $oLink;
- }
- $oImpactedInfraSet = DBObjectSet::FromArray($sLinkClass, $aLinks);
- $oTargetObj->Set($sLinkAttCode, $oImpactedInfraSet);
- }
- return $aItemsNotFound;
- }
- protected function MyObjectInsert($oTargetObj, $sResultLabel, $oChange, &$oRes)
- {
- if ($oRes->IsOk())
- {
- list($bRes, $aIssues) = $oTargetObj->CheckToWrite();
- if ($bRes)
- {
- $iId = $oTargetObj->DBInsertTrackedNoReload($oChange);
- $oRes->LogInfo("Created object ".get_class($oTargetObj)."::$iId");
- $oRes->AddResultObject($sResultLabel, $oTargetObj);
- }
- else
- {
- $oRes->LogError("The ticket could not be created due to forbidden values (or inconsistent values)");
- foreach($aIssues as $iIssue => $sIssue)
- {
- $oRes->LogError("Issue #$iIssue: $sIssue");
- }
- }
- }
- }
- static protected function SoapStructToExternalKeySearch($oExternalKeySearch)
- {
- if (is_null($oExternalKeySearch)) return null;
- if ($oExternalKeySearch->IsVoid()) return null;
- $aRes = array();
- foreach($oExternalKeySearch->conditions as $oSearchCondition)
- {
- $aRes[$oSearchCondition->attcode] = $oSearchCondition->value;
- }
- return $aRes;
- }
- static protected function SoapStructToLinkCreationSpec(SoapLinkCreationSpec $oLinkCreationSpec)
- {
- $aRes = array
- (
- 'class' => $oLinkCreationSpec->class,
- 'search' => array(),
- 'link_values' => array(),
- );
- foreach($oLinkCreationSpec->conditions as $oSearchCondition)
- {
- $aRes['search'][$oSearchCondition->attcode] = $oSearchCondition->value;
- }
- foreach($oLinkCreationSpec->attributes as $oAttributeValue)
- {
- $aRes['link_values'][$oAttributeValue->attcode] = $oAttributeValue->value;
- }
- return $aRes;
- }
- /**
- * Get the server version (TODO: get it dynamically, where ?)
- *
- * @return WebServiceResult
- */
- static public function GetVersion()
- {
- if (ITOP_REVISION == '$WCREV$')
- {
- $sVersionString = ITOP_VERSION.' [dev]';
- }
- else
- {
- // This is a build made from SVN, let display the full information
- $sVersionString = ITOP_VERSION."-".ITOP_REVISION." ".ITOP_BUILD_DATE;
- }
- return $sVersionString;
- }
- public function CreateIncidentTicket($sLogin, $sPassword, $sTitle, $sDescription, $oCallerDesc, $oCustomerDesc, $oServiceDesc, $oServiceSubcategoryDesc, $sProduct, $oWorkgroupDesc, $aSOAPImpactedCIs, $sImpact, $sUrgency)
- {
- if (!UserRights::CheckCredentials($sLogin, $sPassword))
- {
- $oRes = new WebServiceResultFailedLogin($sLogin);
- $this->LogUsage(__FUNCTION__, $oRes);
- return $oRes->ToSoapStructure();
- }
- UserRights::Login($sLogin);
- $aCallerDesc = self::SoapStructToExternalKeySearch($oCallerDesc);
- $aCustomerDesc = self::SoapStructToExternalKeySearch($oCustomerDesc);
- $aServiceDesc = self::SoapStructToExternalKeySearch($oServiceDesc);
- $aServiceSubcategoryDesc = self::SoapStructToExternalKeySearch($oServiceSubcategoryDesc);
- $aWorkgroupDesc = self::SoapStructToExternalKeySearch($oWorkgroupDesc);
- $aImpactedCIs = array();
- if (is_null($aSOAPImpactedCIs)) $aSOAPImpactedCIs = array();
- foreach($aSOAPImpactedCIs as $oImpactedCIs)
- {
- $aImpactedCIs[] = self::SoapStructToLinkCreationSpec($oImpactedCIs);
- }
- $oRes = $this->_CreateIncidentTicket
- (
- $sTitle,
- $sDescription,
- $aCallerDesc,
- $aCustomerDesc,
- $aServiceDesc,
- $aServiceSubcategoryDesc,
- $sProduct,
- $aWorkgroupDesc,
- $aImpactedCIs,
- $sImpact,
- $sUrgency
- );
- return $oRes->ToSoapStructure();
- }
- /**
- * Create an incident ticket from a monitoring system
- * Some CIs might be specified (by their name/IP)
- *
- * @param string sTitle
- * @param string sDescription
- * @param array aCallerDesc
- * @param array aCustomerDesc
- * @param array aServiceDesc
- * @param array aServiceSubcategoryDesc
- * @param string sProduct
- * @param array aWorkgroupDesc
- * @param array aImpactedCIs
- * @param string sImpact
- * @param string sUrgency
- *
- * @return WebServiceResult
- */
- protected function _CreateIncidentTicket($sTitle, $sDescription, $aCallerDesc, $aCustomerDesc, $aServiceDesc, $aServiceSubcategoryDesc, $sProduct, $aWorkgroupDesc, $aImpactedCIs, $sImpact, $sUrgency)
- {
- $oRes = new WebServiceResult();
- try
- {
- $oMyChange = MetaModel::NewObject("CMDBChange");
- $oMyChange->Set("date", time());
- $oMyChange->Set("userinfo", "Administrator");
- $iChangeId = $oMyChange->DBInsertNoReload();
-
- $oNewTicket = MetaModel::NewObject('Incident');
- $this->MyObjectSetScalar('title', 'title', $sTitle, $oNewTicket, $oRes);
- $this->MyObjectSetScalar('description', 'description', $sDescription, $oNewTicket, $oRes);
- $this->MyObjectSetExternalKey('org_id', 'customer', $aCustomerDesc, $oNewTicket, $oRes);
- $this->MyObjectSetExternalKey('caller_id', 'caller', $aCallerDesc, $oNewTicket, $oRes);
-
- $this->MyObjectSetExternalKey('service_id', 'service', $aServiceDesc, $oNewTicket, $oRes);
- $this->MyObjectSetExternalKey('servicesubcategory_id', 'servicesubcategory', $aServiceSubcategoryDesc, $oNewTicket, $oRes);
- $this->MyObjectSetScalar('product', 'product', $sProduct, $oNewTicket, $oRes);
- $this->MyObjectSetExternalKey('workgroup_id', 'workgroup', $aWorkgroupDesc, $oNewTicket, $oRes);
- $aDevicesNotFound = $this->AddLinkedObjects('ci_list', 'impacted_cis', 'FunctionalCI', $aImpactedCIs, $oNewTicket, $oRes);
- if (count($aDevicesNotFound) > 0)
- {
- $this->MyObjectSetScalar('description', 'n/a', $sDescription.' - Related CIs: '.implode(', ', $aDevicesNotFound), $oNewTicket, $oRes);
- }
- else
- {
- $this->MyObjectSetScalar('description', 'n/a', $sDescription, $oNewTicket, $oRes);
- }
- $this->MyObjectSetScalar('impact', 'impact', $sImpact, $oNewTicket, $oRes);
- $this->MyObjectSetScalar('urgency', 'urgency', $sUrgency, $oNewTicket, $oRes);
- $this->MyObjectInsert($oNewTicket, 'created', $oMyChange, $oRes);
- }
- catch (CoreException $e)
- {
- $oRes->LogError($e->getMessage());
- }
- catch (Exception $e)
- {
- $oRes->LogError($e->getMessage());
- }
- $this->LogUsage(__FUNCTION__, $oRes);
- return $oRes;
- }
- }
- ?>
|