Open a web browser in a PHP cli app - php

Disclaimer: I did not think through the right language to use for this project before I developed it. I should have used Python for the webbrowser.open() function knowing that I needed to open browsers. But anyways.. A shot in the dark.
I am working on a PHP command line application. I am almost done, and the last feature allows the user to open a browser using the command line, much like redmine-cli's redmine-cli open [issue #] command.
Checking the source, I can tell that Redmine CLI uses the webbrowser.open.. But I am using PHP [the worst language to make a CLI app out of?].
I knew they wouldn't work, but a shot at the dark, I tried to user the header() function in PHP, as well as embedding the window.open() JS.. Which, obviously doesn't work.
I want to open a web browser in my PHP cli app. Is it possible, or should I just rewrite the app [sigh] in Python?

Related

Warning: exec(): Unable to fork

OK, having issues. My PHP script works except for 1 line.
This line tries to execute an outside command with exec().
I get:
Warning: exec(): Unable to fork
I tried searching for an answer. Several sites say I have to set the permissions on cmd.exe. Whether I do that in system32 or syswow64, I'm not sure.
Also not sure what user to add. ISUR or the user (lime) I get from get_current_user().
Windows won't let me add any users to the security list (right click on file, properties, security, edit).
It also won't let me use icacls cmd.exe /grant IUSR:F (access denied in system32)(The handle is invalid in syswow64)
I've already added full access rights (ISUR, SYSTEM, SERVICE, NETWORK SERVICE, lime, Everyone, and IIS_ISURS) to the folder where a file will be created. I realize this is overkill, and have to pare this down.
I'm going to have to do this in both Windows 7x64 (dev machine) & Windows Server 2012 (production).
So many examples I find are either geared for previous systems, or don't go into enough details.
Can someone please help me? I need to get the script working. Thanks
You do not provide enough information to answer your question...
If your PHP script is going to be executed in a Windows OS, perhaps you could have a look at the COM functions, instead of exec(). COM functions will give you way more control over what you're trying to do (Office automation, perhaps?)
COM is much more powerful than exec(), as it allows you to control the resulting application window itself, if there is one. For example, you can start a new process with an invisible window, or a minimized window.
Some examples can be found in the PHP exec() page itself.
Basically, you can do something like the following, to get an application running:
$shell = new COM("WScript.Shell");
$shell->Run("notepad.exe");
$shell = null;
Googling for "php WScript.Shell" will yield a lot of results on the subject.
Edit: Also, you'll need to add this into your IIS web.config:
<identity impersonate="true" userName="youruser" password="yourpass"/>`
This will set the user that IIS will impersonate when launching processes.

How to package a PHP application\script?

I have a script that uses php and curl to auto logon to a site and perform some actions.
That bit is ok but I now want to send the script to other people so they can use it.
So I can create a batch file that executes the script.
The problem will be the users won't have php instaled on their computer and would probally have to enable curl in the php.ini file too.
To ask the user to perform all these actions would be messy. Is there anyway I could create a basic instalation or something like that, that would package the whole thing toghether?
Thanks for any answers :-)
There's phc -- the open-source PHP compiler.
On another note, are you sure your users will have curl installed? And if not, and you decide to distribute it with your app, will they have all curl's dependencies installed?
Found this googling http://www.bambalam.se/bamcompile/
Maybe it's overdoing things but you can
look into Titanium Desktop
http://www.appcelerator.com/products/titanium-desktop-application-development/
Its Open Source, Cross Plataform and free. It basically lets you create a desktop application with web app technologies ( sort of like Adobe Air), and it has support for php scripts. This will of course mean that you have to create an User Interface with at least a button to run your script.

Convert PHP-MySQL web application to desktop app (exe)

I have developed a PHP-MySQL web application, which is a school-based project.
My client wants this application to be converted into a .exe file such that it can be installed on his desktop and use it.
How the PHP website can be converted to a .exe file and can it be run without the need of a database/server software?
Please advice.
The convenient solution is not to convert the website to .exe. I think it will be better if you have portable server/php/mysql and make the website work from a usb or CD with autorun.
NuSphere's PhpDock claims to do this: It serves as a deployment helper and comes with a bundled web server. However, I don't know about the database part, and it's not free.
PhpDock enables you to deploy any PHP web application as a Stand Alone Windows Desktop application w/o any changes in the code.
I don't know that particular product, but I have been using their IDE for years and am quite happy.
try using a site-specific browser. it will make a desktop app that is basically a portal running to your webapp. try this one:
https://mozillalabs.com/prism/
It allows alot of advanced features like system tray icons and such. I have used it many times!
Hope this helps, JL
Short answer: Not possible.
Long answer: It depends.
You could install a web- and database server on his machine (or create an installer that does it) and run the application locally on his machine.
or
You keep the application on a server and just provide a launcher that opens his browser and points it to the URL of the application.
As Artefacto mentioned, it might be a good idea to switch to SQLite instead of MySQL but depending on how your application is written it might require a lot of code and SQL Query changes.
No. You have at least to remove the dependency on MySQL (and use e.g. sqlite instead).
Then, you would either have to:
Convert the webpages to windows dialogs. This would completely change your application (e.g. what would originally be http "form submissions" would be someting completely different). At this point, it'd much easier to write a .NET application
Bundle a web server (e.g. Apache) with PHP installed.
Another try would be to turn your php project into PHP-GTK (http://gtk.php.net/).
Yet another one is to give HPHP a try (https://github.com/facebook/hiphop-php/wiki/) and try to turn the generated C code into something like a .DLL in .NET and use it for the logic while coding the UI in say, C#.
Just create a simple program in C or C++ that will just add icon in Start menu, desktop and Quickstart. If your client clicks the icon it will open the default OS browser and point it to URI of your application online.
That might fool your client :)
Or maybe it will be enough for him (he might be asking you to convert it to exe because he can't remember URI or something - ask him what is the reason).
You can use xampp open-source project to pack your PHP site into an executable file.
Use the following steps:-
1. Download Xampp source code.
Add your PHP file inside htdocs directory(Ref:- https://sourceforge.net/p/xampp/code/HEAD/tree/win32/xampp/htdocs/).
Now compile the XAMPP source code and distribute it.
For DATABASE creation and initial data loading in the database, you can code your site in such way that if database is not created, it redirects the page to install.php which do the database creation and data loading task using sql file provided(you need to add SQL file containing database structure and required data).
Don't forget to delete the SQL file post installation of database.
Not sure that's gonna be possible but have a look at:
WinBinder
WinBinder is a new open source
extension for PHP, the script
programming language. It allows PHP
programmers to easily build native
Windows applications.
(source: winbinder.org)
Solution 1:
There are several solutions to convert your web application into a desktop application, the one I prefer is the open source solution: PhpDesktop, but unfortunately it only supports SQLite.
Best Solution:
To convert your PHP application with MySQL I know a paid solution that does this: 'ExeOutPut For Desktop', it is the best for this job
Php desktop is the way to go, it's actually very simple to modify to the version of PHP you want to use and is open source too https://github.com/cztomczak/phpdesktop

C++ Serve PHP documents?

I am writing a small web server, nothing fancy, I basically just want to be able to show some files. I would like to use PHP though, and im wondering if just putting the php code inside of the html will be fine, or if I need to actually use some type of PHP library?
http://www.adp-gmbh.ch/win/misc/webserver.html
I just downloaded that and I am going to use that to work off of. Basically I am writing a serverside game plugin that will allow game server owners to access a web control panel for their server. Some features would be possible with PHP so this is my goal. Any help would be appreciated, thanks!
The PHP won't serve itself. What happens in a web server like Apache is before the PHP is served to the user it is passed through a PHP parser. That PHP parser reads, understands and executes anything between (or even ) tags depending on configuration. The resultant output, usually still HTML, is served by the web server.
There are a number of ways to achieve this. Modules to process PHP have been written by Apache but you do not have to use these. PHP.exe on windows, installed from windows.php.net, will do this for you. Given a PHP file as an argument it will parse the PHP and spit the result back out on the standard output.
So, one option for you is to start PHP.exe from within your web server with a re-directed standard output to your program, and serve the result.
How to create a child process with re-directed IO: http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx however, you won't be writing the child process, that'll be PHP.exe
Caveat: I am not sure from a security / in production use perspective if this is the most secure approach, but it would work.
PHP needs to be processed by the PHP runtime. I'm assuming the case you're talking about is that you have a C++ server answering HTTP queries, and you want to write PHP code out with the HTML when you respond to clients.
I'm not aware of any general-purpose PHP library. The most straightforward solution is probably to use PHP as a CGI program.
Here's a link that might be useful for that: http://osdir.com/ml/php-general/2009-06/msg00473.html
This method is nice because you don't need to write the HTML+PHP out to a file first; you can stream it to PHP.
You need execute the PHP page to serve the page it generates.
The easiest thing for you to do would be to add CGI support to your webserver in some basic form. This is non-trivial, but not too difficult. Basically you need to pass PHP an environment and input, and retrieve the output.
Once you have CGI support you can just use any executable, including PHP, to generate webpages.

Making a distribultable standalone program in PHP

I've decided to code some applications in PHP that are supposed to run offline in the user's machine. However, I can't seem to find an user-friendly install wizard to create a local server in where the script will run. Any ideas?
PS: Here's an example of what I want: http://www.nolapro.com
You could go to the old school route and try using PHP-GTK.
Text Tutorial here: http://www.kksou.com/php-gtk2/References/Compiling-standalone-PHP-GTK2-applications-on-windows-using-PriadoBlender.php
or you could go the route that I believe has much more promise: Adobe AIR + PHP
It has the added bonus of running on any platform!
Video tutorials here: http://www.vtc.com/products/Adobe-AIR-PHP-Development-Tutorials.htm
There's also a new player in the game, Appcelerator. It lets you write your code in whatever language you want (PHP, Ruby, Python, etc) and compile it for the platform of choice (iPhone, Android, Windows, OSX). Parts of it are still beta, but it looks unbelievably slick & cool, and there are lots of tutorial videos. http://www.appcelerator.com
I hate to advocate this, because it just feels so wrong. You would probably be better off using a language inteded for use for stand-alone applications, if you're going to be doing this often or in a production setting.
With that said, a colleague of mine used to use the Bambalam PHP to EXE Compiler for this. He actually had a profitable product built around it.
Bambalam will generate an EXE that doesn't rely on any external DLLs, based on your PHP code.
http://www.bambalam.se/bamcompile/
If you have a webapp written, you maybe want to deploy on client, a possible way is use wapache, which is a standalone apache bundled with your application, and an integrated (IE) browser control inside.
http://wapache.sourceforge.net/
A new feature of PHP 5.4 & 6 can help you, the builtin web-server.
http://php.net/manual/en/features.commandline.webserver.php
With this feature you can host locally your php app without external webserver, and access on localhost.
If you really talking about a client application you are really wrong to do this.
If you want to wrap a server + browser to deploy your web based application so it runs local you might check out three options:
1) Deploy a simple webkit browser (you can get a QT Webkit Browser in 30 lines of code) and an apache server that is installed somewhere standalone (not via the apache control script as this uses port 80 and i probably conflicts with another installed webserver.
2) Look at the Firefox PRISMA solution. I have read about this only in a news article but it wrapps the firefox around one single start URL. You have to deploy a webserver in the same way as
3) Try to wrap it as a HTA application. Search the corresponding info on MSDN.
I would prefer (1) as you can add special application interacting code as needed.

Categories