<?php 
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Plugin Name: EZee Copyright Protector
* Plugin URI: https://myplugins.net/
* Description: Prevent copyright infringement on your site. People cannot copy your site content. 
* Version: 1.0.1
* Author: Pratyush Deb
* Text Domain: ezee-cpyright-protector
* Domain Path: /languages
**/
$ezCpyRightProtector = new EZeeCpyrightProtector();

class EZeeCpyrightProtector
{
	public function __construct()
	{
		add_action('wp_head', array($this, 'ezEnqueueStyles'));
		add_action('wp_enqueue_scripts', array($this, 'ezEnqueueScripts'));
		add_action('wp_footer', array($this, 'ezPopupBoxHTML'));
		
		add_action('admin_menu', array($this, 'ezAdminSectionCallback'));
		add_action('plugins_loaded', array($this, 'ezCopyrightPrtLoadTextDomain'));
		add_filter('plugin_action_links_'.plugin_basename(__FILE__), array($this, 'ezCopyrightPrtctSettingLink'));
		register_activation_hook( __FILE__, array($this, 'ezCopyrightPrtActivate'));
		register_uninstall_hook( __FILE__, array('EZeeCpyrightProtector', 'ezCopyrightPrtUninstall')); //
		add_action('admin_post', array($this, 'ezCopyrightAdminRedirects'));
	}
	
	public function ezEnqueueStyles()
	{
		wp_enqueue_style("jquery-ui-css", plugins_url('css/themes/redmond/jquery-ui.min.css', __FILE__)); //include css locally
		wp_enqueue_style("ezee-custom-ui-css", plugins_url('css/style.css', __FILE__));
	}
	
	public function ezEnqueueScripts()
	{
		if ( ! wp_script_is( 'jquery'))
		{
			//Enqueue
			wp_enqueue_script( 'jquery' ); 
		}
	
        wp_enqueue_script('jquery-ui-core');
        wp_enqueue_script('jquery-ui-widget');
        wp_enqueue_script('jquery-ui-mouse');
        wp_enqueue_script('jquery-ui-dialog');
        wp_enqueue_script('jquery-ui-button');
		
		wp_enqueue_script('jquery-ui-position');
		wp_enqueue_script( "ezee_custom", plugins_url("ezee-cpyright-protector/js/custom.js", dirname(__FILE__)), array('jquery'), '1.0', true );
	}
	
	public function ezPopupBoxHTML()
	{
		$cpy_prt_option = get_option("ezee-cpyright-protector-settings");
	
		$cpy_prt_option = sanitize_option( "ezee-cpyright-protector-settings", $cpy_prt_option ); //sanitize retrieved option
		
		$cpt_prt_array = $cpy_prt_option;
		
		$ezcontent = $this->ezCopyrightPrtContent($cpt_prt_array["text"]);
		
		$title = $cpt_prt_array["title"];
		
		/*****July 11 2020****/
		$ezee_version_flag = version_compare(get_bloginfo('version'),'4.1', '<');
		
		wp_localize_script("ezee_custom", "ezee_version", array("ezee_version_flag"=>$ezee_version_flag));
		
		/****July 11 2020 end****/
		
		?>
		<div id="ezee_cprt_protector_dialog-message" title="<?php echo esc_attr($title); //escape attribute value ?>">
		<?php $ezcontent = wp_check_invalid_utf8( $ezcontent, true );
			
			$ezallowed_html = array(
								  "iframe"=>array("src"=>array(), 
													"width"=>array(), 
													"height"=>array(), 
													"border"=>array(), 
													"class"=>array(),
													"name"=>array(),
													"id"=>array()),
								  "img"=>array("src"=>array(), 
												"alt"=>array(), 
												"width"=>array(), 
												"height"=>array(), 
												"class"=>array(),
												"id"=>array()),
								  "audio"=>array("width"=>array(), 
												 "height"=>array(), 
												 "controls"=>array("yes")),
								  "source"=>array("src"=>array(), 
												"type"=>array("video/mp4",
															  "video/m4v",
															  "video/webm",
															  "video/ogv",
															  "video/x-ms-wmv",
															  "video/flv", 
															  "audio/ogg", 
															  "audio/mp3",
															  "audio/m4a",
															  "audio/wav",
															  "audio/wma") 
												)
									); 
												
												
			if(version_compare(get_bloginfo('version'),'5.3', '<') )
			{
				$ezallowed_html["video"] = array("width"=>array(), 
												"height"=>array(), 
												"controls"=>array("yes"));
			}
			
			$ezallowed_html = array_merge(wp_kses_allowed_html("post"), $ezallowed_html);
			
			$ezcontent = wp_kses($ezcontent, $ezallowed_html, array("http", "https")); //allowed html only
			
			echo $ezcontent;
			
				?>
		<div>
		<?php
	}
	
	private function ezCopyrightPrtContent($encoded_html)
	{
		$content = str_replace('\r', '', $encoded_html);
	
		$content = str_replace('\n', '', $content);
		
		$content = stripslashes($content);
		
		$content = apply_filters('the_content', $content);
		
		return $content;
	}
	
	public function ezAdminSectionCallback()
	{
		add_menu_page("EZee Copyright Protector", "EZee Copyright Protector", "administrator", "ezee-cpyright-protector", array($this, "ezAdminBackend"), plugins_url('images/copyright.png', __FILE__));
	
		add_options_page('EZee Copyright Protector', 'EZee Copyright Protector', 'manage_options', 'ezee-cpyright-protector', array($this, "ezAdminBackend"));
	}
	
	public function ezAdminBackend()
	{
		@session_start();
		$settings = array( 'textarea_name' => 'copyrt_notice_text', 'width'=>'500px' );
		$content = "";
		
		$validate_msg=array();
		
		if(!empty($_POST["ezee_cpy_prt_save"]))
		{
			
			if ( ! isset( $_POST['ezee_cpyright_protector_nonce'] ) || ! wp_verify_nonce( $_POST['ezee_cpyright_protector_nonce'], 'save_ezee_cpyright_protector_settings' ) )
			{
				wp_die(__("Form not verified", "ezee-cpyright-protector"));
			}
			
			$copyrt_notice_title = sanitize_text_field($_POST["copyrt_notice_title"]); //sanitization
		
			$copyrt_notice_text = wp_kses_post($_POST["copyrt_notice_text"]); //can't use sanitize_textarea_field as it removes everything including image tags 
			
			$copyrt_notice_text = wp_check_invalid_utf8( $copyrt_notice_text, true );
			
			
			if(empty($copyrt_notice_title))
			{
				$validate_msg[] = __("You must give a heading!", "ezee-cpyright-protector");
			}
			
			if(empty($copyrt_notice_text))
			{
				$validate_msg[] = __("You must give a text!", "ezee-cpyright-protector");
			}
			
			if(empty($validate_msg))
			{
				
				$cpy_protect_settings = array("title"=>$copyrt_notice_title, "text"=>$copyrt_notice_text);
				
				update_option("ezee-cpyright-protector-settings", $cpy_protect_settings);
				
				$_SESSION["success"] = "<div class='updated notice is-dismissible'><p>".__("Settings saved!", "ezee-cpyright-protector")."</p></div>";
				
			}
		}
		
		$cpy_prt_option = get_option("ezee-cpyright-protector-settings");
		
		$cpy_prt_option = sanitize_option("ezee-cpyright-protector-settings", $cpy_prt_option );
		
		$cpy_prt_array = $cpy_prt_option;
		
		$content = stripslashes($cpy_prt_array["text"]);
		
		$title = $cpy_prt_array["title"];
		
		?><h1><?php echo __("EZee Copyright Protector Settings", "ezee-cpyright-protector"); ?></h1>
	<?php if(isset($_SESSION["success"])){ echo $_SESSION["success"]; unset($_SESSION["success"]); } ?>
	<?php if(is_array($validate_msg)){ 
				foreach($validate_msg as $msg)
				{
					?>
					<div class="error notice"><p><?php echo $msg; ?></p></div>
					<?php
				}
			}		?>
	<div id="wpwrap">
	<div class="wrap">
	<form action="" method="POST" >
	<?php wp_nonce_field( 'save_ezee_cpyright_protector_settings', 'ezee_cpyright_protector_nonce' ); ?>
	<table class="form-table" border="0" >
	<tr><td><?php echo __("Copyright Notice Title", "ezee-cpyright-protector"); ?></td><td><input type="text" name="copyrt_notice_title" size="50" value="<?php echo $title; ?>" maxlength="200" /></td></tr>
	<tr><td><?php echo __("Copyright Notice Body", "ezee-cpyright-protector"); ?></td><td><?php wp_editor($content , "ezeeCopyPrtctText1", $settings ); //$content?><!----></td></tr>
	<!---->
	<tr><td><input type="submit" name="ezee_cpy_prt_save" value="<?php echo __("Save", "ezee-cpyright-protector"); ?>" class="button button-primary" /></td>
	<td>&nbsp;</td></tr>
	</table>
	</form>
	</div>
	</div>
<?php
	}
	
	public function ezCopyrightPrtLoadTextDomain(){
		load_plugin_textdomain( 'ezee-cpyright-protector', false, basename( dirname( __FILE__ ) ) . '/languages' ); 
	}

	public function ezCopyrightAdminRedirects()
	{
		global $pagenow;

		/* Redirect */
		if($pagenow == 'admin.php'){
			
			if(!empty($_POST["ezee_cpy_prt_save"]) && $_POST["ezee_cpy_prt_save"] == "Save")
			{
				
				wp_redirect(admin_url('/admin.php?page=ezee-cpyright-protector'));
				exit;
			}
		}
		else if($pagenow == "options-general.php")
		{
			if(!empty($_POST["ezee_cpy_prt_save"]) && $_POST["ezee_cpy_prt_save"] == "Save")
			{
				
				wp_redirect(admin_url('/options-general.php?page=ezee-cpyright-protector'));
				exit;
			}
		}
	}
	
	public function ezCopyrightPrtctSettingLink( $links ) {
		$links[] = '<a href="'.
			admin_url( 'options-general.php?page=ezee-cpyright-protector' ).
			'">'.__('Settings').'</a>';
			
		return $links;
	}
	
	public static function ezCopyrightPrtUninstall() //must be static for call to register_uninstall_hook!
	{
		//  codes to perform during unistallation
		
		delete_option("ezee-cpyright-protector-settings");  //delete plugin data
		
	}
	
	public function ezCopyrightPrtActivate()
	{
		
		$default_settings_array = array("title"=>"Copyright Notice!", "text"=>"Please do not infringe copyright!");
		
		foreach($default_settings_array as $key=>$value)
		{
			$default_settings_array[sanitize_text_field($key)] = sanitize_text_field($value);
		}
		
		if(get_option("ezee-cpyright-protector-settings") == false)
		{
			update_option("ezee-cpyright-protector-settings", $default_settings_array);
		}
	}
	
}