function amrmeta_validate_cache_settings()
{
    global $amain;
    global $aopt;
    $amain['notonuserupdate'] = true;
    if (isset($_POST['notonuserupdate'])) {
        if ($_POST['notonuserupdate'] == 'true') {
            $amain['notonuserupdate'] = true;
        } else {
            $amain['notonuserupdate'] = false;
        }
    } else {
        $amain['notonuserupdate'] = false;
    }
    if (!isset($amain['cache_frequency'])) {
        $amain['cache_frequency'] = 'notauto';
    }
    if (isset($_POST['cache_frequency'])) {
        if (!($_POST['cache_frequency'] == $amain['cache_frequency'])) {
            $amain['cache_frequency'] = $_POST['cache_frequency'];
            ameta_schedule_regular_cacheing($_POST['cache_frequency']);
        }
    } else {
        $amain['cache_frequency'] = 'notauto';
    }
    $amain['version'] = AUSERS_VERSION;
    if (isset($_POST)) {
        ausers_update_option('amr-users-main', $amain);
        //ausers_update_option ('amr-users', $aopt);
    }
    return;
}
function ausers_get_option($option)
{
    // allows user reports to be run either at site level and/or at blog level
    global $ausersadminurl, $amr_nicenames;
    if (amr_is_network_admin()) {
        $result = get_site_option('network_' . $option);
    } else {
        $result = get_option($option);
    }
    if (empty($result)) {
        // it's new, get defaults
        //if ($option == 'amr-users-no-lists' ) 	return ameta_default_main(); // old - leave for upgrade check
        if ($option == 'amr-users-main') {
            // and it's empty
            //-------------------------
            //if (WP_DEBUG) echo '<br />Renaming stored option "amr-users-no-lists" to "amr-users-main" ';
            $amain = get_site_option('amr-users-no-lists');
            // might return default ok, if not will have done upgrade check
            if (empty($amain)) {
                $amain = ausers_get_option('amr-users-no-lists');
                if (empty($amain)) {
                    $amain = ameta_default_main();
                }
            }
            $amain['version'] = AUSERS_VERSION;
            ausers_update_option('amr-users-main', $amain);
            ausers_delete_option('amr-users-no-lists');
            return $amain;
            //-------------------------
        }
        if ($option == 'amr-users') {
            return ameta_default_list_options();
        }
        if ($option == 'amr-users-nicenames-excluded') {
            return array('attachment_count' => true, 'activation_key' => true, 'dismissed_wp_pointers' => true, 'default_password_nag' => true, 'nav_menu_item_count' => true, 'revision_count' => true, 'comment_count' => true, 'show_admin_bar_front' => true, 'show_welcome_panel' => true, 'user_activation_key' => true, 'user_status' => true, 'yim' => true, 'aim' => true, 'jabber' => true, 'reply_count' => true, 'topic_count' => true, 'forum_count' => true, 'use_ssl' => true);
        }
        if ($option == 'amr-users-original-keys') {
            return array();
        }
        if ($option == 'amr-users-custom-headings') {
            return array();
        }
        if ($option == 'amr-users-prefixes-in-use') {
            return array();
        }
        if ($option == 'amr-users-nicenames') {
            $amr_nicenames = ameta_defaultnicenames();
        }
    }
    return $result;
}
function amrmeta_validate_overview()
{
    global $amain;
    global $aopt;
    if (isset($_REQUEST['addnew'])) {
        if (count($amain['names']) < 1) {
            $amain['names'][1] = __('New list', 'amr-users');
        } else {
            $amain['names'][] = __('New list', 'amr-users');
        }
        $amain['no-lists'] = count($amain['names']);
    }
    if (isset($_POST['name'])) {
        $return = amrmeta_validate_names();
        if (is_wp_error($return)) {
            echo $return->get_error_message();
        }
    }
    if (isset($_POST['checkedpublic'])) {
        /* admin has seen the message and navigated to the settings screen and saved */
        $amain['checkedpublic'] = true;
    }
    //	unset($amain['public']);
    //	unset($amain['sortable']);
    //	unset($amain['customnav']);
    //
    if (isset($_POST['list_avatar_size'])) {
        if (is_array($_POST['list_avatar_size'])) {
            foreach ($_POST['list_avatar_size'] as $i => $value) {
                $amain['list_avatar_size'][$i] = (int) $value;
            }
        }
    }
    if (isset($_POST['list_rows_per_page'])) {
        if (is_array($_POST['list_rows_per_page'])) {
            foreach ($_POST['list_rows_per_page'] as $i => $value) {
                $amain['list_rows_per_page'][$i] = (int) $value;
            }
        }
    }
    if (isset($_POST['html_type'])) {
        if (is_array($_POST['html_type'])) {
            foreach ($_POST['html_type'] as $i => $value) {
                if (in_array($value, array('table', 'simple'))) {
                    $amain['html_type'][$i] = $value;
                }
            }
        }
    }
    //
    if (isset($_POST['filter_html_type'])) {
        if (is_array($_POST['filter_html_type'])) {
            foreach ($_POST['filter_html_type'] as $i => $value) {
                if (in_array($value, array('intableheader', 'above', 'none'))) {
                    $amain['filter_html_type'][$i] = $value;
                }
            }
        }
    }
    //
    foreach ($amain['names'] as $i => $n) {
        // clear booleans in case not set
        if (!isset($_REQUEST['ulist']) or $_REQUEST['ulist'] == $i) {
            // in case we are only doing 1 list - insingle view
            $amain['show_search'][$i] = false;
            $amain['show_perpage'][$i] = false;
            $amain['show_pagination'][$i] = false;
            $amain['show_headings'][$i] = false;
            $amain['show_csv'][$i] = false;
            $amain['show_refresh'][$i] = false;
            $amain['public'][$i] = false;
            $amain['customnav'][$i] = false;
            $amain['sortable'][$i] = false;
        }
    }
    if (isset($_POST['sortable'])) {
        if (is_array($_POST['sortable'])) {
            foreach ($_POST['sortable'] as $i => $y) {
                $amain['sortable'][$i] = true;
            }
        }
    }
    if (isset($_POST['public'])) {
        if (is_array($_POST['public'])) {
            foreach ($_POST['public'] as $i => $y) {
                $amain['public'][$i] = true;
            }
        }
    }
    amr_users_clear_all_public_csv($amain['public']);
    amr_users_message(__('Csv lists privacy check done.  Any no longer public lists deleted. ', 'amr-users'));
    if (isset($_POST['show_search'])) {
        if (is_array($_POST['show_search'])) {
            foreach ($_POST['show_search'] as $i => $y) {
                $amain['show_search'][$i] = true;
            }
        }
    }
    if (isset($_POST['customnav'])) {
        if (is_array($_POST['customnav'])) {
            foreach ($_POST['customnav'] as $i => $y) {
                $amain['customnav'][$i] = true;
            }
        }
    }
    if (isset($_POST['show_perpage'])) {
        if (is_array($_REQUEST['show_perpage'])) {
            foreach ($_REQUEST['show_perpage'] as $i => $y) {
                $amain['show_perpage'][$i] = true;
            }
        }
    }
    if (isset($_POST['show_pagination'])) {
        if (is_array($_REQUEST['show_pagination'])) {
            foreach ($_REQUEST['show_pagination'] as $i => $y) {
                $amain['show_pagination'][$i] = true;
            }
        }
    }
    if (isset($_POST['show_headings'])) {
        if (is_array($_REQUEST['show_headings'])) {
            foreach ($_REQUEST['show_headings'] as $i => $y) {
                $amain['show_headings'][$i] = true;
            }
        }
    }
    if (isset($_POST['show_csv'])) {
        if (is_array($_REQUEST['show_csv'])) {
            foreach ($_REQUEST['show_csv'] as $i => $y) {
                $amain['show_csv'][$i] = true;
            }
        }
    }
    if (isset($_POST['show_refresh'])) {
        if (is_array($_REQUEST['show_refresh'])) {
            foreach ($_REQUEST['show_refresh'] as $i => $y) {
                $amain['show_refresh'][$i] = true;
            }
        }
    }
    $amain['version'] = AUSERS_VERSION;
    if (isset($_POST)) {
        ausers_update_option('amr-users-main', $amain);
        //ausers_update_option ('amr-users', $aopt);
    }
    amr_users_message(__('Options Updated', 'amr-users'));
    return;
}
 function clear_cache($reportid)
 {
     global $wpdb;
     $sql = "DELETE FROM " . $this->table_name . " WHERE reportid = '" . $reportid . "'";
     $results = $wpdb->query($sql);
     $opt = ausers_get_option('amr-users-cache-status');
     //track_progress('Reportid = '.$reportid.' opt='.print_r($opt[$reportid], true));
     if (isset($opt[$reportid])) {
         unset($opt[$reportid]);
     }
     $result = ausers_update_option('amr-users-cache-status', $opt);
     return $results;
 }
function amr_check_for_upgrades()
{
    // NB must be in order of the oldest changes first // called from ausers_get_option
    // should already have values then - and will not be new ?
    global $amain, $aopt;
    if (empty($amain)) {
        $amain = ausers_get_option('amr-users-main');
    }
    //if (WP_DEBUG) echo '<div class="message">Debug mode: check doing upgrade check </div>';
    // must be in admin and be admin
    if (!is_admin() or !current_user_can('manage_options')) {
        return;
    }
    // handle a series of updates in order
    if (!isset($amain['version'])) {
        $amain['version'] = '0';
    }
    // really old?
    if (version_compare($amain['version'], AUSERS_VERSION, '=')) {
        return;
    }
    // if same version, don't repeat check
    $prev = $amain['version'];
    echo PHP_EOL . '<div class="updated"><p>';
    // closing div at end
    printf(__('Previous version was %s. ', 'amr-users'), $prev);
    _e('New version activated. ', 'amr-users');
    _e('We may need to process some updates.... checking now... ', 'amr-users');
    // do old changes first - user may not have updated for a while....
    if (!isset($amain['version']) or version_compare($amain['version'], '3.1', '<')) {
        // convert old options from before 3.1
        echo '<br />';
        printf(__('Prev version less than %s', 'amr-users'), '3.1.');
        if (!isset($amain['csv_text'])) {
            $amain['csv_text'] = '<img src="' . plugins_url('amr-users/images/file_export.png') . '" alt="' . __('Csv', 'amr-users') . '"/>';
        }
        if (!isset($amain['refresh_text'])) {
            $amain['refresh_text'] = '<img src="' . plugins_url('amr-users/images/rebuild.png') . '" alt="' . __('Refresh user list cache', 'amr-users') . '"/>';
        }
        ausers_update_option('amr-users-main', $amain);
        echo '<br />' . __('Image links updated.', 'amr-users');
        echo '</p>';
    }
    //
    if (!isset($amain['version']) or version_compare($amain['version'], '3.3.1', '<')) {
        // check for before 3.3.1
        echo '<br />';
        printf(__('Prev version less than %s', 'amr-users'), '3.3.1.');
        $c = new adb_cache();
        $c->deactivate();
        if (!ameta_cache_enable() or !ameta_cachelogging_enable()) {
            echo '<h2>' . __('Problem creating amr user DB tables', 'amr-users') . '</h2>';
        }
        echo '<br />';
        _e('Cacheing tables recreated.', 'amr-users');
    }
    //
    if (!isset($amain['version']) or version_compare($amain['version'], '3.3.6', '<')) {
        // check for before 3.3.6,
        echo '<br />';
        printf(__('Prev version less than %s', 'amr-users'), '3.3.6. ');
        echo '</p>' . __('Minor sub option name change for avatar size', 'amr-users') . '</p>';
        if (!empty($amain['avatar-size'])) {
            $amain['avatar_size'] = $amain['avatar-size'];
        } else {
            $amain['avatar_size'] = '16';
        }
        unset($amain['avatar-size']);
        ausers_update_option('amr-users-main', $amain);
    }
    // 3.4.4  July 2012
    if (!isset($amain['version']) or version_compare($amain['version'], '3.4.4', '<')) {
        // check for before 3.3.,
        echo '<br />';
        printf(__('Prev version less than %s', 'amr-users'), '3.4.4 ');
        echo '<p><b>' . __('New Pagination option default to yes for all lists.', 'amr-users') . '</b></p>';
        if (!isset($amain['show_pagination'])) {
            foreach ($amain['names'] as $i => $n) {
                $amain['show_pagination'][$i] = true;
            }
        }
    }
    $amain['version'] = AUSERS_VERSION;
    ausers_update_option('amr-users-main', $amain);
    // was 'amr-users-no-lists'
    echo '<p>' . __('Finished Update Checks', 'amr-users') . ' ';
    echo ' <a href="http://wordpress.org/extend/plugins/amr-users/changelog/">' . __('Please read the changelog', 'amr-users') . '</a>';
    echo '</p>' . PHP_EOL;
    echo '<br />' . __('As a precaution we will now rebuild the nice names.', 'amr-users');
    echo '<br />' . __('Relax .... you won\'t lose anything.', 'amr-users');
    ameta_rebuildnicenames();
    echo '</div><!-- end updated -->' . PHP_EOL;
}
function amrmeta_validate_excluded_keys()
{
    $excluded_meta_keys = amr_default_excluded_metakeys();
    if (isset($_POST['mex']) and is_array($_POST['mex'])) {
        foreach ($_POST['mex'] as $i => $v) {
            if ($v) {
                $excluded_meta_keys[$i] = true;
            } else {
                $excluded_meta_keys[$i] = false;
            }
        }
    }
    ausers_update_option('amr-users-excluded-meta-keys', $excluded_meta_keys);
    echo amr_users_message(__('Options Updated', 'amr-users'));
    return true;
}
function amrmeta_configure_page()
{
    global $aopt;
    global $amr_nicenames;
    global $pluginpage;
    global $amain;
    ameta_options();
    // should handle emptiness etc
    if (isset($_POST['addnew']) and isset($_POST['copylist'])) {
        $copyfrom = intval($_REQUEST['copylist']);
        $amain['names'][] = __('New list copy of ') . $amain['names'][$copyfrom];
        $aopt['list'][] = $aopt['list'][$copyfrom];
        $ulist = array_pop(array_keys($amain['names']));
        $amain['names'][$ulist] .= ' #' . $ulist;
        ausers_update_option('amr-users', $aopt);
        ausers_update_option('amr-users-main', $amain);
        amr_users_message('Added list: ' . $ulist);
        $_REQUEST['ulist'] = $ulist;
    } else {
        if (!empty($_REQUEST['ulist'])) {
            $ulist = (int) $_REQUEST['ulist'];
        } else {
            $ulist = '1';
        }
    }
    amr_meta_main_admin_header('Configure a user list');
    amr_meta_admin_headings($plugin_page = '');
    // does the nonce check  and formstartetc
    //	else
    if (isset($_REQUEST['rebuild'])) {
        /* can only do one list at a time in realtime */
        amr_rebuild_in_realtime_with_info($ulist);
        echo ausers_form_end();
        return;
    } elseif (!empty($_REQUEST['rebuildback'])) {
        /*  */
        amr_request_cache_with_feedback();
        echo ausers_form_end();
        return;
    } elseif (!empty($_REQUEST['rebuildwarning'])) {
        /*  */
        amr_rebuildwarning($ulist);
        echo ausers_form_end();
        return;
    } elseif (isset($_REQUEST['custom_navigation'])) {
        if (function_exists('amrmeta_custom_navigation_page')) {
            amrmeta_custom_navigation_page($ulist);
            echo ausers_form_end();
            return;
        } else {
            echo 'Function not active';
        }
    } elseif (isset($_REQUEST['grouping'])) {
        if (function_exists('amr_grouping_admin_form')) {
            amr_grouping_admin_form($ulist);
            echo ausers_form_end();
            return;
        } else {
            _e('Grouping Function not active', 'amr-users');
        }
    } elseif (amr_users_can_edit('filtering')) {
        amrmeta_filtering_page($ulist);
    } elseif (isset($_POST['action']) and $_POST['action'] == "save") {
        if (isset($_POST['updateoverview'])) {
            amrmeta_validate_overview();
        } elseif (isset($_POST['update'])) {
            if (!amrmeta_validate_listfields($ulist)) {
                amr_users_message(__('List Fields Validation failed', 'amr-users'));
            }
        } elseif (isset($_POST['configure'])) {
            // ulist already set above, so will just configure
        }
    }
    list_configurable_lists();
    // to allow selection of which to configure
    amrmeta_listfields($ulist);
    echo ausers_form_end();
}
function amr_check_for_table_prefixes($nn)
{
    // use a field that is always there and has the table prefixes
    $prefixes_in_use = array();
    $checkfield = 'user-settings-time';
    foreach ($nn as $i => $n) {
        if (stristr($i, $checkfield)) {
            $prefixes_in_use[] = str_replace($checkfield, '', $i);
        }
    }
    ausers_update_option('amr-users-prefixes-in-use', $prefixes_in_use);
}
function amr_meta_handle_import()
{
    global $amain, $aopt;
    if (isset($_POST['import-list'])) {
        //	require_once(ABSPATH . 'wp-admin/includes/admin.php');
        if (empty($_FILES)) {
            amr_users_message('No file specified', 'amr-users');
            return;
        }
        if (empty($_FILES['importfile'])) {
            amr_users_message('Problem in upload', 'amr-users');
            return;
        }
        $file = $_FILES['importfile'];
        $uploaded = wp_handle_upload($file, array('test_form' => FALSE));
        if (is_wp_error($uploaded)) {
            $errors['upload_error'] = $uploaded;
        }
        if (!empty($errors)) {
            amr_users_message('There was an error uploading your file.', 'amr-users');
            return;
        }
        if (empty($uploaded['file'])) {
            amr_users_message('No import file chosen.', 'amr-users');
            return;
        }
        $content = file_get_contents($uploaded['file']);
        //var_dump ($content);
        $data = unserialize(stripslashes($content));
        //$data = unserialize($content);
        if (!is_array($data) or !isset($data['version'])) {
            echo '<div class="error"><p>';
            _e('Invalid data in settings file', 'amr-users');
            echo '</p></div';
            die;
        }
        if (version_compare($data['version'], $amain['version'], '==')) {
            amr_users_message(sprintf(__('Your plugin version is %s. Imported settings are from plugin version: %s', 'amr-users'), $amain['version'], $data['version']) . ' &nbsp; Yay!');
        } else {
            amr_users_message(sprintf(__('Your plugin version is %s, BUT imported settings are from plugin version: %s', 'amr-users'), $amain['version'], $data['version']) . ' ' . __('Please test thoroughly.', 'amr-users'));
        }
        $toimport = amr_main_to_export();
        // get the list of overview settings
        $amain['names'][] = 'justimported';
        foreach ($amain['names'] as $index => $name) {
            if ($name == 'justimported') {
                $thisindex = $index;
            }
        }
        foreach ($toimport as $setting) {
            if (isset($data[$setting])) {
                $amain[$setting][$thisindex] = $data[$setting];
            }
        }
        $aopt['list'][$thisindex] = $data['list'];
        ausers_update_option('amr-users', $aopt);
        ausers_update_option('amr-users-main', $amain);
        amr_users_message(sprintf(__('List %s will be saved with imported data', 'amr-users'), $thisindex));
    }
}
function amrmeta_validate_mainoptions()
{
    global $amain;
    global $aopt;
    $amain['no_credit'] = 'no_credit';
    if (isset($_POST['no_credit'])) {
        if ($_POST['no_credit'] == 'give_credit') {
            $amain['no_credit'] = 'give_credit';
            $amain['givecreditmessage'] = amr_users_random_message();
        }
    }
    if (!empty($_POST["use_wp_query"])) {
        $amain['use_wp_query'] = true;
    } else {
        $amain['use_wp_query'] = false;
    }
    if (isset($_POST["do_not_use_css"])) {
        $amain['do_not_use_css'] = true;
    } else {
        $amain['do_not_use_css'] = false;
    }
    if (isset($_POST['use_css_on_pages'])) {
        $check = explode(',', $_POST['use_css_on_pages']);
        foreach ($check as $i => $value) {
            $check[$i] = intval($value);
        }
        $amain['use_css_on_pages'] = implode(',', $check);
    } else {
        $amain['use_css_on_pages'] = '';
    }
    if (isset($_POST['csv_text'])) {
        $return = amrmeta_validate_text('csv_text');
        if (is_wp_error($return)) {
            echo $return->get_error_message();
        }
    }
    if (isset($_POST['refresh_text'])) {
        $return = amrmeta_validate_text('refresh_text');
        if (is_wp_error($return)) {
            echo $return->get_error_message();
        }
    }
    if (isset($_POST["rows_per_page"])) {
        $return = amrmeta_validate_rows_per_page();
        if (is_wp_error($return)) {
            echo '<h2>' . $return->get_error_message() . '</h2>';
        }
    }
    if (isset($_POST["avatar_size"])) {
        $return = amrmeta_validate_avatar_size();
        if (is_wp_error($return)) {
            echo '<h2>' . $return->get_error_message() . '</h2>';
        }
    }
    $amain['version'] = AUSERS_VERSION;
    if (isset($_POST)) {
        ausers_update_option('amr-users-main', $amain);
        //ausers_update_option ('amr-users', $aopt);
    }
    return;
}
function amr_users_store_column_headings($ulist, $customcols)
{
    if (!($amr_users_column_headings = ausers_get_option('amr-users-custom-headings'))) {
        $amr_users_column_headings = array();
    }
    $amr_users_column_headings[$ulist] = $customcols;
    $results = ausers_update_option('amr-users-custom-headings', $amr_users_column_headings);
    if ($results) {
        amr_users_message(__('Custom Column Headings Updated', 'amr-users'));
    } else {
        amr_users_message(__('Column headings not updated - no change or error.', 'amr-users'));
    }
    return $results;
}