Encrypting with GPG using PHP exec - php

I try use php exec function to encrypt an given string like
exec("echo test | /usr/local/bin/gpg -e -a -r name#host.local --trust-model always", $output, $encrypted);
echo $encrypted;
That same command works fine in command line and outputs the encrypted message. But for whatever reason I get always 2 as exit code when running in PHP. I figured out that it could be a permission problem. But how to solve it? I read about setting --homedir but without success :(
BIG thanks in advance!
PS: I cannot simply use the gnupg php moduleā€¦

Related

How to pipe Program into PHP (-r) in Windows?

In Windows 7, running php.exe from C:\wamp64\bin\php\php5.6.25\php.exe,
I find that the following prints the PHP usage instructions.
echo 4;|php -r <-- prints php usage instructions
C:\wamp64\bin\php\php5.6.25>echo 4;|php -r
Usage: php [options] [-f] <file> [--] [args...]
php [options] -r <code> [--] [args...]
However, a friend claims that the same command echo 4;|php -r (or perhaps he means echo '4;'|php -r works for him using Linux.
How can I get this behaviour in windows?
Note I'm aware of php -r used to run some code passed as a parameter following it, i'm asking about it running a file or accepting stdin.
There's a few points of confusion here, so I'll do my best to itemize what's wrong.
First, the original command can't work on Linux/POSIX shell because it's not valid:
echo 4; | php -r
Where ; has significant meaning, it's a command separator, and a command can't begin with |.
Fixing this, you get:
echo '4;' | php -r
Where that is at least valid as far as the shell is concerned, but it's still not enough for PHP to deal with. The -r argument requires a second term, code, which is the code to be evaluated. This needs to be supplied inline, not externally as you usually would.
Specifically the -r flag does not mean "run the input file as if it has <?php ... ?> surrounding it" but instead it means "run this bit of code in PHP mode".
For example:
echo '4;' | php
Technically works, but it's not evaluated as PHP code, so it's pointless.
The version that does work in POSIX shell is this:
php -r `echo '4;'`
Where that inlines the output of echo '4;' into the command-line argument itself. I'm not sure Windows can do that without PowerShell involved.
The -r flag is intended for quick little snippets like:
php -r 'echo 5 * 9;'
Where that prints 45 as expected.
Try
echo 4 | php -r
instead of terminating echo 4 and piping nothing into php.
Not that it should matter, "-r" means nothing to php. Nor does 4 (apart from rhyming).

PHP calling diff with two strings

I'm able to call diff via exec() just fine with files, like so:
exec('diff -N -u '.escapeshellarg($oldfile).' '.escapeshellarg($newfile), $lines);
However, attempting to do this with arbitrary strings fails:
exec('diff -N -u <(echo '.escapeshellarg($oldstring).') <(echo '.escapeshellarg($newstring).')', $lines);
If I copy the command being run into bash, it works just fine. But when run in PHP I get no output. Even 2>&1 doesn't yield anything. Capturing the status code yields 1, which should indicate that diff found differences, but I still get 1 even in the case where $newstring === $oldstring.
So I'm not quite sure what's going on. I can only assume that, for some reason, exec doesn't like process substitutions? Any ideas?
PHP's exec runs the command with /bin/sh, which does not support process substitution (even when sh is provided by bash).
You can instead run your command explicitly with bash -c.
Sadly, PHP has no convenience functions for safe and robust execv style execution, so the easiest way to do that to build your diff command and then escape the whole thing:
exec('bash -c ' . escapeshellarg('diff -N -u <(echo '.escapeshellarg($oldstring).') <(echo '.escapeshellarg($newstring).')'), $lines);
What shell is being used? Make sure diff is in the $PATH, otherwise command will fail.

Why is PATH on mac not working? [duplicate]

Can anyone explain how python 2.6 could be getting run by default on my machine? It looks like python points to 2.7, so it seems like which isn't giving me correct information.
~> python --version
Python 2.6.5
~> which python
/opt/local/bin/python
~> /opt/local/bin/python --version
Python 2.7.2
~> ls -l /opt/local/bin/python
lrwxr-xr-x 1 root admin 24 12 Oct 16:02 /opt/local/bin/python -> /opt/local/bin/python2.7
When I generate an error, I see what's really getting run. Why could this be?
~> python -error-making-argument
Unknown option: -e
usage: /Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
And how can I correct it?
From suggestions in comments:
~> alias
alias cp='cp -i'
alias gcc='gcc -Wall'
~> type python
python is /opt/local/bin/python
Bash uses an internal hash table to optimize $PATH lookups. When you install a new program with the same name as an existing program (python in this case) earlier in your $PATH, Bash doesn't know about it and continues to use the old one. The which executable does a full $PATH search and prints out the intended result.
To fix this, run the command hash -d python. This will delete python from Bash's hash table and force it to do a full $PATH search the next time you invoke it. Alternatively, you can also run hash -r to clear out the hash table entirely.
The type builtin will tell you how a given command will be interpreted. If it says that a command is hashed, that means that Bash is going to skip the $PATH search for the executable.
I just checked my .bash_profile, and it contained the following:
# Setting PATH for MacPython 2.6
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:/usr/local/git/bin:${PATH}"
export PATH
Commenting this out has fixed my problem.
If someone can tell me why which and type still gave incorrect answers, I'd be very grateful, and will give them a check-mark!
Thanks for all your guidance!

php shell_exec echo

I'm working on interfacing a microcontroller with a lamp server. I am trying to run the command echo -e -n "data \r" > /dev/ttyUSB0 using shell_exec in php but with no results. It works just fine from the command line. Doing a little experimenting, I discovered that echo -e -n "1 \r" actually echoes -e -n 1. Is there a reason it won't take the -e or -n options?
Here's my code:
<?php
shell_exec('echo -e -n "1 \r" > /dev/ttyUSB0');
?>
Instead of using shell_exec and echo, why not use PHP's filesystem functions?
file_put_contents('/dev/ttyUSB0', "1 \r");
There are some other functions too, try this function maybe you get your answer.
exec(command, $output);
This function takes a command and assigns to $output an array where each element is a line of the generated output.
I ran into similar problem, calling from php
php > echo shell_exec("echo -e aaa\tbbb");
-e aaa bbb
note, that output contains "-e", while I have expected that 'echo' command will interpret -e as flag and would not send it to output.
After doing some investigation I came to following conclusion:
when exec or shell_exec are called from php - new shell interpreter is launched.
php launches "sh".
When I was running on CentOS sh was a symlink to bash, and exec("echo ...") worked as I would have expected.
Now I am running Ubuntu. sh is a symlink to dash, not bash!
And final root cause - builtin echo command in dash does not have/understand '-e' flag, so it just forwards it to output
This is an old question but it goes what worked for me in case someone else comes across with this as well.
Yesterday night I've messing around and struggling with this myself. For this to work, the command you need to use should start by calling the echo bin directly /bin/echo... instead of only echo.
Also don't forget to use single quote /bin/echo... instead of double quote to avoid PHP null byte detection error (this part you did correctly).
I had a similar problem running with -e, I was trying to change the password from a php
running this didnt work
exec("echo -e \"$pass\\n$pass\" | passwd $user");
It said passwords dont match. Checking the echo i saw that -e was includes as a part of the echoing so i changed it to
exec("echo \"$pass\\n$pass\" | passwd $user");
And then it worked.

how to use php call linux GPG encrypt file with a passphase

I have a task which need to
use gpg to encrypt the upload file in php
my code is:
("echo '1234' | gpg --passphrase-fd 0 -c /path/aaa.jpg ");
it works by paste the code in linux
but not work by php any solution
You need to use gnupg_decypt() to decrypt the text.

Categories