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 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件