Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
CentralNic
Badcow-DNS
Commits
40e48e58
Commit
40e48e58
authored
Jan 25, 2017
by
samuelwilliams
Browse files
Deprecated Validator::validate().
parent
8f4a714a
Changes
2
Hide whitespace changes
Inline
Side-by-side
lib/Tests/ValidatorTest.php
View file @
40e48e58
...
...
@@ -175,8 +175,10 @@ class ValidatorTest extends TestCase
604800
,
3600
));
$zone
->
addResourceRecord
(
$soa
);
$this
->
assertEquals
(
Validator
::
ZONE_TOO_MANY_SOA
,
Validator
::
zone
(
$zone
));
Validator
::
validate
(
$zone
);
}
...
...
@@ -199,8 +201,10 @@ class ValidatorTest extends TestCase
604800
,
3600
));
$zone
->
addResourceRecord
(
$soa
);
$this
->
assertEquals
(
Validator
::
ZONE_NO_NS
,
Validator
::
zone
(
$zone
));
Validator
::
validate
(
$zone
);
}
...
...
@@ -218,6 +222,8 @@ class ValidatorTest extends TestCase
$a
->
setComment
(
'This class does not belong here'
);
$zone
->
addResourceRecord
(
$a
);
$this
->
assertEquals
(
Validator
::
ZONE_TOO_MANY_CLASSES
,
Validator
::
zone
(
$zone
));
Validator
::
validate
(
$zone
);
}
...
...
@@ -230,6 +236,15 @@ class ValidatorTest extends TestCase
$this
->
assertTrue
(
Validator
::
validate
(
$zone
));
}
/**
*
*/
public
function
testZone
()
{
$zone
=
$this
->
buildTestZone
();
$this
->
assertEquals
(
Validator
::
ZONE_OKAY
,
Validator
::
zone
(
$zone
));
}
public
function
testWildcard
()
{
$valid_1
=
'*.example.com.'
;
...
...
lib/Validator.php
View file @
40e48e58
...
...
@@ -16,6 +16,18 @@ use Badcow\DNS\Rdata\SoaRdata;
class
Validator
{
const
ZONE_OKAY
=
0
;
const
ZONE_NO_SOA
=
1
;
const
ZONE_TOO_MANY_SOA
=
2
;
const
ZONE_NO_NS
=
4
;
const
ZONE_NO_CLASS
=
8
;
const
ZONE_TOO_MANY_CLASSES
=
16
;
/**
* Validates if $string is suitable as an RR name.
*
...
...
@@ -210,7 +222,9 @@ class Validator
* RFC-1035 especially that:
* 1) 5.2.1 All RRs in the file should be of the same class.
* 2) 5.2.2 Exactly one SOA RR should be present at the top of the zone.
* 3) There is at least one NS record.
*
* @deprecated
* @param ZoneInterface $zone
*
* @throws ZoneException
...
...
@@ -219,38 +233,115 @@ class Validator
*/
public
static
function
validate
(
ZoneInterface
$zone
)
{
$n
umber
_soa
=
0
;
$n
umber
_ns
=
0
;
$n_soa
=
self
::
countResourceRecords
(
$zone
,
SoaRdata
::
TYPE
)
;
$n_ns
=
self
::
countResourceRecords
(
$zone
,
NsRdata
::
TYPE
)
;
$classes
=
[];
foreach
(
$zone
->
getResourceRecords
()
as
$rr
)
{
/* @var $rr ResourceRecordInterface */
if
(
SoaRdata
::
TYPE
===
$rr
->
getRdata
()
->
getType
())
{
$number_soa
+=
1
;
if
(
null
!==
$rr
->
getClass
())
{
$classes
[
$rr
->
getClass
()]
=
null
;
}
}
if
(
NsRdata
::
TYPE
===
$rr
->
getRdata
()
->
getType
())
{
$number_ns
+=
1
;
}
$n_class
=
count
(
$classes
);
if
(
1
!==
$n_soa
)
{
throw
new
ZoneException
(
sprintf
(
'There must be exactly one SOA record, %s given.'
,
$n_soa
));
}
if
(
$n_ns
<
1
)
{
throw
new
ZoneException
(
sprintf
(
'There must be at least one NS record, %s given.'
,
$n_ns
));
}
if
(
1
!==
$n_class
)
{
throw
new
ZoneException
(
sprintf
(
'There must be exactly one type of class, %s given.'
,
$n_class
));
}
return
true
;
}
/**
* Validates that the zone meets
* RFC-1035 especially that:
* 1) 5.2.1 All RRs in the file should be of the same class.
* 2) 5.2.2 Exactly one SOA RR should be present at the top of the zone.
* 3) There is at least one NS record.
*
* Return values are:
* - ZONE_NO_SOA
* - ZONE_TOO_MANY_SOA
* - ZONE_NO_NS
* - ZONE_NO_CLASS
* - ZONE_TOO_MANY_CLASSES
* - ZONE_OKAY
*
* You SHOULD compare these return values to the defined constants of this
* class rather than against integers directly.
*
* @param ZoneInterface $zone
*
* @return integer
*/
public
static
function
zone
(
ZoneInterface
$zone
)
{
$n_soa
=
self
::
countResourceRecords
(
$zone
,
SoaRdata
::
TYPE
);
$n_ns
=
self
::
countResourceRecords
(
$zone
,
NsRdata
::
TYPE
);
$classes
=
[];
foreach
(
$zone
->
getResourceRecords
()
as
$rr
)
{
if
(
null
!==
$rr
->
getClass
())
{
$classes
[
$rr
->
getClass
()]
=
null
;
}
}
if
(
$number_soa
!==
1
)
{
throw
new
ZoneException
(
sprintf
(
'There must be exactly one SOA record, %s given.'
,
$number_soa
));
$n_class
=
count
(
$classes
);
if
(
$n_soa
<
1
)
{
return
self
::
ZONE_NO_SOA
;
}
if
(
$n
umber_ns
<
1
)
{
throw
new
ZoneException
(
sprintf
(
'There must be at least one NS record, %s given.'
,
$number_ns
))
;
if
(
$n
_soa
>
1
)
{
return
self
::
ZONE_TOO_MANY_SOA
;
}
if
(
1
!==
$c
=
count
(
$classes
)
)
{
throw
new
ZoneException
(
sprintf
(
'There must be exactly one type of class, %s given.'
,
$c
))
;
if
(
$n_ns
<
1
)
{
return
self
::
ZONE_NO_NS
;
}
return
true
;
if
(
$n_class
<
1
)
{
return
self
::
ZONE_NO_CLASS
;
}
if
(
$n_class
>
1
)
{
return
self
::
ZONE_TOO_MANY_CLASSES
;
}
return
self
::
ZONE_OKAY
;
}
/**
* @param ZoneInterface $zone
* @param null $type The ResourceRecord type to be counted. If NULL, then the method will return
* the total number of resource records.
*
* @return int The number of records to be counted.
*/
public
static
function
countResourceRecords
(
ZoneInterface
$zone
,
$type
=
null
)
{
if
(
null
===
$type
)
{
return
count
(
$zone
->
getResourceRecords
());
}
$n
=
0
;
foreach
(
$zone
->
getResourceRecords
()
as
$rr
)
{
/* @var $rr ResourceRecordInterface */
if
(
$type
===
$rr
->
getRdata
()
->
getType
())
{
$n
+=
1
;
}
}
return
$n
;
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment