Jump to content

[Problem] $_POST in Addons Module


Open Presta

Recommended Posts

when adding a module addons for sale, prestashop team must validate, and we must replace all $ _POST by Tools :: getvalue 

but in some function we can't like this :  

 

public function copyFromPostOption()
 {
  foreach ($_POST AS $key => $value)
  if (key_exists($key, $this) AND $key != 'id_'.$this->table)
  { 
   if($key == 'content_option')
   {
    foreach ($_POST[$key] as $keyOption=>$opArr)
    {
     if(isset($opArr['opt_fill_column']))
     {
      $_POST[$key][$_POST['type_option']]['opt_fill_column'] = $opArr['opt_fill_column'];
     }
    }
    if ($_POST['type_option'] == 2) //check option static block
    {
     $languages = Language::getLanguages(false);
     foreach ($languages AS $language)
     {
       $temp = str_replace(_PS_BASE_URL_.__PS_BASE_URI__, $this->temp_url,$_POST['content_option'][$_POST['type_option']]['opt_content_static'][(int)($language['id_lang'])]);
       $_POST['content_option'][$_POST['type_option']]['opt_content_static'][(int)($language['id_lang'])] = $temp;
     }
     
     $value = htmlspecialchars(json_encode($_POST['content_option'][$_POST['type_option']]));
     $turned = '\r\n';
     $turn_back = '';
     $value = str_replace( $turned, $turn_back, $value );
    }
    else
    {
     $value = json_encode($_POST['content_option'][$_POST['type_option']]);
    }
    
   }
   $this->{$key} = $value;
  }
 }
  1. We can't Replace foreach ($_POST AS $key => $value) with Tools::getvalue and i is not another function 

    i see all code in prestashop they also use like this
  2. $_POST['content_option'][$_POST['type_option']] Also this Tools::getvalue not enbale to be replaced 
Link to comment
Share on other sites

Purpose of the copyFromPost() is to retrieve the object fields value from .... the POST

 

If you use the Tools::getValue(), you may (but not sure) retrieve a value that is given in the GET and that should not be copied into the object

 

copyFromPost() copies from POST, not from POST or GET

Link to comment
Share on other sites

simply stated, the function copies all the POST'ed parameters to an internal array, and does some special handling for certain parameters.

 

Therefore the developer has a need to cycle through all of the POST'ed parameters, and Tools::getValue does not allow for this.

 

Regardless if the parameter is in both _POST and _GET arrays is a moot point. 

 

What feature is Prestashop trying to enforce by requiring the use of Tools::getValue anyway?  It is a helper function, and performs some URL encoding and parameter cleansing.  Instead of requiring the use of Tools::getValue, provide a separate helper function that performs the same parameter cleansing, and enforce that is used.

 

To say a developer cannot use _POST directly is an unreasonable requirement, there are many valid use cases for it.  I assume you have searched the Prestashop code and modules, right?  PS themselves use _POST, in fact 1410 matches when I search the PS v1.5.6.2 code base

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

In the class php file

 

If I use Tools::safePostVars() in the foreach loop, the changes on the module configuration page don't work:

	public function copyFromPost()
	{
		/* Classical fields */
		foreach (Tools::safePostVars() AS $key => $value)
			if (key_exists($key, $this) AND $key != 'id_'.$this->table)
				$this->{$key} = $value;

		/* Multilingual fields */
		if (sizeof($this->fieldsValidateLang))
		{
			$languages = Language::getLanguages(false);
			foreach ($languages AS $language)
				foreach ($this->fieldsValidateLang AS $field => $validation) 
					if (Tools::getValue($field.'_'.(int)($language['id_lang']))!= false)
						$this->{$field}[(int)($language['id_lang'])] = Tools::getValue($field.'_'.(int)($language['id_lang']));
		}
	}
}

However, if I use $_POST instead of Tools::safePostVars() everything is OK.

Link to comment
Share on other sites

Yes, correct. The loop below works in 1.5, but not in 1.6

foreach (Tools::safePostVars() AS $key => $value)

But instead, if I use $_POST, it works on both.

 

This php file is a class file like modules>blockcmsinfo>infoClass.php of Prestashop 1.6.0.6

 

You can find the same usage there on line 52 in function copyFromPost

Link to comment
Share on other sites

Tools::safePostVars function is identical between 1.5.6.2 and 1.6.0.6, so something else is having an adverse effect here.

 

Now I did look back to PS 1.5.6 and it has a different safePostVars.  So it is PS 1.5.6.1 that introduced a change.  So perhaps the issue is not 1.6.0.6, but instead the new approach introduced with PS 1.5.6.1

 

What version of PS v1.5 did you test where it is working properly?

Link to comment
Share on other sites

Any suggession for this how it work with Tools::getValue ? 


 

$return = $_POST['content_option'][$_POST['type_option']]['input_hidden_id'])  


the form like that : 

 

<p>
<label>Fill the column:</label>
<input type="text" class="opt_fill_column" size="20" name="content_option[0][opt_fill_column]" value="1"></p>  

 
Link to comment
Share on other sites

Emmanuel, perhaps you can address the reason for not allowing _POST to be used directly by modules, when PS core uses it about 1400 different locations.

 

If a module wants to iterate through all the posted parameters without having to predefine the expected parameters, what is the issue that addons team is concerned with?

 

Is the issue about sanitizing the data (which is all Tools::getValue is doing), then why not add a new function that allows for sanitizing the data. Then a module can use _POST directly, sanitize the data themselves, and move on...

Link to comment
Share on other sites

I think a little wiggle room is required on the accepted use of $_POST. As long as the $_POST variable is ran through Tools::safePostVars() before it's used I really don't see the issue with it. 

 

I guess the main issue is the Addons team just don't have the time to police every modules code, so they just outright refuse all use of $_POST to be safe.

 

A quick solution would be just to make Tools::safePostVars() return $_POST after it was done with it. 

Link to comment
Share on other sites

I think a little wiggle room is required on the accepted use of $_POST. As long as the $_POST variable is ran through Tools::safePostVars() before it's used I really don't see the issue with it. 

 

I guess the main issue is the Addons team just don't have the time to police every modules code, so they just outright refuse all use of $_POST to be safe.

 

A quick solution would be just to make Tools::safePostVars() return $_POST after it was done with it. 

 

Tools::safePostVars() request a Array post , but not all time we have a array for all 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...