PHP exec and OSASCRIPT? - php

I have a little Apple script as follow:
beep
delay 2
tell application "Finder" to activate
It just makes a sound, wait 2 second and then bring the "Finder" window to the foreground.
When I run it from the command line, it works fine.
Then I want PHP to call that script using the exec() php function.
<?
$cmd = "/usr/bin/osascript \"myscript.scpt\"";
exec($cmd);
?>
It still works fine.
But when I call that same PHP script from the browser, it doesn't work! The PHP starts, the Apple script starts as well since I can hear the beep sound but its last line is not executed.
I thought that would be an environment variable thing so I made sure they were all the same way as in the terminal:
$cmd = "HOME='/Users/mikael' && … && /usr/bin/osascript \"myscript.scpt\"";
The variables are set properly (as check with env|sort) but still no luck with running my apple script inside a php script displayed in the browser and using the standard MacOS apache stuff.
Any idea?

When osascript runs from PHP, through the web server, it's not running with a login context, so it can't send Apple events to applications running on the desktop (like the Finder). You'll find that a similar issue arises if you try to use osascript over SSH.
Login contexts are a complex, poorly documented area of OS X. You may want to get your hands on a copy of Amit Singh's Mac OS X Internals: A Systems Approach if you want to learn more about them.
If you don't, though, the answer is generally pretty simple: don't depend on osascript working correctly from the web server.

OK, I may have found a way that allows the Apple script to be called from PHP from within a browser.
It is not fully satisfactory but this is what I'm going to do:
So basically instead of using the default macOS apache server, I use this one that sets up a web server & also MySQL: http://www.mamp.info/en/index.html
I mention MAMP but there may be other.
my Apple script is finally run fully using that solution.

I had the same trouble and realised that apache executes as user www.
You can change this by editing etc/apache2/httpd.conf . Change user to your user short name and group to staff.

Related

How to open an external GUI application from web browser using PHP?

I'm new to web development. I'm trying to execute a shell script using PHP's shell_exec(). Inside the script, I'm trying to invoke a GUI application(Qt). When I executed the PHP script from a terminal the application started as expected. But when I opened it from browser an empty blank page appeared.
I'm using Ubuntu with apache2 server running as service. When I searched in google, the similar problem is solved in the Windows environment by allowing apache service to interact with the desktop.
PHP Script:
<?php
$log = shell_exec('sh testcmd.sh');
?>
testcmd.sh:
./Program1
Any help provided will be highly appreciated.
It is somewhat unclear what you're asking.
If you wish that browsing to a certain web site will run a PHP script that will open a GUI app for the client to interact with, the answer is "you can't". The reason is that the way the setup works is that the server and the client run on different machines, and your PHP runs on the server machine. As such, the client never gets to see the running program.
The above is true also for Windows. The answer you quote in your question does not apply to a server running on a different machine than the client.
If, for whatever reason, you want something that works only when the server and client run on the same machine (or there is someone watching the server's display), then you need to do the equivalent of the Windows answer.
The graphics display on Linux (assuming you're not running wayland) is using a protocol called X11. In order for the display to appear, your GUI program needs two things. The first is to know which display it needs to use. This is supplied with an environment variable called DISPLAY. The second is an authorization to actually use that display.
So in order for your PHP script to run a GUI app that will show its GUI, you will need to first do the following steps:
Set the DISPLAY variable to the correct value (copy from your desktop environment).
Run xauth add something, where you can get what something is by running xauth list on your desktop environment.
If you do these two things (in this order), your GUI should show up.

run zenity from php

In my local ubuntu machine I would like to popup a message with zenity from a php script.
so i put:
exec("zenity --info --text 'TEST' " );
but nothing happens.
The same line works fine when typed in a shell.
I tried adding the --display parameter with different combinations but no luck.
Was thinking maybe exec commands need to be enabled somehow for safety reasons?
Or what am I doing wrong?
Thanks
Is this from the terminal (php cli) or mod_php? This seems to work fine on my machine on the terminal. If this is a server box it is highly likely that php is hardened. Check your php.ini.
If you're trying to use zenity from a php script executed by a browser, this is a wholly different story. As the php interpreter will be run as a different user, you need to make sure that this user is allowed to run programs on your X session. Check xauth and read up on how Xorg authenticates sessions etc., but make sure you pay attention to the possibly security implications that may result from any changes you make.

PHP exec() command wont launch python script using sendkeys

First Note: Sorry this is long. Wanted to be thorough.
I really hate to ask a question when there's so much out there online but its been a week of searching and I have nothing to show for it. I'd really appreciate some help. I am a noob but I learn very fast and am more than willing to try alternate languages or whatever else it might take.
The goal:
What I'm trying to do is build a Netflix remote (personal use only) that controls Netflix on the server (Windows 7 PC 32-bit) via keyboard shortcuts (example: spacebar to pause) after a button is pressed in a php page on my ipod touch or android phone. Currently the remote uses USBUIRT to control the TV and IR devices without issue. If you have any alternate methods (that I can build, not buy) to suggest or other languages I could learn that can achieve this, I'm happy to learn.
The issue:
PHP's exec() and system() commands will not launch the python script (nor an exe compiled with py2exe) that simply presses the Windows key (intended to press the key on the server, not the machine loading the php page). I can use USBUIRT's UUTX.exe passing arguments with exec() to control IR devices without issue. But my exe, py, nor pyw files work. I've even tried calling a batch file that then launches the python script and that batch will not launch. The page refreshes and no errors are displayed.
Attempted:
Here's a code that works
$exec = exec("c:\\USBUIRT\\UUTX.exe -r3 -fC:\\USBUIRT\\Pronto.txt LED_Off", $results);
Here's a few attempts that don't work
$exec = exec("c:\\USBUIRT\\test.py", $results);
$exec = exec("python c:\\USBUIRT\\test.py", $results);
$exec = exec("C:\\python25\\python.exe c:\\USBUIRT\\test.py", $results);
All of those I've tried without the dual backslashes and with forward slashes and dual forward slashes. I've left off passing it to variable $exec and that makes no difference. $result outputs
Arraystring(9) "
Copying everything in the exec() into command line works correctly. I've tried moving the file to the htdocs folder, changed folder permissions, and made sure I'm not in safemode in php. Var_dump returns: Array" Using a foreach loop gives no info from the array.
My logs for Apache show only
[Sat Sep 10 19:54:09 2011] [error] [client 127.0.0.1] File does not exist: C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/announce
Setup: Apache 2.2, python 2.5, and php 5.3. Running this on Windows 7 and only connect on the local network, no vpn or the like. Given every associated folder (python, htdocs, the cmd.exe file, usbuirt folder) IUSR, admins, users, and everyone with full control just for initial testing (later I'll of course tighten security up). Safe mode is off on php as well.
Notes: This code I saw on another similar issue doesn't work:
exec("ping google.com -n 1");
No errors in error.log nor event viewer. Putting it inside ob_start(); and getting the results with ob_get_clean(); gives me absolutely nothing. No text or anything at all. I've tried a lot more but I've already written a novel on here so I'll just have to answer the rest as we go. I'll post the full php source or the python script if that is needed but all it does is import sendkeys and press the windows key to pop open the start menu as a basic visual test. I don't know if its permissions, the way I have my setup running, my coding... I just don't know anymore. And again I apologize this is so long and if you do answer, I really appreciate you taking the time to read all this to help out a total stranger.
The PHP server running the script is most likely running as a different user (Network Service) than the account logged in at the GUI Console. I think this kind of setup might work under XP, and/or with the "interactive" field enabled on the service. However, I believe in Vista/7 it is blocked on security grounds. Details are a bit fuzzy as I'm now in front of a Linux box.
A solution would be to run php manually in the session that is running netflix and try again. If this doesn't work, I once wrote a simple client/server in python that takes key commands and converts them to keystrokes. Was pretty easy to do.
Figured it out thanks to the excellent help from Winston Ewert and Gringo Suave.
I set Apache's service to the Local System Account and gave it access to interact with the desktop. This should help if you have Windows XP or Server 2003, but Vista and newer there's an Interactive Services Detection that pops up when you try to launch GUI applications from php. Every command was executing correctly, but were doing so in Session 0. This is because Apache was installed as a service. For most people I would think that reinstalling without setting up Apache as a service would work, but I was considering moving to XAMPP anyway, so having to uninstall Apache helped push my decision.
Ultimately all of the codes I wrote in my original post now work as a result, and my project can move forward. I hope someone else stumbles across this and gets as much help from Winston Ewert and Gringo Suave as I did! Thank you both very much!

Run a PHP script from multiple computers?

I have written a PHP Script, I run the code like this on MS-DOS on my local PC:
C:\wamp\bin\php\php5.3.0\php.exe index.php
Now I want to upload the index.php on the web but on multiple computers (without PHP/Apache installed), how can I run index.php on ms-dos via web?
If that not possible, I can open multiple putty application on each computer and login ssh?
index.php will continues looping - it wont be good to run script on the browser because it may cause timeout via browser.
You can't. If you're using putty on each computer to log into another machine to run the script, the script will still be running on "the other" machine. All you've done is make its output appear on the local computer that's running putty. It'd be exactly the same as opening multiple shells on the server and running the script in each window.
If you want a PHP script to run "locally", then you need PHP installed locally on each machine that will run the script. And like I said in your other similar question, there are PHP compilers which can produce a .exe that MIGHT be portable, but it's not guaranteed to work. PHP can be compiled, but not all scripts are compileable.
Check out bamcompile.
It lets you compile the PHP file to an exe.
Not everything works but it is worth the shot: http://www.bambalam.se/bamcompile/
I think it uses PHP 4.something so don't know wether it works for your script without seeing it
EDIT
You said you are worried about an timeout if ran in browser.
What does the script do?
Perhaps PHP isn't you're best option.
The easiest way would be to have Apache and PHP installed on those computers, and then have a script visible on that computers that runs the command for you.
exec("C:\wamp\bin\php\php5.3.0\php.exe index.php")
This way, you can start running the script without having the script time out from Apache's end.

The horrors of working with executables on windows

I run an executable called Test.exe via exec which in turns runs Outlook.
I am able to run the Test.exe fine but I get the error:
Rejected Safe Mode action : Microsoft Office Outlook. in the windows event viewer.
If I run Test.exe myself via DOS it works fine and no errors. So its something to do with how PHP is running this exectuable. I've enabled apache to run as an admin account but the same thing happens.
What else should I be doing so that Apache can run the executable without any problems?
It works from the command line but not from the Apache process. Not surprisingly because Apache probably runs as a service, with a system account (Non-Desktop interactive).
If you reconfigure the service to run as a user with the right to logon locally and mark the service to be allowed 'Interaction with Desktop', I expect you could do this.
However, I'm at a total loss why anyone, at all, would want to start Outlook from a webserver application....
Sehe is right (but for some reason I cannot comment to his post, whatever...). Usually, you should access MS Office facilities via external code using specific OLE interfaces. You never use CreateProcess to start Outlook or Word from your program, it would be pointless because how can you control it after launching it?
I suggest you to check if you really need this, and if there's a more clean way to do it. You can try to create an external C#/VB.NET executable that performs all the automation you may need to do with Microsoft Outlook, indeed.
What is the actual scenario?
And, I don't like to correct people but if I don't, someone else will in the future and it'll be annoying. Don't say "via DOS" when you use a prompt under Windows. :D
i too am perplexed by would anyone would want to do this.
If you REALLY wanted to do this i guess you could run it as "start test.exe" or write a batch file that would run it. basically anything that would cause a different process to be the one actually launching the app.

Categories