0

When running this from command line as root it works

unoconv -f csv $file

But when running it as www-data this error is returned

Traceback (most recent call last):
  File "/usr/bin/unoconv", line 1114, in <module>
    office_environ(of)
  File "/usr/bin/unoconv", line 203, in office_environ
    os.environ['PATH'] = realpath(office.basepath, 'program') + os.pathsep + os.environ['PATH']
  File "/usr/lib/python3.4/os.py", line 633, in __getitem__
    raise KeyError(key) from None
KeyError: 'PATH'

update

echo shell_exec('echo $PATH');
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
clarkk
  • 27,151
  • 72
  • 200
  • 340
  • What is the value of `www-data`'s `$PATH` and the root's `$PATH`? Try setting `www-data`'s path before running the command. See https://dirtyvital.wordpress.com/2012/11/11/editing-path-temporarily-or-permanently-in-unix-using-bash-shell/. – Jim K Dec 21 '15 at 19:58
  • what do you mean by `www-data`'s path? – clarkk Dec 21 '15 at 20:46
  • I mean set the path to something so that it is not empty. That way the key will exist in the `os.environ` dict. For example you could set it to `.` or a path to an empty directory. – Jim K Dec 21 '15 at 21:17
  • have updated my question.. – clarkk Dec 21 '15 at 21:19
  • Ok, it doesn't seem to be empty. What is `shell_exec` -- are you using PHP? If so then please add that tag, and post the actual PHP code that contains the call. I have not used PHP before but maybe I can help. – Jim K Dec 21 '15 at 21:28
  • The call is `echo $PATH` and it outputs `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin` – clarkk Dec 21 '15 at 21:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98613/discussion-between-jim-k-and-clarkk). – Jim K Dec 21 '15 at 21:33

3 Answers3

1

centos 7.3 with php via php-fpm the env in php is cleaned by php-fpm

u can use putenv to set evn["PATH"] in php code, examples

putenv("PATH=/sbin:/bin:/usr/sbin:/usr/bin"); 
var_dump(shell_exec('unoconv -vvvv -f pdf -o 123.pdf 123.doc));

or u can set env use one line shell cmd

var_dump(shell_exec('PATH=/sbin:/bin:/usr/sbin:/usr/bin'.' unoconv -vvvv -f pdf -o 123.pdf 123.doc));

or u can change /etc/php-fpm.d/www.conf to pass the env to php, add this line

clean_env = no

and the restart php-fpm

systemctl restart php-fpm.service
leafisme
  • 11
  • 1
0

The PHP call you used (pasted from chat):

exec("unoconv -f csv $file")

My guess is that exec() is giving you an environment that is too limited. To work around this, you could set up a polled directory. The PHP script would copy files to be converted into the polled directory and wait for the files to be converted.

Then create a bash script (either running as root or a somewhat more secure user) to run in an infinite loop and check the polled directory for any incoming files. See How to keep polling file in a directory till it arrives in Unix for what the bash script might look like.

When the bash script sees incoming files, it runs unoconv.

Community
  • 1
  • 1
Jim K
  • 12,824
  • 2
  • 22
  • 51
0

Found a solution myself by running libreoffice directly

sudo libreoffice --headless --convert-to csv --outdir $tmp_path $file
clarkk
  • 27,151
  • 72
  • 200
  • 340