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

打破从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.

图片转代码服务由CSDN问答提供 功能建议

我想 eval()使得调用它的函数也结束 。 但我不想停止整个应用程序。

我使用include将第三方应用程序包含到我自己的应用程序中。 第三方应用程序最终调用一个函数,该函数首先允许我通过钩子注入代码,然后在函数结尾处调用 exit();

我不想手动编辑代码。 我也不想使用exec()或readfile()或curl()包含第三方应用程序,或者包含http或任何其他类似方法。 因为我希望客户端上下文保持对第三方脚本的存在。 (否则,第三方脚本会认为我自己的服务器是客户端。例如,第三方脚本将始终看到 $ _ SERVER ['REMOTE_ADDR'] 127.0.0.1 < / code>。我不希望这样。)

简而言之,发生了以下情况:

我的申请:

  // do stuff 
 // ... 
chdir(“path / to / third / party / application”); 
include(“path / to / third / party / application  /index.php");
chdir("path/to/my/application");
// ... 
 //做更多的事情
   
 
 <  p>第三方应用程序: 
 
 
  //做一些事情
 // ... 
doLastStuff(); 
 
函数doLastStuff(){
 // doing 最后的东西
 // ... 
 
 $ hook =“...”;  //从数据库中获取代码
 if($ hook){
 eval($ hook); 
} 
 exit(); 
} 
   
 
 

问题是最后的 exit(); 也会停止我自己的脚本。 我不希望这样。 任何人都可以看到一种方法来阻止 exit()从钩子内部停止我的脚本吗?

我可以把任何字符串放在$里面 钩。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题