简体   繁体   中英

How can I execute CGI files from PHP?

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.

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.

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`;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM