does AG function makeSafe clean an array?

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

does AG function makeSafe clean an array?

Post by onoehring » 2021-02-20 12:08

Hi,

I would like to know, if the included function makeSafe can be used to clean an array with just one call like

Code: Select all

$cleanArray = makeSafe($dirtyArray);
Olaf

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: does AG function makeSafe clean an array?

Post by jsetzer » 2021-02-20 13:02

PHP function array_walk() will do the job. I'm using it almost every day for applying changes to all or certain items of an array: Other useful "every-day" array-functions in PHP:
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: does AG function makeSafe clean an array?

Post by onoehring » 2021-02-20 13:39

Hi Jan,

thank you for the hint.
The current function makeSafe is this:

Code: Select all

	function makeSafe($string, $is_gpc = true) {
		static $cached = []; /* str => escaped_str */

		if(!db_link()) sql("SELECT 1+1", $eo);

		// if this is a previously escaped string, return from cached
		// checking both keys and values
		if(isset($cached[$string])) return $cached[$string];
		$key = array_search($string, $cached);
		if($key !== false) return $string; // already an escaped string

		$cached[$string] = db_escape($string);
		return $cached[$string];
	}
I must admit, currently I too deep into some code to get my head around, that's why I am asking.

I am also not sure, if a variable that is given (from a function of mine) to makeSafe contains an array or a string. Both is possible depending on certain parameters.
That's why it would be great, I the makeSafe would not care about the variable type.

If makeSafe does not allow using an array, it might be worth a feature request (or simply a custom function?) that we are able to throw everything at it. Something like (code not tested)

Code: Select all

function makeSafe_custom($data, $is_gpc = true) {
  $myReturnValue = '';
  if (is_array($data){
    $myReturnValue = array_walk($data, 'makeSafe');
  } else {
    $myReturnValue = makeSafe($data);    
  }
  return $myReturnValue;
}
What do you think?

Olaf

Olaf

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: does AG function makeSafe clean an array?

Post by jsetzer » 2021-02-21 07:29

Did you test your function? Your code throws syntax errors (at least in my IDE).

You will have to use array_map instead of array_walk if you want to return a modified array.

Personally, I try to avoid untyped function- or method- parameters (AKA "mixed"). Sometimes you cannot avoid them, but I got used to write typed parameters which brings some benefit, for example:

Code: Select all

/**
 * array_makeSafe
 * 
 * Applies AppGini's makeSafe function to all entries of $data-array
 *
 * @param  array $data
 * @param  bool $is_gpc
 * @return array
 */
function array_makeSafe(array $data, bool $is_gpc = true)
{
    return array_map(function ($entry) use ($is_gpc) {
        return makeSafe($entry, $is_gpc);
    }, $data);
}
}
Note the type-declaration before the variables (like "array $data") and also note the type declaration of return value (here: array).

This helps me because of syntax-highlighing and intellisense features of my IDE (Visual Studio Code, free). It is more work to write comments on the functions/methods and parameters, but after a couple of years you will be glad to see the docs and comments your have written in the past.

See intellisense features while coding:

gyEVRiu3GS.gif
gyEVRiu3GS.gif (157.02 KiB) Viewed 1895 times

Additionally, the IDE shows syntax errors if I did not use the function correctly, for example when passing in a string instead of an array:


Code_EAqZu24dep.png
Code_EAqZu24dep.png (10.41 KiB) Viewed 1894 times

Usage

Code: Select all

$data = [
    "name" => "Jan's example",
];
var_dump($data);

$data = array_makeSafe($data);
var_dump($data);
Attachments
chrome_n1LeAdItgU.png
chrome_n1LeAdItgU.png (18.6 KiB) Viewed 1895 times
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: does AG function makeSafe clean an array?

Post by jsetzer » 2021-02-21 07:46

If you prefer a "smart" (but untyped) function which automatically detects the type of the input parameter and returns an array OR a string, try the folllowing code:

Code: Select all

function makeSafe_custom($data, $is_gpc = true)
{
    return is_array($data) ? array_map(function ($entry) use ($is_gpc) {
        return makeSafe($entry, $is_gpc);
    }, $data) : makeSafe($data, $is_gpc);
}
Usage

Code: Select all

$data = [
    "name" => "Jan's example",
];
var_dump($data);
$data = makeSafe_custom($data);
var_dump($data);

$text = "Jan's example";
var_dump($text);
$text = makeSafe_custom($text);
var_dump($text);
Attachments
chrome_pWX4aYmtVo.png
chrome_pWX4aYmtVo.png (32.14 KiB) Viewed 1890 times
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: does AG function makeSafe clean an array?

Post by onoehring » 2021-02-21 07:46

Hi Jan,

thank you for your function's code. No, my code was not even tested - I wrote it directly into the AG forum.
I think we can combine both (yours and mine) however:
As I wrote, I can not be sure if a variable comes as an array or a string, thus probably something like this would work for both types:

Code: Select all

function makeSafe_custom($data, $is_gpc = true) {
  $myReturnValue = '';
  if (is_array($data){
    $myReturnValue = array_makeSafe($data);
  } else {
    $myReturnValue = makeSafe($data);    
  }
  return $myReturnValue;
}

....

$data = makeSafe_custom($data);
I know, usually you should probably know what variable type your are expecting ...

Olaf

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: does AG function makeSafe clean an array?

Post by onoehring » 2021-02-21 08:07

Hi,
oops, I missed your 2nd post. Sorry.
Thank you .. for the 2nd function as well.

Olaf

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: does AG function makeSafe clean an array?

Post by jsetzer » 2021-02-21 08:14

If you cannot use typed arguments but need some validation of input parameters, do some validation first:

Code: Select all

function makeSafe_custom($data, $is_gpc = true)
{
     if (is_null($data))
         throw new \Exception("Argument null", 1);

     if (!(is_array($data) || is_string($data)))
         throw new \Exception("Invalid argument", 1);

    return is_array($data) ? array_map(function ($entry) use ($is_gpc) {
        return makeSafe($entry, $is_gpc);
    }, $data) : makeSafe($data, $is_gpc);
}
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools


Post Reply