Wrapping a file in a PHP script

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
HawkleyFox
-


Joined: 09 Apr 2006
Posts: 5

PostPosted: Sun Apr 09, 2006 4:36 pm    Post subject: Wrapping a file in a PHP script Reply with quote

I have a unique question. Is it possible to wrap a file in a PHP script with Abyss? For example, lets say a user requests "image.jpg". Is it possible to get PHP to intercept the request and run a specified script with the file name, image.jpg, as an argument? This would allow me to do some pre-processing, such as sending out a generic "No Hotlinking" image if the refferer doesn't match my host name.

I found this thing, but Abyss has no support for the HTACCESS standard.
http://www.webpronews.com/webdevelopment/basicdevelopment/wpn-37-20040429HTACCESSWrapperswithPHP.html

I basically want to do what the section called "THE BEST THING SINCE BUBBLE WRAP" is describing, but with Abyss. Perhaps a third party CGI wrapper is out there?
Back to top View user's profile Send private message
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Sun Apr 09, 2006 5:14 pm    Post subject: Reply with quote

Edit: Read Aprelium's reply. :-)

Last edited by TRUSTAbyss on Mon Apr 17, 2006 12:19 am; edited 1 time in total
Back to top View user's profile Send private message Visit poster's website
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Mon Apr 10, 2006 10:32 am    Post subject: Re: Wrapping a file in a PHP script Reply with quote

HawkleyFox,

To achieve the same result with PHP, create a files header.php

header.php will contain a few lines of PHP to check the current file name and to execute wrapper.php if it is an image:

Code:
$path_parts = pathinfo($_SERVER['SCRIPT_NAME']);

if (in_array($path_parts['extension'], array("jpg", "gif", "png")))
{
    include('wrapper.php');
    exit;
}


The full path of header.php should be set as the value of the auto_prepend_file parameter in php.ini.
_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
HawkleyFox
-


Joined: 09 Apr 2006
Posts: 5

PostPosted: Mon Apr 10, 2006 1:45 pm    Post subject: Reply with quote

Excellent! I knew there was some way for Abyss to do the same thing (even though it seems this solution is contained completely within PHP).

Thank you Aprelium.

Edit:
Just a little follow up for those who are looking for similar functionality.
It is important to use strtolower() on $path_parts['extension'] in order to keep things consistant. Also, should you decide to transparently pass the image or file unchanged, you must attach the correct mime type to the header, or else PHP will default to 'text/html'. Observe my code below which uses phpThumb to make it less complex to create thumbnails.

imgwrap.php (automatically pre-pended to all scripts PHP parses)
Code:
<?php
$img_array = array("jpg", "gif", "png", "jpeg", "jpe", "bmp");
$path_parts = pathinfo($_SERVER['SCRIPT_NAME']);
if (in_array(strtolower($path_parts['extension']), $img_array))
{
   //Should we thumb it?
   if(isset($_REQUEST['thumb']))
   {
      //Setup our params, and start phpthumb.
      $_REQUEST['src']="http://127.0.0.1".$_SERVER['SCRIPT_NAME'];
      $_REQUEST['w']="92";
      $_REQUEST['h']="92";
      $_REQUEST['type']="jpg";
      $_REQUEST['user']="-";
      include('c:\\FTPRoot\\webserver\\phpthumb.php');
      exit;
   }
   else
   {
      //Transparently pass the image through.
      $mimes = array("jpg" => "image/jpeg", "jpeg" => "image/jpeg", "jpe" => "image/jpeg", "gif" => "image/gif", "png" => "image/png", "bmp" => "image/bmp");
      header("Content-type: ".$mimes[strtolower($path_parts['extension'])]);
   }
}
?>

Hope this helps someone else add some interesting functionality like this to their servers.
Back to top View user's profile Send private message
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Tue Apr 11, 2006 2:27 pm    Post subject: Reply with quote

HawkleyFox,

You're welcome and thank you for sharing this code snippet with Abyss Web Server's users.
_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
techieJimbo
-


Joined: 15 Apr 2006
Posts: 5

PostPosted: Sat Apr 15, 2006 4:48 pm    Post subject: Thanks Reply with quote

Thanks for the info!

I knew there had to be a better way of mimicing Application.cfm in PHP. For many years now every other forum post on the net has said that htaccess was the only way to do it.

They also claim that with newer versions of PHP, its the only way to do it (something to do with a security update; lets hope thats not true).

I will try it and get back to you with Abyss 2.3 and PHP 5.1.2 (latest as of time of post)!
_________________
- Jim
Back to top View user's profile Send private message
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Sun Apr 16, 2006 2:23 pm    Post subject: Re: Thanks Reply with quote

techieJimbo,

Glad to have pointed you to a solution. Could you please share with us the use of this method to mimic application.cfm in PHP? It may be useful for other users of Abyss Web Server.
_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
techieJimbo
-


Joined: 15 Apr 2006
Posts: 5

PostPosted: Sun Apr 16, 2006 7:45 pm    Post subject: Application.cfm -> Application.php Reply with quote

Well, I got a working 'Application.php' code working using the auto_prepend_file directive.

First off, a few notes to READ CAREFULLY.
1) This code was successfully run on Windows XP Pro using Abyss X1 version 2.3 and PHP version 5.1.2

2) If it doesn't work on your system or causes any problems... its not my fault.

3) Read through the code first, make sure it appears as though it will run on your system.

4) Certain parts of the code may need to be tweaked on other systems. It was created with the Windows directory system in mind, and may not run "AS-IS" on other OSes or even other versions of the software used (listed in #1).

Ok, on to what I did...

Quote:

First, I set up the PHP.ini file...
Pick a filename you will use for the prepended file (I used 'prepend.php').

1) Add "prepend.php" (with quotes) to the auto_prepend_file directive in PHP.ini. NOTE: Also make sure that the directory in which you put 'prepend.php' is in the include_path directive!

2) Include the following code to 'prepend.php' (NOTE: May need to be tweaked for your system!):
Code:

<?php
/*
 * Created on Apr 16, 2006
 *
 * FILE: prepend.php
 * APP NAME: Application.php
 * DESCRIPTION: Loops through current and parent directories untill
 * root diectory is reached. Searches for existing Application.php
 * files. If found, includes that file.
 *
 */

#Variables
$AppExists = false;

# Current Directory
$UpDir = dirname($_SERVER['SCRIPT_FILENAME']);

# Continually move up one directory at a time untill
# root directory is reached.
while ($UpDir !== dirname($_SERVER['DOCUMENT_ROOT'])) {
  if (checkApp($UpDir.'\\')) {
    $AppExists = true;
    include_once($UpDir.'\Application.php');
    break;
  }
  $UpDir = dirname($UpDir);
}

function checkApp($checkDir) {
  $testCheck = $checkDir . "Application.php";
  if (file_exists($testCheck)) {
    return true;
  }
  else {
    return false;
  }
}
?>



And there you have it. If Application.php does not exist, nothing is included. If it does, it includes the file. Spans multiple directories to find closest existing Application.php file just as Coldfusion does.

If you have any tweaks, corrections, additions, or problems please post.

QUICK EDIT: Note that Windows will not discriminate with upper/lower-case lettering. "Application.php" and "application.php" will run. On UNIX/LINUX it will only use "Application.php"
_________________
- Jim
Back to top View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group