function install_newsonly_subscriptions()
{
    global $db;
    if (!defined('NEWSONLY_SUBSCRIPTION_VERSION') || NEWSONLY_SUBSCRIPTION_VERSION < 204) {
        // new install or old upgrade. Drop/Create database.
        //		$db->Execute("DROP TABLE IF EXISTS " . TABLE_SUBSCRIBERS );
        $db->Execute("CREATE TABLE IF NOT EXISTS " . TABLE_SUBSCRIBERS . " (\n\t\t\t\t\t\t`subscriber_id` int(11) NOT NULL auto_increment,\n\t\t\t\t\t\t`customers_id` int(11) default NULL,\n\t\t\t\t\t\t`email_address` varchar(96) NOT NULL default '' UNIQUE,\n\t\t\t\t\t\t`email_format` varchar(4) NOT NULL default 'TEXT',\n\t\t\t\t\t\t`confirmed` varchar(8) default NULL,\n\t\t\t\t\t\t`subscribed_date` date NOT NULL default '0000-00-00',\n\t\t\t\t\t\tPRIMARY KEY  (`subscriber_id`)\n\t\t\t\t\t) TYPE=MyISAM;");
        if (!defined('NEWSONLY_SUBSCRIPTION_ENABLED')) {
            $db->Execute("INSERT INTO " . TABLE_CONFIGURATION . " \n\t\t\t\t\t\t\t( `configuration_title` , `configuration_key` , `configuration_value` , `configuration_description` , `configuration_group_id` , `sort_order` , `last_modified` , `date_added` , `use_function` , `set_function` )\n\t\t\t\t\t\t\tVALUES ('Enable Newsletter-only subscriptions?', 'NEWSONLY_SUBSCRIPTION_ENABLED', 'true', 'Are visitors allowed to subscribe to your newsletter without creating a customer account?', '1', '200', NULL , NOW(), NULL , 'zen_cfg_select_option(array(''true'', ''false''),')");
        }
        if (!defined('NEWSONLY_SUBSCRIPTION_VERSION')) {
            $db->Execute("INSERT INTO " . TABLE_CONFIGURATION . " \n\t\t\t\t\t\t\t( `configuration_title` , `configuration_key` , `configuration_value` , `configuration_description` , `configuration_group_id` , `sort_order` , `last_modified` , `date_added` )\n\t\t\t\t\t\t\tVALUES ('News-only Subscriptions Version', 'NEWSONLY_SUBSCRIPTION_VERSION', '205', 'Are visitors allowed to subscribe to your newsletter without creating a customer account?', '0', '0', NULL , NOW() )");
        } else {
            $db->Execute("UPDATE " . TABLE_CONFIGURATION . " \n\t\t\t\t\t\t\t\t\t SET configuration_value = '205', \n\t\t\t\t\t\t\t\t\t\t\t configuration_description = 'Newsletter Subscribe Version',\n\t\t\t\t\t\t\t\t\t\t\t last_modified = NOW() \n\t\t\t\t\t\t\t\t\t WHERE configuration_key = 'NEWSONLY_SUBSCRIPTION_VERSION'");
        }
        if (!defined('NEWSONLY_SUBSCRIPTION_HEADER')) {
            $db->Execute("INSERT INTO " . TABLE_CONFIGURATION . " \n\t\t\t\t\t\t\t( `configuration_title` , `configuration_key` , `configuration_value` , `configuration_description` , `configuration_group_id` , `sort_order` , `last_modified` , `date_added` , `use_function` , `set_function` )\n\t\t\t\t\t\t\tVALUES ('Show Newsletter-only subscription field in header?', 'NEWSONLY_SUBSCRIPTION_HEADER',  'false', 'Show subscribe link in header? Note: You must edit your custom template tpl_header.php file in order to use this. See readme that came with contribution.','19', '200', NULL , NOW(), NULL , 'zen_cfg_select_option(array(''true'', ''false''),')");
        }
        if (!defined('NEWSONLY_SUBSCRIPTION_CC_STATUS')) {
            $db->Execute("INSERT INTO " . TABLE_CONFIGURATION . " \n\t\t\t\t\t\t\t( `configuration_title` , `configuration_key` , `configuration_value` , `configuration_description` , `configuration_group_id` , `sort_order` , `last_modified` , `date_added` , `use_function` , `set_function` )\n\t\t\t\t\t\t\tVALUES ('Send Notice of Newsletter-only Subscriptions To - Status',\n\t\t\t\t\t\t\t'NEWSONLY_SUBSCRIPTION_CC_STATUS',  '0',\n\t\t\t\t\t\t\t'Would you like to send a notice of new newsletter-only subscribers?<br />0=off, 1=on','12', '200', NULL , NOW(), NULL , 'zen_cfg_select_option(array(''0'', ''1''),')");
        }
        if (!defined('NEWSONLY_SUBSCRIPTION_CC')) {
            $db->Execute("INSERT INTO " . TABLE_CONFIGURATION . " \n\t\t\t\t\t\t\t( `configuration_title` , `configuration_key` , `configuration_value` , `configuration_description` , `configuration_group_id` , `sort_order` , `last_modified` , `date_added`  )\n\t\t\t\t\t\t\tVALUES ('Send Notice of Newsletter-only Subscriptions To', 'NEWSONLY_SUBSCRIPTION_CC',  '',\n\t\t\t\t\t\t\t'Send notice of newsletter-only subscriptions to the following email addresses, in this format: Name 1 &lt;email@address1&gt;, Name 2 &lt;email@address2&gt;','12', '201', NULL , NOW() )");
        }
        if (!defined('NEWSONLY_SUBSCRIPTION_TEST_GROUP')) {
            $db->Execute("INSERT INTO " . TABLE_CONFIGURATION . " \n\t\t\t\t\t\t\t( `configuration_title` , `configuration_key` , `configuration_value` , `configuration_description` , `configuration_group_id` , `sort_order` , `last_modified` , `date_added`  )\n\t\t\t\t\t\t\tVALUES ('Newsletter Test Group Email', 'NEWSONLY_SUBSCRIPTION_TEST_GROUP',  '',\n\t\t\t\t\t\t\t'Enter the email addresses of customers and newsletter-only subscribers that you wish to send test emails to.<br />Only valid subscriber/customer emails will work.','12', '202', NULL , NOW() )");
        }
        // update the existing query in the query builder to reflect 'customer only' status
        $db->Execute("UPDATE " . TABLE_QUERY_BUILDER . " SET \n\t\t\t\t\t\t\tquery_name='Customer Account Newsletter Subscribers', \n\t\t\t\t\t\t\tquery_description='Returns name and email address of newsletter subscribers who have a customer account.'\n\t\t\t\t\t\t\tWHERE query_string='select customers_firstname, customers_lastname, customers_email_address from TABLE_CUSTOMERS where customers_newsletter = \\'1\\'';");
        // check for our query strings. add if absent.
        // All newsletter subscribers (customer and news-only)
        $qc1 = $db->Execute("select count(*) as total FROM " . TABLE_QUERY_BUILDER . " WHERE query_string = 'select c.customers_firstname, c.customers_lastname, s.email_address as customers_email_address from TABLE_SUBSCRIBERS as s left join TABLE_CUSTOMERS as c on c.customers_id = s.customers_id '");
        if (empty($qc1->fields['total'])) {
            $db->Execute("INSERT INTO " . TABLE_QUERY_BUILDER . " \n\t\t\t\t\t\t\t(query_category, query_name, query_description, query_string, query_keys_list)\n\t\t\t\t\t\t\tVALUES ('email,newsletters', 'All Newsletter Subscribers',\n\t\t\t\t\t\t\t'Returns name and email address of all Customer Account subscribers and all Newsletter-Only subscribers.',\n\t\t\t\t\t\t\t'select c.customers_firstname, c.customers_lastname, s.email_address as customers_email_address from TABLE_SUBSCRIBERS as s left join TABLE_CUSTOMERS as c on c.customers_id = s.customers_id ', '')");
        }
        // Newsletter-only subscribers - fix on old versions.
        $qc2 = $db->Execute("select count(*) as total FROM " . TABLE_QUERY_BUILDER . " WHERE query_string = 'SELECT email_address FROM TABLE_SUBSCRIBERS WHERE email_format != ''NONE'' and confirmed = 1 and (customers_id IS NULL or customers_id = 0) order by email_address'");
        if (!empty($qc2->fields['total'])) {
            $db->Execute("UPDATE " . TABLE_QUERY_BUILDER . " \n\t\t\t\tSET query_string = 'SELECT email_address as customers_email_address FROM TABLE_SUBSCRIBERS WHERE email_format != ''NONE'' and confirmed = 1 and (customers_id IS NULL or customers_id = 0) order by email_address' \n\t\t\t\tWHERE query_string = 'SELECT email_address FROM TABLE_SUBSCRIBERS WHERE email_format != ''NONE'' and confirmed = 1 and (customers_id IS NULL or customers_id = 0) order by email_address'");
        }
        // Newsletter-only subscribers
        $qc2b = $db->Execute("select count(*) as total FROM " . TABLE_QUERY_BUILDER . " WHERE query_string = 'SELECT email_address as customers_email_address FROM TABLE_SUBSCRIBERS WHERE email_format != ''NONE'' and confirmed = 1 and (customers_id IS NULL or customers_id = 0) order by email_address'");
        if (empty($qc2b->fields['total'])) {
            $db->Execute("INSERT INTO " . TABLE_QUERY_BUILDER . " \n\t\t\t\t\t\t\t(query_category, query_name, query_description, query_string, query_keys_list)\n\t\t\t\t\t\t\tVALUES ('email,newsletters', 'Newsletter-only Subscribers',\n\t\t\t\t\t\t\t'Returns email address of all confirmed Newsletter-Only subscribers.',\n\t\t\t\t\t\t\t'SELECT email_address as customers_email_address FROM TABLE_SUBSCRIBERS WHERE email_format != ''NONE'' and confirmed = 1 and (customers_id IS NULL or customers_id = 0) order by email_address', '')");
        }
        // newsletter-only test group.
        $qc3 = $db->Execute("select count(*) as total FROM " . TABLE_QUERY_BUILDER . " WHERE query_string = 'SELECT s.email_address as customers_email_address FROM TABLE_SUBSCRIBERS as s LEFT JOIN TABLE_CONFIGURATION as q on LOCATE( s.email_address, q.configuration_value) >= 1 WHERE configuration_key = ''NEWSONLY_SUBSCRIPTION_TEST_GROUP'' '");
        if (empty($qc3->fields['total'])) {
            $db->Execute("INSERT INTO " . TABLE_QUERY_BUILDER . " \n\t\t\t\t(query_category, query_name, query_description, query_string)\n\t\t\t\tVALUES ('email,newsletters', 'Email Test Group - Newsletter-only subscribers',\n\t\t\t\t'Returns name and email address of Newsletter-only subscribers designated in Email test group configuration.',\n\t\t\t\t'SELECT s.email_address as customers_email_address FROM TABLE_SUBSCRIBERS as s LEFT JOIN TABLE_CONFIGURATION as q on LOCATE( s.email_address, q.configuration_value) >= 1 WHERE configuration_key = ''NEWSONLY_SUBSCRIPTION_TEST_GROUP'' ') ");
        }
        // customer email testgroup
        $qc4 = $db->Execute("select count(*) as total FROM " . TABLE_QUERY_BUILDER . " WHERE query_string = 'SELECT c.customers_email_address as customers_email_address FROM TABLE_CUSTOMERS as c LEFT JOIN TABLE_CONFIGURATION as q on LOCATE( c.customers_email_address, q.configuration_value) >= 1 WHERE configuration_key = ''NEWSONLY_SUBSCRIPTION_TEST_GROUP'' '");
        if (empty($qc4->fields['total'])) {
            $db->Execute("INSERT INTO " . TABLE_QUERY_BUILDER . " \n\t\t\t\t(query_category, query_name, query_description, query_string)\n\t\t\t\tVALUES ('email,newsletters', 'Email Test Group - Customers',\n\t\t\t\t'Returns name and email address of Newsletter-only subscribers designated in Email test group configuration.',\n\t\t\t\t'SELECT c.customers_email_address as customers_email_address FROM TABLE_CUSTOMERS as c LEFT JOIN TABLE_CONFIGURATION as q on LOCATE( c.customers_email_address, q.configuration_value) >= 1 WHERE configuration_key = ''NEWSONLY_SUBSCRIPTION_TEST_GROUP'' ') ");
        }
        transfer_subscriptions();
    } else {
        if (NEWSONLY_SUBSCRIPTION_VERSION == '204') {
            // Newsletter-only subscribers - fix on old versions.
            $qc2 = $db->Execute("select count(*) as total FROM " . TABLE_QUERY_BUILDER . " WHERE query_string = 'SELECT email_address FROM TABLE_SUBSCRIBERS WHERE email_format != ''NONE'' and confirmed = 1 and (customers_id IS NULL or customers_id = 0) order by email_address'");
            if (!empty($qc2->fields['total'])) {
                $db->Execute("UPDATE " . TABLE_QUERY_BUILDER . " \n\t\t\t\t\t\tSET query_string = 'SELECT email_address as customers_email_address FROM TABLE_SUBSCRIBERS WHERE email_format != ''NONE'' and confirmed = 1 and (customers_id IS NULL or customers_id = 0) order by email_address' \n\t\t\t\t\t\tWHERE query_string = 'SELECT email_address FROM TABLE_SUBSCRIBERS WHERE email_format != ''NONE'' and confirmed = 1 and (customers_id IS NULL or customers_id = 0) order by email_address'");
            }
            // Newsletter-only subscribers
            $qc2b = $db->Execute("select count(*) as total FROM " . TABLE_QUERY_BUILDER . " WHERE query_string = 'SELECT email_address as customers_email_address FROM TABLE_SUBSCRIBERS WHERE email_format != ''NONE'' and confirmed = 1 and (customers_id IS NULL or customers_id = 0) order by email_address'");
            if (empty($qc2b->fields['total'])) {
                $db->Execute("INSERT INTO " . TABLE_QUERY_BUILDER . " \n\t\t\t\t\t\t\t\t\t(query_category, query_name, query_description, query_string, query_keys_list)\n\t\t\t\t\t\t\t\t\tVALUES ('email,newsletters', 'Newsletter-only Subscribers',\n\t\t\t\t\t\t\t\t\t'Returns email address of all confirmed Newsletter-Only subscribers.',\n\t\t\t\t\t\t\t\t\t'SELECT email_address as customers_email_address FROM TABLE_SUBSCRIBERS WHERE email_format != ''NONE'' and confirmed = 1 and (customers_id IS NULL or customers_id = 0) order by email_address', '')");
            }
        } elseif (NEWSONLY_SUBSCRIPTION_VERSION == '205') {
        }
        $db->Execute("UPDATE " . TABLE_CONFIGURATION . " \n\t\t\t\t\t\t\t\t\t SET configuration_value = '205', \n\t\t\t\t\t\t\t\t\t\t\t configuration_description = 'Newsletter Subscribe Version',\n\t\t\t\t\t\t\t\t\t\t\t last_modified = NOW() \n\t\t\t\t\t\t\t\t\t WHERE configuration_key = 'NEWSONLY_SUBSCRIPTION_VERSION'");
    }
}
//  $Id: subscription_manager.php,v 1.1 2006/06/16 01:46:12 Owner Exp $
//
require 'includes/application_top.php';
$action = isset($_GET['action']) ? $_GET['action'] : '';
$current_ns_version = 205;
if ($action == 'install' || $action == 'update_contrib') {
    install_newsonly_subscriptions();
    zen_redirect(zen_href_link(FILENAME_SUBSCRIPTION_MANAGER));
}
if ($action == 'remove_confirmed') {
    remove_newsonly_subscriptions();
    zen_redirect(zen_href_link(FILENAME_SUBSCRIPTION_MANAGER));
}
// this can be called by typing action in address bar if needed.
if ($action == 'transfer_subscriptions') {
    transfer_subscriptions();
    zen_redirect(zen_href_link(FILENAME_SUBSCRIPTION_MANAGER));
}
if ($action == 'import_file') {
    list($imported, $error) = import_subscriptions($_POST['record_delim'], $_POST['field_encl'], $_POST['record_sample'], $_POST['default_format'], $_POST['record_header']);
    if (!empty($error)) {
        $action = 'import';
        $messageStack->add($error, 'warning');
    } elseif (!empty($imported)) {
        zen_redirect(zen_href_link(FILENAME_SUBSCRIPTION_MANAGER, 'imported=' . $imported));
    }
}
if (!defined('NEWSONLY_SUBSCRIPTION_ENABLED')) {
    $error = true;
    $messageStack->add(NEWSONLY_SUBSCRIPTION_NOT_INSTALLED, 'install');
} else {