execute php via cli passing arguments - php

how do I execute via command line a php script passing get paramateres. I have been trying to execute the following
chmod +x /media/linkstation/myfolder/import/import.php
sudo php5 ./media/linkstation/myfolder/import/import.php?id=4
but won't work Could not open input file

You are trying to mix file protocol with http protocol, that won't work.
The parameters need to be separated from the file path.
Edit:
As long as the path is correct, you can try:
sudo php5 ./media/linkstation/myfolder/import/import.php id=4
Now the first arg $argv[1] is the query string, so long as that is a single string (or wrapped in quotations) it will appear on the receiving end of the script. After that you can treat it as you like.
However there are more suitable ways to delivery parameters. You may find command line options more useful: http://php.net/manual/en/function.getopt.php
sudo php5 ./media/linkstation/myfolder/import/import.php --id="4"

Related

Is it possible to execute a cgi script from php?

I am trying to run a LaTeX parser from a dokuwiki website. The LaTeX parser relies on the mimetex cgi module, it is executed from a PhP script using the following command:
shell_exec('/var/www/cgi-bin/mimetex.cgi -f /var/www/html/test1.tex -e /var/www/html/test1.gif');
However, the command is not properly executed when running the PhP script. I ran a test command such as:
shell_exec('cp test1.tex test2.tex');
and was able to check that the file test2.tex was indeed created. I also ran the command:
/var/www/cgi-bin/mimetex.cgi -f /var/www/html/test1.tex -e /var/www/html/test1.gif
from a terminal, and it was executed without error. I also made sure that my apache server is configured so that it can execute cgi scripts following https://www.ionos.com/community/server-cloud-infrastructure/apache/enable-cgi-scripts-on-apache/
I am missing something here... but I don't see what...
You could try to execute the cgi script via a GET request, so it is executed by Apache. A simple way to do this AFAIK:
$response = http_get("http://localhost/cgi-bin/mimetex.cgi");
But you can find other ways also, like using curl.
You will need to pass the tex file as a query parameter, appending it like ?texfile=test1.tex, with proper param name.

running a php program from command line with a default argument list

I want to run my php program from the command line, but I always have to type php program instead of just typing program. How do I make it known to the shell that whenever I type program it runs the program?
Also, I have some arguments that I parse using getopt(). I also have a configuration file, which has a DEFAULT_ARGS= setting. I want the user to be able to set the default args, and if they run program without arguments DEFAULT_ARGS takes over and becomes the argument list.
Without writing my own parser, is there a way to "spoof" the argv[] variable or somehow make getopt() work with the string I've specified in the configuration file instead?
For example, if I wanted to run this on the command line: php program -a 300 --debug, instead I will have DEFAULT_ARGS=-a 300 --debug in my configuration file and simply write program to run the program. This is my end goal.
If your OS os Linux add the sheabang on top of the script
#!/usr/bin/php

Piping PHP file content to PHP command in CLI

My question relate to the use of PHP in CLI. I don't know why the piping of the content of a PHP file to the PHP command works:
cat file.php | php
like in the installation of Composer Composer Installation:
curl -sS https://getcomposer.org/installer | php
If you don't give any argument to PHP, it reads from standard input (commonly called stdin).
If your output buffering is disabled, you can try to run php without argument, and type <?php echo "test\n"; + Enter, you'll see "test". stdin is basically the stream where your keyboard writes, and stdout is basically your terminal, where echo writes.
But the pipe ( | ) changes that behaviour : the standard output of the first program becomes the standard input of the second one.
This is a quite powerful thing our nix system shells offer :-).
As to "why it works": It works because the -cli variant of the PHP binary is designed to read from stdin and process input scripts from there alternatively.
The manpage lists various execution options. See the very last line:
PHP is a widely-used general-purpose scripting language that is espe‐
cially suited for Web development and can be embedded into HTML. This
is the command line interface that enables you to do the following:
You can parse and execute files by using parameter -f followed by the
name of the file to be executed.
Using parameter -r you can directly execute PHP code simply as you
would do inside a .php file when using the eval() function.
It is also possible to process the standard input line by line using
either the parameter -R or -F. In this mode each separate input line
causes the code specified by -R or the file specified by -F to be exe‐
cuted. You can access the input line by $argn. While processing the
input lines $argi contains the number of the actual line being pro‐
cessed. Further more the parameters -B and -E can be used to execute
code (see -r) before and after all input lines have been processed
respectively. Notice that the input is read from STDIN and therefore
reading from STDIN explicitly changes the next input line or skips
input lines.
If none of -r -f -B -R -F or -E is present but a single parameter
is given then this parameter is taken as the filename to parse and
execute (same as with -f). If no parameter is present then the
standard input is read and executed.
Normally you would not do it that way and would simply execute the file by php file.php. In this case, for whatever reason you want to do it that way, you need quotes around it with quotes
php -r "`cat file.php`"
Edit You could do this as well.
Put a hashbang at the front of the file like this
#!/usr/bin/env php
<?php
//code follows here
Then execute with
cat file.php | php

PHP exec() not working properly

I am having difficulty with the PHP exec() function. It seems to not be calling certain functions. For instance, the code echo exec('ls'); produces no output whatsoever (it should, there are files in the directory). That main reason this is a problem for me is that I'm trying execute a .jar from a PHP exec() call.
As far as I know I'm calling the java program properly, but I'm not getting any of the output. The .jar can be executed from the command line on the server. (For the record, it's an apache server).
My php for the .jar execute looks like this:
$output = array();
exec('java -jar testJava.jar', $output);
print_r($output);
All I get for output from this exec() call is Array().
I have had success with exec() executing 'whoami' and 'pwd'. I can't figure out why some functions are working and some aren't. I'm not the most experienced person with PHP either, so I'm not too sure how to diagnose the issue. Any and all help would be appreciated.
The reason why you are not able to execute ls is because of permissions.
If you are running the web server as user A , then you can only ls only those directories which have permissions for user A.
You can either change the permission of the directory or you can change the user under which the server is running by changing the httpd.conf file(i am assuming that you are using apache).
If you are changing the permissions of the directory, then make sure that you change permissions of parent directories also.
To change the web server user, follow following steps:
Open the following file:
vi /etc/httpd/conf/httpd.conf
Search for
User apache
Group apache
Change the user and group name. After changing the user and group, restart the server using following command.
/sbin/service httpd restart
Then you will be able to execute all commands which can be run by that user.
EDIT:
The 'User' should be a non-root user in httpd.conf. Apache by default doesnot serve pages when run as root. You have to set user as a non-root user or else you will get error.
If you want to force apache to run as root, then you have to set a environment variable as below:
env CFLAGS=-DBIG_SECURITY_HOLE
Then you have to rebuild apache before you can run it as root.
I have found the issue - SELinux was blocking PHP from accessing certain functions. Putting SELinux into permissive mode has fixed the issues (although, I'd rather not have to leave SELinux in permissive mode; I'd rather find a way of allowing certain functions if I can).
I have a solution:
command runs from console, but not from php via exec/system/passthru.
The issue is the path to command. It works with the absolute path to command
So that:
wkhtmltopdf "htm1Eufn7.htm" "pdfIZrNcb.pdf"
becomes:
/usr/local/bin/wkhtmltopdf "htm1Eufn7.htm" "pdfIZrNcb.pdf"
And now, it's works from php via exec
Where command binary you can see via whereis wkhtmltopdf
Tore my hair out trying to work out why PHP exec works from command line but not from Apache. At the end, I found the following permissions:
***getsebool -a | grep httpd*** ---->
**httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off**
USE: setsebool -P httpd_ssi_exec 1
SEE: https://linux.die.net/man/8/httpd_selinux
Your problem is not an execution issue but the syntax of the exec command. The second argument is always returned as an array and contains a single line of the output in each index. The return value of the exec function will contain the final line of the commands output. To show the output you can use:
foreach($output as $line) echo "$line\n";
See http://php.net/manual/en/function.exec.php for details. You can also get the command's exit value with a third argument.

Why could wget not work with PHP's exec function?

My script tries to exec() wget but seems to fail (though, no error raises up). What could be the problem? Should I tune PHP somehow? I just installed Apache and PHP on Ubuntu...
Add third parameter to exec() to find out the exit code of wget.
Maybe wget is not in the (search) path of the apache/php process.
Did you try an absolute path to the wget executable?
What is your $_GET['one']? The name of a video file? A number? A url? What's $file? What' $one?
Obvious error sources:
Are all of those variables set? If $one is blank, then wget has nowhere to go to fetch your file. If $_GET['one'] and $file are blank, then your output file will most likely not exist, either because the directory can't be found ($_GET['one']) is empty, or $file is empty, causing wget to try and output to a directory name, which is not allowed.
'illegal' characters in any of the variables. Does $file contain shell meta-characters? Any of ;?*/\ etc...? Those will all screw up the command line.
Why are you using wget anyways? You're passing raw query parameters out to a shell, which is just asking for trouble. It would be trivial to pass in shell metacharacters, which would allow remote users to run ANYTHING on your webserver. Consider the following query:
http://example.com/fetch.php?one=;%20rm%20-rf%20/%20;
which in your script becomes:
wget -O /var/www/videos/; rm -rf / ;/$file $one
and now your script is happily deleting everything on the server which your web server's user has permissions for.

Categories