Testing files for inclusion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Testing files for inclusion

Post by Roja »

I need a method/function/snippet that will allow me to test a file for inclusion.

I deeply prefer not to use eval (and the php -l method). I've tried include, require, output buffering, file get contents..

Basically, I have a (php) file, that I am going to include, and that file might have a parse error. I need to be able to test for that parse error so that the script can keep going, and be able to report that it had an error.

Any ideas?
pentiumhead
Forum Newbie
Posts: 9
Joined: Wed Aug 17, 2005 11:24 am

Post by pentiumhead »

There is a feature in php in which an include file can return a value like this:

Code: Select all

<?php

$var1 =10;
$var 2= 20;

$var3 = include("adder.php");

?>
And the adder.php file will have:

Code: Select all

<?php
$var3 = $var1+$var2;
return $var3;
?>
Think on those lines i think it might work.

Kay
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I couldn't recall if you're running php5 or not, but.... php_check_syntax()

otherwise, I can only think up, after doing the lint run, hooking the error system.. but you'd still bomb on a Fatal... :?
Sander
Forum Commoner
Posts: 38
Joined: Sat Aug 06, 2005 12:43 pm

Post by Sander »

It seems that php_check_syntax() has been removed in v5.0.5 :(
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

pentiumhead wrote:There is a feature in php in which an include file can return a value like this:

Think on those lines i think it might work.

Kay
It doesn't for files that have a parse error. It simply dies at the first parse error.
feyd wrote:I couldn't recall if you're running php5 or not
Unfortunately, the game will be deployed to a wide variety of hosts, ranging from php4.22 up to php5.1+.
feyd wrote:after doing the lint run, hooking the error system..
Do you mean using php -l, for a lint run? Expand your meaning a little bit here, please.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Roja wrote:
Roja wrote:
feyd wrote:I couldn't recall if you're running php5 or not
Unfortunately, the game will be deployed to a wide variety of hosts, ranging from php4.22 up to php5.1+.
suckage.. hmmm...
Roja wrote:
feyd wrote:after doing the lint run, hooking the error system..
Do you mean using php -l, for a lint run? Expand your meaning a little bit here, please.
Yes, sorry, php -l is what I was referring to as a lint run. Another idea, a page subrequest? It'll run the code, but it will readily show you if there are errors in the script, and should generally be supported on most installs... :)
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

feyd wrote:Yes, sorry, php -l is what I was referring to as a lint run. Another idea, a page subrequest? It'll run the code, but it will readily show you if there are errors in the script, and should generally be supported on most installs... :)
What do you mean by a page subrequest? Can you explain further?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I think it might mean something like:

Code: Select all

$testPage = exec("wget $baseURL/$scriptToTest");
// now preg match for "error/warning/notice"
// if it passes then include it
That that still tries to run the code (albeit without the current script's context....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

absolutely I can further explain. Say you have a test_include.php that can be told some reference to a file on the server to test if it doesn't have any issues upon inclusion. You can have that script hook the error handlers so it can print more "structured" errors as well as making sure error_reporting and whatnot is at the levels you want to work with.

Your main script would generate a subrequest like so: file_get_contents('http://www.<site base url>.whatever/test_include.php?testMe.php') or whatever.. It'd add some time to processing, because php would be making URL requests to the server, but no actual errors could show up on this (the main script's) side.

The test_include.php script could do output buffering and capture the standard output of the script if it's supposed to output anything.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

feyd wrote:absolutely I can further explain. Say you have a test_include.php that can be told some reference to a file on the server to test if it doesn't have any issues upon inclusion. You can have that script hook the error handlers so it can print more "structured" errors as well as making sure error_reporting and whatnot is at the levels you want to work with.

Your main script would generate a subrequest like so: file_get_contents('http://www.<site base url>.whatever/test_include.php?testMe.php') or whatever.. It'd add some time to processing, because php would be making URL requests to the server, but no actual errors could show up on this (the main script's) side.

The test_include.php script could do output buffering and capture the standard output of the script if it's supposed to output anything.
I tried doing an error handler, but I admit I didn't try file_get_contents.. its a file that changes status, so I wouldn't want it executed, but there might be some ways around that.

I'll give fgc a try tonight. Thanks for the idea.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

cool, yeah the files would sort-of need to be in a format that supports running the "critical" bits without (or with little input) .. however, you could use cURL to do the request to send additional headers, say if it needs info about the user current being run under or something.. Granted, that will eat even more time in processing, but will make sure no errors so to a client :)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

How often do these files get uploaded/changed? How complicated are they?

Would it be feasible to just put them into a "holding" cell until an admin can manually review/test in a sandbox?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

nielsene wrote:How often do these files get uploaded/changed?
Could be fairly uncommon, but might be a daily or weekly affair.. it all depends on how active things get in the community. Not sure.
nielsene wrote:How complicated are they?
Currently, they range from 6 lines per file to over 1500 lines in another. We are working to get the average down to around 200-300 lines.
nielsene wrote:Would it be feasible to just put them into a "holding" cell until an admin can manually review/test in a sandbox?
Actually, they already are.. thats why I need the parse test - to make it easy for an admin to see if it works or not.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Edit: Apparently it's pretty b0rken.. :(

Anyway, http://pear.php.net/package/PHP_Parser might interest you
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

timvw wrote:Edit: Apparently it's pretty b0rken.. :(

Anyway, http://pear.php.net/package/PHP_Parser might interest you
Like so many things in PEAR, can't use it..

- License incompatible with GPL
- Not installed consistently on hosting providers
- Too difficult for non-admins to install properly

Whats "pretty b0rken"?
Post Reply