None of the exec related PHP functions work under Android - php

I try to crosscompile PHP (trying many versions. now working with 5.3.8) for Android (Android 2.3.3 and Android 4.0.4. Error present in both.). Almost everything is working fine. The most important function for me is executing some shell commands but it does not work! For example this code
<?php
$s = shell_exec("ps");
echo $s;
?>
must output all started processes but in fact displays a blank screen. I try many variants with functions system(), pasthru() and others. All in vain. In php.ini disable_functions empty, safemode off.
In the Internet I found that there is an error with popen() function related to Android OS. But how do I fix this problem?!
I also found this: all exec related PHP functions segfault
This is similar to my case. But for some reasons the author does not write how he corrected the error.
UPDATE: I think I know what is wrong. This link helped me: https://bugs.php.net/bug.php?id=60081 . PHP use for exec commands "/bin/sh" but on Android there is no "/bin/sh". "sh" is located in "/system/bin/sh". I try to copy "/system/bin/sh" -> "/bin/sh" and all work properly. But now there's a new question. How to tell PHP where "sh" is?

Related

PHP exec() under Apache returns null and 255 when running a script

Ok, after having bashed my head on this for hours I decided to ask for help. I have a Windows Server 2008 running Apache 2.4 and PHP 7.1. My application must run a PHP script on the server when the user clicks a button on the browser.
This is working fine on my desktop with Windows 10. However, on the server, exec() returns "null" and an exit code of 255.
I read all I could find on exec() issues and tried the following:
exec("C:\\PHP7\\php.exe -v", $output);
I got the proper response containing PHP's version information.
Then I decided to check the configuration files:
exec("C:\\PHP7\\php.exe --ini", $output);
All files were in place.
Then I decided to perform a syntax check on my script:
exec("C:\\PHP7\\php.exe -d display_errors=1 -l C:\\Apache24\\htdocs\\script.php", $output);
No errors were found.
Finally I decided to check the user account:
exec("whoami", $output);
Got "NT Authority\SYSTEM" as expected. To make sure that the script was able to run under the SYSTEM account I used SysInternals psexec:
psexec -s C:\PHP7\php.exe C:\Apache24\htdocs\script.php
Everything ran smoothly.
In other words, the script shows no problems when executed from the command line, either under a user account or the system account. I have also proven that PHP is being properly invoked by exec().
So, then I decided to check for "hidden" errors in my code adding the following two lines to the very beginning of the script:
error_reporting(E_ALL);
ini_set('display_errors', 1);
But, no joy. And I'm out of ideas.
Can any good soul help me on this?
Thanks a bunch,
Miguel.
Finally! The key to the answer was here: PHP exec() git fetch failing with return value 255.
I was unable to see any errors, even adding the "2>&1" pipe redirection to my commands. After reading that article, I learned that proc_open() is way better than exec(). Quoting from PHP's documentation:
proc_open() is similar to popen() but provides a much greater degree of control over the program execution
So, I replaced my exec() for a few lines of code (refer to the example in the manual) and found that the problem was being caused by the Zend Opcache, which was enabled for CLI. The quickier solution for me was to disable it in the command line:
php.exe -d opcache.enable_cli=0 myscript.php
Voilà! Problem solved!

sh: virtuemart_user_id: command not found

Helo,
This error appeared in my error.log. We are using AWS's Elastic Beanstalk with an EC2 instance with PHP, Joomla 3.4.8 and VM 3.0.12
I have tried to link the error with the access.log to see if it was related with a specific URL. But it is hard since I don't get the date inside the log when the
sh: virtuemart_user_id: command not found
appears.
I have read, related with this issue, that it could be something related with ubuntu. I have searched for shell_exec() and exec() inside the project to see if virtuemart was trying to execute a command, but I didn't get nothing like that.
Any idea why is this happening?
The shell_exec() and exec() methods are not the only way to execute commands, you can use the backsticks (`) like this: $output = ls -al; echo "<pre>$output</pre>"; (read this entry from php documentation for more information: http://php.net/manual/en/language.operators.execution.php)
Since you are using virtuemart, I guess virtuemart_user_id might be one of your database fields. This kind of fields are often refered to by using the backsticks marks (in this case, `virtuemart_user_id`).
Are you sure you are always using `virtuemart_user_id` inside string variables?

Execute a php script within a running php script

I'm trying to execute a php script from a php file, but it seems that i can't get the correct bin path of the php.exe. Its not doing anything as far as i can tell. Lets assume service-oauth.php is a simple echo.
EDIT3:
I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented.
EDIT2:
I tried calling the script directly from the server console as #Dagon suggested and it works, both using the php env variable and the php path to the bin, its clear that the path is correct, but something is preveting to get the output or to run the script using the php exec() function
service-oauth.php
<?php echo "Hello there"; ?>
And this is my script:
$basePath = plugin_dir_path( __FILE__ ); # Wordpress function, asume it works.
$fileToExc = $basePath . 'service-oauth.php';
# PHP_BINDIR: /usr/local/bin
# PHP_BINARY: /usr/bin/php
# exec("which php") /usr/bin/php
$phpPath = exec("which php");
$output = exec("$phpPath $fileToExc");
print_r($output);
There are a lot of answers on stackoverflow that recommend any of those 3 options i commenented in the code, but none of them seems to work, not sure if it is the path or something else that is not working. I've tested this script on my localmachine (windows) and it works (even though i had to use a hardcoded path to the bin since i wans't getting the correct path), but i'm testing on my production server (linux) and is not working.
Note: Let me be clear, that this is not a duplicate, i've tried the following answers in these posts and many others, and it didn't work for me:
How to get path of the php binary on server where it is located
Can't execute PHP script using PHP exec
PHP exec to run a file
How to call shell script from another shell script?
Execute a PHP script from another PHP script
I've also tried using .exe at the end of the binary(windows localmachine), using php-cli instead of php, and i've tested the excec function and it works, but not in this case. Also tried with a shebang in the called script.
It's propably something simple that i'm not aware of but i've been spending a lot of hours searching and testing and nothing so far. Any help is appreciated.
EDIT:
Using scandir on the bin folder shows
scandir("/usr/bin/")
Array(
...
[632] => php
[633] => php-cgi
[634] => php-cli
...
)
Tested if in safe mode using ini_get('safe_mode'), but it seems off.
I fixed it using php-cli instead of php or the php bin path, i must admit that i tried this before but it seems something else was off when i tried this (one of the first things i tried). The answer provided by Keith in Can't execute PHP script using PHP exec , so it ended being a duplicate :S, thanks for the help to those who commented. – Zagen

Wkhmltopdf works fine in the terminal but not in the php

When I am using wkhtmltopdf in the terminal it works fine and pdf is generating correctly.
But when I am trying to use this in the php using 'system' command, it fails and gives apache error that 'cannot connect to X-server'.
In php, I used like this.
system("wkhtmltopdf $url output.pdf");
I tried 'exec' instead of system but giving the same problem.
Please help me out.
It might be a permission error if it works via command line but not PHP. Are you working locally or on a remote server? If you're on shared hosting, try contacting support to see if you have permission to call system, exec, or shell_exec.
Alternatively, try redirecting your standard error to a text file, maybe that will steer you a bit in the right direction:
system("wkhtmltopdf $url output.pdf 2> /tmp/error.txt");

Why has my PHP system() command stopped returning output to my scripts ... i used to work!

I have a PHP script that calls a .bat file using system(). The output is written to the screen and I derive some values from parsing this output. This is running on windows 2003 IIS server. PHP v5.2.0
Specifically I am using this script to launch an Amazon EC2 instance and assign an IP address to it. It has worked great for me so far but recently the problem started.
Here is the code
$resultBatTemp = system("cmd /C C:\Inetpub\ec2\my_batch_file_to_launch_instance.bat");
$resultBat = (string)$resultBatTemp;
$instanceId = substr($resultBat, 9, 10);
...
Once I have this instace Id I can run another batch file that calls associates an ip address with this instance. It would appear that the instance does get launched but I never get the output on the screen.
For some reason this has all stopped working, the page freezes and never refreshes. I also need to completely exit safari or mozilla otherwise all pages from the website fail to load. Only when I relaunch the browser can i view the website again. I've connected to the webserver that hosts these scripts and checked PHP error log but nothing shows there. I've opened a DOS prompt and entered the code from the bat file that way and it connects to amazon and launches the instance fine. Ive isolated this bit of code and removed the system command and the rest of the script runs fine, so it appears that the hold up is with outputting the results of the bat file.
Recently I have purchased a new domain name for the site so this script is running from this domain. Might this cause the problem?
thanks
------------------------------------------------UPDATE-----------------------------------------------
Well hope this helps someone, I didnt find out what was wrong but created a new PHP file with a simple system command that called a .bat file, and a non-existent .bat file expecting to get an error back but nothing - just the usual hang for ages. So I restarted IIS and this fixed the problem. Dont know what was wrong but that did the trick.
Maybe first check what the system() call returns. According to documentation it will return FALSE in case of failure. Also, including your my_batch_file_to_launch_instance.bat in the question might help in solving it.
Try using the passthru function
Also make sure that all your commands are safe use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.

Categories