midweste/spf-lib-flattener

A library to flatten SPF records

dev-main 2024-12-29 20:30 UTC

This package is auto-updated.

Last update: 2024-12-29 20:30:37 UTC


README

No tests yet - Use at your own risk!

Extension of the wonderful SPF-LIB library by mlocati (https://github.com/mlocati/spf-lib)

This PHP library allows you to:

  • flatten a spf record into ips addresses
  • split a flattened spf record into primary and child spf records
  • get the SPF record from a domain name (parent lib mlocati/spf-lib)
  • decode and validate the SPF record (parent lib mlocati/spf-lib)
  • create the value of a TXT record (parent lib mlocati/spf-lib)
  • check if domains and IP addresses satisfy the SPF records (parent lib mlocati/spf-lib)
  • all the rest of what SPF-LIB library by mlocati (https://github.com/mlocati/spf-lib) can do

Short introduction about SPF Flattener/Splitter

This library is meant to address the issue where more than 10 lookups are present in a SPF record.

There are two parts:

  • RecordFlattener - this class will flatten an spf record into an aggregated list of ips addresses
  • RecordSplitter - takes a flattened record and split the ip addresses of into child records like spf1.domain.com spf2.domain.com

Because this is an extension to the wonderful SPF-LIB library by mlocati (https://github.com/mlocati/spf-lib), you can use this PHP library to build, validate, flatten, split, and check the SPF records.

Installation

You can install this library with Composer:

composer require midweste/spf-lib-flattener

Usage

Flattening an spf record from a domain name

namespace SpfLibFlattener;

require __DIR__ . '/../vendor/autoload.php';

$domain = 'example.com';
$spf = new SpfFlattener($domain);

$flatArray = $spf->toFlatArray();
$flatString = $spf->toFlatString();
$flatRecord = $spf->toFlatRecord();

Flattening an spf record from an existing string record

namespace SpfLibFlattener;

require __DIR__ . '/../vendor/autoload.php';

$domain = 'example.com';

// Flatten a record by passing the record as a string
$record = 'v=spf1 include:example.com include:google.com -all';
$spf = SpfFlattener::createFromText($domain, $record);
$flatArray = $spf->toFlatArray();
$flatString = $spf->toFlatString();
$flatRecord = $spf->toFlatRecord();

Splitting an spf record from an existing string record

namespace SpfLibFlattener;

require __DIR__ . '/../vendor/autoload.php';

$domain = 'example.com';
$spf = new SpfFlattener($domain);

$splitter = RecordSplitter::createFromTxt($spf->toFlatString());
$split = $splitter->split(512, 'spf#.' . $domain);

foreach ($split as $name => $r) {
    echo "name:$name record:$r<br/>" . PHP_EOL;
}

Do you want to really say thank you?

You can offer the original author that did all the heavy lifting (mlocati) a monthly coffee or a one-time coffee 😉