<?php

# PLUGIN PREVIEW BY TEXTPATTERN.INFO


/*
    adi_gps - Extract GET & POST variables

    Written by Adi Gilbert

    Released under the GNU General Public License

    Version history:
    0.4        - updated for TXP 4.6 trace
    0.3        - copes with forms which supply data as arrays (checkboxes)
            - new attribute 'glue'
            - TXP 4.6 tag registration
    0.2        - multiple security & functionality improvements (thanks to Gocom)
            - 'name' attribute now accepts comma separated list
            - removed 'txpvar' attribute - TXP variables now always set
            - 'post' attribute deprecated & now ignored (default behaviour now retrieves POST vars)
            - removed 'global' attribute & functionality - not secure
            - 'escape' now enabled by default
            - removed 'list' & 'debug' attributes - debug info now in TXP tag trace (when Production Status = debugging)
            - new attribute 'type'
    0.1        - initial release

*/

// TXP 4.6 tag registration
if (class_exists('\Textpattern\Tag\Registry')) {
    
Txp::get('\Textpattern\Tag\Registry')
        ->
register('adi_gps')
    ;
}

function 
adi_gps($atts) {

    global 
$variable,$trace// i.e. $GLOBALS['variable']

    
extract(lAtts(array(
        
'name'        => '',        // comma separated list of GET/POST vars
        
'new'        => '',        // new variable name
        
'quiet'        => '0',        // return value or not
        
'type'        => 'all',    // 'all' = GET & POST, 'post' = POST only
        
'post'        => '0',        // deprecated & ignored - POST vars now auto extracted
        
'escape'    => 'html',    // escape HTML entities
        
'decode'    => '0',        // perform a urldecode
        
'glue'        => ',',        // join array values together
    
), $atts));

    if (
$name == ''// all vars extracted (GET/POST filtered later)
        
$name_list array_keys(array_merge((array) $_GET, (array) $_POST));
    else
        
$name_list doArray(explode(',',$name),'trim');

    
// remove blanks from list (e.g if "var1,var2," supplied)
    
$name_list array_filter($name_list,'strlen');

    
// massage options if retrieving all vars or multiple vars requested
    
if (($name == '') || (count($name_list) > 1)) {
        
$quiet 1// force quiet mode
        
$new ''// disable var rename
    
}

    
// gps() or ps()
    
if (strtolower($type) == 'post')
        
$func 'ps';
    else
        
$func 'gps';

    
// process var list
    
foreach ($name_list as $index => $name) {
        
$value $func($name); // extract value
        
if (is_array($value)) // if array then glue it together as a string
            
$value implode($glue,$value);
        
$name htmlspecialchars($new !== '' $new $name);
        if (
$decode// convert %chars
            
$value urldecode($value);
        if (
$escape// encode chars (e.g. ampersand becomes &amp;)
            
$value htmlspecialchars($value);
        if (
$new// rename var (single var mode only)
            
$name $new;
        
$variable[$name] = $value// set $GLOBALS['variable'][$name]
        // trace
        
if (is_object($trace)) // TXP 4.6
            
$trace->log('['.__FUNCTION__.': '.$name.' = '.$value.']');
        else
            
trace_add('['.__FUNCTION__.': '.$name.' = '.$value.']');
    }

    if (!
$quiet && isset($value)) // return value
        
return $value;

    return 
'';
}