2013-08-02 22:18:35 +02:00
< ? php
/*
@ name Adblock
@ author Phyks < phyks @ phyks . me >
@ link http :// www . phyks . me
2013-08-04 21:45:04 +02:00
@ licence BEERWARE ( See README . md file )
2014-02-13 02:40:13 +01:00
@ version 2.2 . 0
2013-08-04 21:45:04 +02:00
@ description The adblock plugin for leed allows to block embedded flash contents and / or images in feeds . You can set it fine - grained for each feed . You can also disable images only for mobile devices .
2013-08-04 15:11:29 +02:00
*/
2013-08-02 22:18:35 +02:00
2013-08-04 22:54:58 +02:00
function adblock_isMobileDevice () {
return preg_match ( " /(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up \ .browser|up \ .link|webos|wos)/i " , $_SERVER [ " HTTP_USER_AGENT " ]);
}
function adblock_trim_list ( $input ) {
$output = array ();
foreach ( $input as $key => $value ) {
2013-08-05 13:27:40 +02:00
$output [ $key ] = trim ( $value , " \t \n \r \0 \x0B , \" " );
2013-08-04 22:54:58 +02:00
}
return $output ;
}
2013-08-02 22:18:35 +02:00
2013-08-04 20:13:32 +02:00
function adblock_plugin_treat_events ( & $events ) {
2013-08-04 22:54:58 +02:00
//Set params
2013-08-05 13:27:40 +02:00
$configurationManager = new Configuration ();
$partial = $configurationManager -> get ( 'articleView' ) == " partial " ;
2013-12-08 00:44:49 +01:00
$adblock_constants = file_get_contents ( " adblock_constants.php " );
2013-08-04 22:54:58 +02:00
$adblock_constants = explode ( " \n " , $adblock_constants );
$adblock_params = array ();
foreach ( $adblock_constants as $adblock_constant ) {
if ( trim ( $adblock_constant ) != " " ) {
$adblock_constant = explode ( " = " , $adblock_constant );
$adblock_params [ trim ( $adblock_constant [ 0 ])] = trim ( $adblock_constant [ 1 ]);
}
}
if ( isset ( $adblock_params [ " flash_enabled " ]) && $adblock_params [ " flash_enabled " ] == " 1 " ) {
$filter_flash = true ;
if ( isset ( $adblock_params [ " flash_block " ]) && $adblock_params [ " flash_block " ] == " 1 " ) {
2013-12-09 11:30:35 +01:00
$block_flash = true ;
2013-08-04 22:54:58 +02:00
}
else {
$block_flash = false ;
}
}
else {
$filter_flash = false ;
}
$flash_except_list = explode ( ',' , trim ( $adblock_params [ " flash_list " ], " \t \n \r \0 \x0B , " ));
$flash_except_list = adblock_trim_list ( $flash_except_list );
if ( isset ( $adblock_params [ " img_enabled " ]) && $adblock_params [ " img_enabled " ] == " 1 " ) {
$filter_img = true ;
if ( isset ( $adblock_params [ " img_block " ]) && $adblock_params [ " img_block " ] == " 1 " ) {
$block_img = true ;
}
else {
$block_img = false ;
}
2013-08-05 13:27:40 +02:00
if ( isset ( $adblock_params [ " img_only_mobiles " ]) && $adblock_params [ " img_only_mobiles " ] == " 1 " && ! adblock_isMobileDevice ()) { //If filter only on mobile devices and not a mobile device
2013-08-04 22:54:58 +02:00
$filter_img = false ;
}
}
else {
$filter_img = false ;
}
$img_except_list = explode ( ',' , trim ( $adblock_params [ " img_list " ], " \t \n \r \0 \x0B , " ));
2014-03-13 11:05:59 +01:00
$img_except_list = adblock_trim_list ( $img_except_list );
2013-08-04 22:54:58 +02:00
2013-08-05 11:49:28 +02:00
$elegant_degradation = ( isset ( $adblock_params [ " elegant_degradation " ]) && $adblock_params [ " elegant_degradation " ] == " 1 " ) ? true : false ;
2013-08-04 22:54:58 +02:00
2013-08-04 15:11:29 +02:00
foreach ( $events as $event ) {
2013-08-05 13:27:40 +02:00
$filtered_content = ( $partial ) ? $event -> getDescription : $event -> getContent ();
2013-08-05 14:41:48 +02:00
$modified = false ;
2013-08-04 22:54:58 +02:00
// Flash handling
if ( $filter_flash ) {
if (( $block_flash && ! in_array ( $event -> getFeed (), $flash_except_list )) || ( ! $block_flash && in_array ( $event -> getFeed (), $flash_except_list ))) {
//Replace flash content
2013-08-05 22:42:10 +02:00
preg_match_all ( " #<iframe. { 0,}/(iframe)?>#U " , $filtered_content , $object_list_in_event , PREG_SET_ORDER );
2013-08-04 23:38:42 +02:00
foreach ( $object_list_in_event as $object ) {
2013-08-05 22:42:10 +02:00
if ( ! $elegant_degradation ) {
$replacement_content = '
< span class = " blocked_flash " onclick = " return adblock_unblock_flash(this, \ ''.htmlspecialchars( $object[0] ).' \ '); " > X </ span >
' ;
$filtered_content = str_replace ( $object [ 0 ], $replacement_content , $filtered_content );
2013-08-05 11:49:28 +02:00
}
else {
2013-08-05 22:42:10 +02:00
preg_match ( " #width=[ \" ']([0-9] { 1,})[ \" ']#U " , $object [ 0 ], $width );
preg_match ( " #height=[ \" ']([0-9] { 1,})[ \" ']#U " , $object [ 0 ], $height );
$font_size = min ( $width [ 1 ], $height [ 1 ]);
$replacement_content = '
< span class = " blocked_flash " '.((!empty($width[1]) && !empty($height[1])) ? ' style = " width: '. $width[1] .'px; height: '. $height[1] .'px; padding: 0; padding-top: '.((int) $font_size ) / 2 .'px; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; font-size: '. $font_size .'px; " ' : ' ').' onclick = " return adblock_unblock_flash(this, \ ''.htmlspecialchars( $object[0] ).' \ '); " > X </ span >
' ;
$filtered_content = str_replace ( $object [ 0 ], $replacement_content , $filtered_content );
2013-08-05 11:49:28 +02:00
}
2013-08-04 23:38:42 +02:00
}
2013-08-05 22:42:10 +02:00
$modified = true ;
2013-08-04 22:54:58 +02:00
}
}
2013-12-09 11:30:35 +01:00
2013-08-04 22:54:58 +02:00
// Images handling
if ( $filter_img ) {
2013-08-05 13:27:40 +02:00
if (( $block_img && ! in_array ( $event -> getFeed (), $img_except_list )) || ( ! $block_img && in_array ( $event -> getFeed (), $img_except_list ))) {
2013-08-04 23:23:11 +02:00
//Replace images
2013-08-05 13:27:40 +02:00
preg_match_all ( " #<img. { 0,}src=[ \" '](. { 1,})[ \" ']. { 0,}/?(img)?>#U " , $filtered_content , $img_list_in_event , PREG_SET_ORDER );
2013-08-04 23:23:11 +02:00
foreach ( $img_list_in_event as $img ) {
2013-08-05 13:27:40 +02:00
if ( ! $elegant_degradation ) {
$replacement_content = '
2013-08-05 22:42:10 +02:00
< span class = " blocked_image " onclick = " return adblock_unblock_img(this, \ ''.urlencode( $img[1] ).' \ '); " > X </ span >
2013-08-05 13:27:40 +02:00
' ;
$filtered_content = str_replace ( $img [ 0 ], $replacement_content , $filtered_content );
2013-08-05 11:49:28 +02:00
}
else {
2013-08-05 13:27:40 +02:00
$content_size = getimagesize ( $img [ 1 ]); //Index 0 is width, index 1 is height
2013-08-05 22:42:10 +02:00
$font_size = min ( $content_size [ 0 ], $content_size [ 1 ]);
2013-08-05 13:27:40 +02:00
$replacement_content = '
2013-08-05 22:42:10 +02:00
< span class = " blocked_image " style = " width:'.(int) $content_size[0] .'px; height:'.(int) $content_size[1] .'px; padding:0; padding-top: '.((int) $font_size ) / 2 .'px; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; font-size: '. $font_size .'px; " onclick = " return adblock_unblock_img(this, \ ''.urlencode( $img[1] ).' \ '): " > X </ span >
2013-08-05 13:27:40 +02:00
' ;
$filtered_content = str_replace ( $img [ 0 ], $replacement_content , $filtered_content );
2013-08-05 11:49:28 +02:00
}
2013-08-04 23:23:11 +02:00
}
2013-08-05 14:41:48 +02:00
$modified = true ;
2013-08-04 22:54:58 +02:00
}
}
2013-12-09 11:30:35 +01:00
2013-08-05 22:42:10 +02:00
if ( $modified ) {
if ( $partial )
$event -> setDescription ( $filtered_content );
else
$event -> setContent ( $filtered_content );
}
2013-08-04 15:11:29 +02:00
}
2013-08-02 22:18:35 +02:00
}
2013-08-04 15:11:29 +02:00
function adblock_plugin_setting_link ( & $myUser ) {
2014-02-09 15:46:19 +01:00
$myUser = ( isset ( $_SESSION [ 'currentUser' ]) ? unserialize ( $_SESSION [ 'currentUser' ]) : false );
if ( $myUser != false ) {
echo '
2014-02-13 02:40:13 +01:00
< li >< a class = " toggle " href = " #adblockSettingsBloc " > '._t(' P_ADBLOCK_TITLE ').' </ a ></ li > ' ;
2014-02-09 15:46:19 +01:00
}
2013-08-04 15:11:29 +02:00
}
function adblock_plugin_setting_bloc ( & $myUser ) {
2014-02-09 15:46:19 +01:00
$myUser = ( isset ( $_SESSION [ 'currentUser' ]) ? unserialize ( $_SESSION [ 'currentUser' ]) : false );
if ( $myUser != false ) {
$adblock_constants = file_get_contents ( 'adblock_constants.php' );
$adblock_constants = explode ( " \n " , $adblock_constants );
$adblock_params = array ();
foreach ( $adblock_constants as $adblock_constant ) {
if ( trim ( $adblock_constant ) != " " ) {
$adblock_constant = explode ( " = " , $adblock_constant );
$adblock_params [ trim ( $adblock_constant [ 0 ])] = trim ( $adblock_constant [ 1 ]);
}
2013-08-04 15:11:29 +02:00
}
2013-08-04 20:53:49 +02:00
2014-02-09 15:46:19 +01:00
$flash_enabled = ( isset ( $adblock_params [ " flash_enabled " ]) && $adblock_params [ " flash_enabled " ] == " 1 " ) ? true : false ;
$flash_block = ( isset ( $adblock_params [ " flash_block " ]) && $adblock_params [ " flash_block " ] == " 1 " ) ? true : false ;
if ( isset ( $adblock_params [ " flash_list " ]))
$flash_list = str_replace ( " , " , " \n " , trim ( $adblock_params [ " flash_list " ], " \t \n \r \0 \x0B , " ));
else
$flash_list = " " ;
$img_enabled = ( isset ( $adblock_params [ " img_enabled " ]) && $adblock_params [ " img_enabled " ] == " 1 " ) ? true : false ;
$img_only_mobiles = ( isset ( $adblock_params [ " img_only_mobiles " ]) && $adblock_params [ " img_only_mobiles " ] == 1 ) ? true : false ;
$img_block = ( isset ( $adblock_params [ " img_block " ]) && $adblock_params [ " img_block " ] == " 1 " ) ? true : false ;
if ( isset ( $adblock_params [ " img_list " ]))
$img_list = str_replace ( " , " , " \n " , trim ( $adblock_params [ " img_list " ], " \t \n \r \0 \x0B , " ));
else
$img_list = " " ;
$elegant_degradation = ( isset ( $adblock_params [ " elegant_degradation " ]) && $adblock_params [ " elegant_degradation " ] == " 1 " ) ? true : false ;
$getimagesize_available = function_exists ( " getimagesize " ) && (( ini_get ( " allow_url_fopen " ) == " 1 " ) ? true : false );
echo '
2014-02-13 02:40:13 +01:00
< section id = " adblockSettingsBloc " class = " adblockSettingsBloc " >
< h2 > '._t(' P_ADBLOCK_H2_TITLE ').' </ h2 >
< p > '._t(' P_ADBLOCK_NOTE ').' </ p >
< form action = " action.php?action=adblock_update " method = " POST " >
< fieldset >
< div class = " flash_adblockSettingsBlock " >
< h3 > '._t(' P_ADBLOCK_OPT_FLASH_TITLE ').' </ h3 >
< p >
'._t(' P_ADBLOCK_OPT_FLASH ').' < br />
< input type = " radio " name = " flash_adblock_enable " value = " 1 " id = " flash_adblock_block_enabled " '.(($flash_enabled) ? ' checked = " checked " ' : ' ').' />< label for = " flash_adblock_block_enabled " > '._t(' P_ADBLOCK_OPT_ENABLED ').' </ label >< br />
< input type = " radio " name = " flash_adblock_enable " value = " 0 " id = " flash_adblock_block_disable " '.((!$flash_enabled) ? ' checked = " checked " ' : ' ').' /> < label for = " flash_adblock_block_disable " > '._t(' P_ADBLOCK_OPT_DISABLED ').' </ label >
</ p >
< p > '._t(' P_ADBLOCK_DEFAULT_BEHAVIOR ').' < br />
< input type = " radio " name = " flash_adblock_default_behavior " value = " 1 " id = " flash_adblock_blockall " onchange = " adblock_whitelist_blacklist( \ 'adblock_flash_whitelist_blacklist \ ', 1); " '.(($flash_block) ? ' checked = " checked " ' : ' ').' />< label for = " flash_adblock_blockall " > '._t(' P_ADBLOCK_DEFAULT_BEHAVIOR_BLOCK ').' </ label >< br />
< input type = " radio " name = " flash_adblock_default_behavior " value = " 0 " id = " flash_adblock_allowall " onchange = " adblock_whitelist_blacklist( \ 'adblock_flash_whitelist_blacklist \ ', 0); " '.((!$flash_block) ? ' checked = " checked " ' : ' ').' />< label for = " flash_adblock_allowall " > '._t(' P_ADBLOCK_DEFAULT_BEHAVIOR_ALLOW ').' </ label >
</ p >
< p >< span id = " adblock_flash_whitelist_blacklist " > '.(($flash_block) ? _t(' P_ADBLOCK_WHITELIST ') : _t(' P_ADBLOCK_BLACKLIST ')).' </ span ></ p >
< textarea name = " flash_adblock_list " rows = " 7 " > '.$flash_list.' </ textarea >
</ div >
</ fieldset >
< fieldset >
< div class = " img_adblockSettingsBlock " >
< h3 > '._t(' P_ADBLOCK_OPT_IMAGES_TITLE ').' </ h3 >
< p >
'._t(' P_ADBLOCK_OPT_IMAGES ').' < br />
< input type = " radio " name = " img_adblock_enable " value = " 1 " id = " img_adblock_block_enabled " '.(($img_enabled) ? ' checked = " checked " ' : ' ').' />< label for = " img_adblock_block_enabled " > '._t(' P_ADBLOCK_OPT_ENABLED ').' </ label >< br />
< input type = " radio " name = " img_adblock_enable " value = " 0 " id = " img_adblock_block_disable " '.((!$img_enabled) ? ' checked = " checked " ' : ' ').' />< label for = " img_adblock_block_disable " > '._t(' P_ADBLOCK_OPT_DISABLED ').' </ label >
</ p >
< p > '._t(' P_ADBLOCK_DEFAULT_BEHAVIOR ').' < br />
< input type = " radio " name = " img_adblock_default_behavior " value = " 1 " id = " img_adblock_blockall " onchange = " adblock_whitelist_blacklist( \ 'adblock_img_whitelist_blacklist \ ', 1); " '.(($img_block) ? ' checked = " checked " ' : ' ').' />< label for = " img_adblock_blockall " > '._t(' P_ADBLOCK_DEFAULT_BEHAVIOR_BLOCK ').' </ label >< br />
< input type = " radio " name = " img_adblock_default_behavior " value = " 0 " id = " img_adblock_allowall " onchange = " adblock_whitelist_blacklist( \ 'adblock_img_whitelist_blacklist \ ', 0); " '.((!$img_block) ? ' checked = " checked " ' : ' ').' />< label for = " img_adblock_allowall " > '._t(' P_ADBLOCK_DEFAULT_BEHAVIOR_ALLOW ').' </ label >
</ p >
< p > '._t(' P_ADBLOCK_OPT_IMAGES_MOBILE ').' < br />
< input type = " radio " name = " img_adblock_only_mobiles " value = " 1 " id = " img_adblock_only_mobiles_yes " '.(($img_only_mobiles) ? ' checked = " checked " ' : ' ').' />< label for = " img_adblock_only_mobiles_yes " > '._t(' P_ADBLOCK_OPT_IMAGES_BLOCK_MOBILE ').' </ label >< br />
< input type = " radio " name = " img_adblock_only_mobiles " value = " 0 " id = " img_adblock_only_mobiles_no " '.((!$img_only_mobiles) ? ' checked = " checked " ' : ' ').' />< label for = " img_adblock_only_mobiles_no " > '._t(' P_ADBLOCK_OPT_IMAGES_BLOCK_ALL ').' </ label >
</ p >
< p >< span id = " adblock_img_whitelist_blacklist " > '.(($img_block) ? _t(' P_ADBLOCK_WHITELIST ') : _t(' P_ADBLOCK_BLACKLIST ')).' </ span ></ p >
< textarea name = " img_adblock_list " rows = " 7 " > '.$img_list.' </ textarea >
</ div >
</ fieldset >
< p >
'._t(' P_ADBLOCK_OPT_DEGRADATION ').' < br />
< input type = " radio " name = " adblock_elegant_degradation " value = " 1 " id = " adblock_elegant_degradation_yes " '.(($elegant_degradation) ? ' checked = " checked " ' : ' ').' '.((!$getimagesize_available) ? ' disabled ' : ' ').' />< label for = " adblock_elegant_degradation_yes " > '._t(' P_ADBLOCK_YES ').' </ label > '.((!$getimagesize_available) ? _t(' P_ADBLOCK_OPT_DEGR_MSG ') : ' ').' < br />
< input type = " radio " name = " adblock_elegant_degradation " value = " 0 " id = " adblock_elegant_degradation_no " '.((!$elegant_degradation) ? ' checked = " checked " ' : ' ').' />< label for = " adblock_elegant_degradation_no " > '._t(' P_ADBLOCK_NO ').' </ label >
</ p >
< p id = " adblock_settings_submit " >
< input type = " submit " class = " button " value = " '._t('P_ADBLOCK_SAVE').' " />
</ p >
</ form >
</ section > ' ;
2014-02-09 15:46:19 +01:00
}
2013-08-04 15:11:29 +02:00
}
function adblock_plugin_setting_update ( $_ ) {
2014-02-09 15:46:19 +01:00
$myUser = ( isset ( $_SESSION [ 'currentUser' ]) ? unserialize ( $_SESSION [ 'currentUser' ]) : false );
if ( $myUser != false ) {
if ( $_ [ 'action' ] == 'adblock_update' ) {
$flash_enabled = ( int ) $_ [ 'flash_adblock_enable' ];
$flash_block = ( int ) $_ [ 'flash_adblock_default_behavior' ];
$flash_list = str_replace ( " \r \n " , " , " , trim ( $_ [ " flash_adblock_list " ]));
$flash_list = str_replace ( " \n " , " , " , trim ( $flash_list ));
$img_enabled = ( int ) $_ [ 'img_adblock_enable' ];
$img_block = ( int ) $_ [ 'img_adblock_default_behavior' ];
$img_only_mobiles = ( int ) $_ [ " img_adblock_only_mobiles " ];
$img_list = str_replace ( " \r \n " , " , " , trim ( $_ [ " img_adblock_list " ]));
$img_list = str_replace ( " \n " , " , " , trim ( $img_list ));
$elegant_degradation = ( int ) $_ [ " adblock_elegant_degradation " ];
if ( file_put_contents ( " adblock_constants.php " , " flash_enabled = " . $flash_enabled . " \n flash_block = " . $flash_block . " \n flash_list = " . $flash_list . " \n img_enabled = " . $img_enabled . " \n img_block = " . $img_block . " \n img_only_mobiles = " . $img_only_mobiles . " \n img_list = " . $img_list . " \n elegant_degradation = " . $elegant_degradation ))
header ( 'location: settings.php' );
else
2014-02-13 02:40:13 +01:00
exit ( _t ( 'P_ADBLOCK_ERR_WRITE' ));
2014-02-09 15:46:19 +01:00
}
2013-08-04 20:13:32 +02:00
}
2013-08-04 15:11:29 +02:00
}
Plugin :: addCSS ( " /css/adblock_plugin_css.css " );
Plugin :: addJS ( " /js/adblock_plugin_js.js " );
2013-08-04 20:13:32 +02:00
Plugin :: addHook ( " index_post_treatment " , " adblock_plugin_treat_events " );
2013-08-04 15:11:29 +02:00
Plugin :: addHook ( " setting_post_link " , " adblock_plugin_setting_link " );
Plugin :: addHook ( " setting_post_section " , " adblock_plugin_setting_bloc " );
Plugin :: addHook ( " action_post_case " , " adblock_plugin_setting_update " );
2013-08-02 22:18:35 +02:00
?>