SERVER_NAME header output does not appear to be valid: ".$_SERVER["SERVER_NAME"].""); } // Get values from setupVars.conf $setupVars = parse_ini_file("/etc/pihole/setupVars.conf"); $svFQDN = (!empty($setupVars["FQDN"]) && validate_server_name($setupVars["FQDN"]) === TRUE) ? $setupVars["FQDN"] : ""; $svPasswd = !empty($setupVars["WEBPASSWORD"]); $svEmail = (!empty($setupVars["ADMIN_EMAIL"]) && filter_var($setupVars["ADMIN_EMAIL"], FILTER_VALIDATE_EMAIL)) ? $setupVars["ADMIN_EMAIL"] : ""; unset($setupVars); // Set landing page name, found within /var/www/html/pihole/ $landPage = "../landing.php"; // Set empty array for hostnames to be accepted as self address for splash page $authorizedHosts = []; // Append FQDN to $authorizedHosts if (!empty($svFQDN)) array_push($authorizedHosts, $svFQDN); // Append virtual hostname to $authorizedHosts if (!empty($_SERVER["VIRTUAL_HOST"])) { if (validate_server_name($_SERVER["VIRTUAL_HOST"]) === TRUE) { array_push($authorizedHosts, $_SERVER["VIRTUAL_HOST"]); } else { die("[ERROR]: VIRTUAL_HOST header output does not appear to be valid: ".$_SERVER["VIRTUAL_HOST"].""); } } // Set which extension types get rendered as "Website Blocked" (Including "" for index file extensions) $validExtTypes = array("asp", "htm", "html", "php", "rss", "xml", ""); // Get extension of current URL $currentUrlExt = pathinfo($_SERVER["REQUEST_URI"], PATHINFO_EXTENSION); // Set mobile friendly viewport $viewPort = ''; // Set response header function setHeader($type = "x") { header("X-Pi-hole: A black hole for Internet advertisements."); if (isset($type) && $type === "js") header("Content-Type: application/javascript"); } // Determine block page redirect if ($serverName === "pi.hole") { exit(header("Location: /admin")); } elseif (filter_var($serverName, FILTER_VALIDATE_IP) || in_array($serverName, $authorizedHosts)) { // Show splash page or landing page when directly browsing via IP or auth'd hostname $splashPage = " $viewPort
Pi-hole: Your black hole for Internet advertisements "; $pageType = is_file(getcwd()."/$landPage") ? include $landPage : "$splashPage"; unset($serverName, $svFQDN, $svPasswd, $svEmail, $authorizedHosts, $validExtTypes, $currentUrlExt, $viewPort); exit($pageType); } elseif ($currentUrlExt === "js") { // Set Javascript redirect for blocked sources exit(setHeader("js").'var x = "Pi-hole: A black hole for Internet advertisements."'); } elseif (strpos($_SERVER["REQUEST_URI"], "?") !== FALSE && isset($_SERVER["HTTP_REFERER"])) { // Set blank image upon receiving REQUEST_URI w/ query string & HTTP_REFERRER (Presumably from iframe) exit(setHeader().' '); } elseif (!in_array($currentUrlExt, $validExtTypes) || substr_count($_SERVER["REQUEST_URI"], "?")) { // Set svg image upon receiving non $validExtTypes URL extension or query string (Presumably not from an iframe) $blockImg = 'Blocked by Pi-hole'; exit(setHeader()." $viewPort $blockImg "); } /* Start processing block page from here */ // Get Pi-hole core branch name $phBranch = exec("cd /etc/.pihole/ && git rev-parse --abbrev-ref HEAD"); if ($phBranch !== "master") { error_reporting(E_ALL); ini_set("display_errors", 1); ini_set("display_startup_errors", 1); } // Validate SERVER_IP output if (filter_var($_SERVER['SERVER_ADDR'], FILTER_VALIDATE_IP)) { $serverAddr = $_SERVER["SERVER_ADDR"]; } else { die("[ERROR]: SERVER_IP header output does not appear to be valid: ".$_SERVER["SERVER_ADDR"].""); } // Determine placeholder text based off $svPasswd presence $wlPlaceHolder = empty($svPasswd) ? "No admin password set" : "Javascript disabled"; // Get admin email address $bpAskAdmin = !empty($svEmail) ? '' : ""; // Determine if at least one block list has been generated if (empty(glob("/etc/pihole/list.0.*.domains"))) die("[ERROR]: There are no domain lists generated lists within /etc/pihole/! Please update gravity by running pihole -g, or repair Pi-hole using pihole -r."); // Get contents of adlist.list $adLists = is_file("/etc/pihole/adlists.list") ? "/etc/pihole/adlists.list" : "/etc/pihole/adlists.default"; if (!is_file($adLists)) die("[ERROR]: Unable to find file: $adLists"); // Get all URLs starting with "http" or "www" from $adLists and re-index array numerically $adlistsUrls = array_values(preg_grep("/(^http)|(^www)/i", file($adLists, FILE_IGNORE_NEW_LINES))); if (empty($adlistsUrls)) die("[ERROR]: There are no adlist URL's found within $adLists"); $adlistsCount = count($adlistsUrls) + 3; // +1 because array starts at 0, +2 for Blacklist & Wildcard lists // Get results of queryads.php exact search ini_set("default_socket_timeout", 3); function queryAds($serverName) { $preQueryTime = microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"]; $queryAds = file("http://127.0.0.1/admin/scripts/pi-hole/php/queryads.php?domain=$serverName&exact", FILE_IGNORE_NEW_LINES); $queryTime = sprintf("%.0f", (microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"]) - $preQueryTime); try { if ($queryTime >= ini_get("default_socket_timeout")) { throw new Exception ("Connection timeout (".ini_get("default_socket_timeout")."s)"); } elseif ($queryAds[0][0] === ":") { if (strpos($queryAds[0], "Invalid") !== FALSE) throw new Exception ("Invalid Domain ($serverName)"); if (strpos($queryAds[0], "No exact") !== FALSE) return array("0" => "none"); throw new Exception ("Unhandled error message ($queryAds[0])"); } elseif ($queryAds[0][0] !== "/") { throw new Exception ("Unexpected output ($queryAds[0])"); } return $queryAds; } catch (Exception $e) { return array("0" => "error", "1" => $e->getMessage()); } } $queryAds = queryAds($serverName); if ($queryAds[0] === "error") { die("[ERROR]: Unable to parse results from queryads.php: ".$queryAds[1].""); } // Filter, sort, and count $queryAds array if ($queryAds[0] !== "none") { $queryAds = preg_replace("/(\/etc\/pihole\/)|(\/etc\/dnsmasq\.d\/)/", "", $queryAds); $queryAds = preg_replace("/(^list\.)|(\..*domains)/", "", $queryAds); $featuredTotal = count($queryAds); } // Determine if domain has been blacklisted or wildcarded if ($queryAds[0] === "blacklist.txt") { $intBlacklist = array("π" => $queryAds[0]); $queryAds[0] = "π"; // Manually blacklisted sites do not have a number $notableFlagClass = "blacklist"; } elseif ($queryAds[0] === "whitelist.txt") { $intBlacklist = array("π" => $queryAds[0]); $queryAds[0] = "π"; $notableFlagClass = "noblock"; $wlInfo = "recentwl"; } elseif ($queryAds[0] === "03-pihole-wildcard.conf") { $intBlacklist = array("π" => $queryAds[0]); $queryAds[0] = "π"; $notableFlagClass = "wildcard"; } elseif ($queryAds[0] === "none") { $featuredTotal = "0"; $notableFlagClass = "noblock"; // Determine appropriate info message if CNAME exists $dnsRecord = dns_get_record("$serverName")[0]; if (array_key_exists("target", $dnsRecord)) { $wlInfo = $dnsRecord['target']; } else { $wlInfo = "unknown"; } } // Merge $intBlacklist with $adlistsUrls if domain has been blacklisted or wildcarded if (isset($intBlacklist)) $adlistsUrls = array_merge($intBlacklist, $adlistsUrls); // Set #bpOutput notification $wlOutputClass = (isset($wlInfo) && $wlInfo === "recentwl") ? $wlInfo : "hidden"; $wlOutput = (isset($wlInfo) && $wlInfo !== "recentwl") ? "$wlInfo" : ""; // Get Pi-hole core version if ($phBranch !== "master") { $phVersion = exec("cd /etc/.pihole/ && git describe --long --dirty --tags"); $execTime = microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"]; } else { $phVersion = exec("cd /etc/.pihole/ && git describe --tags --abbrev=0"); } ?> ● <?=$serverName ?>

Open Source Ad Blocker Designed for Raspberry Pi

0) echo ''; ?>
 0) foreach ($queryAds as $num) { echo "[$num]:$adlistsUrls[$num]\n"; } ?>
. Pi-hole ()