. */ namespace SP\Util\Wiki; use DOMDocument; use DOMException; use SP\Config\Config; use SP\Config\ConfigData; use SP\Core\Exceptions\SPException; use SP\Http\XMLRPCResponseParse; use SP\Util\Util; /** * Class DokuWikiApiBase * * @package SP\Util\Wiki */ abstract class DokuWikiApiBase { /** * @var string */ protected $apiUser = ''; /** * @var string */ protected $apiPassword = ''; /** * @var string */ protected $apiUrl = ''; /** * @var ConfigData */ protected $ConfigData; /** * @var DOMDocument */ private $xml; /** * @var \DOMElement */ private $root; /** * @var \DOMElement */ private $params; /** * DokuWikiApiBase constructor. */ public function __construct() { $this->injectDependencies(); } /** * @return string */ public function getXml() { return $this->xml->saveXML(); } /** * @param Config $config */ public function inject(Config $config) { $this->ConfigData = $config->getConfigData(); } /** * Establecer la autorización * * @return bool|string * @throws SPException */ protected function doLogin() { try { $this->createMsg('dokuwiki.login'); $this->addParam($this->apiUser); $this->addParam($this->apiPassword); return $this->callWiki(); } catch (SPException $e) { throw $e; } } /** * Crear la llamada al método de DokuWiki * * @param $function * * @throws SPException */ protected function createMsg($function) { try { $this->xml = new DOMDocument('1.0', 'UTF-8'); $xmlMethodCall = $this->xml->createElement('methodCall'); $this->root = $this->xml->appendChild($xmlMethodCall); $xmlMethodName = $this->xml->createElement('methodName', $function); $this->root->appendChild($xmlMethodName); $this->params = $this->xml->createElement('params'); $this->root->appendChild($this->params); } catch (DOMException $e) { throw new SPException($e->getMessage(), SPException::WARNING, __FUNCTION__); } } /** * Añadir un parámetro * * @param $value * * @throws \SP\Core\Exceptions\SPException */ protected function addParam($value) { try { $xmlParam = $this->xml->createElement('param'); $xmlValue = $this->xml->createElement('value'); if (is_numeric($value)) { $xmlValue->appendChild($this->xml->createElement('int', (int)$value)); } elseif (is_string($value)) { $xmlValue->appendChild($this->xml->createElement('string', $value)); } elseif (is_bool($value)) { $xmlValue->appendChild($this->xml->createElement('boolean', (int)$value)); } $xmlParam->appendChild($xmlValue); $this->params->appendChild($xmlParam); } catch (DOMException $e) { throw new SPException($e->getMessage(), SPException::WARNING, __FUNCTION__); } } /** * Enviar el XML a la wiki y devolver la respuesta */ protected function callWiki() { try { $data['type'] = ['Content-Type: text/xml']; $data['data'] = $this->xml->saveXML(); return Util::getDataFromUrl($this->apiUrl, $data, true, true); } catch (SPException $e) { throw $e; } } /** * Capturar si han habido errores en la consulta XML * * @param XMLRPCResponseParse $Res * * @throws SPException */ protected function catchError(XMLRPCResponseParse $Res) { $error = $Res->getError(); if (count($error) > 0) { throw new SPException( __('Error al realizar la consulta', false), SPException::WARNING, $error['faultString'] ); } } /** * Escribir el error en el registro de eventos * * @param \SP\Core\Exceptions\SPException $e * @param string $source Origen del error */ protected function logException(SPException $e, $source = null) { processException($e); } /** * Establecer los datos de conexión a la API de DokuWiki * * @param string $url La URL de conexión * @param string $user El usuario de conexión * @param string $pass La clave de conexión * * @throws SPException */ protected function setConnectionData($url, $user, $pass) { $this->apiUrl = empty($url) ? $this->ConfigData->getDokuwikiUrl() : $url; $this->apiUser = empty($user) ? $this->ConfigData->getDokuwikiUser() : $user; $this->apiPassword = empty($pass) ? $this->ConfigData->getDokuwikiPass() : $pass; if (empty($this->apiUrl)) { throw new SPException(__('URL de conexión no establecida', false), SPException::WARNING); } } }