| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Prevent Direct Access |
| 4 |
Plugin URI: https://preventdirectaccess.com |
| 5 |
Description: Prevent Direct Access provides a simple solution to prevent Google and AI bot indexing as well as the public from accessing your files without permission. This plugin is required for our Gold version to work properly. |
| 6 |
Version: 2.8.9.1 |
| 7 |
Author: BWPS |
| 8 |
Author URI: https://preventdirectaccess.com |
| 9 |
Tags: files, management |
| 10 |
Requires PHP: 7.0 |
| 11 |
License: GPLv2 or later |
| 12 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html |
| 13 |
Text Domain: prevent-direct-access |
| 14 |
Domain Path: /languages |
| 15 |
*/ |
| 16 |
|
| 17 |
if (! defined('ABSPATH') ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
// Define Some Variable |
| 22 |
define('PDA', __FILE__); |
| 23 |
define('PDA_HOME_PAGE', 'https://preventdirectaccess.com/?utm_source=user-website&utm_medium=%s&utm_campaign=%s'); |
| 24 |
define('PDA_DOWNLOAD_PAGE', 'https://preventdirectaccess.com/pricing/?utm_source=user-website&utm_medium=settings&utm_campaign=sidebar-cta'); |
| 25 |
define('PDA_SIDEBAR_API', 'https://preventdirectaccess.com/wp-json/pda-fss/v1/content'); |
| 26 |
define('PDA_PRICING_PAGE', 'https://preventdirectaccess.com/pricing/?utm_source=user-website&utm_medium=%s&utm_campaign=%s'); |
| 27 |
define('PDA_TEXTDOMAIN', 'prevent-direct-access'); |
| 28 |
define('PDAF_VERSION', '2.8.9.1'); |
| 29 |
define('PDA_LITE_BASE_URL', plugin_dir_url(__FILE__)); |
| 30 |
define('PDA_LITE_BASE_DIR', plugin_dir_path(__FILE__)); |
| 31 |
define('PDA_LITE_PLUGIN_BASE_NAME', plugin_basename( __FILE__ ) ); |
| 32 |
|
| 33 |
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals |
| 34 |
|
| 35 |
// Include Required Files |
| 36 |
require 'includes/repository.php'; |
| 37 |
require 'includes/js-loader.php'; |
| 38 |
require 'includes/helper.php'; |
| 39 |
require 'includes/setting.php'; |
| 40 |
require 'includes/settings_page.php'; |
| 41 |
require 'includes/handle.php'; |
| 42 |
require 'includes/pda_lite_api.php'; |
| 43 |
require 'includes/constants.php'; |
| 44 |
require 'includes/pda_lite_affiliate.php'; |
| 45 |
|
| 46 |
require_once dirname(__FILE__) . '/includes/function.php'; |
| 47 |
require dirname(__FILE__) . '/includes/db-init.php'; |
| 48 |
|
| 49 |
require_once PDA_LITE_BASE_DIR . '/includes/modules/Grid_View/loader.php'; |
| 50 |
require_once PDA_LITE_BASE_DIR . '/includes/modules/Grid_View/service.php'; |
| 51 |
|
| 52 |
// PDA Admin Class Start |
| 53 |
class Pda_Admin |
| 54 |
{ |
| 55 |
|
| 56 |
// Declare some variable |
| 57 |
private $pda_function; |
| 58 |
private $db; |
| 59 |
|
| 60 |
// Start Constructor |
| 61 |
function __construct() |
| 62 |
{ |
| 63 |
/** |
| 64 |
* Do not support multisite mode when PDA Gold has never entered the valid license. |
| 65 |
*/ |
| 66 |
if (! get_option('pda_is_licensed') && is_multisite() ) { |
| 67 |
add_action('admin_notices', array( $this, 'multisite_admin_notices' )); |
| 68 |
add_action('network_admin_notices', array( $this, 'multisite_admin_notices' )); |
| 69 |
// return; |
| 70 |
} |
| 71 |
|
| 72 |
$this->pda_function = new Pda_Function(); |
| 73 |
$this->db = new Pda_Database(); |
| 74 |
$Pda_JS_Loader = new Pda_JS_Loader(PDA_TEXTDOMAIN, PDAF_VERSION); |
| 75 |
|
| 76 |
if (get_option('pda_is_licensed') && defined('PDA_GOLD_V3_VERSION') ) { |
| 77 |
register_uninstall_hook(__FILE__, array( 'Pda_Admin', 'remove_options_when_uninstalling_plugin' )); |
| 78 |
add_action('admin_init', array( $this, 'handle_flush_rewrite_rules' )); |
| 79 |
|
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
add_action('wp_footer', array( $this , 'prevent_right_click')); |
| 84 |
|
| 85 |
add_filter('mod_rewrite_rules', array( $this, 'htaccess_contents' )); |
| 86 |
// TODO: Hide protected file later. |
| 87 |
add_filter('pre_get_posts', array($this, 'hide_posts_media_by_other')); |
| 88 |
|
| 89 |
$this->identifyFeatures(); |
| 90 |
add_action('admin_enqueue_scripts', array( $Pda_JS_Loader, 'admin_load_js' )); |
| 91 |
add_action('wp_ajax_myaction', array( $this, 'so_wp_ajax_function' )); |
| 92 |
add_action('wp_ajax_regenerate-url', array( $this, 'so_wp_ajax_regenerate_url' )); |
| 93 |
add_action('wp_ajax_pda_lite_update_general_settings', array( $this, 'pda_lite_update_general_settings' )); |
| 94 |
add_action('wp_ajax_pda_lite_update_ip_restriction_settings', array( $this, 'pda_lite_update_ip_restriction_settings' )); |
| 95 |
add_action('wp_ajax_pda_free_subscribe', array( $this, 'pda_ajax_pda_free_subscribe' )); |
| 96 |
|
| 97 |
add_action('delete_post', array( $this, 'delete_prevent_direct_access' )); |
| 98 |
add_action('admin_notices', array( $this, 'admin_notices' )); |
| 99 |
add_action('init', array( $this, 'my_endpoint' )); |
| 100 |
add_action('admin_init', array( $this, 'check_htaccess_updated' )); |
| 101 |
add_action('parse_query', array( $this, 'parse_query' )); |
| 102 |
add_action('wp_ajax_pda_subscribe', array( $this, 'pda_ajax_subscribe' )); |
| 103 |
add_action('admin_menu', array( $this, 'pda_add_settings_page' )); |
| 104 |
|
| 105 |
register_activation_hook(__FILE__, array( $this, 'plugin_install' )); |
| 106 |
register_deactivation_hook(__FILE__, array( $this, 'deactivate' )); |
| 107 |
register_uninstall_hook(__FILE__, array( 'Pda_Admin', 'plugin_uninstall' )); |
| 108 |
add_filter('plugin_row_meta', array( $this, 'register_plugins_links' ), 10, 2); |
| 109 |
add_filter('robots_txt', array( $this, 'pda_custom_robots_txt' ), 10, 2); |
| 110 |
|
| 111 |
add_action('the_posts', array( $this, 'modify_protected_media' ), 10); |
| 112 |
add_action('plugins_loaded', array( $this, 'pda_load_text_domain' )); |
| 113 |
|
| 114 |
add_action('upgrader_process_complete', array( $this, 'create_new_table_and_migrate_data' ), 10, 2); |
| 115 |
|
| 116 |
add_action('rest_api_init', array( $this, 'pda_rest_api_init_cb' ), 10, 2); |
| 117 |
|
| 118 |
|
| 119 |
add_filter( 'plugin_action_links_' . PDA_LITE_PLUGIN_BASE_NAME, array( $this, 'handle_plugin_links' ), 30 ); |
| 120 |
|
| 121 |
pda_add_defaults_fn(); |
| 122 |
|
| 123 |
$grid_view_module = new PDAFree\modules\Grid_View\Loader($this); |
| 124 |
$grid_view_module->register(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
public function handle_plugin_links( $links ) { |
| 129 |
|
| 130 |
$setting_url = esc_url( admin_url( 'admin.php?page=wp_pda_options' ) ); |
| 131 |
$plugin_link = '<a href="' . $setting_url . '">' . __( 'Settings', 'prevent-direct-access' ) . '</a>'; |
| 132 |
array_unshift( $links, $plugin_link ); |
| 133 |
|
| 134 |
return $links; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Hide Media Post |
| 139 |
* |
| 140 |
* @param array $query |
| 141 |
* |
| 142 |
* @return query |
| 143 |
*/ |
| 144 |
public function hide_posts_media_by_other($query) |
| 145 |
{ |
| 146 |
|
| 147 |
// Define global variable |
| 148 |
global $pagenow; |
| 149 |
$pda_option = get_option('FREE_PDA_SETTINGS'); |
| 150 |
if (is_array($pda_option) && array_key_exists('hide_protected_files_in_media', $pda_option) && $pda_option['hide_protected_files_in_media'] === "on" ) { |
| 151 |
if(!Pda_Helper::is_admin_user_role() ) { |
| 152 |
global $user_ID; |
| 153 |
$query->set('author', $user_ID); |
| 154 |
} |
| 155 |
} |
| 156 |
return $query; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Remove options when uninstall plugin if PDA Gold is entered license. |
| 161 |
*/ |
| 162 |
public static function remove_options_when_uninstalling_plugin() |
| 163 |
{ |
| 164 |
delete_option('pda_free_is_rewrite_rules'); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Flush rewrite rules if PDA Free update to new version. |
| 169 |
*/ |
| 170 |
public function handle_flush_rewrite_rules() |
| 171 |
{ |
| 172 |
if (defined('PDA_GOLD_V3_VERSION') && ! get_option('pda_free_is_rewrite_rules') ) { |
| 173 |
flush_rewrite_rules(); |
| 174 |
update_option('pda_free_is_rewrite_rules', '1'); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Check File is Protected or not |
| 180 |
* |
| 181 |
* @param integer $id |
| 182 |
* |
| 183 |
* @return Mixed |
| 184 |
*/ |
| 185 |
public function check_protected_file( $id ) |
| 186 |
{ |
| 187 |
$protected = new PDA_Repository; |
| 188 |
$protected_file = $protected->get_status_advance_file_by_post_id($id, true); |
| 189 |
if (isset($protected_file) && $protected_file->is_prevented === "1" ) { |
| 190 |
$result = '<i class="dashicons dashicons-yes protected_yes" style="color: green"></i>'; |
| 191 |
} else { |
| 192 |
$result = '<i class="dashicons dashicons-no protected_no" style="color: red"></i>'; |
| 193 |
} |
| 194 |
|
| 195 |
return $result . '<style> |
| 196 |
.media-types.media-types-required-info { |
| 197 |
display: none; |
| 198 |
} |
| 199 |
</style>'; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Add Attachment |
| 204 |
* |
| 205 |
* @param array $form_fields |
| 206 |
* @param array $post |
| 207 |
* |
| 208 |
* @return form_field |
| 209 |
*/ |
| 210 |
function add_filed_attachment( $form_fields, $post ) |
| 211 |
{ |
| 212 |
$yes_or_no = $this->check_protected_file($post->ID); |
| 213 |
$form_fields[] = array( |
| 214 |
'input' => 'html', |
| 215 |
'label' => 'Protected', |
| 216 |
'html' => $yes_or_no, |
| 217 |
); |
| 218 |
|
| 219 |
return $form_fields; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Register Plugin Link |
| 224 |
* |
| 225 |
* @param string $links |
| 226 |
* @param string $file |
| 227 |
* |
| 228 |
* @return links |
| 229 |
*/ |
| 230 |
public function register_plugins_links( $links, $file ) |
| 231 |
{ |
| 232 |
$base = plugin_basename(__FILE__); |
| 233 |
if ($file == $base ) { |
| 234 |
$links[] = '<a style="color: #cc0000 ; font-weight: bold;" target="_blank" href=' . sprintf(constant('PDA_PRICING_PAGE'), 'pluginpage', 'plugin-upgrade-link') . '>' . __('Upgrade to Gold Version', 'prevent-direct-access') . '</a>'; |
| 235 |
} |
| 236 |
|
| 237 |
return $links; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Add Support Form |
| 242 |
*/ |
| 243 |
public function add_support_form() |
| 244 |
{ |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Add Action and Filter to identify featurs |
| 249 |
*/ |
| 250 |
public function identifyFeatures() |
| 251 |
{ |
| 252 |
add_action('manage_media_custom_column', array( $this, 'media_custom_columns' ), 0, 2); |
| 253 |
add_filter('manage_upload_columns', array( $this, 'free_add_upload_columns' )); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Define endpoints |
| 258 |
*/ |
| 259 |
public function my_endpoint() |
| 260 |
{ |
| 261 |
$configs = Pda_Helper::get_plugin_configs(); |
| 262 |
$endpoint = $configs['endpoint']; |
| 263 |
add_rewrite_endpoint($endpoint, EP_ROOT); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Parse query to check endpoints |
| 268 |
*/ |
| 269 |
public function parse_query( $query ) |
| 270 |
{ |
| 271 |
$configs = Pda_Helper::get_plugin_configs(); |
| 272 |
$endpoint = $configs['endpoint']; |
| 273 |
if (isset($query->query_vars[ $endpoint ]) ) { |
| 274 |
include plugin_dir_path(__FILE__) . '/download.php'; |
| 275 |
exit; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Check htaccess updated |
| 281 |
*/ |
| 282 |
public function check_htaccess_updated() |
| 283 |
{ |
| 284 |
$htaccess_writable = $this->pda_function->htaccess_writable(); |
| 285 |
|
| 286 |
$plugin = plugin_basename(__FILE__); |
| 287 |
$is_plugin_active = is_plugin_active($plugin); |
| 288 |
if ($htaccess_writable !== true && $is_plugin_active ) { |
| 289 |
delete_option('updated_htaccess_success'); |
| 290 |
} |
| 291 |
|
| 292 |
$updated_htaccess_success = get_option('updated_htaccess_success', false); |
| 293 |
if ($updated_htaccess_success === true ) { |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
if ($htaccess_writable === true && $is_plugin_active ) { |
| 298 |
flush_rewrite_rules(); // re-trigger mod_rewrite_rules |
| 299 |
add_option('updated_htaccess_success', true); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Admin Notice |
| 305 |
*/ |
| 306 |
public function admin_notices() |
| 307 |
{ |
| 308 |
global $pagenow, $is_apache; |
| 309 |
|
| 310 |
if ($pagenow == 'plugins.php' || $pagenow == 'upload.php' ) { |
| 311 |
$is_htaccess_writable = $this->pda_function->htaccess_writable(); |
| 312 |
|
| 313 |
$plugin = plugin_basename(__FILE__); |
| 314 |
if ($is_apache && $is_htaccess_writable !== true && is_plugin_active($plugin) ) { |
| 315 |
?> |
| 316 |
<div class="error is-dismissible notice"> |
| 317 |
<p><b><?php echo esc_html__('Prevent Direct Access: ', 'prevent-direct-access'); ?></b> If your <b>.htaccess</b> |
| 318 |
file were writable, we could do this automatically, but it isn’t. So you must either make it |
| 319 |
writable or manually update your .htaccess with the mod_rewrite rules found under <b>Settings >> |
| 320 |
Permalinks</b>. Until then, the plugin can't work yet. </p> |
| 321 |
</div> |
| 322 |
<?php |
| 323 |
} |
| 324 |
|
| 325 |
if (is_plugin_active('json-rest-api/plugin.php') ) { |
| 326 |
// plugin is activated. |
| 327 |
?> |
| 328 |
<div class="error is-dismissible notice"> |
| 329 |
<p><b><?php esc_html_e("Prevent Direct Access: ", 'prevent-direct-access'); ?></b> You are using WP |
| 330 |
REST API. Please update to WordPress REST API (Version 2) |
| 331 |
(https://wordpress.org/plugins/rest-api/) </p> |
| 332 |
</div> |
| 333 |
<?php |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Get IP Block rewrite rules. |
| 340 |
* |
| 341 |
* @return string |
| 342 |
*/ |
| 343 |
public function get_ip_lock() |
| 344 |
{ |
| 345 |
$pda_settings_ip = get_option('FREE_PDA_SETTINGS_IP', false); |
| 346 |
$new_rule = ''; |
| 347 |
|
| 348 |
if (false === $pda_settings_ip || ! is_array($pda_settings_ip) ) { |
| 349 |
return $new_rule; |
| 350 |
} |
| 351 |
$str_ip_lock = $pda_settings_ip['ip_lock']; |
| 352 |
$arr_ip_lock = explode(";", $str_ip_lock); |
| 353 |
if ($arr_ip_lock[0] != null ) { |
| 354 |
// $newRule = "ORDER ALLOW,DENY". PHP_EOL; |
| 355 |
$ip = array( '*.*.*.*', '*.*.*', '*.*', '*' ); |
| 356 |
for ( $i = 0; $i < count($arr_ip_lock); $i ++ ) { |
| 357 |
// $newRule .= "DENY FROM ".str_replace($ip, '', $arr_ip_lock[$i]). PHP_EOL; |
| 358 |
$new_rule .= "RewriteCond %{REMOTE_ADDR} !^" . str_replace($ip, '', $arr_ip_lock[ $i ]) . PHP_EOL; |
| 359 |
} |
| 360 |
|
| 361 |
// $newRule .= "ALLOW FROM ALL". PHP_EOL; |
| 362 |
return $new_rule; |
| 363 |
} |
| 364 |
|
| 365 |
return $new_rule; |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Add rules in htaccess |
| 370 |
* |
| 371 |
* @param array $rules |
| 372 |
* |
| 373 |
* @return Mixed |
| 374 |
*/ |
| 375 |
public function htaccess_contents( $rules ) |
| 376 |
{ |
| 377 |
// If we don't check condition, when Gold enters license and flush the rules. It will run this function and add the Free rules. |
| 378 |
if (get_option('pda_is_licensed') && defined('PDA_GOLD_V3_VERSION') ) { |
| 379 |
return $rules; |
| 380 |
} |
| 381 |
|
| 382 |
// eg. index.php?pre_dir_acc_61co625547=$1 [R=301,L] |
| 383 |
$configs = Pda_Helper::get_plugin_configs(); |
| 384 |
$endpoint = $configs['endpoint']; |
| 385 |
$downloadFileRedirect = str_replace(trailingslashit(site_url()), '', 'index.php') . "?{$endpoint}=$1 [L]" . PHP_EOL; |
| 386 |
|
| 387 |
// $newRule .= "RewriteCond %{REMOTE_HOST} !^192.168.1.2$" . PHP_EOL; |
| 388 |
$newRule = $this->get_ip_lock(); |
| 389 |
$newRule .= "# Prevent Direct Access Rewrite Rules" . PHP_EOL; |
| 390 |
$newRule .= "RewriteRule private/([a-zA-Z0-9]+)$ " . $downloadFileRedirect; |
| 391 |
$newRule .= "RewriteCond %{REQUEST_FILENAME} -s" . PHP_EOL; |
| 392 |
$newRule .= "RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]" . PHP_EOL; |
| 393 |
$newRule .= "RewriteCond %{HTTP_USER_AGENT} !Twitterbot/[0-9]" . PHP_EOL; |
| 394 |
$newRule .= "RewriteCond %{HTTP_USER_AGENT} !Googlebot/[0-9]" . PHP_EOL; |
| 395 |
|
| 396 |
$directAccessPath = str_replace(trailingslashit(site_url()), '', 'index.php') . "?{$endpoint}=$1&is_direct_access=true&file_type=$2 [QSA,L]" . PHP_EOL; |
| 397 |
// eg. RewriteRule wp-content/uploads(/[a-zA-Z_\-\s0-9\.]+)+\.([a-zA-Z0-9]+)$ index.php?pre_dir_acc_61co625547=$1&is_direct_access=true&file_type=$2 [QSA,L] |
| 398 |
$upload_dir_url = str_replace("https", "http", wp_upload_dir()['baseurl']); |
| 399 |
$site_url = str_replace("https", "http", site_url()); |
| 400 |
$newRule .= "RewriteRule " . str_replace(trailingslashit($site_url), '', $upload_dir_url) . "/_pda" . "(\/[A-Za-z0-9_@.\/&+-]+)+\.([A-Za-z0-9_@.\/&+-]+)$ " . $directAccessPath; |
| 401 |
$newRule .= "# Prevent Direct Access Rewrite Rules End" . PHP_EOL; |
| 402 |
|
| 403 |
$hot_linking_rules = $this->generate_hot_linking_rules(); |
| 404 |
$newRule .= $hot_linking_rules . PHP_EOL; |
| 405 |
|
| 406 |
$option_index = $this->add_option_indexes_rule($rules); |
| 407 |
|
| 408 |
return $newRule . $rules . $option_index . PHP_EOL; |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Generate image hot linking rules for htaccess file follow feature "Prevent Image Hotlinking" in setting page |
| 413 |
* If enable feature "Prevent Image Hotlinking" => write rules |
| 414 |
* |
| 415 |
* @return string |
| 416 |
*/ |
| 417 |
private function generate_hot_linking_rules() |
| 418 |
{ |
| 419 |
$pda_settings = get_option(PDA_Lite_Constants::OPTION_NAME); |
| 420 |
$not_render_rules = false === $pda_settings || ! is_array($pda_settings) || ! array_key_exists('enable_image_hot_linking', $pda_settings) || 'on' !== $pda_settings['enable_image_hot_linking']; |
| 421 |
if ($not_render_rules ) { |
| 422 |
return ''; |
| 423 |
} |
| 424 |
|
| 425 |
$domain = home_url('/', is_ssl() ? 'https' : 'http'); |
| 426 |
$rules = array( |
| 427 |
'# Prevent Direct Access Prevent Hotlinking Rules', |
| 428 |
'RewriteCond %{HTTP_REFERER} !^$', |
| 429 |
"RewriteCond %{HTTP_REFERER} !^$domain [NC]", |
| 430 |
'RewriteRule \.(gif|jpg|jpeg|bmp|zip|rar|mp3|flv|swf|xml|png|css|pdf)$ - [F]', |
| 431 |
'# Prevent Direct Access Prevent Hotlinking Rules End', |
| 432 |
'', |
| 433 |
); |
| 434 |
|
| 435 |
return implode("\n", $rules); |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Add Index Rules |
| 440 |
* |
| 441 |
* @param string $rules |
| 442 |
* |
| 443 |
* @return string |
| 444 |
*/ |
| 445 |
private function add_option_indexes_rule( $rules ) |
| 446 |
{ |
| 447 |
$pda_settings_ip = get_option('FREE_PDA_SETTINGS'); |
| 448 |
|
| 449 |
$enable_directory_listing = is_array($pda_settings_ip) |
| 450 |
&& array_key_exists('enable_directory_listing', $pda_settings_ip) |
| 451 |
&& $pda_settings_ip['enable_directory_listing'] === 'on'; |
| 452 |
$option_index = strpos($rules, "Options -Indexes") === false && $enable_directory_listing ? "Options -Indexes" : ''; |
| 453 |
|
| 454 |
return $option_index; |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* Add Column |
| 459 |
* |
| 460 |
* @param array $columns |
| 461 |
* |
| 462 |
* @return string |
| 463 |
*/ |
| 464 |
public function add_upload_columns( $columns ) |
| 465 |
{ |
| 466 |
$is_htaccess_writable = $this->pda_function->htaccess_writable(); |
| 467 |
if ($is_htaccess_writable === true ) { |
| 468 |
$columns['direct_access'] = '<a href="#" status="true">Prevent Direct Access</a>'; |
| 469 |
} |
| 470 |
|
| 471 |
return $columns; |
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* Add Custom Column in media |
| 476 |
* |
| 477 |
* @param string $column_name |
| 478 |
* @param integer $id |
| 479 |
* |
| 480 |
* @return string |
| 481 |
*/ |
| 482 |
public function media_custom_columns( $column_name, $id ) |
| 483 |
{ |
| 484 |
$repository = new PDA_Repository; |
| 485 |
$post = get_post($id); |
| 486 |
// Change the logic to check whether the protected file which is not using the private links. |
| 487 |
$checked = $repository->is_protected_file($post->ID); |
| 488 |
$pda_class = $checked ? '' : PDA_Lite_Constants::PDA_LITE_CLASS_FOR_FILE_UNPROTECTED; |
| 489 |
$pda_icon = $checked ? '<i class="fa fa-check-circle" aria-hidden="true"></i>' : '<i class="fa fa-times-circle" aria-hidden="true"></i>'; |
| 490 |
$pda_text = $checked ? PDA_Lite_Constants::PDA_LITE_FILE_PROTECTED : PDA_Lite_Constants::PDA_LITE_FILE_UNPROTECTED; |
| 491 |
$title_text = $checked ? PDA_Lite_Constants::PDA_LITE_TITLE_FOR_FILE_PROTECTED : PDA_Lite_Constants::PDA_LITE_TITLE_FOR_FILE_UNPROTECTED; |
| 492 |
if ($column_name == 'direct_access' ) { |
| 493 |
?> |
| 494 |
<div id="pda-v3-column_<?php echo esc_attr($post->ID); ?>" class="pda-gold-v3-tools"> |
| 495 |
<p id="pda-v3-wrap-status_<?php echo esc_attr($post->ID); ?>"> |
| 496 |
<span id="pda-v3-text_<?php echo esc_attr($post->ID); ?>" |
| 497 |
class="protection-status <?php echo esc_attr($pda_class); ?>" |
| 498 |
title="<?php echo esc_attr($title_text); ?>"> |
| 499 |
<?php echo wp_kses_post( $pda_icon ); ?> |
| 500 |
<?php echo esc_html($pda_text); ?> |
| 501 |
</span> |
| 502 |
</p> |
| 503 |
<div> |
| 504 |
<a class="pda_gold_btn" |
| 505 |
id="pda_gold-<?php echo esc_attr( $post->ID ); ?>"><?php echo esc_html__('Configure file protection', 'prevent-direct-access') ?></a> |
| 506 |
</div> |
| 507 |
</div> |
| 508 |
<?php |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Add Upload Column |
| 514 |
* |
| 515 |
* @param string $columns |
| 516 |
* |
| 517 |
* @return string |
| 518 |
*/ |
| 519 |
public function free_add_upload_columns( $columns ) |
| 520 |
{ |
| 521 |
$columns['direct_access'] = __('Prevent Direct Access', 'prevent-direct-access'); |
| 522 |
|
| 523 |
return $columns; |
| 524 |
} |
| 525 |
|
| 526 |
/** |
| 527 |
* Check ajax Request |
| 528 |
*/ |
| 529 |
public function so_wp_ajax_function() |
| 530 |
{ |
| 531 |
if (! isset($_REQUEST['security_check'], $_POST['id'], $_POST['is_prevented']) ) { |
| 532 |
wp_die( |
| 533 |
esc_html__( 'Invalid data.', 'prevent-direct-access' ) |
| 534 |
); |
| 535 |
} |
| 536 |
|
| 537 |
if (! current_user_can('upload_files') ) { |
| 538 |
wp_die( |
| 539 |
esc_html__( 'You do not have permission to do this.', 'prevent-direct-access' ) |
| 540 |
); |
| 541 |
} |
| 542 |
|
| 543 |
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['security_check'] ) ); |
| 544 |
$post_id = absint($_POST['id']); |
| 545 |
if (! wp_verify_nonce($nonce, 'pda_ajax_nonce' . $post_id) ) { |
| 546 |
wp_die( |
| 547 |
esc_html__( 'invalid_nonce.', 'prevent-direct-access' ) |
| 548 |
); |
| 549 |
} |
| 550 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 551 |
$is_prevented = wp_validate_boolean( wp_unslash( $_POST['is_prevented'] ) ) ; |
| 552 |
$file_result = $this->insert_prevent_direct_access($post_id, $is_prevented); |
| 553 |
|
| 554 |
//move file to _pda |
| 555 |
$this->handle_move_file($post_id); |
| 556 |
|
| 557 |
wp_send_json($file_result); |
| 558 |
wp_die(); |
| 559 |
} |
| 560 |
|
| 561 |
/** |
| 562 |
* Handle to move file |
| 563 |
* |
| 564 |
* @param integer $post_id |
| 565 |
*/ |
| 566 |
public function handle_move_file( $post_id ) |
| 567 |
{ |
| 568 |
$handle = new Pda_Free_Handle(); |
| 569 |
$protected = new PDA_Repository; |
| 570 |
$protected_file = $protected->get_status_advance_file_by_post_id($post_id, true); |
| 571 |
if (isset($protected_file) && $protected_file->is_prevented === "1" ) { |
| 572 |
$handle->move_file_to_pda($post_id); |
| 573 |
} else { |
| 574 |
$handle->un_protect_file($post_id); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
/** |
| 579 |
* Check Nonce |
| 580 |
* |
| 581 |
* @param string $nonce |
| 582 |
* @param integer $post_id |
| 583 |
*/ |
| 584 |
public function check_nonce( $nonce, $post_id ) |
| 585 |
{ |
| 586 |
if (! wp_verify_nonce($nonce, 'pda_ajax_nonce' . $post_id) ) { |
| 587 |
wp_die( |
| 588 |
esc_html__( 'invalid_nonce.', 'prevent-direct-access' ) |
| 589 |
); |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
/** |
| 594 |
* Ajax Request for security check |
| 595 |
*/ |
| 596 |
public function so_wp_ajax_regenerate_url() |
| 597 |
{ |
| 598 |
if (! isset($_REQUEST['security_check'], $_POST['id']) ) { |
| 599 |
wp_die( |
| 600 |
esc_html__( 'Invalid data', 'prevent-direct-access' ) |
| 601 |
); |
| 602 |
} |
| 603 |
|
| 604 |
if (! current_user_can('upload_files') ) { |
| 605 |
wp_die( |
| 606 |
esc_html__( 'You do not have permission to do this.', 'prevent-direct-access' ) |
| 607 |
); |
| 608 |
} |
| 609 |
|
| 610 |
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['security_check'] ) ); |
| 611 |
$post_id = absint($_POST['id']); |
| 612 |
//$this->check_nonce($nonce, $post_id); |
| 613 |
if (! wp_verify_nonce($nonce, 'pda_ajax_nonce' . $post_id) ) { |
| 614 |
wp_die( |
| 615 |
esc_html__( 'invalid_nonce.', 'prevent-direct-access' ) |
| 616 |
); |
| 617 |
} |
| 618 |
|
| 619 |
$repository = new PDA_Repository; |
| 620 |
$result = $repository->update_private_link_by_post_id($post_id); |
| 621 |
if ($result < 1 || $result === false ) { |
| 622 |
$file_result = array( 'error' => "Cannot re-generate private link" ); |
| 623 |
} else { |
| 624 |
$file_result = $repository->get_advance_file_by_post_id($post_id); |
| 625 |
$file_result->url = site_url() . '/private/' . $file_result->url; |
| 626 |
} |
| 627 |
wp_send_json($file_result); |
| 628 |
wp_die(); |
| 629 |
} |
| 630 |
|
| 631 |
/** |
| 632 |
* Check file Limitation |
| 633 |
*/ |
| 634 |
public function is_file_limitation_over() |
| 635 |
{ |
| 636 |
|
| 637 |
$repository = new PDA_Repository; |
| 638 |
$limitation = $repository->check_advance_file_limitation(); |
| 639 |
$config = include 'includes/config.php'; |
| 640 |
|
| 641 |
return $limitation >= $config->ms; |
| 642 |
} |
| 643 |
|
| 644 |
/** |
| 645 |
* Show file limitation warning |
| 646 |
*/ |
| 647 |
public function is_file_limitation_to_show_warn() |
| 648 |
{ |
| 649 |
|
| 650 |
$repository = new PDA_Repository; |
| 651 |
$limitation = $repository->check_advance_file_limitation(); |
| 652 |
$config = include 'includes/config.php'; |
| 653 |
|
| 654 |
return $limitation >= $config->ms_warn; |
| 655 |
} |
| 656 |
|
| 657 |
/** |
| 658 |
* Check and Insert Prevent protection |
| 659 |
* |
| 660 |
* @param integer $post_id |
| 661 |
* @param boolean $is_prevented |
| 662 |
* |
| 663 |
* @return Mixed |
| 664 |
*/ |
| 665 |
public function insert_prevent_direct_access( $post_id, $is_prevented ) |
| 666 |
{ |
| 667 |
$repository = new PDA_Repository; |
| 668 |
$exist = Pda_Helper::does_url_exists($post_id); |
| 669 |
if (! $exist ) { |
| 670 |
$meta_value = $repository->get_post_meta_by_post_id($post_id)->meta_value; |
| 671 |
$upload_dir = wp_upload_dir(); |
| 672 |
$fileUrl = path_join($upload_dir['basedir'], $meta_value); |
| 673 |
|
| 674 |
return array( |
| 675 |
'error' => sprintf( |
| 676 |
// translators: %s is the full filesystem path of the missing file. |
| 677 |
__( 'The file %s does not exist.', 'prevent-direct-access' ), |
| 678 |
$fileUrl |
| 679 |
), |
| 680 |
); |
| 681 |
} |
| 682 |
if ($is_prevented && $this->is_file_limitation_over() ) { |
| 683 |
$file_result = array( |
| 684 |
'error' => __("Our Free version only allows you to protect up to 9 files. Please upgrade to the Gold version for many more premium features!", 'prevent-direct-access'), |
| 685 |
); |
| 686 |
} else { |
| 687 |
$file_info = array( |
| 688 |
'time' => current_time('mysql'), |
| 689 |
'post_id' => $post_id, |
| 690 |
'is_prevented' => $is_prevented, |
| 691 |
'url' => Pda_Helper::generate_unique_string(), |
| 692 |
); |
| 693 |
$result = $repository->create_advance_file($file_info); |
| 694 |
if ($result < 1 || $result === false ) { |
| 695 |
$file_result = array( |
| 696 |
'error' => __("This file is already protected. Please reload your page.", 'prevent-direct-access'), |
| 697 |
); |
| 698 |
} else { |
| 699 |
$file_result = $repository->get_advance_file_by_post_id($file_info['post_id']); |
| 700 |
$file_result->url = site_url() . '/private/' . $file_result->url; |
| 701 |
} |
| 702 |
if (is_multisite() ) { |
| 703 |
$file_result = array( |
| 704 |
'error' => __("This file is not protect with multisite.", 'prevent-direct-access'), |
| 705 |
); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
return $file_result; |
| 710 |
} |
| 711 |
|
| 712 |
/** |
| 713 |
* Delete file by post id |
| 714 |
* |
| 715 |
* @param integer $post_id |
| 716 |
*/ |
| 717 |
public function delete_prevent_direct_access( $post_id ) |
| 718 |
{ |
| 719 |
$repository = new PDA_Repository; |
| 720 |
$repository->delete_advance_file_by_post_id($post_id); |
| 721 |
} |
| 722 |
|
| 723 |
/** |
| 724 |
* Call when deactive plugin |
| 725 |
*/ |
| 726 |
public function deactivate() |
| 727 |
{ |
| 728 |
remove_filter('mod_rewrite_rules', array( $this, 'htaccess_contents' )); |
| 729 |
flush_rewrite_rules(); |
| 730 |
$this->db->remove_db_options(); |
| 731 |
} |
| 732 |
|
| 733 |
/** |
| 734 |
* Call at time of plugin install |
| 735 |
*/ |
| 736 |
public function plugin_install() |
| 737 |
{ |
| 738 |
flush_rewrite_rules(); |
| 739 |
$this->db->create_new_table(); |
| 740 |
} |
| 741 |
|
| 742 |
/** |
| 743 |
* Call at time of plugin uninstall |
| 744 |
*/ |
| 745 |
public static function plugin_uninstall() |
| 746 |
{ |
| 747 |
if (get_option('pda_is_licensed') ) { |
| 748 |
return; |
| 749 |
} |
| 750 |
|
| 751 |
Pda_Database::uninstall_static(); |
| 752 |
$un_protect = new PDA_Repository(); |
| 753 |
$un_protect->un_protect_files(); |
| 754 |
} |
| 755 |
|
| 756 |
/** |
| 757 |
* create new table and migrate data |
| 758 |
*/ |
| 759 |
public function create_new_table_and_migrate_data() |
| 760 |
{ |
| 761 |
if (get_option('pda_free_migrated') !== 'true' ) { |
| 762 |
$this->db->create_new_table(); |
| 763 |
$repository = new PDA_Repository; |
| 764 |
$repository->migrate_data_to_new_table(); |
| 765 |
update_option('pda_free_migrated', 'true'); |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
/** |
| 770 |
* Update post meta on pda subscribe |
| 771 |
*/ |
| 772 |
public function pda_ajax_subscribe() |
| 773 |
{ |
| 774 |
$check = check_ajax_referer('pda_subscribe', 'security_check'); |
| 775 |
if ($check == 1 ) { |
| 776 |
if ( isset( $_POST['action'] ) && sanitize_text_field( wp_unslash( $_POST['action'] ) ) === 'pda_subscribe' ) { |
| 777 |
$uid = get_current_user_id(); |
| 778 |
update_user_meta($uid, 'pda_subscribed', true); |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
/** |
| 784 |
* Add Setting Page menu |
| 785 |
*/ |
| 786 |
public function pda_add_settings_page() |
| 787 |
{ |
| 788 |
$pda_settings_page = add_menu_page(__('Prevent Direct Access', 'prevent-direct-access'), __('Prevent Direct Access', 'prevent-direct-access'), 'manage_options', 'wp_pda_options', null, 'dashicons-hidden'); |
| 789 |
$go_pro_icon = "<span style=\"color:#F44F45;\"><svg width=\"12\" height=\"12\" viewBox=\"0 0 1792 1792\" xmlns=\"http://www.w3.org/2000/svg\"><path fill=\"currentColor\" d=\"M1728 647q0 22-26 48l-363 354 86 500q1 7 1 20 0 21-10.5 35.5t-30.5 14.5q-19 0-40-12l-449-236-449 236q-22 12-40 12-21 0-31.5-14.5t-10.5-35.5q0-6 2-20l86-500-364-354q-25-27-25-48 0-37 56-46l502-73 225-455q19-41 49-41t49 41l225 455 502 73q56 9 56 46z\"/></svg></span>"; |
| 790 |
add_submenu_page( |
| 791 |
'wp_pda_options', __('Settings', 'prevent-direct-access'), __('Settings', 'prevent-direct-access'), 'manage_options', 'wp_pda_options', array( |
| 792 |
$this, |
| 793 |
'pda_options_do_page', |
| 794 |
) |
| 795 |
); |
| 796 |
$go_pro_page = add_submenu_page( |
| 797 |
'wp_pda_options', __('Go PRO', 'prevent-direct-access'), __('Go Pro ', 'prevent-direct-access') . $go_pro_icon, 'manage_options', 'wp_pda_gopro', array( |
| 798 |
$this, |
| 799 |
'pda_options_do_go_pro_page', |
| 800 |
) |
| 801 |
); |
| 802 |
add_action('admin_print_styles-' . $pda_settings_page, array( $this, 'pda_setting_pages' )); |
| 803 |
add_action('admin_print_styles-' . $go_pro_page, array( $this, 'pda_setting_go_pro' )); |
| 804 |
} |
| 805 |
|
| 806 |
/** |
| 807 |
* Enqueue JS and CSS on setting page |
| 808 |
*/ |
| 809 |
public function pda_setting_pages() |
| 810 |
{ |
| 811 |
wp_register_style('pda_setting_css', plugin_dir_url(__FILE__) . ( 'css/prevent-direct-access-lite-setting.css' ), array(), PDAF_VERSION); |
| 812 |
wp_enqueue_style('pda_setting_css'); |
| 813 |
wp_register_style('pda_rating_subscribe_css', plugin_dir_url(__FILE__) . ( 'css/prevent-direct-access-lite-rating-subscribe.css' ), array(), PDAF_VERSION); |
| 814 |
wp_enqueue_style('pda_rating_subscribe_css'); |
| 815 |
} |
| 816 |
|
| 817 |
/** |
| 818 |
* Register and Enqueue style on go pro page |
| 819 |
*/ |
| 820 |
public function pda_setting_go_pro() |
| 821 |
{ |
| 822 |
wp_register_style('pda_setting_go_pro_css', plugin_dir_url(__FILE__) . ( 'css/pda_setting_go_pro.css' ), array(), PDAF_VERSION); |
| 823 |
wp_enqueue_style('pda_setting_go_pro_css'); |
| 824 |
} |
| 825 |
|
| 826 |
/** |
| 827 |
* Render setting page |
| 828 |
*/ |
| 829 |
public function pda_options_do_page() |
| 830 |
{ |
| 831 |
$setting_page = new PDA_SettingsPage(); |
| 832 |
$setting_page->render_settings_page(); |
| 833 |
} |
| 834 |
|
| 835 |
/** |
| 836 |
* Render pro option on setting page |
| 837 |
*/ |
| 838 |
public function pda_options_do_go_pro_page() |
| 839 |
{ |
| 840 |
$setting_page = new PDA_SettingsPage(); |
| 841 |
$setting_page->render_go_pro_page(); |
| 842 |
} |
| 843 |
|
| 844 |
/** |
| 845 |
* Create custom robot text |
| 846 |
* |
| 847 |
* @param array $output |
| 848 |
* |
| 849 |
* @return Mixed |
| 850 |
*/ |
| 851 |
function pda_custom_robots_txt( $output ) |
| 852 |
{ |
| 853 |
$repository = new PDA_Repository; |
| 854 |
$protected_posts = $repository->get_protected_post(); |
| 855 |
$rules = "Disallow: /wp-includes/" . PHP_EOL . "Disallow: /wp-content/plugins/" . PHP_EOL; |
| 856 |
foreach ( $protected_posts as $post ) { |
| 857 |
$post_link = str_replace(site_url(), '', get_permalink($post)); |
| 858 |
$url = str_replace(site_url(), '', wp_get_attachment_url($post->ID)); |
| 859 |
$rules .= "Disallow: $post_link" . PHP_EOL; |
| 860 |
$rules .= "Disallow: $url" . PHP_EOL; |
| 861 |
} |
| 862 |
$output .= $rules . PHP_EOL; |
| 863 |
|
| 864 |
return $output; |
| 865 |
} |
| 866 |
|
| 867 |
/** |
| 868 |
* Check file is protected or not |
| 869 |
* |
| 870 |
* @param boolean $is_prevented |
| 871 |
* |
| 872 |
* @return bool |
| 873 |
*/ |
| 874 |
public function check_protected_file_yes_or_no( $is_prevented ) |
| 875 |
{ |
| 876 |
$protected = new PDA_Repository; |
| 877 |
$protected_true = $protected->get_protected_posts($is_prevented); |
| 878 |
|
| 879 |
return $protected_true; |
| 880 |
} |
| 881 |
|
| 882 |
/** |
| 883 |
* Modify the protected media |
| 884 |
* |
| 885 |
* @param array $post_object |
| 886 |
* |
| 887 |
* @return object |
| 888 |
*/ |
| 889 |
public function modify_protected_media( $post_object ) |
| 890 |
{ |
| 891 |
$protected = $this->check_protected_file_yes_or_no(1); |
| 892 |
$protected_files = array(); |
| 893 |
$un_protected_files = array(); |
| 894 |
for ( $i = 0; $i < count($post_object); $i ++ ) { |
| 895 |
if (in_array($post_object[ $i ]->ID, array_column($protected, 'post_id')) ) { |
| 896 |
array_push($protected_files, $post_object[ $i ]); |
| 897 |
} else { |
| 898 |
array_push($un_protected_files, $post_object[ $i ]); |
| 899 |
} |
| 900 |
} |
| 901 |
// Filtering results based on URL parameter only (no data processing). |
| 902 |
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 903 |
if (isset($_GET['protected_media']) ) { |
| 904 |
if ($_GET['protected_media'] == 1 ) { |
| 905 |
return $protected_files; |
| 906 |
} elseif ($_GET['protected_media'] == 2 ) { |
| 907 |
return $un_protected_files; |
| 908 |
} elseif ($_GET['protected_media'] == 0 ) { |
| 909 |
return $post_object; |
| 910 |
} |
| 911 |
} else { |
| 912 |
return $post_object; |
| 913 |
} |
| 914 |
// phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 915 |
} |
| 916 |
|
| 917 |
/** |
| 918 |
* Load Text Domain |
| 919 |
*/ |
| 920 |
public function pda_load_text_domain() |
| 921 |
{ |
| 922 |
// phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound |
| 923 |
load_plugin_textdomain('prevent-direct-access', false, basename(dirname(__FILE__)) . '/languages/'); |
| 924 |
} |
| 925 |
|
| 926 |
/** |
| 927 |
* Init Rest API |
| 928 |
*/ |
| 929 |
function pda_rest_api_init_cb() |
| 930 |
{ |
| 931 |
$api = new PDA_Lite_API(); |
| 932 |
$api->register_rest_routes(); |
| 933 |
} |
| 934 |
|
| 935 |
/** |
| 936 |
* Add Affiliate Submenu |
| 937 |
*/ |
| 938 |
function pda_add_affiliate_submenu() |
| 939 |
{ |
| 940 |
$setting_affiliate = new PDA_Lite_Affiliate(); |
| 941 |
|
| 942 |
add_submenu_page( |
| 943 |
'wp_pda_options', __('Invite & Earn', 'prevent-direct-access'), __('Invite & Earn', 'prevent-direct-access'), 'manage_options', PDA_Lite_Constants::AFFILIATE_PAGE_PREFIX, array( |
| 944 |
$setting_affiliate, |
| 945 |
'render_ui', |
| 946 |
) |
| 947 |
); |
| 948 |
} |
| 949 |
|
| 950 |
/** |
| 951 |
* Update General Setting |
| 952 |
*/ |
| 953 |
function pda_lite_update_general_settings() |
| 954 |
{ |
| 955 |
if (! isset($_REQUEST['settings'], $_REQUEST['security_check']) ) { |
| 956 |
wp_die( |
| 957 |
esc_html__( 'Invalid data', 'prevent-direct-access' ) |
| 958 |
); |
| 959 |
} |
| 960 |
|
| 961 |
if (! current_user_can('manage_options') ) { |
| 962 |
wp_die( |
| 963 |
esc_html__( 'You do not have permission to do this.', 'prevent-direct-access' ) |
| 964 |
); |
| 965 |
} |
| 966 |
|
| 967 |
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['security_check'] ) ); |
| 968 |
if (! wp_verify_nonce($nonce, 'pda_ajax_nonce_v3') ) { |
| 969 |
wp_die( |
| 970 |
esc_html__( 'invalid_nonce', 'prevent-direct-access' ) |
| 971 |
); |
| 972 |
} |
| 973 |
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 974 |
|
| 975 |
$settings = isset( $_REQUEST['settings'] ) && is_array( $_REQUEST['settings'] ) ? wp_unslash( $_REQUEST['settings'] ) : array(); |
| 976 |
$settings = array_map('sanitize_text_field', $settings); |
| 977 |
|
| 978 |
update_option( |
| 979 |
PDA_Lite_Constants::OPTION_NAME, array( |
| 980 |
'hide_protected_files_in_media' => array_key_exists('hide_protected_files_in_media', $settings) ? $settings['hide_protected_files_in_media'] : null, |
| 981 |
'disable_right_click' => array_key_exists('disable_right_click', $settings) ? $settings['disable_right_click'] : null, |
| 982 |
'enable_image_hot_linking' => array_key_exists('enable_image_hot_linking', $settings) ? $settings['enable_image_hot_linking'] : null, |
| 983 |
'search_result_page_404' => array_key_exists('search_result_page_404', $settings) ? $settings['search_result_page_404'] : null, |
| 984 |
'enable_directory_listing' => array_key_exists('enable_directory_listing', $settings) ? $settings['enable_directory_listing'] : null, |
| 985 |
'file_access_permission' => array_key_exists('file_access_permission', $settings) ? $settings['file_access_permission'] : '', |
| 986 |
) |
| 987 |
); |
| 988 |
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 989 |
/** |
| 990 |
* Add filter to write htaccess rules |
| 991 |
*/ |
| 992 |
add_filter('mod_rewrite_rules', array( $this, 'htaccess_contents' )); |
| 993 |
flush_rewrite_rules(); |
| 994 |
|
| 995 |
wp_send_json($settings); |
| 996 |
wp_die(); |
| 997 |
} |
| 998 |
|
| 999 |
/** |
| 1000 |
* Update IP Restriction Settings |
| 1001 |
*/ |
| 1002 |
public function pda_lite_update_ip_restriction_settings() |
| 1003 |
{ |
| 1004 |
if (! current_user_can('manage_options') ) { |
| 1005 |
return wp_send_json_error( |
| 1006 |
array( |
| 1007 |
'success' => false, |
| 1008 |
'message' => __( 'You do not have permission to do this.', 'prevent-direct-access' ), |
| 1009 |
), |
| 1010 |
403 |
| 1011 |
); |
| 1012 |
} |
| 1013 |
|
| 1014 |
$nonce = isset($_REQUEST['security_check']) ? sanitize_text_field( wp_unslash( $_REQUEST['security_check'] ) ) : false; |
| 1015 |
if (! $nonce || ! wp_verify_nonce($nonce, 'pda_ajax_nonce_v3') ) { |
| 1016 |
return wp_send_json_error( |
| 1017 |
array( |
| 1018 |
'success' => false, |
| 1019 |
'message' => __( 'Invalid nonce', 'prevent-direct-access' ), |
| 1020 |
), |
| 1021 |
400 |
| 1022 |
); |
| 1023 |
} |
| 1024 |
if (! isset($_POST['settings']) ) { |
| 1025 |
return wp_send_json_error( |
| 1026 |
array( |
| 1027 |
'success' => false, |
| 1028 |
'message' => __( 'IP does not exist', 'prevent-direct-access' ), |
| 1029 |
), |
| 1030 |
400 |
| 1031 |
); |
| 1032 |
} |
| 1033 |
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 1034 |
$settings = isset( $_POST['settings'] ) && is_array( $_POST['settings'] ) ? wp_unslash( $_POST['settings'] ) : array(); |
| 1035 |
$settings = array_map('sanitize_text_field', $settings); |
| 1036 |
|
| 1037 |
update_option('FREE_PDA_SETTINGS_IP', array( 'ip_lock' => $settings['pda_free_pl_blacklist_ips'] )); |
| 1038 |
|
| 1039 |
return wp_send_json_success( |
| 1040 |
array( |
| 1041 |
'success' => true, |
| 1042 |
) |
| 1043 |
); |
| 1044 |
} |
| 1045 |
|
| 1046 |
/** |
| 1047 |
* Ajax Request for PDA free subscribe |
| 1048 |
*/ |
| 1049 |
function pda_ajax_pda_free_subscribe() |
| 1050 |
{ |
| 1051 |
if (! isset($_POST['email']) ) { |
| 1052 |
return wp_send_json_error( |
| 1053 |
array( |
| 1054 |
'success' => false, |
| 1055 |
'message' => 'Email is required', |
| 1056 |
), |
| 1057 |
400 |
| 1058 |
); |
| 1059 |
} |
| 1060 |
$check = check_ajax_referer('pda_free_subscribe', 'security_check'); |
| 1061 |
if ( $check == 1 ) { |
| 1062 |
if ( isset( $_POST['action'] ) && sanitize_text_field( wp_unslash( $_POST['action'] ) ) === 'pda_free_subscribe' ) { |
| 1063 |
$data = array( |
| 1064 |
'email' => sanitize_email( wp_unslash( $_POST['email'] ) ), |
| 1065 |
'plugin' => 'pda', |
| 1066 |
); |
| 1067 |
$args = array( |
| 1068 |
'body' => json_encode($data), |
| 1069 |
'timeout' => '100', |
| 1070 |
'redirection' => '5', |
| 1071 |
'httpversion' => '1.0', |
| 1072 |
'blocking' => true, |
| 1073 |
'headers' => array( |
| 1074 |
'x-api-key' => 'SUDi1LNlAv3T2nQ4QIX9Sadtr7Ghg9UD1PnHvyWe', |
| 1075 |
'Content-Type' => 'application/json', |
| 1076 |
), |
| 1077 |
); |
| 1078 |
$response = wp_remote_post( |
| 1079 |
'https://loks4vx5i7.execute-api.ap-southeast-1.amazonaws.com/prod/mail', |
| 1080 |
$args |
| 1081 |
); |
| 1082 |
if (is_wp_error($response) ) { |
| 1083 |
$result['message'] = $response->get_error_message(); |
| 1084 |
} else { |
| 1085 |
$result['data'] = json_decode(wp_remote_retrieve_body($response)); |
| 1086 |
$uid = get_current_user_id(); |
| 1087 |
update_user_meta($uid, 'pda_free_subscribe', true); |
| 1088 |
} |
| 1089 |
|
| 1090 |
return $result; |
| 1091 |
} |
| 1092 |
|
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
/** |
| 1097 |
* Show admin notices to remind user to upgrade PDA Gold because PDA Free is only supported single site. |
| 1098 |
*/ |
| 1099 |
public function multisite_admin_notices() |
| 1100 |
{ |
| 1101 |
global $pagenow; |
| 1102 |
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 1103 |
if ($pagenow !== 'plugins.php' |
| 1104 |
&& $pagenow !== 'upload.php' |
| 1105 |
&& (!isset($_GET['page']) |
| 1106 |
|| $_GET['page'] !== 'wp_pda_options' ) |
| 1107 |
) { |
| 1108 |
return; |
| 1109 |
} |
| 1110 |
// phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 1111 |
$plugin_name = 'Prevent Direct Access'; |
| 1112 |
|
| 1113 |
$message = sprintf( |
| 1114 |
// translators: %1$s is the URL to the pricing page used in the upgrade link. |
| 1115 |
__(': Our PDA Lite only supports WordPress single site. Please <a target="_blank" rel="noopener" href="%1$s">upgrade to Gold version</a> for our file protection to work properly.', 'prevent-direct-access'), |
| 1116 |
sprintf( |
| 1117 |
constant('PDA_PRICING_PAGE'), 'notification', 'notification-link' |
| 1118 |
) |
| 1119 |
); |
| 1120 |
?> |
| 1121 |
<div class="error is-dismissible notice"> |
| 1122 |
<p> |
| 1123 |
<b><?php echo esc_html($plugin_name); ?></b><?php echo wp_kses_post($message); ?> |
| 1124 |
</p> |
| 1125 |
</div> |
| 1126 |
<?php |
| 1127 |
} |
| 1128 |
|
| 1129 |
/** |
| 1130 |
* Prevent right-click |
| 1131 |
*/ |
| 1132 |
function prevent_right_click() |
| 1133 |
{ |
| 1134 |
$pda_option = get_option('FREE_PDA_SETTINGS'); |
| 1135 |
$is_admin = is_admin(); |
| 1136 |
$is_administrator = current_user_can( 'administrator' ); |
| 1137 |
$is_editor = current_user_can( 'editor' ); |
| 1138 |
if (is_array($pda_option) && array_key_exists('disable_right_click', $pda_option) && $pda_option['disable_right_click'] === "on" && !$is_admin && !$is_administrator && !$is_editor ) { |
| 1139 |
|
| 1140 |
$noscript_message = apply_filters('pda_noscript_message', 'Please enable JavaScript in your browser to view the content'); |
| 1141 |
$disable_right_click_message = apply_filters('pda_disable_right_click_message', __( 'Right-click is disabled' , 'prevent-direct-access')); |
| 1142 |
$show_alert = apply_filters('pda_show_alert_on_right_clicks', true); |
| 1143 |
$developer_tools_message = apply_filters('pda_disable_developer_tools_message', __('Please close the developer tools to continue using this site', 'prevent-direct-access')); |
| 1144 |
$disable_developer_tools_message = apply_filters('pda_disable_developer_tools', true); |
| 1145 |
$pda_disable_tool_tipe = apply_filters('pda_disable_tool_tipe', true); |
| 1146 |
$is_mobile = wp_is_mobile(); |
| 1147 |
do_action('pda_lite_disable_right_click_on_iframe'); |
| 1148 |
|
| 1149 |
?> |
| 1150 |
|
| 1151 |
<noscript> |
| 1152 |
<div style="position: fixed; top: 0px; left: 0px; z-index: 30000000; |
| 1153 |
height: 100%; width: 100%; background-color: #FFFFFF"> |
| 1154 |
<p style="margin-left: 10px"><?php echo esc_html( $noscript_message ); ?></p> |
| 1155 |
</div> |
| 1156 |
</noscript> |
| 1157 |
|
| 1158 |
<script> |
| 1159 |
|
| 1160 |
const show_alert = <?php echo esc_js( wp_json_encode( $show_alert ) ); ?>; |
| 1161 |
const pda_disable_tool_tipe = <?php echo esc_js( wp_json_encode( $pda_disable_tool_tipe ) ); ?>; |
| 1162 |
const disable_developer_tools_message = <?php echo esc_js( wp_json_encode( $disable_developer_tools_message ) ); ?>; |
| 1163 |
var is_mobile = <?php echo esc_js( wp_json_encode( $is_mobile ) ); ?>; |
| 1164 |
|
| 1165 |
|
| 1166 |
document.addEventListener('contextmenu', function (event) { |
| 1167 |
|
| 1168 |
if ( show_alert ) { |
| 1169 |
|
| 1170 |
event.preventDefault(); |
| 1171 |
if( pda_disable_tool_tipe ){ |
| 1172 |
|
| 1173 |
const existingMessage = document.querySelector('.custom-context-message'); |
| 1174 |
if (existingMessage) { |
| 1175 |
existingMessage.remove(); |
| 1176 |
} |
| 1177 |
|
| 1178 |
const message = document.createElement('div'); |
| 1179 |
message.className = 'custom-context-message'; |
| 1180 |
message.textContent = '<?php echo esc_attr( $disable_right_click_message ); ?>'; |
| 1181 |
|
| 1182 |
// Style the message |
| 1183 |
Object.assign(message.style, { |
| 1184 |
position: 'absolute', |
| 1185 |
top: `${event.clientY + window.scrollY}px`, |
| 1186 |
left: `${event.clientX + window.scrollX}px`, |
| 1187 |
backgroundColor: 'rgba(0, 0, 0, 0.8)', |
| 1188 |
color: '#fff', |
| 1189 |
padding: '5px 10px', |
| 1190 |
borderRadius: '5px', |
| 1191 |
zIndex: '1000', |
| 1192 |
pointerEvents: 'none', // Prevent interference with the user’s actions |
| 1193 |
fontSize: '14px', |
| 1194 |
}); |
| 1195 |
|
| 1196 |
|
| 1197 |
document.body.appendChild(message); |
| 1198 |
setTimeout(() => { |
| 1199 |
message.remove(); |
| 1200 |
}, 2000); |
| 1201 |
|
| 1202 |
} |
| 1203 |
|
| 1204 |
} |
| 1205 |
}); |
| 1206 |
|
| 1207 |
|
| 1208 |
if( disable_developer_tools_message ){ |
| 1209 |
|
| 1210 |
// Detect if the DevTools is open |
| 1211 |
(function detectDevTools() { |
| 1212 |
const threshold = 160; // Minimum height of the DevTools panel |
| 1213 |
const devtools = { |
| 1214 |
open: false, |
| 1215 |
orientation: null, |
| 1216 |
}; |
| 1217 |
|
| 1218 |
const check = () => { |
| 1219 |
const widthThreshold = window.outerWidth - window.innerWidth > threshold; |
| 1220 |
const heightThreshold = window.outerHeight - window.innerHeight > threshold; |
| 1221 |
const orientation = widthThreshold ? 'vertical' : 'horizontal'; |
| 1222 |
|
| 1223 |
if ( |
| 1224 |
!(heightThreshold && widthThreshold) && |
| 1225 |
((window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized) || |
| 1226 |
widthThreshold || |
| 1227 |
heightThreshold) |
| 1228 |
) { |
| 1229 |
if (!devtools.open || devtools.orientation !== orientation) { |
| 1230 |
devtools.open = true; |
| 1231 |
devtools.orientation = orientation; |
| 1232 |
document.addEventListener('keydown', function (e) { |
| 1233 |
if ( |
| 1234 |
e.key === "F12" || // Block F12 key |
| 1235 |
(e.ctrlKey && e.shiftKey && (e.key === 'I' || e.key === 'C' || e.key === 'J' || e.key === 'U')) // Block Ctrl+Shift+I, Ctrl+Shift+C, Ctrl+Shift+J, Ctrl+Shift+U |
| 1236 |
|| e.key === 'Escape' |
| 1237 |
) { |
| 1238 |
e.preventDefault(); |
| 1239 |
} |
| 1240 |
}); |
| 1241 |
|
| 1242 |
if ( !is_mobile ) { // Check the is mobile device or not. |
| 1243 |
|
| 1244 |
alert('<?php echo esc_js($developer_tools_message); ?>'); |
| 1245 |
window.location.reload(); // Optionally, reload the page if DevTools is detected |
| 1246 |
} |
| 1247 |
|
| 1248 |
} |
| 1249 |
} else { |
| 1250 |
devtools.open = false; |
| 1251 |
devtools.orientation = null; |
| 1252 |
} |
| 1253 |
}; |
| 1254 |
|
| 1255 |
setInterval(check, 500); // Check every 500ms |
| 1256 |
})(); |
| 1257 |
|
| 1258 |
document.onkeydown = function (e) { |
| 1259 |
// disable F12 key |
| 1260 |
if(e.keyCode == 123) { |
| 1261 |
return false; |
| 1262 |
} |
| 1263 |
var ctrlOrMeta = e.ctrlKey || e.metaKey; |
| 1264 |
|
| 1265 |
// disable I key |
| 1266 |
if(ctrlOrMeta && e.shiftKey && e.keyCode == 73){ |
| 1267 |
return false; |
| 1268 |
} |
| 1269 |
|
| 1270 |
// disable J key |
| 1271 |
if(ctrlOrMeta && e.shiftKey && e.keyCode == 74) { |
| 1272 |
return false; |
| 1273 |
} |
| 1274 |
|
| 1275 |
// disable P key |
| 1276 |
if(ctrlOrMeta && e.keyCode == 80) { |
| 1277 |
return false; |
| 1278 |
} |
| 1279 |
|
| 1280 |
// disable S key |
| 1281 |
if(ctrlOrMeta && e.keyCode == 83) { |
| 1282 |
return false; |
| 1283 |
} |
| 1284 |
|
| 1285 |
// disable U key |
| 1286 |
if(ctrlOrMeta && e.keyCode == 85) { |
| 1287 |
return false; |
| 1288 |
} |
| 1289 |
|
| 1290 |
// disable D key |
| 1291 |
if((ctrlOrMeta && e.keyCode == 68) || (e.altKey && e.keyCode == 68)) { |
| 1292 |
return false; |
| 1293 |
} |
| 1294 |
|
| 1295 |
// disable F key |
| 1296 |
if(ctrlOrMeta && e.keyCode == 70) { |
| 1297 |
return false; |
| 1298 |
} |
| 1299 |
|
| 1300 |
// disable G key |
| 1301 |
if(ctrlOrMeta && e.keyCode == 71) { |
| 1302 |
return false; |
| 1303 |
} |
| 1304 |
|
| 1305 |
// disable F3 key |
| 1306 |
if (e.keyCode == 114) { |
| 1307 |
return false; |
| 1308 |
} |
| 1309 |
|
| 1310 |
// disable shift + F3 key |
| 1311 |
if (e.shiftKey && e.keyCode == 114) { |
| 1312 |
return false; |
| 1313 |
} |
| 1314 |
|
| 1315 |
if(ctrlOrMeta && e.shiftKey && e.keyCode == 71) { |
| 1316 |
return false; |
| 1317 |
} |
| 1318 |
} |
| 1319 |
|
| 1320 |
} |
| 1321 |
|
| 1322 |
</script> |
| 1323 |
<style> |
| 1324 |
/* Disable select text */ |
| 1325 |
* { |
| 1326 |
-webkit-touch-callout: none; /* Safari */ |
| 1327 |
-webkit-user-select: none; /* Chrome */ |
| 1328 |
-moz-user-select: none; /* Firefox */ |
| 1329 |
-ms-user-select: none; /* Internet Explorer/Edge */ |
| 1330 |
user-select: none; |
| 1331 |
} |
| 1332 |
|
| 1333 |
input, textarea { |
| 1334 |
-webkit-touch-callout: unset; /* Safari */ |
| 1335 |
-webkit-user-select: text; /* Chrome */ |
| 1336 |
-moz-user-select: text; /* Firefox */ |
| 1337 |
-ms-user-select: text; /* Internet Explorer/Edge */ |
| 1338 |
user-select: text; |
| 1339 |
} |
| 1340 |
|
| 1341 |
/* Disable drag image */ |
| 1342 |
img { |
| 1343 |
-webkit-user-drag: none; |
| 1344 |
-khtml-user-drag: none; |
| 1345 |
-moz-user-drag: none; |
| 1346 |
-o-user-drag: none; |
| 1347 |
user-drag: none; |
| 1348 |
} |
| 1349 |
</style> |
| 1350 |
<?php |
| 1351 |
} |
| 1352 |
} |
| 1353 |
} |
| 1354 |
$pda_admin = new Pda_Admin(); |
| 1355 |
|
| 1356 |
|
| 1357 |
/* Plugin Analytics Data */ |
| 1358 |
function wpfolio_pda_analytics_load() { |
| 1359 |
|
| 1360 |
require_once dirname( __FILE__ ) . '/wpfolio-analytics/wpfolio-analytics.php'; |
| 1361 |
|
| 1362 |
$wpfolio_analytics = wpfolio_pda_anylc_init_module( array( |
| 1363 |
'id' => 9, |
| 1364 |
'file' => plugin_basename( __FILE__ ), |
| 1365 |
'name' => 'Prevent Direct Access', |
| 1366 |
'slug' => 'wp_pda_options', |
| 1367 |
'tempslug' => 'wp_pda_options_optin', |
| 1368 |
'type' => 'plugin', |
| 1369 |
'menu' => 'wp_pda_options', |
| 1370 |
'redirect_page' => 'wp_pda_options', |
| 1371 |
'text_domain' => 'prevent-direct-access', |
| 1372 |
)); |
| 1373 |
|
| 1374 |
return $wpfolio_analytics; |
| 1375 |
} |
| 1376 |
|
| 1377 |
// Init Analytics |
| 1378 |
wpfolio_pda_analytics_load(); |
| 1379 |
|
| 1380 |
require_once __DIR__ . '/includes/admin/class-pda-deactivation-feedback.php'; |
| 1381 |
|
| 1382 |
?> |