CodeIgniter Forums
Strange parse error in Form_validation library : unexpected T_STRING eval()'d code - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Strange parse error in Form_validation library : unexpected T_STRING eval()'d code (/showthread.php?tid=16039)

Pages: 1 2


Strange parse error in Form_validation library : unexpected T_STRING eval()'d code - El Forum - 02-23-2009

[eluser]Isuka[/eluser]
Hi there,

My client got a strange error when he's using the application I build for him.
I don't understand the error and when I try everything is fine for me so I can't reproduce the bug.

Here is the error message :
Code:
Parse error: syntax error, unexpected T_STRING in /var/www/coursdiderot/inscription/system/libraries/Form_validation.php(457) : eval()'d code on line 1

So the error is related with the Form_validation library but I don't know what to do with that.

The website run under CodeIgniter 1.7. I didn't upgrade the application to the new CI version yet so maybe this bug is fixed in the 1.7.1 but I'd like to be sure.

Thanks for any advice Smile


Strange parse error in Form_validation library : unexpected T_STRING eval()'d code - El Forum - 02-23-2009

[eluser]pistolPete[/eluser]
Which php version is your client using?
I can't find an eval() in the Form validation class of CI 1.7.1
Could you post some more code?

Just a guess: Are you using CI's feature "Rewrite PHP Short Tags" ?
Code:
$config['rewrite_short_tags'] = ?



Strange parse error in Form_validation library : unexpected T_STRING eval()'d code - El Forum - 02-23-2009

[eluser]xwero[/eluser]
It seems they fixed it in the latest version but in 1.7 you find following line at line 450
Code:
$post .= ' = $array;';

Which should be
Code:
$post .= " = $array;"; /* or */ $post .= ' = '.$array.';';



Strange parse error in Form_validation library : unexpected T_STRING eval()'d code - El Forum - 02-23-2009

[eluser]Isuka[/eluser]
Thanks for helping.

The PHP version of the server is 5.2.8.
I don't use the Rewrite PHP Short Tags feature, I always use "<?php" tag.

Here is the function with the eval() call, line 457 of my Form_validation library (the 1.7 one):
Code:
// --------------------------------------------------------------------

/**
* Re-populate the _POST array with our finalized and processed data
*
* @access    private
* @return    null
*/        
function _reset_post_array()
{
    foreach ($this->_field_data as $field => $row)
    {
        if ( ! is_null($row['postdata']))
        {
            if ($row['is_array'] == FALSE)
            {
                if (isset($_POST[$row['field']]))
                {
                    $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
                }
            }
            else
            {
                $post = '$_POST["';
                
                if (count($row['keys']) == 1)
                {
                    $post .= current($row['keys']);
                    $post .= '"]';
                }
                else
                {
                    $i = 0;
                    foreach ($row['keys'] as $val)
                    {
                        if ($i == 0)
                        {
                            $post .= $val.'"]';
                            $i++;
                            continue;
                        }
                    
                        $post .= '["'.$val.'"]';
                    }
                }
                
                if (is_array($row['postdata']))
                {                    
                    $array = array();
                    foreach ($row['postdata'] as $k => $v)
                    {
                        $array[$k] = $this->prep_for_form($v);
                    }
                    
                    $post .= ' = $array;';
                }
                else
                {                        
                    $post .= ' = "'.$this->prep_for_form($row['postdata']).'";';
                }

                eval($post);
            }
        }
    }
}
I also can show my controller code but I can't reproduce the bug, so I really don't know where my code can be wrong Tongue I try to know more with my client.


Strange parse error in Form_validation library : unexpected T_STRING eval()'d code - El Forum - 02-23-2009

[eluser]Isuka[/eluser]
[quote author="xwero" date="1235415618"]It seems they fixed it in the latest version but in the version with the eval function you find following line at 450
Code:
$post .= ' = $array;';

Which should be
Code:
$post .= " = $array;"; /* or */ $post .= ' = '.$array.';';
[/quote]

Thanks xwero for this Smile

EDIT :

I tried with both
Code:
$post .= " = $array;"; /* and */ $post .= ' = '.$array.';';
and now I can see the error. So the fix don't work but I know now the error come from this line Smile The best thing I can do is maybe to upgrade to 1.7.1.


Strange parse error in Form_validation library : unexpected T_STRING eval()'d code - El Forum - 02-23-2009

[eluser]xwero[/eluser]
You can replace the whole function if you want
Code:
function _reset_post_array()
    {
        foreach ($this->_field_data as $field => $row)
        {
            if ( ! is_null($row['postdata']))
            {
                if ($row['is_array'] == FALSE)
                {
                    if (isset($_POST[$row['field']]))
                    {
                        $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
                    }
                }
                else
                {
                    // start with a reference
                    $post_ref =& $_POST;
                    
                    // before we assign values, make a reference to the right POST key
                    if (count($row['keys']) == 1)
                    {
                        $post_ref =& $post_ref[current($row['keys'])];
                    }
                    else
                    {
                        foreach ($row['keys'] as $val)
                        {
                            $post_ref =& $post_ref[$val];
                        }
                    }

                    if (is_array($row['postdata']))
                    {
                        $array = array();
                        foreach ($row['postdata'] as $k => $v)
                        {
                            $array[$k] = $this->prep_for_form($v);
                        }

                        $post_ref = $array;
                    }
                    else
                    {
                        $post_ref = $this->prep_for_form($row['postdata']);
                    }
                }
            }
        }
    }
Then you don't have the eval function.


Strange parse error in Form_validation library : unexpected T_STRING eval()'d code - El Forum - 02-23-2009

[eluser]TheFuzzy0ne[/eluser]
If you create this file, it should fix the issue for you.

./system/application/libraries/MY_Form_validation
Code:
class MY_Form_validation extends CI_Form_validation {
    function _reset_post_array()
    {
        foreach ($this->_field_data as $field => $row)
        {
            if ( ! is_null($row['postdata']))
            {
                if ($row['is_array'] == FALSE)
                {
                    if (isset($_POST[$row['field']]))
                    {
                        $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
                    }
                }
                else
                {
                    $post = '$_POST["';
                
                    if (count($row['keys']) == 1)
                    {
                        $post .= current($row['keys']);
                        $post .= '"]';
                    }
                    else
                    {
                        $i = 0;
                        foreach ($row['keys'] as $val)
                        {
                            if ($i == 0)
                            {
                                $post .= $val.'"]';
                                $i++;
                                continue;
                            }
                    
                            $post .= '["'.$val.'"]';
                        }
                    }
                
                    if (is_array($row['postdata']))
                    {                    
                        $array = array();
                        foreach ($row['postdata'] as $k => $v)
                        {
                            $array[$k] = $this->prep_for_form($v);
                        }
                    
                        $post .= ' = '.$array.';';
                    }
                    else
                    {                        
                        $post .= ' = "'.$this->prep_for_form($row['postdata']).'";';
                    }

                    eval($post);
                }
            }
        }
    }
}
// End of file: MY_Form_validation.php
// Location: ./system/application/libraries/MY_Form_validation.php
The above code is untested.

EDIT 1: I think xwero's function is missing the final eval();
EDIT 2: Hmmm, his function is quite different. Me = confused...


Strange parse error in Form_validation library : unexpected T_STRING eval()'d code - El Forum - 02-23-2009

[eluser]Isuka[/eluser]
I have changed the _reset_post_array() function with xwero's one. I don't see any error. I call my client to see if it's ok with him. Thanks for your help guys Smile


Strange parse error in Form_validation library : unexpected T_STRING eval()'d code - El Forum - 02-23-2009

[eluser]xwero[/eluser]
The code Isuka provided is the code from CI 1.7, the code i provided is from CI 1.7.1. In the 1.7.1 version the code changes the post global by using a reference where in the 1.7 function it's done by using eval.


Strange parse error in Form_validation library : unexpected T_STRING eval()'d code - El Forum - 02-23-2009

[eluser]TheFuzzy0ne[/eluser]
Aaah. That makes sense now. Thanks for clearing that up. Smile