The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2012, 08:24:19 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: 'Intercepting' PHP Serverside  (Read 1242 times)
hydra
Rookie
**
Offline Offline

Posts: 15



View Profile
« on: June 05, 2007, 01:54:45 PM »

Bit of a strange question I know, but say you had a standalone application which manages a site(generating static html). You couldn't edit the application, or the outputted code, only modify the code when served.

In effect:-
1. Page 01 is requested
2. Page 01 is opened by ... and analysed
3. Functions performed on Page 01 by ...
4. Result is outputted in desired format

Couple of examples could be :-

Ex1. Externalising Javascript (on the fly) i.e. any inline JS between <script></script> tags into js/file_method_1.js, js/file_method_2.js and replacing code with an external JS file/method call.

Ex2. Removing any HTML comments
« Last Edit: June 05, 2007, 01:59:09 PM by hydra » Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #1 on: June 05, 2007, 02:05:19 PM »

1) Put the "application" on another port on your apache server.
2) in Apache, using mod_rewrite, if the request is for HTML (or the app, whatever that looks like) rewrite the request into a PHP handler. If it's not (ie it's a graphic request or something) then let it flow through.
3) In your php handler, rethrow the request into Apache on the new port and collect the output of the application into a string variable.
4) Process as necessary, echo/print the string in PHP and it will go out to the original requestor.

Post again if these steps don't make sense... and I'd love to hear if someone has another method that is more efficient...

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
hydra
Rookie
**
Offline Offline

Posts: 15



View Profile
« Reply #2 on: June 05, 2007, 04:08:32 PM »

Cheers Perk - will be testing over next few days and let you know how it goes

Logged

No links in signatures please
hydra
Rookie
**
Offline Offline

Posts: 15



View Profile
« Reply #3 on: June 15, 2007, 04:49:17 PM »

/* p.s please move this to php suggestions - from code repository */

Reply from Perkiset
1) Put the "application" on another port on your apache server.
2) in Apache, using mod_rewrite, if the request is for HTML (or the app, whatever that looks like) rewrite the request into a PHP handler. If it's not (ie it's a graphic request or something) then let it flow through.
3) In your php handler, rethrow the request into Apache on the new port and collect the output of the application into a string variable.
4) Process as necessary, echo/print the string in PHP and it will go out to the original requestor.

Post again if these steps don't make sense... and I'd love to hear if someone has another method that is more efficient...

------------------------------

Would different ports make a difference?
Am currently working on same port, but put 'application' in `app` folder.
Rewrites point to folder instead of different port

------------------------------

p.s. could you possibly post mini samples of step 2/3/4 code. Think I may be on wrong path.
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 9896



View Profile
« Reply #4 on: June 15, 2007, 09:56:52 PM »

2) in Apache, using mod_rewrite, if the request is for HTML (or the app, whatever that looks like) rewrite the request into a PHP handler. If it's not (ie it's a graphic request or something) then let it flow through.

The important part about this, is that you're going to hijack all requests to <the old application> into your PHP handler... which is going to throw the request back into Apache. The only reason you'd need to use a different port is if you can't put another domain on the box, or perhaps you don't want to use 127.0.0.1 ... or something. You could make "internal.mydomain.com" be "127.0.0.1" and then put the handler for the application on THAT virtual host, rather than your main domain. Since all calls are coming into your PHP handler rather than the application, you'll need to call the application yourself to let it do it's thing before you can manipulate it.

Although this is slightly different (and I  REALLY don't want to know if this is in fact what you're up to), the functional metaphor for this structure would be a "Man In The Middle" attack... you're hijacking requests, then proxying them to the actual handler, getting your hands on the answer before sending it back to the user.

Recapping now: calls to the original domain get routed into a PHP handler, as illustrated below. Calls for graphics and such are immediately handled and don't get rewritten. The PHP routine rethrows the request into the server, so when results come back they are now in your hands in the PHP script, where you can have your way with it before you send it back to the surfer.

Code:
RewriteEngine on
# First - if the call is in the /graphics dir, or it is suffixed by .gif or .jpg then
# do no further rewriting...
RewriteCond   %{REQUEST_URI}   /graphics   [OR]
RewriteCond   %{REQUEST_URI    .gif       [OR]
RewriteCond   %{REQUEST_URI}  .jpg
RewriteRule    ^(.*)$    -      [L]

# Rewrite all else into the app...
RewriteRule    ^(.*)$    /www/sites/mainhandler.php?$1

Now in PHP you use any number of ways to throw a request right back up against the server ie., cURL, fopen, file_get_contents, there's a bunch of ways. There's even a webrequest class in the php repository here. You'll need another virtual host:

Code:
<VirtualHost 127.0.0.1:80>
   ServerName   127.0.0.1
   DocumentRoot   /aDir/aDir
</VirtualHost>

This host is the one that is now answering requests for your application.

In PHP, when you get the "answer" back from the application, it'll be all text and then you can have your way with it... finally simply doing a "echo $theModifiedStringBuffer;" so that the final output that you want to go to the user, does.

Is that any clearer? I know for some this is difficult to wrap their brains around.

Good luck,
/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!