Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private $filters = array();
- /*
- - Add Filter
- - 'Unpacks' a text file containing words that should be removed or
- blocked in strings it is used against. New line = new word.
- $filterName - the name of the filter, used when there are multiple
- filters used at different points in the website.
- $filterPath - where the text file containing the words is located
- path relative to where this file is.
- */
- public function addFilter($filterName, $filterPath)
- {
- $filter = array(
- $filterName
- );
- // open file
- $success = true;
- $file = fopen($filterPath, "r") or $success = false;
- if ($success) {
- // sort filtered words
- while (($line = fgets($file)) !== false) {
- // remove whitespaces
- $line = trim($line);
- // add to filter
- array_push($filter, $line);
- }
- // append filter
- fclose($file);
- array_push($this->filters, $filter);
- return true;
- }
- return false;
- }
- /*
- - Add to Filter
- - Adds word(s) to the filter.
- $fileName - The name (and path) of the filter text file to add words to.
- $words - An array containing at least one word to add to the filter.
- */
- public function addToFilter($fileName, $words) {
- $file = '';
- $newWords = array();
- if (file_exists($fileName)) {
- $f = fopen($fileName, "r");
- while (($line = fgets($f)) !== false) {
- $line = trim($line);
- array_push($newWords, $line);
- }
- fclose($f);
- }
- foreach ($words as $word) {
- $included = false;
- foreach ($newWords as $newWord) {
- if ($word == $newWord)
- $included = true;
- }
- if (!$included)
- array_push($newWords, $word);
- }
- foreach ($newWords as $newWord) {
- $file .= trim($newWord) . PHP_EOL;
- }
- $f = fopen($fileName, "w+");
- fwrite($f, $file);
- fclose($f);
- return true;
- }
- /*
- - Remove from Filter
- - Removes word(s) from the filter.
- $fileName - The name (and path) of the filter text file to remove words from.
- $words - An array containing at least one word to remove from the filter.
- */
- public function removeFromFilter($fileName, $words) {
- $file = '';
- $newWords = array();
- if (file_exists($fileName)) {
- $f = fopen($fileName, "r");
- while (($line = fgets($f)) !== false) {
- $line = trim($line);
- $continue = true;
- foreach ($words as $word) {
- if ($word == $line)
- $continue = false;
- }
- if ($continue)
- array_push($newWords, $line);
- }
- } else
- return false;
- foreach ($newWords as $newWord) {
- $file .= trim($newWord) . PHP_EOL;
- }
- $f = fopen($fileName, "w+");
- fwrite($f, $file);
- fclose($f);
- return true;
- }
- /*
- - Filter String
- - Checks the string against words that are included in a certain filter.
- - NOTE: If you enter an invalid filter name then this function will return
- false anyway and may cause some confusion.
- - If it returns false then the string was filtered and there was no replace string.
- $filterName - the name of the filter to check the string against.
- $string - the string to be checked for banned/filtered words.
- $strict - true means the word just needs to contain a filtered
- word to be blocked, false means the word must match
- the filtered word to be blocked.
- $replace[=''] - if set, replaces the word with this word, otherwise
- simply returns "" AKA false.
- */
- public function filterString($filterName, $string, $strict = true, $replace = '') {
- // get the correct filter
- $filter = null;
- foreach ($this->filters as $thisFilter) {
- if ($thisFilter[0] == $filterName)
- $filter = $thisFilter;
- }
- if ($filter == null)
- return "";
- else {
- $newString = "";
- // iterate through each word in the string
- foreach (explode(" ", $string) as $char) {
- $match = false;
- $index = 0;
- // iterate through each word in the filter bank
- foreach ($filter as $word) {
- if ($index > 0) {
- if ($strict) {
- // if the word contains this filtered word
- if (stripos($char, $word) !== false)
- $match = true;
- } else {
- // if the word IS this filtered word
- if (strtolower($char) == strtolower($word))
- $match = true;
- }
- }
- $index++;
- }
- if ($match) {
- if ($replace == "")
- return false;
- else
- $newString .= $replace . " ";
- } else
- $newString .= $char . " ";
- }
- return $newString;
- }
- }
- /*
- - IP Banner
- - Allows you to blacklist certain IP addresses
- - You can get someone's IP with the PHP function $_SERVER['REMOTE_ADDR'];
- - Get IPs to ban via text file
- - Returns true if the IP is banned
- $fileName - Path to the text file relative to location of this script
- containing the IP addresses to be blacklisted separated by a new line.
- $redirectPath - Path to another page to redirect to if the user's IP is blacklisted.
- Optional, if left blank nothing will happen except return true if this
- user's IP matches a banned IP address.
- */
- public function blacklist($fileName, $redirectPath = '') {
- $success = true;
- if (file_exists($fileName)) {
- $file = fopen($fileName, "r") or $success = false;
- $blacklisted = false;
- if ($success) {
- // get my IP
- $ip = $_SERVER['REMOTE_ADDR'];
- // sort ip addresses
- while (($line = fgets($file)) !== false) {
- // remove whitespaces
- $line = trim($line);
- // check if this IP = blacklisted IP
- if ($ip == $line)
- $blacklisted = true;
- }
- fclose($file);
- if ($blacklisted) {
- if ($redirectPath == '')
- return true;
- else {
- header('location: ' . $redirectPath);
- die();
- }
- }
- }
- }
- return false;
- }
- /*
- - Add to IP Blacklist
- - Adds multiple (or just one) ip address(es) to the blacklist.
- $fileName - The path and name of the blacklist text file (include the extension .txt)
- $ipAddresses - An array containing the IP addresses (or just address) to be blacklisted.
- */
- public function addToBlacklist($fileName, $ipAddresses) {
- $file = '';
- $ips = array();
- if (file_exists($fileName)) {
- $f = fopen($fileName, "r");
- while (($line = fgets($f)) !== false) {
- $line = trim($line);
- array_push($ips, $line);
- }
- fclose($f);
- }
- foreach ($ipAddresses as $ipAddress) {
- $included = false;
- foreach ($ips as $ip) {
- if ($ipAddress == $ip)
- $included = true;
- }
- if (!$included)
- array_push($ips, $ipAddress);
- }
- foreach ($ips as $ip) {
- $file .= trim($ip) . PHP_EOL;
- }
- $f = fopen($fileName, "w+");
- fwrite($f, $file);
- fclose($f);
- return true;
- }
- /*
- - Remove from IP Blacklist
- - Removes multiple (or just one) ip address(es) from the blacklist.
- $fileName - The path and name of the blacklist text file (include the extension .txt)
- $ipAddresses - An array containing the IP addresses (or just address) to be removed from
- the blacklist.
- */
- public function removeFromBlacklist($fileName, $ipAddresses) {
- $file = '';
- $ips = array();
- if (file_exists($fileName)) {
- $f = fopen($fileName, "r");
- while (($line = fgets($f)) !== false) {
- $line = trim($line);
- $continue = true;
- foreach ($ipAddresses as $ipAddress) {
- if ($ipAddress == $line)
- $continue = false;
- }
- if ($continue)
- array_push($ips, $line);
- }
- } else
- return false;
- foreach ($ips as $ip) {
- $file .= trim($ip) . PHP_EOL;
- }
- $f = fopen($fileName, "w+");
- fwrite($f, $file);
- fclose($f);
- return true;
- }
- /*
- - IP White Lister
- - Allows you to whitelist certain IP addresses so only those IP addresses can access
- the site.
- - Returns true if user is on the whitelist.
- $fileName - Path to the text file relative to the location of this script
- containing the IP addresses to be whitelisted separated by a new line.
- $redirectPath - Path to another page to redirect to if the user's IP is not whitelisted.
- Optional, if left blank nothing will happen except return true if this
- user's IP matches a whitelisted IP address.
- */
- public function whitelist($fileName, $redirectPath = '') {
- $success = true;
- $file = fopen($fileName, "r") or $success = false;
- $whitelisted = false;
- if ($success) {
- // get my IP
- $ip = $_SERVER['REMOTE_ADDR'];
- // sort ip addresses
- while (($line = fgets($file)) !== false) {
- // remove whitespaces
- $line = trim($line);
- // check if this IP = whitelisted IP
- if ($ip == $line)
- $whitelisted = true;
- }
- fclose($file);
- if (!$whitelisted) {
- if ($redirectPath == '')
- return false;
- else {
- header('location: ' . $redirectPath);
- die();
- }
- }
- }
- return true;
- }
- /*
- - Add to IP Whitelist
- - Adds multiple (or just one) ip address(es) to the whitelist.
- $fileName - The path and name of the whitelist text file (include the extension .txt)
- $ipAddresses - An array containing the IP addresses (or just address) to be whitelisted.
- */
- public function addToWhitelist($fileName, $ipAddresses) {
- $file = '';
- $ips = array();
- if (file_exists($fileName)) {
- $f = fopen($fileName, "r");
- while (($line = fgets($f)) !== false) {
- $line = trim($line);
- array_push($ips, $line);
- }
- fclose($f);
- }
- foreach ($ipAddresses as $ipAddress) {
- $included = false;
- foreach ($ips as $ip) {
- if ($ipAddress == $ip)
- $included = true;
- }
- if (!$included)
- array_push($ips, $ipAddress);
- }
- foreach ($ips as $ip) {
- $file .= trim($ip) . PHP_EOL;
- }
- $f = fopen($fileName, "w+");
- fwrite($f, $file);
- fclose($f);
- return true;
- }
- /*
- - Remove from IP Whitelist
- - Removes multiple (or just one) ip address(es) from the whitelist.
- $fileName - The path and name of the whitelist text file (include the extension .txt)
- $ipAddresses - An array containing the IP addresses (or just address) to be removed from
- the whitelist.
- */
- public function removeFromWhitelist($fileName, $ipAddresses) {
- $file = '';
- $ips = array();
- if (file_exists($fileName)) {
- $f = fopen($fileName, "r");
- while (($line = fgets($f)) !== false) {
- $line = trim($line);
- $continue = true;
- foreach ($ipAddresses as $ipAddress) {
- if ($ipAddress == $line)
- $continue = false;
- }
- if ($continue)
- array_push($ips, $line);
- }
- } else
- return false;
- foreach ($ips as $ip) {
- $file .= trim($ip) . PHP_EOL;
- }
- $f = fopen($fileName, "w+");
- fwrite($f, $file);
- fclose($f);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment