1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Badcow DNS Library. |
7
|
|
|
* |
8
|
|
|
* (c) Samuel Williams <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Badcow\DNS\Rdata; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\Parser\StringIterator; |
17
|
|
|
use Badcow\DNS\Parser\Tokens; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @see https://tools.ietf.org/html/rfc1035#section-3.3.14 |
21
|
|
|
*/ |
22
|
|
|
class TXT implements RdataInterface |
23
|
|
|
{ |
24
|
|
|
use RdataTrait; |
25
|
|
|
|
26
|
|
|
const TYPE = 'TXT'; |
27
|
|
|
const TYPE_CODE = 16; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string|null |
31
|
|
|
*/ |
32
|
|
|
private $text; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string|null $text |
36
|
|
|
*/ |
37
|
21 |
|
public function setText(?string $text): void |
38
|
|
|
{ |
39
|
21 |
|
if (null === $text) { |
40
|
|
|
$this->text = null; |
41
|
|
|
|
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
21 |
|
$this->text = stripslashes($text); |
46
|
21 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string|null |
50
|
|
|
*/ |
51
|
6 |
|
public function getText(): ?string |
52
|
|
|
{ |
53
|
6 |
|
return (string) $this->text ?? ''; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
4 |
|
public function toText(): string |
60
|
|
|
{ |
61
|
4 |
|
return sprintf('"%s"', addslashes($this->text ?? '')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
1 |
|
public function toWire(): string |
68
|
|
|
{ |
69
|
1 |
|
return $this->text ?? ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
1 |
|
public static function fromWire(string $rdata): RdataInterface |
76
|
|
|
{ |
77
|
1 |
|
$txt = new self(); |
78
|
1 |
|
$txt->setText($rdata); |
79
|
|
|
|
80
|
1 |
|
return $txt; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
8 |
|
public static function fromText(string $text): RdataInterface |
87
|
|
|
{ |
88
|
8 |
|
$string = new StringIterator($text); |
89
|
8 |
|
$txt = new StringIterator(); |
90
|
|
|
|
91
|
8 |
|
while ($string->valid()) { |
92
|
8 |
|
self::handleTxt($string, $txt); |
93
|
8 |
|
$string->next(); |
94
|
|
|
} |
95
|
|
|
|
96
|
8 |
|
$rdata = new self(); |
97
|
8 |
|
$rdata->setText((string) $txt); |
98
|
|
|
|
99
|
8 |
|
return $rdata; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param StringIterator $string |
104
|
|
|
* @param StringIterator $txt |
105
|
|
|
*/ |
106
|
10 |
|
public static function handleTxt(StringIterator $string, StringIterator $txt): void |
107
|
|
|
{ |
108
|
10 |
|
if ($string->isNot(Tokens::DOUBLE_QUOTES)) { |
109
|
3 |
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
10 |
|
$string->next(); |
113
|
|
|
|
114
|
10 |
|
while ($string->isNot(Tokens::DOUBLE_QUOTES) && $string->valid()) { |
115
|
10 |
|
if ($string->is(Tokens::BACKSLASH)) { |
116
|
5 |
|
$string->next(); |
117
|
|
|
} |
118
|
|
|
|
119
|
10 |
|
$txt->append($string->current()); |
120
|
10 |
|
$string->next(); |
121
|
|
|
} |
122
|
10 |
|
} |
123
|
|
|
} |
124
|
|
|
|