Advertisement
reenadak

manage filters

Feb 20th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.99 KB | None | 0 0
  1.    
  2.     private $filters = array();
  3.    
  4.     /*
  5.     - Add Filter
  6.  
  7.     - 'Unpacks' a text file containing words that should be removed or
  8.       blocked in strings it is used against. New line = new word.
  9.    
  10.     $filterName     - the name of the filter, used when there are multiple
  11.                       filters used at different points in the website.
  12.    
  13.     $filterPath     - where the text file containing the words is located
  14.                       path relative to where this file is.
  15.     */
  16.    
  17.     public function addFilter($filterName, $filterPath)
  18.     {      
  19.         $filter = array(
  20.             $filterName
  21.         );
  22.        
  23.         // open file
  24.         $success = true;
  25.         $file = fopen($filterPath, "r") or $success = false;
  26.        
  27.         if ($success) {
  28.        
  29.             // sort filtered words
  30.             while (($line = fgets($file)) !== false) {
  31.                 // remove whitespaces
  32.                 $line = trim($line);
  33.                
  34.                 // add to filter
  35.                 array_push($filter, $line);
  36.             }
  37.            
  38.             // append filter
  39.             fclose($file);
  40.             array_push($this->filters, $filter);
  41.            
  42.             return true;
  43.         }
  44.        
  45.         return false;
  46.     }
  47.    
  48.     /*
  49.     - Add to Filter
  50.     - Adds word(s) to the filter.
  51.    
  52.     $fileName     - The name (and path) of the filter text file to add words to.
  53.     $words        - An array containing at least one word to add to the filter.
  54.     */
  55.    
  56.     public function addToFilter($fileName, $words) {
  57.         $file = '';
  58.         $newWords = array();
  59.        
  60.         if (file_exists($fileName)) {
  61.             $f = fopen($fileName, "r");
  62.            
  63.             while (($line = fgets($f)) !== false) {
  64.                 $line = trim($line);
  65.                 array_push($newWords, $line);
  66.             }
  67.            
  68.             fclose($f);
  69.         }
  70.            
  71.         foreach ($words as $word) {
  72.             $included = false;
  73.  
  74.             foreach ($newWords as $newWord) {
  75.                 if ($word == $newWord)
  76.                     $included = true;
  77.             }
  78.  
  79.             if (!$included)
  80.                 array_push($newWords, $word);
  81.         }
  82.        
  83.         foreach ($newWords as $newWord) {
  84.             $file .= trim($newWord) . PHP_EOL;
  85.         }
  86.        
  87.         $f = fopen($fileName, "w+");
  88.         fwrite($f, $file);
  89.         fclose($f);
  90.        
  91.         return true;
  92.     }
  93.    
  94.     /*
  95.     - Remove from Filter
  96.     - Removes word(s) from the filter.
  97.    
  98.     $fileName     - The name (and path) of the filter text file to remove words from.
  99.     $words        - An array containing at least one word to remove from the filter.
  100.     */
  101.    
  102.     public function removeFromFilter($fileName, $words) {
  103.         $file = '';
  104.         $newWords = array();
  105.        
  106.         if (file_exists($fileName)) {
  107.             $f = fopen($fileName, "r");
  108.            
  109.             while (($line = fgets($f)) !== false) {
  110.                 $line = trim($line);
  111.                 $continue = true;
  112.                
  113.                 foreach ($words as $word) {
  114.                     if ($word == $line)
  115.                         $continue = false;
  116.                 }
  117.                
  118.                 if ($continue)
  119.                     array_push($newWords, $line);
  120.             }
  121.         } else
  122.             return false;
  123.        
  124.         foreach ($newWords as $newWord) {
  125.             $file .= trim($newWord) . PHP_EOL;
  126.         }
  127.        
  128.         $f = fopen($fileName, "w+");
  129.         fwrite($f, $file);
  130.         fclose($f);
  131.        
  132.         return true;
  133.     }
  134.    
  135.     /*
  136.     - Filter String
  137.     - Checks the string against words that are included in a certain filter.
  138.     - NOTE: If you enter an invalid filter name then this function will return
  139.             false anyway and may cause some confusion.
  140.    
  141.     - If it returns false then the string was filtered and there was no replace string.
  142.    
  143.     $filterName     - the name of the filter to check the string against.
  144.     $string         - the string to be checked for banned/filtered words.
  145.     $strict         - true means the word just needs to contain a filtered
  146.                       word to be blocked, false means the word must match
  147.                       the filtered word to be blocked.
  148.     $replace[='']   - if set, replaces the word with this word, otherwise
  149.                       simply returns "" AKA false.
  150.     */
  151.    
  152.     public function filterString($filterName, $string, $strict = true, $replace = '') {
  153.        
  154.         // get the correct filter
  155.         $filter = null;
  156.        
  157.         foreach ($this->filters as $thisFilter) {
  158.             if ($thisFilter[0] == $filterName)
  159.                 $filter = $thisFilter;
  160.         }
  161.        
  162.         if ($filter == null)
  163.             return "";
  164.         else {
  165.            
  166.             $newString = "";
  167.            
  168.             // iterate through each word in the string
  169.             foreach (explode(" ", $string) as $char) {
  170.                
  171.                 $match = false;
  172.                
  173.                 $index = 0;
  174.                
  175.                 // iterate through each word in the filter bank
  176.                 foreach ($filter as $word) {
  177.                     if ($index > 0) {
  178.                         if ($strict) {
  179.                             // if the word contains this filtered word
  180.                             if (stripos($char, $word) !== false)
  181.                                 $match = true;
  182.                         } else {
  183.                             // if the word IS this filtered word
  184.                             if (strtolower($char) == strtolower($word))
  185.                                 $match = true;
  186.                         }
  187.                     }
  188.                    
  189.                     $index++;
  190.                 }
  191.                
  192.                 if ($match) {
  193.                     if ($replace == "")
  194.                         return false;
  195.                     else
  196.                         $newString .= $replace . " ";
  197.                 } else
  198.                     $newString .= $char . " ";
  199.             }
  200.            
  201.             return $newString;
  202.         }
  203.     }
  204.    
  205.  
  206.        
  207.     /*
  208.     - IP Banner
  209.     - Allows you to blacklist certain IP addresses
  210.     - You can get someone's IP with the PHP function $_SERVER['REMOTE_ADDR'];
  211.    
  212.     - Get IPs to ban via text file
  213.     - Returns true if the IP is banned
  214.    
  215.     $fileName     - Path to the text file relative to location of this script
  216.                     containing the IP addresses to be blacklisted separated by a new line.
  217.     $redirectPath - Path to another page to redirect to if the user's IP is blacklisted.
  218.                     Optional, if left blank nothing will happen except return true if this
  219.                     user's IP matches a banned IP address.
  220.     */
  221.    
  222.     public function blacklist($fileName, $redirectPath = '') {
  223.         $success = true;
  224.        
  225.         if (file_exists($fileName)) {
  226.             $file = fopen($fileName, "r") or $success = false;
  227.  
  228.             $blacklisted = false;
  229.  
  230.             if ($success) {
  231.                 // get my IP
  232.                 $ip = $_SERVER['REMOTE_ADDR'];
  233.  
  234.                 // sort ip addresses
  235.                 while (($line = fgets($file)) !== false) {
  236.                     // remove whitespaces
  237.                     $line = trim($line);
  238.  
  239.                     // check if this IP = blacklisted IP
  240.                     if ($ip == $line)
  241.                         $blacklisted = true;
  242.                 }
  243.  
  244.                 fclose($file);
  245.  
  246.                 if ($blacklisted) {
  247.                     if ($redirectPath == '')
  248.                         return true;
  249.                     else {
  250.                         header('location: ' . $redirectPath);
  251.                         die();
  252.                     }
  253.                 }
  254.  
  255.             }
  256.         }
  257.        
  258.         return false;
  259.     }
  260.    
  261.     /*
  262.     - Add to IP Blacklist
  263.     - Adds multiple (or just one) ip address(es) to the blacklist.
  264.    
  265.     $fileName     - The path and name of the blacklist text file (include the extension .txt)
  266.     $ipAddresses  - An array containing the IP addresses (or just address) to be blacklisted.
  267.     */
  268.    
  269.     public function addToBlacklist($fileName, $ipAddresses) {
  270.         $file = '';
  271.         $ips = array();
  272.        
  273.         if (file_exists($fileName)) {
  274.             $f = fopen($fileName, "r");
  275.            
  276.             while (($line = fgets($f)) !== false) {
  277.                 $line = trim($line);
  278.                 array_push($ips, $line);
  279.             }
  280.            
  281.             fclose($f);
  282.         }
  283.            
  284.         foreach ($ipAddresses as $ipAddress) {
  285.             $included = false;
  286.  
  287.             foreach ($ips as $ip) {
  288.                 if ($ipAddress == $ip)
  289.                     $included = true;
  290.             }
  291.  
  292.             if (!$included)
  293.                 array_push($ips, $ipAddress);
  294.         }
  295.        
  296.         foreach ($ips as $ip) {
  297.             $file .= trim($ip) . PHP_EOL;
  298.         }
  299.        
  300.         $f = fopen($fileName, "w+");
  301.         fwrite($f, $file);
  302.         fclose($f);
  303.        
  304.         return true;
  305.     }
  306.    
  307.     /*
  308.     - Remove from IP Blacklist
  309.     - Removes multiple (or just one) ip address(es) from the blacklist.
  310.    
  311.     $fileName     - The path and name of the blacklist text file (include the extension .txt)
  312.     $ipAddresses  - An array containing the IP addresses (or just address) to be removed from
  313.                     the blacklist.
  314.     */
  315.    
  316.     public function removeFromBlacklist($fileName, $ipAddresses) {
  317.         $file = '';
  318.         $ips = array();
  319.        
  320.         if (file_exists($fileName)) {
  321.             $f = fopen($fileName, "r");
  322.            
  323.             while (($line = fgets($f)) !== false) {
  324.                 $line = trim($line);
  325.                 $continue = true;
  326.                
  327.                 foreach ($ipAddresses as $ipAddress) {
  328.                     if ($ipAddress == $line)
  329.                         $continue = false;
  330.                 }
  331.                
  332.                 if ($continue)
  333.                     array_push($ips, $line);
  334.             }
  335.         } else
  336.             return false;
  337.        
  338.         foreach ($ips as $ip) {
  339.             $file .= trim($ip) . PHP_EOL;
  340.         }
  341.        
  342.         $f = fopen($fileName, "w+");
  343.         fwrite($f, $file);
  344.         fclose($f);
  345.        
  346.         return true;
  347.     }
  348.    
  349.     /*
  350.     - IP White Lister
  351.     - Allows you to whitelist certain IP addresses so only those IP addresses can access
  352.       the site.
  353.     - Returns true if user is on the whitelist.
  354.      
  355.     $fileName     - Path to the text file relative to the location of this script
  356.                     containing the IP addresses to be whitelisted separated by a new line.
  357.     $redirectPath - Path to another page to redirect to if the user's IP is not whitelisted.
  358.                     Optional, if left blank nothing will happen except return true if this
  359.                     user's IP matches a whitelisted IP address.
  360.     */
  361.    
  362.     public function whitelist($fileName, $redirectPath = '') {
  363.         $success = true;
  364.         $file = fopen($fileName, "r") or $success = false;
  365.        
  366.         $whitelisted = false;
  367.        
  368.         if ($success) {
  369.             // get my IP
  370.             $ip = $_SERVER['REMOTE_ADDR'];
  371.            
  372.             // sort ip addresses
  373.             while (($line = fgets($file)) !== false) {
  374.                 // remove whitespaces
  375.                 $line = trim($line);
  376.                
  377.                 // check if this IP = whitelisted IP
  378.                 if ($ip == $line)
  379.                     $whitelisted = true;
  380.             }
  381.            
  382.             fclose($file);
  383.            
  384.             if (!$whitelisted) {
  385.                 if ($redirectPath == '')
  386.                     return false;
  387.                 else {
  388.                     header('location: ' . $redirectPath);
  389.                     die();
  390.                 }
  391.             }
  392.         }
  393.        
  394.         return true;
  395.     }
  396.    
  397.     /*
  398.     - Add to IP Whitelist
  399.     - Adds multiple (or just one) ip address(es) to the whitelist.
  400.    
  401.     $fileName     - The path and name of the whitelist text file (include the extension .txt)
  402.     $ipAddresses  - An array containing the IP addresses (or just address) to be whitelisted.
  403.     */
  404.    
  405.     public function addToWhitelist($fileName, $ipAddresses) {
  406.         $file = '';
  407.         $ips = array();
  408.        
  409.         if (file_exists($fileName)) {
  410.             $f = fopen($fileName, "r");
  411.            
  412.             while (($line = fgets($f)) !== false) {
  413.                 $line = trim($line);
  414.                 array_push($ips, $line);
  415.             }
  416.            
  417.             fclose($f);
  418.         }
  419.            
  420.         foreach ($ipAddresses as $ipAddress) {
  421.             $included = false;
  422.  
  423.             foreach ($ips as $ip) {
  424.                 if ($ipAddress == $ip)
  425.                     $included = true;
  426.             }
  427.  
  428.             if (!$included)
  429.                 array_push($ips, $ipAddress);
  430.         }
  431.        
  432.         foreach ($ips as $ip) {
  433.             $file .= trim($ip) . PHP_EOL;
  434.         }
  435.        
  436.         $f = fopen($fileName, "w+");
  437.         fwrite($f, $file);
  438.         fclose($f);
  439.        
  440.         return true;
  441.     }
  442.    
  443.     /*
  444.     - Remove from IP Whitelist
  445.     - Removes multiple (or just one) ip address(es) from the whitelist.
  446.    
  447.     $fileName     - The path and name of the whitelist text file (include the extension .txt)
  448.     $ipAddresses  - An array containing the IP addresses (or just address) to be removed from
  449.                     the whitelist.
  450.     */
  451.    
  452.     public function removeFromWhitelist($fileName, $ipAddresses) {
  453.         $file = '';
  454.         $ips = array();
  455.        
  456.         if (file_exists($fileName)) {
  457.             $f = fopen($fileName, "r");
  458.            
  459.             while (($line = fgets($f)) !== false) {
  460.                 $line = trim($line);
  461.                 $continue = true;
  462.                
  463.                 foreach ($ipAddresses as $ipAddress) {
  464.                     if ($ipAddress == $line)
  465.                         $continue = false;
  466.                 }
  467.                
  468.                 if ($continue)
  469.                     array_push($ips, $line);
  470.             }
  471.         } else
  472.             return false;
  473.        
  474.         foreach ($ips as $ip) {
  475.             $file .= trim($ip) . PHP_EOL;
  476.         }
  477.        
  478.         $f = fopen($fileName, "w+");
  479.         fwrite($f, $file);
  480.         fclose($f);
  481.        
  482.         return true;
  483.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement