PHP how to launch windows GUI application - php

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

Related

PHP exec() , showing executed exe file in task manager but not displaying

i have used simple PHP script to execute exe file through php script, below is my PHP code
<? exec('"C:\Windows\notepad.exe"');?>
my problem is, when i check task manager after executing above code, notepad.exe is there but it is not diplaying
same way if i execute below mentioned code
<? exec('"C:\Program Files\WinSCP\WinSCP.exe"'); ?>
task manager shows WinSCP.exe, but winscp is not diplaying
can anyone tell me why applications are not showing ?
Whatever your web server is (Apache, Nginx, IIS...) it's clearly not configured to run with the credentials of your administrator user account. That's the sensible default. Background services are designed to run unattended.
You have several choice, from worse to better:
Kill security in your web server configuration. If web server runs as service, open Windows Service Manager and configure it to run with your account (don't forget to come back here if you change your password in the future). This way you allow any stranger in your computer, local network and/or internet to run programs in your name, in this case Notepad and WinSCP.
If the web server allows it, run it from a command-prompt in your own session. It's similar to #1 but will at least not run 24/7.
Any of the above, with a restricted account.
Don't use a web server. PHP can run from the command-line just fine, what means that you can even create a *.bat file and assign an icon to it.
As about your code, when it finally works your script will sleep until you close Notepad. This can be a bug or a feature depending on your needs.
Had the same problem with WAMP 2.4 & windows XP, the following helped:
Win+R, services.msc
edit wampapache and wampmysqld to allow affecting desktop.
Alternatively you can try:
Win+R, services.msc
edit wampapache and wampmysqld to log on as the same user.

PHP Web App to run Console SUDO Commands (Deciding how to do it)

My company have a cloud dedicated server, hosted in google cloud, running centos 7 with apache2, php5.5 and mariadb running.
The webserver is running a private application for business clients and companys. Every client has his own database and subdomain, so they, and their own clients can access his applicacion going to http://theirname.example.net/
I've created an interactive command line script for client creation. It creates user and secure password, ftp custom folder, create mysqldatabase and populate it with a sql file, create subdomains and other thing. Also i've made another version of this script with no user interaction, receiving parameters as the client name via command line arguments ( /path-to/script.sh usertocreate mysqluser mysql pass).
So, here is the deal, i want to create a web interface, password protected, just available for my company IP address, this interface should be able to run these SH scripts (not the interactive ones) with sudo permissions.
I was thinking on create a subserver in other port (like http://example.org:2501) using another instance of apache (or other webserver) which runs with a specified user with sudo permissions enables only in the neccesary folders.
Before doing anything, i've created a PHP script which runs commands to console, and tried to run SUDO commands with that. Allowing apache user to sudoers list (just to make it work during develop). I could run these scripts from my web app without SUDO permissions, but they where not working at 100% (since some commands require SUDO). When i try with sudo i receive a code 127 error responde (permission problem).
I stopped there and decided to investigate the best way to do this.
I have full control of the server machine. Apache2 and everything normal is running well. (mariadb, proftpd with passive mode active -100 ports added- )
SELINUX is DISABLED. Firewalld Running
SSH is available for use
I can install another webserver in another port to accomplish this. If a lot of HTTPD configurations should be changed to accomplish this, i will preffer to install another webserver
I can also install any 3rd party software.
I'm a PHP Developer with low experience in other programming languages, if it's necesary to invoke any other programming language to do this (maybe like Python) i would love some docummentation links
Access to this web application would be limited only to my company's static ip address and will be protected
Any thoughts/ideas ? Thanks in advance
PS: If someone want to edit my text and add some colours and format, edition will be aprovved
EDIT TLDR: I want to run another httpd in another with a webapi. This webapi should can run console commands as SUDO. Access to this webserver will be limited to my company's IP. I'm not sure if this is the best way to do it and i want opinions. Also, i'm not pretty sure if its possible to run sudo commands from php without any trouble.

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.

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

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.

LAMP Web Server: Executing Application on Server Side can't detect USB device

I have set up a LAMP Web Server and I am looking to run an application on the server side when the client clicks a button on the servers web interface. This application will look for a certain USB Device, by Serial Number, open it up and send a packet of bytes to the device.
I have an index.html, which only has a button with an action to call my test.php file which uses shell_exec() to call my application.
When the application is invoked through the web interface, the application writes out an error indicating that it couldn't open the USB Device (this a built in error for this application, so the application works, it just can not locate the usb device).
But when I invoke the application via the Terminal, the application finds the usb device and writes to it no problem.
I am looking for some advice! Simply is what I'm doing feasible? If so, how can I get the application to find the usb device when invoked via the web interface? I have a feeling it has something to do with permissions, you never know.
test.php:
<?php
echo shell_exec("/home/pi/FDTI_test/FDTI_test_application");
?>
NOTE:
The usb device is connected, works great with its driver, and is connected to the server via usb.
The application works when invoked via the terminal on server side, but not when invoked via web interface.
I think your on the right track with this being a permissions issue.
In a typical LAMP stack, the php process runs as a module in the apache process, unless you've configured it differently. In my server OS of choice, the php process runs as the user 'www-data' by default.
Probably the easiest solution would be to give sudo permission to your web user account, and set the sudoers file to NOPASSWD. This is very insecure, so only do this in rare cases.
<?php echo shell_exec("sudo /home/pi/FDTI_test/FDTI_test_application"); ?>
The next easiest option is to give the web user account permission to write to the USB device directly. Depending on your distribution, you may only need to add the user to the 'adm' group.
sudo usermod -a -G adm www-data
Again, this may not be the most secure method, but more secure than the first option.
Lastly, you could look into the hardest solution which would be to install a patched version of apache which allows suexec. This is about as equally as insecure as the second option, but much more difficult to implement. (I would have included a link to a tutorial, but I'm limited to 2 links as this is my first answer.)
Hope This Helps!

Categories