Why I cannot run a shell command using PHP 7? - php

I'm having trouble running a python script using shell_exec or exec using PHP 7 (PHP Version 7.0.33-0+deb9u3).
If I run: var_dump(shell_exec("/usr/bin/python3 /home/pi/Documents/GTranslate/translator.py")); or var_dump(shell_exec("/home/pi/Documents/GTranslate/translator.py"));,
I obtain back NULL.
No problems with user permissions
I've checked the permission running:
$scriptPath = "/home/pi/Documents/GTranslate/translator.py";
var_dump(array(
'file' => is_file($scriptPath),
'readable' => is_readable($scriptPath),
'executable' => is_executable($scriptPath)
));
I've obtained everything true.
Missing safe_mode from php.ini
In my php.ini (the one shown by phpinfo();) there is no fields like safe_mode. Reading the PHP Wiki since PHP 5.4.0 the function has been removed.
shell_exec() is working
The funcion shell_exec() is not disabled because if I run the command:
echo shell_exec("ls -halt /home/pi/Documents/GTranslate/translator.py");
I obtain back:
-r-xr-x--x 1 pi pi 1.7K Aug 11 12:13 /home/pi/Documents/GTranslate/translator.py
Where is the problem?

Related

Is there an alternative to this command, that can be run on a windows machine?

I have written a PHP script which runs this command in a shell:
dns-sd -B _roomcast-capi._tcp
The expected result is something containing the 'Instance Name', like this:
Browsing for _roomcast-capi._tcp
DATE: ---Tue 02 Apr 2019---
16:22:49.344 ...STARTING...
Timestamp A/R Flags if Domain Service Type Instance Name
16:22:49.345 Add 3 5 local. _roomcast-capi._tcp. roomcast-d36aa71c4b694059811ebf105537f91e
16:22:49.345 Add 2 6 local. _roomcast-capi._tcp. roomcast-d36aa71c4b694059811ebf105537f91e
It is working fine when i am on my OSx machine, but from my research it will probably not have the same result when i try it on my windows PC - is this correct?
If this is the case, then my question is this: Are there any alternative commands that i can run on windows, that will return a similar result to the command above?
So it turns out that the dns-sd command is part of the Bonjour service pack, so this explains why it is working on OSx and not windows.
The simple solution was to install Bonjour on my windows machine, and the dns-sd command started working in the command line.

Different $_SERVER output when running from command line or system()

If have these very simple scripts:
foo.php:
<?php
system("php -c /etc/php7/cli/php.ini /some-path/exec.php");
?>
exec.php:
<?php
print_r($_SERVER);
?>
Running exec.php from command line I get the expected output for $_SERVER:
...
[LESSKEY] => /etc/lesskey.bin
[NNTPSERVER] => news
[MANPATH] => /usr/share/man:/usr/local/man:/usr/local/share/man
[XDG_SESSION_ID] => 2
[HOSTNAME] => AAEB-DEV203LD
[XKEYSYMDB] => /usr/X11R6/lib/X11/XKeysymDB
[HOST] => AAEB-DEV203LD
[TERM] => linux
[SHELL] => /bin/bash
...
When exec.php is executed by the system function in the foo.php script called in a browser tab then the output for $_SERVER is totally different and it looks more or less exactly like the Environment section from phpinfo():
...
[APACHE_CONF_INCLUDE_FILES] =>
[mpm_found] => true
[APACHE_CONF_INCLUDE_DIRS] =>
[SYSCONFIG_FILE] => /etc/sysconfig/apache2
[APACHE_START_TIMEOUT] => 2
[HTTPD_MODULE_IDS] => actions_module alias_module ...
[APACHE_SERVERNAME] =>
...
What is the problem:
In the $_SERVER output - running via system() function - there is missing lot of information which we need.
I didn't find anything in the Internet which can give me a hint why the output differs so much.
OS: SLES 12.3
PHP: 7.2.10
Apache: 2.4 MPM
My question:
Why does the output differ so much when running under command line and system() within the Apache session and can I get the same output for the system() function when calling exec.php from the command line with php -c /etc/php7/cli/php.ini /some-path/exec.php?
Good. I could sort it out how it works, it wasn't clear to me.
Running php from command line PHP sets into $_SERVER all exported environment variables. Therefore $_SERVER has the entry HOSTNAME.
Running exec() from a script which is executed in a browser tab PHP sets $_SERVER with the content of Environment section as displayed by phpinfo().
When I want to have set the entry HOSTNAME in $_SERVER I have to call putenv("HOSTNAME=value"); before exec().

How to get php readline() to default to Vim mode?

This is on CentOS 7.2, PHP version 5.6.20. According to readline_info():
<?php
print_r(readline_info());
gives
Array
(
[line_buffer] =>
[point] => 0
[end] => 0
[library_version] => EditLine wrapper
[readline_name] =>
[attempted_completion_over] => 0
)
OK, so readline() is using EditLine. I created a ~/.editrc file with:
bind -v
The permission is 664. I then tried:
<?php
$line = readline('Enter: ');
But it doesn't seem to read the file, and it's not in Vim mode. Doing an Alt-X followed by a manual command of "bind -v" puts it in Vim mode, so I know it has Vim mode.
I read somewhere else that I might need to manually set the path in an environment variable. So tried setting the full path:
$ export EDITRC=/home/work/.editrc
Still nothing. I've also seen that "export EL_EDITOR=vi" is supposed to work, but still doesn't work.
Does anyone have any way to make this work? I really need Vim mode to work.

Cant access environment variables in php

Im having issues accessing OS environment variables in php
I have apache/php installed on a centos 6.3 image
in httpd.conf mod mod_env.so is loaded
in php.ini I have set variables_order = "EGPCS"
restarted httpd (many times)
in shell if I type "env" I get
DB_PORT_28017_TCP_PROTO=tcp
HOSTNAME=c6188a8bd77f
DB_NAME=/rockmongo/db
DB_PORT_27017_TCP=tcp://172.17.0.36:27017
TERM=xterm
DB_PORT_28017_TCP_PORT=28017
DB_PORT=tcp://172.17.0.36:27017
DB_PORT_27017_TCP_PORT=27017
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/etc/php.d
DB_PORT_27017_TCP_PROTO=tcp
DB_PORT_28017_TCP_ADDR=172.17.0.36
DB_PORT_28017_TCP=tcp://172.17.0.36:28017
SHLVL=1
HOME=/
DB_PORT_27017_TCP_ADDR=172.17.0.36
container=lxc
_=/usr/bin/env
OLDPWD=/etc
which has the variables im after, however if I execute print_r($_ENV); in php I get
Array ( [TERM] => xterm [PATH] => /sbin:/usr/sbin:/bin:/usr/bin [PWD] => / [LANG] => C [SHLVL] => 2 [_] => /usr/sbin/httpd )
have also looked in $_SERVER & $GLOBALS.
Interestingly if I execute php -i in shell I see the env variables set correctly in _ENV
I should note im running this in a docker container, however I dont believe it is a issue as variables display correctly in #env & #php -i. I think I have a issue with my httpd/php config
Anyone have advice for this?
thanks
I ended up having a few options
if docker container needs to run multiple services, setting env vars to /etc/environment will make them available for all users. I added the following line to my Dockerfile CMD
CMD ["env | grep _ >> /etc/environment"]
if docker container runs a single service, its best to set the entry point to the desired application, env vars will automatically be passed to application user. this is my Dockerfile CDM & ENTRYPOINT to run apache
ENTRYPOINT ["/usr/sbin/httpd"]
CMD ["-D", "FOREGROUND"]
Dagon is correct.
Unless you logged in as your web server User (apache?) you may not see the same environment variables. You can see them easily with a phpinfo test file though:
<?php
phpinfo();
?>
Or you can set your own with a .htaccess file:
SetEnv HTTP_MY_VARIABLE "my value";
From dwitz: You can also make the environment variables available system wide with this:
env | grep _ >> /etc/environment
Sorry can't comment yet... So had to create an answer.
Recently i wrote a library to get values from environment variables and parse to the PHP data types. This library can be used to parse environment variables to PHP data types (like the casting to integer, float, null, boolean), parse the complex data structures like a JSON string and more with the contribution of the commnunity.
The library is available here: https://github.com/jpcercal/environment
As you say, the environment are already loaded. Then, to get the values from environment variable (independently of the environment CLI, Apache, Nginx, PHP Built-in Server and more) to do it:
<?php
// ...
require "vendor/autoload.php";
// ...
var_dump(Cekurte\Environment\Environment::get("YOUR_ENV_VARIABLE_NAME"));
Enjoy it.

php custom C++ module works from command line, not on webserver

I made a custom PHP module with C++ and Swig. It works from the command line, but not with my webserver:
php index.php
php-cgi index.php
Both of those work fine.
I'm using lighttpd and php. I didn't configure these in any special way. I just installed them using sudo apt-get install.
Unfortunately if I make a webpage I get this:
Fatal error: Call to undefined function minikey_to_wif() in /var/www/index.php on line 6
Calling function_exists("minikey_to_wif") returns False too.
The phpinfo() does not show my module called minikey, and shows the same configuration path as the file I edited (/etc/php5/cgi/php.ini):
extension=/path/to/php-ext/minikey/minikey.so
I also tried copying it to where the other PHP extensions seem to be installed (/usr/lib/php5/20090626+lfs/) but that didn't work either.
I've been stopping, and starting lighttpd countless times. Each time, when I run ps aux | grep php, there are no results. I've also rebooted a few times to no effect. I have no idea what's up.
OK found the answer.
The extension relied on a library which was installed in a non-standard location. Normally I set LD_LIBRARY_PATH in ~/.bashrc. But when the web server ran the extension, it didn't have that environment variable.
Fix was to create a file in /etc/ld.so.conf.d/genjix.conf with /home/genjix/usr/lib and run ldconfig as root.
Try running phpinfo() using lighty. Make sure that its pointing to the correct php.ini.
If you think this is the problem you can launch php using the -c switch from inside lighttpd.conf with "bin-path" => "/path/to/php-cgi -c /path/to/php.ini":
e.g.
fastcgi.server = (
".php" => (
(
"bin-path" => "/path/to/php-cgi -c /path/to/php.ini",
"socket" => "/tmp/php.socket",
"max-procs" => 4,
"idle-timeout" => 30,
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "4",
"PHP_FCGI_MAX_REQUESTS" => "1000"
)
)
)
)

Categories