2016-12-04 21:17:08 +00:00
< ? php
2017-04-03 15:29:57 +00:00
/* Detailed Pi-hole Block Page: Show "Website Blocked" if user browses to site, but not to image/file requests based on the work of WaLLy3K for DietPi & Pi-Hole */
2016-12-04 21:17:08 +00:00
2017-03-28 22:02:00 +00:00
function validIP ( $address ){
if ( preg_match ( '/[.:0]/' , $address ) && ! preg_match ( '/[1-9a-f]/' , $address )) {
// Test if address contains either `:` or `0` but not 1-9 or a-f
return false ;
}
return ! filter_var ( $address , FILTER_VALIDATE_IP ) === false ;
}
2016-12-04 21:17:08 +00:00
$uri = escapeshellcmd ( $_SERVER [ 'REQUEST_URI' ]);
$serverName = escapeshellcmd ( $_SERVER [ 'SERVER_NAME' ]);
2017-03-05 05:58:21 +00:00
// If the server name is 'pi.hole', it's likely a user trying to get to the admin panel.
// Let's be nice and redirect them.
if ( $serverName === 'pi.hole' )
{
2017-03-09 06:38:56 +00:00
header ( 'HTTP/1.1 301 Moved Permanently' );
2017-03-05 05:58:21 +00:00
header ( " Location: /admin/ " );
}
2016-12-04 21:17:08 +00:00
// Retrieve server URI extension (EG: jpg, exe, php)
2017-03-28 22:02:00 +00:00
ini_set ( 'pcre.recursion_limit' , 100 );
2016-12-04 21:17:08 +00:00
$uriExt = pathinfo ( $uri , PATHINFO_EXTENSION );
// Define which URL extensions get rendered as "Website Blocked"
$webExt = array ( 'asp' , 'htm' , 'html' , 'php' , 'rss' , 'xml' );
2017-04-20 11:52:54 +00:00
// Get IPv4 and IPv6 addresses from setupVars.conf (if available)
$setupVars = parse_ini_file ( " /etc/pihole/setupVars.conf " );
$ipv4 = isset ( $setupVars [ " IPV4_ADDRESS " ]) ? explode ( " / " , $setupVars [ " IPV4_ADDRESS " ])[ 0 ] : $_SERVER [ 'SERVER_ADDR' ];
$ipv6 = isset ( $setupVars [ " IPV6_ADDRESS " ]) ? explode ( " / " , $setupVars [ " IPV6_ADDRESS " ])[ 0 ] : $_SERVER [ 'SERVER_ADDR' ];
2017-04-03 14:28:18 +00:00
$AUTHORIZED_HOSTNAMES = array (
$ipv4 ,
$ipv6 ,
2017-04-20 11:52:54 +00:00
str_replace ( array ( " [ " , " ] " ), array ( " " , " " ), $_SERVER [ " SERVER_ADDR " ]),
2017-04-03 14:28:18 +00:00
" pi.hole " ,
" localhost " );
// Allow user set virtual hostnames
$virtual_host = getenv ( 'VIRTUAL_HOST' );
2017-04-20 11:52:54 +00:00
if ( ! empty ( $virtual_host ))
2017-04-03 14:28:18 +00:00
array_push ( $AUTHORIZED_HOSTNAMES , $virtual_host );
2017-03-28 22:02:00 +00:00
// Immediately quit since we didn't block this page (the IP address or pi.hole is explicitly requested)
2017-04-03 14:28:18 +00:00
if ( validIP ( $serverName ) || in_array ( $serverName , $AUTHORIZED_HOSTNAMES ))
2017-03-28 22:02:00 +00:00
{
http_response_code ( 404 );
die ();
}
2016-12-04 21:17:08 +00:00
if ( in_array ( $uriExt , $webExt ) || empty ( $uriExt ))
{
// Requested resource has an extension listed in $webExt
// or no extension (index access to some folder incl. the root dir)
$showPage = true ;
}
else
{
// Something else
$showPage = false ;
}
// Handle incoming URI types
if ( ! $showPage )
{
?>
2015-06-07 04:28:41 +00:00
< html >
2016-08-20 00:45:17 +00:00
< head >
2016-12-04 21:17:08 +00:00
< script > window . close (); </ script ></ head >
< body >
2016-12-05 11:34:30 +00:00
< img src = " data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 " >
2016-12-04 21:17:08 +00:00
</ body >
</ html >
< ? php
die ();
}
2017-04-03 15:29:57 +00:00
// Get Pi-hole version
2016-12-04 21:17:08 +00:00
$piHoleVersion = exec ( 'cd /etc/.pihole/ && git describe --tags --abbrev=0' );
2016-12-05 11:12:01 +00:00
// Don't show the URI if it is the root directory
if ( $uri == " / " )
{
$uri = " " ;
}
2016-12-04 21:17:08 +00:00
?>
<! DOCTYPE html >
2017-03-25 15:11:37 +00:00
< html >
2016-12-04 21:17:08 +00:00
< head >
< meta charset = 'UTF-8' />
< title > Website Blocked </ title >
2016-12-30 19:42:15 +00:00
< link rel = 'stylesheet' href = 'http://pi.hole/pihole/blockingpage.css' />
2016-12-30 18:43:29 +00:00
< link rel = 'shortcut icon' href = 'http://pi.hole/admin/img/favicon.png' type = 'image/png' />
2016-12-04 21:17:08 +00:00
< meta name = 'viewport' content = 'width=device-width,initial-scale=1.0,maximum-scale=1.0, user-scalable=no' />
< meta name = 'robots' content = 'noindex,nofollow' />
2016-08-20 00:45:17 +00:00
</ head >
2016-12-06 13:05:02 +00:00
< body id = " body " >
2016-12-04 21:17:08 +00:00
< header >
< h1 >< a href = '/' > Website Blocked </ a ></ h1 >
</ header >
< main >
< div > Access to the following site has been blocked :< br />
< span class = 'pre msg' >< ? php echo $serverName . $uri ; ?> </span></div>
2016-12-15 09:38:24 +00:00
< div > If you have an ongoing use for this website , please ask the owner of the Pi - hole in your network to have it whitelisted .</ div >
2016-12-05 11:12:01 +00:00
< input id = " domain " type = " hidden " value = " <?php echo $serverName ; ?> " >
< input id = " quiet " type = " hidden " value = " yes " >
< button id = " btnSearch " class = " buttons blocked " type = " button " style = " visibility: hidden; " ></ button >
This page is blocked because it is explicitly contained within the following block list ( s ) :
2016-12-05 13:39:07 +00:00
< pre id = " output " style = " width: 100%; height: 100%; " hidden = " true " ></ pre >< br />
2016-12-30 12:03:45 +00:00
< div class = 'buttons blocked' >
< a class = 'safe33' href = 'javascript:history.back()' > Go back </ a >
< a class = 'safe33' id = " whitelisting " > Whitelist this page </ a >
< a class = 'safe33' href = 'javascript:window.close()' > Close window </ a >
</ div >
2017-01-09 14:02:31 +00:00
< div style = " width: 98%; text-align: center; padding: 10px; " hidden = " true " id = " whitelistingform " >
< p > Note that whitelisting domains which are blocked using the wildcard method won ' t work .</ p >
< p > Password required !</ p >< br />
2016-12-30 19:13:15 +00:00
< form >
2016-12-30 12:03:45 +00:00
< input name = " list " type = " hidden " value = " white " >< br />
Domain :< br />
< input name = " domain " value = " <?php echo $serverName ?> " disabled >< br />< br />
Password :< br />
< input type = " password " id = " pw " name = " pw " >< br />< br />
< button class = " buttons33 safe " id = " btnAdd " type = " button " > Whitelist </ button >
</ form >< br />
< pre id = " whitelistingoutput " style = " width: 100%; height: 100%; padding: 5px; " hidden = " true " ></ pre >< br />
</ div >
2016-12-04 21:17:08 +00:00
</ main >
< footer > Generated < ? php echo date ( 'D g:i A, M d' ); ?> by Pi-hole <?php echo $piHoleVersion; ?></footer>
2016-12-30 20:36:53 +00:00
< script src = " http://pi.hole/admin/scripts/vendor/jquery.min.js " ></ script >
2017-01-11 16:07:08 +00:00
< script >
2017-01-11 23:01:27 +00:00
// Create event for when the output is appended to
2017-01-11 16:07:08 +00:00
( function ( $ ) {
var origAppend = $ . fn . append ;
$ . fn . append = function () {
return origAppend . apply ( this , arguments ) . trigger ( " append " );
};
})( jQuery );
</ script >
2016-12-30 20:36:53 +00:00
< script src = " http://pi.hole/admin/scripts/pi-hole/js/queryads.js " ></ script >
2016-12-05 11:12:01 +00:00
< script >
2016-12-30 17:59:05 +00:00
function inIframe () {
try {
return window . self !== window . top ;
} catch ( e ) {
return true ;
}
}
2016-12-06 13:05:02 +00:00
2016-12-30 17:59:05 +00:00
// Try to detect if page is loaded within iframe
if ( inIframe ())
{
// Within iframe
// hide content of page
$ ( '#body' ) . hide ();
// remove background
document . body . style . backgroundImage = " none " ;
}
else
{
// Query adlists
$ ( " #btnSearch " ) . click ();
}
2016-12-30 12:03:45 +00:00
2016-12-30 17:59:05 +00:00
$ ( " #whitelisting " ) . on ( " click " , function (){ $ ( " #whitelistingform " ) . removeAttr ( " hidden " ); });
2016-12-30 12:03:45 +00:00
2017-01-11 23:01:27 +00:00
// Remove whitelist functionality if the domain was blocked because of a wildcard
2017-01-11 16:07:08 +00:00
$ ( " #output " ) . bind ( " append " , function (){
if ( $ ( " #output " ) . contents ()[ 0 ] . data . indexOf ( " Wildcard blocking " ) !== - 1 )
{
$ ( " #whitelisting " ) . hide ();
$ ( " #whitelistingform " ) . hide ();
}
});
2016-12-30 17:59:05 +00:00
function add () {
2016-12-30 12:03:45 +00:00
var domain = $ ( " #domain " );
var pw = $ ( " #pw " );
if ( domain . val () . length === 0 ){
return ;
}
$ . ajax ({
2016-12-30 20:36:53 +00:00
url : " admin/scripts/pi-hole/php/add.php " ,
2016-12-30 12:03:45 +00:00
method : " post " ,
data : { " domain " : domain . val (), " list " : " white " , " pw " : pw . val ()},
success : function ( response ) {
$ ( " #whitelistingoutput " ) . removeAttr ( " hidden " );
if ( response . indexOf ( " Pi-hole blocking " ) !== - 1 )
{
// Reload page after 5 seconds
setTimeout ( function (){ window . location . reload ( 1 );}, 5000 );
2016-12-30 12:27:33 +00:00
$ ( " #whitelistingoutput " ) . html ( " ---> Success <---<br/>You may have to flush your DNS cache " );
2016-12-30 12:03:45 +00:00
}
else
{
$ ( " #whitelistingoutput " ) . html ( " ---> " + response + " <--- " );
}
},
error : function ( jqXHR , exception ) {
$ ( " #whitelistingoutput " ) . removeAttr ( " hidden " );
2016-12-30 18:43:29 +00:00
$ ( " #whitelistingoutput " ) . html ( " ---> Unknown Error <--- " );
2016-12-30 12:03:45 +00:00
}
});
}
// Handle enter button for adding domains
$ ( document ) . keypress ( function ( e ) {
2016-12-30 19:13:15 +00:00
if ( e . which === 13 && $ ( " #pw " ) . is ( " :focus " )) {
2016-12-30 12:03:45 +00:00
add ();
}
});
// Handle buttons
$ ( " #btnAdd " ) . on ( " click " , function () {
add ();
});
2016-12-05 11:12:01 +00:00
</ script >
2015-06-07 04:28:41 +00:00
</ body >
2016-08-20 00:45:17 +00:00
</ html >