简体   繁体   English

在PHP中使用Javascript如何得到eval的结果?

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

I actually work on a tool named jedox.我实际上在开发一个名为 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:使用此工具,我可以制作宏(如 excel),但在 PHP 中。Jedox 提供了一些宏示例,其中一个示例中有以下代码:

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

It's a PHP code that run JS code.它是运行 JS 代码的 PHP 代码。 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.但问题是我不知道这段代码是如何工作的,我唯一知道的是它执行 JS 代码但不能返回任何东西,所以如果你把return放在代码中它不会返回任何值。 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. PS:如果我尝试调查 test() 返回的值,我会得到一个与另一个数组嵌套的数组,该数组具有这 2 个值“eval”和 function。PS2:显然您无法使用传统工具正确运行此代码。 So to help me you should have jedox, I guess:/...所以为了帮助我,你应该有 jedox,我猜:/...

On the client side, someone must be getting those two strings and executing them.在客户端,必须有人获取这两个字符串并执行它们。 The PHP code ("host side") is not actually doing that. PHP 代码(“主机端”)实际上并没有这样做。

You may could put the Javascript code into a file.您可以将 Javascript 代码放入文件中。 Then execute the file using NodeJS and get the value.然后使用 NodeJS 执行文件并获取值。

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.另请注意,在大多数共享主机和服务器中,默认情况下禁用shell_exec function。 You can enable it through you php.ini .您可以通过php.ini启用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM