function amr_meta_handle_export_request()
{
    global $amain;
    check_admin_referer('amr-meta');
    $ulist = (int) $_REQUEST['csv'];
    $tofile = amr_is_tofile($ulist);
    $capability = apply_filters('amr-users-export-csv', 'list_users', $ulist);
    amr_meta_main_admin_header(__('Export a user list', 'amr-users'), $capability);
    // pass capability
    amr_meta_admin_headings($plugin_page = '');
    // does the nonce check  and formstartetc
    if (isset($_REQUEST['csvfiltered'])) {
        echo amr_generate_csv($ulist, true, true, 'txt', "'", chr(9), chr(13) . chr(10), $tofile);
    } else {
        echo amr_generate_csv($ulist, true, false, 'csv', '"', ',', chr(13) . chr(10), $tofile);
    }
    echo ausers_form_end();
    return;
}
function amrmeta_cache_logs_page()
{
    global $aopt;
    global $amr_nicenames;
    global $pluginpage;
    global $amain;
    //	amr_meta_main_admin_header('Cache Logs');
    amr_meta_admin_headings($plugin_page = '');
    // does the nonce check etc
    $c = new adb_cache();
    if (isset($_POST['trashlog'])) {
        /*  jobs having a problem - allow try again option */
        $c->delete_all_logs();
        //return;
    } else {
        echo alist_trashlogs();
        echo $c->cache_log();
    }
    echo ausers_form_end();
}
function amr_meta_handle_export_request()
{
    global $amain;
    $ulist = (int) $_REQUEST['csv'];
    if (empty($amain['public'][$ulist])) {
        check_admin_referer('amr-meta');
        $tofile = false;
    } else {
        $tofile = true;
    }
    amr_meta_main_admin_header(__('Export a user list', 'amr-users'));
    amr_meta_admin_headings($plugin_page = '');
    // does the nonce check  and formstartetc
    if (isset($_REQUEST['csvfiltered'])) {
        echo amr_generate_csv($ulist, true, true, 'txt', "'", chr(9), chr(13) . chr(10), $tofile);
    } else {
        echo amr_generate_csv($ulist, true, false, 'csv', '"', ',', chr(13) . chr(10), $tofile);
    }
    echo ausers_form_end();
    return;
}
function amrmeta_cachestatus_page()
{
    global $aopt;
    global $amr_nicenames;
    global $pluginpage;
    global $amain;
    $c = new adb_cache();
    //	amr_meta_main_admin_header('Cache Status');
    amr_meta_admin_headings($plugin_page = '');
    // does the nonce check etc
    if (isset($_REQUEST['rebuildback'])) {
        echo '<p>' . __('Background cache request received', 'amr-users') . '</p>';
        if (isset($_REQUEST['rebuildreal'])) {
            $ulist = (int) $_REQUEST['rebuildreal'];
            amr_request_cache_with_feedback($ulist);
        } else {
            amr_request_cache_with_feedback();
        }
        return;
    }
    /* then we have a request to kick off run */
    if (isset($_POST['trashlog'])) {
        /*  jobs having a problem - allow try again option */
        $c->delete_all_logs();
        //return;
    } elseif (isset($_POST['trashcache'])) {
        /*  jobs havign a problem - allow try again option */
        $c->clear_all_cache();
        //return;
    } elseif (isset($_POST['trashcachestatus'])) {
        /*  jobs havign a problem - allow try again option */
        amr_trash_the_cache();
        //return;
    }
    $c->cache_status();
    echo alist_rebuild();
    echo alist_trashcache_status();
    echo alist_trashcache();
    echo ausers_form_end();
}
function amr_meta_overview_page()
{
    /* the main setting spage  - num of lists and names of lists */
    global $amain;
    global $aopt;
    if (empty($amain)) {
        $amain = ausers_get_option('amr-users-main');
    }
    //amr_meta_main_admin_header('Overview of configured user lists'.' '.AUSERS_VERSION);
    amr_meta_admin_headings($plugin_page = '');
    // does the nonce check etc
    if (isset($_POST['import-list'])) {
        amr_meta_handle_import();
    } elseif (isset($_POST['action']) and $_POST['action'] == "save") {
        if (!empty($_POST['reset'])) {
            amr_meta_reset();
            return;
        } elseif (isset($_POST['export-list'])) {
            amr_meta_handle_export();
        } else {
            amrmeta_validate_overview();
        }
    } else {
        amr_handle_copy_delete();
    }
    if (!ameta_cache_enable() or !ameta_cachelogging_enable()) {
        echo '<h2>' . __('Problem creating DB tables', 'amr-users') . '</h2>';
    }
    if (!isset($amain['checkedpublic'])) {
        echo '<input type="hidden" name="checkedpublic" value="true"/>';
    }
    echo '<h2>' . __('Overview &amp; tools', 'amr-users') . '</h2>';
    echo PHP_EOL . '<div class="wrap"><!-- one wrap -->' . PHP_EOL;
    if (!isset($amain['names'])) {
        echo '<h2>' . __('There is a problem - Some overview list settings got lost somehow.  Try reset options.', 'amr-users') . '</h2>';
    } else {
        amr_meta_overview_onelist_headings();
        amr_meta_overview_onelist_headings_middle();
        foreach ($amain['names'] as $i => $name) {
            //for ($i = 1; $i <= $amain['no-lists']; $i++)	{
            amr_meta_overview_onelist_settings($i);
            echo '</tr>';
        }
        amr_meta_overview_onelist_headings_end();
    }
    echo '</div><!-- end of one wrap --> <br />' . PHP_EOL;
    //echo '<div style="clear: both; float:right; padding-right:100px;" class="submit">';
    echo ausers_submit();
    echo '<input class="button-primary" type="submit" name="addnew" value="' . __('Add new', 'amr-users') . '" />';
    amr_list_export_form();
    echo ausers_form_end();
    amr_list_import_form();
    // different form
}
function amrmeta_cache_settings_page()
{
    global $aopt;
    global $amr_nicenames;
    global $pluginpage;
    global $amain;
    if (empty($amain)) {
        $amain = ausers_get_option('amr-users-main');
    }
    $tabs['settings'] = __('Cache Settings', 'amr-users');
    $tabs['logs'] = __('Cache Logs', 'amr-users');
    $tabs['status'] = __('Cache Status', 'amr-users');
    if (isset($_GET['tab'])) {
        if ($_GET['tab'] == 'logs') {
            amr_users_do_tabs($tabs, 'logs');
            amrmeta_cache_logs_page();
            return;
        } elseif ($_GET['tab'] == 'status') {
            amr_users_do_tabs($tabs, 'status');
            amrmeta_cachestatus_page();
            return;
        }
    }
    amr_users_do_tabs($tabs, 'settings');
    //amr_meta_main_admin_header('Cache Settings');
    amr_meta_admin_headings($plugin_page = '');
    // does the nonce check etc
    if (!ameta_cache_enable() or !ameta_cachelogging_enable()) {
        echo '<h2>Problem creating DB tables</h2>';
    }
    if (isset($_POST['action']) and $_POST['action'] == "save") {
        amrmeta_validate_cache_settings();
    }
    if (isset($_REQUEST['rebuildback'])) {
        echo '<p>' . __('Background cache request received', 'amr-users') . '</p>';
        if (isset($_REQUEST['rebuildreal'])) {
            $ulist = (int) $_REQUEST['rebuildreal'];
            amr_request_cache_with_feedback($ulist);
        } else {
            amr_request_cache_with_feedback();
        }
        return;
    } elseif (isset($_REQUEST['rebuildreal'])) {
        /* can only do one list at a time in realtime */
        $ulist = (int) $_REQUEST['rebuildreal'];
        amr_rebuild_in_realtime_with_info($ulist);
        //echo amr_build_cache_for_one($_REQUEST['rebuildreal']);
        //echo '<h2>'.sprintf(__('Cache rebuilt for %s ','amr-users'),$_REQUEST['rebuildreal']).'</h2>'; /* check that allowed */
        //echo au_view_link(__('View Report','amr-users'), $_REQUEST['rebuildreal'], __('View the recently cached report','amr-users'));
        return;
    } else {
        /* validation will have been done */
        $freq = array('notauto' => __('No scheduled auto cacheing', 'amr-users'), 'hourly' => __('Hourly', 'amr-users'), 'twicedaily' => __('Twice daily', 'amr-users'), 'daily' => __('Daily', 'amr-users'));
        if (!isset($amain['cache_frequency'])) {
            $freqchosen = 'notauto';
        } else {
            $freqchosen = $amain['cache_frequency'];
        }
        echo '<h3>';
        _e('Activate regular cache rebuild ? ', 'amr-users');
        echo '</h3><span><em>';
        echo '<p>';
        _e('This cacheing grabs all the raw data it can find and does some preprocessing. ', 'amr-users');
        echo '<br />';
        _e('The data is stored in a flat db table for later formatting and reporting. ', 'amr-users');
        echo '</p>';
        echo '<p>';
        _e('The cache log will tell you the last few times that the cache was rebuilt and why. ', 'amr-users');
        echo '<a href="' . admin_url('admin.php?page=ameta-admin-cache-settings.php&tab=logs') . '">' . __('Go to cache log', 'amr-users') . '</a>';
        echo '<br />';
        _e('A cron plugin may also be useful.', 'amr-users');
        echo ' <a href="http://wpusersplugin.com/related-plugins/amr-cron-manager/">amr cron manager</a>';
        echo '</p>';
        echo '<p><a target="_blank" href="http://wpusersplugin.com/3458/cacheing-amr-users/">' . __('More information', 'amr-users') . '</a></p>';
        echo '</em>	</span>	<p>';
        /*		echo '<label for="notonuserupdate">
        			<input type="checkbox" size="2" id="notonuserupdate" 
        				name="notonuserupdate" ';
        		echo (empty($amain['notonuserupdate'])) ? '' :' checked="checked" '; 
        		echo '/>';
        		_e('Do NOT re-cache on user update', 'amr-users'); 
        		echo '</label>';
        */
        echo '<ul><li>';
        echo '<label for="notonuserupdate">
			<input type="radio" size="2" id="notonuserupdate" 
				name="notonuserupdate" value="true"';
        echo empty($amain['notonuserupdate']) ? '' : ' checked="checked" ';
        echo '/>';
        _e('Do NOT re-cache on user update', 'amr-users');
        echo '</label>';
        echo '</li><li>';
        //echo '<br />';
        echo '<label for="doonuserupdate">
			<input type="radio" size="2" id="doonuserupdate" 
				name="notonuserupdate" value="false"';
        echo $amain['notonuserupdate'] ? '' : ' checked="checked" ';
        echo '/>';
        _e('Do re-cache on user update', 'amr-users');
        echo '</label>';
        echo '</li></ul>';
        echo '</p><br />';
        echo '<br />';
        echo '<p><em><b>';
        _e('If you have very frequent user updates consider only cacheing at regular intervals', 'amr-users');
        echo '</b> ';
        _e('This will help prevent excessive database activity', 'amr-users');
        echo '<br />';
        _e('EG: Are you tracking every page ? every login.. you do not want it recaching all the time?!', 'amr-users');
        _e('Rather cache hourly only.  A refresh can be requested.', 'amr-users');
        echo '<br />';
        _e('Wordpress transients are also used to cache the html in public lists and front end', 'amr-users');
        echo '</em></p>';
        echo '<p><em><b>';
        _e('To switch off all auto cacheing, select "Do not.." above AND "No..." below.', 'amr-users');
        echo '</b><br />';
        _e('Lists will then be re-generated on manual refresh request only.', 'amr-users');
        echo '</em></p>';
        foreach ($freq as $i => $f) {
            echo '<label><input type="radio" name="cache_frequency" value="' . $i . '" ';
            if ($i == $freqchosen) {
                echo ' checked="checked" ';
            }
            echo '/>';
            echo $f;
            echo '</label><br />';
        }
        echo alist_update();
        echo alist_rebuild();
    }
    echo ausers_form_end();
}
function amr_userlist($atts)
{
    global $ausers_do_network;
    global $amain, $aopt;
    remove_filter('the_content', 'wpautop');
    // doesn't work for everyone - why not ?
    $ausers_do_network = false;
    ameta_options();
    // amain will be set
    $criteria = array('show_csv', 'show_headings', 'show_search', 'show_perpage', 'show_pagination', 'show_refresh', 'show_randompage', 'shuffle', 'start_empty');
    //	if (WP_DEBUG)  {
    //		echo 'If Debug only: Attributes from shortcode: '; var_dump($atts);
    //	}
    // compatibility
    if (!empty($atts['headings'])) {
        $atts['show_headings'] = $atts['headings'];
        unset($atts['headings']);
    }
    if (isset($_REQUEST['list'])) {
        /* allow admin users to test lists from the front end, by adding list=x to the url */
        $num = (int) $_REQUEST['list'];
        if ($num > 0 and $num <= count($amain['names'])) {
            $list = $num;
        }
    } else {
        if (!empty($atts['list'])) {
            $list = (int) $atts['list'];
        } else {
            $list = 1;
        }
    }
    // else use whatever was in shortcode
    //
    $options = array();
    foreach ($criteria as $i) {
        if (isset($amain[$i][$list])) {
            $options[$i] = $amain[$i][$list];
        }
    }
    // override with shortcode
    if (!empty($atts)) {
        foreach ($atts as $i => $value) {
            $options[$i] = $atts[$i];
            if ($options[$i] === 'false') {
                // allow for the word false to be used instead of 0
                $options[$i] = false;
            }
        }
    }
    if (ausers_ok_to_show_list($list)) {
        $html = alist_one('user', $list, $options);
        //		if ($options['show_search'] or $options['show_perpage'])	{
        $html = ausers_form_start() . $html . ausers_form_end();
        //		}
        return $html;
    } else {
        return '<p><strong>' . __('Not logged in or no permission for non public user list', 'amr-users') . '</strong></p>';
    }
    //		return('<!-- '.__('Inadequate permission for non public user list','amr-users').' -->');
}
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_list_user_meta()
{
    /* Echos out the paginated version of the requested list */
    global $aopt;
    global $amain;
    global $amr_nicenames;
    global $thiscache;
    if (isset($_POST['info_update']) or amr_is_bulk_request('ym_update')) {
        amr_ym_bulk_update();
        return;
    }
    ameta_options();
    if (!isset($aopt['list'])) {
        _e("No lists Defined", 'amr-users');
        return false;
    }
    if (isset($_REQUEST['ulist'])) {
        $l = (int) $_REQUEST['ulist'];
    } else {
        if (isset($_REQUEST['page'])) {
            /*  somehow needs to be ? instead of & in wordpress admin, so we don't get as separate  */
            $param = 'ulist=';
            $l = substr(stristr($_REQUEST['page'], $param), strlen($param));
        } else {
            //echo '<br />what is happening ?';
            //var_dump($_REQUEST);
        }
    }
    if ($l < 1) {
        $l = 1;
    }
    /* just do the first list */
    //if (WP_DEBUG) echo '<br /> List requested  ='.$l;
    if (isset($_REQUEST['csv'])) {
        amr_meta_handle_export_request();
        return;
    }
    $thiscache = new adb_cache();
    // nlr?
    amr_list_user_admin_headings($l);
    // will only do if in_admin
    echo ausers_form_start();
    if (empty($_REQUEST['filtering']) and empty($_REQUEST['headings'])) {
        ausers_bulk_actions();
    }
    // will check capabilities
    echo alist_one('user', $l, array());
    /* list the user list with the explanatory headings */
    if (empty($_REQUEST['filtering']) and empty($_REQUEST['headings'])) {
        ausers_bulk_actions();
    }
    // will check capabilities
    if (function_exists('amr_ym_bulk_update_form') and amr_is_ym_in_list($l)) {
        // only show form if we have a ym field
        amr_ym_bulk_update_form();
    }
    echo ausers_form_end();
    return;
}