donglu3243 2015-04-03 12:00
浏览 110
已采纳

打破从eval中调用eval的函数

I want to eval() to make the function that calls it to end as well. But I don't want to stop the entire application.

I am using include to include an third party application into my own application. The third party application eventually calls a function that first allows me to inject code via a hook, end then at the end of the function it simply calls exit();.

I don't want to edit their code manually. And I also don't want to include the third party application using exec() or readfile() or curl() or include over http or any other similar method. Because I want the client context to stay existing toward the third party script. (Otherwise, the third party script will think that my own server is the client. For example, the third party script will always see that $_SERVER['REMOTE_ADDR'] is 127.0.0.1. And I don't want that.)

In short, the following is happening:

My application:

// do stuff
// ...
chdir("path/to/third/party/application");
include ("path/to/third/party/application/index.php");
chdir("path/to/my/application");
// ...
// do more stuff

Third party application:

// do some stuff
// ...
doLastStuff();

function doLastStuff() {
    // doing some last stuff
    // ...

    $hook = "..."; // get code from database
    if ($hook) {
        eval($hook);
    }
    exit();
}

The problem is that the exit(); at the end also stops my own script. And I don't want that. Can anyone see a way to stop to avoid that exit() stopping my script as well, from inside the hook?

I can put what ever string inside $hook.

  • 写回答

1条回答 默认 最新

  • dongyan1974 2015-04-03 13:02
    关注

    I fixed this myself already. The trick was to raise an Exception inside the eval'd code. And to put the include in a try/catch in my own application.

    My Application:

    // do stuff
    // ...
    chdir("path/to/third/party/application");
    try {
        include ("path/to/third/party/application/index.php");
    }
    catch (Exception $e) {
    }
    chdir("path/to/my/application");
    // ...
    // do more stuff
    

    Third party application:

    // do some stuff
    // ...
    doLastStuff();
    
    function doLastStuff() {
        // doing some last stuff
        // ...
    
        $hook = "throw new Exception();"; // get code from database
    
        if ($hook) {
            eval($hook);
        }
        exit(); // will no longer be executed...
    }
    

    I could also raise a custom derived Exception class so I only catch my own Exception and leave other Exceptions uncaught.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog