【问题标题】:PHP exec node dynamic script from memory内存中的PHP exec节点动态脚本
【发布时间】:2016-10-13 01:37:16
【问题描述】:

我正在尝试通过 PHP 中的 Node.js 运行代码,而无需触及文件系统(磁盘)。这可能吗?

我在做什么

<?php

function renderJs($script) {
  $path = __DIR__.'/'.md5($script).'.js';
  file_put_contents($path, $script);
  $rendered = `node $path`;
  unlink($path);

  return $rendered;
}

$val = renderJs('console.log(1+1)'); // 2

我想避免的事情

  • 必须将文件保存到磁盘
  • 安装额外的 PHP 扩展(例如 V8js)

理想情况下,应该是这样的

$magicBlob = php_magic($script);
$rendered = `node $magicBlob`;

我不能像 in 那样评估它

$rendered = `node --eval "$script"`;

因为脚本中的引号可能会破坏我的外部引用。

【问题讨论】:

  • 您应该能够 pipe code 到 Node 并获得结果(如果有的话)。允许人们在您的系统上执行任意代码时要非常小心。我想积极地把这个沙盒化。

标签: php node.js


【解决方案1】:

使用escapeshellarg 对我有用:

$js = <<<EOF

hello = function (name) {
    console.log('`Hello there '+name+"!!`");
}

hello('PHP');
EOF;

$rendered = system("node --eval ".escapeshellarg($js));

我不知道这个想法有多好,但是......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2022-12-10
    • 2013-12-12
    相关资源
    最近更新 更多