function amr_generate_csv($ulist, $strip_endings, $strip_html = false, $suffix, $wrapper, $delimiter, $nextrow, $tofile = false)
{
    /* get the whole cached file - write to file? but security / privacy ? */
    /* how big */
    $c = new adb_cache();
    $rptid = $c->reportid($ulist);
    $total = $c->get_cache_totallines($rptid);
    $lines = $c->get_cache_report_lines($rptid, 1, $total + 1);
    /* we want the heading line (line1), but not the internal nameslines (line 0) , plus all the data lines, so neeed total + 1 */
    if (isset($lines) and is_array($lines)) {
        $t = count($lines);
    } else {
        $t = 0;
    }
    $csv = '';
    if ($t > 0) {
        if ($strip_endings) {
            foreach ($lines as $k => $line) {
                $csv .= apply_filters('amr_users_csv_line', $line['csvcontent']) . $nextrow;
            }
        } else {
            foreach ($lines as $k => $line) {
                $csv .= $line['csvcontent'] . $nextrow;
            }
        }
        $csv = str_replace('","', $wrapper . $delimiter . $wrapper, $csv);
        /* we already have in std csv - allow for other formats */
        $csv = str_replace($nextrow . '"', $nextrow . $wrapper, $csv);
        $csv = str_replace('"' . $nextrow, $wrapper . $nextrow, $csv);
        if ($csv[0] == '"') {
            $csv[0] = $wrapper;
        }
    }
    if (amr_debug()) {
        echo '<br />In Debug only: Csv setup: Report: ' . $ulist . ' ' . $c->reportname($ulist) . ' ' . sprintf(__('%s lines found, 1 heading line, the rest data.', 'amr-users'), $t);
        $bytes = mb_strlen($csv);
        echo ' Size = ' . amru_convert_mem($bytes) . '<br />';
    }
    if ($tofile) {
        $csvfile = amr_users_to_csv($ulist, $csv, $suffix);
        $csvurl = amr_users_get_csv_link($ulist);
        //return ($csvurl);
        $html = '<br />' . __('Public user list csv file: ', 'amr-users') . '<br />' . $csvurl;
    } else {
        echo '<p>' . sprintf(__('List %s, %s lines, plus heading line'), $ulist, $t) . '</p>';
        $html = amr_csv_form($csv, $suffix);
    }
    return $html;
}
function amr_request_cache($list = null)
{
    global $aopt;
    global $amain;
    $logcache = new adb_cache();
    $network = ausers_job_prefix();
    if (!empty($list)) {
        if ($logcache->cache_in_progress($logcache->reportid($list, 'user'))) {
            $text = sprintf(__('Cache of %s already in progress', 'amr-users'), $list);
            $logcache->log_cache_event($text);
            return $text;
        }
        if ($text = $logcache->cache_already_scheduled($list)) {
            $new_text = __('Report ', 'amr-users') . $list . ': ' . $text;
            $logcache->log_cache_event($new_text);
            return $new_text;
        }
        $time = time() + 5;
        $text = sprintf($network . __('Schedule background cacheing of report: %s', 'amr-users'), $list);
        $logcache->log_cache_event($text);
        $args[] = $list;
        wp_schedule_single_event($time, 'amr_' . $network . 'reportcacheing', $args);
        /* request for now a single run of the build function */
        return $text;
    } else {
        ameta_options();
        if (empty($aopt['list'])) {
            $text = $network . __('Error: No stored options found.', 'amr-users');
            $logcache->log_cache_event($text);
            return $text;
        } else {
            $no_rpts = count($aopt['list']);
        }
        $logcache->log_cache_event('<b>' . $network . sprintf(__('Received background cache request for %s reports', 'amr-users'), $no_rpts) . '</b>');
        $returntext = '';
        $time_increment = 60;
        $nexttime = time();
        foreach ($aopt['list'] as $i => $l) {
            if (!empty($amain['names'][$i])) {
                //if ($i <= count($amain['names'])) {
                $args = array('report' => $i);
                if ($text = $logcache->cache_already_scheduled($i)) {
                    $new_text = __('All reports: ', 'amr-users') . $text;
                    $logcache->log_cache_event($new_text);
                    $returntext .= $new_text . '<br />';
                    return $returntext;
                } else {
                    wp_schedule_single_event($nexttime, 'amr_' . $network . 'reportcacheing', $args);
                    /* request for now a single run of the build function */
                    $nexttime = $nexttime + $time_increment;
                    unset($args);
                    $text = sprintf(__('Schedule background cacheing of report: %s', 'amr-users'), $i);
                    $logcache->log_cache_event($text);
                    $returntext .= $text . '<br />';
                }
            }
        }
        return $returntext;
    }
    //$result = spawn_cron( time()); /* kick it off soon */
    // time()+3600 = one hour from now.
}
function amr_get_csv_lines($ulist)
{
    /* get the whole cached file - write to file? but security / privacy ? */
    /* how big */
    $c = new adb_cache();
    $rptid = $c->reportid($ulist);
    //$total = $c->get_cache_totallines ($rptid );  // nlr should rather pass 0 to get all
    $lines = $c->get_cache_report_lines($rptid, 0, 0);
    if (empty($lines)) {
        return false;
    }
    // 20140722 no headings lines at moment either
    $headinglines = $c->get_column_headings($rptid);
    array_unshift($lines, $headinglines[1]);
    //$lines = $c->get_cache_report_lines($rptid,0,$total);
    /* we want the heading line (line1), but not the internal nameslines (line 0) , 
    	plus all the data lines, so neeed total + 1 */
    return $lines;
}
function alist_one($type = 'user', $ulist = 1, $options)
{
    //options  can be headings, csv, show_search, show_perpage
    /* Get the fields to use for the chosen list type */
    global $aopt, $amain, $amrusers_fieldfiltering, $amr_current_list, $amr_search_result_count;
    global $amr_refreshed_heading;
    if (empty($aopt['list'][$ulist])) {
        printf(__('No such list: %s', 'amr-users'), $ulist);
        return;
    }
    $l = $aopt['list'][$ulist];
    /* *get the config */
    do_action('amr-add-criteria-to-list', $ulist);
    // allows one to force criteria into the request field for example (eg: show only logged in user)
    $transient_suffix = amr_check_use_transient($ulist, $options);
    if ($transient_suffix) {
        // no filters, no search, no sort, nothing special happening
        //if (WP_DEBUG) echo '<br />using transient: '.$transient_suffix.'<br />';
        $html = get_transient('amr-users-html-for-list-' . $transient_suffix);
        if (!empty($html)) {
            if (current_user_can('administrator')) {
                echo '<br /><a href="' . add_query_arg('refresh', '1') . '" title="' . __('Note to logged in admin only: Now using temporary saved html (transient) for frontend.  Click to refresh.', 'amr-users') . '">!</a>';
            }
            return $html;
        }
    }
    $caption = '';
    $sortedbynow = '';
    if (empty($amain['list_rows_per_page'][$ulist])) {
        $amain['list_rows_per_page'][$ulist] = $amain['rows_per_page'];
    }
    $rowsperpage = amr_rows_per_page($amain['list_rows_per_page'][$ulist]);
    // will check request
    //  use $options as our 'request' input so shortcode parameters will work.
    // allow _REQUEST to override $options
    /*$request_override_allowed = array(
    		'filter',
    		'fieldvaluefilter',
    		'fieldnamefilter',
    		'sort'); */
    foreach ($_REQUEST as $param => $value) {
        // we do not know the column names, so jsut transfer all?
        $options[$param] = $value;
    }
    // figure out what we are doing - searching, filtering -------------------------------------------------------
    $search = '';
    if (!empty($options['su'])) {
        $search = strip_tags($options['su']);
    } elseif (isset($_REQUEST['clear_filtering'])) {
        // we do not need these then
        unset($_REQUEST['fieldnamefilter']);
        unset($_REQUEST['fieldvaluefilter']);
        unset($_REQUEST['filter']);
        //do we neeed to unset the individual cols? maybe not
    }
    $amrusers_fieldfiltering = false;
    if (!empty($options['filter'])) {
        //if (WP_DEBUG) {echo '<h1>Filtering</h1>';}
        foreach (array('fieldnamefilter', 'fieldvaluefilter') as $i => $filtertype) {
            if (isset($options[$filtertype])) {
                //if (WP_DEBUG) {echo '<br />doing: '.$filtertype;}
                foreach ($options[$filtertype] as $i => $col) {
                    if (empty($options[$col])) {
                        //ie showing all
                        unset($options[$filtertype][$i]);
                        unset($options[$col]);
                    } else {
                        $amrusers_fieldfiltering = true;
                    }
                    // set as we are maybe doing realtime filtering flag
                }
            }
        }
    }
    $c = new adb_cache();
    $rptid = $c->reportid($ulist, $type);
    if ($amrusers_fieldfiltering) {
        $lines = amr_build_user_data_maybe_cache($ulist);
        // since we are filtering, we will run realtime, but not save, else we would lose the normal report
        if (empty($lines)) {
            return;
        }
        $totalitems = count($lines);
        //if (WP_DEBUG) echo '<br /> field filtering & $totalitems='.$totalitems;
    } else {
        if (!$c->cache_exists($rptid) or isset($options['refresh'])) {
            if (amr_debug()) {
                _e('If debug only: Either refresh requested OR no cache exists.  A rebuild will be initiated .... ', 'amr-users');
            }
            $success = amr_try_build_cache_now($c, $ulist, $rptid);
            //$lines = amr_build_user_data_maybe_cache($ulist);
            $totalitems = $c->get_cache_totallines($rptid);
            //now need the lines, but first, paging check will tell us how many
            $amrusers_fieldfiltering = false;
            // already done if it must be
        } else {
            $totalitems = $c->get_cache_totallines($rptid);
        }
    }
    //---------- setup paging variables
    if ($totalitems < 1) {
        _e('No lines found.', 'amr-users');
        echo amr_users_get_refresh_link($ulist);
        return;
    }
    if ($rowsperpage > $totalitems) {
        $rowsperpage = $totalitems;
    }
    $lastpage = ceil($totalitems / $rowsperpage);
    if (!empty($_REQUEST['listpage'])) {
        // if we requested a page MUST use that
        $page = (int) $_REQUEST['listpage'];
    } else {
        // is a random page stipulated ?
        if (isset($options['show_randompage'])) {
            // allows a random page
            $page = rand(1, $lastpage);
        } else {
            // else.....start at the very beginning, a very good place to start...
            $page = 1;
        }
    }
    if ($page > $lastpage) {
        $page = $lastpage;
    }
    if ($page == 1) {
        $start = 1;
    } else {
        $start = 1 + ($page - 1) * $rowsperpage;
    }
    $shuffle = false;
    if (!empty($options['shuffle'])) {
        $shuffle = true;
    }
    $filtercol = array();
    //------------------------------------------------------------------------------------------		get the data
    if (!$amrusers_fieldfiltering) {
        // because already have lines if were doing field level filtering
        $headinglines = $c->get_cache_report_lines($rptid, 0, 2);
        /* get the internal heading names  for internal plugin use only */
        /* get the user defined heading names */
        if (!defined('str_getcsv')) {
            $icols = amr_str_getcsv($headinglines[0]['csvcontent'], ',', '"', '\\');
        } else {
            $icols = str_getcsv($headinglines[0]['csvcontent'], ',', '"', '\\');
        }
        $icols = array_unique($icols);
        //since may end up with two indices, eg if filtering and grouping by same value
        if (!defined('str_getcsv')) {
            $cols = amr_str_getcsv($headinglines[1]['csvcontent'], '","', '"', '\\');
        } else {
            $cols = str_getcsv($headinglines[1]['csvcontent'], ',', '"', '\\');
        }
        if (isset($options['filter']) or !empty($options['sort']) or !empty($options['su'])) {
            $lines = amr_get_lines_to_array($c, $rptid, 2, $totalitems + 1, $icols, $shuffle);
        } else {
            if (isset($options['start_empty'])) {
                $lines = array();
                $totalitems = 0;
            } else {
                $lines = amr_get_lines_to_array($c, $rptid, $start + 1, $rowsperpage, $icols, $shuffle);
            }
        }
    } else {
        // we are field filtering
        unset($lines[0]);
        // the tech lines and the headings line
        unset($lines[1]);
        $totalitems = count($lines);
        // must be here, only reset for field filtering
        $s = $l['selected'];
        asort($s);
        /* get the selected fields in the display  order requested */
        $cols = amr_build_col_headings($s);
        $icols = amr_build_cols($s);
        foreach ($lines as $i => $j) {
            $lines[$i] = amr_convert_indices($j, $icols);
        }
    }
    //------------------------------------------------------------------------------------------		display time filter check
    if (isset($options['filter'])) {
        // then we are filtering
        //if (amr_debug()) {
        //	var_dump($options['filter']);
        //	echo '<br />Check for filtering at display time <br />'; var_dump($icols);
        //	}
        foreach ($icols as $cindex => $col) {
            if (!empty($options[$col])) {
                if (!(isset($options['fieldnamefilter']) and in_array($col, $options['fieldnamefilter'])) and !(isset($options['fieldvaluefilter']) and in_array($col, $options['fieldvaluefilter']))) {
                    $filtercol[$col] = esc_attr($options[$col]);
                }
            }
        }
        if (!empty($options['index'])) {
            $filtercol['index'] = strip_tags($options['index']);
        }
        if (false and !$amrusers_fieldfiltering and empty($filtercol) and current_user_can('manage_options')) {
            //NO LONGER REQUIRED, keep for debug only helpful maybe message nlr or perhaps only if by url?  But could be trying own html? and be confused
            echo '<p>';
            _e('This Message shows to admin only!', 'amr-users');
            echo '<br />';
            _e('Filter requested.', 'amr-users');
            _e('Maybe you chose "show all", which is OKAY... or are attempting some own html or link ? .', 'amr-users');
            echo '<br />';
            _e('No valid filter column given.', 'amr-users');
            echo '<br />';
            _e('Column filter Usage is :', 'amr-users');
            echo '<br /><strong>';
            echo '?filter=hide&column_name=value<br />';
            echo '?filter=show&column_name=value</br> ';
            echo '?filter=1&column_name=value';
            echo '</strong></br> ';
            _e('Note: Hide only works if the column is currently being displayed.', 'amr-users');
            _e('For this list, expecting column_name to be one of ', 'amr-users');
            echo '<br />' . implode('<br />', $icols) . '<br />';
            echo '</p>';
        }
        if (!empty($filtercol)) {
            // for each of the filter columns that are not field filters
            foreach ($filtercol as $fcol => $value) {
                //if (amr_debug()) {echo '<hr>Apply filters for field "'.$fcol. '" against... '.$value; }
                foreach ($lines as $i => $line) {
                    //if (WP_DEBUG) {echo '<br>line=';  var_dump($line);}
                    if ($value === '*') {
                        if (empty($line[$fcol])) {
                            unset($lines[$i]);
                        } else {
                        }
                    } elseif ($value === '-') {
                        if (!empty($line[$fcol])) {
                            unset($lines[$i]);
                        } else {
                        }
                    } elseif (empty($line[$fcol])) {
                        unset($lines[$i]);
                    } else {
                        if ($fcol == 'ID') {
                            // id can have  filtering  - allows link to profile page
                            if (!($line[$fcol] == $value)) {
                                /// amr ??
                                unset($lines[$i]);
                            }
                        } else {
                            if (!strstr($line[$fcol], $value)) {
                                // fuzzy filtering - hmm why - maybe not???
                                // is it to avoid situation where value may have spaces before/after ???
                                unset($lines[$i]);
                            }
                        }
                    }
                    //else if (!($line[$fcol] == $value)) {  strisstr will catch these ?
                    //}
                    if ($options['filter'] == 'hide') {
                        unset($lines[$i][$fcol]);
                    }
                }
                // if hiding, delete that column
                if ($options['filter'] == 'hide') {
                    foreach ($icols as $cindex => $col) {
                        if ($fcol == $col) {
                            unset($icols[$cindex]);
                            unset($cols[$cindex]);
                        }
                    }
                }
                // end delete col
                //if (WP_DEBUG) echo '<br />Lines left '.count($lines);
            }
            //-----------------------------------------------------------------------------
            $amr_search_result_count = count($lines);
            $totalitems = $amr_search_result_count;
            // slice the right section of the returned values based on rowsperpage and currentpage
            // update the paging variables
            if ($amr_search_result_count > 0 and $rowsperpage > $amr_search_result_count) {
                $rowsperpage = $amr_search_result_count;
            }
            $lastpage = ceil($amr_search_result_count / $rowsperpage);
            if ($page > $lastpage) {
                $page = $lastpage;
            }
            if ($page == 1) {
                $start = 1;
            } else {
                $start = 1 + ($page - 1) * $rowsperpage;
            }
        }
        //echo '<br />count lines = '.$amr_search_result_count. ' '.$start. ' '. $rowsperpage;
        $lines = array_slice($lines, $start - 1, $rowsperpage, true);
    }
    //end if
    //------------------------------------------------------------------------------------------	 check for sort or search
    if (!empty($options['sort']) or !empty($search)) {
        /* then we want to sort, so have to fetch ALL the lines first and THEN sort.  Keep page number in case workingthrough the list  ! */
        // if searching also want all the lines first so can search within and do pagination correctly
        if ($lines) {
            $linesunsorted = amr_check_for_sort_request($lines);
            $linesunsorted = array_values($linesunsorted);
            /* reindex as our indexing is stuffed and splice will not work properly */
            //if (!empty($search)) $totalitems = count($linesunsorted);	//save total here before splice
            $lines = array_splice($linesunsorted, $start - 1, $rowsperpage);
            unset($linesunsorted);
            // free up memory?
            /* now fix the cache headings*/
            $sortedbynow = '';
            if (!empty($options['sort'])) {
                foreach ($icols as $i => $t) {
                    if ($t == $options['sort']) {
                        $sortedbynow = strip_tags($cols[$i]);
                    }
                }
                $sortedbynow = '<li><em>' . __('Sorted by:', 'amr-users') . '</em>' . $sortedbynow . '</li><li class="sort">';
            }
        }
    }
    //------------------------------------------------------------------------------------------------------------------finished filtering and sorting
    //var_dump($lines);
    $html = amr_display_final_list($lines, $icols, $cols, $page, $rowsperpage, $totalitems, $caption, $search, $ulist, $c, $filtercol, $sortedbynow, $options);
    if ($transient_suffix) {
        // ie no filters, no search, no sort, nothing special happening
        $expiration = empty($amain['transient_expiration']) ? 60 : $amain['transient_expiration'];
        //allow setting later
        set_transient('amr-users-html-for-list-' . $transient_suffix, $html, $expiration);
        track_progress('Transient set for html for list ' . $transient_suffix);
    }
    return $html;
}
function amr_rebuildwarning($list)
{
    $logcache = new adb_cache();
    if ($logcache->cache_in_progress($logcache->reportid($list, 'user'))) {
        $text = sprintf(__('Cache of %s already in progress', 'amr-users'), $list);
        $logcache->log_cache_event($text);
        echo $text;
        return;
    } else {
        $text = $logcache->cache_already_scheduled($list);
        if (!empty($text)) {
            $new_text = __('Report ', 'amr-users') . $list . ': ' . $text;
            $logcache->log_cache_event($new_text);
            amr_users_message($new_text);
            //return;	 - let it run anyway
        }
    }
    echo alist_rebuildreal($list);
    return;
}
function amr_handle_copy_delete()
{
    global $amain, $aopt;
    if (!current_user_can('administrator')) {
        _e('Inadequate access', 'amr-users');
        return;
    }
    if (isset($_GET['copylist'])) {
        $source = (int) $_REQUEST['copylist'];
        if (!isset($amain['names'][$source])) {
            echo 'Error copying list ' . $source;
        }
        $next = 1;
        // get the current max index
        foreach ($amain['names'] as $j => $name) {
            $next = max($next, $j);
        }
        $next = $next + 1;
        //
        foreach ($amain as $j => $setting) {
            if (is_array($setting)) {
                echo '<br />copying ' . $j . ' from list ' . $source;
                if (!empty($amain[$j][$source])) {
                    $amain[$j][$next] = $amain[$j][$source];
                }
            }
        }
        $amain['names'][$next] .= __(' - copy', 'amr-users');
        $amain['no-lists'] = count($amain['names']);
        if (!empty($aopt['list'][$source])) {
            echo '<br />copying settings from list ' . $source;
            $aopt['list'][$next] = $aopt['list'][$source];
        }
        ausers_update_option('amr-users-main', $amain);
        ausers_update_option('amr-users', $aopt);
    } elseif (isset($_GET['deletelist'])) {
        $source = (int) $_REQUEST['deletelist'];
        if (!isset($amain['names'][$source])) {
            amr_users_message(sprintf(__('Error deleting list %S', 'amr-users'), $source));
        } else {
            foreach ($amain as $j => $setting) {
                if (is_array($setting)) {
                    //if (WP_DEBUG) echo '<br />deleting '.$j.' from list '.$source;
                    if (isset($amain[$j][$source])) {
                        unset($amain[$j][$source]);
                    }
                }
            }
        }
        $amain['no-lists'] = count($amain['names']);
        if (!empty($aopt['list'][$source])) {
            unset($aopt['list'][$source]);
        }
        $acache = new adb_cache();
        $acache->clear_cache($acache->reportid($source));
        ausers_update_option('amr-users-main', $amain);
        ausers_update_option('amr-users', $aopt);
        amr_users_message(__('List and the cache deleted.', 'amr-users'));
    }
}