Passed
Push — devel-3.0 ( 2581df...bf8280 )
by Rubén
03:03
created

DokuWikiApiBase   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 189
rs 10
c 0
b 0
f 0
wmc 22

10 Methods

Rating   Name   Duplication   Size   Complexity  
A callWiki() 0 9 2
A inject() 0 3 1
A catchError() 0 7 2
A setConnectionData() 0 8 5
A __construct() 0 3 1
A logException() 0 3 1
A createMsg() 0 15 2
A getXml() 0 3 1
A addParam() 0 18 5
A doLogin() 0 9 2
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Services\Wiki;
26
27
use DOMDocument;
28
use SP\Config\Config;
29
use SP\Config\ConfigData;
30
use SP\Core\Exceptions\SPException;
31
use SP\Http\XMLRPCResponseParse;
32
use SP\Util\Util;
33
34
/**
35
 * Class DokuWikiApiBase
36
 *
37
 * @package SP\Services\Wiki
38
 * @deprecated
39
 */
40
abstract class DokuWikiApiBase
41
{
42
    /**
43
     * @var string
44
     */
45
    protected $apiUser = '';
46
    /**
47
     * @var string
48
     */
49
    protected $apiPassword = '';
50
    /**
51
     * @var string
52
     */
53
    protected $apiUrl = '';
54
    /**
55
     * @var ConfigData
56
     */
57
    protected $ConfigData;
58
    /**
59
     * @var DOMDocument
60
     */
61
    private $xml;
62
    /**
63
     * @var \DOMElement
64
     */
65
    private $root;
66
    /**
67
     * @var \DOMElement
68
     */
69
    private $params;
70
71
    /**
72
     * DokuWikiApiBase constructor.
73
     */
74
    public function __construct()
75
    {
76
        $this->injectDependencies();
0 ignored issues
show
Bug introduced by
The method injectDependencies() does not exist on SP\Services\Wiki\DokuWikiApiBase. Did you maybe mean inject()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        $this->/** @scrutinizer ignore-call */ 
77
               injectDependencies();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getXml()
83
    {
84
        return $this->xml->saveXML();
85
    }
86
87
    /**
88
     * @param Config $config
89
     */
90
    public function inject(Config $config)
91
    {
92
        $this->ConfigData = $config->getConfigData();
93
    }
94
95
    /**
96
     * Establecer la autorización
97
     *
98
     * @return bool|string
99
     * @throws SPException
100
     */
101
    protected function doLogin()
102
    {
103
        try {
104
            $this->createMsg('dokuwiki.login');
105
            $this->addParam($this->apiUser);
106
            $this->addParam($this->apiPassword);
107
            return $this->callWiki();
108
        } catch (SPException $e) {
109
            throw $e;
110
        }
111
    }
112
113
    /**
114
     * Crear la llamada al método de DokuWiki
115
     *
116
     * @param $function
117
     *
118
     * @throws SPException
119
     */
120
    protected function createMsg($function)
121
    {
122
        try {
123
            $this->xml = new DOMDocument('1.0', 'UTF-8');
124
125
            $xmlMethodCall = $this->xml->createElement('methodCall');
126
            $this->root = $this->xml->appendChild($xmlMethodCall);
127
128
            $xmlMethodName = $this->xml->createElement('methodName', $function);
129
            $this->root->appendChild($xmlMethodName);
130
131
            $this->params = $this->xml->createElement('params');
132
            $this->root->appendChild($this->params);
133
        } catch (\Exception $e) {
134
            throw new SPException($e->getMessage(), SPException::WARNING, __FUNCTION__);
135
        }
136
    }
137
138
    /**
139
     * Añadir un parámetro
140
     *
141
     * @param $value
142
     *
143
     * @throws \SP\Core\Exceptions\SPException
144
     */
145
    protected function addParam($value)
146
    {
147
        try {
148
            $xmlParam = $this->xml->createElement('param');
149
            $xmlValue = $this->xml->createElement('value');
150
151
            if (is_numeric($value)) {
152
                $xmlValue->appendChild($this->xml->createElement('int', (int)$value));
153
            } elseif (is_string($value)) {
154
                $xmlValue->appendChild($this->xml->createElement('string', $value));
155
            } elseif (is_bool($value)) {
156
                $xmlValue->appendChild($this->xml->createElement('boolean', (int)$value));
157
            }
158
159
            $xmlParam->appendChild($xmlValue);
160
            $this->params->appendChild($xmlParam);
161
        } catch (\Exception $e) {
162
            throw new SPException($e->getMessage(), SPException::WARNING, __FUNCTION__);
163
        }
164
    }
165
166
    /**
167
     * Enviar el XML a la wiki y devolver la respuesta
168
     *
169
     * @throws SPException
170
     */
171
    protected function callWiki()
172
    {
173
        try {
174
            $data['type'] = ['Content-Type: text/xml'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
175
            $data['data'] = $this->xml->saveXML();
176
177
            return Util::getDataFromUrl($this->apiUrl, $data, true, true);
0 ignored issues
show
Bug introduced by
The method getDataFromUrl() does not exist on SP\Util\Util. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
            return Util::/** @scrutinizer ignore-call */ getDataFromUrl($this->apiUrl, $data, true, true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
178
        } catch (SPException $e) {
179
            throw $e;
180
        }
181
    }
182
183
    /**
184
     * Capturar si han habido errores en la consulta XML
185
     *
186
     * @param XMLRPCResponseParse $Res
187
     *
188
     * @throws SPException
189
     */
190
    protected function catchError(XMLRPCResponseParse $Res)
191
    {
192
        $error = $Res->getError();
193
194
        if (count($error) > 0) {
195
            throw new SPException(
196
                __('Error al realizar la consulta', false), SPException::WARNING, $error['faultString']
197
            );
198
        }
199
    }
200
201
    /**
202
     * Escribir el error en el registro de eventos
203
     *
204
     * @param \SP\Core\Exceptions\SPException $e
205
     * @param string                          $source Origen del error
206
     */
207
    protected function logException(SPException $e, $source = null)
0 ignored issues
show
Unused Code introduced by
The parameter $source is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

207
    protected function logException(SPException $e, /** @scrutinizer ignore-unused */ $source = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
208
    {
209
        processException($e);
210
    }
211
212
    /**
213
     * Establecer los datos de conexión a la API de DokuWiki
214
     *
215
     * @param string $url  La URL de conexión
216
     * @param string $user El usuario de conexión
217
     * @param string $pass La clave de conexión
218
     *
219
     * @throws SPException
220
     */
221
    protected function setConnectionData($url, $user, $pass)
222
    {
223
        $this->apiUrl = empty($url) ? $this->ConfigData->getDokuwikiUrl() : $url;
224
        $this->apiUser = empty($user) ? $this->ConfigData->getDokuwikiUser() : $user;
225
        $this->apiPassword = empty($pass) ? $this->ConfigData->getDokuwikiPass() : $pass;
226
227
        if (empty($this->apiUrl)) {
228
            throw new SPException(__('URL de conexión no establecida', false), SPException::WARNING);
229
        }
230
    }
231
}