如何从 PHP 执行 CGI 文件?

发布于 2024-07-29 02:52:50 字数 338 浏览 2 评论 0原文

我正在尝试制作一个网络应用程序来为我管理我的 Mercurial 存储库。 我希望它能够在我告诉它加载存储库 X 时:

  • 连接到 MySQL 服务器并确保 X 存在。
  • 检查是否允许用户访问存储库。
  • 如果上述情况成立,则从 mysql 服务器获取 X 的位置。
  • 运行包含存储库路径的 hgweb cgi 脚本 (python)。

问题是这样的,我想:获取 hgweb 脚本,修改它,然后运行它。 但我不想:获取 hgweb 脚本,修改它,将其写入文件并重定向到那里。 我正在使用 Apache 来运行 httpd 进程。

I'm trying to make a web app that will manage my Mercurial repositories for me.
I want it so that when I tell it to load repository X:

  • Connect to a MySQL server and make sure X exists.
  • Check if the user is allowed to access the repository.
  • If above is true, get the location of X from a mysql server.
  • Run a hgweb cgi script (python) containing the path of the repository.

Here is the problem, I want to: take the hgweb script, modify it, and run it.
But I do not want to: take the hgweb script, modify it, write it to a file and redirect there.
I am using Apache to run the httpd process.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半夏半凉 2024-08-05 02:52:50

Ryan Ballantyne 发布了正确的答案(我投票了)。 反引号运算符是执行 shell 脚本的方式。

最简单的解决方案可能是修改 hgweb 脚本,使其本身不“包含”存储库的路径。 相反,将其作为命令行参数传递。 这意味着您不必担心在任何地方修改和编写 hgweb 脚本。 您所要做的就是:

//do stuff to get location of repository from MySQL into variable $x
//run shell script
$res = `python hgweb.py $x`;

Ryan Ballantyne has the right answer posted (I upvoted it). The backtick operator is the way to execute a shell script.

The simplest solution is probably to modify the hgweb script so that it doesn't "contain" the path to the repository, per se. Instead, pass it as a command-line argument. This means you don't have to worry about modifying and writing the hgweb script anywhere. All you'd have to do is:

//do stuff to get location of repository from MySQL into variable $x
//run shell script
$res = `python hgweb.py $x`;
也只是曾经 2024-08-05 02:52:50

您可以从 PHP 中运行 shell 脚本。 有多种方法可以做到这一点,以及某些主机未提供适当权限的复杂情况,所有这些都在 php.net 上有详细记录。 也就是说,最简单的方法是将命令括在反引号中。 因此,要解压缩文件,我可以说:

`unzip /path/to/file`

所以,如果您的 python 脚本可以从命令行环境运行(或者您可以修改它以便运行),那么这似乎是首选方法。

You can run shell scripts from within PHP. There are various ways to do it, and complications with some hosts not providing the proper permissions, all of which are well-documented on php.net. That said, the simplest way is to simply enclose your command in backticks. So, to unzip a file, I could say:

`unzip /path/to/file`

SO, if your python script is such that it can be run from a command-line environment (or you could modify it so to run), this would seem to be the preferred method.

爱冒险 2024-08-05 02:52:50

至于你的问题,不,你不可能让 php 执行修改后的脚本而不将其写入某个地方,无论是磁盘上的文件、映射到 ram 的虚拟文件还是类似的文件。

听起来您可能正在尝试用树枝敲击铁路道钉。 如果您要根据 MySQL 中存储的用户权限来过滤访问,您是否查看过现有的 HG 解决方案以确保没有比 hgweb 更适用的解决方案? 它确实是为了做好一件事而构建的,这远远超出了它的正常范围。

我可能建议研究 apache 的本机身份验证作为控制对存储库的访问的更方便的方法,然后只提供存储库而不修改脚本。

As far as you question, no, you're not likely to get php to execute a modified script without writing it somewhere, whether that's a file on the disk, a virtual file mapped to ram, or something similar.

It sounds like you might be trying to pound a railroad spike with a twig. If you're to the point where you're filtering access based on user permissions stored in MySQL, have you looked at existing HG solutions to make sure there isn't something more applicable than hgweb? It's really built for doing exactly one thing well, and this is a fair bit beyond it's normal realm.

I might suggest looking into apache's native authentication as a more convenient method for controlling access to repositories, then just serve the repo without modifying the script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文