Using PHP to execute ssh/command line properly - php

So I need a bit of help in regards to ssh/command lines being run from php.
Out of the gate - yes exec is enabled on the server - its our own dedicated box
The setup. I have created a script that uses cpanel's api to create sub domains
for a particular account. That's all fine and dandy and everything works as expected.
Now - we have a wildcard SSL installed as well.
The long and short of it is this. Under the present conditions if you pulled up:
https://user.domain.com you would only see the top root directory for the account,
NOT the directory it should be in. But if you use it without the https - it does what
it should. Now it's a dedicated box with HostGator and they had this script they
installed for me to do what needs to be done - and that works as expected.
The script HostGator installed is located at:
/var/cpanel/userdata/account/mdssl
So the line they gave me to run the script and do what it should do is as follows:
./mdssl clone domain.com user.domain.com
So my question is - because I have never really worked with shell stuff
inside of php, would the following line be a workable example of how to execute the script?
shell_exec('/var/cpanel/userdata/account/mdssl clone domain.com user.domain.com');
or do I just need the exec command? And should the opening line be any different?
I just need to tap that line using php and pass the 'user' field to it and have the
script do its thing to clone the setup for the SSL so that the wildcard features work
properly and the users account lands in the proper directory it should.
Any insight is appreciated! :)
Thank you very much.

I've only used exec() or system()
They're always tricky and tend not to behave as expected.
I would start off with
ini_set("display_errors", 1);
exec('/var/cpanel/userdata/account/mdssl clone domain.com user.domain.com', $output);
var_dump($output);
See what the output says or if a PHP error is occurring.
You may also need to escape out of the current directory of whatever default you're in with ../ and make sure apache user has access to mdssl (or whatever user your system uses). You may need to sudo it.

Related

PHP script not looking in includes folder

I have a new install. Ubuntu 22.04, Apache2, PHP 8.1 and I'm trying to use PHPMailer. My test page with phpinfo all looks good and shows the includes directory /usr/share/php. My form and script are in a subfolder of my document root and it loads fine. But when I fill out the form and the script starts it errors out unable to find PHPMailer. It's only looking in the subfolder the script is in, not the global includes directory. I don't want to put the mod in every form folder and I know it's supposed to work from the includes folder. Is this an Apache or php config issue? Or something I have to specify in the vhost?
Just verifying and closing out with the answer
I found the issue early today. Thanks to bad/tired eyes, I wasn't seeing the leading dot so it was looking for ./usr... Once i fixed that i was able to sort out the Ubuntu method of calling it from the libphp-phpmailer pkg autoload.php. I was able to get the script working with a few code corrections. The only thing I didn't sort out was PHPMailer:ParseAdresses but i only had a few recipients for this form so it wasn't that big an issue. My form is back online and my managers are happy 😊

File not found with subdomain and php's system() function

In a php script i want to check certain file types, which i do like this:
$info = explode('/',system("file -bi -- uploads/img/test.gif"));
if ( $info[0])
echo 'ok';
This is working just fine, as long as the url is without subdomain, f.e domain.com, but is not working under en.domain.com. It appears, that the file is just not found.
On the other hand, if in the same script i check for existance of the file:
if ( file_exists("uploads/img/test.gif"))
echo 'exists';
The file is found, wheter the current url is domain.com or en.domain.com. Why is the file not found, if the system() function is used?
solved
Friends, i finally figured out the solution, after nearly three working days. Maybe you can imagine how relieved i am.
the solution was in php options to activate mod_rewrite. this was allready activated in regular domain, but not for subdomains. guess i could have thought of this earlier. Even though your answers didnt lead me to how to solve this, i still thank you for your input, as it was going in the same directions i was trying to solve it myself the past days.
The shell command will not necessarily take your file root to be the same as the php command (one will be the user's root directory and the other will be the webserver I think), use absolute path(s) in the shell command and this will probably work how you want it.
You could work out where the two url's are with the file command by using pwd, e.g.
echo system('pwd');
Or you could use finfo-open instead of the shell command which should avoid the problem.

CodeIgniter + Command Line + Cron + Cpanel

SOLVED:
Crap... why is it always you figure something out right AFTER you finally decided to ask for help!!
If anyone else is having this problem, try running from /usr/local/bin/php instead of /usr/bin/php to get codeigniter to pick up on the URI segments.
QUESTION
I have the latest installation of codeigniter and everything seems to be working fine locally. I recently put my files on my server and everything except my cron command is working.
When I try to call a controller through cron (as described here: http://codeigniter.com/user_guide/general/cli.html), I am not getting the controller. Instead, the output I get is simply my default controller (login page).
This worked fine locally and the index.php IS getting called because I'm getting emailed the output from the default page.
Someone asked a similar question here: CodeIgniter Cron Job on Shared Hosting? Responders suggested that it was a problem with Cpanel (which is what i'm now trying to use to set up the cron job), but talking to my hosting provider, they said the whole command is being run. Nevertheless, CI isn't grabbing the URI for the controller. I also checked to make sure the base_url is set correctly (which is proven by the file working fine through a URL call).
Any thoughts? I just can't seem to figure out why it's not grabbing the URI when in command line format: /usr/bin/php index.php controller_class method
This was the answer:
Use /usr/local/bin/php instead of /usr/bin/php to get codeigniter to pick up on the URI segments.
Here is solution first you need to find path from phpinfo document_root path
php5 /home/abc/public_html/index.php folder_name controller_name function_name
It's 100% working
I'm glad you got it working Mike. I ran into similar problems and ended up using the CI Cron Job Bootstrapper on a recent project that was running on a cPanel host. I'm just posting this incase it helps someone else.
For me this is what worked:
php54 -f /home/{cpanel_user}/public_html/index.php controller function

Apache doesn't execute my copied PHP files, but testing.php works

I've just installed LAMP on my Ubuntu 9.10 machine, and everything works fine except when I copy my PHP files from another computer.
The LAMP guides I've followed also made me create a phpinfo() test file, which works, but when I try to type in e.g. index.php absolutely nothing happens - just a blank page in FireFox. :(
The files are in the exact same directory.
I'm thinking it's probably something with permissions and so on, but since I'm new to both PHP and Ubuntu, I'm kind of lost. It's like I can't create a PHP file with my file browser, but only by using the terminal - like when I created the testing.php from the LAMP guide.
Whaddayaknow... I made an error, tried to:
echo "Hello" world
which, even though I'm a PHP noob, I clearly know is wrong.
I think I'll have to figure out how to enable some sort of error reporting, a blank page is clearly not good enough.
You mean you have a index.php (copied from another computer) and a test.php (edited by hand, with a call to phpinfo()) in the same apache directory, the second works from your browser and the first doesnt ?
That can be a permission issue, or some compilation error in your php.
About permissions, for files should be readable from the apache server (more precisely, form the user that runs the apache server). You can type chmod a+r index.php.
YOu can also check your apache error logs (location dependent on installation). In any case it's vital to know where the error logs are if your are developing a web site.

PHP script unable to find a file when called by a flash program

I'm working on a program right now that calls the script mail.php located in /var/www/vhosts/company/httpdocs. mail.php is trying to execute require_once dirname(__FILE__).'/../pear/Mail.php' to do an smtp send and the require_once is failing. My PEAR directory's located in /var/www/vhosts/company/pear. I then tried to add /var/www/vhosts/company/pear to the include_path but require_once is still failing.
I decided to take a step back and replace mail.php as a simple script that does file_exists(dirname(__FILE__).'/../pear/Mail.php') and prints the result to a logfile. When I run the script independently, it works fine and returns 1. When the flash program runs it, it's returning nothing. Printing out dirname(__FILE__).'/../pear/Mail.php' returns the same regardless if I run the script independently or if the flash file runs it. I've also tried chmod 777 on the Mail.php PEAR file but that didn't do anything.
Any ideas on what's going on?
I would bet anything it has to do with two things:
1) Flash/Actionscript normally does not access local file paths.
In other words, it probably isn't even executing the file.
As a compiled client-side module it needs an actual web-accessible URL. Part of the problem here is the design itself. Try it with an HTTP request within the actionscript and you will have better results. If you don't have access to the flash file.. well tough beans there.
Now if you are running a mail routine through actionscript? I would say that is a security risk. You are better off having the actionscript pass the routine to an AJAX receiver routine that checks session credentials and then sends mail.
2) CWDUP restrictions on the server.
Depending on certain server configurations, excecutables normally do not have access to filepaths outside their own root. (i.e. an executible cannot call ....\another directory\other file.) Some servers will allow this, but many wont.
You may want to make sure your PEAR directory is in your php.ini path variable. This way you don't need to use CWDUPs in your directory name at all, it will find it in the includes directory. (which is normally how pear modules work.)
So rather than using a buncha dot-dots.. try working down from the top.
$mailpath=$_SERVER['DOCUMENT_ROOT'].'\include\mail.php';
As a last resort, you can try copying the mail.php routine into the same directory and see if that works. If that still fails, then its your include path to PEAR. (as the mail.php is probably calling PEAR functions.)

Categories