tags:

views:

32

answers:

2

I would like to implement a simple plugin system for my script. I'll be including the files of the plugins which the user selects as available.

Currently, if any of the files has some parse error, it causes the complete thing to go down, until I hack into the db to remove that plugin entry.

Is there any easy way to do this? (Somehow check the files for parse errors atleast)?

A: 

No. A parse error is a fatal error that you cannot recover from.

Aistina
There are ways around this, see my answer.
Pekka
+4  A: 

You could make a HTTP request to a PHP file that tries to include the file, and outputs "OK" at the end.

Check_include.php:

$filename = $_GET["plugin"];

$filename = sanitize_filename($filename); // Here you make sure only plugins can be loaded

include ($filename);
echo "OK";

then, in your admin panel or whatever, call

file_get_contents("http://mydomain/check_include.php?plugin=my_new_plugin");

and see whether the result contains the word "OK". If the result is okay, there was no fatal error, and you can activate the plugin; otherwise, you can even output the error message that got output when trying to include it.

WordPress uses a variation of this involving an IFRAME to check new plugins.

Note: Making a HTTP request is expensive. You should under no circumstances do this every time the plugin is included, but only once on installation. Use a flag of some sort (e.g. a underscore _ in front of the plugin name) to enable / disable plugins.

Pekka
good mechanism... +1
Marcx
I don't think `file_get_contents` invokes the PHP parser..?
Aistina
@Aistina that's exactly the point: You make a HTTP request to a PHP script on your local web server, invoking a new instance of the parser. That way, if the include crashes, it will have no effect on the script currently running.
Pekka