Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
modbot committed Sep 16, 2020
1 parent 56774a4 commit e1b96c1
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "modularr/whitelist",
"type": "library",
"description": "This is a very simple PHP Whitelist Library.",
"keywords": ["php","whitelist"],
"license": "UNLICENSE",
"autoload": {
"classmap": ["src/", "src/Whitelist.php"]
}
}
19 changes: 19 additions & 0 deletions example/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
# Load the Class
include('../Whitelist.php');

# Setup a New WhiteList
$whitelist = new Whitelist('whitelist');
$whitelist->Add('duckduckgo.com'); # Add a Custom entry address.

$site = 'duckduckgo.com'; # Example string to test against

# Verify against WhiteList
if( $whitelist->Verify($site) )
{
echo $site.' is whitelisted.';
}
else
{
echo $site.' is NOT whitelisted.';
}
3 changes: 3 additions & 0 deletions example/whitelist
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
google.com
youtube.com
duckduckgo.com
85 changes: 85 additions & 0 deletions src/Whitelist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* This is a very simple PHP Whitelist Library.
*
* @package Whitelist
* @license http://unlicense.org UnLicense
*/
class Whitelist
{
/**
* WhiteList data.
*
* @var array
* @access protected
*/
protected $whitelisted = array();

/**
* Constructor
*
* @param $file NULL not required
*/
public function __construct($file=NULL)
{
# Only if initialized by default.
if($file != NULL)
{
# Load the File
$this->Load($file);
}
}

/**
* Verification method.
*
* This method verifies the input against the Whitelist.
*
* @param $input
*/
public function Verify($input)
{
# Normalize data.
$input = strtolower($input);

# Verify the Input Against the Array
if(in_array($input,$this->whitelisted))
{
return 1;
}
}

/**
* Add Method.
*
* This method adds a new item in the WhiteList Array manually.
*
* @param $input
*/
public function Add($input)
{
# Manually Add Item to the Array
$this->whitelisted[] = $input;
}

/**
* This method Loads a File, from that result it Splits and stores each newline into the WhiteList array.
*
* @param $file
* @return $whitelisted
*/
public function Load($file)
{
# Read File
$string = file_get_contents($file);

# Split Items into the Array
$this->whitelisted = array_map('trim', explode("\n", $string));

# Lowercase all entries to Normalise data
$this->whitelisted = array_map('strtolower', $this->whitelisted);

# Return Whitelist in case someone wants to check it.
return $this->whitelisted;
}
}

0 comments on commit e1b96c1

Please sign in to comment.