@link http://www.phyks.me @licence BEERWARE (See README.md file) @version 2.2.0 @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. */ 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) { $output[$key] = trim($value, "\t\n\r\0\x0B,\""); } return $output; } function adblock_plugin_treat_events(&$events) { //Set params $configurationManager = new Configuration(); $partial = $configurationManager->get('articleView') == "partial"; $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]); } } 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") { $block_flash = true; } 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; } 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 $filter_img = false; } } else { $filter_img = false; } $img_except_list = explode(',', trim($adblock_params["img_list"], "\t\n\r\0\x0B,")); $img_except_list = adblock_trim_list($img_except_list); $elegant_degradation = (isset($adblock_params["elegant_degradation"]) && $adblock_params["elegant_degradation"] == "1") ? true : false; foreach($events as $event) { $filtered_content = ($partial) ? $event->getDescription : $event->getContent(); $modified = false; // 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 preg_match_all("##U", $filtered_content, $object_list_in_event, PREG_SET_ORDER); foreach($object_list_in_event as $object) { if(!$elegant_degradation) { $replacement_content = ' X '; $filtered_content = str_replace($object[0], $replacement_content, $filtered_content); } else { 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 = ' X '; $filtered_content = str_replace($object[0], $replacement_content, $filtered_content); } } $modified = true; } } // Images handling if($filter_img) { if(($block_img && !in_array($event->getFeed(), $img_except_list)) || (!$block_img && in_array($event->getFeed(), $img_except_list))) { //Replace images preg_match_all("##U", $filtered_content, $img_list_in_event, PREG_SET_ORDER); foreach($img_list_in_event as $img) { if(!$elegant_degradation) { $replacement_content = ' X '; $filtered_content = str_replace($img[0], $replacement_content, $filtered_content); } else { $content_size = getimagesize($img[1]); //Index 0 is width, index 1 is height $font_size = min($content_size[0], $content_size[1]); $replacement_content = ' X '; $filtered_content = str_replace($img[0], $replacement_content, $filtered_content); } } $modified = true; } } if($modified) { if($partial) $event->setDescription($filtered_content); else $event->setContent($filtered_content); } } } function adblock_plugin_setting_link(&$myUser) { $myUser = (isset($_SESSION['currentUser'])?unserialize($_SESSION['currentUser']):false); if($myUser!=false) { echo '
  • '._t('P_ADBLOCK_TITLE').'
  • '; } } function adblock_plugin_setting_bloc(&$myUser) { $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]); } } $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 '

    '._t('P_ADBLOCK_H2_TITLE').'

    '._t('P_ADBLOCK_NOTE').'

    '._t('P_ADBLOCK_OPT_FLASH_TITLE').'

    '._t('P_ADBLOCK_OPT_FLASH').'

    '._t('P_ADBLOCK_DEFAULT_BEHAVIOR').'

    '.(($flash_block) ? _t('P_ADBLOCK_WHITELIST') : _t('P_ADBLOCK_BLACKLIST')).'

    '._t('P_ADBLOCK_OPT_IMAGES_TITLE').'

    '._t('P_ADBLOCK_OPT_IMAGES').'

    '._t('P_ADBLOCK_DEFAULT_BEHAVIOR').'

    '._t('P_ADBLOCK_OPT_IMAGES_MOBILE').'

    '.(($img_block) ? _t('P_ADBLOCK_WHITELIST') : _t('P_ADBLOCK_BLACKLIST')).'

    '._t('P_ADBLOCK_OPT_DEGRADATION').'
    '.((!$getimagesize_available) ? _t('P_ADBLOCK_OPT_DEGR_MSG') : '').'

    '; } } function adblock_plugin_setting_update($_) { $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."\nflash_block = ".$flash_block."\nflash_list = ".$flash_list."\nimg_enabled = ".$img_enabled."\nimg_block = ".$img_block."\nimg_only_mobiles = ".$img_only_mobiles."\nimg_list = ".$img_list."\nelegant_degradation = ".$elegant_degradation)) header('location: settings.php'); else exit(_t('P_ADBLOCK_ERR_WRITE')); } } } Plugin::addCSS("/css/adblock_plugin_css.css"); Plugin::addJS("/js/adblock_plugin_js.js"); Plugin::addHook("index_post_treatment", "adblock_plugin_treat_events"); 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"); ?>