Actions

JEXEC

From Joomla! Documentation

Revision as of 03:33, 2 July 2009 by Pasamio (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This page documents the _JEXEC check that is used to protect many PHP files, explaining why it is used, when to use it and situations where it shouldn't be used.

See also: Why_do_most_of_the_Joomla!_PHP_files_start_with_"defined('_JEXEC')...?

What?

The following line is commonly found at the start of Joomla! PHP files:

   defined('_JEXEC') or die('Restricted access');


Why?

"_JEXEC" is a constant that is typically defined in the 'index.php' file at the root of the Joomla! instance and is used to mark a secure entry point into Joomla!. The defined or die check makes sure that _JEXEC has been defined in the pathway to get to the file. This is used to ensure that a file that could expose path information because functions, variables or classes aren't defined in that file trip PHP's error reporting and expose a path.

When?

The check should be added to files that when accessed directly cause a path exposure. For example, the following error occurs when the Backlink System Plugin (/plugins/system/backlink.php file) has had the _JEXEC check disabled:

Fatal error: Call to undefined function jimport() in /Users/pasamio/Sites/workspace/joomla_15/plugins/system/backlink.php on line 18

As is evidenced, the 'jimport' function doesn't exist when the file is directly called so PHP raises an error and exposes the path to the file. Adding the defined or die check to this file will cause a "Restricted access" message to be displayed when the file is accessed.

So the general rule for the JEXEC check is if the PHP file depends on another file to operate properly. Typically if you access a file directly without the JEXEC check and a PHP error is raised (presuming your PHP error reporting is set to show errors by default) about a missing variable, function, object or similar then the file needs to be protected.

Some files don't need to be protected from this check. They might be files with no external dependencies (e.g. a simple class or bit of code) or they might be external files that can operate without being within Joomla!. Examples of this include TinyMCE's GZip'd Javascript generator which is entirely self contained.