diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9c2d1fa --- /dev/null +++ b/composer.json @@ -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"] + } +} diff --git a/example/example.php b/example/example.php new file mode 100644 index 0000000..d892a52 --- /dev/null +++ b/example/example.php @@ -0,0 +1,19 @@ +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.'; +} diff --git a/example/whitelist b/example/whitelist new file mode 100644 index 0000000..a581efd --- /dev/null +++ b/example/whitelist @@ -0,0 +1,3 @@ +google.com +youtube.com +duckduckgo.com \ No newline at end of file diff --git a/src/Whitelist.php b/src/Whitelist.php new file mode 100644 index 0000000..486ed45 --- /dev/null +++ b/src/Whitelist.php @@ -0,0 +1,85 @@ +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; + } +}