How to get the result of eval when I use Javascript in PHP?

248 views Asked by At

I actually work on a tool named jedox. With this tool I can make macro(like excel) but in PHP. Jedox provide some example of macro and in one of these example there is this code:

function test()
{
 return array(array('eval', "(function(){
  console.log('salut')
  }())"));
}

It's a PHP code that run JS code. But the problem is I don't know how this code work, the only thing I know about it is that it execute JS code but can't return anything, so if you put return in the code it will not return any value. I have no way to retrieve the value returned.

So my question is how should I supposed to retrieve a value from this ?

PS: if I try to investigate about the value returned by test() I get an array nested with another array with those 2 values 'eval' and the function. PS2: Apparently you can't run this code correctly with traditional tool. So to help me you should have jedox, I guess :/ ...

2

There are 2 answers

1
Mike Robinson On

On the client side, someone must be getting those two strings and executing them. The PHP code ("host side") is not actually doing that.

0
Mohammad Zarifiyan On

You may could put the Javascript code into a file. Then execute the file using NodeJS and get the value.

For example:

function test() {
    file_put_contents('test.js', <<< TEXT
(function(){
   console.log('salut')
}())
TEXT);

   return shell_exec('node test.js');
}
    
echo test(); // Return: sault

Also notice that in most shared hosts and servers shell_exec function is disabled by default. You can enable it through you php.ini.