Running a Zend_Application from the command line? - php

Is there a blessed way to run a Zend_Application from the command line? That is, I want to run a shell script that invokes a Zend_Application, loads its configuration, and then calls a specific controller action OR run an arbitrary command line script with access to the applications configurations, models, etc.
I can think of a few ways to hack this together, but it seems like the kind of thing where there may be an official (but poorly documented) way of doing it.

here's one way: http://webfractor.wordpress.com/2008/08/14/using-zend-framework-from-the-command-line/
if there is actually a web server in the mix, you could also of course trigger w/wget or lynx... wget --quiet http://server/app/doTheThing

Related

Executing Python scripts in PHP: Different methods of execution?

I want to execute a Python script from PHP and apparently there are two completely different methods to do it.
The first method is to install Python on Wampserver (which I am using). The method described here involves extra files (mod_wsgi) and some modification (httpd.conf). I tried this method and Wampserver started failing (localhost not available) which was resolved by removing mod_wsgi.
The second method is by simply using the shell_exec command (example) which executes the Python script without any problems. I was a little surprised at how easy it was to do it and I have no idea why it works. I assume that the shell used is that of the current server's. Does this mean that I can also use installed external libraries in that script (e.g. OpenCV / cv2)?
What are the differences between the two methods and why is method 1 so complicated?

How to exit shell mode in system()

I have a php script which should use some preset system aliases. I.e. "alias ll=ls -l"
In terminal "ll" works but from php system("ll") outputs
sh: ll: command not found
How do I exit "sh" and execute my terminal commands?
P.S.: May be I missunderstood the basic linux components shell and bash. In this case, please correct me/the post
The PHP docs aren't clear about this, but presumably PHP's system is a reflection of Standard C's system(3), which hands the argument command to the command interpreter sh(1). If you want to avoid the shell, you'll need to use another feature of PHP besides system (like explicit fork/exec). That's context, but it won't help you solve your problem.
In your case it seems you just want the aliases in an rcfile. Scripts invoked by system calls aren't going to read your rcfile unless you take extraordinary steps to make that happen, and even if you do, it's going to be non-obvious to the maintenance programmer. I'd strongly advise you to put the alias in the script or command argument itself, or simply call the command explicitly (ls -al).
You can also manually source the rcfile from your script, or call system(csh -i 'yourcommands') to force csh to be invoked as an interactive shell (which should cause your rcfile to be read). I think this is a bad idea because it is effectively forcing the shell to behave inconsistently with its environment, but I'm including it here for completeness.
Most of the above I got from a quick read through the Startup and shutdown section of the csh manual on my Mac (Mavericks). There are other potential solutions there that I haven't laid out, as well.

Unable to delete Linux user using PHP and bash script

I'm able to delete linux users directly from the shell using the command ./del_user.sh
del_user.sh is as below
#!/bin/sh
userdel username
When I run this script from a PHP page, it doesn't work.
echo shell_exec('./del_user.sh');
Both the files are in the same directory.
What could be the problem?
del_user.sh does not take an argument so you're running the script on whatever hardcoded user is in the script. This won't work unless you modify the script to allow a command line argument.
While this task can be accomplished by PHP, this is certainly not a primary function and should probably be delegated to a more suitable application.
As an aside, you haven't stated what your goal is or what this script is supposed to do. Is this part of some user management application or is it simply a one-off for some small task?
Answer:
To enable this, you'd have to give Apache the ability to sudo so it can gain temporarily raised priveleges. You may have to do some googling on how to do this depending on your OS but this link provides some direction on how to do it in Ubuntu: Ubuntu Forums
I would also recommend against using a bash script as its not really necessary to do this. You could use a PHP script that accepts a command line argument. So in your main script you could have something like this: shell_exec('php /path/to/del_user.php username');
Then in del_user.php you'd have something like shell_exec('userdel '.$argv[1]);. More info on commandline arguments can be found here: $argv
Lastly, you could put it directly into your main script instead of using shell_exec twice. In other words, just use shell_exec('userdel '.$username); instead of calling a script. Since Apache will be able to sudo, it should be able to execute this directly.

Set cron job for absolute path

I am using MVC framework. Now I want to set up cron such that the URL "http://www.xyz.com/controllera/functiona" should be executed. what should i write in the path section for it.
I got something about "GET" command but it wasnt clear.
Can someone please help me out with it?
As you didn't specify any framework the only way to run this cron is this command
wget --spider 'http://www.xyz.com/controllera/functiona'
I assume you are using an MVC framework as controllera is in the url. If it was Kohana (2.3) framework I would have run it by
/usr/bin/php /path/to/index.php controller/method
Most framework has cli interface to run a controller method. Search for your framework.
See these links for different frameworks.
Zend Framework
Kohana 2 & 3
Codeignier
Yii
I don't understand the "module called cron" part of your question. I believe you are confused, cron is a service on Linux and other Unix systems configured thru crontab.
A crontab(5) entry is defined by a time and date and a command to run.
On Linux and Posix systems, you cannot execute or run an URL. Running something involves the execve(2) system call, which requires an executable file path (and arguments).
Perhaps you want to retrieve some URL using the HTTP protocol. You might use a command-line HTTP client, like wget or curl.
So perhaps the command you want to run in your crontab might be
wget http://www.xyz.com/controllera/functiona
but you could use curl
My guess is that you are confused, and don't understand well enough your question. Consider reading some material.
For example, to retrieve that URL once a day at 3 pm, you would have the following crontab entry:
# run everyday at 3 pm a GET HTTP request
0 15 * * * /usr/bin/wget http://www.xyz.com/controllera/functiona
Use the crontab(1) command to configure your crontab (which may contain several entries, and several variable definitions, so you may have to edit it).

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

Categories