Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended TXT parsing #112

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Records/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ public function __construct(array $attributes)
$key = str_replace('-', '_', $key);

if (property_exists($this, $key)) {
$this->$key = $this->cast($key, $value);
$return = $this->cast($key, $value);
if(is_array($return)){
foreach($return as $key => $subvalue){
$this->$key = $subvalue;
}
} else {
$this->$key = $return;
}
}
}
}
Expand Down Expand Up @@ -141,4 +148,11 @@ protected function castType(string $value): string
{
return mb_strtoupper($value);
}

protected function castV(string $value): object
{
preg_match('/v=([a-zA-Z0-9]+);?\W(.*)/', $value, $matches);
$v = "Spatie\\Dns\\TXTRecords\\".mb_strtoupper($matches[1]);
return new $v($matches[2]);
}
}
23 changes: 18 additions & 5 deletions src/Records/TXT.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

namespace Spatie\Dns\Records;

use Spatie\Dns\TXTRecords;

/**
* @method string txt()
* @method object v()
*/
class TXT extends Record
{
protected string $txt;
protected object $v;

public static function parse(string $line): ?self
{
$attributes = static::lineToArray($line, 5);
$attributes = static::lineToArray($line, 6);

if (count($attributes) < 5) {
if (count($attributes) < 6) {
return null;
}

Expand All @@ -23,17 +27,25 @@ public static function parse(string $line): ?self
'class' => $attributes[2],
'type' => $attributes[3],
'txt' => $attributes[4],
'v' => $attributes[5]
]);
}

public function __toString(): string
{
return "{$this->host}.\t\t{$this->ttl}\t{$this->class}\t{$this->type}\t\"{$this->txt}\"";
return "{$this->host}.\t\t{$this->ttl}\t{$this->class}\t{$this->type}\t\"{$this->txt}\t\"{$this->v}\"";
}

protected function castTxt(string $value): array
{
return array('txt' => $this->prepareText($value), 'v' => $this->castV($value));
}

protected function castTxt(string $value): string
protected function castV(string $value): object
{
return $this->prepareText($value);
preg_match('/v=([a-zA-Z0-9]+);?\W(.*)/', $value, $matches);
$v = "Spatie\\Dns\\TXTRecords\\".mb_strtoupper($matches[1]);
return new $v($matches[2]);
}

public function toArray()
Expand All @@ -44,6 +56,7 @@ public function toArray()
'class' => $this->class,
'type' => $this->type,
'txt' => $this->txt,
'v' => $this->v
];
}
}
35 changes: 35 additions & 0 deletions src/TXTRecords/BIMI1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Spatie\Dns\TXTRecords;

class BIMI1 extends V {

protected string $l;
protected string $a;

function __construct(string $value)
{
$this->type = 'BIMI';
$this->version = 1;
$this->l = $this->cast('l',$value);
$this->a = $this->cast('a',$value);
}

function castL(string $value): string
{
preg_match('/l=(https\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}\/\S*)?/', $value, $matches);
if(count($matches) < 2){
return "";
}
return str_replace(";", "",$this->prepareText($matches[1]));
}

function castA(string $value): string
{
preg_match('/a=(https\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}\/\S*)?/', $value, $matches);
if(count($matches) < 2){
return "";
}
return str_replace(";", "",$this->prepareText($matches[1]));
}
}
35 changes: 35 additions & 0 deletions src/TXTRecords/DKIM1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Spatie\Dns\TXTRecords;

class DKIM1 extends V {

protected string $k;
protected string $p;

function __construct(string $value)
{
$this->type = 'DKIM';
$this->version = 1;
$this->k = $this->cast('k',$value);
$this->p = $this->cast('p',$value);
}

function castK(string $value): string
{
preg_match('/k=([a-zA-Z0-9]+)/', $value, $matches);
if(count($matches) < 2){
return "";
}
return str_replace(";", "",$this->prepareText($matches[1]));
}

function castP(string $value): string
{
preg_match('/p=([a-zA-Z0-9_\/\+-=]+)/', $value, $matches);
if(count($matches) < 2){
return "";
}
return str_replace(";", "",$this->prepareText($matches[1]));
}
}
91 changes: 91 additions & 0 deletions src/TXTRecords/DMARC1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Spatie\Dns\TXTRecords;

class DMARC1 extends V {

protected string $p;
protected array $rua;
protected array $ruf;
protected string $sp;
protected int $pct;
protected string $fo;

function __construct(string $value)
{
$this->type = 'DMARC';
$this->version = 1;
$this->p = $this->cast('p',$value);
$this->rua = $this->cast('rua',$value);
$this->ruf = $this->cast('ruf',$value);
$this->sp = $this->cast('sp',$value);
$this->pct = $this->cast('pct',$value);
$this->fo = $this->cast('fo',$value);
}

function castP(string $value): string
{
preg_match('/p=(none|quarantine|reject)/', $value, $matches);
if(count($matches) < 2){
return "";
}
return str_replace(";", "",$this->prepareText($matches[1]));
}

function castRua(string $value): array
{
preg_match("/rua=([^;]*)(?:;|$)/i", $value, $matches);
if (isset($matches[1])) {
$emails = preg_split("/\s*,/", $matches[1]);
foreach ($emails as $key => $email) {
$emails[$key] = $this->prepareText($email);
}
}
else {
return array();
}
return $emails;
}

function castRuf(string $value): array
{
preg_match("/ruf=([^;]*)(?:;|$)/i", $value, $matches);
if (isset($matches[1])) {
$emails = preg_split("/\s*,/", $matches[1]);
foreach ($emails as $key => $email) {
$emails[$key] = $this->prepareText($email);
}
}
else {
return array();
}
return $emails;
}

function castSp(string $value): string
{
preg_match('/sp=(none|quarantine|reject)/', $value, $matches);
if(count($matches) < 2){
return "";
}
return str_replace(";", "",$this->prepareText($matches[1]));
}

function castPct(string $value): int
{
preg_match('/pct=([0-9]{1-3}+)/', $value, $matches);
if(count($matches) < 2){
return 100;
}
return str_replace(";", "",$this->prepareInt($matches[1]));
}

function castFo(string $value): string
{
preg_match('/fo=([a-zA-Z0-9:]+)/', $value, $matches);
if(count($matches) < 2){
return "";
}
return str_replace(";", "",$this->prepareText($matches[1]));
}
}
27 changes: 27 additions & 0 deletions src/TXTRecords/SPF1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Spatie\Dns\TXTRecords;

class SPF1 extends V {

protected array $value;

function __construct(string $value)
{
$this->type = 'SPF';
$this->version = 1;
$this->value = $this->cast('value',$value);
}

function castValue(string $value): array
{
preg_match_all('/([a-zA-Z0-9:\.-~?]+)+/', $value, $matches);
if(count($matches) < 1){
return array();
}
foreach ($matches[1] as $key => $match) {
$matches[1][$key] = $this->prepareText($match);
}
return str_replace(";", "",$matches[1]);
}
}
21 changes: 21 additions & 0 deletions src/TXTRecords/STSV1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Spatie\Dns\TXTRecords;

class STSV1 extends V {

protected int $id;

function __construct(string $value)
{
$this->type = 'STS';
$this->version = 1;
$this->id = $this->cast('id',$value);
}

function castId(string $value): int
{
preg_match('/id=([0-9]+)/', $value, $matches);
return $this->prepareInt($matches[1]);
}
}
30 changes: 30 additions & 0 deletions src/TXTRecords/TLSRPTV1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Spatie\Dns\TXTRecords;

class TLSRPTV1 extends V {

protected array $rua;

function __construct(string $value)
{
$this->type = 'TLSRPT';
$this->version = 1;
$this->rua = $this->cast('rua',$value);
}

function castRua(string $value): array
{
preg_match("/rua=([^;]*)(?:;|$)/i", $value, $matches);
if (isset($matches[1])) {
$emails = preg_split("/\s*,/", $matches[1]);
foreach ($emails as $key => $email) {
$emails[$key] = $this->prepareText($email);
}
}
else {
return array();
}
return $emails;
}
}
Loading
Loading