Run exec() in PHP as a different Windows user? - php

I'm trying to create a Cordova project in a PHP script, using :
$command = "cordova create test";
exec($command);
This doesn't work because the cordova command (which is a NodeJS plugin) is only available to the normal Windows user, and not the local system account that runs the Apache service (so also not to PHP).
I found some answers on here that relate to linux, but I need this to work on Windows.
Is there a way to change the user that runs exec() to my normal Windows account?
UPDATE
After adding C:\Users\<ACCOUNTNAME>\AppData\Roaming\npm to the PATH variable it works. That's where the cordova.cmd file is located (on my system).

Make the binary available to the system account. That's it.

Related

running exe app using php exec funtion make the page hangs

I am using apache server under xampp. I have some matlab exe file that I want to execute. I used this template
$tmp = exec($command, $output, $return_var);
while $command contains the exact command to execute the file using cmd.
What is happening is the page hangs and when I debug, I found that the server hang while calling this exec command.
I searched the web and tried many things like run the Apache service and my user account and give the user all administrator privileges, but unfortunately it still stuck.
Any help or advice would be appreciated.
The problem with my code was because the Matlab code is accessing file system and also reads an excel sheet. So, it was all about permissions.
The solution for this problem has 2 parts:
1. Apache should run as a a windows service with checking allow service to interact with desktop in the properties.
2. I found that there is no way to access windows COM objects to read excel in normal way. So, instead we should use xlsread in basic mode.
That's all.

PHP how to launch windows GUI application

Sorry if it seems the silly question. I could not launch windows GUI application by PHP any way.
I have tried any workaround that I have found out from the similar questions but they did not work at all.
My command:
$cmd = 'E:\soft\Notepad++\notepad++.exe E:\text.php';
I can run that command by the Window Command Line tool and it worked fine, the notepad++ launched and opened the GUI with the expected content. I would like to do that in php
I have opened the windows services and set the option "Allow service to interact with desktop" (checked) for "wampapache" service and restart it as well.
I have tried with each of following commands:
pclose(popen("start /B $cmd", "r"));
OR
system("start $cmd");
OR
exec("C:\\windows\\system32\\cmd.exe /c START " . $cmd);
OR
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($cmd, 0,false);
All of them gave me the same of result:
it just ran the application IN THE BACKGROUND, I could see that app process that is running by looking at the Task Manager of Windows, but the GUI DID NOT DISPLAY.
My PHP version is 5.4.3
Any help is appreciated.
I assume that PHP is running within Apache, which in turn is a service.
Now starting any application from service will not have its GUI displayed, as Service is running in separate session which does not allow interaction with users Desktop.
See this answer for more details:
Service starting a process wont show GUI C#
However there can be other ways to achieve this.
Create a custom C++ (or equivalent) application that will create your target GUI application for the given user. The answer How can a Windows service execute a GUI application? explains CreateProcessAsUser() for that. This method will require user name and password to be specified.
Create custom client-server kind of application. The server part will always be running inside in User mode where the GUI needs to be displayed. And the client will be called from PHP. When client is invoked, it will signal the Server part using IPC like event. Server can start the GUI application in turn.
Use the Microsoft's PSEXEC utility to start the process in GUI. However this will need user name, password and session id.
psexec.exe \\REMOTE -u USER -p PASS -i SESSION -d C:\Windows\Notepad.exe
SESSION is the session id (Use Task Manager -> User Tab to see your session ID)
USER, PASS is the user name and password for the user

Using PDFCreator's Command Line Arguments in PHP exec()

I have tried to use the Zend Framework to do my PDF conversion in PHP but cannot figure out how to install it. I found this program that allows for PDFs to be printed/converted via the command line, so I thought I could do an exec() command in PHP to run the program and convert my file on the server for me.
The program is PDFCreator from pdfforge.org and I am trying to convert .DOC files to .PDF files. This command line below works perfectly when running from cmd.exe. However, when running it from PHP I believe that it is trying to execute under the user System and not working properly. (I did a php exec('whoami') and it came back as System.) I also changed the user of which Apache opens up as to an Admin account and still have been unable to get it to work.
"C:\Program Files\PDFCreator\PDFCreator.exe" /OptionsFile"C:\PDFCreator.ini" /PF"C:\test.doc"
Any thoughts as to why it I cannot get the same behavior when running the command using exec()?
Thats because Apache user and account user should be the same, ex: your windows user is Pedro, so Apache user should be Pedro...you shoul open the services.msc and in the session tab allow apache to interact with the current desktop....in that way, the share the same windows session and desktop....but it shoud represent a security risk and it's not recommended.
Saludos.

PHP: mount USB device

I am writing a PHP script for the GUI of a Cent OS server. For testing purposes I have set up the Apache as localhost. Cent OS does not automount a connected USB device. In PHP I need to check if a USB device is plugged in then mount it and list the file content for the user. This is quite simple in bash, however I am unable to make it work in PHP. I have played around with both the exec() and the shell_exec() functions with no avail. I have tried the commands both manually i.e.:
shell_exec("sudo mount /dev/sdb1 /mnt");
and through a bash script:
exec("./mountlist.sh");
Is there a function in PHP I can call that will do the same thing, or am I missing something with the exec and shell_exec functions. Both functions work fine in the shell. Since mount is a root command I included sudo in the bash script.
You can't sudo from inside a PHP script - there is no way to type in the sudo password.
You could create a shell script and use the STICKY bit to have it run as the root user
http://www.dba-oracle.com/linux/sticky_bit.htm
That's how the passwd command can write to the password file owned by root even though you are running it as a normal user.
Also - you could grant the web user permission to mount/unmount file systems (add him to the fuse group on most systems), but that's more open-ended and rather dangerous if your web server gets hacked, so I would go with shell scripts and sticky bits for your purposes.

PHP - exec,shell_exec,system not working

i read so many similar questions but nothing works with me
im using wamp 2.2 ,Apache 2.0 , PHP V5.3.8
safe_mode=off - disabled_functions deleted from php.ini i'm trying to exec
exec("chrome.exe google.com");
nothing happens and when i try
exec("calc");
the windows shows weird message to execute calc in different user although i changed the user for the apache service to Administrator and i verify the user using
exec("whoami");
where is the problem?
Environment path for CMD and php shell might not be the same.
You either have to give entire path of the file, or export the path of chrome.exe in php first
Also, I believe that chrome.exe opening a page requires XServer, php can't open graphical interfaces in shell. These commands are not passed to actual shell as a parent, so php file can't "launch" applications for you.
Please read specifics of the exec function:
PHP exec Reference
Also try adding 2> errors.txt to see what are the errors if any in the execution of the programme.
--
The program needs to know what X server to connect to, and it needs to have permissions to connect to that server. You specify the X server with the DISPLAY environment variable; this will usually be set automatically if you are running the PHP program from a terminal in
X, or from a GNOME panel or something similar; however, if you are running the PHP script in some other manner, it likely won't know what X server to connect to.
X has various ways of specifying permission to connect to a server, but the most common one is using a file called ".Xauthority" in the users home directory. Because only the user who is logged in at the X server can read this file, they are the only user who can run GUI programs. So, if you start the PHP user as the same user who is logged in at the X server, you shouldn't have any problem with permissions. However, if the PHP program is running as a different user, you will have to give that user permission to access the X server.
Reference: http://bytes.com/topic/php/answers/838364-cant-launch-graphical-apps-php-exec-ubuntu-8-04-system
(I know that link is for linux and won't have exact same solution for Windows, but exec() still needs to know which X interface to refer to)
Stop Apache running as a service.
When windows runs a service it it is not running directly as the user that started the server or manages due to this it could well be starting chrome in a service environment so you wont see it load on your desktop there are 2 ways to get around this
Stop apache service browse to your apache directory and run httpd.exe manualy then try your script it should work or if it is not required to be running though a web request so it not using anything from the browser you can allow it to work with c:\wamp\php\php.exe yourfile.php (your php path should be replaced for c:\wamp\php)
Try PHP script?
<?php shell_exec('notepad.exe');?>
It's working.

Categories