Jump to content

Recommended Posts

I am trying to build a php page, that will send a shell command to print a pdf.

 

I am using a centos 5.4 with cups installed. I can print fine when I enter the shell command manually into the terminal screen and hit enter, but I would like a user to be able to go to load a page via the browser and it sends the print request.

 

The following is the code I am using does not seem to work:

 

<?php

$cmd = 'lpr -o sides=two-side-long-edge order2.pdf';
echo "<pre>".shell_exec($cmd)."</pre>";

?>

 

Help would be great.

Link to comment
https://forums.phpfreaks.com/topic/193285-shell-command-via-php-script-to-print/
Share on other sites

I'm assuming you have full access to this machine, which you'll likely need to fix the issue.  The issue is probably permissions related.

 

You need to figure out which user Apache is running as, probably one of: nobody, httpd, apache, apache2, or httpd2

 

Next you need to make sure that user can actually log into the machine.  For example, as root you might run:

usermod -s /bin/bash apache2

 

To allow user apache2 to log in with the bash shell.

 

Then as root, switch to that user:

su apache2

 

Now change directories into your application's directory where you're calling shell_exec() from.  Try and execute the command as the user from the console:

apache2@/var/www/html/yourapp/$ lpr -o sides=two-side-long-edge order2.pdf

 

Does it work?  Do you receive any errors?

 

When you're done, log out from the web user:

exit

 

You should be back at root.  Disallow shells for the user (pick the appropriate command)

usermod -s /bin/false

usermod -s /bin/nologin

 

I'm not Linux guru, so my advice may not be the best advice.  But it should tell you pretty clearly if your user that apache is running as can run the command properly.

 

Another feature that might stop this from working is some sort of PHP safe_mode setting or the php_suhosin (or whatever it's called) patch / module.

You type all that in the terminal screen from the root account.

 

If httpd is running as user root on your machine, then you have some serious installation and configuration issues to fix.

 

httpd should start as root and then fork to a different user account on the machine.  The user account it forks to is typically one of: nobody, httpd, httpd2, apache, apache2

 

That user account will not have the full access to the machine that root has.

 

How did you go about installing apache / httpd?

In my terminal screen, I got permission denied for the users nobody and apache when typing in the following:

 

nobody@/var/www/html/trail/$ lpr -o sides=two-sides-long-edge order2.pdf 

or 

apache@/var/www/html/trail/$ lpr -o sides=two-sides-long-edge order2.pdf 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.