dukeRD
6/1/2012 - 5:02 PM

A small MODX snippet to extract POST, GET, SESSION & COOKIE values

A small MODX snippet to extract POST, GET, SESSION & COOKIE values

<?php
/*
 * A small MODX snippet to extract POST, GET, SESSION & COOKIE values - (c) 2012-2016 ackwa.fr 
 *
 * @version : 1.0.4
 * @see     : https://gist.github.com/gists/2853625
 * @name    : argv.php
 * @author  : g.noel@ackwa.fr
 * @usage   : [[!argv?key=`myparam`&default=`1`]]          -> return the value
 *            [[!argv?key=`myparam`&default=`1`&toph=`1`]] -> set [[+argv.myparam]]
 *            [[!argv?key=`myparam`&from=`GP`]]            -> return the value from GET or POST
 *            [[!argv?key=`key1,key2`]]                    -> set [[+argv.key1]] & [[+argv.key2]]
 *            [[!argv?key=`*`]]                            -> set all [[+argv.xxx]]
 *
 * @history : 31/05/12 - 1.0.0 - Initial revision
 *            31/08/12 - 1.0.1 - Add a source(s) priority parameter (default PGSC)
 *            21/09/12 - 1.0.2 - Change default initialization
 *            07/11/13 - 1.0.3 - Add support for more than one key
 *            23/11/16 - 1.0.4 - Correction initialisation $sDefaults
 */
$sDefault  = $modx->getOption('default' , $scriptProperties,         '');
$sDefaults = $modx->getOption('defaults', $scriptProperties,  $sDefault);
$sKeys     = $modx->getOption('key'     , $scriptProperties,         '');
$sKeys     = $modx->getOption('keys'    , $scriptProperties,     $sKeys);
$bPHolder  = $modx->getOption('toph'    , $scriptProperties,      false);
$sFrom     = $modx->getOption('from'    , $scriptProperties,     'PGSC');
$bDebug    = false;

/*
 * Initialisations
 */
$bMulti    = false;
$lKeys     = array();
$lDefaults = array();
$lValues   = array();

/*
 * Data sources
 */
$lSources = array('P' => '_POST', 'G' => '_GET', 'S' => '_SESSION', 'C' => '_COOKIE');

/*
 * Test requested key
 */
if ($sKeys) {
    /*
     * Get all data. In this mode, for security, SESSION data are filtered
     */
    if ('*' == $sKeys) {
        foreach(str_split($sFrom) as $sSource) {
            if ('S' != $sSource) $lKeys = array_merge($lKeys, array_keys($GLOBALS[$lSources[$sSource]]));
        }
    }
    else {
        $lKeys     = explode(',', $sKeys);
        $lDefaults = explode(',', $sDefaults);
    }

    /*
     * Extract value(s)
     */
    foreach($lKeys as $iKey => $sKey) {
        if ($sKey) {
            $lValues[$sKey] = $lDefaults[$iKey];
            foreach(str_split($sFrom) as $sSource) {
                if (isset($GLOBALS[$lSources[$sSource]][$sKey])) {
                    $lValues[$sKey] = $GLOBALS[$lSources[$sSource]][$sKey];
                    break;
                }   
            }
        }
    }
    if ($bDebug) {echo '<pre style="font-size:9px;">';print_r($lValues);echo '</pre>';}
}
$bMulti = (count($lValues) > 1);

/*
 * Test output mode : return value or set placeholder
 */
if ($bPHolder || $bMulti) {
    $modx->toPlaceholders($lValues, 'argv');
    $lValues = array();
}

return (($bPHolder || $bMulti) ? '' : ($sKeys ? $lValues[$sKeys] : ''));