Issues (493)

lib/SP/Http/Xml.php (2 issues)

1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2019, 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\Http;
26
27
use Klein\Response;
28
29
/**
30
 * Class Xml
31
 *
32
 * @package SP\Http
33
 */
34
final class Xml
35
{
36
    const SAFE = [
37
        'from' => ['&', '<', '>', '"', "\'"],
38
        'to' => ['&amp;', '&lt;', '&gt;', '&quot;', '&apos;']
39
    ];
40
41
    /**
42
     * @var Response
43
     */
44
    private $response;
45
46
    /**
47
     * Xml constructor.
48
     *
49
     * @param Response $response
50
     */
51
    public function __construct(Response $response)
52
    {
53
        $this->response = $response;
54
    }
55
56
    /**
57
     * Devuelve una respuesta en formato XML con el estado y el mensaje.
58
     *
59
     * @param string $description mensaje a devolver
60
     * @param int    $status      devuelve el estado
61
     */
62
    public function printXml(string $description, int $status = 1)
63
    {
64
        if (!is_string($description)) {
0 ignored issues
show
The condition is_string($description) is always true.
Loading history...
65
            return;
66
        }
67
68
        $xml[] = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$xml was never initialized. Although not strictly required by PHP, it is generally a good practice to add $xml = array(); before regardless.
Loading history...
69
        $xml[] = '<root>';
70
        $xml[] = '<status>' . $status . '</status>';
71
        $xml[] = '<description>' . $this->safeString($description) . '</description>';
72
        $xml[] = '</root>';
73
74
        $this->response
75
            ->header('Content-Type', 'application/xml')
76
            ->body(implode(PHP_EOL, $xml))
77
            ->send(true);
78
    }
79
80
    /**
81
     * @param string $string
82
     *
83
     * @return mixed
84
     */
85
    public function safeString(string $string)
86
    {
87
        return str_replace(self::SAFE['from'], self::SAFE['to'], $string);
88
    }
89
}