</tr>
	<tr>
		<th valign="top"><?php 
_e('License Key', 'adrotate-pro');
?>
</th>
		<td><label for="adrotate_geo_pass"><input name="adrotate_geo_pass" type="text" class="search-input" size="50" value="<?php 
echo $adrotate_config['geo_pass'];
?>
" autocomplete="off" /></label></td>
	</tr>
</table>

<?php 
if ($adrotate_config['enable_geo'] > 0) {
    adrotate_geolocation();
    if (isset($_SESSION['adrotate-geo'])) {
        $geo = $_SESSION['adrotate-geo'];
        $geo_source = 'Session data';
    } else {
        $geo = adrotate_get_cookie('geo');
        $geo_source = 'Cookie';
    }
    ?>
	<h3>Your Geo Targeting Data</h3>
	<p><strong>CAUTION! When you change Geo Services the cookie needs to refresh. You may have to save the settings once or twice for that to happen.</strong><br />
	If re-saving doesn't seem to help, remove the cookie manually from your browsers debug/info console.<br /><br />
	Cookie or _SESSION: <?php 
    echo $geo_source;
    ?>
<br />
function adrotate_options_submit()
{
    if (wp_verify_nonce($_POST['adrotate_nonce_settings'], 'adrotate_settings')) {
        $settings_tab = esc_attr($_POST['adrotate_settings_tab']);
        if ($settings_tab == 'general') {
            $config = get_option('adrotate_config');
            $config['textwidget_shortcodes'] = isset($_POST['adrotate_textwidget_shortcodes']) ? 'Y' : 'N';
            $config['live_preview'] = isset($_POST['adrotate_live_preview']) ? 'Y' : 'N';
            $config['mobile_dynamic_mode'] = isset($_POST['adrotate_mobile_dynamic_mode']) ? 'Y' : 'N';
            $config['jquery'] = isset($_POST['adrotate_jquery']) ? 'Y' : 'N';
            $config['jsfooter'] = isset($_POST['adrotate_jsfooter']) ? 'Y' : 'N';
            $adblock_disguise = strtolower(trim($_POST['adrotate_adblock_disguise']));
            $config['adblock_disguise'] = strlen($adblock_disguise) > 0 ? preg_replace('/[^a-z]/', '', strtolower(substr($adblock_disguise, 0, 6))) : "";
            $banner_folder = strtolower(trim($_POST['adrotate_banner_folder']));
            $config['banner_folder'] = strlen($banner_folder) > 0 ? preg_replace('/[^a-zA-Z0-9\\/\\-_]/', '', $banner_folder) : "wp-content/banners/";
            $config['adblock'] = isset($_POST['adrotate_adblock']) ? 'Y' : 'N';
            $config['adblock_loggedin'] = isset($_POST['adrotate_adblock_loggedin']) ? 'Y' : 'N';
            $adblock_timer = trim($_POST['adrotate_adblock_timer']);
            $config['adblock_timer'] = (strlen($adblock_timer) > 0 and (is_numeric($adblock_timer) and $adblock_timer >= 1 and $adblock_timer <= 20)) ? $adblock_timer : 5;
            $adblock_message = trim($_POST['adrotate_adblock_message']);
            $config['adblock_message'] = strlen($adblock_message) > 0 ? strip_tags(htmlspecialchars(trim($adblock_message, "\t\n "), ENT_QUOTES)) : "Ad blocker detected! Please wait %time% seconds or disable your ad blocker!";
            update_option('adrotate_config', $config);
            // Sort out crawlers
            $crawlers = explode(',', trim($_POST['adrotate_crawlers']));
            $new_crawlers = array();
            foreach ($crawlers as $crawler) {
                $crawler = preg_replace('/[^a-zA-Z0-9\\[\\]\\-_:; ]/i', '', trim($crawler));
                if (strlen($crawler) > 0) {
                    $new_crawlers[] = $crawler;
                }
            }
            update_option('adrotate_crawlers', $new_crawlers);
        }
        if ($settings_tab == 'notifications') {
            $notifications = get_option('adrotate_notifications');
            // Notifications
            $notifications['notification_email'] = isset($_POST['adrotate_notification_email']) ? 'Y' : 'N';
            $notifications['notification_push'] = isset($_POST['adrotate_notification_push']) ? 'Y' : 'N';
            $notifications['notification_dashboard'] = isset($_POST['adrotate_notification_dashboard']) ? 'N' : 'Y';
            // Filter and validate notification addresses, if not set, turn option off.
            $notification_emails = $_POST['adrotate_notification_email_publisher'];
            if (strlen($notification_emails) > 0) {
                $notification_emails = explode(',', trim($notification_emails));
                foreach ($notification_emails as $notification_email) {
                    $notification_email = trim($notification_email);
                    if (strlen($notification_email) > 0) {
                        if (is_email($notification_email)) {
                            $clean_notification_email[] = $notification_email;
                        }
                    }
                }
                $notifications['notification_email_publisher'] = array_unique(array_slice($clean_notification_email, 0, 5));
            } else {
                $notifications['notification_email_publisher'] = array();
            }
            // Filter and validate advertiser addresses
            $advertiser_emails = $_POST['adrotate_notification_email_advertiser'];
            if (strlen($advertiser_emails) > 0) {
                $advertiser_emails = explode(',', trim($advertiser_emails));
                foreach ($advertiser_emails as $advertiser_email) {
                    $advertiser_email = trim($advertiser_email);
                    if (strlen($advertiser_email) > 0) {
                        if (is_email($advertiser_email)) {
                            $clean_advertiser_email[] = $advertiser_email;
                        }
                    }
                }
                $notifications['notification_email_advertiser'] = array_unique(array_slice($clean_advertiser_email, 0, 2));
            } else {
                $notifications['notification_email_advertiser'] = array(get_option('admin_email'));
            }
            // Push Notifications
            $notifications['notification_push_geo'] = isset($_POST['adrotate_notification_push_geo']) ? 'Y' : 'N';
            $notifications['notification_push_status'] = isset($_POST['adrotate_notification_push_status']) ? 'Y' : 'N';
            $notifications['notification_push_queue'] = isset($_POST['adrotate_notification_push_queue']) ? 'Y' : 'N';
            $notifications['notification_push_approved'] = isset($_POST['adrotate_notification_push_approved']) ? 'Y' : 'N';
            $notifications['notification_push_rejected'] = isset($_POST['adrotate_notification_push_rejected']) ? 'Y' : 'N';
            $notifications['notification_push_user'] = strlen($_POST['adrotate_notification_push_user']) > 0 ? preg_replace('/[^a-z0-9.]+/i', '', trim(esc_attr($_POST['adrotate_notification_push_user']))) : '';
            $notifications['notification_push_api'] = strlen($_POST['adrotate_notification_push_api']) > 0 ? preg_replace('/[^a-z0-9.]+/i', '', trim(esc_attr($_POST['adrotate_notification_push_api']))) : '';
            update_option('adrotate_notifications', $notifications);
        }
        if ($settings_tab == 'stats') {
            $config = get_option('adrotate_config');
            $stats = trim($_POST['adrotate_stats']);
            $config['stats'] = (is_numeric($stats) and $stats >= 0 and $stats <= 3) ? $stats : 1;
            $config['enable_loggedin_impressions'] = isset($_POST['adrotate_enable_loggedin_impressions']) ? 'Y' : 'N';
            $config['enable_loggedin_clicks'] = isset($_POST['adrotate_enable_loggedin_clicks']) ? 'Y' : 'N';
            $impression_timer = trim($_POST['adrotate_impression_timer']);
            $config['impression_timer'] = (is_numeric($impression_timer) and $impression_timer >= 10 and $impression_timer <= 3600) ? $impression_timer : 60;
            $click_timer = trim($_POST['adrotate_click_timer']);
            $config['click_timer'] = (is_numeric($click_timer) and $click_timer >= 60 and $click_timer <= 86400) ? $click_timer : 86400;
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'geo') {
            $config = get_option('adrotate_config');
            $geo = trim($_POST['adrotate_enable_geo']);
            $config['enable_geo'] = (is_numeric($geo) and $geo >= 0 and $geo <= 5) ? $geo : 0;
            $geo_cookie = trim($_POST['adrotate_geo_cookie_life']);
            $config['geo_cookie_life'] = is_numeric($geo_cookie) ? $geo_cookie : 86400;
            $geo_email = trim($_POST['adrotate_geo_email']);
            $config['geo_email'] = strlen($geo_email) > 0 ? $geo_email : '';
            $geo_pass = trim($_POST['adrotate_geo_pass']);
            $config['geo_pass'] = strlen($geo_pass) > 0 ? $geo_pass : '';
            // Try to update the Geo Cookie for Admin
            if ($config['enable_geo'] > 0) {
                adrotate_geolocation();
            }
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'advertisers') {
            $config = get_option('adrotate_config');
            $config['enable_advertisers'] = isset($_POST['adrotate_enable_advertisers']) ? 'Y' : 'N';
            $config['enable_editing'] = isset($_POST['adrotate_enable_editing']) ? 'Y' : 'N';
            $config['enable_mobile_advertisers'] = isset($_POST['adrotate_enable_mobile_advertisers']) ? 1 : 0;
            $config['enable_geo_advertisers'] = isset($_POST['adrotate_enable_geo_advertisers']) ? 1 : 0;
            if (isset($_POST['adrotate_role'])) {
                adrotate_prepare_roles('add');
            } else {
                adrotate_prepare_roles('remove');
            }
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'roles') {
            $config = get_option('adrotate_config');
            adrotate_set_capability($_POST['adrotate_advertiser'], "adrotate_advertiser");
            adrotate_set_capability($_POST['adrotate_global_report'], "adrotate_global_report");
            adrotate_set_capability($_POST['adrotate_ad_manage'], "adrotate_ad_manage");
            adrotate_set_capability($_POST['adrotate_ad_delete'], "adrotate_ad_delete");
            adrotate_set_capability($_POST['adrotate_group_manage'], "adrotate_group_manage");
            adrotate_set_capability($_POST['adrotate_group_delete'], "adrotate_group_delete");
            adrotate_set_capability($_POST['adrotate_schedule_manage'], "adrotate_schedule_manage");
            adrotate_set_capability($_POST['adrotate_schedule_delete'], "adrotate_schedule_delete");
            adrotate_set_capability($_POST['adrotate_moderate'], "adrotate_moderate");
            adrotate_set_capability($_POST['adrotate_moderate_approve'], "adrotate_moderate_approve");
            $config['advertiser'] = $_POST['adrotate_advertiser'];
            $config['global_report'] = $_POST['adrotate_global_report'];
            $config['ad_manage'] = $_POST['adrotate_ad_manage'];
            $config['ad_delete'] = $_POST['adrotate_ad_delete'];
            $config['group_manage'] = $_POST['adrotate_group_manage'];
            $config['group_delete'] = $_POST['adrotate_group_delete'];
            $config['schedule_manage'] = $_POST['adrotate_schedule_manage'];
            $config['schedule_delete'] = $_POST['adrotate_schedule_delete'];
            $config['moderate'] = $_POST['adrotate_moderate'];
            $config['moderate_approve'] = $_POST['adrotate_moderate_approve'];
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'misc') {
            $config = get_option('adrotate_config');
            $config['widgetalign'] = isset($_POST['adrotate_widgetalign']) ? 'Y' : 'N';
            $config['widgetpadding'] = isset($_POST['adrotate_widgetpadding']) ? 'Y' : 'N';
            $config['adminbar'] = isset($_POST['adrotate_adminbar']) ? 'Y' : 'N';
            $config['hide_schedules'] = isset($_POST['adrotate_hide_schedules']) ? 'Y' : 'N';
            $config['w3caching'] = isset($_POST['adrotate_w3caching']) ? 'Y' : 'N';
            update_option('adrotate_config', $config);
        }
        if ($settings_tab == 'maintenance') {
            $debug = get_option('adrotate_debug');
            $debug['general'] = isset($_POST['adrotate_debug']) ? true : false;
            $debug['publisher'] = isset($_POST['adrotate_debug_publisher']) ? true : false;
            $debug['advertiser'] = isset($_POST['adrotate_debug_advertiser']) ? true : false;
            $debug['geo'] = isset($_POST['adrotate_debug_geo']) ? true : false;
            $debug['timers'] = isset($_POST['adrotate_debug_timers']) ? true : false;
            $debug['track'] = isset($_POST['adrotate_debug_track']) ? true : false;
            update_option('adrotate_debug', $debug);
        }
        // Return to dashboard
        adrotate_return('adrotate-settings', 400, array('tab' => $settings_tab));
    } else {
        adrotate_nonce_error();
        exit;
    }
}