How do I build a Program that accesses an online script - php

I want to write a program to contact a PHP script online, and show the HTML output. Basically, it will be a browser that only access the programmed URL.

Similar to Seva's answer but a little better integrated with the Windows GUI. I would put together a simple HTA that used JavaScript to load the required page into an IFRAME. HTA's are executable and provide a simple GUI that is basically IE without all of the controls.
A google search for "Microsoft HTA" should get you going, clearly you already know HTML so putting an HTA together will be easy as it is just HTML and some scripting in either JavaScript or VBscript.
http://technet.microsoft.com/en-us/scriptcenter/dd742317.aspx

You don't need a program. Just execute the URL as a file. In Windows, that'll open the browser. If you need to ship at least something, ship a BAT file with the following contents:
#start http://www.mysite.com/
The builtin start command means "find the right executable for the specified parameter and call it, passing the parameter to it". The default browser is the one registered to handle the http schema.
Or you can ship a URL shortcut. To make one, open the site in IE, and drag from the address bar to the desktop. That'll give you a file with a .URL extension that you can
"execute", either from the shell or from the command line.

It doesn't matter if there is a PHP or Python or anything else on server because scripts give their output in HTML.
What you need is some kind of HTTP-client library like libcurl

Related

Embedding a web browser within a php script

In my project I have to do some screen scrapping.The source pages return data after executing the javascript embedded within them.In my php script I fetch the page using file_get_contents() and as usual,it returns the page simply as text.My question is that,is there a way to get the final output from the webpage (the output after executing javascript).
I know some of you might suggest embedding a webbrowser inside and using that to execute the page.But how to do that?.Is there a working browser available.Or is there executable non GUI versions of opensource browsers such as chromium,so that I can run it as a CGI script or something
You will have to have some real browser like client for this, php alone won't cut it. For automation purposes you are most likely want a "headless" (without gui) browser like PhantomJS (the new hotness). Check out this answer.

Get HTML after Javascript - server application

I need a way to get full HTML content of a page after Javascript has loaded and executed.
It must be built as server side application (Linux) and can include any 3rd party software (some browser without GUI or anything else that could help).
Can this be done using PHP or Cpp, if not what other options do I have ?
This is a strange subject - I'm having hard time finding information on it.
Thank you for any help.
So I found something on subject - Any way to run Firefox with GreaseMonkey scripts without a GUI/X session , but if anyone has something to add I'm still open for suggestions.
It seems that Node.js could help
You want this http://phantomjs.org/. It uses JavaScript but lets you do anything you could do in a web browser (including viewing current state of the DOM).

Emulating PHP's CLI in a browser

I'm considering the idea of a browser-based PHP IDE and am curious about the possibility of emulating the command line through the browser, but I'm not familiar enough with developing tools for the CLI to know if it's something that could be done easily or at all. I'd like to do some more investigation, but so far haven't been able to find very many resources on it.
From a high level, my first instinct is to set up a text input which would feed commands to a PHP script via AJAX and return any output onto the page. I'm just not familiar enough with the CLI to know how to interface with it in that context.
I don't need actual code, though that would be useful too, but I'm looking for more of which functions, classes or APIs I should investigate further. Ideally, I would prefer something baked into PHP (assume PHP 5.3) and not a third-party library. How would you tackle this? Are there any resources or projects I should know about?
Edit: The use case for this would be a localhost or development server, not a public facing site.
Call this function trough a RPC or a direct POST from javascript, which does things in this order:
Write the PHP code to a file (with a random name) in a folder (with a random name), where it will sit alone, execute, and then be deleted at the end of execution.
The current PHP process will not run the code in that file. Instead it has to have exec permissions (safe_mode off). exec('php -c /path/to/security_tight/php.ini') (see php -?)
Catch any ouput and send it back to the browser. You are protected from any weird errors. Instead of exec I recomment popen so you can kill the process and manually control the timeout of waiting for it to finish (in case you kill that process, you can easily send back an error to the browser);
You need lax/normal security (same as the entire IDE backend) for the normal PHP process which runs when called through the browser.
You need strict and paranoid security for the php.ini and php process which runs the temporary script (go ahead and even separate it on another machine which has no network/internet access and has its state reverted to factory every hour just to be sure).
Don't use eval(), it is not suitable for this scenario. An attacker can jump out into your application and use your current permissions and variables state against you.
The basic version would be
you scripts outputs a form with a line input
The form action points to your script
The script takes the input on the form and passes it to eval
pass any output from eval to the browser
output the form again
The problem is, that defined functions and variables are lost between each request.
Would you could to is to add each line that is entered to your session. Lets say
$inputline = $_GET['line'];
$_SESSION['script'] .= $inputline . PHP_EOL;
eval($_SESSION['script'];
by this, on each session a the full PHP script is executed (and of course you will get the full output).
Another option would be to create some kind of daemon (basically an instance of a php -a call) that runs on the server in the background and gets your input from the browser and passes the output.
You could connect this daemon to two FIFO devices (one for the input and one for the output) and communicate via simple fopen.
For each user that is using your script, a new daemon process has to be spawned.
Needless to say, that it is important to secure your script against abuse.
Recently I read about a PHP interpreter written in Javascript php.js, so you could write and execute PHP code using your browser only. I'm not sure if this is what you need in the end but it sounds interesting.
We've tested some products at my university for ssh-accessing our lab servers and used some of the Web-SSH-Tools - they basically do exactly what you want. The Shell-In-A-Box-Project may be bound to any interpreter you like and may be used with an interactive php-interpreter, if desired (on the demo-page, they used a basic-interpreter). The project may serve as a basis for a true PHP-IDE. These have the advantage of being capable of interacting with any console-based editor as well (e.g. vi, emacs or nano), as well as being able to give administrative commands (e.g. creating folders, changing ownerships or ACLs or rebooting a service).
Mozilla also has a full-featured webbased IDE called Bespin, which is also highly extensible and configurable.
As you stated, that the page is not for the public, you of course have to protect the page with Authentication and SSL to combat session hijacking.

Opening windows application (media player) using php

I have written a .php script. When there is some event, it writes to a file. It's working perfectly!
Now I want to open Windows Media Player or any other player using this php script as I also want to play a sound when the event occurs. I tried embedding the sound file as follow:
echo "< embed src =\"$file\" hidden=\"true\" autostart=\"true\">";
But it only writes to the file n do not play the audio, when the event occurs.
I came across 'exec' command which didnt work. (I am not sure about how it works. An example would be a great help!)
Does anyone know how to start a windows application using php script?
EDIT REMOVED
Thanks,
Sagar.
You can play a wave file in your server side folder in a php file by adding this
<?php exec('c:\windows\system32\cmd.exe /c START C:\website\beep.wav');
?>
and this would play the beep.wav usually through whatever sound appy is installed on your machine, like windows media player.
You cannot start console based application through Apache+PHP application. The reason is, as Apache webserver is running as a system service in a separate logon session, it cannot start UI based applications which requires a login console session.
Anyway, you can embed the audio inside the html page. Refer the http://www.w3schools.com/html/html_sounds.asp page to get more details.
Comment about running Windows GUI programs using PHP: http://www.php.net/manual/en/book.exec.php#87943
But, I think you only need to embed player on your page using some HTML tag as Aravind described.
SagarJ,
You can embed the audio file in html and will be executed by the browser/browser plugin application already installed in the system.
So, you need not to worry about that.

Easiest way to execute local file from Firefox?

I am developing a web application.
I would like to extend my error messages (and their backtraces) so that I can click on the mentioned file, and have it opened automatically in my PHP IDE.
I would like to make this a easy-to-activate feature so that whoever works on the web application, can easily map the error message to point to their local copy of the site, and open their IDE.
What - short of developing a custom FF extension - is the simplest way you can think of to execute a local command (a batch file that calls the IDE) on click in Firefox on Windows(7)?
I have looked for extensions but had no luck. Maybe using another extension like Firebug or Greasemonkey?
Security is not an issue, as this is supposed to work on the developer's workstation only and I can change my local Firefox's settings.
You can add a new protocol (like "edit://") to windows (http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx) and write a small handler program that picks a filename from the "edit://" url and passes that file to the editor. This way i taught windows to understand txmt links (http://blog.macromates.com/2007/the-textmate-url-scheme/) in the way my mac does.
There's not a way to do this with javascript. But it looks possible with a firefox addon. Have a look at this.
http://mozex.mozdev.org/
MozEX is an extension which allows the user to use external programs for these actions:
* edit content of textareas (possibly utilizing a spell-checker, color syntax etc.)
* view page source
* handle mailto, news, telnet and FTP links
* download files
* ... and many more :)
The universal handler lets you enter a list of protocol schemes, e.g., "abc://,def://" and a
command to handle them. So you just have your application generate a url that begins with
your chosen (made up) protocol, and mozex will intercept a click on the url and send the
url to your chosen command as a paramater.
I think this is exactly what you want.
I think that the closest you can get to this, is by having the configuration of the web browser associate a particular mime type with a given "helper application" (here the IDE program), and to have the HTTP server return such a file.
Otherwise, security concerns dictate that browser would not run any "abritrary" program/logic on the client.
Pekka,
After reading the thread so far, it seems that you want to build an application that somehow authenticates with the server--i.e.: the "average user" wouldn't have access to it. If this were the case, then delivering it through the browser is an impossibility without writing a custom extension.
Running authentication through GreaseMonkey is difficult, but once the client is authenticated, there is no real way to "run" the trace.
If the server generates a batch file or some kind of instruction set (script, shortcut, etc.), you can simply configure the browser (or have the local instance of your app configure the browser) to run the file. The problem here is that you have no way to automatically authenticate!
The only other way I can imagine that you could get this to work is via a Java applet, which would only be cumbersome and require Java to initialize every time you wanted to import a trace.
The problem you have is that the browser is inherently secure. It's designed to protect the computer from malware, rogue websites, etc etc., and so without developing a custom extension for the browser, there's no way to make the hop to any applications that run in tandem with the browser.
So on that note, I'll suggest that you reconsider writing a Firefox XUL extension as mentioned above. You'll probably need to implement some XPCOM code to make it work, too. Here are some resources that will help get you started:
https://developer.mozilla.org/en/xpcom
https://developer.mozilla.org/En/XUL
http://ted.mielczarek.org/code/mozilla/extensiondev/
https://developer.mozilla.org/en/XUL_Tutorial/Introduction
I don't know which IDE you're using, but in for example Eclipse you can also use the built-in webbrowser to test your webapp and the exceptions/traces in the Eclipse console log already have links to the source code in question. Easy as that. See if your IDE provides something similar.

Categories