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.

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

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样