Changeset 701 for trunk/lib


Ignore:
Timestamp:
Sep 3, 2019 7:35:42 AM (5 years ago)
Author:
anonymous
Message:

Add query-string-subkey-parsing to getFormData('foo[bar]')

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Utilities.inc.php

    r696 r701  
    10531053 * quotes if necessary.
    10541054 *
    1055  * @param string $var (optional) The name of the form variable to look for.
     1055 * @param string $key (optional) The name of a $_REQUEST key.
    10561056 * @param string $default (optional) The value to return if the
    10571057 *                                   variable is not there.
    1058  * @return mixed      A cleaned GET or POST if no $var specified.
    1059  * @return string     A cleaned form $var if found, or $default.
    1060  */
    1061 function getFormData($var=null, $default=null)
     1058 * @return mixed      A cleaned GET or POST array if no key specified.
     1059 * @return string     A cleaned form value if found, or $default.
     1060 */
     1061function getFormData($key=null, $default=null)
    10621062{
    10631063    $app =& App::getInstance();
    10641064
    1065     if ('POST' == getenv('REQUEST_METHOD') && is_null($var)) {
     1065    if ('POST' == getenv('REQUEST_METHOD') && null === $key) {
    10661066        return dispelMagicQuotes($_POST, $app->getParam('always_dispel_magicquotes'));
    1067     } else if ('GET' == getenv('REQUEST_METHOD') && is_null($var)) {
     1067    } else if ('GET' == getenv('REQUEST_METHOD') && null === $key) {
    10681068        return dispelMagicQuotes($_GET, $app->getParam('always_dispel_magicquotes'));
    10691069    }
    1070     if (isset($_POST[$var])) {
    1071         return dispelMagicQuotes($_POST[$var], $app->getParam('always_dispel_magicquotes'));
    1072     } else if (isset($_GET[$var])) {
    1073         return dispelMagicQuotes($_GET[$var], $app->getParam('always_dispel_magicquotes'));
     1070
     1071    if (isset($_REQUEST[$key])) {
     1072        // $key is found in the flat array of REQUEST.
     1073        return dispelMagicQuotes($_REQUEST[$key], $app->getParam('always_dispel_magicquotes'));
     1074    } else if (mb_strpos($key, '[') !== false && isset($_REQUEST[strtok($key, '[')]) && preg_match_all('/\[([a-z0-9._~-]+)\]/', $key, $matches)) {
     1075        // $key is formatted with sub-keys, e.g., getFormData('foo[bar][baz]') and top level key (`foo`) exists in REQUEST.
     1076        // Extract these as sub-keys and access REQUEST as a multi-dimensional array, e.g., $_REQUEST[foo][bar][baz].
     1077        $leaf = $_REQUEST[strtok($key, '[')];
     1078        foreach ($matches[1] as $subkey) {
     1079            if (is_array($leaf) && isset($leaf[$subkey])) {
     1080                $leaf = $leaf[$subkey];
     1081            } else {
     1082                $leaf = null;
     1083            }
     1084        }
     1085        return $leaf;
    10741086    } else {
    10751087        return $default;
     
    10771089}
    10781090
    1079 function getPost($var=null, $default=null)
     1091function getPost($key=null, $default=null)
    10801092{
    10811093    $app =& App::getInstance();
    10821094
    1083     if (is_null($var)) {
     1095    if (null === $key) {
    10841096        return dispelMagicQuotes($_POST, $app->getParam('always_dispel_magicquotes'));
    10851097    }
    1086     if (isset($_POST[$var])) {
    1087         return dispelMagicQuotes($_POST[$var], $app->getParam('always_dispel_magicquotes'));
     1098    if (isset($_POST[$key])) {
     1099        return dispelMagicQuotes($_POST[$key], $app->getParam('always_dispel_magicquotes'));
    10881100    } else {
    10891101        return $default;
     
    10911103}
    10921104
    1093 function getGet($var=null, $default=null)
     1105function getGet($key=null, $default=null)
    10941106{
    10951107    $app =& App::getInstance();
    1096     if (is_null($var)) {
     1108
     1109    if (null === $key) {
    10971110        return dispelMagicQuotes($_GET, $app->getParam('always_dispel_magicquotes'));
    10981111    }
    1099     if (isset($_GET[$var])) {
    1100         return dispelMagicQuotes($_GET[$var], $app->getParam('always_dispel_magicquotes'));
     1112    if (isset($_GET[$key])) {
     1113        return dispelMagicQuotes($_GET[$key], $app->getParam('always_dispel_magicquotes'));
    11011114    } else {
    11021115        return $default;
Note: See TracChangeset for help on using the changeset viewer.