<?php

/*
Plugin Name: OptinLinks
Plugin URI: http://www.optinlinks.com
Description: The world's most powerful list-building plugin.
Version: 1.1.0
Author:  Don Westendorp, P. Dylan Ross - StrongMRKTG LLC
Author URI: http://www.optinlinks.com
License: OptinLinks End User License Agreement
*/

/*  Copyright 2014  StrongMRKTG LLC (email : support@optinlinks.com)

END USER LICENSE AGREEMENT

This copy of OptinLinks ("the Software Product") and accompanying documentation is licensed and not sold. This Software Product is protected by copyright laws and treaties, as well as laws and treaties related to other forms of intellectual property. StrongMRKTG LLCor its subsidiaries, affiliates, and suppliers (collectively "Strong MRKTG") own intellectual property rights in the Software Product. The Licensee's ("you" or "your") license to download, use, copy, or change the Software Product is subject to these rights and to all the terms and conditions of this End User License Agreement ("Agreement").

Acceptance
YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS AGREEMENT BY SELECTING THE "ACCEPT" OPTION AND DOWNLOADING THE SOFTWARE PRODUCT OR BY INSTALLING, USING, OR COPYING THE SOFTWARE PRODUCT. YOU MUST AGREE TO ALL OF THE TERMS OF THIS AGREEMENT BEFORE YOU WILL BE ALLOWED TO DOWNLOAD THE SOFTWARE PRODUCT. IF YOU DO NOT AGREE TO ALL OF THE TERMS OF THIS AGREEMENT, YOU MUST SELECT "DECLINE" AND YOU MUST NOT INSTALL, USE, OR COPY THE SOFTWARE PRODUCT.

License Grant
This Agreement entitles you to install and use unliimited copies of the Software Product on web domains that you own. In addition, you may make one archival copy of the Software Product. The archival copy must be on a storage medium other than a hard drive, and may only be used for the reinstallation of the Software Product.  Multiple copy use or installation is only allowed on web sites and web domains that you own. If you wish to install this software on to a web site owned by another person or business you must obtain a license for them on an individual basis.

Restrictions on Transfer
Without first obtaining the express written consent of Strong MRKTG, you may not assign your rights and obligations under this Agreement, or redistribute, encumber, sell, rent, lease, sublicense, or otherwise transfer your rights to the Software Product.

Restrictions on Alteration
You may not create any derivative work of the Software Product or its accompanying documentation. Derivative works include but are not limited to translations. You may modify the software to suit your own personal needs, but you may not redistribute these changes without written consent from Strong MRKTG.

Restrictions on Copying
You may not copy any part of the Software Product except to the extent that licensed use inherently demands the creation of a temporary copy stored in computer memory and not permanently affixed on storage medium. You may make one archival copy which must be stored on a medium other than a computer hard drive.

Limited Software Product Warranty
For a period of 45 days from the date of shipment or from the date that you download the Software Product, as applicable, Strong MRKTG warrants that when properly installed and used under normal conditions, the Software Product will perform substantially as advertised.

Disclaimer of Warranties and Limitation of Liability
UNLESS OTHERWISE EXPLICITLY AGREED TO IN WRITING BY STRONG MRKTG, STRONG MRKTG MAKES NO OTHER WARRANTIES, EXPRESS OR IMPLIED, IN FACT OR IN LAW, INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OTHER THAN AS SET FORTH IN THIS AGREEMENT OR IN THE LIMITED WARRANTY DOCUMENTS PROVIDED WITH THE SOFTWARE PRODUCT.

STRONG MRKTG makes no warranty that the Software Product will meet your requirements or operate under your specific conditions of use. STRONG MRKTG makes no warranty that operation of the Software Product will be secure, error free, or free from interruption. YOU MUST DETERMINE WHETHER THE SOFTWARE PRODUCT SUFFICIENTLY MEETS YOUR REQUIREMENTS FOR SECURITY AND UNINTERRUPTABILITY. YOU BEAR SOLE RESPONSIBILITY AND ALL LIABILITY FOR ANY LOSS INCURRED DUE TO FAILURE OF THE SOFTWARE PRODUCT TO MEET YOUR REQUIREMENTS. STRONG MRKTG WILL NOT, UNDER ANY CIRCUMSTANCES, BE RESPONSIBLE OR LIABLE FOR THE LOSS OF DATA ON ANY COMPUTER OR INFORMATION STORAGE DEVICE.

UNDER NO CIRCUMSTANCES SHALL STRONG MRKTG, ITS DIRECTORS, OFFICERS, EMPLOYEES OR AGENTS BE LIABLE TO YOU OR ANY OTHER PARTY FOR INDIRECT, CONSEQUENTIAL, SPECIAL, INCIDENTAL, PUNITIVE, OR EXEMPLARY DAMAGES OF ANY KIND (INCLUDING LOST REVENUES OR PROFITS OR LOSS OF BUSINESS) RESULTING FROM THIS AGREEMENT, OR FROM THE FURNISHING, PERFORMANCE, INSTALLATION, OR USE OF THE SOFTWARE PRODUCT, WHETHER DUE TO A BREACH OF CONTRACT, BREACH OF WARRANTY, OR THE NEGLIGENCE OF STRONG MRKTG OR ANY OTHER PARTY, EVEN IF STRONG MRKTG IS ADVISED BEFOREHAND OF THE POSSIBILITY OF SUCH DAMAGES. TO THE EXTENT THAT THE APPLICABLE JURISDICTION LIMITS STRONG MRKTG'S ABILITY TO DISCLAIM ANY IMPLIED WARRANTIES, THIS DISCLAIMER SHALL BE EFFECTIVE TO THE MAXIMUM EXTENT PERMITTED.

Limitation of Remedies and Damages
Your remedy for a breach of this Agreement or of any warranty included in this Agreement is the correction or replacement of the Software Product. Selection of whether to correct or replace shall be solely at the discretion of Strong MRKTG. Strong MRKTG reserves the right to substitute a functionally equivalent copy of the Software Product as a replacement. If Strong MRKTG is unable to provide a replacement or substitute Software Product or corrections to the Software Product, your sole alternate remedy shall be a refund of the purchase price for the Software Product exclusive of any costs for shipping and handling.

Any claim must be made within the applicable warranty period. All warranties cover only defects arising under normal use and do not include malfunctions or failure resulting from misuse, abuse, neglect, alteration, problems with electrical power, acts of nature, unusual temperatures or humidity, improper installation, or damage determined by Strong MRKTG to have been caused by you. All limited warranties on the Software Product are granted only to you and are non-transferable. You agree to indemnify and hold Strong MRKTG harmless from all claims, judgments, liabilities, expenses, or costs arising from your breach of this Agreement and/or acts or omissions.

Governing Law, Jurisdiction and Costs
This Agreement is governed by the laws of California, without regard to California's conflict or choice of law provisions.

Severability
If any provision of this Agreement shall be held to be invalid or unenforceable, the remainder of this Agreement shall remain in full force and effect. To the extent any express or implied restrictions are not permitted by applicable laws, these express or implied restrictions shall remain in force and effect to the maximum extent permitted by such applicable laws.
*/


/*

IMPORTANT CONSTANTS
Use these to change things about this plugin if you know what you're doing

*/

/*----------------------------------------------------


DB PREFIX OVERRIDE FOR MULTIPLE BLOGS
If you want to run this plugin on multiple blogs that have tables 
IN THE SAME DATABASE with DIFFERENT prefixes change this constant
from '$wpdb->prefix' to a prefix ('example_') of your choosing.
This cannot be a BLANK value ('')


-----------------------------------------------------*/


$OPT_DATABASE_PREFIX_OVERRIDE = $wpdb->prefix;


ini_set('log_errors', true);
ini_set('error_log', dirname(__FILE__).'/opt_errors.log');

/*-------------------------------------------------------*/



if(isset($wpdb->prefix))
{
	define('OPT_DB_PREFIX',$OPT_DATABASE_PREFIX_OVERRIDE);
	define('DWPC_DB_PREFIX',$OPT_DATABASE_PREFIX_OVERRIDE);										
}
else
{
	define('OPT_DB_PREFIX','get_the_table_prefix');
	define('DWPC_DB_PREFIX','get_the_table_prefix');
}



//Hook into install and uninstall
include_once dirname( __FILE__ ).'/includes/opt_tables.php';
register_activation_hook(__FILE__,'opt_install');
register_deactivation_hook(__FILE__,'opt_uninstall');
add_action( 'admin_menu', 'opt_create_menu' );


//Add Javascript	
add_action('admin_enqueue_scripts', 'opt_load_scripts');
add_action('wp_enqueue_scripts','opt_load_frontend_scripts');


//Don't change this unless you know exactly what you're doing
define('OPT_PLUGIN_DIRECTORY',str_replace('/opt.php','',plugin_basename(__FILE__)));


//STYLE UP THE PAGE
add_action('admin_head', 'opt_admin_head');


//START THE APPLICATION WOOHOO!
include_once dirname( __FILE__ ).'/includes/opt_apptop.php';

add_action('wp_loaded','opt_apptop');


//add_action('wp_footer','opt_footer');
$opt_cookie_name = opt_settings('cookie_name');
if($opt_cookie_name == ''){
	$opt_cookie_name = opt_random_string(16,32);
	opt_settings('cookie_name',$opt_cookie_name);	
}
define('OPT_COOKIE_NAME',$opt_cookie_name);

define(OPT_ICONTACT_KEY,'tR6KVAHKbiiIPylHNtu484BMtD85hMnl');
define(OPT_CONSTANTCONTACT_KEY,'bb5z6v7szfw9h62xbade8zjb');
define(OPT_AWEBER_APPID,'2db8a3fc');

//editor button init
add_action('init','opt_add_button_action');
function opt_add_button_action(){
	if(!current_user_can('edit_posts') && !current_user_can('edit_pages')){
		return;	
	}

	add_filter('mce_external_plugins','opt_register_button_plugin_filter');
	add_filter('mce_buttons','opt_register_button_filter');	

}

function opt_register_button_plugin_filter($plugins){
	$plugins['OptinLinksShortcodeHelper'] = plugin_dir_url(__FILE__).'includes/opt_optin_link_shortcode_helper.php';
	return $plugins;
}
function opt_register_button_filter($buttons){
	array_push($buttons,'|','OptinLinksShortcodeHelper');
	return $buttons;
}

function opt_refresh_mce($ver) {
  $ver += 3;
  return $ver;
}

add_filter( 'tiny_mce_version', 'opt_refresh_mce');


//Javascript user enqueuer
function opt_load_frontend_scripts() {
	
		//jQuery		
		wp_enqueue_script('jquery');
		
		//Ajax
		wp_enqueue_script('opt-ajax', plugin_dir_url(__FILE__) . 'admin_pages/ajax/opt_ajax_handler.js', array('jquery'));		wp_localize_script('opt-ajax', 'opt_vars', array(
				'opt_nonce' => wp_create_nonce('opt-nonce')
			)
		);
		
}

//Javascript admin enqueuer
function opt_load_scripts() {
	
	
	if(empty($_GET['opt_viewing'])) {	
	
	//jQuery		
	wp_enqueue_script('jquery');
	wp_enqueue_script('jquery-ui-core');
	wp_enqueue_script('jquery-ui-datepicker');
	wp_enqueue_script('jquery-ui-slider');
	wp_enqueue_script('jquery-ui-resizable');
	wp_enqueue_script('jquery-ui-accordion');
		
	//Flot Graph
	wp_enqueue_script('jquery-flot', plugin_dir_url(__FILE__) . 'admin_pages/jquery/graph/jquery.flot.js', array('jquery'));
	wp_enqueue_script('jquery-flot.resize', plugin_dir_url(__FILE__) . 'admin_pages/jquery/graph/jquery.flot.resize.js', array('jquery'));
	wp_enqueue_script('jquery-flot.selection', plugin_dir_url(__FILE__) . 'admin_pages/jquery/graph/jquery.flot.selection.js', array('jquery'));
	wp_enqueue_script('jquery-flot.orderBars', plugin_dir_url(__FILE__) . 'admin_pages/jquery/graph/jquery.flot.orderBars.js', array('jquery'));
	
	//jQuery
	wp_enqueue_script('jcanvas', plugin_dir_url(__FILE__) . 'admin_pages/jquery/jcanvas.js', array('jquery'));
	
	//color picker
	wp_enqueue_style( 'wp-color-picker' );
    wp_enqueue_script( 'wp-color-picker');
	
	//Ajax
	wp_enqueue_script('opt-ajax', plugin_dir_url(__FILE__) . 'admin_pages/ajax/opt_ajax_handler.js', array('jquery'));
	wp_localize_script('opt-ajax', 'opt_vars', array(
			'opt_nonce' => wp_create_nonce('opt-nonce')
		)
	);
	
	}
	
}



//Creates menu items for the plugin
function opt_create_menu() {
	


if(isset($_GET['opt_mode'])) { if($_GET['opt_mode']==0 || $_GET['opt_mode']==1) { $opt_mode = $_GET['opt_mode']; opt_settings('opt_mode',$_GET['opt_mode']); }}

if($opt_mode=='') { $opt_mode = 0; opt_settings('opt_mode',0); }
	
$mode = opt_settings('opt_mode');	
//add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );

$user = wp_get_current_user();
if ( $user->roles[0]!='subscriber' ) {
	
	
	
	add_menu_page( 

	__('OptinLinks Dashboard'),

	__('OptinLinks'),

	0,

	OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_index.php',

	'',

	plugins_url('/images/icon.png', __FILE__));
	
	add_submenu_page( 

	OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_index.php',

	__("Create And Manage OptinLinks Campaigns And Individual OptinLinks"),

	__("Campaigns"),

	0,

	OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_admin_campaigns.php'

	);
	
	//sec enterprise		
		add_submenu_page( 
	
		OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_index.php',
	
		__("Manage Your OptinLinks Clients"),
	
		__("Client Manager"),
	
		0,
	
		OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_admin_clients.php'
	
		);
		
	//esec enterprise	
	
	add_submenu_page( 

	OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_index.php',

	__("Secured Page Management And Statistics"),

	__("Secure Pages"),

	0,

	OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_admin_secured_pages.php'

	);
	
	
	add_submenu_page( 

	OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_index.php',

	__("Optin Form Creation, Management, And Statistics"),

	__("Optin Forms"),

	0,

	OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_admin_forms.php'

	);
	
	add_submenu_page( 

	OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_index.php',

	__("Setup Integration With All Of Your Autoresponders And Lists"),

	__("Mailer Integration"),

	0,

	OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_admin_autoresponder.php'

	);
	
	
	
	//sec advanced
	if($mode == 1)
	{
		
		
		add_submenu_page( 
	
		OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_index.php',
	
		__("Full OptinLinks Statistics"),
	
		__("Statistics Log"),
	
		0,
	
		OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_admin_stats.php'
	
		);
		
		add_submenu_page( 
	
		OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_index.php',
	
		__("Split Test Optin Forms"),
	
		__("Split Tests"),
	
		0,
	
		OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_admin_split_tests.php'
	
		);
		
		add_submenu_page( 
	
		OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_index.php',
	
		__("Traffic Filtering Management"),
	
		__("Traffic Filters"),
	
		0,
	
		OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_admin_filters.php'
	
		);
	}
	//esec advanced
		
		add_submenu_page( 
	
		OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_index.php',
	
		__("View And Restore Deleted Items"),
	
		__("Trash"),
	
		0,
	
		OPT_PLUGIN_DIRECTORY.'/admin_pages/opt_admin_trash.php'
	
		);
	
}
	
}


add_action( 'wp_ajax_nopriv_opt_create_lead', 'opt_create_lead' );
add_action( 'wp_ajax_opt_create_lead', 'opt_create_lead' );
add_action( 'wp_ajax_nopriv_opt_check_facebook', 'opt_check_facebook' );
add_action( 'wp_ajax_opt_check_facebook', 'opt_check_facebook' );
add_action( 'wp_ajax_nopriv_opt_check_lead', 'opt_check_lead' );
add_action( 'wp_ajax_opt_check_lead', 'opt_check_lead' );
add_action( 'wp_ajax_nopriv_opt_forms_tracking', 'opt_forms_tracking' );
add_action( 'wp_ajax_opt_forms_tracking', 'opt_forms_tracking' );
add_action( 'wp_ajax_nopriv_opt_links_tracking', 'opt_links_tracking' );
add_action( 'wp_ajax_opt_links_tracking', 'opt_links_tracking' );
add_action('wp_ajax_opthlp', 'opthlp' );
add_action('wp_head', 'opt_set_js_variables');
add_action('wp_head','opt_form_tracking');
add_action( 'wp_ajax_opt_ajax_setting', 'opt_ajax_setting' );
add_shortcode('OptinLink','optin_link_shortcode');
add_shortcode('OptinLinkButton','opt_button_shortcode');

//Admin Head
function opt_admin_head() {  
	
echo '<link rel="stylesheet" type="text/css" href="' .plugins_url('opt_admin.css', __FILE__). '">'; 
echo '<link rel="stylesheet" type="text/css" href="' .plugins_url('admin_pages/jquery/date_picker/css/redmond/jquery-ui.css', __FILE__). '">';

}

?>