Advertisement
Guest User

Untitled

a guest
May 31st, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.46 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. types:
  5. adblock -> ||sub.domain.com^
  6. url -> http://sub.domain.com
  7. domains with paths will be skipped to avoid false positives
  8. */
  9.  
  10. class Parser
  11. {
  12.     private $lists;
  13.     private $domains;
  14.  
  15.     function __construct()
  16.     {
  17.         $this->lists = array();
  18.         $this->domains = array();
  19.     }
  20.  
  21.     public
  22.     function pushList($name, $param, $url, $type, $allowpath = false)
  23.     {
  24.         array_push($this->lists, array(
  25.             "name" => $name,
  26.             "param" => $param,
  27.             "url" => $url,
  28.             "type" => $type,
  29.             "allowpath" => $allowpath
  30.         ));
  31.     }
  32.  
  33.     public
  34.     function run($param, $allowpath = false)
  35.     {
  36.         $num = 0;
  37.         foreach($this->lists as & $list)
  38.         {
  39.             if (($param == $list["param"]) || ($param == "all"))
  40.             {
  41.                 switch ($list["type"])
  42.                 {
  43.                 case "adblock":
  44.                     array_push($this->domains, "#" . $list["name"]);
  45.                     $this->parseAdblock($list["url"], "||", "^", $allowpath);
  46.                     $num++;
  47.                     break;
  48.  
  49.                 case "url":
  50.                     array_push($this->domains, "#" . $list["name"]);
  51.                     $this->parseList($list["url"], $allowpath);
  52.                     $num++;
  53.                     break;
  54.  
  55.                 default:
  56.                     break;
  57.                 }
  58.  
  59.                 if ($param != "all") break;
  60.             }
  61.         }
  62.  
  63.         return $num;
  64.     }
  65.  
  66.     public
  67.     function countList()
  68.     {
  69.         return count($this->domains);
  70.     }
  71.  
  72.     public
  73.     function printList($before, $after)
  74.     {
  75.         $this->domains = array_unique($this->domains, SORT_STRING);
  76.         foreach($this->domains as & $domain)
  77.         {
  78.             echo $before . $domain . $after;;
  79.         }
  80.     }
  81.  
  82.     private
  83.     function parseList($url, $allowpath)
  84.     {
  85.         $data = file_get_contents($url);
  86.         $lines = explode("\n", $data);
  87.         foreach($lines as & $line)
  88.         {
  89.             $line = $this->_trim($line);
  90.             if (substr($line, 0, 1) == "#") continue; //skip comment
  91.             if ($this->_containsIP($line)) continue;
  92.             if ($domain = $this->_extractDomain($line))
  93.             {
  94.                 if (!$this->_hasPath($domain) || $allowpath)
  95.                 {
  96.                     array_push($this->domains, $this->_trimPath($domain));
  97.                 }
  98.             }
  99.         }
  100.     }
  101.  
  102.     private
  103.     function parseAdblock($url, $start, $end, $allowpath)
  104.     {
  105.         $data = file_get_contents($url);
  106.         $lines = explode("\n", $data);
  107.         foreach($lines as & $line)
  108.         {
  109.             $line = $this->_trim($line);
  110.             if (substr($line, 0, 1) == "#") continue; //skip comment
  111.             if (substr($line, 0, 1) == "!") continue; //skip comment
  112.             if ($this->_containsIP($line)) continue;
  113.             if (strpos($line, "*") !== false) continue; //skip wildcard
  114.             if (substr_count($line, $end) > 1) continue; //fix for ||twitter.com^*/scribe^ and similar
  115.  
  116.             if ((substr($line, 0, strlen($start)) == $start) && (substr($line, (strlen($end) * -1)) == $end))
  117.             {
  118.                 if ($domain = $this->_extractDomain($line))
  119.                 {
  120.                     if (!$this->_hasPath($domain) || $allowpath)
  121.                     {
  122.                         array_push($this->domains, $this->_trimPath($domain));
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.     }
  128.  
  129.     private
  130.     function _containsIP($str)
  131.     {
  132.         return preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $str, $ip);
  133.     }
  134.  
  135.     private
  136.     function _trim($str)
  137.     {
  138.         return rtrim(trim(strtolower($str)) , "/"); //lowercase, remove all whitespaces, trim slash at the end
  139.     }
  140.  
  141.     private
  142.     function _extractDomain($str)
  143.     {
  144.         if (preg_match("#([a-z0-9_~-]+\.)+[a-z]{2,}(\/\S*)?#i", $str, $domain)) //extract domain + path
  145.         {
  146.             return $domain[0];
  147.         }
  148.         else return false;
  149.     }
  150.  
  151.     private
  152.     function _hasPath($str)
  153.     {
  154.         if (strpos($str, "/") == 0)
  155.         {
  156.             return false;
  157.         }
  158.         else return true;
  159.     }
  160.  
  161.     private
  162.     function _trimPath($str)
  163.     {
  164.         $pos = strpos($str, "/");
  165.         if($pos)
  166.         {
  167.             return substr($str, 0, $pos);
  168.         } else return $str;
  169.     }
  170. }
  171.  
  172.  
  173. if (isset($_GET["list"])) //all param may will time out
  174. {
  175.     $parser = new Parser();
  176.  
  177.     /* name, param, url, type allowpath (optional) */
  178.     $parser->pushList("Anti-PopAds", "antipopads", "https://raw.githubusercontent.com/Yhonay/antipopads/master/popads.txt", "adblock");
  179.     $parser->pushList("Adware Filters", "adware_filters", "https://easylist-downloads.adblockplus.org/adwarefilters.txt", "adblock");
  180.     $parser->pushList("EasyPrivacy + EasyList", "easyprivacy_easylist", "https://easylist-downloads.adblockplus.org/adwarefilters.txt", "adblock");
  181.     $parser->pushList("Fanboys Ultimate List", "fanboy_ultimate", "https://fanboy.co.nz/r/fanboy-ultimate.txt", "adblock");
  182.     $parser->pushList("Blockzilla", "blockzilla", "https://raw.githubusercontent.com/zpacman/Blockzilla/master/Blockzilla.txt", "adblock");
  183.     $parser->pushList("Openpish", "openpish", "https://openphish.com/feed.txt", "url");
  184.     $parser->pushList("Malware URLs", "malwareurls", "http://malwareurls.joxeankoret.com/normal.txt", "url");
  185.     $parser->pushList("EasyList Germany", "easylist_de", "https://easylist-downloads.adblockplus.org/easylistgermany.txt", "adblock");
  186.     $parser->pushList("Anti-Adblock Killer", "aak", "https://raw.githubusercontent.com/reek/anti-adblock-killer/master/anti-adblock-killer-filters.txt", "adblock");
  187.     $parser->pushList("Adguard Russian Filter", "adguard_ru", "https://adguard.com/en/filter-rules.html?id=1", "adblock");
  188.     $parser->pushList("Adguard English", "adguard_en", "https://adguard.com/en/filter-rules.html?id=2", "adblock");
  189.     $parser->pushList("Adguard Spyware Filter", "adguard_spy", "https://adguard.com/en/filter-rules.html?id=3", "adblock");
  190.     $parser->pushList("Adguard German", "adguard_de", "https://adguard.com/en/filter-rules.html?id=6", "adblock");
  191.     $parser->pushList("Adguard Japanese", "adguard_jap", "https://adguard.com/en/filter-rules.html?id=7", "adblock");
  192.     $parser->pushList("Adguard Dutch", "adguard_nl", "https://adguard.com/en/filter-rules.html?id=8", "adblock");
  193.     $parser->pushList("Adguard Spanish", "adguard_es", "https://adguard.com/en/filter-rules.html?id=9", "adblock");
  194.     $parser->pushList("Adguard Mobile Ads Filter", "adguard_mobile", "https://adguard.com/en/filter-rules.html?id=11", "adblock");
  195.     $parser->pushList("Adguard Safari Filter", "adguard_safari", "https://adguard.com/en/filter-rules.html?id=12", "adblock");
  196.     $parser->pushList("Adguard Annoyances Filter", "adguard_annoy", "https://adguard.com/en/filter-rules.html?id=14", "adblock");
  197.     $parser->pushList("Adguard DNS Filter", "adguard_dns", "https://adguard.com/en/filter-rules.html?id=15", "adblock");
  198.     $parser->pushList("Adguard French", "adguard_fr", "https://adguard.com/en/filter-rules.html?id=16", "adblock");
  199.  
  200.     $allowpath = false;
  201.  
  202.     //will lead to tons of wrongfully blocked domains
  203.     if (isset($_GET["allowpath"])) $allowpath = true;
  204.  
  205.     if($parser->run($_GET["list"], $allowpath) > 0)
  206.     {
  207.         $parser->printList("", "\n");
  208.     } else die("#error, requested list not found");
  209. }
  210. else
  211. {
  212.     die("#error, no arguments given");
  213. }
  214.  
  215. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement