Apache/PHP + LibreOffice - php

I've installed the LibreOffice RPMS (have tried both 3.5.3 and 3.4.6) on my CentOS machine, and I'm trying to make PHP run a shell script that calls LibreOffice. In the shell script, it runs executes this line:
/opt/libreoffice3.4/program/soffice --headless -convert-to $extension.pdf "$1" -outdir $folder
However, this command will cause the following message to appear:
./createpdf.sh: line 8: /opt/libreoffice3.4/program/soffice: Permission denied
The line itself is fine. If I echo it and run it manually in SSH, it works fine. This is most likely because I'm running it as a different user (note: not as the user that installed it, and not as root), with different permissions.
Desperate as I was, I've already tried chmodding the entire libreoffice folder to 777, and tried to make the 'apache' user the owner. No luck there. Would anyone have a clue as to why it's not letting apache run it, and how I can solve this?

As an alternative to running from the command line, have you considered running UNO (the Open/Libre Office alternative to COM) with the PUNO PHP wrapper. That way, you don't need to worry about permissions

See also Universal Network Objects (UNO): there are Python, Java, etc. bridges for use it. There are applications for simplify the use for convertions, see Docvert and JODConverter (jODconverter and pyODconverter).
All of then can be called as web-service or exec by PHP.

Related

Unable to run inkscape in PHP shell_exec() command

I have a design tool extension that is used on a website I'm working on. The design tool uses inkscape command line to export images. There is a php interface to work with the command line operations which ultimately calls shell_exec($inkscapeCmd). After noticing the image files weren't being exported, I created a few tests to try and debug. I changed the execute line to shell_exec($inkscapeCmd . ' 2>&1') in order to see the error message:
sh: inkscape: command not found
...which is odd since it's definitely installed and accessible. I added a check for the user on my test page to make sure the commands were being executed by a user with access to inkscape:
$processUser = posix_getpwuid(posix_geteuid());
echo 'user: ' . $processUser['name'];
Then I ssh'd into the server to confirm that I could run the same commands as that user, and was able to run them without any issues (which also confirmed inkscape was in the PATH). I'm able to run other, basic shell commands from PHP without issue, like so:
echo shell_exec('ls');
But now I'm at a loss; I'm not sure what else to check to determine why I'm getting the 'command not found' error. Any direction would be helpful.
The server (rather old, I know):
CentOS 6.7
PHP 5.3.3
Inkscape v0.47
The process does not have the location for inkscape in it's path.
You will have to supply to full path to the executable.
Example
/usr/bin/inkscape
At the command line type 'whereis inkscape' to find is location.

Running wp-cli commands through SSH with phpseclib

I am automating installing multiple WordPress blogs on a server. Basically, I need to run multiple wp-cli commands.
Using phpseclib and doing exec(), doesn't work...
When I do something like:
$ssh->exec('wp core download');
I will just get:
/usr/bin/env: php: No such file or directory
Even though I can run it fine, in a normal ssh session...
If I try and $ssh->write the command out and do '\n' it doesn't seem to do anything. Even if I just try to do a simple command like: touch foo.txt
Although that test "touch" command will work with exec...
The system is Ubuntu 14.04...
Any ideas?
I have to connect via SSH from PHP to do this for multiple domains on a server, as new customers come on.
The path to PHP probably needs to be defined. When you SH in with the regular SSH client it's probably running any number of Bash initialization files.
In light of this I have two thoughts.
Try to use a PTY. eg.
$ssh->enablePTY();
$ssh->exec('passwd');
echo $ssh->read();
More info: http://phpseclib.sourceforge.net/ssh/pty.html
Are you doing $ssh->read('[prompt]'); after doing the write("command\n")? You may need to read the stream to get the command to actually be run.

Printing Word document from PHP in WAMP

So, I have this printer that doesn't have drivers for any OS other than Windows, so, I wanted to run a server on a Windows PC that is attached to the printer, so that I can upload a file from any device in the local network and have a PHP script invoke MS Word on the file to print it. The command I have to invoke Word to print a document works when I give it from the command line and the file upload works, but when the command is run from the PHP script with system() or exec(), it does not work. I see WINWORD.exe running from the Task Manager, but with no GUI showing and no printing happening. I am running the latest WAMP on Windows 8 and I have tried going into services.msc and changing the logon for wampapache to my user which is an administrator or enabling the checkbox to allow the service to interact with the desktop, but none of that worked. This is the actual command I am using: "C:\Program Files\Microsoft Office\Office14\WINWORD.EXE" C:\path\file.txt /q /n /mFilePrintDefault /mFileExit. It's in a batch file that I run using exec()
EDIT: I am not trying to print to a network printer, this is a printer that is connected to the server computer and I am using the newest version of WAMP, so I can't use the PHP functions in the linked question. Also, I am trying to print Word documents not raw text. This is specifically about PHP's exec() not working for this case.
I honestly think this is your best bet:
How To Make Microsoft Word Documents with PHP
Since you seem to be having problems with the .bat file approach, I'd suggest trying a Powershell script instead. For example:
print_doc.ps1 =>
Param([string] $filePath)
# This should handle any file type, including an MS-Word .doc/.docx
Start-Process -FilePath $filePath -Verb print
In PHP, you can exec() the script something like this:
'powershell.exe -noprofile -executionpolicy bypass -file \path\to\script\print_doc.ps1 "' . $path . '"'
STRONG SUGGESTIONS:
Make sure the path is valid INSIDE OF PHP.
You can use file_exists() and/or realpath() to verify this.
Enable verbose error logging. In Powershell, I like to use try/catch blocks and the $error object.
Good luck!

Git pull “permission denied” when using shell_exec in PHP

I'm trying to make a hook on bitbucket, that executes a php file, and this file executes the pull command:
shell_exec('/usr/local/cpanel/3rdparty/bin/git pull');
The pull command works fine on the SSH console, but the PHP returns the error:
Permission denied (publickey). fatal: Could not read from remote
repository.
Please make sure you have the correct access rights and the repository
exists.
The command --version shows the path to git is right, whoiami returns the same user on both, so I don't know if it is a permission issue.
What can be going wrong?
Edit: An additional issue: the alias I added for git don't work on PHP, only the full path as above. Via terminal it works just fine. Maybe it's the same reason why the key don't work in php.
Edit 2: $PATH is different on both.
When you run this command within a PHP script you are not running the command as yourself:
shell_exec('/usr/local/cpanel/3rdparty/bin/git pull');
The reason it works from the terminal console is you run the command as yourself from the console. But on a web server, you are not the user running the command. Remember: When you run PHP on a web server, it is a an Apache module. Meaning the web server user—which could be www-data, root or even apache on some systems—is running the PHP script which then runs the shell_exec command.
So it would never work as you have it setup. Perhaps you can kludge something together that would allow a key-pair to be used by the web server for these purposes, but that seems like a security risk waiting to happen.

Accessing kd Debugger via php

I work at a small computer shop, and we have to analyze windows minidumps all the time. My idea was to install the Windows Debugging Tools on a windows PC and use apache/PHP as an interface to it. That way I could just set up an HTML upload form that would accept the minidump file, run it through KD, then spit out the output.
It nearly works. I created a special user just for apache so I could assign it write privaleges to C:\symbols, and I use the following code:
<?php
$kdScript = "\"\\Program Files\\Debugging Tools for Windows (x86)\\kd.exe\" -c \"!analyze -v;Q\" -y srv*c:\symbols*http://msdl.microsoft.com/download/symbols -z ";
$kdScript .= $_FILES["myFile"]["tmp_name"];
$output = `$kdScript`;
print("<pre>$output</pre>");
?>
The problem I'm having is that the symbols are not downloaded as they should be. I've verified apache is running as the user I think it is by calling "whoami" from inside backticks. I've verified that I can run the windows version of wget from within backticks, so I have access to the network. I can file_put_contents() into a new file under C:\symbols, so I have file creation permissions.
Also, I tried having PHP simply output the command to the browser so I could copy and paste it into a terminal. I was able to run a command prompt as my apache user via "runas", paste the command from PHP's output into the prompt, and it worked as expected, downloading all the symbols it needed to C:\symbols. Of course, I had to point it to a dump file NOT in the PHP temp directory, but this shouldn't make a difference.
What could be the problem? Just as a side note, all of this is local on a trusted pc in a company that has a total of 3 employees/owners. Security for this project is irrelavent.
Not sure what your exact problem is, but the symbol server client code is finicky and not very debuggable, it took us lots of tinkering to implement our version of this. You can always direct folks there or use it yourself:
http://www.osronline.com/page.cfm?name=analyze
-scott

Categories