如何从 PHP 执行 CGI 文件?
我正在尝试制作一个网络应用程序来为我管理我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ryan Ballantyne 发布了正确的答案(我投票了)。 反引号运算符是执行 shell 脚本的方式。
最简单的解决方案可能是修改 hgweb 脚本,使其本身不“包含”存储库的路径。 相反,将其作为命令行参数传递。 这意味着您不必担心在任何地方修改和编写 hgweb 脚本。 您所要做的就是:
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:
您可以从 PHP 中运行 shell 脚本。 有多种方法可以做到这一点,以及某些主机未提供适当权限的复杂情况,所有这些都在 php.net 上有详细记录。 也就是说,最简单的方法是将命令括在反引号中。 因此,要解压缩文件,我可以说:
所以,如果您的 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:
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.
至于你的问题,不,你不可能让 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.