-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
modbot
committed
Sep 16, 2020
1 parent
56774a4
commit e1b96c1
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
google.com | ||
youtube.com | ||
duckduckgo.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |